Bug 874808 - simpleDownload should accept string arguments and options. r=paolo

This commit is contained in:
Raymond Lee 2013-06-11 17:10:48 +08:00
parent 12ef4a49cd
commit e6efcf0b8f
2 changed files with 40 additions and 3 deletions

View File

@ -33,6 +33,10 @@ XPCOMUtils.defineLazyModuleGetter(this, "DownloadStore",
"resource://gre/modules/DownloadStore.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DownloadUIHelper",
"resource://gre/modules/DownloadUIHelper.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
"resource://gre/modules/FileUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/commonjs/sdk/core/promise.js");
XPCOMUtils.defineLazyModuleGetter(this, "Services",
@ -109,22 +113,39 @@ this.Downloads = {
* reference to a Download object using the createDownload function.
*
* @param aSource
* The nsIURI for the download source, or alternative DownloadSource.
* The nsIURI or string containing the URI spec for the download
* source, or alternative DownloadSource.
* @param aTarget
* The nsIFile for the download target, or alternative DownloadTarget.
* The nsIFile or string containing the file path, or alternative
* DownloadTarget.
* @param aOptions
* The object contains different additional options or null.
* { isPrivate: Indicates whether the download originated from a
* private window.
* }
*
* @return {Promise}
* @resolves When the download has finished successfully.
* @rejects JavaScript exception if the download failed.
*/
simpleDownload: function D_simpleDownload(aSource, aTarget) {
simpleDownload: function D_simpleDownload(aSource, aTarget, aOptions) {
// Wrap the arguments into simple objects resembling DownloadSource and
// DownloadTarget, if they are not objects of that type already.
if (aSource instanceof Ci.nsIURI) {
aSource = { uri: aSource };
} else if (typeof aSource == "string" ||
(typeof aSource == "object" && "charAt" in aSource)) {
aSource = { uri: NetUtil.newURI(aSource) };
}
if (aSource && aOptions && ("isPrivate" in aOptions)) {
aSource.isPrivate = aOptions.isPrivate;
}
if (aTarget instanceof Ci.nsIFile) {
aTarget = { file: aTarget };
} else if (typeof aTarget == "string" ||
(typeof aTarget == "object" && "charAt" in aTarget)) {
aTarget = { file: new FileUtils.File(aTarget) };
}
// Create and start the actual download.

View File

@ -83,6 +83,22 @@ add_task(function test_simpleDownload_object_arguments()
yield promiseVerifyContents(targetFile, TEST_DATA_SHORT);
});
/**
* Tests simpleDownload with string arguments.
*/
add_task(function test_simpleDownload_string_arguments()
{
let targetFile = getTempFile(TEST_TARGET_FILE_NAME);
yield Downloads.simpleDownload(TEST_SOURCE_URI.spec,
targetFile.path);
yield promiseVerifyContents(targetFile, TEST_DATA_SHORT);
targetFile = getTempFile(TEST_TARGET_FILE_NAME);
yield Downloads.simpleDownload(new String(TEST_SOURCE_URI.spec),
new String(targetFile.path));
yield promiseVerifyContents(targetFile, TEST_DATA_SHORT);
});
/**
* Tests that the getPublicDownloadList function returns the same list when
* called multiple times. More detailed tests are implemented separately for