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: Addressed another comment Created Jan. 20, 2014, 8:50 a.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') | 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-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 29 matching lines...) Expand all
69 deprecatedOptions.forEach(function(option) 75 deprecatedOptions.forEach(function(option)
70 { 76 {
71 if (option in localStorage) 77 if (option in localStorage)
72 delete localStorage[option]; 78 delete localStorage[option];
73 }); 79 });
74 } 80 }
75 81
76 // Remove deprecated options before we do anything else. 82 // Remove deprecated options before we do anything else.
77 removeDeprecatedOptions(); 83 removeDeprecatedOptions();
78 84
79 /**
80 * Checks whether a page is whitelisted.
81 * @param {String} url
82 * @param {String} [parentUrl] URL of the parent frame
83 * @param {String} [type] content type to be checked, default is "DOCUMENT"
84 * @return {Filter} filter that matched the URL or null if not whitelisted
85 */
86 function isWhitelisted(url, parentUrl, type)
87 {
88 // Ignore fragment identifier
89 var index = url.indexOf("#");
90 if (index >= 0)
91 url = url.substring(0, index);
92
93 var result = defaultMatcher.matchesAny(url, type || "DOCUMENT", extractHostFro mURL(parentUrl || url), false);
94 return (result instanceof WhitelistFilter ? result : null);
95 }
96
97 var activeNotification = null; 85 var activeNotification = null;
98 86
99 // Adds or removes browser action icon according to options. 87 // Adds or removes browser action icon according to options.
100 function refreshIconAndContextMenu(tab) 88 function refreshIconAndContextMenu(tab)
101 { 89 {
102 if(!/^https?:/.test(tab.url)) 90 if(!/^https?:/.test(tab.url))
103 return; 91 return;
104 92
105 var iconFilename; 93 var iconFilename;
106 if (require("info").platform == "safari") 94 if (require("info").platform == "safari")
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 && typeof webkitNotifications !== "undefined") 300 && typeof webkitNotifications !== "undefined")
313 { 301 {
314 var notification = webkitNotifications.createHTMLNotification("notification. html"); 302 var notification = webkitNotifications.createHTMLNotification("notification. html");
315 notification.show(); 303 notification.show();
316 notification.addEventListener("close", prepareNotificationIconAndPopup); 304 notification.addEventListener("close", prepareNotificationIconAndPopup);
317 } 305 }
318 else 306 else
319 prepareNotificationIconAndPopup(); 307 prepareNotificationIconAndPopup();
320 } 308 }
321 309
322 /**
323 * This function is a hack - we only know the tabId and document URL for a
324 * message but we need to know the frame ID. Try to find it in webRequest"s
325 * frame data.
326 */
327 function getFrameId(tab, url)
328 {
329 for (var frameId in frames.get(tab))
330 if (getFrameUrl(tab, frameId) == url)
331 return frameId;
332 return -1;
333 }
334
335 ext.onMessage.addListener(function (msg, sender, sendResponse) 310 ext.onMessage.addListener(function (msg, sender, sendResponse)
336 { 311 {
337 switch (msg.type) 312 switch (msg.type)
338 { 313 {
339 case "get-selectors": 314 case "get-selectors":
340 var selectors = null; 315 var selectors = null;
341 var frameId = sender.tab ? getFrameId(sender.tab, msg.frameUrl) : -1;
342 316
343 if (!isFrameWhitelisted(sender.tab, frameId, "DOCUMENT") && 317 if (!isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT") &&
344 !isFrameWhitelisted(sender.tab, frameId, "ELEMHIDE")) 318 !isFrameWhitelisted(sender.tab, sender.frame, "ELEMHIDE"))
345 { 319 {
346 var noStyleRules = false; 320 var noStyleRules = false;
347 var host = extractHostFromURL(msg.frameUrl); 321 var host = extractHostFromURL(sender.frame.url);
348 for (var i = 0; i < noStyleRulesHosts.length; i++) 322 for (var i = 0; i < noStyleRulesHosts.length; i++)
349 { 323 {
350 var noStyleHost = noStyleRulesHosts[i]; 324 var noStyleHost = noStyleRulesHosts[i];
351 if (host == noStyleHost || (host.length > noStyleHost.length && 325 if (host == noStyleHost || (host.length > noStyleHost.length &&
352 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost)) 326 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost))
353 { 327 {
354 noStyleRules = true; 328 noStyleRules = true;
355 } 329 }
356 } 330 }
357 selectors = ElemHide.getSelectorsForDomain(host, false); 331 selectors = ElemHide.getSelectorsForDomain(host, false);
358 if (noStyleRules) 332 if (noStyleRules)
359 { 333 {
360 selectors = selectors.filter(function(s) 334 selectors = selectors.filter(function(s)
361 { 335 {
362 return !/\[style[\^\$]?=/.test(s); 336 return !/\[style[\^\$]?=/.test(s);
363 }); 337 });
364 } 338 }
365 } 339 }
366 340
367 sendResponse(selectors); 341 sendResponse(selectors);
368 break; 342 break;
369 case "should-collapse": 343 case "should-collapse":
370 var frameId = sender.tab ? getFrameId(sender.tab, msg.documentUrl) : -1; 344 if (isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT"))
371
372 if (isFrameWhitelisted(sender.tab, frameId, "DOCUMENT"))
373 { 345 {
374 sendResponse(false); 346 sendResponse(false);
375 break; 347 break;
376 } 348 }
377 349
378 var requestHost = extractHostFromURL(msg.url); 350 var requestHost = extractHostFromURL(msg.url);
379 var documentHost = extractHostFromURL(msg.documentUrl); 351 var documentHost = extractHostFromURL(sender.frame.url);
380 var thirdParty = isThirdParty(requestHost, documentHost); 352 var thirdParty = isThirdParty(requestHost, documentHost);
381 var filter = defaultMatcher.matchesAny(msg.url, msg.mediatype, documentHos t, thirdParty); 353 var filter = defaultMatcher.matchesAny(msg.url, msg.mediatype, documentHos t, thirdParty);
382 if (filter instanceof BlockingFilter) 354 if (filter instanceof BlockingFilter)
383 { 355 {
384 var collapse = filter.collapse; 356 var collapse = filter.collapse;
385 if (collapse == null) 357 if (collapse == null)
386 collapse = Prefs.hidePlaceholders; 358 collapse = Prefs.hidePlaceholders;
387 sendResponse(collapse); 359 sendResponse(collapse);
388 } 360 }
389 else 361 else
(...skipping 14 matching lines...) Expand all
404 for (var i = 0; i < msg.filters.length; i++) 376 for (var i = 0; i < msg.filters.length; i++)
405 FilterStorage.addFilter(Filter.fromText(msg.filters[i])); 377 FilterStorage.addFilter(Filter.fromText(msg.filters[i]));
406 } 378 }
407 break; 379 break;
408 case "add-subscription": 380 case "add-subscription":
409 openOptions(function(tab) 381 openOptions(function(tab)
410 { 382 {
411 tab.sendMessage(msg); 383 tab.sendMessage(msg);
412 }); 384 });
413 break; 385 break;
386 case "add-key-exception":
387 processKeyException(msg.token, sender.tab, sender.frame);
388 break;
414 case "forward": 389 case "forward":
415 if (sender.tab) 390 if (sender.tab)
416 { 391 {
417 sender.tab.sendMessage(msg.payload, sendResponse); 392 sender.tab.sendMessage(msg.payload, sendResponse);
418 // Return true to indicate that we want to call 393 // Return true to indicate that we want to call
419 // sendResponse asynchronously 394 // sendResponse asynchronously
420 return true; 395 return true;
421 } 396 }
422 break; 397 break;
423 default: 398 default:
(...skipping 20 matching lines...) Expand all
444 tab.sendMessage({type: "clickhide-deactivate"}); 419 tab.sendMessage({type: "clickhide-deactivate"});
445 refreshIconAndContextMenu(tab); 420 refreshIconAndContextMenu(tab);
446 }); 421 });
447 422
448 setTimeout(function() 423 setTimeout(function()
449 { 424 {
450 var notificationToShow = Notification.getNextToShow(); 425 var notificationToShow = Notification.getNextToShow();
451 if (notificationToShow) 426 if (notificationToShow)
452 showNotification(notificationToShow); 427 showNotification(notificationToShow);
453 }, 3 * 60 * 1000); 428 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « no previous file | chrome/ext/background.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld