xref: /aosp_15_r20/external/cronet/base/android/jni_android.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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.
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 <atomic>
12 #include <string>
13 
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"
20 #include "third_party/jni_zero/jni_zero.h"
21 
22 namespace base {
23 namespace android {
24 
25 // Used to mark symbols to be exported in a shared library's symbol table.
26 #define JNI_EXPORT __attribute__ ((visibility("default")))
27 
28 // Contains the registration method information for initializing JNI bindings.
29 struct RegistrationMethod {
30   const char* name;
31   bool (*func)(JNIEnv* env);
32 };
33 
34 using LogFatalCallback = void (*)(const char* message);
35 
36 BASE_EXPORT extern LogFatalCallback g_log_fatal_callback_for_testing;
37 BASE_EXPORT extern const char kUnableToGetStackTraceMessage[];
38 BASE_EXPORT extern const char kReetrantOutOfMemoryMessage[];
39 BASE_EXPORT extern const char kReetrantExceptionMessage[];
40 BASE_EXPORT extern const char kUncaughtExceptionMessage[];
41 BASE_EXPORT extern const char kUncaughtExceptionHandlerFailedMessage[];
42 BASE_EXPORT extern const char kOomInGetJavaExceptionInfoMessage[];
43 
44 // Attaches the current thread to the VM (if necessary) and return the JNIEnv*.
AttachCurrentThread()45 inline JNIEnv* AttachCurrentThread() {
46   return jni_zero::AttachCurrentThread();
47 }
48 
49 // Same to AttachCurrentThread except that thread name will be set to
50 // |thread_name| if it is the first call. Otherwise, thread_name won't be
51 // changed. AttachCurrentThread() doesn't regard underlying platform thread
52 // name, but just resets it to "Thread-???". This function should be called
53 // right after new thread is created if it is important to keep thread name.
AttachCurrentThreadWithName(const std::string & thread_name)54 inline JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name) {
55   return jni_zero::AttachCurrentThreadWithName(thread_name);
56 }
57 
58 // Detaches the current thread from VM if it is attached.
DetachFromVM()59 inline void DetachFromVM() {
60   jni_zero::DetachFromVM();
61 }
62 
63 // Initializes the global JVM.
64 BASE_EXPORT void InitVM(JavaVM* vm);
65 
66 // Returns true if the global JVM has been initialized.
IsVMInitialized()67 inline bool IsVMInitialized() {
68   return jni_zero::IsVMInitialized();
69 }
70 
71 // Returns the global JVM, or nullptr if it has not been initialized.
GetVM()72 inline JavaVM* GetVM() {
73   return jni_zero::GetVM();
74 }
75 
76 // Do not allow any future native->java calls.
77 // This is necessary in gtest DEATH_TESTS to prevent
78 // GetJavaStackTraceIfPresent() from accessing a defunct JVM (due to fork()).
79 // https://crbug.com/1484834
DisableJvmForTesting()80 inline void DisableJvmForTesting() {
81   return jni_zero::DisableJvmForTesting();
82 }
83 
84 // Finds the class named |class_name| and returns it.
85 // Use this method instead of invoking directly the JNI FindClass method (to
86 // prevent leaking local references).
87 // This method triggers a fatal assertion if the class could not be found.
88 // Use HasClass if you need to check whether the class exists.
GetClass(JNIEnv * env,const char * class_name)89 inline ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env,
90                                            const char* class_name) {
91   return jni_zero::GetClass(env, class_name);
92 }
93 
94 
95 // Returns true if an exception is pending in the provided JNIEnv*.
HasException(JNIEnv * env)96 inline bool HasException(JNIEnv* env) {
97   return jni_zero::HasException(env);
98 }
99 
100 // If an exception is pending in the provided JNIEnv*, this function clears it
101 // and returns true.
ClearException(JNIEnv * env)102 inline bool ClearException(JNIEnv* env) {
103   return jni_zero::ClearException(env);
104 }
105 
106 // This function will call CHECK() macro if there's any pending exception.
107 BASE_EXPORT void CheckException(JNIEnv* env);
108 
109 // This returns a string representation of the java stack trace.
110 BASE_EXPORT std::string GetJavaExceptionInfo(
111     JNIEnv* env,
112     const JavaRef<jthrowable>& throwable);
113 // This returns a string representation of the java stack trace.
114 BASE_EXPORT std::string GetJavaStackTraceIfPresent();
115 
116 using MethodID = jni_zero::MethodID;
117 }  // namespace android
118 }  // namespace base
119 
120 #endif  // BASE_ANDROID_JNI_ANDROID_H_
121