Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: base/android/scoped_java_ref.h

Issue 9358028: Upstream Android JNI code, allowing us to use more ScopedJava references. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased and added a TODO Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/android/jni_string.cc ('k') | base/android/scoped_java_ref.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_ANDROID_SCOPED_JAVA_REF_H_ 5 #ifndef BASE_ANDROID_SCOPED_JAVA_REF_H_
6 #define BASE_ANDROID_SCOPED_JAVA_REF_H_ 6 #define BASE_ANDROID_SCOPED_JAVA_REF_H_
7 7
8 #include <jni.h> 8 #include <jni.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 10
11 #include "base/logging.h" 11 #include "base/basictypes.h"
12 12
13 namespace base { 13 namespace base {
14 namespace android { 14 namespace android {
15 15
16 // Forward declare the generic java reference template class. 16 // Forward declare the generic java reference template class.
17 template<typename T> class JavaRef; 17 template<typename T> class JavaRef;
18 18
19 // Template specialization of JavaRef, which acts as the base class for all 19 // Template specialization of JavaRef, which acts as the base class for all
20 // other JavaRef<> template types. This allows you to e.g. pass 20 // other JavaRef<> template types. This allows you to e.g. pass
21 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>& 21 // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>&
22 template<> 22 template<>
23 class JavaRef<jobject> { 23 class JavaRef<jobject> {
24 public: 24 public:
25 JNIEnv* env() const { return env_; } 25 JNIEnv* env() const { return env_; }
26 jobject obj() const { return obj_; } 26 jobject obj() const { return obj_; }
27 27
28 bool is_null() const { return obj_ == NULL; }
29
28 protected: 30 protected:
29 // Initializes a NULL reference. 31 // Initializes a NULL reference.
30 JavaRef(); 32 JavaRef();
31 33
32 // Takes ownership of the |obj| reference passed; requires it to be a local 34 // Takes ownership of the |obj| reference passed; requires it to be a local
33 // reference type. 35 // reference type.
34 JavaRef(JNIEnv* env, jobject obj); 36 JavaRef(JNIEnv* env, jobject obj);
35 37
36 ~JavaRef(); 38 ~JavaRef();
37 39
38 // The following are implementation detail convenience methods, for 40 // The following are implementation detail convenience methods, for
39 // use by the sub-classes. 41 // use by the sub-classes.
40 void SetNewLocalRef(JNIEnv* env, jobject obj); 42 void SetNewLocalRef(JNIEnv* env, jobject obj);
41 void SetNewGlobalRef(JNIEnv* env, jobject obj); 43 void SetNewGlobalRef(JNIEnv* env, jobject obj);
44 void ResetLocalRef();
45 void ResetGlobalRef();
42 jobject ReleaseInternal(); 46 jobject ReleaseInternal();
43 47
44 private: 48 private:
45 JNIEnv* env_; 49 JNIEnv* env_;
46 jobject obj_; 50 jobject obj_;
47 51
48 DISALLOW_COPY_AND_ASSIGN(JavaRef); 52 DISALLOW_COPY_AND_ASSIGN(JavaRef);
49 }; 53 };
50 54
51 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful 55 // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 this->Reset(); 101 this->Reset();
98 } 102 }
99 103
100 // Overloaded assignment operator defined for consistency with the implicit 104 // Overloaded assignment operator defined for consistency with the implicit
101 // copy constructor. 105 // copy constructor.
102 void operator=(const ScopedJavaLocalRef<T>& other) { 106 void operator=(const ScopedJavaLocalRef<T>& other) {
103 this->Reset(other); 107 this->Reset(other);
104 } 108 }
105 109
106 void Reset() { 110 void Reset() {
107 this->SetNewLocalRef(NULL, NULL); 111 this->ResetLocalRef();
108 } 112 }
109 113
110 template<typename U> 114 template<typename U>
111 void Reset(const U& other) { 115 void Reset(const U& other) {
112 this->Reset(other.env(), other.obj()); 116 this->Reset(other.env(), other.obj());
113 } 117 }
114 118
115 template<typename U> 119 template<typename U>
116 void Reset(JNIEnv* env, U obj) { 120 void Reset(JNIEnv* env, U obj) {
117 implicit_cast<T>(obj); // Ensure U is assignable to T 121 implicit_cast<T>(obj); // Ensure U is assignable to T
(...skipping 23 matching lines...) Expand all
141 template<typename U> 145 template<typename U>
142 explicit ScopedJavaGlobalRef(const U& other) { 146 explicit ScopedJavaGlobalRef(const U& other) {
143 this->Reset(other); 147 this->Reset(other);
144 } 148 }
145 149
146 ~ScopedJavaGlobalRef() { 150 ~ScopedJavaGlobalRef() {
147 this->Reset(); 151 this->Reset();
148 } 152 }
149 153
150 void Reset() { 154 void Reset() {
151 this->SetNewGlobalRef(NULL, NULL); 155 this->ResetGlobalRef();
152 } 156 }
153 157
154 template<typename U> 158 template<typename U>
155 void Reset(const U& other) { 159 void Reset(const U& other) {
156 this->Reset(other.env(), other.obj()); 160 this->Reset(other.env(), other.obj());
157 } 161 }
158 162
159 template<typename U> 163 template<typename U>
160 void Reset(JNIEnv* env, U obj) { 164 void Reset(JNIEnv* env, U obj) {
161 implicit_cast<T>(obj); // Ensure U is assignable to T 165 implicit_cast<T>(obj); // Ensure U is assignable to T
162 this->SetNewGlobalRef(env, obj); 166 this->SetNewGlobalRef(env, obj);
163 } 167 }
164 168
165 // Releases the global reference to the caller. The caller *must* delete the 169 // Releases the global reference to the caller. The caller *must* delete the
166 // global reference when it is done with it. 170 // global reference when it is done with it.
167 T Release() { 171 T Release() {
168 return static_cast<T>(this->ReleaseInternal()); 172 return static_cast<T>(this->ReleaseInternal());
169 } 173 }
170 }; 174 };
171 175
172 } // namespace android 176 } // namespace android
173 } // namespace base 177 } // namespace base
174 178
175 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ 179 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_
OLDNEW
« no previous file with comments | « base/android/jni_string.cc ('k') | base/android/scoped_java_ref.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698