1 // Copyright 2012 The Chromium Authors
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_
14 #include "base/android/scoped_java_ref.h"
15 #include "base/auto_reset.h"
16 #include "base/base_export.h"
17 #include "base/compiler_specific.h"
18 #include "base/debug/debugging_buildflags.h"
19 #include "base/debug/stack_trace.h"
24 // Used to mark symbols to be exported in a shared library's symbol table.
25 #define JNI_EXPORT __attribute__ ((visibility("default")))
27 // Contains the registration method information for initializing JNI bindings.
28 struct RegistrationMethod {
30 bool (*func)(JNIEnv* env);
33 // Attaches the current thread to the VM (if necessary) and return the JNIEnv*.
34 BASE_EXPORT JNIEnv* AttachCurrentThread();
36 // Same to AttachCurrentThread except that thread name will be set to
37 // |thread_name| if it is the first call. Otherwise, thread_name won't be
38 // changed. AttachCurrentThread() doesn't regard underlying platform thread
39 // name, but just resets it to "Thread-???". This function should be called
40 // right after new thread is created if it is important to keep thread name.
41 BASE_EXPORT JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name);
43 // Detaches the current thread from VM if it is attached.
44 BASE_EXPORT void DetachFromVM();
46 // Initializes the global JVM.
47 BASE_EXPORT void InitVM(JavaVM* vm);
49 // Returns true if the global JVM has been initialized.
50 BASE_EXPORT bool IsVMInitialized();
52 // Returns the global JVM, or nullptr if it has not been initialized.
53 BASE_EXPORT JavaVM* GetVM();
55 // Do not allow any future native->java calls.
56 // This is necessary in gtest DEATH_TESTS to prevent
57 // GetJavaStackTraceIfPresent() from accessing a defunct JVM (due to fork()).
58 // https://crbug.com/1484834
59 BASE_EXPORT void DisableJvmForTesting();
61 // Initializes the global ClassLoader used by the GetClass and LazyGetClass
62 // methods. This is needed because JNI will use the base ClassLoader when there
63 // is no Java code on the stack. The base ClassLoader doesn't know about any of
64 // the application classes and will fail to lookup anything other than system
66 void InitGlobalClassLoader(JNIEnv* env);
68 // Finds the class named |class_name| and returns it.
69 // Use this method instead of invoking directly the JNI FindClass method (to
70 // prevent leaking local references).
71 // This method triggers a fatal assertion if the class could not be found.
72 // Use HasClass if you need to check whether the class exists.
73 BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env,
74 const char* class_name,
75 const char* split_name);
76 BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env,
77 const char* class_name);
79 // The method will initialize |atomic_class_id| to contain a global ref to the
80 // class. And will return that ref on subsequent calls. It's the caller's
81 // responsibility to release the ref when it is no longer needed.
82 // The caller is responsible to zero-initialize |atomic_method_id|.
83 // It's fine to simultaneously call this on multiple threads referencing the
84 // same |atomic_method_id|.
85 BASE_EXPORT jclass LazyGetClass(JNIEnv* env,
86 const char* class_name,
87 const char* split_name,
88 std::atomic<jclass>* atomic_class_id);
89 BASE_EXPORT jclass LazyGetClass(
91 const char* class_name,
92 std::atomic<jclass>* atomic_class_id);
94 // This class is a wrapper for JNIEnv Get(Static)MethodID.
95 class BASE_EXPORT MethodID {
102 // Returns the method ID for the method with the specified name and signature.
103 // This method triggers a fatal assertion if the method could not be found.
105 static jmethodID Get(JNIEnv* env,
107 const char* method_name,
108 const char* jni_signature);
110 // The caller is responsible to zero-initialize |atomic_method_id|.
111 // It's fine to simultaneously call this on multiple threads referencing the
112 // same |atomic_method_id|.
114 static jmethodID LazyGet(JNIEnv* env,
116 const char* method_name,
117 const char* jni_signature,
118 std::atomic<jmethodID>* atomic_method_id);
121 // Returns true if an exception is pending in the provided JNIEnv*.
122 BASE_EXPORT bool HasException(JNIEnv* env);
124 // If an exception is pending in the provided JNIEnv*, this function clears it
126 BASE_EXPORT bool ClearException(JNIEnv* env);
128 // This function will call CHECK() macro if there's any pending exception.
129 BASE_EXPORT void CheckException(JNIEnv* env);
131 // This returns a string representation of the java stack trace.
132 BASE_EXPORT std::string GetJavaExceptionInfo(
134 const JavaRef<jthrowable>& throwable);
135 // This returns a string representation of the java stack trace.
136 BASE_EXPORT std::string GetJavaStackTraceIfPresent();
138 } // namespace android
141 #endif // BASE_ANDROID_JNI_ANDROID_H_