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

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

Issue 5641446598115328: Move referrer mapping to a dedicated class (Closed)
Patch Set: Created July 15, 2014, 12:58 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 | src/org/adblockplus/android/ReferrerMapping.java » ('j') | 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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 private Subscription[] subscriptions; 86 private Subscription[] subscriptions;
87 /** 87 /**
88 * Indicates whether filtering is enabled or not. 88 * Indicates whether filtering is enabled or not.
89 */ 89 */
90 private boolean filteringEnabled = false; 90 private boolean filteringEnabled = false;
91 91
92 private ABPEngine abpEngine; 92 private ABPEngine abpEngine;
93 93
94 private static AdblockPlus instance; 94 private static AdblockPlus instance;
95 95
96 private static class ReferrerMappingCache extends LinkedHashMap<String, String > 96 private final ReferrerMapping referrerMapping = new ReferrerMapping();
97 {
98 private static final long serialVersionUID = 1L;
99 private static final int MAX_SIZE = 5000;
100
101 public ReferrerMappingCache()
102 {
103 super(MAX_SIZE + 1, 0.75f, true);
104 }
105
106 @Override
107 protected boolean removeEldestEntry(final Map.Entry<String, String> eldest)
108 {
109 return size() > MAX_SIZE;
110 }
111 };
112
113 private final ReferrerMappingCache referrerMapping = new ReferrerMappingCache( );
114 97
115 /** 98 /**
116 * Returns pointer to itself (singleton pattern). 99 * Returns pointer to itself (singleton pattern).
117 */ 100 */
118 public static AdblockPlus getApplication() 101 public static AdblockPlus getApplication()
119 { 102 {
120 return instance; 103 return instance;
121 } 104 }
122 105
123 public int getBuildNumber() 106 public int getBuildNumber()
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 * Request referrer header 335 * Request referrer header
353 * @param accept 336 * @param accept
354 * Request accept header 337 * Request accept header
355 * @return true if matched filter was found 338 * @return true if matched filter was found
356 * @throws Exception 339 * @throws Exception
357 */ 340 */
358 public boolean matches(final String url, final String query, final String refe rrer, final String accept) 341 public boolean matches(final String url, final String query, final String refe rrer, final String accept)
359 { 342 {
360 final String fullUrl = !"".equals(query) ? url + "?" + query : url; 343 final String fullUrl = !"".equals(query) ? url + "?" + query : url;
361 if (referrer != null) 344 if (referrer != null)
362 referrerMapping.put(fullUrl, referrer); 345 referrerMapping.add(fullUrl, referrer);
363 346
364 if (!filteringEnabled) 347 if (!filteringEnabled)
365 return false; 348 return false;
366 349
367 String contentType = null; 350 String contentType = null;
368 351
369 if (accept != null) 352 if (accept != null)
370 { 353 {
371 if (accept.contains("text/css")) 354 if (accept.contains("text/css"))
372 contentType = "STYLESHEET"; 355 contentType = "STYLESHEET";
(...skipping 12 matching lines...) Expand all
385 else if (RE_IMAGE.matcher(url).find()) 368 else if (RE_IMAGE.matcher(url).find())
386 contentType = "IMAGE"; 369 contentType = "IMAGE";
387 else if (RE_FONT.matcher(url).find()) 370 else if (RE_FONT.matcher(url).find())
388 contentType = "FONT"; 371 contentType = "FONT";
389 else if (RE_HTML.matcher(url).find()) 372 else if (RE_HTML.matcher(url).find())
390 contentType = "SUBDOCUMENT"; 373 contentType = "SUBDOCUMENT";
391 } 374 }
392 if (contentType == null) 375 if (contentType == null)
393 contentType = "OTHER"; 376 contentType = "OTHER";
394 377
395 final List<String> referrerChain = buildReferrerChain(referrer); 378 final List<String> referrerChain = referrerMapping.buildReferrerChain(referr er);
396 Log.d("Referrer chain", fullUrl + ": " + referrerChain.toString()); 379 Log.d("Referrer chain", fullUrl + ": " + referrerChain.toString());
397 final String[] referrerChainArray = referrerChain.toArray(new String[referre rChain.size()]); 380 final String[] referrerChainArray = referrerChain.toArray(new String[referre rChain.size()]);
398 return abpEngine.matches(fullUrl, contentType, referrerChainArray); 381 return abpEngine.matches(fullUrl, contentType, referrerChainArray);
399 } 382 }
400 383
401 private List<String> buildReferrerChain(String url)
402 {
403 final List<String> referrerChain = new ArrayList<String>();
404 // We need to limit the chain length to ensure we don't block indefinitely i f there's
405 // a referrer loop.
406 final int maxChainLength = 10;
407 for (int i = 0; i < maxChainLength && url != null; i++)
408 {
409 referrerChain.add(0, url);
410 url = referrerMapping.get(url);
411 }
412 return referrerChain;
413 }
414
415 /** 384 /**
416 * Checks if filtering is enabled. 385 * Checks if filtering is enabled.
417 */ 386 */
418 public boolean isFilteringEnabled() 387 public boolean isFilteringEnabled()
419 { 388 {
420 return filteringEnabled; 389 return filteringEnabled;
421 } 390 }
422 391
423 /** 392 /**
424 * Enables or disables filtering. 393 * Enables or disables filtering.
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 Log.e(TAG, e.getMessage(), e); 505 Log.e(TAG, e.getMessage(), e);
537 } 506 }
538 507
539 // Set crash handler 508 // Set crash handler
540 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); 509 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this));
541 510
542 // Initiate update check 511 // Initiate update check
543 scheduleUpdater(0); 512 scheduleUpdater(0);
544 } 513 }
545 } 514 }
OLDNEW
« no previous file with comments | « no previous file | src/org/adblockplus/android/ReferrerMapping.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld