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

Delta Between Two Patch Sets: lib/prefs.js

Issue 29760565: Issue 6599 - Detect data corruption of storage.local (Closed)
Left Patch Set: Created April 24, 2018, 11:02 a.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 | « dependencies ('k') | lib/subscriptionInit.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 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 * 206 *
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 /**
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
216 /** 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);
(...skipping 28 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 ext.storage.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; 304 {
280 305 promise = new Promise((resolve, reject) =>
281 let callback = () => 306 {
282 { 307 setTimeout(
283 lastUpdate = performance.now(); 308 () =>
284 updateScheduled = false; 309 {
285 savePref(pref); 310 lastUpdate = performance.now();
286 }; 311 promise = null;
287 312 savePref(pref).then(resolve, reject);
288 let timeElapsed = performance.now() - lastUpdate; 313 },
289 if (timeElapsed < MIN_UPDATE_INTERVAL) 314 lastUpdate + MIN_UPDATE_INTERVAL - performance.now()
290 { 315 );
291 setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed); 316 });
292 updateScheduled = true;
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 ext.storage.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 = new Promise((resolve, reject) => 339 let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then(
331 { 340 items =>
332 ext.storage.get(prefs.map(prefToKey)).then(items =>
333 { 341 {
334 for (let key in items) 342 for (let key in items)
335 overrides[keyToPref(key)] = items[key]; 343 overrides[keyToPref(key)] = items[key];
336 344 }
337 resolve(); 345 );
338 }, reject); 346
339 }); 347 let managedLoaded;
340 348 if ("managed" in browser.storage)
341 let managedLoaded = new Promise(resolve => 349 {
342 { 350 managedLoaded = browser.storage.managed.get(null).then(
343 if ("managed" in browser.storage) 351 items =>
344 {
345 browser.storage.managed.get(null, items =>
346 { 352 {
347 // Opera doesn't support browser.storage.managed, but instead simply
348 // removing the API, Opera sets browser.runtime.lastError when using it.
349 // So we have to retrieve that error, to prevent it from showing up
350 // in the console.
351 browser.runtime.lastError;
352
353 for (let key in items) 353 for (let key in items)
354 defaults[key] = items[key]; 354 defaults[key] = items[key];
355 355 },
356 resolve(); 356
357 }); 357 // Opera doesn't support browser.storage.managed, but instead of simply
358 } 358 // removing the API, it gives an asynchronous error which we ignore here.
359 else 359 () => {}
360 { 360 );
361 resolve(); 361 }
362 } 362 else
363 }); 363 {
364 managedLoaded = Promise.resolve();
365 }
364 366
365 function onLoaded() 367 function onLoaded()
366 { 368 {
367 ext.storage.onChanged.addListener(changes => 369 browser.storage.onChanged.addListener(changes =>
368 { 370 {
369 for (let key in changes) 371 for (let key in changes)
370 { 372 {
371 let pref = keyToPref(key); 373 let pref = keyToPref(key);
372 if (pref && pref in defaults) 374 if (pref && pref in defaults)
373 { 375 {
374 let change = changes[key]; 376 let change = changes[key];
375 if ("newValue" in change && change.newValue != defaults[pref]) 377 if ("newValue" in change && change.newValue != defaults[pref])
376 overrides[pref] = change.newValue; 378 overrides[pref] = change.newValue;
377 else 379 else
378 delete overrides[pref]; 380 delete overrides[pref];
379 381
380 eventEmitter.emit(pref); 382 eventEmitter.emit(pref);
381 } 383 }
382 } 384 }
383 }); 385 });
384 } 386 }
385 387
386 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); 388 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded);
387 } 389 }
388 390
389 init(); 391 init();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld