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" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 namespace android { | 16 namespace android { |
16 | 17 |
17 // Used to mark symbols to be exported in a shared library's symbol table. | 18 // Used to mark symbols to be exported in a shared library's symbol table. |
18 #define JNI_EXPORT __attribute__ ((visibility("default"))) | 19 #define JNI_EXPORT __attribute__ ((visibility("default"))) |
19 | 20 |
20 // Contains the registration method information for initializing JNI bindings. | 21 // Contains the registration method information for initializing JNI bindings. |
21 struct RegistrationMethod { | 22 struct RegistrationMethod { |
(...skipping 28 matching lines...) Expand all Loading... | |
50 // Use HasClass if you need to check whether the class exists. | 51 // Use HasClass if you need to check whether the class exists. |
51 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name); | 52 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name); |
52 | 53 |
53 // Similar to the above, but the caller is responsible to manage the jclass | 54 // Similar to the above, but the caller is responsible to manage the jclass |
54 // lifetime. | 55 // lifetime. |
55 jclass GetUnscopedClass(JNIEnv* env, const char* class_name) WARN_UNUSED_RESULT; | 56 jclass GetUnscopedClass(JNIEnv* env, const char* class_name) WARN_UNUSED_RESULT; |
56 | 57 |
57 // Returns true iff the class |class_name| could be found. | 58 // Returns true iff the class |class_name| could be found. |
58 bool HasClass(JNIEnv* env, const char* class_name); | 59 bool HasClass(JNIEnv* env, const char* class_name); |
59 | 60 |
60 // Returns the method ID for the method with the specified name and signature. | 61 // This class is a wrapper for JNIEnv Get(Static)MethodID. |
61 // This method triggers a fatal assertion if the method could not be found. | 62 class MethodID { |
62 // Use HasMethod if you need to check whether a method exists. | 63 public: |
63 jmethodID GetMethodID(JNIEnv* env, | 64 enum MethodType { |
64 const JavaRef<jclass>& clazz, | 65 METHODTYPE_STATIC, |
65 const char* method_name, | 66 METHODTYPE_NORMAL, |
66 const char* jni_signature); | 67 }; |
67 | 68 |
68 // Similar to GetMethodID, but takes a raw jclass. | 69 // Wether or not to CHECK if an exception happens when |
69 jmethodID GetMethodID(JNIEnv* env, | 70 // trying to obtain a method id. |
70 jclass clazz, | 71 enum ExceptionCheck { |
71 const char* method_name, | 72 EXCEPTIONCHECK_NO, |
72 const char* jni_signature); | 73 EXCEPTIONCHECK_YES, |
74 }; | |
73 | 75 |
74 // Unlike GetMethodID, returns NULL if the method could not be found. | 76 template<MethodType method_type, |
75 jmethodID GetMethodIDOrNull(JNIEnv* env, | 77 ExceptionCheck exception_check> |
76 jclass clazz, | 78 static jmethodID Get(JNIEnv* env, |
77 const char* method_name, | 79 jclass clazz, |
78 const char* jni_signature); | 80 const char* method_name, |
81 const char* jni_signature); | |
79 | 82 |
80 // Returns the method ID for the static method with the specified name and | 83 // The caller is responsible to zero-initialize |atomic_method_id|. |
81 // signature. | 84 // If it's set, it'll return immediately. Otherwise, it'll call into |
82 // This method triggers a fatal assertion if the method could not be found. | 85 // ::Get() above. If there's a race, it's ok since the values are the same |
joth
2012/10/04 17:59:59
nit: if there's a race... is all implementation de
bulach
2012/10/04 18:58:17
Done.
| |
83 // Use HasMethod if you need to check whether a method exists. | 86 // (and the duplicated effort will happen only once). |
84 jmethodID GetStaticMethodID(JNIEnv* env, | 87 template<MethodType method_type, |
85 const JavaRef<jclass>& clazz, | 88 ExceptionCheck exception_check> |
86 const char* method_name, | 89 static jmethodID LazyGet(JNIEnv* env, |
87 const char* jni_signature); | 90 jclass clazz, |
88 | 91 const char* method_name, |
89 // Similar to the GetStaticMethodID, but takes a raw jclass. | 92 const char* jni_signature, |
90 jmethodID GetStaticMethodID(JNIEnv* env, | 93 base::subtle::AtomicWord* atomic_method_id); |
91 jclass clazz, | 94 }; |
92 const char* method_name, | |
93 const char* jni_signature); | |
94 | |
95 // Unlike GetStaticMethodID, returns NULL if the method could not be found. | |
96 jmethodID GetStaticMethodIDOrNull(JNIEnv* env, | |
97 jclass clazz, | |
98 const char* method_name, | |
99 const char* jni_signature); | |
100 | |
101 // Returns true iff |clazz| has a method with the specified name and signature. | |
102 bool HasMethod(JNIEnv* env, | |
103 const JavaRef<jclass>& clazz, | |
104 const char* method_name, | |
105 const char* jni_signature); | |
106 | 95 |
107 // Gets the method ID from the class name. Clears the pending Java exception | 96 // Gets the method ID from the class name. Clears the pending Java exception |
108 // and returns NULL if the method is not found. Caches results. Note that | 97 // and returns NULL if the method is not found. Caches results. Note that |
109 // GetMethodID() below avoids a class lookup, but does not cache results. | 98 // MethodID() above avoids a class lookup, but does not cache results. |
joth
2012/10/04 17:59:59
nit: MethodID above is not a function. MethodID::G
bulach
2012/10/04 18:58:17
Done.
| |
110 // Strings passed to this function are held in the cache and MUST remain valid | 99 // Strings passed to this function are held in the cache and MUST remain valid |
111 // beyond the duration of all future calls to this function, across all | 100 // beyond the duration of all future calls to this function, across all |
112 // threads. In practice, this means that the function should only be used with | 101 // threads. In practice, this means that the function should only be used with |
113 // string constants. | 102 // string constants. |
114 jmethodID GetMethodIDFromClassName(JNIEnv* env, | 103 jmethodID GetMethodIDFromClassName(JNIEnv* env, |
115 const char* class_name, | 104 const char* class_name, |
116 const char* method, | 105 const char* method, |
117 const char* jni_signature); | 106 const char* jni_signature); |
118 | 107 |
119 // Gets the field ID for a class field. | 108 // Gets the field ID for a class field. |
(...skipping 24 matching lines...) Expand all Loading... | |
144 // and returns true. | 133 // and returns true. |
145 bool ClearException(JNIEnv* env); | 134 bool ClearException(JNIEnv* env); |
146 | 135 |
147 // This function will call CHECK() macro if there's any pending exception. | 136 // This function will call CHECK() macro if there's any pending exception. |
148 void CheckException(JNIEnv* env); | 137 void CheckException(JNIEnv* env); |
149 | 138 |
150 } // namespace android | 139 } // namespace android |
151 } // namespace base | 140 } // namespace base |
152 | 141 |
153 #endif // BASE_ANDROID_JNI_ANDROID_H_ | 142 #endif // BASE_ANDROID_JNI_ANDROID_H_ |
OLD | NEW |