Bug # 14034: Convert to nsIModule to prepare for memory leak fixing. Review: dp@netscape.com

This commit is contained in:
rjc%netscape.com 1999-10-05 23:39:48 +00:00
Родитель dbe1a08a25
Коммит f02c6c3fd8
2 изменённых файлов: 493 добавлений и 1 удалений

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

@ -992,6 +992,250 @@ nsDirectoryViewerFactory::CreateInstanceForDocument(nsIContentViewerContainer* a
////////////////////////////////////////////////////////////////////////
// Component Exports
#if 1
#include "nsIModule.h"
// Module implementation
class nsDirectoryViewerModule : public nsIModule
{
public:
nsDirectoryViewerModule();
virtual ~nsDirectoryViewerModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mDirectoryViewerFactory;
};
nsDirectoryViewerModule::nsDirectoryViewerModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsDirectoryViewerModule::~nsDirectoryViewerModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsDirectoryViewerModule, NS_GET_IID(nsIModule))
// Perform our one-time intialization for this module
nsresult
nsDirectoryViewerModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsDirectoryViewerModule::Shutdown()
{
// Release the factory object
mDirectoryViewerFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsDirectoryViewerModule::GetClassObject(nsIComponentManager *aCompMgr,
const nsCID& aClass,
const nsIID& aIID,
void** r_classObj)
{
nsresult rv;
// 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;
}
}
nsCOMPtr<nsIGenericFactory> fact;
// Choose the appropriate factory, based on the desired instance
// class type (aClass).
if (aClass.Equals(kDirectoryViewerFactoryCID))
{
if (!mDirectoryViewerFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mDirectoryViewerFactory),
nsDirectoryViewerFactory::Create);
}
fact = mDirectoryViewerFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsDirectoryViewerModule: unable to create factory for %s\n", cs);
nsCRT::free(cs);
#endif
}
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[] = {
{ "Directory Viewer", &kDirectoryViewerFactoryCID, NS_DOCUMENT_LOADER_FACTORY_PROGID_PREFIX "view/application/http-index-format" },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsDirectoryViewerModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering nsDirectoryViewerModule\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("nsDirectoryViewerModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsDirectoryViewerModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering nsDirectoryViewerModule\n");
#endif
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("nsDirectoryViewerModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsDirectoryViewerModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsDirectoryViewerModule *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
nsDirectoryViewerModule *m = new nsDirectoryViewerModule();
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;
}
#else
extern "C" PR_IMPLEMENT(nsresult)
NSGetFactory(nsISupports* aServiceMgr,
const nsCID &aClass,
@ -1071,4 +1315,4 @@ NSUnregisterSelf(nsISupports* aServMgr, const char* aPath)
return NS_OK;
}
#endif

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

@ -1048,6 +1048,252 @@ RelatedLinksHandlerImpl::DoCommand(nsISupportsArray/*<nsIRDFResource>*/* aSource
////////////////////////////////////////////////////////////////////////
// Component Exports
#if 1
#include "nsIModule.h"
// Module implementation
class nsRelatedLinksModule : public nsIModule
{
public:
nsRelatedLinksModule();
virtual ~nsRelatedLinksModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mRelatedLinksFactory;
};
nsRelatedLinksModule::nsRelatedLinksModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsRelatedLinksModule::~nsRelatedLinksModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsRelatedLinksModule, NS_GET_IID(nsIModule))
// Perform our one-time intialization for this module
nsresult
nsRelatedLinksModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsRelatedLinksModule::Shutdown()
{
// Release the factory object
mRelatedLinksFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsRelatedLinksModule::GetClassObject(nsIComponentManager *aCompMgr,
const nsCID& aClass,
const nsIID& aIID,
void** r_classObj)
{
nsresult rv;
// 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;
}
}
nsCOMPtr<nsIGenericFactory> fact;
// Choose the appropriate factory, based on the desired instance
// class type (aClass).
if (aClass.Equals(kRelatedLinksHandlerCID))
{
if (!mRelatedLinksFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mRelatedLinksFactory),
NS_NewRelatedLinksHandler);
}
fact = mRelatedLinksFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsRelatedLinksModule: unable to create factory for %s\n", cs);
nsCRT::free(cs);
#endif
}
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[] = {
{ "Related Links Handler", &kRelatedLinksHandlerCID, NS_RELATEDLINKSHANDLER_PROGID },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsRelatedLinksModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering nsRelatedLinksModule\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("nsRelatedLinksModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsRelatedLinksModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering nsRelatedLinksModule\n");
#endif
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("nsRelatedLinksModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsRelatedLinksModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsRelatedLinksModule *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
nsRelatedLinksModule *m = new nsRelatedLinksModule();
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;
}
#else
extern "C" PR_IMPLEMENT(nsresult)
NSGetFactory(nsISupports* aServiceMgr,
const nsCID &aClass,
@ -1126,3 +1372,5 @@ NSUnregisterSelf(nsISupports* aServMgr, const char* aPath)
return NS_OK;
}
#endif