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