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 |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 // Forward declare the generic java reference template class. | 38 // Forward declare the generic java reference template class. |
39 template<typename T> class JavaRef; | 39 template<typename T> class JavaRef; |
40 | 40 |
41 // Template specialization of JavaRef, which acts as the base class for all | 41 // Template specialization of JavaRef, which acts as the base class for all |
42 // other JavaRef<> template types. This allows you to e.g. pass | 42 // other JavaRef<> template types. This allows you to e.g. pass |
43 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>& | 43 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>& |
44 template<> | 44 template<> |
45 class BASE_EXPORT JavaRef<jobject> { | 45 class BASE_EXPORT JavaRef<jobject> { |
46 public: | 46 public: |
| 47 // Initializes a null reference. Don't add anything else here; it's inlined. |
| 48 JavaRef() : obj_(nullptr) {} |
| 49 |
47 // Allow nullptr to be converted to JavaRef. This avoids having to declare an | 50 // Allow nullptr to be converted to JavaRef. This avoids having to declare an |
48 // empty ScopedJavaLocalRef just to pass null to a function with a JavaRef | 51 // empty JavaRef just to pass null to a function, and makes C++ "nullptr" and |
49 // parameter, and makes C++ "nullptr" and Java "null" equivalent. | 52 // Java "null" equivalent. |
50 JavaRef(std::nullptr_t) : JavaRef() {} | 53 JavaRef(std::nullptr_t) : JavaRef() {} |
51 | 54 |
52 // Public to allow destruction of temporary JavaRef objects created by the | 55 // Public to allow destruction of null JavaRef objects. |
53 // nullptr conversion. Don't add anything else here; it's inlined. | 56 // Don't add anything else here; it's inlined. |
54 ~JavaRef() {} | 57 ~JavaRef() {} |
55 | 58 |
56 jobject obj() const { return obj_; } | 59 jobject obj() const { return obj_; } |
57 | 60 |
58 bool is_null() const { return obj_ == nullptr; } | 61 bool is_null() const { return obj_ == nullptr; } |
59 | 62 |
60 protected: | 63 protected: |
61 // Initializes a null reference. Don't add anything else here; it's inlined. | |
62 JavaRef() : obj_(nullptr) {} | |
63 | |
64 // Takes ownership of the |obj| reference passed; requires it to be a local | 64 // Takes ownership of the |obj| reference passed; requires it to be a local |
65 // reference type. | 65 // reference type. |
66 #if DCHECK_IS_ON() | 66 #if DCHECK_IS_ON() |
67 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON. | 67 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON. |
68 JavaRef(JNIEnv* env, jobject obj); | 68 JavaRef(JNIEnv* env, jobject obj); |
69 #else | 69 #else |
70 // Don't add anything else here; it's inlined. | 70 // Don't add anything else here; it's inlined. |
71 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {} | 71 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {} |
72 #endif | 72 #endif |
73 | 73 |
(...skipping 12 matching lines...) Expand all Loading... |
86 | 86 |
87 DISALLOW_COPY_AND_ASSIGN(JavaRef); | 87 DISALLOW_COPY_AND_ASSIGN(JavaRef); |
88 }; | 88 }; |
89 | 89 |
90 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful | 90 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful |
91 // for allowing functions to accept a reference without having to mandate | 91 // for allowing functions to accept a reference without having to mandate |
92 // whether it is a local or global type. | 92 // whether it is a local or global type. |
93 template<typename T> | 93 template<typename T> |
94 class JavaRef : public JavaRef<jobject> { | 94 class JavaRef : public JavaRef<jobject> { |
95 public: | 95 public: |
| 96 JavaRef() {} |
96 JavaRef(std::nullptr_t) : JavaRef<jobject>(nullptr) {} | 97 JavaRef(std::nullptr_t) : JavaRef<jobject>(nullptr) {} |
97 ~JavaRef() {} | 98 ~JavaRef() {} |
98 | 99 |
99 T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); } | 100 T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); } |
100 | 101 |
101 protected: | 102 protected: |
102 JavaRef() {} | |
103 | |
104 JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {} | 103 JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {} |
105 | 104 |
106 private: | 105 private: |
107 DISALLOW_COPY_AND_ASSIGN(JavaRef); | 106 DISALLOW_COPY_AND_ASSIGN(JavaRef); |
108 }; | 107 }; |
109 | 108 |
110 // Holds a local reference to a JNI method parameter. | 109 // Holds a local reference to a JNI method parameter. |
111 // Method parameters should not be deleted, and so this class exists purely to | 110 // Method parameters should not be deleted, and so this class exists purely to |
112 // wrap them as a JavaRef<T> in the JNI binding generator. Do not create | 111 // wrap them as a JavaRef<T> in the JNI binding generator. Do not create |
113 // instances manually. | 112 // instances manually. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 // by value as this is the normal usage pattern. | 153 // by value as this is the normal usage pattern. |
155 ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other) | 154 ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other) |
156 : env_(other.env_) { | 155 : env_(other.env_) { |
157 this->SetNewLocalRef(env_, other.obj()); | 156 this->SetNewLocalRef(env_, other.obj()); |
158 } | 157 } |
159 | 158 |
160 ScopedJavaLocalRef(ScopedJavaLocalRef<T>&& other) : env_(other.env_) { | 159 ScopedJavaLocalRef(ScopedJavaLocalRef<T>&& other) : env_(other.env_) { |
161 this->swap(other); | 160 this->swap(other); |
162 } | 161 } |
163 | 162 |
164 template <typename U> | 163 explicit ScopedJavaLocalRef(const JavaRef<T>& other) : env_(nullptr) { |
165 explicit ScopedJavaLocalRef(const U& other) : env_(nullptr) { | |
166 this->Reset(other); | 164 this->Reset(other); |
167 } | 165 } |
168 | 166 |
169 // Assumes that |obj| is a local reference to a Java object and takes | 167 // Assumes that |obj| is a local reference to a Java object and takes |
170 // ownership of this local reference. | 168 // ownership of this local reference. |
| 169 // TODO(torne): this shouldn't be used outside of JNI helper functions but |
| 170 // there are currently some cases where there aren't helpers for things. |
171 ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {} | 171 ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {} |
172 | 172 |
173 ~ScopedJavaLocalRef() { | 173 ~ScopedJavaLocalRef() { |
174 this->Reset(); | 174 this->Reset(); |
175 } | 175 } |
176 | 176 |
177 // Overloaded assignment operator defined for consistency with the implicit | 177 // Overloaded assignment operator defined for consistency with the implicit |
178 // copy constructor. | 178 // copy constructor. |
179 void operator=(const ScopedJavaLocalRef<T>& other) { | 179 void operator=(const ScopedJavaLocalRef<T>& other) { |
180 this->Reset(other); | 180 this->Reset(other); |
181 } | 181 } |
182 | 182 |
183 void operator=(ScopedJavaLocalRef<T>&& other) { | 183 void operator=(ScopedJavaLocalRef<T>&& other) { |
184 env_ = other.env_; | 184 env_ = other.env_; |
185 this->swap(other); | 185 this->swap(other); |
186 } | 186 } |
187 | 187 |
188 void Reset() { | 188 void Reset() { |
189 this->ResetLocalRef(env_); | 189 this->ResetLocalRef(env_); |
190 } | 190 } |
191 | 191 |
192 template<typename U> | 192 void Reset(const ScopedJavaLocalRef<T>& other) { |
193 void Reset(const ScopedJavaLocalRef<U>& other) { | |
194 // We can copy over env_ here as |other| instance must be from the same | 193 // We can copy over env_ here as |other| instance must be from the same |
195 // thread as |this| local ref. (See class comment for multi-threading | 194 // thread as |this| local ref. (See class comment for multi-threading |
196 // limitations, and alternatives). | 195 // limitations, and alternatives). |
197 this->Reset(other.env_, other.obj()); | 196 this->Reset(other.env_, other.obj()); |
198 } | 197 } |
199 | 198 |
200 template<typename U> | 199 void Reset(const JavaRef<T>& other) { |
201 void Reset(const U& other) { | |
202 // If |env_| was not yet set (is still null) it will be attached to the | 200 // If |env_| was not yet set (is still null) it will be attached to the |
203 // current thread in SetNewLocalRef(). | 201 // current thread in SetNewLocalRef(). |
204 this->Reset(env_, other.obj()); | 202 this->Reset(env_, other.obj()); |
205 } | 203 } |
206 | 204 |
207 template<typename U> | 205 // Creates a new local reference to the Java object, unlike the constructor |
208 void Reset(JNIEnv* env, U obj) { | 206 // with the same parameters that takes ownership of the existing reference. |
209 static_assert(std::is_convertible<U, T>::value, | 207 // TODO(torne): these should match as this is confusing. |
210 "U must be convertible to T"); | 208 void Reset(JNIEnv* env, T obj) { env_ = this->SetNewLocalRef(env, obj); } |
211 env_ = this->SetNewLocalRef(env, obj); | |
212 } | |
213 | 209 |
214 // Releases the local reference to the caller. The caller *must* delete the | 210 // Releases the local reference to the caller. The caller *must* delete the |
215 // local reference when it is done with it. Note that calling a Java method | 211 // local reference when it is done with it. Note that calling a Java method |
216 // is *not* a transfer of ownership and Release() should not be used. | 212 // is *not* a transfer of ownership and Release() should not be used. |
217 T Release() { | 213 T Release() { |
218 return static_cast<T>(this->ReleaseInternal()); | 214 return static_cast<T>(this->ReleaseInternal()); |
219 } | 215 } |
220 | 216 |
221 private: | 217 private: |
222 // This class is only good for use on the thread it was created on so | 218 // This class is only good for use on the thread it was created on so |
(...skipping 19 matching lines...) Expand all Loading... |
242 ScopedJavaGlobalRef(std::nullptr_t) {} | 238 ScopedJavaGlobalRef(std::nullptr_t) {} |
243 | 239 |
244 ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { | 240 ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { |
245 this->Reset(other); | 241 this->Reset(other); |
246 } | 242 } |
247 | 243 |
248 ScopedJavaGlobalRef(ScopedJavaGlobalRef<T>&& other) { this->swap(other); } | 244 ScopedJavaGlobalRef(ScopedJavaGlobalRef<T>&& other) { this->swap(other); } |
249 | 245 |
250 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); } | 246 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); } |
251 | 247 |
252 template<typename U> | 248 explicit ScopedJavaGlobalRef(const JavaRef<T>& other) { this->Reset(other); } |
253 explicit ScopedJavaGlobalRef(const U& other) { | |
254 this->Reset(other); | |
255 } | |
256 | 249 |
257 ~ScopedJavaGlobalRef() { | 250 ~ScopedJavaGlobalRef() { |
258 this->Reset(); | 251 this->Reset(); |
259 } | 252 } |
260 | 253 |
261 // Overloaded assignment operator defined for consistency with the implicit | 254 // Overloaded assignment operator defined for consistency with the implicit |
262 // copy constructor. | 255 // copy constructor. |
263 void operator=(const ScopedJavaGlobalRef<T>& other) { | 256 void operator=(const ScopedJavaGlobalRef<T>& other) { |
264 this->Reset(other); | 257 this->Reset(other); |
265 } | 258 } |
266 | 259 |
267 void operator=(ScopedJavaGlobalRef<T>&& other) { this->swap(other); } | 260 void operator=(ScopedJavaGlobalRef<T>&& other) { this->swap(other); } |
268 | 261 |
269 void Reset() { | 262 void Reset() { |
270 this->ResetGlobalRef(); | 263 this->ResetGlobalRef(); |
271 } | 264 } |
272 | 265 |
273 template<typename U> | 266 void Reset(const JavaRef<T>& other) { this->Reset(nullptr, other.obj()); } |
274 void Reset(const U& other) { | |
275 this->Reset(nullptr, other.obj()); | |
276 } | |
277 | 267 |
278 template<typename U> | 268 void Reset(JNIEnv* env, const JavaParamRef<T>& other) { |
279 void Reset(JNIEnv* env, const JavaParamRef<U>& other) { | |
280 this->Reset(env, other.obj()); | 269 this->Reset(env, other.obj()); |
281 } | 270 } |
282 | 271 |
283 template<typename U> | 272 void Reset(JNIEnv* env, T obj) { this->SetNewGlobalRef(env, obj); } |
284 void Reset(JNIEnv* env, U obj) { | |
285 static_assert(std::is_convertible<U, T>::value, | |
286 "U must be convertible to T"); | |
287 this->SetNewGlobalRef(env, obj); | |
288 } | |
289 | 273 |
290 // Releases the global reference to the caller. The caller *must* delete the | 274 // Releases the global reference to the caller. The caller *must* delete the |
291 // global reference when it is done with it. Note that calling a Java method | 275 // global reference when it is done with it. Note that calling a Java method |
292 // is *not* a transfer of ownership and Release() should not be used. | 276 // is *not* a transfer of ownership and Release() should not be used. |
293 T Release() { | 277 T Release() { |
294 return static_cast<T>(this->ReleaseInternal()); | 278 return static_cast<T>(this->ReleaseInternal()); |
295 } | 279 } |
296 }; | 280 }; |
297 | 281 |
298 // Temporary type for parameters to Java functions, to allow incremental | 282 // Temporary type for parameters to Java functions, to allow incremental |
299 // migration from bare jobject to JavaRef. Don't use outside JNI generator. | 283 // migration from bare jobject to JavaRef. Don't use outside JNI generator. |
300 template <typename T> | 284 template <typename T> |
301 class JavaRefOrBare { | 285 class JavaRefOrBare { |
302 public: | 286 public: |
303 JavaRefOrBare(std::nullptr_t) : obj_(nullptr) {} | 287 JavaRefOrBare(std::nullptr_t) : obj_(nullptr) {} |
304 JavaRefOrBare(const JavaRef<T>& ref) : obj_(ref.obj()) {} | 288 JavaRefOrBare(const JavaRef<T>& ref) : obj_(ref.obj()) {} |
305 JavaRefOrBare(T obj) : obj_(obj) {} | 289 JavaRefOrBare(T obj) : obj_(obj) {} |
306 T obj() const { return obj_; } | 290 T obj() const { return obj_; } |
307 | 291 |
308 private: | 292 private: |
309 T obj_; | 293 T obj_; |
310 | 294 |
311 DISALLOW_COPY_AND_ASSIGN(JavaRefOrBare); | 295 DISALLOW_COPY_AND_ASSIGN(JavaRefOrBare); |
312 }; | 296 }; |
313 | 297 |
314 } // namespace android | 298 } // namespace android |
315 } // namespace base | 299 } // namespace base |
316 | 300 |
317 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 301 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
OLD | NEW |