2013-02-09 23:50:28 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <prthread.h>
|
|
|
|
|
2013-02-10 01:30:52 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-02-09 23:50:28 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2015-07-30 17:53:38 +00:00
|
|
|
#include "mozilla/SyncRunnable.h"
|
2013-02-09 23:50:28 +00:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "AndroidBridge.h"
|
|
|
|
|
2013-02-10 01:30:52 +00:00
|
|
|
extern "C" {
|
|
|
|
jclass __jsjni_GetGlobalClassRef(const char *className);
|
|
|
|
}
|
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class GetGlobalClassRefRunnable : public mozilla::Runnable {
|
2013-02-10 01:30:52 +00:00
|
|
|
public:
|
|
|
|
GetGlobalClassRefRunnable(const char *className, jclass *foundClass) :
|
|
|
|
mClassName(className), mResult(foundClass) {}
|
2016-08-08 02:18:10 +00:00
|
|
|
NS_IMETHOD Run() override {
|
2013-02-10 01:30:52 +00:00
|
|
|
*mResult = __jsjni_GetGlobalClassRef(mClassName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
const char *mClassName;
|
|
|
|
jclass *mResult;
|
|
|
|
};
|
|
|
|
|
2013-02-09 23:50:28 +00:00
|
|
|
extern "C" {
|
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
jclass
|
|
|
|
jsjni_FindClass(const char *className) {
|
|
|
|
// FindClass outside the main thread will run into problems due
|
|
|
|
// to missing the classpath
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-08-13 04:53:39 +00:00
|
|
|
JNIEnv *env = mozilla::jni::GetGeckoThreadEnv();
|
2013-02-09 23:50:28 +00:00
|
|
|
return env->FindClass(className);
|
|
|
|
}
|
|
|
|
|
2013-02-10 01:30:52 +00:00
|
|
|
jclass
|
|
|
|
__jsjni_GetGlobalClassRef(const char *className) {
|
|
|
|
// root class globally
|
2015-08-13 04:53:39 +00:00
|
|
|
JNIEnv *env = mozilla::jni::GetGeckoThreadEnv();
|
2013-02-10 01:30:52 +00:00
|
|
|
jclass globalRef = static_cast<jclass>(env->NewGlobalRef(env->FindClass(className)));
|
|
|
|
if (!globalRef)
|
2013-10-10 20:42:52 +00:00
|
|
|
return nullptr;
|
2013-02-10 01:30:52 +00:00
|
|
|
|
|
|
|
// return the newly create global reference
|
|
|
|
return globalRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
jclass
|
|
|
|
jsjni_GetGlobalClassRef(const char *className) {
|
2015-07-28 21:56:34 +00:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return __jsjni_GetGlobalClassRef(className);
|
|
|
|
}
|
|
|
|
|
2013-02-10 01:30:52 +00:00
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
mozilla::DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
jclass foundClass;
|
2015-03-17 16:29:17 +00:00
|
|
|
nsCOMPtr<nsIRunnable> runnable_ref(new GetGlobalClassRefRunnable(className,
|
2013-02-10 01:30:52 +00:00
|
|
|
&foundClass));
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<mozilla::SyncRunnable> sr = new mozilla::SyncRunnable(runnable_ref);
|
2015-07-30 17:53:38 +00:00
|
|
|
sr->DispatchToThread(mainThread);
|
2013-02-10 01:30:52 +00:00
|
|
|
if (!foundClass)
|
2013-10-10 20:42:52 +00:00
|
|
|
return nullptr;
|
2013-02-10 01:30:52 +00:00
|
|
|
|
|
|
|
return foundClass;
|
|
|
|
}
|
|
|
|
|
2013-02-09 23:50:28 +00:00
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
jmethodID
|
|
|
|
jsjni_GetStaticMethodID(jclass methodClass,
|
|
|
|
const char *methodName,
|
|
|
|
const char *signature) {
|
2015-08-13 04:53:39 +00:00
|
|
|
JNIEnv *env = mozilla::jni::GetGeckoThreadEnv();
|
2013-02-09 23:50:28 +00:00
|
|
|
return env->GetStaticMethodID(methodClass, methodName, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
bool
|
|
|
|
jsjni_ExceptionCheck() {
|
2015-08-13 04:53:39 +00:00
|
|
|
JNIEnv *env = mozilla::jni::GetGeckoThreadEnv();
|
2013-02-09 23:50:28 +00:00
|
|
|
return env->ExceptionCheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
void
|
|
|
|
jsjni_CallStaticVoidMethodA(jclass cls,
|
|
|
|
jmethodID method,
|
|
|
|
jvalue *values) {
|
2015-08-13 04:53:39 +00:00
|
|
|
JNIEnv *env = mozilla::jni::GetGeckoThreadEnv();
|
2013-02-09 23:50:28 +00:00
|
|
|
|
|
|
|
mozilla::AutoLocalJNIFrame jniFrame(env);
|
|
|
|
env->CallStaticVoidMethodA(cls, method, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
int
|
|
|
|
jsjni_CallStaticIntMethodA(jclass cls,
|
|
|
|
jmethodID method,
|
|
|
|
jvalue *values) {
|
2015-08-13 04:53:39 +00:00
|
|
|
JNIEnv *env = mozilla::jni::GetGeckoThreadEnv();
|
2013-02-09 23:50:28 +00:00
|
|
|
|
|
|
|
mozilla::AutoLocalJNIFrame jniFrame(env);
|
|
|
|
return env->CallStaticIntMethodA(cls, method, values);
|
|
|
|
}
|
2013-05-02 06:42:53 +00:00
|
|
|
|
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
jobject jsjni_GetGlobalContextRef() {
|
|
|
|
return mozilla::AndroidBridge::Bridge()->GetGlobalContextRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
JavaVM* jsjni_GetVM() {
|
2015-08-13 04:53:39 +00:00
|
|
|
JavaVM* jvm;
|
|
|
|
JNIEnv* const env = mozilla::jni::GetGeckoThreadEnv();
|
|
|
|
MOZ_ALWAYS_TRUE(!env->GetJavaVM(&jvm));
|
|
|
|
return jvm;
|
2013-05-02 06:42:53 +00:00
|
|
|
}
|
2013-11-08 01:07:47 +00:00
|
|
|
|
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
JNIEnv* jsjni_GetJNIForThread() {
|
2015-08-13 04:53:39 +00:00
|
|
|
return mozilla::jni::GetEnvForThread();
|
2013-11-08 01:07:47 +00:00
|
|
|
}
|
2015-08-13 04:53:39 +00:00
|
|
|
|
|
|
|
// For compatibility with JNI.jsm; some addons bundle their own JNI.jsm,
|
|
|
|
// so we cannot just change the function name used in JNI.jsm.
|
|
|
|
__attribute__ ((visibility("default")))
|
|
|
|
JNIEnv* GetJNIForThread() {
|
|
|
|
return mozilla::jni::GetEnvForThread();
|
|
|
|
}
|
2013-02-09 23:50:28 +00:00
|
|
|
}
|