OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 12 |
13 namespace base { | 13 namespace base { |
14 namespace android { | 14 namespace android { |
15 | 15 |
16 // Attach the current thread to the VM (if necessary) and return the JNIEnv*. | 16 // Attach the current thread to the VM (if necessary) and return the JNIEnv*. |
17 JNIEnv* AttachCurrentThread(); | 17 JNIEnv* AttachCurrentThread(); |
18 | 18 |
19 // Detach the current thread from VM if it is attached. | 19 // Detach the current thread from VM if it is attached. |
20 void DetachFromVM(); | 20 void DetachFromVM(); |
21 | 21 |
22 // Initializes the global JVM. It is not necessarily called before | 22 // Initializes the global JVM. It is not necessarily called before |
23 // InitApplicationContext(). | 23 // InitApplicationContext(). |
24 void InitVM(JavaVM* vm); | 24 void InitVM(JavaVM* vm); |
25 | 25 |
26 // Initializes the global application context object. The |context| can be any | 26 // Initializes the global application context object. The |context| can be any |
27 // valid reference to the application context. Internally holds a global ref to | 27 // valid reference to the application context. Internally holds a global ref to |
28 // the context. InitVM and InitApplicationContext maybe called in either order. | 28 // the context. InitVM and InitApplicationContext maybe called in either order. |
29 // TODO: We might combine InitVM() and InitApplicationContext() into one method. | |
30 void InitApplicationContext(const JavaRef<jobject>& context); | 29 void InitApplicationContext(const JavaRef<jobject>& context); |
31 | 30 |
32 // Gets a global ref to the application context set with | 31 // Gets a global ref to the application context set with |
33 // InitApplicationContext(). Ownership is retained by the function - the caller | 32 // InitApplicationContext(). Ownership is retained by the function - the caller |
34 // must NOT release it. | 33 // must NOT release it. |
35 jobject GetApplicationContext(); | 34 const jobject GetApplicationContext(); |
| 35 |
| 36 // Finds the class named |class_name| and returns it. |
| 37 // Use this method instead of invoking directly the JNI FindClass method (to |
| 38 // prevent leaking local references). |
| 39 // This method triggers a fatal assertion if the class could not be found. |
| 40 // Use HasClass if you need to check whether the class exists. |
| 41 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name); |
| 42 |
| 43 // Returns true iff the class |class_name| could be found. |
| 44 bool HasClass(JNIEnv* env, const char* class_name); |
| 45 |
| 46 // Returns the method ID for the method with the specified name and signature. |
| 47 // This method triggers a fatal assertion if the method could not be found. |
| 48 // Use HasMethod if you need to check whether a method exists. |
| 49 jmethodID GetMethodID(JNIEnv* env, |
| 50 const JavaRef<jclass>& clazz, |
| 51 const char* method_name, |
| 52 const char* jni_signature); |
| 53 |
| 54 // Returns the method ID for the static method with the specified name and |
| 55 // signature. |
| 56 // This method triggers a fatal assertion if the method could not be found. |
| 57 // Use HasMethod if you need to check whether a method exists. |
| 58 jmethodID GetStaticMethodID(JNIEnv* env, |
| 59 const JavaRef<jclass>& clazz, |
| 60 const char* method_name, |
| 61 const char* jni_signature); |
| 62 |
| 63 // Returns true iff |clazz| has a method with the specified name and signature. |
| 64 bool HasMethod(JNIEnv* env, |
| 65 const JavaRef<jclass>& clazz, |
| 66 const char* method_name, |
| 67 const char* jni_signature); |
36 | 68 |
37 // Gets the method ID from the class name. Clears the pending Java exception | 69 // Gets the method ID from the class name. Clears the pending Java exception |
38 // and returns NULL if the method is not found. Caches results. Note that | 70 // and returns NULL if the method is not found. Caches results. Note that |
39 // GetMethodID() below avoids a class lookup, but does not cache results. | 71 // GetMethodID() below avoids a class lookup, but does not cache results. |
40 // Strings passed to this function are held in the cache and MUST remain valid | 72 // Strings passed to this function are held in the cache and MUST remain valid |
41 // beyond the duration of all future calls to this function, across all | 73 // beyond the duration of all future calls to this function, across all |
42 // threads. In practice, this means that the function should only be used with | 74 // threads. In practice, this means that the function should only be used with |
43 // string constants. | 75 // string constants. |
44 jmethodID GetMethodIDFromClassName(JNIEnv* env, | 76 jmethodID GetMethodIDFromClassName(JNIEnv* env, |
45 const char* class_name, | 77 const char* class_name, |
46 const char* method, | 78 const char* method, |
47 const char* jni_signature); | 79 const char* jni_signature); |
48 | 80 |
49 // Get the method ID for a method. Will clear the pending Java | 81 // Gets the field ID for a class field. |
50 // exception and return 0 if the method is not found. | 82 // This method triggers a fatal assertion if the field could not be found. |
51 jmethodID GetMethodID(JNIEnv* env, | |
52 jclass clazz, | |
53 const char* const method, | |
54 const char* const jni_signature); | |
55 | |
56 // Get the method ID for a class static method. Will clear the pending Java | |
57 // exception and return 0 if the method is not found. | |
58 jmethodID GetStaticMethodID(JNIEnv* env, | |
59 jclass clazz, | |
60 const char* const method, | |
61 const char* const jni_signature); | |
62 | |
63 // Gets the field ID for a class field. Clears the pending Java exception and | |
64 // returns NULL if the field is not found. | |
65 jfieldID GetFieldID(JNIEnv* env, | 83 jfieldID GetFieldID(JNIEnv* env, |
66 jclass clazz, | 84 const JavaRef<jclass>& clazz, |
67 const char* field, | 85 const char* field_name, |
68 const char* jni_signature); | 86 const char* jni_signature); |
69 | 87 |
70 // Returns true if an exception is pending in the provided JNIEnv*. If an | 88 // Returns true if |clazz| as a field with the given name and signature. |
71 // exception is pending, this function prints and then clears it. | 89 // TODO(jcivelli): Determine whether we explicitly have to pass the environment. |
72 bool CheckException(JNIEnv* env); | 90 bool HasField(JNIEnv* env, |
| 91 const JavaRef<jclass>& clazz, |
| 92 const char* field_name, |
| 93 const char* jni_signature); |
| 94 |
| 95 // Gets the field ID for a static class field. |
| 96 // This method triggers a fatal assertion if the field could not be found. |
| 97 jfieldID GetStaticFieldID(JNIEnv* env, |
| 98 const JavaRef<jclass>& clazz, |
| 99 const char* field_name, |
| 100 const char* jni_signature); |
| 101 |
| 102 // Returns true if an exception is pending in the provided JNIEnv*. |
| 103 bool HasException(JNIEnv* env); |
| 104 |
| 105 // If an exception is pending in the provided JNIEnv*, this function clears it |
| 106 // and returns true. |
| 107 bool ClearException(JNIEnv* env); |
| 108 |
| 109 // This function will call CHECK() macro if there's any pending exception. |
| 110 void CheckException(JNIEnv* env); |
73 | 111 |
74 } // namespace android | 112 } // namespace android |
75 } // namespace base | 113 } // namespace base |
76 | 114 |
77 #endif // BASE_ANDROID_JNI_ANDROID_H_ | 115 #endif // BASE_ANDROID_JNI_ANDROID_H_ |
OLD | NEW |