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

Side by Side Diff: lib/prefs.js

Issue 29760680: Issue 4580 - Removed ext.storage (Closed)
Patch Set: Created April 24, 2018, 5:18 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
« lib/io.js ('K') | « lib/io.js ('k') | no next file » | 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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 return key.substr(keyPrefix.length); 254 return key.substr(keyPrefix.length);
255 } 255 }
256 256
257 function prefToKey(pref) 257 function prefToKey(pref)
258 { 258 {
259 return keyPrefix + pref; 259 return keyPrefix + pref;
260 } 260 }
261 261
262 function savePref(pref) 262 function savePref(pref)
263 { 263 {
264 ext.storage.set(prefToKey(pref), overrides[pref]); 264 browser.storage.local.set({[prefToKey(pref)]: overrides[pref]});
265 } 265 }
266 266
267 let customSave = new Map(); 267 let customSave = new Map();
268 if (require("../buildtools/info").platform == "gecko") 268 if (require("../buildtools/info").platform == "gecko")
269 { 269 {
270 // Saving one storage value causes all others to be saved as well on Gecko. 270 // 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 271 // Make sure that updating ad counter doesn't cause the filters data to be
272 // saved frequently as a side-effect. 272 // saved frequently as a side-effect.
273 const MIN_UPDATE_INTERVAL = 60 * 1000; 273 const MIN_UPDATE_INTERVAL = 60 * 1000;
274 let lastUpdate = -MIN_UPDATE_INTERVAL; 274 let lastUpdate = -MIN_UPDATE_INTERVAL;
(...skipping 28 matching lines...) Expand all
303 set(value) 303 set(value)
304 { 304 {
305 let defaultValue = defaults[pref]; 305 let defaultValue = defaults[pref];
306 306
307 if (typeof value != typeof defaultValue) 307 if (typeof value != typeof defaultValue)
308 throw new Error("Attempt to change preference type"); 308 throw new Error("Attempt to change preference type");
309 309
310 if (value == defaultValue) 310 if (value == defaultValue)
311 { 311 {
312 delete overrides[pref]; 312 delete overrides[pref];
313 ext.storage.remove(prefToKey(pref)); 313 browser.storage.local.remove(prefToKey(pref));
314 } 314 }
315 else 315 else
316 { 316 {
317 overrides[pref] = value; 317 overrides[pref] = value;
318 (customSave.get(pref) || savePref)(pref); 318 (customSave.get(pref) || savePref)(pref);
319 } 319 }
320 }, 320 },
321 enumerable: true 321 enumerable: true
322 }); 322 });
323 } 323 }
324 324
325 function init() 325 function init()
326 { 326 {
327 let prefs = Object.keys(defaults); 327 let prefs = Object.keys(defaults);
328 prefs.forEach(addPreference); 328 prefs.forEach(addPreference);
329 329
330 let localLoaded = new Promise(resolve => 330 let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then(
331 { 331 items =>
332 ext.storage.get(prefs.map(prefToKey), items =>
333 { 332 {
334 for (let key in items) 333 for (let key in items)
335 overrides[keyToPref(key)] = items[key]; 334 overrides[keyToPref(key)] = items[key];
335 },
336 (error) =>
kzar 2018/04/24 20:38:56 Nit: Superfluous parenthesis, also could we just p
Sebastian Noack 2018/04/24 21:12:55 Acknowledged.
kzar 2018/04/25 10:33:39 Ah right, well it's not a big deal then.
337 {
338 console.error(error);
339 }
340 );
336 341
337 resolve(); 342 let managedLoaded;
338 }); 343 if ("managed" in browser.storage)
339 });
340
341 let managedLoaded = new Promise(resolve =>
342 { 344 {
343 if ("managed" in browser.storage) 345 managedLoaded = browser.storage.managed.get(null).then(
344 { 346 items =>
345 browser.storage.managed.get(null, items =>
346 { 347 {
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) 348 for (let key in items)
354 defaults[key] = items[key]; 349 defaults[key] = items[key];
350 },
355 351
356 resolve(); 352 // Opera doesn't support browser.storage.managed, but instead of simply
357 }); 353 // removing the API, it gives an asynchronous error which we ignore here.
358 } 354 () => {}
359 else 355 );
360 { 356 }
361 resolve(); 357 else
362 } 358 {
363 }); 359 managedLoaded = Promise.resolve();
360 }
364 361
365 function onLoaded() 362 function onLoaded()
366 { 363 {
367 ext.storage.onChanged.addListener(changes => 364 browser.storage.onChanged.addListener(changes =>
368 { 365 {
369 for (let key in changes) 366 for (let key in changes)
370 { 367 {
371 let pref = keyToPref(key); 368 let pref = keyToPref(key);
372 if (pref && pref in defaults) 369 if (pref && pref in defaults)
373 { 370 {
374 let change = changes[key]; 371 let change = changes[key];
375 if ("newValue" in change && change.newValue != defaults[pref]) 372 if ("newValue" in change && change.newValue != defaults[pref])
376 overrides[pref] = change.newValue; 373 overrides[pref] = change.newValue;
377 else 374 else
378 delete overrides[pref]; 375 delete overrides[pref];
379 376
380 eventEmitter.emit(pref); 377 eventEmitter.emit(pref);
381 } 378 }
382 } 379 }
383 }); 380 });
384 } 381 }
385 382
386 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded); 383 Prefs.untilLoaded = Promise.all([localLoaded, managedLoaded]).then(onLoaded);
387 } 384 }
388 385
389 init(); 386 init();
OLDNEW
« lib/io.js ('K') | « lib/io.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld