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