Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 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 package org.adblockplus.android; | 18 package org.adblockplus.android; |
19 | 19 |
20 import java.io.BufferedReader; | 20 import java.io.BufferedReader; |
21 import java.io.File; | 21 import java.io.File; |
22 import java.io.FileNotFoundException; | 22 import java.io.FileNotFoundException; |
23 import java.io.IOException; | 23 import java.io.IOException; |
24 import java.io.InputStream; | 24 import java.io.InputStream; |
25 import java.io.InputStreamReader; | 25 import java.io.InputStreamReader; |
26 import java.util.ArrayList; | |
26 import java.util.Calendar; | 27 import java.util.Calendar; |
28 import java.util.LinkedHashMap; | |
29 import java.util.List; | |
30 import java.util.Map; | |
27 import java.util.TimeZone; | 31 import java.util.TimeZone; |
28 import java.util.regex.Pattern; | 32 import java.util.regex.Pattern; |
29 | 33 |
30 import org.adblockplus.android.updater.AlarmReceiver; | 34 import org.adblockplus.android.updater.AlarmReceiver; |
31 | 35 |
32 import android.app.ActivityManager; | 36 import android.app.ActivityManager; |
33 import android.app.ActivityManager.RunningServiceInfo; | 37 import android.app.ActivityManager.RunningServiceInfo; |
34 import android.app.AlarmManager; | 38 import android.app.AlarmManager; |
35 import android.app.Application; | 39 import android.app.Application; |
36 import android.app.PendingIntent; | 40 import android.app.PendingIntent; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 private Subscription[] subscriptions; | 81 private Subscription[] subscriptions; |
78 | 82 |
79 /** | 83 /** |
80 * Indicates whether filtering is enabled or not. | 84 * Indicates whether filtering is enabled or not. |
81 */ | 85 */ |
82 private boolean filteringEnabled = false; | 86 private boolean filteringEnabled = false; |
83 | 87 |
84 private ABPEngine abpEngine; | 88 private ABPEngine abpEngine; |
85 | 89 |
86 private static AdblockPlus instance; | 90 private static AdblockPlus instance; |
91 | |
92 private static class ReferrerMappingCache extends LinkedHashMap<String, String > | |
93 { | |
94 private static final long serialVersionUID = 1L; | |
95 private static final int MAX_SIZE = 5000; | |
96 | |
97 public ReferrerMappingCache() | |
98 { | |
99 super(MAX_SIZE + 1, 0.75f, true); | |
100 } | |
101 | |
102 @Override | |
103 protected boolean removeEldestEntry(Map.Entry<String, String> eldest) | |
104 { | |
105 return size() > MAX_SIZE; | |
106 } | |
107 }; | |
108 | |
109 private ReferrerMappingCache referrerMapping = new ReferrerMappingCache(); | |
87 | 110 |
88 /** | 111 /** |
89 * Returns pointer to itself (singleton pattern). | 112 * Returns pointer to itself (singleton pattern). |
90 */ | 113 */ |
91 public static AdblockPlus getApplication() | 114 public static AdblockPlus getApplication() |
92 { | 115 { |
93 return instance; | 116 return instance; |
94 } | 117 } |
95 | 118 |
96 public int getBuildNumber() | 119 public int getBuildNumber() |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 * Request query string | 354 * Request query string |
332 * @param referrer | 355 * @param referrer |
333 * Request referrer header | 356 * Request referrer header |
334 * @param accept | 357 * @param accept |
335 * Request accept header | 358 * Request accept header |
336 * @return true if matched filter was found | 359 * @return true if matched filter was found |
337 * @throws Exception | 360 * @throws Exception |
338 */ | 361 */ |
339 public boolean matches(String url, String query, String referrer, String accep t) | 362 public boolean matches(String url, String query, String referrer, String accep t) |
340 { | 363 { |
364 if (referrer != null) | |
365 referrerMapping.put(url, referrer); | |
366 | |
341 if (!filteringEnabled) | 367 if (!filteringEnabled) |
342 return false; | 368 return false; |
343 | 369 |
344 String contentType = null; | 370 String contentType = null; |
345 | 371 |
346 if (accept != null) | 372 if (accept != null) |
347 { | 373 { |
348 if (accept.contains("text/css")) | 374 if (accept.contains("text/css")) |
349 contentType = "STYLESHEET"; | 375 contentType = "STYLESHEET"; |
350 else if (accept.contains("image/*")) | 376 else if (accept.contains("image/*")) |
(...skipping 10 matching lines...) Expand all Loading... | |
361 contentType = "IMAGE"; | 387 contentType = "IMAGE"; |
362 else if (RE_FONT.matcher(url).matches()) | 388 else if (RE_FONT.matcher(url).matches()) |
363 contentType = "FONT"; | 389 contentType = "FONT"; |
364 } | 390 } |
365 if (contentType == null) | 391 if (contentType == null) |
366 contentType = "OTHER"; | 392 contentType = "OTHER"; |
367 | 393 |
368 if (!"".equals(query)) | 394 if (!"".equals(query)) |
369 url = url + "?" + query; | 395 url = url + "?" + query; |
370 | 396 |
371 return abpEngine.matches(url, contentType, referrer); | 397 final List<String> referrerChain = buildReferrerChain(referrer); |
398 Log.d("Referrer chain", url + ": " + referrerChain.toString()); | |
399 String[] referrerChainArray = referrerChain.toArray(new String[referrerChain .size()]); | |
400 return abpEngine.matches(url, contentType, referrerChainArray); | |
401 } | |
402 | |
403 private List<String> buildReferrerChain(String url) | |
404 { | |
405 final List<String> referrerChain = new ArrayList<String>(); | |
406 if (url != null) | |
407 { | |
408 referrerChain.add(url); | |
409 | |
410 final String referrer = referrerMapping.get(url); | |
411 if (referrer != null) | |
412 referrerChain.addAll(buildReferrerChain(referrer)); | |
413 } | |
Wladimir Palant
2013/11/27 14:00:30
Please don't use recursive algorithms where they d
Felix Dahlke
2013/11/27 14:47:32
Valid points, fixed.
| |
414 return referrerChain; | |
372 } | 415 } |
373 | 416 |
374 /** | 417 /** |
375 * Checks if filtering is enabled. | 418 * Checks if filtering is enabled. |
376 */ | 419 */ |
377 public boolean isFilteringEnabled() | 420 public boolean isFilteringEnabled() |
378 { | 421 { |
379 return filteringEnabled; | 422 return filteringEnabled; |
380 } | 423 } |
381 | 424 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 Log.e(TAG, e.getMessage(), e); | 539 Log.e(TAG, e.getMessage(), e); |
497 } | 540 } |
498 | 541 |
499 // Set crash handler | 542 // Set crash handler |
500 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); | 543 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); |
501 | 544 |
502 // Initiate update check | 545 // Initiate update check |
503 scheduleUpdater(0); | 546 scheduleUpdater(0); |
504 } | 547 } |
505 } | 548 } |
OLD | NEW |