Converted dll to use nsIModule - bug 14034, r=dp

This commit is contained in:
neeti%netscape.com 1999-10-16 03:05:55 +00:00
Родитель b6a3ba2192
Коммит 47af20e309
1 изменённых файлов: 217 добавлений и 98 удалений

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

@ -18,7 +18,9 @@
#define NS_IMPL_IDS
#include "pratom.h"
#include "nsIFactory.h"
#include "nsCOMPtr.h"
#include "nsIModule.h"
#include "nsIGenericFactory.h"
#include "nsIServiceManager.h"
#include "nsRepository.h"
#include "nsIPICS.h"
@ -57,6 +59,7 @@
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIPICSIID, NS_IPICS_IID);
static NS_DEFINE_IID(kPICSCID, NS_PICS_CID);
static NS_DEFINE_IID(kIPrefIID, NS_IPREF_IID);
static NS_DEFINE_CID(kPrefCID, NS_PREF_CID);
@ -254,27 +257,26 @@ static nsPICS* gPICS = nsnull; // The one-and-only PICSService
////////////////////////////////////////////////////////////////////////////////
class nsPICSFactory : public nsIFactory {
// Module implementation
class nsPICSModule : public nsIModule
{
public:
nsPICSModule();
virtual ~nsPICSModule();
NS_DECL_ISUPPORTS
NS_DECL_ISUPPORTS
// nsIFactory methods:
NS_DECL_NSIMODULE
NS_IMETHOD CreateInstance(nsISupports *aOuter,
REFNSIID aIID,
void **aResult);
protected:
nsresult Initialize();
NS_IMETHOD LockFactory(PRBool aLock);
// nsPICS methods:
nsPICSFactory(void);
virtual ~nsPICSFactory(void);
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mFactory;
};
StateRet_t
TargetCallback(CSLabel_t *pCSLabel,
CSParse_t * pCSParse,
@ -1578,114 +1580,231 @@ nsPICS::GetRootURL(nsIURI* aURL)
}
#endif
////////////////////////////////////////////////////////////////////////////////
// nsPICSFactory Implementation
//----------------------------------------------------------------------
static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
NS_IMPL_ISUPPORTS(nsPICSFactory, kIFactoryIID);
// Functions used to create new instances of a given object by the
// generic factory.
nsPICSFactory::nsPICSFactory(void)
{
NS_INIT_REFCNT();
static NS_IMETHODIMP
CreateNewPICS(nsISupports* aOuter, REFNSIID aIID, void **aResult)
{
if (!aResult) {
return NS_ERROR_INVALID_POINTER;
}
if (aOuter) {
*aResult = nsnull;
return NS_ERROR_NO_AGGREGATION;
}
nsIPICS* inst;
nsresult rv = NS_NewPICS(&inst);
if (NS_FAILED(rv)) {
*aResult = nsnull;
return rv;
}
rv = inst->QueryInterface(aIID, aResult);
if (NS_FAILED(rv)) {
*aResult = nsnull;
}
NS_RELEASE(inst); /* get rid of extra refcnt */
return rv;
}
nsPICSFactory::~nsPICSFactory(void)
{
//----------------------------------------------------------------------
nsPICSModule::nsPICSModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsPICSModule::~nsPICSModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsPICSModule, NS_GET_IID(nsIModule))
// Perform our one-time intialization for this module
nsresult
nsPICSFactory::CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult)
nsPICSModule::Initialize()
{
if (! aResult)
return NS_ERROR_NULL_POINTER;
if (aOuter)
return NS_ERROR_NO_AGGREGATION;
*aResult = nsnull;
nsresult rv;
nsIPICS* inst = nsnull;
if (NS_FAILED(rv = NS_NewPICS(&inst)))
return rv;
if (!inst)
return NS_ERROR_OUT_OF_MEMORY;
rv = inst->QueryInterface(aIID, aResult);
if (NS_FAILED(rv)) {
*aResult = NULL;
}
return rv;
}
nsresult
nsPICSFactory::LockFactory(PRBool aLock)
{
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// DLL Entry Points:
static NS_DEFINE_IID(kPICSCID, NS_PICS_CID);
extern "C" NS_EXPORT nsresult
NSGetFactory(nsISupports* servMgr,
const nsCID &aClass,
const char *aClassName,
const char *aProgID,
nsIFactory **aFactory)
{
if (! aFactory)
return NS_ERROR_NULL_POINTER;
if (aClass.Equals(kPICSCID)) {
nsPICSFactory *factory = new nsPICSFactory();
if (factory == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(factory);
*aFactory = factory;
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
return NS_NOINTERFACE;
}
extern "C" NS_EXPORT PRBool
NSCanUnload(nsISupports* serviceMgr)
// Shutdown this module, releasing all of the module resources
void
nsPICSModule::Shutdown()
{
return PR_FALSE;
// Release the factory object
mFactory = nsnull;
}
extern "C" PR_IMPLEMENT(nsresult)
NSRegisterSelf(nsISupports* serviceMgr, const char* aPath)
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsPICSModule::GetClassObject(nsIComponentManager *aCompMgr,
const nsCID& aClass,
const nsIID& aIID,
void** r_classObj)
{
return nsRepository::RegisterComponent(kPICSCID,
"PICS",
NS_PICS_PROGID,
aPath,PR_TRUE, PR_TRUE);
nsresult rv;
return NS_OK;
// 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(kPICSCID)) {
if (!mFactory) {
// Create and save away the factory object for creating
// new instances of PICS. 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),
CreateNewPICS);
}
fact = mFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsPICSModule: unable to create factory for %s\n", cs);
nsCRT::free(cs);
#endif
}
if (fact) {
rv = fact->QueryInterface(aIID, r_classObj);
}
return rv;
}
extern "C" PR_IMPLEMENT(nsresult)
NSUnregisterSelf(nsISupports* serviceMgr, const char* aPath)
//----------------------------------------
struct Components {
const char* mDescription;
const nsID* mCID;
const char* mProgID;
};
// The list of components we register
static Components gComponents[] = {
{ "PICS", &kPICSCID,
NS_PICS_PROGID, },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsPICSModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv;
nsresult rv = NS_OK;
rv = nsRepository::UnregisterComponent(kPICSCID, aPath);
#ifdef DEBUG
printf("*** Registering PICS components\n");
#endif
return rv;
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("nsPICSModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsPICSModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering PICS components\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("nsPICSModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsPICSModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsPICSModule *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
nsPICSModule *m = new nsPICSModule();
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;
}
////////////////////////////////////////////////////////////////////////////////