Bug 1406181 - Move serialize/deserialize helpers from ext-c-storage.js to ExtensionStorage.jsm. r=kmag
☠☠ backed out by 3d3c72079cfd ☠ ☠
authorLuca Greco <lgreco@mozilla.com>
Wed, 18 Oct 2017 19:31:27 +0200
changeset 421086 fdac47b8ef20d45c7828f2185c063ee92019c5a0
parent 421085 561dd8c84f8576b2c55286fec1937539c15a31d6
child 421087 912a2eaf4d26414f9ae2acd38afe8be383391dbc
push id34088
push userdluca@mozilla.com
push dateMon, 04 Jun 2018 17:56:32 +0000
treeherdermozilla-central@5b15326286d4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1406181
milestone62.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 1406181 - Move serialize/deserialize helpers from ext-c-storage.js to ExtensionStorage.jsm. r=kmag MozReview-Commit-ID: 4iundHpQ8d2
toolkit/components/extensions/ExtensionStorage.jsm
toolkit/components/extensions/child/ext-storage.js
--- a/toolkit/components/extensions/ExtensionStorage.jsm
+++ b/toolkit/components/extensions/ExtensionStorage.jsm
@@ -340,15 +340,68 @@ var ExtensionStorage = {
       Services.obs.removeObserver(this, "xpcom-shutdown");
     } else if (topic == "extension-invalidate-storage-cache") {
       for (let promise of this.jsonFilePromises.values()) {
         promise.then(jsonFile => { jsonFile.finalize(); });
       }
       this.jsonFilePromises.clear();
     }
   },
+
+  /**
+   * Serializes the given storage items for transporting between processes.
+   *
+   * @param {BaseContext} context
+   *        The context to use for the created StructuredCloneHolder
+   *        objects.
+   * @param {Array<string>|object} items
+   *        The items to serialize. If an object is provided, its
+   *        values are serialized to StructuredCloneHolder objects.
+   *        Otherwise, it is returned as-is.
+   * @returns {Array<string>|object}
+   */
+  serializeForContext(context, items) {
+    if (items && typeof items === "object" && !Array.isArray(items)) {
+      let result = {};
+      for (let [key, value] of Object.entries(items)) {
+        try {
+          result[key] = new StructuredCloneHolder(value, context.cloneScope);
+        } catch (e) {
+          throw new ExtensionUtils.ExtensionError(String(e));
+        }
+      }
+      return result;
+    }
+    return items;
+  },
+
+  /**
+   * Deserializes the given storage items into the given extension context.
+   *
+   * @param {BaseContext} context
+   *        The context to use to deserialize the StructuredCloneHolder objects.
+   * @param {object} items
+   *        The items to deserialize. Any property of the object which
+   *        is a StructuredCloneHolder instance is deserialized into
+   *        the extension scope. Any other object is cloned into the
+   *        extension scope directly.
+   * @returns {object}
+   */
+  deserializeForContext(context, items) {
+    let result = new context.cloneScope.Object();
+    for (let [key, value] of Object.entries(items)) {
+      if (value && typeof value === "object" &&
+          Cu.getClassName(value, true) === "StructuredCloneHolder") {
+        value = value.deserialize(context.cloneScope);
+      } else {
+        value = Cu.cloneInto(value, context.cloneScope);
+      }
+      result[key] = value;
+    }
+    return result;
+  },
 };
 
 XPCOMUtils.defineLazyGetter(
   ExtensionStorage, "extensionDir",
   () => OS.Path.join(OS.Constants.Path.profileDir, "browser-extension-data"));
 
 ExtensionStorage.init();
--- a/toolkit/components/extensions/child/ext-storage.js
+++ b/toolkit/components/extensions/child/ext-storage.js
@@ -1,72 +1,22 @@
 "use strict";
 
 ChromeUtils.defineModuleGetter(this, "ExtensionStorage",
                                "resource://gre/modules/ExtensionStorage.jsm");
 ChromeUtils.defineModuleGetter(this, "TelemetryStopwatch",
                                "resource://gre/modules/TelemetryStopwatch.jsm");
 
-var {
-  ExtensionError,
-} = ExtensionUtils;
-
 const storageGetHistogram = "WEBEXT_STORAGE_LOCAL_GET_MS";
 const storageSetHistogram = "WEBEXT_STORAGE_LOCAL_SET_MS";
 
 this.storage = class extends ExtensionAPI {
   getAPI(context) {
-    /**
-     * Serializes the given storage items for transporting to the parent
-     * process.
-     *
-     * @param {Array<string>|object} items
-     *        The items to serialize. If an object is provided, its
-     *        values are serialized to StructuredCloneHolder objects.
-     *        Otherwise, it is returned as-is.
-     * @returns {Array<string>|object}
-     */
-    function serialize(items) {
-      if (items && typeof items === "object" && !Array.isArray(items)) {
-        let result = {};
-        for (let [key, value] of Object.entries(items)) {
-          try {
-            result[key] = new StructuredCloneHolder(value, context.cloneScope);
-          } catch (e) {
-            throw new ExtensionError(String(e));
-          }
-        }
-        return result;
-      }
-      return items;
-    }
-
-    /**
-     * Deserializes the given storage items from the parent process into
-     * the extension context.
-     *
-     * @param {object} items
-     *        The items to deserialize. Any property of the object which
-     *        is a StructuredCloneHolder instance is deserialized into
-     *        the extension scope. Any other object is cloned into the
-     *        extension scope directly.
-     * @returns {object}
-     */
-    function deserialize(items) {
-      let result = new context.cloneScope.Object();
-      for (let [key, value] of Object.entries(items)) {
-        if (value && typeof value === "object" && Cu.getClassName(value, true) === "StructuredCloneHolder") {
-          value = value.deserialize(context.cloneScope);
-        } else {
-          value = Cu.cloneInto(value, context.cloneScope);
-        }
-        result[key] = value;
-      }
-      return result;
-    }
+    const serialize = ExtensionStorage.serializeForContext.bind(null, context);
+    const deserialize = ExtensionStorage.deserializeForContext.bind(null, context);
 
     function sanitize(items) {
       // The schema validator already takes care of arrays (which are only allowed
       // to contain strings). Strings and null are safe values.
       if (typeof items != "object" || items === null || Array.isArray(items)) {
         return items;
       }
       // If we got here, then `items` is an object generated by `ObjectType`'s