Converted to use nsIModule to prepare for memory leaks fixing. Bug# 14034. r=dveditz

This commit is contained in:
neeti%netscape.com 1999-10-07 03:39:36 +00:00
parent 5ee3473d40
commit cf444aab7c
3 changed files with 222 additions and 172 deletions

View File

@ -35,6 +35,9 @@
{0xbc, 0xf8, 0x00, 0x80, 0x5f, 0x0e, 0x13, 0x53} \
}
#define NS_JAR_PROGID \
"component://netscape/libjar"
#define NS_IJARFactory_IID \
{ /* 04501DB4-0409-11d3-BCF8-00805F0E1353 */ \
0x04501DB4, \

View File

@ -31,22 +31,24 @@
#include "nsIFactory.h"
class nsJARFactory : public nsIFactory
// Module implementation
class nsJARModule : public nsIModule
{
public:
nsJARFactory();
virtual ~nsJARFactory();
public:
nsJARModule();
virtual ~nsJARModule();
NS_DECL_ISUPPORTS
NS_IMETHOD CreateInstance(nsISupports *aOuter,
REFNSIID aIID,
void **aResult);
NS_IMETHOD LockFactory(PRBool aLock);
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mFactory;
};

View File

@ -34,6 +34,8 @@
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsCOMPtr.h"
#include "nsIModule.h"
#include "nsIGenericFactory.h"
#include "nsJAR.h"
#include "nsIJARFactory.h"
@ -57,187 +59,230 @@ static NS_DEFINE_IID(kJAR_CID, NS_JAR_CID);
static NS_DEFINE_IID(kIJARFactory_IID, NS_IJARFactory_IID);
static NS_DEFINE_IID(kJARFactory_CID, NS_JARFactory_CID);
/*---------------------------------------------------------
* nsJARFactory implementation
*--------------------------------------------------------*/
static PRInt32 gJARInstanceCnt = 0;
static PRInt32 gJARLock = 0;
/* constructor */
nsJARFactory::nsJARFactory(void)
{
mRefCnt=0;
PR_AtomicIncrement(&gJARInstanceCnt);
//----------------------------------------------------------------------
// Functions used to create new instances of a given object by the
// generic factory.
static NS_IMETHODIMP
CreateNewJAR(nsISupports* aOuter, REFNSIID aIID, void **aResult)
{
if (!aResult) {
return NS_ERROR_INVALID_POINTER;
}
if (aOuter) {
*aResult = nsnull;
return NS_ERROR_NO_AGGREGATION;
}
nsJAR* inst = new nsJAR();
if (inst == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(inst);
nsresult rv = inst->QueryInterface(aIID, aResult);
if (NS_FAILED(rv)) {
*aResult = nsnull;
}
NS_RELEASE(inst); /* get rid of extra refcnt */
return rv;
}
/* destructor */
nsJARFactory::~nsJARFactory(void)
//----------------------------------------------------------------------
nsJARModule::nsJARModule()
: mInitialized(PR_FALSE)
{
PR_AtomicDecrement(&gJARInstanceCnt);
NS_INIT_ISUPPORTS();
}
/* The ubiquitous QueryInterface method */
NS_IMETHODIMP
nsJARFactory::QueryInterface(REFNSIID aIID, void** aInstancePtr)
nsJARModule::~nsJARModule()
{
if (aInstancePtr == NULL)
{
return NS_ERROR_NULL_POINTER;
}
// Always NULL result, in case of failure
*aInstancePtr = NULL;
if ( aIID.Equals(kISupportsIID) )
{
*aInstancePtr = (void*) this;
}
else if ( aIID.Equals(kIFactoryIID) )
{
*aInstancePtr = (void*) this;
}
if (aInstancePtr == NULL)
{
return NS_ERROR_NO_INTERFACE;
}
AddRef();
return NS_OK;
Shutdown();
}
NS_IMPL_ADDREF(nsJARFactory);
NS_IMPL_RELEASE(nsJARFactory);
/*------------------------------------------------------------------------*/
/* The JARFactory CreateInstance Method */
/*------------------------------------------------------------------------*/
NS_IMPL_ISUPPORTS(nsJARModule, NS_GET_IID(nsIModule))
// Perform our one-time intialization for this module
nsresult
nsJARModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsJARModule::Shutdown()
{
// Release the factory object
mFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsJARFactory::CreateInstance(nsISupports *aOuter,
REFNSIID aIID,
void **aResult)
nsJARModule::GetClassObject(nsIComponentManager *aCompMgr,
const nsCID& aClass,
const nsIID& aIID,
void** r_classObj)
{
if (aResult == NULL)
{
return NS_ERROR_NULL_POINTER;
}
*aResult = NULL;
nsJAR* pJAR = new nsJAR();
nsresult rv;
if (pJAR == NULL)
return NS_ERROR_OUT_OF_MEMORY;
// Defensive programming: Initialize *r_classObj in case of error below
if (!r_classObj) {
return NS_ERROR_INVALID_POINTER;
}
*r_classObj = NULL;
nsresult result = pJAR->QueryInterface(aIID, aResult);
// Do one-time-only initialization if necessary
if (!mInitialized) {
rv = Initialize();
if (NS_FAILED(rv)) {
// Initialization failed! yikes!
return rv;
}
}
if (result != NS_OK)
delete pJAR;
// Choose the appropriate factory, based on the desired instance
// class type (aClass).
nsCOMPtr<nsIGenericFactory> fact;
if (aClass.Equals(kJAR_CID)) {
if (!mFactory) {
// Create and save away the factory object for creating
// new instances of JAR. This way if we are called
// again for the factory, we won't need to create a new
// one.
rv = NS_NewGenericFactory(getter_AddRefs(mFactory),
CreateNewJAR);
}
fact = mFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsJARModule: unable to create factory for %s\n", cs);
nsCRT::free(cs);
#endif
}
return result;
if (fact) {
rv = fact->QueryInterface(aIID, r_classObj);
}
return rv;
}
//----------------------------------------
struct Components {
const char* mDescription;
const nsID* mCID;
const char* mProgID;
};
// The list of components we register
static Components gComponents[] = {
{ "LibJAR Component", &kJAR_CID,
"component://netscape/libjar", },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsJARModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering jar components\n");
#endif
Components* cp = gComponents;
Components* end = cp + NUM_COMPONENTS;
while (cp < end) {
rv = aCompMgr->RegisterComponentSpec(*cp->mCID, cp->mDescription,
cp->mProgID, aPath, PR_TRUE,
PR_TRUE);
if (NS_FAILED(rv)) {
#ifdef DEBUG
printf("nsJARModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsJARFactory::LockFactory(PRBool aLock)
nsJARModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
if (aLock)
PR_AtomicIncrement(&gJARLock);
else
PR_AtomicDecrement(&gJARLock);
return NS_OK;
}
/*----------------------------------------------------------
* Exported XPCOM Factory stuff
*---------------------------------------------------------*/
extern "C" NS_EXPORT nsresult
NSGetFactory(nsISupports* serviceMgr,
const nsCID &aClass,
const char *aClassName,
const char *aProgID,
nsIFactory **aFactory)
{
if (aFactory == NULL)
{
return NS_ERROR_NULL_POINTER;
}
*aFactory = NULL;
nsISupports *inst;
if ( aClass.Equals(kJAR_CID) )
{
inst = new nsJARFactory();
}
else
{
return NS_ERROR_ILLEGAL_VALUE;
}
if (inst == NULL)
{
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult res = inst->QueryInterface(kIFactoryIID, (void**) aFactory);
if (NS_FAILED(res))
{
delete inst;
}
return res;
}
/*----------------------------------------------------------
* XPCOM registry stuff
*---------------------------------------------------------*/
extern "C" NS_EXPORT nsresult
NSRegisterSelf(nsISupports* aServMgr, const char *path)
{
#ifdef NS_DEBUG
printf("*** Register libjar\n");
#ifdef DEBUG
printf("*** Unregistering jar components\n");
#endif
nsresult rv;
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
if (NS_FAILED(rv)) return rv;
nsIComponentManager* compMgr;
rv = servMgr->GetService(kComponentManagerCID,
nsIComponentManager::GetIID(),
(nsISupports**)&compMgr);
if (NS_FAILED(rv)) return rv;
rv = compMgr->RegisterComponent(kJAR_CID,
NULL, NULL, path,
PR_TRUE, PR_TRUE);
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
return rv;
}
extern "C" NS_EXPORT nsresult
NSUnregisterSelf(nsISupports* aServMgr, const char *path)
{
#ifdef NS_DEBUG
printf("*** Unregister libjar\n");
Components* cp = gComponents;
Components* end = cp + NUM_COMPONENTS;
while (cp < end) {
nsresult rv = aCompMgr->UnregisterComponentSpec(*cp->mCID, aPath);
if (NS_FAILED(rv)) {
#ifdef DEBUG
printf("nsJARModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
nsresult rv;
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
if (NS_FAILED(rv)) return rv;
nsIComponentManager* compMgr;
rv = servMgr->GetService(kComponentManagerCID,
nsIComponentManager::GetIID(),
(nsISupports**)&compMgr);
if (NS_FAILED(rv)) return rv;
rv = compMgr->UnregisterComponent(kJAR_CID, path);
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
return rv;
return NS_OK;
}
NS_IMETHODIMP
nsJARModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsJARModule *gModule = NULL;
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager *servMgr,
nsIFileSpec* location,
nsIModule** return_cobj)
{
nsresult rv = NS_OK;
NS_ENSURE_ARG_POINTER(return_cobj);
NS_ENSURE_NOT(gModule, NS_ERROR_FAILURE);
// Create and initialize the module instance
nsJARModule *m = new nsJARModule();
if (!m) {
return NS_ERROR_OUT_OF_MEMORY;
}
// Increase refcnt and store away nsIModule interface to m in return_cobj
rv = m->QueryInterface(NS_GET_IID(nsIModule), (void**)return_cobj);
if (NS_FAILED(rv)) {
delete m;
m = nsnull;
}
gModule = m; // WARNING: Weak Reference
return rv;
}