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

Side by Side Diff: lib/notification.js

Issue 5256408131960832: Issue 2420 - Move notification show logic to core (Closed)
Patch Set: Address comments Created June 8, 2015, 7:31 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 | « no previous file | lib/ui.js » ('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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 {Prefs} = require("prefs"); 24 let {Prefs} = require("prefs");
25 let {Downloader, Downloadable, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); 25 let {Downloader, Downloadable, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader");
26 let {Utils} = require("utils"); 26 let {Utils} = require("utils");
27 let {Matcher} = require("matcher"); 27 let {Matcher} = require("matcher");
28 let {Filter} = require("filterClasses"); 28 let {Filter} = require("filterClasses");
29 29
30 let INITIAL_DELAY = 12 * MILLIS_IN_MINUTE; 30 let INITIAL_DELAY = 12 * MILLIS_IN_MINUTE;
Felix Dahlke 2015/06/08 19:34:35 Nevermind this, just based this commit on an earli
31 let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; 31 let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
32 let EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; 32 let EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY;
33 let STARTUP_SHOW_DELAY = 3 * MILLIS_IN_MINUTE;
33 let TYPE = { 34 let TYPE = {
34 information: 0, 35 information: 0,
35 question: 1, 36 question: 1,
36 critical: 2 37 critical: 2
37 }; 38 };
38 39
39 let listeners = {}; 40 let showListeners = [];
41 let questionListeners = {};
40 42
41 function getNumericalSeverity(notification) 43 function getNumericalSeverity(notification)
42 { 44 {
43 return (notification.type in TYPE ? TYPE[notification.type] : TYPE.information ); 45 return (notification.type in TYPE ? TYPE[notification.type] : TYPE.information );
44 } 46 }
45 47
46 function saveNotificationData() 48 function saveNotificationData()
47 { 49 {
48 // HACK: JSON values aren't saved unless they are assigned a different object. 50 // HACK: JSON values aren't saved unless they are assigned a different object.
49 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); 51 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata));
(...skipping 24 matching lines...) Expand all
74 * @class 76 * @class
75 */ 77 */
76 let Notification = exports.Notification = 78 let Notification = exports.Notification =
77 { 79 {
78 /** 80 /**
79 * Called on module startup. 81 * Called on module startup.
80 */ 82 */
81 init: function() 83 init: function()
82 { 84 {
83 downloader = new Downloader(this._getDownloadables.bind(this), INITIAL_DELAY , CHECK_INTERVAL); 85 downloader = new Downloader(this._getDownloadables.bind(this), INITIAL_DELAY , CHECK_INTERVAL);
84 onShutdown.add(function()
85 {
86 downloader.cancel();
87 });
88
89 downloader.onExpirationChange = this._onExpirationChange.bind(this); 86 downloader.onExpirationChange = this._onExpirationChange.bind(this);
90 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this); 87 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this);
91 downloader.onDownloadError = this._onDownloadError.bind(this); 88 downloader.onDownloadError = this._onDownloadError.bind(this);
89 onShutdown.add(() => downloader.cancel());
90
91 notificationTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
92 notificationTimer.initWithCallback(Notification.showNext.bind(this),
93 STARTUP_SHOW_DELAY,
94 Ci.nsITimer.TYPE_ONE_SHOT);
95 onShutdown.add(() => notificationTimer.cancel());
92 }, 96 },
93 97
94 /** 98 /**
95 * Yields a Downloadable instances for the notifications download. 99 * Yields a Downloadable instances for the notifications download.
96 */ 100 */
97 _getDownloadables: function*() 101 _getDownloadables: function*()
98 { 102 {
99 let downloadable = new Downloadable(Prefs.notificationurl); 103 let downloadable = new Downloadable(Prefs.notificationurl);
100 if (typeof Prefs.notificationdata.lastError === "number") 104 if (typeof Prefs.notificationdata.lastError === "number")
101 downloadable.lastError = Prefs.notificationdata.lastError; 105 downloadable.lastError = Prefs.notificationdata.lastError;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 }, 155 },
152 156
153 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback) 157 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback)
154 { 158 {
155 Prefs.notificationdata.lastError = Date.now(); 159 Prefs.notificationdata.lastError = Date.now();
156 Prefs.notificationdata.downloadStatus = error; 160 Prefs.notificationdata.downloadStatus = error;
157 saveNotificationData(); 161 saveNotificationData();
158 }, 162 },
159 163
160 /** 164 /**
165 * Adds a listener for notifications to be shown.
166 * @param {Function} listener Listener to be invoked when a notification is
167 * to be shown
168 */
169 addShowListener: function(listener)
170 {
171 if (showListeners.indexOf(listener) == -1)
172 showListeners.push(listener);
173 },
174
175 /**
176 * Removes the supplied listener.
177 * @param {Function} listener Listener that was added via addShowListener()
178 */
179 removeShowListener: function(listener)
180 {
181 let index = showListeners.indexOf(listener);
182 if (index != -1)
183 showListeners.splice(index, 1);
184 },
185
186 /**
187 * Removes all listeners added via addShowListener().
188 */
189 removeAllShowListeners: function()
190 {
191 showListeners = [];
192 },
193
194 /**
161 * Determines which notification is to be shown next. 195 * Determines which notification is to be shown next.
162 * @param {String} url URL to match notifications to (optional) 196 * @param {String} url URL to match notifications to (optional)
163 * @return {Object} notification to be shown, or null if there is none 197 * @return {Object} notification to be shown, or null if there is none
164 */ 198 */
165 getNextToShow: function(url) 199 _getNextToShow: function(url)
166 { 200 {
167 function checkTarget(target, parameter, name, version) 201 function checkTarget(target, parameter, name, version)
168 { 202 {
169 let minVersionKey = parameter + "MinVersion"; 203 let minVersionKey = parameter + "MinVersion";
170 let maxVersionKey = parameter + "MaxVersion"; 204 let maxVersionKey = parameter + "MaxVersion";
171 return !((parameter in target && target[parameter] != name) || 205 return !((parameter in target && target[parameter] != name) ||
172 (minVersionKey in target && Services.vc.compare(version, target[m inVersionKey]) < 0) || 206 (minVersionKey in target && Services.vc.compare(version, target[m inVersionKey]) < 0) ||
173 (maxVersionKey in target && Services.vc.compare(version, target[m axVersionKey]) > 0)); 207 (maxVersionKey in target && Services.vc.compare(version, target[m axVersionKey]) > 0));
174 } 208 }
175 209
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 261 }
228 if (!match) 262 if (!match)
229 continue; 263 continue;
230 } 264 }
231 265
232 if (!notificationToShow 266 if (!notificationToShow
233 || getNumericalSeverity(notification) > getNumericalSeverity(notificat ionToShow)) 267 || getNumericalSeverity(notification) > getNumericalSeverity(notificat ionToShow))
234 notificationToShow = notification; 268 notificationToShow = notification;
235 } 269 }
236 270
237 if (notificationToShow && "id" in notificationToShow)
238 {
239 if (notificationToShow.type !== "question")
240 this.markAsShown(notificationToShow.id);
241 }
242
243 return notificationToShow; 271 return notificationToShow;
244 }, 272 },
245 273
274 /**
275 * Invokes the listeners added via addShowListener() with the next
276 * notification to be shown.
277 * @param {String} url URL to match notifications to (optional)
278 */
279 showNext: function(url)
280 {
281 let notification = Notification._getNextToShow(url);
282 if (notification)
283 for (let showListener of showListeners)
284 showListener(notification);
285 },
286
287 /**
288 * Marks a notification as shown.
289 * @param {String} id ID of the notification to be marked as shown
290 */
246 markAsShown: function(id) 291 markAsShown: function(id)
247 { 292 {
248 if (Prefs.notificationdata.shown.indexOf(id) > -1) 293 if (Prefs.notificationdata.shown.indexOf(id) > -1)
249 return; 294 return;
250 295
251 Prefs.notificationdata.shown.push(id); 296 Prefs.notificationdata.shown.push(id);
252 saveNotificationData(); 297 saveNotificationData();
253 }, 298 },
254 299
255 /** 300 /**
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 let index = localData.indexOf(notification); 341 let index = localData.indexOf(notification);
297 if (index > -1) 342 if (index > -1)
298 localData.splice(index, 1); 343 localData.splice(index, 1);
299 }, 344 },
300 345
301 /** 346 /**
302 * Adds a listener for question-type notifications 347 * Adds a listener for question-type notifications
303 */ 348 */
304 addQuestionListener: function(/**string*/ id, /**function(approved)*/ listener ) 349 addQuestionListener: function(/**string*/ id, /**function(approved)*/ listener )
305 { 350 {
306 if (!(id in listeners)) 351 if (!(id in questionListeners))
307 listeners[id] = []; 352 questionListeners[id] = [];
308 if (listeners[id].indexOf(listener) === -1) 353 if (questionListeners[id].indexOf(listener) === -1)
309 listeners[id].push(listener); 354 questionListeners[id].push(listener);
310 }, 355 },
311 356
312 /** 357 /**
313 * Removes a listener that was previously added via addQuestionListener 358 * Removes a listener that was previously added via addQuestionListener
314 */ 359 */
315 removeQuestionListener: function(/**string*/ id, /**function(approved)*/ liste ner) 360 removeQuestionListener: function(/**string*/ id, /**function(approved)*/ liste ner)
316 { 361 {
317 if (!(id in listeners)) 362 if (!(id in questionListeners))
318 return; 363 return;
319 let index = listeners[id].indexOf(listener); 364 let index = questionListeners[id].indexOf(listener);
320 if (index > -1) 365 if (index > -1)
321 listeners[id].splice(index, 1); 366 questionListeners[id].splice(index, 1);
322 if (listeners[id].length === 0) 367 if (questionListeners[id].length === 0)
323 delete listeners[id]; 368 delete questionListeners[id];
324 }, 369 },
325 370
326 /** 371 /**
327 * Notifies listeners about interactions with a notification 372 * Notifies question listeners about interactions with a notification
328 * @param {String} id notification ID 373 * @param {String} id notification ID
329 * @param {Boolean} approved indicator whether notification has been approved or not 374 * @param {Boolean} approved indicator whether notification has been approved or not
330 */ 375 */
331 triggerQuestionListeners: function(id, approved) 376 triggerQuestionListeners: function(id, approved)
332 { 377 {
333 if (!(id in listeners)) 378 if (!(id in questionListeners))
334 return; 379 return;
335 let questionListeners = listeners[id]; 380 let questionListenersForId = questionListeners[id];
336 for (let listener of questionListeners) 381 for (let listener of questionListenersForId)
337 listener(approved); 382 listener(approved);
338 }, 383 },
339 384
340 /** 385 /**
341 * Toggles whether notifications of a specific category should be ignored 386 * Toggles whether notifications of a specific category should be ignored
342 * @param {String} category notification category identifier 387 * @param {String} category notification category identifier
343 * @param {Boolean} [forceValue] force specified value 388 * @param {Boolean} [forceValue] force specified value
344 */ 389 */
345 toggleIgnoreCategory: function(category, forceValue) 390 toggleIgnoreCategory: function(category, forceValue)
346 { 391 {
347 let categories = Prefs.notifications_ignoredcategories; 392 let categories = Prefs.notifications_ignoredcategories;
348 let index = categories.indexOf(category); 393 let index = categories.indexOf(category);
349 if (index == -1 && forceValue !== false) 394 if (index == -1 && forceValue !== false)
350 { 395 {
351 categories.push(category); 396 categories.push(category);
352 Prefs.notifications_showui = true; 397 Prefs.notifications_showui = true;
353 } 398 }
354 else if (index != -1 && forceValue !== true) 399 else if (index != -1 && forceValue !== true)
355 categories.splice(index, 1); 400 categories.splice(index, 1);
356 401
357 // HACK: JSON values aren't saved unless they are assigned a different objec t. 402 // HACK: JSON values aren't saved unless they are assigned a different objec t.
358 Prefs.notifications_ignoredcategories = JSON.parse(JSON.stringify(categories )); 403 Prefs.notifications_ignoredcategories = JSON.parse(JSON.stringify(categories ));
359 } 404 }
360 }; 405 };
361 Notification.init(); 406 Notification.init();
OLDNEW
« no previous file with comments | « no previous file | lib/ui.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld