From 1726edbc21978243ada56a79d73bbb003beb8abf Mon Sep 17 00:00:00 2001 From: Raymond Lee Date: Tue, 11 Jun 2013 17:10:48 +0800 Subject: [PATCH] Bug 874808 - simpleDownload should accept string arguments and options. r=paolo --- .../components/jsdownloads/src/Downloads.jsm | 27 ++++++++++++++++--- .../jsdownloads/test/unit/test_Downloads.js | 16 +++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/toolkit/components/jsdownloads/src/Downloads.jsm b/toolkit/components/jsdownloads/src/Downloads.jsm index 55979bc70e3c..111a797335c1 100644 --- a/toolkit/components/jsdownloads/src/Downloads.jsm +++ b/toolkit/components/jsdownloads/src/Downloads.jsm @@ -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. diff --git a/toolkit/components/jsdownloads/test/unit/test_Downloads.js b/toolkit/components/jsdownloads/test/unit/test_Downloads.js index f8eb61899538..faf55fb6b211 100644 --- a/toolkit/components/jsdownloads/test/unit/test_Downloads.js +++ b/toolkit/components/jsdownloads/test/unit/test_Downloads.js @@ -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