| 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 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 const BASE_EXPORT jobject GetApplicationContext(); | 49 const BASE_EXPORT jobject GetApplicationContext(); |
| 50 | 50 |
| 51 // Finds the class named |class_name| and returns it. | 51 // Finds the class named |class_name| and returns it. |
| 52 // Use this method instead of invoking directly the JNI FindClass method (to | 52 // Use this method instead of invoking directly the JNI FindClass method (to |
| 53 // prevent leaking local references). | 53 // prevent leaking local references). |
| 54 // This method triggers a fatal assertion if the class could not be found. | 54 // This method triggers a fatal assertion if the class could not be found. |
| 55 // Use HasClass if you need to check whether the class exists. | 55 // Use HasClass if you need to check whether the class exists. |
| 56 BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, | 56 BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, |
| 57 const char* class_name); | 57 const char* class_name); |
| 58 | 58 |
| 59 // Returns true iff the class |class_name| could be found. | |
| 60 BASE_EXPORT bool HasClass(JNIEnv* env, const char* class_name); | |
| 61 | |
| 62 // This class is a wrapper for JNIEnv Get(Static)MethodID. | 59 // This class is a wrapper for JNIEnv Get(Static)MethodID. |
| 63 class BASE_EXPORT MethodID { | 60 class BASE_EXPORT MethodID { |
| 64 public: | 61 public: |
| 65 enum Type { | 62 enum Type { |
| 66 TYPE_STATIC, | 63 TYPE_STATIC, |
| 67 TYPE_INSTANCE, | 64 TYPE_INSTANCE, |
| 68 }; | 65 }; |
| 69 | 66 |
| 70 // Returns the method ID for the method with the specified name and signature. | 67 // Returns the method ID for the method with the specified name and signature. |
| 71 // This method triggers a fatal assertion if the method could not be found. | 68 // This method triggers a fatal assertion if the method could not be found. |
| 72 template<Type type> | 69 template<Type type> |
| 73 static jmethodID Get(JNIEnv* env, | 70 static jmethodID Get(JNIEnv* env, |
| 74 jclass clazz, | 71 jclass clazz, |
| 75 const char* method_name, | 72 const char* method_name, |
| 76 const char* jni_signature); | 73 const char* jni_signature); |
| 77 | 74 |
| 78 // The caller is responsible to zero-initialize |atomic_method_id|. | 75 // The caller is responsible to zero-initialize |atomic_method_id|. |
| 79 // It's fine to simultaneously call this on multiple threads referencing the | 76 // It's fine to simultaneously call this on multiple threads referencing the |
| 80 // same |atomic_method_id|. | 77 // same |atomic_method_id|. |
| 81 template<Type type> | 78 template<Type type> |
| 82 static jmethodID LazyGet(JNIEnv* env, | 79 static jmethodID LazyGet(JNIEnv* env, |
| 83 jclass clazz, | 80 jclass clazz, |
| 84 const char* method_name, | 81 const char* method_name, |
| 85 const char* jni_signature, | 82 const char* jni_signature, |
| 86 base::subtle::AtomicWord* atomic_method_id); | 83 base::subtle::AtomicWord* atomic_method_id); |
| 87 }; | 84 }; |
| 88 | 85 |
| 89 // Gets the method ID from the class name. Clears the pending Java exception | |
| 90 // and returns NULL if the method is not found. Caches results. Note that | |
| 91 // MethodID::Get() above avoids a class lookup, but does not cache results. | |
| 92 // Strings passed to this function are held in the cache and MUST remain valid | |
| 93 // beyond the duration of all future calls to this function, across all | |
| 94 // threads. In practice, this means that the function should only be used with | |
| 95 // string constants. | |
| 96 BASE_EXPORT jmethodID GetMethodIDFromClassName(JNIEnv* env, | |
| 97 const char* class_name, | |
| 98 const char* method, | |
| 99 const char* jni_signature); | |
| 100 | |
| 101 // Gets the field ID for a class field. | |
| 102 // This method triggers a fatal assertion if the field could not be found. | |
| 103 BASE_EXPORT jfieldID GetFieldID(JNIEnv* env, | |
| 104 const JavaRef<jclass>& clazz, | |
| 105 const char* field_name, | |
| 106 const char* jni_signature); | |
| 107 | |
| 108 // Returns true if |clazz| as a field with the given name and signature. | |
| 109 // TODO(jcivelli): Determine whether we explicitly have to pass the environment. | |
| 110 BASE_EXPORT bool HasField(JNIEnv* env, | |
| 111 const JavaRef<jclass>& clazz, | |
| 112 const char* field_name, | |
| 113 const char* jni_signature); | |
| 114 | |
| 115 // Gets the field ID for a static class field. | |
| 116 // This method triggers a fatal assertion if the field could not be found. | |
| 117 BASE_EXPORT jfieldID GetStaticFieldID(JNIEnv* env, | |
| 118 const JavaRef<jclass>& clazz, | |
| 119 const char* field_name, | |
| 120 const char* jni_signature); | |
| 121 | |
| 122 // Returns true if an exception is pending in the provided JNIEnv*. | 86 // Returns true if an exception is pending in the provided JNIEnv*. |
| 123 BASE_EXPORT bool HasException(JNIEnv* env); | 87 BASE_EXPORT bool HasException(JNIEnv* env); |
| 124 | 88 |
| 125 // If an exception is pending in the provided JNIEnv*, this function clears it | 89 // If an exception is pending in the provided JNIEnv*, this function clears it |
| 126 // and returns true. | 90 // and returns true. |
| 127 BASE_EXPORT bool ClearException(JNIEnv* env); | 91 BASE_EXPORT bool ClearException(JNIEnv* env); |
| 128 | 92 |
| 129 // This function will call CHECK() macro if there's any pending exception. | 93 // This function will call CHECK() macro if there's any pending exception. |
| 130 BASE_EXPORT void CheckException(JNIEnv* env); | 94 BASE_EXPORT void CheckException(JNIEnv* env); |
| 131 | 95 |
| 132 } // namespace android | 96 } // namespace android |
| 133 } // namespace base | 97 } // namespace base |
| 134 | 98 |
| 135 #endif // BASE_ANDROID_JNI_ANDROID_H_ | 99 #endif // BASE_ANDROID_JNI_ANDROID_H_ |
| OLD | NEW |