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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 this->Reset(env_, other.obj()); | 147 this->Reset(env_, other.obj()); |
148 } | 148 } |
149 | 149 |
150 template<typename U> | 150 template<typename U> |
151 void Reset(JNIEnv* env, U obj) { | 151 void Reset(JNIEnv* env, U obj) { |
152 implicit_cast<T>(obj); // Ensure U is assignable to T | 152 implicit_cast<T>(obj); // Ensure U is assignable to T |
153 env_ = this->SetNewLocalRef(env, obj); | 153 env_ = this->SetNewLocalRef(env, obj); |
154 } | 154 } |
155 | 155 |
156 // Releases the local reference to the caller. The caller *must* delete the | 156 // Releases the local reference to the caller. The caller *must* delete the |
157 // local reference when it is done with it. | 157 // local reference when it is done with it. Note that calling a Java method |
| 158 // is *not* a transfer of ownership and Release() should not be used. |
158 T Release() { | 159 T Release() { |
159 return static_cast<T>(this->ReleaseInternal()); | 160 return static_cast<T>(this->ReleaseInternal()); |
160 } | 161 } |
161 | 162 |
162 private: | 163 private: |
163 // This class is only good for use on the thread it was created on so | 164 // 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. | 165 // it's safe to cache the non-threadsafe JNIEnv* inside this object. |
165 JNIEnv* env_; | 166 JNIEnv* env_; |
166 }; | 167 }; |
167 | 168 |
(...skipping 30 matching lines...) Expand all Loading... |
198 this->Reset(NULL, other.obj()); | 199 this->Reset(NULL, other.obj()); |
199 } | 200 } |
200 | 201 |
201 template<typename U> | 202 template<typename U> |
202 void Reset(JNIEnv* env, U obj) { | 203 void Reset(JNIEnv* env, U obj) { |
203 implicit_cast<T>(obj); // Ensure U is assignable to T | 204 implicit_cast<T>(obj); // Ensure U is assignable to T |
204 this->SetNewGlobalRef(env, obj); | 205 this->SetNewGlobalRef(env, obj); |
205 } | 206 } |
206 | 207 |
207 // Releases the global reference to the caller. The caller *must* delete the | 208 // Releases the global reference to the caller. The caller *must* delete the |
208 // global reference when it is done with it. | 209 // global reference when it is done with it. Note that calling a Java method |
| 210 // is *not* a transfer of ownership and Release() should not be used. |
209 T Release() { | 211 T Release() { |
210 return static_cast<T>(this->ReleaseInternal()); | 212 return static_cast<T>(this->ReleaseInternal()); |
211 } | 213 } |
212 }; | 214 }; |
213 | 215 |
214 } // namespace android | 216 } // namespace android |
215 } // namespace base | 217 } // namespace base |
216 | 218 |
217 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 219 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
OLD | NEW |