• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_FML_PLATFORM_ANDROID_SCOPED_JAVA_REF_H_
6 #define FLUTTER_FML_PLATFORM_ANDROID_SCOPED_JAVA_REF_H_
7 
8 #include <jni.h>
9 #include <stddef.h>
10 
11 #include "flutter/fml/macros.h"
12 
13 namespace fml {
14 namespace jni {
15 
16 // Creates a new local reference frame, in which at least a given number of
17 // local references can be created. Note that local references already created
18 // in previous local frames are still valid in the current local frame.
19 class ScopedJavaLocalFrame {
20  public:
21   explicit ScopedJavaLocalFrame(JNIEnv* env);
22   ScopedJavaLocalFrame(JNIEnv* env, int capacity);
23   ~ScopedJavaLocalFrame();
24 
25  private:
26   // This class is only good for use on the thread it was created on so
27   // it's safe to cache the non-threadsafe JNIEnv* inside this object.
28   JNIEnv* env_;
29 
30   FML_DISALLOW_COPY_AND_ASSIGN(ScopedJavaLocalFrame);
31 };
32 
33 // Forward declare the generic java reference template class.
34 template <typename T>
35 class JavaRef;
36 
37 // Template specialization of JavaRef, which acts as the base class for all
38 // other JavaRef<> template types. This allows you to e.g. pass
39 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>&
40 template <>
41 class JavaRef<jobject> {
42  public:
obj()43   jobject obj() const { return obj_; }
44 
is_null()45   bool is_null() const { return obj_ == NULL; }
46 
47  protected:
48   // Initializes a NULL reference.
49   JavaRef();
50 
51   // Takes ownership of the |obj| reference passed; requires it to be a local
52   // reference type.
53   JavaRef(JNIEnv* env, jobject obj);
54 
55   ~JavaRef();
56 
57   // The following are implementation detail convenience methods, for
58   // use by the sub-classes.
59   JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj);
60   void SetNewGlobalRef(JNIEnv* env, jobject obj);
61   void ResetLocalRef(JNIEnv* env);
62   void ResetGlobalRef();
63   jobject ReleaseInternal();
64 
65  private:
66   jobject obj_;
67 
68   FML_DISALLOW_COPY_AND_ASSIGN(JavaRef);
69 };
70 
71 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful
72 // for allowing functions to accept a reference without having to mandate
73 // whether it is a local or global type.
74 template <typename T>
75 class JavaRef : public JavaRef<jobject> {
76  public:
obj()77   T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); }
78 
79  protected:
JavaRef()80   JavaRef() {}
~JavaRef()81   ~JavaRef() {}
82 
JavaRef(JNIEnv * env,T obj)83   JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {}
84 
85  private:
86   FML_DISALLOW_COPY_AND_ASSIGN(JavaRef);
87 };
88 
89 // Holds a local reference to a Java object. The local reference is scoped
90 // to the lifetime of this object.
91 // Instances of this class may hold onto any JNIEnv passed into it until
92 // destroyed. Therefore, since a JNIEnv is only suitable for use on a single
93 // thread, objects of this class must be created, used, and destroyed, on a
94 // single thread.
95 // Therefore, this class should only be used as a stack-based object and from a
96 // single thread. If you wish to have the reference outlive the current
97 // callstack (e.g. as a class member) or you wish to pass it across threads,
98 // use a ScopedJavaGlobalRef instead.
99 template <typename T>
100 class ScopedJavaLocalRef : public JavaRef<T> {
101  public:
ScopedJavaLocalRef()102   ScopedJavaLocalRef() : env_(NULL) {}
103 
104   // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned
105   // by value as this is the normal usage pattern.
ScopedJavaLocalRef(const ScopedJavaLocalRef<T> & other)106   ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other) : env_(other.env_) {
107     this->SetNewLocalRef(env_, other.obj());
108   }
109 
110   template <typename U>
ScopedJavaLocalRef(const U & other)111   explicit ScopedJavaLocalRef(const U& other) : env_(NULL) {
112     this->Reset(other);
113   }
114 
115   // Assumes that |obj| is a local reference to a Java object and takes
116   // ownership  of this local reference.
ScopedJavaLocalRef(JNIEnv * env,T obj)117   ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {}
118 
~ScopedJavaLocalRef()119   ~ScopedJavaLocalRef() { this->Reset(); }
120 
121   // Overloaded assignment operator defined for consistency with the implicit
122   // copy constructor.
123   void operator=(const ScopedJavaLocalRef<T>& other) { this->Reset(other); }
124 
Reset()125   void Reset() { this->ResetLocalRef(env_); }
126 
127   template <typename U>
Reset(const ScopedJavaLocalRef<U> & other)128   void Reset(const ScopedJavaLocalRef<U>& other) {
129     // We can copy over env_ here as |other| instance must be from the same
130     // thread as |this| local ref. (See class comment for multi-threading
131     // limitations, and alternatives).
132     this->Reset(other.env_, other.obj());
133   }
134 
135   template <typename U>
Reset(const U & other)136   void Reset(const U& other) {
137     // If |env_| was not yet set (is still NULL) it will be attached to the
138     // current thread in SetNewLocalRef().
139     this->Reset(env_, other.obj());
140   }
141 
142   template <typename U>
Reset(JNIEnv * env,U obj)143   void Reset(JNIEnv* env, U obj) {
144     env_ = this->SetNewLocalRef(env, obj);
145   }
146 
147   // Releases the local reference to the caller. The caller *must* delete the
148   // local reference when it is done with it.
Release()149   T Release() { return static_cast<T>(this->ReleaseInternal()); }
150 
151  private:
152   // This class is only good for use on the thread it was created on so
153   // it's safe to cache the non-threadsafe JNIEnv* inside this object.
154   JNIEnv* env_;
155 };
156 
157 // Holds a global reference to a Java object. The global reference is scoped
158 // to the lifetime of this object. This class does not hold onto any JNIEnv*
159 // passed to it, hence it is safe to use across threads (within the constraints
160 // imposed by the underlying Java object that it references).
161 template <typename T>
162 class ScopedJavaGlobalRef : public JavaRef<T> {
163  public:
ScopedJavaGlobalRef()164   ScopedJavaGlobalRef() {}
165 
ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T> & other)166   explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) {
167     this->Reset(other);
168   }
169 
ScopedJavaGlobalRef(JNIEnv * env,T obj)170   ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); }
171 
172   template <typename U>
ScopedJavaGlobalRef(const U & other)173   explicit ScopedJavaGlobalRef(const U& other) {
174     this->Reset(other);
175   }
176 
~ScopedJavaGlobalRef()177   ~ScopedJavaGlobalRef() { this->Reset(); }
178 
Reset()179   void Reset() { this->ResetGlobalRef(); }
180 
181   template <typename U>
Reset(const U & other)182   void Reset(const U& other) {
183     this->Reset(NULL, other.obj());
184   }
185 
186   template <typename U>
Reset(JNIEnv * env,U obj)187   void Reset(JNIEnv* env, U obj) {
188     this->SetNewGlobalRef(env, obj);
189   }
190 
191   // Releases the global reference to the caller. The caller *must* delete the
192   // global reference when it is done with it.
Release()193   T Release() { return static_cast<T>(this->ReleaseInternal()); }
194 };
195 
196 }  // namespace jni
197 }  // namespace fml
198 
199 #endif  // FLUTTER_FML_PLATFORM_ANDROID_SCOPED_JAVA_REF_H_
200