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. 11, 2014, 4:51 p.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 = {};
41
40 function getNumericalSeverity(notification) 42 function getNumericalSeverity(notification)
41 { 43 {
42 return (notification.severity in SEVERITY ? SEVERITY[notification.severity] : SEVERITY.information); 44 return (notification.type in TYPE ? TYPE[notification.type] : TYPE.information );
43 } 45 }
44 46
45 function saveNotificationData() 47 function saveNotificationData()
46 { 48 {
47 // 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.
48 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); 50 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata));
49 } 51 }
50 52
51 function localize(translations, locale) 53 function localize(translations, locale)
52 { 54 {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 Prefs.notificationdata.lastCheck = downloadable.lastCheck; 120 Prefs.notificationdata.lastCheck = downloadable.lastCheck;
119 Prefs.notificationdata.softExpiration = downloadable.softExpiration; 121 Prefs.notificationdata.softExpiration = downloadable.softExpiration;
120 Prefs.notificationdata.hardExpiration = downloadable.hardExpiration; 122 Prefs.notificationdata.hardExpiration = downloadable.hardExpiration;
121 saveNotificationData(); 123 saveNotificationData();
122 }, 124 },
123 125
124 _onDownloadSuccess: function(downloadable, responseText, errorCallback, redire ctCallback) 126 _onDownloadSuccess: function(downloadable, responseText, errorCallback, redire ctCallback)
125 { 127 {
126 try 128 try
127 { 129 {
128 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;
129 } 141 }
130 catch (e) 142 catch (e)
131 { 143 {
132 Cu.reportError(e); 144 Cu.reportError(e);
133 errorCallback("synchronize_invalid_data"); 145 errorCallback("synchronize_invalid_data");
134 return; 146 return;
135 } 147 }
136 148
137 Prefs.notificationdata.lastError = 0; 149 Prefs.notificationdata.lastError = 0;
138 Prefs.notificationdata.downloadStatus = "synchronize_ok"; 150 Prefs.notificationdata.downloadStatus = "synchronize_ok";
139 [Prefs.notificationdata.softExpiration, Prefs.notificationdata.hardExpiratio n] = downloader.processExpirationInterval(EXPIRATION_INTERVAL); 151 [Prefs.notificationdata.softExpiration, Prefs.notificationdata.hardExpiratio n] = downloader.processExpirationInterval(EXPIRATION_INTERVAL);
140 saveNotificationData(); 152 saveNotificationData();
141 }, 153 },
142 154
143 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback) 155 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback)
144 { 156 {
145 Prefs.notificationdata.lastError = Date.now(); 157 Prefs.notificationdata.lastError = Date.now();
146 Prefs.notificationdata.downloadStatus = error; 158 Prefs.notificationdata.downloadStatus = error;
147 saveNotificationData(); 159 saveNotificationData();
148 }, 160 },
149 161
150 /** 162 /**
151 * Determines which notification is to be shown next. 163 * Determines which notification is to be shown next.
152 * @param {String} url URL to match notifications to 164 * @param {String} url URL to match notifications to (optional)
153 * @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
154 */ 166 */
155 getNextToShow: function(url) 167 getNextToShow: function(url)
156 { 168 {
157 function checkTarget(target, parameter, name, version) 169 function checkTarget(target, parameter, name, version)
158 { 170 {
159 let minVersionKey = parameter + "MinVersion"; 171 let minVersionKey = parameter + "MinVersion";
160 let maxVersionKey = parameter + "MaxVersion"; 172 let maxVersionKey = parameter + "MaxVersion";
161 return !((parameter in target && target[parameter] != name) || 173 return !((parameter in target && target[parameter] != name) ||
162 (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
174 } 186 }
175 187
176 let notifications = localData.concat(remoteData); 188 let notifications = localData.concat(remoteData);
177 if (notifications.length === 0) 189 if (notifications.length === 0)
178 return null; 190 return null;
179 191
180 let {addonName, addonVersion, application, applicationVersion, platform, pla tformVersion} = require("info"); 192 let {addonName, addonVersion, application, applicationVersion, platform, pla tformVersion} = require("info");
181 let notificationToShow = null; 193 let notificationToShow = null;
182 for each (let notification in notifications) 194 for each (let notification in notifications)
183 { 195 {
184 if ((typeof notification.severity === "undefined" || notification.severity !== "critical") 196 if ((typeof notification.type === "undefined" || notification.type !== "cr itical")
185 && Prefs.notificationdata.shown.indexOf(notification.id) !== -1) 197 && Prefs.notificationdata.shown.indexOf(notification.id) !== -1)
186 continue; 198 continue;
187 199
188 if (typeof url === "string" || notification.urlFilters instanceof Array) 200 if (typeof url === "string" || notification.urlFilters instanceof Array)
189 { 201 {
190 if (typeof url === "string" && notification.urlFilters instanceof Array) 202 if (typeof url === "string" && notification.urlFilters instanceof Array)
191 { 203 {
192 let matcher = new Matcher(); 204 let matcher = new Matcher();
193 for each (let urlFilter in notification.urlFilters) 205 for each (let urlFilter in notification.urlFilters)
194 matcher.add(Filter.fromText(urlFilter)); 206 matcher.add(Filter.fromText(urlFilter));
(...skipping 14 matching lines...) Expand all
209 checkTarget(target, "platform", platform, platformVersion)) 221 checkTarget(target, "platform", platform, platformVersion))
210 { 222 {
211 match = true; 223 match = true;
212 break; 224 break;
213 } 225 }
214 } 226 }
215 if (!match) 227 if (!match)
216 continue; 228 continue;
217 } 229 }
218 230
219 if (!notificationToShow || getNumericalSeverity(notification) > getNumeric alSeverity(notificationToShow)) 231 if (!notificationToShow
232 || getNumericalSeverity(notification) > getNumericalSeverity(notificat ionToShow))
220 notificationToShow = notification; 233 notificationToShow = notification;
221 } 234 }
222 235
223 if (notificationToShow && "id" in notificationToShow) 236 if (notificationToShow && "id" in notificationToShow)
224 { 237 {
225 if (notificationToShow.severity !== "question") 238 if (notificationToShow.type !== "question")
226 this.markAsShown(notificationToShow.id); 239 this.markAsShown(notificationToShow.id);
227 } 240 }
228 241
229 return notificationToShow; 242 return notificationToShow;
230 }, 243 },
231 244
232 markAsShown: function(id) 245 markAsShown: function(id)
233 { 246 {
234 if (Prefs.notificationdata.shown.indexOf(id) > -1) 247 if (Prefs.notificationdata.shown.indexOf(id) > -1)
235 return; 248 return;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 288
276 /** 289 /**
277 * Removes an existing local notification. 290 * Removes an existing local notification.
278 * @param {Object} notification notification to remove 291 * @param {Object} notification notification to remove
279 */ 292 */
280 removeNotification: function(notification) 293 removeNotification: function(notification)
281 { 294 {
282 let index = localData.indexOf(notification); 295 let index = localData.indexOf(notification);
283 if (index > -1) 296 if (index > -1)
284 localData.splice(index, 1); 297 localData.splice(index, 1);
298 },
299
300 /**
301 * Adds a listener for question-type notifications
302 */
303 addQuestionListener: function(/**string*/ id, /**function(approved)*/ listener )
304 {
305 if (!(id in listeners))
306 listeners[id] = [];
307 if (listeners[id].indexOf(listener) === -1)
308 listeners[id].push(listener);
309 },
310
311 /**
312 * Removes a listener that was previously added via addQuestionListener
313 */
314 removeQuestionListener: function(/**string*/ id, /**function(approved)*/ liste ner)
315 {
316 if (!(id in listeners))
317 return;
318 let index = listeners[id].indexOf(listener);
319 if (index > -1)
320 listeners[id].splice(index, 1);
321 if (listeners[id].length === 0)
322 delete listeners[id];
323 },
324
325 /**
326 * Notifies listeners about interactions with a notification
327 * @param {String} id notification ID
328 * @param {Boolean} approved indicator whether notification has been approved or not
329 */
330 triggerQuestionListeners: function(id, approved)
331 {
332 if (!(id in listeners))
333 return;
334 for each (let listener in listeners[id])
335 listener(approved);
285 } 336 }
286 }; 337 };
287 Notification.init(); 338 Notification.init();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld