Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / android / scoped_java_ref.h
blob37e7fd3507ca480ee29209df24d3e7e2cf2b7472
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_
8 #include <jni.h>
9 #include <stddef.h>
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
13 #include "base/logging.h"
15 namespace base {
16 namespace android {
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 {
22 public:
23 explicit ScopedJavaLocalFrame(JNIEnv* env);
24 ScopedJavaLocalFrame(JNIEnv* env, int capacity);
25 ~ScopedJavaLocalFrame();
27 private:
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.
30 JNIEnv* env_;
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>&
41 template<>
42 class BASE_EXPORT JavaRef<jobject> {
43 public:
44 jobject obj() const { return obj_; }
46 bool is_null() const { return obj_ == NULL; }
48 protected:
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
53 // reference type.
54 #if DCHECK_IS_ON()
55 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON.
56 JavaRef(JNIEnv* env, jobject obj);
57 #else
58 // Don't add anything else here; it's inlined.
59 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {}
60 #endif
62 // Don't add anything else here; it's inlined.
63 ~JavaRef() {}
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();
73 private:
74 jobject obj_;
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.
82 template<typename T>
83 class JavaRef : public JavaRef<jobject> {
84 public:
85 T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); }
87 protected:
88 JavaRef() {}
89 ~JavaRef() {}
91 JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {}
93 private:
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
102 // single thread.
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.
107 template<typename T>
108 class ScopedJavaLocalRef : public JavaRef<T> {
109 public:
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)
115 : env_(other.env_) {
116 this->SetNewLocalRef(env_, other.obj());
119 template<typename U>
120 explicit ScopedJavaLocalRef(const U& other)
121 : env_(NULL) {
122 this->Reset(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() {
130 this->Reset();
133 // Overloaded assignment operator defined for consistency with the implicit
134 // copy constructor.
135 void operator=(const ScopedJavaLocalRef<T>& other) {
136 this->Reset(other);
139 void Reset() {
140 this->ResetLocalRef(env_);
143 template<typename U>
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());
151 template<typename U>
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());
158 template<typename U>
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.
167 T Release() {
168 return static_cast<T>(this->ReleaseInternal());
171 private:
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.
174 JNIEnv* env_;
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).
181 template<typename T>
182 class ScopedJavaGlobalRef : public JavaRef<T> {
183 public:
184 ScopedJavaGlobalRef() {}
186 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) {
187 this->Reset(other);
190 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); }
192 template<typename U>
193 explicit ScopedJavaGlobalRef(const U& other) {
194 this->Reset(other);
197 ~ScopedJavaGlobalRef() {
198 this->Reset();
201 void Reset() {
202 this->ResetGlobalRef();
205 template<typename U>
206 void Reset(const U& other) {
207 this->Reset(NULL, other.obj());
210 template<typename U>
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.
219 T Release() {
220 return static_cast<T>(this->ReleaseInternal());
224 } // namespace android
225 } // namespace base
227 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_