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

Delta Between Two Patch Sets: lib/notification.js

Issue 5538776168267776: Implemented anti-adblock message notification (Closed)
Left Patch Set: Created Feb. 12, 2014, 11:55 a.m.
Right Patch Set: Created Feb. 13, 2014, 12:50 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 | « defaults/prefs.js ('k') | lib/ui.js » ('j') | 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
(...skipping 13 matching lines...) Expand all
24 let {TimeLine} = require("timeline"); 24 let {TimeLine} = require("timeline");
25 let {Prefs} = require("prefs"); 25 let {Prefs} = require("prefs");
26 let {Downloader, Downloadable, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); 26 let {Downloader, Downloadable, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader");
27 let {Utils} = require("utils"); 27 let {Utils} = require("utils");
28 let {Matcher} = require("matcher"); 28 let {Matcher} = require("matcher");
29 let {Filter} = require("filterClasses"); 29 let {Filter} = require("filterClasses");
30 30
31 let INITIAL_DELAY = 12 * MILLIS_IN_MINUTE; 31 let INITIAL_DELAY = 12 * MILLIS_IN_MINUTE;
32 let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; 32 let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
33 let EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; 33 let EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY;
34 let SEVERITY = { 34 let TYPE = {
35 information: 0, 35 information: 0,
36 question: 1, 36 question: 1,
37 critical: 2 37 critical: 2
38 }; 38 };
39 39
40 let listeners = []; 40 let listeners = {};
41 41
42 function getNumericalSeverity(notification) 42 function getNumericalSeverity(notification)
43 { 43 {
44 return (notification.severity in SEVERITY ? SEVERITY[notification.severity] : SEVERITY.information); 44 return (notification.type in TYPE ? TYPE[notification.type] : TYPE.information );
Felix Dahlke 2014/02/12 15:09:27 I actually thought it's fine to call this TYPE, ju
Thomas Greiner 2014/02/12 18:28:02 Done.
45 } 45 }
46 46
47 function saveNotificationData() 47 function saveNotificationData()
48 { 48 {
49 // HACK: JSON values aren't saved unless they are assigned a different object. 49 // HACK: JSON values aren't saved unless they are assigned a different object.
50 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); 50 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata));
51 } 51 }
52 52
53 function localize(translations, locale) 53 function localize(translations, locale)
54 { 54 {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 Prefs.notificationdata.lastCheck = downloadable.lastCheck; 120 Prefs.notificationdata.lastCheck = downloadable.lastCheck;
121 Prefs.notificationdata.softExpiration = downloadable.softExpiration; 121 Prefs.notificationdata.softExpiration = downloadable.softExpiration;
122 Prefs.notificationdata.hardExpiration = downloadable.hardExpiration; 122 Prefs.notificationdata.hardExpiration = downloadable.hardExpiration;
123 saveNotificationData(); 123 saveNotificationData();
124 }, 124 },
125 125
126 _onDownloadSuccess: function(downloadable, responseText, errorCallback, redire ctCallback) 126 _onDownloadSuccess: function(downloadable, responseText, errorCallback, redire ctCallback)
127 { 127 {
128 try 128 try
129 { 129 {
130 Prefs.notificationdata.data = JSON.parse(responseText); 130 let data = JSON.parse(responseText);
131 for each (let notification in data.notifications)
132 {
133 if ("severity" in notification)
134 {
135 if (!("type" in notification))
136 notification.type = notification.severity;
137 delete notification.severity;
138 }
139 }
140 Prefs.notificationdata.data = data;
131 } 141 }
132 catch (e) 142 catch (e)
133 { 143 {
134 Cu.reportError(e); 144 Cu.reportError(e);
135 errorCallback("synchronize_invalid_data"); 145 errorCallback("synchronize_invalid_data");
136 return; 146 return;
137 } 147 }
138 148
139 Prefs.notificationdata.lastError = 0; 149 Prefs.notificationdata.lastError = 0;
140 Prefs.notificationdata.downloadStatus = "synchronize_ok"; 150 Prefs.notificationdata.downloadStatus = "synchronize_ok";
141 [Prefs.notificationdata.softExpiration, Prefs.notificationdata.hardExpiratio n] = downloader.processExpirationInterval(EXPIRATION_INTERVAL); 151 [Prefs.notificationdata.softExpiration, Prefs.notificationdata.hardExpiratio n] = downloader.processExpirationInterval(EXPIRATION_INTERVAL);
142 saveNotificationData(); 152 saveNotificationData();
143 }, 153 },
144 154
145 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback) 155 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback)
146 { 156 {
147 Prefs.notificationdata.lastError = Date.now(); 157 Prefs.notificationdata.lastError = Date.now();
148 Prefs.notificationdata.downloadStatus = error; 158 Prefs.notificationdata.downloadStatus = error;
149 saveNotificationData(); 159 saveNotificationData();
150 }, 160 },
151 161
152 /** 162 /**
153 * Determines which notification is to be shown next. 163 * Determines which notification is to be shown next.
154 * @param {String} url URL to match notifications to 164 * @param {String} url URL to match notifications to (optional)
Felix Dahlke 2014/02/12 15:09:27 Would be nice to point out here that it's optional
Thomas Greiner 2014/02/12 18:28:02 Done.
155 * @return {Object} notification to be shown, or null if there is none 165 * @return {Object} notification to be shown, or null if there is none
156 */ 166 */
157 getNextToShow: function(url) 167 getNextToShow: function(url)
158 { 168 {
159 function checkTarget(target, parameter, name, version) 169 function checkTarget(target, parameter, name, version)
160 { 170 {
161 let minVersionKey = parameter + "MinVersion"; 171 let minVersionKey = parameter + "MinVersion";
162 let maxVersionKey = parameter + "MaxVersion"; 172 let maxVersionKey = parameter + "MaxVersion";
163 return !((parameter in target && target[parameter] != name) || 173 return !((parameter in target && target[parameter] != name) ||
164 (minVersionKey in target && Services.vc.compare(version, target[m inVersionKey]) < 0) || 174 (minVersionKey in target && Services.vc.compare(version, target[m inVersionKey]) < 0) ||
(...skipping 11 matching lines...) Expand all
176 } 186 }
177 187
178 let notifications = localData.concat(remoteData); 188 let notifications = localData.concat(remoteData);
179 if (notifications.length === 0) 189 if (notifications.length === 0)
180 return null; 190 return null;
181 191
182 let {addonName, addonVersion, application, applicationVersion, platform, pla tformVersion} = require("info"); 192 let {addonName, addonVersion, application, applicationVersion, platform, pla tformVersion} = require("info");
183 let notificationToShow = null; 193 let notificationToShow = null;
184 for each (let notification in notifications) 194 for each (let notification in notifications)
185 { 195 {
186 if ((typeof notification.severity === "undefined" || notification.severity !== "critical") 196 if ((typeof notification.type === "undefined" || notification.type !== "cr itical")
187 && Prefs.notificationdata.shown.indexOf(notification.id) !== -1) 197 && Prefs.notificationdata.shown.indexOf(notification.id) !== -1)
188 continue; 198 continue;
189 199
190 if (typeof url === "string" || notification.urlFilters instanceof Array) 200 if (typeof url === "string" || notification.urlFilters instanceof Array)
191 { 201 {
192 if (typeof url === "string" && notification.urlFilters instanceof Array) 202 if (typeof url === "string" && notification.urlFilters instanceof Array)
193 { 203 {
194 let matcher = new Matcher(); 204 let matcher = new Matcher();
195 for each (let urlFilter in notification.urlFilters) 205 for each (let urlFilter in notification.urlFilters)
196 matcher.add(Filter.fromText(urlFilter)); 206 matcher.add(Filter.fromText(urlFilter));
(...skipping 14 matching lines...) Expand all
211 checkTarget(target, "platform", platform, platformVersion)) 221 checkTarget(target, "platform", platform, platformVersion))
212 { 222 {
213 match = true; 223 match = true;
214 break; 224 break;
215 } 225 }
216 } 226 }
217 if (!match) 227 if (!match)
218 continue; 228 continue;
219 } 229 }
220 230
221 if (!notificationToShow || getNumericalSeverity(notification) > getNumeric alSeverity(notificationToShow)) 231 if (!notificationToShow
Felix Dahlke 2014/02/12 15:09:27 Can you restore the line break? To avoid unrelated
Thomas Greiner 2014/02/12 18:28:02 Done.
232 || getNumericalSeverity(notification) > getNumericalSeverity(notificat ionToShow))
222 notificationToShow = notification; 233 notificationToShow = notification;
223 } 234 }
224 235
225 if (notificationToShow && "id" in notificationToShow) 236 if (notificationToShow && "id" in notificationToShow)
226 { 237 {
227 if (notificationToShow.severity !== "question") 238 if (notificationToShow.type !== "question")
228 this.markAsShown(notificationToShow.id); 239 this.markAsShown(notificationToShow.id);
229 } 240 }
230 241
231 return notificationToShow; 242 return notificationToShow;
232 }, 243 },
233 244
234 markAsShown: function(id) 245 markAsShown: function(id)
235 { 246 {
236 if (Prefs.notificationdata.shown.indexOf(id) > -1) 247 if (Prefs.notificationdata.shown.indexOf(id) > -1)
237 return; 248 return;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 * @param {Object} notification notification to remove 291 * @param {Object} notification notification to remove
281 */ 292 */
282 removeNotification: function(notification) 293 removeNotification: function(notification)
283 { 294 {
284 let index = localData.indexOf(notification); 295 let index = localData.indexOf(notification);
285 if (index > -1) 296 if (index > -1)
286 localData.splice(index, 1); 297 localData.splice(index, 1);
287 }, 298 },
288 299
289 /** 300 /**
290 * Adds a listener 301 * Adds a listener for question-type notifications
291 */ 302 */
292 addListener: function(/**function(id, approved)*/ listener) 303 addQuestionListener: function(/**string*/ id, /**function(approved)*/ listener )
Felix Dahlke 2014/02/12 15:09:27 I think it'd make more sense to register listeners
Thomas Greiner 2014/02/12 18:28:02 Done. Although from a consistency standpoint I'm m
293 { 304 {
294 if (listeners.indexOf(listener) === -1) 305 if (!(id in listeners))
295 listeners.push(listener); 306 listeners[id] = [];
296 }, 307 if (listeners[id].indexOf(listener) === -1)
297 308 listeners[id].push(listener);
298 /** 309 },
299 * Removes a listener that was previously added via addListener 310
300 */ 311 /**
301 removeListener: function(/**function(id, approved)*/ listener) 312 * Removes a listener that was previously added via addQuestionListener
302 { 313 */
303 let index = listeners.indexOf(listener); 314 removeQuestionListener: function(/**string*/ id, /**function(approved)*/ liste ner)
315 {
316 if (!(id in listeners))
317 return;
318 let index = listeners[id].indexOf(listener);
304 if (index > -1) 319 if (index > -1)
305 listeners.splice(index, 1); 320 listeners[id].splice(index, 1);
321 if (listeners[id].length === 0)
322 delete listeners[id];
306 }, 323 },
307 324
308 /** 325 /**
309 * Notifies listeners about interactions with a notification 326 * Notifies listeners about interactions with a notification
310 * @param {String} id notification ID 327 * @param {String} id notification ID
311 * @param {Boolean} approved indicator whether notification has been approved or not 328 * @param {Boolean} approved indicator whether notification has been approved or not
312 */ 329 */
313 triggerListeners: function(id, approved) 330 triggerQuestionListeners: function(id, approved)
314 { 331 {
315 for each (let listener in listeners) 332 if (!(id in listeners))
316 listener(id, approved); 333 return;
334 for each (let listener in listeners[id])
335 listener(approved);
317 } 336 }
318 }; 337 };
319 Notification.init(); 338 Notification.init();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld