2014-04-01 23:16:52 +04: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 "NativeJSContainer.h"
|
|
|
|
#include "AndroidBridge.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "js/StructuredClone.h"
|
2014-07-11 07:37:40 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-04-01 23:16:55 +04:00
|
|
|
#include "mozilla/Vector.h"
|
2014-04-01 23:16:53 +04:00
|
|
|
#include "prthread.h"
|
2014-07-12 11:43:08 +04:00
|
|
|
#include "nsJSUtils.h"
|
2014-04-01 23:16:52 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
|
|
|
|
class NativeJSContainer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void InitJNI(JNIEnv* env) {
|
|
|
|
if (jNativeJSContainer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
jNativeJSContainer = AndroidBridge::GetClassGlobalRef(
|
|
|
|
env, "org/mozilla/gecko/util/NativeJSContainer");
|
|
|
|
MOZ_ASSERT(jNativeJSContainer);
|
|
|
|
jContainerNativeObject = AndroidBridge::GetFieldID(
|
|
|
|
env, jNativeJSContainer, "mNativeObject", "J");
|
|
|
|
MOZ_ASSERT(jContainerNativeObject);
|
|
|
|
jContainerConstructor = AndroidBridge::GetMethodID(
|
|
|
|
env, jNativeJSContainer, "<init>", "(J)V");
|
|
|
|
MOZ_ASSERT(jContainerConstructor);
|
2014-04-01 23:16:54 +04:00
|
|
|
|
|
|
|
jNativeJSObject = AndroidBridge::GetClassGlobalRef(
|
|
|
|
env, "org/mozilla/gecko/util/NativeJSObject");
|
|
|
|
MOZ_ASSERT(jNativeJSObject);
|
|
|
|
jObjectContainer = AndroidBridge::GetFieldID(
|
|
|
|
env, jNativeJSObject, "mContainer",
|
|
|
|
"Lorg/mozilla/gecko/util/NativeJSContainer;");
|
|
|
|
MOZ_ASSERT(jObjectContainer);
|
2014-04-01 23:16:55 +04:00
|
|
|
jObjectIndex = AndroidBridge::GetFieldID(
|
|
|
|
env, jNativeJSObject, "mObjectIndex", "I");
|
|
|
|
MOZ_ASSERT(jObjectIndex);
|
|
|
|
jObjectConstructor = AndroidBridge::GetMethodID(
|
|
|
|
env, jNativeJSObject, "<init>",
|
|
|
|
"(Lorg/mozilla/gecko/util/NativeJSContainer;I)V");
|
|
|
|
MOZ_ASSERT(jContainerConstructor);
|
2014-05-12 20:50:47 +04:00
|
|
|
|
|
|
|
jBundle = AndroidBridge::GetClassGlobalRef(
|
|
|
|
env, "android/os/Bundle");
|
|
|
|
MOZ_ASSERT(jBundle);
|
|
|
|
jBundleConstructor = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "<init>", "(I)V");
|
|
|
|
MOZ_ASSERT(jBundleConstructor);
|
|
|
|
jBundlePutBoolean = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "putBoolean",
|
|
|
|
"(Ljava/lang/String;Z)V");
|
|
|
|
MOZ_ASSERT(jBundlePutBoolean);
|
|
|
|
jBundlePutBundle = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "putBundle",
|
|
|
|
"(Ljava/lang/String;Landroid/os/Bundle;)V");
|
|
|
|
MOZ_ASSERT(jBundlePutBundle);
|
|
|
|
jBundlePutDouble = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "putDouble",
|
|
|
|
"(Ljava/lang/String;D)V");
|
|
|
|
MOZ_ASSERT(jBundlePutDouble);
|
|
|
|
jBundlePutInt = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "putInt",
|
|
|
|
"(Ljava/lang/String;I)V");
|
|
|
|
MOZ_ASSERT(jBundlePutInt);
|
|
|
|
jBundlePutString = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "putString",
|
|
|
|
"(Ljava/lang/String;Ljava/lang/String;)V");
|
|
|
|
MOZ_ASSERT(jBundlePutString);
|
2014-05-17 02:25:29 +04:00
|
|
|
jBundlePutBooleanArray = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "putBooleanArray",
|
|
|
|
"(Ljava/lang/String;[Z)V");
|
|
|
|
MOZ_ASSERT(jBundlePutBooleanArray);
|
|
|
|
jBundlePutDoubleArray = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "putDoubleArray",
|
|
|
|
"(Ljava/lang/String;[D)V");
|
|
|
|
MOZ_ASSERT(jBundlePutDoubleArray);
|
|
|
|
jBundlePutIntArray = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "putIntArray",
|
|
|
|
"(Ljava/lang/String;[I)V");
|
|
|
|
MOZ_ASSERT(jBundlePutIntArray);
|
2014-05-17 02:25:29 +04:00
|
|
|
jBundlePutStringArray = AndroidBridge::GetMethodID(
|
|
|
|
env, jBundle, "putStringArray",
|
|
|
|
"(Ljava/lang/String;[Ljava/lang/String;)V");
|
|
|
|
MOZ_ASSERT(jBundlePutStringArray);
|
|
|
|
|
|
|
|
jObject = AndroidBridge::GetClassGlobalRef(
|
|
|
|
env, "java/lang/Object");
|
|
|
|
MOZ_ASSERT(jObject);
|
|
|
|
jString = AndroidBridge::GetClassGlobalRef(
|
|
|
|
env, "java/lang/String");
|
|
|
|
MOZ_ASSERT(jString);
|
2014-04-01 23:16:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static jobject CreateInstance(JNIEnv* env, JSContext* cx,
|
|
|
|
JS::HandleObject object) {
|
|
|
|
return CreateInstance(env, new NativeJSContainer(cx, object));
|
|
|
|
}
|
|
|
|
|
|
|
|
static NativeJSContainer* FromInstance(JNIEnv* env, jobject instance) {
|
|
|
|
MOZ_ASSERT(instance);
|
|
|
|
|
|
|
|
const jlong fieldValue =
|
|
|
|
env->GetLongField(instance, jContainerNativeObject);
|
|
|
|
NativeJSContainer* const nativeObject =
|
|
|
|
reinterpret_cast<NativeJSContainer* const>(
|
|
|
|
static_cast<uintptr_t>(fieldValue));
|
|
|
|
if (!nativeObject) {
|
|
|
|
AndroidBridge::ThrowException(env,
|
|
|
|
"java/lang/NullPointerException",
|
|
|
|
"Uninitialized NativeJSContainer");
|
|
|
|
}
|
|
|
|
return nativeObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DisposeInstance(JNIEnv* env, jobject instance) {
|
|
|
|
NativeJSContainer* const container = FromInstance(env, instance);
|
|
|
|
if (container) {
|
|
|
|
env->SetLongField(instance, jContainerNativeObject,
|
|
|
|
static_cast<jlong>(reinterpret_cast<uintptr_t>(nullptr)));
|
|
|
|
delete container;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:53 +04:00
|
|
|
static jobject CloneInstance(JNIEnv* env, jobject instance) {
|
|
|
|
NativeJSContainer* const container = FromInstance(env, instance);
|
|
|
|
if (!container || !container->EnsureObject(env)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
JSContext* const cx = container->mThreadContext;
|
2014-06-10 20:46:00 +04:00
|
|
|
JS::RootedObject object(cx, *container->mJSObject);
|
2014-04-01 23:16:53 +04:00
|
|
|
MOZ_ASSERT(object);
|
|
|
|
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
if (!buffer.write(cx, JS::RootedValue(cx, JS::ObjectValue(*object)))) {
|
|
|
|
AndroidBridge::ThrowException(env,
|
|
|
|
"java/lang/UnsupportedOperationException",
|
|
|
|
"Cannot serialize object");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return CreateInstance(env, new NativeJSContainer(cx, Move(buffer)));
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
static jobject GetInstanceFromObject(JNIEnv* env, jobject object) {
|
|
|
|
MOZ_ASSERT(object);
|
|
|
|
|
|
|
|
const jobject instance = env->GetObjectField(object, jObjectContainer);
|
|
|
|
MOZ_ASSERT(instance);
|
2014-04-01 23:16:55 +04:00
|
|
|
return instance;
|
2014-04-01 23:16:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSContext* GetContextFromObject(JNIEnv* env, jobject object) {
|
|
|
|
MOZ_ASSERT(object);
|
2014-04-01 23:16:55 +04:00
|
|
|
AutoLocalJNIFrame frame(env, 1);
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
NativeJSContainer* const container =
|
|
|
|
FromInstance(env, GetInstanceFromObject(env, object));
|
|
|
|
if (!container) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return container->mThreadContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject* GetObjectFromObject(JNIEnv* env, jobject object) {
|
|
|
|
MOZ_ASSERT(object);
|
2014-04-01 23:16:55 +04:00
|
|
|
AutoLocalJNIFrame frame(env, 1);
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
NativeJSContainer* const container =
|
|
|
|
FromInstance(env, GetInstanceFromObject(env, object));
|
|
|
|
if (!container ||
|
|
|
|
!container->EnsureObject(env)) { // Do thread check
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
const jint index = env->GetIntField(object, jObjectIndex);
|
|
|
|
if (index < 0) {
|
|
|
|
// -1 for index field means it's the root object of the container
|
2014-06-10 20:46:00 +04:00
|
|
|
return *container->mJSObject;
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
2014-06-10 20:46:00 +04:00
|
|
|
return *container->mRootedObjects[index];
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static jobject CreateObjectInstance(JNIEnv* env, jobject object,
|
2014-05-12 20:50:46 +04:00
|
|
|
JSContext* cx,
|
2014-04-01 23:16:55 +04:00
|
|
|
JS::HandleObject jsObject) {
|
|
|
|
MOZ_ASSERT(object);
|
|
|
|
MOZ_ASSERT(jsObject);
|
|
|
|
AutoLocalJNIFrame frame(env, 2);
|
|
|
|
|
|
|
|
jobject instance = GetInstanceFromObject(env, object);
|
|
|
|
NativeJSContainer* const container = FromInstance(env, instance);
|
|
|
|
if (!container) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
size_t newIndex = container->mRootedObjects.length();
|
2014-07-11 07:37:40 +04:00
|
|
|
PersistentObjectPtr rootedJSObject =
|
|
|
|
MakeUnique<PersistentObject>(cx, jsObject);
|
2014-06-10 20:46:00 +04:00
|
|
|
if (!container->mRootedObjects.append(Move(rootedJSObject))) {
|
2014-04-01 23:16:55 +04:00
|
|
|
AndroidBridge::ThrowException(env,
|
|
|
|
"java/lang/OutOfMemoryError", "Cannot allocate object");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const jobject newObject =
|
|
|
|
env->NewObject(jNativeJSObject, jObjectConstructor,
|
|
|
|
instance, newIndex);
|
|
|
|
MOZ_ASSERT(newObject);
|
|
|
|
return frame.Pop(newObject);
|
2014-04-01 23:16:54 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:53 +04:00
|
|
|
// Make sure we have a JSObject and deserialize if necessary/possible
|
|
|
|
bool EnsureObject(JNIEnv* env) {
|
|
|
|
if (mJSObject) {
|
|
|
|
if (PR_GetCurrentThread() != mThread) {
|
|
|
|
AndroidBridge::ThrowException(env,
|
|
|
|
"java/lang/IllegalThreadStateException",
|
|
|
|
"Using NativeJSObject off its thread");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!SwitchContextToCurrentThread()) {
|
|
|
|
AndroidBridge::ThrowException(env,
|
|
|
|
"java/lang/IllegalThreadStateException",
|
|
|
|
"Not available for this thread");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::RootedValue value(mThreadContext);
|
|
|
|
MOZ_ASSERT(mBuffer.data());
|
|
|
|
MOZ_ALWAYS_TRUE(mBuffer.read(mThreadContext, &value));
|
|
|
|
if (value.isObject()) {
|
2014-07-11 07:37:40 +04:00
|
|
|
mJSObject = MakeUnique<PersistentObject>(mThreadContext, &value.toObject());
|
2014-04-01 23:16:53 +04:00
|
|
|
}
|
|
|
|
if (!mJSObject) {
|
|
|
|
AndroidBridge::ThrowException(env,
|
|
|
|
"java/lang/IllegalStateException", "Cannot deserialize data");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mBuffer.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:50:47 +04:00
|
|
|
static jclass jBundle;
|
2014-05-17 02:25:29 +04:00
|
|
|
static jclass jNativeJSContainer;
|
|
|
|
static jclass jNativeJSObject;
|
|
|
|
static jclass jObject;
|
|
|
|
static jclass jString;
|
|
|
|
|
2014-05-12 20:50:47 +04:00
|
|
|
static jmethodID jBundleConstructor;
|
|
|
|
static jmethodID jBundlePutBoolean;
|
|
|
|
static jmethodID jBundlePutBundle;
|
|
|
|
static jmethodID jBundlePutDouble;
|
|
|
|
static jmethodID jBundlePutInt;
|
|
|
|
static jmethodID jBundlePutString;
|
2014-05-17 02:25:29 +04:00
|
|
|
static jmethodID jBundlePutBooleanArray;
|
|
|
|
static jmethodID jBundlePutDoubleArray;
|
|
|
|
static jmethodID jBundlePutIntArray;
|
2014-05-17 02:25:29 +04:00
|
|
|
static jmethodID jBundlePutStringArray;
|
2014-05-12 20:50:47 +04:00
|
|
|
|
2014-04-01 23:16:52 +04:00
|
|
|
private:
|
|
|
|
static jfieldID jContainerNativeObject;
|
|
|
|
static jmethodID jContainerConstructor;
|
2014-04-01 23:16:54 +04:00
|
|
|
static jfieldID jObjectContainer;
|
2014-04-01 23:16:55 +04:00
|
|
|
static jfieldID jObjectIndex;
|
|
|
|
static jmethodID jObjectConstructor;
|
2014-04-01 23:16:52 +04:00
|
|
|
|
|
|
|
static jobject CreateInstance(JNIEnv* env,
|
|
|
|
NativeJSContainer* nativeObject) {
|
|
|
|
InitJNI(env);
|
|
|
|
const jobject newObject =
|
|
|
|
env->NewObject(jNativeJSContainer, jContainerConstructor,
|
|
|
|
static_cast<jlong>(
|
|
|
|
reinterpret_cast<uintptr_t>(nativeObject)));
|
|
|
|
AndroidBridge::HandleUncaughtException(env);
|
|
|
|
MOZ_ASSERT(newObject);
|
|
|
|
return newObject;
|
|
|
|
}
|
|
|
|
|
2014-06-10 20:46:00 +04:00
|
|
|
typedef JS::PersistentRooted<JSObject*> PersistentObject;
|
2014-07-11 07:37:40 +04:00
|
|
|
typedef UniquePtr<PersistentObject> PersistentObjectPtr;
|
2014-06-10 20:46:00 +04:00
|
|
|
|
2014-04-01 23:16:53 +04:00
|
|
|
// Thread that the object is valid on
|
|
|
|
PRThread* mThread;
|
|
|
|
// Context that the object is valid in
|
|
|
|
JSContext* mThreadContext;
|
|
|
|
// Deserialized object, or nullptr if object is in serialized form
|
2014-06-10 20:46:00 +04:00
|
|
|
PersistentObjectPtr mJSObject;
|
2014-04-01 23:16:53 +04:00
|
|
|
// Serialized object, or empty if object is in deserialized form
|
|
|
|
JSAutoStructuredCloneBuffer mBuffer;
|
2014-04-01 23:16:55 +04:00
|
|
|
// Objects derived from mJSObject
|
2014-06-10 20:46:00 +04:00
|
|
|
Vector<PersistentObjectPtr, 0> mRootedObjects;
|
2014-04-01 23:16:52 +04:00
|
|
|
|
2014-04-01 23:16:53 +04:00
|
|
|
// Create a new container containing the given deserialized object
|
2014-04-01 23:16:52 +04:00
|
|
|
NativeJSContainer(JSContext* cx, JS::HandleObject object)
|
2014-04-01 23:16:53 +04:00
|
|
|
: mThread(PR_GetCurrentThread())
|
|
|
|
, mThreadContext(cx)
|
2014-06-10 20:46:00 +04:00
|
|
|
, mJSObject(new PersistentObject(cx, object))
|
2014-04-01 23:16:52 +04:00
|
|
|
{
|
|
|
|
}
|
2014-04-01 23:16:53 +04:00
|
|
|
|
|
|
|
// Create a new container containing the given serialized object
|
|
|
|
NativeJSContainer(JSContext* cx, JSAutoStructuredCloneBuffer&& buffer)
|
|
|
|
: mThread(PR_GetCurrentThread())
|
|
|
|
, mThreadContext(cx)
|
2014-06-10 20:46:00 +04:00
|
|
|
, mJSObject(nullptr)
|
2014-04-01 23:16:53 +04:00
|
|
|
, mBuffer(Forward<JSAutoStructuredCloneBuffer>(buffer))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SwitchContextToCurrentThread() {
|
|
|
|
PRThread* const currentThread = PR_GetCurrentThread();
|
|
|
|
if (currentThread == mThread) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-01 23:16:52 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
jclass NativeJSContainer::jNativeJSContainer = 0;
|
|
|
|
jfieldID NativeJSContainer::jContainerNativeObject = 0;
|
|
|
|
jmethodID NativeJSContainer::jContainerConstructor = 0;
|
2014-04-01 23:16:54 +04:00
|
|
|
jclass NativeJSContainer::jNativeJSObject = 0;
|
|
|
|
jfieldID NativeJSContainer::jObjectContainer = 0;
|
2014-04-01 23:16:55 +04:00
|
|
|
jfieldID NativeJSContainer::jObjectIndex = 0;
|
|
|
|
jmethodID NativeJSContainer::jObjectConstructor = 0;
|
2014-05-12 20:50:47 +04:00
|
|
|
jclass NativeJSContainer::jBundle = 0;
|
|
|
|
jmethodID NativeJSContainer::jBundleConstructor = 0;
|
|
|
|
jmethodID NativeJSContainer::jBundlePutBoolean = 0;
|
|
|
|
jmethodID NativeJSContainer::jBundlePutBundle = 0;
|
|
|
|
jmethodID NativeJSContainer::jBundlePutDouble = 0;
|
|
|
|
jmethodID NativeJSContainer::jBundlePutInt = 0;
|
|
|
|
jmethodID NativeJSContainer::jBundlePutString = 0;
|
2014-05-17 02:25:29 +04:00
|
|
|
jmethodID NativeJSContainer::jBundlePutBooleanArray = 0;
|
|
|
|
jmethodID NativeJSContainer::jBundlePutDoubleArray = 0;
|
|
|
|
jmethodID NativeJSContainer::jBundlePutIntArray = 0;
|
2014-05-17 02:25:29 +04:00
|
|
|
jmethodID NativeJSContainer::jBundlePutStringArray = 0;
|
|
|
|
jclass NativeJSContainer::jString = 0;
|
|
|
|
jclass NativeJSContainer::jObject = 0;
|
2014-04-01 23:16:52 +04:00
|
|
|
|
|
|
|
jobject
|
|
|
|
CreateNativeJSContainer(JNIEnv* env, JSContext* cx, JS::HandleObject object)
|
|
|
|
{
|
|
|
|
return NativeJSContainer::CreateInstance(env, cx, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace widget
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class JSJNIString
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JSJNIString(JNIEnv* env, jstring str)
|
|
|
|
: mEnv(env)
|
|
|
|
, mJNIString(str)
|
|
|
|
, mJSString(!str ? nullptr :
|
2014-07-22 08:43:21 +04:00
|
|
|
reinterpret_cast<const char16_t*>(env->GetStringChars(str, nullptr)))
|
2014-04-01 23:16:54 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~JSJNIString() {
|
|
|
|
if (mJNIString) {
|
|
|
|
mEnv->ReleaseStringChars(mJNIString,
|
|
|
|
reinterpret_cast<const jchar*>(mJSString));
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 08:43:21 +04:00
|
|
|
operator const char16_t*() const {
|
2014-04-01 23:16:54 +04:00
|
|
|
return mJSString;
|
|
|
|
}
|
|
|
|
size_t Length() const {
|
|
|
|
return static_cast<size_t>(mEnv->GetStringLength(mJNIString));
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
JNIEnv* const mEnv;
|
|
|
|
const jstring mJNIString;
|
2014-07-22 08:43:21 +04:00
|
|
|
const char16_t* const mJSString;
|
2014-04-01 23:16:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
2014-05-12 20:50:47 +04:00
|
|
|
CheckJSCall(JNIEnv* env, bool result)
|
|
|
|
{
|
2014-04-01 23:16:54 +04:00
|
|
|
if (!result) {
|
|
|
|
AndroidBridge::ThrowException(env,
|
|
|
|
"java/lang/UnsupportedOperationException", "JSAPI call failed");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-12 20:50:47 +04:00
|
|
|
CheckJNIArgument(JNIEnv* env, jobject arg)
|
|
|
|
{
|
2014-04-01 23:16:54 +04:00
|
|
|
if (!arg) {
|
|
|
|
AndroidBridge::ThrowException(env,
|
|
|
|
"java/lang/IllegalArgumentException", "Null argument");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
template <bool (*InValue)(JSContext*, JS::HandleValue)> bool
|
|
|
|
CheckProperty(JNIEnv* env, JSContext* cx, JS::HandleValue val) {
|
|
|
|
if (!(*InValue)(cx, val)) {
|
|
|
|
AndroidBridge::ThrowException(env,
|
2014-08-06 22:38:29 +04:00
|
|
|
"org/mozilla/gecko/util/NativeJSObject$InvalidPropertyException",
|
2014-05-17 02:25:29 +04:00
|
|
|
"Property type mismatch");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
bool
|
2014-07-22 08:43:21 +04:00
|
|
|
AppendJSON(const char16_t* buf, uint32_t len, void* data)
|
2014-04-01 23:16:54 +04:00
|
|
|
{
|
|
|
|
static_cast<nsAutoString*>(data)->Append(buf, len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
template <typename U, typename V,
|
|
|
|
bool (JS::Value::*IsMethod)() const,
|
2014-05-17 02:25:29 +04:00
|
|
|
V (JS::Value::*ToMethod)() const,
|
|
|
|
typename UA,
|
|
|
|
UA (JNIEnv::*NewArrayMethod)(jsize),
|
|
|
|
void (JNIEnv::*SetArrayRegionMethod)(UA, jsize, jsize, const U*)>
|
2014-04-01 23:16:55 +04:00
|
|
|
struct PrimitiveProperty
|
|
|
|
{
|
|
|
|
typedef U Type; // JNI type
|
2014-05-17 02:25:29 +04:00
|
|
|
typedef UA ArrayType; // JNI array type
|
2014-04-01 23:16:55 +04:00
|
|
|
typedef V NativeType; // JSAPI type
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
static bool InValue(JSContext* cx, JS::HandleValue val) {
|
2014-04-01 23:16:55 +04:00
|
|
|
return (static_cast<const JS::Value&>(val).*IsMethod)();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Type FromValue(JNIEnv* env, jobject instance,
|
|
|
|
JSContext* cx, JS::HandleValue val) {
|
|
|
|
return static_cast<Type>(
|
|
|
|
(static_cast<const JS::Value&>(val).*ToMethod)());
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
|
|
|
|
static ArrayType NewArray(JNIEnv* env, jobject instance, JSContext* cx,
|
|
|
|
JS::HandleObject array, size_t length) {
|
2014-07-11 07:37:40 +04:00
|
|
|
UniquePtr<Type[]> buffer = MakeUnique<Type[]>(length);
|
2014-05-17 02:25:29 +04:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
JS::RootedValue elem(cx);
|
|
|
|
if (!CheckJSCall(env, JS_GetElement(cx, array, i, &elem)) ||
|
2014-05-21 06:28:39 +04:00
|
|
|
!CheckProperty<PrimitiveProperty::InValue>(env, cx, elem)) {
|
2014-05-17 02:25:29 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
buffer[i] = FromValue(env, instance, cx, elem);
|
|
|
|
}
|
|
|
|
AutoLocalJNIFrame frame(env, 1);
|
|
|
|
ArrayType jarray = (env->*NewArrayMethod)(length);
|
|
|
|
if (!jarray) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-11 07:37:40 +04:00
|
|
|
(env->*SetArrayRegionMethod)(jarray, 0, length, buffer.get());
|
2014-05-17 02:25:29 +04:00
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return frame.Pop(jarray);
|
|
|
|
}
|
2014-04-01 23:16:55 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Statically cast from bool to jboolean (unsigned char); it works
|
|
|
|
// since false and JNI_FALSE have the same value (0), and true and
|
|
|
|
// JNI_TRUE have the same value (1).
|
|
|
|
typedef PrimitiveProperty<jboolean, bool,
|
2014-05-17 02:25:29 +04:00
|
|
|
&JS::Value::isBoolean, &JS::Value::toBoolean,
|
|
|
|
jbooleanArray, &JNIEnv::NewBooleanArray,
|
|
|
|
&JNIEnv::SetBooleanArrayRegion> BooleanProperty;
|
2014-04-01 23:16:55 +04:00
|
|
|
|
|
|
|
typedef PrimitiveProperty<jdouble, double,
|
2014-05-17 02:25:29 +04:00
|
|
|
&JS::Value::isNumber, &JS::Value::toNumber,
|
|
|
|
jdoubleArray, &JNIEnv::NewDoubleArray,
|
|
|
|
&JNIEnv::SetDoubleArrayRegion> DoubleProperty;
|
2014-04-01 23:16:55 +04:00
|
|
|
|
|
|
|
typedef PrimitiveProperty<jint, int32_t,
|
2014-05-17 02:25:29 +04:00
|
|
|
&JS::Value::isInt32, &JS::Value::toInt32,
|
|
|
|
jintArray, &JNIEnv::NewIntArray,
|
|
|
|
&JNIEnv::SetIntArrayRegion> IntProperty;
|
2014-04-01 23:16:55 +04:00
|
|
|
|
|
|
|
struct StringProperty
|
|
|
|
{
|
|
|
|
typedef jstring Type;
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
static jclass Class() {
|
|
|
|
return NativeJSContainer::jString;
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
static bool InValue(JSContext* cx, JS::HandleValue val) {
|
2014-04-01 23:16:55 +04:00
|
|
|
return val.isString();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Type FromValue(JNIEnv* env, jobject instance,
|
|
|
|
JSContext* cx, JS::HandleValue val) {
|
2014-05-12 20:50:47 +04:00
|
|
|
const JS::RootedString str(cx, val.toString());
|
|
|
|
return FromValue(env, instance, cx, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Type FromValue(JNIEnv* env, jobject instance,
|
|
|
|
JSContext* cx, const JS::HandleString str) {
|
2014-07-12 11:43:08 +04:00
|
|
|
nsAutoJSString autoStr;
|
|
|
|
if (!CheckJSCall(env, autoStr.init(cx, str))) {
|
2014-04-01 23:16:55 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-12 11:53:01 +04:00
|
|
|
jstring ret = env->NewString(
|
|
|
|
reinterpret_cast<const jchar*>(autoStr.BeginReading()),
|
|
|
|
autoStr.Length());
|
2014-04-01 23:16:55 +04:00
|
|
|
MOZ_ASSERT(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
template <jclass& C, jobject (*FactoryMethod)
|
2014-05-12 20:50:46 +04:00
|
|
|
(JNIEnv*, jobject, JSContext*, JS::HandleObject)>
|
|
|
|
struct BaseObjectProperty
|
2014-04-01 23:16:55 +04:00
|
|
|
{
|
|
|
|
typedef jobject Type;
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
static jclass Class() {
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
static bool InValue(JSContext* cx, JS::HandleValue val) {
|
2014-04-01 23:16:55 +04:00
|
|
|
return val.isObjectOrNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Type FromValue(JNIEnv* env, jobject instance,
|
|
|
|
JSContext* cx, JS::HandleValue val) {
|
|
|
|
if (val.isNull()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
JS::RootedObject object(cx, &val.toObject());
|
2014-05-12 20:50:46 +04:00
|
|
|
return FactoryMethod(env, instance, cx, object);
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-12 20:50:46 +04:00
|
|
|
jobject GetBundle(JNIEnv*, jobject, JSContext*, JS::HandleObject);
|
|
|
|
|
|
|
|
// Returns a NativeJSObject from a JSObject
|
2014-05-17 02:25:29 +04:00
|
|
|
typedef BaseObjectProperty<NativeJSContainer::jNativeJSObject,
|
2014-05-12 20:50:46 +04:00
|
|
|
NativeJSContainer::CreateObjectInstance> ObjectProperty;
|
|
|
|
|
|
|
|
// Returns a Bundle from a JSObject
|
2014-05-17 02:25:29 +04:00
|
|
|
typedef BaseObjectProperty<NativeJSContainer::jBundle,
|
|
|
|
GetBundle> BundleProperty;
|
|
|
|
|
|
|
|
template <class BaseProperty>
|
|
|
|
struct ObjectArrayWrapper : public BaseProperty
|
|
|
|
{
|
|
|
|
typedef jobjectArray ArrayType;
|
|
|
|
|
|
|
|
static ArrayType NewArray(JNIEnv* env, jobject instance, JSContext* cx,
|
|
|
|
JS::HandleObject array, size_t length) {
|
|
|
|
AutoLocalJNIFrame frame(env, 2);
|
|
|
|
ArrayType jarray = env->NewObjectArray(
|
|
|
|
length, BaseProperty::Class(), nullptr);
|
|
|
|
if (!jarray) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
JS::RootedValue elem(cx);
|
|
|
|
if (!CheckJSCall(env, JS_GetElement(cx, array, i, &elem)) ||
|
|
|
|
!CheckProperty<BaseProperty::InValue>(env, cx, elem)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
typename BaseProperty::Type jelem =
|
|
|
|
BaseProperty::FromValue(env, instance, cx, elem);
|
2014-05-21 02:55:36 +04:00
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
env->SetObjectArrayElement(jarray, i, jelem);
|
|
|
|
env->DeleteLocalRef(jelem);
|
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return frame.Pop(jarray);
|
|
|
|
}
|
|
|
|
};
|
2014-05-12 20:50:46 +04:00
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
template <class T>
|
|
|
|
struct ArrayProperty
|
|
|
|
{
|
|
|
|
typedef T Base;
|
|
|
|
typedef typename T::ArrayType Type;
|
|
|
|
|
|
|
|
static bool InValue(JSContext* cx, JS::HandleValue val) {
|
|
|
|
if (!val.isObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JS::RootedObject obj(cx, &val.toObject());
|
|
|
|
uint32_t length = 0;
|
|
|
|
if (!JS_IsArrayObject(cx, obj) ||
|
|
|
|
!JS_GetArrayLength(cx, obj, &length)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!length) {
|
|
|
|
// Empty arrays are always okay.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// We only check to see the first element is the target type. If the
|
|
|
|
// array has mixed types, we'll throw an error during actual conversion.
|
|
|
|
JS::RootedValue element(cx);
|
|
|
|
return JS_GetElement(cx, obj, 0, &element) &&
|
|
|
|
Base::InValue(cx, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Type FromValue(JNIEnv* env, jobject instance,
|
|
|
|
JSContext* cx, JS::HandleValue val) {
|
|
|
|
JS::RootedObject obj(cx, &val.toObject());
|
|
|
|
uint32_t length = 0;
|
|
|
|
if (!CheckJSCall(env, JS_GetArrayLength(cx, obj, &length))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return Base::NewArray(env, instance, cx, obj, length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef ArrayProperty<BooleanProperty> BooleanArrayProperty;
|
|
|
|
typedef ArrayProperty<DoubleProperty> DoubleArrayProperty;
|
|
|
|
typedef ArrayProperty<IntProperty> IntArrayProperty;
|
2014-05-17 02:25:29 +04:00
|
|
|
typedef ArrayProperty<ObjectArrayWrapper<StringProperty>> StringArrayProperty;
|
|
|
|
typedef ArrayProperty<ObjectArrayWrapper<ObjectProperty>> ObjectArrayProperty;
|
|
|
|
typedef ArrayProperty<ObjectArrayWrapper<BundleProperty>> BundleArrayProperty;
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
struct HasProperty
|
|
|
|
{
|
|
|
|
typedef jboolean Type;
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
static bool InValue(JSContext* cx, JS::HandleValue val) {
|
2014-04-01 23:16:55 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Type FromValue(JNIEnv* env, jobject instance,
|
|
|
|
JSContext* cx, JS::HandleValue val) {
|
2014-04-01 23:16:56 +04:00
|
|
|
return JNI_TRUE;
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class FallbackOption {
|
2014-04-01 23:16:56 +04:00
|
|
|
THROW,
|
|
|
|
RETURN,
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-04-01 23:16:56 +04:00
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
template <class Property>
|
|
|
|
typename Property::Type
|
2014-04-01 23:16:56 +04:00
|
|
|
GetProperty(JNIEnv* env, jobject instance, jstring name,
|
|
|
|
FallbackOption option = FallbackOption::THROW,
|
2014-05-12 20:50:47 +04:00
|
|
|
typename Property::Type fallback = typename Property::Type())
|
|
|
|
{
|
2014-04-01 23:16:55 +04:00
|
|
|
MOZ_ASSERT(env);
|
|
|
|
MOZ_ASSERT(instance);
|
|
|
|
|
|
|
|
JSContext* const cx =
|
|
|
|
NativeJSContainer::GetContextFromObject(env, instance);
|
2014-05-12 20:50:47 +04:00
|
|
|
if (!cx) {
|
|
|
|
return typename Property::Type();
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
const JS::RootedObject object(cx,
|
|
|
|
NativeJSContainer::GetObjectFromObject(env, instance));
|
|
|
|
const JSJNIString strName(env, name);
|
|
|
|
JS::RootedValue val(cx);
|
|
|
|
|
|
|
|
if (!object ||
|
|
|
|
!CheckJNIArgument(env, name) ||
|
|
|
|
!CheckJSCall(env,
|
|
|
|
JS_GetUCProperty(cx, object, strName, strName.Length(), &val))) {
|
|
|
|
return typename Property::Type();
|
|
|
|
}
|
2014-05-29 01:04:56 +04:00
|
|
|
if (val.isUndefined() || val.isNull()) {
|
2014-04-01 23:16:56 +04:00
|
|
|
if (option == FallbackOption::THROW) {
|
|
|
|
AndroidBridge::ThrowException(env,
|
2014-08-06 22:38:29 +04:00
|
|
|
"org/mozilla/gecko/util/NativeJSObject$InvalidPropertyException",
|
2014-04-01 23:16:56 +04:00
|
|
|
"Property does not exist");
|
|
|
|
}
|
|
|
|
return fallback;
|
|
|
|
}
|
2014-05-17 02:25:29 +04:00
|
|
|
if (!CheckProperty<Property::InValue>(env, cx, val)) {
|
2014-04-01 23:16:56 +04:00
|
|
|
return fallback;
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
|
|
|
return Property::FromValue(env, instance, cx, val);
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:50:46 +04:00
|
|
|
jobject
|
|
|
|
GetBundle(JNIEnv* env, jobject instance, JSContext* cx, JS::HandleObject obj)
|
|
|
|
{
|
2014-05-12 20:50:47 +04:00
|
|
|
AutoLocalJNIFrame frame(env, 1);
|
|
|
|
|
|
|
|
const JS::AutoIdArray ids(cx, JS_Enumerate(cx, obj));
|
|
|
|
if (!CheckJSCall(env, !!ids)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t length = ids.length();
|
|
|
|
const jobject newBundle = env->NewObject(
|
|
|
|
NativeJSContainer::jBundle,
|
|
|
|
NativeJSContainer::jBundleConstructor,
|
|
|
|
static_cast<jint>(length));
|
|
|
|
if (!newBundle) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ids.length(); i++) {
|
|
|
|
AutoLocalJNIFrame loopFrame(env, 2);
|
|
|
|
|
|
|
|
const JS::RootedId id(cx, ids[i]);
|
|
|
|
JS::RootedValue idVal(cx);
|
|
|
|
if (!CheckJSCall(env, JS_IdToValue(cx, id, &idVal))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JS::RootedString idStr(cx, JS::ToString(cx, idVal));
|
|
|
|
if (!CheckJSCall(env, !!idStr)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const jstring name =
|
|
|
|
StringProperty::FromValue(env, instance, cx, idStr);
|
|
|
|
JS::RootedValue val(cx);
|
|
|
|
if (!name ||
|
|
|
|
!CheckJSCall(env, JS_GetPropertyById(cx, obj, id, &val))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-21 02:55:36 +04:00
|
|
|
#define PUT_IN_BUNDLE_IF_TYPE_IS(TYPE) \
|
|
|
|
if (TYPE##Property::InValue(cx, val)) { \
|
|
|
|
const TYPE##Property::Type jval = \
|
|
|
|
TYPE##Property::FromValue(env, instance, cx, val); \
|
|
|
|
if (env->ExceptionCheck()) { \
|
|
|
|
return nullptr; \
|
|
|
|
} \
|
|
|
|
env->CallVoidMethod(newBundle, \
|
|
|
|
NativeJSContainer::jBundlePut##TYPE, name, jval); \
|
|
|
|
if (env->ExceptionCheck()) { \
|
|
|
|
return nullptr; \
|
|
|
|
} \
|
2014-05-12 20:50:47 +04:00
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
((void) 0) // Accommodate trailing semicolon.
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
// Scalar values are faster to check, so check them first.
|
2014-05-12 20:50:47 +04:00
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(Boolean);
|
|
|
|
// Int can be casted to double, so check int first.
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(Int);
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(Double);
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(String);
|
2014-05-17 02:25:29 +04:00
|
|
|
// There's no "putObject", so don't check ObjectProperty
|
|
|
|
|
|
|
|
// Check for array types if scalar checks all failed.
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(BooleanArray);
|
|
|
|
// XXX because we only check the first element of an array,
|
|
|
|
// a double array can potentially be seen as an int array.
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(IntArray);
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(DoubleArray);
|
2014-05-17 02:25:29 +04:00
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(StringArray);
|
|
|
|
// There's no "putObjectArray", so don't check ObjectArrayProperty
|
|
|
|
// There's no "putBundleArray", so don't check BundleArrayProperty
|
2014-05-17 02:25:29 +04:00
|
|
|
|
2014-05-12 20:50:47 +04:00
|
|
|
// Use Bundle as the default catch-all for objects
|
|
|
|
PUT_IN_BUNDLE_IF_TYPE_IS(Bundle);
|
|
|
|
|
|
|
|
#undef PUT_IN_BUNDLE_IF_TYPE_IS
|
|
|
|
|
|
|
|
// We tried all supported types; just bail.
|
|
|
|
AndroidBridge::ThrowException(env,
|
|
|
|
"java/lang/UnsupportedOperationException",
|
|
|
|
"Unsupported property type");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return frame.Pop(newBundle);
|
2014-05-12 20:50:46 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
} // namespace
|
|
|
|
|
2014-04-01 23:16:52 +04:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
NS_EXPORT void JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSContainer_dispose(JNIEnv* env, jobject instance)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(env);
|
|
|
|
NativeJSContainer::DisposeInstance(env, instance);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:53 +04:00
|
|
|
NS_EXPORT jobject JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSContainer_clone(JNIEnv* env, jobject instance)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(env);
|
|
|
|
return NativeJSContainer::CloneInstance(env, instance);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
NS_EXPORT jboolean JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getBoolean(JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
|
|
|
return GetProperty<BooleanProperty>(env, instance, name);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:56 +04:00
|
|
|
NS_EXPORT jboolean JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optBoolean(JNIEnv* env, jobject instance,
|
|
|
|
jstring name, jboolean fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<BooleanProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
NS_EXPORT jbooleanArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getBooleanArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<BooleanArrayProperty>(env, instance, name);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_EXPORT jbooleanArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optBooleanArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name, jbooleanArray fallback)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<BooleanArrayProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2014-05-12 20:50:46 +04:00
|
|
|
NS_EXPORT jobject JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getBundle(JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
|
|
|
return GetProperty<BundleProperty>(env, instance, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_EXPORT jobject JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optBundle(JNIEnv* env, jobject instance,
|
|
|
|
jstring name, jobject fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<BundleProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
NS_EXPORT jobjectArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getBundleArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<BundleArrayProperty>(env, instance, name);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_EXPORT jobjectArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optBundleArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name, jobjectArray fallback)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<BundleArrayProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
NS_EXPORT jdouble JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getDouble(JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
|
|
|
return GetProperty<DoubleProperty>(env, instance, name);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:56 +04:00
|
|
|
NS_EXPORT jdouble JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optDouble(JNIEnv* env, jobject instance,
|
|
|
|
jstring name, jdouble fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<DoubleProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
NS_EXPORT jdoubleArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getDoubleArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<DoubleArrayProperty>(env, instance, name);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_EXPORT jdoubleArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optDoubleArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name, jdoubleArray fallback)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<DoubleArrayProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
NS_EXPORT jint JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getInt(JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
|
|
|
return GetProperty<IntProperty>(env, instance, name);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:56 +04:00
|
|
|
NS_EXPORT jint JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optInt(JNIEnv* env, jobject instance,
|
|
|
|
jstring name, jint fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<IntProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
NS_EXPORT jintArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getIntArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<IntArrayProperty>(env, instance, name);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_EXPORT jintArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optIntArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name, jintArray fallback)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<IntArrayProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
NS_EXPORT jobject JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getObject(JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
|
|
|
return GetProperty<ObjectProperty>(env, instance, name);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:56 +04:00
|
|
|
NS_EXPORT jobject JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optObject(JNIEnv* env, jobject instance,
|
|
|
|
jstring name, jobject fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<ObjectProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
NS_EXPORT jobjectArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getObjectArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<ObjectArrayProperty>(env, instance, name);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_EXPORT jobjectArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optObjectArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name, jobjectArray fallback)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<ObjectArrayProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
NS_EXPORT jstring JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getString(JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
|
|
|
return GetProperty<StringProperty>(env, instance, name);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:56 +04:00
|
|
|
NS_EXPORT jstring JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optString(JNIEnv* env, jobject instance,
|
|
|
|
jstring name, jstring fallback)
|
|
|
|
{
|
|
|
|
return GetProperty<StringProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:25:29 +04:00
|
|
|
NS_EXPORT jobjectArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_getStringArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<StringArrayProperty>(env, instance, name);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_EXPORT jobjectArray JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_optStringArray(
|
|
|
|
JNIEnv* env, jobject instance, jstring name, jobjectArray fallback)
|
|
|
|
{
|
2014-05-17 02:25:29 +04:00
|
|
|
return GetProperty<StringArrayProperty>(env, instance, name, FallbackOption::RETURN, fallback);
|
2014-05-17 02:25:29 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:55 +04:00
|
|
|
NS_EXPORT jboolean JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_has(JNIEnv* env, jobject instance, jstring name)
|
|
|
|
{
|
2014-04-01 23:16:56 +04:00
|
|
|
return GetProperty<HasProperty>(env, instance, name, FallbackOption::RETURN, JNI_FALSE);
|
2014-04-01 23:16:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-12 20:50:46 +04:00
|
|
|
NS_EXPORT jobject JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_toBundle(JNIEnv* env, jobject instance)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(env);
|
|
|
|
MOZ_ASSERT(instance);
|
|
|
|
|
|
|
|
JSContext* const cx = NativeJSContainer::GetContextFromObject(env, instance);
|
2014-05-12 20:50:47 +04:00
|
|
|
if (!cx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:50:46 +04:00
|
|
|
const JS::RootedObject object(cx, NativeJSContainer::GetObjectFromObject(env, instance));
|
|
|
|
if (!object) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return GetBundle(env, instance, cx, object);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
NS_EXPORT jstring JNICALL
|
|
|
|
Java_org_mozilla_gecko_util_NativeJSObject_toString(JNIEnv* env, jobject instance)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(env);
|
|
|
|
MOZ_ASSERT(instance);
|
|
|
|
|
|
|
|
JSContext* const cx = NativeJSContainer::GetContextFromObject(env, instance);
|
2014-05-12 20:50:47 +04:00
|
|
|
if (!cx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:54 +04:00
|
|
|
const JS::RootedObject object(cx, NativeJSContainer::GetObjectFromObject(env, instance));
|
|
|
|
JS::RootedValue value(cx, JS::ObjectValue(*object));
|
|
|
|
nsAutoString json;
|
|
|
|
|
|
|
|
if (!object ||
|
|
|
|
!CheckJSCall(env,
|
2015-05-14 00:07:34 +03:00
|
|
|
JS_Stringify(cx, &value, nullptr, JS::NullHandleValue, AppendJSON, &json))) {
|
2014-04-01 23:16:54 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
jstring ret = env->NewString(reinterpret_cast<const jchar*>(json.get()), json.Length());
|
|
|
|
MOZ_ASSERT(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:16:52 +04:00
|
|
|
} // extern "C"
|