From 71d1d8ef0e424b46237ef302cb429f2b7b9ad006 Mon Sep 17 00:00:00 2001 From: Marina Samuel Date: Thu, 1 Aug 2013 15:18:02 -0400 Subject: [PATCH] Bug 883959: Part 2: Rework download progress. r=sfoster. --- browser/metro/base/content/downloads.js | 31 +++++++++++-------- .../base/tests/mochitest/browser_downloads.js | 12 ++++++- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/browser/metro/base/content/downloads.js b/browser/metro/base/content/downloads.js index adc262abcff..6c66bcc36c1 100644 --- a/browser/metro/base/content/downloads.js +++ b/browser/metro/base/content/downloads.js @@ -12,6 +12,7 @@ var Downloads = { * downloads if it starts before other downloads have completed. */ _downloadCount: 0, + _downloadsInProgress: 0, _inited: false, _progressAlert: null, _lastSec: Infinity, @@ -99,25 +100,26 @@ var Downloads = { }, cancelDownload: function dh_cancelDownload(aDownload) { - this._progressNotificationInfo.delete(aDownload.guid); - this._runDownloadBooleanMap.delete(aDownload.targetFile.path); - this._downloadCount--; - if (this._progressNotificationInfo.size == 0) { - this._notificationBox.removeNotification(this._progressNotification); - this._progressNotification = null; - } - let fileURI = aDownload.target; if (!(fileURI && fileURI.spec)) { Util.dumpLn("Cant remove download file for: "+aDownload.id+", fileURI is invalid"); - return; } - let file = this._getLocalFile(fileURI); try { - this.manager.cancelDownload(aDownload.id); + let file = this._getLocalFile(fileURI); if (file && file.exists()) file.remove(false); + this.manager.cancelDownload(aDownload.id); + + // If cancelling was successful, stop tracking the download. + this._progressNotificationInfo.delete(aDownload.guid); + this._runDownloadBooleanMap.delete(aDownload.targetFile.path); + this._downloadCount--; + this._downloadsInProgress--; + if (this._downloadsInProgress <= 0) { + this._notificationBox.removeNotification(this._progressNotification); + this._progressNotification = null; + } } catch (ex) { Util.dumpLn("Failed to cancel download, with id: "+aDownload.id+", download target URI spec: " + fileURI.spec); Util.dumpLn("Failed download source:"+(aDownload.source && aDownload.source.spec)); @@ -367,6 +369,7 @@ var Downloads = { break; case "dl-start": this._downloadCount++; + this._downloadsInProgress++; let download = aSubject.QueryInterface(Ci.nsIDownload); if (!this._progressNotificationInfo.get(download.guid)) { this._progressNotificationInfo.set(download.guid, {}); @@ -378,18 +381,19 @@ var Downloads = { this.updateInfobar(download); break; case "dl-done": + this._downloadsInProgress--; download = aSubject.QueryInterface(Ci.nsIDownload); let runAfterDownload = this._runDownloadBooleanMap.get(download.targetFile.path); if (runAfterDownload) { this.openDownload(download); } - this._progressNotificationInfo.delete(download.guid); this._runDownloadBooleanMap.delete(download.targetFile.path); - if (this._progressNotificationInfo.size == 0) { + if (this._downloadsInProgress == 0) { if (this._downloadCount > 1 || !runAfterDownload) { this._showDownloadCompleteNotification(download); } + this._progressNotificationInfo.clear(); this._downloadCount = 0; this._notificationBox.removeNotification(this._progressNotification); this._progressNotification = null; @@ -397,6 +401,7 @@ var Downloads = { break; case "dl-failed": download = aSubject.QueryInterface(Ci.nsIDownload); + this._showDownloadFailedNotification(download); break; } }, diff --git a/browser/metro/base/tests/mochitest/browser_downloads.js b/browser/metro/base/tests/mochitest/browser_downloads.js index 2297ab6843b..5c323b352d4 100644 --- a/browser/metro/base/tests/mochitest/browser_downloads.js +++ b/browser/metro/base/tests/mochitest/browser_downloads.js @@ -331,4 +331,14 @@ gTests.push({ } }); - +/** + * Make sure the cancelled/aborted downloads are handled correctly. + */ +gTests.push({ + desc: "Cancel/Abort Downloads", + run: function(){ + todo(false, "Ensure that a cancelled/aborted download is in the correct state \ + including correct values for state variables (e.g. _downloadCount, _downloadsInProgress) \ + and the existence of the downloaded file."); + } +}); \ No newline at end of file