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-02 00:48:02 +00:00
parent caad83220b
commit 38e1d06d8d
3 changed files with 844 additions and 1 deletions

View File

@ -17,6 +17,294 @@
*/
/*Factory for internal browser security resource managers*/
#if 1
#include "nsCOMPtr.h"
#include "nsIModule.h"
#include "nsIGenericFactory.h"
#include "nsIScriptSecurityManager.h"
#include "nsScriptSecurityManager.h"
#include "nsIPrincipal.h"
#include "nsCodebasePrincipal.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(nsCodebasePrincipal)
static NS_IMETHODIMP
Construct_nsIScriptSecurityManager(nsISupports *aOuter, REFNSIID aIID,
void **aResult)
{
if (!aResult)
return NS_ERROR_NULL_POINTER;
*aResult = nsnull;
if (aOuter)
return NS_ERROR_NO_AGGREGATION;
nsScriptSecurityManager *obj = nsScriptSecurityManager::GetScriptSecurityManager();
if (!obj)
return NS_ERROR_OUT_OF_MEMORY;
if (NS_FAILED(obj->QueryInterface(aIID, aResult)))
return NS_ERROR_FAILURE;
return NS_OK;
}
// Module implementation
class nsSecurityManagerModule : public nsIModule
{
public:
nsSecurityManagerModule();
virtual ~nsSecurityManagerModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mScriptSecurityManagerFactory;
nsCOMPtr<nsIGenericFactory> mCodebasePrincipalFactory;
};
//----------------------------------------------------------------------
nsSecurityManagerModule::nsSecurityManagerModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsSecurityManagerModule::~nsSecurityManagerModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsSecurityManagerModule, NS_GET_IID(nsIModule))
// Perform our one-time intialization for this module
nsresult
nsSecurityManagerModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsSecurityManagerModule::Shutdown()
{
// XXX FIX this: nsScriptSecurityManager::GetScriptSecurityManager() leaks!
// Release the factory object
mScriptSecurityManagerFactory = nsnull;
mCodebasePrincipalFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsSecurityManagerModule::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(nsScriptSecurityManager::GetCID()))
{
if (!mScriptSecurityManagerFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mScriptSecurityManagerFactory),
nsCodebasePrincipalConstructor);
}
fact = mScriptSecurityManagerFactory;
}
else if (aClass.Equals(nsCodebasePrincipal::GetCID()))
{
if (!mCodebasePrincipalFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mCodebasePrincipalFactory),
Construct_nsIScriptSecurityManager);
}
fact = mCodebasePrincipalFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsSecurityManagerModule: 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[] = {
{ "Script Security Manager", &nsScriptSecurityManager::GetCID(), NS_SCRIPTSECURITYMANAGER_PROGID },
{ "Codebase Principal", &nsCodebasePrincipal::GetCID(), NS_CODEBASEPRINCIPAL_PROGID }
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsSecurityManagerModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering nsSecurityManagerModule\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("nsSecurityManagerModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsSecurityManagerModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering nsSecurityManagerModule\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("nsSecurityManagerModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsSecurityManagerModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsSecurityManagerModule *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
nsSecurityManagerModule *m = new nsSecurityManagerModule();
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
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsISupports.h"
@ -82,14 +370,20 @@ NSGetFactory(nsISupports *aServMgr, const nsCID &aClass,
return NS_OK;
}
/***************************************************************************/
extern "C" NS_EXPORT PRBool
NSCanUnload(nsISupports *aServMgr)
{
return PR_FALSE;
}
extern "C" NS_EXPORT nsresult
NSRegisterSelf(nsISupports *aServMgr, const char *aPath)
{
@ -117,11 +411,14 @@ NSRegisterSelf(nsISupports *aServMgr, const char *aPath)
return NS_ERROR_FAILURE;
return NS_OK;
}
extern "C" NS_EXPORT nsresult
NSUnregisterSelf(nsISupports *aServMgr, const char *aPath)
{
#ifdef DEBUG
printf("*** Unregistering Security***\n");
printf("*** Unregistering Security ***\n");
#endif
nsresult rv;
NS_WITH_SERVICE1(nsIComponentManager, compMgr, aServMgr,
@ -136,3 +433,5 @@ NSUnregisterSelf(nsISupports *aServMgr, const char *aPath)
return NS_ERROR_FAILURE;
return NS_OK;
}
#endif

View File

@ -21,6 +21,263 @@
#include "prmon.h"
#include "prlog.h"
#if 1
#include "nsCOMPtr.h"
#include "nsIModule.h"
#include "nsIGenericFactory.h"
#include "nsJVMManager.h"
#include "nsCJVMManagerFactory.h"
static NS_DEFINE_CID(kJVMManagerCID, NS_JVMMANAGER_CID);
// Module implementation
class nsCJVMManagerModule : public nsIModule
{
public:
nsCJVMManagerModule();
virtual ~nsCJVMManagerModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mCJVMManager;
};
//----------------------------------------------------------------------
nsCJVMManagerModule::nsCJVMManagerModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsCJVMManagerModule::~nsCJVMManagerModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsCJVMManagerModule, NS_GET_IID(nsIModule))
// Perform our one-time intialization for this module
nsresult
nsCJVMManagerModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsCJVMManagerModule::Shutdown()
{
// Release the factory object
mCJVMManager = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsCJVMManagerModule::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(kJVMManagerCID))
{
if (!mCJVMManager) {
rv = NS_NewGenericFactory(getter_AddRefs(mCJVMManager),
nsJVMManager::Create);
}
fact = mCJVMManager;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsCJVMManagerModule: 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[] = {
{ "JVM Manager Service", &kJVMManagerCID, "component://netscape/oji/jvm-mgr" },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsCJVMManagerModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering nsCJVMManagerModule\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("nsCJVMManagerModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsCJVMManagerModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering nsCJVMManagerModule\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("nsCJVMManagerModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsCJVMManagerModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsCJVMManagerModule *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
nsCJVMManagerModule *m = new nsCJVMManagerModule();
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
#include "nsJVMManager.h"
#include "nsCJVMManagerFactory.h"
#include "nsIComponentManager.h"
@ -181,3 +438,6 @@ nsCJVMManagerFactory::nsCJVMManagerFactory(void)
nsCJVMManagerFactory::~nsCJVMManagerFactory()
{
}
#endif

View File

@ -16,6 +16,288 @@
* Reserved.
*/
#if 1
#include "nsCOMPtr.h"
#include "nsIModule.h"
#include "nsIGenericFactory.h"
#include "nsIMIMEService.h"
#include "nsMIMEService.h"
#include "nsMIMEServiceFactory.h"
#include "nsIProtocolHandler.h" // for NS_NETWORK_PROTOCOL_PROGID_PREFIX
static NS_DEFINE_CID(kMIMEServiceCID, NS_MIMESERVICE_CID);
//----------------------------------------------------------------------
// Functions used to create new instances of a given object by the
// generic factory.
static NS_IMETHODIMP
CreateNewMIMEService(nsISupports* aOuter, REFNSIID aIID, void **aResult)
{
if (!aResult) {
return NS_ERROR_INVALID_POINTER;
}
if (aOuter) {
*aResult = nsnull;
return NS_ERROR_NO_AGGREGATION;
}
nsMIMEService *MIMEService = new nsMIMEService();
if (!MIMEService)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(MIMEService);
nsresult rv = MIMEService->QueryInterface(aIID, aResult);
if (NS_FAILED(rv)) {
*aResult = nsnull;
}
NS_RELEASE(MIMEService);
return rv;
}
// Module implementation
class nsMIMEServiceModule : public nsIModule
{
public:
nsMIMEServiceModule();
virtual ~nsMIMEServiceModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mMIMEServiceModuleFactory;
};
//----------------------------------------------------------------------
nsMIMEServiceModule::nsMIMEServiceModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsMIMEServiceModule::~nsMIMEServiceModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsMIMEServiceModule, NS_GET_IID(nsIModule))
// Perform our one-time intialization for this module
nsresult
nsMIMEServiceModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsMIMEServiceModule::Shutdown()
{
// Release the factory object
mMIMEServiceModuleFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsMIMEServiceModule::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(kMIMEServiceCID))
{
if (!mMIMEServiceModuleFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mMIMEServiceModuleFactory),
CreateNewMIMEService);
}
fact = mMIMEServiceModuleFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsMIMEServiceModule: 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[] = {
{ "MIME type file extensions mapping", &kMIMEServiceCID, "component:||netscape|mime" },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsMIMEServiceModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering nsMIMEServiceModule\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("nsMIMEServiceModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsMIMEServiceModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering nsMIMEServiceModule\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("nsMIMEServiceModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsMIMEServiceModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsMIMEServiceModule *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
nsMIMEServiceModule *m = new nsMIMEServiceModule();
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
/*
The nsHTTPHandlerFactory implementation. This was directly
@ -182,3 +464,5 @@ NSUnregisterSelf(nsISupports* aServMgr, const char* aPath)
return NS_OK;
}
#endif