Bug 1284020: Treat window.fetch/window.XMLHttpRequest the same as the global variants in content scripts. r=aswan
authorKris Maglione <maglione.k@gmail.com>
Tue, 12 Jul 2016 15:45:07 -0700
changeset 329977 3e3b1eb03a908d02dac0bcb25c93eb8b734b5124
parent 329976 b50931952fbb76037bff76715e1f17f7fd80e34c
child 329978 a097b61f58102776941bd3fa0023092ac3f27a3f
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan
bugs1284020
milestone50.0a1
Bug 1284020: Treat window.fetch/window.XMLHttpRequest the same as the global variants in content scripts. r=aswan MozReview-Commit-ID: Cuz6CiNZdrd
devtools/client/debugger/test/mochitest/browser_dbg_sources-webext-contentscript.js
toolkit/components/extensions/ExtensionContent.jsm
toolkit/components/extensions/test/mochitest/test_ext_permission_xhr.html
--- a/devtools/client/debugger/test/mochitest/browser_dbg_sources-webext-contentscript.js
+++ b/devtools/client/debugger/test/mochitest/browser_dbg_sources-webext-contentscript.js
@@ -37,17 +37,17 @@ function test() {
     let options = {
       source: `moz-extension://${uuid}/webext-content-script.js`,
       line: 1
     };
     [,, gPanel] = yield initDebugger(TAB_URL, options);
     gDebugger = gPanel.panelWin;
     gSources = gDebugger.DebuggerView.Sources;
 
-    is(gSources.values.length, 1, "Should have 1 source");
+    is(gSources.values.length, 2, "Should have 2 sources");
 
     let item = gSources.getItemForAttachment(attachment => {
       return attachment.source.url.includes("moz-extension");
     });
 
     ok(item, "Got the expected WebExtensions ContentScript source");
     ok(item && item.attachment.source.url.includes(item.attachment.group),
        "The source is in the expected source group");
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -381,16 +381,22 @@ class ExtensionContext extends BaseConte
 
       this.sandbox = Cu.Sandbox(prin, {
         metadata,
         sandboxPrototype: contentWindow,
         wantXrays: true,
         isWebExtensionContentScript: true,
         wantGlobalProperties: ["XMLHttpRequest", "fetch"],
       });
+
+      Cu.evalInSandbox(`
+        window.JSON = JSON;
+        window.XMLHttpRequest = XMLHttpRequest;
+        window.fetch = fetch;
+      `, this.sandbox);
     }
 
     let delegate = {
       getSender(context, target, sender) {
         // Nothing to do here.
       },
     };
 
--- a/toolkit/components/extensions/test/mochitest/test_ext_permission_xhr.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_permission_xhr.html
@@ -12,24 +12,26 @@
 
 <script type="text/javascript">
 "use strict";
 
 /* eslint-disable mozilla/balanced-listeners */
 
 add_task(function* test_simple() {
   function runTests(cx) {
-    function xhr(url) {
-      return new Promise((resolve, reject) => {
-        let req = new XMLHttpRequest();
-        req.open("GET", url);
-        req.addEventListener("load", resolve);
-        req.addEventListener("error", reject);
-        req.send();
-      });
+    function xhr(XMLHttpRequest) {
+      return (url) => {
+        return new Promise((resolve, reject) => {
+          let req = new XMLHttpRequest();
+          req.open("GET", url);
+          req.addEventListener("load", resolve);
+          req.addEventListener("error", reject);
+          req.send();
+        });
+      };
     }
 
     function run(shouldFail, fetch) {
       function passListener() {
         browser.test.succeed(`${cx}.${fetch.name} pass listener`);
       }
 
       function failListener() {
@@ -38,20 +40,24 @@ add_task(function* test_simple() {
 
       if (shouldFail) {
         return fetch("http://example.org/example.txt").then(failListener, passListener);
       } else {
         return fetch("http://example.com/example.txt").then(passListener, failListener);
       }
     }
 
-    return run(true, xhr)
-      .then(() => run(false, xhr))
+    return run(true, xhr(XMLHttpRequest))
+      .then(() => run(false, xhr(XMLHttpRequest)))
+      .then(() => run(true, xhr(window.XMLHttpRequest)))
+      .then(() => run(false, xhr(window.XMLHttpRequest)))
       .then(() => run(true, fetch))
       .then(() => run(false, fetch))
+      .then(() => run(true, window.fetch))
+      .then(() => run(false, window.fetch))
       .catch(err => {
         browser.test.fail(`Error: ${err} :: ${err.stack}`);
         browser.test.notifyFail("permission_xhr");
       });
   }
 
   function background(runTests) {
     runTests("bg").then(() => {