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

Side by Side Diff: lib/prefs.js

Issue 29760565: Issue 6599 - Detect data corruption of storage.local (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « dependencies ('k') | lib/subscriptionInit.js » ('j') | no next file with comments »
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-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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 * @type {number} 207 * @type {number}
208 */ 208 */
209 defaults.last_updates_page_displayed = 0; 209 defaults.last_updates_page_displayed = 0;
210 210
211 /** 211 /**
212 * @namespace 212 * @namespace
213 * @static 213 * @static
214 */ 214 */
215 let Prefs = exports.Prefs = { 215 let Prefs = exports.Prefs = {
216 /** 216 /**
217 * Sets the given preference.
218 *
219 * @param {string} preference
220 * @param {any} value
221 * @return {Promise} A promise that resolves when the underlying
222 browser.storage.local.set/remove() operation completes
223 */
224 set(preference, value)
225 {
226 let defaultValue = defaults[preference];
227
228 if (typeof value != typeof defaultValue)
229 throw new Error("Attempt to change preference type");
230
231 if (value == defaultValue)
232 {
233 delete overrides[preference];
234 return browser.storage.local.remove(prefToKey(preference));
235 }
236
237 overrides[preference] = value;
238 return (customSave.get(preference) || savePref)(preference);
239 },
240
241 /**
217 * Adds a callback that is called when the 242 * Adds a callback that is called when the
218 * value of a specified preference changed. 243 * value of a specified preference changed.
219 * 244 *
220 * @param {string} preference 245 * @param {string} preference
221 * @param {function} callback 246 * @param {function} callback
222 */ 247 */
223 on(preference, callback) 248 on(preference, callback)
224 { 249 {
225 eventEmitter.on(preference, callback); 250 eventEmitter.on(preference, callback);
226 }, 251 },
(...skipping 27 matching lines...) Expand all
254 return key.substr(keyPrefix.length); 279 return key.substr(keyPrefix.length);
255 } 280 }
256 281
257 function prefToKey(pref) 282 function prefToKey(pref)
258 { 283 {
259 return keyPrefix + pref; 284 return keyPrefix + pref;
260 } 285 }
261 286
262 function savePref(pref) 287 function savePref(pref)
263 { 288 {
264 browser.storage.local.set({[prefToKey(pref)]: overrides[pref]}); 289 return browser.storage.local.set({[prefToKey(pref)]: overrides[pref]});
265 } 290 }
266 291
267 let customSave = new Map(); 292 let customSave = new Map();
268 if (require("../buildtools/info").platform == "gecko") 293 if (require("../buildtools/info").platform == "gecko")
269 { 294 {
270 // Saving one storage value causes all others to be saved as well on Gecko. 295 // Saving one storage value causes all others to be saved as well on Gecko.
271 // Make sure that updating ad counter doesn't cause the filters data to be 296 // Make sure that updating ad counter doesn't cause the filters data to be
272 // saved frequently as a side-effect. 297 // saved frequently as a side-effect.
273 const MIN_UPDATE_INTERVAL = 60 * 1000; 298 const MIN_UPDATE_INTERVAL = 60 * 1000;
274 let lastUpdate = -MIN_UPDATE_INTERVAL; 299 let lastUpdate = -MIN_UPDATE_INTERVAL;
275 let updateScheduled = false; 300 let promise = null;
276 customSave.set("blocked_total", pref => 301 customSave.set("blocked_total", pref =>
277 { 302 {
278 if (updateScheduled) 303 if (!promise)
279 return;
280
281 let callback = () =>
282 { 304 {
283 lastUpdate = performance.now(); 305 promise = new Promise((resolve, reject) =>
284 updateScheduled = false; 306 {
285 savePref(pref); 307 setTimeout(
286 }; 308 () =>
287 309 {
288 let timeElapsed = performance.now() - lastUpdate; 310 lastUpdate = performance.now();
289 if (timeElapsed < MIN_UPDATE_INTERVAL) 311 promise = null;
290 { 312 savePref(pref).then(resolve, reject);
291 setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed); 313 },
292 updateScheduled = true; 314 lastUpdate + MIN_UPDATE_INTERVAL - performance.now()
315 );
316 });
293 } 317 }
294 else 318 return promise;
295 callback();
296 }); 319 });
297 } 320 }
298 321
299 function addPreference(pref) 322 function addPreference(pref)
300 { 323 {
301 Object.defineProperty(Prefs, pref, { 324 Object.defineProperty(Prefs, pref, {
302 get() { return (pref in overrides ? overrides : defaults)[pref]; }, 325 get() { return (pref in overrides ? overrides : defaults)[pref]; },
303 set(value) 326 set(value)
304 { 327 {
305 let defaultValue = defaults[pref]; 328 Prefs.set(pref, value);
306
307 if (typeof value != typeof defaultValue)
308 throw new Error("Attempt to change preference type");
309
310 if (value == defaultValue)
311 {
312 delete overrides[pref];
313 browser.storage.local.remove(prefToKey(pref));
314 }
315 else
316 {
317 overrides[pref] = value;
318 (customSave.get(pref) || savePref)(pref);
319 }
320 }, 329 },
321 enumerable: true 330 enumerable: true
322 }); 331 });
323 } 332 }
324 333
325 function init() 334 function init()
326 { 335 {
327 let prefs = Object.keys(defaults); 336 let prefs = Object.keys(defaults);
328 prefs.forEach(addPreference); 337 prefs.forEach(addPreference);
329 338
330 let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then( 339 let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then(
331 items => 340 items =>
332 { 341 {
333 for (let key in items) 342 for (let key in items)
334 overrides[keyToPref(key)] = items[key]; 343 overrides[keyToPref(key)] = items[key];
335 },
336 (error) =>
337 {
338 console.error(error);
339 } 344 }
340 ); 345 );
341 346
342 let managedLoaded; 347 let managedLoaded;
343 if ("managed" in browser.storage) 348 if ("managed" in browser.storage)
344 { 349 {
345 managedLoaded = browser.storage.managed.get(null).then( 350 managedLoaded = browser.storage.managed.get(null).then(
346 items => 351 items =>
347 { 352 {
348 for (let key in items) 353 for (let key in items)
(...skipping 28 matching lines...) Expand all
377 eventEmitter.emit(pref); 382 eventEmitter.emit(pref);
378 } 383 }
379 } 384 }
380 }); 385 });
381 } 386 }
382 387
383 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); 388 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded);
384 } 389 }
385 390
386 init(); 391 init();
OLDNEW
« no previous file with comments | « dependencies ('k') | lib/subscriptionInit.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld