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

Powered by Google App Engine
This is Rietveld