1999-04-07 00:54:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-04-07 00:54:09 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-04-07 00:54:09 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-04-07 00:54:09 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-04-07 00:54:09 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define NS_IMPL_IDS
|
|
|
|
#include "prlock.h"
|
|
|
|
#include "nsIFactory.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsRepository.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsObserverService.h"
|
|
|
|
#include "nsIObserverList.h"
|
|
|
|
#include "nsObserverList.h"
|
|
|
|
#include "nsHashtable.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
1999-08-23 14:14:16 +04:00
|
|
|
static NS_DEFINE_CID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static nsObserverService* gObserverService = nsnull; // The one-and-only ObserverService
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsObserverService Implementation
|
|
|
|
|
|
|
|
|
1999-08-23 14:14:16 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsObserverService, nsIObserverService)
|
1999-04-07 00:54:09 +04:00
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_COM nsresult NS_NewObserverService(nsIObserverService** anObserverService)
|
1999-04-07 00:54:09 +04:00
|
|
|
{
|
|
|
|
return nsObserverService::GetObserverService(anObserverService);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverService::nsObserverService()
|
|
|
|
: mObserverTopicTable(NULL)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mObserverTopicTable = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverService::~nsObserverService(void)
|
|
|
|
{
|
|
|
|
if(mObserverTopicTable)
|
|
|
|
delete mObserverTopicTable;
|
|
|
|
gObserverService = nsnull;
|
|
|
|
}
|
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_METHOD
|
|
|
|
nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsObserverService* os = new nsObserverService();
|
|
|
|
if (os == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(os);
|
|
|
|
rv = os->QueryInterface(aIID, aInstancePtr);
|
|
|
|
NS_RELEASE(os);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
nsresult nsObserverService::GetObserverService(nsIObserverService** anObserverService)
|
|
|
|
{
|
|
|
|
if (! gObserverService) {
|
|
|
|
nsObserverService* it = new nsObserverService();
|
|
|
|
if (! it)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
gObserverService = it;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(gObserverService);
|
|
|
|
*anObserverService = gObserverService;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-30 11:58:55 +04:00
|
|
|
static PRBool
|
|
|
|
ReleaseObserverList(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsIObserverList* observerList = NS_STATIC_CAST(nsIObserverList*, aData);
|
|
|
|
NS_RELEASE(observerList);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
nsresult nsObserverService::GetObserverList(const nsString& aTopic, nsIObserverList** anObserverList)
|
1999-04-07 00:54:09 +04:00
|
|
|
{
|
|
|
|
if (anObserverList == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mObserverTopicTable == NULL) {
|
1999-07-30 11:58:55 +04:00
|
|
|
mObserverTopicTable = new nsObjectHashtable(nsnull, nsnull, // should never be cloned
|
|
|
|
ReleaseObserverList, nsnull,
|
|
|
|
256, PR_TRUE);
|
1999-04-07 00:54:09 +04:00
|
|
|
if (mObserverTopicTable == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-22 06:11:04 +04:00
|
|
|
nsStringKey key(aTopic);
|
1999-05-29 04:51:17 +04:00
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
nsIObserverList *topicObservers = nsnull;
|
1999-05-01 02:55:21 +04:00
|
|
|
if (mObserverTopicTable->Exists(&key)) {
|
1999-04-07 00:54:09 +04:00
|
|
|
topicObservers = (nsIObserverList *) mObserverTopicTable->Get(&key);
|
|
|
|
if (topicObservers != NULL) {
|
|
|
|
*anObserverList = topicObservers;
|
|
|
|
} else {
|
|
|
|
NS_NewObserverList(&topicObservers);
|
|
|
|
mObserverTopicTable->Put(&key, topicObservers);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_NewObserverList(&topicObservers);
|
|
|
|
*anObserverList = topicObservers;
|
|
|
|
mObserverTopicTable->Put(&key, topicObservers);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
nsresult nsObserverService::AddObserver(nsIObserver* anObserver, const PRUnichar* aTopic)
|
1999-04-20 18:50:47 +04:00
|
|
|
{
|
|
|
|
nsIObserverList* anObserverList;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (anObserver == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTopic == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
1999-09-02 22:03:30 +04:00
|
|
|
nsAutoString topic(aTopic);
|
|
|
|
rv = GetObserverList(topic, &anObserverList);
|
1999-04-20 18:50:47 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (anObserverList) {
|
1999-05-29 04:51:17 +04:00
|
|
|
return anObserverList->AddObserver(&anObserver);
|
1999-04-20 18:50:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
nsresult nsObserverService::RemoveObserver(nsIObserver* anObserver, const PRUnichar* aTopic)
|
1999-04-20 18:50:47 +04:00
|
|
|
{
|
|
|
|
nsIObserverList* anObserverList;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (anObserver == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTopic == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
1999-09-02 22:03:30 +04:00
|
|
|
nsAutoString topic(aTopic);
|
|
|
|
rv = GetObserverList(topic, &anObserverList);
|
1999-04-20 18:50:47 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (anObserverList) {
|
1999-05-29 04:51:17 +04:00
|
|
|
return anObserverList->RemoveObserver(&anObserver);
|
1999-04-20 18:50:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
nsresult nsObserverService::EnumerateObserverList(const PRUnichar* aTopic, nsIEnumerator** anEnumerator)
|
1999-04-20 18:50:47 +04:00
|
|
|
{
|
|
|
|
nsIObserverList* anObserverList;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (anEnumerator == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTopic == NULL)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
1999-09-02 22:03:30 +04:00
|
|
|
nsAutoString topic(aTopic);
|
|
|
|
rv = GetObserverList(topic, &anObserverList);
|
1999-04-20 18:50:47 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (anObserverList) {
|
|
|
|
return anObserverList->EnumerateObserverList(anEnumerator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
// 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();
|
|
|
|
}
|
1999-11-15 23:57:24 +03:00
|
|
|
NS_IF_RELEASE(base);
|
1999-05-29 04:51:17 +04:00
|
|
|
}
|
|
|
|
// Go on to next observer in list.
|
|
|
|
rv = observers->Next();
|
|
|
|
}
|
|
|
|
// Release the observer list.
|
|
|
|
observers->Release();
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|