Bug 1406181 - Move serialize/deserialize helpers from ext-c-storage.js to ExtensionStorage.jsm. draft
authorLuca Greco <lgreco@mozilla.com>
Wed, 18 Oct 2017 19:31:27 +0200
changeset 804735 cb4e8439928dc0d63fe0e1a5c69a231f8657513b
parent 804634 cec4a3cecc29ff97860198969b6fdff24b9e93bb
child 804736 1dba24e1d58c9daa7a97338b7a166914e9f3fb98
push id112456
push userluca.greco@alcacoop.it
push dateWed, 06 Jun 2018 14:37:14 +0000
bugs1406181
milestone62.0a1
Bug 1406181 - Move serialize/deserialize helpers from ext-c-storage.js to ExtensionStorage.jsm. 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