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: Fixed circular dependency Created April 26, 2018, 6:22 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
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/remove() operation completes
+ */
+ set(preference, value)
+ {
+ let defaultValue = defaults[preference];
+
+ if (typeof value != typeof defaultValue)
+ throw new Error("Attempt to change preference type");
Thomas Greiner 2018/04/27 14:50:12 Why don't we return `Promise.reject()` here? It's
Sebastian Noack 2018/04/27 16:01:03 Well, if we already know immediately/synchronously
Thomas Greiner 2018/04/27 16:27:33 True, if that's a requirement we could check twice
Sebastian Noack 2018/04/27 16:33:22 Well, compatibility with the setter property isn't
Thomas Greiner 2018/04/27 17:03:32 As I said, we can still throw the error in the set
Sebastian Noack 2018/04/27 17:11:11 Yes, we could signal the error asynchronously here
Thomas Greiner 2018/04/27 17:16:06 Note that assertions may fail asynchronously in wh
+
+ if (value == defaultValue)
+ {
+ delete overrides[preference];
+ return browser.storage.local.remove(prefToKey(preference));
+ }
+
+ 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)
{
- browser.storage.local.set({[prefToKey(pref)]: overrides[pref]});
+ 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();
Thomas Greiner 2018/04/27 14:50:12 What if an error occurs in `savePref()`? I'd argue
Sebastian Noack 2018/04/27 16:01:04 Yes, returning a resolved promise immediately is a
Sebastian Noack 2018/04/27 16:14:52 Never mind, it turned out returning asynchronously
Thomas Greiner 2018/04/27 16:27:33 Thanks.
}
- else
- callback();
+ return Promise.resolve();
});
}
@@ -302,21 +328,7 @@
get() { return (pref in overrides ? overrides : defaults)[pref]; },
set(value)
Thomas Greiner 2018/04/27 14:50:12 Suggestion: What about marking this setter as depr
Sebastian Noack 2018/04/27 16:01:03 We didn't decide yet, whether we want to consider
Thomas Greiner 2018/04/27 16:27:33 Acknowledged.
{
- let defaultValue = defaults[pref];
-
- if (typeof value != typeof defaultValue)
- throw new Error("Attempt to change preference type");
-
- if (value == defaultValue)
- {
- delete overrides[pref];
- browser.storage.local.remove(prefToKey(pref));
- }
- else
- {
- overrides[pref] = value;
- (customSave.get(pref) || savePref)(pref);
- }
+ Prefs.set(pref, value);
},
enumerable: true
});
@@ -332,10 +344,6 @@
{
for (let key in items)
overrides[keyToPref(key)] = items[key];
- },
- (error) =>
- {
- console.error(error);
}
);
« no previous file with comments | « dependencies ('k') | lib/subscriptionInit.js » ('j') | lib/subscriptionInit.js » ('J')

Powered by Google App Engine
This is Rietveld