Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: jni/JniJsEngine.cpp

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Fixed wrong signature for getElementHidingSelectors. Created March 20, 2014, 12:17 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « jni/JniFilterEngine.cpp ('k') | jni/JniJsValue.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: jni/JniJsEngine.cpp
diff --git a/jni/JniJsEngine.cpp b/jni/JniJsEngine.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..59a4a9efbac1147b1f95e1c05f056fda9453d49a
--- /dev/null
+++ b/jni/JniJsEngine.cpp
@@ -0,0 +1,262 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <AdblockPlus.h>
+#include "Utils.h"
+#include "JniCallbacks.h"
+
+static void TransformAppInfo(JNIEnv* env, jobject jAppInfo, AdblockPlus::AppInfo& appInfo)
+{
+ jclass clazz = env->GetObjectClass(jAppInfo);
+
+ appInfo.application = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo, "application");
+ appInfo.applicationVersion = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo, "applicationVersion");
+ appInfo.id = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo, "id");
+ appInfo.locale = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo, "locale");
+ appInfo.name = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo, "name");
+ appInfo.version = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo, "version");
+
+ appInfo.developmentBuild = AdblockPlus::Android::JniGetBooleanField(env, clazz, jAppInfo, "developmentBuild");
+}
+
+static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo)
+{
+ AdblockPlus::AppInfo appInfo;
+
+ TransformAppInfo(env, jAppInfo, appInfo);
+
+ TRY
+ {
+ return AdblockPlus::Android::JniPtr2Long(new AdblockPlus::JsEnginePtr(AdblockPlus::JsEngine::New(appInfo)));
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ delete AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+}
+
+static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName, jlong jCallbackPtr)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ AdblockPlus::Android::JniEventCallback* callback = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::Android::JniEventCallback>(
+ jCallbackPtr);
+ std::string eventName = AdblockPlus::Android::JniJava2StdString(env, jEventName);
+ AdblockPlus::JsEngine::EventCallback eCallback = std::tr1::bind(&AdblockPlus::Android::JniEventCallback::Callback, callback,
+ std::tr1::placeholders::_1);
+
+ TRY
+ {
+ engine->SetEventCallback(eventName, eCallback);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ std::string eventName = AdblockPlus::Android::JniJava2StdString(env, jEventName);
+
+ TRY
+ {
+ engine->RemoveEventCallback(eventName);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ std::string source = AdblockPlus::Android::JniJava2StdString(env, jSource);
+ std::string filename = AdblockPlus::Android::JniJava2StdString(env, jFilename);
+
+ TRY
+ {
+ AdblockPlus::JsValuePtr jsValue = engine->Evaluate(source, filename);
+ return AdblockPlus::Android::NewJniJsValue(env, jsValue);
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName, jarray jJsPtrs)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+ std::string eventName = AdblockPlus::Android::JniJava2StdString(env, jEventName);
+ AdblockPlus::JsValueList args;
+
+ if (jJsPtrs)
+ {
+ jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0);
+
+ jsize length = env->GetArrayLength(jJsPtrs);
+
+ for (jsize i = 0; i < length; i++)
+ {
+ args.push_back(AdblockPlus::Android::JniGetJsValuePtr(ptrs[i]));
+ }
+
+ env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT);
+ }
+
+ TRY
+ {
+ engine->TriggerEvent(eventName, args);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr, jstring jBasePath)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ TRY
+ {
+ AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem());
+
+ std::string basePath = AdblockPlus::Android::JniJava2StdString(env, jBasePath);
+ reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBasePath(basePath);
+
+ engine->SetFileSystem(fileSystem);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ TRY
+ {
+ AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest());
+
+ engine->SetWebRequest(webRequest);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ TRY
+ {
+ AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem());
+
+ engine->SetLogSystem(logSystem);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong logSystemPtr)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ TRY
+ {
+ AdblockPlus::LogSystemPtr logSystem(AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::Android::JniLogSystemCallback>(logSystemPtr));
+
+ engine->SetLogSystem(logSystem);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong webRequestPtr)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ TRY
+ {
+ AdblockPlus::WebRequestPtr& webRequest = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::WebRequestPtr>(webRequestPtr);
+
+ engine->SetWebRequest(webRequest);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlong value)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ TRY
+ {
+ AdblockPlus::JsValuePtr jsValue = engine->NewValue(static_cast<int64_t>(value));
+ return AdblockPlus::Android::NewJniJsValue(env, jsValue);
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ TRY
+ {
+ AdblockPlus::JsValuePtr jsValue = engine->NewValue(value == JNI_TRUE ? true : false);
+ return AdblockPlus::Android::NewJniJsValue(env, jsValue);
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, jstring value)
+{
+ AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
+
+ TRY
+ {
+ std::string strValue = AdblockPlus::Android::JniJava2StdString(env, value);
+ AdblockPlus::JsValuePtr jsValue = engine->NewValue(strValue);
+ return AdblockPlus::Android::NewJniJsValue(env, jsValue);
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+//JsValuePtr NewObject();
+//JsValuePtr NewCallback(v8::InvocationCallback callback);
+//static JsEnginePtr FromArguments(const v8::Arguments& arguments);
+//JsValueList ConvertArguments(const v8::Arguments& arguments);
+
+static JNINativeMethod methods[] =
+{
+{ (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor },
+{ (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
+
+{ (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEventCallback },
+{ (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRemoveEventCallback },
+{ (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerEvent },
+
+{ (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsValue"), (void*)JniEvaluate },
+
+{ (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSetDefaultFileSystem },
+{ (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem },
+{ (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem },
+{ (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest },
+{ (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest },
+
+{ (char*)"newValue", (char*)"(JJ)"TYP("JsValue"), (void*)JniNewLongValue },
+{ (char*)"newValue", (char*)"(JZ)"TYP("JsValue"), (void*)JniNewBooleanValue },
+{ (char*)"newValue", (char*)"(JLjava/lang/String;)"TYP("JsValue"), (void*)JniNewStringValue },
+
+};
+
+extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_android_api_JsEngine_registerNatives(JNIEnv *env, jclass clazz)
+{
+ env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
+}
« no previous file with comments | « jni/JniFilterEngine.cpp ('k') | jni/JniJsValue.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld