- add sources.
[platform/framework/web/crosswalk.git] / src / base / android / jni_android.h
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
16 namespace base {
17 namespace 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.
23 struct 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*.
29 BASE_EXPORT JNIEnv* AttachCurrentThread();
30
31 // Detach the current thread from VM if it is attached.
32 BASE_EXPORT void DetachFromVM();
33
34 // Initializes the global JVM. It is not necessarily called before
35 // InitApplicationContext().
36 BASE_EXPORT void InitVM(JavaVM* vm);
37
38 // Returns true if the global JVM has been initialized.
39 BASE_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.
44 BASE_EXPORT void InitApplicationContext(const JavaRef<jobject>& context);
45
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();
50
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);
58
59 // This class is a wrapper for JNIEnv Get(Static)MethodID.
60 class BASE_EXPORT MethodID {
61  public:
62   enum Type {
63     TYPE_STATIC,
64     TYPE_INSTANCE,
65   };
66
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.
69   template<Type type>
70   static jmethodID Get(JNIEnv* env,
71                        jclass clazz,
72                        const char* method_name,
73                        const char* jni_signature);
74
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|.
78   template<Type type>
79   static jmethodID LazyGet(JNIEnv* env,
80                            jclass clazz,
81                            const char* method_name,
82                            const char* jni_signature,
83                            base::subtle::AtomicWord* atomic_method_id);
84 };
85
86 // Returns true if an exception is pending in the provided JNIEnv*.
87 BASE_EXPORT bool HasException(JNIEnv* env);
88
89 // If an exception is pending in the provided JNIEnv*, this function clears it
90 // and returns true.
91 BASE_EXPORT bool ClearException(JNIEnv* env);
92
93 // This function will call CHECK() macro if there's any pending exception.
94 BASE_EXPORT void CheckException(JNIEnv* env);
95
96 }  // namespace android
97 }  // namespace base
98
99 #endif  // BASE_ANDROID_JNI_ANDROID_H_