mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-11 13:48:23 +02:00
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8
This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:
ChromeUtils.import("resource://gre/modules/Services.jsm");
is approximately the same as the following, in the new model:
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs
This was done using the followng script:
https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8
Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16750
--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
202 lines
7.3 KiB
JavaScript
202 lines
7.3 KiB
JavaScript
/*
|
|
Any copyright is dedicated to the Public Domain.
|
|
http://creativecommons.org/publicdomain/zero/1.0/
|
|
*/
|
|
|
|
// This must be loaded in the remote process for this test to be useful
|
|
const TEST_URL = "http://example.com/browser/netwerk/test/browser/dummy.html";
|
|
|
|
const expectedRemote = gMultiProcessBrowser ? "true" : "";
|
|
|
|
const resProtocol = Cc["@mozilla.org/network/protocol;1?name=resource"]
|
|
.getService(Ci.nsIResProtocolHandler);
|
|
|
|
function frameScript() {
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
let resProtocol = Cc["@mozilla.org/network/protocol;1?name=resource"]
|
|
.getService(Ci.nsIResProtocolHandler);
|
|
|
|
addMessageListener("Test:ResolveURI", function({ data: uri }) {
|
|
uri = Services.io.newURI(uri);
|
|
try {
|
|
let resolved = resProtocol.resolveURI(uri);
|
|
sendAsyncMessage("Test:ResolvedURI", resolved);
|
|
}
|
|
catch (e) {
|
|
sendAsyncMessage("Test:ResolvedURI", null);
|
|
}
|
|
});
|
|
}
|
|
|
|
function waitForEvent(obj, name, capturing, chromeEvent) {
|
|
info("Waiting for " + name);
|
|
return new Promise((resolve) => {
|
|
function listener(event) {
|
|
info("Saw " + name);
|
|
obj.removeEventListener(name, listener, capturing, chromeEvent);
|
|
resolve(event);
|
|
}
|
|
|
|
obj.addEventListener(name, listener, capturing, chromeEvent);
|
|
});
|
|
}
|
|
|
|
function resolveURI(uri) {
|
|
uri = Services.io.newURI(uri);
|
|
try {
|
|
return resProtocol.resolveURI(uri);
|
|
}
|
|
catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function remoteResolveURI(uri) {
|
|
return new Promise((resolve) => {
|
|
let manager = gBrowser.selectedBrowser.messageManager;
|
|
|
|
function listener({ data: resolved }) {
|
|
manager.removeMessageListener("Test:ResolvedURI", listener);
|
|
resolve(resolved);
|
|
}
|
|
|
|
manager.addMessageListener("Test:ResolvedURI", listener);
|
|
manager.sendAsyncMessage("Test:ResolveURI", uri);
|
|
});
|
|
}
|
|
|
|
var loadTestTab = async function() {
|
|
gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, TEST_URL);
|
|
let browser = gBrowser.selectedBrowser;
|
|
await BrowserTestUtils.browserLoaded(browser);
|
|
browser.messageManager.loadFrameScript("data:,(" + frameScript.toString() + ")();", true);
|
|
return browser;
|
|
};
|
|
|
|
// Restarts the child process by crashing it then reloading the tab
|
|
var restart = async function() {
|
|
let browser = gBrowser.selectedBrowser;
|
|
// If the tab isn't remote this would crash the main process so skip it
|
|
if (browser.getAttribute("remote") != "true")
|
|
return browser;
|
|
|
|
await BrowserTestUtils.crashBrowser(browser);
|
|
|
|
browser.reload();
|
|
|
|
await BrowserTestUtils.browserLoaded(browser);
|
|
is(browser.getAttribute("remote"), expectedRemote, "Browser should be in the right process");
|
|
browser.messageManager.loadFrameScript("data:,(" + frameScript.toString() + ")();", true);
|
|
return browser;
|
|
};
|
|
|
|
// Sanity check that this test is going to be useful
|
|
add_task(async function() {
|
|
let browser = await loadTestTab();
|
|
|
|
// This must be loaded in the remote process for this test to be useful
|
|
is(browser.getAttribute("remote"), expectedRemote, "Browser should be in the right process");
|
|
|
|
let local = resolveURI("resource://gre/modules/Services.jsm");
|
|
let remote = await remoteResolveURI("resource://gre/modules/Services.jsm");
|
|
is(local, remote, "Services.jsm should resolve in both processes");
|
|
|
|
gBrowser.removeCurrentTab();
|
|
});
|
|
|
|
// Add a mapping, update it then remove it
|
|
add_task(async function() {
|
|
let browser = await loadTestTab();
|
|
|
|
info("Set");
|
|
resProtocol.setSubstitution("testing", Services.io.newURI("chrome://global/content"));
|
|
let local = resolveURI("resource://testing/test.js");
|
|
let remote = await remoteResolveURI("resource://testing/test.js");
|
|
is(local, "chrome://global/content/test.js", "Should resolve in main process");
|
|
is(remote, "chrome://global/content/test.js", "Should resolve in child process");
|
|
|
|
info("Change");
|
|
resProtocol.setSubstitution("testing", Services.io.newURI("chrome://global/skin"));
|
|
local = resolveURI("resource://testing/test.js");
|
|
remote = await remoteResolveURI("resource://testing/test.js");
|
|
is(local, "chrome://global/skin/test.js", "Should resolve in main process");
|
|
is(remote, "chrome://global/skin/test.js", "Should resolve in child process");
|
|
|
|
info("Clear");
|
|
resProtocol.setSubstitution("testing", null);
|
|
local = resolveURI("resource://testing/test.js");
|
|
remote = await remoteResolveURI("resource://testing/test.js");
|
|
is(local, null, "Shouldn't resolve in main process");
|
|
is(remote, null, "Shouldn't resolve in child process");
|
|
|
|
gBrowser.removeCurrentTab();
|
|
});
|
|
|
|
// Add a mapping, restart the child process then check it is still there
|
|
add_task(async function() {
|
|
let browser = await loadTestTab();
|
|
|
|
info("Set");
|
|
resProtocol.setSubstitution("testing", Services.io.newURI("chrome://global/content"));
|
|
let local = resolveURI("resource://testing/test.js");
|
|
let remote = await remoteResolveURI("resource://testing/test.js");
|
|
is(local, "chrome://global/content/test.js", "Should resolve in main process");
|
|
is(remote, "chrome://global/content/test.js", "Should resolve in child process");
|
|
|
|
await restart();
|
|
|
|
local = resolveURI("resource://testing/test.js");
|
|
remote = await remoteResolveURI("resource://testing/test.js");
|
|
is(local, "chrome://global/content/test.js", "Should resolve in main process");
|
|
is(remote, "chrome://global/content/test.js", "Should resolve in child process");
|
|
|
|
info("Change");
|
|
resProtocol.setSubstitution("testing", Services.io.newURI("chrome://global/skin"));
|
|
|
|
await restart();
|
|
|
|
local = resolveURI("resource://testing/test.js");
|
|
remote = await remoteResolveURI("resource://testing/test.js");
|
|
is(local, "chrome://global/skin/test.js", "Should resolve in main process");
|
|
is(remote, "chrome://global/skin/test.js", "Should resolve in child process");
|
|
|
|
info("Clear");
|
|
resProtocol.setSubstitution("testing", null);
|
|
|
|
await restart();
|
|
|
|
local = resolveURI("resource://testing/test.js");
|
|
remote = await remoteResolveURI("resource://testing/test.js");
|
|
is(local, null, "Shouldn't resolve in main process");
|
|
is(remote, null, "Shouldn't resolve in child process");
|
|
|
|
gBrowser.removeCurrentTab();
|
|
});
|
|
|
|
// Adding a mapping to a resource URI should work
|
|
add_task(async function() {
|
|
let browser = await loadTestTab();
|
|
|
|
info("Set");
|
|
resProtocol.setSubstitution("testing", Services.io.newURI("chrome://global/content"));
|
|
resProtocol.setSubstitution("testing2", Services.io.newURI("resource://testing"));
|
|
let local = resolveURI("resource://testing2/test.js");
|
|
let remote = await remoteResolveURI("resource://testing2/test.js");
|
|
is(local, "chrome://global/content/test.js", "Should resolve in main process");
|
|
is(remote, "chrome://global/content/test.js", "Should resolve in child process");
|
|
|
|
info("Clear");
|
|
resProtocol.setSubstitution("testing", null);
|
|
local = resolveURI("resource://testing2/test.js");
|
|
remote = await remoteResolveURI("resource://testing2/test.js");
|
|
is(local, "chrome://global/content/test.js", "Should resolve in main process");
|
|
is(remote, "chrome://global/content/test.js", "Should resolve in child process");
|
|
|
|
resProtocol.setSubstitution("testing2", null);
|
|
local = resolveURI("resource://testing2/test.js");
|
|
remote = await remoteResolveURI("resource://testing2/test.js");
|
|
is(local, null, "Shouldn't resolve in main process");
|
|
is(remote, null, "Shouldn't resolve in child process");
|
|
|
|
gBrowser.removeCurrentTab();
|
|
});
|