content/base/test/chrome/test_bug682305.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=682305
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>XMLHttpRequest send and channel implemented in JS</title>
michael@0 8 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
michael@0 10 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script>
michael@0 11 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
michael@0 12 </head>
michael@0 13 <body>
michael@0 14 <a target="_blank"
michael@0 15 href="https://bugzilla.mozilla.org/show_bug.cgi?id=682305">Mozilla Bug 682305</a>
michael@0 16 <p id="display"></p>
michael@0 17 <div id="content" style="display: none">
michael@0 18
michael@0 19 </div>
michael@0 20 <pre id="test">
michael@0 21 <script class="testbody" type="application/javascript;version=1.8">
michael@0 22 SimpleTest.waitForExplicitFinish();
michael@0 23
michael@0 24 /*
michael@0 25 * Register a custom nsIProtocolHandler service
michael@0 26 * in order to be able to implement *and use* an
michael@0 27 * nsIChannel component written in Javascript.
michael@0 28 */
michael@0 29
michael@0 30 var Cc = Components.classes;
michael@0 31 var Ci = Components.interfaces;
michael@0 32 var Cr = Components.results;
michael@0 33 var Cu = Components.utils;
michael@0 34
michael@0 35 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 36 var SimpleURI = Cc["@mozilla.org/network/simple-uri;1"];
michael@0 37 var ios = Cc["@mozilla.org/network/io-service;1"]
michael@0 38 .getService(Ci.nsIIOService);
michael@0 39
michael@0 40 var PROTOCOL_SCHEME = "jsproto";
michael@0 41
michael@0 42
michael@0 43 function CustomChannel(uri) {
michael@0 44 this.URI = this.originalURI = uri;
michael@0 45 }
michael@0 46 CustomChannel.prototype = {
michael@0 47 URI: null,
michael@0 48 originalURI: null,
michael@0 49 contentCharset: "utf-8",
michael@0 50 contentLength: 0,
michael@0 51 contentType: "text/plain",
michael@0 52 owner: Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal),
michael@0 53 securityInfo: null,
michael@0 54 notificationCallbacks: null,
michael@0 55 loadFlags: 0,
michael@0 56 loadGroup: null,
michael@0 57 name: null,
michael@0 58 status: Cr.NS_OK,
michael@0 59 asyncOpen: function(listener, context) {
michael@0 60 let stream = this.open();
michael@0 61 try {
michael@0 62 listener.onStartRequest(this, context);
michael@0 63 } catch(e) {}
michael@0 64 try {
michael@0 65 listener.onDataAvailable(this, context, stream, 0, stream.available());
michael@0 66 } catch(e) {}
michael@0 67 try {
michael@0 68 listener.onStopRequest(this, context, Cr.NS_OK);
michael@0 69 } catch(e) {}
michael@0 70 },
michael@0 71 open: function() {
michael@0 72 let data = "bar";
michael@0 73 let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
michael@0 74 stream.setData(data, data.length);
michael@0 75 return stream;
michael@0 76 },
michael@0 77 isPending: function() {
michael@0 78 return false;
michael@0 79 },
michael@0 80 cancel: function() {
michael@0 81 throw Cr.NS_ERROR_NOT_IMPLEMENTED;
michael@0 82 },
michael@0 83 suspend: function() {
michael@0 84 throw Cr.NS_ERROR_NOT_IMPLEMENTED;
michael@0 85 },
michael@0 86 resume: function() {
michael@0 87 throw Cr.NS_ERROR_NOT_IMPLEMENTED;
michael@0 88 },
michael@0 89 QueryInterface: XPCOMUtils.generateQI([Ci.nsIChannel, Ci.nsIRequest])
michael@0 90 };
michael@0 91
michael@0 92
michael@0 93 function CustomProtocol() {}
michael@0 94 CustomProtocol.prototype = {
michael@0 95 get scheme() {
michael@0 96 return PROTOCOL_SCHEME;
michael@0 97 },
michael@0 98 get protocolFlags() {
michael@0 99 return (Ci.nsIProtocolHandler.URI_NORELATIVE |
michael@0 100 Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE |
michael@0 101 Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD);
michael@0 102 },
michael@0 103 get defaultPort() {
michael@0 104 return -1;
michael@0 105 },
michael@0 106 allowPort: function allowPort() {
michael@0 107 return false;
michael@0 108 },
michael@0 109 newURI: function newURI(spec, charset, baseURI) {
michael@0 110 var uri = SimpleURI.createInstance(Ci.nsIURI)
michael@0 111 uri.spec = spec;
michael@0 112 return uri.QueryInterface(Ci.nsIURI);
michael@0 113 },
michael@0 114 newChannel: function newChannel(URI) {
michael@0 115 return new CustomChannel(URI);
michael@0 116 },
michael@0 117 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
michael@0 118 Ci.nsISupportsWeakReference,
michael@0 119 Ci.nsIProtocolHandler])
michael@0 120 };
michael@0 121
michael@0 122 var gFactory = {
michael@0 123 register: function() {
michael@0 124 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 125
michael@0 126 var classID = Components.ID("{ed064287-1e76-49ba-a28d-dc74394a8334}");
michael@0 127 var description = PROTOCOL_SCHEME + ": protocol";
michael@0 128 var contractID = "@mozilla.org/network/protocol;1?name=" + PROTOCOL_SCHEME;
michael@0 129 var factory = XPCOMUtils._getFactory(CustomProtocol);
michael@0 130
michael@0 131 registrar.registerFactory(classID, description, contractID, factory);
michael@0 132
michael@0 133 this.unregister = function() {
michael@0 134 registrar.unregisterFactory(classID, factory);
michael@0 135 delete this.unregister;
michael@0 136 };
michael@0 137 }
michael@0 138 };
michael@0 139
michael@0 140 // Register the custom procotol handler
michael@0 141 gFactory.register();
michael@0 142
michael@0 143 // Then, checks if XHR works with it
michael@0 144 var xhr = new XMLHttpRequest();
michael@0 145 xhr.open("GET", PROTOCOL_SCHEME + ":foo", true);
michael@0 146 xhr.onload = function () {
michael@0 147 is(xhr.responseText, "bar", "protocol doesn't work");
michael@0 148 gFactory.unregister();
michael@0 149 SimpleTest.finish();
michael@0 150 }
michael@0 151 try {
michael@0 152 xhr.send(null);
michael@0 153 } catch(e) {
michael@0 154 ok(false, e);
michael@0 155 }
michael@0 156 </script>
michael@0 157 </pre>
michael@0 158 </body>
michael@0 159 </html>

mercurial