Wait to write out the permissions file until after 2 seconds of inactivity, to speed up deleting of losts of entries.

bug 236641, r=dwitte, sr=darin, a=dveditz
This commit is contained in:
mvl%exedo.nl 2004-03-12 18:02:38 +00:00
Родитель 9f589c5847
Коммит 12a783a50e
2 изменённых файлов: 49 добавлений и 8 удалений

Просмотреть файл

@ -180,6 +180,8 @@ static const char kPermissionsFileName[] = "hostperm.1";
static const char kOldPermissionsFileName[] = "cookperm.txt";
static const char kPermissionChangeNotification[] = PERM_CHANGE_NOTIFICATION;
static const PRUint32 kLazyWriteTimeout = 2000; //msec
NS_IMPL_ISUPPORTS3(nsPermissionManager, nsIPermissionManager, nsIObserver, nsISupportsWeakReference)
nsPermissionManager::nsPermissionManager()
@ -189,6 +191,9 @@ nsPermissionManager::nsPermissionManager()
nsPermissionManager::~nsPermissionManager()
{
if (mWriteTimer)
mWriteTimer->Cancel();
RemoveTypeStrings();
RemoveAllFromMemory();
}
@ -244,7 +249,7 @@ nsPermissionManager::Add(nsIURI *aURI,
if (NS_FAILED(rv)) return rv;
mChangedList = PR_TRUE;
Write();
LazyWrite();
return NS_OK;
}
@ -334,7 +339,7 @@ nsPermissionManager::Remove(const nsACString &aHost,
--mHostCount;
}
mChangedList = PR_TRUE;
Write();
LazyWrite();
// Notify Observers
if (oldPermission != nsIPermissionManager::UNKNOWN_ACTION)
@ -351,7 +356,7 @@ nsPermissionManager::RemoveAll()
{
RemoveAllFromMemory();
NotifyObservers(nsnull, NS_LITERAL_STRING("cleared").get());
Write();
LazyWrite();
return NS_OK;
}
@ -456,6 +461,11 @@ NS_IMETHODIMP nsPermissionManager::Observe(nsISupports *aSubject, const char *aT
if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
// The profile is about to change.
if (mWriteTimer) {
mWriteTimer->Cancel();
mWriteTimer = 0;
}
// Dump current permission. This will be done by calling
// RemoveAllFromMemory which clears the memory-resident
@ -465,13 +475,15 @@ NS_IMETHODIMP nsPermissionManager::Observe(nsISupports *aSubject, const char *aT
// was accepted). If this condition ever changes, the permission
// file would need to be updated here.
RemoveTypeStrings();
RemoveAllFromMemory();
if (!nsCRT::strcmp(someData, NS_LITERAL_STRING("shutdown-cleanse").get()))
if (!nsCRT::strcmp(someData, NS_LITERAL_STRING("shutdown-cleanse").get())) {
if (mPermissionsFile) {
mPermissionsFile->Remove(PR_FALSE);
}
} else {
Write();
}
RemoveTypeStrings();
RemoveAllFromMemory();
}
else if (!nsCRT::strcmp(aTopic, "profile-do-change")) {
// The profile has aleady changed.
@ -790,6 +802,28 @@ AddEntryToList(nsHostEntry *entry, void *arg)
return PL_DHASH_NEXT;
}
void
nsPermissionManager::LazyWrite()
{
if (mWriteTimer) {
mWriteTimer->SetDelay(kLazyWriteTimeout);
} else {
mWriteTimer = do_CreateInstance("@mozilla.org/timer;1");
if (mWriteTimer) {
mWriteTimer->InitWithFuncCallback(DoLazyWrite, this, kLazyWriteTimeout,
nsITimer::TYPE_ONE_SHOT);
}
}
}
void
nsPermissionManager::DoLazyWrite(nsITimer *aTimer,
void *aClosure)
{
nsPermissionManager *service = NS_REINTERPRET_CAST(nsPermissionManager*, aClosure);
service->Write();
service->mWriteTimer = 0;
}
nsresult
nsPermissionManager::Write()

Просмотреть файл

@ -48,6 +48,7 @@
#include "nsIFile.h"
#include "nsTHashtable.h"
#include "nsString.h"
#include "nsITimer.h"
class nsIPermission;
@ -171,8 +172,13 @@ private:
nsHostEntry *GetHostEntry(const nsAFlatCString &aHost,
PRUint32 aType);
// Use LazyWrite to save the permissions file on a timer. It will write
// the file only once if repeatedly hammered quickly.
void LazyWrite();
static void DoLazyWrite(nsITimer *aTimer, void *aClosure);
nsresult Write();
nsresult Read();
nsresult Write();
void NotifyObserversWithPermission(const nsACString &aHost,
const char *aType,
PRUint32 aPermission,
@ -184,6 +190,7 @@ private:
nsCOMPtr<nsIObserverService> mObserverService;
nsCOMPtr<nsIFile> mPermissionsFile;
nsCOMPtr<nsITimer> mWriteTimer;
nsTHashtable<nsHostEntry> mHostTable;
PRUint32 mHostCount;
PRPackedBool mChangedList;