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