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

Side by Side Diff: background.js

Issue 11161031: Show notifications on startup (Chrome) (Closed)
Patch Set: Addressed issues Created July 23, 2013, 12:51 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 | « .hgsubstate ('k') | icons/notification-critical.png » ('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
(...skipping 15 matching lines...) Expand all
26 { 26 {
27 this.Subscription = Subscription; 27 this.Subscription = Subscription;
28 this.DownloadableSubscription = DownloadableSubscription; 28 this.DownloadableSubscription = DownloadableSubscription;
29 } 29 }
30 var FilterStorage = require("filterStorage").FilterStorage; 30 var FilterStorage = require("filterStorage").FilterStorage;
31 var ElemHide = require("elemHide").ElemHide; 31 var ElemHide = require("elemHide").ElemHide;
32 var defaultMatcher = require("matcher").defaultMatcher; 32 var defaultMatcher = require("matcher").defaultMatcher;
33 var Prefs = require("prefs").Prefs; 33 var Prefs = require("prefs").Prefs;
34 var Synchronizer = require("synchronizer").Synchronizer; 34 var Synchronizer = require("synchronizer").Synchronizer;
35 var Utils = require("utils").Utils; 35 var Utils = require("utils").Utils;
36 var Notification = require("notification").Notification;
36 37
37 // Some types cannot be distinguished 38 // Some types cannot be distinguished
38 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 39 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
39 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER; 40 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER;
40 41
41 var isFirstRun = false; 42 var isFirstRun = false;
42 var seenDataCorruption = false; 43 var seenDataCorruption = false;
43 require("filterNotifier").FilterNotifier.addListener(function(action) 44 require("filterNotifier").FilterNotifier.addListener(function(action)
44 { 45 {
45 if (action == "load") 46 if (action == "load")
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 { 101 {
101 // Ignore fragment identifier 102 // Ignore fragment identifier
102 var index = url.indexOf("#"); 103 var index = url.indexOf("#");
103 if (index >= 0) 104 if (index >= 0)
104 url = url.substring(0, index); 105 url = url.substring(0, index);
105 106
106 var result = defaultMatcher.matchesAny(url, type || "DOCUMENT", extractHostFro mURL(parentUrl || url), false); 107 var result = defaultMatcher.matchesAny(url, type || "DOCUMENT", extractHostFro mURL(parentUrl || url), false);
107 return (result instanceof WhitelistFilter ? result : null); 108 return (result instanceof WhitelistFilter ? result : null);
108 } 109 }
109 110
111 var activeNotification = null;
112
110 // Adds or removes page action icon according to options. 113 // Adds or removes page action icon according to options.
111 function refreshIconAndContextMenu(tab) 114 function refreshIconAndContextMenu(tab)
112 { 115 {
113 // The tab could have been closed by the time this function is called 116 // The tab could have been closed by the time this function is called
114 if(!tab) 117 if(!tab)
115 return; 118 return;
116 119
117 var excluded = isWhitelisted(tab.url); 120 var excluded = isWhitelisted(tab.url);
118 var iconFilename = excluded ? "icons/abp-19-whitelisted.png" : "icons/abp-19.p ng"; 121 var iconFilename = excluded ? "icons/abp-19-whitelisted.png" : "icons/abp-19.p ng";
119 chrome.pageAction.setIcon({tabId: tab.id, path: iconFilename}); 122
123 if (activeNotification)
124 startIconAnimation(tab, iconFilename);
125 else
126 chrome.pageAction.setIcon({tabId: tab.id, path: iconFilename});
120 127
121 // Only show icon for pages we can influence (http: and https:) 128 // Only show icon for pages we can influence (http: and https:)
122 if(/^https?:/.test(tab.url)) 129 if(/^https?:/.test(tab.url))
123 { 130 {
124 chrome.pageAction.setTitle({tabId: tab.id, title: "Adblock Plus"}); 131 chrome.pageAction.setTitle({tabId: tab.id, title: "Adblock Plus"});
125 if ("shouldShowIcon" in localStorage && localStorage["shouldShowIcon"] == "f alse") 132 if ("shouldShowIcon" in localStorage && localStorage["shouldShowIcon"] == "f alse")
126 chrome.pageAction.hide(tab.id); 133 chrome.pageAction.hide(tab.id);
127 else 134 else
128 chrome.pageAction.show(tab.id); 135 chrome.pageAction.show(tab.id);
129 136
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 chrome.tabs.onUpdated.removeListener(listener); 473 chrome.tabs.onUpdated.removeListener(listener);
467 onLoad(); 474 onLoad();
468 } 475 }
469 }; 476 };
470 chrome.tabs.onUpdated.addListener(listener); 477 chrome.tabs.onUpdated.addListener(listener);
471 } 478 }
472 }); 479 });
473 } 480 }
474 } 481 }
475 482
483 var iconAnimationTimer = null;
484 var animatedIconTab = null;
485
486 function stopIconAnimation()
487 {
488 if (!iconAnimationTimer)
489 return;
490
491 clearTimeout(iconAnimationTimer);
492 iconAnimationTimer = null;
493 animatedIconTab = null;
494 }
495
496 function loadImages(imageFiles, callback)
497 {
498 var images = {};
499 var imagesLoaded = 0;
500 imageFiles.forEach(function(imageFile)
501 {
502 var image = new Image();
503 image.src = imageFile;
504 image.addEventListener("load", function()
505 {
506 images[imageFile] = image;
507 if (++imagesLoaded === imageFiles.length)
508 callback(images);
509 });
510 });
511 }
512
513 function startIconAnimation(tab, iconPath)
514 {
515 stopIconAnimation();
516 animatedIconTab = tab;
517
518 var severitySuffix = activeNotification.severity === "critical"
519 ? "critical" : "information";
520 var notificationIconPath = "icons/notification-" + severitySuffix + ".png";
521 var iconFiles = [iconPath, notificationIconPath];
522 loadImages(iconFiles, function(images)
523 {
524 var icon = images[iconPath];
525 var notificationIcon = images[notificationIconPath];
526
527 var canvas = document.createElement("canvas");
528 canvas.width = icon.width;
529 canvas.height = icon.height;
530 var context = canvas.getContext("2d");
531
532 var currentFrame = 0;
533 var frameOpacities = [0, 0.2, 0.4, 0.6, 0.8,
534 1, 1, 1, 1, 1,
535 0.8, 0.6, 0.4, 0.2, 0];
536
537 function animationStep()
538 {
539 var opacity = frameOpacities[currentFrame];
540 context.clearRect(0, 0, canvas.width, canvas.height);
541 context.globalAlpha = 1;
542 context.drawImage(icon, 0, 0);
543 context.globalAlpha = opacity;
544 context.drawImage(notificationIcon, 0, 0);
545 var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
546 chrome.pageAction.setIcon({tabId: tab.id, imageData: imageData});
547
548 var interval;
549 currentFrame++;
550 if (currentFrame < frameOpacities.length)
551 {
552 var duration = 3000;
553 interval = duration / frameOpacities.length;
554 }
555 else
556 {
557 currentFrame = 0;
558 interval = 10000;
559 }
560 iconAnimationTimer = setTimeout(animationStep, interval);
561 }
562 animationStep();
563 });
564 }
565
566 function prepareNotificationIconAndPopup()
567 {
568 activeNotification.onClicked = function()
569 {
570 var tab = animatedIconTab;
571 stopIconAnimation();
572 activeNotification = null;
573 refreshIconAndContextMenu(tab);
574 };
575
576 chrome.windows.getLastFocused({populate: true}, function(window)
577 {
578 chrome.tabs.query({active: true, windowId: window.id}, function(tabs)
579 {
580 tabs.forEach(refreshIconAndContextMenu);
581 });
582 });
583 }
584
585 function showNotification(notification)
586 {
587 activeNotification = notification;
588
589 if (activeNotification.severity === "critical")
590 {
591 var notification = webkitNotifications.createHTMLNotification("notification. html");
592 notification.show();
593 notification.addEventListener("close", prepareNotificationIconAndPopup);
594 }
595 else
596 prepareNotificationIconAndPopup();
597 }
598
476 /** 599 /**
477 * This function is a hack - we only know the tabId and document URL for a 600 * This function is a hack - we only know the tabId and document URL for a
478 * message but we need to know the frame ID. Try to find it in webRequest's 601 * message but we need to know the frame ID. Try to find it in webRequest's
479 * frame data. 602 * frame data.
480 */ 603 */
481 function getFrameId(tabId, url) 604 function getFrameId(tabId, url)
482 { 605 {
483 if (tabId in frames) 606 if (tabId in frames)
484 { 607 {
485 for (var f in frames[tabId]) 608 for (var f in frames[tabId])
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 refreshIconAndContextMenu(windows[i].tabs[j]); 725 refreshIconAndContextMenu(windows[i].tabs[j]);
603 }); 726 });
604 727
605 // Update icon if a tab changes location 728 // Update icon if a tab changes location
606 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 729 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
607 { 730 {
608 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"}) 731 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"})
609 if(changeInfo.status == "loading") 732 if(changeInfo.status == "loading")
610 refreshIconAndContextMenu(tab); 733 refreshIconAndContextMenu(tab);
611 }); 734 });
735
736 // Refresh icon when switching tabs or windows
737 chrome.tabs.onActivated.addListener(function(activeInfo)
738 {
739 refreshIconAndContextMenu(animatedIconTab);
740 chrome.tabs.get(activeInfo.tabId, refreshIconAndContextMenu);
741 });
742 chrome.windows.onFocusChanged.addListener(function(windowId)
743 {
744 refreshIconAndContextMenu(animatedIconTab);
745 chrome.tabs.query({active: true, windowId: windowId}, function(tabs)
746 {
747 tabs.forEach(refreshIconAndContextMenu);
748 });
749 });
750
751 setTimeout(function()
752 {
753 var notificationToShow = Notification.getNextToShow();
754 if (notificationToShow)
755 showNotification(notificationToShow);
756 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « .hgsubstate ('k') | icons/notification-critical.png » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld