widget/android/AndroidJNIWrapper.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial