From 50871e6493388cdbc01a0e7932b0403f178f58e1 Mon Sep 17 00:00:00 2001 From: spider Date: Mon, 8 Jun 1998 19:44:54 +0000 Subject: [PATCH] Clean up Factory Creation --- shell/public/nsIShellInstance.h | 6 ++ shell/src/nsShellInstance.cpp | 109 +++++++++++++++++++++----------- shell/src/windows/winmain.cpp | 10 +-- 3 files changed, 84 insertions(+), 41 deletions(-) diff --git a/shell/public/nsIShellInstance.h b/shell/public/nsIShellInstance.h index 40bccf443bf0..cbc9e155e84e 100644 --- a/shell/public/nsIShellInstance.h +++ b/shell/public/nsIShellInstance.h @@ -32,6 +32,12 @@ class nsIApplicationShell; { 0xbf88e640, 0xdf99, 0x11d1, \ {0x92, 0x44, 0x00, 0x80, 0x5f, 0x8a, 0x7a, 0xb6} } +// 90487580-fefe-11d1-becd-00805f8a8dbd +#define NS_SHELLINSTANCE_CID \ + { 0x90487580, 0xfefe, 0x11d1, \ + {0xbe, 0xcd, 0x00, 0x80, 0x5f, 0x8a, 0x8d, 0xbd} } + + // Interface to the application shell. class nsIShellInstance : public nsISupports { public: diff --git a/shell/src/nsShellInstance.cpp b/shell/src/nsShellInstance.cpp index d51447a946bc..b82ec20081b8 100644 --- a/shell/src/nsShellInstance.cpp +++ b/shell/src/nsShellInstance.cpp @@ -33,14 +33,19 @@ static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID); +static NS_DEFINE_IID(kCShellInstance, NS_SHELLINSTANCE_CID); class nsShellInstanceFactory : public nsIFactory { public: - nsShellInstanceFactory(); + nsShellInstanceFactory(const nsCID &aClass); ~nsShellInstanceFactory(); - NS_DECL_ISUPPORTS + // nsISupports methods + NS_IMETHOD QueryInterface(const nsIID &aIID, + void **aResult); + NS_IMETHOD_(nsrefcnt) AddRef(void); + NS_IMETHOD_(nsrefcnt) Release(void); NS_IMETHOD CreateInstance(nsISupports * aOuter, const nsIID &aIID, @@ -48,6 +53,10 @@ public: NS_IMETHOD LockFactory(PRBool aLock); +private: + nsrefcnt mRefCnt; + nsCID mClassID; + }; nsShellInstance::nsShellInstance() @@ -127,6 +136,10 @@ nsresult nsShellInstance::RegisterFactories() #define WIDGET_DLL "libwidgetunix.so" #endif + + static NS_DEFINE_IID(kIWidgetIID, NS_IWIDGET_IID); + NSRepository::RegisterFactory(kIWidgetIID, WIDGET_DLL, PR_FALSE, PR_FALSE); + // register graphics classes static NS_DEFINE_IID(kCRenderingContextIID, NS_RENDERING_CONTEXT_CID); static NS_DEFINE_IID(kCDeviceContextIID, NS_DEVICE_CONTEXT_CID); @@ -230,61 +243,83 @@ nsIWidget * nsShellInstance::GetApplicationWidget() return (mApplicationWindow); } -nsShellInstanceFactory::nsShellInstanceFactory() +nsShellInstanceFactory::nsShellInstanceFactory(const nsCID &aClass) { + mRefCnt = 0; + mClassID = aClass; } nsShellInstanceFactory::~nsShellInstanceFactory() { + NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction"); } -nsresult nsShellInstanceFactory::QueryInterface(REFNSIID aIID, void** aInstancePtr) -{ - if (NULL == aInstancePtr) { - return NS_ERROR_NULL_POINTER; - } +nsresult nsShellInstanceFactory::QueryInterface(const nsIID &aIID, + void **aResult) +{ + if (aResult == NULL) { + return NS_ERROR_NULL_POINTER; + } - if (aIID.Equals(kIFactoryIID)) { - *aInstancePtr = (void*)(nsShellInstanceFactory*)this; - AddRef(); - return NS_OK; - } + // Always NULL result, in case of failure + *aResult = NULL; - if (aIID.Equals(kISupportsIID)) { - *aInstancePtr = (void*)(nsISupports*)(nsShellInstanceFactory*)this; - AddRef(); - return NS_OK; - } + if (aIID.Equals(kISupportsIID)) { + *aResult = (void *)(nsISupports*)this; + } else if (aIID.Equals(kIFactoryIID)) { + *aResult = (void *)(nsIFactory*)this; + } - return NS_NOINTERFACE; -} + if (*aResult == NULL) { + return NS_NOINTERFACE; + } -NS_IMPL_ADDREF(nsShellInstanceFactory) -NS_IMPL_RELEASE(nsShellInstanceFactory) + AddRef(); // Increase reference count for caller + return NS_OK; +} + +nsrefcnt nsShellInstanceFactory::AddRef() +{ + return ++mRefCnt; +} + +nsrefcnt nsShellInstanceFactory::Release() +{ + if (--mRefCnt == 0) { + delete this; + return 0; // Don't access mRefCnt after deleting! + } + return mRefCnt; +} nsresult nsShellInstanceFactory::CreateInstance(nsISupports * aOuter, const nsIID &aIID, void ** aResult) { - if (aResult == NULL) { - return NS_ERROR_NULL_POINTER; + if (aResult == NULL) { + return NS_ERROR_NULL_POINTER; + } + + *aResult = NULL; + + nsISupports *inst = nsnull; + + if (mClassID.Equals(kCShellInstance)) { + inst = (nsISupports *)new nsShellInstance(); } - *aResult = NULL ; - - nsISupports * inst = new nsShellInstance() ; - - if (inst == NULL) { - return NS_ERROR_OUT_OF_MEMORY; - } + if (inst == NULL) { + return NS_ERROR_OUT_OF_MEMORY; + } nsresult res = inst->QueryInterface(aIID, aResult); - if (res != NS_OK) { - delete inst ; - } + if (res != NS_OK) { + // We didn't get the right interface, so clean up + delete inst; + } - return res; + return res; } @@ -294,13 +329,13 @@ nsresult nsShellInstanceFactory::LockFactory(PRBool aLock) } // return the proper factory to the caller -extern "C" NS_WEB nsresult NSGetFactory(const nsCID &aClass, nsIFactory **aFactory) +extern "C" NS_SHELL nsresult NSGetFactory(const nsCID &aClass, nsIFactory **aFactory) { if (nsnull == aFactory) { return NS_ERROR_NULL_POINTER; } - *aFactory = new nsShellInstanceFactory(); + *aFactory = new nsShellInstanceFactory(aClass); if (nsnull == aFactory) { return NS_ERROR_OUT_OF_MEMORY; diff --git a/shell/src/windows/winmain.cpp b/shell/src/windows/winmain.cpp index 6dc0cd8b45e5..66a2734b1d9a 100644 --- a/shell/src/windows/winmain.cpp +++ b/shell/src/windows/winmain.cpp @@ -31,7 +31,9 @@ extern nsIID kIXPCOMApplicationShellCID ; static NS_DEFINE_IID(kIApplicationShellIID, NS_IAPPLICATIONSHELL_IID); static NS_DEFINE_IID(kCApplicationShellIID, NS_IAPPLICATIONSHELL_CID); -static NS_DEFINE_IID(kCShellInstanceIID, NS_ISHELLINSTANCE_IID); + +static NS_DEFINE_IID(kIShellInstanceIID, NS_ISHELLINSTANCE_IID); +static NS_DEFINE_IID(kCShellInstanceCID, NS_SHELLINSTANCE_CID); int PASCAL WinMain(HANDLE instance, HANDLE prevInstance, LPSTR cmdParam, int nCmdShow) { @@ -41,11 +43,11 @@ int PASCAL WinMain(HANDLE instance, HANDLE prevInstance, LPSTR cmdParam, int nCm nsIApplicationShell * pApplicationShell ; // Let get a ShellInstance for this Application instance - NSRepository::RegisterFactory(kCShellInstanceIID, SHELL_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kCShellInstanceCID, SHELL_DLL, PR_FALSE, PR_FALSE); - result = NSRepository::CreateInstance(kCShellInstanceIID, + result = NSRepository::CreateInstance(kCShellInstanceCID, NULL, - kCShellInstanceIID, + kIShellInstanceIID, (void **) &pShellInstance) ; if (result != NS_OK)