Bug 1199698 - [webext] Fix chrome.* from content scripts (r=gabor)
authorBill McCloskey <billm@mozilla.com>
Fri, 28 Aug 2015 15:31:15 -0700
changeset 260931 56c54e7c4006cf6cb6e68f681de01622028d4bc3
parent 260930 64f12a2877b5a3ccc1f8e616206b35a0e86670f6
child 260932 03e18dc3de5060a9f8428d0fe28b8e017f5c2285
push id64624
push userwmccloskey@mozilla.com
push dateFri, 04 Sep 2015 16:45:15 +0000
treeherdermozilla-inbound@3be72de9ac6a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgabor
bugs1199698
milestone43.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 1199698 - [webext] Fix chrome.* from content scripts (r=gabor)
toolkit/components/extensions/ExtensionContent.jsm
toolkit/components/extensions/test/extensions/content_script/background.js
toolkit/components/extensions/test/extensions/content_script/content_script.js
toolkit/components/extensions/test/extensions/content_script/manifest.json
toolkit/components/extensions/test/extensions/content_script/moz.build
toolkit/components/extensions/test/mochitest/test_extension_contentscript.html
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -218,17 +218,20 @@ function ExtensionContext(extensionId, c
   };
 
   let url = contentWindow.location.href;
   let broker = ExtensionContent.getBroker(mm);
   this.messenger = new Messenger(this, broker, {id: extensionId, frameId, url},
                                  {id: extensionId, frameId}, delegate);
 
   let chromeObj = Cu.createObjectIn(this.sandbox, {defineAs: "browser"});
-  this.sandbox.wrappedJSObject.chrome = this.sandbox.wrappedJSObject.browser;
+
+  // Sandboxes don't get Xrays for some weird compatibility
+  // reason. However, we waive here anyway in case that changes.
+  Cu.waiveXrays(this.sandbox).chrome = Cu.waiveXrays(this.sandbox).browser;
   injectAPI(api(this), chromeObj);
 
   this.onClose = new Set();
 }
 
 ExtensionContext.prototype = {
   get cloneScope() {
     return this.sandbox;
--- a/toolkit/components/extensions/test/extensions/content_script/background.js
+++ b/toolkit/components/extensions/test/extensions/content_script/background.js
@@ -1,5 +1,10 @@
 browser.runtime.onMessage.addListener(([msg, expectedState, readyState], sender) => {
+  if (msg == "chrome-namespace-ok") {
+    browser.test.sendMessage(msg);
+    return;
+  }
+
   browser.test.assertEq(msg, "script-run", "message type is correct");
   browser.test.assertEq(readyState, expectedState, "readyState is correct");
   browser.test.sendMessage("script-run-" + expectedState);
 });
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/extensions/content_script/content_script.js
@@ -0,0 +1,1 @@
+chrome.runtime.sendMessage(["chrome-namespace-ok"]);
--- a/toolkit/components/extensions/test/extensions/content_script/manifest.json
+++ b/toolkit/components/extensions/test/extensions/content_script/manifest.json
@@ -14,15 +14,20 @@
       "matches": ["http://mochi.test/tests/toolkit/components/extensions/test/mochitest/file_contentscript_*.html"],
       "js": ["content_script_end.js"],
       "run_at": "document_end"
     },
     {
       "matches": ["http://mochi.test/tests/toolkit/components/extensions/test/mochitest/file_contentscript_*.html"],
       "js": ["content_script_idle.js"],
       "run_at": "document_idle"
+    },
+    {
+      "matches": ["http://mochi.test/tests/toolkit/components/extensions/test/mochitest/file_contentscript_*.html"],
+      "js": ["content_script.js"],
+      "run_at": "document_idle"
     }
   ],
 
   "background": {
     "scripts": ["background.js"]
   }
 }
--- a/toolkit/components/extensions/test/extensions/content_script/moz.build
+++ b/toolkit/components/extensions/test/extensions/content_script/moz.build
@@ -1,13 +1,14 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 TESTING_JS_MODULES.extensions.content_script += [
     'background.js',
+    'content_script.js',
     'content_script_end.js',
     'content_script_idle.js',
     'content_script_start.js',
     'manifest.json',
 ]
--- a/toolkit/components/extensions/test/mochitest/test_extension_contentscript.html
+++ b/toolkit/components/extensions/test/mochitest/test_extension_contentscript.html
@@ -23,25 +23,27 @@ add_task(function* test_contentscript()
   let completeCount = 0;
   extension.onMessage("script-run-loading", () => { loadingCount++; });
   extension.onMessage("script-run-interactive", () => { interactiveCount++; });
 
   let completePromise = new Promise(resolve => {
     extension.onMessage("script-run-complete", () => { completeCount++; resolve(); });
   });
 
+  let chromeNamespacePromise = extension.awaitMessage("chrome-namespace-ok");
+
   yield new Promise(resolve => { setTimeout(resolve, 0); });
 
   let win = window.open();
 
   win.location = "file_contentscript_page1.html";
 
   ok(true, "page loaded");
 
-  yield Promise.all([waitForLoad(win), completePromise]);
+  yield Promise.all([waitForLoad(win), completePromise, chromeNamespacePromise]);
   info("test page loaded");
 
   win.close();
 
   is(loadingCount, 1, "document_start script ran exactly once");
   is(interactiveCount, 1, "document_end script ran exactly once");
   is(completeCount, 1, "document_idle script ran exactly once");