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"
13 #include "base/logging.h"
14 #include "base/template_util.h"
19 // Creates a new local reference frame, in which at least a given number of
20 // local references can be created. Note that local references already created
21 // in previous local frames are still valid in the current local frame.
22 class BASE_EXPORT ScopedJavaLocalFrame
{
24 explicit ScopedJavaLocalFrame(JNIEnv
* env
);
25 ScopedJavaLocalFrame(JNIEnv
* env
, int capacity
);
26 ~ScopedJavaLocalFrame();
29 // This class is only good for use on the thread it was created on so
30 // it's safe to cache the non-threadsafe JNIEnv* inside this object.
33 DISALLOW_COPY_AND_ASSIGN(ScopedJavaLocalFrame
);
36 // Forward declare the generic java reference template class.
37 template<typename T
> class JavaRef
;
39 // Template specialization of JavaRef, which acts as the base class for all
40 // other JavaRef<> template types. This allows you to e.g. pass
41 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>&
43 class BASE_EXPORT JavaRef
<jobject
> {
45 jobject
obj() const { return obj_
; }
47 bool is_null() const { return obj_
== NULL
; }
50 // Initializes a NULL reference. Don't add anything else here; it's inlined.
51 JavaRef() : obj_(NULL
) {}
53 // Takes ownership of the |obj| reference passed; requires it to be a local
56 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON.
57 JavaRef(JNIEnv
* env
, jobject obj
);
59 // Don't add anything else here; it's inlined.
60 JavaRef(JNIEnv
* env
, jobject obj
) : obj_(obj
) {}
63 // Don't add anything else here; it's inlined.
66 // The following are implementation detail convenience methods, for
67 // use by the sub-classes.
68 JNIEnv
* SetNewLocalRef(JNIEnv
* env
, jobject obj
);
69 void SetNewGlobalRef(JNIEnv
* env
, jobject obj
);
70 void ResetLocalRef(JNIEnv
* env
);
71 void ResetGlobalRef();
72 jobject
ReleaseInternal();
77 DISALLOW_COPY_AND_ASSIGN(JavaRef
);
80 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful
81 // for allowing functions to accept a reference without having to mandate
82 // whether it is a local or global type.
84 class JavaRef
: public JavaRef
<jobject
> {
86 T
obj() const { return static_cast<T
>(JavaRef
<jobject
>::obj()); }
92 JavaRef(JNIEnv
* env
, T obj
) : JavaRef
<jobject
>(env
, obj
) {}
95 DISALLOW_COPY_AND_ASSIGN(JavaRef
);
98 // Holds a local reference to a JNI method parameter.
99 // Method parameters should not be deleted, and so this class exists purely to
100 // wrap them as a JavaRef<T> in the JNI binding generator. Do not create
101 // instances manually.
103 class JavaParamRef
: public JavaRef
<T
> {
105 // Assumes that |obj| is a parameter passed to a JNI method from Java.
106 // Does not assume ownership as parameters should not be deleted.
107 JavaParamRef(JNIEnv
* env
, T obj
) : JavaRef
<T
>(env
, obj
) {}
111 // TODO(torne): remove this cast once we're using JavaRef consistently.
112 // http://crbug.com/506850
113 operator T() const { return JavaRef
<T
>::obj(); }
116 DISALLOW_COPY_AND_ASSIGN(JavaParamRef
);
119 // Holds a local reference to a Java object. The local reference is scoped
120 // to the lifetime of this object.
121 // Instances of this class may hold onto any JNIEnv passed into it until
122 // destroyed. Therefore, since a JNIEnv is only suitable for use on a single
123 // thread, objects of this class must be created, used, and destroyed, on a
125 // Therefore, this class should only be used as a stack-based object and from a
126 // single thread. If you wish to have the reference outlive the current
127 // callstack (e.g. as a class member) or you wish to pass it across threads,
128 // use a ScopedJavaGlobalRef instead.
130 class ScopedJavaLocalRef
: public JavaRef
<T
> {
132 ScopedJavaLocalRef() : env_(NULL
) {}
134 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned
135 // by value as this is the normal usage pattern.
136 ScopedJavaLocalRef(const ScopedJavaLocalRef
<T
>& other
)
138 this->SetNewLocalRef(env_
, other
.obj());
142 explicit ScopedJavaLocalRef(const U
& other
)
147 // Assumes that |obj| is a local reference to a Java object and takes
148 // ownership of this local reference.
149 ScopedJavaLocalRef(JNIEnv
* env
, T obj
) : JavaRef
<T
>(env
, obj
), env_(env
) {}
151 ~ScopedJavaLocalRef() {
155 // Overloaded assignment operator defined for consistency with the implicit
157 void operator=(const ScopedJavaLocalRef
<T
>& other
) {
162 this->ResetLocalRef(env_
);
166 void Reset(const ScopedJavaLocalRef
<U
>& other
) {
167 // We can copy over env_ here as |other| instance must be from the same
168 // thread as |this| local ref. (See class comment for multi-threading
169 // limitations, and alternatives).
170 this->Reset(other
.env_
, other
.obj());
174 void Reset(const U
& other
) {
175 // If |env_| was not yet set (is still NULL) it will be attached to the
176 // current thread in SetNewLocalRef().
177 this->Reset(env_
, other
.obj());
181 void Reset(JNIEnv
* env
, U obj
) {
182 static_assert(base::is_convertible
<U
, T
>::value
,
183 "U must be convertible to T");
184 env_
= this->SetNewLocalRef(env
, obj
);
187 // Releases the local reference to the caller. The caller *must* delete the
188 // local reference when it is done with it. Note that calling a Java method
189 // is *not* a transfer of ownership and Release() should not be used.
191 return static_cast<T
>(this->ReleaseInternal());
195 // This class is only good for use on the thread it was created on so
196 // it's safe to cache the non-threadsafe JNIEnv* inside this object.
199 // Prevent ScopedJavaLocalRef(JNIEnv*, T obj) from being used to take
200 // ownership of a JavaParamRef's underlying object - parameters are not
201 // allowed to be deleted and so should not be owned by ScopedJavaLocalRef.
202 // TODO(torne): this can be removed once JavaParamRef no longer has an
203 // implicit conversion back to T.
204 ScopedJavaLocalRef(JNIEnv
* env
, const JavaParamRef
<T
>& other
);
207 // Holds a global reference to a Java object. The global reference is scoped
208 // to the lifetime of this object. This class does not hold onto any JNIEnv*
209 // passed to it, hence it is safe to use across threads (within the constraints
210 // imposed by the underlying Java object that it references).
212 class ScopedJavaGlobalRef
: public JavaRef
<T
> {
214 ScopedJavaGlobalRef() {}
216 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef
<T
>& other
) {
220 ScopedJavaGlobalRef(JNIEnv
* env
, T obj
) { this->Reset(env
, obj
); }
223 explicit ScopedJavaGlobalRef(const U
& other
) {
227 ~ScopedJavaGlobalRef() {
232 this->ResetGlobalRef();
236 void Reset(const U
& other
) {
237 this->Reset(NULL
, other
.obj());
241 void Reset(JNIEnv
* env
, const JavaParamRef
<U
>& other
) {
242 this->Reset(env
, other
.obj());
246 void Reset(JNIEnv
* env
, U obj
) {
247 static_assert(base::is_convertible
<U
, T
>::value
,
248 "U must be convertible to T");
249 this->SetNewGlobalRef(env
, obj
);
252 // Releases the global reference to the caller. The caller *must* delete the
253 // global reference when it is done with it. Note that calling a Java method
254 // is *not* a transfer of ownership and Release() should not be used.
256 return static_cast<T
>(this->ReleaseInternal());
260 } // namespace android
263 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_