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: Created Feb. 22, 2014, 10:45 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') | 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
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 delete localStorage[option]; 78 delete localStorage[option];
79 }); 79 });
80 } 80 }
81 81
82 // Remove deprecated options before we do anything else. 82 // Remove deprecated options before we do anything else.
83 removeDeprecatedOptions(); 83 removeDeprecatedOptions();
84 84
85 var activeNotification = null; 85 var activeNotification = null;
86 86
87 // Adds or removes browser action icon according to options. 87 // Adds or removes browser action icon according to options.
88 function refreshIconAndContextMenu(tab) 88 function refreshIconAndContextMenu(page)
89 { 89 {
90 if(!/^https?:/.test(tab.url)) 90 if(!/^https?:/.test(page.url))
91 return; 91 return;
92 92
93 var iconFilename; 93 var iconFilename;
94 if (require("info").platform == "safari") 94 if (require("info").platform == "safari")
95 // There is no grayscale version of the icon for whitelisted tabs 95 // There is no grayscale version of the icon for whitelisted pages
96 // when using Safari, because icons are grayscale already and icons 96 // when using Safari, because icons are grayscale already and icons
97 // aren't per tab in Safari. 97 // aren't per page in Safari.
98 iconFilename = "icons/abp-16.png" 98 iconFilename = "icons/abp-16.png"
99 else 99 else
100 { 100 {
101 var excluded = isWhitelisted(tab.url); 101 var excluded = isWhitelisted(page.url);
102 iconFilename = excluded ? "icons/abp-19-whitelisted.png" : "icons/abp-19.png "; 102 iconFilename = excluded ? "icons/abp-19-whitelisted.png" : "icons/abp-19.png ";
103 } 103 }
104 104
105 tab.browserAction.setIcon(iconFilename); 105 page.browserAction.setIcon(iconFilename);
106 iconAnimation.registerTab(tab, iconFilename); 106 iconAnimation.registerPage(page, iconFilename);
107 107
108 // Set context menu status according to whether current tab has whitelisted do main 108 // Set context menu status according to whether the page has a whitelisted dom ain
109 if (excluded) 109 if (excluded)
110 ext.contextMenus.hideMenuItems(); 110 ext.contextMenus.hideMenuItems();
111 else 111 else
112 ext.contextMenus.showMenuItems(); 112 ext.contextMenus.showMenuItems();
113 } 113 }
114 114
115 /** 115 /**
116 * Old versions for Opera stored patterns.ini in the localStorage object, this 116 * Old versions for Opera stored patterns.ini in the localStorage object, this
117 * will import it into FilterStorage properly. 117 * will import it into FilterStorage properly.
118 * @return {Boolean} true if data import is in progress 118 * @return {Boolean} true if data import is in progress
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 } 187 }
188 else 188 else
189 addAcceptable = false; 189 addAcceptable = false;
190 } 190 }
191 191
192 if (!addSubscription && !addAcceptable) 192 if (!addSubscription && !addAcceptable)
193 return; 193 return;
194 194
195 function notifyUser() 195 function notifyUser()
196 { 196 {
197 ext.windows.getLastFocused(function(win) 197 ext.pages.open(ext.getURL("firstRun.html"));
198 {
199 win.openTab(ext.getURL("firstRun.html"));
200 });
201 } 198 }
202 199
203 if (addSubscription) 200 if (addSubscription)
204 { 201 {
205 // Load subscriptions data 202 // Load subscriptions data
206 var request = new XMLHttpRequest(); 203 var request = new XMLHttpRequest();
207 request.open("GET", "subscriptions.xml"); 204 request.open("GET", "subscriptions.xml");
208 request.addEventListener("load", function() 205 request.addEventListener("load", function()
209 { 206 {
210 var node = Utils.chooseFilterSubscription(request.responseXML.getElementsB yTagName("subscription")); 207 var node = Utils.chooseFilterSubscription(request.responseXML.getElementsB yTagName("subscription"));
(...skipping 14 matching lines...) Expand all
225 } 222 }
226 else 223 else
227 notifyUser(); 224 notifyUser();
228 } 225 }
229 226
230 function setContextMenu() 227 function setContextMenu()
231 { 228 {
232 if (Prefs.shouldShowBlockElementMenu) 229 if (Prefs.shouldShowBlockElementMenu)
233 { 230 {
234 // Register context menu item 231 // Register context menu item
235 ext.contextMenus.addMenuItem(ext.i18n.getMessage("block_element"), ["image", "video", "audio"], function(srcUrl, tab) 232 ext.contextMenus.addMenuItem(ext.i18n.getMessage("block_element"), ["image", "video", "audio"], function(srcUrl, page)
236 { 233 {
237 if (srcUrl) 234 if (srcUrl)
238 tab.sendMessage({type: "clickhide-new-filter", filter: srcUrl}); 235 page.sendMessage({type: "clickhide-new-filter", filter: srcUrl});
239 }); 236 });
240 } 237 }
241 else 238 else
242 ext.contextMenus.removeMenuItems(); 239 ext.contextMenus.removeMenuItems();
243 } 240 }
244 241
245 Prefs.addListener(function(name) 242 Prefs.addListener(function(name)
246 { 243 {
247 if (name == "shouldShowBlockElementMenu") 244 if (name == "shouldShowBlockElementMenu")
248 setContextMenu(); 245 setContextMenu();
249 }); 246 });
250 setContextMenu(); 247 setContextMenu();
251 248
252 /** 249 /**
253 * Opens options tab or focuses an existing one, within the last focused window . 250 * Opens options page or focuses an existing one, within the last focused windo w.
254 * @param {Function} callback function to be called with the 251 * @param {Function} callback function to be called with the
255 Tab object of the options tab 252 Page object of the options page
256 */ 253 */
257 function openOptions(callback) 254 function openOptions(callback)
258 { 255 {
259 ext.windows.getLastFocused(function(win) 256 ext.pages.query({lastFocusedWindow: true}, function(pages)
Wladimir Palant 2014/04/04 14:00:35 The compat info for Chrome needs to be changed - l
Sebastian Noack 2014/04/07 13:15:25 Done.
260 { 257 {
261 win.getAllTabs(function(tabs) 258 var optionsUrl = ext.getURL("options.html");
259
260 for (var i = 0; i < pages.length; i++)
262 { 261 {
263 var optionsUrl = ext.getURL("options.html"); 262 var page = pages[i];
263 if (page.url == optionsUrl)
264 {
265 page.activate();
266 if (callback)
267 callback(page);
268 return;
269 }
270 }
264 271
265 for (var i = 0; i < tabs.length; i++) 272 ext.pages.open(optionsUrl, callback);
Wladimir Palant 2014/04/04 14:00:35 Won't this break abp: links? The add-subscription
Sebastian Noack 2014/04/07 13:15:25 It was wishful thinking, that the callback passed
266 {
267 if (tabs[i].url == optionsUrl)
268 {
269 tabs[i].activate();
270 if (callback)
271 callback(tabs[i]);
272 return;
273 }
274 }
275
276 win.openTab(optionsUrl, callback && function(tab)
277 {
278 tab.onCompleted.addListener(callback);
279 });
280 });
281 }); 273 });
282 } 274 }
283 275
284 function prepareNotificationIconAndPopup() 276 function prepareNotificationIconAndPopup()
285 { 277 {
286 activeNotification.onClicked = function() 278 activeNotification.onClicked = function()
287 { 279 {
288 iconAnimation.stop(); 280 iconAnimation.stop();
289 activeNotification = null; 281 activeNotification = null;
290 }; 282 };
(...skipping 16 matching lines...) Expand all
307 prepareNotificationIconAndPopup(); 299 prepareNotificationIconAndPopup();
308 } 300 }
309 301
310 ext.onMessage.addListener(function (msg, sender, sendResponse) 302 ext.onMessage.addListener(function (msg, sender, sendResponse)
311 { 303 {
312 switch (msg.type) 304 switch (msg.type)
313 { 305 {
314 case "get-selectors": 306 case "get-selectors":
315 var selectors = null; 307 var selectors = null;
316 308
317 if (!isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT") && 309 if (!isFrameWhitelisted(sender.page, sender.frame, "DOCUMENT") &&
318 !isFrameWhitelisted(sender.tab, sender.frame, "ELEMHIDE")) 310 !isFrameWhitelisted(sender.page, sender.frame, "ELEMHIDE"))
319 { 311 {
320 var noStyleRules = false; 312 var noStyleRules = false;
321 var host = extractHostFromURL(sender.frame.url); 313 var host = extractHostFromURL(sender.frame.url);
322 for (var i = 0; i < noStyleRulesHosts.length; i++) 314 for (var i = 0; i < noStyleRulesHosts.length; i++)
323 { 315 {
324 var noStyleHost = noStyleRulesHosts[i]; 316 var noStyleHost = noStyleRulesHosts[i];
325 if (host == noStyleHost || (host.length > noStyleHost.length && 317 if (host == noStyleHost || (host.length > noStyleHost.length &&
326 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost)) 318 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost))
327 { 319 {
328 noStyleRules = true; 320 noStyleRules = true;
329 } 321 }
330 } 322 }
331 selectors = ElemHide.getSelectorsForDomain(host, false); 323 selectors = ElemHide.getSelectorsForDomain(host, false);
332 if (noStyleRules) 324 if (noStyleRules)
333 { 325 {
334 selectors = selectors.filter(function(s) 326 selectors = selectors.filter(function(s)
335 { 327 {
336 return !/\[style[\^\$]?=/.test(s); 328 return !/\[style[\^\$]?=/.test(s);
337 }); 329 });
338 } 330 }
339 } 331 }
340 332
341 sendResponse(selectors); 333 sendResponse(selectors);
342 break; 334 break;
343 case "should-collapse": 335 case "should-collapse":
344 if (isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT")) 336 if (isFrameWhitelisted(sender.page, sender.frame, "DOCUMENT"))
345 { 337 {
346 sendResponse(false); 338 sendResponse(false);
347 break; 339 break;
348 } 340 }
349 341
350 var requestHost = extractHostFromURL(msg.url); 342 var requestHost = extractHostFromURL(msg.url);
351 var documentHost = extractHostFromURL(sender.frame.url); 343 var documentHost = extractHostFromURL(sender.frame.url);
352 var thirdParty = isThirdParty(requestHost, documentHost); 344 var thirdParty = isThirdParty(requestHost, documentHost);
353 var filter = defaultMatcher.matchesAny(msg.url, msg.mediatype, documentHos t, thirdParty); 345 var filter = defaultMatcher.matchesAny(msg.url, msg.mediatype, documentHos t, thirdParty);
354 if (filter instanceof BlockingFilter) 346 if (filter instanceof BlockingFilter)
355 { 347 {
356 var collapse = filter.collapse; 348 var collapse = filter.collapse;
357 if (collapse == null) 349 if (collapse == null)
358 collapse = Prefs.hidePlaceholders; 350 collapse = Prefs.hidePlaceholders;
359 sendResponse(collapse); 351 sendResponse(collapse);
360 } 352 }
361 else 353 else
362 sendResponse(false); 354 sendResponse(false);
363 break; 355 break;
364 case "get-domain-enabled-state": 356 case "get-domain-enabled-state":
365 // Returns whether this domain is in the exclusion list. 357 // Returns whether this domain is in the exclusion list.
366 // The browser action popup asks us this. 358 // The browser action popup asks us this.
367 if(sender.tab) 359 if(sender.page)
368 { 360 {
369 sendResponse({enabled: !isWhitelisted(sender.tab.url)}); 361 sendResponse({enabled: !isWhitelisted(sender.page.url)});
370 return; 362 return;
371 } 363 }
372 break; 364 break;
373 case "add-filters": 365 case "add-filters":
374 if (msg.filters && msg.filters.length) 366 if (msg.filters && msg.filters.length)
375 { 367 {
376 for (var i = 0; i < msg.filters.length; i++) 368 for (var i = 0; i < msg.filters.length; i++)
377 FilterStorage.addFilter(Filter.fromText(msg.filters[i])); 369 FilterStorage.addFilter(Filter.fromText(msg.filters[i]));
378 } 370 }
379 break; 371 break;
380 case "add-subscription": 372 case "add-subscription":
381 openOptions(function(tab) 373 openOptions(function(page)
382 { 374 {
383 tab.sendMessage(msg); 375 page.sendMessage(msg);
384 }); 376 });
385 break; 377 break;
386 case "add-key-exception": 378 case "add-key-exception":
387 processKeyException(msg.token, sender.tab, sender.frame); 379 processKeyException(msg.token, sender.page, sender.frame);
388 break; 380 break;
389 case "forward": 381 case "forward":
390 if (sender.tab) 382 if (sender.page)
391 { 383 {
392 sender.tab.sendMessage(msg.payload, sendResponse); 384 sender.page.sendMessage(msg.payload, sendResponse);
393 // Return true to indicate that we want to call 385 // Return true to indicate that we want to call
394 // sendResponse asynchronously 386 // sendResponse asynchronously
395 return true; 387 return true;
396 } 388 }
397 break; 389 break;
398 default: 390 default:
399 sendResponse({}); 391 sendResponse({});
400 break; 392 break;
401 } 393 }
402 }); 394 });
403 395
404 // Show icon as browser action for all tabs that already exist 396 // Show icon as browser action for all pages that already exist
405 ext.windows.getAll(function(windows) 397 ext.pages.query({}, function(pages)
406 { 398 {
407 for (var i = 0; i < windows.length; i++) 399 pages.forEach(refreshIconAndContextMenu);
408 {
409 windows[i].getAllTabs(function(tabs)
410 {
411 tabs.forEach(refreshIconAndContextMenu);
412 });
413 }
414 }); 400 });
415 401
416 // Update icon if a tab changes location 402 // Update icon for new loaded pages
417 ext.tabs.onLoading.addListener(function(tab) 403 ext.pages.onLoading.addListener(function(page)
418 { 404 {
419 tab.sendMessage({type: "clickhide-deactivate"}); 405 page.sendMessage({type: "clickhide-deactivate"});
420 refreshIconAndContextMenu(tab); 406 refreshIconAndContextMenu(page);
421 }); 407 });
422 408
423 setTimeout(function() 409 setTimeout(function()
424 { 410 {
425 var notificationToShow = Notification.getNextToShow(); 411 var notificationToShow = Notification.getNextToShow();
426 if (notificationToShow) 412 if (notificationToShow)
427 showNotification(notificationToShow); 413 showNotification(notificationToShow);
428 }, 3 * 60 * 1000); 414 }, 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