1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/android/AndroidJNIWrapper.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include <android/log.h> 1.10 +#include <dlfcn.h> 1.11 +#include <prthread.h> 1.12 + 1.13 +#include "mozilla/DebugOnly.h" 1.14 +#include "mozilla/Assertions.h" 1.15 +#include "nsThreadUtils.h" 1.16 +#include "AndroidBridge.h" 1.17 + 1.18 +extern "C" { 1.19 + jclass __jsjni_GetGlobalClassRef(const char *className); 1.20 +} 1.21 + 1.22 +class GetGlobalClassRefRunnable : public nsRunnable { 1.23 + public: 1.24 + GetGlobalClassRefRunnable(const char *className, jclass *foundClass) : 1.25 + mClassName(className), mResult(foundClass) {} 1.26 + NS_IMETHOD Run() { 1.27 + *mResult = __jsjni_GetGlobalClassRef(mClassName); 1.28 + return NS_OK; 1.29 + } 1.30 + private: 1.31 + const char *mClassName; 1.32 + jclass *mResult; 1.33 +}; 1.34 + 1.35 +extern "C" { 1.36 + __attribute__ ((visibility("default"))) 1.37 + jclass 1.38 + jsjni_FindClass(const char *className) { 1.39 + // FindClass outside the main thread will run into problems due 1.40 + // to missing the classpath 1.41 + MOZ_ASSERT(NS_IsMainThread()); 1.42 + JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); 1.43 + return env->FindClass(className); 1.44 + } 1.45 + 1.46 + jclass 1.47 + __jsjni_GetGlobalClassRef(const char *className) { 1.48 + // root class globally 1.49 + JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); 1.50 + jclass globalRef = static_cast<jclass>(env->NewGlobalRef(env->FindClass(className))); 1.51 + if (!globalRef) 1.52 + return nullptr; 1.53 + 1.54 + // return the newly create global reference 1.55 + return globalRef; 1.56 + } 1.57 + 1.58 + __attribute__ ((visibility("default"))) 1.59 + jclass 1.60 + jsjni_GetGlobalClassRef(const char *className) { 1.61 + nsCOMPtr<nsIThread> mainThread; 1.62 + mozilla::DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(mainThread)); 1.63 + MOZ_ASSERT(NS_SUCCEEDED(rv)); 1.64 + 1.65 + jclass foundClass; 1.66 + nsRefPtr<nsIRunnable> runnable_ref(new GetGlobalClassRefRunnable(className, 1.67 + &foundClass)); 1.68 + mainThread->Dispatch(runnable_ref, NS_DISPATCH_SYNC); 1.69 + if (!foundClass) 1.70 + return nullptr; 1.71 + 1.72 + return foundClass; 1.73 + } 1.74 + 1.75 + __attribute__ ((visibility("default"))) 1.76 + jmethodID 1.77 + jsjni_GetStaticMethodID(jclass methodClass, 1.78 + const char *methodName, 1.79 + const char *signature) { 1.80 + JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); 1.81 + return env->GetStaticMethodID(methodClass, methodName, signature); 1.82 + } 1.83 + 1.84 + __attribute__ ((visibility("default"))) 1.85 + bool 1.86 + jsjni_ExceptionCheck() { 1.87 + JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); 1.88 + return env->ExceptionCheck(); 1.89 + } 1.90 + 1.91 + __attribute__ ((visibility("default"))) 1.92 + void 1.93 + jsjni_CallStaticVoidMethodA(jclass cls, 1.94 + jmethodID method, 1.95 + jvalue *values) { 1.96 + JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); 1.97 + 1.98 + mozilla::AutoLocalJNIFrame jniFrame(env); 1.99 + env->CallStaticVoidMethodA(cls, method, values); 1.100 + } 1.101 + 1.102 + __attribute__ ((visibility("default"))) 1.103 + int 1.104 + jsjni_CallStaticIntMethodA(jclass cls, 1.105 + jmethodID method, 1.106 + jvalue *values) { 1.107 + JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); 1.108 + 1.109 + mozilla::AutoLocalJNIFrame jniFrame(env); 1.110 + return env->CallStaticIntMethodA(cls, method, values); 1.111 + } 1.112 + 1.113 + __attribute__ ((visibility("default"))) 1.114 + jobject jsjni_GetGlobalContextRef() { 1.115 + return mozilla::AndroidBridge::Bridge()->GetGlobalContextRef(); 1.116 + } 1.117 + 1.118 + __attribute__ ((visibility("default"))) 1.119 + JavaVM* jsjni_GetVM() { 1.120 + return mozilla::AndroidBridge::GetVM(); 1.121 + } 1.122 + 1.123 + __attribute__ ((visibility("default"))) 1.124 + JNIEnv* jsjni_GetJNIForThread() { 1.125 + return GetJNIForThread(); 1.126 + } 1.127 +}