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
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test for protocol handlers</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 type="text/javascript">
"use strict";
/* eslint-disable mozilla/balanced-listeners */
/* global addMessageListener, sendAsyncMessage */
add_task(async function test_protocolHandler() {
let extensionData = {
manifest: {
"protocol_handlers": [
{
"protocol": "ext+foo",
"name": "a foo protocol handler",
"uriTemplate": "foo.html?val=%s",
},
],
},
background() {
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 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(() => {
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;
data.preferredApplicationHandler = !protoInfo.preferredApplicationHandler;
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;
protoInfo.preferredApplicationHandler = handler;
protoInfo.alwaysAskBeforeHandling = false;
const handlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"]
.getService(Ci.nsIHandlerService);
handlerSvc.store(protoInfo);
sendAsyncMessage("handlerData", data);
});
});
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, "no preferred handler is set");
is(data.handlers, 1, "one handler is set");
ok(data.isWebHandler, "the handler is a web handler");
is(data.uriTemplate, `${handlerUrl}?val=%s`, "correct url template");
chromeScript.destroy();
let win = window.open("ext+foo:test");
let query = await extension.awaitMessage("test-query");
is(query, "?val=ext%2Bfoo%3Atest", "test query ok");
win.close();
// 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_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(), "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(), "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>
|