1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for protocol handlers</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script 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 type="text/javascript">
"use strict";
/* eslint-disable mozilla/balanced-listeners */
/* global addMessageListener, sendAsyncMessage */
function protocolChromeScript() {
addMessageListener("setup", () => {
let data = {};
const protoSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Ci.nsIExternalProtocolService);
let protoInfo = protoSvc.getProtocolHandlerInfo("ext+foo");
data.preferredAction = protoInfo.preferredAction === protoInfo.useHelperApp;
let handlers = protoInfo.possibleApplicationHandlers;
data.handlers = handlers.length;
let handler = handlers.queryElementAt(0, Ci.nsIHandlerApp);
data.isWebHandler = handler instanceof Ci.nsIWebHandlerApp;
data.uriTemplate = handler.uriTemplate;
// ext+ protocols should be set as default when there is only one
data.preferredApplicationHandler = protoInfo.preferredApplicationHandler == handler;
data.alwaysAskBeforeHandling = protoInfo.alwaysAskBeforeHandling;
const handlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"]
.getService(Ci.nsIHandlerService);
handlerSvc.store(protoInfo);
sendAsyncMessage("handlerData", data);
});
}
add_task(async function test_protocolHandler() {
await SpecialPowers.pushPrefEnv({set: [
["extensions.allowPrivateBrowsingByDefault", false],
]});
let extensionData = {
manifest: {
"protocol_handlers": [
{
"protocol": "ext+foo",
"name": "a foo protocol handler",
"uriTemplate": "foo.html?val=%s",
},
],
},
background() {
browser.test.onMessage.addListener(async (msg, arg) => {
if (msg == "open") {
let tab = await browser.tabs.create({url: arg});
browser.test.sendMessage("opened", tab.id);
} else if (msg == "close") {
await browser.tabs.remove(arg);
browser.test.sendMessage("closed");
}
});
browser.test.sendMessage("test-url", browser.runtime.getURL("foo.html"));
},
files: {
"foo.js": function() {
browser.test.sendMessage("test-query", location.search);
},
"foo.html": `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="foo.js"><\/script>
</head>
</html>`,
},
};
let pb_extension = ExtensionTestUtils.loadExtension({
background() {
browser.test.onMessage.addListener(async (msg, arg) => {
if (msg == "open") {
let tab = await browser.windows.create({url: arg, incognito: true});
browser.test.sendMessage("opened", tab.id);
} else if (msg == "close") {
await browser.windows.remove(arg);
browser.test.sendMessage("closed");
}
});
},
incognitoOverride: "spanning",
});
await pb_extension.startup();
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
let handlerUrl = await extension.awaitMessage("test-url");
// Ensure that the protocol handler is configured, and set it as default to
// bypass the dialog.
let chromeScript = SpecialPowers.loadChromeScript(protocolChromeScript);
let msg = chromeScript.promiseOneMessage("handlerData");
chromeScript.sendAsyncMessage("setup");
let data = await msg;
ok(data.preferredAction, "using a helper application is the preferred action");
ok(data.preferredApplicationHandler, "handler was set as default handler");
is(data.handlers, 1, "one handler is set");
ok(!data.alwaysAskBeforeHandling, "will not show dialog");
ok(data.isWebHandler, "the handler is a web handler");
is(data.uriTemplate, `${handlerUrl}?val=%s`, "correct url template");
chromeScript.destroy();
extension.sendMessage("open", "ext+foo:test");
let id = await extension.awaitMessage("opened");
let query = await extension.awaitMessage("test-query");
is(query, "?val=ext%2Bfoo%3Atest", "test query ok");
extension.sendMessage("close", id);
await extension.awaitMessage("closed");
// Test the protocol in a private window, watch for the
// console error.
consoleMonitor.start([{message: /NS_ERROR_FILE_NOT_FOUND/}]);
// Expect the chooser window to be open, close it.
chromeScript = SpecialPowers.loadChromeScript(async () => {
const {BrowserTestUtils} = ChromeUtils.import("resource://testing-common/BrowserTestUtils.jsm");
let window = await BrowserTestUtils.domWindowOpened(undefined, win => {
return BrowserTestUtils.waitForEvent(win, "load", false, event => {
let win = event.target.defaultView;
return win.document.documentElement.getAttribute("id") === "handling";
});
});
let entry = window.document.getElementById("items").firstChild;
sendAsyncMessage("handling", {name: entry.getAttribute("name"), disabled: entry.disabled});
window.close();
});
let testData = chromeScript.promiseOneMessage("handling");
pb_extension.sendMessage("open", "ext+foo:test");
id = await pb_extension.awaitMessage("opened");
await consoleMonitor.finished();
let entry = await testData;
is(entry.name, "a foo protocol handler", "entry is correct");
ok(entry.disabled, "handler is disabled");
pb_extension.sendMessage("close", id);
await pb_extension.awaitMessage("closed");
await pb_extension.unload();
// Shutdown the addon, then ensure the protocol was removed.
await extension.unload();
chromeScript = SpecialPowers.loadChromeScript(() => {
addMessageListener("setup", () => {
const protoSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Ci.nsIExternalProtocolService);
let protoInfo = protoSvc.getProtocolHandlerInfo("ext+foo");
sendAsyncMessage("preferredApplicationHandler", !protoInfo.preferredApplicationHandler);
let handlers = protoInfo.possibleApplicationHandlers;
sendAsyncMessage("handlerData", {
preferredApplicationHandler: !protoInfo.preferredApplicationHandler,
handlers: handlers.length,
});
});
});
msg = chromeScript.promiseOneMessage("handlerData");
chromeScript.sendAsyncMessage("setup");
data = await msg;
ok(data.preferredApplicationHandler, "no preferred handler is set");
is(data.handlers, 0, "no handler is set");
chromeScript.destroy();
});
add_task(async function test_protocolHandler_two() {
let extensionData = {
manifest: {
"protocol_handlers": [
{
"protocol": "ext+foo",
"name": "a foo protocol handler",
"uriTemplate": "foo.html?val=%s",
},
{
"protocol": "ext+foo",
"name": "another foo protocol handler",
"uriTemplate": "foo2.html?val=%s",
},
],
},
};
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
// Ensure that the protocol handler is configured, and set it as default,
// but because there are two handlers, the dialog is not bypassed. We
// don't test the actual dialog ui, it's been here forever and works based
// on the alwaysAskBeforeHandling value.
let chromeScript = SpecialPowers.loadChromeScript(protocolChromeScript);
let msg = chromeScript.promiseOneMessage("handlerData");
chromeScript.sendAsyncMessage("setup");
let data = await msg;
ok(data.preferredAction, "using a helper application is the preferred action");
ok(data.preferredApplicationHandler, "preferred handler is set");
is(data.handlers, 2, "two handlers are set");
ok(data.alwaysAskBeforeHandling, "will show dialog");
ok(data.isWebHandler, "the handler is a web handler");
chromeScript.destroy();
await extension.unload();
});
add_task(async function test_protocolHandler_https_target() {
let extensionData = {
manifest: {
"protocol_handlers": [
{
"protocol": "ext+foo",
"name": "http target",
"uriTemplate": "https://example.com/foo.html?val=%s",
},
],
},
};
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
ok(true, "https uriTemplate target works");
await extension.unload();
});
add_task(async function test_protocolHandler_http_target() {
let extensionData = {
manifest: {
"protocol_handlers": [
{
"protocol": "ext+foo",
"name": "http target",
"uriTemplate": "http://example.com/foo.html?val=%s",
},
],
},
};
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
ok(true, "http uriTemplate target works");
await extension.unload();
});
add_task(async function test_protocolHandler_restricted_protocol() {
let extensionData = {
manifest: {
"protocol_handlers": [
{
"protocol": "http",
"name": "take over the http protocol",
"uriTemplate": "http.html?val=%s",
},
],
},
};
consoleMonitor.start([{message: /processing protocol_handlers\.0\.protocol/}]);
let extension = ExtensionTestUtils.loadExtension(extensionData);
await Assert.rejects(extension.startup(),
/startup failed/,
"unable to register restricted handler protocol");
await consoleMonitor.finished();
});
add_task(async function test_protocolHandler_restricted_uriTemplate() {
let extensionData = {
manifest: {
"protocol_handlers": [
{
"protocol": "ext+foo",
"name": "take over the http protocol",
"uriTemplate": "ftp://example.com/file.txt",
},
],
},
};
consoleMonitor.start([{message: /processing protocol_handlers\.0\.uriTemplate/}]);
let extension = ExtensionTestUtils.loadExtension(extensionData);
await Assert.rejects(extension.startup(),
/startup failed/,
"unable to register restricted handler uriTemplate");
await consoleMonitor.finished();
});
add_task(async function test_protocolHandler_duplicate() {
let extensionData = {
manifest: {
"protocol_handlers": [
{
"protocol": "ext+foo",
"name": "foo protocol",
"uriTemplate": "foo.html?val=%s",
},
{
"protocol": "ext+foo",
"name": "foo protocol",
"uriTemplate": "foo.html?val=%s",
},
],
},
};
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
// Get the count of handlers installed.
let chromeScript = SpecialPowers.loadChromeScript(() => {
addMessageListener("setup", () => {
const protoSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Ci.nsIExternalProtocolService);
let protoInfo = protoSvc.getProtocolHandlerInfo("ext+foo");
let handlers = protoInfo.possibleApplicationHandlers;
sendAsyncMessage("handlerData", handlers.length);
});
});
let msg = chromeScript.promiseOneMessage("handlerData");
chromeScript.sendAsyncMessage("setup");
let data = await msg;
is(data, 1, "cannot re-register the same handler config");
chromeScript.destroy();
await extension.unload();
});
</script>
</body>
</html>
|