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

Side by Side Diff: libadblockplus-android/jni/JniJsEngine.cpp

Issue 29536629: Issue 5556 - Update to use libadblockplus revision hg:566f64c8a2a8 (Closed) Base URL: github.com:abby-sergz/libadblockplus-android.git
Patch Set: address comment Created Sept. 8, 2017, 12:20 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus.h> 18 #include <AdblockPlus.h>
19 #include <AdblockPlus/DefaultFileSystem.h>
20 #include "Utils.h" 19 #include "Utils.h"
21 #include "JniCallbacks.h" 20 #include "JniCallbacks.h"
22 #include "JniJsEngine.h"
23
24 static void TransformAppInfo(JNIEnv* env, jobject jAppInfo, AdblockPlus::AppInfo & appInfo)
25 {
26 jclass clazz = env->GetObjectClass(jAppInfo);
27
28 appInfo.application = JniGetStringField(env, clazz, jAppInfo, "application");
29 appInfo.applicationVersion = JniGetStringField(env, clazz, jAppInfo, "applicat ionVersion");
30 appInfo.locale = JniGetStringField(env, clazz, jAppInfo, "locale");
31 appInfo.name = JniGetStringField(env, clazz, jAppInfo, "name");
32 appInfo.version = JniGetStringField(env, clazz, jAppInfo, "version");
33
34 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme ntBuild");
35 }
36 21
37 static AdblockPlus::JsEngine& GetJsEngineRef(jlong ptr) 22 static AdblockPlus::JsEngine& GetJsEngineRef(jlong ptr)
38 { 23 {
39 return *JniLongToTypePtr<JniJsEngine>(ptr)->jsEngine; 24 return *JniLongToTypePtr<AdblockPlus::JsEngine>(ptr);
40 }
41
42 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo,
43 jobject logSystem, jobject webRequest, jstring jBasePath)
44 {
45 AdblockPlus::AppInfo appInfo;
46
47 TransformAppInfo(env, jAppInfo, appInfo);
48
49 try
50 {
51 AdblockPlus::TimerPtr timer = AdblockPlus::CreateDefaultTimer();
52 JniJsEngine* jniJsEngine = new JniJsEngine();
53 jniJsEngine->timer = timer.get();
54 jniJsEngine->jsEngine = AdblockPlus::JsEngine::New(appInfo, std::move(timer) );
55 if (logSystem)
56 {
57 jniJsEngine->jsEngine->SetLogSystem(std::make_shared<JniLogSystemCallback> (env, logSystem));
58 }
59 if (webRequest)
60 {
61 jniJsEngine->jsEngine->SetWebRequest(std::make_shared<JniWebRequest>(env, webRequest));
62 }
63 if (jBasePath)
64 {
65 auto fileSystem = std::make_shared<AdblockPlus::DefaultFileSystemSync>();
66 std::string basePath = JniJavaToStdString(env, jBasePath);
67 fileSystem->SetBasePath(basePath);
68 jniJsEngine->jsEngine->SetFileSystem(fileSystem);
69 }
70
71 return JniPtrToLong(jniJsEngine);
72 }
73 CATCH_THROW_AND_RETURN(env, 0)
74 }
75
76 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
77 {
78 delete JniLongToTypePtr<JniJsEngine>(ptr);
79 } 25 }
80 26
81 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr) 27 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr)
82 { 28 {
83 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr); 29 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
84 30
85 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); 31 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr);
86 std::string eventName = JniJavaToStdString(env, jEventName); 32 std::string eventName = JniJavaToStdString(env, jEventName);
87 33
88 auto eCallback = [callback](AdblockPlus::JsValueList&& params) 34 auto eCallback = [callback](AdblockPlus::JsValueList&& params)
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 } 132 }
187 133
188 // TODO: List of functions that lack JNI bindings 134 // TODO: List of functions that lack JNI bindings
189 //JsValuePtr NewObject(); 135 //JsValuePtr NewObject();
190 //JsValuePtr NewCallback(v8::InvocationCallback callback); 136 //JsValuePtr NewCallback(v8::InvocationCallback callback);
191 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); 137 //static JsEnginePtr FromArguments(const v8::Arguments& arguments);
192 //JsValueList ConvertArguments(const v8::Arguments& arguments); 138 //JsValueList ConvertArguments(const v8::Arguments& arguments);
193 139
194 static JNINativeMethod methods[] = 140 static JNINativeMethod methods[] =
195 { 141 {
196 { (char*)"ctor", (char*)"(" TYP("AppInfo") TYP("LogSystem") TYP("WebRequest") "Ljava/lang/String;)J", (void*)JniCtor },
197 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
198
199 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv entCallback }, 142 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv entCallback },
200 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem oveEventCallback }, 143 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem oveEventCallback },
201 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE vent }, 144 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE vent },
202 145
203 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV alue"), (void*)JniEvaluate }, 146 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV alue"), (void*)JniEvaluate },
204 147
205 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, 148 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue },
206 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } , 149 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } ,
207 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue } 150 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue }
208 }; 151 };
209 152
210 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz) 153 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz)
211 { 154 {
212 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 155 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
213 } 156 }
OLDNEW
« no previous file with comments | « libadblockplus-android/jni/JniJsEngine.h ('k') | libadblockplus-android/jni/JniNotification.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld