Cleanup setting of 'sysroot' in common.gypi
[chromium-blink-merge.git] / base / android / scoped_java_ref.h
blobcad63b75617a106c39b26fc02eed16c8293f804d
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"
14 #include "base/template_util.h"
16 namespace base {
17 namespace android {
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 {
23 public:
24 explicit ScopedJavaLocalFrame(JNIEnv* env);
25 ScopedJavaLocalFrame(JNIEnv* env, int capacity);
26 ~ScopedJavaLocalFrame();
28 private:
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.
31 JNIEnv* env_;
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>&
42 template<>
43 class BASE_EXPORT JavaRef<jobject> {
44 public:
45 jobject obj() const { return obj_; }
47 bool is_null() const { return obj_ == NULL; }
49 protected:
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
54 // reference type.
55 #if DCHECK_IS_ON()
56 // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON.
57 JavaRef(JNIEnv* env, jobject obj);
58 #else
59 // Don't add anything else here; it's inlined.
60 JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {}
61 #endif
63 // Don't add anything else here; it's inlined.
64 ~JavaRef() {}
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();
74 private:
75 jobject obj_;
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.
83 template<typename T>
84 class JavaRef : public JavaRef<jobject> {
85 public:
86 T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); }
88 protected:
89 JavaRef() {}
90 ~JavaRef() {}
92 JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {}
94 private:
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.
102 template<typename T>
103 class JavaParamRef : public JavaRef<T> {
104 public:
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) {}
109 ~JavaParamRef() {}
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(); }
115 private:
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
124 // single thread.
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.
129 template<typename T>
130 class ScopedJavaLocalRef : public JavaRef<T> {
131 public:
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)
137 : env_(other.env_) {
138 this->SetNewLocalRef(env_, other.obj());
141 template<typename U>
142 explicit ScopedJavaLocalRef(const U& other)
143 : env_(NULL) {
144 this->Reset(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() {
152 this->Reset();
155 // Overloaded assignment operator defined for consistency with the implicit
156 // copy constructor.
157 void operator=(const ScopedJavaLocalRef<T>& other) {
158 this->Reset(other);
161 void Reset() {
162 this->ResetLocalRef(env_);
165 template<typename U>
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());
173 template<typename U>
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());
180 template<typename U>
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.
190 T Release() {
191 return static_cast<T>(this->ReleaseInternal());
194 private:
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.
197 JNIEnv* env_;
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).
211 template<typename T>
212 class ScopedJavaGlobalRef : public JavaRef<T> {
213 public:
214 ScopedJavaGlobalRef() {}
216 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) {
217 this->Reset(other);
220 ScopedJavaGlobalRef(JNIEnv* env, T obj) { this->Reset(env, obj); }
222 template<typename U>
223 explicit ScopedJavaGlobalRef(const U& other) {
224 this->Reset(other);
227 ~ScopedJavaGlobalRef() {
228 this->Reset();
231 void Reset() {
232 this->ResetGlobalRef();
235 template<typename U>
236 void Reset(const U& other) {
237 this->Reset(NULL, other.obj());
240 template<typename U>
241 void Reset(JNIEnv* env, const JavaParamRef<U>& other) {
242 this->Reset(env, other.obj());
245 template<typename U>
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.
255 T Release() {
256 return static_cast<T>(this->ReleaseInternal());
260 } // namespace android
261 } // namespace base
263 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_