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

Delta Between Two Patch Sets: src/org/adblockplus/android/AdblockPlus.java

Issue 5172657418928128: Determine the frame structure (Closed)
Left Patch Set: Use LRU caching for the referrer mappings Created Nov. 27, 2013, 12:01 p.m.
Right Patch Set: Build referrer chains iteratively and limit their length Created Nov. 27, 2013, 2:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/org/adblockplus/android/ABPEngine.java ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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.ArrayList;
27 import java.util.Calendar; 27 import java.util.Calendar;
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.LinkedHashMap; 28 import java.util.LinkedHashMap;
31 import java.util.List; 29 import java.util.List;
32 import java.util.Map; 30 import java.util.Map;
33 import java.util.Set;
34 import java.util.TimeZone; 31 import java.util.TimeZone;
35 import java.util.regex.Pattern; 32 import java.util.regex.Pattern;
36 33
37 import org.adblockplus.android.updater.AlarmReceiver; 34 import org.adblockplus.android.updater.AlarmReceiver;
38 35
39 import android.app.ActivityManager; 36 import android.app.ActivityManager;
40 import android.app.ActivityManager.RunningServiceInfo; 37 import android.app.ActivityManager.RunningServiceInfo;
41 import android.app.AlarmManager; 38 import android.app.AlarmManager;
42 import android.app.Application; 39 import android.app.Application;
43 import android.app.PendingIntent; 40 import android.app.PendingIntent;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 82
86 /** 83 /**
87 * Indicates whether filtering is enabled or not. 84 * Indicates whether filtering is enabled or not.
88 */ 85 */
89 private boolean filteringEnabled = false; 86 private boolean filteringEnabled = false;
90 87
91 private ABPEngine abpEngine; 88 private ABPEngine abpEngine;
92 89
93 private static AdblockPlus instance; 90 private static AdblockPlus instance;
94 91
95 private static class ReferrerMappingCache extends LinkedHashMap<String, Set<St ring>> 92 private static class ReferrerMappingCache extends LinkedHashMap<String, String >
96 { 93 {
97 private static final long serialVersionUID = 1L; 94 private static final long serialVersionUID = 1L;
98 private static final int MAX_SIZE = 2; 95 private static final int MAX_SIZE = 5000;
Felix Dahlke 2013/11/27 12:19:33 Note that I've set this to 5000, not 2. I'd rather
Wladimir Palant 2013/11/27 12:59:01 I think that "how much memory can we sacrifice" is
Felix Dahlke 2013/11/27 13:38:59 I think we're still not on the same page here. IMO
99 96
100 public ReferrerMappingCache() 97 public ReferrerMappingCache()
101 { 98 {
102 super(MAX_SIZE, 0.75f, true); 99 super(MAX_SIZE + 1, 0.75f, true);
Wladimir Palant 2013/11/27 12:59:01 I think that the first parameter should be MAX_SIZ
Felix Dahlke 2013/11/27 13:38:59 Done.
103 } 100 }
104 101
105 @Override 102 @Override
106 protected boolean removeEldestEntry(Map.Entry<String, Set<String>> eldest) 103 protected boolean removeEldestEntry(Map.Entry<String, String> eldest)
107 { 104 {
108 return size() > MAX_SIZE; 105 return size() > MAX_SIZE;
109 } 106 }
110 }; 107 };
111 108
112 private ReferrerMappingCache referrerMapping = new ReferrerMappingCache(); 109 private ReferrerMappingCache referrerMapping = new ReferrerMappingCache();
113 110
114 /** 111 /**
115 * Returns pointer to itself (singleton pattern). 112 * Returns pointer to itself (singleton pattern).
116 */ 113 */
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 { 321 {
325 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref erences(this); 322 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref erences(this);
326 final Editor editor = preferences.edit(); 323 final Editor editor = preferences.edit();
327 editor.putBoolean("notified_about_acceptable_ads", notified); 324 editor.putBoolean("notified_about_acceptable_ads", notified);
328 editor.commit(); 325 editor.commit();
329 } 326 }
330 327
331 public boolean isNotifiedAboutAcceptableAds() 328 public boolean isNotifiedAboutAcceptableAds()
332 { 329 {
333 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref erences(this); 330 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref erences(this);
334 return preferences.getBoolean("notified_about_acceptable_ads", false); 331 return preferences.getBoolean("notified_about_acceptable_ads", false);
Felix Dahlke 2013/11/27 12:19:33 This change is unrelated, caused by rebasing.
335 } 332 }
336 333
337 /** 334 /**
338 * Returns ElemHide selectors for domain. 335 * Returns ElemHide selectors for domain.
339 * 336 *
340 * @param domain The domain 337 * @param domain The domain
341 * @return A list of CSS selectors 338 * @return A list of CSS selectors
342 */ 339 */
343 public String[] getSelectorsForDomain(final String domain) 340 public String[] getSelectorsForDomain(final String domain)
344 { 341 {
(...skipping 13 matching lines...) Expand all
358 * @param referrer 355 * @param referrer
359 * Request referrer header 356 * Request referrer header
360 * @param accept 357 * @param accept
361 * Request accept header 358 * Request accept header
362 * @return true if matched filter was found 359 * @return true if matched filter was found
363 * @throws Exception 360 * @throws Exception
364 */ 361 */
365 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)
366 { 363 {
367 if (referrer != null) 364 if (referrer != null)
368 recordReferrer(url, referrer); 365 referrerMapping.put(url, referrer);
369 366
370 if (!filteringEnabled) 367 if (!filteringEnabled)
371 return false; 368 return false;
372 369
373 String contentType = null; 370 String contentType = null;
374 371
375 if (accept != null) 372 if (accept != null)
376 { 373 {
377 if (accept.contains("text/css")) 374 if (accept.contains("text/css"))
378 contentType = "STYLESHEET"; 375 contentType = "STYLESHEET";
(...skipping 11 matching lines...) Expand all
390 contentType = "IMAGE"; 387 contentType = "IMAGE";
391 else if (RE_FONT.matcher(url).matches()) 388 else if (RE_FONT.matcher(url).matches())
392 contentType = "FONT"; 389 contentType = "FONT";
393 } 390 }
394 if (contentType == null) 391 if (contentType == null)
395 contentType = "OTHER"; 392 contentType = "OTHER";
396 393
397 if (!"".equals(query)) 394 if (!"".equals(query))
398 url = url + "?" + query; 395 url = url + "?" + query;
399 396
400 final List<List<String>> referrerChains = referrer != null 397 final List<String> referrerChain = buildReferrerChain(referrer);
401 ? buildReferrerChains(referrer) 398 Log.d("Referrer chain", url + ": " + referrerChain.toString());
402 : Collections.singletonList(Collections.<String>emptyList()); 399 String[] referrerChainArray = referrerChain.toArray(new String[referrerChain .size()]);
403 for (List<String> referrerChain : referrerChains) 400 return abpEngine.matches(url, contentType, referrerChainArray);
404 { 401 }
405 Log.d("Referrer chain", url + ": " + referrerChain.toString()); 402
406 String[] referrerChainArray = referrerChain.toArray(new String[referrerCha in.size()]); 403 private List<String> buildReferrerChain(String url)
407 if (abpEngine.matches(url, contentType, referrerChainArray)) 404 {
408 return true; 405 final List<String> referrerChain = new ArrayList<String>();
409 } 406 // We need to limit the chain length to ensure we don't block indefinitely i f there's
410 return false; 407 // a referrer loop.
411 } 408 final int maxChainLength = 10;
412 409 for (int i = 0; i < maxChainLength && url != null; i++)
413 private void recordReferrer(String url, String referrer) 410 {
414 {
415 if (!referrerMapping.containsKey(url))
416 referrerMapping.put(url, new HashSet<String>());
417 Set<String> referrers = referrerMapping.get(url);
418 referrers.add(referrer);
419 }
420
421 private List<List<String>> buildReferrerChains(String url)
422 {
423 List<List<String>> referrerChains = new ArrayList<List<String>>();
424 Set<String> referrers = referrerMapping.get(url);
425 if (referrers == null)
426 {
427 List<String> referrerChain = new ArrayList<String>();
428 referrerChain.add(url); 411 referrerChain.add(url);
429 referrerChains.add(referrerChain); 412 url = referrerMapping.get(url);
430 return referrerChains; 413 }
431 } 414 return referrerChain;
432
433 for (String referrer : referrers) {
434 List<List<String>> currentReferrerChains = buildReferrerChains(referrer);
435 for (List<String> referrerChain : currentReferrerChains)
436 {
437 referrerChain.add(0, url);
438 referrerChains.add(referrerChain);
439 }
440 }
441 return referrerChains;
442 } 415 }
443 416
444 /** 417 /**
445 * Checks if filtering is enabled. 418 * Checks if filtering is enabled.
446 */ 419 */
447 public boolean isFilteringEnabled() 420 public boolean isFilteringEnabled()
448 { 421 {
449 return filteringEnabled; 422 return filteringEnabled;
450 } 423 }
451 424
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 Log.e(TAG, e.getMessage(), e); 539 Log.e(TAG, e.getMessage(), e);
567 } 540 }
568 541
569 // Set crash handler 542 // Set crash handler
570 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); 543 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this));
571 544
572 // Initiate update check 545 // Initiate update check
573 scheduleUpdater(0); 546 scheduleUpdater(0);
574 } 547 }
575 } 548 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld