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

Delta Between Two Patch Sets: lib/subscriptionInit.js

Issue 29760565: Issue 6599 - Detect data corruption of storage.local (Closed)
Left Patch Set: Added comments, show uninstall page also when data corruption occurred Created April 24, 2018, 5:51 p.m.
Right Patch Set: Fixed blocked_total optimization logic Created April 27, 2018, 4:55 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 | « lib/prefs.js ('k') | lib/uninstall.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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present 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 16 matching lines...) Expand all
27 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier"); 27 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier");
28 const info = require("../buildtools/info"); 28 const info = require("../buildtools/info");
29 const {Prefs} = require("./prefs"); 29 const {Prefs} = require("./prefs");
30 const {Synchronizer} = require("../adblockpluscore/lib/synchronizer"); 30 const {Synchronizer} = require("../adblockpluscore/lib/synchronizer");
31 const {Utils} = require("./utils"); 31 const {Utils} = require("./utils");
32 const {initNotifications} = require("./notificationHelper"); 32 const {initNotifications} = require("./notificationHelper");
33 const {updatesVersion} = require("../adblockplusui/lib/prefs"); 33 const {updatesVersion} = require("../adblockplusui/lib/prefs");
34 34
35 let firstRun; 35 let firstRun;
36 let subscriptionsCallback = null; 36 let subscriptionsCallback = null;
37 let reinitialized = false;
38 let dataCorrupted = false;
37 39
38 /** 40 /**
39 * If there aren't any filters, the default subscriptions are added. 41 * If there aren't any filters, the default subscriptions are added.
40 * However, if patterns.ini already did exist and/or any preference 42 * However, if patterns.ini already did exist and/or any preference
41 * is set to a non-default value, this indicates that this isn't the 43 * is set to a non-default value, this indicates that this isn't the
42 * first run, but something went wrong. 44 * first run, but something went wrong.
43 * 45 *
44 * This function detects the first run, and makes sure that the user 46 * This function detects the first run, and makes sure that the user
45 * gets notified (on the first run page) if the data appears incomplete 47 * gets notified (on the first run page) if the data appears incomplete
46 * and therefore will be reinitialized. 48 * and therefore will be reinitialized.
47 */ 49 */
48 function detectFirstRun() 50 function detectFirstRun()
49 { 51 {
50 firstRun = FilterStorage.subscriptions.length == 0; 52 firstRun = FilterStorage.subscriptions.length == 0;
51 53
52 if (firstRun && (!FilterStorage.firstRun || Prefs.currentVersion)) 54 if (firstRun && (!FilterStorage.firstRun || Prefs.currentVersion))
53 exports.reinitialized = true; 55 reinitialized = true;
54 56
55 Prefs.currentVersion = info.addonVersion; 57 Prefs.currentVersion = info.addonVersion;
56 } 58 }
57 59
58 /** 60 /**
59 * Determines whether to add the default ad blocking subscription. 61 * Determines whether to add the default ad blocking subscription.
60 * Returns true, if there are no filter subscriptions besides those 62 * Returns true, if there are no filter subscriptions besides those
61 * other subscriptions added automatically, and no custom filters. 63 * other subscriptions added automatically, and no custom filters.
62 * 64 *
63 * On first run, this logic should always result in true since there 65 * On first run, this logic should always result in true since there
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 function supportsNotificationsWithButtons() 140 function supportsNotificationsWithButtons()
139 { 141 {
140 // Microsoft Edge (as of EdgeHTML 16) doesn't have the notifications API. 142 // Microsoft Edge (as of EdgeHTML 16) doesn't have the notifications API.
141 // Opera gives an asynchronous error when buttons are provided (we cannot 143 // Opera gives an asynchronous error when buttons are provided (we cannot
142 // detect that behavior without attempting to show a notification). 144 // detect that behavior without attempting to show a notification).
143 if (!("notifications" in browser) || info.application == "opera") 145 if (!("notifications" in browser) || info.application == "opera")
144 return false; 146 return false;
145 147
146 // Firefox throws synchronously if the "buttons" option is provided. 148 // Firefox throws synchronously if the "buttons" option is provided.
147 // If buttons are supported (i.e. on Chrome), this fails with 149 // If buttons are supported (i.e. on Chrome), this fails with
148 // a different error message due to missing required options. 150 // an asynchronous error due to missing required options.
149 // https://bugzilla.mozilla.org/show_bug.cgi?id=1190681 151 // https://bugzilla.mozilla.org/show_bug.cgi?id=1190681
150 try 152 try
151 { 153 {
152 browser.notifications.create({buttons: []}); 154 browser.notifications.create({buttons: []}).catch(() => {});
153 } 155 }
154 catch (e) 156 catch (e)
155 { 157 {
156 if (e.toString().includes('"buttons" is unsupported')) 158 if (e.toString().includes('"buttons" is unsupported'))
157 return false; 159 return false;
158 } 160 }
159 161
160 return true; 162 return true;
161 } 163 }
162 164
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 } 220 }
219 } 221 }
220 222
221 return subscriptions; 223 return subscriptions;
222 }); 224 });
223 } 225 }
224 226
225 return subscriptions; 227 return subscriptions;
226 } 228 }
227 229
228 function finishInitialization(subscriptions) 230 function addSubscriptionsAndNotifyUser(subscriptions)
229 { 231 {
230 if (subscriptionsCallback) 232 if (subscriptionsCallback)
231 subscriptions = subscriptionsCallback(subscriptions); 233 subscriptions = subscriptionsCallback(subscriptions);
232 234
233 for (let subscription of subscriptions) 235 for (let subscription of subscriptions)
234 { 236 {
235 FilterStorage.addSubscription(subscription); 237 FilterStorage.addSubscription(subscription);
236 if (subscription instanceof DownloadableSubscription && 238 if (subscription instanceof DownloadableSubscription &&
237 !subscription.lastDownload) 239 !subscription.lastDownload)
238 Synchronizer.execute(subscription); 240 Synchronizer.execute(subscription);
239 } 241 }
240 242
241 // Show first run page or the updates page. The latter is only shown 243 // Show first run page or the updates page. The latter is only shown
242 // on Chromium (since the current updates page announces features that 244 // on Chromium (since the current updates page announces features that
243 // aren't new to Firefox users), and only if this version of the 245 // aren't new to Firefox users), and only if this version of the
244 // updates page hasn't been shown yet. 246 // updates page hasn't been shown yet.
245 if (firstRun || info.platform == "chromium" && 247 if (firstRun || info.platform == "chromium" &&
246 updatesVersion > Prefs.last_updates_page_displayed) 248 updatesVersion > Prefs.last_updates_page_displayed)
247 { 249 {
248 Prefs.set("last_updates_page_displayed", updatesVersion).catch(() => 250 return Prefs.set("last_updates_page_displayed", updatesVersion).catch(() =>
249 { 251 {
250 exports.dataCorrupted = true; 252 dataCorrupted = true;
251 }).then(() => 253 }).then(() =>
252 { 254 {
253 if (!Prefs.suppress_first_run_page) 255 if (!Prefs.suppress_first_run_page)
254 { 256 {
255 // Always show the first run page if a data corruption was detected 257 // Always show the first run page if a data corruption was detected
256 // (either though failure of reading from or writing to storage.local). 258 // (either through failure of reading from or writing to storage.local).
257 // The first run page notifies the user about the data corruption. 259 // The first run page notifies the user about the data corruption.
258 let url; 260 let url;
259 if (firstRun || exports.dataCorrupted) 261 if (firstRun || dataCorrupted)
260 url = "firstRun.html"; 262 url = "firstRun.html";
261 else 263 else
262 url = "updates.html"; 264 url = "updates.html";
263 browser.tabs.create({url}); 265 browser.tabs.create({url});
264 } 266 }
265 }); 267 });
266 } 268 }
267
268 initNotifications();
269 } 269 }
270 270
271 Promise.all([ 271 Promise.all([
272 FilterNotifier.once("load"), 272 FilterNotifier.once("load"),
273 Prefs.untilLoaded.catch(() => { exports.dataCorrupted = true; }) 273 Prefs.untilLoaded.catch(() => { dataCorrupted = true; })
274 ]).then(detectFirstRun) 274 ]).then(detectFirstRun)
275 .then(getSubscriptions) 275 .then(getSubscriptions)
276 .then(finishInitialization); 276 .then(addSubscriptionsAndNotifyUser)
277 277 // We have to require the "uninstall" module on demand,
278 /** 278 // as the "uninstall" module in turn requires this module.
279 * Indicates whether the default filter subscriptions have been added 279 .then(() => { require("./uninstall").setUninstallURL(); })
280 * again because there weren't any subscriptions even though this wasn't 280 .then(initNotifications);
281
282 /**
283 * Gets a value indicating whether the default filter subscriptions have been
284 * added again because there weren't any subscriptions even though this wasn't
281 * the first run. 285 * the first run.
282 * 286 *
283 * @type {boolean} 287 * @return {boolean}
284 */ 288 */
285 exports.reinitialized = false; 289 exports.isReinitialized = () => reinitialized;
286 290
287 /** 291 /**
288 * Indicates whether a data corruption was detected. 292 * Gets a value indicating whether a data corruption was detected.
289 * 293 *
290 * @type {boolean} 294 * @return {boolean}
291 */ 295 */
292 exports.dataCorrupted = false; 296 exports.isDataCorrupted = () => dataCorrupted;
293 297
294 /** 298 /**
295 * Sets a callback that is called with an array of subscriptions to be added 299 * Sets a callback that is called with an array of subscriptions to be added
296 * during initialization. The callback must return an array of subscriptions 300 * during initialization. The callback must return an array of subscriptions
297 * that will effectively be added. 301 * that will effectively be added.
298 * 302 *
299 * @param {function} callback 303 * @param {function} callback
300 */ 304 */
301 exports.setSubscriptionsCallback = callback => 305 exports.setSubscriptionsCallback = callback =>
302 { 306 {
303 subscriptionsCallback = callback; 307 subscriptionsCallback = callback;
304 }; 308 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld