Bug #63105 --> clean up temp files created by helper apps on exit.

r/sr=bienvenu,sspitzer
This commit is contained in:
mscott%netscape.com 2001-07-11 03:23:24 +00:00
parent 92455ebc29
commit b8b4cb6a6d
3 changed files with 98 additions and 10 deletions

View File

@ -37,8 +37,6 @@
#include "nsCURILoader.h"
#include "nsIWebProgress.h"
#include "nsIWebProgressListener.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
// used to manage our in memory data source of helper applications
#include "nsRDFCID.h"
@ -60,6 +58,7 @@
// used for http content header disposition information.
#include "nsIHttpChannel.h"
#include "nsIAtom.h"
#include "nsIObserverService.h" // so we can be an xpcom shutdown observer
#ifdef XP_MAC
#include "nsILocalFileMac.h"
@ -125,6 +124,7 @@ NS_INTERFACE_MAP_BEGIN(nsExternalHelperAppService)
NS_INTERFACE_MAP_ENTRY(nsPIExternalAppLauncher)
NS_INTERFACE_MAP_ENTRY(nsIExternalProtocolService)
NS_INTERFACE_MAP_ENTRY(nsIMIMEService)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_END_THREADSAFE
nsExternalHelperAppService::nsExternalHelperAppService() : mDataSourceInitialized(PR_FALSE)
@ -136,6 +136,13 @@ nsExternalHelperAppService::nsExternalHelperAppService() : mDataSourceInitialize
PRInt32 hashTableSize = sizeof(defaultMimeEntries) / sizeof(defaultMimeEntries[0]);
mMimeInfoCache = new nsHashtable(hashTableSize);
AddDefaultMimeTypesToCache();
/* Add an observer to XPCOM shutdown */
nsresult rv = NS_OK;
nsCOMPtr<nsIObserverService> obs = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
if (obs)
rv = obs->AddObserver(NS_STATIC_CAST(nsIObserver*, this), NS_LITERAL_STRING(NS_XPCOM_SHUTDOWN_OBSERVER_ID).get());
}
nsExternalHelperAppService::~nsExternalHelperAppService()
@ -405,12 +412,12 @@ nsresult nsExternalHelperAppService::FillContentHandlerProperties(const char * a
if (trueString.Equals(stringValue))
aMIMEInfo->SetPreferredAction(nsIMIMEInfo::handleInternally);
// always ask
FillLiteralValueFromTarget(contentTypeHandlerNodeResource,kNC_AlwaysAsk, &stringValue);
if (trueString.Equals(stringValue))
aMIMEInfo->SetAlwaysAskBeforeHandling(PR_TRUE);
else
aMIMEInfo->SetAlwaysAskBeforeHandling(PR_FALSE);
// always ask --> these fields aren't stored in the data source anymore
//FillLiteralValueFromTarget(contentTypeHandlerNodeResource,kNC_AlwaysAsk, &stringValue);
//if (trueString.Equals(stringValue))
// aMIMEInfo->SetAlwaysAskBeforeHandling(PR_TRUE);
// else
// aMIMEInfo->SetAlwaysAskBeforeHandling(PR_FALSE);
// now digest the external application information
@ -511,6 +518,71 @@ NS_IMETHODIMP nsExternalHelperAppService::LoadUrl(nsIURI * aURL)
return NS_ERROR_NOT_IMPLEMENTED;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Methods related to deleting temporary files on exit
//////////////////////////////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP nsExternalHelperAppService::DeleteTemporaryFileOnExit(nsIFile * aTemporaryFile)
{
nsresult rv = NS_OK;
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;
if (!mTemporaryFilesList)
rv = NS_NewISupportsArray(getter_AddRefs(mTemporaryFilesList));
NS_ENSURE_SUCCESS(rv, rv);
mTemporaryFilesList->AppendElement(localFile);
return NS_OK;
}
nsresult nsExternalHelperAppService::ExpungeTemporaryFiles()
{
if (!mTemporaryFilesList) return NS_OK;
nsresult rv = NS_OK;
PRUint32 numEntries = 0;
mTemporaryFilesList->Count(&numEntries);
nsCOMPtr<nsISupports> element;
nsCOMPtr<nsILocalFile> localFile;
for (PRUint32 index = 0; index < numEntries; index ++)
{
element = getter_AddRefs(mTemporaryFilesList->ElementAt(index));
if (element)
{
localFile = do_QueryInterface(element);
if (localFile)
localFile->Delete(PR_FALSE);
}
}
mTemporaryFilesList->Clear();
return NS_OK;
}
/* XPCOM Shutdown observer */
NS_IMETHODIMP
nsExternalHelperAppService::Observe(nsISupports *aSubject, const PRUnichar *aTopic, const PRUnichar *someData )
{
// we must be shutting down xpcom so remove our temporary files...
ExpungeTemporaryFiles();
nsresult rv = NS_OK;
nsCOMPtr<nsIObserverService> obs = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
if (obs)
rv = obs->RemoveObserver(NS_STATIC_CAST(nsIObserver*, this), NS_LITERAL_STRING(NS_XPCOM_SHUTDOWN_OBSERVER_ID).get());
return NS_OK;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// begin external app handler implementation
//////////////////////////////////////////////////////////////////////////////////////////////////////
@ -924,7 +996,7 @@ NS_IMETHODIMP nsExternalAppHandler::OnStopRequest(nsIRequest *request, nsISuppor
nsresult nsExternalAppHandler::ExecuteDesiredAction()
{
nsresult rv;
nsresult rv = NS_OK;
if (mProgressWindowCreated && !mCanceled)
{
nsMIMEInfoHandleAction action = nsIMIMEInfo::saveToDisk;
@ -1109,6 +1181,9 @@ nsresult nsExternalAppHandler::OpenWithApplication(nsIFile * aApplication)
if (helperAppService)
{
rv = helperAppService->LaunchAppWithTempFile(mMimeInfo, mTempFile);
// be sure to add this temporary file to our list of files which need deleted on exit...
helperAppService->DeleteTemporaryFileOnExit(mTempFile);
}
}

View File

@ -41,13 +41,15 @@
#include "nsIRDFResource.h"
#include "nsHashtable.h"
#include "nsCOMPtr.h"
#include "nsIObserver.h"
#include "nsISupportsArray.h"
class nsExternalAppHandler;
class nsIMIMEInfo;
class nsIRDFService;
class nsExternalHelperAppService : public nsIExternalHelperAppService, public nsPIExternalAppLauncher,
public nsIExternalProtocolService, public nsIMIMEService
public nsIExternalProtocolService, public nsIMIMEService, public nsIObserver
{
public:
NS_DECL_ISUPPORTS
@ -55,6 +57,7 @@ public:
NS_DECL_NSPIEXTERNALAPPLAUNCHER
NS_DECL_NSIEXTERNALPROTOCOLSERVICE
NS_DECL_NSIMIMESERVICE
NS_DECL_NSIOBSERVER
nsExternalHelperAppService();
virtual ~nsExternalHelperAppService();
@ -110,6 +113,10 @@ protected:
virtual nsresult AddDefaultMimeTypesToCache();
virtual nsresult GetMIMEInfoForMimeTypeFromExtras(const char * aContentType, nsIMIMEInfo ** aMIMEInfo );
protected:
// functions related to the tempory file cleanup service provided by nsExternalHelperAppService
nsresult ExpungeTemporaryFiles();
nsCOMPtr<nsISupportsArray> mTemporaryFilesList;
};
// this is a small private struct used to help us initialize some

View File

@ -65,6 +65,12 @@ interface nsPIExternalAppLauncher : nsISupports
// launchAppWithTempFile --> used by the handler to actually launch the helper application
// aTempFile --> the file which contains the data we want the helper app to use when it is launched
void launchAppWithTempFile(in nsIMIMEInfo aMIMEInfo, in nsIFile aTempFile);
// mscott --> eventually I should move this into a new service so other consumers can add temporary files
// they want deleted on exit.
// aTemporaryFile --> a temporary file we should delete on exit.
void deleteTemporaryFileOnExit(in nsIFile aTemporaryFile);
};
// a helper app launcher is a small object created to handle the launching of an external application.