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: Detect when writing to storage.local fails but reading succeed Created April 24, 2018, 2:31 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
243 // Show first run page or the updates page. The latter is only shown
244 // on Chromium (since the current updates page announces features that
245 // aren't new to Firefox users), and only if this version of the
246 // updates page hasn't been shown yet.
241 if (firstRun || info.platform == "chromium" && 247 if (firstRun || info.platform == "chromium" &&
242 updatesVersion > Prefs.last_updates_page_displayed) 248 updatesVersion > Prefs.last_updates_page_displayed)
243 { 249 {
244 Prefs.set("last_updates_page_displayed", updatesVersion).catch(() => 250 return Prefs.set("last_updates_page_displayed", updatesVersion).catch(() =>
245 { 251 {
246 exports.dataCorrupted = true; 252 dataCorrupted = true;
247 }).then(() => 253 }).then(() =>
248 { 254 {
249 if (!Prefs.suppress_first_run_page) 255 if (!Prefs.suppress_first_run_page)
250 { 256 {
257 // Always show the first run page if a data corruption was detected
258 // (either through failure of reading from or writing to storage.local).
259 // The first run page notifies the user about the data corruption.
251 let url; 260 let url;
252 if (firstRun || exports.dataCorrupted) 261 if (firstRun || dataCorrupted)
253 url = "firstRun.html"; 262 url = "firstRun.html";
254 else 263 else
255 url = "updates.html"; 264 url = "updates.html";
256 browser.tabs.create({url}); 265 browser.tabs.create({url});
257 } 266 }
258 }); 267 });
259 } 268 }
260
261 initNotifications();
262 } 269 }
263 270
264 Promise.all([ 271 Promise.all([
265 FilterNotifier.once("load"), 272 FilterNotifier.once("load"),
266 Prefs.untilLoaded.catch(() => { exports.dataCorrupted = true; }) 273 Prefs.untilLoaded.catch(() => { dataCorrupted = true; })
267 ]).then(detectFirstRun) 274 ]).then(detectFirstRun)
268 .then(getSubscriptions) 275 .then(getSubscriptions)
269 .then(finishInitialization); 276 .then(addSubscriptionsAndNotifyUser)
270 277 // We have to require the "uninstall" module on demand,
271 /** 278 // as the "uninstall" module in turn requires this module.
272 * Indicates whether the default filter subscriptions have been added 279 .then(() => { require("./uninstall").setUninstallURL(); })
273 * 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
274 * the first run. 285 * the first run.
275 * 286 *
276 * @type {boolean} 287 * @return {boolean}
277 */ 288 */
278 exports.reinitialized = false; 289 exports.isReinitialized = () => reinitialized;
279 290
280 /** 291 /**
281 * Indicates whether a data corruption was detcted. 292 * Gets a value indicating whether a data corruption was detected.
282 * 293 *
283 * @type {boolean} 294 * @return {boolean}
284 */ 295 */
285 exports.dataCorrupted = false; 296 exports.isDataCorrupted = () => dataCorrupted;
286 297
287 /** 298 /**
288 * 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
289 * during initialization. The callback must return an array of subscriptions 300 * during initialization. The callback must return an array of subscriptions
290 * that will effectively be added. 301 * that will effectively be added.
291 * 302 *
292 * @param {function} callback 303 * @param {function} callback
293 */ 304 */
294 exports.setSubscriptionsCallback = callback => 305 exports.setSubscriptionsCallback = callback =>
295 { 306 {
296 subscriptionsCallback = callback; 307 subscriptionsCallback = callback;
297 }; 308 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld