From ff0ee4c6c948eb5c9d23230e0b71c2333d6058b8 Mon Sep 17 00:00:00 2001 From: "mscott%netscape.com" Date: Fri, 17 Dec 1999 00:42:33 +0000 Subject: [PATCH] fix circular reference between cookie notify and the cookie service. remove circular reference on the cookie service. r=morse --- extensions/cookie/nsCookieHTTPNotify.cpp | 7 ++- extensions/cookie/nsCookieHTTPNotify.h | 6 ++- extensions/cookie/nsCookieService.cpp | 61 +++--------------------- 3 files changed, 17 insertions(+), 57 deletions(-) diff --git a/extensions/cookie/nsCookieHTTPNotify.cpp b/extensions/cookie/nsCookieHTTPNotify.cpp index fdb0be48de33..adbbb17a187b 100644 --- a/extensions/cookie/nsCookieHTTPNotify.cpp +++ b/extensions/cookie/nsCookieHTTPNotify.cpp @@ -52,6 +52,7 @@ NS_COOKIE nsresult NS_NewCookieHTTPNotify(nsIHTTPNotify** aHTTPNotify) nsCookieHTTPNotify::nsCookieHTTPNotify() { NS_INIT_REFCNT(); + mCookieService = nsnull; } NS_IMETHODIMP @@ -88,7 +89,11 @@ nsCookieHTTPNotify::SetupCookieService() { nsresult rv = NS_OK; if (!mCookieService) - mCookieService = do_GetService(NS_COOKIESERVICE_PROGID, &rv); + { + nsCOMPtr cookieService = do_GetService(NS_COOKIESERVICE_PROGID, &rv); + // we want a NON-owning reference to the cookie service + mCookieService = cookieService; + } return rv; } diff --git a/extensions/cookie/nsCookieHTTPNotify.h b/extensions/cookie/nsCookieHTTPNotify.h index 7b3ed9ba2238..c2b20d524477 100644 --- a/extensions/cookie/nsCookieHTTPNotify.h +++ b/extensions/cookie/nsCookieHTTPNotify.h @@ -54,7 +54,11 @@ private: nsCOMPtr mCookieHeader; nsCOMPtr mSetCookieHeader; nsCOMPtr mExpiresHeader; - nsCOMPtr mCookieService; + + // we can't have an owning reference to the cookie service! the cookie service + // owns us so we are introducing a circular reference count! + // the good news is, if mCookieService ever goes away, we'll be going away too. + nsICookieService * mCookieService; NS_IMETHOD SetupCookieService(); }; diff --git a/extensions/cookie/nsCookieService.cpp b/extensions/cookie/nsCookieService.cpp index 42252bb0a0e6..28053e16ada5 100644 --- a/extensions/cookie/nsCookieService.cpp +++ b/extensions/cookie/nsCookieService.cpp @@ -44,9 +44,6 @@ public: // nsISupports NS_DECL_ISUPPORTS - // nsICookieService - static nsresult GetCookieService(nsICookieService** aCookieService); - NS_IMETHOD GetCookieString(nsIURI *aURL, nsString& aCookie); NS_IMETHOD SetCookieString(nsIURI *aURL, const nsString& aCookie); NS_IMETHOD SetCookieStringFromHttp(nsIURI *aURL, const char *aCookie, const char *aExpires); @@ -57,47 +54,25 @@ public: nsCookieService(); virtual ~nsCookieService(void); + nsresult Init(); protected: private: - nsIHTTPNotify *mCookieHTTPNotify; - nsresult Init(); + nsCOMPtr mCookieHTTPNotify; }; -static nsCookieService* gCookieService = nsnull; // The one-and-only CookieService - //////////////////////////////////////////////////////////////////////////////// // nsCookieService Implementation NS_IMPL_ISUPPORTS1(nsCookieService, nsICookieService); -NS_EXPORT nsresult NS_NewCookieService(nsICookieService** aCookieService) { - return nsCookieService::GetCookieService(aCookieService); -} - nsCookieService::nsCookieService() { NS_INIT_REFCNT(); - mCookieHTTPNotify = nsnull; Init(); } nsCookieService::~nsCookieService(void) { - NS_IF_RELEASE(mCookieHTTPNotify); - gCookieService = nsnull; -} - -nsresult nsCookieService::GetCookieService(nsICookieService** aCookieService) { - if (! gCookieService) { - nsCookieService* it = new nsCookieService(); - if (! it) { - return NS_ERROR_OUT_OF_MEMORY; - } - gCookieService = it; - } - NS_ADDREF(gCookieService); - *aCookieService = gCookieService; - return NS_OK; } nsresult nsCookieService::Init() { @@ -107,7 +82,7 @@ nsresult nsCookieService::Init() { rv = eventQService->CreateThreadEventQueue(); if (NS_FAILED(rv)) return rv; - if (NS_FAILED(rv = NS_NewCookieHTTPNotify(&mCookieHTTPNotify))) { + if (NS_FAILED(rv = NS_NewCookieHTTPNotify(getter_AddRefs(mCookieHTTPNotify)))) { return rv; } @@ -184,32 +159,8 @@ NS_IMETHODIMP nsCookieService::Cookie_GetPermissionListForViewer(nsString& aPerm // NOTE: This creates an instance of objects by using the default constructor // // NS_GENERIC_FACTORY_CONSTRUCTOR(nsCookieService) -static NS_IMETHODIMP -CreateNewCookieService(nsISupports* aOuter, REFNSIID aIID, void **aResult) -{ - if (!aResult) { - return NS_ERROR_INVALID_POINTER; - } - if (aOuter) { - *aResult = nsnull; - return NS_ERROR_NO_AGGREGATION; - } - nsICookieService* inst = nsnull; - nsresult rv = NS_NewCookieService(&inst); - if (NS_FAILED(rv)) { - *aResult = nsnull; - return rv; - } - if(!inst) { - return NS_ERROR_OUT_OF_MEMORY; - } - rv = inst->QueryInterface(aIID, aResult); - if (NS_FAILED(rv)) { - *aResult = nsnull; - } - NS_RELEASE(inst); /* get rid of extra refcnt */ - return rv; -} + +NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsCookieService, Init) //////////////////////////////////////////////////////////////////////// // Define a table of CIDs implemented by this module along with other @@ -218,7 +169,7 @@ CreateNewCookieService(nsISupports* aOuter, REFNSIID aIID, void **aResult) // static nsModuleComponentInfo components[] = { { "CookieService", NS_COOKIESERVICE_CID, - NS_COOKIESERVICE_PROGID, CreateNewCookieService, }, // XXX Singleton + NS_COOKIESERVICE_PROGID, nsCookieServiceConstructor, }, // XXX Singleton }; ////////////////////////////////////////////////////////////////////////