2012-05-10 22:58:48 +04:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<!--
|
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=682305
|
|
|
|
-->
|
|
|
|
<head>
|
|
|
|
<title>XMLHttpRequest send and channel implemented in JS</title>
|
2019-04-15 23:56:58 +03:00
|
|
|
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
2012-05-10 22:58:48 +04:00
|
|
|
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<a target="_blank"
|
|
|
|
href="https://bugzilla.mozilla.org/show_bug.cgi?id=682305">Mozilla Bug 682305</a>
|
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<pre id="test">
|
2017-02-23 00:10:07 +03:00
|
|
|
<script class="testbody" type="application/javascript">
|
2019-05-28 16:49:27 +03:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2012-05-10 22:58:48 +04:00
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register a custom nsIProtocolHandler service
|
2018-12-12 01:54:23 +03:00
|
|
|
* in order to be able to implement *and use* an
|
2012-05-10 22:58:48 +04:00
|
|
|
* nsIChannel component written in Javascript.
|
|
|
|
*/
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2018-12-12 17:56:54 +03:00
|
|
|
|
2015-09-18 19:27:52 +03:00
|
|
|
var contentSecManager = Cc["@mozilla.org/contentsecuritymanager;1"]
|
|
|
|
.getService(Ci.nsIContentSecurityManager);
|
2012-05-10 22:58:48 +04:00
|
|
|
|
|
|
|
var PROTOCOL_SCHEME = "jsproto";
|
|
|
|
|
|
|
|
|
2015-04-09 20:10:33 +03:00
|
|
|
function CustomChannel(uri, loadInfo) {
|
2018-12-12 17:56:54 +03:00
|
|
|
this.URI = this.originalURI = uri;
|
2015-04-09 20:10:33 +03:00
|
|
|
this.loadInfo = loadInfo;
|
2012-05-10 22:58:48 +04:00
|
|
|
}
|
|
|
|
CustomChannel.prototype = {
|
|
|
|
URI: null,
|
|
|
|
originalURI: null,
|
2015-04-09 20:10:33 +03:00
|
|
|
loadInfo: null,
|
2012-05-10 22:58:48 +04:00
|
|
|
contentCharset: "utf-8",
|
|
|
|
contentLength: 0,
|
|
|
|
contentType: "text/plain",
|
|
|
|
owner: Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal),
|
|
|
|
securityInfo: null,
|
|
|
|
notificationCallbacks: null,
|
|
|
|
loadFlags: 0,
|
|
|
|
loadGroup: null,
|
|
|
|
name: null,
|
|
|
|
status: Cr.NS_OK,
|
2019-02-12 19:08:25 +03:00
|
|
|
asyncOpen(listener) {
|
|
|
|
// throws an error if security checks fail
|
|
|
|
var outListener = contentSecManager.performSecurityCheck(this, listener);
|
2012-05-10 22:58:48 +04:00
|
|
|
let stream = this.open();
|
|
|
|
try {
|
2019-02-28 02:41:54 +03:00
|
|
|
outListener.onStartRequest(this);
|
2018-12-12 01:54:23 +03:00
|
|
|
} catch (e) {}
|
2012-05-10 22:58:48 +04:00
|
|
|
try {
|
2019-02-28 02:42:27 +03:00
|
|
|
outListener.onDataAvailable(this, stream, 0, stream.available());
|
2018-12-12 01:54:23 +03:00
|
|
|
} catch (e) {}
|
2012-05-10 22:58:48 +04:00
|
|
|
try {
|
2019-02-28 02:41:54 +03:00
|
|
|
outListener.onStopRequest(this, Cr.NS_OK);
|
2018-12-12 01:54:23 +03:00
|
|
|
} catch (e) {}
|
2012-05-10 22:58:48 +04:00
|
|
|
},
|
2018-12-12 01:54:23 +03:00
|
|
|
open() {
|
2019-02-12 19:08:25 +03:00
|
|
|
// throws an error if security checks fail
|
|
|
|
contentSecManager.performSecurityCheck(this, null);
|
|
|
|
|
2012-05-10 22:58:48 +04:00
|
|
|
let data = "bar";
|
|
|
|
let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
|
|
|
|
stream.setData(data, data.length);
|
|
|
|
return stream;
|
|
|
|
},
|
2018-12-12 01:54:23 +03:00
|
|
|
isPending() {
|
2012-05-10 22:58:48 +04:00
|
|
|
return false;
|
|
|
|
},
|
2018-12-12 01:54:23 +03:00
|
|
|
cancel() {
|
2020-05-05 20:41:36 +03:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
|
2012-05-10 22:58:48 +04:00
|
|
|
},
|
2018-12-12 01:54:23 +03:00
|
|
|
suspend() {
|
2020-05-05 20:41:36 +03:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
|
2012-05-10 22:58:48 +04:00
|
|
|
},
|
2018-12-12 01:54:23 +03:00
|
|
|
resume() {
|
2020-05-05 20:41:36 +03:00
|
|
|
throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
|
2012-05-10 22:58:48 +04:00
|
|
|
},
|
Bug 1649221: Update ChromeUtils.generateQI callers to pass strings. r=mccr8,remote-protocol-reviewers,marionette-reviewers,perftest-reviewers,webcompat-reviewers,geckoview-reviewers,preferences-reviewers,agi,whimboo,Bebe,twisniewski
Differential Revision: https://phabricator.services.mozilla.com/D81594
2020-07-11 02:58:28 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsIChannel", "nsIRequest"]),
|
2012-05-10 22:58:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function CustomProtocol() {}
|
|
|
|
CustomProtocol.prototype = {
|
|
|
|
get scheme() {
|
|
|
|
return PROTOCOL_SCHEME;
|
|
|
|
},
|
|
|
|
get protocolFlags() {
|
|
|
|
return (Ci.nsIProtocolHandler.URI_NORELATIVE |
|
|
|
|
Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE |
|
|
|
|
Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD);
|
|
|
|
},
|
|
|
|
get defaultPort() {
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
allowPort: function allowPort() {
|
|
|
|
return false;
|
|
|
|
},
|
2019-04-03 13:45:48 +03:00
|
|
|
newChannel: function newChannel(URI, loadInfo) {
|
2015-04-09 20:10:33 +03:00
|
|
|
return new CustomChannel(URI, loadInfo);
|
|
|
|
},
|
Bug 1649221: Update ChromeUtils.generateQI callers to pass strings. r=mccr8,remote-protocol-reviewers,marionette-reviewers,perftest-reviewers,webcompat-reviewers,geckoview-reviewers,preferences-reviewers,agi,whimboo,Bebe,twisniewski
Differential Revision: https://phabricator.services.mozilla.com/D81594
2020-07-11 02:58:28 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsISupportsWeakReference", "nsIProtocolHandler"]),
|
2012-05-10 22:58:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
var gFactory = {
|
2018-12-12 01:54:23 +03:00
|
|
|
register() {
|
2012-05-10 22:58:48 +04:00
|
|
|
var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
|
|
|
|
|
|
|
|
var classID = Components.ID("{ed064287-1e76-49ba-a28d-dc74394a8334}");
|
|
|
|
var description = PROTOCOL_SCHEME + ": protocol";
|
|
|
|
var contractID = "@mozilla.org/network/protocol;1?name=" + PROTOCOL_SCHEME;
|
|
|
|
var factory = XPCOMUtils._getFactory(CustomProtocol);
|
|
|
|
|
|
|
|
registrar.registerFactory(classID, description, contractID, factory);
|
|
|
|
|
|
|
|
this.unregister = function() {
|
|
|
|
registrar.unregisterFactory(classID, factory);
|
|
|
|
delete this.unregister;
|
|
|
|
};
|
2018-12-12 01:54:23 +03:00
|
|
|
},
|
2012-05-10 22:58:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Register the custom procotol handler
|
|
|
|
gFactory.register();
|
|
|
|
|
|
|
|
// Then, checks if XHR works with it
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", PROTOCOL_SCHEME + ":foo", true);
|
2018-12-12 01:54:23 +03:00
|
|
|
xhr.onload = function() {
|
2012-05-10 22:58:48 +04:00
|
|
|
is(xhr.responseText, "bar", "protocol doesn't work");
|
|
|
|
gFactory.unregister();
|
|
|
|
SimpleTest.finish();
|
2018-12-12 01:54:23 +03:00
|
|
|
};
|
2012-05-10 22:58:48 +04:00
|
|
|
try {
|
|
|
|
xhr.send(null);
|
2018-12-12 01:54:23 +03:00
|
|
|
} catch (e) {
|
2012-05-10 22:58:48 +04:00
|
|
|
ok(false, e);
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|