Modifying sample to use NS_MODULE macros. r=alecf
This commit is contained in:
@@ -19,257 +19,40 @@
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsIModule.h"
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsISample.h"
|
||||
|
||||
static NS_DEFINE_CID(kSampleCID, NS_SAMPLE_CID);
|
||||
#include "nsSample.h"
|
||||
|
||||
// Module implementation
|
||||
class nsSampleModule : public nsIModule
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Define the contructor function for the object nsSample
|
||||
//
|
||||
// What this does is defines a functions nsSampleConstructor which we
|
||||
// will specific in the nsModuleComponentInfo table. This functions will
|
||||
// be used the generic factory to create an instance of nsSample.
|
||||
//
|
||||
// NOTE: This creates an instance of nsSample by using the default
|
||||
// constructor.
|
||||
//
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSampleImpl)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Define a table of CIDs implemented by this module along with other
|
||||
// information like the function to create an instance, progid, and
|
||||
// class name.
|
||||
//
|
||||
static nsModuleComponentInfo components[] =
|
||||
{
|
||||
public:
|
||||
nsSampleModule();
|
||||
virtual ~nsSampleModule();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_DECL_NSIMODULE
|
||||
|
||||
protected:
|
||||
nsresult Initialize();
|
||||
|
||||
void Shutdown();
|
||||
|
||||
PRBool mInitialized;
|
||||
nsCOMPtr<nsIGenericFactory> mFactory;
|
||||
{ NS_SAMPLE_CID, &nsSampleImplConstructor, NS_SAMPLE_PROGID,
|
||||
"Sample Component" },
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Implement the Module object
|
||||
//
|
||||
NS_IMPL_MODULE(nsSampleModule, components)
|
||||
|
||||
// Functions used to create new instances of a given object by the
|
||||
// generic factory.
|
||||
|
||||
#define MAKE_CTOR(_name) \
|
||||
static NS_IMETHODIMP \
|
||||
CreateNew##_name(nsISupports* aOuter, REFNSIID aIID, void **aResult) \
|
||||
{ \
|
||||
if (!aResult) { \
|
||||
return NS_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
if (aOuter) { \
|
||||
*aResult = nsnull; \
|
||||
return NS_ERROR_NO_AGGREGATION; \
|
||||
} \
|
||||
nsCOMPtr<nsI##_name> inst; \
|
||||
nsresult rv = NS_New##_name(getter_AddRefs(inst)); \
|
||||
if (NS_FAILED(rv)) { \
|
||||
*aResult = nsnull; \
|
||||
return rv; \
|
||||
} \
|
||||
rv = inst->QueryInterface(aIID, aResult); \
|
||||
if (NS_FAILED(rv)) { \
|
||||
*aResult = nsnull; \
|
||||
} \
|
||||
return rv; \
|
||||
}
|
||||
|
||||
MAKE_CTOR(Sample)
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
nsSampleModule::nsSampleModule()
|
||||
: mInitialized(PR_FALSE)
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
}
|
||||
|
||||
nsSampleModule::~nsSampleModule()
|
||||
{
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(nsSampleModule, NS_GET_IID(nsIModule))
|
||||
|
||||
// Perform our one-time intialization for this module
|
||||
nsresult
|
||||
nsSampleModule::Initialize()
|
||||
{
|
||||
if (mInitialized) {
|
||||
return NS_OK;
|
||||
}
|
||||
mInitialized = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// Shutdown this module, releasing all of the module resources
|
||||
void
|
||||
nsSampleModule::Shutdown()
|
||||
{
|
||||
// Release the factory object
|
||||
mFactory = nsnull;
|
||||
}
|
||||
|
||||
// Create a factory object for creating instances of aClass.
|
||||
NS_IMETHODIMP
|
||||
nsSampleModule::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(kSampleCID)) {
|
||||
if (!mFactory) {
|
||||
// Create and save away the factory object for creating
|
||||
// new instances of Sample. 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),
|
||||
CreateNewSample);
|
||||
}
|
||||
fact = mFactory;
|
||||
}
|
||||
else {
|
||||
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
|
||||
#ifdef DEBUG
|
||||
char* cs = aClass.ToString();
|
||||
printf("+++ nsSampleModule: 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[] = {
|
||||
{ "Sample World Component", &kSampleCID,
|
||||
"component://mozilla/sample/sample-world", },
|
||||
};
|
||||
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSampleModule::RegisterSelf(nsIComponentManager *aCompMgr,
|
||||
nsIFileSpec* aPath,
|
||||
const char* registryLocation,
|
||||
const char* componentType)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("*** Registering sample 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("nsSampleModule: unable to register %s component => %x\n",
|
||||
cp->mDescription, rv);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSampleModule::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("nsSampleModule: unable to unregister %s component => %x\n",
|
||||
cp->mDescription, rv);
|
||||
#endif
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSampleModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
|
||||
{
|
||||
if (!okToUnload) {
|
||||
return NS_ERROR_INVALID_POINTER;
|
||||
}
|
||||
*okToUnload = PR_FALSE;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
static nsSampleModule *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_FALSE(gModule, NS_ERROR_FAILURE);
|
||||
|
||||
// Create and initialize the module instance
|
||||
nsSampleModule *m = new nsSampleModule();
|
||||
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;
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Implment the NSGetModule() exported function for your module
|
||||
//
|
||||
NS_IMPL_NSGETMODULE(nsSampleModule)
|
||||
|
||||
Reference in New Issue
Block a user