2007-08-24 22:08:37 +04:00
|
|
|
/**
|
|
|
|
* Read count bytes from stream and return as a String object
|
|
|
|
*/
|
|
|
|
function read_stream(stream, count) {
|
|
|
|
/* assume stream has non-ASCII data */
|
|
|
|
var wrapper =
|
|
|
|
Components.classes["@mozilla.org/binaryinputstream;1"]
|
|
|
|
.createInstance(Components.interfaces.nsIBinaryInputStream);
|
|
|
|
wrapper.setInputStream(stream);
|
|
|
|
/* JS methods can be called with a maximum of 65535 arguments, and input
|
|
|
|
streams don't have to return all the data they make .available() when
|
|
|
|
asked to .read() that number of bytes. */
|
|
|
|
var data = [];
|
|
|
|
while (count > 0) {
|
|
|
|
var bytes = wrapper.readByteArray(Math.min(65535, count));
|
|
|
|
data.push(String.fromCharCode.apply(null, bytes));
|
|
|
|
count -= bytes.length;
|
|
|
|
if (bytes.length == 0)
|
|
|
|
do_throw("Nothing read from input stream!");
|
|
|
|
}
|
|
|
|
return data.join('');
|
|
|
|
}
|
|
|
|
|
2007-08-25 04:47:54 +04:00
|
|
|
const CL_EXPECT_FAILURE = 0x1;
|
2008-08-01 04:55:14 +04:00
|
|
|
const CL_EXPECT_GZIP = 0x2;
|
2010-08-11 07:07:09 +04:00
|
|
|
const CL_EXPECT_3S_DELAY = 0x4;
|
|
|
|
const CL_SUSPEND = 0x8;
|
2012-02-09 23:47:30 +04:00
|
|
|
const CL_ALLOW_UNKNOWN_CL = 0x10;
|
2012-03-23 03:39:31 +04:00
|
|
|
const CL_EXPECT_LATE_FAILURE = 0x20;
|
2010-08-11 07:07:09 +04:00
|
|
|
|
|
|
|
const SUSPEND_DELAY = 3000;
|
2007-08-25 04:47:54 +04:00
|
|
|
|
2007-08-24 22:08:37 +04:00
|
|
|
/**
|
|
|
|
* A stream listener that calls a callback function with a specified
|
|
|
|
* context and the received data when the channel is loaded.
|
|
|
|
*
|
|
|
|
* Signature of the closure:
|
|
|
|
* void closure(in nsIRequest request, in ACString data, in JSObject context);
|
|
|
|
*
|
|
|
|
* This listener makes sure that various parts of the channel API are
|
2007-08-25 04:47:54 +04:00
|
|
|
* implemented correctly and that the channel's status is a success code
|
2012-03-23 03:39:31 +04:00
|
|
|
* (you can pass CL_EXPECT_FAILURE or CL_EXPECT_LATE_FAILURE as flags
|
|
|
|
* to allow a failure code)
|
2007-08-24 22:08:37 +04:00
|
|
|
*
|
|
|
|
* Note that it also requires a valid content length on the channel and
|
|
|
|
* is thus not fully generic.
|
|
|
|
*/
|
2007-08-25 04:47:54 +04:00
|
|
|
function ChannelListener(closure, ctx, flags) {
|
2007-08-24 22:08:37 +04:00
|
|
|
this._closure = closure;
|
|
|
|
this._closurectx = ctx;
|
2007-08-25 04:47:54 +04:00
|
|
|
this._flags = flags;
|
2007-08-24 22:08:37 +04:00
|
|
|
}
|
|
|
|
ChannelListener.prototype = {
|
|
|
|
_closure: null,
|
|
|
|
_closurectx: null,
|
|
|
|
_buffer: "",
|
|
|
|
_got_onstartrequest: false,
|
|
|
|
_got_onstoprequest: false,
|
|
|
|
_contentLen: -1,
|
2010-08-11 07:07:09 +04:00
|
|
|
_lastEvent: 0,
|
2007-08-24 22:08:37 +04:00
|
|
|
|
|
|
|
QueryInterface: function(iid) {
|
2008-08-01 04:55:14 +04:00
|
|
|
if (iid.equals(Components.interfaces.nsIStreamListener) ||
|
|
|
|
iid.equals(Components.interfaces.nsIRequestObserver) ||
|
|
|
|
iid.equals(Components.interfaces.nsISupports))
|
2007-08-24 22:08:37 +04:00
|
|
|
return this;
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
onStartRequest: function(request, context) {
|
2009-11-15 07:54:52 +03:00
|
|
|
try {
|
|
|
|
if (this._got_onstartrequest)
|
|
|
|
do_throw("Got second onStartRequest event!");
|
|
|
|
this._got_onstartrequest = true;
|
2010-08-11 07:07:09 +04:00
|
|
|
this._lastEvent = Date.now();
|
2007-08-24 22:08:37 +04:00
|
|
|
|
2009-11-15 07:54:52 +03:00
|
|
|
request.QueryInterface(Components.interfaces.nsIChannel);
|
2010-07-28 22:33:06 +04:00
|
|
|
try {
|
|
|
|
this._contentLen = request.contentLength;
|
|
|
|
}
|
|
|
|
catch (ex) {
|
2012-02-09 23:47:30 +04:00
|
|
|
if (!(this._flags & (CL_EXPECT_FAILURE | CL_ALLOW_UNKNOWN_CL)))
|
2010-07-28 22:33:06 +04:00
|
|
|
do_throw("Could not get contentLength");
|
|
|
|
}
|
2012-02-09 23:47:30 +04:00
|
|
|
if (this._contentLen == -1 && !(this._flags & (CL_EXPECT_FAILURE | CL_ALLOW_UNKNOWN_CL)))
|
2009-11-15 07:54:52 +03:00
|
|
|
do_throw("Content length is unknown in onStartRequest!");
|
2010-08-11 07:07:09 +04:00
|
|
|
|
|
|
|
if (this._flags & CL_SUSPEND) {
|
|
|
|
request.suspend();
|
|
|
|
do_timeout(SUSPEND_DELAY, function() { request.resume(); });
|
|
|
|
}
|
|
|
|
|
2009-11-15 07:54:52 +03:00
|
|
|
} catch (ex) {
|
|
|
|
do_throw("Error in onStartRequest: " + ex);
|
|
|
|
}
|
2007-08-24 22:08:37 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
onDataAvailable: function(request, context, stream, offset, count) {
|
2009-11-15 07:54:52 +03:00
|
|
|
try {
|
2010-08-11 07:07:09 +04:00
|
|
|
let current = Date.now();
|
|
|
|
|
2009-11-15 07:54:52 +03:00
|
|
|
if (!this._got_onstartrequest)
|
|
|
|
do_throw("onDataAvailable without onStartRequest event!");
|
|
|
|
if (this._got_onstoprequest)
|
|
|
|
do_throw("onDataAvailable after onStopRequest event!");
|
|
|
|
if (!request.isPending())
|
|
|
|
do_throw("request reports itself as not pending from onDataAvailable!");
|
|
|
|
if (this._flags & CL_EXPECT_FAILURE)
|
|
|
|
do_throw("Got data despite expecting a failure");
|
2007-08-24 22:08:37 +04:00
|
|
|
|
2010-08-11 07:07:09 +04:00
|
|
|
if (current - this._lastEvent >= SUSPEND_DELAY &&
|
|
|
|
!(this._flags & CL_EXPECT_3S_DELAY))
|
|
|
|
do_throw("Data received after significant unexpected delay");
|
|
|
|
else if (current - this._lastEvent < SUSPEND_DELAY &&
|
|
|
|
this._flags & CL_EXPECT_3S_DELAY)
|
|
|
|
do_throw("Data received sooner than expected");
|
|
|
|
else if (current - this._lastEvent >= SUSPEND_DELAY &&
|
|
|
|
this._flags & CL_EXPECT_3S_DELAY)
|
|
|
|
this._flags &= ~CL_EXPECT_3S_DELAY; // No more delays expected
|
|
|
|
|
2009-11-15 07:54:52 +03:00
|
|
|
this._buffer = this._buffer.concat(read_stream(stream, count));
|
2010-08-11 07:07:09 +04:00
|
|
|
this._lastEvent = current;
|
2009-11-15 07:54:52 +03:00
|
|
|
} catch (ex) {
|
|
|
|
do_throw("Error in onDataAvailable: " + ex);
|
|
|
|
}
|
2007-08-24 22:08:37 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest: function(request, context, status) {
|
2009-11-15 07:54:52 +03:00
|
|
|
try {
|
2011-09-22 16:59:12 +04:00
|
|
|
var success = Components.isSuccessCode(status);
|
2012-06-06 10:59:05 +04:00
|
|
|
if (!this._got_onstartrequest)
|
2009-11-15 07:54:52 +03:00
|
|
|
do_throw("onStopRequest without onStartRequest event!");
|
|
|
|
if (this._got_onstoprequest)
|
|
|
|
do_throw("Got second onStopRequest event!");
|
|
|
|
this._got_onstoprequest = true;
|
2012-03-23 03:39:31 +04:00
|
|
|
if ((this._flags & (CL_EXPECT_FAILURE | CL_EXPECT_LATE_FAILURE)) && success)
|
2009-11-15 07:54:52 +03:00
|
|
|
do_throw("Should have failed to load URL (status is " + status.toString(16) + ")");
|
2012-03-23 03:39:31 +04:00
|
|
|
else if (!(this._flags & (CL_EXPECT_FAILURE | CL_EXPECT_LATE_FAILURE)) && !success)
|
2009-11-15 07:54:52 +03:00
|
|
|
do_throw("Failed to load URL: " + status.toString(16));
|
|
|
|
if (status != request.status)
|
|
|
|
do_throw("request.status does not match status arg to onStopRequest!");
|
|
|
|
if (request.isPending())
|
|
|
|
do_throw("request reports itself as pending from onStopRequest!");
|
2012-03-23 03:39:31 +04:00
|
|
|
if (!(this._flags & (CL_EXPECT_FAILURE | CL_EXPECT_LATE_FAILURE)) &&
|
2009-11-15 07:54:52 +03:00
|
|
|
!(this._flags & CL_EXPECT_GZIP) &&
|
|
|
|
this._contentLen != -1)
|
|
|
|
do_check_eq(this._buffer.length, this._contentLen)
|
|
|
|
} catch (ex) {
|
|
|
|
do_throw("Error in onStopRequest: " + ex);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this._closure(request, this._buffer, this._closurectx);
|
|
|
|
} catch (ex) {
|
|
|
|
do_throw("Error in closure function: " + ex);
|
|
|
|
}
|
2007-08-24 22:08:37 +04:00
|
|
|
}
|
|
|
|
};
|
2010-07-28 22:33:06 +04:00
|
|
|
|
|
|
|
var ES_ABORT_REDIRECT = 0x01;
|
|
|
|
|
|
|
|
function ChannelEventSink(flags)
|
|
|
|
{
|
|
|
|
this._flags = flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelEventSink.prototype = {
|
|
|
|
QueryInterface: function(iid) {
|
|
|
|
if (iid.equals(Ci.nsIInterfaceRequestor) ||
|
|
|
|
iid.equals(Ci.nsISupports))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
getInterface: function(iid) {
|
|
|
|
if (iid.equals(Ci.nsIChannelEventSink))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback) {
|
|
|
|
if (this._flags & ES_ABORT_REDIRECT)
|
2010-07-28 22:33:06 +04:00
|
|
|
throw Cr.NS_BINDING_ABORTED;
|
2010-08-05 06:15:55 +04:00
|
|
|
|
|
|
|
callback.onRedirectVerifyCallback(Cr.NS_OK);
|
2010-07-28 22:33:06 +04:00
|
|
|
}
|
|
|
|
};
|
2012-09-18 20:04:04 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that implements nsILoadContext. Use it as callbacks for channel when
|
|
|
|
* test needs it.
|
|
|
|
*/
|
|
|
|
function LoadContextCallback(appId, inBrowserElement, isPrivate, isContent) {
|
|
|
|
this.appId = appId;
|
|
|
|
this.isInBrowserElement = inBrowserElement;
|
|
|
|
this.usePrivateBrowsing = isPrivate;
|
|
|
|
this.isContent = isContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadContextCallback.prototype = {
|
|
|
|
associatedWindow: null,
|
|
|
|
topWindow : null,
|
|
|
|
isAppOfType: function(appType) {
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
},
|
|
|
|
QueryInterface: function(iid) {
|
|
|
|
if (iid == Ci.nsILoadContext ||
|
|
|
|
Ci.nsIInterfaceRequestor ||
|
|
|
|
Ci.nsISupports) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
getInterface: function(iid) {
|
|
|
|
if (iid.equals(Ci.nsILoadContext))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|