Bug 460609 - Temporary files for helper applications are not deleted when leaving Private Browsing mode; r,sr=bzbarsky a=blocking1.9.1
authorEhsan Akhgari <ehsan.akhgari@gmail.com>
Fri, 14 Nov 2008 02:08:28 +0330
changeset 21635 5c289d7b5f17808a110c5babdf7dd3723751f4d8
parent 21634 a0f72d018c4ae03799f0f2e1a5d105d8bebc7147
child 21636 55f801c977646fd89c65ebd33b3d93be5deabf86
push id3626
push userehsan.akhgari@gmail.com
push dateThu, 13 Nov 2008 22:39:22 +0000
treeherdermozilla-central@5c289d7b5f17 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky, blocking1.9.1
bugs460609
milestone1.9.1b2pre
Bug 460609 - Temporary files for helper applications are not deleted when leaving Private Browsing mode; r,sr=bzbarsky a=blocking1.9.1
uriloader/exthandler/nsExternalHelperAppService.cpp
uriloader/exthandler/nsExternalHelperAppService.h
--- a/uriloader/exthandler/nsExternalHelperAppService.cpp
+++ b/uriloader/exthandler/nsExternalHelperAppService.cpp
@@ -21,16 +21,17 @@
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Scott MacGregor <mscott@netscape.com>
  *   Bill Law <law@netscape.com>
  *   Christian Biesinger <cbiesinger@web.de>
  *   Dan Mosedale <dmose@mozilla.org>
  *   Myk Melez <myk@mozilla.org>
+ *   Ehsan Akhgari <ehsan.akhgari@gmail.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either of 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
@@ -119,16 +120,18 @@
 #include "nsCRT.h"
 
 #include "nsLocalHandlerApp.h"
 
 #include "nsIRandomGenerator.h"
 #include "plbase64.h"
 #include "prmem.h"
 
+#include "nsIPrivateBrowsingService.h"
+
 // Buffer file writes in 32kb chunks
 #define BUFFERED_OUTPUT_SIZE (1024 * 32)
 
 // Download Folder location constants
 #define NS_PREF_DOWNLOAD_DIR        "browser.download.dir"
 #define NS_PREF_DOWNLOAD_FOLDERLIST "browser.download.folderList"
 enum {
   NS_FOLDER_VALUE_DESKTOP = 0
@@ -559,36 +562,45 @@ NS_IMPL_ISUPPORTS6(
   nsExternalHelperAppService,
   nsIExternalHelperAppService,
   nsPIExternalAppLauncher,
   nsIExternalProtocolService,
   nsIMIMEService,
   nsIObserver,
   nsISupportsWeakReference)
 
-nsExternalHelperAppService::nsExternalHelperAppService()
+nsExternalHelperAppService::nsExternalHelperAppService() :
+  mInPrivateBrowsing(PR_FALSE)
 {
   gExtProtSvc = this;
 }
 nsresult nsExternalHelperAppService::Init()
 {
+  nsCOMPtr<nsIPrivateBrowsingService> pbs =
+    do_GetService(NS_PRIVATE_BROWSING_SERVICE_CONTRACTID);
+  if (pbs) {
+    pbs->GetPrivateBrowsingEnabled(&mInPrivateBrowsing);
+  }
+
   // Add an observer for profile change
   nsresult rv = NS_OK;
   nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1", &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
 #ifdef PR_LOGGING
   if (!mLog) {
     mLog = PR_NewLogModule("HelperAppService");
     if (!mLog)
       return NS_ERROR_OUT_OF_MEMORY;
   }
 #endif
 
-  return obs->AddObserver(this, "profile-before-change", PR_TRUE);
+  rv = obs->AddObserver(this, "profile-before-change", PR_TRUE);
+  NS_ENSURE_SUCCESS(rv, rv);
+  return obs->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, PR_TRUE);
 }
 
 nsExternalHelperAppService::~nsExternalHelperAppService()
 {
   gExtProtSvc = nsnull;
 }
 
 NS_IMETHODIMP nsExternalHelperAppService::DoContent(const nsACString& aMimeContentType,
@@ -922,43 +934,54 @@ NS_IMETHODIMP nsExternalHelperAppService
   PRBool isFile = PR_FALSE;
   nsCOMPtr<nsILocalFile> localFile (do_QueryInterface(aTemporaryFile, &rv));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // as a safety measure, make sure the nsIFile is really a file and not a directory object.
   localFile->IsFile(&isFile);
   if (!isFile) return NS_OK;
 
-  mTemporaryFilesList.AppendObject(localFile);
+  if (mInPrivateBrowsing)
+    mTemporaryPrivateFilesList.AppendObject(localFile);
+  else
+    mTemporaryFilesList.AppendObject(localFile);
 
   return NS_OK;
 }
 
 void nsExternalHelperAppService::FixFilePermissions(nsILocalFile* aFile)
 {
   // This space intentionally left blank
 }
 
-nsresult nsExternalHelperAppService::ExpungeTemporaryFiles()
+void nsExternalHelperAppService::ExpungeTemporaryFilesHelper(nsCOMArray<nsILocalFile> &fileList)
 {
-  PRInt32 numEntries = mTemporaryFilesList.Count();
+  PRInt32 numEntries = fileList.Count();
   nsILocalFile* localFile;
   for (PRInt32 index = 0; index < numEntries; index++)
   {
-    localFile = mTemporaryFilesList[index];
+    localFile = fileList[index];
     if (localFile) {
       // First make the file writable, since the temp file is probably readonly.
       localFile->SetPermissions(0600);
       localFile->Remove(PR_FALSE);
     }
   }
 
-  mTemporaryFilesList.Clear();
+  fileList.Clear();
+}
 
-  return NS_OK;
+void nsExternalHelperAppService::ExpungeTemporaryFiles()
+{
+  ExpungeTemporaryFilesHelper(mTemporaryFilesList);
+}
+
+void nsExternalHelperAppService::ExpungeTemporaryPrivateFiles()
+{
+  ExpungeTemporaryFilesHelper(mTemporaryPrivateFilesList);
 }
 
 static const char kExternalWarningPrefPrefix[] = 
   "network.protocol-handler.warn-external.";
 static const char kExternalWarningDefaultPref[] = 
   "network.protocol-handler.warn-external-default";
 
 NS_IMETHODIMP
@@ -1037,16 +1060,23 @@ nsExternalHelperAppService::SetProtocolH
 }
  
 // XPCOM profile change observer
 NS_IMETHODIMP
 nsExternalHelperAppService::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData )
 {
   if (!strcmp(aTopic, "profile-before-change")) {
     ExpungeTemporaryFiles();
+  } else if (!strcmp(aTopic, NS_PRIVATE_BROWSING_SWITCH_TOPIC)) {
+    if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_ENTER).Equals(someData))
+      mInPrivateBrowsing = PR_TRUE;
+    else if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_LEAVE).Equals(someData)) {
+      mInPrivateBrowsing = PR_FALSE;
+      ExpungeTemporaryPrivateFiles();
+    }
   }
   return NS_OK;
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////
 // begin external app handler implementation 
 //////////////////////////////////////////////////////////////////////////////////////////////////////
 
--- a/uriloader/exthandler/nsExternalHelperAppService.h
+++ b/uriloader/exthandler/nsExternalHelperAppService.h
@@ -19,16 +19,17 @@
  * Portions created by the Initial Developer are Copyright (C) 1999
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Scott MacGregor <mscott@netscape.com>
  *   Christian Biesinger <cbiesinger@web.de>
  *   Dan Mosedale <dmose@mozilla.org>
  *   Myk Melez <myk@mozilla.org>
+ *   Ehsan Akhgari <ehsan.akhgari@gmail.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either of 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
@@ -196,24 +197,43 @@ protected:
   static PRLogModuleInfo* mLog;
 
 #endif
   // friend, so that it can access the nspr log module and FixFilePermissions
   friend class nsExternalAppHandler;
   friend class nsExternalLoadRequest;
 
   /**
+   * Helper function for ExpungeTemporaryFiles and ExpungeTemporaryPrivateFiles
+   */
+  static void ExpungeTemporaryFilesHelper(nsCOMArray<nsILocalFile> &fileList);
+  /**
    * Functions related to the tempory file cleanup service provided by
    * nsExternalHelperAppService
    */
-  NS_HIDDEN_(nsresult) ExpungeTemporaryFiles();
+  void ExpungeTemporaryFiles();
+  /**
+   * Functions related to the tempory file cleanup service provided by
+   * nsExternalHelperAppService (for the temporary files added during
+   * the private browsing mode)
+   */
+  void ExpungeTemporaryPrivateFiles();
   /**
    * Array for the files that should be deleted
    */
   nsCOMArray<nsILocalFile> mTemporaryFilesList;
+  /**
+   * Array for the files that should be deleted (for the temporary files
+   * added during the private browsing mode)
+   */
+  nsCOMArray<nsILocalFile> mTemporaryPrivateFilesList;
+  /**
+   * Whether we are in private browsing mode
+   */
+  PRBool mInPrivateBrowsing;
 };
 
 /**
  * We need to read the data out of the incoming stream into a buffer which we
  * can then use to write the data into the output stream representing the
  * temp file.
  */
 #define DATA_BUFFER_SIZE (4096*2)