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.
4
5#ifndef BASE_ANDROID_JNI_ANDROID_H_
6#define BASE_ANDROID_JNI_ANDROID_H_
7
8#include <jni.h>
9#include <sys/types.h>
10
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"
15
16namespace base {
17namespace android {
18
19// Used to mark symbols to be exported in a shared library's symbol table.
20#define JNI_EXPORT __attribute__ ((visibility("default")))
21
22// Contains the registration method information for initializing JNI bindings.
23struct RegistrationMethod {
24  const char* name;
25  bool (*func)(JNIEnv* env);
26};
27
28// Attach the current thread to the VM (if necessary) and return the JNIEnv*.
29BASE_EXPORT JNIEnv* AttachCurrentThread();
30
31// Detach the current thread from VM if it is attached.
32BASE_EXPORT void DetachFromVM();
33
34// Initializes the global JVM. It is not necessarily called before
35// InitApplicationContext().
36BASE_EXPORT void InitVM(JavaVM* vm);
37
38// Returns true if the global JVM has been initialized.
39BASE_EXPORT bool IsVMInitialized();
40
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.
44BASE_EXPORT void InitApplicationContext(JNIEnv* env,
45                                        const JavaRef<jobject>& context);
46
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.
50const BASE_EXPORT jobject GetApplicationContext();
51
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.
57BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env,
58                                                const char* class_name);
59
60// This class is a wrapper for JNIEnv Get(Static)MethodID.
61class BASE_EXPORT MethodID {
62 public:
63  enum Type {
64    TYPE_STATIC,
65    TYPE_INSTANCE,
66  };
67
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.
70  template<Type type>
71  static jmethodID Get(JNIEnv* env,
72                       jclass clazz,
73                       const char* method_name,
74                       const char* jni_signature);
75
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|.
79  template<Type type>
80  static jmethodID LazyGet(JNIEnv* env,
81                           jclass clazz,
82                           const char* method_name,
83                           const char* jni_signature,
84                           base::subtle::AtomicWord* atomic_method_id);
85};
86
87// Returns true if an exception is pending in the provided JNIEnv*.
88BASE_EXPORT bool HasException(JNIEnv* env);
89
90// If an exception is pending in the provided JNIEnv*, this function clears it
91// and returns true.
92BASE_EXPORT bool ClearException(JNIEnv* env);
93
94// This function will call CHECK() macro if there's any pending exception.
95BASE_EXPORT void CheckException(JNIEnv* env);
96
97}  // namespace android
98}  // namespace base
99
100#endif  // BASE_ANDROID_JNI_ANDROID_H_
101