Bug 920573 - HTTP cache v2: Remove all cache files during shutdown when "Clear history when Firefox closes" + "Cache" setting is checked, r=honzab

This commit is contained in:
Michal Novotny 2014-04-09 10:06:59 +02:00
parent 47d3ae2f7e
commit dcb822b5ea
5 changed files with 110 additions and 4 deletions

View File

@ -1213,6 +1213,10 @@ CacheFileIOManager::Shutdown()
CacheIndex::Shutdown();
if (CacheObserver::ClearCacheOnShutdown()) {
gInstance->SyncRemoveAllCacheFiles();
}
nsRefPtr<CacheFileIOManager> ioMan;
ioMan.swap(gInstance);
@ -2854,7 +2858,9 @@ CacheFileIOManager::FindTrashDirToRemove()
nsresult rv;
MOZ_ASSERT(mIOThread->IsCurrentThread());
// We call this method on the main thread during shutdown when user wants to
// remove all cache files.
MOZ_ASSERT(mIOThread->IsCurrentThread() || mShuttingDown);
nsCOMPtr<nsISimpleEnumerator> iter;
rv = mCacheDirectory->GetDirectoryEntries(getter_AddRefs(iter));
@ -3281,6 +3287,81 @@ CacheFileIOManager::NSPRHandleUsed(CacheFileHandle *aHandle)
mHandlesByLastUsed.AppendElement(aHandle);
}
nsresult
CacheFileIOManager::SyncRemoveDir(nsIFile *aFile, const char *aDir)
{
nsresult rv;
nsCOMPtr<nsIFile> file;
if (!aDir) {
file = aFile;
} else {
rv = aFile->Clone(getter_AddRefs(file));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = file->AppendNative(nsDependentCString(aDir));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
#ifdef PR_LOGGING
nsAutoCString path;
file->GetNativePath(path);
#endif
LOG(("CacheFileIOManager::SyncRemoveDir() - Removing directory %s",
path.get()));
rv = file->Remove(true);
if (NS_WARN_IF(NS_FAILED(rv))) {
LOG(("CacheFileIOManager::SyncRemoveDir() - Removing failed! [rv=0x%08x]",
rv));
}
return rv;
}
void
CacheFileIOManager::SyncRemoveAllCacheFiles()
{
LOG(("CacheFileIOManager::SyncRemoveAllCacheFiles()"));
nsresult rv;
SyncRemoveDir(mCacheDirectory, kEntriesDir);
SyncRemoveDir(mCacheDirectory, kDoomedDir);
// Clear any intermediate state of trash dir enumeration.
mFailedTrashDirs.Clear();
mTrashDir = nullptr;
while (true) {
// FindTrashDirToRemove() fills mTrashDir if there is any trash directory.
rv = FindTrashDirToRemove();
if (rv == NS_ERROR_NOT_AVAILABLE) {
LOG(("CacheFileIOManager::SyncRemoveAllCacheFiles() - No trash directory "
"found."));
break;
}
if (NS_WARN_IF(NS_FAILED(rv))) {
LOG(("CacheFileIOManager::SyncRemoveAllCacheFiles() - "
"FindTrashDirToRemove() returned an unexpected error. [rv=0x%08x]",
rv));
break;
}
rv = SyncRemoveDir(mTrashDir, nullptr);
if (NS_FAILED(rv)) {
nsAutoCString leafName;
mTrashDir->GetNativeLeafName(leafName);
mFailedTrashDirs.AppendElement(leafName);
}
}
}
// Memory reporting
namespace { // anon

View File

@ -342,6 +342,10 @@ private:
nsresult OpenNSPRHandle(CacheFileHandle *aHandle, bool aCreate = false);
void NSPRHandleUsed(CacheFileHandle *aHandle);
// Removing all cache files during shutdown
nsresult SyncRemoveDir(nsIFile *aFile, const char *aDir);
void SyncRemoveAllCacheFiles();
nsresult ScheduleMetadataWriteInternal(CacheFile * aFile);
nsresult UnscheduleMetadataWriteInternal(CacheFile * aFile);
nsresult ShutdownMetadataWriteSchedulingInternal();

View File

@ -490,9 +490,11 @@ CacheIndex::Shutdown()
CacheIndexAutoLock lock(index);
bool sanitize = CacheObserver::ClearCacheOnShutdown();
LOG(("CacheIndex::Shutdown() - [state=%d, indexOnDiskIsValid=%d, "
"dontMarkIndexClean=%d]", index->mState, index->mIndexOnDiskIsValid,
index->mDontMarkIndexClean));
"dontMarkIndexClean=%d, sanitize=%d]", index->mState,
index->mIndexOnDiskIsValid, index->mDontMarkIndexClean, sanitize));
MOZ_ASSERT(index->mShuttingDown);
@ -510,7 +512,7 @@ CacheIndex::Shutdown()
// no break
case READY:
if (index->mIndexOnDiskIsValid && !index->mDontMarkIndexClean) {
if (NS_FAILED(index->WriteLogToDisk())) {
if (!sanitize && NS_FAILED(index->WriteLogToDisk())) {
index->RemoveIndexFromDisk();
}
} else {
@ -528,6 +530,10 @@ CacheIndex::Shutdown()
MOZ_ASSERT(false, "Unexpected state!");
}
if (sanitize) {
index->RemoveIndexFromDisk();
}
return NS_OK;
}

View File

@ -62,6 +62,12 @@ uint32_t CacheObserver::sMaxDiskEntrySize = kDefaultMaxDiskEntrySize;
static uint32_t const kDefaultCompressionLevel = 1;
uint32_t CacheObserver::sCompressionLevel = kDefaultCompressionLevel;
static bool kDefaultSanitizeOnShutdown = false;
bool CacheObserver::sSanitizeOnShutdown = kDefaultSanitizeOnShutdown;
static bool kDefaultClearCacheOnShutdown = false;
bool CacheObserver::sClearCacheOnShutdown = kDefaultClearCacheOnShutdown;
NS_IMPL_ISUPPORTS2(CacheObserver,
nsIObserver,
nsISupportsWeakReference)
@ -188,6 +194,11 @@ CacheObserver::AttachToPreferences()
"browser.cache.frecency_half_life_hours", kDefaultHalfLifeHours)));
break;
}
mozilla::Preferences::AddBoolVarCache(
&sSanitizeOnShutdown, "privacy.sanitize.sanitizeOnShutdown", kDefaultSanitizeOnShutdown);
mozilla::Preferences::AddBoolVarCache(
&sClearCacheOnShutdown, "privacy.clearOnShutdown.cache", kDefaultClearCacheOnShutdown);
}
// static

View File

@ -47,6 +47,8 @@ class CacheObserver : public nsIObserver
{ return sHalfLifeHours * 60 * 60; }
static int32_t const HalfLifeExperiment()
{ return sHalfLifeExperiment; }
static bool const ClearCacheOnShutdown()
{ return sSanitizeOnShutdown && sClearCacheOnShutdown; }
static void ParentDirOverride(nsIFile ** aDir);
static bool const EntryIsTooBig(int64_t aSize, bool aUsingDisk);
@ -69,6 +71,8 @@ private:
static uint32_t sCompressionLevel;
static uint32_t sHalfLifeHours;
static int32_t sHalfLifeExperiment;
static bool sSanitizeOnShutdown;
static bool sClearCacheOnShutdown;
// Non static properties, accessible via sSelf
nsCOMPtr<nsIFile> mCacheParentDirectoryOverride;