• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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 JNI_ZERO_JNI_METHODS_H_
6 #define JNI_ZERO_JNI_METHODS_H_
7 
8 #include <jni.h>
9 
10 #include <atomic>
11 #include <string>
12 
13 #include "third_party/jni_zero/java_refs.h"
14 #include "third_party/jni_zero/jni_export.h"
15 
16 namespace jni_zero {
17 // Attaches the current thread to the VM (if necessary) and return the JNIEnv*.
18 JNI_ZERO_COMPONENT_BUILD_EXPORT JNIEnv* AttachCurrentThread();
19 
20 // Same to AttachCurrentThread except that thread name will be set to
21 // |thread_name| if it is the first call. Otherwise, thread_name won't be
22 // changed. AttachCurrentThread() doesn't regard underlying platform thread
23 // name, but just resets it to "Thread-???". This function should be called
24 // right after new thread is created if it is important to keep thread name.
25 JNI_ZERO_COMPONENT_BUILD_EXPORT JNIEnv* AttachCurrentThreadWithName(
26     const std::string& thread_name);
27 
28 // Detaches the current thread from VM if it is attached.
29 JNI_ZERO_COMPONENT_BUILD_EXPORT void DetachFromVM();
30 
31 // Initializes the global JVM.
32 JNI_ZERO_COMPONENT_BUILD_EXPORT void InitVM(JavaVM* vm);
33 
34 // Returns true if the global JVM has been initialized.
35 JNI_ZERO_COMPONENT_BUILD_EXPORT bool IsVMInitialized();
36 
37 // Returns the global JVM, or nullptr if it has not been initialized.
38 JNI_ZERO_COMPONENT_BUILD_EXPORT JavaVM* GetVM();
39 
40 // Do not allow any future native->java calls.
41 // This is necessary in gtest DEATH_TESTS to prevent
42 // GetJavaStackTraceIfPresent() from accessing a defunct JVM (due to fork()).
43 // https://crbug.com/1484834
44 JNI_ZERO_COMPONENT_BUILD_EXPORT void DisableJvmForTesting();
45 
46 JNI_ZERO_COMPONENT_BUILD_EXPORT void SetExceptionHandler(
47     void (*callback)(JNIEnv*));
48 
49 // Returns true if an exception is pending in the provided JNIEnv*.
50 JNI_ZERO_COMPONENT_BUILD_EXPORT bool HasException(JNIEnv* env);
51 
52 // If an exception is pending in the provided JNIEnv*, this function clears it
53 // and returns true.
54 JNI_ZERO_COMPONENT_BUILD_EXPORT bool ClearException(JNIEnv* env);
55 
56 // If there's any pending exception, this function will call the set exception
57 // handler, or if none are set, it will fatally LOG.
58 JNI_ZERO_COMPONENT_BUILD_EXPORT void CheckException(JNIEnv* env);
59 
60 // Sets a function to call instead of just using JNIEnv.FindClass. Useful for
61 // chrome's "splits" which need to be resolved in special ClassLoaders. The
62 // class name parameter (first string) will be given in package.dot.Format. The
63 // second parameter is the split name, which will just be an empty string if not
64 // used.
65 JNI_ZERO_COMPONENT_BUILD_EXPORT void SetClassResolver(
66     jclass (*resolver)(JNIEnv*, const char*, const char*));
67 
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 JNI_ZERO_COMPONENT_BUILD_EXPORT ScopedJavaLocalRef<jclass>
74 GetClass(JNIEnv* env, const char* class_name, const char* split_name);
75 JNI_ZERO_COMPONENT_BUILD_EXPORT ScopedJavaLocalRef<jclass> GetClass(
76     JNIEnv* env,
77     const char* class_name);
78 
79 // This class is a wrapper for JNIEnv Get(Static)MethodID.
80 class JNI_ZERO_COMPONENT_BUILD_EXPORT MethodID {
81  public:
82   enum Type {
83     TYPE_STATIC,
84     TYPE_INSTANCE,
85   };
86 
87   // Returns the method ID for the method with the specified name and signature.
88   // This method triggers a fatal assertion if the method could not be found.
89   template <Type type>
90   static jmethodID Get(JNIEnv* env,
91                        jclass clazz,
92                        const char* method_name,
93                        const char* jni_signature);
94 
95   // The caller is responsible to zero-initialize |atomic_method_id|.
96   // It's fine to simultaneously call this on multiple threads referencing the
97   // same |atomic_method_id|.
98   template <Type type>
99   static jmethodID LazyGet(JNIEnv* env,
100                            jclass clazz,
101                            const char* method_name,
102                            const char* jni_signature,
103                            std::atomic<jmethodID>* atomic_method_id);
104 };
105 }  // namespace jni_zero
106 
107 #endif  // JNI_ZERO_JNI_METHODS_H
108