| 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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 220 |
| 221 // Holds a global reference to a Java object. The global reference is scoped | 221 // Holds a global reference to a Java object. The global reference is scoped |
| 222 // to the lifetime of this object. This class does not hold onto any JNIEnv* | 222 // to the lifetime of this object. This class does not hold onto any JNIEnv* |
| 223 // passed to it, hence it is safe to use across threads (within the constraints | 223 // passed to it, hence it is safe to use across threads (within the constraints |
| 224 // imposed by the underlying Java object that it references). | 224 // imposed by the underlying Java object that it references). |
| 225 template<typename T> | 225 template<typename T> |
| 226 class ScopedJavaGlobalRef : public JavaRef<T> { | 226 class ScopedJavaGlobalRef : public JavaRef<T> { |
| 227 public: | 227 public: |
| 228 ScopedJavaGlobalRef() {} | 228 ScopedJavaGlobalRef() {} |
| 229 | 229 |
| 230 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { | 230 ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { |
| 231 this->Reset(other); | 231 this->Reset(other); |
| 232 } | 232 } |
| 233 | 233 |
| 234 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); } | 234 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); } |
| 235 | 235 |
| 236 template<typename U> | 236 template<typename U> |
| 237 explicit ScopedJavaGlobalRef(const U& other) { | 237 explicit ScopedJavaGlobalRef(const U& other) { |
| 238 this->Reset(other); | 238 this->Reset(other); |
| 239 } | 239 } |
| 240 | 240 |
| 241 ~ScopedJavaGlobalRef() { | 241 ~ScopedJavaGlobalRef() { |
| 242 this->Reset(); | 242 this->Reset(); |
| 243 } | 243 } |
| 244 | 244 |
| 245 // Overloaded assignment operator defined for consistency with the implicit |
| 246 // copy constructor. |
| 247 void operator=(const ScopedJavaGlobalRef<T>& other) { |
| 248 this->Reset(other); |
| 249 } |
| 250 |
| 245 void Reset() { | 251 void Reset() { |
| 246 this->ResetGlobalRef(); | 252 this->ResetGlobalRef(); |
| 247 } | 253 } |
| 248 | 254 |
| 249 template<typename U> | 255 template<typename U> |
| 250 void Reset(const U& other) { | 256 void Reset(const U& other) { |
| 251 this->Reset(NULL, other.obj()); | 257 this->Reset(NULL, other.obj()); |
| 252 } | 258 } |
| 253 | 259 |
| 254 template<typename U> | 260 template<typename U> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 268 // is *not* a transfer of ownership and Release() should not be used. | 274 // is *not* a transfer of ownership and Release() should not be used. |
| 269 T Release() { | 275 T Release() { |
| 270 return static_cast<T>(this->ReleaseInternal()); | 276 return static_cast<T>(this->ReleaseInternal()); |
| 271 } | 277 } |
| 272 }; | 278 }; |
| 273 | 279 |
| 274 } // namespace android | 280 } // namespace android |
| 275 } // namespace base | 281 } // namespace base |
| 276 | 282 |
| 277 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 283 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| OLD | NEW |