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 // Forward declare the generic java reference template class.
18 template<typename T
> class JavaRef
;
20 // Template specialization of JavaRef, which acts as the base class for all
21 // other JavaRef<> template types. This allows you to e.g. pass
22 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>&
24 class BASE_EXPORT JavaRef
<jobject
> {
26 jobject
obj() const { return obj_
; }
28 bool is_null() const { return obj_
== NULL
; }
31 // Initializes a NULL reference.
34 // Takes ownership of the |obj| reference passed; requires it to be a local
36 JavaRef(JNIEnv
* env
, jobject obj
);
40 // The following are implementation detail convenience methods, for
41 // use by the sub-classes.
42 JNIEnv
* SetNewLocalRef(JNIEnv
* env
, jobject obj
);
43 void SetNewGlobalRef(JNIEnv
* env
, jobject obj
);
44 void ResetLocalRef(JNIEnv
* env
);
45 void ResetGlobalRef();
46 jobject
ReleaseInternal();
51 DISALLOW_COPY_AND_ASSIGN(JavaRef
);
54 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful
55 // for allowing functions to accept a reference without having to mandate
56 // whether it is a local or global type.
58 class JavaRef
: public JavaRef
<jobject
> {
60 T
obj() const { return static_cast<T
>(JavaRef
<jobject
>::obj()); }
66 JavaRef(JNIEnv
* env
, T obj
) : JavaRef
<jobject
>(env
, obj
) {}
69 DISALLOW_COPY_AND_ASSIGN(JavaRef
);
72 // Holds a local reference to a Java object. The local reference is scoped
73 // to the lifetime of this object.
74 // Instances of this class may hold onto any JNIEnv passed into it until
75 // destroyed. Therefore, since a JNIEnv is only suitable for use on a single
76 // thread, objects of this class must be created, used, and destroyed, on a
78 // Therefore, this class should only be used as a stack-based object and from a
79 // single thread. If you wish to have the reference outlive the current
80 // callstack (e.g. as a class member) or you wish to pass it across threads,
81 // use a ScopedJavaGlobalRef instead.
83 class ScopedJavaLocalRef
: public JavaRef
<T
> {
85 ScopedJavaLocalRef() : env_(NULL
) {}
87 // Non-explicit copy constructor, to allow ScopedJavaLocalRef to be returned
88 // by value as this is the normal usage pattern.
89 ScopedJavaLocalRef(const ScopedJavaLocalRef
<T
>& other
)
91 this->SetNewLocalRef(env_
, other
.obj());
95 explicit ScopedJavaLocalRef(const U
& other
)
100 // Assumes that |obj| is a local reference to a Java object and takes
101 // ownership of this local reference.
102 ScopedJavaLocalRef(JNIEnv
* env
, T obj
) : JavaRef
<T
>(env
, obj
), env_(env
) {}
104 ~ScopedJavaLocalRef() {
108 // Overloaded assignment operator defined for consistency with the implicit
110 void operator=(const ScopedJavaLocalRef
<T
>& other
) {
115 this->ResetLocalRef(env_
);
119 void Reset(const ScopedJavaLocalRef
<U
>& other
) {
120 // We can copy over env_ here as |other| instance must be from the same
121 // thread as |this| local ref. (See class comment for multi-threading
122 // limitations, and alternatives).
123 this->Reset(other
.env_
, other
.obj());
127 void Reset(const U
& other
) {
128 // If |env_| was not yet set (is still NULL) it will be attached to the
129 // current thread in SetNewLocalRef().
130 this->Reset(env_
, other
.obj());
134 void Reset(JNIEnv
* env
, U obj
) {
135 implicit_cast
<T
>(obj
); // Ensure U is assignable to T
136 env_
= this->SetNewLocalRef(env
, obj
);
139 // Releases the local reference to the caller. The caller *must* delete the
140 // local reference when it is done with it.
142 return static_cast<T
>(this->ReleaseInternal());
146 // This class is only good for use on the thread it was created on so
147 // it's safe to cache the non-threadsafe JNIEnv* inside this object.
151 // Holds a global reference to a Java object. The global reference is scoped
152 // to the lifetime of this object. This class does not hold onto any JNIEnv*
153 // passed to it, hence it is safe to use across threads (within the constraints
154 // imposed by the underlying Java object that it references).
156 class ScopedJavaGlobalRef
: public JavaRef
<T
> {
158 ScopedJavaGlobalRef() {}
160 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef
<T
>& other
) {
165 explicit ScopedJavaGlobalRef(const U
& other
) {
169 ~ScopedJavaGlobalRef() {
174 this->ResetGlobalRef();
178 void Reset(const U
& other
) {
179 this->Reset(NULL
, other
.obj());
183 void Reset(JNIEnv
* env
, U obj
) {
184 implicit_cast
<T
>(obj
); // Ensure U is assignable to T
185 this->SetNewGlobalRef(env
, obj
);
188 // Releases the global reference to the caller. The caller *must* delete the
189 // global reference when it is done with it.
191 return static_cast<T
>(this->ReleaseInternal());
195 } // namespace android
198 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_