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/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/logging.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 namespace android { | 16 namespace android { |
16 | 17 |
17 // Creates a new local reference frame, in which at least a given number of | 18 // Creates a new local reference frame, in which at least a given number of |
18 // local references can be created. Note that local references already created | 19 // local references can be created. Note that local references already created |
19 // in previous local frames are still valid in the current local frame. | 20 // in previous local frames are still valid in the current local frame. |
20 class BASE_EXPORT ScopedJavaLocalFrame { | 21 class BASE_EXPORT ScopedJavaLocalFrame { |
21 public: | 22 public: |
22 explicit ScopedJavaLocalFrame(JNIEnv* env); | 23 explicit ScopedJavaLocalFrame(JNIEnv* env); |
(...skipping 15 matching lines...) Expand all Loading... |
38 // other JavaRef<> template types. This allows you to e.g. pass | 39 // other JavaRef<> template types. This allows you to e.g. pass |
39 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>& | 40 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>& |
40 template<> | 41 template<> |
41 class BASE_EXPORT JavaRef<jobject> { | 42 class BASE_EXPORT JavaRef<jobject> { |
42 public: | 43 public: |
43 jobject obj() const { return obj_; } | 44 jobject obj() const { return obj_; } |
44 | 45 |
45 bool is_null() const { return obj_ == NULL; } | 46 bool is_null() const { return obj_ == NULL; } |
46 | 47 |
47 protected: | 48 protected: |
48 // Initializes a NULL reference. | 49 // Initializes a NULL reference. Don't add anything else here; it's inlined. |
49 JavaRef(); | 50 JavaRef() : obj_(NULL) {} |
50 | 51 |
51 // Takes ownership of the |obj| reference passed; requires it to be a local | 52 // Takes ownership of the |obj| reference passed; requires it to be a local |
52 // reference type. | 53 // reference type. |
| 54 #if DCHECK_IS_ON() |
| 55 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON. |
53 JavaRef(JNIEnv* env, jobject obj); | 56 JavaRef(JNIEnv* env, jobject obj); |
| 57 #else |
| 58 // Don't add anything else here; it's inlined. |
| 59 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {} |
| 60 #endif |
54 | 61 |
55 ~JavaRef(); | 62 // Don't add anything else here; it's inlined. |
| 63 ~JavaRef() {} |
56 | 64 |
57 // The following are implementation detail convenience methods, for | 65 // The following are implementation detail convenience methods, for |
58 // use by the sub-classes. | 66 // use by the sub-classes. |
59 JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj); | 67 JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj); |
60 void SetNewGlobalRef(JNIEnv* env, jobject obj); | 68 void SetNewGlobalRef(JNIEnv* env, jobject obj); |
61 void ResetLocalRef(JNIEnv* env); | 69 void ResetLocalRef(JNIEnv* env); |
62 void ResetGlobalRef(); | 70 void ResetGlobalRef(); |
63 jobject ReleaseInternal(); | 71 jobject ReleaseInternal(); |
64 | 72 |
65 private: | 73 private: |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 this->Reset(env_, other.obj()); | 155 this->Reset(env_, other.obj()); |
148 } | 156 } |
149 | 157 |
150 template<typename U> | 158 template<typename U> |
151 void Reset(JNIEnv* env, U obj) { | 159 void Reset(JNIEnv* env, U obj) { |
152 implicit_cast<T>(obj); // Ensure U is assignable to T | 160 implicit_cast<T>(obj); // Ensure U is assignable to T |
153 env_ = this->SetNewLocalRef(env, obj); | 161 env_ = this->SetNewLocalRef(env, obj); |
154 } | 162 } |
155 | 163 |
156 // Releases the local reference to the caller. The caller *must* delete the | 164 // Releases the local reference to the caller. The caller *must* delete the |
157 // local reference when it is done with it. | 165 // local reference when it is done with it. Note that calling a Java method |
| 166 // is *not* a transfer of ownership and Release() should not be used. |
158 T Release() { | 167 T Release() { |
159 return static_cast<T>(this->ReleaseInternal()); | 168 return static_cast<T>(this->ReleaseInternal()); |
160 } | 169 } |
161 | 170 |
162 private: | 171 private: |
163 // This class is only good for use on the thread it was created on so | 172 // This class is only good for use on the thread it was created on so |
164 // it's safe to cache the non-threadsafe JNIEnv* inside this object. | 173 // it's safe to cache the non-threadsafe JNIEnv* inside this object. |
165 JNIEnv* env_; | 174 JNIEnv* env_; |
166 }; | 175 }; |
167 | 176 |
(...skipping 30 matching lines...) Expand all Loading... |
198 this->Reset(NULL, other.obj()); | 207 this->Reset(NULL, other.obj()); |
199 } | 208 } |
200 | 209 |
201 template<typename U> | 210 template<typename U> |
202 void Reset(JNIEnv* env, U obj) { | 211 void Reset(JNIEnv* env, U obj) { |
203 implicit_cast<T>(obj); // Ensure U is assignable to T | 212 implicit_cast<T>(obj); // Ensure U is assignable to T |
204 this->SetNewGlobalRef(env, obj); | 213 this->SetNewGlobalRef(env, obj); |
205 } | 214 } |
206 | 215 |
207 // Releases the global reference to the caller. The caller *must* delete the | 216 // Releases the global reference to the caller. The caller *must* delete the |
208 // global reference when it is done with it. | 217 // global reference when it is done with it. Note that calling a Java method |
| 218 // is *not* a transfer of ownership and Release() should not be used. |
209 T Release() { | 219 T Release() { |
210 return static_cast<T>(this->ReleaseInternal()); | 220 return static_cast<T>(this->ReleaseInternal()); |
211 } | 221 } |
212 }; | 222 }; |
213 | 223 |
214 } // namespace android | 224 } // namespace android |
215 } // namespace base | 225 } // namespace base |
216 | 226 |
217 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 227 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
OLD | NEW |