Bug 508902 - NetUtil.jsm should have a newURI method.

r=bz
sr=bsmedberg
This commit is contained in:
Shawn Wilsher 2009-09-02 13:24:49 -07:00
parent eb5adbff74
commit 36e4260c09
2 changed files with 92 additions and 10 deletions

View File

@ -22,6 +22,7 @@
* *
* Contributor(s): * Contributor(s):
* Boris Zbarsky <bzbarsky@mit.edu> (original author) * Boris Zbarsky <bzbarsky@mit.edu> (original author)
* Shawn Wilsher <me@shawnwilsher.com>
* *
* Alternatively, the contents of this file may be used under the terms of * Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or * either the GNU General Public License Version 2 or later (the "GPL"), or
@ -45,8 +46,15 @@ let EXPORTED_SYMBOLS = [
* Necko utilities * Necko utilities
*/ */
////////////////////////////////////////////////////////////////////////////////
//// Constants
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cc = Components.classes; const Cc = Components.classes;
const Cr = Components.results;
////////////////////////////////////////////////////////////////////////////////
//// NetUtil Object
const NetUtil = { const NetUtil = {
/** /**
@ -54,22 +62,29 @@ const NetUtil = {
* to aSink (an output stream). The copy will happen on some background * to aSink (an output stream). The copy will happen on some background
* thread. Both streams will be closed when the copy completes. * thread. Both streams will be closed when the copy completes.
* *
* @param aSource the input stream to read from * @param aSource
* @param aSink the output stream to write to * The input stream to read from
* @param aCallback [optional] a function that will be called at copy * @param aSink
* completion with a single argument: the nsresult status code for * The output stream to write to
* the copy operation. * @param aCallback [optional]
* A function that will be called at copy completion with a single
* argument: the nsresult status code for the copy operation.
* *
* @return an nsIRequest representing the copy operation (for example, this * @return An nsIRequest representing the copy operation (for example, this
* can be used to cancel the copying). The consumer can ignore the * can be used to cancel the copying). The consumer can ignore the
* return value if desired. * return value if desired.
*/ */
asyncCopy: function _asyncCopy(aSource, aSink, aCallback) { asyncCopy: function NetUtil_asyncCopy(aSource, aSink, aCallback)
{
if (!aSource || !aSink) { if (!aSource || !aSink) {
throw "Must have a source and a sink"; let exception = new Components.Exception(
"Must have a source and a sink",
Cr.NS_ERROR_INVALID_ARG,
Components.stack.caller
);
throw exception;
} }
const ioUtil = Cc["@mozilla.org/io-util;1"].getService(Ci.nsIIOUtil);
var sourceBuffered = ioUtil.inputStreamIsBuffered(aSource); var sourceBuffered = ioUtil.inputStreamIsBuffered(aSource);
var sinkBuffered = ioUtil.outputStreamIsBuffered(aSink); var sinkBuffered = ioUtil.outputStreamIsBuffered(aSink);
@ -108,5 +123,43 @@ const NetUtil = {
// start the copying // start the copying
copier.asyncCopy(observer, null); copier.asyncCopy(observer, null);
return copier; return copier;
} },
/**
* Constructs a new URI for the given spec, character set, and base URI.
*
* @param aSpec
* The spec for the desired URI.
* @param aOriginCharset [optional]
* The character set for the URI.
* @param aBaseURI [optional]
* The base URI for the spec.
*
* @return an nsIURI object.
*/
newURI: function NetUtil_newURI(aSpec, aOriginCharset, aBaseURI)
{
if (!aSpec) {
let exception = new Components.Exception(
"Must have a non-null spec",
Cr.NS_ERROR_INVALID_ARG,
Components.stack.caller
);
throw exception;
}
return ioService.newURI(aSpec, aOriginCharset, aBaseURI);
},
}; };
////////////////////////////////////////////////////////////////////////////////
//// Initialization
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
// Define our lazy getters.
XPCOMUtils.defineLazyServiceGetter(this, "ioUtil", "@mozilla.org/io-util;1",
"nsIIOUtil");
XPCOMUtils.defineLazyServiceGetter(this, "ioService",
"@mozilla.org/network/io-service;1",
"nsIIOService");

View File

@ -44,6 +44,7 @@
Components.utils.import("resource://gre/modules/NetUtil.jsm"); Components.utils.import("resource://gre/modules/NetUtil.jsm");
const Cc = Components.classes; const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cr = Components.results;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
//// Helper Methods //// Helper Methods
@ -146,12 +147,40 @@ function test_async_write_file_nsISafeOutputStream()
}); });
} }
function test_newURI_no_spec_throws()
{
try {
NetUtil.newURI();
do_throw("should throw!");
}
catch (e) {
do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
}
run_next_test();
}
function test_newURI()
{
let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
// Check that we get the same URI back from the IO service and the utility method.
const TEST_URI = "http://mozilla.org";
let iosURI = ios.newURI(TEST_URI, null, null);
let NetUtilURI = NetUtil.newURI(TEST_URI);
do_check_true(iosURI.equals(NetUtilURI));
run_next_test();
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
//// Test Runner //// Test Runner
let tests = [ let tests = [
test_async_write_file, test_async_write_file,
test_async_write_file_nsISafeOutputStream, test_async_write_file_nsISafeOutputStream,
test_newURI_no_spec_throws,
test_newURI,
]; ];
let index = 0; let index = 0;