Loading toolkit/components/downloads/public/nsIDownloadManager.idl +12 −1 Original line number Diff line number Diff line Loading @@ -51,7 +51,7 @@ interface nsIDownloadProgressListener; interface nsISimpleEnumerator; interface mozIStorageConnection; [scriptable, uuid(1c015a52-c4e3-4c1c-9071-2c2eaeed8bfc)] [scriptable, uuid(bbf6561b-d7d7-48fa-967c-9cc46cc372b4)] interface nsIDownloadManager : nsISupports { // Download States const short DOWNLOAD_NOTSTARTED = -1; Loading Loading @@ -150,6 +150,17 @@ interface nsIDownloadManager : nsISupports { */ void resumeDownload(in unsigned long aID); /** * Retries a failed download. * * @param aID The unique ID of the download. * @throws NS_ERROR_NOT_AVAILALE if the download id is not known. * @throws NS_ERROR_FAILURE if the download is not in the following states: * nsIDownloadManager::DOWNLOAD_CANCELED * nsIDownloadManager::DOWNLOAD_FAILED */ void retryDownload(in unsigned long aID); /** * Opens the Download Manager front end, selecting the specified download. * Loading toolkit/components/downloads/src/nsDownloadManager.cpp +141 −19 Original line number Diff line number Diff line Loading @@ -428,6 +428,105 @@ nsDownloadManager::GetRetentionBehavior() return val; } nsresult nsDownloadManager::GetDownloadFromDB(PRUint32 aID, nsDownload **retVal) { NS_ASSERTION(!FindDownload(aID), "If it is a current download, you should not call this method!"); // First, let's query the database and see if it even exists nsCOMPtr<mozIStorageStatement> stmt; nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING( "SELECT id, state, startTime, source, target, name " "FROM moz_downloads " "WHERE id = ?1"), getter_AddRefs(stmt)); NS_ENSURE_SUCCESS(rv, rv); rv = stmt->BindInt64Parameter(0, aID); NS_ENSURE_SUCCESS(rv, rv); PRBool hasResults = PR_FALSE; rv = stmt->ExecuteStep(&hasResults); if (NS_FAILED(rv) || !hasResults) return NS_ERROR_NOT_AVAILABLE; // We have a download, so lets create it nsRefPtr<nsDownload> dl = new nsDownload(); if (!dl) return NS_ERROR_OUT_OF_MEMORY; // Setting all properties of the download now dl->mCancelable = nsnull; dl->mID = stmt->AsInt64(0); dl->mDownloadState = stmt->AsInt32(1); dl->mStartTime = stmt->AsInt64(2); nsCString source; stmt->GetUTF8String(3, source); rv = NS_NewURI(getter_AddRefs(dl->mSource), source); NS_ENSURE_SUCCESS(rv, rv); nsCString target; stmt->GetUTF8String(4, target); rv = NS_NewURI(getter_AddRefs(dl->mTarget), target); NS_ENSURE_SUCCESS(rv, rv); stmt->GetString(5, dl->mDisplayName); nsCOMPtr<nsILocalFile> file; rv = dl->GetTargetFile(getter_AddRefs(file)); NS_ENSURE_SUCCESS(rv, rv); PRBool fileExists; if (NS_SUCCEEDED(file->Exists(&fileExists)) && fileExists) { if (dl->mDownloadState == nsIDownloadManager::DOWNLOAD_FINISHED) { dl->mPercentComplete = 100; PRInt64 size; rv = file->GetFileSize(&size); NS_ENSURE_SUCCESS(rv, rv); dl->mMaxBytes = dl->mCurrBytes = size; } else { dl->mPercentComplete = -1; dl->mMaxBytes = LL_MAXUINT; } } else { dl->mPercentComplete = 0; dl->mMaxBytes = LL_MAXUINT; dl->mCurrBytes = 0; } // Addrefing and returning NS_ADDREF(*retVal = dl); return NS_OK; } nsresult nsDownloadManager::AddToCurrentDownloads(nsDownload *aDl) { // If this is an install operation, ensure we have a progress listener for the // install and track this download separately. if (aDl->mDownloadType == nsIXPInstallManagerUI::DOWNLOAD_TYPE_INSTALL) { if (!mXPIProgress) { mXPIProgress = new nsXPIProgressListener(this); if (!mXPIProgress) return NS_ERROR_OUT_OF_MEMORY; } nsIXPIProgressDialog *dialog = mXPIProgress.get(); nsXPIProgressListener *listener = NS_STATIC_CAST(nsXPIProgressListener*, dialog); listener->AddDownload(aDl); } mCurrentDownloads.AppendObject(aDl); return NS_OK; } /////////////////////////////////////////////////////////////////////////////// //// nsIDownloadManager NS_IMETHODIMP nsDownloadManager::GetActiveDownloadCount(PRInt32 *aResult) { Loading @@ -442,9 +541,6 @@ nsDownloadManager::GetActiveDownloads(nsISimpleEnumerator **aResult) return NS_NewArrayEnumerator(aResult, mCurrentDownloads); } /////////////////////////////////////////////////////////////////////////////// // nsIDownloadManager NS_IMETHODIMP nsDownloadManager::AddDownload(DownloadType aDownloadType, nsIURI* aSource, Loading Loading @@ -502,21 +598,8 @@ nsDownloadManager::AddDownload(DownloadType aDownloadType, NS_ENSURE_TRUE(id, NS_ERROR_FAILURE); dl->mID = id; // If this is an install operation, ensure we have a progress listener for the // install and track this download separately. if (aDownloadType == nsIXPInstallManagerUI::DOWNLOAD_TYPE_INSTALL) { if (!mXPIProgress) { mXPIProgress = new nsXPIProgressListener(this); if (!mXPIProgress) return NS_ERROR_OUT_OF_MEMORY; } nsIXPIProgressDialog* dialog = mXPIProgress.get(); nsXPIProgressListener* listener = NS_STATIC_CAST(nsXPIProgressListener*, dialog); listener->AddDownload(*aDownload); } mCurrentDownloads.AppendObject(dl); rv = AddToCurrentDownloads(dl); NS_ENSURE_SUCCESS(rv, rv); NS_ADDREF(*aDownload = dl); Loading Loading @@ -601,6 +684,44 @@ nsDownloadManager::CancelDownload(PRUint32 aID) return NS_OK; } NS_IMETHODIMP nsDownloadManager::RetryDownload(PRUint32 aID) { nsRefPtr<nsDownload> dl; nsresult rv = GetDownloadFromDB(aID, getter_AddRefs(dl)); NS_ENSURE_SUCCESS(rv, rv); // if our download is not canceled or failed, we should fail if (dl->mDownloadState != nsIDownloadManager::DOWNLOAD_FAILED && dl->mDownloadState != nsIDownloadManager::DOWNLOAD_CANCELED) return NS_ERROR_FAILURE; // we are redownloading this, so we need to link the download manager to the // download else we'll try to dereference null pointers - eww dl->mDownloadManager = this; dl->SetStartTime(PR_Now()); rv = dl->SetState(nsIDownloadManager::DOWNLOAD_NOTSTARTED); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIWebBrowserPersist> wbp = do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", &rv); NS_ENSURE_SUCCESS(rv, rv); // Creates a cycle that will be broken in nsDownload::OnStateChange dl->mCancelable = wbp; wbp->SetProgressListener(dl); rv = wbp->SetPersistFlags(nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES | nsIWebBrowserPersist::PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION); NS_ENSURE_SUCCESS(rv, rv); rv = AddToCurrentDownloads(dl); NS_ENSURE_SUCCESS(rv, rv); return wbp->SaveURI(dl->mSource, nsnull, nsnull, nsnull, nsnull, dl->mTarget); } NS_IMETHODIMP nsDownloadManager::RemoveDownload(PRUint32 aID) { Loading Loading @@ -1187,7 +1308,7 @@ nsDownload::nsDownload() : mDownloadState(nsIDownloadManager::DOWNLOAD_NOTSTARTE mID(0), mPercentComplete(0), mCurrBytes(LL_ZERO), mMaxBytes(LL_ZERO), mMaxBytes(LL_MAXUINT), mStartTime(LL_ZERO), mLastUpdate(PR_Now() - (PRUint32)gUpdateInterval), mPaused(PR_FALSE), Loading Loading @@ -1641,6 +1762,7 @@ nsresult nsDownload::UpdateDB() { NS_ASSERTION(mID, "Download ID is stored as zero. This is bad!"); NS_ASSERTION(mDownloadManager, "Egads! We have no download manager!"); nsCOMPtr<mozIStorageStatement> stmt; nsresult rv = mDownloadManager->mDBConn->CreateStatement(NS_LITERAL_CSTRING( Loading toolkit/components/downloads/src/nsDownloadManager.h +2 −0 Original line number Diff line number Diff line Loading @@ -96,6 +96,8 @@ protected: nsresult InitDB(PRBool *aDoImport); nsresult CreateTable(); nsresult ImportDownloadHistory(); nsresult GetDownloadFromDB(PRUint32 aID, nsDownload **retVal); nsresult AddToCurrentDownloads(nsDownload *aDl); /** * Adds a download with the specified information to the DB. Loading toolkit/components/downloads/test/unit/head_download_manager.js +78 −0 Original line number Diff line number Diff line Loading @@ -66,6 +66,7 @@ if (!profileDir) { file.append("downloads.rdf"); return file; } print("*** Throwing trying to get " + prop); throw Cr.NS_ERROR_FAILURE; }, QueryInterface: function(iid) { Loading @@ -86,3 +87,80 @@ function importDownloadsFile(aFName) file.copyTo(newFile, "downloads.rdf"); } function cleanup() { // removing rdf var rdfFile = dirSvc.get("DLoads", Ci.nsIFile); if (rdfFile.exists()) rdfFile.remove(true); // removing database var dbFile = dirSvc.get("ProfD", Ci.nsIFile); dbFile.append("downloads.sqlite"); if (dbFile.exists()) try { dbFile.remove(true); } catch(e) { /* stupid windows box */ } // removing downloaded file var destFile = dirSvc.get("ProfD", Ci.nsIFile); destFile.append("download.result"); if (destFile.exists()) destFile.remove(true); } var gDownloadCount = 0; function addDownload() { 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; var destFile = dirSvc.get("ProfD", Ci.nsIFile); destFile.append("download.result"); var srcFile = dirSvc.get("ProfD", Ci.nsIFile); srcFile.append("LICENSE"); var dl = dm.addDownload(nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD, createURI("http://localhost:4444/LICENSE"), createURI(destFile), null, null, null, Math.round(Date.now() * 1000), null, persist); // This will throw if it isn't found, and that would mean test failure, so no // try catch block var test = dm.getDownload(dl.id); // it is part of the active downloads now, even if it hasn't started. gDownloadCount++; persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener); persist.saveURI(dl.source, null, null, null, null, dl.targetFile); return dl; } function getDownloadListener() { return { onDownloadStateChange: function(aState, aDownload) { if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) gDownloadCount--; if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_CANCELED || aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FAILED) { gDownloadCount--; } if (gDownloadCount == 0) httpserv.stop(); }, onStateChange: function(a, b, c, d, e) { }, onProgressChange: function(a, b, c, d, e, f, g) { }, onStatusChange: function(a, b, c, d, e) { }, onLocationChange: function(a, b, c, d) { }, onSecurityChange: function(a, b, c, d) { } }; } cleanup(); toolkit/components/downloads/test/unit/test_bug_382825.js 0 → 100644 +95 −0 Original line number Diff line number Diff line /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Download Manager Test Code. * * The Initial Developer of the Original Code is * Mozilla Corporation. * Portions created by the Initial Developer are Copyright (C) 2007 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Shawn Wilsher <me@shawnwilsher.com> (Original Author) * * 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 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ // This tests the retryDownload function of nsIDownloadManager. This function // was added in Bug 382825. const nsIDownloadManager = Ci.nsIDownloadManager; const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager); function test_retry_canceled() { var dl = addDownload(); // since we are going to be retrying a failed download, we need to inflate // this so it doesn't stop our server gDownloadCount++; dm.cancelDownload(dl.id); do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state); // Our download object will no longer be updated. dm.retryDownload(dl.id); } function test_retry_bad() { try { dm.retryDownload(0); do_throw("Hey! We expect to get an exception with this!"); } catch(e) { do_check_eq(Components.lastResult, Cr.NS_ERROR_NOT_AVAILABLE); } } var tests = [test_retry_canceled, test_retry_bad]; var httpserv = null; function run_test() { httpserv = new nsHttpServer(); httpserv.registerDirectory("/", dirSvc.get("ProfD", Ci.nsILocalFile)); httpserv.start(4444); dm.addListener(getDownloadListener()); for (var i = 0; i < tests.length; i++) tests[i](); cleanup(); var thread = Cc["@mozilla.org/thread-manager;1"] .getService().currentThread; while (!httpserv.isStopped()) thread.processNextEvent(true); // get rid of any pending requests while (thread.hasPendingEvents()) thread.processNextEvent(true); } Loading
toolkit/components/downloads/public/nsIDownloadManager.idl +12 −1 Original line number Diff line number Diff line Loading @@ -51,7 +51,7 @@ interface nsIDownloadProgressListener; interface nsISimpleEnumerator; interface mozIStorageConnection; [scriptable, uuid(1c015a52-c4e3-4c1c-9071-2c2eaeed8bfc)] [scriptable, uuid(bbf6561b-d7d7-48fa-967c-9cc46cc372b4)] interface nsIDownloadManager : nsISupports { // Download States const short DOWNLOAD_NOTSTARTED = -1; Loading Loading @@ -150,6 +150,17 @@ interface nsIDownloadManager : nsISupports { */ void resumeDownload(in unsigned long aID); /** * Retries a failed download. * * @param aID The unique ID of the download. * @throws NS_ERROR_NOT_AVAILALE if the download id is not known. * @throws NS_ERROR_FAILURE if the download is not in the following states: * nsIDownloadManager::DOWNLOAD_CANCELED * nsIDownloadManager::DOWNLOAD_FAILED */ void retryDownload(in unsigned long aID); /** * Opens the Download Manager front end, selecting the specified download. * Loading
toolkit/components/downloads/src/nsDownloadManager.cpp +141 −19 Original line number Diff line number Diff line Loading @@ -428,6 +428,105 @@ nsDownloadManager::GetRetentionBehavior() return val; } nsresult nsDownloadManager::GetDownloadFromDB(PRUint32 aID, nsDownload **retVal) { NS_ASSERTION(!FindDownload(aID), "If it is a current download, you should not call this method!"); // First, let's query the database and see if it even exists nsCOMPtr<mozIStorageStatement> stmt; nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING( "SELECT id, state, startTime, source, target, name " "FROM moz_downloads " "WHERE id = ?1"), getter_AddRefs(stmt)); NS_ENSURE_SUCCESS(rv, rv); rv = stmt->BindInt64Parameter(0, aID); NS_ENSURE_SUCCESS(rv, rv); PRBool hasResults = PR_FALSE; rv = stmt->ExecuteStep(&hasResults); if (NS_FAILED(rv) || !hasResults) return NS_ERROR_NOT_AVAILABLE; // We have a download, so lets create it nsRefPtr<nsDownload> dl = new nsDownload(); if (!dl) return NS_ERROR_OUT_OF_MEMORY; // Setting all properties of the download now dl->mCancelable = nsnull; dl->mID = stmt->AsInt64(0); dl->mDownloadState = stmt->AsInt32(1); dl->mStartTime = stmt->AsInt64(2); nsCString source; stmt->GetUTF8String(3, source); rv = NS_NewURI(getter_AddRefs(dl->mSource), source); NS_ENSURE_SUCCESS(rv, rv); nsCString target; stmt->GetUTF8String(4, target); rv = NS_NewURI(getter_AddRefs(dl->mTarget), target); NS_ENSURE_SUCCESS(rv, rv); stmt->GetString(5, dl->mDisplayName); nsCOMPtr<nsILocalFile> file; rv = dl->GetTargetFile(getter_AddRefs(file)); NS_ENSURE_SUCCESS(rv, rv); PRBool fileExists; if (NS_SUCCEEDED(file->Exists(&fileExists)) && fileExists) { if (dl->mDownloadState == nsIDownloadManager::DOWNLOAD_FINISHED) { dl->mPercentComplete = 100; PRInt64 size; rv = file->GetFileSize(&size); NS_ENSURE_SUCCESS(rv, rv); dl->mMaxBytes = dl->mCurrBytes = size; } else { dl->mPercentComplete = -1; dl->mMaxBytes = LL_MAXUINT; } } else { dl->mPercentComplete = 0; dl->mMaxBytes = LL_MAXUINT; dl->mCurrBytes = 0; } // Addrefing and returning NS_ADDREF(*retVal = dl); return NS_OK; } nsresult nsDownloadManager::AddToCurrentDownloads(nsDownload *aDl) { // If this is an install operation, ensure we have a progress listener for the // install and track this download separately. if (aDl->mDownloadType == nsIXPInstallManagerUI::DOWNLOAD_TYPE_INSTALL) { if (!mXPIProgress) { mXPIProgress = new nsXPIProgressListener(this); if (!mXPIProgress) return NS_ERROR_OUT_OF_MEMORY; } nsIXPIProgressDialog *dialog = mXPIProgress.get(); nsXPIProgressListener *listener = NS_STATIC_CAST(nsXPIProgressListener*, dialog); listener->AddDownload(aDl); } mCurrentDownloads.AppendObject(aDl); return NS_OK; } /////////////////////////////////////////////////////////////////////////////// //// nsIDownloadManager NS_IMETHODIMP nsDownloadManager::GetActiveDownloadCount(PRInt32 *aResult) { Loading @@ -442,9 +541,6 @@ nsDownloadManager::GetActiveDownloads(nsISimpleEnumerator **aResult) return NS_NewArrayEnumerator(aResult, mCurrentDownloads); } /////////////////////////////////////////////////////////////////////////////// // nsIDownloadManager NS_IMETHODIMP nsDownloadManager::AddDownload(DownloadType aDownloadType, nsIURI* aSource, Loading Loading @@ -502,21 +598,8 @@ nsDownloadManager::AddDownload(DownloadType aDownloadType, NS_ENSURE_TRUE(id, NS_ERROR_FAILURE); dl->mID = id; // If this is an install operation, ensure we have a progress listener for the // install and track this download separately. if (aDownloadType == nsIXPInstallManagerUI::DOWNLOAD_TYPE_INSTALL) { if (!mXPIProgress) { mXPIProgress = new nsXPIProgressListener(this); if (!mXPIProgress) return NS_ERROR_OUT_OF_MEMORY; } nsIXPIProgressDialog* dialog = mXPIProgress.get(); nsXPIProgressListener* listener = NS_STATIC_CAST(nsXPIProgressListener*, dialog); listener->AddDownload(*aDownload); } mCurrentDownloads.AppendObject(dl); rv = AddToCurrentDownloads(dl); NS_ENSURE_SUCCESS(rv, rv); NS_ADDREF(*aDownload = dl); Loading Loading @@ -601,6 +684,44 @@ nsDownloadManager::CancelDownload(PRUint32 aID) return NS_OK; } NS_IMETHODIMP nsDownloadManager::RetryDownload(PRUint32 aID) { nsRefPtr<nsDownload> dl; nsresult rv = GetDownloadFromDB(aID, getter_AddRefs(dl)); NS_ENSURE_SUCCESS(rv, rv); // if our download is not canceled or failed, we should fail if (dl->mDownloadState != nsIDownloadManager::DOWNLOAD_FAILED && dl->mDownloadState != nsIDownloadManager::DOWNLOAD_CANCELED) return NS_ERROR_FAILURE; // we are redownloading this, so we need to link the download manager to the // download else we'll try to dereference null pointers - eww dl->mDownloadManager = this; dl->SetStartTime(PR_Now()); rv = dl->SetState(nsIDownloadManager::DOWNLOAD_NOTSTARTED); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIWebBrowserPersist> wbp = do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", &rv); NS_ENSURE_SUCCESS(rv, rv); // Creates a cycle that will be broken in nsDownload::OnStateChange dl->mCancelable = wbp; wbp->SetProgressListener(dl); rv = wbp->SetPersistFlags(nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES | nsIWebBrowserPersist::PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION); NS_ENSURE_SUCCESS(rv, rv); rv = AddToCurrentDownloads(dl); NS_ENSURE_SUCCESS(rv, rv); return wbp->SaveURI(dl->mSource, nsnull, nsnull, nsnull, nsnull, dl->mTarget); } NS_IMETHODIMP nsDownloadManager::RemoveDownload(PRUint32 aID) { Loading Loading @@ -1187,7 +1308,7 @@ nsDownload::nsDownload() : mDownloadState(nsIDownloadManager::DOWNLOAD_NOTSTARTE mID(0), mPercentComplete(0), mCurrBytes(LL_ZERO), mMaxBytes(LL_ZERO), mMaxBytes(LL_MAXUINT), mStartTime(LL_ZERO), mLastUpdate(PR_Now() - (PRUint32)gUpdateInterval), mPaused(PR_FALSE), Loading Loading @@ -1641,6 +1762,7 @@ nsresult nsDownload::UpdateDB() { NS_ASSERTION(mID, "Download ID is stored as zero. This is bad!"); NS_ASSERTION(mDownloadManager, "Egads! We have no download manager!"); nsCOMPtr<mozIStorageStatement> stmt; nsresult rv = mDownloadManager->mDBConn->CreateStatement(NS_LITERAL_CSTRING( Loading
toolkit/components/downloads/src/nsDownloadManager.h +2 −0 Original line number Diff line number Diff line Loading @@ -96,6 +96,8 @@ protected: nsresult InitDB(PRBool *aDoImport); nsresult CreateTable(); nsresult ImportDownloadHistory(); nsresult GetDownloadFromDB(PRUint32 aID, nsDownload **retVal); nsresult AddToCurrentDownloads(nsDownload *aDl); /** * Adds a download with the specified information to the DB. Loading
toolkit/components/downloads/test/unit/head_download_manager.js +78 −0 Original line number Diff line number Diff line Loading @@ -66,6 +66,7 @@ if (!profileDir) { file.append("downloads.rdf"); return file; } print("*** Throwing trying to get " + prop); throw Cr.NS_ERROR_FAILURE; }, QueryInterface: function(iid) { Loading @@ -86,3 +87,80 @@ function importDownloadsFile(aFName) file.copyTo(newFile, "downloads.rdf"); } function cleanup() { // removing rdf var rdfFile = dirSvc.get("DLoads", Ci.nsIFile); if (rdfFile.exists()) rdfFile.remove(true); // removing database var dbFile = dirSvc.get("ProfD", Ci.nsIFile); dbFile.append("downloads.sqlite"); if (dbFile.exists()) try { dbFile.remove(true); } catch(e) { /* stupid windows box */ } // removing downloaded file var destFile = dirSvc.get("ProfD", Ci.nsIFile); destFile.append("download.result"); if (destFile.exists()) destFile.remove(true); } var gDownloadCount = 0; function addDownload() { 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; var destFile = dirSvc.get("ProfD", Ci.nsIFile); destFile.append("download.result"); var srcFile = dirSvc.get("ProfD", Ci.nsIFile); srcFile.append("LICENSE"); var dl = dm.addDownload(nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD, createURI("http://localhost:4444/LICENSE"), createURI(destFile), null, null, null, Math.round(Date.now() * 1000), null, persist); // This will throw if it isn't found, and that would mean test failure, so no // try catch block var test = dm.getDownload(dl.id); // it is part of the active downloads now, even if it hasn't started. gDownloadCount++; persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener); persist.saveURI(dl.source, null, null, null, null, dl.targetFile); return dl; } function getDownloadListener() { return { onDownloadStateChange: function(aState, aDownload) { if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) gDownloadCount--; if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_CANCELED || aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FAILED) { gDownloadCount--; } if (gDownloadCount == 0) httpserv.stop(); }, onStateChange: function(a, b, c, d, e) { }, onProgressChange: function(a, b, c, d, e, f, g) { }, onStatusChange: function(a, b, c, d, e) { }, onLocationChange: function(a, b, c, d) { }, onSecurityChange: function(a, b, c, d) { } }; } cleanup();
toolkit/components/downloads/test/unit/test_bug_382825.js 0 → 100644 +95 −0 Original line number Diff line number Diff line /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Download Manager Test Code. * * The Initial Developer of the Original Code is * Mozilla Corporation. * Portions created by the Initial Developer are Copyright (C) 2007 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Shawn Wilsher <me@shawnwilsher.com> (Original Author) * * 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 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ // This tests the retryDownload function of nsIDownloadManager. This function // was added in Bug 382825. const nsIDownloadManager = Ci.nsIDownloadManager; const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager); function test_retry_canceled() { var dl = addDownload(); // since we are going to be retrying a failed download, we need to inflate // this so it doesn't stop our server gDownloadCount++; dm.cancelDownload(dl.id); do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state); // Our download object will no longer be updated. dm.retryDownload(dl.id); } function test_retry_bad() { try { dm.retryDownload(0); do_throw("Hey! We expect to get an exception with this!"); } catch(e) { do_check_eq(Components.lastResult, Cr.NS_ERROR_NOT_AVAILABLE); } } var tests = [test_retry_canceled, test_retry_bad]; var httpserv = null; function run_test() { httpserv = new nsHttpServer(); httpserv.registerDirectory("/", dirSvc.get("ProfD", Ci.nsILocalFile)); httpserv.start(4444); dm.addListener(getDownloadListener()); for (var i = 0; i < tests.length; i++) tests[i](); cleanup(); var thread = Cc["@mozilla.org/thread-manager;1"] .getService().currentThread; while (!httpserv.isStopped()) thread.processNextEvent(true); // get rid of any pending requests while (thread.hasPendingEvents()) thread.processNextEvent(true); }