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

Side by Side Diff: background.js

Issue 5464830253203456: Refactored the abstraction layer to address prerendered pages on Safari caused by leaky abstraction (Closed)
Patch Set: Rebased and addressed comments Created April 7, 2014, 1:14 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') | 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 72
73 if (canUseChromeNotifications) 73 if (canUseChromeNotifications)
74 initChromeNotifications(); 74 initChromeNotifications();
75 initAntiAdblockNotification(); 75 initAntiAdblockNotification();
76 } 76 }
77 77
78 // update browser actions when whitelisting might have changed, 78 // update browser actions when whitelisting might have changed,
79 // due to loading filters or saving filter changes 79 // due to loading filters or saving filter changes
80 if (action == "load" || action == "save") 80 if (action == "load" || action == "save")
81 { 81 {
82 ext.windows.getAll(function(windows) 82 ext.pages.query({}, function(pages)
83 { 83 {
84 for (var i = 0; i < windows.length; i++) 84 pages.forEach(refreshIconAndContextMenu);
85 {
86 windows[i].getAllTabs(function(tabs)
87 {
88 tabs.forEach(refreshIconAndContextMenu);
89 });
90 }
91 }); 85 });
92 } 86 }
93 }); 87 });
94 88
95 // Special-case domains for which we cannot use style-based hiding rules. 89 // Special-case domains for which we cannot use style-based hiding rules.
96 // See http://crbug.com/68705. 90 // See http://crbug.com/68705.
97 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"]; 91 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"];
98 92
99 function removeDeprecatedOptions() 93 function removeDeprecatedOptions()
100 { 94 {
101 var deprecatedOptions = ["specialCaseYouTube", "experimental", "disableInlineT extAds"]; 95 var deprecatedOptions = ["specialCaseYouTube", "experimental", "disableInlineT extAds"];
102 deprecatedOptions.forEach(function(option) 96 deprecatedOptions.forEach(function(option)
103 { 97 {
104 if (option in ext.storage) 98 if (option in ext.storage)
105 delete ext.storage[option]; 99 delete ext.storage[option];
106 }); 100 });
107 } 101 }
108 102
109 // Remove deprecated options before we do anything else. 103 // Remove deprecated options before we do anything else.
110 removeDeprecatedOptions(); 104 removeDeprecatedOptions();
111 105
112 var activeNotification = null; 106 var activeNotification = null;
113 107
114 // Adds or removes browser action icon according to options. 108 // Adds or removes browser action icon according to options.
115 function refreshIconAndContextMenu(tab) 109 function refreshIconAndContextMenu(page)
116 { 110 {
117 var whitelisted = isWhitelisted(tab.url); 111 var whitelisted = isWhitelisted(page.url);
118 112
119 var iconFilename; 113 var iconFilename;
120 if (whitelisted && require("info").platform != "safari") 114 if (whitelisted && require("info").platform != "safari")
121 // There is no grayscale version of the icon for whitelisted tabs 115 // There is no grayscale version of the icon for whitelisted pages
122 // when using Safari, because icons are grayscale already and icons 116 // when using Safari, because icons are grayscale already and icons
123 // aren't per tab in Safari. 117 // aren't per page in Safari.
124 iconFilename = "icons/abp-$size-whitelisted.png"; 118 iconFilename = "icons/abp-$size-whitelisted.png";
125 else 119 else
126 iconFilename = "icons/abp-$size.png"; 120 iconFilename = "icons/abp-$size.png";
127 121
128 tab.browserAction.setIcon(iconFilename); 122 page.browserAction.setIcon(iconFilename);
129 iconAnimation.registerTab(tab, iconFilename); 123 iconAnimation.registerPage(page, iconFilename);
130 124
131 // Set context menu status according to whether current tab has whitelisted do main 125 // show or hide the context menu entry dependent on whether
132 if (whitelisted || !/^https?:/.test(tab.url)) 126 // adblocking is active on that page
127 if (whitelisted || !/^https?:/.test(page.url))
133 ext.contextMenus.hideMenuItems(); 128 ext.contextMenus.hideMenuItems();
134 else 129 else
135 ext.contextMenus.showMenuItems(); 130 ext.contextMenus.showMenuItems();
136 } 131 }
137 132
138 /** 133 /**
139 * Old versions for Opera stored patterns.ini in the localStorage object, this 134 * Old versions for Opera stored patterns.ini in the localStorage object, this
140 * will import it into FilterStorage properly. 135 * will import it into FilterStorage properly.
141 * @return {Boolean} true if data import is in progress 136 * @return {Boolean} true if data import is in progress
142 */ 137 */
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 FilterStorage.addSubscription(subscription); 215 FilterStorage.addSubscription(subscription);
221 if (subscription instanceof DownloadableSubscription && !subscription.lastDo wnload) 216 if (subscription instanceof DownloadableSubscription && !subscription.lastDo wnload)
222 Synchronizer.execute(subscription); 217 Synchronizer.execute(subscription);
223 } 218 }
224 219
225 if (!addSubscription && !addAcceptable) 220 if (!addSubscription && !addAcceptable)
226 return; 221 return;
227 222
228 function notifyUser() 223 function notifyUser()
229 { 224 {
230 ext.windows.getLastFocused(function(win) 225 ext.pages.open(ext.getURL("firstRun.html"));
231 {
232 win.openTab(ext.getURL("firstRun.html"));
233 });
234 } 226 }
235 227
236 if (addSubscription) 228 if (addSubscription)
237 { 229 {
238 // Load subscriptions data 230 // Load subscriptions data
239 var request = new XMLHttpRequest(); 231 var request = new XMLHttpRequest();
240 request.open("GET", "subscriptions.xml"); 232 request.open("GET", "subscriptions.xml");
241 request.addEventListener("load", function() 233 request.addEventListener("load", function()
242 { 234 {
243 var node = Utils.chooseFilterSubscription(request.responseXML.getElementsB yTagName("subscription")); 235 var node = Utils.chooseFilterSubscription(request.responseXML.getElementsB yTagName("subscription"));
(...skipping 14 matching lines...) Expand all
258 } 250 }
259 else 251 else
260 notifyUser(); 252 notifyUser();
261 } 253 }
262 254
263 function setContextMenu() 255 function setContextMenu()
264 { 256 {
265 if (Prefs.shouldShowBlockElementMenu) 257 if (Prefs.shouldShowBlockElementMenu)
266 { 258 {
267 // Register context menu item 259 // Register context menu item
268 ext.contextMenus.addMenuItem(ext.i18n.getMessage("block_element"), ["image", "video", "audio"], function(srcUrl, tab) 260 ext.contextMenus.addMenuItem(ext.i18n.getMessage("block_element"), ["image", "video", "audio"], function(srcUrl, page)
269 { 261 {
270 if (srcUrl) 262 if (srcUrl)
271 tab.sendMessage({type: "clickhide-new-filter", filter: srcUrl}); 263 page.sendMessage({type: "clickhide-new-filter", filter: srcUrl});
272 }); 264 });
273 } 265 }
274 else 266 else
275 ext.contextMenus.removeMenuItems(); 267 ext.contextMenus.removeMenuItems();
276 } 268 }
277 269
278 Prefs.addListener(function(name) 270 Prefs.addListener(function(name)
279 { 271 {
280 if (name == "shouldShowBlockElementMenu") 272 if (name == "shouldShowBlockElementMenu")
281 setContextMenu(); 273 setContextMenu();
282 }); 274 });
283 setContextMenu(); 275 setContextMenu();
284 276
285 /** 277 /**
286 * Opens options tab or focuses an existing one, within the last focused window . 278 * Opens options page or focuses an existing one, within the last focused windo w.
287 * @param {Function} callback function to be called with the 279 * @param {Function} callback function to be called with the
288 Tab object of the options tab 280 Page object of the options page
289 */ 281 */
290 function openOptions(callback) 282 function openOptions(callback)
291 { 283 {
292 ext.windows.getLastFocused(function(win) 284 ext.pages.query({lastFocusedWindow: true}, function(pages)
293 { 285 {
294 win.getAllTabs(function(tabs) 286 var optionsUrl = ext.getURL("options.html");
287
288 for (var i = 0; i < pages.length; i++)
295 { 289 {
296 var optionsUrl = ext.getURL("options.html"); 290 var page = pages[i];
291 if (page.url == optionsUrl)
292 {
293 page.activate();
294 if (callback)
295 callback(page);
296 return;
297 }
298 }
297 299
298 for (var i = 0; i < tabs.length; i++) 300 ext.pages.open(optionsUrl, callback);
299 {
300 if (tabs[i].url == optionsUrl)
301 {
302 tabs[i].activate();
303 if (callback)
304 callback(tabs[i]);
305 return;
306 }
307 }
308
309 win.openTab(optionsUrl, callback && function(tab)
310 {
311 tab.onCompleted.addListener(callback);
312 });
313 });
314 }); 301 });
315 } 302 }
316 303
317 function prepareNotificationIconAndPopup() 304 function prepareNotificationIconAndPopup()
318 { 305 {
319 var animateIcon = (activeNotification.type !== "question"); 306 var animateIcon = (activeNotification.type !== "question");
320 activeNotification.onClicked = function() 307 activeNotification.onClicked = function()
321 { 308 {
322 if (animateIcon) 309 if (animateIcon)
323 iconAnimation.stop(); 310 iconAnimation.stop();
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 prepareNotificationIconAndPopup(); 471 prepareNotificationIconAndPopup();
485 } 472 }
486 473
487 ext.onMessage.addListener(function (msg, sender, sendResponse) 474 ext.onMessage.addListener(function (msg, sender, sendResponse)
488 { 475 {
489 switch (msg.type) 476 switch (msg.type)
490 { 477 {
491 case "get-selectors": 478 case "get-selectors":
492 var selectors = null; 479 var selectors = null;
493 480
494 if (!isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT") && 481 if (!isFrameWhitelisted(sender.page, sender.frame, "DOCUMENT") &&
495 !isFrameWhitelisted(sender.tab, sender.frame, "ELEMHIDE")) 482 !isFrameWhitelisted(sender.page, sender.frame, "ELEMHIDE"))
496 { 483 {
497 var noStyleRules = false; 484 var noStyleRules = false;
498 var host = extractHostFromURL(sender.frame.url); 485 var host = extractHostFromURL(sender.frame.url);
499 for (var i = 0; i < noStyleRulesHosts.length; i++) 486 for (var i = 0; i < noStyleRulesHosts.length; i++)
500 { 487 {
501 var noStyleHost = noStyleRulesHosts[i]; 488 var noStyleHost = noStyleRulesHosts[i];
502 if (host == noStyleHost || (host.length > noStyleHost.length && 489 if (host == noStyleHost || (host.length > noStyleHost.length &&
503 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost)) 490 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost))
504 { 491 {
505 noStyleRules = true; 492 noStyleRules = true;
506 } 493 }
507 } 494 }
508 selectors = ElemHide.getSelectorsForDomain(host, false); 495 selectors = ElemHide.getSelectorsForDomain(host, false);
509 if (noStyleRules) 496 if (noStyleRules)
510 { 497 {
511 selectors = selectors.filter(function(s) 498 selectors = selectors.filter(function(s)
512 { 499 {
513 return !/\[style[\^\$]?=/.test(s); 500 return !/\[style[\^\$]?=/.test(s);
514 }); 501 });
515 } 502 }
516 } 503 }
517 504
518 sendResponse(selectors); 505 sendResponse(selectors);
519 break; 506 break;
520 case "should-collapse": 507 case "should-collapse":
521 if (isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT")) 508 if (isFrameWhitelisted(sender.page, sender.frame, "DOCUMENT"))
522 { 509 {
523 sendResponse(false); 510 sendResponse(false);
524 break; 511 break;
525 } 512 }
526 513
527 var requestHost = extractHostFromURL(msg.url); 514 var requestHost = extractHostFromURL(msg.url);
528 var documentHost = extractHostFromURL(sender.frame.url); 515 var documentHost = extractHostFromURL(sender.frame.url);
529 var thirdParty = isThirdParty(requestHost, documentHost); 516 var thirdParty = isThirdParty(requestHost, documentHost);
530 var filter = defaultMatcher.matchesAny(msg.url, msg.mediatype, documentHos t, thirdParty); 517 var filter = defaultMatcher.matchesAny(msg.url, msg.mediatype, documentHos t, thirdParty);
531 if (filter instanceof BlockingFilter) 518 if (filter instanceof BlockingFilter)
532 { 519 {
533 var collapse = filter.collapse; 520 var collapse = filter.collapse;
534 if (collapse == null) 521 if (collapse == null)
535 collapse = Prefs.hidePlaceholders; 522 collapse = Prefs.hidePlaceholders;
536 sendResponse(collapse); 523 sendResponse(collapse);
537 } 524 }
538 else 525 else
539 sendResponse(false); 526 sendResponse(false);
540 break; 527 break;
541 case "get-domain-enabled-state": 528 case "get-domain-enabled-state":
542 // Returns whether this domain is in the exclusion list. 529 // Returns whether this domain is in the exclusion list.
543 // The browser action popup asks us this. 530 // The browser action popup asks us this.
544 if(sender.tab) 531 if(sender.page)
545 { 532 {
546 sendResponse({enabled: !isWhitelisted(sender.tab.url)}); 533 sendResponse({enabled: !isWhitelisted(sender.page.url)});
547 return; 534 return;
548 } 535 }
549 break; 536 break;
550 case "add-filters": 537 case "add-filters":
551 if (msg.filters && msg.filters.length) 538 if (msg.filters && msg.filters.length)
552 { 539 {
553 for (var i = 0; i < msg.filters.length; i++) 540 for (var i = 0; i < msg.filters.length; i++)
554 FilterStorage.addFilter(Filter.fromText(msg.filters[i])); 541 FilterStorage.addFilter(Filter.fromText(msg.filters[i]));
555 } 542 }
556 break; 543 break;
557 case "add-subscription": 544 case "add-subscription":
558 openOptions(function(tab) 545 openOptions(function(page)
559 { 546 {
560 tab.sendMessage(msg); 547 page.sendMessage(msg);
561 }); 548 });
562 break; 549 break;
563 case "add-key-exception": 550 case "add-key-exception":
564 processKeyException(msg.token, sender.tab, sender.frame); 551 processKeyException(msg.token, sender.page, sender.frame);
565 break; 552 break;
566 case "forward": 553 case "forward":
567 if (sender.tab) 554 if (sender.page)
568 { 555 {
569 sender.tab.sendMessage(msg.payload, sendResponse); 556 sender.page.sendMessage(msg.payload, sendResponse);
570 // Return true to indicate that we want to call 557 // Return true to indicate that we want to call
571 // sendResponse asynchronously 558 // sendResponse asynchronously
572 return true; 559 return true;
573 } 560 }
574 break; 561 break;
575 default: 562 default:
576 sendResponse({}); 563 sendResponse({});
577 break; 564 break;
578 } 565 }
579 }); 566 });
580 567
581 // Update icon if a tab changes location 568 // update icon when page changes location
582 ext.tabs.onLoading.addListener(function(tab) 569 ext.pages.onLoading.addListener(function(page)
583 { 570 {
584 tab.sendMessage({type: "clickhide-deactivate"}); 571 page.sendMessage({type: "clickhide-deactivate"});
585 refreshIconAndContextMenu(tab); 572 refreshIconAndContextMenu(page);
586 }); 573 });
587 574
588 setTimeout(function() 575 setTimeout(function()
589 { 576 {
590 var notificationToShow = Notification.getNextToShow(); 577 var notificationToShow = Notification.getNextToShow();
591 if (notificationToShow) 578 if (notificationToShow)
592 showNotification(notificationToShow); 579 showNotification(notificationToShow);
593 }, 3 * 60 * 1000); 580 }, 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