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: Added comment, fixed typo Created April 27, 2018, 4 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 updateScheduled = false;
276 customSave.set("blocked_total", pref => 301 customSave.set("blocked_total", pref =>
277 { 302 {
278 if (updateScheduled) 303 if (!updateScheduled)
279 return; 304 {
305 let callback = () =>
306 {
307 lastUpdate = performance.now();
308 updateScheduled = false;
309 savePref(pref);
310 };
280 311
281 let callback = () => 312 let timeElapsed = performance.now() - lastUpdate;
282 { 313 if (timeElapsed < MIN_UPDATE_INTERVAL)
283 lastUpdate = performance.now(); 314 {
284 updateScheduled = false; 315 setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed);
285 savePref(pref); 316 updateScheduled = true;
286 }; 317 }
287 318 else
288 let timeElapsed = performance.now() - lastUpdate; 319 callback();
289 if (timeElapsed < MIN_UPDATE_INTERVAL)
290 {
291 setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed);
292 updateScheduled = true;
293 } 320 }
294 else 321 return Promise.resolve();
295 callback();
296 }); 322 });
297 } 323 }
298 324
299 function addPreference(pref) 325 function addPreference(pref)
300 { 326 {
301 Object.defineProperty(Prefs, pref, { 327 Object.defineProperty(Prefs, pref, {
302 get() { return (pref in overrides ? overrides : defaults)[pref]; }, 328 get() { return (pref in overrides ? overrides : defaults)[pref]; },
303 set(value) 329 set(value)
304 { 330 {
305 let defaultValue = defaults[pref]; 331 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 }, 332 },
321 enumerable: true 333 enumerable: true
322 }); 334 });
323 } 335 }
324 336
325 function init() 337 function init()
326 { 338 {
327 let prefs = Object.keys(defaults); 339 let prefs = Object.keys(defaults);
328 prefs.forEach(addPreference); 340 prefs.forEach(addPreference);
329 341
330 let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then( 342 let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then(
331 items => 343 items =>
332 { 344 {
333 for (let key in items) 345 for (let key in items)
334 overrides[keyToPref(key)] = items[key]; 346 overrides[keyToPref(key)] = items[key];
335 },
336 (error) =>
337 {
338 console.error(error);
339 } 347 }
340 ); 348 );
341 349
342 let managedLoaded; 350 let managedLoaded;
343 if ("managed" in browser.storage) 351 if ("managed" in browser.storage)
344 { 352 {
345 managedLoaded = browser.storage.managed.get(null).then( 353 managedLoaded = browser.storage.managed.get(null).then(
346 items => 354 items =>
347 { 355 {
348 for (let key in items) 356 for (let key in items)
(...skipping 28 matching lines...) Expand all
377 eventEmitter.emit(pref); 385 eventEmitter.emit(pref);
378 } 386 }
379 } 387 }
380 }); 388 });
381 } 389 }
382 390
383 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); 391 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded);
384 } 392 }
385 393
386 init(); 394 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