Bug 410150 - "nsCacheService: only add observers that we really need" (Use array/for loops for the Add/Remove observers) [p=alfredkayser@gmail.com (Alfred Kayser) r=dcamp sr=biesi a1.9=damons]

This commit is contained in:
reed%reedloden.com 2008-02-10 04:53:28 +00:00
Родитель f99f6c6530
Коммит 545a0b1293
1 изменённых файлов: 45 добавлений и 46 удалений

89
netwerk/cache/src/nsCacheService.cpp поставляемый
Просмотреть файл

@ -91,6 +91,25 @@
#define MEMORY_CACHE_CAPACITY_PREF "browser.cache.memory.capacity" #define MEMORY_CACHE_CAPACITY_PREF "browser.cache.memory.capacity"
#define MEMORY_CACHE_MAX_ENTRY_SIZE_PREF "browser.cache.memory.max_entry_size" #define MEMORY_CACHE_MAX_ENTRY_SIZE_PREF "browser.cache.memory.max_entry_size"
static const char * observerList[] = {
"profile-before-change",
"profile-after-change",
NS_XPCOM_SHUTDOWN_OBSERVER_ID
};
static const char * prefList[] = {
#ifdef NECKO_DISK_CACHE
DISK_CACHE_ENABLE_PREF,
DISK_CACHE_CAPACITY_PREF,
DISK_CACHE_DIR_PREF,
#endif
#ifdef NECKO_OFFLINE_CACHE
OFFLINE_CACHE_ENABLE_PREF,
OFFLINE_CACHE_CAPACITY_PREF,
OFFLINE_CACHE_DIR_PREF,
#endif
MEMORY_CACHE_ENABLE_PREF,
MEMORY_CACHE_CAPACITY_PREF
};
class nsCacheProfilePrefObserver : public nsIObserver class nsCacheProfilePrefObserver : public nsIObserver
{ {
@ -151,40 +170,24 @@ nsCacheProfilePrefObserver::Install()
// install profile-change observer // install profile-change observer
nsCOMPtr<nsIObserverService> observerService = do_GetService("@mozilla.org/observer-service;1", &rv); nsCOMPtr<nsIObserverService> observerService = do_GetService("@mozilla.org/observer-service;1", &rv);
if (NS_FAILED(rv)) return rv; NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_ARG(observerService); NS_ENSURE_ARG(observerService);
rv = observerService->AddObserver(this, "profile-before-change", PR_FALSE); for (int i=0; i<NS_ARRAY_LENGTH(observerList); i++) {
if (NS_FAILED(rv)) rv2 = rv; rv = observerService->AddObserver(this, observerList[i], PR_FALSE);
if (NS_FAILED(rv))
rv = observerService->AddObserver(this, "profile-after-change", PR_FALSE); rv2 = rv;
if (NS_FAILED(rv)) rv2 = rv; }
// install xpcom shutdown observer
rv = observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
if (NS_FAILED(rv)) rv2 = rv;
// install preferences observer // install preferences observer
nsCOMPtr<nsIPrefBranch2> branch = do_GetService(NS_PREFSERVICE_CONTRACTID); nsCOMPtr<nsIPrefBranch2> branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
if (!branch) return NS_ERROR_FAILURE; if (!branch) return NS_ERROR_FAILURE;
char * prefList[] = { for (int i=0; i<NS_ARRAY_LENGTH(prefList); i++) {
DISK_CACHE_ENABLE_PREF,
DISK_CACHE_CAPACITY_PREF,
DISK_CACHE_DIR_PREF,
OFFLINE_CACHE_ENABLE_PREF,
OFFLINE_CACHE_CAPACITY_PREF,
OFFLINE_CACHE_DIR_PREF,
MEMORY_CACHE_ENABLE_PREF,
MEMORY_CACHE_CAPACITY_PREF
};
int listCount = NS_ARRAY_LENGTH(prefList);
for (int i=0; i<listCount; i++) {
rv = branch->AddObserver(prefList[i], this, PR_FALSE); rv = branch->AddObserver(prefList[i], this, PR_FALSE);
if (NS_FAILED(rv)) rv2 = rv; if (NS_FAILED(rv))
rv2 = rv;
} }
// Determine if we have a profile already // Determine if we have a profile already
@ -202,8 +205,9 @@ nsCacheProfilePrefObserver::Install()
} }
rv = ReadPrefs(branch); rv = ReadPrefs(branch);
NS_ENSURE_SUCCESS(rv, rv);
return NS_SUCCEEDED(rv) ? rv2 : rv; return rv2;
} }
@ -214,28 +218,19 @@ nsCacheProfilePrefObserver::Remove()
nsCOMPtr<nsIObserverService> obs = nsCOMPtr<nsIObserverService> obs =
do_GetService("@mozilla.org/observer-service;1"); do_GetService("@mozilla.org/observer-service;1");
if (obs) { if (obs) {
obs->RemoveObserver(this, "profile-before-change"); for (int i=0; i<NS_ARRAY_LENGTH(observerList); i++) {
obs->RemoveObserver(this, "profile-after-change"); obs->RemoveObserver(this, observerList[i]);
obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID); }
} }
// remove Pref Service observers // remove Pref Service observers
nsCOMPtr<nsIPrefBranch2> prefs = nsCOMPtr<nsIPrefBranch2> prefs =
do_GetService(NS_PREFSERVICE_CONTRACTID); do_GetService(NS_PREFSERVICE_CONTRACTID);
if (prefs) { if (prefs) {
// remove Disk cache pref observers for (int i=0; i<NS_ARRAY_LENGTH(prefList); i++) {
prefs->RemoveObserver(DISK_CACHE_ENABLE_PREF, this); // remove cache pref observers
prefs->RemoveObserver(DISK_CACHE_CAPACITY_PREF, this); prefs->RemoveObserver(prefList[i], this);
prefs->RemoveObserver(DISK_CACHE_DIR_PREF, this); }
// remove Offline cache pref observers
prefs->RemoveObserver(OFFLINE_CACHE_ENABLE_PREF, this);
prefs->RemoveObserver(OFFLINE_CACHE_CAPACITY_PREF, this);
prefs->RemoveObserver(OFFLINE_CACHE_DIR_PREF, this);
// remove Memory cache pref observers
prefs->RemoveObserver(MEMORY_CACHE_ENABLE_PREF, this);
prefs->RemoveObserver(MEMORY_CACHE_CAPACITY_PREF, this);
} }
} }
@ -301,7 +296,9 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
// XXX the next time the profile changes (browser launch) // XXX the next time the profile changes (browser launch)
#endif #endif
} else } else
#endif // !NECKO_DISK_CACHE
#ifdef NECKO_OFFLINE_CACHE
// which preference changed? // which preference changed?
if (!strcmp(OFFLINE_CACHE_ENABLE_PREF, data.get())) { if (!strcmp(OFFLINE_CACHE_ENABLE_PREF, data.get())) {
@ -318,14 +315,14 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
mOfflineCacheCapacity = PR_MAX(0, capacity); mOfflineCacheCapacity = PR_MAX(0, capacity);
nsCacheService::SetOfflineCacheCapacity(mOfflineCacheCapacity); nsCacheService::SetOfflineCacheCapacity(mOfflineCacheCapacity);
#if 0 #if 0
} else if (!strcmp(DISK_OFFLINE_DIR_PREF, data.get())) { } else if (!strcmp(OFFLINE_CACHE_DIR_PREF, data.get())) {
// XXX We probaby don't want to respond to this pref except after // XXX We probaby don't want to respond to this pref except after
// XXX profile changes. Ideally, there should be some kind of user // XXX profile changes. Ideally, there should be some kind of user
// XXX notification that the pref change won't take effect until // XXX notification that the pref change won't take effect until
// XXX the next time the profile changes (browser launch) // XXX the next time the profile changes (browser launch)
#endif #endif
} else } else
#endif // !NECKO_DISK_CACHE #endif // !NECKO_OFFLINE_CACHE
if (!strcmp(MEMORY_CACHE_ENABLE_PREF, data.get())) { if (!strcmp(MEMORY_CACHE_ENABLE_PREF, data.get())) {
@ -404,7 +401,9 @@ nsCacheProfilePrefObserver::ReadPrefs(nsIPrefBranch* branch)
if (directory) if (directory)
mDiskCacheParentDirectory = do_QueryInterface(directory, &rv); mDiskCacheParentDirectory = do_QueryInterface(directory, &rv);
} }
#endif // !NECKO_DISK_CACHE
#ifdef NECKO_OFFLINE_CACHE
// read offline cache device prefs // read offline cache device prefs
mOfflineCacheEnabled = PR_TRUE; // presume offline cache is enabled mOfflineCacheEnabled = PR_TRUE; // presume offline cache is enabled
(void) branch->GetBoolPref(OFFLINE_CACHE_ENABLE_PREF, (void) branch->GetBoolPref(OFFLINE_CACHE_ENABLE_PREF,
@ -445,7 +444,7 @@ nsCacheProfilePrefObserver::ReadPrefs(nsIPrefBranch* branch)
if (directory) if (directory)
mOfflineCacheParentDirectory = do_QueryInterface(directory, &rv); mOfflineCacheParentDirectory = do_QueryInterface(directory, &rv);
} }
#endif // !NECKO_DISK_CACHE #endif // !NECKO_OFFLINE_CACHE
// read memory cache device prefs // read memory cache device prefs
(void) branch->GetBoolPref(MEMORY_CACHE_ENABLE_PREF, &mMemoryCacheEnabled); (void) branch->GetBoolPref(MEMORY_CACHE_ENABLE_PREF, &mMemoryCacheEnabled);