| OLD | NEW |
| 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 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme
ntBuild"); | 34 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme
ntBuild"); |
| 35 } | 35 } |
| 36 | 36 |
| 37 static AdblockPlus::JsEngine& GetJsEngineRef(jlong ptr) | 37 static AdblockPlus::JsEngine& GetJsEngineRef(jlong ptr) |
| 38 { | 38 { |
| 39 return *JniLongToTypePtr<JniJsEngine>(ptr)->jsEngine; | 39 return *JniLongToTypePtr<JniJsEngine>(ptr)->jsEngine; |
| 40 } | 40 } |
| 41 | 41 |
| 42 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo, | 42 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo, |
| 43 jobject logSystem, jobject webRequest) | 43 jobject logSystem, jobject webRequest, jstring jBasePath) |
| 44 { | 44 { |
| 45 AdblockPlus::AppInfo appInfo; | 45 AdblockPlus::AppInfo appInfo; |
| 46 | 46 |
| 47 TransformAppInfo(env, jAppInfo, appInfo); | 47 TransformAppInfo(env, jAppInfo, appInfo); |
| 48 | 48 |
| 49 try | 49 try |
| 50 { | 50 { |
| 51 AdblockPlus::TimerPtr timer = AdblockPlus::CreateDefaultTimer(); | 51 AdblockPlus::TimerPtr timer = AdblockPlus::CreateDefaultTimer(); |
| 52 JniJsEngine* jniJsEngine = new JniJsEngine(); | 52 JniJsEngine* jniJsEngine = new JniJsEngine(); |
| 53 jniJsEngine->timer = timer.get(); | 53 jniJsEngine->timer = timer.get(); |
| 54 jniJsEngine->jsEngine = AdblockPlus::JsEngine::New(appInfo, std::move(timer)
); | 54 jniJsEngine->jsEngine = AdblockPlus::JsEngine::New(appInfo, std::move(timer)
); |
| 55 if (logSystem) | 55 if (logSystem) |
| 56 { | 56 { |
| 57 jniJsEngine->jsEngine->SetLogSystem(std::make_shared<JniLogSystemCallback>
(env, logSystem)); | 57 jniJsEngine->jsEngine->SetLogSystem(std::make_shared<JniLogSystemCallback>
(env, logSystem)); |
| 58 } | 58 } |
| 59 if (webRequest) | 59 if (webRequest) |
| 60 { | 60 { |
| 61 jniJsEngine->jsEngine->SetWebRequest(std::make_shared<JniWebRequest>(env,
webRequest)); | 61 jniJsEngine->jsEngine->SetWebRequest(std::make_shared<JniWebRequest>(env,
webRequest)); |
| 62 } | 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 } |
| 63 | 70 |
| 64 return JniPtrToLong(jniJsEngine); | 71 return JniPtrToLong(jniJsEngine); |
| 65 } | 72 } |
| 66 CATCH_THROW_AND_RETURN(env, 0) | 73 CATCH_THROW_AND_RETURN(env, 0) |
| 67 } | 74 } |
| 68 | 75 |
| 69 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) | 76 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
| 70 { | 77 { |
| 71 delete JniLongToTypePtr<JniJsEngine>(ptr); | 78 delete JniLongToTypePtr<JniJsEngine>(ptr); |
| 72 } | 79 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); | 144 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); |
| 138 } | 145 } |
| 139 | 146 |
| 140 try | 147 try |
| 141 { | 148 { |
| 142 engine.TriggerEvent(eventName, std::move(args)); | 149 engine.TriggerEvent(eventName, std::move(args)); |
| 143 } | 150 } |
| 144 CATCH_AND_THROW(env) | 151 CATCH_AND_THROW(env) |
| 145 } | 152 } |
| 146 | 153 |
| 147 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr
, jstring jBasePath) | |
| 148 { | |
| 149 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr); | |
| 150 | |
| 151 try | |
| 152 { | |
| 153 auto fileSystem = std::make_shared<AdblockPlus::DefaultFileSystemSync>(); | |
| 154 | |
| 155 std::string basePath = JniJavaToStdString(env, jBasePath); | |
| 156 fileSystem->SetBasePath(basePath); | |
| 157 | |
| 158 engine.SetFileSystem(fileSystem); | |
| 159 } | |
| 160 CATCH_AND_THROW(env) | |
| 161 } | |
| 162 | |
| 163 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo
ng value) | 154 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo
ng value) |
| 164 { | 155 { |
| 165 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr); | 156 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr); |
| 166 | 157 |
| 167 try | 158 try |
| 168 { | 159 { |
| 169 return NewJniJsValue(env, engine.NewValue(static_cast<int64_t>(value))); | 160 return NewJniJsValue(env, engine.NewValue(static_cast<int64_t>(value))); |
| 170 } | 161 } |
| 171 CATCH_THROW_AND_RETURN(env, 0) | 162 CATCH_THROW_AND_RETURN(env, 0) |
| 172 } | 163 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 195 } | 186 } |
| 196 | 187 |
| 197 // TODO: List of functions that lack JNI bindings | 188 // TODO: List of functions that lack JNI bindings |
| 198 //JsValuePtr NewObject(); | 189 //JsValuePtr NewObject(); |
| 199 //JsValuePtr NewCallback(v8::InvocationCallback callback); | 190 //JsValuePtr NewCallback(v8::InvocationCallback callback); |
| 200 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 191 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
| 201 //JsValueList ConvertArguments(const v8::Arguments& arguments); | 192 //JsValueList ConvertArguments(const v8::Arguments& arguments); |
| 202 | 193 |
| 203 static JNINativeMethod methods[] = | 194 static JNINativeMethod methods[] = |
| 204 { | 195 { |
| 205 { (char*)"ctor", (char*)"(" TYP("AppInfo") TYP("LogSystem") TYP("WebRequest")
")J", (void*)JniCtor }, | 196 { (char*)"ctor", (char*)"(" TYP("AppInfo") TYP("LogSystem") TYP("WebRequest")
"Ljava/lang/String;)J", (void*)JniCtor }, |
| 206 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, | 197 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, |
| 207 | 198 |
| 208 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv
entCallback }, | 199 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv
entCallback }, |
| 209 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem
oveEventCallback }, | 200 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem
oveEventCallback }, |
| 210 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE
vent }, | 201 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE
vent }, |
| 211 | 202 |
| 212 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV
alue"), (void*)JniEvaluate }, | 203 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV
alue"), (void*)JniEvaluate }, |
| 213 | 204 |
| 214 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe
tDefaultFileSystem }, | |
| 215 | |
| 216 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, | 205 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, |
| 217 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue }
, | 206 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue }
, |
| 218 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni
NewStringValue } | 207 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni
NewStringValue } |
| 219 }; | 208 }; |
| 220 | 209 |
| 221 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r
egisterNatives(JNIEnv *env, jclass clazz) | 210 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r
egisterNatives(JNIEnv *env, jclass clazz) |
| 222 { | 211 { |
| 223 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 212 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
| 224 } | 213 } |
| OLD | NEW |