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.
5 #ifndef BASE_ANDROID_JNI_ANDROID_H_
6 #define BASE_ANDROID_JNI_ANDROID_H_
11 #include "base/android/scoped_java_ref.h"
12 #include "base/compiler_specific.h"
17 // Used to mark symbols to be exported in a shared library's symbol table.
18 #define JNI_EXPORT __attribute__ ((visibility("default")))
20 // Contains the registration method information for initializing JNI bindings.
21 struct RegistrationMethod
{
23 bool (*func
)(JNIEnv
* env
);
26 // Attach the current thread to the VM (if necessary) and return the JNIEnv*.
27 JNIEnv
* AttachCurrentThread();
29 // Detach the current thread from VM if it is attached.
32 // Initializes the global JVM. It is not necessarily called before
33 // InitApplicationContext().
34 void InitVM(JavaVM
* vm
);
36 // Initializes the global application context object. The |context| can be any
37 // valid reference to the application context. Internally holds a global ref to
38 // the context. InitVM and InitApplicationContext maybe called in either order.
39 void InitApplicationContext(const JavaRef
<jobject
>& context
);
41 // Gets a global ref to the application context set with
42 // InitApplicationContext(). Ownership is retained by the function - the caller
43 // must NOT release it.
44 const jobject
GetApplicationContext();
46 // Finds the class named |class_name| and returns it.
47 // Use this method instead of invoking directly the JNI FindClass method (to
48 // prevent leaking local references).
49 // This method triggers a fatal assertion if the class could not be found.
50 // Use HasClass if you need to check whether the class exists.
51 ScopedJavaLocalRef
<jclass
> GetClass(JNIEnv
* env
, const char* class_name
);
53 // Similar to the above, but the caller is responsible to manage the jclass
55 jclass
GetUnscopedClass(JNIEnv
* env
, const char* class_name
) WARN_UNUSED_RESULT
;
57 // Returns true iff the class |class_name| could be found.
58 bool HasClass(JNIEnv
* env
, const char* class_name
);
60 // Returns the method ID for the method with the specified name and signature.
61 // This method triggers a fatal assertion if the method could not be found.
62 // Use HasMethod if you need to check whether a method exists.
63 jmethodID
GetMethodID(JNIEnv
* env
,
64 const JavaRef
<jclass
>& clazz
,
65 const char* method_name
,
66 const char* jni_signature
);
68 // Similar to GetMethodID, but takes a raw jclass.
69 jmethodID
GetMethodID(JNIEnv
* env
,
71 const char* method_name
,
72 const char* jni_signature
);
74 // Returns the method ID for the static method with the specified name and
76 // This method triggers a fatal assertion if the method could not be found.
77 // Use HasMethod if you need to check whether a method exists.
78 jmethodID
GetStaticMethodID(JNIEnv
* env
,
79 const JavaRef
<jclass
>& clazz
,
80 const char* method_name
,
81 const char* jni_signature
);
83 // Similar to the GetStaticMethodID, but takes a raw jclass.
84 jmethodID
GetStaticMethodID(JNIEnv
* env
,
86 const char* method_name
,
87 const char* jni_signature
);
90 // Returns true iff |clazz| has a method with the specified name and signature.
91 bool HasMethod(JNIEnv
* env
,
92 const JavaRef
<jclass
>& clazz
,
93 const char* method_name
,
94 const char* jni_signature
);
96 // Gets the method ID from the class name. Clears the pending Java exception
97 // and returns NULL if the method is not found. Caches results. Note that
98 // GetMethodID() below avoids a class lookup, but does not cache results.
99 // Strings passed to this function are held in the cache and MUST remain valid
100 // beyond the duration of all future calls to this function, across all
101 // threads. In practice, this means that the function should only be used with
103 jmethodID
GetMethodIDFromClassName(JNIEnv
* env
,
104 const char* class_name
,
106 const char* jni_signature
);
108 // Gets the field ID for a class field.
109 // This method triggers a fatal assertion if the field could not be found.
110 jfieldID
GetFieldID(JNIEnv
* env
,
111 const JavaRef
<jclass
>& clazz
,
112 const char* field_name
,
113 const char* jni_signature
);
115 // Returns true if |clazz| as a field with the given name and signature.
116 // TODO(jcivelli): Determine whether we explicitly have to pass the environment.
117 bool HasField(JNIEnv
* env
,
118 const JavaRef
<jclass
>& clazz
,
119 const char* field_name
,
120 const char* jni_signature
);
122 // Gets the field ID for a static class field.
123 // This method triggers a fatal assertion if the field could not be found.
124 jfieldID
GetStaticFieldID(JNIEnv
* env
,
125 const JavaRef
<jclass
>& clazz
,
126 const char* field_name
,
127 const char* jni_signature
);
129 // Returns true if an exception is pending in the provided JNIEnv*.
130 bool HasException(JNIEnv
* env
);
132 // If an exception is pending in the provided JNIEnv*, this function clears it
134 bool ClearException(JNIEnv
* env
);
136 // This function will call CHECK() macro if there's any pending exception.
137 void CheckException(JNIEnv
* env
);
139 } // namespace android
142 #endif // BASE_ANDROID_JNI_ANDROID_H_