|
1 /* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include <android/log.h> |
|
7 #include <dlfcn.h> |
|
8 #include <prthread.h> |
|
9 |
|
10 #include "mozilla/DebugOnly.h" |
|
11 #include "mozilla/Assertions.h" |
|
12 #include "nsThreadUtils.h" |
|
13 #include "AndroidBridge.h" |
|
14 |
|
15 extern "C" { |
|
16 jclass __jsjni_GetGlobalClassRef(const char *className); |
|
17 } |
|
18 |
|
19 class GetGlobalClassRefRunnable : public nsRunnable { |
|
20 public: |
|
21 GetGlobalClassRefRunnable(const char *className, jclass *foundClass) : |
|
22 mClassName(className), mResult(foundClass) {} |
|
23 NS_IMETHOD Run() { |
|
24 *mResult = __jsjni_GetGlobalClassRef(mClassName); |
|
25 return NS_OK; |
|
26 } |
|
27 private: |
|
28 const char *mClassName; |
|
29 jclass *mResult; |
|
30 }; |
|
31 |
|
32 extern "C" { |
|
33 __attribute__ ((visibility("default"))) |
|
34 jclass |
|
35 jsjni_FindClass(const char *className) { |
|
36 // FindClass outside the main thread will run into problems due |
|
37 // to missing the classpath |
|
38 MOZ_ASSERT(NS_IsMainThread()); |
|
39 JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); |
|
40 return env->FindClass(className); |
|
41 } |
|
42 |
|
43 jclass |
|
44 __jsjni_GetGlobalClassRef(const char *className) { |
|
45 // root class globally |
|
46 JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); |
|
47 jclass globalRef = static_cast<jclass>(env->NewGlobalRef(env->FindClass(className))); |
|
48 if (!globalRef) |
|
49 return nullptr; |
|
50 |
|
51 // return the newly create global reference |
|
52 return globalRef; |
|
53 } |
|
54 |
|
55 __attribute__ ((visibility("default"))) |
|
56 jclass |
|
57 jsjni_GetGlobalClassRef(const char *className) { |
|
58 nsCOMPtr<nsIThread> mainThread; |
|
59 mozilla::DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(mainThread)); |
|
60 MOZ_ASSERT(NS_SUCCEEDED(rv)); |
|
61 |
|
62 jclass foundClass; |
|
63 nsRefPtr<nsIRunnable> runnable_ref(new GetGlobalClassRefRunnable(className, |
|
64 &foundClass)); |
|
65 mainThread->Dispatch(runnable_ref, NS_DISPATCH_SYNC); |
|
66 if (!foundClass) |
|
67 return nullptr; |
|
68 |
|
69 return foundClass; |
|
70 } |
|
71 |
|
72 __attribute__ ((visibility("default"))) |
|
73 jmethodID |
|
74 jsjni_GetStaticMethodID(jclass methodClass, |
|
75 const char *methodName, |
|
76 const char *signature) { |
|
77 JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); |
|
78 return env->GetStaticMethodID(methodClass, methodName, signature); |
|
79 } |
|
80 |
|
81 __attribute__ ((visibility("default"))) |
|
82 bool |
|
83 jsjni_ExceptionCheck() { |
|
84 JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); |
|
85 return env->ExceptionCheck(); |
|
86 } |
|
87 |
|
88 __attribute__ ((visibility("default"))) |
|
89 void |
|
90 jsjni_CallStaticVoidMethodA(jclass cls, |
|
91 jmethodID method, |
|
92 jvalue *values) { |
|
93 JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); |
|
94 |
|
95 mozilla::AutoLocalJNIFrame jniFrame(env); |
|
96 env->CallStaticVoidMethodA(cls, method, values); |
|
97 } |
|
98 |
|
99 __attribute__ ((visibility("default"))) |
|
100 int |
|
101 jsjni_CallStaticIntMethodA(jclass cls, |
|
102 jmethodID method, |
|
103 jvalue *values) { |
|
104 JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); |
|
105 |
|
106 mozilla::AutoLocalJNIFrame jniFrame(env); |
|
107 return env->CallStaticIntMethodA(cls, method, values); |
|
108 } |
|
109 |
|
110 __attribute__ ((visibility("default"))) |
|
111 jobject jsjni_GetGlobalContextRef() { |
|
112 return mozilla::AndroidBridge::Bridge()->GetGlobalContextRef(); |
|
113 } |
|
114 |
|
115 __attribute__ ((visibility("default"))) |
|
116 JavaVM* jsjni_GetVM() { |
|
117 return mozilla::AndroidBridge::GetVM(); |
|
118 } |
|
119 |
|
120 __attribute__ ((visibility("default"))) |
|
121 JNIEnv* jsjni_GetJNIForThread() { |
|
122 return GetJNIForThread(); |
|
123 } |
|
124 } |