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 |
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 "Utils.h" | 19 #include "Utils.h" |
20 #include "JniCallbacks.h" | 20 #include "JniCallbacks.h" |
21 | 21 |
22 static jobject SubscriptionsToArrayList(JNIEnv* env, std::vector<AdblockPlus::Su
bscription>& subscriptions) | 22 static jobject SubscriptionsToArrayList(JNIEnv* env, std::vector<AdblockPlus::Su
bscription>&& subscriptions) |
23 { | 23 { |
24 jobject list = NewJniArrayList(env); | 24 jobject list = NewJniArrayList(env); |
25 | 25 |
26 for (std::vector<AdblockPlus::Subscription>::iterator it = subscriptions.begin
(), end = subscriptions.end(); it != end; it++) | 26 for (std::vector<AdblockPlus::Subscription>::iterator it = subscriptions.begin
(), end = subscriptions.end(); it != end; it++) |
27 { | 27 { |
28 JniAddObjectToList(env, list, NewJniSubscription(env, *it)); | 28 JniAddObjectToList(env, list, NewJniSubscription(env, std::move(*it))); |
29 } | 29 } |
30 | 30 |
31 return list; | 31 return list; |
32 } | 32 } |
33 | 33 |
34 static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env, | 34 static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env, |
35 jobject jContentType) | 35 jobject jContentType) |
36 { | 36 { |
37 JniLocalReference<jclass> contentTypeClass(env, | 37 JniLocalReference<jclass> contentTypeClass(env, |
38 env->GetObjectClass(jContentType)); | 38 env->GetObjectClass(jContentType)); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 CATCH_THROW_AND_RETURN(env, JNI_FALSE); | 91 CATCH_THROW_AND_RETURN(env, JNI_FALSE); |
92 } | 92 } |
93 | 93 |
94 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin
g jText) | 94 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin
g jText) |
95 { | 95 { |
96 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); | 96 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); |
97 std::string text = JniJavaToStdString(env, jText); | 97 std::string text = JniJavaToStdString(env, jText); |
98 | 98 |
99 try | 99 try |
100 { | 100 { |
101 AdblockPlus::Filter filter = engine->GetFilter(text); | 101 return NewJniFilter(env, engine->GetFilter(text)); |
102 | |
103 return NewJniFilter(env, filter); | |
104 } | 102 } |
105 CATCH_THROW_AND_RETURN(env, 0); | 103 CATCH_THROW_AND_RETURN(env, 0); |
106 } | 104 } |
107 | 105 |
108 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr) | 106 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr) |
109 { | 107 { |
110 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); | 108 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); |
111 | 109 |
112 try | 110 try |
113 { | 111 { |
114 std::vector<AdblockPlus::Filter> filters = engine->GetListedFilters(); | 112 std::vector<AdblockPlus::Filter> filters = engine->GetListedFilters(); |
115 | 113 |
116 jobject list = NewJniArrayList(env); | 114 jobject list = NewJniArrayList(env); |
117 | 115 |
118 for (std::vector<AdblockPlus::Filter>::iterator it = filters.begin(), end =
filters.end(); it != end; it++) | 116 for (std::vector<AdblockPlus::Filter>::iterator it = filters.begin(), end =
filters.end(); it != end; it++) |
119 { | 117 { |
120 JniAddObjectToList(env, list, *JniLocalReference<jobject>(env, NewJniFilte
r(env, *it))); | 118 JniAddObjectToList(env, list, *JniLocalReference<jobject>(env, NewJniFilte
r(env, std::move(*it)))); |
121 } | 119 } |
122 | 120 |
123 return list; | 121 return list; |
124 } | 122 } |
125 CATCH_THROW_AND_RETURN(env, 0); | 123 CATCH_THROW_AND_RETURN(env, 0); |
126 } | 124 } |
127 | 125 |
128 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr,
jstring jUrl) | 126 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr,
jstring jUrl) |
129 { | 127 { |
130 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); | 128 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); |
131 std::string url = JniJavaToStdString(env, jUrl); | 129 std::string url = JniJavaToStdString(env, jUrl); |
132 | 130 |
133 try | 131 try |
134 { | 132 { |
135 AdblockPlus::Subscription subscription = engine->GetSubscription(url); | 133 return NewJniSubscription(env, engine->GetSubscription(url)); |
136 | |
137 return NewJniSubscription(env, subscription); | |
138 } | 134 } |
139 CATCH_THROW_AND_RETURN(env, 0); | 135 CATCH_THROW_AND_RETURN(env, 0); |
140 } | 136 } |
141 | 137 |
142 static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr
, jstring jUrl) | 138 static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr
, jstring jUrl) |
143 { | 139 { |
144 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); | 140 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); |
145 std::string url = JniJavaToStdString(env, jUrl); | 141 std::string url = JniJavaToStdString(env, jUrl); |
146 | 142 |
147 try | 143 try |
148 { | 144 { |
149 engine->ShowNextNotification(url); | 145 engine->ShowNextNotification(url); |
150 } | 146 } |
151 CATCH_AND_THROW(env); | 147 CATCH_AND_THROW(env); |
152 } | 148 } |
153 | 149 |
154 static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz, | 150 static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz, |
155 jlong ptr, jlong callbackPtr) | 151 jlong ptr, jlong callbackPtr) |
156 { | 152 { |
157 AdblockPlus::FilterEnginePtr& engine = | 153 AdblockPlus::FilterEnginePtr& engine = |
158 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); | 154 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); |
| 155 |
159 JniShowNotificationCallback* const callback = | 156 JniShowNotificationCallback* const callback = |
160 JniLongToTypePtr<JniShowNotificationCallback>(callbackPtr); | 157 JniLongToTypePtr<JniShowNotificationCallback>(callbackPtr); |
161 AdblockPlus::FilterEngine::ShowNotificationCallback showNotificationCallback = | 158 |
162 std::bind(&JniShowNotificationCallback::Callback, callback, | 159 auto showNotificationCallback = [callback](AdblockPlus::Notification&& notific
ation) |
163 std::placeholders::_1); | 160 { |
| 161 callback->Callback(std::move(notification)); |
| 162 }; |
| 163 |
164 try | 164 try |
165 { | 165 { |
166 engine->SetShowNotificationCallback(showNotificationCallback); | 166 engine->SetShowNotificationCallback(showNotificationCallback); |
167 } | 167 } |
168 CATCH_AND_THROW(env) | 168 CATCH_AND_THROW(env) |
169 } | 169 } |
170 | 170 |
171 static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz,
jlong ptr) | 171 static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz,
jlong ptr) |
172 { | 172 { |
173 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); | 173 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); |
174 | 174 |
175 try | 175 try |
176 { | 176 { |
177 engine->RemoveShowNotificationCallback(); | 177 engine->RemoveShowNotificationCallback(); |
178 } | 178 } |
179 CATCH_AND_THROW(env); | 179 CATCH_AND_THROW(env); |
180 } | 180 } |
181 | 181 |
182 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon
g ptr) | 182 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon
g ptr) |
183 { | 183 { |
184 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); | 184 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); |
185 | 185 |
186 try | 186 try |
187 { | 187 { |
188 std::vector<AdblockPlus::Subscription> subscriptions = engine->GetListedSubs
criptions(); | 188 return SubscriptionsToArrayList(env, engine->GetListedSubscriptions()); |
189 | |
190 return SubscriptionsToArrayList(env, subscriptions); | |
191 } | 189 } |
192 CATCH_THROW_AND_RETURN(env, 0); | 190 CATCH_THROW_AND_RETURN(env, 0); |
193 } | 191 } |
194 | 192 |
195 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz,
jlong ptr) | 193 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz,
jlong ptr) |
196 { | 194 { |
197 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); | 195 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); |
198 | 196 |
199 try | 197 try |
200 { | 198 { |
201 std::vector<AdblockPlus::Subscription> subscriptions = engine->FetchAvailabl
eSubscriptions(); | 199 return SubscriptionsToArrayList(env, engine->FetchAvailableSubscriptions()); |
202 | |
203 return SubscriptionsToArrayList(env, subscriptions); | |
204 } | 200 } |
205 CATCH_THROW_AND_RETURN(env, 0); | 201 CATCH_THROW_AND_RETURN(env, 0); |
206 } | 202 } |
207 | 203 |
208 static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz, | 204 static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz, |
209 jlong ptr) | 205 jlong ptr) |
210 { | 206 { |
211 AdblockPlus::FilterEnginePtr& engine = | 207 AdblockPlus::FilterEnginePtr& engine = |
212 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); | 208 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); |
213 try | 209 try |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } | 243 } |
248 | 244 |
249 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, | 245 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, |
250 jlong ptr, jlong filterPtr) | 246 jlong ptr, jlong filterPtr) |
251 { | 247 { |
252 AdblockPlus::FilterEnginePtr& engine = | 248 AdblockPlus::FilterEnginePtr& engine = |
253 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); | 249 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); |
254 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>( | 250 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>( |
255 filterPtr); | 251 filterPtr); |
256 | 252 |
257 const AdblockPlus::FilterEngine::FilterChangeCallback filterCallback = | 253 auto filterCallback = [callback](const std::string& arg, AdblockPlus::JsValue&
& jsValue) |
258 std::bind(&JniFilterChangeCallback::Callback, callback, | 254 { |
259 std::placeholders::_1, std::placeholders::_2); | 255 callback->Callback(arg, std::move(jsValue)); |
| 256 }; |
260 | 257 |
261 try | 258 try |
262 { | 259 { |
263 engine->SetFilterChangeCallback(filterCallback); | 260 engine->SetFilterChangeCallback(filterCallback); |
264 } | 261 } |
265 CATCH_AND_THROW(env) | 262 CATCH_AND_THROW(env) |
266 } | 263 } |
267 | 264 |
268 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl
ong updaterPtr) | 265 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl
ong updaterPtr) |
269 { | 266 { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 318 |
322 std::string url = JniJavaToStdString(env, jUrl); | 319 std::string url = JniJavaToStdString(env, jUrl); |
323 AdblockPlus::FilterEngine::ContentType contentType = | 320 AdblockPlus::FilterEngine::ContentType contentType = |
324 ConvertContentType(env, jContentType); | 321 ConvertContentType(env, jContentType); |
325 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl); | 322 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl); |
326 | 323 |
327 try | 324 try |
328 { | 325 { |
329 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen
tUrl); | 326 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen
tUrl); |
330 | 327 |
331 return (filterPtr.get() ? NewJniFilter(env, *filterPtr.get()) : 0); | 328 return filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0; |
332 } | 329 } |
333 CATCH_THROW_AND_RETURN(env, 0) | 330 CATCH_THROW_AND_RETURN(env, 0) |
334 } | 331 } |
335 | 332 |
336 static void JavaStringArrayToStringVector(JNIEnv* env, jobjectArray jArray, | 333 static void JavaStringArrayToStringVector(JNIEnv* env, jobjectArray jArray, |
337 std::vector<std::string>& out) | 334 std::vector<std::string>& out) |
338 { | 335 { |
339 if (jArray) | 336 if (jArray) |
340 { | 337 { |
341 jsize len = env->GetArrayLength(jArray); | 338 jsize len = env->GetArrayLength(jArray); |
(...skipping 19 matching lines...) Expand all Loading... |
361 AdblockPlus::FilterEngine::ContentType contentType = | 358 AdblockPlus::FilterEngine::ContentType contentType = |
362 ConvertContentType(env, jContentType); | 359 ConvertContentType(env, jContentType); |
363 | 360 |
364 std::vector<std::string> documentUrls; | 361 std::vector<std::string> documentUrls; |
365 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); | 362 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); |
366 | 363 |
367 try | 364 try |
368 { | 365 { |
369 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen
tUrls); | 366 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen
tUrls); |
370 | 367 |
371 return (filterPtr.get() ? NewJniFilter(env, *filterPtr) : 0); | 368 return (filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0); |
372 } | 369 } |
373 CATCH_THROW_AND_RETURN(env, 0) | 370 CATCH_THROW_AND_RETURN(env, 0) |
374 } | 371 } |
375 | 372 |
376 static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlon
g ptr, | 373 static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlon
g ptr, |
377 jstring jUrl, jobjectArray jDocumentUrls) | 374 jstring jUrl, jobjectArray jDocumentUrls) |
378 { | 375 { |
379 AdblockPlus::FilterEnginePtr& engine = | 376 AdblockPlus::FilterEnginePtr& engine = |
380 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); | 377 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); |
381 | 378 |
(...skipping 26 matching lines...) Expand all Loading... |
408 } | 405 } |
409 | 406 |
410 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring
jPref) | 407 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring
jPref) |
411 { | 408 { |
412 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); | 409 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); |
413 | 410 |
414 std::string pref = JniJavaToStdString(env, jPref); | 411 std::string pref = JniJavaToStdString(env, jPref); |
415 | 412 |
416 try | 413 try |
417 { | 414 { |
418 AdblockPlus::JsValue value = engine->GetPref(pref); | 415 return NewJniJsValue(env, engine->GetPref(pref)); |
419 | |
420 return NewJniJsValue(env, value); | |
421 } | 416 } |
422 CATCH_THROW_AND_RETURN(env, 0) | 417 CATCH_THROW_AND_RETURN(env, 0) |
423 } | 418 } |
424 | 419 |
425 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr
ef, jlong jsValue) | 420 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr
ef, jlong jsValue) |
426 { | 421 { |
427 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); | 422 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn
ginePtr>(ptr); |
428 | 423 |
429 std::string pref = JniJavaToStdString(env, jPref); | 424 std::string pref = JniJavaToStdString(env, jPref); |
430 const AdblockPlus::JsValue& value = JniGetJsValue(jsValue); | 425 const AdblockPlus::JsValue& value = JniGetJsValue(jsValue); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", (
void*)JniGetHostFromURL }, | 510 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", (
void*)JniGetHostFromURL }, |
516 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J
niSetAllowedConnectionType }, | 511 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J
niSetAllowedConnectionType }, |
517 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn
iGetAllowedConnectionType }, | 512 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn
iGetAllowedConnectionType }, |
518 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } | 513 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
519 }; | 514 }; |
520 | 515 |
521 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi
ne_registerNatives(JNIEnv *env, jclass clazz) | 516 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi
ne_registerNatives(JNIEnv *env, jclass clazz) |
522 { | 517 { |
523 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 518 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
524 } | 519 } |
LEFT | RIGHT |