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, fixed issues with tab/window switching Created July 23, 2013, 10:24 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 | « .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 function refreshAll(tabs)
569 {
570 tabs.forEach(refreshIconAndContextMenu);
571 }
572
573 activeNotification.onClicked = function()
574 {
575 stopIconAnimation();
576 activeNotification = null;
577 chrome.tabs.query(null, refreshAll);
578 };
579
580 chrome.tabs.query({active: true, currentWindow: true}, refreshAll);
Felix Dahlke 2013/07/23 10:38:08 I just noticed that I still use currentWindow here
Felix Dahlke 2013/07/23 11:24:57 Done. Note that this was incorrect anyway, the cur
581 }
582
583 function showNotification(notification)
584 {
585 activeNotification = notification;
586
587 if (activeNotification.severity === "critical")
588 {
589 var notification = webkitNotifications.createHTMLNotification("notification. html");
590 notification.show();
591 notification.addEventListener("close", prepareNotificationIconAndPopup);
592 }
593 else
594 prepareNotificationIconAndPopup();
595 }
596
476 /** 597 /**
477 * This function is a hack - we only know the tabId and document URL for a 598 * 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 599 * message but we need to know the frame ID. Try to find it in webRequest's
479 * frame data. 600 * frame data.
480 */ 601 */
481 function getFrameId(tabId, url) 602 function getFrameId(tabId, url)
482 { 603 {
483 if (tabId in frames) 604 if (tabId in frames)
484 { 605 {
485 for (var f in frames[tabId]) 606 for (var f in frames[tabId])
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 refreshIconAndContextMenu(windows[i].tabs[j]); 723 refreshIconAndContextMenu(windows[i].tabs[j]);
603 }); 724 });
604 725
605 // Update icon if a tab changes location 726 // Update icon if a tab changes location
606 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 727 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
607 { 728 {
608 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"}) 729 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"})
609 if(changeInfo.status == "loading") 730 if(changeInfo.status == "loading")
610 refreshIconAndContextMenu(tab); 731 refreshIconAndContextMenu(tab);
611 }); 732 });
733
734 // Refresh icon when switching tabs or windows
735 chrome.tabs.onActivated.addListener(function(activeInfo)
736 {
737 refreshIconAndContextMenu(animatedIconTab);
738 chrome.tabs.get(activeInfo.tabId, function(tab)
739 {
740 refreshIconAndContextMenu(tab);
741 });
742 });
743 chrome.windows.onFocusChanged.addListener(function(windowId)
744 {
745 refreshIconAndContextMenu(animatedIconTab);
746 chrome.tabs.query({active: true, windowId: windowId}, function(tabs)
747 {
748 refreshIconAndContextMenu(tabs[0]);
749 });
750 });
751
752 setTimeout(function()
753 {
754 var notificationToShow = Notification.getNextToShow();
755 if (notificationToShow)
756 showNotification(notificationToShow);
757 }, 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