faf53b7400bbdecc305db289f470f05f70ab6251
[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 <string>
12
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"
17
18 namespace base {
19 namespace android {
20
21 // Used to mark symbols to be exported in a shared library's symbol table.
22 #define JNI_EXPORT __attribute__ ((visibility("default")))
23
24 // Contains the registration method information for initializing JNI bindings.
25 struct RegistrationMethod {
26   const char* name;
27   bool (*func)(JNIEnv* env);
28 };
29
30 // Attaches the current thread to the VM (if necessary) and return the JNIEnv*.
31 BASE_EXPORT JNIEnv* AttachCurrentThread();
32
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);
39
40 // Detaches the current thread from VM if it is attached.
41 BASE_EXPORT void DetachFromVM();
42
43 // Initializes the global JVM. It is not necessarily called before
44 // InitApplicationContext().
45 BASE_EXPORT void InitVM(JavaVM* vm);
46
47 // Returns true if the global JVM has been initialized.
48 BASE_EXPORT bool IsVMInitialized();
49
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);
55
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();
60
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);
68
69 // This class is a wrapper for JNIEnv Get(Static)MethodID.
70 class BASE_EXPORT MethodID {
71  public:
72   enum Type {
73     TYPE_STATIC,
74     TYPE_INSTANCE,
75   };
76
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.
79   template<Type type>
80   static jmethodID Get(JNIEnv* env,
81                        jclass clazz,
82                        const char* method_name,
83                        const char* jni_signature);
84
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|.
88   template<Type type>
89   static jmethodID LazyGet(JNIEnv* env,
90                            jclass clazz,
91                            const char* method_name,
92                            const char* jni_signature,
93                            base::subtle::AtomicWord* atomic_method_id);
94 };
95
96 // Returns true if an exception is pending in the provided JNIEnv*.
97 BASE_EXPORT bool HasException(JNIEnv* env);
98
99 // If an exception is pending in the provided JNIEnv*, this function clears it
100 // and returns true.
101 BASE_EXPORT bool ClearException(JNIEnv* env);
102
103 // This function will call CHECK() macro if there's any pending exception.
104 BASE_EXPORT void CheckException(JNIEnv* env);
105
106 }  // namespace android
107 }  // namespace base
108
109 #endif  // BASE_ANDROID_JNI_ANDROID_H_