proposal-followup - minor changes on ProxyContext and always use context.cloneScope as the context sandbox. f?kmag,aswan draft
authorLuca Greco <lgreco@mozilla.com>
Fri, 05 Aug 2016 17:57:18 +0200
changeset 397389 c9bcb18e8fc86e587b1343188d280ef1ade3b055
parent 397388 fb8d23b86089ba01475140294adddc7e841eee8a
child 527440 4ef975f57667db960605cac5af6c950ded8f71da
push id25281
push userluca.greco@alcacoop.it
push dateFri, 05 Aug 2016 18:59:22 +0000
milestone51.0a1
proposal-followup - minor changes on ProxyContext and always use context.cloneScope as the context sandbox. f?kmag,aswan MozReview-Commit-ID: JTRQJnzb4lY
toolkit/components/extensions/Extension.jsm
toolkit/components/extensions/test/xpcshell/test_ext_contexts.js
--- a/toolkit/components/extensions/Extension.jsm
+++ b/toolkit/components/extensions/Extension.jsm
@@ -310,32 +310,33 @@ ExtensionContext = class extends BaseCon
 
 class ProxyContext extends ExtensionContext {
   constructor(extension, params, messageManager, principal) {
     params.contentWindow = null;
     params.uri = NetUtil.newURI(params.url);
 
     super(extension, params);
     this.messageManager = messageManager;
-    this.principal_ = principal;
+
+    Object.defineProperty(
+      this, "principal",
+      {value: principal, enumerable: true, configurable: true}
+    );
 
     this.apiObj = {};
-    GlobalManager.injectInObject(extension, this, null, this.apiObj, ["storage", "test"]);
+    this.injectAPIsInObject(this.apiObj, ["storage", "test"]);
 
     this.listenerProxies = new Map();
 
-    this.sandbox = Cu.Sandbox(principal, {});
-  }
+    let sandbox = Cu.Sandbox(principal, {});
 
-  get principal() {
-    return this.principal_;
-  }
-
-  get cloneScope() {
-    return this.sandbox;
+    Object.defineProperty(
+      this, "cloneScope",
+      {value: sandbox, enumerable: true, configurable: true}
+    );
   }
 
   get externallyVisible() {
     return false;
   }
 }
 
 function findPathInObject(obj, path) {
@@ -418,17 +419,17 @@ let ParentAPIManager = {
         childId: data.childId,
         callId: data.callId,
         args: cbArgs,
         lastError: lastError ? lastError.message : null,
       });
     }
 
     let args = data.args;
-    args = Cu.cloneInto(args, context.sandbox);
+    args = Cu.cloneInto(args, context.cloneScope);
     if (data.callId) {
       args = args.concat(callback);
     }
     try {
       findPathInObject(context.apiObj, data.path)[data.name](...args);
     } catch (e) {
       let msg = e.message || "API failed";
       target.messageManager.sendAsyncMessage("API:CallResult", {
@@ -449,17 +450,17 @@ let ParentAPIManager = {
         name: data.name,
         args: listenerArgs,
       });
     }
 
     let ref = data.path.concat(data.name).join(".");
     context.listenerProxies.set(ref, listener);
 
-    let args = Cu.cloneInto(data.args, context.sandbox);
+    let args = Cu.cloneInto(data.args, context.cloneScope);
     findPathInObject(context.apiObj, data.path)[data.name].addListener(listener, ...args);
   },
 
   removeListener(data) {
     let context = this.proxyContexts.get(data.childId);
     let ref = data.path.concat(data.name).join(".");
     let listener = context.listenerProxies.get(ref);
     findPathInObject(context.apiObj, data.path)[data.name].removeListener(listener);
--- a/toolkit/components/extensions/test/xpcshell/test_ext_contexts.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_contexts.js
@@ -146,41 +146,41 @@ class Context extends BaseContext {
 
 let ssm = Services.scriptSecurityManager;
 const PRINCIPAL1 = ssm.createCodebasePrincipalFromOrigin("http://www.example.org");
 const PRINCIPAL2 = ssm.createCodebasePrincipalFromOrigin("http://www.somethingelse.org");
 
 // Test that toJSON() works in the json sandbox
 add_task(function* test_stringify_toJSON() {
   let context = new Context(PRINCIPAL1);
-  let obj = Cu.evalInSandbox("({hidden: true, toJSON() { return {visible: true}; } })", context.sandbox);
+  let obj = Cu.evalInSandbox("({hidden: true, toJSON() { return {visible: true}; } })", context.cloneScope);
 
   let stringified = context.jsonStringify(obj);
   let expected = JSON.stringify({visible: true});
   equal(stringified, expected, "Stringified object with toJSON() method is as expected");
 });
 
 // Test that stringifying in inaccessible property throws
 add_task(function* test_stringify_inaccessible() {
   let context = new Context(PRINCIPAL1);
-  let sandbox = context.sandbox;
+  let sandbox = context.cloneScope;
   let sandbox2 = Cu.Sandbox(PRINCIPAL2);
 
   Cu.waiveXrays(sandbox).subobj = Cu.evalInSandbox("({ subobject: true })", sandbox2);
   let obj = Cu.evalInSandbox("({ local: true, nested: subobj })", sandbox);
   Assert.throws(() => {
     context.jsonStringify(obj);
   });
 });
 
 add_task(function* test_stringify_accessible() {
   // Test that an accessible property from another global is included
   let principal = ssm.createExpandedPrincipal([PRINCIPAL1, PRINCIPAL2]);
   let context = new Context(principal);
-  let sandbox = context.sandbox;
+  let sandbox = context.cloneScope;
   let sandbox2 = Cu.Sandbox(PRINCIPAL2);
 
   Cu.waiveXrays(sandbox).subobj = Cu.evalInSandbox("({ subobject: true })", sandbox2);
   let obj = Cu.evalInSandbox("({ local: true, nested: subobj })", sandbox);
   let stringified = context.jsonStringify(obj);
 
   let expected = JSON.stringify({local: true, nested: {subobject: true}});
   equal(stringified, expected, "Stringified object with accessible property is as expected");