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(JNIEnv* env,
45
const JavaRef<jobject>& context);
47
// Gets a global ref to the application context set with
48
// InitApplicationContext(). Ownership is retained by the function - the caller
49
// must NOT release it.
50
const BASE_EXPORT jobject GetApplicationContext();
52
// Finds the class named |class_name| and returns it.
53
// Use this method instead of invoking directly the JNI FindClass method (to
54
// prevent leaking local references).
55
// This method triggers a fatal assertion if the class could not be found.
56
// Use HasClass if you need to check whether the class exists.
57
BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env,
58
const char* class_name);
60
// This class is a wrapper for JNIEnv Get(Static)MethodID.
61
class BASE_EXPORT MethodID {
68
// Returns the method ID for the method with the specified name and signature.
69
// This method triggers a fatal assertion if the method could not be found.
71
static jmethodID Get(JNIEnv* env,
73
const char* method_name,
74
const char* jni_signature);
76
// The caller is responsible to zero-initialize |atomic_method_id|.
77
// It's fine to simultaneously call this on multiple threads referencing the
78
// same |atomic_method_id|.
80
static jmethodID LazyGet(JNIEnv* env,
82
const char* method_name,
83
const char* jni_signature,
84
base::subtle::AtomicWord* atomic_method_id);
87
// Returns true if an exception is pending in the provided JNIEnv*.
88
BASE_EXPORT bool HasException(JNIEnv* env);
90
// If an exception is pending in the provided JNIEnv*, this function clears it
92
BASE_EXPORT bool ClearException(JNIEnv* env);
94
// This function will call CHECK() macro if there's any pending exception.
95
BASE_EXPORT void CheckException(JNIEnv* env);
97
} // namespace android
100
#endif // BASE_ANDROID_JNI_ANDROID_H_