1999-04-06 20:54:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2001-10-22 22:01:27 +00:00
|
|
|
#include "prlog.h"
|
2006-03-21 14:43:56 +00:00
|
|
|
#include "nsAutoPtr.h"
|
1999-04-06 20:54:09 +00:00
|
|
|
#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"
|
2005-11-30 18:51:27 +00:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
1999-04-06 20:54:09 +00:00
|
|
|
#include "nsObserverService.h"
|
|
|
|
#include "nsObserverList.h"
|
|
|
|
#include "nsHashtable.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2001-10-20 02:07:33 +00:00
|
|
|
#include "nsIWeakReference.h"
|
2006-08-08 18:18:50 +00:00
|
|
|
#include "nsEnumeratorUtils.h"
|
2012-11-08 01:24:27 +00:00
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
2001-10-20 02:07:33 +00:00
|
|
|
|
2005-01-14 11:25:05 +00:00
|
|
|
#define NOTIFY_GLOBAL_OBSERVERS
|
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
|
2012-10-29 23:32:10 +00:00
|
|
|
static PRLogModuleInfo*
|
|
|
|
GetObserverServiceLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("ObserverService");
|
|
|
|
return sLog;
|
|
|
|
}
|
|
|
|
#define LOG(x) PR_LOG(GetObserverServiceLog(), PR_LOG_DEBUG, x)
|
2006-11-23 08:43:28 +00:00
|
|
|
#else
|
|
|
|
#define LOG(x)
|
2001-10-22 22:01:27 +00:00
|
|
|
#endif /* PR_LOGGING */
|
1999-04-06 20:54:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsObserverService Implementation
|
|
|
|
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(nsObserverService, nsIObserverService, nsObserverService)
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
nsObserverService::nsObserverService() :
|
2011-10-17 14:59:28 +00:00
|
|
|
mShuttingDown(false)
|
1999-04-06 20:54:09 +00:00
|
|
|
{
|
2006-03-21 14:43:56 +00:00
|
|
|
mObserverTopicTable.Init();
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverService::~nsObserverService(void)
|
|
|
|
{
|
2006-03-21 14:43:56 +00:00
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsObserverService::Shutdown()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mShuttingDown = true;
|
2006-03-21 14:43:56 +00:00
|
|
|
|
|
|
|
if (mObserverTopicTable.IsInitialized())
|
|
|
|
mObserverTopicTable.Clear();
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
2010-06-10 18:11:11 +00:00
|
|
|
nsresult
|
1999-05-26 01:38:36 +00:00
|
|
|
nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
|
|
|
{
|
2006-11-23 08:43:28 +00:00
|
|
|
LOG(("nsObserverService::Create()"));
|
2001-10-22 22:01:27 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
nsRefPtr<nsObserverService> os = new nsObserverService();
|
|
|
|
|
2009-08-08 22:45:46 +00:00
|
|
|
if (!os || !os->mObserverTopicTable.IsInitialized())
|
1999-05-26 01:38:36 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
return os->QueryInterface(aIID, aInstancePtr);
|
1999-07-30 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
#define NS_ENSURE_VALIDCALL \
|
2006-05-10 17:30:15 +00:00
|
|
|
if (!NS_IsMainThread()) { \
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_ERROR("Using observer service off the main thread!"); \
|
|
|
|
return NS_ERROR_UNEXPECTED; \
|
|
|
|
} \
|
|
|
|
if (mShuttingDown) { \
|
|
|
|
NS_ERROR("Using observer service after XPCOM shutdown!"); \
|
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; \
|
2006-03-09 03:14:32 +00:00
|
|
|
}
|
1999-04-20 14:50:47 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool ownsWeak)
|
1999-04-20 14:50:47 +00:00
|
|
|
{
|
2006-11-23 08:43:28 +00:00
|
|
|
LOG(("nsObserverService::AddObserver(%p: %s)",
|
|
|
|
(void*) anObserver, aTopic));
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_ENSURE_VALIDCALL
|
|
|
|
NS_ENSURE_ARG(anObserver && aTopic);
|
1999-04-20 14:50:47 +00:00
|
|
|
|
2012-11-08 01:24:27 +00:00
|
|
|
if (mozilla::net::IsNeckoChild() && !strncmp(aTopic, "http-on-", 8)) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
nsObserverList *observerList = mObserverTopicTable.PutEntry(aTopic);
|
|
|
|
if (!observerList)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-03-09 03:14:32 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
return observerList->AddObserver(anObserver, ownsWeak);
|
1999-04-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic)
|
1999-04-20 14:50:47 +00:00
|
|
|
{
|
2006-11-23 08:43:28 +00:00
|
|
|
LOG(("nsObserverService::RemoveObserver(%p: %s)",
|
|
|
|
(void*) anObserver, aTopic));
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_ENSURE_VALIDCALL
|
|
|
|
NS_ENSURE_ARG(anObserver && aTopic);
|
2006-03-09 03:14:32 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
|
|
|
|
if (!observerList)
|
|
|
|
return NS_ERROR_FAILURE;
|
1999-04-20 14:50:47 +00:00
|
|
|
|
2009-08-12 09:49:52 +00:00
|
|
|
/* This death grip is to protect against stupid consumers who call
|
|
|
|
RemoveObserver from their Destructor, see bug 485834/bug 325392. */
|
|
|
|
nsCOMPtr<nsIObserver> kungFuDeathGrip(anObserver);
|
2006-03-21 14:43:56 +00:00
|
|
|
return observerList->RemoveObserver(anObserver);
|
2006-03-09 03:14:32 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::EnumerateObservers(const char* aTopic,
|
|
|
|
nsISimpleEnumerator** anEnumerator)
|
2006-03-09 03:14:32 +00:00
|
|
|
{
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_ENSURE_VALIDCALL
|
|
|
|
NS_ENSURE_ARG(aTopic && anEnumerator);
|
2006-03-09 03:14:32 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
|
|
|
|
if (!observerList)
|
|
|
|
return NS_NewEmptyEnumerator(anEnumerator);
|
2006-03-09 03:14:32 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
return observerList->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.
|
2005-01-14 11:25:05 +00:00
|
|
|
NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
2006-03-21 14:43:56 +00:00
|
|
|
const PRUnichar *someData)
|
|
|
|
{
|
2006-11-23 08:43:28 +00:00
|
|
|
LOG(("nsObserverService::NotifyObservers(%s)", aTopic));
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_ENSURE_VALIDCALL
|
|
|
|
NS_ENSURE_ARG(aTopic);
|
2005-01-14 11:25:05 +00:00
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
|
|
|
|
if (observerList)
|
|
|
|
observerList->NotifyObservers(aSubject, aTopic, someData);
|
2006-03-04 14:04:05 +00:00
|
|
|
|
2006-03-09 03:14:32 +00:00
|
|
|
#ifdef NOTIFY_GLOBAL_OBSERVERS
|
2006-03-21 14:43:56 +00:00
|
|
|
observerList = mObserverTopicTable.GetEntry("*");
|
|
|
|
if (observerList)
|
|
|
|
observerList->NotifyObservers(aSubject, aTopic, someData);
|
2006-03-09 03:14:32 +00:00
|
|
|
#endif
|
2006-03-21 14:43:56 +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
|
|
|
|
2012-04-30 19:01:11 +00:00
|
|
|
static PLDHashOperator
|
|
|
|
UnmarkGrayObserverEntry(nsObserverList* aObserverList, void* aClosure)
|
|
|
|
{
|
|
|
|
if (aObserverList) {
|
|
|
|
aObserverList->UnmarkGrayStrongObservers();
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::UnmarkGrayStrongObservers()
|
|
|
|
{
|
|
|
|
NS_ENSURE_VALIDCALL
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mObserverTopicTable.EnumerateEntries(UnmarkGrayObserverEntry, nullptr);
|
2012-04-30 19:01:11 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|