Bug 1274708 Use Context.jsonStringify() in ExtensionStorage r?kmag draft
authorAndrew Swan <aswan@mozilla.com>
Tue, 14 Jun 2016 08:56:31 -0700
changeset 379719 03a152d69f40e7fc734a856f8b7b956942009494
parent 379718 742be2054363e5d353a485b3d8037d6a8c84f381
child 523547 5d50f273dea75c17e9846155ebe0d629c1a0f96a
push id21029
push useraswan@mozilla.com
push dateThu, 16 Jun 2016 17:15:51 +0000
reviewerskmag
bugs1274708
milestone50.0a1
Bug 1274708 Use Context.jsonStringify() in ExtensionStorage r?kmag MozReview-Commit-ID: Ba1w33Jl42n
toolkit/components/extensions/ExtensionStorage.jsm
toolkit/components/extensions/ext-storage.js
--- a/toolkit/components/extensions/ExtensionStorage.jsm
+++ b/toolkit/components/extensions/ExtensionStorage.jsm
@@ -66,27 +66,23 @@ this.ExtensionStorage = {
   extensionDir: Path.join(profileDir, "browser-extension-data"),
 
   /**
    * Sanitizes the given value, and returns a JSON-compatible
    * representation of it, based on the privileges of the given global.
    *
    * @param {value} value
    *        The value to sanitize.
-   * @param {object} global
-   *        The global for which to sanitize the value.
+   * @param {Context} context
+   *        The extension context in which to sanitize the value
    * @returns {value}
    *        The sanitized value.
    */
-  sanitize(value, global) {
-    // We can't trust that the global has privileges to access this
-    // value enough to clone it using a privileged JSON object.
-    let JSON_ = Cu.waiveXrays(global.JSON);
-
-    let json = JSON_.stringify(value, jsonReplacer);
+  sanitize(value, context) {
+    let json = context.jsonStringify(value, jsonReplacer);
     return JSON.parse(json);
   },
 
   getExtensionDir(extensionId) {
     return Path.join(this.extensionDir, extensionId);
   },
 
   getStorageFile(extensionId) {
@@ -128,21 +124,21 @@ this.ExtensionStorage = {
       "ExtensionStorage: Finish writing extension data",
       promise);
 
     return promise.then(() => {
       AsyncShutdown.profileBeforeChange.removeBlocker(promise);
     });
   },
 
-  set(extensionId, items, global) {
+  set(extensionId, items, context) {
     return this.read(extensionId).then(extData => {
       let changes = {};
       for (let prop in items) {
-        let item = this.sanitize(items[prop], global);
+        let item = this.sanitize(items[prop], context);
         changes[prop] = {oldValue: extData[prop], newValue: item};
         extData[prop] = item;
       }
 
       this.notifyListeners(extensionId, changes);
 
       return this.write(extensionId);
     });
--- a/toolkit/components/extensions/ext-storage.js
+++ b/toolkit/components/extensions/ext-storage.js
@@ -13,17 +13,17 @@ var {
 extensions.registerSchemaAPI("storage", (extension, context) => {
   return {
     storage: {
       local: {
         get: function(keys) {
           return ExtensionStorage.get(extension.id, keys);
         },
         set: function(items) {
-          return ExtensionStorage.set(extension.id, items, context.cloneScope);
+          return ExtensionStorage.set(extension.id, items, context);
         },
         remove: function(items) {
           return ExtensionStorage.remove(extension.id, items);
         },
         clear: function() {
           return ExtensionStorage.clear(extension.id);
         },
       },