Bug 1624062 - Support webRequest listeners from extension pages in debug mode r=mixedpuppy
authorRob Wu <rob@robwu.nl>
Mon, 23 Mar 2020 16:16:26 +0000
changeset 520268 b61cb05fbfc9c0091aa0945a19df81d6a7ad306a
parent 520267 76b49244a016345b6b67a2c93b4ccd8a3e1a7d4e
child 520269 fec7d30f881143c9aa5ed4348bf378385ba8425a
push id37246
push useropoprus@mozilla.com
push dateWed, 25 Mar 2020 03:40:33 +0000
treeherdermozilla-central@14b59d4adc95 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1624062
milestone76.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1624062 - Support webRequest listeners from extension pages in debug mode r=mixedpuppy And remove an unused "ext"/"extension parameter from an existing test. Differential Revision: https://phabricator.services.mozilla.com/D67734
toolkit/components/extensions/ExtensionCommon.jsm
toolkit/components/extensions/test/xpcshell/test_ext_webRequest_from_extension_page.js
toolkit/components/extensions/test/xpcshell/test_ext_webRequest_requestSize.js
toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
--- a/toolkit/components/extensions/ExtensionCommon.jsm
+++ b/toolkit/components/extensions/ExtensionCommon.jsm
@@ -2178,31 +2178,31 @@ class EventManager {
     if (!DELAYED_BG_STARTUP) {
       this.persistent = null;
     }
 
     this.unregister = new Map();
     this.remove = new Map();
 
     if (this.persistent) {
-      if (this.context.viewType !== "background") {
-        this.persistent = null;
-      }
       if (AppConstants.DEBUG) {
         if (this.context.envType !== "addon_parent") {
           throw new Error(
             "Persistent event managers can only be created for addon_parent"
           );
         }
         if (!this.persistent.module || !this.persistent.event) {
           throw new Error(
             "Persistent event manager must specify module and event"
           );
         }
       }
+      if (this.context.viewType !== "background") {
+        this.persistent = null;
+      }
     }
   }
 
   /*
    * Information about listeners to persistent events is associated with
    * the extension to which they belong.  Any extension thas has such
    * listeners has a property called `persistentListeners` that is a
    * 3-level Map.  The first 2 keys are the module name (e.g., webRequest)
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_webRequest_from_extension_page.js
@@ -0,0 +1,57 @@
+"use strict";
+
+const server = createHttpServer({ hosts: ["example.com"] });
+server.registerPathHandler("/HELLO", (req, res) => {
+  res.write("BYE");
+});
+
+add_task(async function request_from_extension_page() {
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: ["http://example.com/", "webRequest", "webRequestBlocking"],
+    },
+    files: {
+      "tab.html": `<!DOCTYPE html><script src="tab.js"></script>`,
+      "tab.js": async function() {
+        browser.webRequest.onHeadersReceived.addListener(
+          details => {
+            let { responseHeaders } = details;
+            responseHeaders.push({
+              name: "X-Added-by-Test",
+              value: "TheValue",
+            });
+            return { responseHeaders };
+          },
+          {
+            urls: ["http://example.com/HELLO"],
+          },
+          ["blocking", "responseHeaders"]
+        );
+
+        // Ensure that listener is registered (workaround for bug 1300234).
+        await browser.runtime.getPlatformInfo();
+
+        let response = await fetch("http://example.com/HELLO");
+        browser.test.assertEq(
+          "TheValue",
+          response.headers.get("X-added-by-test"),
+          "expected response header from webRequest listener"
+        );
+        browser.test.assertEq(
+          await response.text(),
+          "BYE",
+          "Expected response from server"
+        );
+        browser.test.sendMessage("done");
+      },
+    },
+  });
+  await extension.startup();
+  let contentPage = await ExtensionTestUtils.loadContentPage(
+    `moz-extension://${extension.uuid}/tab.html`,
+    { extension }
+  );
+  await extension.awaitMessage("done");
+  await contentPage.close();
+  await extension.unload();
+});
--- a/toolkit/components/extensions/test/xpcshell/test_ext_webRequest_requestSize.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_webRequest_requestSize.js
@@ -43,16 +43,15 @@ const EXTENSION_DATA = {
   },
 };
 
 add_task(async function test_request_response_size() {
   let ext = ExtensionTestUtils.loadExtension(EXTENSION_DATA);
   await ext.startup();
 
   let contentPage = await ExtensionTestUtils.loadContentPage(
-    `${gServerUrl}/dummy`,
-    { ext }
+    `${gServerUrl}/dummy`
   );
   await ext.awaitMessage("check");
   await ext.awaitMessage("done");
   await contentPage.close();
   await ext.unload();
 });
--- a/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
@@ -167,16 +167,17 @@ skip-if = true # Too frequent intermitte
 [test_ext_userScripts_telemetry.js]
 [test_ext_webRequest_auth.js]
 skip-if = os == "android" && debug
 [test_ext_webRequest_cached.js]
 skip-if = os == "android" && processor != "x86_64" # Bug 1573511
 [test_ext_webRequest_cancelWithReason.js]
 [test_ext_webRequest_filterResponseData.js]
 skip-if = os == "android" && debug
+[test_ext_webRequest_from_extension_page.js]
 [test_ext_webRequest_incognito.js]
 skip-if = os == "android" && debug
 [test_ext_webRequest_host.js]
 skip-if = os == "android" && debug
 [test_ext_webRequest_permission.js]
 skip-if = os == "android" && debug
 [test_ext_webRequest_requestSize.js]
 [test_ext_webRequest_responseBody.js]