Bug 1406181 - Move serialize/deserialize helpers from ext-c-storage.js to ExtensionStorage.jsm.
MozReview-Commit-ID: 4iundHpQ8d2
--- 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