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

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

Issue 1312153003: jni_generator: Pass object parameters as JavaParamRef. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 3 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
« no previous file with comments | « base/android/record_user_action.cc ('k') | base/android/trace_event_binding.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) 2012 The Chromium Authors. All rights reserved. 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 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
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 protected: 87 protected:
88 JavaRef() {} 88 JavaRef() {}
89 ~JavaRef() {} 89 ~JavaRef() {}
90 90
91 JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {} 91 JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {}
92 92
93 private: 93 private:
94 DISALLOW_COPY_AND_ASSIGN(JavaRef); 94 DISALLOW_COPY_AND_ASSIGN(JavaRef);
95 }; 95 };
96 96
97 // Holds a local reference to a JNI method parameter.
98 // Method parameters should not be deleted, and so this class exists purely to
99 // wrap them as a JavaRef<T> in the JNI binding generator. Do not create
100 // instances manually.
101 template<typename T>
102 class JavaParamRef : public JavaRef<T> {
103 public:
104 // Assumes that |obj| is a parameter passed to a JNI method from Java.
105 // Does not assume ownership as parameters should not be deleted.
106 JavaParamRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj) {}
107
108 ~JavaParamRef() {}
109
110 // TODO(torne): remove this cast once we're using JavaRef consistently.
111 // http://crbug.com/506850
112 operator T() const { return JavaRef<T>::obj(); }
113
114 private:
115 DISALLOW_COPY_AND_ASSIGN(JavaParamRef);
116 };
117
97 // Holds a local reference to a Java object. The local reference is scoped 118 // Holds a local reference to a Java object. The local reference is scoped
98 // to the lifetime of this object. 119 // to the lifetime of this object.
99 // Instances of this class may hold onto any JNIEnv passed into it until 120 // 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 121 // 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 122 // thread, objects of this class must be created, used, and destroyed, on a
102 // single thread. 123 // single thread.
103 // Therefore, this class should only be used as a stack-based object and from a 124 // 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 125 // 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, 126 // callstack (e.g. as a class member) or you wish to pass it across threads,
106 // use a ScopedJavaGlobalRef instead. 127 // use a ScopedJavaGlobalRef instead.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 void Reset() { 222 void Reset() {
202 this->ResetGlobalRef(); 223 this->ResetGlobalRef();
203 } 224 }
204 225
205 template<typename U> 226 template<typename U>
206 void Reset(const U& other) { 227 void Reset(const U& other) {
207 this->Reset(NULL, other.obj()); 228 this->Reset(NULL, other.obj());
208 } 229 }
209 230
210 template<typename U> 231 template<typename U>
232 void Reset(JNIEnv* env, const JavaParamRef<U>& other) {
233 this->Reset(env, other.obj());
234 }
235
236 template<typename U>
211 void Reset(JNIEnv* env, U obj) { 237 void Reset(JNIEnv* env, U obj) {
212 implicit_cast<T>(obj); // Ensure U is assignable to T 238 implicit_cast<T>(obj); // Ensure U is assignable to T
213 this->SetNewGlobalRef(env, obj); 239 this->SetNewGlobalRef(env, obj);
214 } 240 }
215 241
216 // Releases the global reference to the caller. The caller *must* delete the 242 // 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 243 // 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. 244 // is *not* a transfer of ownership and Release() should not be used.
219 T Release() { 245 T Release() {
220 return static_cast<T>(this->ReleaseInternal()); 246 return static_cast<T>(this->ReleaseInternal());
221 } 247 }
222 }; 248 };
223 249
224 } // namespace android 250 } // namespace android
225 } // namespace base 251 } // namespace base
226 252
227 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ 253 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_
OLDNEW
« no previous file with comments | « base/android/record_user_action.cc ('k') | base/android/trace_event_binding.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698