From b9fe63f8b736b6d7fdb09d7ef7a66fcd4b4cd469 Mon Sep 17 00:00:00 2001 From: "darin%netscape.com" Date: Wed, 26 Mar 2003 02:27:39 +0000 Subject: [PATCH] fixes bug 199108 "Changes to cookie prefs need restart to take effect" patch=dwitte r=kaie sr=darin --- extensions/cookie/nsCookieService.cpp | 12 ++++++--- extensions/cookie/nsCookieService.h | 4 +-- extensions/cookie/nsCookies.cpp | 39 +++++++++++++-------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/extensions/cookie/nsCookieService.cpp b/extensions/cookie/nsCookieService.cpp index 95de3521fa30..56139bfb1fae 100644 --- a/extensions/cookie/nsCookieService.cpp +++ b/extensions/cookie/nsCookieService.cpp @@ -89,9 +89,7 @@ nsCookieService::~nsCookieService() // clean up memory COOKIE_RemoveAll(); - if (gCookiePrefObserver) { - delete gCookiePrefObserver; - } + NS_IF_RELEASE(gCookiePrefObserver); if (sCookieList) { delete sCookieList; } @@ -99,6 +97,8 @@ nsCookieService::~nsCookieService() nsresult nsCookieService::Init() { + nsresult rv; + // install the main cookie pref observer (defined in nsCookieService.h) // this will be integrated into nsCookieService when nsCookies is removed. gCookiePrefObserver = new nsCookiePrefObserver(); @@ -109,8 +109,12 @@ nsresult nsCookieService::Init() return NS_ERROR_OUT_OF_MEMORY; } + // init nsCookiePrefObserver, and fail if it can't get the prefbranch + NS_ADDREF(gCookiePrefObserver); + rv = gCookiePrefObserver->Init(); + if (NS_FAILED(rv)) return rv; + // cache mCookieFile - nsresult rv; rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mCookieFile)); if (NS_SUCCEEDED(rv)) { rv = mCookieFile->AppendNative(NS_LITERAL_CSTRING(kCookieFileName2)); diff --git a/extensions/cookie/nsCookieService.h b/extensions/cookie/nsCookieService.h index 2c43481ffdea..f6112af7dc5f 100644 --- a/extensions/cookie/nsCookieService.h +++ b/extensions/cookie/nsCookieService.h @@ -72,9 +72,7 @@ class nsCookiePrefObserver : public nsIObserver nsCookiePrefObserver(); virtual ~nsCookiePrefObserver(); nsresult Init(); - nsresult Install(); nsresult ReadPrefs(); - nsresult Remove(); // member variables for caching prefs #ifdef MOZ_PHOENIX @@ -115,7 +113,7 @@ public: NS_DECL_NSICOOKIESERVICE nsCookieService(); - virtual ~nsCookieService(void); + virtual ~nsCookieService(); nsresult Init(); protected: diff --git a/extensions/cookie/nsCookies.cpp b/extensions/cookie/nsCookies.cpp index beeb361ee1c4..224b16a1048f 100644 --- a/extensions/cookie/nsCookies.cpp +++ b/extensions/cookie/nsCookies.cpp @@ -125,15 +125,18 @@ static const char kCookiesStrictDomains[] = "network.cookie.strictDomains"; nsCookiePrefObserver::nsCookiePrefObserver() { - nsresult rv; +} - nsCOMPtr prefInternal; +nsresult +nsCookiePrefObserver::Init() +{ + nsresult rv; // install and cache the preferences observer mPrefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); - if (NS_SUCCEEDED(rv)) { - prefInternal = do_QueryInterface(mPrefBranch, &rv); - } + if (NS_FAILED(rv)) return rv; + + nsCOMPtr prefInternal = do_QueryInterface(mPrefBranch, &rv); // add observers if (NS_SUCCEEDED(rv)) { @@ -151,20 +154,22 @@ nsCookiePrefObserver::nsCookiePrefObserver() #endif prefInternal->AddObserver(kCookiesAskPermission, this, PR_TRUE); prefInternal->AddObserver(kCookiesStrictDomains, this, PR_TRUE); - - // initialize prefs - rv = ReadPrefs(); - if (NS_FAILED(rv)) { - NS_WARNING("Error occured reading cookie preferences"); - } } + + // initialize prefs + rv = ReadPrefs(); + if (NS_FAILED(rv)) { + NS_WARNING("Error occured reading cookie preferences"); + } + + return NS_OK; } nsCookiePrefObserver::~nsCookiePrefObserver() { } -NS_IMPL_ISUPPORTS1(nsCookiePrefObserver, nsIObserver) +NS_IMPL_ISUPPORTS2(nsCookiePrefObserver, nsIObserver, nsISupportsWeakReference) NS_IMETHODIMP nsCookiePrefObserver::Observe(nsISupports *aSubject, @@ -173,9 +178,8 @@ nsCookiePrefObserver::Observe(nsISupports *aSubject, { nsresult rv; - // check the topic, and the cached prefservice - if (!mPrefBranch || - nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic)) { + // check the topic + if (nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic)) { return NS_ERROR_FAILURE; } @@ -300,11 +304,6 @@ nsCookiePrefObserver::ReadPrefs() { nsresult rv, rv2 = NS_OK; - // check the prefservice is cached - if (!mPrefBranch) { - return NS_ERROR_FAILURE; - } - PRInt32 tempPrefValue; #ifdef MOZ_PHOENIX rv = mPrefBranch->GetBoolPref(kCookiesEnabled, &tempPrefValue);