1999-04-07 00:54:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2001-09-29 00:14:13 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-04-07 00:54:09 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
2001-09-29 00:14:13 +04: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.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
1999-12-01 02:36:32 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of 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"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2001-10-23 02:01:27 +04:00
|
|
|
#include "prlog.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "prlock.h"
|
|
|
|
#include "nsIFactory.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2001-09-07 01:13:11 +04:00
|
|
|
#include "nsIComponentManager.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "nsIObserverService.h"
|
2005-11-30 21:51:27 +03:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "nsObserverService.h"
|
|
|
|
#include "nsObserverList.h"
|
|
|
|
#include "nsHashtable.h"
|
2001-10-20 06:07:33 +04:00
|
|
|
#include "nsIWeakReference.h"
|
|
|
|
|
2005-01-14 14:25:05 +03:00
|
|
|
#define NOTIFY_GLOBAL_OBSERVERS
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2001-10-23 02:01:27 +04: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-07 00:54:09 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsObserverService Implementation
|
|
|
|
|
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsObserverService, nsIObserverService)
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
nsObserverService::nsObserverService()
|
2001-10-20 00:52:59 +04:00
|
|
|
: mObserverTopicTable(nsnull)
|
1999-04-07 00:54:09 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverService::~nsObserverService(void)
|
|
|
|
{
|
|
|
|
if(mObserverTopicTable)
|
|
|
|
delete mObserverTopicTable;
|
|
|
|
}
|
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_METHOD
|
|
|
|
nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
|
|
|
{
|
2001-10-23 02:01:27 +04:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!observerServiceLog)
|
|
|
|
observerServiceLog = PR_NewLogModule("ObserverService");
|
|
|
|
#endif
|
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsObserverService* os = new nsObserverService();
|
2001-10-20 00:52:59 +04:00
|
|
|
if (os == nsnull)
|
1999-05-26 05:38:36 +04:00
|
|
|
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
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static PRBool PR_CALLBACK
|
1999-07-30 11:58:55 +04:00
|
|
|
ReleaseObserverList(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
2001-10-20 00:52:59 +04:00
|
|
|
nsObserverList* observerList = NS_STATIC_CAST(nsObserverList*, aData);
|
|
|
|
delete(observerList);
|
1999-07-30 11:58:55 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
nsresult nsObserverService::GetObserverList(const char* aTopic, nsObserverList** anObserverList)
|
1999-04-07 00:54:09 +04:00
|
|
|
{
|
2001-10-20 00:52:59 +04:00
|
|
|
if (anObserverList == nsnull)
|
1999-04-07 00:54:09 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2005-01-14 14:25:05 +03:00
|
|
|
|
|
|
|
if(mObserverTopicTable == nsnull)
|
2001-10-20 00:52:59 +04:00
|
|
|
{
|
|
|
|
mObserverTopicTable = new nsObjectHashtable(nsnull,
|
|
|
|
nsnull, // should never be cloned
|
|
|
|
ReleaseObserverList,
|
|
|
|
nsnull,
|
|
|
|
256,
|
|
|
|
PR_TRUE);
|
|
|
|
if (mObserverTopicTable == nsnull)
|
1999-04-07 00:54:09 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2005-01-14 14:25:05 +03:00
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2005-01-14 14:25:05 +03:00
|
|
|
nsCStringKey key(aTopic);
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
nsObserverList *topicObservers;
|
|
|
|
topicObservers = (nsObserverList *) mObserverTopicTable->Get(&key);
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
if (topicObservers)
|
|
|
|
{
|
2005-01-14 14:25:05 +03:00
|
|
|
*anObserverList = topicObservers;
|
2001-10-20 00:52:59 +04:00
|
|
|
return NS_OK;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
2005-11-30 21:51:27 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
topicObservers = new nsObserverList(rv);
|
2001-10-20 00:52:59 +04:00
|
|
|
if (!topicObservers)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2005-11-30 21:51:27 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete topicObservers;
|
|
|
|
return rv;
|
|
|
|
}
|
2001-10-20 00:52:59 +04:00
|
|
|
|
|
|
|
*anObserverList = topicObservers;
|
|
|
|
mObserverTopicTable->Put(&key, topicObservers);
|
|
|
|
|
2005-01-14 14:25:05 +03:00
|
|
|
return NS_OK;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
NS_IMETHODIMP nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic, PRBool ownsWeak)
|
1999-04-20 18:50:47 +04:00
|
|
|
{
|
2005-01-14 14:25:05 +03:00
|
|
|
nsObserverList* anObserverList;
|
|
|
|
nsresult rv;
|
1999-04-20 18:50:47 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
if (anObserver == nsnull || aTopic == nsnull)
|
1999-04-20 18:50:47 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2005-01-14 14:25:05 +03:00
|
|
|
rv = GetObserverList(aTopic, &anObserverList);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-04-20 18:50:47 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
return anObserverList->AddObserver(anObserver, ownsWeak);
|
1999-04-20 18:50:47 +04:00
|
|
|
}
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
NS_IMETHODIMP nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic)
|
1999-04-20 18:50:47 +04:00
|
|
|
{
|
2005-01-14 14:25:05 +03:00
|
|
|
nsObserverList* anObserverList;
|
|
|
|
nsresult rv;
|
1999-04-20 18:50:47 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
if (anObserver == nsnull || aTopic == nsnull)
|
1999-04-20 18:50:47 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2005-01-14 14:25:05 +03:00
|
|
|
rv = GetObserverList(aTopic, &anObserverList);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-04-20 18:50:47 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
return anObserverList->RemoveObserver(anObserver);
|
1999-04-20 18:50:47 +04:00
|
|
|
}
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
NS_IMETHODIMP nsObserverService::EnumerateObservers(const char* aTopic, nsISimpleEnumerator** anEnumerator)
|
1999-04-20 18:50:47 +04:00
|
|
|
{
|
2005-01-14 14:25:05 +03:00
|
|
|
nsObserverList* anObserverList;
|
|
|
|
nsresult rv;
|
1999-04-20 18:50:47 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
if (anEnumerator == nsnull || aTopic == nsnull)
|
1999-04-20 18:50:47 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2005-01-14 14:25:05 +03:00
|
|
|
rv = GetObserverList(aTopic, &anObserverList);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-04-20 18:50:47 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
return anObserverList->GetObserverList(anEnumerator);
|
1999-04-20 18:50:47 +04:00
|
|
|
}
|
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.
|
2005-01-14 14:25:05 +03:00
|
|
|
NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *someData) {
|
1999-05-29 04:51:17 +04:00
|
|
|
nsresult rv = NS_OK;
|
2005-01-14 14:25:05 +03:00
|
|
|
#ifdef NOTIFY_GLOBAL_OBSERVERS
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> globalObservers;
|
|
|
|
#endif
|
2001-10-20 00:52:59 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> observers;
|
|
|
|
nsCOMPtr<nsISupports> observerRef;
|
|
|
|
|
2005-01-14 14:25:05 +03:00
|
|
|
#ifdef NOTIFY_GLOBAL_OBSERVERS
|
|
|
|
EnumerateObservers("*", getter_AddRefs(globalObservers));
|
|
|
|
#endif
|
|
|
|
rv = EnumerateObservers(aTopic, getter_AddRefs(observers));
|
|
|
|
#ifdef NOTIFY_GLOBAL_OBSERVERS
|
|
|
|
/* If there are no global observers and we failed to get normal observers
|
|
|
|
* then we return the error indicating failure to get normal observers.
|
|
|
|
*/
|
|
|
|
if (!globalObservers && NS_FAILED(rv))
|
2001-10-20 00:52:59 +04:00
|
|
|
return rv;
|
2005-01-14 14:25:05 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
do
|
2001-10-20 00:52:59 +04:00
|
|
|
{
|
2005-01-14 14:25:05 +03:00
|
|
|
PRBool more = PR_FALSE;
|
|
|
|
/* If observers is non null then null it out unless it really
|
|
|
|
* has more elements (i.e. that call doesn't fail).
|
|
|
|
*/
|
|
|
|
if (observers && NS_FAILED(observers->HasMoreElements(&more)) || !more)
|
|
|
|
{
|
|
|
|
#ifdef NOTIFY_GLOBAL_OBSERVERS
|
|
|
|
if (observers = globalObservers)
|
|
|
|
globalObservers = nsnull;
|
|
|
|
#else
|
|
|
|
observers = nsnull;
|
|
|
|
#endif
|
|
|
|
}
|
2001-10-20 06:07:33 +04:00
|
|
|
else
|
2005-01-14 14:25:05 +03:00
|
|
|
{
|
|
|
|
observers->GetNext(getter_AddRefs(observerRef));
|
|
|
|
nsCOMPtr<nsIObserver> observer = do_QueryInterface(observerRef);
|
|
|
|
if (observer)
|
|
|
|
observer->Observe(aSubject, aTopic, someData);
|
|
|
|
}
|
|
|
|
} while (observers);
|
2001-10-20 00:52:59 +04:00
|
|
|
return NS_OK;
|
1999-05-29 04:51:17 +04:00
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|