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_
11 #include "base/android/scoped_java_ref.h"
12 #include "base/atomicops.h"
13 #include "base/base_export.h"
14 #include "base/compiler_specific.h"
19 // Used to mark symbols to be exported in a shared library's symbol table.
20 #define JNI_EXPORT __attribute__ ((visibility("default")))
22 // Contains the registration method information for initializing JNI bindings.
23 struct RegistrationMethod
{
25 bool (*func
)(JNIEnv
* env
);
28 // Attach the current thread to the VM (if necessary) and return the JNIEnv*.
29 BASE_EXPORT JNIEnv
* AttachCurrentThread();
31 // Detach the current thread from VM if it is attached.
32 BASE_EXPORT
void DetachFromVM();
34 // Initializes the global JVM. It is not necessarily called before
35 // InitApplicationContext().
36 BASE_EXPORT
void InitVM(JavaVM
* vm
);
38 // Returns true if the global JVM has been initialized.
39 BASE_EXPORT
bool IsVMInitialized();
41 // Initializes the global application context object. The |context| can be any
42 // valid reference to the application context. Internally holds a global ref to
43 // the context. InitVM and InitApplicationContext maybe called in either order.
44 BASE_EXPORT
void InitApplicationContext(const JavaRef
<jobject
>& context
);
46 // Gets a global ref to the application context set with
47 // InitApplicationContext(). Ownership is retained by the function - the caller
48 // must NOT release it.
49 const BASE_EXPORT jobject
GetApplicationContext();
51 // Finds the class named |class_name| and returns it.
52 // Use this method instead of invoking directly the JNI FindClass method (to
53 // prevent leaking local references).
54 // This method triggers a fatal assertion if the class could not be found.
55 // Use HasClass if you need to check whether the class exists.
56 BASE_EXPORT ScopedJavaLocalRef
<jclass
> GetClass(JNIEnv
* env
,
57 const char* class_name
);
59 // This class is a wrapper for JNIEnv Get(Static)MethodID.
60 class BASE_EXPORT MethodID
{
67 // Returns the method ID for the method with the specified name and signature.
68 // This method triggers a fatal assertion if the method could not be found.
70 static jmethodID
Get(JNIEnv
* env
,
72 const char* method_name
,
73 const char* jni_signature
);
75 // The caller is responsible to zero-initialize |atomic_method_id|.
76 // It's fine to simultaneously call this on multiple threads referencing the
77 // same |atomic_method_id|.
79 static jmethodID
LazyGet(JNIEnv
* env
,
81 const char* method_name
,
82 const char* jni_signature
,
83 base::subtle::AtomicWord
* atomic_method_id
);
86 // Returns true if an exception is pending in the provided JNIEnv*.
87 BASE_EXPORT
bool HasException(JNIEnv
* env
);
89 // If an exception is pending in the provided JNIEnv*, this function clears it
91 BASE_EXPORT
bool ClearException(JNIEnv
* env
);
93 // This function will call CHECK() macro if there's any pending exception.
94 BASE_EXPORT
void CheckException(JNIEnv
* env
);
96 } // namespace android
99 #endif // BASE_ANDROID_JNI_ANDROID_H_