1999-04-06 20:54:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2001-09-28 20:14:13 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-04-06 20:54:09 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00: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/
|
|
|
|
*
|
|
|
|
* 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-06 20:54:09 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
1999-11-30 23:36:32 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2001-10-22 22:01:27 +00:00
|
|
|
#include "prlog.h"
|
1999-04-06 20:54:09 +00:00
|
|
|
#include "prlock.h"
|
|
|
|
#include "nsIFactory.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2001-09-06 21:13:11 +00:00
|
|
|
#include "nsIComponentManager.h"
|
1999-04-06 20:54:09 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsObserverService.h"
|
|
|
|
#include "nsObserverList.h"
|
|
|
|
#include "nsHashtable.h"
|
2001-10-20 02:07:33 +00:00
|
|
|
#include "nsIWeakReference.h"
|
|
|
|
|
|
|
|
#define NS_WEAK_OBSERVERS
|
1999-04-06 20:54:09 +00:00
|
|
|
|
1999-08-23 10:14:16 +00:00
|
|
|
static NS_DEFINE_CID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
|
1999-04-06 20:54:09 +00:00
|
|
|
|
|
|
|
|
2001-10-22 22:01:27 +00:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
// Log module for nsObserverService logging...
|
|
|
|
//
|
|
|
|
// To enable logging (see prlog.h for full details):
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=ObserverService:5
|
|
|
|
// set NSPR_LOG_FILE=nspr.log
|
|
|
|
//
|
|
|
|
// this enables PR_LOG_DEBUG level information and places all output in
|
|
|
|
// the file nspr.log
|
|
|
|
PRLogModuleInfo* observerServiceLog = nsnull;
|
|
|
|
#endif /* PR_LOGGING */
|
1999-04-06 20:54:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsObserverService Implementation
|
|
|
|
|
|
|
|
|
2000-03-05 21:26:01 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsObserverService, nsIObserverService)
|
1999-04-06 20:54:09 +00:00
|
|
|
|
|
|
|
nsObserverService::nsObserverService()
|
2001-10-19 20:52:59 +00:00
|
|
|
: mObserverTopicTable(nsnull)
|
1999-04-06 20:54:09 +00:00
|
|
|
{
|
|
|
|
mObserverTopicTable = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverService::~nsObserverService(void)
|
|
|
|
{
|
|
|
|
if(mObserverTopicTable)
|
|
|
|
delete mObserverTopicTable;
|
|
|
|
}
|
|
|
|
|
1999-05-26 01:38:36 +00:00
|
|
|
NS_METHOD
|
|
|
|
nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
|
|
|
{
|
2001-10-22 22:01:27 +00:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!observerServiceLog)
|
|
|
|
observerServiceLog = PR_NewLogModule("ObserverService");
|
|
|
|
#endif
|
|
|
|
|
1999-05-26 01:38:36 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsObserverService* os = new nsObserverService();
|
2001-10-19 20:52:59 +00:00
|
|
|
if (os == nsnull)
|
1999-05-26 01:38:36 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(os);
|
|
|
|
rv = os->QueryInterface(aIID, aInstancePtr);
|
|
|
|
NS_RELEASE(os);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2000-05-02 22:38:04 +00:00
|
|
|
static PRBool PR_CALLBACK
|
1999-07-30 07:58:55 +00:00
|
|
|
ReleaseObserverList(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
2001-10-19 20:52:59 +00:00
|
|
|
nsObserverList* observerList = NS_STATIC_CAST(nsObserverList*, aData);
|
|
|
|
delete(observerList);
|
1999-07-30 07:58:55 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
nsresult nsObserverService::GetObserverList(const char* aTopic, nsObserverList** anObserverList)
|
1999-04-06 20:54:09 +00:00
|
|
|
{
|
2001-10-19 20:52:59 +00:00
|
|
|
if (anObserverList == nsnull)
|
1999-04-06 20:54:09 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
if(mObserverTopicTable == nsnull)
|
|
|
|
{
|
|
|
|
mObserverTopicTable = new nsObjectHashtable(nsnull,
|
|
|
|
nsnull, // should never be cloned
|
|
|
|
ReleaseObserverList,
|
|
|
|
nsnull,
|
|
|
|
256,
|
|
|
|
PR_TRUE);
|
|
|
|
if (mObserverTopicTable == nsnull)
|
1999-04-06 20:54:09 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2001-10-19 20:52:59 +00:00
|
|
|
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
nsCStringKey key(aTopic);
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
nsObserverList *topicObservers;
|
|
|
|
topicObservers = (nsObserverList *) mObserverTopicTable->Get(&key);
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
if (topicObservers)
|
|
|
|
{
|
|
|
|
*anObserverList = topicObservers;
|
|
|
|
return NS_OK;
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
topicObservers = new nsObserverList();
|
|
|
|
if (!topicObservers)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
*anObserverList = topicObservers;
|
|
|
|
mObserverTopicTable->Put(&key, topicObservers);
|
|
|
|
|
1999-04-06 20:54:09 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
NS_IMETHODIMP nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic, PRBool ownsWeak)
|
1999-04-20 14:50:47 +00:00
|
|
|
{
|
2001-10-19 20:52:59 +00:00
|
|
|
nsObserverList* anObserverList;
|
1999-04-20 14:50:47 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
if (anObserver == nsnull || aTopic == nsnull)
|
1999-04-20 14:50:47 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2001-02-13 01:34:14 +00:00
|
|
|
rv = GetObserverList(aTopic, &anObserverList);
|
1999-04-20 14:50:47 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
return anObserverList->AddObserver(anObserver, ownsWeak);
|
1999-04-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
NS_IMETHODIMP nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic)
|
1999-04-20 14:50:47 +00:00
|
|
|
{
|
2001-10-19 20:52:59 +00:00
|
|
|
nsObserverList* anObserverList;
|
1999-04-20 14:50:47 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
if (anObserver == nsnull || aTopic == nsnull)
|
1999-04-20 14:50:47 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2001-02-13 01:34:14 +00:00
|
|
|
rv = GetObserverList(aTopic, &anObserverList);
|
1999-04-20 14:50:47 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
return anObserverList->RemoveObserver(anObserver);
|
1999-04-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
NS_IMETHODIMP nsObserverService::EnumerateObservers(const char* aTopic, nsISimpleEnumerator** anEnumerator)
|
1999-04-20 14:50:47 +00:00
|
|
|
{
|
2001-10-19 20:52:59 +00:00
|
|
|
nsObserverList* anObserverList;
|
1999-04-20 14:50:47 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
if (anEnumerator == nsnull || aTopic == nsnull)
|
1999-04-20 14:50:47 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2001-02-13 01:34:14 +00:00
|
|
|
rv = GetObserverList(aTopic, &anObserverList);
|
1999-04-20 14:50:47 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
return anObserverList->GetObserverList(anEnumerator);
|
1999-04-20 14:50:47 +00:00
|
|
|
}
|
1999-04-06 20:54:09 +00:00
|
|
|
|
1999-05-29 00:51:17 +00:00
|
|
|
// Enumerate observers of aTopic and call Observe on each.
|
2001-10-19 20:52:59 +00:00
|
|
|
NS_IMETHODIMP nsObserverService::NotifyObservers( nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *someData ) {
|
1999-05-29 00:51:17 +00:00
|
|
|
nsresult rv = NS_OK;
|
2001-10-19 20:52:59 +00:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> observers;
|
|
|
|
nsCOMPtr<nsISupports> observerRef;
|
|
|
|
|
|
|
|
rv = EnumerateObservers( aTopic, getter_AddRefs(observers) );
|
|
|
|
if ( NS_FAILED( rv ) )
|
|
|
|
return rv;
|
|
|
|
PRBool loop = PR_TRUE;
|
|
|
|
while( NS_SUCCEEDED(observers->HasMoreElements(&loop)) && loop)
|
|
|
|
{
|
|
|
|
observers->GetNext(getter_AddRefs(observerRef));
|
|
|
|
nsCOMPtr<nsIObserver> observer = do_QueryInterface(observerRef);
|
|
|
|
if ( observer )
|
|
|
|
observer->Observe( aSubject, aTopic, someData );
|
2001-10-20 02:07:33 +00:00
|
|
|
#ifdef NS_WEAK_OBSERVERS
|
|
|
|
else
|
|
|
|
{ // check for weak reference.
|
|
|
|
nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(observerRef);
|
|
|
|
if ( weakRef )
|
|
|
|
weakRef->QueryReferent(NS_GET_IID(nsIObserver), getter_AddRefs(observer));
|
|
|
|
|
|
|
|
if ( observer )
|
|
|
|
observer->Observe( aSubject, aTopic, someData );
|
2001-10-22 22:01:27 +00:00
|
|
|
|
|
|
|
PR_LOG(observerServiceLog, PR_LOG_DEBUG, ("Notification - %s\n", aTopic ? aTopic : "undefined"));
|
|
|
|
|
2001-10-20 02:07:33 +00:00
|
|
|
}
|
|
|
|
#endif
|
1999-05-29 00:51:17 +00:00
|
|
|
}
|
2001-10-19 20:52:59 +00:00
|
|
|
return NS_OK;
|
1999-05-29 00:51:17 +00:00
|
|
|
}
|
1999-04-06 20:54:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|