LEFT | RIGHT |
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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 { | 49 { |
50 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); | 50 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); |
51 } | 51 } |
52 | 52 |
53 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js
tring jEventName, jlong jCallbackPtr) | 53 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js
tring jEventName, jlong jCallbackPtr) |
54 { | 54 { |
55 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 55 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
56 | 56 |
57 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); | 57 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); |
58 std::string eventName = JniJavaToStdString(env, jEventName); | 58 std::string eventName = JniJavaToStdString(env, jEventName); |
59 const AdblockPlus::JsEngine::EventCallback eCallback = | 59 |
60 std::bind(&JniEventCallback::Callback, callback, std::placeholders::_1); | 60 auto eCallback = [callback](AdblockPlus::JsValueList&& params) |
| 61 { |
| 62 callback->Callback(std::move(params)); |
| 63 }; |
61 | 64 |
62 try | 65 try |
63 { | 66 { |
64 engine->SetEventCallback(eventName, eCallback); | 67 engine->SetEventCallback(eventName, eCallback); |
65 } | 68 } |
66 CATCH_AND_THROW(env) | 69 CATCH_AND_THROW(env) |
67 } | 70 } |
68 | 71 |
69 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr,
jstring jEventName) | 72 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr,
jstring jEventName) |
70 { | 73 { |
(...skipping 10 matching lines...) Expand all Loading... |
81 | 84 |
82 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring
jSource, jstring jFilename) | 85 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring
jSource, jstring jFilename) |
83 { | 86 { |
84 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 87 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
85 | 88 |
86 std::string source = JniJavaToStdString(env, jSource); | 89 std::string source = JniJavaToStdString(env, jSource); |
87 std::string filename = JniJavaToStdString(env, jFilename); | 90 std::string filename = JniJavaToStdString(env, jFilename); |
88 | 91 |
89 try | 92 try |
90 { | 93 { |
91 AdblockPlus::JsValue jsValue = engine->Evaluate(source, filename); | 94 return NewJniJsValue(env, engine->Evaluate(source, filename)); |
92 return NewJniJsValue(env, jsValue); | |
93 } | 95 } |
94 CATCH_THROW_AND_RETURN(env, 0) | 96 CATCH_THROW_AND_RETURN(env, 0) |
95 } | 97 } |
96 | 98 |
97 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin
g jEventName, jarray jJsPtrs) | 99 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin
g jEventName, jarray jJsPtrs) |
98 { | 100 { |
99 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 101 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
100 std::string eventName = JniJavaToStdString(env, jEventName); | 102 std::string eventName = JniJavaToStdString(env, jEventName); |
101 AdblockPlus::JsValueList args; | 103 AdblockPlus::JsValueList args; |
102 | 104 |
103 if (jJsPtrs) | 105 if (jJsPtrs) |
104 { | 106 { |
105 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); | 107 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); |
106 | 108 |
107 jsize length = env->GetArrayLength(jJsPtrs); | 109 jsize length = env->GetArrayLength(jJsPtrs); |
108 | 110 |
109 for (jsize i = 0; i < length; i++) | 111 for (jsize i = 0; i < length; i++) |
110 { | 112 { |
111 args.push_back(JniGetJsValue(ptrs[i])); | 113 args.push_back(JniGetJsValue(ptrs[i])); |
112 } | 114 } |
113 | 115 |
114 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); | 116 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); |
115 } | 117 } |
116 | 118 |
117 try | 119 try |
118 { | 120 { |
119 engine->TriggerEvent(eventName, args); | 121 engine->TriggerEvent(eventName, std::move(args)); |
120 } | 122 } |
121 CATCH_AND_THROW(env) | 123 CATCH_AND_THROW(env) |
122 } | 124 } |
123 | 125 |
124 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr
, jstring jBasePath) | 126 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr
, jstring jBasePath) |
125 { | 127 { |
126 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 128 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
127 | 129 |
128 try | 130 try |
129 { | 131 { |
130 const AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSyst
em()); | 132 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); |
131 | 133 |
132 std::string basePath = JniJavaToStdString(env, jBasePath); | 134 std::string basePath = JniJavaToStdString(env, jBasePath); |
133 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase
Path(basePath); | 135 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase
Path(basePath); |
134 | 136 |
135 engine->SetFileSystem(fileSystem); | 137 engine->SetFileSystem(fileSystem); |
136 } | 138 } |
137 CATCH_AND_THROW(env) | 139 CATCH_AND_THROW(env) |
138 } | 140 } |
139 | 141 |
140 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr
) | 142 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr
) |
141 { | 143 { |
142 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 144 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
143 | 145 |
144 try | 146 try |
145 { | 147 { |
146 const AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebReque
st()); | 148 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); |
147 engine->SetWebRequest(webRequest); | 149 engine->SetWebRequest(webRequest); |
148 } | 150 } |
149 CATCH_AND_THROW(env) | 151 CATCH_AND_THROW(env) |
150 } | 152 } |
151 | 153 |
152 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) | 154 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) |
153 { | 155 { |
154 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 156 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
155 | 157 |
156 try | 158 try |
157 { | 159 { |
158 const AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem(
)); | 160 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); |
159 | 161 |
160 engine->SetLogSystem(logSystem); | 162 engine->SetLogSystem(logSystem); |
161 } | 163 } |
162 CATCH_AND_THROW(env) | 164 CATCH_AND_THROW(env) |
163 } | 165 } |
164 | 166 |
165 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong
logSystemPtr) | 167 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong
logSystemPtr) |
166 { | 168 { |
167 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 169 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
168 | 170 |
169 try | 171 try |
170 { | 172 { |
171 const AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::L
ogSystemPtr>(logSystemPtr); | 173 AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::LogSyst
emPtr>(logSystemPtr); |
172 | 174 |
173 engine->SetLogSystem(logSystem); | 175 engine->SetLogSystem(logSystem); |
174 } | 176 } |
175 CATCH_AND_THROW(env) | 177 CATCH_AND_THROW(env) |
176 } | 178 } |
177 | 179 |
178 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong
webRequestPtr) | 180 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong
webRequestPtr) |
179 { | 181 { |
180 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 182 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
181 | 183 |
182 try | 184 try |
183 { | 185 { |
184 const AdblockPlus::WebRequestPtr& webRequest = | 186 AdblockPlus::WebRequestPtr& webRequest = *JniLongToTypePtr<AdblockPlus::WebR
equestPtr>(webRequestPtr); |
185 *JniLongToTypePtr<AdblockPlus::WebRequestPtr>(webRequestPtr); | |
186 | 187 |
187 engine->SetWebRequest(webRequest); | 188 engine->SetWebRequest(webRequest); |
188 } | 189 } |
189 CATCH_AND_THROW(env) | 190 CATCH_AND_THROW(env) |
190 } | 191 } |
191 | 192 |
192 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo
ng value) | 193 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo
ng value) |
193 { | 194 { |
194 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 195 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
195 | 196 |
196 try | 197 try |
197 { | 198 { |
198 AdblockPlus::JsValue jsValue = engine->NewValue(static_cast<int64_t>(value))
; | 199 return NewJniJsValue(env, engine->NewValue(static_cast<int64_t>(value))); |
199 return NewJniJsValue(env, jsValue); | |
200 } | 200 } |
201 CATCH_THROW_AND_RETURN(env, 0) | 201 CATCH_THROW_AND_RETURN(env, 0) |
202 } | 202 } |
203 | 203 |
204 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr,
jboolean value) | 204 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr,
jboolean value) |
205 { | 205 { |
206 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 206 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
207 | 207 |
208 try | 208 try |
209 { | 209 { |
210 AdblockPlus::JsValue jsValue = engine->NewValue(value == JNI_TRUE ? true : f
alse); | 210 return NewJniJsValue(env, engine->NewValue(value == JNI_TRUE ? true : false)
); |
211 return NewJniJsValue(env, jsValue); | |
212 } | 211 } |
213 CATCH_THROW_AND_RETURN(env, 0) | 212 CATCH_THROW_AND_RETURN(env, 0) |
214 } | 213 } |
215 | 214 |
216 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j
string value) | 215 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j
string value) |
217 { | 216 { |
218 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 217 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
219 | 218 |
220 try | 219 try |
221 { | 220 { |
222 std::string strValue = JniJavaToStdString(env, value); | 221 std::string strValue = JniJavaToStdString(env, value); |
223 AdblockPlus::JsValue jsValue = engine->NewValue(strValue); | 222 return NewJniJsValue(env, engine->NewValue(strValue)); |
224 return NewJniJsValue(env, jsValue); | |
225 } | 223 } |
226 CATCH_THROW_AND_RETURN(env, 0) | 224 CATCH_THROW_AND_RETURN(env, 0) |
227 } | 225 } |
228 | 226 |
229 // TODO: List of functions that lack JNI bindings | 227 // TODO: List of functions that lack JNI bindings |
230 //JsValue NewObject(); | 228 //JsValuePtr NewObject(); |
231 //JsValue NewCallback(v8::InvocationCallback callback); | 229 //JsValuePtr NewCallback(v8::InvocationCallback callback); |
232 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 230 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
233 //JsValueList ConvertArguments(const v8::Arguments& arguments); | 231 //JsValueList ConvertArguments(const v8::Arguments& arguments); |
234 | 232 |
235 static JNINativeMethod methods[] = | 233 static JNINativeMethod methods[] = |
236 { | 234 { |
237 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor }, | 235 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor }, |
238 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, | 236 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, |
239 | 237 |
240 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv
entCallback }, | 238 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv
entCallback }, |
241 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem
oveEventCallback }, | 239 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem
oveEventCallback }, |
242 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE
vent }, | 240 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE
vent }, |
243 | 241 |
244 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV
alue"), (void*)JniEvaluate }, | 242 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV
alue"), (void*)JniEvaluate }, |
245 | 243 |
246 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe
tDefaultFileSystem }, | 244 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe
tDefaultFileSystem }, |
247 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem }, | 245 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem }, |
248 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem }
, | 246 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem }
, |
249 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest }, | 247 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest }, |
250 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest
}, | 248 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest
}, |
251 | 249 |
252 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, | 250 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, |
253 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue }
, | 251 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue }
, |
254 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni
NewStringValue } | 252 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni
NewStringValue } |
255 }; | 253 }; |
256 | 254 |
257 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r
egisterNatives(JNIEnv *env, jclass clazz) | 255 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r
egisterNatives(JNIEnv *env, jclass clazz) |
258 { | 256 { |
259 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 257 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
260 } | 258 } |
LEFT | RIGHT |