Bug 1276338 - chrome.runtime.id is undefined in content scripts, r?kmag draft
authorBob Silverberg <bsilverberg@mozilla.com>
Tue, 31 May 2016 11:55:30 -0400
changeset 373639 a194b0e919b6bc04846464eae99b80cdfd83d5b3
parent 372916 4dbe106594fa8197404081eb801cbd4b2a42d06b
child 522436 a6cc7cdbe76f0a48aeb3cf9aacf63955b549fc4f
push id19800
push userbmo:bob.silverberg@gmail.com
push dateWed, 01 Jun 2016 00:22:59 +0000
reviewerskmag
bugs1276338
milestone49.0a1
Bug 1276338 - chrome.runtime.id is undefined in content scripts, r?kmag MozReview-Commit-ID: BXqB1rW9DzB
toolkit/components/extensions/ExtensionContent.jsm
toolkit/components/extensions/test/mochitest/mochitest.ini
toolkit/components/extensions/test/mochitest/test_ext_runtime_id.html
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -78,16 +78,20 @@ var api = context => {
           connectInfo = extensionId;
           extensionId = null;
         }
         let name = connectInfo && connectInfo.name || "";
         let recipient = extensionId ? {extensionId} : {extensionId: context.extensionId};
         return context.messenger.connect(context.messageManager, name, recipient);
       },
 
+      get id() {
+        return context.extensionId;
+      },
+
       get lastError() {
         return context.lastError;
       },
 
       getManifest: function() {
         return Cu.cloneInto(context.extension.manifest, context.cloneScope);
       },
 
--- a/toolkit/components/extensions/test/mochitest/mochitest.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest.ini
@@ -56,16 +56,17 @@ skip-if = buildapp == 'b2g' # runat != d
 [test_ext_permission_xhr.html]
 skip-if = buildapp == 'b2g' # JavaScript error: jar:remoteopenfile:///data/local/tmp/generated-extension.xpi!/content.js, line 46: NS_ERROR_ILLEGAL_VALUE:
 [test_ext_runtime_connect.html]
 skip-if = (os == 'android' || buildapp == 'b2g') # port.sender.tab is undefined on b2g. Bug 1258975 on android.
 [test_ext_runtime_connect2.html]
 skip-if = (os == 'android' || buildapp == 'b2g') # port.sender.tab is undefined on b2g. Bug 1258975 on android.
 [test_ext_runtime_disconnect.html]
 [test_ext_runtime_getPlatformInfo.html]
+[test_ext_runtime_id.html]
 [test_ext_runtime_sendMessage.html]
 [test_ext_sandbox_var.html]
 [test_ext_sendmessage_reply.html]
 skip-if = (os == 'android' || buildapp == 'b2g') # sender.tab is undefined on b2g. Bug 1258975 on android.
 [test_ext_sendmessage_reply2.html]
 skip-if = (os == 'android' || buildapp == 'b2g') # sender.tab is undefined on b2g. Bug 1258975 on android.
 [test_ext_sendmessage_doublereply.html]
 skip-if = (os == 'android' || buildapp == 'b2g') # sender.tab is undefined on b2g. Bug 1258975 on android.
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/mochitest/test_ext_runtime_id.html
@@ -0,0 +1,60 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test for browser.runtime.id</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";
+
+add_task(function* test_runtime_id() {
+  function background() {
+    browser.test.sendMessage("background-id", browser.runtime.id);
+  }
+
+  function content() {
+    browser.test.sendMessage("content-id", browser.runtime.id);
+  }
+
+  let uuidGenerator = SpecialPowers.Cc["@mozilla.org/uuid-generator;1"].getService(SpecialPowers.Ci.nsIUUIDGenerator);
+  let id = uuidGenerator.generateUUID().number;
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      "content_scripts": [{
+        "matches": ["http://mochi.test/*/file_sample.html"],
+        "run_at": "document_start",
+        "js": ["content_script.js"],
+      }],
+    },
+
+    background: `(${background})()`,
+
+    files: {
+      "content_script.js": `(${content})()`,
+    },
+  }, id);
+
+  yield extension.startup();
+
+  let backgroundId = yield extension.awaitMessage("background-id");
+  is(backgroundId, id, "runtime.id from background script is correct");
+  let win = window.open("file_sample.html");
+  let contentId = yield extension.awaitMessage("content-id");
+  is(contentId, id, "runtime.id from content script is correct");
+
+  win.close();
+  yield extension.unload();
+});
+
+</script>
+
+</body>
+</html>