XPIDL-ifying nsIObserver/nsIObserverService

This commit is contained in:
law%netscape.com 1999-05-29 00:51:17 +00:00
Родитель 9fdb531955
Коммит 0b7b490358
18 изменённых файлов: 202 добавлений и 198 удалений

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

@ -54,8 +54,8 @@ public:
PRUint32 numOfAttributes, const PRUnichar* nameArray[],
const PRUnichar* valueArray[]);
NS_IMETHOD Notify(nsISupports** result);
NS_DECL_IOBSERVER
NS_DECL_ISUPPORTS

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

@ -57,9 +57,6 @@ static NS_DEFINE_IID(kIPICSIID, NS_IPICS_IID);
static NS_DEFINE_IID(kIPrefIID, NS_IPREF_IID);
static NS_DEFINE_CID(kPrefCID, NS_PREF_CID);
static NS_DEFINE_IID(kIObserverServiceIID, NS_IOBSERVERSERVICE_IID);
static NS_DEFINE_IID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
static NS_DEFINE_IID(kDocLoaderServiceCID, NS_DOCUMENTLOADER_SERVICE_CID);
static NS_DEFINE_IID(kIDocumentLoaderIID, NS_IDOCUMENTLOADER_IID);
@ -527,12 +524,13 @@ nsPICS::Init()
if (NS_FAILED(rv = NS_NewPICSElementObserver(&mPICSElementObserver)))
return rv;
rv = nsServiceManager::GetService(kObserverServiceCID,
rv = nsServiceManager::GetService(NS_OBSERVERSERVICE_PROGID,
nsIObserverService::GetIID(),
(nsISupports **)&anObserverService);
if(rv == NS_OK) {
rv = anObserverService->AddObserver(&mPICSElementObserver, &aTopic);
rv = anObserverService->AddObserver(mPICSElementObserver, aTopic);
nsServiceManager::ReleaseService( NS_OBSERVERSERVICE_PROGID, anObserverService );
if (NS_FAILED(rv))
return rv;

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

@ -176,7 +176,7 @@ NS_IMETHODIMP nsPICSElementObserver::Notify(PRUint32 aDocumentID, eHTMLTags aTag
}
NS_IMETHODIMP nsPICSElementObserver::Notify(nsISupports** result)
NS_IMETHODIMP nsPICSElementObserver::Observe(nsISupports*, const PRUnichar*, const PRUnichar*)
{
return NS_ERROR_NOT_IMPLEMENTED;
}

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

@ -31,9 +31,6 @@ static NS_DEFINE_CID(kPrefCID, NS_PREF_CID);
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
static NS_DEFINE_CID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
static NS_DEFINE_CID(kObserverCID, NS_OBSERVER_CID);
static NS_DEFINE_IID(kCParserIID, NS_IPARSER_IID);
static NS_DEFINE_IID(kCParserCID, NS_PARSER_IID);
@ -86,12 +83,10 @@ PRInt32 main(PRInt32 argc, char *argv[])
if (res == NS_OK) {
// Load preferences
nsComponentManager::RegisterComponent(kPrefCID, NULL, NULL, PREF_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kObserverServiceCID, "ObserverService", NS_OBSERVERSERVICE_PROGID, BASE_DLL,PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kObserverCID, "Observer", NS_OBSERVER_PROGID, BASE_DLL,PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kNetServiceCID, NULL, NULL, NETLIB_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kCParserCID, NULL, NULL, RAPTORHTMLPARS_DLL, PR_FALSE, PR_FALSE);
res = nsServiceManager::GetService(kObserverServiceCID,
res = nsServiceManager::GetService(NS_OBSERVERSERVICE_PROGID,
nsIObserverService::GetIID(),
(nsISupports **)&anObserverService);

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

@ -37,9 +37,7 @@
#include "nsIContentViewerContainer.h"
static NS_DEFINE_IID(kIElementObserverIID, NS_IELEMENTOBSERVER_IID);
static NS_DEFINE_IID(kIObserverIID, NS_IOBSERVER_IID);
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
static NS_DEFINE_IID(kIMetaCharsetServiceIID, NS_IMETA_CHARSET_SERVICE_IID);
static NS_DEFINE_IID(kDocLoaderServiceCID, NS_DOCUMENTLOADER_SERVICE_CID);
@ -79,7 +77,7 @@ public:
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
/* methode for nsIObserver */
NS_IMETHOD Notify(nsISupports** result);
NS_DECL_IOBSERVER
/* methode for nsIMetaCharsetService */
NS_IMETHOD Start();
@ -123,7 +121,7 @@ NS_IMETHODIMP nsMetaCharsetObserver::QueryInterface(REFNSIID aIID, void** aInsta
NS_ADDREF_THIS();
return NS_OK;
}
if( aIID.Equals ( kIObserverIID )) {
if( aIID.Equals ( nsIObserver::GetIID() )) {
*aInstancePtr = (void*) ((nsIObserver*) this);
NS_ADDREF_THIS();
return NS_OK;
@ -320,7 +318,7 @@ done:
return res;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP nsMetaCharsetObserver::Notify(nsISupports** result)
NS_IMETHODIMP nsMetaCharsetObserver::Observe(nsISupports*, const PRUnichar*, const PRUnichar*)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
@ -331,15 +329,15 @@ NS_IMETHODIMP nsMetaCharsetObserver::Start()
nsAutoString htmlTopic("htmlparser");
nsIObserverService* anObserverService = nsnull;
res = nsServiceManager::GetService(kObserverServiceCID,
res = nsServiceManager::GetService(NS_OBSERVERSERVICE_PROGID,
nsIObserverService::GetIID(),
(nsISupports**) &anObserverService);
if(NS_FAILED(res))
goto done;
res = anObserverService->AddObserver(&mHack, &htmlTopic);
res = anObserverService->AddObserver(mHack, htmlTopic);
nsServiceManager::ReleaseService(kObserverServiceCID,
nsServiceManager::ReleaseService(NS_OBSERVERSERVICE_PROGID,
anObserverService);
done:
return res;
@ -351,15 +349,15 @@ NS_IMETHODIMP nsMetaCharsetObserver::End()
nsAutoString htmlTopic("htmlparser");
nsIObserverService* anObserverService = nsnull;
res = nsServiceManager::GetService(kObserverServiceCID,
res = nsServiceManager::GetService(NS_OBSERVERSERVICE_PROGID,
nsIObserverService::GetIID(),
(nsISupports**) &anObserverService);
if(NS_FAILED(res))
goto done;
res = anObserverService->RemoveObserver(&mHack, &htmlTopic);
res = anObserverService->RemoveObserver(mHack, htmlTopic);
nsServiceManager::ReleaseService(kObserverServiceCID,
nsServiceManager::ReleaseService(NS_OBSERVERSERVICE_PROGID,
anObserverService);
done:
return res;

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

@ -29,7 +29,7 @@
#include "nsSpecialSystemDirectory.h"
#include "nsIThread.h"
#include "nsDeque.h"
#include "nsIObserver.h"
#include "nsObserver.h"
#include "nsTraceRefcnt.h"
#include "nsXPIDLString.h"
#include "nsIEnumerator.h"

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

@ -52,8 +52,6 @@ static NS_DEFINE_CID(kArenaCID, NS_ARENA_CID);
static NS_DEFINE_CID(kBufferCID, NS_BUFFER_CID);
static NS_DEFINE_CID(kByteBufferCID, NS_BYTEBUFFER_CID);
static NS_DEFINE_CID(kPageManagerCID, NS_PAGEMANAGER_CID);
static NS_DEFINE_CID(kObserverCID, NS_OBSERVER_CID);
static NS_DEFINE_CID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
static NS_DEFINE_CID(kPropertiesCID, NS_PROPERTIES_CID);
static NS_DEFINE_CID(kSupportsArrayCID, NS_SUPPORTSARRAY_CID);
static NS_DEFINE_CID(kUnicharBufferCID, NS_UNICHARBUFFER_CID);
@ -216,13 +214,13 @@ nsresult NS_InitXPCOM(nsIServiceManager* *result)
nsSupportsArray::Create);
if (NS_FAILED(rv)) return rv;
rv = RegisterGenericFactory(compMgr, kObserverCID,
rv = RegisterGenericFactory(compMgr, nsObserver::GetCID(),
NS_OBSERVER_CLASSNAME,
NS_OBSERVER_PROGID,
nsObserver::Create);
if (NS_FAILED(rv)) return rv;
rv = RegisterGenericFactory(compMgr, kObserverServiceCID,
rv = RegisterGenericFactory(compMgr, nsObserverService::GetCID(),
NS_OBSERVERSERVICE_CLASSNAME,
NS_OBSERVERSERVICE_PROGID,
nsObserverService::Create);

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

@ -7,9 +7,7 @@ nsIArena.h
nsIAtom.h
nsIBuffer.h
nsIByteBuffer.h
nsIObserver.h
nsIObserverList.h
nsIObserverService.h
nsIPageManager.h
nsIProperties.h
nsISimpleEnumerator.h

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

@ -1,2 +1,4 @@
nsICollection.idl
nsIEnumerator.idl
nsIObserver.idl
nsIObserverService.idl

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

@ -31,8 +31,10 @@ MODULE = xpcom
XPIDL_MODULE = xpcom_ds
XPIDLSRCS = \
nsICollection.idl \
nsIEnumerator.idl \
nsICollection.idl \
nsIEnumerator.idl \
nsIObserver.idl \
nsIObserverService.idl \
$(NULL)
CPPSRCS = \
@ -78,9 +80,7 @@ EXPORTS = \
nsIAtom.h \
nsIBuffer.h \
nsIByteBuffer.h \
nsIObserver.h \
nsIObserverList.h \
nsIObserverService.h \
nsIPageManager.h \
nsIProperties.h \
nsISimpleEnumerator.h \

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

@ -33,9 +33,7 @@ EXPORTS = \
nsIAtom.h \
nsIBuffer.h \
nsIByteBuffer.h \
nsIObserver.h \
nsIObserverList.h \
nsIObserverService.h \
nsIPageManager.h \
nsIProperties.h \
nsISimpleEnumerator.h \
@ -63,6 +61,8 @@ XPILD_MODULE = xpcom_ds
XPIDLSRCS = \
.\nsICollection.idl \
.\nsIEnumerator.idl \
.\nsIObserver.idl \
.\nsIObserverService.idl \
$(NULL)
################################################################################

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

@ -1,46 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsIObserver_h__
#define nsIObserver_h__
#include "nsISupports.h"
#include "nscore.h"
// {DB242E01-E4D9-11d2-9DDE-000064657374}
#define NS_IOBSERVER_IID \
{ 0xdb242e01, 0xe4d9, 0x11d2, { 0x9d, 0xde, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } }
// {DB242E03-E4D9-11d2-9DDE-000064657374}
#define NS_OBSERVER_CID \
{ 0xdb242e03, 0xe4d9, 0x11d2, { 0x9d, 0xde, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } }
#define NS_OBSERVER_PROGID "component://netscape/observer"
#define NS_OBSERVER_CLASSNAME "Observer"
class nsIObserver : public nsISupports {
public:
static const nsIID& GetIID() { static nsIID iid = NS_IOBSERVER_IID; return iid; }
NS_IMETHOD Notify(nsISupports** result) = 0;
};
extern NS_COM nsresult NS_NewObserver(nsIObserver** anObserver, nsISupports* outer = NULL);
#endif /* nsIObserver_h__ */

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

@ -1,50 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsIObserverService_h__
#define nsIObserverService_h__
#include "nsISupports.h"
#include "nsIObserverList.h"
#include "nsString.h"
// {D07F5192-E3D1-11d2-8ACD-00105A1B8860}
#define NS_IOBSERVERSERVICE_IID \
{ 0xd07f5192, 0xe3d1, 0x11d2, { 0x8a, 0xcd, 0x0, 0x10, 0x5a, 0x1b, 0x88, 0x60 } }
// {D07F5195-E3D1-11d2-8ACD-00105A1B8860}
#define NS_OBSERVERSERVICE_CID \
{ 0xd07f5195, 0xe3d1, 0x11d2, { 0x8a, 0xcd, 0x0, 0x10, 0x5a, 0x1b, 0x88, 0x60 } }
#define NS_OBSERVERSERVICE_PROGID "component://netscape/observer-service"
#define NS_OBSERVERSERVICE_CLASSNAME "Observer Service"
class nsIObserverService : public nsISupports {
public:
static const nsIID& GetIID() { static nsIID iid = NS_IOBSERVERSERVICE_IID; return iid; }
NS_IMETHOD AddObserver(nsIObserver** anObserver, nsString* aTopic) = 0;
NS_IMETHOD RemoveObserver(nsIObserver** anObserver, nsString* aTopic) = 0;
NS_IMETHOD EnumerateObserverList(nsIEnumerator** anEnumerator, nsString* aTopic) = 0;
};
extern NS_COM nsresult NS_NewObserverService(nsIObserverService** anObserverService);
#endif /* nsIObserverService_h__ */

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

@ -28,7 +28,6 @@
static NS_DEFINE_IID(kIObserverIID, NS_IOBSERVER_IID);
static NS_DEFINE_IID(kObserverCID, NS_OBSERVER_CID);
////////////////////////////////////////////////////////////////////////////////
// nsObserver Implementation
@ -83,10 +82,10 @@ nsObserver::AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr)
return NS_NOINTERFACE;
}
nsresult nsObserver::Notify(nsISupports** result)
{
return NS_OK;
NS_IMETHODIMP
nsObserver::Observe( nsISupports *, const PRUnichar *, const PRUnichar * ) {
nsresult rv = NS_OK;
return rv;
}
////////////////////////////////////////////////////////////////////////////////

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

@ -22,10 +22,16 @@
#include "nsIObserver.h"
#include "nsAgg.h"
// {DB242E03-E4D9-11d2-9DDE-000064657374}
#define NS_OBSERVER_CID \
{ 0xdb242e03, 0xe4d9, 0x11d2, { 0x9d, 0xde, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } }
class nsObserver : public nsIObserver {
public:
NS_DEFINE_STATIC_CID_ACCESSOR( NS_OBSERVER_CID )
NS_IMETHOD Notify(nsISupports** result);
NS_DECL_IOBSERVER
nsObserver(nsISupports* outer);
virtual ~nsObserver(void);
@ -39,4 +45,6 @@ private:
};
extern NS_COM nsresult NS_NewObserver(nsIObserver** anObserver, nsISupports* outer = NULL);
#endif /* nsObserver_h___ */

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

@ -31,7 +31,6 @@
static NS_DEFINE_IID(kIObserverServiceIID, NS_IOBSERVERSERVICE_IID);
static NS_DEFINE_IID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
////////////////////////////////////////////////////////////////////////////////
static nsObserverService* gObserverService = nsnull; // The one-and-only ObserverService
@ -88,7 +87,7 @@ nsresult nsObserverService::GetObserverService(nsIObserverService** anObserverSe
return NS_OK;
}
nsresult nsObserverService::GetObserverList(nsIObserverList** anObserverList, nsString* aTopic)
nsresult nsObserverService::GetObserverList(const nsString& aTopic, nsIObserverList** anObserverList)
{
if (anObserverList == NULL)
{
@ -105,17 +104,22 @@ nsresult nsObserverService::GetObserverList(nsIObserverList** anObserverList, ns
// Safely convert to a C-string
char buf[128];
char* topic = buf;
if ((*aTopic).Length() >= sizeof(buf))
topic = new char[(*aTopic).Length() + 1];
char *temp = 0;
if (aTopic.Length() >= sizeof(buf))
topic = temp = new char[aTopic.Length() + 1];
if (topic == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
(*aTopic).ToCString(topic, (*aTopic).Length() + 1);
aTopic.ToCString(topic, aTopic.Length() + 1);
nsCStringKey key(topic);
if ( temp ) {
delete [] temp;
}
nsIObserverList *topicObservers = nsnull;
if (mObserverTopicTable->Exists(&key)) {
topicObservers = (nsIObserverList *) mObserverTopicTable->Get(&key);
@ -135,7 +139,7 @@ nsresult nsObserverService::GetObserverList(nsIObserverList** anObserverList, ns
return NS_OK;
}
nsresult nsObserverService::AddObserver(nsIObserver** anObserver, nsString* aTopic)
nsresult nsObserverService::AddObserver(nsIObserver* anObserver, const PRUnichar* aTopic)
{
nsIObserverList* anObserverList;
nsresult rv;
@ -150,17 +154,17 @@ nsresult nsObserverService::AddObserver(nsIObserver** anObserver, nsString* aTop
return NS_ERROR_NULL_POINTER;
}
rv = GetObserverList(&anObserverList, aTopic);
rv = GetObserverList(aTopic, &anObserverList);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
return anObserverList->AddObserver(anObserver);
return anObserverList->AddObserver(&anObserver);
}
return NS_ERROR_FAILURE;
}
nsresult nsObserverService::RemoveObserver(nsIObserver** anObserver, nsString* aTopic)
nsresult nsObserverService::RemoveObserver(nsIObserver* anObserver, const PRUnichar* aTopic)
{
nsIObserverList* anObserverList;
nsresult rv;
@ -175,17 +179,17 @@ nsresult nsObserverService::RemoveObserver(nsIObserver** anObserver, nsString* a
return NS_ERROR_NULL_POINTER;
}
rv = GetObserverList(&anObserverList, aTopic);
rv = GetObserverList(aTopic, &anObserverList);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
return anObserverList->RemoveObserver(anObserver);
return anObserverList->RemoveObserver(&anObserver);
}
return NS_ERROR_FAILURE;
}
nsresult nsObserverService::EnumerateObserverList(nsIEnumerator** anEnumerator, nsString* aTopic)
nsresult nsObserverService::EnumerateObserverList(const PRUnichar* aTopic, nsIEnumerator** anEnumerator)
{
nsIObserverList* anObserverList;
nsresult rv;
@ -200,7 +204,7 @@ nsresult nsObserverService::EnumerateObserverList(nsIEnumerator** anEnumerator,
return NS_ERROR_NULL_POINTER;
}
rv = GetObserverList(&anObserverList, aTopic);
rv = GetObserverList(aTopic, &anObserverList);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
@ -210,5 +214,41 @@ nsresult nsObserverService::EnumerateObserverList(nsIEnumerator** anEnumerator,
return NS_ERROR_FAILURE;
}
// Enumerate observers of aTopic and call Observe on each.
nsresult nsObserverService::Notify( nsISupports *aSubject,
const PRUnichar *aTopic,
const PRUnichar *someData ) {
nsresult rv = NS_OK;
nsIEnumerator *observers;
// Get observer list enumerator.
rv = this->EnumerateObserverList( aTopic, &observers );
if ( NS_SUCCEEDED( rv ) ) {
// Go to start of observer list.
rv = observers->First();
// Continue until error or end of list.
while ( observers->IsDone() != NS_OK && NS_SUCCEEDED(rv) ) {
// Get current item (observer).
nsISupports *base;
rv = observers->CurrentItem( &base );
if ( NS_SUCCEEDED( rv ) ) {
// Convert item to nsIObserver.
nsIObserver *observer;
rv = base->QueryInterface( nsIObserver::GetIID(), (void**)&observer );
if ( NS_SUCCEEDED( rv ) && observer ) {
// Tell the observer what's up.
observer->Observe( aSubject, aTopic, someData );
// Release the observer.
observer->Release();
}
}
// Go on to next observer in list.
rv = observers->Next();
}
// Release the observer list.
observers->Release();
rv = NS_OK;
}
return rv;
}
////////////////////////////////////////////////////////////////////////////////

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

@ -20,18 +20,22 @@
#define nsObserverService_h___
#include "nsIObserverService.h"
#include "nsIObserverList.h"
class nsHashtable;
class nsString;
// {D07F5195-E3D1-11d2-8ACD-00105A1B8860}
#define NS_OBSERVERSERVICE_CID \
{ 0xd07f5195, 0xe3d1, 0x11d2, { 0x8a, 0xcd, 0x0, 0x10, 0x5a, 0x1b, 0x88, 0x60 } }
class nsObserverService : public nsIObserverService {
public:
NS_DEFINE_STATIC_CID_ACCESSOR( NS_OBSERVERSERVICE_CID )
static nsresult GetObserverService(nsIObserverService** anObserverService);
NS_IMETHOD AddObserver(nsIObserver** anObserver, nsString* aTopic);
NS_IMETHOD RemoveObserver(nsIObserver** anObserver, nsString* aTopic);
NS_IMETHOD EnumerateObserverList(nsIEnumerator** anEnumerator, nsString* aTopic);
NS_DECL_IOBSERVERSERVICE
nsObserverService();
virtual ~nsObserverService(void);
@ -43,7 +47,7 @@ public:
private:
NS_IMETHOD GetObserverList(nsIObserverList** anObserverList, nsString* aTopic);
NS_IMETHOD GetObserverList(const nsString& aTopic, nsIObserverList** anObserverList);
nsHashtable* mObserverTopicTable;

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

@ -22,72 +22,132 @@
#include "nsIObserverService.h"
#include "nsIObserver.h"
#include "nsString.h"
#include "prprf.h"
static NS_DEFINE_IID(kIObserverServiceIID, NS_IOBSERVERSERVICE_IID);
static NS_DEFINE_IID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
static NS_DEFINE_IID(kIObserverIID, NS_IOBSERVER_IID);
static NS_DEFINE_IID(kObserverCID, NS_OBSERVER_CID);
static nsIObserverService *anObserverService = NULL;
static void testResult( nsresult rv ) {
if ( NS_SUCCEEDED( rv ) ) {
cout << "...ok" << endl;
} else {
cout << "...failed, rv=0x" << hex << (int)rv << endl;
}
return;
}
extern ostream &operator<<( ostream &s, nsString &str ) {
const char *cstr = str.ToNewCString();
s << cstr;
delete [] (char*)cstr;
return s;
}
class TestObserver : public nsIObserver {
public:
TestObserver( const nsString &name = "unknown" )
: mName( name ) {
NS_INIT_REFCNT();
}
NS_DECL_ISUPPORTS
NS_DECL_IOBSERVER
nsString mName;
};
NS_IMPL_ISUPPORTS( TestObserver, nsIObserver::GetIID() );
NS_IMETHODIMP
TestObserver::Observe( nsISupports *aSubject,
const PRUnichar *aTopic,
const PRUnichar *someData ) {
nsString topic( aTopic );
nsString data( someData );
cout << mName << " has observed something: subject@" << (void*)aSubject
<< " name=" << ((TestObserver*)aSubject)->mName
<< " aTopic=" << topic
<< " someData=" << data << endl;
return NS_OK;
}
int main(int argc, char *argv[])
{
nsIObserverService *anObserverService = NULL;
nsString topicA( "topic-A" );
nsString topicB( "topic-B" );
nsresult rv;
rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup,
"components");
if (NS_FAILED(rv)) return rv;
nsresult res = nsComponentManager::CreateInstance(kObserverServiceCID,
nsresult res = nsComponentManager::CreateInstance(NS_OBSERVERSERVICE_PROGID,
NULL,
kIObserverServiceIID,
nsIObserverService::GetIID(),
(void **) &anObserverService);
if (res == NS_OK) {
nsString aTopic("htmlparser");
nsIObserver *anObserver;
nsIObserver *aObserver = nsnull;
nsIObserver *bObserver = nsnull;
nsIObserver *aObserver = new TestObserver("Observer-A");
aObserver->AddRef();
nsIObserver *bObserver = new TestObserver("Observer-B");
bObserver->AddRef();
nsresult res = nsRepository::CreateInstance(kObserverCID,
NULL,
kIObserverIID,
(void **) &anObserver);
rv = NS_NewObserver(&aObserver);
if (NS_FAILED(rv)) return rv;
rv = anObserverService->AddObserver(&aObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
cout << "Adding Observer-A as observer of topic-A..." << endl;
rv = anObserverService->AddObserver(aObserver, topicA);
testResult(rv);
rv = NS_NewObserver(&bObserver);
if (NS_FAILED(rv)) return rv;
cout << "Adding Observer-B as observer of topic-A..." << endl;
rv = anObserverService->AddObserver(bObserver, topicA);
testResult(rv);
cout << "Adding Observer-B as observer of topic-B..." << endl;
rv = anObserverService->AddObserver(bObserver, topicB);
testResult(rv);
rv = anObserverService->AddObserver(&bObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
cout << "Testing Notify(observer-A, topic-A)..." << endl;
rv = anObserverService->Notify( aObserver,
topicA,
nsString("Testing Notify(observer-A, topic-A)") );
testResult(rv);
cout << "Testing Notify(observer-B, topic-B)..." << endl;
rv = anObserverService->Notify( bObserver,
topicB,
nsString("Testing Notify(observer-B, topic-B)") );
testResult(rv);
cout << "Testing EnumerateObserverList (for topic-A)..." << endl;
nsIEnumerator* e;
rv = anObserverService->EnumerateObserverList(&e, &aTopic);
if (NS_FAILED(rv)) return rv;
nsISupports *inst;
rv = anObserverService->EnumerateObserverList(topicA, &e);
testResult(rv);
for (e->First(); e->IsDone() != NS_OK; e->Next()) {
rv = e->CurrentItem(&inst);
if (NS_SUCCEEDED(rv)) {
rv = inst->QueryInterface(nsIObserver::GetIID(),(void**)&anObserver);
cout << "Enumerating observers of topic-A..." << endl;
if ( NS_SUCCEEDED( rv ) ) {
nsISupports *inst;
for (e->First(); e->IsDone() != NS_OK; e->Next()) {
rv = e->CurrentItem(&inst);
if (NS_SUCCEEDED(rv)) {
rv = inst->QueryInterface(nsIObserver::GetIID(),(void**)&anObserver);
cout << "Calling observe on enumerated observer "
<< ((TestObserver*)inst)->mName << "..." << endl;
rv = anObserver->Observe( inst, topicA, nsString("during enumeration") );
testResult(rv);
}
}
rv = anObserver->Notify(nsnull);
}
cout << "...done enumerating observers of topic-A" << endl;
rv = anObserverService->RemoveObserver(&aObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
cout << "Removing Observer-A..." << endl;
rv = anObserverService->RemoveObserver(aObserver, topicA);
testResult(rv);
rv = anObserverService->RemoveObserver(&bObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
cout << "Removing Observer-B (topic-A)..." << endl;
rv = anObserverService->RemoveObserver(bObserver, topicB);
testResult(rv);
cout << "Removing Observer-B (topic-B)..." << endl;
rv = anObserverService->RemoveObserver(bObserver, topicA);
testResult(rv);
}
return NS_OK;