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

Side by Side Diff: src/org/adblockplus/android/AdblockPlus.java

Issue 5172657418928128: Determine the frame structure (Closed)
Patch Set: Use LRU caching for the referrer mappings Created Nov. 27, 2013, 12:01 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
OLDNEW
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.Collections;
29 import java.util.HashSet;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
27 import java.util.TimeZone; 34 import java.util.TimeZone;
28 import java.util.regex.Pattern; 35 import java.util.regex.Pattern;
29 36
30 import org.adblockplus.android.updater.AlarmReceiver; 37 import org.adblockplus.android.updater.AlarmReceiver;
31 38
32 import android.app.ActivityManager; 39 import android.app.ActivityManager;
33 import android.app.ActivityManager.RunningServiceInfo; 40 import android.app.ActivityManager.RunningServiceInfo;
34 import android.app.AlarmManager; 41 import android.app.AlarmManager;
35 import android.app.Application; 42 import android.app.Application;
36 import android.app.PendingIntent; 43 import android.app.PendingIntent;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 private Subscription[] subscriptions; 84 private Subscription[] subscriptions;
78 85
79 /** 86 /**
80 * Indicates whether filtering is enabled or not. 87 * Indicates whether filtering is enabled or not.
81 */ 88 */
82 private boolean filteringEnabled = false; 89 private boolean filteringEnabled = false;
83 90
84 private ABPEngine abpEngine; 91 private ABPEngine abpEngine;
85 92
86 private static AdblockPlus instance; 93 private static AdblockPlus instance;
94
95 private static class ReferrerMappingCache extends LinkedHashMap<String, Set<St ring>>
96 {
97 private static final long serialVersionUID = 1L;
98 private static final int MAX_SIZE = 2;
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
100 public ReferrerMappingCache()
101 {
102 super(MAX_SIZE, 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 }
104
105 @Override
106 protected boolean removeEldestEntry(Map.Entry<String, Set<String>> eldest)
107 {
108 return size() > MAX_SIZE;
109 }
110 };
111
112 private ReferrerMappingCache referrerMapping = new ReferrerMappingCache();
87 113
88 /** 114 /**
89 * Returns pointer to itself (singleton pattern). 115 * Returns pointer to itself (singleton pattern).
90 */ 116 */
91 public static AdblockPlus getApplication() 117 public static AdblockPlus getApplication()
92 { 118 {
93 return instance; 119 return instance;
94 } 120 }
95 121
96 public int getBuildNumber() 122 public int getBuildNumber()
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 { 324 {
299 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref erences(this); 325 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref erences(this);
300 final Editor editor = preferences.edit(); 326 final Editor editor = preferences.edit();
301 editor.putBoolean("notified_about_acceptable_ads", notified); 327 editor.putBoolean("notified_about_acceptable_ads", notified);
302 editor.commit(); 328 editor.commit();
303 } 329 }
304 330
305 public boolean isNotifiedAboutAcceptableAds() 331 public boolean isNotifiedAboutAcceptableAds()
306 { 332 {
307 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref erences(this); 333 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref erences(this);
308 return preferences.getBoolean("notified_about_acceptable_ads", false); 334 return preferences.getBoolean("notified_about_acceptable_ads", false);
Felix Dahlke 2013/11/27 12:19:33 This change is unrelated, caused by rebasing.
309 } 335 }
310 336
311 /** 337 /**
312 * Returns ElemHide selectors for domain. 338 * Returns ElemHide selectors for domain.
313 * 339 *
314 * @param domain The domain 340 * @param domain The domain
315 * @return A list of CSS selectors 341 * @return A list of CSS selectors
316 */ 342 */
317 public String[] getSelectorsForDomain(final String domain) 343 public String[] getSelectorsForDomain(final String domain)
318 { 344 {
(...skipping 12 matching lines...) Expand all
331 * Request query string 357 * Request query string
332 * @param referrer 358 * @param referrer
333 * Request referrer header 359 * Request referrer header
334 * @param accept 360 * @param accept
335 * Request accept header 361 * Request accept header
336 * @return true if matched filter was found 362 * @return true if matched filter was found
337 * @throws Exception 363 * @throws Exception
338 */ 364 */
339 public boolean matches(String url, String query, String referrer, String accep t) 365 public boolean matches(String url, String query, String referrer, String accep t)
340 { 366 {
367 if (referrer != null)
368 recordReferrer(url, referrer);
369
341 if (!filteringEnabled) 370 if (!filteringEnabled)
342 return false; 371 return false;
343 372
344 String contentType = null; 373 String contentType = null;
345 374
346 if (accept != null) 375 if (accept != null)
347 { 376 {
348 if (accept.contains("text/css")) 377 if (accept.contains("text/css"))
349 contentType = "STYLESHEET"; 378 contentType = "STYLESHEET";
350 else if (accept.contains("image/*")) 379 else if (accept.contains("image/*"))
(...skipping 10 matching lines...) Expand all
361 contentType = "IMAGE"; 390 contentType = "IMAGE";
362 else if (RE_FONT.matcher(url).matches()) 391 else if (RE_FONT.matcher(url).matches())
363 contentType = "FONT"; 392 contentType = "FONT";
364 } 393 }
365 if (contentType == null) 394 if (contentType == null)
366 contentType = "OTHER"; 395 contentType = "OTHER";
367 396
368 if (!"".equals(query)) 397 if (!"".equals(query))
369 url = url + "?" + query; 398 url = url + "?" + query;
370 399
371 return abpEngine.matches(url, contentType, referrer); 400 final List<List<String>> referrerChains = referrer != null
401 ? buildReferrerChains(referrer)
402 : Collections.singletonList(Collections.<String>emptyList());
403 for (List<String> referrerChain : referrerChains)
404 {
405 Log.d("Referrer chain", url + ": " + referrerChain.toString());
406 String[] referrerChainArray = referrerChain.toArray(new String[referrerCha in.size()]);
407 if (abpEngine.matches(url, contentType, referrerChainArray))
408 return true;
409 }
410 return false;
411 }
412
413 private void recordReferrer(String url, String referrer)
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);
429 referrerChains.add(referrerChain);
430 return referrerChains;
431 }
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;
372 } 442 }
373 443
374 /** 444 /**
375 * Checks if filtering is enabled. 445 * Checks if filtering is enabled.
376 */ 446 */
377 public boolean isFilteringEnabled() 447 public boolean isFilteringEnabled()
378 { 448 {
379 return filteringEnabled; 449 return filteringEnabled;
380 } 450 }
381 451
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 Log.e(TAG, e.getMessage(), e); 566 Log.e(TAG, e.getMessage(), e);
497 } 567 }
498 568
499 // Set crash handler 569 // Set crash handler
500 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); 570 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this));
501 571
502 // Initiate update check 572 // Initiate update check
503 scheduleUpdater(0); 573 scheduleUpdater(0);
504 } 574 }
505 } 575 }
OLDNEW
« src/org/adblockplus/android/ABPEngine.java ('K') | « src/org/adblockplus/android/ABPEngine.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld