Convert to a module from a component

This commit is contained in:
kipp%netscape.com 1999-09-30 21:47:04 +00:00
parent 7a4fd32215
commit 0ca8147bb6

View File

@ -655,8 +655,229 @@ CreateJSComponentLoader(nsISupports *aOuter, const nsIID &iid, void **result)
return rv;
}
//----------------------------------------------------------------------
static NS_DEFINE_CID(kmozJSComponentLoaderCID, MOZJSCOMPONENTLOADER_CID);
// Module implementation for the sample library
class nsJSComponentLoaderModule : public nsIModule
{
public:
nsJSComponentLoaderModule();
virtual ~nsJSComponentLoaderModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mFactory;
};
static NS_DEFINE_IID(kIModuleIID, NS_IMODULE_IID);
nsJSComponentLoaderModule::nsJSComponentLoaderModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsJSComponentLoaderModule::~nsJSComponentLoaderModule()
{
Shutdown();
}
NS_IMPL_ISUPPORTS(nsJSComponentLoaderModule, kIModuleIID)
// Perform our one-time intialization for this module
nsresult
nsJSComponentLoaderModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsJSComponentLoaderModule::Shutdown()
{
// Release the factory object
mFactory = nsnull;
}
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsJSComponentLoaderModule::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(kmozJSComponentLoaderCID)) {
if (!mFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mFactory),
CreateJSComponentLoader);
}
fact = mFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsJSComponentLoaderModule: 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[] = {
{ "JavaScript Component Loader", &kmozJSComponentLoaderCID,
mozJSComponentLoaderProgID, },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsJSComponentLoaderModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering JSComponentLoader 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("nsJSComponentLoaderModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
rv = aCompMgr->RegisterComponentLoader(jsComponentTypeName,
mozJSComponentLoaderProgID,
PR_TRUE);
return rv;
}
NS_IMETHODIMP
nsJSComponentLoaderModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering JSComponentLoader 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("nsJSComponentLoaderModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsJSComponentLoaderModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsJSComponentLoaderModule *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, "nsJSComponentLoaderModule: Module already created.");
// Create an initialize the layout module instance
nsJSComponentLoaderModule *m = new nsJSComponentLoaderModule();
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;
}
#if 0
extern "C" nsresult
NSGetFactory(nsISupports* servMgr, const nsCID &aClass, const char *aClassName,
const char *aProgID, nsIFactory **aFactory)
@ -704,3 +925,4 @@ NSRegisterSelf(nsISupports *aServMgr, const char *aLocation)
mozJSComponentLoaderProgID,
PR_TRUE);
}
#endif