B2G/gecko/toolkit/components/downloads/test/unit/head_download_manager.js
/**
 * Adds a download to the DM, and starts it.
 * @param aParams (optional): an optional object which contains the function
 *                            parameters:
 *                              resultFileName: leaf node for the target file
 *                              targetFile: nsIFile for the target (overrides resultFileName)
 *                              sourceURI: the download source URI
 *                              downloadName: the display name of the download
 *                              runBeforeStart: a function to run before starting the download
 *                              isPrivate: whether the download is private or not
 */
Adds a download to the DM, and starts it.
Adds a download to the DM and starts it .
function addDownload(aParams)
{
  if (!aParams)
    aParams = {};
  if (!("resultFileName" in aParams))
    aParams.resultFileName = "download.result";
  if (!("targetFile" in aParams)) {
    aParams.targetFile = dirSvc.get("ProfD", Ci.nsIFile);
    aParams.targetFile.append(aParams.resultFileName);
  }
  if (!("sourceURI" in aParams))
    aParams.sourceURI = "http://localhost:4444/head_download_manager.js";
  if (!("downloadName" in aParams))
    aParams.downloadName = null;
  if (!("runBeforeStart" in aParams))
    aParams.runBeforeStart = function () {};
  const nsIWBP = Ci.nsIWebBrowserPersist;
  var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
                .createInstance(Ci.nsIWebBrowserPersist);
  persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
                         nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
                         nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
  gDownloadCount++;
  let dm = downloadUtils.downloadManager;
  var dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
                          createURI(aParams.sourceURI),
                          createURI(aParams.targetFile), aParams.downloadName, null,
                          Math.round(Date.now() * 1000), null, persist, aParams.isPrivate);
  var test = dm.getDownload(dl.id);
  aParams.runBeforeStart.call(undefined, dl);
  persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener);
  persist.savePrivacyAwareURI(dl.source, null, null, null, null, dl.targetFile,
                              aParams.isPrivate);
  return dl;
}
function addDownload ( aParams ) { if ( ! aParams ) aParams = { } ; if ( ! ( "resultFileName" in aParams ) ) aParams . resultFileName = "download.result" ; if ( ! ( "targetFile" in aParams ) ) { aParams . targetFile = dirSvc . get ( "ProfD" , Ci . nsIFile ) ; aParams . targetFile . append ( aParams . resultFileName ) ; } if ( ! ( "sourceURI" in aParams ) ) aParams . sourceURI = "http://localhost:4444/head_download_manager.js" ; if ( ! ( "downloadName" in aParams ) ) aParams . downloadName = null ; if ( ! ( "runBeforeStart" in aParams ) ) aParams . runBeforeStart = function ( ) { } ; const nsIWBP = Ci . nsIWebBrowserPersist ; var persist = Cc [ "@mozilla.org/embedding/browser/nsWebBrowserPersist;1" ] . createInstance ( Ci . nsIWebBrowserPersist ) ; persist . persistFlags = nsIWBP . PERSIST_FLAGS_REPLACE_EXISTING_FILES | nsIWBP . PERSIST_FLAGS_BYPASS_CACHE | nsIWBP . PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION ; gDownloadCount ++ ; let dm = downloadUtils . downloadManager ; var dl = dm . addDownload ( Ci . nsIDownloadManager . DOWNLOAD_TYPE_DOWNLOAD , createURI ( aParams . sourceURI ) , createURI ( aParams . targetFile ) , aParams . downloadName , null , Math . round ( Date . now ( ) * 1000 ) , null , persist , aParams . isPrivate ) ; var test = dm . getDownload ( dl . id ) ; aParams . runBeforeStart . call ( undefined , dl ) ; persist . progressListener = dl . QueryInterface ( Ci . nsIWebProgressListener ) ; persist . savePrivacyAwareURI ( dl . source , null , null , null , null , dl . targetFile , aParams . isPrivate ) ; return dl ; }
Adds a download to the DM, and starts it.
Adds a download to the DM and starts it .
// it is part of the active downloads the moment addDownload is called // This will throw if it isn't found, and that would mean test failure, so no // try catch block