Bug 1587541 - Enable content scripts for OOP iframes in browser tabs r=robwu

Differential Revision: https://phabricator.services.mozilla.com/D77941
This commit is contained in:
Tomislav Jovanovic 2020-06-12 22:47:34 +00:00
Родитель e9ae922ddc
Коммит fb21526937
4 изменённых файлов: 94 добавлений и 1 удалений

Просмотреть файл

@ -460,7 +460,15 @@ void ExtensionPolicyService::CheckDocument(Document* aDocument) {
if (win) { if (win) {
nsIDocShell* docShell = win->GetDocShell(); nsIDocShell* docShell = win->GetDocShell();
RefPtr<ContentFrameMessageManager> mm = docShell->GetMessageManager(); RefPtr<ContentFrameMessageManager> mm = docShell->GetMessageManager();
if (!mm || !mMessageManagers.Contains(mm)) { nsString group = win->GetBrowsingContext()->Top()->GetMessageManagerGroup();
// Currently, we use frame scripts to select specific kinds of browsers
// where we want to run content scripts.
if ((!mm || !mMessageManagers.Contains(mm)) &&
// With Fission, OOP iframes don't have a frame message manager, so we
// use the browser's MessageManagerGroup attribute to decide if content
// scripts should run. The "browsers" group includes iframes from tabs.
!group.EqualsLiteral("browsers")) {
return; return;
} }

Просмотреть файл

@ -0,0 +1,6 @@
<!DOCTYPE HTML>
<meta charset="utf-8">
<img src="file_image_great.png"/>
Load a cross-origin iframe from example.net <p>
<iframe src="http://example.net/tests/toolkit/components/extensions/test/mochitest/file_sample.html"></iframe>

Просмотреть файл

@ -40,6 +40,7 @@ support-files =
file_webNavigation_manualSubframe_page1.html file_webNavigation_manualSubframe_page1.html
file_webNavigation_manualSubframe_page2.html file_webNavigation_manualSubframe_page2.html
file_with_about_blank.html file_with_about_blank.html
file_with_xorigin_frame.html
head.js head.js
head_cookies.js head_cookies.js
head_notifications.js head_notifications.js
@ -81,6 +82,7 @@ skip-if = (os == 'linux' && debug) || (toolkit == 'android' && debug) # bug 1348
[test_ext_contentscript_canvas.html] [test_ext_contentscript_canvas.html]
skip-if = (os == 'android') || (verify && debug && (os == 'linux')) # Bug 1617062 skip-if = (os == 'android') || (verify && debug && (os == 'linux')) # Bug 1617062
[test_ext_contentscript_devtools_metadata.html] [test_ext_contentscript_devtools_metadata.html]
[test_ext_contentscript_fission_frame.html]
[test_ext_contentscript_incognito.html] [test_ext_contentscript_incognito.html]
skip-if = os == 'android' # Android does not support multiple windows. skip-if = os == 'android' # Android does not support multiple windows.
[test_ext_contentscript_permission.html] [test_ext_contentscript_permission.html]

Просмотреть файл

@ -0,0 +1,77 @@
<!doctype html>
<head>
<title>Test content script in cross-origin frame</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script src="head.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<script>
"use strict";
add_task(async function test_content_script_cross_origin_frame() {
const extension = ExtensionTestUtils.loadExtension({
manifest: {
content_scripts: [{
matches: ["http://example.net/*/file_sample.html"],
all_frames: true,
js: ["cs.js"],
}],
},
background() {
browser.runtime.onConnect.addListener(port => {
port.onMessage.addListener(async num => {
let { tab, url, frameId } = port.sender;
browser.test.assertTrue(frameId > 0, "sender frameId is ok");
browser.test.assertTrue(url.endsWith("file_sample.html"), "url is ok");
let result = await browser.tabs.sendMessage(tab.id, num);
port.postMessage(result);
port.disconnect();
});
});
},
files: {
"cs.js"() {
let text = document.body.innerText;
browser.test.assertEq(text, "Sample text", "CS can access page DOM");
let manifest = browser.runtime.getManifest();
browser.test.assertEq(manifest.version, "1.0");
browser.test.assertEq(manifest.name, "Generated extension");
browser.runtime.onMessage.addListener(async num => {
browser.test.log("content script received tabs.sendMessage");
return num * 3;
})
let response;
let port = browser.runtime.connect();
port.onMessage.addListener(num => {
response = num;
});
port.onDisconnect.addListener(() => {
browser.test.assertEq(response, 21, "Got correct response");
browser.test.notifyPass();
});
port.postMessage(7);
},
},
});
await extension.startup();
let base = "http://example.org/tests/toolkit/components/extensions/test";
let win = window.open(`${base}/mochitest/file_with_xorigin_frame.html`);
await extension.awaitFinish();
win.close();
await extension.unload();
});
</script>