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

This commit is contained in:
neeti%netscape.com 1999-10-12 13:33:42 +00:00
Родитель 21f3308118
Коммит 37039bfbbe
2 изменённых файлов: 428 добавлений и 94 удалений

Просмотреть файл

@ -22,6 +22,8 @@
#include "nsIChromeRegistry.h" #include "nsIChromeRegistry.h"
#include "nscore.h" #include "nscore.h"
#include "rdf.h" #include "rdf.h"
#include "nsCOMPtr.h"
#include "nsIModule.h"
#ifdef NECKO #ifdef NECKO
#include "nsChromeProtocolHandler.h" #include "nsChromeProtocolHandler.h"
#endif #endif
@ -50,73 +52,238 @@ NS_ConstructChromeRegistry(nsISupports *aOuter, REFNSIID aIID, void **aResult)
return rv; return rv;
} }
extern "C" PR_IMPLEMENT(nsresult) // Module implementation
NSGetFactory(nsISupports* aServMgr, class nsChromeModule : public nsIModule
const nsCID &aClass, {
const char *aClassName, public:
const char *aProgID, nsChromeModule();
nsIFactory **aFactory) virtual ~nsChromeModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mChromeRegistryFactory;
nsCOMPtr<nsIGenericFactory> mChromeProtocolHandlerFactory;
};
//----------------------------------------------------------------------
// Functions used to create new instances of a given object by the
// generic factory.
//----------------------------------------------------------------------
nsChromeModule::nsChromeModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsChromeModule::~nsChromeModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsChromeModule, NS_GET_IID(nsIModule))
// Perform our one-time intialization for this module
nsresult
nsChromeModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsChromeModule::Shutdown()
{
// Release the factory objects
mChromeRegistryFactory = nsnull;
mChromeProtocolHandlerFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsChromeModule::GetClassObject(nsIComponentManager *aCompMgr,
const nsCID& aClass,
const nsIID& aIID,
void** r_classObj)
{ {
nsresult rv; nsresult rv;
NS_ASSERTION(aFactory != nsnull, "bad factory pointer");
nsIGenericFactory* fact; // Defensive programming: Initialize *r_classObj in case of error below
if (!r_classObj) {
return NS_ERROR_INVALID_POINTER;
}
*r_classObj = NULL;
// Do one-time-only initialization if necessary
if (!mInitialized) {
rv = Initialize();
if (NS_FAILED(rv)) {
// Initialization failed! yikes!
return rv;
}
}
// Choose the appropriate factory, based on the desired instance
// class type (aClass).
nsCOMPtr<nsIGenericFactory> fact;
if (aClass.Equals(kChromeRegistryCID)) { if (aClass.Equals(kChromeRegistryCID)) {
rv = NS_NewGenericFactory(&fact, NS_ConstructChromeRegistry); if (!mChromeRegistryFactory) {
// Create and save away the factory object for creating
// new instances of ChromeRegistry. This way if we are called
// again for the factory, we won't need to create a new
// one.
rv = NS_NewGenericFactory(getter_AddRefs(mChromeRegistryFactory),
NS_ConstructChromeRegistry);
}
fact = mChromeRegistryFactory;
} }
#ifdef NECKO
else if (aClass.Equals(kChromeProtocolHandlerCID)) { else if (aClass.Equals(kChromeProtocolHandlerCID)) {
rv = NS_NewGenericFactory(&fact, nsChromeProtocolHandler::Create); if (!mChromeProtocolHandlerFactory) {
// Create and save away the factory object for creating
// new instances of ChromeProtocolHandler. This way if we are called
// again for the factory, we won't need to create a new
// one.
rv = NS_NewGenericFactory(getter_AddRefs(mChromeProtocolHandlerFactory),
nsChromeProtocolHandler::Create);
}
fact = mChromeProtocolHandlerFactory;
} }
#endif
else { else {
rv = NS_ERROR_FAILURE; rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsChromeModule: unable to create factory for %s\n", cs);
nsCRT::free(cs);
#endif
}
if (fact) {
rv = fact->QueryInterface(aIID, r_classObj);
} }
if (NS_SUCCEEDED(rv))
*aFactory = fact;
return rv; return rv;
} }
extern "C" PR_IMPLEMENT(nsresult) //----------------------------------------
NSRegisterSelf(nsISupports* aServMgr , const char* aPath)
struct Components {
const char* mDescription;
const nsID* mCID;
const char* mProgID;
};
// The list of components we register
static Components gComponents[] = {
{ "Chrome Registry", &kChromeRegistryCID,
NS_RDF_DATASOURCE_PROGID_PREFIX "chrome", },
{ "Chrome Protocol Handler", &kChromeProtocolHandlerCID,
NS_NETWORK_PROTOCOL_PROGID_PREFIX "chrome", },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsChromeModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{ {
nsresult rv; nsresult rv = NS_OK;
NS_WITH_SERVICE1(nsIComponentManager, compMgr, #ifdef DEBUG
aServMgr, kComponentManagerCID, &rv); printf("*** Registering chrome components\n");
if (NS_FAILED(rv)) return rv;
rv = compMgr->RegisterComponent(kChromeRegistryCID,
"Chrome Registry",
NS_RDF_DATASOURCE_PROGID_PREFIX "chrome",
aPath,
PR_TRUE, PR_TRUE);
#ifdef NECKO
if (NS_FAILED(rv)) return rv;
rv = compMgr->RegisterComponent(kChromeProtocolHandlerCID,
"Chrome Protocol Handler",
NS_NETWORK_PROTOCOL_PROGID_PREFIX "chrome",
aPath, PR_TRUE, PR_TRUE);
#endif #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("nsChromeModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv; return rv;
} }
extern "C" PR_IMPLEMENT(nsresult) NS_IMETHODIMP
NSUnregisterSelf(nsISupports* aServMgr, const char* aPath) nsChromeModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{ {
nsresult rv; #ifdef DEBUG
NS_WITH_SERVICE1(nsIComponentManager, compMgr, printf("*** Unregistering chrome components\n");
aServMgr, kComponentManagerCID, &rv);
if (NS_FAILED(rv)) return rv;
rv = compMgr->UnregisterComponent(kChromeRegistryCID, aPath);
#ifdef NECKO
if (NS_FAILED(rv)) return rv;
rv = compMgr->UnregisterComponent(kChromeProtocolHandlerCID, aPath);
#endif #endif
return rv; 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("nsChromeModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
} }
NS_IMETHODIMP
nsChromeModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsChromeModule *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
nsChromeModule *m = new nsChromeModule();
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;
}

Просмотреть файл

@ -22,6 +22,8 @@
#include "nsIChromeRegistry.h" #include "nsIChromeRegistry.h"
#include "nscore.h" #include "nscore.h"
#include "rdf.h" #include "rdf.h"
#include "nsCOMPtr.h"
#include "nsIModule.h"
#ifdef NECKO #ifdef NECKO
#include "nsChromeProtocolHandler.h" #include "nsChromeProtocolHandler.h"
#endif #endif
@ -50,73 +52,238 @@ NS_ConstructChromeRegistry(nsISupports *aOuter, REFNSIID aIID, void **aResult)
return rv; return rv;
} }
extern "C" PR_IMPLEMENT(nsresult) // Module implementation
NSGetFactory(nsISupports* aServMgr, class nsChromeModule : public nsIModule
const nsCID &aClass, {
const char *aClassName, public:
const char *aProgID, nsChromeModule();
nsIFactory **aFactory) virtual ~nsChromeModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mChromeRegistryFactory;
nsCOMPtr<nsIGenericFactory> mChromeProtocolHandlerFactory;
};
//----------------------------------------------------------------------
// Functions used to create new instances of a given object by the
// generic factory.
//----------------------------------------------------------------------
nsChromeModule::nsChromeModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsChromeModule::~nsChromeModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsChromeModule, NS_GET_IID(nsIModule))
// Perform our one-time intialization for this module
nsresult
nsChromeModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsChromeModule::Shutdown()
{
// Release the factory objects
mChromeRegistryFactory = nsnull;
mChromeProtocolHandlerFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsChromeModule::GetClassObject(nsIComponentManager *aCompMgr,
const nsCID& aClass,
const nsIID& aIID,
void** r_classObj)
{ {
nsresult rv; nsresult rv;
NS_ASSERTION(aFactory != nsnull, "bad factory pointer");
nsIGenericFactory* fact; // Defensive programming: Initialize *r_classObj in case of error below
if (!r_classObj) {
return NS_ERROR_INVALID_POINTER;
}
*r_classObj = NULL;
// Do one-time-only initialization if necessary
if (!mInitialized) {
rv = Initialize();
if (NS_FAILED(rv)) {
// Initialization failed! yikes!
return rv;
}
}
// Choose the appropriate factory, based on the desired instance
// class type (aClass).
nsCOMPtr<nsIGenericFactory> fact;
if (aClass.Equals(kChromeRegistryCID)) { if (aClass.Equals(kChromeRegistryCID)) {
rv = NS_NewGenericFactory(&fact, NS_ConstructChromeRegistry); if (!mChromeRegistryFactory) {
// Create and save away the factory object for creating
// new instances of ChromeRegistry. This way if we are called
// again for the factory, we won't need to create a new
// one.
rv = NS_NewGenericFactory(getter_AddRefs(mChromeRegistryFactory),
NS_ConstructChromeRegistry);
}
fact = mChromeRegistryFactory;
} }
#ifdef NECKO
else if (aClass.Equals(kChromeProtocolHandlerCID)) { else if (aClass.Equals(kChromeProtocolHandlerCID)) {
rv = NS_NewGenericFactory(&fact, nsChromeProtocolHandler::Create); if (!mChromeProtocolHandlerFactory) {
// Create and save away the factory object for creating
// new instances of ChromeProtocolHandler. This way if we are called
// again for the factory, we won't need to create a new
// one.
rv = NS_NewGenericFactory(getter_AddRefs(mChromeProtocolHandlerFactory),
nsChromeProtocolHandler::Create);
}
fact = mChromeProtocolHandlerFactory;
} }
#endif
else { else {
rv = NS_ERROR_FAILURE; rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsChromeModule: unable to create factory for %s\n", cs);
nsCRT::free(cs);
#endif
}
if (fact) {
rv = fact->QueryInterface(aIID, r_classObj);
} }
if (NS_SUCCEEDED(rv))
*aFactory = fact;
return rv; return rv;
} }
extern "C" PR_IMPLEMENT(nsresult) //----------------------------------------
NSRegisterSelf(nsISupports* aServMgr , const char* aPath)
struct Components {
const char* mDescription;
const nsID* mCID;
const char* mProgID;
};
// The list of components we register
static Components gComponents[] = {
{ "Chrome Registry", &kChromeRegistryCID,
NS_RDF_DATASOURCE_PROGID_PREFIX "chrome", },
{ "Chrome Protocol Handler", &kChromeProtocolHandlerCID,
NS_NETWORK_PROTOCOL_PROGID_PREFIX "chrome", },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsChromeModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{ {
nsresult rv; nsresult rv = NS_OK;
NS_WITH_SERVICE1(nsIComponentManager, compMgr, #ifdef DEBUG
aServMgr, kComponentManagerCID, &rv); printf("*** Registering chrome components\n");
if (NS_FAILED(rv)) return rv;
rv = compMgr->RegisterComponent(kChromeRegistryCID,
"Chrome Registry",
NS_RDF_DATASOURCE_PROGID_PREFIX "chrome",
aPath,
PR_TRUE, PR_TRUE);
#ifdef NECKO
if (NS_FAILED(rv)) return rv;
rv = compMgr->RegisterComponent(kChromeProtocolHandlerCID,
"Chrome Protocol Handler",
NS_NETWORK_PROTOCOL_PROGID_PREFIX "chrome",
aPath, PR_TRUE, PR_TRUE);
#endif #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("nsChromeModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv; return rv;
} }
extern "C" PR_IMPLEMENT(nsresult) NS_IMETHODIMP
NSUnregisterSelf(nsISupports* aServMgr, const char* aPath) nsChromeModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{ {
nsresult rv; #ifdef DEBUG
NS_WITH_SERVICE1(nsIComponentManager, compMgr, printf("*** Unregistering chrome components\n");
aServMgr, kComponentManagerCID, &rv);
if (NS_FAILED(rv)) return rv;
rv = compMgr->UnregisterComponent(kChromeRegistryCID, aPath);
#ifdef NECKO
if (NS_FAILED(rv)) return rv;
rv = compMgr->UnregisterComponent(kChromeProtocolHandlerCID, aPath);
#endif #endif
return rv; 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("nsChromeModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
} }
NS_IMETHODIMP
nsChromeModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsChromeModule *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
nsChromeModule *m = new nsChromeModule();
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;
}