| 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/atomicops.h" |
| 13 #include "base/base_export.h" |
| 13 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 14 | 15 |
| 15 namespace base { | 16 namespace base { |
| 16 namespace android { | 17 namespace android { |
| 17 | 18 |
| 18 // 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. |
| 19 #define JNI_EXPORT __attribute__ ((visibility("default"))) | 20 #define JNI_EXPORT __attribute__ ((visibility("default"))) |
| 20 | 21 |
| 21 // Contains the registration method information for initializing JNI bindings. | 22 // Contains the registration method information for initializing JNI bindings. |
| 22 struct RegistrationMethod { | 23 struct RegistrationMethod { |
| 23 const char* name; | 24 const char* name; |
| 24 bool (*func)(JNIEnv* env); | 25 bool (*func)(JNIEnv* env); |
| 25 }; | 26 }; |
| 26 | 27 |
| 27 // Attach the current thread to the VM (if necessary) and return the JNIEnv*. | 28 // Attach the current thread to the VM (if necessary) and return the JNIEnv*. |
| 28 JNIEnv* AttachCurrentThread(); | 29 BASE_EXPORT JNIEnv* AttachCurrentThread(); |
| 29 | 30 |
| 30 // Detach the current thread from VM if it is attached. | 31 // Detach the current thread from VM if it is attached. |
| 31 void DetachFromVM(); | 32 BASE_EXPORT void DetachFromVM(); |
| 32 | 33 |
| 33 // Initializes the global JVM. It is not necessarily called before | 34 // Initializes the global JVM. It is not necessarily called before |
| 34 // InitApplicationContext(). | 35 // InitApplicationContext(). |
| 35 void InitVM(JavaVM* vm); | 36 BASE_EXPORT void InitVM(JavaVM* vm); |
| 36 | 37 |
| 37 // Initializes the global application context object. The |context| can be any | 38 // Initializes the global application context object. The |context| can be any |
| 38 // valid reference to the application context. Internally holds a global ref to | 39 // valid reference to the application context. Internally holds a global ref to |
| 39 // the context. InitVM and InitApplicationContext maybe called in either order. | 40 // the context. InitVM and InitApplicationContext maybe called in either order. |
| 40 void InitApplicationContext(const JavaRef<jobject>& context); | 41 BASE_EXPORT void InitApplicationContext(const JavaRef<jobject>& context); |
| 41 | 42 |
| 42 // Gets a global ref to the application context set with | 43 // Gets a global ref to the application context set with |
| 43 // InitApplicationContext(). Ownership is retained by the function - the caller | 44 // InitApplicationContext(). Ownership is retained by the function - the caller |
| 44 // must NOT release it. | 45 // must NOT release it. |
| 45 const jobject GetApplicationContext(); | 46 const BASE_EXPORT jobject GetApplicationContext(); |
| 46 | 47 |
| 47 // Finds the class named |class_name| and returns it. | 48 // Finds the class named |class_name| and returns it. |
| 48 // Use this method instead of invoking directly the JNI FindClass method (to | 49 // Use this method instead of invoking directly the JNI FindClass method (to |
| 49 // prevent leaking local references). | 50 // prevent leaking local references). |
| 50 // This method triggers a fatal assertion if the class could not be found. | 51 // This method triggers a fatal assertion if the class could not be found. |
| 51 // Use HasClass if you need to check whether the class exists. | 52 // Use HasClass if you need to check whether the class exists. |
| 52 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name); | 53 BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, |
| 54 const char* class_name); |
| 53 | 55 |
| 54 // Similar to the above, but the caller is responsible to manage the jclass | 56 // Similar to the above, but the caller is responsible to manage the jclass |
| 55 // lifetime. | 57 // lifetime. |
| 56 jclass GetUnscopedClass(JNIEnv* env, const char* class_name) WARN_UNUSED_RESULT; | 58 BASE_EXPORT jclass GetUnscopedClass(JNIEnv* env, |
| 59 const char* class_name) WARN_UNUSED_RESULT; |
| 57 | 60 |
| 58 // Returns true iff the class |class_name| could be found. | 61 // Returns true iff the class |class_name| could be found. |
| 59 bool HasClass(JNIEnv* env, const char* class_name); | 62 BASE_EXPORT bool HasClass(JNIEnv* env, const char* class_name); |
| 60 | 63 |
| 61 // This class is a wrapper for JNIEnv Get(Static)MethodID. | 64 // This class is a wrapper for JNIEnv Get(Static)MethodID. |
| 62 class MethodID { | 65 class BASE_EXPORT MethodID { |
| 63 public: | 66 public: |
| 64 enum Type { | 67 enum Type { |
| 65 TYPE_STATIC, | 68 TYPE_STATIC, |
| 66 TYPE_INSTANCE, | 69 TYPE_INSTANCE, |
| 67 }; | 70 }; |
| 68 | 71 |
| 69 // Returns the method ID for the method with the specified name and signature. | 72 // Returns the method ID for the method with the specified name and signature. |
| 70 // This method triggers a fatal assertion if the method could not be found. | 73 // This method triggers a fatal assertion if the method could not be found. |
| 71 template<Type type> | 74 template<Type type> |
| 72 static jmethodID Get(JNIEnv* env, | 75 static jmethodID Get(JNIEnv* env, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 85 base::subtle::AtomicWord* atomic_method_id); | 88 base::subtle::AtomicWord* atomic_method_id); |
| 86 }; | 89 }; |
| 87 | 90 |
| 88 // Gets the method ID from the class name. Clears the pending Java exception | 91 // Gets the method ID from the class name. Clears the pending Java exception |
| 89 // and returns NULL if the method is not found. Caches results. Note that | 92 // and returns NULL if the method is not found. Caches results. Note that |
| 90 // MethodID::Get() above avoids a class lookup, but does not cache results. | 93 // MethodID::Get() above avoids a class lookup, but does not cache results. |
| 91 // Strings passed to this function are held in the cache and MUST remain valid | 94 // Strings passed to this function are held in the cache and MUST remain valid |
| 92 // beyond the duration of all future calls to this function, across all | 95 // beyond the duration of all future calls to this function, across all |
| 93 // threads. In practice, this means that the function should only be used with | 96 // threads. In practice, this means that the function should only be used with |
| 94 // string constants. | 97 // string constants. |
| 95 jmethodID GetMethodIDFromClassName(JNIEnv* env, | 98 BASE_EXPORT jmethodID GetMethodIDFromClassName(JNIEnv* env, |
| 96 const char* class_name, | 99 const char* class_name, |
| 97 const char* method, | 100 const char* method, |
| 98 const char* jni_signature); | 101 const char* jni_signature); |
| 99 | 102 |
| 100 // Gets the field ID for a class field. | 103 // Gets the field ID for a class field. |
| 101 // This method triggers a fatal assertion if the field could not be found. | 104 // This method triggers a fatal assertion if the field could not be found. |
| 102 jfieldID GetFieldID(JNIEnv* env, | 105 BASE_EXPORT jfieldID GetFieldID(JNIEnv* env, |
| 103 const JavaRef<jclass>& clazz, | 106 const JavaRef<jclass>& clazz, |
| 104 const char* field_name, | 107 const char* field_name, |
| 105 const char* jni_signature); | 108 const char* jni_signature); |
| 106 | 109 |
| 107 // Returns true if |clazz| as a field with the given name and signature. | 110 // Returns true if |clazz| as a field with the given name and signature. |
| 108 // TODO(jcivelli): Determine whether we explicitly have to pass the environment. | 111 // TODO(jcivelli): Determine whether we explicitly have to pass the environment. |
| 109 bool HasField(JNIEnv* env, | 112 BASE_EXPORT bool HasField(JNIEnv* env, |
| 110 const JavaRef<jclass>& clazz, | |
| 111 const char* field_name, | |
| 112 const char* jni_signature); | |
| 113 | |
| 114 // Gets the field ID for a static class field. | |
| 115 // This method triggers a fatal assertion if the field could not be found. | |
| 116 jfieldID GetStaticFieldID(JNIEnv* env, | |
| 117 const JavaRef<jclass>& clazz, | 113 const JavaRef<jclass>& clazz, |
| 118 const char* field_name, | 114 const char* field_name, |
| 119 const char* jni_signature); | 115 const char* jni_signature); |
| 120 | 116 |
| 117 // Gets the field ID for a static class field. |
| 118 // This method triggers a fatal assertion if the field could not be found. |
| 119 BASE_EXPORT jfieldID GetStaticFieldID(JNIEnv* env, |
| 120 const JavaRef<jclass>& clazz, |
| 121 const char* field_name, |
| 122 const char* jni_signature); |
| 123 |
| 121 // Returns true if an exception is pending in the provided JNIEnv*. | 124 // Returns true if an exception is pending in the provided JNIEnv*. |
| 122 bool HasException(JNIEnv* env); | 125 BASE_EXPORT bool HasException(JNIEnv* env); |
| 123 | 126 |
| 124 // If an exception is pending in the provided JNIEnv*, this function clears it | 127 // If an exception is pending in the provided JNIEnv*, this function clears it |
| 125 // and returns true. | 128 // and returns true. |
| 126 bool ClearException(JNIEnv* env); | 129 BASE_EXPORT bool ClearException(JNIEnv* env); |
| 127 | 130 |
| 128 // This function will call CHECK() macro if there's any pending exception. | 131 // This function will call CHECK() macro if there's any pending exception. |
| 129 void CheckException(JNIEnv* env); | 132 BASE_EXPORT void CheckException(JNIEnv* env); |
| 130 | 133 |
| 131 } // namespace android | 134 } // namespace android |
| 132 } // namespace base | 135 } // namespace base |
| 133 | 136 |
| 134 #endif // BASE_ANDROID_JNI_ANDROID_H_ | 137 #endif // BASE_ANDROID_JNI_ANDROID_H_ |
| OLD | NEW |