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

Delta Between Two Patch Sets: lib/notification.js

Issue 11175021: Add Notification module (Closed)
Left Patch Set: Created July 18, 2013, 12:09 p.m.
Right Patch Set: Download notifications (changes by Wladimir) Created July 18, 2013, 5:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/main.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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 /** 18 /**
19 * @fileOverview Handles notifications. 19 * @fileOverview Handles notifications.
20 */ 20 */
21 21
22 Cu.import("resource://gre/modules/Services.jsm"); 22 Cu.import("resource://gre/modules/Services.jsm");
23 23
24 let {TimeLine} = require("timeline");
24 let {Prefs} = require("prefs"); 25 let {Prefs} = require("prefs");
26 let {Downloader, Downloadable, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader");
25 27
26 function compareSeverity(notification1, notification2) 28 let INITIAL_DELAY = 12 * MILLIS_IN_MINUTE;
Felix Dahlke 2013/07/19 14:01:45 How about using const for these?
Wladimir Palant 2013/07/19 14:55:11 That will not have the results you expect. This co
29 let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
30 let EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY;
31
32 function getNumericalSeverity(notification)
27 { 33 {
28 let levels = {information: 0, critical: 1}; 34 let levels = {information: 0, critical: 1};
29 return levels[notification1.severity] - levels[notification2.severity]; 35 return (notification.severity in levels ? levels[notification.severity] : leve ls.information);
Felix Dahlke 2013/07/19 14:01:45 If we return -1 for unknown severities, those mess
Wladimir Palant 2013/07/19 14:55:11 I would rather have "information" be the default,
30 } 36 }
37
38 function saveNotificationData()
39 {
40 // HACK: JSON values aren't saved unless they are assigned a different object.
41 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata));
42 }
43
44 /**
45 * The object providing actual downloading functionality.
46 * @type Downloader
47 */
48 let downloader = null;
31 49
32 /** 50 /**
33 * Regularly fetches notifications and decides which to show. 51 * Regularly fetches notifications and decides which to show.
34 * @class 52 * @class
35 */ 53 */
36 let Notification = exports.Notification = 54 let Notification = exports.Notification =
37 { 55 {
38 /** 56 /**
57 * Called on module startup.
58 */
59 init: function()
60 {
61 TimeLine.enter("Entered Notification.init()");
62
63 downloader = new Downloader(this.getDownloadables.bind(this), INITIAL_DELAY, CHECK_INTERVAL);
64 onShutdown.add(function()
65 {
66 downloader.cancel();
67 });
68
69 downloader.onExpirationChange = this._onExpirationChange.bind(this);
70 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this);
71 downloader.onDownloadError = this._onDownloadError.bind(this);
72
73 TimeLine.leave("Notification.init() done");
74 },
75
76 /**
77 * Yields a Downloadable instances for the notifications download.
78 */
79 getDownloadables: function()
Felix Dahlke 2013/07/19 14:01:45 Discussed a bit on IRC before, but it seems like g
Wladimir Palant 2013/07/19 14:55:11 Pretty unlikely, I guess that _getDownloadables wo
80 {
81 let downloadable = new Downloadable(Prefs.notificationurl);
82 if (typeof Prefs.notificationdata.lastError == "number")
Felix Dahlke 2013/07/19 14:01:45 I've noticed that Prefs.notificationdata is undefi
Felix Dahlke 2013/07/19 14:38:38 Oh well, just remembered that the initial pref val
Wladimir Palant 2013/07/19 14:55:11 Right, we need to add a default for it.
83 downloadable.lastError = Prefs.notificationdata.lastError;
84 if (typeof Prefs.notificationdata.lastCheck == "number")
85 downloadable.lastCheck = Prefs.notificationdata.lastCheck;
86 if (typeof Prefs.notificationdata.data == "object" && typeof Prefs.notificat iondata.data.version == "number")
87 downloadable.lastVersion = Prefs.notificationdata.data.version;
88 if (typeof Prefs.notificationdata.softExpiration == "number")
89 downloadable.softExpiration = Prefs.notificationdata.softExpiration;
90 if (typeof Prefs.notificationdata.hardExpiration == "number")
91 downloadable.hardExpiration = Prefs.notificationdata.hardExpiration;
92 yield downloadable;
93 },
94
95 _onExpirationChange: function(downloadable)
96 {
97 Prefs.notificationdata.lastCheck = downloadable.lastCheck;
98 Prefs.notificationdata.softExpiration = downloadable.softExpiration;
99 Prefs.notificationdata.hardExpiration = downloadable.hardExpiration;
100 saveNotificationData();
101 },
102
103 _onDownloadSuccess: function(downloadable, responseText, errorCallback, redire ctCallback)
104 {
105 try
106 {
107 Prefs.notificationdata.data = JSON.parse(responseText);
108 }
109 catch (e)
110 {
111 Cu.reportError(e);
112 errorCallback("synchronize_invalid_data");
113 return;
114 }
115
116 Prefs.notificationdata.lastError = 0;
117 Prefs.notificationdata.downloadStatus = "synchronize_ok";
118 [Prefs.notificationdata.softExpiration, Prefs.notificationdata.hardExpiratio n] = downloader.processExpirationInterval(EXPIRATION_INTERVAL);
119 saveNotificationData();
120 },
121
122 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback)
123 {
124 Prefs.notificationdata.lastError = Date.now();
125 Prefs.notificationdata.downloadStatus = error;
126 saveNotificationData();
127 },
128
129 /**
39 * Determines which notification is to be shown next. 130 * Determines which notification is to be shown next.
40 * @param {Array of Object} notifications active notifications 131 * @param {Array of Object} notifications active notifications
41 * @return {Object} notification to be shown, or null if there is none 132 * @return {Object} notification to be shown, or null if there is none
42 */ 133 */
43 getNextToShow: function(notifications) 134 getNextToShow: function()
44 { 135 {
45 if (!Prefs.shownNotifications) 136 if (typeof Prefs.notificationdata.data != "object" || !(Prefs.notificationda ta.data.notifications instanceof Array))
46 Prefs.shownNotifications = []; 137 return null;
47 138
48 let notificationToShow; 139 if (!(Prefs.notificationdata.shown instanceof Array))
140 {
141 Prefs.notificationdata.shown = [];
142 saveNotificationData();
143 }
144
145 let {application, addonVersion} = require("info");
146 let notifications = Prefs.notificationdata.data.notifications;
147 let notificationToShow = null;
49 for each (let notification in notifications) 148 for each (let notification in notifications)
50 { 149 {
51 if (notification.severity === "information" 150 if ((typeof notification.severity == "undefined" || notification.severity === "information")
Felix Dahlke 2013/07/19 14:01:45 I think using both == and === in the same line is
Wladimir Palant 2013/07/19 14:55:11 True :)
52 && Prefs.shownNotifications.indexOf(notification.timestamp) !== -1) 151 && Prefs.notificationdata.shown.indexOf(notification.timestamp) !== -1 )
53 continue; 152 continue;
54 153
55 let info = require("info"); 154 if (notification.platforms instanceof Array
56 let platform = info.application; 155 && notification.platforms.indexOf(application) === -1)
57 let version = info.addonVersion;
58
59 if ("platforms" in notification
60 && notification.platforms.indexOf("chrome") === -1)
61 continue; 156 continue;
62 157
63 if ("minVersion" in notification 158 if ("minVersion" in notification
64 && Services.vc.compare(version, notification.minVersion) < 0) 159 && Services.vc.compare(addonVersion, notification.minVersion) < 0)
65 continue; 160 continue;
66 161
67 if ("maxVersion" in notification 162 if ("maxVersion" in notification
68 && Services.vc.compare(version, notification.maxVersion) > 0) 163 && Services.vc.compare(addonVersion, notification.maxVersion) > 0)
69 continue; 164 continue;
70 165
71 if (!notificationToShow 166 if (!notificationToShow
72 || compareSeverity(notification, notificationToShow) > 0) 167 || getNumericalSeverity(notification) > getNumericalSeverity(notificat ionToShow))
73 notificationToShow = notification; 168 notificationToShow = notification;
74 } 169 }
75 170
76 if (notificationToShow && "timestamp" in notificationToShow) 171 if (notificationToShow && "timestamp" in notificationToShow)
77 Prefs.shownNotifications.push(notificationToShow.timestamp); 172 {
173 Prefs.notificationdata.shown.push(notificationToShow.timestamp);
Wladimir Palant 2013/07/19 08:16:32 It's probably better to call that field "id" rathe
Felix Dahlke 2013/07/19 14:01:45 Yeah, fine by me. You already address this, so...
174 saveNotificationData();
175 }
78 176
79 return notificationToShow; 177 return notificationToShow;
80 } 178 }
81 }; 179 };
180 Notification.init();
LEFTRIGHT
« lib/main.js ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld