Bug 1390282 - Allow RemotePageManager to accept an array of URLs r=mossop

MozReview-Commit-ID: vTQmUDabHI

--HG--
extra : rebase_source : 8f95cfaf56b5d51318b05ab816ac6278e89713d6
This commit is contained in:
ahillier 2017-08-15 16:05:56 -04:00
Родитель fa2370b799
Коммит ca7e4abd3f
4 изменённых файлов: 78 добавлений и 13 удалений

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

@ -56,29 +56,35 @@ MessageListener.prototype = {
/**
* Creates a RemotePages object which listens for new remote pages of a
* particular URL. A "RemotePage:Init" message will be dispatched to this object
* for every page loaded. Message listeners added to this object receive
* messages from all loaded pages from the requested url.
* Creates a RemotePages object which listens for new remote pages of some
* particular URLs. A "RemotePage:Init" message will be dispatched to this
* object for every page loaded. Message listeners added to this object receive
* messages from all loaded pages from the requested urls.
*/
this.RemotePages = function(url) {
this.url = url;
this.RemotePages = function(urls) {
this.urls = Array.isArray(urls) ? urls : [urls];
this.messagePorts = new Set();
this.listener = new MessageListener();
this.destroyed = false;
RemotePageManager.addRemotePageListener(url, this.portCreated.bind(this));
this.portCreated = this.portCreated.bind(this);
this.portMessageReceived = this.portMessageReceived.bind(this);
for (const url of this.urls) {
RemotePageManager.addRemotePageListener(url, this.portCreated);
}
}
RemotePages.prototype = {
url: null,
urls: null,
messagePorts: null,
listener: null,
destroyed: null,
destroy() {
RemotePageManager.removeRemotePageListener(this.url);
for (const url of this.urls) {
RemotePageManager.removeRemotePageListener(url);
}
for (let port of this.messagePorts.values()) {
this.removeMessagePort(port);
@ -89,7 +95,7 @@ RemotePages.prototype = {
this.destroyed = true;
},
// Called when a page matching the url has loaded in a frame.
// Called when a page matching one of the urls has loaded in a frame.
portCreated(port) {
this.messagePorts.add(port);

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

@ -3,6 +3,7 @@ support-files =
dummy_page.html
metadata_*.html
testremotepagemanager.html
testremotepagemanager2.html
file_WebNavigation_page1.html
file_WebNavigation_page2.html
file_WebNavigation_page3.html

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

@ -36,7 +36,7 @@ function waitForPort(url, createTab = true) {
});
}
function waitForPage(pages) {
function waitForPage(pages, url = TEST_URL) {
return new Promise((resolve) => {
function listener({ target }) {
pages.removeMessageListener("RemotePage:Init", listener);
@ -45,7 +45,7 @@ function waitForPage(pages) {
}
pages.addMessageListener("RemotePage:Init", listener);
gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, TEST_URL);
gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, url);
});
}
@ -359,7 +359,7 @@ add_task(async function check_port_properties() {
});
// Test sending messages to all remote pages works
add_task(async function remote_pages_multiple() {
add_task(async function remote_pages_multiple_pages() {
let pages = new RemotePages(TEST_URL);
let port1 = await waitForPage(pages);
let port2 = await waitForPage(pages);
@ -393,6 +393,45 @@ add_task(async function remote_pages_multiple() {
gBrowser.removeTab(gBrowser.getTabForBrowser(port2.browser));
});
// Test that RemotePages with multiple urls works
add_task(async function remote_pages_multiple_urls() {
const TEST_URLS = [TEST_URL, TEST_URL.replace(".html", "2.html")];
const pages = new RemotePages(TEST_URLS);
const ports = [];
// Load two pages for each url
for (const [i, url] of TEST_URLS.entries()) {
const port = await waitForPage(pages, url);
is(port.browser, gBrowser.selectedBrowser, `port${i} is for the correct browser`);
ports.push(port);
ports.push(await waitForPage(pages, url));
}
let unloadPromise = waitForMessage(pages, "RemotePage:Unload", ports.pop());
gBrowser.removeCurrentTab();
await unloadPromise;
const pongPorts = new Set();
await new Promise(resolve => {
function listener({ name, target, data }) {
is(name, "Pong", "Should have seen the right response.");
is(data.str, "FAKE_DATA", "String should pass through");
is(data.counter, 1235, "Counter should be incremented");
pongPorts.add(target);
if (pongPorts.size === ports.length)
resolve();
}
pages.addMessageListener("Pong", listener);
pages.sendAsyncMessage("Ping", {str: "FAKE_DATA", counter: 1234});
});
ports.forEach(port => ok(pongPorts.has(port)));
pages.destroy();
ports.forEach(port => gBrowser.removeTab(gBrowser.getTabForBrowser(port.browser)));
});
// Test sending various types of data across the boundary
add_task(async function send_data() {
let port = await waitForPort(TEST_URL);

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

@ -0,0 +1,19 @@
<!DOCTYPE HTML>
<!-- A second page to test that RemotePages works with multiple urls -->
<html>
<head>
<script type="text/javascript">
/* global addMessageListener, sendAsyncMessage */
addMessageListener("Ping", function(message) {
sendAsyncMessage("Pong", {
str: message.data.str,
counter: message.data.counter + 1
});
});
</script>
</head>
<body>
</body>
</html>