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

Side by Side Diff: background.js

Issue 6346177440120832: Added abstraction for frames, to fix domain-based rules, whitelisting and ad counter on Safari (Closed)
Patch Set: Created Dec. 21, 2013, 7:48 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 | chrome/ext/background.js » ('j') | chrome/ext/background.js » ('J')
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-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 with(require("filterClasses")) 18 with(require("filterClasses"))
19 { 19 {
20 this.Filter = Filter; 20 this.Filter = Filter;
21 this.RegExpFilter = RegExpFilter; 21 this.RegExpFilter = RegExpFilter;
22 this.BlockingFilter = BlockingFilter; 22 this.BlockingFilter = BlockingFilter;
23 this.WhitelistFilter = WhitelistFilter; 23 this.WhitelistFilter = WhitelistFilter;
24 } 24 }
25 with(require("subscriptionClasses")) 25 with(require("subscriptionClasses"))
26 { 26 {
27 this.Subscription = Subscription; 27 this.Subscription = Subscription;
28 this.DownloadableSubscription = DownloadableSubscription; 28 this.DownloadableSubscription = DownloadableSubscription;
29 } 29 }
30 with(require("whitelisting"))
31 {
32 this.isWhitelisted = isWhitelisted;
33 this.isFrameWhitelisted = isFrameWhitelisted;
34 this.processKeyException = processKeyException;
35 }
30 var FilterStorage = require("filterStorage").FilterStorage; 36 var FilterStorage = require("filterStorage").FilterStorage;
31 var ElemHide = require("elemHide").ElemHide; 37 var ElemHide = require("elemHide").ElemHide;
32 var defaultMatcher = require("matcher").defaultMatcher; 38 var defaultMatcher = require("matcher").defaultMatcher;
33 var Prefs = require("prefs").Prefs; 39 var Prefs = require("prefs").Prefs;
34 var Synchronizer = require("synchronizer").Synchronizer; 40 var Synchronizer = require("synchronizer").Synchronizer;
35 var Utils = require("utils").Utils; 41 var Utils = require("utils").Utils;
36 var Notification = require("notification").Notification; 42 var Notification = require("notification").Notification;
37 43
38 // Some types cannot be distinguished 44 // Some types cannot be distinguished
39 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 45 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 } 89 }
84 90
85 defaultOptionValue("shouldShowBlockElementMenu", "true"); 91 defaultOptionValue("shouldShowBlockElementMenu", "true");
86 92
87 removeDeprecatedOptions(); 93 removeDeprecatedOptions();
88 } 94 }
89 95
90 // Upgrade options before we do anything else. 96 // Upgrade options before we do anything else.
91 setDefaultOptions(); 97 setDefaultOptions();
92 98
93 /**
94 * Checks whether a page is whitelisted.
95 * @param {String} url
96 * @param {String} [parentUrl] URL of the parent frame
97 * @param {String} [type] content type to be checked, default is "DOCUMENT"
98 * @return {Filter} filter that matched the URL or null if not whitelisted
99 */
100 function isWhitelisted(url, parentUrl, type)
101 {
102 // Ignore fragment identifier
103 var index = url.indexOf("#");
104 if (index >= 0)
105 url = url.substring(0, index);
106
107 var result = defaultMatcher.matchesAny(url, type || "DOCUMENT", extractHostFro mURL(parentUrl || url), false);
108 return (result instanceof WhitelistFilter ? result : null);
109 }
110
111 var activeNotification = null; 99 var activeNotification = null;
112 100
113 // Adds or removes browser action icon according to options. 101 // Adds or removes browser action icon according to options.
114 function refreshIconAndContextMenu(tab) 102 function refreshIconAndContextMenu(tab)
115 { 103 {
116 if(!/^https?:/.test(tab.url)) 104 if(!/^https?:/.test(tab.url))
117 return; 105 return;
118 106
119 var iconFilename; 107 var iconFilename;
120 if (require("info").platform == "safari") 108 if (require("info").platform == "safari")
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 && typeof webkitNotifications !== "undefined") 310 && typeof webkitNotifications !== "undefined")
323 { 311 {
324 var notification = webkitNotifications.createHTMLNotification("notification. html"); 312 var notification = webkitNotifications.createHTMLNotification("notification. html");
325 notification.show(); 313 notification.show();
326 notification.addEventListener("close", prepareNotificationIconAndPopup); 314 notification.addEventListener("close", prepareNotificationIconAndPopup);
327 } 315 }
328 else 316 else
329 prepareNotificationIconAndPopup(); 317 prepareNotificationIconAndPopup();
330 } 318 }
331 319
332 /**
333 * This function is a hack - we only know the tabId and document URL for a
334 * message but we need to know the frame ID. Try to find it in webRequest"s
335 * frame data.
336 */
337 function getFrameId(tab, url)
338 {
339 for (var frameId in frames.get(tab))
340 if (getFrameUrl(tab, frameId) == url)
341 return frameId;
342 return -1;
343 }
344
345 ext.onMessage.addListener(function (msg, sender, sendResponse) 320 ext.onMessage.addListener(function (msg, sender, sendResponse)
346 { 321 {
347 switch (msg.type) 322 switch (msg.type)
348 { 323 {
349 case "get-selectors": 324 case "get-selectors":
350 var selectors = null; 325 var selectors = null;
351 var frameId = sender.tab ? getFrameId(sender.tab, msg.frameUrl) : -1;
352 326
353 if (!isFrameWhitelisted(sender.tab, frameId, "DOCUMENT") && 327 if (!isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT") &&
354 !isFrameWhitelisted(sender.tab, frameId, "ELEMHIDE")) 328 !isFrameWhitelisted(sender.tab, sender.frame, "ELEMHIDE"))
355 { 329 {
356 var noStyleRules = false; 330 var noStyleRules = false;
357 var host = extractHostFromURL(msg.frameUrl); 331 var host = extractHostFromURL(sender.frame.url);
358 for (var i = 0; i < noStyleRulesHosts.length; i++) 332 for (var i = 0; i < noStyleRulesHosts.length; i++)
359 { 333 {
360 var noStyleHost = noStyleRulesHosts[i]; 334 var noStyleHost = noStyleRulesHosts[i];
361 if (host == noStyleHost || (host.length > noStyleHost.length && 335 if (host == noStyleHost || (host.length > noStyleHost.length &&
362 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost)) 336 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost))
363 { 337 {
364 noStyleRules = true; 338 noStyleRules = true;
365 } 339 }
366 } 340 }
367 selectors = ElemHide.getSelectorsForDomain(host, false); 341 selectors = ElemHide.getSelectorsForDomain(host, false);
368 if (noStyleRules) 342 if (noStyleRules)
369 { 343 {
370 selectors = selectors.filter(function(s) 344 selectors = selectors.filter(function(s)
371 { 345 {
372 return !/\[style[\^\$]?=/.test(s); 346 return !/\[style[\^\$]?=/.test(s);
373 }); 347 });
374 } 348 }
375 } 349 }
376 350
377 sendResponse(selectors); 351 sendResponse(selectors);
378 break; 352 break;
379 case "should-collapse": 353 case "should-collapse":
380 var frameId = sender.tab ? getFrameId(sender.tab, msg.documentUrl) : -1; 354 if (isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT"))
381
382 if (isFrameWhitelisted(sender.tab, frameId, "DOCUMENT"))
383 { 355 {
384 sendResponse(false); 356 sendResponse(false);
385 break; 357 break;
386 } 358 }
387 359
388 var requestHost = extractHostFromURL(msg.url); 360 var requestHost = extractHostFromURL(msg.url);
389 var documentHost = extractHostFromURL(msg.documentUrl); 361 var documentHost = extractHostFromURL(sender.frame.url);
390 var thirdParty = isThirdParty(requestHost, documentHost); 362 var thirdParty = isThirdParty(requestHost, documentHost);
391 var filter = defaultMatcher.matchesAny(msg.url, msg.mediatype, documentHos t, thirdParty); 363 var filter = defaultMatcher.matchesAny(msg.url, msg.mediatype, documentHos t, thirdParty);
392 if (filter instanceof BlockingFilter) 364 if (filter instanceof BlockingFilter)
393 { 365 {
394 var collapse = filter.collapse; 366 var collapse = filter.collapse;
395 if (collapse == null) 367 if (collapse == null)
396 collapse = (localStorage.hidePlaceholders != "false"); 368 collapse = (localStorage.hidePlaceholders != "false");
397 sendResponse(collapse); 369 sendResponse(collapse);
398 } 370 }
399 else 371 else
(...skipping 14 matching lines...) Expand all
414 for (var i = 0; i < msg.filters.length; i++) 386 for (var i = 0; i < msg.filters.length; i++)
415 FilterStorage.addFilter(Filter.fromText(msg.filters[i])); 387 FilterStorage.addFilter(Filter.fromText(msg.filters[i]));
416 } 388 }
417 break; 389 break;
418 case "add-subscription": 390 case "add-subscription":
419 openOptions(function(tab) 391 openOptions(function(tab)
420 { 392 {
421 tab.sendMessage(msg); 393 tab.sendMessage(msg);
422 }); 394 });
423 break; 395 break;
396 case "add-key-exception":
397 processKeyException(msg.token, sender.tab, sender.frame);
398 break;
424 case "forward": 399 case "forward":
425 if (sender.tab) 400 if (sender.tab)
426 { 401 {
427 sender.tab.sendMessage(msg.payload, sendResponse); 402 sender.tab.sendMessage(msg.payload, sendResponse);
428 // Return true to indicate that we want to call 403 // Return true to indicate that we want to call
429 // sendResponse asynchronously 404 // sendResponse asynchronously
430 return true; 405 return true;
431 } 406 }
432 break; 407 break;
433 default: 408 default:
(...skipping 20 matching lines...) Expand all
454 tab.sendMessage({type: "clickhide-deactivate"}); 429 tab.sendMessage({type: "clickhide-deactivate"});
455 refreshIconAndContextMenu(tab); 430 refreshIconAndContextMenu(tab);
456 }); 431 });
457 432
458 setTimeout(function() 433 setTimeout(function()
459 { 434 {
460 var notificationToShow = Notification.getNextToShow(); 435 var notificationToShow = Notification.getNextToShow();
461 if (notificationToShow) 436 if (notificationToShow)
462 showNotification(notificationToShow); 437 showNotification(notificationToShow);
463 }, 3 * 60 * 1000); 438 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « no previous file | chrome/ext/background.js » ('j') | chrome/ext/background.js » ('J')

Powered by Google App Engine
This is Rietveld