Bug 1274708 Use Context.jsonStringify() in ExtensionStorage r?kmag draft
authorAndrew Swan <aswan@mozilla.com>
Tue, 14 Jun 2016 08:56:31 -0700
changeset 379694 977d69813d5a3c1d1f95756db7b711735fec0a26
parent 379693 2d1f01f426a05229aa606eac54b012489fb89dd5
child 523541 202d3a0cdb5804aeb5eb74ab6ff4cd004aa693e8
push id21023
push useraswan@mozilla.com
push dateThu, 16 Jun 2016 15:32:37 +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);
         },
       },