Bug 532146 - NetUtil.jsm needs a helper for creating new channels

Adds a new method for creating an nsIChannel from a string spec, an nsIURI, or
an nsIFile.
r=bz
sr=vlad
This commit is contained in:
Shawn Wilsher 2009-12-15 23:33:06 -08:00
parent 8b5747b4d8
commit 7ca65cca32
2 changed files with 97 additions and 0 deletions

View File

@ -204,6 +204,41 @@ const NetUtil = {
return this.ioService.newURI(aTarget, aOriginCharset, aBaseURI);
},
/**
* Constructs a new channel for the given spec, character set, and base URI,
* or nsIURI, or nsIFile.
*
* @param aWhatToLoad
* The string spec for the desired URI, an nsIURI, or an nsIFile.
* @param aOriginCharset [optional]
* The character set for the URI. Only used if aWhatToLoad is a
* string.
* @param aBaseURI [optional]
* The base URI for the spec. Only used if aWhatToLoad is a string.
*
* @return an nsIChannel object.
*/
newChannel: function NetUtil_newChannel(aWhatToLoad, aOriginCharset,
aBaseURI)
{
if (!aWhatToLoad) {
let exception = new Components.Exception(
"Must have a non-null string spec, nsIURI, or nsIFile object",
Cr.NS_ERROR_INVALID_ARG,
Components.stack.caller
);
throw exception;
}
let uri = aWhatToLoad;
if (!(aWhatToLoad instanceof Ci.nsIURI)) {
// We either have a string or an nsIFile that we'll need a URI for.
uri = this.newURI(aWhatToLoad, aOriginCharset, aBaseURI);
}
return this.ioService.newChannelFromURI(uri);
},
/**
* Returns a reference to nsIIOService.
*

View File

@ -286,6 +286,64 @@ function test_asyncFetch_does_not_block()
});
}
function test_newChannel_no_specifier()
{
try {
NetUtil.newChannel();
do_throw("should throw!");
}
catch (e) {
do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
}
run_next_test();
}
function test_newChannel_with_string()
{
const TEST_SPEC = "http://mozilla.org";
// Check that we get the same URI back from channel the IO service creates and
// the channel the utility method creates.
let ios = NetUtil.ioService;
let iosChannel = ios.newChannel(TEST_SPEC, null, null);
let NetUtilChannel = NetUtil.newChannel(TEST_SPEC);
do_check_true(iosChannel.URI.equals(NetUtilChannel.URI));
run_next_test();
}
function test_newChannel_with_nsIURI()
{
const TEST_SPEC = "http://mozilla.org";
// Check that we get the same URI back from channel the IO service creates and
// the channel the utility method creates.
let uri = NetUtil.newURI(TEST_SPEC);
let iosChannel = NetUtil.ioService.newChannelFromURI(uri);
let NetUtilChannel = NetUtil.newChannel(uri);
do_check_true(iosChannel.URI.equals(NetUtilChannel.URI));
run_next_test();
}
function test_newChannel_with_nsIFile()
{
let file = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties).
get("TmpD", Ci.nsIFile);
file.append("NetUtil-test-file.tmp");
// Check that we get the same URI back from channel the IO service creates and
// the channel the utility method creates.
let uri = NetUtil.newURI(file);
let iosChannel = NetUtil.ioService.newChannelFromURI(uri);
let NetUtilChannel = NetUtil.newChannel(uri);
do_check_true(iosChannel.URI.equals(NetUtilChannel.URI));
run_next_test();
}
////////////////////////////////////////////////////////////////////////////////
//// Test Runner
@ -300,6 +358,10 @@ let tests = [
test_asyncFetch_no_callback,
test_asyncFetch,
test_asyncFetch_does_not_block,
test_newChannel_no_specifier,
test_newChannel_with_string,
test_newChannel_with_nsIURI,
test_newChannel_with_nsIFile,
];
let index = 0;