Bug 1320181 - Storage sanitizer should return an empty object belonging to the correct scope.
MozReview-Commit-ID: Bx95Cgx0EuH
--- a/toolkit/components/extensions/ExtensionStorage.jsm
+++ b/toolkit/components/extensions/ExtensionStorage.jsm
@@ -13,17 +13,30 @@ const Cr = Components.results;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS",
"resource://gre/modules/osfile.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown",
"resource://gre/modules/AsyncShutdown.jsm");
-function jsonReplacer(key, value) {
+/**
+ * Helper function used to sanitize the objects that have to be saved in the ExtensionStorage.
+ *
+ * @param {BaseContext} context
+ * The current extension context.
+ * @param {string} key
+ * The key of the current JSON property.
+ * @param {any} value
+ * The value of the current JSON property.
+ *
+ * @returns {any}
+ * The sanitized value of the property.
+ */
+function jsonReplacer(context, key, value) {
switch (typeof(value)) {
// Serialize primitive types as-is.
case "string":
case "number":
case "boolean":
return value;
case "object":
@@ -44,17 +57,17 @@ function jsonReplacer(key, value) {
return String(value);
}
break;
}
if (!key) {
// If this is the root object, and we can't serialize it, serialize
// the value to an empty object.
- return {};
+ return new context.cloneScope.Object();
}
// Everything else, omit entirely.
return undefined;
}
this.ExtensionStorage = {
cache: new Map(),
@@ -67,17 +80,17 @@ this.ExtensionStorage = {
* @param {value} value
* The value to sanitize.
* @param {Context} context
* The extension context in which to sanitize the value
* @returns {value}
* The sanitized value.
*/
sanitize(value, context) {
- let json = context.jsonStringify(value, jsonReplacer);
+ let json = context.jsonStringify(value, jsonReplacer.bind(null, context));
return JSON.parse(json);
},
getExtensionDir(extensionId) {
return OS.Path.join(this.extensionDir, extensionId);
},
getStorageFile(extensionId) {