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: Use just one click handler for all links Created July 22, 2013, 9:34 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 activeNotification.tabIcons[tab.id] = iconFilename;
Wladimir Palant 2013/07/23 07:25:04 This will leak memory if many tabs are opened and
Felix Dahlke 2013/07/23 10:38:08 Done.
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 iconAnimationRestartTimer = null;
Wladimir Palant 2013/07/23 07:25:04 A single timer should definitely be sufficient.
Felix Dahlke 2013/07/23 10:38:08 Done.
485
486 function stopIconAnimation()
487 {
488 if (iconAnimationRestartTimer)
489 {
490 clearTimeout(iconAnimationRestartTimer)
491 iconAnimationRestartTimer = null;
492 }
493
494 if (!iconAnimationTimer)
495 return;
496
497 clearTimeout(iconAnimationTimer);
498 iconAnimationTimer = null;
499 chrome.tabs.getAllInWindow(null, function(tabs)
500 {
501 tabs.forEach(refreshIconAndContextMenu);
502 });
503 }
504
505 function loadImages(imageFiles, callback)
506 {
507 var images = {};
508 var imagesLoaded = 0;
509 imageFiles.forEach(function(imageFile)
510 {
511 var image = new Image();
512 image.src = imageFile;
513 image.addEventListener("load", function()
514 {
515 images[imageFile] = image;
516 if (++imagesLoaded === imageFiles.length)
517 callback(images);
518 });
519 });
520 }
521
522 function startIconAnimation()
523 {
524 stopIconAnimation();
525
526 var normalIconFile = "icons/abp-19.png";
527 var whitelistedIconFile = "icons/abp-19-whitelisted.png";
528 var severitySuffix = activeNotification.severity === "critical"
529 ? "critical" : "information";
530 var notificationIconFile = "icons/notification-" + severitySuffix + ".png";
531 var iconFiles = [normalIconFile, whitelistedIconFile ,notificationIconFile];
532 loadImages(iconFiles, function(images)
533 {
534 var normalIcon = images[normalIconFile];
535 var notificationIcon = images[notificationIconFile];
536 var canvas = document.createElement("canvas");
537 canvas.width = normalIcon.width;
538 canvas.height = normalIcon.height;
539 var context = canvas.getContext("2d");
540
541 var animationStartTime = Date.now();
542 var animationInterval = 1000 / 5;
543 function animationStep()
544 {
545 var timeElapsed = Date.now() - animationStartTime;
546 var duration = 3000;
547 if (timeElapsed > duration)
548 {
549 stopIconAnimation();
550 iconAnimationRestartTimer = setTimeout(startIconAnimation, 5000);
551 return;
552 }
553
554 chrome.tabs.getSelected(null, function(tab)
555 {
556 var icon = images[activeNotification.tabIcons[tab.id]];
557 var animationTime = timeElapsed % duration;
558 var showDelay = 1000;
559 var fadeInEndTime = duration / 2 - showDelay / 2;
560 var fadeOutStartTime = fadeInEndTime + showDelay;
561
562 var opacity;
563 if (animationTime < fadeInEndTime)
564 opacity = animationTime / fadeInEndTime;
565 else if (animationTime > fadeOutStartTime)
566 opacity = 1 - (animationTime - fadeOutStartTime)
567 / (duration - fadeOutStartTime);
568
569 context.clearRect(0, 0, canvas.width, canvas.height);
570 context.globalAlpha = 1;
571 context.drawImage(icon, 0, 0);
572 context.globalAlpha = opacity;
573 context.drawImage(notificationIcon, 0, 0);
574 var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
575
576 chrome.pageAction.setIcon({tabId: tab.id, imageData: imageData});
577 iconAnimationTimer = setTimeout(animationStep, animationInterval);
578 });
579 }
580 animationStep();
581 });
582 }
583
584 function prepareNotificationIconAndPopup()
585 {
586 activeNotification.tabIcons = {};
587 activeNotification.onClicked = function()
588 {
589 stopIconAnimation();
590 activeNotification = null;
591 };
592 startIconAnimation();
593
594 chrome.tabs.getAllInWindow(null, function(tabs)
595 {
596 tabs.forEach(refreshIconAndContextMenu);
597 });
598 }
599
600 function showNotification(notification)
601 {
602 activeNotification = notification;
603
604 if (activeNotification.severity === "critical")
605 {
606 var notification = webkitNotifications.createHTMLNotification("notification. html");
607 notification.show();
608 notification.addEventListener("close", prepareNotificationIconAndPopup);
609 }
610 else
611 prepareNotificationIconAndPopup();
612 }
613
476 /** 614 /**
477 * This function is a hack - we only know the tabId and document URL for a 615 * 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 616 * message but we need to know the frame ID. Try to find it in webRequest's
479 * frame data. 617 * frame data.
480 */ 618 */
481 function getFrameId(tabId, url) 619 function getFrameId(tabId, url)
482 { 620 {
483 if (tabId in frames) 621 if (tabId in frames)
484 { 622 {
485 for (var f in frames[tabId]) 623 for (var f in frames[tabId])
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 refreshIconAndContextMenu(windows[i].tabs[j]); 740 refreshIconAndContextMenu(windows[i].tabs[j]);
603 }); 741 });
604 742
605 // Update icon if a tab changes location 743 // Update icon if a tab changes location
606 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 744 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
607 { 745 {
608 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"}) 746 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"})
609 if(changeInfo.status == "loading") 747 if(changeInfo.status == "loading")
610 refreshIconAndContextMenu(tab); 748 refreshIconAndContextMenu(tab);
611 }); 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