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

Unified Diff: lib/prefs.js

Issue 29760565: Issue 6599 - Detect data corruption of storage.local (Closed)
Patch Set: Removed redundant Promise wrapper Created April 24, 2018, 2:55 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/subscriptionInit.js » ('j') | lib/subscriptionInit.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/prefs.js
===================================================================
--- a/lib/prefs.js
+++ b/lib/prefs.js
@@ -214,6 +214,31 @@
*/
let Prefs = exports.Prefs = {
/**
+ * Sets the given preference.
+ *
+ * @param {string} preference
+ * @param {any} value
+ * @return {Promise} A promise that resolves when the underlying
+ browser.storage.local.set() operation completes
+ */
+ set(preference, value)
+ {
+ let defaultValue = defaults[preference];
+
+ if (typeof value != typeof defaultValue)
+ throw new Error("Attempt to change preference type");
+
+ if (value == defaultValue)
+ {
+ delete overrides[preference];
+ return browser.storage.local.remove(prefToKey(preference));
kzar 2018/04/24 16:18:46 Nit: Since we're just calling browser.storage.loca
Sebastian Noack 2018/04/24 17:49:25 Well, nothing changed in this regard. Except for r
+ }
+
+ overrides[preference] = value;
+ return (customSave.get(preference) || savePref)(preference);
+ },
+
+ /**
* Adds a callback that is called when the
* value of a specified preference changed.
*
@@ -261,7 +286,7 @@
function savePref(pref)
{
- ext.storage.set(prefToKey(pref), overrides[pref]);
kzar 2018/04/24 16:18:46 As discussed in IRC let's replace ext.storage with
Sebastian Noack 2018/04/24 17:49:25 There we go: https://codereview.adblockplus.org/29
+ return browser.storage.local.set(prefToKey(pref), overrides[pref]);
}
let customSave = new Map();
@@ -275,24 +300,25 @@
let updateScheduled = false;
customSave.set("blocked_total", pref =>
{
- if (updateScheduled)
- return;
-
- let callback = () =>
+ if (!updateScheduled)
{
- lastUpdate = performance.now();
- updateScheduled = false;
- savePref(pref);
- };
+ let callback = () =>
+ {
+ lastUpdate = performance.now();
+ updateScheduled = false;
+ savePref(pref);
+ };
- let timeElapsed = performance.now() - lastUpdate;
- if (timeElapsed < MIN_UPDATE_INTERVAL)
- {
- setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed);
- updateScheduled = true;
+ let timeElapsed = performance.now() - lastUpdate;
+ if (timeElapsed < MIN_UPDATE_INTERVAL)
+ {
+ setTimeout(callback, MIN_UPDATE_INTERVAL - timeElapsed);
+ updateScheduled = true;
+ }
+ else
+ callback();
}
- else
- callback();
+ return Promise.resolve();
});
}
@@ -302,21 +328,7 @@
get() { return (pref in overrides ? overrides : defaults)[pref]; },
set(value)
{
- let defaultValue = defaults[pref];
-
- if (typeof value != typeof defaultValue)
- throw new Error("Attempt to change preference type");
-
- if (value == defaultValue)
- {
- delete overrides[pref];
- ext.storage.remove(prefToKey(pref));
- }
- else
- {
- overrides[pref] = value;
- (customSave.get(pref) || savePref)(pref);
- }
+ Prefs.set(pref, value);
},
enumerable: true
});
@@ -327,15 +339,10 @@
let prefs = Object.keys(defaults);
prefs.forEach(addPreference);
- let localLoaded = new Promise(resolve =>
+ let localLoaded = browser.storage.local.get(prefs.map(prefToKey)).then(items =>
kzar 2018/04/24 16:18:46 Nit: This line is too long in Rietveld, did it pas
Sebastian Noack 2018/04/24 17:49:25 This is no longer the case after the rebase.
kzar 2018/04/25 10:53:51 Acknowledged.
{
- ext.storage.get(prefs.map(prefToKey), items =>
- {
- for (let key in items)
- overrides[keyToPref(key)] = items[key];
-
- resolve();
- });
+ for (let key in items)
+ overrides[keyToPref(key)] = items[key];
kzar 2018/04/24 16:18:46 We just had the key above, so is there not a way w
Sebastian Noack 2018/04/24 17:49:25 What do you have in mind? let keys = prefs.map(
kzar 2018/04/25 10:53:51 Yea, fair enough I can't see a much better way to
kzar 2018/04/25 10:59:07 (I had assumed that browser.storage.local.get woul
});
let managedLoaded = new Promise(resolve =>
« no previous file with comments | « no previous file | lib/subscriptionInit.js » ('j') | lib/subscriptionInit.js » ('J')

Powered by Google App Engine
This is Rietveld