Bug 593815 - Remove all files left behind when a download is canceled. [r=sdwilsh, sr=bz, a=blocker]
authorWes Johnston <wjohnston@mozilla.com>
Mon, 22 Nov 2010 19:36:45 -0800
changeset 58404 17cecf2c18244b09dadd870e3aeede1f036e269c
parent 58403 9bbfdb7bc9a86b654c4e5079f8bf6023c386963f
child 58405 bdd47e6e4f17fc8ea84c2a9abc469f16645aba44
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewerssdwilsh, bz, blocker
bugs593815
milestone2.0b8pre
Bug 593815 - Remove all files left behind when a download is canceled. [r=sdwilsh, sr=bz, a=blocker]
toolkit/components/downloads/src/nsDownloadManager.cpp
uriloader/exthandler/nsExternalHelperAppService.cpp
--- a/toolkit/components/downloads/src/nsDownloadManager.cpp
+++ b/toolkit/components/downloads/src/nsDownloadManager.cpp
@@ -1511,16 +1511,25 @@ nsDownloadManager::CancelDownload(PRUint
   // cancel.
   if (dl->mTempFile) {
     PRBool exists;
     dl->mTempFile->Exists(&exists);
     if (exists)
       dl->mTempFile->Remove(PR_FALSE);
   }
 
+  nsCOMPtr<nsILocalFile> file;
+  if (NS_SUCCEEDED(dl->GetTargetFile(getter_AddRefs(file))))
+  {
+    PRBool exists;
+    file->Exists(&exists);
+    if (exists)
+      file->Remove(PR_FALSE);
+  }
+
   nsresult rv = dl->SetState(nsIDownloadManager::DOWNLOAD_CANCELED);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDownloadManager::RetryDownload(PRUint32 aID)
@@ -2770,22 +2779,16 @@ nsDownload::MoveTempToTarget()
 nsresult
 nsDownload::OpenWithApplication()
 {
   // First move the temporary file to the target location
   nsCOMPtr<nsILocalFile> target;
   nsresult rv = GetTargetFile(getter_AddRefs(target));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  // Make sure the suggested name is unique since in this case we don't
-  // have a file name that was guaranteed to be unique by going through
-  // the File Save dialog
-  rv = target->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
-  NS_ENSURE_SUCCESS(rv, rv);
-
   // Move the temporary file to the target location
   rv = MoveTempToTarget();
   NS_ENSURE_SUCCESS(rv, rv);
 
   // We do not verify the return value here because, irrespective of success
   // or failure of the method, the deletion of temp file has to take place, as
   // per the corresponding preference. But we store this separately as this is
   // what we ultimately return from this function.
--- a/uriloader/exthandler/nsExternalHelperAppService.cpp
+++ b/uriloader/exthandler/nsExternalHelperAppService.cpp
@@ -2347,17 +2347,17 @@ nsresult nsExternalAppHandler::OpenWithA
 #endif
     }
 
     // make the tmp file readonly so users won't edit it and lose the changes
     // only if we're going to delete the file
     if (deleteTempFileOnExit || gExtProtSvc->InPrivateBrowsing())
       mFinalFileDestination->SetPermissions(0400);
 
-    rv = mMimeInfo->LaunchWithFile(mFinalFileDestination);        
+    rv = mMimeInfo->LaunchWithFile(mFinalFileDestination);
     if (NS_FAILED(rv))
     {
       // Send error notification.
       nsAutoString path;
       mFinalFileDestination->GetPath(path);
       SendStatusChange(kLaunchError, rv, nsnull, path);
       Cancel(rv); // Cancel, and clean up temp file.
     }
@@ -2478,16 +2478,23 @@ NS_IMETHODIMP nsExternalAppHandler::Canc
   // We leave the partial file for everything else because it could be useful
   // e.g., resume a download
   if (mTempFile && !mReceivedDispositionInfo)
   {
     mTempFile->Remove(PR_FALSE);
     mTempFile = nsnull;
   }
 
+  // If we have already created a final destination file, we remove it as well
+  if (mFinalFileDestination)
+  {
+    mFinalFileDestination->Remove(PR_FALSE);
+    mFinalFileDestination = nsnull;
+  }
+
   // Release the listener, to break the reference cycle with it (we are the
   // observer of the listener).
   mWebProgressListener = nsnull;
 
   return NS_OK;
 }
 
 void nsExternalAppHandler::ProcessAnyRefreshTags()