Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
3 * Copyright (C) 2006-2013 Eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
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/>. | |
16 */ | |
17 | |
18 /** | |
19 * @fileOverview Handles notifications. | |
20 */ | |
21 | |
22 Cu.import("resource://gre/modules/Services.jsm"); | |
23 | |
24 let {TimeLine} = require("timeline"); | |
25 let {Prefs} = require("prefs"); | |
26 let {Downloader, Downloadable, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); | |
27 | |
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) | |
33 { | |
34 let levels = {information: 0, critical: 1}; | |
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,
| |
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; | |
49 | |
50 /** | |
51 * Regularly fetches notifications and decides which to show. | |
52 * @class | |
53 */ | |
54 let Notification = exports.Notification = | |
55 { | |
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 /** | |
130 * Determines which notification is to be shown next. | |
131 * @param {Array of Object} notifications active notifications | |
132 * @return {Object} notification to be shown, or null if there is none | |
133 */ | |
134 getNextToShow: function() | |
135 { | |
136 if (typeof Prefs.notificationdata.data != "object" || !(Prefs.notificationda ta.data.notifications instanceof Array)) | |
137 return null; | |
138 | |
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; | |
148 for each (let notification in notifications) | |
149 { | |
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 :)
| |
151 && Prefs.notificationdata.shown.indexOf(notification.timestamp) !== -1 ) | |
152 continue; | |
153 | |
154 if (notification.platforms instanceof Array | |
155 && notification.platforms.indexOf(application) === -1) | |
156 continue; | |
157 | |
158 if ("minVersion" in notification | |
159 && Services.vc.compare(addonVersion, notification.minVersion) < 0) | |
160 continue; | |
161 | |
162 if ("maxVersion" in notification | |
163 && Services.vc.compare(addonVersion, notification.maxVersion) > 0) | |
164 continue; | |
165 | |
166 if (!notificationToShow | |
167 || getNumericalSeverity(notification) > getNumericalSeverity(notificat ionToShow)) | |
168 notificationToShow = notification; | |
169 } | |
170 | |
171 if (notificationToShow && "timestamp" in notificationToShow) | |
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 } | |
176 | |
177 return notificationToShow; | |
178 } | |
179 }; | |
180 Notification.init(); | |
OLD | NEW |