Backed out changeset 4dd2466573ec (bug 1234677) for Mochitest test_chrome_ext_background_page.html failure

This commit is contained in:
Iris Hsiao 2016-07-20 12:54:00 +08:00
parent 98d7b727d9
commit d677544a02
15 changed files with 44 additions and 257 deletions

View File

@ -5,9 +5,9 @@
function genericChecker() {
let kind = "background";
let path = window.location.pathname;
if (path.includes("/popup.html")) {
if (path.indexOf("popup") != -1) {
kind = "popup";
} else if (path.includes("/page.html")) {
} else if (path.indexOf("page") != -1) {
kind = "page";
}
@ -34,7 +34,7 @@ function genericChecker() {
browser.tabs.query({
windowId: args[0],
}, tabs => {
let tab = tabs.find(tab => tab.url.includes("/page.html"));
let tab = tabs.find(tab => tab.url.indexOf("page.html") != -1);
browser.tabs.remove(tab.id, () => {
browser.test.sendMessage("closed");
});

View File

@ -33,13 +33,6 @@ interface nsIAddonPolicyService : nsISupports
*/
AString getAddonCSP(in AString aAddonId);
/**
* Returns the generated background page as a data-URI, if any. If the addon
* does not have an auto-generated background page, an empty string is
* returned.
*/
ACString getGeneratedBackgroundPageUrl(in ACString aAddonId);
/**
* Returns true if unprivileged code associated with the given addon may load
* data from |aURI|.

View File

@ -78,39 +78,6 @@ private:
NS_IMPL_ISUPPORTS(PipeCloser, nsIRequestObserver)
bool
ExtensionProtocolHandler::ResolveSpecialCases(const nsACString& aHost,
const nsACString& aPath,
const nsACString& aPathname,
nsACString& aResult)
{
// Create special moz-extension:-pages such as moz-extension://foo/_blank.html
// for all registered extensions. We can't just do this as a substitution
// because substitutions can only match on host.
if (!SubstitutingProtocolHandler::HasSubstitution(aHost)) {
return false;
}
if (aPathname.EqualsLiteral("/_blank.html")) {
aResult.AssignLiteral("about:blank");
return true;
}
if (aPathname.EqualsLiteral("/_generated_background_page.html")) {
nsCOMPtr<nsIAddonPolicyService> aps =
do_GetService("@mozilla.org/addons/policy-service;1");
if (!aps) {
return false;
}
nsresult rv = aps->GetGeneratedBackgroundPageUrl(aHost, aResult);
NS_ENSURE_SUCCESS(rv, false);
if (!aResult.IsEmpty()) {
MOZ_RELEASE_ASSERT(Substring(aResult, 0, 5).Equals("data:"));
return true;
}
}
return false;
}
nsresult
ExtensionProtocolHandler::SubstituteChannel(nsIURI* aURI,
nsILoadInfo* aLoadInfo,

View File

@ -28,10 +28,18 @@ public:
protected:
~ExtensionProtocolHandler() {}
bool ResolveSpecialCases(const nsACString& aHost,
const nsACString& aPath,
const nsACString& aPathname,
nsACString& aResult) override;
bool ResolveSpecialCases(const nsACString& aHost, const nsACString& aPath, nsACString& aResult) override
{
// Create a special about:blank-like moz-extension://foo/_blank.html for all
// registered extensions. We can't just do this as a substitution because
// substitutions can only match on host.
if (SubstitutingProtocolHandler::HasSubstitution(aHost) && aPath.EqualsLiteral("/_blank.html")) {
aResult.AssignLiteral("about:blank");
return true;
}
return false;
}
virtual nsresult SubstituteChannel(nsIURI* uri, nsILoadInfo* aLoadInfo, nsIChannel** result) override;
};

View File

@ -343,12 +343,6 @@ SubstitutingProtocolHandler::ResolveURI(nsIURI *uri, nsACString &result)
nsAutoCString host;
nsAutoCString path;
nsAutoCString pathname;
nsCOMPtr<nsIURL> url = do_QueryInterface(uri);
if (!url) {
return NS_ERROR_MALFORMED_URI;
}
rv = uri->GetAsciiHost(host);
if (NS_FAILED(rv)) return rv;
@ -356,10 +350,7 @@ SubstitutingProtocolHandler::ResolveURI(nsIURI *uri, nsACString &result)
rv = uri->GetPath(path);
if (NS_FAILED(rv)) return rv;
rv = url->GetFilePath(pathname);
if (NS_FAILED(rv)) return rv;
if (ResolveSpecialCases(host, path, pathname, result)) {
if (ResolveSpecialCases(host, path, result)) {
return NS_OK;
}
@ -368,8 +359,17 @@ SubstitutingProtocolHandler::ResolveURI(nsIURI *uri, nsACString &result)
if (NS_FAILED(rv)) return rv;
// Unescape the path so we can perform some checks on it.
NS_UnescapeURL(pathname);
if (pathname.FindChar('\\') != -1) {
nsCOMPtr<nsIURL> url = do_QueryInterface(uri);
if (!url) {
return NS_ERROR_MALFORMED_URI;
}
nsAutoCString unescapedPath;
rv = url->GetFilePath(unescapedPath);
if (NS_FAILED(rv)) return rv;
NS_UnescapeURL(unescapedPath);
if (unescapedPath.FindChar('\\') != -1) {
return NS_ERROR_MALFORMED_URI;
}

View File

@ -55,10 +55,7 @@ protected:
// Override this in the subclass to check for special case when resolving URIs
// _before_ checking substitutions.
virtual bool ResolveSpecialCases(const nsACString& aHost,
const nsACString& aPath,
const nsACString& aPathname,
nsACString& aResult)
virtual bool ResolveSpecialCases(const nsACString& aHost, const nsACString& aPath, nsACString& aResult)
{
return false;
}

View File

@ -66,7 +66,7 @@ nsResProtocolHandler::GetSubstitutionInternal(const nsACString& root, nsIURI **r
{
nsAutoCString uri;
if (!ResolveSpecialCases(root, NS_LITERAL_CSTRING("/"), NS_LITERAL_CSTRING("/"), uri)) {
if (!ResolveSpecialCases(root, NS_LITERAL_CSTRING("/"), uri)) {
return NS_ERROR_NOT_AVAILABLE;
}
@ -76,7 +76,6 @@ nsResProtocolHandler::GetSubstitutionInternal(const nsACString& root, nsIURI **r
bool
nsResProtocolHandler::ResolveSpecialCases(const nsACString& aHost,
const nsACString& aPath,
const nsACString& aPathname,
nsACString& aResult)
{
if (aHost.Equals("") || aHost.Equals(kAPP)) {

View File

@ -52,9 +52,7 @@ protected:
nsresult GetSubstitutionInternal(const nsACString& aRoot, nsIURI** aResult) override;
virtual ~nsResProtocolHandler() {}
bool ResolveSpecialCases(const nsACString& aHost,
const nsACString& aPath,
const nsACString& aPathname,
bool ResolveSpecialCases(const nsACString& aHost, const nsACString& aPath,
nsACString& aResult) override;
private:

View File

@ -161,7 +161,6 @@ var Service = {
this.aps.setAddonLoadURICallback(extension.id, this.checkAddonMayLoad.bind(this, extension));
this.aps.setAddonLocalizeCallback(extension.id, extension.localize.bind(extension));
this.aps.setAddonCSP(extension.id, extension.manifest.content_security_policy);
this.aps.setBackgroundPageUrlCallback(uuid, this.generateBackgroundPageUrl.bind(this, extension));
},
// Called when an extension is unloaded.
@ -171,7 +170,6 @@ var Service = {
this.aps.setAddonLoadURICallback(extension.id, null);
this.aps.setAddonLocalizeCallback(extension.id, null);
this.aps.setAddonCSP(extension.id, null);
this.aps.setBackgroundPageUrlCallback(uuid, null);
let handler = Services.io.getProtocolHandler("moz-extension");
handler.QueryInterface(Ci.nsISubstitutingProtocolHandler);
@ -202,21 +200,6 @@ var Service = {
return extension.whiteListedHosts.matchesIgnoringPath(uri);
},
generateBackgroundPageUrl(extension) {
let background_scripts = extension.manifest.background &&
extension.manifest.background.scripts;
if (!background_scripts) {
return;
}
let html = "<!DOCTYPE html>\n<body>\n";
for (let script of background_scripts) {
script = script.replace(/"/g, "&quot;");
html += `<script src="${script}"></script>\n`;
}
html += "</body>\n</html>\n";
return "data:text/html;charset=utf-8," + encodeURIComponent(html);
},
// Finds the add-on ID associated with a given moz-extension:// URI.
// This is used to set the addonId on the originAttributes for the
// nsIPrincipal attached to the URI.

View File

@ -12,8 +12,8 @@ var backgroundPagesMap = new WeakMap();
// Responsible for the background_page section of the manifest.
function BackgroundPage(options, extension) {
this.extension = extension;
this.scripts = options.scripts || [];
this.page = options.page || null;
this.isGenerated = !!options.scripts;
this.contentWindow = null;
this.chromeWebNav = null;
this.webNav = null;
@ -28,8 +28,9 @@ BackgroundPage.prototype = {
let url;
if (this.page) {
url = this.extension.baseURI.resolve(this.page);
} else if (this.isGenerated) {
url = this.extension.baseURI.resolve("_generated_background_page.html");
} else {
// TODO: Chrome uses "_generated_background_page.html" for this.
url = this.extension.baseURI.resolve("_blank.html");
}
if (!this.extension.isExtensionURL(url)) {
@ -102,6 +103,16 @@ BackgroundPage.prototype = {
}
event.currentTarget.removeEventListener("load", loadListener, true);
if (this.scripts) {
let doc = window.document;
for (let script of this.scripts) {
let tag = doc.createElement("script");
tag.setAttribute("src", script);
tag.async = false;
doc.body.appendChild(tag);
}
}
if (this.extension.onStartup) {
this.extension.onStartup();
}

View File

@ -85,9 +85,6 @@ skip-if = (os == 'android' || buildapp == 'b2g') # unimplemented api. Bug 125897
[test_ext_background_window_properties.html]
[test_ext_background_sub_windows.html]
[test_ext_background_api_injection.html]
[test_ext_background_generated_url.html]
[test_ext_background_generated_reload.html]
[test_ext_background_generated_load_events.html]
[test_ext_i18n.html]
skip-if = (os == 'android') # Bug 1258975 on android.
[test_ext_web_accessible_resources.html]

View File

@ -1,47 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test load events in _generated_background_page.html</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>
"use strict";
/* eslint-disable mozilla/balanced-listeners */
add_task(function* test_DOMContentLoaded_in_generated_background_page() {
function backgroundScript() {
function reportListener(event) {
browser.test.sendMessage("eventname", event.type);
}
document.addEventListener("DOMContentLoaded", reportListener);
window.addEventListener("load", reportListener);
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
background: {
scripts: ["bg.js"],
},
web_accessible_resources: ["_generated_background_page.html"],
},
files: {
"bg.js": `(${backgroundScript})();`,
},
});
yield extension.startup();
is("DOMContentLoaded", yield extension.awaitMessage("eventname"));
is("load", yield extension.awaitMessage("eventname"));
yield extension.unload();
});
</script>
</body>
</html>

View File

@ -1,50 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test reload of _generated_background_page.html</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>
"use strict";
add_task(function* test_reload_generated_background_page() {
function backgroundScript() {
if (location.hash !== "#firstrun") {
browser.test.sendMessage("first run");
location.hash = "#firstrun";
browser.test.assertEq("#firstrun", location.hash);
location.reload();
} else {
browser.test.notifyPass("second run");
}
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
background: {
scripts: ["bg.js"],
},
},
files: {
"bg.js": `(${backgroundScript})();`,
},
});
yield extension.startup();
info("Waiting for first message");
yield extension.awaitMessage("first run");
info("Waiting for second message");
yield extension.awaitFinish("second run");
info("Received both messages");
yield extension.unload();
});
</script>
</body>
</html>

View File

@ -1,47 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test _generated_background_page.html</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>
"use strict";
add_task(function* test_url_of_generated_background_page() {
function backgroundScript() {
const EXPECTED_URL = browser.runtime.getURL("/_generated_background_page.html");
browser.test.assertEq(EXPECTED_URL, location.href);
browser.test.sendMessage("script done", EXPECTED_URL);
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
background: {
scripts: ["bg.js"],
},
web_accessible_resources: ["_generated_background_page.html"],
},
files: {
"bg.js": `(${backgroundScript})();`,
},
});
yield extension.startup();
const EXPECTED_URL = yield extension.awaitMessage("script done");
let win = window.open(EXPECTED_URL);
ok(win, "Should open new tab at URL: " + EXPECTED_URL);
yield extension.awaitMessage("script done");
win.close();
yield extension.unload();
});
</script>
</body>
</html>

View File

@ -28,7 +28,6 @@ function AddonPolicyService()
{
this.wrappedJSObject = this;
this.cspStrings = new Map();
this.backgroundPageUrlCallbacks = new Map();
this.mayLoadURICallbacks = new Map();
this.localizeCallbacks = new Map();
@ -56,16 +55,6 @@ AddonPolicyService.prototype = {
return csp || this.defaultCSP;
},
/**
* Returns the generated background page as a data-URI, if any. If the addon
* does not have an auto-generated background page, an empty string is
* returned.
*/
getGeneratedBackgroundPageUrl(aAddonId) {
let cb = this.backgroundPageUrlCallbacks.get(aAddonId);
return cb && cb(aAddonId) || '';
},
/*
* Invokes a callback (if any) associated with the addon to determine whether
* unprivileged code running within the addon is allowed to perform loads from
@ -145,17 +134,6 @@ AddonPolicyService.prototype = {
}
},
/**
* Set the callback that generates a data-URL for the background page.
*/
setBackgroundPageUrlCallback(aAddonId, aCallback) {
if (aCallback) {
this.backgroundPageUrlCallbacks.set(aAddonId, aCallback);
} else {
this.backgroundPageUrlCallbacks.delete(aAddonId);
}
},
/*
* Sets the callbacks used by the stream converter service to localize
* add-on resources.