make the NS_GetFactory stuff use generic factory code instead of doing it all by hand r=dp

This commit is contained in:
pavlov%netscape.com 2000-03-16 23:42:23 +00:00
parent 03c290f510
commit d5ba4ef60f
8 changed files with 78 additions and 459 deletions

@ -30,17 +30,44 @@
//////////////////////////////////////////////////////////////////////
// GIF Decoder Implementation
NS_IMPL_ISUPPORTS(GIFDecoder, NS_GET_IID(nsIImgDecoder));
NS_IMPL_ISUPPORTS1(GIFDecoder, nsIImgDecoder);
GIFDecoder::GIFDecoder(il_container* aContainer)
{
NS_INIT_REFCNT();
ilContainer = aContainer;
};
}
GIFDecoder::~GIFDecoder(void)
{
};
}
NS_METHOD
GIFDecoder::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
nsresult rv;
if (aOuter) return NS_ERROR_NO_AGGREGATION;
il_container *ic = new il_container();
if (!ic) return NS_ERROR_OUT_OF_MEMORY;
GIFDecoder *decoder = new GIFDecoder(ic);
if (!decoder) {
delete ic;
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(decoder);
rv = decoder->QueryInterface(aIID, aResult);
NS_RELEASE(decoder);
/* why are we creating and destroying this object for no reason? */
delete ic; /* is a place holder */
return rv;
}
NS_IMETHODIMP
GIFDecoder::ImgDInit()

@ -1,4 +1,4 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
@ -45,6 +45,8 @@ public:
NS_DECL_ISUPPORTS
static NS_METHOD Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
/* stream */
NS_IMETHOD ImgDInit();

@ -18,10 +18,8 @@
* Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
*/
#include "nsGIFModule.h"
#include "nsGIFDecoder.h"
#include "nsIComponentManager.h"
#include "nsIGenericFactory.h"
@ -29,186 +27,14 @@
#include "nsCOMPtr.h"
static NS_DEFINE_CID(kGIFDecoderCID, NS_GIFDECODER_CID);
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
//////////////////////////////////////////////////////////////////////
// Module Global
//
// This is used by the module to count object. Constructors and
// destructors of objects created by this module need to
// inc/dec the object count for the module. Unload decision will
// be taken based on this.
//
// Constructor:
// gModule->IncrementObjCount();
//
// Descructor:
// gModule->DecrementObjCount();
//
// WARNING: This is weak reference. XPCOM guarantees that this module
// object will be deleted just before the dll is unloaded. Hence,
// holding a weak reference is ok.
static nsGIFModule *gModule = NULL;
//////////////////////////////////////////////////////////////////////
// Module entry point
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager *servMgr,
nsIFile* aPath,
nsIModule** return_cobj)
static nsModuleComponentInfo components[] =
{
nsGIFModule *gifModule;
nsresult rv = NS_OK;
{ "GIF Decoder",
NS_GIFDECODER_CID,
"component://netscape/image/decoder&type=image/gif",
GIFDecoder::Create }
};
NS_ASSERTION(return_cobj, "Null argument");
NS_ASSERTION(gModule == NULL, "nsGIFModule: Module already created.");
NS_IMPL_NSGETMODULE("nsGIFModule", components)
gifModule = new nsGIFModule;
if (gifModule == NULL) return NS_ERROR_OUT_OF_MEMORY;
rv = gifModule->QueryInterface(NS_GET_IID(nsIModule), (void **)return_cobj);
if (NS_FAILED(rv))
{
delete gifModule;
gifModule = NULL;
}
// WARNING: Weak Reference
gModule = gifModule;
return rv;
}
//////////////////////////////////////////////////////////////////////
// GIF Decoder Factory using nsIGenericFactory
static NS_IMETHODIMP
nsGIFDecoderCreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
GIFDecoder *gifdec = NULL;
*aResult = NULL;
il_container* ic = NULL;
if (aOuter && !aIID.Equals(kISupportsIID))
return NS_NOINTERFACE;
ic = new il_container();
if (!ic)
{
return NS_ERROR_OUT_OF_MEMORY;
}
gifdec = new GIFDecoder(ic);
if (!gifdec)
{
delete ic;
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult res = gifdec->QueryInterface(aIID, aResult);
if (NS_FAILED(res))
{
*aResult = NULL;
delete gifdec;
}
delete ic; /* is a place holder */
return res;
}
//////////////////////////////////////////////////////////////////////
// GIF Decoder Module Implementation
NS_IMPL_ISUPPORTS(nsGIFModule, NS_GET_IID(nsIModule))
nsGIFModule::nsGIFModule(void)
: mObjCount(-1), mClassObject(NULL)
{
NS_INIT_ISUPPORTS();
}
nsGIFModule::~nsGIFModule(void)
{
NS_ASSERTION(mObjCount <= 0, "Module released while having outstanding objects.");
if (mClassObject)
{
int refcnt;
NS_RELEASE2(mClassObject, refcnt);
NS_ASSERTION(refcnt == 0, "nsGIFModule::~nsGIFModule() ClassObject refcount nonzero");
}
}
//
// The class object for us is just the factory and nothing more.
//
NS_IMETHODIMP
nsGIFModule::GetClassObject(nsIComponentManager *aCompMgr, const nsCID & aClass,
const nsIID &aIID, void **r_classObj)
{
nsresult rv;
if( !aClass.Equals(kGIFDecoderCID))
return NS_ERROR_FACTORY_NOT_REGISTERED;
// If this aint the first time, return the cached class object
if (mClassObject == NULL)
{
nsCOMPtr<nsIGenericFactory> fact;
rv = NS_NewGenericFactory(getter_AddRefs(fact), nsGIFDecoderCreateInstance);
if (NS_FAILED(rv)) return rv;
// Store the class object in our global
rv = fact->QueryInterface(NS_GET_IID(nsISupports), (void **)&mClassObject);
if (NS_FAILED(rv)) return rv;
}
rv = mClassObject->QueryInterface(aIID, r_classObj);
return rv;
}
NS_IMETHODIMP
nsGIFModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFile* aPath,
const char *registryLocation,
const char *componentType)
{
nsresult rv;
rv = aCompMgr->RegisterComponentSpec(kGIFDecoderCID,
"Netscape GIFDec",
"component://netscape/image/decoder&type=image/gif",
aPath, PR_TRUE, PR_TRUE);
return rv;
}
NS_IMETHODIMP
nsGIFModule::UnregisterSelf(nsIComponentManager *aCompMgr,
nsIFile* aPath,
const char *registryLocation)
{
nsresult rv;
rv = aCompMgr->UnregisterComponentSpec(kGIFDecoderCID, aPath);
return rv;
}
NS_IMETHODIMP
nsGIFModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (mObjCount < 0)
{
// Dll doesn't count objects.
return NS_ERROR_FAILURE;
}
PRBool unloadable = PR_FALSE;
if (mObjCount == 0)
{
// This dll can be unloaded now
unloadable = PR_TRUE;
}
if (okToUnload) *okToUnload = unloadable;
return NS_OK;
}
//////////////////////////////////////////////////////////////////////

@ -1,45 +0,0 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#include "nsIModule.h"
class nsGIFModule : public nsIModule
{
public:
NS_DECL_ISUPPORTS
// Construction, Init and desstruction
nsGIFModule();
virtual ~nsGIFModule();
// nsIModule Interfaces
NS_DECL_NSIMODULE
// Facility for counting object instances
int IncrementObjCount() { if (mObjCount == -1) mObjCount = 0; return ++mObjCount; }
int DecrementObjCount() { if (mObjCount == -1) mObjCount = 0; return --mObjCount; }
int GetObjCount() { return mObjCount; }
private:
int mObjCount;
nsISupports * mClassObject;
};

@ -42,7 +42,35 @@ PNGDecoder::~PNGDecoder(void)
{
};
NS_IMPL_ISUPPORTS(PNGDecoder, NS_GET_IID(nsIImgDecoder))
NS_IMPL_ISUPPORTS1(PNGDecoder, nsIImgDecoder)
NS_METHOD
PNGDecoder::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
nsresult rv;
if (aOuter) return NS_ERROR_NO_AGGREGATION;
il_container *ic = new il_container();
if (!ic) return NS_ERROR_OUT_OF_MEMORY;
PNGDecoder *decoder = new PNGDecoder(ic);
if (!decoder) {
delete ic;
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(decoder);
rv = decoder->QueryInterface(aIID, aResult);
NS_RELEASE(decoder);
/* why are we creating and destroying this object for no reason? */
delete ic; /* is a place holder */
return rv;
}
/*----------------------------------------------------*/
// api functions

@ -37,6 +37,8 @@ public:
NS_DECL_ISUPPORTS
static NS_METHOD Create(nsISupports *aOuter, const nsIID &aIID, void **aResult);
/* stream */
NS_IMETHOD ImgDInit();

@ -18,10 +18,8 @@
* Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
*/
#include "nsPNGModule.h"
#include "nsPNGDecoder.h"
#include "nsIComponentManager.h"
#include "nsIGenericFactory.h"
@ -29,184 +27,14 @@
#include "nsCOMPtr.h"
static NS_DEFINE_CID(kPNGDecoderCID, NS_PNGDECODER_CID);
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
//////////////////////////////////////////////////////////////////////
// Module Global
//
// This is used by the module to count object. Constructors and
// destructors of objects created by this module need to
// inc/dec the object count for the module. Unload decision will
// be taken based on this.
//
// Constructor:
// gModule->IncrementObjCount();
//
// Descructor:
// gModule->DecrementObjCount();
//
// WARNING: This is weak reference. XPCOM guarantees that this module
// object will be deleted just before the dll is unloaded. Hence,
// holding a weak reference is ok.
static nsPNGModule *gModule = NULL;
//////////////////////////////////////////////////////////////////////
// Module entry point
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager *servMgr,
nsIFile* aPath,
nsIModule** return_cobj)
static nsModuleComponentInfo components[] =
{
nsPNGModule *module;
nsresult rv = NS_OK;
{ "PNG Decoder",
NS_PNGDECODER_CID,
"component://netscape/image/decoder&type=image/png",
PNGDecoder::Create }
};
NS_ASSERTION(return_cobj, "Null argument");
NS_ASSERTION(gModule == NULL, "nsPNGModule: Module already created.");
NS_IMPL_NSGETMODULE("nsPNGModule", components)
module = new nsPNGModule;
if (module == NULL) return NS_ERROR_OUT_OF_MEMORY;
rv = module->QueryInterface(NS_GET_IID(nsIModule), (void **)return_cobj);
if (NS_FAILED(rv))
{
delete module;
module = NULL;
}
// WARNING: Weak Reference
gModule = module;
return rv;
}
//////////////////////////////////////////////////////////////////////
// PNG Decoder Factory using nsIGenericFactory
static NS_IMETHODIMP
nsPNGDecoderCreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
PNGDecoder *dec = NULL;
*aResult = NULL;
il_container* ic = NULL;
if (aOuter && !aIID.Equals(kISupportsIID))
return NS_NOINTERFACE;
ic = new il_container();
if (!ic)
{
return NS_ERROR_OUT_OF_MEMORY;
}
dec = new PNGDecoder(ic);
if (!dec)
{
delete ic;
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult res = dec->QueryInterface(aIID, aResult);
if (NS_FAILED(res))
{
*aResult = NULL;
delete dec;
}
delete ic; /* is a place holder */
return res;
}
//////////////////////////////////////////////////////////////////////
// PNG Decoder Module Implementation
NS_IMPL_ISUPPORTS(nsPNGModule, NS_GET_IID(nsIModule))
nsPNGModule::nsPNGModule(void)
: mObjCount(-1), mClassObject(NULL)
{
NS_INIT_ISUPPORTS();
}
nsPNGModule::~nsPNGModule(void)
{
NS_ASSERTION(mObjCount <= 0, "Module released while having outstanding objects.");
if (mClassObject)
{
int refcnt;
NS_RELEASE2(mClassObject, refcnt);
NS_ASSERTION(refcnt == 0, "nsPNGModule::~nsPNGModule() ClassObject refcount nonzero");
}
}
//
// The class object for us is just the factory and nothing more.
//
NS_IMETHODIMP
nsPNGModule::GetClassObject(nsIComponentManager *aCompMgr, const nsCID & aClass,
const nsIID &aIID, void **r_classObj)
{
nsresult rv;
if( !aClass.Equals(kPNGDecoderCID))
return NS_ERROR_FACTORY_NOT_REGISTERED;
// If this aint the first time, return the cached class object
if (mClassObject == NULL)
{
nsCOMPtr<nsIGenericFactory> fact;
rv = NS_NewGenericFactory(getter_AddRefs(fact), nsPNGDecoderCreateInstance);
if (NS_FAILED(rv)) return rv;
// Store the class object in our global
rv = fact->QueryInterface(NS_GET_IID(nsISupports), (void **)&mClassObject);
if (NS_FAILED(rv)) return rv;
}
rv = mClassObject->QueryInterface(aIID, r_classObj);
return rv;
}
NS_IMETHODIMP
nsPNGModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFile* aPath,
const char *registryLocation,
const char *componentType)
{
nsresult rv;
rv = aCompMgr->RegisterComponentSpec(kPNGDecoderCID,
"Netscape PNGDec",
"component://netscape/image/decoder&type=image/png",
aPath, PR_TRUE, PR_TRUE);
return rv;
}
NS_IMETHODIMP
nsPNGModule::UnregisterSelf(nsIComponentManager *aCompMgr,
nsIFile* aPath,
const char *registryLocation)
{
nsresult rv;
rv = aCompMgr->UnregisterComponentSpec(kPNGDecoderCID, aPath);
return rv;
}
NS_IMETHODIMP
nsPNGModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (mObjCount < 0)
{
// Dll doesn't count objects.
return NS_ERROR_FAILURE;
}
PRBool unloadable = PR_FALSE;
if (mObjCount == 0)
{
// This dll can be unloaded now
unloadable = PR_TRUE;
}
if (okToUnload) *okToUnload = unloadable;
return NS_OK;
}

@ -1,49 +0,0 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#ifndef nsPNGModule_h___
#define nsPNGModule_h___
#include "nsIModule.h"
class nsPNGModule : public nsIModule
{
public:
NS_DECL_ISUPPORTS
// Construction, Init and desstruction
nsPNGModule();
virtual ~nsPNGModule();
// nsIModule Interfaces
NS_DECL_NSIMODULE
// Facility for counting object instances
int IncrementObjCount() { if (mObjCount == -1) mObjCount = 0; return ++mObjCount; }
int DecrementObjCount() { if (mObjCount == -1) mObjCount = 0; return --mObjCount; }
int GetObjCount() { return mObjCount; }
private:
int mObjCount;
nsISupports* mClassObject;
};
#endif /* nsPNGModule_h___ */