Bug 1247081: Support fragment IDs and query strings in web_accessible_resources URLs. r?billm draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 12 Feb 2016 22:36:45 -0800
changeset 333225 a6f88226e5295d20316d492f129fd948bb53d8d9
parent 333224 fd90f5266bd50e711857089143f8e64e2477f660
child 514671 e209781cf45f13eaf80ce4d566d2c5533dbeb367
push id11298
push usermaglione.k@gmail.com
push dateTue, 23 Feb 2016 00:37:01 +0000
reviewersbillm
bugs1247081
milestone47.0a1
Bug 1247081: Support fragment IDs and query strings in web_accessible_resources URLs. r?billm MozReview-Commit-ID: KHOnavpnVfA
toolkit/components/extensions/ExtensionManagement.jsm
toolkit/components/extensions/test/mochitest/mochitest.ini
toolkit/components/extensions/test/mochitest/test_ext_web_accessible_resources.html
--- a/toolkit/components/extensions/ExtensionManagement.jsm
+++ b/toolkit/components/extensions/ExtensionManagement.jsm
@@ -179,17 +179,17 @@ var Service = {
   // determines this.
   extensionURILoadableByAnyone(uri) {
     let uuid = uri.host;
     let extension = this.uuidMap.get(uuid);
     if (!extension) {
       return false;
     }
 
-    let path = uri.path;
+    let path = uri.QueryInterface(Ci.nsIURL).filePath;
     if (path.length > 0 && path[0] == "/") {
       path = path.substr(1);
     }
     return extension.webAccessibleResources.has(path);
   },
 
   // Checks whether a given extension can load this URI (typically via
   // an XML HTTP request). The manifest.json |permissions| directive
--- a/toolkit/components/extensions/test/mochitest/mochitest.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest.ini
@@ -62,12 +62,13 @@ skip-if = e10s || buildapp == 'b2g' # Us
 skip-if = buildapp == 'b2g' # unimplemented api.
 [test_ext_alarms.html]
 [test_ext_background_window_properties.html]
 [test_ext_background_sub_windows.html]
 [test_ext_background_api_injection.html]
 [test_ext_jsversion.html]
 skip-if = e10s || buildapp == 'b2g' # Uses a console monitor which doesn't work from a content process. The code being tested doesn't run in a tab content process in any case.
 [test_ext_i18n.html]
+[test_ext_web_accessible_resources.html]
 [test_ext_webrequest.html]
 skip-if = buildapp == 'b2g' # webrequest api uninplemented (bug 1199504)
 [test_ext_webnavigation.html]
 skip-if = buildapp == 'b2g' # needs TabManager which is not yet implemented
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/mochitest/test_ext_web_accessible_resources.html
@@ -0,0 +1,133 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test the web_accessible_resources manifest directive</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
+  <script type="text/javascript" src="head.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<script type="text/javascript">
+"use strict";
+
+/* eslint-disable mozilla/balanced-listeners */
+
+add_task(function* test_web_accessible_resources() {
+  function background() {
+    let gotURL;
+    let tabId;
+
+    function loadFrame(url) {
+      return new Promise(resolve => {
+        browser.tabs.sendMessage(tabId, ["load-iframe", url], reply => {
+          resolve(reply);
+        });
+      });
+    }
+
+    let urls = [
+      [browser.extension.getURL("accessible.html"), true],
+      [browser.extension.getURL("accessible.html") + "?foo=bar", true],
+      [browser.extension.getURL("accessible.html") + "#!foo=bar", true],
+      [browser.extension.getURL("forbidden.html"), false],
+    ];
+
+    function runTest() {
+      if (!urls.length) {
+        browser.test.notifyPass("web-accessible-resources");
+        return;
+      }
+
+      let [url, shouldLoad] = urls.shift();
+      return loadFrame(url).then(success => {
+        browser.test.assertEq(shouldLoad, success, "Load was successful");
+        if (shouldLoad) {
+          browser.test.assertEq(url, gotURL, "Got expected url");
+        } else {
+          browser.test.assertEq(undefined, gotURL, "Got no url");
+        }
+        gotURL = undefined;
+
+        return runTest();
+      });
+    }
+
+    browser.runtime.onMessage.addListener(([msg, url], sender) => {
+      if (msg == "content-script-ready") {
+        tabId = sender.tab.id;
+        runTest();
+      } else if (msg == "page-script") {
+        browser.test.assertEq(undefined, gotURL, "Should have gotten only one message");
+        browser.test.assertEq("string", typeof(url), "URL should be a string");
+        gotURL = url;
+      }
+    });
+
+    browser.test.sendMessage("ready");
+  }
+
+  function contentScript() {
+    browser.runtime.onMessage.addListener(([msg, url], sender, respond) => {
+      if (msg == "load-iframe") {
+        let iframe = document.createElement("iframe");
+        iframe.setAttribute("src", url);
+        iframe.addEventListener("load", () => { respond(true); });
+        iframe.addEventListener("error", () => { respond(false); });
+        document.body.appendChild(iframe);
+        return true;
+      }
+    });
+    browser.runtime.sendMessage(["content-script-ready"]);
+  }
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      content_scripts: [
+        {
+          "matches": ["http://example.com/"],
+          "js": ["content_script.js"],
+          "run_at": "document_idle",
+        },
+      ],
+
+      "web_accessible_resources": [
+        "accessible.html",
+      ],
+    },
+
+    background: `(${background})()`,
+
+    files: {
+      "content_script.js": `(${contentScript})()`,
+
+      "accessible.html": `<html><head>
+        <meta charset="utf-8">
+        <script>browser.runtime.sendMessage(["page-script", location.href]);</${"script"}>
+      </head></html>`,
+
+      "inaccessible.html": `<html><head>
+        <meta charset="utf-8">
+        <script>browser.runtime.sendMessage(["page-script", location.href]);</${"script"}>
+      </head></html>`,
+    },
+  });
+
+  yield extension.startup();
+
+  yield extension.awaitMessage("ready");
+
+  let win = window.open("http://example.com/");
+
+  yield extension.awaitFinish("web-accessible-resources");
+
+  win.close();
+
+  yield extension.unload();
+});
+</script>
+
+</body>
+</html>