Bug #14034: Convert to nsIModule to prepare for memory leak fixing. Also fix leak of search stream listener. Review: sdagley@netscape.com

This commit is contained in:
rjc%netscape.com 1999-10-06 01:37:47 +00:00
Родитель 0cd13bcaa0
Коммит a429ff2900
3 изменённых файлов: 431 добавлений и 96 удалений

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

@ -117,17 +117,41 @@ public:
InternetSearchDataSourceCallback(nsIRDFDataSource *ds, nsIRDFResource *parent, nsIRDFResource *engine);
virtual ~InternetSearchDataSourceCallback(void);
NS_METHOD Init();
// nsIStreamObserver methods:
NS_DECL_NSISTREAMOBSERVER
// nsIStreamObserver methods:
NS_DECL_NSISTREAMOBSERVER
// nsIStreamListener methods:
NS_DECL_NSISTREAMLISTENER
// nsIStreamListener methods:
NS_DECL_NSISTREAMLISTENER
};
class InternetSearchDataSource : public nsIRDFDataSource
nsresult
NS_NewInternetSearchStreamListener(nsIRDFDataSource *aDataSource, nsIRDFResource *aParent,
nsIRDFResource *aEngine, nsIStreamListener** aResult)
{
InternetSearchDataSourceCallback* result =
new InternetSearchDataSourceCallback(aDataSource, aParent, aEngine);
if (! result)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = result->Init();
if (NS_FAILED(rv)) {
delete result;
return rv;
}
NS_ADDREF(result);
*aResult = result;
return NS_OK;
}
class InternetSearchDataSource : public nsIInternetSearchService, public nsIRDFDataSource
{
private:
static PRInt32 gRefCnt;
@ -164,16 +188,17 @@ static nsresult GetData(nsString data, char *sectionToFind, char *attribToFind,
nsresult GetInputs(nsString data, nsString text, nsString &input);
nsresult GetURL(nsIRDFResource *source, nsIRDFLiteral** aResult);
InternetSearchDataSource(void);
virtual ~InternetSearchDataSource(void);
NS_METHOD Init();
public:
friend class InternetSearchDataSourceCallback;
NS_DECL_ISUPPORTS
InternetSearchDataSource(void);
virtual ~InternetSearchDataSource(void);
nsresult Init();
NS_DECL_NSIINTERNETSEARCHSERVICE
// nsIRDFDataSource methods
@ -371,12 +396,13 @@ InternetSearchDataSource::~InternetSearchDataSource (void)
// NS_IMPL_ISUPPORTS(InternetSearchDataSource, InternetSearchDataSource::GetIID());
NS_IMPL_ISUPPORTS2(InternetSearchDataSource, nsIInternetSearchService, nsIRDFDataSource);
/*
NS_IMPL_ADDREF(InternetSearchDataSource);
NS_IMPL_RELEASE(InternetSearchDataSource);
NS_IMETHODIMP
InternetSearchDataSource::QueryInterface(REFNSIID aIID, void **aResult)
{
@ -400,10 +426,11 @@ InternetSearchDataSource::QueryInterface(REFNSIID aIID, void **aResult)
NS_ADDREF(this);
return NS_OK;
}
*/
nsresult
NS_METHOD
InternetSearchDataSource::Init()
{
nsresult rv = NS_ERROR_OUT_OF_MEMORY;
@ -1150,8 +1177,9 @@ InternetSearchDataSource::DoSearch(nsIRDFResource *source, nsIRDFResource *engin
if (!searchURL)
return(NS_ERROR_NULL_POINTER);
InternetSearchDataSourceCallback *callback = new InternetSearchDataSourceCallback(mInner, source, engine);
if (nsnull != callback)
nsCOMPtr<nsIStreamListener> callback;
rv = NS_NewInternetSearchStreamListener(mInner, source, engine, getter_AddRefs(callback));
if (NS_SUCCEEDED(rv) && (nsnull != callback))
{
nsCOMPtr<nsIURI> url;
if (NS_SUCCEEDED(rv = NS_NewURI(getter_AddRefs(url), (const char*) searchURL)))
@ -1159,7 +1187,7 @@ InternetSearchDataSource::DoSearch(nsIRDFResource *source, nsIRDFResource *engin
if (method.EqualsIgnoreCase("post"))
{
nsCOMPtr<nsIChannel> channel;
// XXX: Null LoadGroup ?
// XXX: Null LoadGroup ?
if (NS_SUCCEEDED(rv = NS_OpenURI(getter_AddRefs(channel), url, nsnull)))
{
nsCOMPtr<nsIHTTPChannel> httpChannel = do_QueryInterface(channel);
@ -1692,7 +1720,46 @@ InternetSearchDataSourceCallback::InternetSearchDataSourceCallback(nsIRDFDataSou
NS_IF_ADDREF(mDataSource);
NS_IF_ADDREF(mParent);
NS_IF_ADDREF(mEngine);
}
InternetSearchDataSourceCallback::~InternetSearchDataSourceCallback()
{
NS_IF_RELEASE(mDataSource);
NS_IF_RELEASE(mParent);
NS_IF_RELEASE(mEngine);
if (mLine)
{
nsCRT::free(mLine);
mLine = nsnull;
}
if (--gRefCnt == 0)
{
NS_IF_RELEASE(kNC_LastSearchRoot);
NS_IF_RELEASE(kNC_Child);
NS_IF_RELEASE(kNC_URL);
NS_IF_RELEASE(kNC_Name);
NS_IF_RELEASE(kNC_Data);
NS_IF_RELEASE(kNC_Relevance);
NS_IF_RELEASE(kNC_RelevanceSort);
NS_IF_RELEASE(kNC_Site);
NS_IF_RELEASE(kNC_Engine);
NS_IF_RELEASE(kNC_loading);
NS_IF_RELEASE(kNC_HTML);
NS_IF_RELEASE(kNC_Banner);
nsServiceManager::ReleaseService(kRDFServiceCID, gRDFService);
}
}
NS_METHOD
InternetSearchDataSourceCallback::Init()
{
if (gRefCnt++ == 0)
{
nsresult rv = nsServiceManager::GetService(kRDFServiceCID,
@ -1714,37 +1781,7 @@ InternetSearchDataSourceCallback::InternetSearchDataSourceCallback(nsIRDFDataSou
gRDFService->GetResource(NC_NAMESPACE_URI "HTML", &kNC_HTML);
gRDFService->GetResource(NC_NAMESPACE_URI "Banner", &kNC_Banner);
}
}
InternetSearchDataSourceCallback::~InternetSearchDataSourceCallback()
{
NS_IF_RELEASE(mDataSource);
NS_IF_RELEASE(mParent);
NS_IF_RELEASE(mEngine);
if (mLine)
{
nsCRT::free(mLine);
mLine = nsnull;
}
if (--gRefCnt == 0)
{
NS_RELEASE(kNC_LastSearchRoot);
NS_RELEASE(kNC_Child);
NS_RELEASE(kNC_URL);
NS_RELEASE(kNC_Name);
NS_RELEASE(kNC_Data);
NS_RELEASE(kNC_Relevance);
NS_RELEASE(kNC_RelevanceSort);
NS_RELEASE(kNC_Site);
NS_RELEASE(kNC_Engine);
NS_RELEASE(kNC_loading);
NS_RELEASE(kNC_HTML);
NS_RELEASE(kNC_Banner);
}
return(NS_OK);
}
@ -2424,7 +2461,7 @@ InternetSearchDataSourceCallback::OnStopRequest(nsIChannel* channel, nsISupports
NS_IMPL_ISUPPORTS(InternetSearchDataSourceCallback, InternetSearchDataSourceCallback::GetIID());
NS_IMPL_ISUPPORTS(InternetSearchDataSourceCallback, nsIStreamListener::GetIID());

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

@ -117,17 +117,41 @@ public:
InternetSearchDataSourceCallback(nsIRDFDataSource *ds, nsIRDFResource *parent, nsIRDFResource *engine);
virtual ~InternetSearchDataSourceCallback(void);
NS_METHOD Init();
// nsIStreamObserver methods:
NS_DECL_NSISTREAMOBSERVER
// nsIStreamObserver methods:
NS_DECL_NSISTREAMOBSERVER
// nsIStreamListener methods:
NS_DECL_NSISTREAMLISTENER
// nsIStreamListener methods:
NS_DECL_NSISTREAMLISTENER
};
class InternetSearchDataSource : public nsIRDFDataSource
nsresult
NS_NewInternetSearchStreamListener(nsIRDFDataSource *aDataSource, nsIRDFResource *aParent,
nsIRDFResource *aEngine, nsIStreamListener** aResult)
{
InternetSearchDataSourceCallback* result =
new InternetSearchDataSourceCallback(aDataSource, aParent, aEngine);
if (! result)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = result->Init();
if (NS_FAILED(rv)) {
delete result;
return rv;
}
NS_ADDREF(result);
*aResult = result;
return NS_OK;
}
class InternetSearchDataSource : public nsIInternetSearchService, public nsIRDFDataSource
{
private:
static PRInt32 gRefCnt;
@ -164,16 +188,17 @@ static nsresult GetData(nsString data, char *sectionToFind, char *attribToFind,
nsresult GetInputs(nsString data, nsString text, nsString &input);
nsresult GetURL(nsIRDFResource *source, nsIRDFLiteral** aResult);
InternetSearchDataSource(void);
virtual ~InternetSearchDataSource(void);
NS_METHOD Init();
public:
friend class InternetSearchDataSourceCallback;
NS_DECL_ISUPPORTS
InternetSearchDataSource(void);
virtual ~InternetSearchDataSource(void);
nsresult Init();
NS_DECL_NSIINTERNETSEARCHSERVICE
// nsIRDFDataSource methods
@ -371,12 +396,13 @@ InternetSearchDataSource::~InternetSearchDataSource (void)
// NS_IMPL_ISUPPORTS(InternetSearchDataSource, InternetSearchDataSource::GetIID());
NS_IMPL_ISUPPORTS2(InternetSearchDataSource, nsIInternetSearchService, nsIRDFDataSource);
/*
NS_IMPL_ADDREF(InternetSearchDataSource);
NS_IMPL_RELEASE(InternetSearchDataSource);
NS_IMETHODIMP
InternetSearchDataSource::QueryInterface(REFNSIID aIID, void **aResult)
{
@ -400,10 +426,11 @@ InternetSearchDataSource::QueryInterface(REFNSIID aIID, void **aResult)
NS_ADDREF(this);
return NS_OK;
}
*/
nsresult
NS_METHOD
InternetSearchDataSource::Init()
{
nsresult rv = NS_ERROR_OUT_OF_MEMORY;
@ -1150,8 +1177,9 @@ InternetSearchDataSource::DoSearch(nsIRDFResource *source, nsIRDFResource *engin
if (!searchURL)
return(NS_ERROR_NULL_POINTER);
InternetSearchDataSourceCallback *callback = new InternetSearchDataSourceCallback(mInner, source, engine);
if (nsnull != callback)
nsCOMPtr<nsIStreamListener> callback;
rv = NS_NewInternetSearchStreamListener(mInner, source, engine, getter_AddRefs(callback));
if (NS_SUCCEEDED(rv) && (nsnull != callback))
{
nsCOMPtr<nsIURI> url;
if (NS_SUCCEEDED(rv = NS_NewURI(getter_AddRefs(url), (const char*) searchURL)))
@ -1159,7 +1187,7 @@ InternetSearchDataSource::DoSearch(nsIRDFResource *source, nsIRDFResource *engin
if (method.EqualsIgnoreCase("post"))
{
nsCOMPtr<nsIChannel> channel;
// XXX: Null LoadGroup ?
// XXX: Null LoadGroup ?
if (NS_SUCCEEDED(rv = NS_OpenURI(getter_AddRefs(channel), url, nsnull)))
{
nsCOMPtr<nsIHTTPChannel> httpChannel = do_QueryInterface(channel);
@ -1692,7 +1720,46 @@ InternetSearchDataSourceCallback::InternetSearchDataSourceCallback(nsIRDFDataSou
NS_IF_ADDREF(mDataSource);
NS_IF_ADDREF(mParent);
NS_IF_ADDREF(mEngine);
}
InternetSearchDataSourceCallback::~InternetSearchDataSourceCallback()
{
NS_IF_RELEASE(mDataSource);
NS_IF_RELEASE(mParent);
NS_IF_RELEASE(mEngine);
if (mLine)
{
nsCRT::free(mLine);
mLine = nsnull;
}
if (--gRefCnt == 0)
{
NS_IF_RELEASE(kNC_LastSearchRoot);
NS_IF_RELEASE(kNC_Child);
NS_IF_RELEASE(kNC_URL);
NS_IF_RELEASE(kNC_Name);
NS_IF_RELEASE(kNC_Data);
NS_IF_RELEASE(kNC_Relevance);
NS_IF_RELEASE(kNC_RelevanceSort);
NS_IF_RELEASE(kNC_Site);
NS_IF_RELEASE(kNC_Engine);
NS_IF_RELEASE(kNC_loading);
NS_IF_RELEASE(kNC_HTML);
NS_IF_RELEASE(kNC_Banner);
nsServiceManager::ReleaseService(kRDFServiceCID, gRDFService);
}
}
NS_METHOD
InternetSearchDataSourceCallback::Init()
{
if (gRefCnt++ == 0)
{
nsresult rv = nsServiceManager::GetService(kRDFServiceCID,
@ -1714,37 +1781,7 @@ InternetSearchDataSourceCallback::InternetSearchDataSourceCallback(nsIRDFDataSou
gRDFService->GetResource(NC_NAMESPACE_URI "HTML", &kNC_HTML);
gRDFService->GetResource(NC_NAMESPACE_URI "Banner", &kNC_Banner);
}
}
InternetSearchDataSourceCallback::~InternetSearchDataSourceCallback()
{
NS_IF_RELEASE(mDataSource);
NS_IF_RELEASE(mParent);
NS_IF_RELEASE(mEngine);
if (mLine)
{
nsCRT::free(mLine);
mLine = nsnull;
}
if (--gRefCnt == 0)
{
NS_RELEASE(kNC_LastSearchRoot);
NS_RELEASE(kNC_Child);
NS_RELEASE(kNC_URL);
NS_RELEASE(kNC_Name);
NS_RELEASE(kNC_Data);
NS_RELEASE(kNC_Relevance);
NS_RELEASE(kNC_RelevanceSort);
NS_RELEASE(kNC_Site);
NS_RELEASE(kNC_Engine);
NS_RELEASE(kNC_loading);
NS_RELEASE(kNC_HTML);
NS_RELEASE(kNC_Banner);
}
return(NS_OK);
}
@ -2424,7 +2461,7 @@ InternetSearchDataSourceCallback::OnStopRequest(nsIChannel* channel, nsISupports
NS_IMPL_ISUPPORTS(InternetSearchDataSourceCallback, InternetSearchDataSourceCallback::GetIID());
NS_IMPL_ISUPPORTS(InternetSearchDataSourceCallback, nsIStreamListener::GetIID());

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

@ -19,6 +19,7 @@
#include "nsRDFCID.h"
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsIModule.h"
#include "nsIFactory.h"
#include "nsIGenericFactory.h"
#include "nsIComponentManager.h"
@ -36,6 +37,264 @@ NS_IMETHODIMP NS_NewInternetSearchService(nsISupports* aOuter, REFNSIID aIID, vo
#if 1
// Module implementation
class nsSearchModule : public nsIModule
{
public:
nsSearchModule();
virtual ~nsSearchModule();
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
protected:
nsresult Initialize();
void Shutdown();
PRBool mInitialized;
nsCOMPtr<nsIGenericFactory> mInternetSearchFactory;
nsCOMPtr<nsIGenericFactory> mLocalSearchFactory;
};
nsSearchModule::nsSearchModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsSearchModule::~nsSearchModule()
{
Shutdown();
}
// Perform our one-time intialization for this module
nsresult
nsSearchModule::Initialize()
{
if (mInitialized) {
return NS_OK;
}
mInitialized = PR_TRUE;
return NS_OK;
}
// Shutdown this module, releasing all of the module resources
void
nsSearchModule::Shutdown()
{
// Release the factory object
mInternetSearchFactory = nsnull;
mLocalSearchFactory = nsnull;
}
NS_IMPL_ISUPPORTS(nsSearchModule, NS_GET_IID(nsIModule))
// Create a factory object for creating instances of aClass.
NS_IMETHODIMP
nsSearchModule::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(kInternetSearchServiceCID))
{
if (!mInternetSearchFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mInternetSearchFactory),
NS_NewInternetSearchService);
}
fact = mInternetSearchFactory;
}
else if (aClass.Equals(kLocalSearchServiceCID))
{
if (!mLocalSearchFactory) {
rv = NS_NewGenericFactory(getter_AddRefs(mLocalSearchFactory),
NS_NewLocalSearchService);
}
fact = mLocalSearchFactory;
}
else {
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
#ifdef DEBUG
char* cs = aClass.ToString();
printf("+++ nsSearchModule: 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[] = {
{ "LocalSearch", &kLocalSearchServiceCID, NS_LOCALSEARCH_SERVICE_PROGID },
{ "LocalSearch", &kLocalSearchServiceCID, NS_LOCALSEARCH_DATASOURCE_PROGID },
{ "InternetSearch", &kInternetSearchServiceCID, NS_INTERNETSEARCH_DATASOURCE_PROGID },
{ "InternetSearch", &kInternetSearchServiceCID, NS_INTERNETSEARCH_DATASOURCE_PROGID },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
NS_IMETHODIMP
nsSearchModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult rv = NS_OK;
#ifdef DEBUG
printf("*** Registering nsSearchModule\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("nsSearchModule: unable to register %s component => %x\n",
cp->mDescription, rv);
#endif
break;
}
cp++;
}
return rv;
}
NS_IMETHODIMP
nsSearchModule::UnregisterSelf(nsIComponentManager* aCompMgr,
nsIFileSpec* aPath,
const char* registryLocation)
{
#ifdef DEBUG
printf("*** Unregistering nsSearchModule\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("nsSearchModule: unable to unregister %s component => %x\n",
cp->mDescription, rv);
#endif
}
cp++;
}
return NS_OK;
}
NS_IMETHODIMP
nsSearchModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = PR_FALSE;
return NS_ERROR_FAILURE;
}
//----------------------------------------------------------------------
static nsSearchModule *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
nsSearchModule *m = new nsSearchModule();
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
////////////////////////////////////////////////////////////////////////
// Component Exports
@ -135,3 +394,5 @@ NSUnregisterSelf(nsISupports* aServMgr, const char* aPath)
return NS_OK;
}
#endif