1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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_
13 #include "base/android/scoped_java_ref.h"
14 #include "base/atomicops.h"
15 #include "base/base_export.h"
16 #include "base/compiler_specific.h"
21 // Used to mark symbols to be exported in a shared library's symbol table.
22 #define JNI_EXPORT __attribute__ ((visibility("default")))
24 // Contains the registration method information for initializing JNI bindings.
25 struct RegistrationMethod
{
27 bool (*func
)(JNIEnv
* env
);
30 // Attaches the current thread to the VM (if necessary) and return the JNIEnv*.
31 BASE_EXPORT JNIEnv
* AttachCurrentThread();
33 // Same to AttachCurrentThread except that thread name will be set to
34 // |thread_name| if it is the first call. Otherwise, thread_name won't be
35 // changed. AttachCurrentThread() doesn't regard underlying platform thread
36 // name, but just resets it to "Thread-???". This function should be called
37 // right after new thread is created if it is important to keep thread name.
38 BASE_EXPORT JNIEnv
* AttachCurrentThreadWithName(const std::string
& thread_name
);
40 // Detaches the current thread from VM if it is attached.
41 BASE_EXPORT
void DetachFromVM();
43 // Initializes the global JVM. It is not necessarily called before
44 // InitApplicationContext().
45 BASE_EXPORT
void InitVM(JavaVM
* vm
);
47 // Returns true if the global JVM has been initialized.
48 BASE_EXPORT
bool IsVMInitialized();
50 // Initializes the global application context object. The |context| can be any
51 // valid reference to the application context. Internally holds a global ref to
52 // the context. InitVM and InitApplicationContext maybe called in either order.
53 BASE_EXPORT
void InitApplicationContext(JNIEnv
* env
,
54 const JavaRef
<jobject
>& context
);
56 // Gets a global ref to the application context set with
57 // InitApplicationContext(). Ownership is retained by the function - the caller
58 // must NOT release it.
59 const BASE_EXPORT jobject
GetApplicationContext();
61 // Finds the class named |class_name| and returns it.
62 // Use this method instead of invoking directly the JNI FindClass method (to
63 // prevent leaking local references).
64 // This method triggers a fatal assertion if the class could not be found.
65 // Use HasClass if you need to check whether the class exists.
66 BASE_EXPORT ScopedJavaLocalRef
<jclass
> GetClass(JNIEnv
* env
,
67 const char* class_name
);
69 // This class is a wrapper for JNIEnv Get(Static)MethodID.
70 class BASE_EXPORT MethodID
{
77 // Returns the method ID for the method with the specified name and signature.
78 // This method triggers a fatal assertion if the method could not be found.
80 static jmethodID
Get(JNIEnv
* env
,
82 const char* method_name
,
83 const char* jni_signature
);
85 // The caller is responsible to zero-initialize |atomic_method_id|.
86 // It's fine to simultaneously call this on multiple threads referencing the
87 // same |atomic_method_id|.
89 static jmethodID
LazyGet(JNIEnv
* env
,
91 const char* method_name
,
92 const char* jni_signature
,
93 base::subtle::AtomicWord
* atomic_method_id
);
96 // Returns true if an exception is pending in the provided JNIEnv*.
97 BASE_EXPORT
bool HasException(JNIEnv
* env
);
99 // If an exception is pending in the provided JNIEnv*, this function clears it
101 BASE_EXPORT
bool ClearException(JNIEnv
* env
);
103 // This function will call CHECK() macro if there's any pending exception.
104 BASE_EXPORT
void CheckException(JNIEnv
* env
);
106 } // namespace android
109 #endif // BASE_ANDROID_JNI_ANDROID_H_