Left: | ||
Right: |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 13 matching lines...) Expand all Loading... | |
24 import java.util.List; | 24 import java.util.List; |
25 import java.util.Locale; | 25 import java.util.Locale; |
26 import java.util.Set; | 26 import java.util.Set; |
27 | 27 |
28 import org.adblockplus.libadblockplus.AppInfo; | 28 import org.adblockplus.libadblockplus.AppInfo; |
29 import org.adblockplus.libadblockplus.Filter; | 29 import org.adblockplus.libadblockplus.Filter; |
30 import org.adblockplus.libadblockplus.FilterChangeCallback; | 30 import org.adblockplus.libadblockplus.FilterChangeCallback; |
31 import org.adblockplus.libadblockplus.FilterEngine; | 31 import org.adblockplus.libadblockplus.FilterEngine; |
32 import org.adblockplus.libadblockplus.FilterEngine.ContentType; | 32 import org.adblockplus.libadblockplus.FilterEngine.ContentType; |
33 import org.adblockplus.libadblockplus.JsEngine; | 33 import org.adblockplus.libadblockplus.JsEngine; |
34 import org.adblockplus.libadblockplus.JsValue; | |
34 import org.adblockplus.libadblockplus.LogSystem; | 35 import org.adblockplus.libadblockplus.LogSystem; |
35 import org.adblockplus.libadblockplus.ShowNotificationCallback; | 36 import org.adblockplus.libadblockplus.ShowNotificationCallback; |
36 import org.adblockplus.libadblockplus.Subscription; | 37 import org.adblockplus.libadblockplus.Subscription; |
37 import org.adblockplus.libadblockplus.UpdateAvailableCallback; | 38 import org.adblockplus.libadblockplus.UpdateAvailableCallback; |
38 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; | 39 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; |
39 | 40 |
40 import android.content.Context; | 41 import android.content.Context; |
41 import android.content.pm.PackageInfo; | 42 import android.content.pm.PackageInfo; |
42 import android.content.pm.PackageManager.NameNotFoundException; | 43 import android.content.pm.PackageManager.NameNotFoundException; |
43 import android.os.Build.VERSION; | 44 import android.os.Build.VERSION; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 public boolean isElemhideEnabled() | 226 public boolean isElemhideEnabled() |
226 { | 227 { |
227 return this.elemhideEnabled; | 228 return this.elemhideEnabled; |
228 } | 229 } |
229 | 230 |
230 private static org.adblockplus.libadblockplus.android.Subscription convertJsSu bscription(final Subscription jsSubscription) | 231 private static org.adblockplus.libadblockplus.android.Subscription convertJsSu bscription(final Subscription jsSubscription) |
231 { | 232 { |
232 final org.adblockplus.libadblockplus.android.Subscription subscription = | 233 final org.adblockplus.libadblockplus.android.Subscription subscription = |
233 new org.adblockplus.libadblockplus.android.Subscription(); | 234 new org.adblockplus.libadblockplus.android.Subscription(); |
234 | 235 |
235 subscription.title = jsSubscription.getProperty("title").toString(); | 236 JsValue jsTitle = jsSubscription.getProperty("title"); |
diegocarloslima
2017/03/30 14:19:29
If JsValue implemented AutoCloseable, we could avo
anton
2017/03/30 14:34:15
i've been thinking about it and it makes no sense
diegocarloslima
2017/03/30 14:47:18
Yeah, he can forget on both approaches. The advant
| |
236 subscription.url = jsSubscription.getProperty("url").toString(); | 237 try |
238 { | |
239 subscription.title = jsTitle.toString(); | |
240 } | |
241 finally | |
242 { | |
243 jsTitle.dispose(); | |
244 } | |
245 | |
246 JsValue jsUrl = jsSubscription.getProperty("url"); | |
247 try | |
248 { | |
249 subscription.url = jsUrl.toString(); | |
250 } | |
251 finally | |
252 { | |
253 jsUrl.dispose(); | |
254 } | |
237 | 255 |
238 return subscription; | 256 return subscription; |
239 } | 257 } |
240 | 258 |
241 private static org.adblockplus.libadblockplus.android.Subscription[] convertJs Subscriptions( | 259 private static org.adblockplus.libadblockplus.android.Subscription[] convertJs Subscriptions( |
242 final List<Subscription> jsSubscriptions) | 260 final List<Subscription> jsSubscriptions) |
243 { | 261 { |
244 final org.adblockplus.libadblockplus.android.Subscription[] subscriptions = | 262 final org.adblockplus.libadblockplus.android.Subscription[] subscriptions = |
245 new org.adblockplus.libadblockplus.android.Subscription[jsSubscriptions.si ze()]; | 263 new org.adblockplus.libadblockplus.android.Subscription[jsSubscriptions.si ze()]; |
246 | 264 |
247 for (int i = 0; i < subscriptions.length; i++) | 265 for (int i = 0; i < subscriptions.length; i++) |
248 { | 266 { |
249 subscriptions[i] = convertJsSubscription(jsSubscriptions.get(i)); | 267 subscriptions[i] = convertJsSubscription(jsSubscriptions.get(i)); |
250 } | 268 } |
251 | 269 |
252 return subscriptions; | 270 return subscriptions; |
253 } | 271 } |
254 | 272 |
255 public org.adblockplus.libadblockplus.android.Subscription[] getRecommendedSub scriptions() | 273 public org.adblockplus.libadblockplus.android.Subscription[] getRecommendedSub scriptions() |
256 { | 274 { |
257 return convertJsSubscriptions(this.filterEngine.fetchAvailableSubscriptions( )); | 275 List<Subscription> subscriptions = this.filterEngine.fetchAvailableSubscript ions(); |
276 try | |
277 { | |
278 return convertJsSubscriptions(subscriptions); | |
279 } | |
280 finally | |
281 { | |
282 for (Subscription eachSubscription : subscriptions) | |
283 { | |
284 eachSubscription.dispose(); | |
285 } | |
286 } | |
258 } | 287 } |
259 | 288 |
260 public org.adblockplus.libadblockplus.android.Subscription[] getListedSubscrip tions() | 289 public org.adblockplus.libadblockplus.android.Subscription[] getListedSubscrip tions() |
261 { | 290 { |
262 return convertJsSubscriptions(this.filterEngine.getListedSubscriptions()); | 291 List<Subscription> subscriptions = this.filterEngine.getListedSubscriptions( ); |
292 try | |
293 { | |
294 return convertJsSubscriptions(subscriptions); | |
295 } | |
296 finally | |
297 { | |
298 for (Subscription eachSubscription : subscriptions) | |
299 { | |
300 eachSubscription.dispose(); | |
301 } | |
302 } | |
263 } | 303 } |
264 | 304 |
265 public void clearSubscriptions() | 305 public void clearSubscriptions() |
266 { | 306 { |
267 for (final Subscription s : this.filterEngine.getListedSubscriptions()) | 307 for (final Subscription s : this.filterEngine.getListedSubscriptions()) |
268 { | 308 { |
269 s.removeFromList(); | 309 try |
310 { | |
311 s.removeFromList(); | |
312 } | |
313 finally | |
314 { | |
315 s.dispose(); | |
316 } | |
270 } | 317 } |
271 } | 318 } |
272 | 319 |
273 public void setSubscription(final String url) | 320 public void setSubscription(final String url) |
274 { | 321 { |
275 clearSubscriptions(); | 322 clearSubscriptions(); |
276 | 323 |
277 final Subscription sub = this.filterEngine.getSubscription(url); | 324 final Subscription sub = this.filterEngine.getSubscription(url); |
278 if (sub != null) | 325 if (sub != null) |
279 { | 326 { |
280 sub.addToList(); | 327 try |
328 { | |
329 sub.addToList(); | |
330 } | |
331 finally | |
332 { | |
333 sub.dispose(); | |
334 } | |
281 } | 335 } |
282 } | 336 } |
283 | 337 |
284 public void setSubscriptions(Collection<String> urls) | 338 public void setSubscriptions(Collection<String> urls) |
285 { | 339 { |
286 clearSubscriptions(); | 340 clearSubscriptions(); |
287 | 341 |
288 for (String eachUrl : urls) | 342 for (String eachUrl : urls) |
289 { | 343 { |
290 final Subscription sub = this.filterEngine.getSubscription(eachUrl); | 344 final Subscription sub = this.filterEngine.getSubscription(eachUrl); |
291 if (sub != null) | 345 if (sub != null) |
292 { | 346 { |
293 sub.addToList(); | 347 try |
348 { | |
349 sub.addToList(); | |
350 } | |
351 finally | |
352 { | |
353 sub.dispose(); | |
354 } | |
294 } | 355 } |
295 } | 356 } |
296 } | 357 } |
297 | 358 |
298 public void refreshSubscriptions() | 359 public void refreshSubscriptions() |
299 { | 360 { |
300 for (final Subscription s : this.filterEngine.getListedSubscriptions()) | 361 for (final Subscription s : this.filterEngine.getListedSubscriptions()) |
301 { | 362 { |
302 s.updateFilters(); | 363 try |
364 { | |
365 s.updateFilters(); | |
366 } | |
367 finally | |
368 { | |
369 s.dispose(); | |
370 } | |
303 } | 371 } |
304 } | 372 } |
305 | 373 |
306 public boolean isAcceptableAdsEnabled() | 374 public boolean isAcceptableAdsEnabled() |
307 { | 375 { |
308 final String url = getAcceptableAdsSubscriptionURL(); | 376 final String url = getAcceptableAdsSubscriptionURL(); |
309 List<Subscription> subscriptions = this.filterEngine.getListedSubscriptions( ); | 377 List<Subscription> subscriptions = this.filterEngine.getListedSubscriptions( ); |
310 for (Subscription eachSubscription : subscriptions) | 378 try |
311 { | 379 { |
312 if (eachSubscription.getProperty("url").toString().equals(url)) | 380 for (Subscription eachSubscription : subscriptions) |
313 { | 381 { |
314 return true; | 382 JsValue jsUrl = eachSubscription.getProperty("url"); |
383 try | |
384 { | |
385 if (jsUrl.toString().equals(url)) | |
386 { | |
387 return true; | |
388 } | |
389 } | |
390 finally | |
391 { | |
392 jsUrl.dispose(); | |
393 } | |
394 } | |
395 return false; | |
396 } | |
397 finally | |
398 { | |
399 if (subscriptions != null) | |
400 { | |
401 for (Subscription eachSubscription : subscriptions) | |
402 { | |
403 eachSubscription.dispose(); | |
404 } | |
315 } | 405 } |
316 } | 406 } |
317 return false; | |
318 } | 407 } |
319 | 408 |
320 public void setEnabled(final boolean enabled) | 409 public void setEnabled(final boolean enabled) |
321 { | 410 { |
322 this.enabled = enabled; | 411 this.enabled = enabled; |
323 } | 412 } |
324 | 413 |
325 public boolean isEnabled() | 414 public boolean isEnabled() |
326 { | 415 { |
327 return enabled; | 416 return enabled; |
328 } | 417 } |
329 | 418 |
330 public String getAcceptableAdsSubscriptionURL() | 419 public String getAcceptableAdsSubscriptionURL() |
331 { | 420 { |
332 return this.filterEngine.getPref("subscriptions_exceptionsurl").toString(); | 421 JsValue jsPref = this.filterEngine.getPref("subscriptions_exceptionsurl"); |
422 try | |
423 { | |
424 return jsPref.toString(); | |
425 } | |
426 finally | |
427 { | |
428 jsPref.dispose(); | |
429 } | |
333 } | 430 } |
334 | 431 |
335 public void setAcceptableAdsEnabled(final boolean enabled) | 432 public void setAcceptableAdsEnabled(final boolean enabled) |
336 { | 433 { |
337 final String url = getAcceptableAdsSubscriptionURL(); | 434 final String url = getAcceptableAdsSubscriptionURL(); |
338 final Subscription sub = this.filterEngine.getSubscription(url); | 435 final Subscription sub = this.filterEngine.getSubscription(url); |
339 if (sub != null) | 436 if (sub != null) |
340 { | 437 { |
341 if (enabled) | 438 try |
342 { | 439 { |
343 sub.addToList(); | 440 if (enabled) |
441 { | |
442 sub.addToList(); | |
443 } | |
444 else | |
445 { | |
446 sub.removeFromList(); | |
447 } | |
344 } | 448 } |
345 else | 449 finally |
346 { | 450 { |
347 sub.removeFromList(); | 451 sub.dispose(); |
348 } | 452 } |
349 } | 453 } |
350 } | 454 } |
351 | 455 |
352 public String getDocumentationLink() | 456 public String getDocumentationLink() |
353 { | 457 { |
354 return this.filterEngine.getPref("documentation_link").toString(); | 458 JsValue jsPref = this.filterEngine.getPref("documentation_link"); |
459 try | |
460 { | |
461 return jsPref.toString(); | |
462 } | |
463 finally | |
464 { | |
465 jsPref.dispose(); | |
466 } | |
355 } | 467 } |
356 | 468 |
357 public boolean matches(final String fullUrl, final ContentType contentType, fi nal String[] referrerChainArray) | 469 public boolean matches(final String fullUrl, final ContentType contentType, fi nal String[] referrerChainArray) |
358 { | 470 { |
359 if (!enabled) | 471 if (!enabled) |
360 { | 472 { |
361 return false; | 473 return false; |
362 } | 474 } |
363 | 475 |
364 final Filter filter = this.filterEngine.matches(fullUrl, contentType, referr erChainArray); | 476 final Filter filter = this.filterEngine.matches(fullUrl, contentType, referr erChainArray); |
365 | 477 |
366 if (filter == null) | 478 if (filter == null) |
367 { | 479 { |
368 return false; | 480 return false; |
369 } | 481 } |
370 | 482 |
371 // hack: if there is no referrer, block only if filter is domain-specific | |
372 // (to re-enable in-app ads blocking, proposed on 12.11.2012 Monday meeting) | |
373 // (documentUrls contains the referrers on Android) | |
374 try | 483 try |
375 { | 484 { |
376 if (referrerChainArray.length == 0 && (filter.getProperty("text").toString ()).contains("||")) | 485 // hack: if there is no referrer, block only if filter is domain-specific |
486 // (to re-enable in-app ads blocking, proposed on 12.11.2012 Monday meetin g) | |
487 // (documentUrls contains the referrers on Android) | |
488 try | |
377 { | 489 { |
378 return false; | 490 JsValue jsText = filter.getProperty("text"); |
491 try | |
492 { | |
493 if (referrerChainArray.length == 0 && (jsText.toString()).contains("|| ")) | |
494 { | |
495 return false; | |
496 } | |
497 } | |
498 finally | |
499 { | |
500 jsText.dispose(); | |
501 } | |
379 } | 502 } |
380 } catch (NullPointerException e) { | 503 catch (NullPointerException e) |
504 { | |
505 } | |
506 | |
507 return filter.getType() != Filter.Type.EXCEPTION; | |
381 } | 508 } |
382 | 509 finally |
383 return filter.getType() != Filter.Type.EXCEPTION; | 510 { |
511 filter.dispose(); | |
512 } | |
384 } | 513 } |
385 | 514 |
386 public boolean isDocumentWhitelisted(final String url, final String[] referrer ChainArray) | 515 public boolean isDocumentWhitelisted(final String url, final String[] referrer ChainArray) |
387 { | 516 { |
388 return this.filterEngine.isDocumentWhitelisted(url, referrerChainArray); | 517 return this.filterEngine.isDocumentWhitelisted(url, referrerChainArray); |
389 } | 518 } |
390 | 519 |
391 public boolean isDomainWhitelisted(final String url, final String[] referrerCh ainArray) | 520 public boolean isDomainWhitelisted(final String url, final String[] referrerCh ainArray) |
392 { | 521 { |
393 if (whitelistedDomains == null) | 522 if (whitelistedDomains == null) |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
459 public void setWhitelistedDomains(List<String> domains) | 588 public void setWhitelistedDomains(List<String> domains) |
460 { | 589 { |
461 this.whitelistedDomains = domains; | 590 this.whitelistedDomains = domains; |
462 } | 591 } |
463 | 592 |
464 public List<String> getWhitelistedDomains() | 593 public List<String> getWhitelistedDomains() |
465 { | 594 { |
466 return whitelistedDomains; | 595 return whitelistedDomains; |
467 } | 596 } |
468 } | 597 } |
OLD | NEW |