OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef BASE_ANDROID_JNI_METHOD_ID_ANDROID_H_ | |
6 #define BASE_ANDROID_JNI_METHOD_ID_ANDROID_H_ | |
7 | |
8 #include <jni.h> | |
9 #include <sys/types.h> | |
10 | |
11 #include "base/android/jni_android.h" | |
12 #include "base/atomicops.h" | |
13 #include "base/logging.h" | |
14 | |
15 namespace base { | |
16 namespace android { | |
17 | |
18 // This class is a wrapper for JNIEnv Get(Static)MethodID. | |
joth
2012/10/03 17:31:52
call out this isn't a class at all, just a namespa
| |
19 // It's mostly used internally by the autogenerated JNI bindings. | |
20 class MethodID { | |
21 public: | |
22 enum MethodType { | |
23 METHODTYPE_STATIC, | |
24 METHODTYPE_NORMAL, | |
25 }; | |
26 | |
27 // Wether or not to CHECK if an exception happens when | |
28 // trying to obtain a method id. | |
29 enum ExceptionCheck { | |
30 EXCEPTIONCHECK_NO, | |
31 EXCEPTIONCHECK_YES, | |
32 }; | |
33 | |
34 template<MethodType method_type, | |
35 ExceptionCheck exception_check> | |
36 static jmethodID Get(JNIEnv* env, | |
37 jclass clazz, | |
38 const char* method_name, | |
39 const char* jni_signature) { | |
40 jmethodID id = method_type == METHODTYPE_STATIC ? | |
41 env->GetStaticMethodID(clazz, method_name, jni_signature) : | |
42 env->GetMethodID(clazz, method_name, jni_signature); | |
43 if (exception_check == EXCEPTIONCHECK_YES) { | |
44 CHECK(base::android::ClearException(env) || id) << | |
45 "Failed to find " << | |
46 (method_type == METHODTYPE_STATIC ? "static " : "") << | |
47 "method " << method_name << " " << jni_signature; | |
48 } else if (base::android::HasException(env)) { | |
49 env->ExceptionClear(); | |
50 } | |
51 return id; | |
joth
2012/10/03 17:31:52
I'd still prefer these method bodies into a .cc fi
| |
52 } | |
53 | |
54 // The caller is responsible to zero-initialize |atomic_method_id|. | |
55 // If it's set, it'll return immediately. Otherwise, it'll call into | |
56 // ::Get() above. If there's a race, it's ok since the values are the same | |
57 // (and the duplicated effort will happen only once). | |
58 template<MethodType method_type, | |
59 ExceptionCheck exception_check> | |
60 static jmethodID LazyGet(JNIEnv* env, | |
61 jclass clazz, | |
62 const char* method_name, | |
63 const char* jni_signature, | |
64 base::subtle::AtomicWord* atomic_method_id) { | |
65 COMPILE_ASSERT(sizeof(subtle::AtomicWord) >= sizeof(jmethodID), | |
66 AtomicWord_SmallerThan_jMethodID); | |
67 subtle::AtomicWord value = base::subtle::Acquire_Load(atomic_method_id); | |
joth
2012/10/03 17:31:52
FWIW, I realized this doesn't need to be an 'acqui
| |
68 if (value) | |
69 return reinterpret_cast<jmethodID>(value); | |
70 jmethodID id = MethodID::Get<method_type, exception_check>( | |
71 env, clazz, method_name, jni_signature); | |
72 base::subtle::Acquire_Store( | |
joth
2012/10/03 17:31:52
nit: C++ style is to align params with open paren
| |
73 atomic_method_id, reinterpret_cast<subtle::AtomicWord>(id)); | |
74 return id; | |
75 } | |
76 }; | |
joth
2012/10/03 17:31:52
if you leave as a class, DISALLOW_IMPLICIT_CTOR an
| |
77 | |
78 } // namespace android | |
79 } // namespace base | |
80 | |
81 #endif // BASE_ANDROID_JNI_METHOD_ID_ANDROID_H_ | |
OLD | NEW |