Bug 1320181 - Storage sanitizer should return an empty object belonging to the correct scope. r=kmag
authorLuca Greco <lgreco@mozilla.com>
Mon, 28 Nov 2016 20:11:06 +0100
changeset 325157 f463014033520214bc213683fcb086ee3848cd30
parent 325156 30afe7e8fb7115df44796764ea85d784797e135a
child 325158 a8e20bf36959426f0a6eb4b5df29ef3dd85ad4bd
push id84610
push userphilringnalda@gmail.com
push dateSat, 03 Dec 2016 06:28:13 +0000
treeherdermozilla-inbound@1b2237e0b5e0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1320181
milestone53.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1320181 - Storage sanitizer should return an empty object belonging to the correct scope. r=kmag MozReview-Commit-ID: Bx95Cgx0EuH
toolkit/components/extensions/ExtensionStorage.jsm
--- 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) {