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
3 // found in the LICENSE file.
5 #ifndef BASE_ANDROID_SCOPED_JAVA_REF_H_
6 #define BASE_ANDROID_SCOPED_JAVA_REF_H_
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
17 // 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 // in previous local frames are still valid in the current local frame.
20 class BASE_EXPORT ScopedJavaLocalFrame {
22 explicit ScopedJavaLocalFrame(JNIEnv* env);
23 ScopedJavaLocalFrame(JNIEnv* env, int capacity);
24 ~ScopedJavaLocalFrame();
27 // This class is only good for use on the thread it was created on so
28 // it's safe to cache the non-threadsafe JNIEnv* inside this object.
31 DISALLOW_COPY_AND_ASSIGN(ScopedJavaLocalFrame);
34 // Forward declare the generic java reference template class.
35 template<typename T> class JavaRef;
37 // Template specialization of JavaRef, which acts as the base class for all
38 // other JavaRef<> template types. This allows you to e.g. pass
39 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>&
41 class BASE_EXPORT JavaRef<jobject> {
43 jobject obj() const { return obj_; }
45 bool is_null() const { return obj_ == NULL; }
48 // Initializes a NULL reference.
51 // Takes ownership of the |obj| reference passed; requires it to be a local
53 JavaRef(JNIEnv* env, jobject obj);
57 // The following are implementation detail convenience methods, for
58 // use by the sub-classes.
59 JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj);
60 void SetNewGlobalRef(JNIEnv* env, jobject obj);
61 void ResetLocalRef(JNIEnv* env);
62 void ResetGlobalRef();
63 jobject ReleaseInternal();
68 DISALLOW_COPY_AND_ASSIGN(JavaRef);
71 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful
72 // for allowing functions to accept a reference without having to mandate
73 // whether it is a local or global type.
75 class JavaRef : public JavaRef<jobject> {
77 T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); }
83 JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {}
86 DISALLOW_COPY_AND_ASSIGN(JavaRef);
89 // Holds a local reference to a Java object. The local reference is scoped
90 // to the lifetime of this object.
91 // Instances of this class may hold onto any JNIEnv passed into it until
92 // destroyed. Therefore, since a JNIEnv is only suitable for use on a single
93 // thread, objects of this class must be created, used, and destroyed, on a
95 // Therefore, this class should only be used as a stack-based object and from a
96 // single thread. If you wish to have the reference outlive the current
97 // callstack (e.g. as a class member) or you wish to pass it across threads,
98 // use a ScopedJavaGlobalRef instead.
100 class ScopedJavaLocalRef : public JavaRef<T> {
102 ScopedJavaLocalRef() : env_(NULL) {}
104 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned
105 // by value as this is the normal usage pattern.
106 ScopedJavaLocalRef(const ScopedJavaLocalRef<T>& other)
108 this->SetNewLocalRef(env_, other.obj());
112 explicit ScopedJavaLocalRef(const U& other)
117 // Assumes that |obj| is a local reference to a Java object and takes
118 // ownership of this local reference.
119 ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {}
121 ~ScopedJavaLocalRef() {
125 // Overloaded assignment operator defined for consistency with the implicit
127 void operator=(const ScopedJavaLocalRef<T>& other) {
132 this->ResetLocalRef(env_);
136 void Reset(const ScopedJavaLocalRef<U>& other) {
137 // We can copy over env_ here as |other| instance must be from the same
138 // thread as |this| local ref. (See class comment for multi-threading
139 // limitations, and alternatives).
140 this->Reset(other.env_, other.obj());
144 void Reset(const U& other) {
145 // If |env_| was not yet set (is still NULL) it will be attached to the
146 // current thread in SetNewLocalRef().
147 this->Reset(env_, other.obj());
151 void Reset(JNIEnv* env, U obj) {
152 implicit_cast<T>(obj); // Ensure U is assignable to T
153 env_ = this->SetNewLocalRef(env, obj);
156 // Releases the local reference to the caller. The caller *must* delete the
157 // local reference when it is done with it.
159 return static_cast<T>(this->ReleaseInternal());
163 // 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.
168 // Holds a global reference to a Java object. The global reference is scoped
169 // to the lifetime of this object. This class does not hold onto any JNIEnv*
170 // passed to it, hence it is safe to use across threads (within the constraints
171 // imposed by the underlying Java object that it references).
173 class ScopedJavaGlobalRef : public JavaRef<T> {
175 ScopedJavaGlobalRef() {}
177 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) {
182 explicit ScopedJavaGlobalRef(const U& other) {
186 ~ScopedJavaGlobalRef() {
191 this->ResetGlobalRef();
195 void Reset(const U& other) {
196 this->Reset(NULL, other.obj());
200 void Reset(JNIEnv* env, U obj) {
201 implicit_cast<T>(obj); // Ensure U is assignable to T
202 this->SetNewGlobalRef(env, obj);
205 // Releases the global reference to the caller. The caller *must* delete the
206 // global reference when it is done with it.
208 return static_cast<T>(this->ReleaseInternal());
212 } // namespace android
215 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_