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"
18 // Creates a new local reference frame, in which at least a given number of
19 // local references can be created. Note that local references already created
20 // in previous local frames are still valid in the current local frame.
21 class BASE_EXPORT ScopedJavaLocalFrame
{
23 explicit ScopedJavaLocalFrame(JNIEnv
* env
);
24 ScopedJavaLocalFrame(JNIEnv
* env
, int capacity
);
25 ~ScopedJavaLocalFrame();
28 // This class is only good for use on the thread it was created on so
29 // it's safe to cache the non-threadsafe JNIEnv* inside this object.
32 DISALLOW_COPY_AND_ASSIGN(ScopedJavaLocalFrame
);
35 // Forward declare the generic java reference template class.
36 template<typename T
> class JavaRef
;
38 // Template specialization of JavaRef, which acts as the base class for all
39 // other JavaRef<> template types. This allows you to e.g. pass
40 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>&
42 class BASE_EXPORT JavaRef
<jobject
> {
44 jobject
obj() const { return obj_
; }
46 bool is_null() const { return obj_
== NULL
; }
49 // Initializes a NULL reference. Don't add anything else here; it's inlined.
50 JavaRef() : obj_(NULL
) {}
52 // Takes ownership of the |obj| reference passed; requires it to be a local
55 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON.
56 JavaRef(JNIEnv
* env
, jobject obj
);
58 // Don't add anything else here; it's inlined.
59 JavaRef(JNIEnv
* env
, jobject obj
) : obj_(obj
) {}
62 // Don't add anything else here; it's inlined.
65 // The following are implementation detail convenience methods, for
66 // use by the sub-classes.
67 JNIEnv
* SetNewLocalRef(JNIEnv
* env
, jobject obj
);
68 void SetNewGlobalRef(JNIEnv
* env
, jobject obj
);
69 void ResetLocalRef(JNIEnv
* env
);
70 void ResetGlobalRef();
71 jobject
ReleaseInternal();
76 DISALLOW_COPY_AND_ASSIGN(JavaRef
);
79 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful
80 // for allowing functions to accept a reference without having to mandate
81 // whether it is a local or global type.
83 class JavaRef
: public JavaRef
<jobject
> {
85 T
obj() const { return static_cast<T
>(JavaRef
<jobject
>::obj()); }
91 JavaRef(JNIEnv
* env
, T obj
) : JavaRef
<jobject
>(env
, obj
) {}
94 DISALLOW_COPY_AND_ASSIGN(JavaRef
);
97 // Holds a local reference to a JNI method parameter.
98 // Method parameters should not be deleted, and so this class exists purely to
99 // wrap them as a JavaRef<T> in the JNI binding generator. Do not create
100 // instances manually.
102 class JavaParamRef
: public JavaRef
<T
> {
104 // Assumes that |obj| is a parameter passed to a JNI method from Java.
105 // Does not assume ownership as parameters should not be deleted.
106 JavaParamRef(JNIEnv
* env
, T obj
) : JavaRef
<T
>(env
, obj
) {}
110 // TODO(torne): remove this cast once we're using JavaRef consistently.
111 // http://crbug.com/506850
112 operator T() const { return JavaRef
<T
>::obj(); }
115 DISALLOW_COPY_AND_ASSIGN(JavaParamRef
);
118 // Holds a local reference to a Java object. The local reference is scoped
119 // to the lifetime of this object.
120 // Instances of this class may hold onto any JNIEnv passed into it until
121 // destroyed. Therefore, since a JNIEnv is only suitable for use on a single
122 // thread, objects of this class must be created, used, and destroyed, on a
124 // Therefore, this class should only be used as a stack-based object and from a
125 // single thread. If you wish to have the reference outlive the current
126 // callstack (e.g. as a class member) or you wish to pass it across threads,
127 // use a ScopedJavaGlobalRef instead.
129 class ScopedJavaLocalRef
: public JavaRef
<T
> {
131 ScopedJavaLocalRef() : env_(NULL
) {}
133 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned
134 // by value as this is the normal usage pattern.
135 ScopedJavaLocalRef(const ScopedJavaLocalRef
<T
>& other
)
137 this->SetNewLocalRef(env_
, other
.obj());
141 explicit ScopedJavaLocalRef(const U
& other
)
146 // Assumes that |obj| is a local reference to a Java object and takes
147 // ownership of this local reference.
148 ScopedJavaLocalRef(JNIEnv
* env
, T obj
) : JavaRef
<T
>(env
, obj
), env_(env
) {}
150 ~ScopedJavaLocalRef() {
154 // Overloaded assignment operator defined for consistency with the implicit
156 void operator=(const ScopedJavaLocalRef
<T
>& other
) {
161 this->ResetLocalRef(env_
);
165 void Reset(const ScopedJavaLocalRef
<U
>& other
) {
166 // We can copy over env_ here as |other| instance must be from the same
167 // thread as |this| local ref. (See class comment for multi-threading
168 // limitations, and alternatives).
169 this->Reset(other
.env_
, other
.obj());
173 void Reset(const U
& other
) {
174 // If |env_| was not yet set (is still NULL) it will be attached to the
175 // current thread in SetNewLocalRef().
176 this->Reset(env_
, other
.obj());
180 void Reset(JNIEnv
* env
, U obj
) {
181 implicit_cast
<T
>(obj
); // Ensure U is assignable to T
182 env_
= this->SetNewLocalRef(env
, obj
);
185 // Releases the local reference to the caller. The caller *must* delete the
186 // local reference when it is done with it. Note that calling a Java method
187 // is *not* a transfer of ownership and Release() should not be used.
189 return static_cast<T
>(this->ReleaseInternal());
193 // This class is only good for use on the thread it was created on so
194 // it's safe to cache the non-threadsafe JNIEnv* inside this object.
197 // Prevent ScopedJavaLocalRef(JNIEnv*, T obj) from being used to take
198 // ownership of a JavaParamRef's underlying object - parameters are not
199 // allowed to be deleted and so should not be owned by ScopedJavaLocalRef.
200 // TODO(torne): this can be removed once JavaParamRef no longer has an
201 // implicit conversion back to T.
202 ScopedJavaLocalRef(JNIEnv
* env
, const JavaParamRef
<T
>& other
);
205 // Holds a global reference to a Java object. The global reference is scoped
206 // to the lifetime of this object. This class does not hold onto any JNIEnv*
207 // passed to it, hence it is safe to use across threads (within the constraints
208 // imposed by the underlying Java object that it references).
210 class ScopedJavaGlobalRef
: public JavaRef
<T
> {
212 ScopedJavaGlobalRef() {}
214 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef
<T
>& other
) {
218 ScopedJavaGlobalRef(JNIEnv
* env
, T obj
) { this->Reset(env
, obj
); }
221 explicit ScopedJavaGlobalRef(const U
& other
) {
225 ~ScopedJavaGlobalRef() {
230 this->ResetGlobalRef();
234 void Reset(const U
& other
) {
235 this->Reset(NULL
, other
.obj());
239 void Reset(JNIEnv
* env
, const JavaParamRef
<U
>& other
) {
240 this->Reset(env
, other
.obj());
244 void Reset(JNIEnv
* env
, U obj
) {
245 implicit_cast
<T
>(obj
); // Ensure U is assignable to T
246 this->SetNewGlobalRef(env
, obj
);
249 // Releases the global reference to the caller. The caller *must* delete the
250 // global reference when it is done with it. Note that calling a Java method
251 // is *not* a transfer of ownership and Release() should not be used.
253 return static_cast<T
>(this->ReleaseInternal());
257 } // namespace android
260 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_