content/base/test/chrome/test_bug682305.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/chrome/test_bug682305.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,159 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=682305
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>XMLHttpRequest send and channel implemented in JS</title>
    1.11 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <script type="application/javascript"  src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
    1.13 +  <script type="application/javascript"  src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script>
    1.14 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
    1.15 +</head>
    1.16 +<body>
    1.17 +<a target="_blank"
    1.18 +   href="https://bugzilla.mozilla.org/show_bug.cgi?id=682305">Mozilla Bug 682305</a>
    1.19 +<p id="display"></p>
    1.20 +<div id="content" style="display: none">
    1.21 +
    1.22 +</div>
    1.23 +<pre id="test">
    1.24 +<script class="testbody" type="application/javascript;version=1.8">
    1.25 +SimpleTest.waitForExplicitFinish();
    1.26 +
    1.27 +/*
    1.28 + * Register a custom nsIProtocolHandler service
    1.29 + * in order to be able to implement *and use* an 
    1.30 + * nsIChannel component written in Javascript.
    1.31 + */
    1.32 +
    1.33 +var Cc = Components.classes;
    1.34 +var Ci = Components.interfaces;
    1.35 +var Cr = Components.results;
    1.36 +var Cu = Components.utils;
    1.37 +
    1.38 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.39 +var SimpleURI = Cc["@mozilla.org/network/simple-uri;1"];
    1.40 +var ios = Cc["@mozilla.org/network/io-service;1"]
    1.41 +            .getService(Ci.nsIIOService);
    1.42 +
    1.43 +var PROTOCOL_SCHEME = "jsproto";
    1.44 +
    1.45 +
    1.46 +function CustomChannel(uri) {
    1.47 +	this.URI = this.originalURI = uri;
    1.48 +}
    1.49 +CustomChannel.prototype = {
    1.50 +  URI: null,
    1.51 +  originalURI: null,
    1.52 +  contentCharset: "utf-8",
    1.53 +  contentLength: 0,
    1.54 +  contentType: "text/plain",
    1.55 +  owner: Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal),
    1.56 +  securityInfo: null,
    1.57 +  notificationCallbacks: null,
    1.58 +  loadFlags: 0,
    1.59 +  loadGroup: null,
    1.60 +  name: null,
    1.61 +  status: Cr.NS_OK,
    1.62 +  asyncOpen: function(listener, context) {
    1.63 +    let stream = this.open();
    1.64 +    try {
    1.65 +      listener.onStartRequest(this, context);
    1.66 +    } catch(e) {}
    1.67 +    try {
    1.68 +      listener.onDataAvailable(this, context, stream, 0, stream.available());
    1.69 +    } catch(e) {}
    1.70 +    try {
    1.71 +      listener.onStopRequest(this, context, Cr.NS_OK);
    1.72 +    } catch(e) {}
    1.73 +  },
    1.74 +  open: function() {
    1.75 +    let data = "bar";
    1.76 +    let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
    1.77 +    stream.setData(data, data.length);
    1.78 +    return stream;
    1.79 +  },
    1.80 +  isPending: function() {
    1.81 +    return false;
    1.82 +  },
    1.83 +  cancel: function() {
    1.84 +    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
    1.85 +  },
    1.86 +  suspend: function() {
    1.87 +    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
    1.88 +  },
    1.89 +  resume: function() {
    1.90 +    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
    1.91 +  },
    1.92 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIChannel, Ci.nsIRequest])
    1.93 +};
    1.94 +
    1.95 +
    1.96 +function CustomProtocol() {}
    1.97 +CustomProtocol.prototype = {
    1.98 +  get scheme() {
    1.99 +    return PROTOCOL_SCHEME;
   1.100 +  },
   1.101 +  get protocolFlags() {
   1.102 +    return (Ci.nsIProtocolHandler.URI_NORELATIVE |
   1.103 +            Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE |
   1.104 +            Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD);
   1.105 +  },
   1.106 +  get defaultPort() {
   1.107 +    return -1;
   1.108 +  },
   1.109 +  allowPort: function allowPort() {
   1.110 +    return false;
   1.111 +  },
   1.112 +  newURI: function newURI(spec, charset, baseURI) {
   1.113 +    var uri = SimpleURI.createInstance(Ci.nsIURI)
   1.114 +    uri.spec = spec;
   1.115 +    return uri.QueryInterface(Ci.nsIURI);
   1.116 +  },
   1.117 +  newChannel: function newChannel(URI) {    
   1.118 +    return new CustomChannel(URI);
   1.119 +  },
   1.120 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
   1.121 +                                         Ci.nsISupportsWeakReference,
   1.122 +                                         Ci.nsIProtocolHandler])
   1.123 +};
   1.124 +
   1.125 +var gFactory = {
   1.126 +  register: function() {
   1.127 +    var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
   1.128 +
   1.129 +    var classID = Components.ID("{ed064287-1e76-49ba-a28d-dc74394a8334}");
   1.130 +    var description = PROTOCOL_SCHEME + ": protocol";
   1.131 +    var contractID = "@mozilla.org/network/protocol;1?name=" + PROTOCOL_SCHEME;
   1.132 +    var factory = XPCOMUtils._getFactory(CustomProtocol);
   1.133 +
   1.134 +    registrar.registerFactory(classID, description, contractID, factory);
   1.135 +
   1.136 +    this.unregister = function() {
   1.137 +      registrar.unregisterFactory(classID, factory);
   1.138 +      delete this.unregister;
   1.139 +    };
   1.140 +  }
   1.141 +};
   1.142 +
   1.143 +// Register the custom procotol handler
   1.144 +gFactory.register();
   1.145 +
   1.146 +// Then, checks if XHR works with it
   1.147 +var xhr = new XMLHttpRequest();
   1.148 +xhr.open("GET", PROTOCOL_SCHEME + ":foo", true);
   1.149 +xhr.onload = function () {
   1.150 +  is(xhr.responseText, "bar", "protocol doesn't work");
   1.151 +  gFactory.unregister();
   1.152 +  SimpleTest.finish();
   1.153 +}
   1.154 +try {
   1.155 +  xhr.send(null);
   1.156 +} catch(e) {
   1.157 +  ok(false, e);
   1.158 +}
   1.159 +</script>
   1.160 +</pre>
   1.161 +</body>
   1.162 +</html>

mercurial