fix 22921 r=dp change the implementation of nsIModule to use the generic module.

This commit is contained in:
ftang%netscape.com 2000-01-08 01:52:02 +00:00
parent 2fd1c82e85
commit 9d5a3181d2
3 changed files with 28 additions and 678 deletions

View File

@ -33,7 +33,6 @@
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
static NS_DEFINE_CID(kI18nCompatibilityCID, NS_I18NCOMPATIBILITY_CID);
static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
///////////////////////////////////////////////////////////////////////////////////////////
@ -60,183 +59,11 @@ NS_IMETHODIMP nsI18nCompatibility::CSIDtoCharsetName(PRUint16 csid, PRUnichar **
return NS_OK;
}
///////////////////////////////////////////////////////////////////////////////////////////
PRInt32 g_InstanceCount = 0;
PRInt32 g_LockCount = 0;
NS_IMETHODIMP
NS_NewI18nCompatibility(nsISupports* aOuter, const nsIID& aIID,
void** aResult)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsI18nCompatibility)
static nsModuleComponentInfo components[] =
{
nsresult rv;
if (!aResult) {
return NS_ERROR_INVALID_POINTER;
}
if (aOuter) {
*aResult = nsnull;
return NS_ERROR_NO_AGGREGATION;
}
nsI18nCompatibility * inst = new nsI18nCompatibility();
if (inst == NULL) {
*aResult = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
rv = inst->QueryInterface(aIID, aResult);
if (NS_FAILED(rv)) {
delete inst;
*aResult = nsnull;
}
return rv;
}
//----------------------------------------------------------------------------
class nsI18nCompModule : public nsIModule
{
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
private:
PRBool mInitialized;
void Shutdown();
public:
nsI18nCompModule();
virtual ~nsI18nCompModule();
nsresult Initialize();
protected:
nsCOMPtr<nsIGenericFactory> mFactory;
{ "I18n compatibility", NS_I18NCOMPATIBILITY_CID,
NS_I18NCOMPATIBILITY_PROGID, nsI18nCompatibilityConstructor}
};
static nsI18nCompModule * gModule = NULL;
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager * compMgr,
nsIFileSpec* location,
nsIModule** return_cobj)
{
nsresult rv = NS_OK;
NS_ENSURE_ARG_POINTER(return_cobj);
NS_ENSURE_FALSE(gModule, NS_ERROR_FAILURE);
// Create an initialize the module instance
nsI18nCompModule * m = new nsI18nCompModule();
if (!m) {
return NS_ERROR_OUT_OF_MEMORY;
}
// Increase refcnt and store away nsIModule interface to m in return_cobj
rv = m->QueryInterface(nsIModule::GetIID(), (void**)return_cobj);
if (NS_FAILED(rv)) {
delete m;
m = nsnull;
}
gModule = m; // WARNING: Weak Reference
return rv;
}
NS_IMPL_ISUPPORTS(nsI18nCompModule, nsIModule::GetIID())
nsI18nCompModule::nsI18nCompModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsI18nCompModule::~nsI18nCompModule()
{
Shutdown();
}
nsresult nsI18nCompModule::Initialize()
{
return NS_OK;
}
void nsI18nCompModule::Shutdown()
{
mFactory = nsnull;
}
NS_IMETHODIMP nsI18nCompModule::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;
if (!mInitialized) {
rv = Initialize();
if (NS_FAILED(rv)) {
return rv;
}
mInitialized = PR_TRUE;
}
nsCOMPtr<nsIGenericFactory> fact;
if (aClass.Equals(kI18nCompatibilityCID)) {
if (!mFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mFactory),
NS_NewI18nCompatibility);
}
fact = mFactory;
} else {
return NS_ERROR_FACTORY_NOT_REGISTERED;
}
if (fact) {
rv = fact->QueryInterface(aIID, r_classObj);
}
return rv;
}
NS_IMETHODIMP nsI18nCompModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv;
rv = aCompMgr->RegisterComponentSpec(kI18nCompatibilityCID,
"I18n compatibility",
NS_I18NCOMPATIBILITY_PROGID,
aPath,
PR_TRUE, PR_TRUE);
return rv;
}
NS_IMETHODIMP nsI18nCompModule::UnregisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
nsresult rv;
rv = aCompMgr->UnregisterComponentSpec(kI18nCompatibilityCID, aPath);
return rv;
}
NS_IMETHODIMP nsI18nCompModule::CanUnload(nsIComponentManager *aCompMgr,
PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
NS_IMPL_NSGETMODULE("I18nCompatibility", components)

View File

@ -19,7 +19,6 @@
*
* Contributor(s):
*/
#include "nsCOMPtr.h"
#include "nsIModule.h"
#include "nsIGenericFactory.h"
#include "nsLWBrkCIID.h"
@ -31,248 +30,11 @@
PRInt32 g_InstanceCount = 0;
PRInt32 g_LockCount = 0;
static NS_DEFINE_IID(kLWBrkCID, NS_LWBRK_CID);
NS_GENERIC_FACTORY_CONSTRUCTOR(nsLWBreakerFImp)
// Module implementation for the sample library
class nsLWBrkModule : public nsIModule
{
public:
nsLWBrkModule();
virtual ~nsLWBrkModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mFactory;
static nsModuleComponentInfo components[] = {
{ "Line and Word Breaker", NS_LWBRK_CID,
NS_LWBRK_PROGID, nsLWBreakerFImpConstructor}
};
//----------------------------------------------------------------------
// This function creates a new instance of nsLWBreakerFImp which is
// itself a factory. The callers know that and use it to create
// specific types of line/word breakers.
static NS_IMETHODIMP
CreateNewLWBrkThing(nsISupports* aOuter, REFNSIID aIID, void **aResult)
{
if (!aResult) {
return NS_ERROR_INVALID_POINTER;
}
if (aOuter) {
*aResult = nsnull;
return NS_ERROR_NO_AGGREGATION;
}
nsLWBreakerFImp* inst = new nsLWBreakerFImp();
if (!inst) {
*aResult = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult rv = inst->QueryInterface(aIID, aResult);
if (NS_FAILED(rv)) {
delete inst;
*aResult = nsnull;
}
return rv;
}
//----------------------------------------------------------------------
static NS_DEFINE_IID(kIModuleIID, NS_IMODULE_IID);
nsLWBrkModule::nsLWBrkModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsLWBrkModule::~nsLWBrkModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsLWBrkModule, kIModuleIID)
// Perform our one-time intialization for this module
nsresult
nsLWBrkModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsLWBrkModule::Shutdown()
{
// Release the factory object
mFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsLWBrkModule::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;
}
}
// Choose the appropriate factory, based on the desired instance
// class type (aClass).
nsCOMPtr<nsIGenericFactory> fact;
if (aClass.Equals(kLWBrkCID)) {
if (!mFactory) {
// Create a new factory-factory.
rv = NS_NewGenericFactory(getter_AddRefs(mFactory),
CreateNewLWBrkThing);
}
fact = mFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsLWBrkModule: 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[] = {
{ "Line and Word Breaker", &kLWBrkCID,
NS_LWBRK_PROGID, },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsLWBrkModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering lwbrk 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("nsLWBrkModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsLWBrkModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering sample 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("nsLWBrkModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsLWBrkModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = (g_InstanceCount == 0) && (g_LockCount == 0);
return NS_OK;
}
//----------------------------------------------------------------------
static nsLWBrkModule *gModule = NULL;
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager *servMgr,
nsIFileSpec* location,
nsIModule** return_cobj)
{
nsresult rv = NS_OK;
NS_ASSERTION(return_cobj, "Null argument");
NS_ASSERTION(gModule == NULL, "nsLWBrkModule: Module already created.");
// Create an initialize the layout module instance
nsLWBrkModule *m = new nsLWBrkModule();
if (!m) {
return NS_ERROR_OUT_OF_MEMORY;
}
// Increase refcnt and store away nsIModule interface to m in return_cobj
rv = m->QueryInterface(nsIModule::GetIID(), (void**)return_cobj);
if (NS_FAILED(rv)) {
delete m;
m = nsnull;
}
gModule = m; // WARNING: Weak Reference
return rv;
}
NS_IMPL_NSGETMODULE("nsLWBrkModule", components);

View File

@ -31,40 +31,6 @@
#include "nsSaveAsCharset.h"
#include "nsUUDll.h"
PRInt32 g_InstanceCount = 0;
PRInt32 g_LockCount = 0;
static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID);
static NS_DEFINE_CID(kHankakuToZenkakuCID, NS_HANKAKUTOZENKAKU_CID);
static NS_DEFINE_CID(kEntityConverterCID, NS_ENTITYCONVERTER_CID);
static NS_DEFINE_CID(kSaveAsCharsetCID, NS_SAVEASCHARSET_CID);
// Module implementation
class nsUcharUtilModule : public nsIModule
{
public:
nsUcharUtilModule();
virtual ~nsUcharUtilModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mHankakuToZenkakuFactory;
nsCOMPtr<nsIGenericFactory> mUnicharUtilFactory;
nsCOMPtr<nsIGenericFactory> mEntityConverterFactory;
nsCOMPtr<nsIGenericFactory> mSaveAsCharsetFactory;
};
//----------------------------------------------------------------------
// Functions used to create new instances of a given object by the
// generic factory.
@ -94,235 +60,30 @@ CreateNew##_name(nsISupports* aOuter, REFNSIID aIID, void **aResult) \
}
MAKE_CTOR(CaseConversion)
MAKE_CTOR(HankakuToZenkaku)
MAKE_CTOR(EntityConverter)
MAKE_CTOR(SaveAsCharset)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsCaseConversionImp2)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsEntityConverter)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSaveAsCharset)
//----------------------------------------------------------------------
// Since our class still refer to this two per dll global leave it here untill
// we change their code
static NS_DEFINE_IID(kIModuleIID, NS_IMODULE_IID);
PRInt32 g_InstanceCount = 0;
PRInt32 g_LockCount = 0;
nsUcharUtilModule::nsUcharUtilModule()
: mInitialized(PR_FALSE)
static nsModuleComponentInfo components[] =
{
NS_INIT_ISUPPORTS();
}
nsUcharUtilModule::~nsUcharUtilModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsUcharUtilModule, kIModuleIID)
// Perform our one-time intialization for this module
nsresult
nsUcharUtilModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsUcharUtilModule::Shutdown()
{
// Release the factory object
mHankakuToZenkakuFactory = nsnull;
mUnicharUtilFactory = nsnull;
mEntityConverterFactory = nsnull;
mSaveAsCharsetFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsUcharUtilModule::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;
}
}
// Choose the appropriate factory, based on the desired instance
// class type (aClass).
nsCOMPtr<nsIGenericFactory> fact;
if (aClass.Equals(kUnicharUtilCID)) {
if (!mUnicharUtilFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mUnicharUtilFactory),
CreateNewCaseConversion);
}
fact = mUnicharUtilFactory;
}
else
if (aClass.Equals(kEntityConverterCID)) {
if (!mEntityConverterFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mEntityConverterFactory),
CreateNewEntityConverter);
}
fact = mEntityConverterFactory;
}
else
if (aClass.Equals(kSaveAsCharsetCID)) {
if (!mSaveAsCharsetFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mSaveAsCharsetFactory),
CreateNewSaveAsCharset);
}
fact = mSaveAsCharsetFactory;
}
else
if (aClass.Equals(kHankakuToZenkakuCID)) {
if(!mHankakuToZenkakuFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mHankakuToZenkakuFactory),
CreateNewHankakuToZenkaku);
}
fact = mHankakuToZenkakuFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsUcharUtilModule: 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;
{ "Unichar Utility", NS_UNICHARUTIL_CID,
NS_UNICHARUTIL_PROGID, nsCaseConversionImp2Constructor},
{ "Unicode To Entity Converter", NS_ENTITYCONVERTER_CID,
NS_ENTITYCONVERTER_PROGID, nsEntityConverterConstructor },
{ "Unicode To Charset Converter", NS_SAVEASCHARSET_CID,
NS_SAVEASCHARSET_PROGID, nsSaveAsCharsetConstructor},
{ "Japanese Hankaku To Zenkaku", NS_HANKAKUTOZENKAKU_CID,
NS_HANKAKUTOZENKAKU_PROGID, CreateNewHankakuToZenkaku},
};
// The list of components we register
static Components gComponents[] = {
{ "Unichar Utility", &kUnicharUtilCID,
NS_UNICHARUTIL_PROGID, },
{ "Unicode To Entity Converter", &kEntityConverterCID,
NS_ENTITYCONVERTER_PROGID, },
{ "Unicode To Charset Converter", &kSaveAsCharsetCID,
NS_SAVEASCHARSET_PROGID, },
{ "Japanese Hankaku To Zenkaku", &kHankakuToZenkakuCID,
NS_HANKAKUTOZENKAKU_PROGID, },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMPL_NSGETMODULE("UcharUtil", components)
NS_IMETHODIMP
nsUcharUtilModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering UcharUtil 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("nsUcharUtilModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsUcharUtilModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering UcharUtil 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("nsUcharUtilModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsUcharUtilModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = (g_InstanceCount == 0) && (g_LockCount == 0);
return NS_OK;
}
//----------------------------------------------------------------------
static nsUcharUtilModule *gModule = NULL;
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager *servMgr,
nsIFileSpec* location,
nsIModule** return_cobj)
{
nsresult rv = NS_OK;
NS_ASSERTION(return_cobj, "Null argument");
NS_ASSERTION(gModule == NULL, "nsUcharUtilModule: Module already created.");
// Create and initialize the module instance
nsUcharUtilModule *m = new nsUcharUtilModule();
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;
}