зеркало из https://github.com/mozilla/gecko-dev.git
Bug 874808 - simpleDownload should accept string arguments and options. r=paolo
This commit is contained in:
Родитель
69b2411801
Коммит
1726edbc21
|
@ -33,6 +33,10 @@ XPCOMUtils.defineLazyModuleGetter(this, "DownloadStore",
|
||||||
"resource://gre/modules/DownloadStore.jsm");
|
"resource://gre/modules/DownloadStore.jsm");
|
||||||
XPCOMUtils.defineLazyModuleGetter(this, "DownloadUIHelper",
|
XPCOMUtils.defineLazyModuleGetter(this, "DownloadUIHelper",
|
||||||
"resource://gre/modules/DownloadUIHelper.jsm");
|
"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",
|
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
|
||||||
"resource://gre/modules/commonjs/sdk/core/promise.js");
|
"resource://gre/modules/commonjs/sdk/core/promise.js");
|
||||||
XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
||||||
|
@ -109,22 +113,39 @@ this.Downloads = {
|
||||||
* reference to a Download object using the createDownload function.
|
* reference to a Download object using the createDownload function.
|
||||||
*
|
*
|
||||||
* @param aSource
|
* @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
|
* @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}
|
* @return {Promise}
|
||||||
* @resolves When the download has finished successfully.
|
* @resolves When the download has finished successfully.
|
||||||
* @rejects JavaScript exception if the download failed.
|
* @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
|
// Wrap the arguments into simple objects resembling DownloadSource and
|
||||||
// DownloadTarget, if they are not objects of that type already.
|
// DownloadTarget, if they are not objects of that type already.
|
||||||
if (aSource instanceof Ci.nsIURI) {
|
if (aSource instanceof Ci.nsIURI) {
|
||||||
aSource = { uri: aSource };
|
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) {
|
if (aTarget instanceof Ci.nsIFile) {
|
||||||
aTarget = { file: aTarget };
|
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.
|
// Create and start the actual download.
|
||||||
|
|
|
@ -83,6 +83,22 @@ add_task(function test_simpleDownload_object_arguments()
|
||||||
yield promiseVerifyContents(targetFile, TEST_DATA_SHORT);
|
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
|
* Tests that the getPublicDownloadList function returns the same list when
|
||||||
* called multiple times. More detailed tests are implemented separately for
|
* called multiple times. More detailed tests are implemented separately for
|
||||||
|
|
Загрузка…
Ссылка в новой задаче