michael@0: /* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "mozilla/DebugOnly.h" michael@0: #include "mozilla/Assertions.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "AndroidBridge.h" michael@0: michael@0: extern "C" { michael@0: jclass __jsjni_GetGlobalClassRef(const char *className); michael@0: } michael@0: michael@0: class GetGlobalClassRefRunnable : public nsRunnable { michael@0: public: michael@0: GetGlobalClassRefRunnable(const char *className, jclass *foundClass) : michael@0: mClassName(className), mResult(foundClass) {} michael@0: NS_IMETHOD Run() { michael@0: *mResult = __jsjni_GetGlobalClassRef(mClassName); michael@0: return NS_OK; michael@0: } michael@0: private: michael@0: const char *mClassName; michael@0: jclass *mResult; michael@0: }; michael@0: michael@0: extern "C" { michael@0: __attribute__ ((visibility("default"))) michael@0: jclass michael@0: jsjni_FindClass(const char *className) { michael@0: // FindClass outside the main thread will run into problems due michael@0: // to missing the classpath michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); michael@0: return env->FindClass(className); michael@0: } michael@0: michael@0: jclass michael@0: __jsjni_GetGlobalClassRef(const char *className) { michael@0: // root class globally michael@0: JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); michael@0: jclass globalRef = static_cast(env->NewGlobalRef(env->FindClass(className))); michael@0: if (!globalRef) michael@0: return nullptr; michael@0: michael@0: // return the newly create global reference michael@0: return globalRef; michael@0: } michael@0: michael@0: __attribute__ ((visibility("default"))) michael@0: jclass michael@0: jsjni_GetGlobalClassRef(const char *className) { michael@0: nsCOMPtr mainThread; michael@0: mozilla::DebugOnly rv = NS_GetMainThread(getter_AddRefs(mainThread)); michael@0: MOZ_ASSERT(NS_SUCCEEDED(rv)); michael@0: michael@0: jclass foundClass; michael@0: nsRefPtr runnable_ref(new GetGlobalClassRefRunnable(className, michael@0: &foundClass)); michael@0: mainThread->Dispatch(runnable_ref, NS_DISPATCH_SYNC); michael@0: if (!foundClass) michael@0: return nullptr; michael@0: michael@0: return foundClass; michael@0: } michael@0: michael@0: __attribute__ ((visibility("default"))) michael@0: jmethodID michael@0: jsjni_GetStaticMethodID(jclass methodClass, michael@0: const char *methodName, michael@0: const char *signature) { michael@0: JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); michael@0: return env->GetStaticMethodID(methodClass, methodName, signature); michael@0: } michael@0: michael@0: __attribute__ ((visibility("default"))) michael@0: bool michael@0: jsjni_ExceptionCheck() { michael@0: JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); michael@0: return env->ExceptionCheck(); michael@0: } michael@0: michael@0: __attribute__ ((visibility("default"))) michael@0: void michael@0: jsjni_CallStaticVoidMethodA(jclass cls, michael@0: jmethodID method, michael@0: jvalue *values) { michael@0: JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); michael@0: michael@0: mozilla::AutoLocalJNIFrame jniFrame(env); michael@0: env->CallStaticVoidMethodA(cls, method, values); michael@0: } michael@0: michael@0: __attribute__ ((visibility("default"))) michael@0: int michael@0: jsjni_CallStaticIntMethodA(jclass cls, michael@0: jmethodID method, michael@0: jvalue *values) { michael@0: JNIEnv *env = mozilla::AndroidBridge::GetJNIEnv(); michael@0: michael@0: mozilla::AutoLocalJNIFrame jniFrame(env); michael@0: return env->CallStaticIntMethodA(cls, method, values); michael@0: } michael@0: michael@0: __attribute__ ((visibility("default"))) michael@0: jobject jsjni_GetGlobalContextRef() { michael@0: return mozilla::AndroidBridge::Bridge()->GetGlobalContextRef(); michael@0: } michael@0: michael@0: __attribute__ ((visibility("default"))) michael@0: JavaVM* jsjni_GetVM() { michael@0: return mozilla::AndroidBridge::GetVM(); michael@0: } michael@0: michael@0: __attribute__ ((visibility("default"))) michael@0: JNIEnv* jsjni_GetJNIForThread() { michael@0: return GetJNIForThread(); michael@0: } michael@0: }