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

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

Issue 29523682: Issue 5556 - use FilterEngine& instead of FilterEnginePtr because FilterEnginePtr is not available (Closed) Base URL: github.com:abby-sergz/libadblockplus-android.git
Patch Set: Created Aug. 22, 2017, 2:40 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 } 47 }
48 48
49 namespace 49 namespace
50 { 50 {
51 struct JniFilterEngine 51 struct JniFilterEngine
52 { 52 {
53 AdblockPlus::ITimer* timer; 53 AdblockPlus::ITimer* timer;
54 AdblockPlus::FilterEnginePtr filterEngine; 54 AdblockPlus::FilterEnginePtr filterEngine;
55 }; 55 };
56 56
57 AdblockPlus::FilterEnginePtr& GetFilterEnginePtrRef(jlong ptr) 57 AdblockPlus::FilterEngine& GetFilterEngineRef(jlong ptr)
58 { 58 {
59 return JniLongToTypePtr<JniFilterEngine>(ptr)->filterEngine; 59 return *JniLongToTypePtr<JniFilterEngine>(ptr)->filterEngine;
60 } 60 }
61 } 61 }
62 62
63 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jniJsEnginePtr, jl ong isAllowedConnectionCallbackPtr) 63 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jniJsEnginePtr, jl ong isAllowedConnectionCallbackPtr)
64 { 64 {
65 try 65 try
66 { 66 {
67 auto jniJsEngine = JniLongToTypePtr<JniJsEngine>(jniJsEnginePtr); 67 auto jniJsEngine = JniLongToTypePtr<JniJsEngine>(jniJsEnginePtr);
68 auto jsEngine = jniJsEngine->jsEngine; 68 auto jsEngine = jniJsEngine->jsEngine;
69 auto jniFilterEngine = new JniFilterEngine(); 69 auto jniFilterEngine = new JniFilterEngine();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 103
104 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 104 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
105 { 105 {
106 delete JniLongToTypePtr<JniFilterEngine>(ptr); 106 delete JniLongToTypePtr<JniFilterEngine>(ptr);
107 } 107 }
108 108
109 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr) 109 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
110 { 110 {
111 try 111 try
112 { 112 {
113 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 113 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
114 114
115 return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE; 115 return engine.IsFirstRun() ? JNI_TRUE : JNI_FALSE;
116 } 116 }
117 CATCH_THROW_AND_RETURN(env, JNI_FALSE); 117 CATCH_THROW_AND_RETURN(env, JNI_FALSE);
118 } 118 }
119 119
120 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jText) 120 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jText)
121 { 121 {
122 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 122 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
123 std::string text = JniJavaToStdString(env, jText); 123 std::string text = JniJavaToStdString(env, jText);
124 124
125 try 125 try
126 { 126 {
127 return NewJniFilter(env, engine->GetFilter(text)); 127 return NewJniFilter(env, engine.GetFilter(text));
128 } 128 }
129 CATCH_THROW_AND_RETURN(env, 0); 129 CATCH_THROW_AND_RETURN(env, 0);
130 } 130 }
131 131
132 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr) 132 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr)
133 { 133 {
134 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 134 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
135 135
136 try 136 try
137 { 137 {
138 std::vector<AdblockPlus::Filter> filters = engine->GetListedFilters(); 138 std::vector<AdblockPlus::Filter> filters = engine.GetListedFilters();
139 139
140 jobject list = NewJniArrayList(env); 140 jobject list = NewJniArrayList(env);
141 141
142 for (std::vector<AdblockPlus::Filter>::iterator it = filters.begin(), end = filters.end(); it != end; it++) 142 for (std::vector<AdblockPlus::Filter>::iterator it = filters.begin(), end = filters.end(); it != end; it++)
143 { 143 {
144 JniAddObjectToList(env, list, *JniLocalReference<jobject>(env, NewJniFilte r(env, std::move(*it)))); 144 JniAddObjectToList(env, list, *JniLocalReference<jobject>(env, NewJniFilte r(env, std::move(*it))));
145 } 145 }
146 146
147 return list; 147 return list;
148 } 148 }
149 CATCH_THROW_AND_RETURN(env, 0); 149 CATCH_THROW_AND_RETURN(env, 0);
150 } 150 }
151 151
152 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl) 152 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl)
153 { 153 {
154 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 154 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
155 std::string url = JniJavaToStdString(env, jUrl); 155 std::string url = JniJavaToStdString(env, jUrl);
156 156
157 try 157 try
158 { 158 {
159 return NewJniSubscription(env, engine->GetSubscription(url)); 159 return NewJniSubscription(env, engine.GetSubscription(url));
160 } 160 }
161 CATCH_THROW_AND_RETURN(env, 0); 161 CATCH_THROW_AND_RETURN(env, 0);
162 } 162 }
163 163
164 static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr , jstring jUrl) 164 static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr , jstring jUrl)
165 { 165 {
166 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 166 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
167 std::string url = JniJavaToStdString(env, jUrl); 167 std::string url = JniJavaToStdString(env, jUrl);
168 168
169 try 169 try
170 { 170 {
171 engine->ShowNextNotification(url); 171 engine.ShowNextNotification(url);
172 } 172 }
173 CATCH_AND_THROW(env); 173 CATCH_AND_THROW(env);
174 } 174 }
175 175
176 static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz, 176 static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz,
177 jlong ptr, jlong callbackPtr) 177 jlong ptr, jlong callbackPtr)
178 { 178 {
179 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 179 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
180 180
181 JniShowNotificationCallback* const callback = 181 JniShowNotificationCallback* const callback =
182 JniLongToTypePtr<JniShowNotificationCallback>(callbackPtr); 182 JniLongToTypePtr<JniShowNotificationCallback>(callbackPtr);
183 183
184 auto showNotificationCallback = [callback](AdblockPlus::Notification&& notific ation) 184 auto showNotificationCallback = [callback](AdblockPlus::Notification&& notific ation)
185 { 185 {
186 callback->Callback(std::move(notification)); 186 callback->Callback(std::move(notification));
187 }; 187 };
188 188
189 try 189 try
190 { 190 {
191 engine->SetShowNotificationCallback(showNotificationCallback); 191 engine.SetShowNotificationCallback(showNotificationCallback);
192 } 192 }
193 CATCH_AND_THROW(env) 193 CATCH_AND_THROW(env)
194 } 194 }
195 195
196 static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz, jlong ptr) 196 static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz, jlong ptr)
197 { 197 {
198 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 198 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
199 199
200 try 200 try
201 { 201 {
202 engine->RemoveShowNotificationCallback(); 202 engine.RemoveShowNotificationCallback();
203 } 203 }
204 CATCH_AND_THROW(env); 204 CATCH_AND_THROW(env);
205 } 205 }
206 206
207 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon g ptr) 207 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon g ptr)
208 { 208 {
209 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 209 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
210 210
211 try 211 try
212 { 212 {
213 return SubscriptionsToArrayList(env, engine->GetListedSubscriptions()); 213 return SubscriptionsToArrayList(env, engine.GetListedSubscriptions());
214 } 214 }
215 CATCH_THROW_AND_RETURN(env, 0); 215 CATCH_THROW_AND_RETURN(env, 0);
216 } 216 }
217 217
218 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz, jlong ptr) 218 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz, jlong ptr)
219 { 219 {
220 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 220 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
221 221
222 try 222 try
223 { 223 {
224 return SubscriptionsToArrayList(env, engine->FetchAvailableSubscriptions()); 224 return SubscriptionsToArrayList(env, engine.FetchAvailableSubscriptions());
225 } 225 }
226 CATCH_THROW_AND_RETURN(env, 0); 226 CATCH_THROW_AND_RETURN(env, 0);
227 } 227 }
228 228
229 static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz, 229 static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz,
230 jlong ptr) 230 jlong ptr)
231 { 231 {
232 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 232 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
233 try 233 try
234 { 234 {
235 engine->RemoveUpdateAvailableCallback(); 235 engine.RemoveUpdateAvailableCallback();
236 } 236 }
237 CATCH_AND_THROW(env) 237 CATCH_AND_THROW(env)
238 } 238 }
239 239
240 static void JNICALL JniSetUpdateAvailableCallback(JNIEnv* env, jclass clazz, 240 static void JNICALL JniSetUpdateAvailableCallback(JNIEnv* env, jclass clazz,
241 jlong ptr, jlong callbackPtr) 241 jlong ptr, jlong callbackPtr)
242 { 242 {
243 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 243 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
244 JniUpdateAvailableCallback* const callback = 244 JniUpdateAvailableCallback* const callback =
245 JniLongToTypePtr<JniUpdateAvailableCallback>(callbackPtr); 245 JniLongToTypePtr<JniUpdateAvailableCallback>(callbackPtr);
246 246
247 const AdblockPlus::FilterEngine::UpdateAvailableCallback updateAvailableCallba ck = 247 const AdblockPlus::FilterEngine::UpdateAvailableCallback updateAvailableCallba ck =
248 std::bind(&JniUpdateAvailableCallback::Callback, callback, 248 std::bind(&JniUpdateAvailableCallback::Callback, callback,
249 std::placeholders::_1); 249 std::placeholders::_1);
250 try 250 try
251 { 251 {
252 engine->SetUpdateAvailableCallback(updateAvailableCallback); 252 engine.SetUpdateAvailableCallback(updateAvailableCallback);
253 } 253 }
254 CATCH_AND_THROW(env) 254 CATCH_AND_THROW(env)
255 } 255 }
256 256
257 static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo ng ptr) 257 static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo ng ptr)
258 { 258 {
259 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 259 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
260 260
261 try 261 try
262 { 262 {
263 engine->RemoveFilterChangeCallback(); 263 engine.RemoveFilterChangeCallback();
264 } 264 }
265 CATCH_AND_THROW(env) 265 CATCH_AND_THROW(env)
266 } 266 }
267 267
268 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, 268 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz,
269 jlong ptr, jlong filterPtr) 269 jlong ptr, jlong filterPtr)
270 { 270 {
271 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 271 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
272 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>( 272 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>(
273 filterPtr); 273 filterPtr);
274 274
275 auto filterCallback = [callback](const std::string& arg, AdblockPlus::JsValue& & jsValue) 275 auto filterCallback = [callback](const std::string& arg, AdblockPlus::JsValue& & jsValue)
276 { 276 {
277 callback->Callback(arg, std::move(jsValue)); 277 callback->Callback(arg, std::move(jsValue));
278 }; 278 };
279 279
280 try 280 try
281 { 281 {
282 engine->SetFilterChangeCallback(filterCallback); 282 engine.SetFilterChangeCallback(filterCallback);
283 } 283 }
284 CATCH_AND_THROW(env) 284 CATCH_AND_THROW(env)
285 } 285 }
286 286
287 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr) 287 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr)
288 { 288 {
289 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 289 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
290 JniUpdateCheckDoneCallback* callback = 290 JniUpdateCheckDoneCallback* callback =
291 JniLongToTypePtr<JniUpdateCheckDoneCallback>(updaterPtr); 291 JniLongToTypePtr<JniUpdateCheckDoneCallback>(updaterPtr);
292 292
293 AdblockPlus::FilterEngine::UpdateCheckDoneCallback 293 AdblockPlus::FilterEngine::UpdateCheckDoneCallback
294 updateCheckDoneCallback = 0; 294 updateCheckDoneCallback = 0;
295 295
296 if (updaterPtr) 296 if (updaterPtr)
297 { 297 {
298 updateCheckDoneCallback = 298 updateCheckDoneCallback =
299 std::bind(&JniUpdateCheckDoneCallback::Callback, callback, 299 std::bind(&JniUpdateCheckDoneCallback::Callback, callback,
300 std::placeholders::_1); 300 std::placeholders::_1);
301 } 301 }
302 302
303 try 303 try
304 { 304 {
305 engine->ForceUpdateCheck(updateCheckDoneCallback); 305 engine.ForceUpdateCheck(updateCheckDoneCallback);
306 } 306 }
307 CATCH_AND_THROW(env) 307 CATCH_AND_THROW(env)
308 } 308 }
309 309
310 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, 310 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz,
311 jlong ptr, jstring jDomain) 311 jlong ptr, jstring jDomain)
312 { 312 {
313 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 313 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
314 314
315 std::string domain = JniJavaToStdString(env, jDomain); 315 std::string domain = JniJavaToStdString(env, jDomain);
316 316
317 try 317 try
318 { 318 {
319 std::vector<std::string> selectors = engine->GetElementHidingSelectors( 319 std::vector<std::string> selectors = engine.GetElementHidingSelectors(
320 domain); 320 domain);
321 321
322 jobject list = NewJniArrayList(env); 322 jobject list = NewJniArrayList(env);
323 323
324 for (std::vector<std::string>::iterator it = selectors.begin(), end = 324 for (std::vector<std::string>::iterator it = selectors.begin(), end =
325 selectors.end(); it != end; it++) 325 selectors.end(); it != end; it++)
326 { 326 {
327 JniAddObjectToList(env, list, 327 JniAddObjectToList(env, list,
328 *JniLocalReference<jstring>(env, env->NewStringUTF(it->c_str()))); 328 *JniLocalReference<jstring>(env, env->NewStringUTF(it->c_str())));
329 } 329 }
330 330
331 return list; 331 return list;
332 } 332 }
333 CATCH_THROW_AND_RETURN(env, 0) 333 CATCH_THROW_AND_RETURN(env, 0)
334 } 334 }
335 335
336 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl) 336 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl)
337 { 337 {
338 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 338 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
339 339
340 std::string url = JniJavaToStdString(env, jUrl); 340 std::string url = JniJavaToStdString(env, jUrl);
341 AdblockPlus::FilterEngine::ContentType contentType = 341 AdblockPlus::FilterEngine::ContentType contentType =
342 ConvertContentType(env, jContentType); 342 ConvertContentType(env, jContentType);
343 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl); 343 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl);
344 344
345 try 345 try
346 { 346 {
347 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrl); 347 AdblockPlus::FilterPtr filterPtr = engine.Matches(url, contentType, document Url);
348 348
349 return filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0; 349 return filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0;
350 } 350 }
351 CATCH_THROW_AND_RETURN(env, 0) 351 CATCH_THROW_AND_RETURN(env, 0)
352 } 352 }
353 353
354 static void JavaStringArrayToStringVector(JNIEnv* env, jobjectArray jArray, 354 static void JavaStringArrayToStringVector(JNIEnv* env, jobjectArray jArray,
355 std::vector<std::string>& out) 355 std::vector<std::string>& out)
356 { 356 {
357 if (jArray) 357 if (jArray)
358 { 358 {
359 jsize len = env->GetArrayLength(jArray); 359 jsize len = env->GetArrayLength(jArray);
360 360
361 for (jsize i = 0; i < len; i++) 361 for (jsize i = 0; i < len; i++)
362 { 362 {
363 out.push_back( 363 out.push_back(
364 JniJavaToStdString(env, 364 JniJavaToStdString(env,
365 *JniLocalReference<jstring>(env, 365 *JniLocalReference<jstring>(env,
366 static_cast<jstring>( 366 static_cast<jstring>(
367 env->GetObjectArrayElement(jArray, i))))); 367 env->GetObjectArrayElement(jArray, i)))));
368 } 368 }
369 } 369 }
370 } 370 }
371 371
372 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr, 372 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr,
373 jstring jUrl, jobject jContentType, jobjectArray jDocumentUrls) 373 jstring jUrl, jobject jContentType, jobjectArray jDocumentUrls)
374 { 374 {
375 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 375 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
376 376
377 std::string url = JniJavaToStdString(env, jUrl); 377 std::string url = JniJavaToStdString(env, jUrl);
378 AdblockPlus::FilterEngine::ContentType contentType = 378 AdblockPlus::FilterEngine::ContentType contentType =
379 ConvertContentType(env, jContentType); 379 ConvertContentType(env, jContentType);
380 380
381 std::vector<std::string> documentUrls; 381 std::vector<std::string> documentUrls;
382 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); 382 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls);
383 383
384 try 384 try
385 { 385 {
386 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrls); 386 AdblockPlus::FilterPtr filterPtr = engine.Matches(url, contentType, document Urls);
387 387
388 return (filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0); 388 return (filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0);
389 } 389 }
390 CATCH_THROW_AND_RETURN(env, 0) 390 CATCH_THROW_AND_RETURN(env, 0)
391 } 391 }
392 392
393 static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr, 393 static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr,
394 jstring jUrl, jobjectArray jDocumentUrls) 394 jstring jUrl, jobjectArray jDocumentUrls)
395 { 395 {
396 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 396 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
397 397
398 std::string url = JniJavaToStdString(env, jUrl); 398 std::string url = JniJavaToStdString(env, jUrl);
399 std::vector<std::string> documentUrls; 399 std::vector<std::string> documentUrls;
400 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); 400 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls);
401 try 401 try
402 { 402 {
403 return engine->IsDocumentWhitelisted(url, documentUrls) ? 403 return engine.IsDocumentWhitelisted(url, documentUrls) ?
404 JNI_TRUE : JNI_FALSE; 404 JNI_TRUE : JNI_FALSE;
405 } 405 }
406 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 406 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
407 } 407 }
408 408
409 static jboolean JNICALL JniIsElemhideWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr, 409 static jboolean JNICALL JniIsElemhideWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr,
410 jstring jUrl, jobjectArray jDocumentUrls) 410 jstring jUrl, jobjectArray jDocumentUrls)
411 { 411 {
412 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 412 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
413 413
414 std::string url = JniJavaToStdString(env, jUrl); 414 std::string url = JniJavaToStdString(env, jUrl);
415 std::vector<std::string> documentUrls; 415 std::vector<std::string> documentUrls;
416 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); 416 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls);
417 try 417 try
418 { 418 {
419 return engine->IsElemhideWhitelisted(url, documentUrls) ? 419 return engine.IsElemhideWhitelisted(url, documentUrls) ?
420 JNI_TRUE : JNI_FALSE; 420 JNI_TRUE : JNI_FALSE;
421 } 421 }
422 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 422 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
423 } 423 }
424 424
425 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref) 425 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref)
426 { 426 {
427 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 427 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
428 428
429 std::string pref = JniJavaToStdString(env, jPref); 429 std::string pref = JniJavaToStdString(env, jPref);
430 430
431 try 431 try
432 { 432 {
433 return NewJniJsValue(env, engine->GetPref(pref)); 433 return NewJniJsValue(env, engine.GetPref(pref));
434 } 434 }
435 CATCH_THROW_AND_RETURN(env, 0) 435 CATCH_THROW_AND_RETURN(env, 0)
436 } 436 }
437 437
438 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr ef, jlong jsValue) 438 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr ef, jlong jsValue)
439 { 439 {
440 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 440 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
441 441
442 std::string pref = JniJavaToStdString(env, jPref); 442 std::string pref = JniJavaToStdString(env, jPref);
443 const AdblockPlus::JsValue& value = JniGetJsValue(jsValue); 443 const AdblockPlus::JsValue& value = JniGetJsValue(jsValue);
444 444
445 try 445 try
446 { 446 {
447 engine->SetPref(pref, value); 447 engine.SetPref(pref, value);
448 } 448 }
449 CATCH_AND_THROW(env) 449 CATCH_AND_THROW(env)
450 } 450 }
451 451
452 static jstring JNICALL JniGetHostFromURL(JNIEnv* env, jclass clazz, jlong ptr, j string jurl) 452 static jstring JNICALL JniGetHostFromURL(JNIEnv* env, jclass clazz, jlong ptr, j string jurl)
453 { 453 {
454 if (jurl == NULL) 454 if (jurl == NULL)
455 { 455 {
456 return NULL; 456 return NULL;
457 } 457 }
458 458
459 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 459 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
460 460
461 std::string url = JniJavaToStdString(env, jurl); 461 std::string url = JniJavaToStdString(env, jurl);
462 try 462 try
463 { 463 {
464 std::string host = engine->GetHostFromURL(url); 464 std::string host = engine.GetHostFromURL(url);
465 465
466 return JniStdStringToJava(env, host); 466 return JniStdStringToJava(env, host);
467 } 467 }
468 CATCH_THROW_AND_RETURN(env, 0) 468 CATCH_THROW_AND_RETURN(env, 0)
469 } 469 }
470 470
471 static void JNICALL JniSetAllowedConnectionType(JNIEnv* env, jclass clazz, jlong ptr, jstring jvalue) 471 static void JNICALL JniSetAllowedConnectionType(JNIEnv* env, jclass clazz, jlong ptr, jstring jvalue)
472 { 472 {
473 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 473 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
474 474
475 std::string stdValue; 475 std::string stdValue;
476 const std::string* value = (jvalue != NULL 476 const std::string* value = (jvalue != NULL
477 ? &(stdValue = JniJavaToStdString(env, jvalue)) 477 ? &(stdValue = JniJavaToStdString(env, jvalue))
478 : NULL); 478 : NULL);
479 479
480 try 480 try
481 { 481 {
482 engine->SetAllowedConnectionType(value); 482 engine.SetAllowedConnectionType(value);
483 } 483 }
484 CATCH_AND_THROW(env) 484 CATCH_AND_THROW(env)
485 } 485 }
486 486
487 static jstring JNICALL JniGetAllowedConnectionType(JNIEnv* env, jclass clazz, jl ong ptr) 487 static jstring JNICALL JniGetAllowedConnectionType(JNIEnv* env, jclass clazz, jl ong ptr)
488 { 488 {
489 try 489 try
490 { 490 {
491 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 491 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
492 std::unique_ptr<std::string> value = engine->GetAllowedConnectionType(); 492 std::unique_ptr<std::string> value = engine.GetAllowedConnectionType();
493 493
494 if (value == NULL) 494 if (value == NULL)
495 { 495 {
496 return NULL; 496 return NULL;
497 } 497 }
498 498
499 return JniStdStringToJava(env, *value.get()); 499 return JniStdStringToJava(env, *value.get());
500 } 500 }
501 CATCH_THROW_AND_RETURN(env, 0) 501 CATCH_THROW_AND_RETURN(env, 0)
502 } 502 }
503 503
504 static void JNICALL JniSetAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlong ptr, jboolean jvalue) 504 static void JNICALL JniSetAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlong ptr, jboolean jvalue)
505 { 505 {
506 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 506 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
507 507
508 try 508 try
509 { 509 {
510 engine->SetAAEnabled(jvalue == JNI_TRUE); 510 engine.SetAAEnabled(jvalue == JNI_TRUE);
511 } 511 }
512 CATCH_AND_THROW(env) 512 CATCH_AND_THROW(env)
513 } 513 }
514 514
515 static jboolean JNICALL JniIsAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlo ng ptr) 515 static jboolean JNICALL JniIsAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlo ng ptr)
516 { 516 {
517 try 517 try
518 { 518 {
519 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 519 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
520 return engine->IsAAEnabled() ? JNI_TRUE : JNI_FALSE; 520 return engine.IsAAEnabled() ? JNI_TRUE : JNI_FALSE;
521 } 521 }
522 CATCH_THROW_AND_RETURN(env, 0) 522 CATCH_THROW_AND_RETURN(env, 0)
523 } 523 }
524 524
525 static jstring JNICALL JniGetAcceptableAdsSubscriptionURL(JNIEnv* env, jclass cl azz, jlong ptr) 525 static jstring JNICALL JniGetAcceptableAdsSubscriptionURL(JNIEnv* env, jclass cl azz, jlong ptr)
526 { 526 {
527 try 527 try
528 { 528 {
529 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 529 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
530 std::string url = engine->GetAAUrl(); 530 std::string url = engine.GetAAUrl();
531 return JniStdStringToJava(env, url); 531 return JniStdStringToJava(env, url);
532 } 532 }
533 CATCH_THROW_AND_RETURN(env, 0) 533 CATCH_THROW_AND_RETURN(env, 0)
534 } 534 }
535 535
536 static void JNICALL JniUpdateFiltersAsync(JNIEnv* env, jclass clazz, jlong ptr, jstring jSubscriptionUrl) 536 static void JNICALL JniUpdateFiltersAsync(JNIEnv* env, jclass clazz, jlong ptr, jstring jSubscriptionUrl)
537 { 537 {
538 std::string subscriptionUrl = JniJavaToStdString(env, jSubscriptionUrl); 538 std::string subscriptionUrl = JniJavaToStdString(env, jSubscriptionUrl);
539 auto jniFilterEngine = JniLongToTypePtr<JniFilterEngine>(ptr); 539 auto jniFilterEngine = JniLongToTypePtr<JniFilterEngine>(ptr);
540 std::weak_ptr<AdblockPlus::FilterEngine> weakFilterEngine = jniFilterEngine->f ilterEngine; 540 jniFilterEngine->timer->SetTimer(std::chrono::milliseconds(0), [jniFilterEngin e, subscriptionUrl]
541 jniFilterEngine->timer->SetTimer(std::chrono::milliseconds(0), [weakFilterEngi ne, subscriptionUrl]
542 { 541 {
543 auto filterEngine = weakFilterEngine.lock(); 542 auto& filterEngine = *jniFilterEngine->filterEngine;
544 if (!filterEngine) 543 for (auto& subscription : filterEngine.GetListedSubscriptions())
545 {
546 return;
547 }
548 for (auto& subscription : filterEngine->GetListedSubscriptions())
549 { 544 {
550 if (stringBeginsWith(subscriptionUrl, subscription.GetProperty("url").AsSt ring())) 545 if (stringBeginsWith(subscriptionUrl, subscription.GetProperty("url").AsSt ring()))
551 { 546 {
552 subscription.UpdateFilters(); 547 subscription.UpdateFilters();
553 return; 548 return;
554 } 549 }
555 } 550 }
556 }); 551 });
557 } 552 }
558 553
(...skipping 28 matching lines...) Expand all
587 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled }, 582 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled },
588 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL }, 583 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL },
589 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, 584 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
590 { (char*)"updateFiltersAsync", (char*)"(JLjava/lang/String;)V", (void*)JniUpda teFiltersAsync } 585 { (char*)"updateFiltersAsync", (char*)"(JLjava/lang/String;)V", (void*)JniUpda teFiltersAsync }
591 }; 586 };
592 587
593 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz) 588 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz)
594 { 589 {
595 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 590 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
596 } 591 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld