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 Java object. The local reference is scoped
98 // to the lifetime of this object.
99 // Instances of this class may hold onto any JNIEnv passed into it until
100 // destroyed. Therefore, since a JNIEnv is only suitable for use on a single
101 // thread, objects of this class must be created, used, and destroyed, on a
103 // Therefore, this class should only be used as a stack-based object and from a
104 // single thread. If you wish to have the reference outlive the current
105 // callstack (e.g. as a class member) or you wish to pass it across threads,
106 // use a ScopedJavaGlobalRef instead.
108 class ScopedJavaLocalRef
: public JavaRef
<T
> {
110 ScopedJavaLocalRef() : env_(NULL
) {}
112 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned
113 // by value as this is the normal usage pattern.
114 ScopedJavaLocalRef(const ScopedJavaLocalRef
<T
>& other
)
116 this->SetNewLocalRef(env_
, other
.obj());
120 explicit ScopedJavaLocalRef(const U
& other
)
125 // Assumes that |obj| is a local reference to a Java object and takes
126 // ownership of this local reference.
127 ScopedJavaLocalRef(JNIEnv
* env
, T obj
) : JavaRef
<T
>(env
, obj
), env_(env
) {}
129 ~ScopedJavaLocalRef() {
133 // Overloaded assignment operator defined for consistency with the implicit
135 void operator=(const ScopedJavaLocalRef
<T
>& other
) {
140 this->ResetLocalRef(env_
);
144 void Reset(const ScopedJavaLocalRef
<U
>& other
) {
145 // We can copy over env_ here as |other| instance must be from the same
146 // thread as |this| local ref. (See class comment for multi-threading
147 // limitations, and alternatives).
148 this->Reset(other
.env_
, other
.obj());
152 void Reset(const U
& other
) {
153 // If |env_| was not yet set (is still NULL) it will be attached to the
154 // current thread in SetNewLocalRef().
155 this->Reset(env_
, other
.obj());
159 void Reset(JNIEnv
* env
, U obj
) {
160 implicit_cast
<T
>(obj
); // Ensure U is assignable to T
161 env_
= this->SetNewLocalRef(env
, obj
);
164 // Releases the local reference to the caller. The caller *must* delete the
165 // local reference when it is done with it. Note that calling a Java method
166 // is *not* a transfer of ownership and Release() should not be used.
168 return static_cast<T
>(this->ReleaseInternal());
172 // This class is only good for use on the thread it was created on so
173 // it's safe to cache the non-threadsafe JNIEnv* inside this object.
177 // Holds a global reference to a Java object. The global reference is scoped
178 // to the lifetime of this object. This class does not hold onto any JNIEnv*
179 // passed to it, hence it is safe to use across threads (within the constraints
180 // imposed by the underlying Java object that it references).
182 class ScopedJavaGlobalRef
: public JavaRef
<T
> {
184 ScopedJavaGlobalRef() {}
186 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef
<T
>& other
) {
190 ScopedJavaGlobalRef(JNIEnv
* env
, T obj
) { this->Reset(env
, obj
); }
193 explicit ScopedJavaGlobalRef(const U
& other
) {
197 ~ScopedJavaGlobalRef() {
202 this->ResetGlobalRef();
206 void Reset(const U
& other
) {
207 this->Reset(NULL
, other
.obj());
211 void Reset(JNIEnv
* env
, U obj
) {
212 implicit_cast
<T
>(obj
); // Ensure U is assignable to T
213 this->SetNewGlobalRef(env
, obj
);
216 // Releases the global reference to the caller. The caller *must* delete the
217 // global reference when it is done with it. Note that calling a Java method
218 // is *not* a transfer of ownership and Release() should not be used.
220 return static_cast<T
>(this->ReleaseInternal());
224 } // namespace android
227 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_