Bug 632227 - NetUtil should have a helper method to read an input stream asynchronously

r=bz
sr=biesi
This commit is contained in:
Shawn Wilsher 2011-03-31 10:24:42 -07:00
Родитель c6cb5388b6
Коммит 7d081ee3fa
2 изменённых файлов: 39 добавлений и 7 удалений

Просмотреть файл

@ -130,21 +130,22 @@ const NetUtil = {
/** /**
* Asynchronously opens a source and fetches the response. A source can be * Asynchronously opens a source and fetches the response. A source can be
* an nsIURI, nsIFile, string spec, or nsIChannel. The provided callback * an nsIURI, nsIFile, string spec, nsIChannel, or nsIInputStream. The
* will get an input stream containing the response, the result code, and a * provided callback will get an input stream containing the response, the
* reference to the request. * result code, and a reference to the request.
* *
* @param aSource * @param aSource
* The nsIURI, nsIFile, string spec, or nsIChannel to open. * The nsIURI, nsIFile, string spec, nsIChannel, or nsIInputStream
* to open.
* Note: If passing an nsIChannel whose notificationCallbacks is * Note: If passing an nsIChannel whose notificationCallbacks is
* already set, callers are responsible for implementations * already set, callers are responsible for implementations
* of nsIBadCertListener/nsISSLErrorListener. * of nsIBadCertListener/nsISSLErrorListener.
* @param aCallback * @param aCallback
* The callback function that will be notified upon completion. It * The callback function that will be notified upon completion. It
* will get two arguments: * will get two arguments:
* 1) An nsIInputStream containing the data from the channel, if any. * 1) An nsIInputStream containing the data from aSource, if any.
* 2) The status code from opening the source. * 2) The status code from opening the source.
* 3) Reference to the channel (as an nsIRequest). * 3) Reference to the nsIRequest.
*/ */
asyncFetch: function NetUtil_asyncOpen(aSource, aCallback) asyncFetch: function NetUtil_asyncOpen(aSource, aCallback)
{ {
@ -174,6 +175,15 @@ const NetUtil = {
} }
}); });
// Input streams are handled slightly differently from everything else.
if (aSource instanceof Ci.nsIInputStream) {
let pump = Cc["@mozilla.org/network/input-stream-pump;1"].
createInstance(Ci.nsIInputStreamPump);
pump.init(aSource, -1, -1, 0, 0, true);
pump.asyncRead(listener, null);
return;
}
let channel = aSource; let channel = aSource;
if (!(channel instanceof Ci.nsIChannel)) { if (!(channel instanceof Ci.nsIChannel)) {
channel = this.newChannel(aSource); channel = this.newChannel(aSource);

Просмотреть файл

@ -16,7 +16,7 @@
* The Original Code is Necko Test Code. * The Original Code is Necko Test Code.
* *
* The Initial Developer of the Original Code is * The Initial Developer of the Original Code is
* Mozilla Corporation. * the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2009 * Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved. * the Initial Developer. All Rights Reserved.
* *
@ -352,6 +352,27 @@ function test_asyncFetch_with_nsIFile()
}); });
} }
function test_asyncFetch_with_nsIInputString()
{
const TEST_DATA = "this is a test string";
let istream = Cc["@mozilla.org/io/string-input-stream;1"].
createInstance(Ci.nsIStringInputStream);
istream.setData(TEST_DATA, TEST_DATA.length);
// Read the input stream asynchronously.
NetUtil.asyncFetch(istream, function(aInputStream, aResult) {
// Check that we had success.
do_check_true(Components.isSuccessCode(aResult));
// Check that we got the right data.
do_check_eq(aInputStream.available(), TEST_DATA.length);
do_check_eq(NetUtil.readInputStreamToString(aInputStream, TEST_DATA.length),
TEST_DATA);
run_next_test();
});
}
function test_asyncFetch_does_not_block() function test_asyncFetch_does_not_block()
{ {
// Create our channel that has no data. // Create our channel that has no data.
@ -533,6 +554,7 @@ let tests = [
test_asyncFetch_with_nsIURI, test_asyncFetch_with_nsIURI,
test_asyncFetch_with_string, test_asyncFetch_with_string,
test_asyncFetch_with_nsIFile, test_asyncFetch_with_nsIFile,
test_asyncFetch_with_nsIInputString,
test_asyncFetch_does_not_block, test_asyncFetch_does_not_block,
test_newChannel_no_specifier, test_newChannel_no_specifier,
test_newChannel_with_string, test_newChannel_with_string,