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