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: Created June 8, 2015, 11:10 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 | « no previous file | lib/ui.js » ('j') | lib/ui.js » ('J')
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
(...skipping 12 matching lines...) Expand all
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 = 1 * MILLIS_IN_MINUTE; 30 let INITIAL_DELAY = 1 * MILLIS_IN_MINUTE;
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 27 matching lines...) Expand all
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() 86 onShutdown.add(function()
85 { 87 {
86 downloader.cancel(); 88 downloader.cancel();
87 }); 89 });
Wladimir Palant 2015/06/08 11:33:15 Mind changing this into arrow function as well for
Felix Dahlke 2015/06/08 19:34:34 Done.
88 90
89 downloader.onExpirationChange = this._onExpirationChange.bind(this); 91 downloader.onExpirationChange = this._onExpirationChange.bind(this);
90 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this); 92 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this);
91 downloader.onDownloadError = this._onDownloadError.bind(this); 93 downloader.onDownloadError = this._onDownloadError.bind(this);
94
95 notificationTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
96 notificationTimer.initWithCallback(Notification.showNext,
Wladimir Palant 2015/06/08 11:33:15 Notification.showNext.bind(this)? The next person
Felix Dahlke 2015/06/08 19:34:34 Done.
97 STARTUP_SHOW_DELAY,
98 Ci.nsITimer.TYPE_ONE_SHOT);
99 onShutdown.add(() => notificationTimer.cancel());
92 }, 100 },
93 101
94 /** 102 /**
95 * Yields a Downloadable instances for the notifications download. 103 * Yields a Downloadable instances for the notifications download.
96 */ 104 */
97 _getDownloadables: function*() 105 _getDownloadables: function*()
98 { 106 {
99 let downloadable = new Downloadable(Prefs.notificationurl); 107 let downloadable = new Downloadable(Prefs.notificationurl);
100 if (typeof Prefs.notificationdata.lastError === "number") 108 if (typeof Prefs.notificationdata.lastError === "number")
101 downloadable.lastError = Prefs.notificationdata.lastError; 109 downloadable.lastError = Prefs.notificationdata.lastError;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 }, 159 },
152 160
153 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback) 161 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback)
154 { 162 {
155 Prefs.notificationdata.lastError = Date.now(); 163 Prefs.notificationdata.lastError = Date.now();
156 Prefs.notificationdata.downloadStatus = error; 164 Prefs.notificationdata.downloadStatus = error;
157 saveNotificationData(); 165 saveNotificationData();
158 }, 166 },
159 167
160 /** 168 /**
169 * Adds a listener for notifications to be shown.
170 * @param {Function} listener Listener to be invoked when a notification is
171 * to be shown
172 */
173 addShowListener: function(listener)
174 {
175 if (showListeners.indexOf(listener) == -1)
176 showListeners.push(listener);
177 },
178
179 /**
180 * Removes the supplied listener.
181 * @param {Function} listener Listener that was added via addShowListener()
182 */
183 removeShowListener: function(listener)
184 {
185 showListeners.splice(showListeners.indexOf(listener));
Wladimir Palant 2015/06/08 11:33:15 For reference: [1,2,3].splice(1) => [1] [1,2,3].s
Felix Dahlke 2015/06/08 19:34:34 Done.
186 },
187
188 /**
189 * Removes all listeners added via addShowListener().
190 */
191 removeAllShowListeners: function()
Wladimir Palant 2015/06/08 11:33:15 Why do we need that function? Seems to be a footgu
Felix Dahlke 2015/06/08 19:34:34 Done.
Wladimir Palant 2015/06/08 20:45:15 Why am I still seeing it? :)
Felix Dahlke 2015/06/08 22:01:53 Good question, done for real now.
192 {
193 showListeners = [];
194 },
195
196 /**
161 * Determines which notification is to be shown next. 197 * Determines which notification is to be shown next.
162 * @param {String} url URL to match notifications to (optional) 198 * @param {String} url URL to match notifications to (optional)
163 * @return {Object} notification to be shown, or null if there is none 199 * @return {Object} notification to be shown, or null if there is none
164 */ 200 */
165 getNextToShow: function(url) 201 _getNextToShow: function(url)
166 { 202 {
167 function checkTarget(target, parameter, name, version) 203 function checkTarget(target, parameter, name, version)
168 { 204 {
169 let minVersionKey = parameter + "MinVersion"; 205 let minVersionKey = parameter + "MinVersion";
170 let maxVersionKey = parameter + "MaxVersion"; 206 let maxVersionKey = parameter + "MaxVersion";
171 return !((parameter in target && target[parameter] != name) || 207 return !((parameter in target && target[parameter] != name) ||
172 (minVersionKey in target && Services.vc.compare(version, target[m inVersionKey]) < 0) || 208 (minVersionKey in target && Services.vc.compare(version, target[m inVersionKey]) < 0) ||
173 (maxVersionKey in target && Services.vc.compare(version, target[m axVersionKey]) > 0)); 209 (maxVersionKey in target && Services.vc.compare(version, target[m axVersionKey]) > 0));
174 } 210 }
175 211
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 263 }
228 if (!match) 264 if (!match)
229 continue; 265 continue;
230 } 266 }
231 267
232 if (!notificationToShow 268 if (!notificationToShow
233 || getNumericalSeverity(notification) > getNumericalSeverity(notificat ionToShow)) 269 || getNumericalSeverity(notification) > getNumericalSeverity(notificat ionToShow))
234 notificationToShow = notification; 270 notificationToShow = notification;
235 } 271 }
236 272
237 if (notificationToShow && "id" in notificationToShow)
238 {
239 if (notificationToShow.type !== "question")
240 this.markAsShown(notificationToShow.id);
241 }
242
243 return notificationToShow; 273 return notificationToShow;
244 }, 274 },
245 275
276 /**
277 * Invokes the listeners added via addShowListener() with the next
278 * notification to be shown.
279 * @param {String} url URL to match notifications to (optional)
280 */
281 showNext: function(url)
282 {
283 let notification = Notification._getNextToShow(url);
284 if (notification)
285 for (let showListener of showListeners)
286 showListener(notification);
287 },
288
289 /**
290 * Marks a notification as shown.
291 * @param {String} id ID of the notification to be marked as shown
292 */
246 markAsShown: function(id) 293 markAsShown: function(id)
247 { 294 {
248 if (Prefs.notificationdata.shown.indexOf(id) > -1) 295 if (Prefs.notificationdata.shown.indexOf(id) > -1)
249 return; 296 return;
250 297
251 Prefs.notificationdata.shown.push(id); 298 Prefs.notificationdata.shown.push(id);
252 saveNotificationData(); 299 saveNotificationData();
253 }, 300 },
254 301
255 /** 302 /**
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 let index = localData.indexOf(notification); 343 let index = localData.indexOf(notification);
297 if (index > -1) 344 if (index > -1)
298 localData.splice(index, 1); 345 localData.splice(index, 1);
299 }, 346 },
300 347
301 /** 348 /**
302 * Adds a listener for question-type notifications 349 * Adds a listener for question-type notifications
303 */ 350 */
304 addQuestionListener: function(/**string*/ id, /**function(approved)*/ listener ) 351 addQuestionListener: function(/**string*/ id, /**function(approved)*/ listener )
305 { 352 {
306 if (!(id in listeners)) 353 if (!(id in questionListeners))
307 listeners[id] = []; 354 questionListeners[id] = [];
308 if (listeners[id].indexOf(listener) === -1) 355 if (questionListeners[id].indexOf(listener) === -1)
309 listeners[id].push(listener); 356 questionListeners[id].push(listener);
310 }, 357 },
311 358
312 /** 359 /**
313 * Removes a listener that was previously added via addQuestionListener 360 * Removes a listener that was previously added via addQuestionListener
314 */ 361 */
315 removeQuestionListener: function(/**string*/ id, /**function(approved)*/ liste ner) 362 removeQuestionListener: function(/**string*/ id, /**function(approved)*/ liste ner)
316 { 363 {
317 if (!(id in listeners)) 364 if (!(id in questionListeners))
318 return; 365 return;
319 let index = listeners[id].indexOf(listener); 366 let index = questionListeners[id].indexOf(listener);
320 if (index > -1) 367 if (index > -1)
321 listeners[id].splice(index, 1); 368 questionListeners[id].splice(index, 1);
322 if (listeners[id].length === 0) 369 if (questionListeners[id].length === 0)
323 delete listeners[id]; 370 delete questionListeners[id];
324 }, 371 },
325 372
326 /** 373 /**
327 * Notifies listeners about interactions with a notification 374 * Notifies question listeners about interactions with a notification
328 * @param {String} id notification ID 375 * @param {String} id notification ID
329 * @param {Boolean} approved indicator whether notification has been approved or not 376 * @param {Boolean} approved indicator whether notification has been approved or not
330 */ 377 */
331 triggerQuestionListeners: function(id, approved) 378 triggerQuestionListeners: function(id, approved)
332 { 379 {
333 if (!(id in listeners)) 380 if (!(id in questionListeners))
334 return; 381 return;
335 let questionListeners = listeners[id]; 382 for (let listener of questionListeners[id])
Wladimir Palant 2015/06/08 11:33:15 Please keep using a temporary variable here - othe
Felix Dahlke 2015/06/08 19:34:34 Done.
Wladimir Palant 2015/06/08 20:45:15 I would have called that variable "listeners" or s
Felix Dahlke 2015/06/08 22:01:53 True, wasn't a brilliant name. Done, since I was u
336 for (let listener of questionListeners)
337 listener(approved); 383 listener(approved);
338 }, 384 },
339 385
340 /** 386 /**
341 * Toggles whether notifications of a specific category should be ignored 387 * Toggles whether notifications of a specific category should be ignored
342 * @param {String} category notification category identifier 388 * @param {String} category notification category identifier
343 * @param {Boolean} [forceValue] force specified value 389 * @param {Boolean} [forceValue] force specified value
344 */ 390 */
345 toggleIgnoreCategory: function(category, forceValue) 391 toggleIgnoreCategory: function(category, forceValue)
346 { 392 {
347 let categories = Prefs.notifications_ignoredcategories; 393 let categories = Prefs.notifications_ignoredcategories;
348 let index = categories.indexOf(category); 394 let index = categories.indexOf(category);
349 if (index == -1 && forceValue !== false) 395 if (index == -1 && forceValue !== false)
350 { 396 {
351 categories.push(category); 397 categories.push(category);
352 Prefs.notifications_showui = true; 398 Prefs.notifications_showui = true;
353 } 399 }
354 else if (index != -1 && forceValue !== true) 400 else if (index != -1 && forceValue !== true)
355 categories.splice(index, 1); 401 categories.splice(index, 1);
356 402
357 // HACK: JSON values aren't saved unless they are assigned a different objec t. 403 // HACK: JSON values aren't saved unless they are assigned a different objec t.
358 Prefs.notifications_ignoredcategories = JSON.parse(JSON.stringify(categories )); 404 Prefs.notifications_ignoredcategories = JSON.parse(JSON.stringify(categories ));
359 } 405 }
360 }; 406 };
361 Notification.init(); 407 Notification.init();
OLDNEW
« no previous file with comments | « no previous file | lib/ui.js » ('j') | lib/ui.js » ('J')

Powered by Google App Engine
This is Rietveld