OLD | NEW |
---|---|
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_JNI_ANDROID_H_ | 5 #ifndef BASE_ANDROID_JNI_ANDROID_H_ |
6 #define BASE_ANDROID_JNI_ANDROID_H_ | 6 #define BASE_ANDROID_JNI_ANDROID_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 | 10 |
11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
12 #include "base/atomicops.h" | |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/logging.h" | |
13 | 15 |
14 namespace base { | 16 namespace base { |
15 namespace android { | 17 namespace android { |
16 | 18 |
17 // Used to mark symbols to be exported in a shared library's symbol table. | 19 // Used to mark symbols to be exported in a shared library's symbol table. |
18 #define JNI_EXPORT __attribute__ ((visibility("default"))) | 20 #define JNI_EXPORT __attribute__ ((visibility("default"))) |
19 | 21 |
20 // Contains the registration method information for initializing JNI bindings. | 22 // Contains the registration method information for initializing JNI bindings. |
21 struct RegistrationMethod { | 23 struct RegistrationMethod { |
22 const char* name; | 24 const char* name; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 // Returns true if an exception is pending in the provided JNIEnv*. | 131 // Returns true if an exception is pending in the provided JNIEnv*. |
130 bool HasException(JNIEnv* env); | 132 bool HasException(JNIEnv* env); |
131 | 133 |
132 // If an exception is pending in the provided JNIEnv*, this function clears it | 134 // If an exception is pending in the provided JNIEnv*, this function clears it |
133 // and returns true. | 135 // and returns true. |
134 bool ClearException(JNIEnv* env); | 136 bool ClearException(JNIEnv* env); |
135 | 137 |
136 // This function will call CHECK() macro if there's any pending exception. | 138 // This function will call CHECK() macro if there's any pending exception. |
137 void CheckException(JNIEnv* env); | 139 void CheckException(JNIEnv* env); |
138 | 140 |
141 class LazyMethodID { | |
142 public: | |
143 enum MethodType { | |
144 METHODTYPE_STATIC, | |
145 METHODTYPE_NORMAL, | |
146 }; | |
147 | |
148 enum ExceptionCheck { | |
149 EXCEPTIONCHECK_YES, | |
Sami
2012/10/02 15:14:05
Super-nit: NO=0, YES=1 would seem more logical.
bulach
2012/10/02 15:28:30
:) done..
| |
150 EXCEPTIONCHECK_NO, | |
151 }; | |
152 | |
153 template<MethodType method_type, ExceptionCheck exception_check> | |
154 static void Get(JNIEnv* env, | |
155 jclass clazz, | |
156 const char* method_name, | |
157 const char* jni_signature, | |
158 jmethodID* method_id) { | |
159 subtle::AtomicWord* atomic = reinterpret_cast<subtle::AtomicWord*>( | |
Sami
2012/10/02 15:14:05
I'm not sure which is better for this use, but an
bulach
2012/10/02 15:28:30
hmm... we have base::internal::ThreadLocalPointer,
| |
160 method_id); | |
161 subtle::AtomicWord value = base::subtle::Acquire_Load(atomic); | |
162 if (value) | |
163 return; | |
164 jmethodID id = method_type == METHODTYPE_STATIC ? | |
165 env->GetStaticMethodID(clazz, method_name, jni_signature) : | |
166 env->GetMethodID(clazz, method_name, jni_signature); | |
167 if (exception_check == EXCEPTIONCHECK_YES) { | |
168 CHECK(id || base::android::ClearException(env)) << | |
169 "Failed to find " << | |
170 (method_type == METHODTYPE_STATIC ? "static " : "") << | |
171 "method " << method_name << " " << jni_signature; | |
172 } else if (base::android::HasException(env)) { | |
173 env->ExceptionClear(); | |
174 } | |
175 base::subtle::Acquire_Store( | |
176 atomic, reinterpret_cast<subtle::AtomicWord>(id)); | |
177 } | |
178 }; | |
179 | |
180 | |
139 } // namespace android | 181 } // namespace android |
140 } // namespace base | 182 } // namespace base |
141 | 183 |
142 #endif // BASE_ANDROID_JNI_ANDROID_H_ | 184 #endif // BASE_ANDROID_JNI_ANDROID_H_ |
OLD | NEW |