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 <string> | 11 #include <string> |
12 | 12 |
| 13 #include "base/android/context_utils.h" |
13 #include "base/android/scoped_java_ref.h" | 14 #include "base/android/scoped_java_ref.h" |
14 #include "base/atomicops.h" | 15 #include "base/atomicops.h" |
15 #include "base/base_export.h" | 16 #include "base/base_export.h" |
16 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 namespace android { | 20 namespace android { |
20 | 21 |
21 // Used to mark symbols to be exported in a shared library's symbol table. | 22 // Used to mark symbols to be exported in a shared library's symbol table. |
22 #define JNI_EXPORT __attribute__ ((visibility("default"))) | 23 #define JNI_EXPORT __attribute__ ((visibility("default"))) |
(...skipping 17 matching lines...) Expand all Loading... |
40 // Same to AttachCurrentThread except that thread name will be set to | 41 // Same to AttachCurrentThread except that thread name will be set to |
41 // |thread_name| if it is the first call. Otherwise, thread_name won't be | 42 // |thread_name| if it is the first call. Otherwise, thread_name won't be |
42 // changed. AttachCurrentThread() doesn't regard underlying platform thread | 43 // changed. AttachCurrentThread() doesn't regard underlying platform thread |
43 // name, but just resets it to "Thread-???". This function should be called | 44 // name, but just resets it to "Thread-???". This function should be called |
44 // right after new thread is created if it is important to keep thread name. | 45 // right after new thread is created if it is important to keep thread name. |
45 BASE_EXPORT JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name); | 46 BASE_EXPORT JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name); |
46 | 47 |
47 // Detaches the current thread from VM if it is attached. | 48 // Detaches the current thread from VM if it is attached. |
48 BASE_EXPORT void DetachFromVM(); | 49 BASE_EXPORT void DetachFromVM(); |
49 | 50 |
50 // Initializes the global JVM. It is not necessarily called before | 51 // Initializes the global JVM. |
51 // InitApplicationContext(). | |
52 BASE_EXPORT void InitVM(JavaVM* vm); | 52 BASE_EXPORT void InitVM(JavaVM* vm); |
53 | 53 |
54 // Returns true if the global JVM has been initialized. | 54 // Returns true if the global JVM has been initialized. |
55 BASE_EXPORT bool IsVMInitialized(); | 55 BASE_EXPORT bool IsVMInitialized(); |
56 | 56 |
57 // Initializes the global application context object. The |context| can be any | |
58 // valid reference to the application context. Internally holds a global ref to | |
59 // the context. InitVM and InitApplicationContext maybe called in either order. | |
60 BASE_EXPORT void InitApplicationContext(JNIEnv* env, | |
61 const JavaRef<jobject>& context); | |
62 | |
63 // Initializes the global ClassLoader used by the GetClass and LazyGetClass | 57 // Initializes the global ClassLoader used by the GetClass and LazyGetClass |
64 // methods. This is needed because JNI will use the base ClassLoader when there | 58 // methods. This is needed because JNI will use the base ClassLoader when there |
65 // is no Java code on the stack. The base ClassLoader doesn't know about any of | 59 // is no Java code on the stack. The base ClassLoader doesn't know about any of |
66 // the application classes and will fail to lookup anything other than system | 60 // the application classes and will fail to lookup anything other than system |
67 // classes. | 61 // classes. |
68 BASE_EXPORT void InitReplacementClassLoader( | 62 BASE_EXPORT void InitReplacementClassLoader( |
69 JNIEnv* env, | 63 JNIEnv* env, |
70 const JavaRef<jobject>& class_loader); | 64 const JavaRef<jobject>& class_loader); |
71 | 65 |
72 // Gets a global ref to the application context set with | |
73 // InitApplicationContext(). Ownership is retained by the function - the caller | |
74 // must NOT release it. | |
75 const BASE_EXPORT jobject GetApplicationContext(); | |
76 | |
77 // Finds the class named |class_name| and returns it. | 66 // Finds the class named |class_name| and returns it. |
78 // Use this method instead of invoking directly the JNI FindClass method (to | 67 // Use this method instead of invoking directly the JNI FindClass method (to |
79 // prevent leaking local references). | 68 // prevent leaking local references). |
80 // This method triggers a fatal assertion if the class could not be found. | 69 // This method triggers a fatal assertion if the class could not be found. |
81 // Use HasClass if you need to check whether the class exists. | 70 // Use HasClass if you need to check whether the class exists. |
82 BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, | 71 BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, |
83 const char* class_name); | 72 const char* class_name); |
84 | 73 |
85 // The method will initialize |atomic_class_id| to contain a global ref to the | 74 // The method will initialize |atomic_class_id| to contain a global ref to the |
86 // class. And will return that ref on subsequent calls. It's the caller's | 75 // class. And will return that ref on subsequent calls. It's the caller's |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 BASE_EXPORT void CheckException(JNIEnv* env); | 120 BASE_EXPORT void CheckException(JNIEnv* env); |
132 | 121 |
133 // This returns a string representation of the java stack trace. | 122 // This returns a string representation of the java stack trace. |
134 BASE_EXPORT std::string GetJavaExceptionInfo(JNIEnv* env, | 123 BASE_EXPORT std::string GetJavaExceptionInfo(JNIEnv* env, |
135 jthrowable java_throwable); | 124 jthrowable java_throwable); |
136 | 125 |
137 } // namespace android | 126 } // namespace android |
138 } // namespace base | 127 } // namespace base |
139 | 128 |
140 #endif // BASE_ANDROID_JNI_ANDROID_H_ | 129 #endif // BASE_ANDROID_JNI_ANDROID_H_ |
OLD | NEW |