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

Side by Side Diff: background.js

Issue 5630329503088640: Fixed miscellaneous issues with anti-adblock notification port (Closed)
Patch Set: Created March 28, 2014, 3:29 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 | webrequest.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
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.ActiveFilter = ActiveFilter;
22 this.RegExpFilter = RegExpFilter; 21 this.RegExpFilter = RegExpFilter;
23 this.BlockingFilter = BlockingFilter; 22 this.BlockingFilter = BlockingFilter;
24 this.WhitelistFilter = WhitelistFilter; 23 this.WhitelistFilter = WhitelistFilter;
25 } 24 }
26 with(require("subscriptionClasses")) 25 with(require("subscriptionClasses"))
27 { 26 {
28 this.Subscription = Subscription; 27 this.Subscription = Subscription;
29 this.DownloadableSubscription = DownloadableSubscription; 28 this.DownloadableSubscription = DownloadableSubscription;
30 } 29 }
31 with(require("whitelisting")) 30 with(require("whitelisting"))
32 { 31 {
33 this.isWhitelisted = isWhitelisted; 32 this.isWhitelisted = isWhitelisted;
34 this.isFrameWhitelisted = isFrameWhitelisted; 33 this.isFrameWhitelisted = isFrameWhitelisted;
35 this.processKeyException = processKeyException; 34 this.processKeyException = processKeyException;
36 } 35 }
37 var FilterStorage = require("filterStorage").FilterStorage; 36 var FilterStorage = require("filterStorage").FilterStorage;
38 var ElemHide = require("elemHide").ElemHide; 37 var ElemHide = require("elemHide").ElemHide;
39 var defaultMatcher = require("matcher").defaultMatcher; 38 var defaultMatcher = require("matcher").defaultMatcher;
40 var Prefs = require("prefs").Prefs; 39 var Prefs = require("prefs").Prefs;
41 var Synchronizer = require("synchronizer").Synchronizer; 40 var Synchronizer = require("synchronizer").Synchronizer;
42 var Utils = require("utils").Utils; 41 var Utils = require("utils").Utils;
43 var Notification = require("notification").Notification; 42 var Notification = require("notification").Notification;
44 var initAntiAdblockNotification = require("antiadblockInit").initAntiAdblockNoti fication; 43 var initAntiAdblockNotification = require("antiadblockInit").initAntiAdblockNoti fication;
45 44
46 // Some types cannot be distinguished 45 // Some types cannot be distinguished
47 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 46 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
48 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER; 47 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER;
49 48
49 // Chrome on Linux does not fully support chrome.notifications until version 35
50 // https://code.google.com/p/chromium/issues/detail?id=291485
51 var canUseChromeNotifications = require("info").platform == "chromium"
52 && "notifications" in chrome
53 && (navigator.platform.indexOf("Linux") == -1 || parseInt(require("info").appl icationVersion) > 34);
54
50 var isFirstRun = false; 55 var isFirstRun = false;
51 var seenDataCorruption = false; 56 var seenDataCorruption = false;
52 require("filterNotifier").FilterNotifier.addListener(function(action) 57 require("filterNotifier").FilterNotifier.addListener(function(action)
53 { 58 {
54 if (action == "load") 59 if (action == "load")
55 { 60 {
56 var importingOldData = importOldData(); 61 var importingOldData = importOldData();
57 62
58 var addonVersion = require("info").addonVersion; 63 var addonVersion = require("info").addonVersion;
59 var prevVersion = ext.storage.currentVersion; 64 var prevVersion = ext.storage.currentVersion;
60 if (prevVersion != addonVersion) 65 if (prevVersion != addonVersion)
61 { 66 {
62 isFirstRun = !prevVersion; 67 isFirstRun = !prevVersion;
63 ext.storage.currentVersion = addonVersion; 68 ext.storage.currentVersion = addonVersion;
64 if (!importingOldData) 69 if (!importingOldData)
65 addSubscription(prevVersion); 70 addSubscription(prevVersion);
66 } 71 }
67 72
73 if (canUseChromeNotifications)
74 initChromeNotifications();
68 initAntiAdblockNotification(); 75 initAntiAdblockNotification();
69 } 76 }
70 77
71 // update browser actions when whitelisting might have changed, 78 // update browser actions when whitelisting might have changed,
72 // due to loading filters or saving filter changes 79 // due to loading filters or saving filter changes
73 if (action == "load" || action == "save") 80 if (action == "load" || action == "save")
74 { 81 {
75 ext.windows.getAll(function(windows) 82 ext.windows.getAll(function(windows)
76 { 83 {
77 for (var i = 0; i < windows.length; i++) 84 for (var i = 0; i < windows.length; i++)
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 }); 314 });
308 } 315 }
309 316
310 function prepareNotificationIconAndPopup() 317 function prepareNotificationIconAndPopup()
311 { 318 {
312 var animateIcon = (activeNotification.type !== "question"); 319 var animateIcon = (activeNotification.type !== "question");
313 activeNotification.onClicked = function() 320 activeNotification.onClicked = function()
314 { 321 {
315 if (animateIcon) 322 if (animateIcon)
316 iconAnimation.stop(); 323 iconAnimation.stop();
317 activeNotification = null; 324 notificationClosed();
318 }; 325 };
319 if (animateIcon) 326 if (animateIcon)
320 iconAnimation.update(activeNotification.type); 327 iconAnimation.update(activeNotification.type);
321 } 328 }
322 329
323 function openNotificationLinks() 330 function openNotificationLinks()
324 { 331 {
325 if (activeNotification.links) 332 if (activeNotification.links)
326 { 333 {
327 activeNotification.links.forEach(function(link) 334 activeNotification.links.forEach(function(link)
328 { 335 {
329 ext.windows.getLastFocused(function(win) 336 ext.windows.getLastFocused(function(win)
330 { 337 {
331 win.openTab(Utils.getDocLink(link)); 338 win.openTab(Utils.getDocLink(link));
332 }); 339 });
333 }); 340 });
334 } 341 }
335 } 342 }
336 343
337 function notificationButtonClick(id, index) 344 function notificationButtonClick(buttonIndex)
338 { 345 {
339 if (activeNotification.type === "question") 346 if (activeNotification.type === "question")
340 { 347 {
341 Notification.triggerQuestionListeners(activeNotification.id, index === 0); 348 Notification.triggerQuestionListeners(activeNotification.id, buttonIndex === 0);
342 Notification.markAsShown(activeNotification.id); 349 Notification.markAsShown(activeNotification.id);
343 activeNotification.onClicked(); 350 activeNotification.onClicked();
344 } 351 }
345 else if (activeNotification.links && activeNotification.links[index]) 352 else if (activeNotification.links && activeNotification.links[buttonIndex])
346 { 353 {
347 ext.windows.getLastFocused(function(win) 354 ext.windows.getLastFocused(function(win)
348 { 355 {
349 win.openTab(Utils.getDocLink(activeNotification.links[index])); 356 win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex]));
350 }); 357 });
351 } 358 }
352 } 359 }
353 360
361 function notificationClosed()
362 {
363 activeNotification = null;
364 }
365
366 function initChromeNotifications()
367 {
368 // Chrome hides notifications in notification center when clicked so we need t o close them
369 function clearActiveNotification(notificationId)
370 {
371 if (activeNotification && activeNotification.type != "question")
Felix Dahlke 2014/03/28 16:16:39 Why check for the notification type here? Seems to
Thomas Greiner 2014/03/28 17:26:51 What this function does is remove the notification
Felix Dahlke 2014/03/28 17:33:46 Ah, no I get it. No it's fine let's go with 2 if y
Thomas Greiner 2014/03/28 18:23:17 For completeness' sake, what I meant for (2) was:
372 return;
373
374 chrome.notifications.clear(notificationId, function(wasCleared)
375 {
376 if (wasCleared)
377 notificationClosed();
378 });
379 }
380
381 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt onIndex)
382 {
383 notificationButtonClick(buttonIndex);
384 clearActiveNotification(notificationId);
Thomas Greiner 2014/03/28 15:57:08 I noticed that even notifications that were dismis
385 });
386 chrome.notifications.onClicked.addListener(clearActiveNotification);
387 chrome.notifications.onClosed.addListener(notificationClosed);
388 }
389
354 function showNotification(notification) 390 function showNotification(notification)
355 { 391 {
356 if (activeNotification && activeNotification.id === notification.id) 392 if (activeNotification && activeNotification.id === notification.id)
357 return; 393 return;
358 394
359 activeNotification = notification; 395 activeNotification = notification;
360 if (activeNotification.type === "critical" || activeNotification.type === "que stion") 396 if (activeNotification.type === "critical" || activeNotification.type === "que stion")
361 { 397 {
362 var hasWebkitNotifications = typeof webkitNotifications !== "undefined"; 398 var hasWebkitNotifications = typeof webkitNotifications !== "undefined";
363 if (hasWebkitNotifications && "createHTMLNotification" in webkitNotification s) 399 if (hasWebkitNotifications && "createHTMLNotification" in webkitNotification s)
364 { 400 {
365 var notification = webkitNotifications.createHTMLNotification("notificatio n.html"); 401 var notification = webkitNotifications.createHTMLNotification("notificatio n.html");
366 notification.show(); 402 notification.show();
367 prepareNotificationIconAndPopup(); 403 prepareNotificationIconAndPopup();
368 return; 404 return;
369 } 405 }
370 406
371 var texts = Notification.getLocalizedTexts(notification); 407 var texts = Notification.getLocalizedTexts(notification);
372 var title = texts.title || ""; 408 var title = texts.title || "";
373 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 409 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
374 var iconUrl = ext.getURL("icons/abp-128.png"); 410 var iconUrl = ext.getURL("icons/abp-128.png");
375 var hasLinks = activeNotification.links && activeNotification.links.length > 0; 411 var hasLinks = activeNotification.links && activeNotification.links.length > 0;
376 412
377 // Chrome on Linux does not fully support chrome.notifications yet 413 if (canUseChromeNotifications)
378 // https://code.google.com/p/chromium/issues/detail?id=291485
379 if (require("info").platform == "chromium" && "notifications" in chrome && n avigator.platform.indexOf("Linux") == -1)
380 { 414 {
381 var opts = { 415 var opts = {
382 type: "basic", 416 type: "basic",
383 title: title, 417 title: title,
384 message: message, 418 message: message,
385 iconUrl: iconUrl, 419 iconUrl: iconUrl,
386 buttons: [], 420 buttons: [],
387 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically 421 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
388 }; 422 };
389 if (activeNotification.type === "question") 423 if (activeNotification.type === "question")
390 { 424 {
391 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")}); 425 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")});
392 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")}); 426 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")});
393 } 427 }
394 else 428 else
395 { 429 {
396 var regex = /<a>(.*?)<\/a>/g; 430 var regex = /<a>(.*?)<\/a>/g;
397 var plainMessage = texts.message || ""; 431 var plainMessage = texts.message || "";
398 var match; 432 var match;
399 while (match = regex.exec(plainMessage)) 433 while (match = regex.exec(plainMessage))
400 opts.buttons.push({title: match[1]}); 434 opts.buttons.push({title: match[1]});
401 } 435 }
402 436
403 chrome.notifications.create("", opts, function() {}); 437 chrome.notifications.create("", opts, function() {});
404 chrome.notifications.onButtonClicked.addListener(notificationButtonClick);
405 } 438 }
406 else if (hasWebkitNotifications && "createNotification" in webkitNotificatio ns && activeNotification.type !== "question") 439 else if (hasWebkitNotifications && "createNotification" in webkitNotificatio ns && activeNotification.type !== "question")
407 { 440 {
408 if (hasLinks) 441 if (hasLinks)
409 message += " " + ext.i18n.getMessage("notification_without_buttons"); 442 message += " " + ext.i18n.getMessage("notification_without_buttons");
410 443
411 var notification = webkitNotifications.createNotification(iconUrl, title, message); 444 var notification = webkitNotifications.createNotification(iconUrl, title, message);
412 notification.show(); 445 notification.show();
413 notification.addEventListener("click", openNotificationLinks, false); 446 notification.addEventListener("click", openNotificationLinks, false);
447 notification.addEventListener("close", notificationClosed, false);
414 } 448 }
415 else 449 else
416 { 450 {
417 var message = title + "\n" + message; 451 var message = title + "\n" + message;
418 if (hasLinks) 452 if (hasLinks)
419 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 453 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
420 454
421 var approved = confirm(message); 455 var approved = confirm(message);
422 if (activeNotification.type === "question") 456 if (activeNotification.type === "question")
423 notificationButtonClick(null, approved ? 0 : 1); 457 notificationButtonClick(null, approved ? 0 : 1);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 tab.sendMessage({type: "clickhide-deactivate"}); 562 tab.sendMessage({type: "clickhide-deactivate"});
529 refreshIconAndContextMenu(tab); 563 refreshIconAndContextMenu(tab);
530 }); 564 });
531 565
532 setTimeout(function() 566 setTimeout(function()
533 { 567 {
534 var notificationToShow = Notification.getNextToShow(); 568 var notificationToShow = Notification.getNextToShow();
535 if (notificationToShow) 569 if (notificationToShow)
536 showNotification(notificationToShow); 570 showNotification(notificationToShow);
537 }, 3 * 60 * 1000); 571 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « no previous file | webrequest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld