xref: /aosp_15_r20/external/libchrome/base/android/jni_android.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_ANDROID_JNI_ANDROID_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_ANDROID_JNI_ANDROID_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <jni.h>
9*635a8641SAndroid Build Coastguard Worker #include <sys/types.h>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <string>
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker #include "base/android/scoped_java_ref.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/atomicops.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/debug/debugging_buildflags.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/debug/stack_trace.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
22*635a8641SAndroid Build Coastguard Worker 
23*635a8641SAndroid Build Coastguard Worker // When profiling is enabled (enable_profiling=true) this macro is added to
24*635a8641SAndroid Build Coastguard Worker // all generated JNI stubs so that it becomes the last thing that runs before
25*635a8641SAndroid Build Coastguard Worker // control goes into Java.
26*635a8641SAndroid Build Coastguard Worker //
27*635a8641SAndroid Build Coastguard Worker // This macro saves stack frame pointer of the current function. Saved value
28*635a8641SAndroid Build Coastguard Worker // used later by JNI_LINK_SAVED_FRAME_POINTER.
29*635a8641SAndroid Build Coastguard Worker #define JNI_SAVE_FRAME_POINTER \
30*635a8641SAndroid Build Coastguard Worker   base::android::JNIStackFrameSaver jni_frame_saver(__builtin_frame_address(0))
31*635a8641SAndroid Build Coastguard Worker 
32*635a8641SAndroid Build Coastguard Worker // When profiling is enabled (enable_profiling=true) this macro is added to
33*635a8641SAndroid Build Coastguard Worker // all generated JNI callbacks so that it becomes the first thing that runs
34*635a8641SAndroid Build Coastguard Worker // after control returns from Java.
35*635a8641SAndroid Build Coastguard Worker //
36*635a8641SAndroid Build Coastguard Worker // This macro links stack frame of the current function to the stack frame
37*635a8641SAndroid Build Coastguard Worker // saved by JNI_SAVE_FRAME_POINTER, allowing frame-based unwinding
38*635a8641SAndroid Build Coastguard Worker // (used by the heap profiler) to produce complete traces.
39*635a8641SAndroid Build Coastguard Worker #define JNI_LINK_SAVED_FRAME_POINTER                    \
40*635a8641SAndroid Build Coastguard Worker   base::debug::ScopedStackFrameLinker jni_frame_linker( \
41*635a8641SAndroid Build Coastguard Worker       __builtin_frame_address(0),                       \
42*635a8641SAndroid Build Coastguard Worker       base::android::JNIStackFrameSaver::SavedFrame())
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker #else
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker // Frame-based stack unwinding is not supported, do nothing.
47*635a8641SAndroid Build Coastguard Worker #define JNI_SAVE_FRAME_POINTER
48*635a8641SAndroid Build Coastguard Worker #define JNI_LINK_SAVED_FRAME_POINTER
49*635a8641SAndroid Build Coastguard Worker 
50*635a8641SAndroid Build Coastguard Worker #endif  // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
51*635a8641SAndroid Build Coastguard Worker 
52*635a8641SAndroid Build Coastguard Worker namespace base {
53*635a8641SAndroid Build Coastguard Worker namespace android {
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker // Used to mark symbols to be exported in a shared library's symbol table.
56*635a8641SAndroid Build Coastguard Worker #define JNI_EXPORT __attribute__ ((visibility("default")))
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker // Contains the registration method information for initializing JNI bindings.
59*635a8641SAndroid Build Coastguard Worker struct RegistrationMethod {
60*635a8641SAndroid Build Coastguard Worker   const char* name;
61*635a8641SAndroid Build Coastguard Worker   bool (*func)(JNIEnv* env);
62*635a8641SAndroid Build Coastguard Worker };
63*635a8641SAndroid Build Coastguard Worker 
64*635a8641SAndroid Build Coastguard Worker // Attaches the current thread to the VM (if necessary) and return the JNIEnv*.
65*635a8641SAndroid Build Coastguard Worker BASE_EXPORT JNIEnv* AttachCurrentThread();
66*635a8641SAndroid Build Coastguard Worker 
67*635a8641SAndroid Build Coastguard Worker // Same to AttachCurrentThread except that thread name will be set to
68*635a8641SAndroid Build Coastguard Worker // |thread_name| if it is the first call. Otherwise, thread_name won't be
69*635a8641SAndroid Build Coastguard Worker // changed. AttachCurrentThread() doesn't regard underlying platform thread
70*635a8641SAndroid Build Coastguard Worker // name, but just resets it to "Thread-???". This function should be called
71*635a8641SAndroid Build Coastguard Worker // right after new thread is created if it is important to keep thread name.
72*635a8641SAndroid Build Coastguard Worker BASE_EXPORT JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name);
73*635a8641SAndroid Build Coastguard Worker 
74*635a8641SAndroid Build Coastguard Worker // Detaches the current thread from VM if it is attached.
75*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void DetachFromVM();
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker // Initializes the global JVM.
78*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void InitVM(JavaVM* vm);
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker // Returns true if the global JVM has been initialized.
81*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool IsVMInitialized();
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker // Initializes the global ClassLoader used by the GetClass and LazyGetClass
84*635a8641SAndroid Build Coastguard Worker // methods. This is needed because JNI will use the base ClassLoader when there
85*635a8641SAndroid Build Coastguard Worker // is no Java code on the stack. The base ClassLoader doesn't know about any of
86*635a8641SAndroid Build Coastguard Worker // the application classes and will fail to lookup anything other than system
87*635a8641SAndroid Build Coastguard Worker // classes.
88*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void InitReplacementClassLoader(
89*635a8641SAndroid Build Coastguard Worker     JNIEnv* env,
90*635a8641SAndroid Build Coastguard Worker     const JavaRef<jobject>& class_loader);
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker // Finds the class named |class_name| and returns it.
93*635a8641SAndroid Build Coastguard Worker // Use this method instead of invoking directly the JNI FindClass method (to
94*635a8641SAndroid Build Coastguard Worker // prevent leaking local references).
95*635a8641SAndroid Build Coastguard Worker // This method triggers a fatal assertion if the class could not be found.
96*635a8641SAndroid Build Coastguard Worker // Use HasClass if you need to check whether the class exists.
97*635a8641SAndroid Build Coastguard Worker BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env,
98*635a8641SAndroid Build Coastguard Worker                                                 const char* class_name);
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker // The method will initialize |atomic_class_id| to contain a global ref to the
101*635a8641SAndroid Build Coastguard Worker // class. And will return that ref on subsequent calls.  It's the caller's
102*635a8641SAndroid Build Coastguard Worker // responsibility to release the ref when it is no longer needed.
103*635a8641SAndroid Build Coastguard Worker // The caller is responsible to zero-initialize |atomic_method_id|.
104*635a8641SAndroid Build Coastguard Worker // It's fine to simultaneously call this on multiple threads referencing the
105*635a8641SAndroid Build Coastguard Worker // same |atomic_method_id|.
106*635a8641SAndroid Build Coastguard Worker BASE_EXPORT jclass LazyGetClass(
107*635a8641SAndroid Build Coastguard Worker     JNIEnv* env,
108*635a8641SAndroid Build Coastguard Worker     const char* class_name,
109*635a8641SAndroid Build Coastguard Worker     base::subtle::AtomicWord* atomic_class_id);
110*635a8641SAndroid Build Coastguard Worker 
111*635a8641SAndroid Build Coastguard Worker // This class is a wrapper for JNIEnv Get(Static)MethodID.
112*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT MethodID {
113*635a8641SAndroid Build Coastguard Worker  public:
114*635a8641SAndroid Build Coastguard Worker   enum Type {
115*635a8641SAndroid Build Coastguard Worker     TYPE_STATIC,
116*635a8641SAndroid Build Coastguard Worker     TYPE_INSTANCE,
117*635a8641SAndroid Build Coastguard Worker   };
118*635a8641SAndroid Build Coastguard Worker 
119*635a8641SAndroid Build Coastguard Worker   // Returns the method ID for the method with the specified name and signature.
120*635a8641SAndroid Build Coastguard Worker   // This method triggers a fatal assertion if the method could not be found.
121*635a8641SAndroid Build Coastguard Worker   template<Type type>
122*635a8641SAndroid Build Coastguard Worker   static jmethodID Get(JNIEnv* env,
123*635a8641SAndroid Build Coastguard Worker                        jclass clazz,
124*635a8641SAndroid Build Coastguard Worker                        const char* method_name,
125*635a8641SAndroid Build Coastguard Worker                        const char* jni_signature);
126*635a8641SAndroid Build Coastguard Worker 
127*635a8641SAndroid Build Coastguard Worker   // The caller is responsible to zero-initialize |atomic_method_id|.
128*635a8641SAndroid Build Coastguard Worker   // It's fine to simultaneously call this on multiple threads referencing the
129*635a8641SAndroid Build Coastguard Worker   // same |atomic_method_id|.
130*635a8641SAndroid Build Coastguard Worker   template<Type type>
131*635a8641SAndroid Build Coastguard Worker   static jmethodID LazyGet(JNIEnv* env,
132*635a8641SAndroid Build Coastguard Worker                            jclass clazz,
133*635a8641SAndroid Build Coastguard Worker                            const char* method_name,
134*635a8641SAndroid Build Coastguard Worker                            const char* jni_signature,
135*635a8641SAndroid Build Coastguard Worker                            base::subtle::AtomicWord* atomic_method_id);
136*635a8641SAndroid Build Coastguard Worker };
137*635a8641SAndroid Build Coastguard Worker 
138*635a8641SAndroid Build Coastguard Worker // Returns true if an exception is pending in the provided JNIEnv*.
139*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool HasException(JNIEnv* env);
140*635a8641SAndroid Build Coastguard Worker 
141*635a8641SAndroid Build Coastguard Worker // If an exception is pending in the provided JNIEnv*, this function clears it
142*635a8641SAndroid Build Coastguard Worker // and returns true.
143*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool ClearException(JNIEnv* env);
144*635a8641SAndroid Build Coastguard Worker 
145*635a8641SAndroid Build Coastguard Worker // This function will call CHECK() macro if there's any pending exception.
146*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void CheckException(JNIEnv* env);
147*635a8641SAndroid Build Coastguard Worker 
148*635a8641SAndroid Build Coastguard Worker // This returns a string representation of the java stack trace.
149*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string GetJavaExceptionInfo(JNIEnv* env,
150*635a8641SAndroid Build Coastguard Worker                                              jthrowable java_throwable);
151*635a8641SAndroid Build Coastguard Worker 
152*635a8641SAndroid Build Coastguard Worker #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
153*635a8641SAndroid Build Coastguard Worker 
154*635a8641SAndroid Build Coastguard Worker // Saves caller's PC and stack frame in a thread-local variable.
155*635a8641SAndroid Build Coastguard Worker // Implemented only when profiling is enabled (enable_profiling=true).
156*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT JNIStackFrameSaver {
157*635a8641SAndroid Build Coastguard Worker  public:
158*635a8641SAndroid Build Coastguard Worker   JNIStackFrameSaver(void* current_fp);
159*635a8641SAndroid Build Coastguard Worker   ~JNIStackFrameSaver();
160*635a8641SAndroid Build Coastguard Worker   static void* SavedFrame();
161*635a8641SAndroid Build Coastguard Worker 
162*635a8641SAndroid Build Coastguard Worker  private:
163*635a8641SAndroid Build Coastguard Worker   void* previous_fp_;
164*635a8641SAndroid Build Coastguard Worker 
165*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(JNIStackFrameSaver);
166*635a8641SAndroid Build Coastguard Worker };
167*635a8641SAndroid Build Coastguard Worker 
168*635a8641SAndroid Build Coastguard Worker #endif  // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
169*635a8641SAndroid Build Coastguard Worker 
170*635a8641SAndroid Build Coastguard Worker }  // namespace android
171*635a8641SAndroid Build Coastguard Worker }  // namespace base
172*635a8641SAndroid Build Coastguard Worker 
173*635a8641SAndroid Build Coastguard Worker #endif  // BASE_ANDROID_JNI_ANDROID_H_
174