2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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-07 00:54:09 +04:00
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2006-03-21 17:43:56 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2015-06-16 21:58:04 +03:00
|
|
|
#include "nsIConsoleService.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "nsIObserverService.h"
|
2005-11-30 21:51:27 +03:00
|
|
|
#include "nsIObserver.h"
|
2015-06-16 21:58:04 +03:00
|
|
|
#include "nsIScriptError.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "nsObserverService.h"
|
|
|
|
#include "nsObserverList.h"
|
2015-06-16 21:58:04 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2006-08-08 22:18:50 +04:00
|
|
|
#include "nsEnumeratorUtils.h"
|
2015-04-11 04:25:58 +03:00
|
|
|
#include "xpcpublic.h"
|
2012-11-08 05:24:27 +04:00
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
2013-08-26 22:56:23 +04:00
|
|
|
#include "mozilla/Services.h"
|
2001-10-20 06:07:33 +04:00
|
|
|
|
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
|
|
|
// 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
|
|
|
|
//
|
2015-06-04 01:25:57 +03:00
|
|
|
// this enables LogLevel::Debug level information and places all output in
|
2001-10-23 02:01:27 +04:00
|
|
|
// the file nspr.log
|
2015-10-19 22:50:14 +03:00
|
|
|
static mozilla::LazyLogModule sObserverServiceLog("ObserverService");
|
|
|
|
#define LOG(x) MOZ_LOG(sObserverServiceLog, mozilla::LogLevel::Debug, x)
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2013-11-07 09:35:30 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-07-10 02:49:32 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aAnonymize)
|
2013-08-26 22:56:23 +04:00
|
|
|
{
|
2015-07-10 02:49:32 +03:00
|
|
|
struct SuspectObserver
|
|
|
|
{
|
|
|
|
SuspectObserver(const char* aTopic, size_t aReferentCount)
|
|
|
|
: mTopic(aTopic)
|
|
|
|
, mReferentCount(aReferentCount)
|
|
|
|
{}
|
|
|
|
const char* mTopic;
|
|
|
|
size_t mReferentCount;
|
|
|
|
};
|
2014-07-09 19:15:21 +04:00
|
|
|
|
2015-07-13 01:55:27 +03:00
|
|
|
size_t totalNumStrong = 0;
|
|
|
|
size_t totalNumWeakAlive = 0;
|
|
|
|
size_t totalNumWeakDead = 0;
|
2015-07-10 02:49:32 +03:00
|
|
|
nsTArray<SuspectObserver> suspectObservers;
|
|
|
|
|
|
|
|
for (auto iter = mObserverTopicTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsObserverList* observerList = iter.Get();
|
|
|
|
if (!observerList) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
|
2015-07-13 01:55:27 +03:00
|
|
|
size_t topicNumStrong = 0;
|
|
|
|
size_t topicNumWeakAlive = 0;
|
|
|
|
size_t topicNumWeakDead = 0;
|
|
|
|
|
2015-07-10 02:49:32 +03:00
|
|
|
nsTArray<ObserverRef>& observers = observerList->mObservers;
|
|
|
|
for (uint32_t i = 0; i < observers.Length(); i++) {
|
|
|
|
if (observers[i].isWeakRef) {
|
|
|
|
nsCOMPtr<nsIObserver> observerRef(
|
|
|
|
do_QueryReferent(observers[i].asWeak()));
|
|
|
|
if (observerRef) {
|
2015-07-13 01:55:27 +03:00
|
|
|
topicNumWeakAlive++;
|
2015-07-10 02:49:32 +03:00
|
|
|
} else {
|
2015-07-13 01:55:27 +03:00
|
|
|
topicNumWeakDead++;
|
2015-07-10 02:49:32 +03:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
2015-07-13 01:55:27 +03:00
|
|
|
topicNumStrong++;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2013-08-26 22:56:23 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 01:55:27 +03:00
|
|
|
totalNumStrong += topicNumStrong;
|
|
|
|
totalNumWeakAlive += topicNumWeakAlive;
|
|
|
|
totalNumWeakDead += topicNumWeakDead;
|
|
|
|
|
2015-07-10 02:49:32 +03:00
|
|
|
// Keep track of topics that have a suspiciously large number
|
|
|
|
// of referents (symptom of leaks).
|
2015-07-13 01:55:27 +03:00
|
|
|
size_t topicTotal = topicNumStrong + topicNumWeakAlive + topicNumWeakDead;
|
|
|
|
if (topicTotal > kSuspectReferentCount) {
|
|
|
|
SuspectObserver suspect(observerList->GetKey(), topicTotal);
|
2015-07-10 02:49:32 +03:00
|
|
|
suspectObservers.AppendElement(suspect);
|
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2013-08-26 22:56:23 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
// These aren't privacy-sensitive and so don't need anonymizing.
|
|
|
|
nsresult rv;
|
2015-07-10 02:49:32 +03:00
|
|
|
for (uint32_t i = 0; i < suspectObservers.Length(); i++) {
|
|
|
|
SuspectObserver& suspect = suspectObservers[i];
|
|
|
|
nsPrintfCString suspectPath("observer-service-suspect/referent(topic=%s)",
|
|
|
|
suspect.mTopic);
|
2014-07-09 19:15:21 +04:00
|
|
|
rv = aHandleReport->Callback(
|
|
|
|
/* process */ EmptyCString(),
|
2015-07-10 02:49:32 +03:00
|
|
|
suspectPath, KIND_OTHER, UNITS_COUNT, suspect.mReferentCount,
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_LITERAL_CSTRING("A topic with a suspiciously large number of "
|
|
|
|
"referents. This may be symptomatic of a leak "
|
|
|
|
"if the number of referents is high with "
|
|
|
|
"respect to the number of windows."),
|
|
|
|
aData);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2013-08-26 22:56:23 +04:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = aHandleReport->Callback(
|
|
|
|
/* process */ EmptyCString(),
|
|
|
|
NS_LITERAL_CSTRING("observer-service/referent/strong"),
|
2015-07-13 01:55:27 +03:00
|
|
|
KIND_OTHER, UNITS_COUNT, totalNumStrong,
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_LITERAL_CSTRING("The number of strong references held by the "
|
|
|
|
"observer service."),
|
|
|
|
aData);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = aHandleReport->Callback(
|
|
|
|
/* process */ EmptyCString(),
|
|
|
|
NS_LITERAL_CSTRING("observer-service/referent/weak/alive"),
|
2015-07-13 01:55:27 +03:00
|
|
|
KIND_OTHER, UNITS_COUNT, totalNumWeakAlive,
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_LITERAL_CSTRING("The number of weak references held by the "
|
|
|
|
"observer service that are still alive."),
|
|
|
|
aData);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = aHandleReport->Callback(
|
|
|
|
/* process */ EmptyCString(),
|
|
|
|
NS_LITERAL_CSTRING("observer-service/referent/weak/dead"),
|
2015-07-13 01:55:27 +03:00
|
|
|
KIND_OTHER, UNITS_COUNT, totalNumWeakDead,
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_LITERAL_CSTRING("The number of weak references held by the "
|
|
|
|
"observer service that are dead."),
|
|
|
|
aData);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2013-08-26 22:56:23 +04:00
|
|
|
}
|
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsObserverService Implementation
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsObserverService,
|
|
|
|
nsIObserverService,
|
|
|
|
nsObserverService,
|
|
|
|
nsIMemoryReporter)
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
nsObserverService::nsObserverService()
|
|
|
|
: mShuttingDown(false)
|
1999-04-07 00:54:09 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverService::~nsObserverService(void)
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
Shutdown();
|
2006-03-21 17:43:56 +03:00
|
|
|
}
|
|
|
|
|
2013-08-26 22:56:23 +04:00
|
|
|
void
|
|
|
|
nsObserverService::RegisterReporter()
|
|
|
|
{
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-07-09 19:15:21 +04:00
|
|
|
RegisterWeakMemoryReporter(this);
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2013-08-26 22:56:23 +04:00
|
|
|
}
|
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
void
|
|
|
|
nsObserverService::Shutdown()
|
|
|
|
{
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-07-09 19:15:21 +04:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2013-08-26 22:56:23 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
mShuttingDown = true;
|
2006-03-21 17:43:56 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
mObserverTopicTable.Clear();
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
2014-07-09 19:15:21 +04:00
|
|
|
nsObserverService::Create(nsISupports* aOuter, const nsIID& aIID,
|
|
|
|
void** aInstancePtr)
|
1999-05-26 05:38:36 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
LOG(("nsObserverService::Create()"));
|
2001-10-23 02:01:27 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsObserverService> os = new nsObserverService();
|
2006-03-21 17:43:56 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!os) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
// The memory reporter can not be immediately registered here because
|
|
|
|
// the nsMemoryReporterManager may attempt to get the nsObserverService
|
|
|
|
// during initialization, causing a recursive GetService.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRunnableMethod<nsObserverService>> registerRunnable =
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_NewRunnableMethod(os, &nsObserverService::RegisterReporter);
|
|
|
|
NS_DispatchToCurrentThread(registerRunnable);
|
2013-08-26 22:56:23 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return os->QueryInterface(aIID, aInstancePtr);
|
1999-07-30 11:58:55 +04:00
|
|
|
}
|
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
#define NS_ENSURE_VALIDCALL \
|
2006-05-10 21:30:15 +04:00
|
|
|
if (!NS_IsMainThread()) { \
|
2014-04-14 23:04:26 +04:00
|
|
|
MOZ_CRASH("Using observer service off the main thread!"); \
|
2006-03-21 17:43:56 +03:00
|
|
|
return NS_ERROR_UNEXPECTED; \
|
|
|
|
} \
|
|
|
|
if (mShuttingDown) { \
|
|
|
|
NS_ERROR("Using observer service after XPCOM shutdown!"); \
|
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; \
|
2006-03-09 06:14:32 +03:00
|
|
|
}
|
1999-04-20 18:50:47 +04:00
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 19:15:21 +04:00
|
|
|
nsObserverService::AddObserver(nsIObserver* aObserver, const char* aTopic,
|
|
|
|
bool aOwnsWeak)
|
1999-04-20 18:50:47 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
LOG(("nsObserverService::AddObserver(%p: %s)",
|
|
|
|
(void*)aObserver, aTopic));
|
2006-11-23 11:43:28 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_ENSURE_VALIDCALL
|
|
|
|
if (NS_WARN_IF(!aObserver) || NS_WARN_IF(!aTopic)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
1999-04-20 18:50:47 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (mozilla::net::IsNeckoChild() && !strncmp(aTopic, "http-on-", 8)) {
|
2015-06-16 21:58:04 +03:00
|
|
|
nsCOMPtr<nsIConsoleService> console(do_GetService(NS_CONSOLESERVICE_CONTRACTID));
|
|
|
|
nsCOMPtr<nsIScriptError> error(do_CreateInstance(NS_SCRIPTERROR_CONTRACTID));
|
|
|
|
error->Init(NS_LITERAL_STRING("http-on-* observers only work in the parent process"),
|
|
|
|
EmptyString(), EmptyString(), 0, 0,
|
|
|
|
nsIScriptError::warningFlag, "chrome javascript");
|
|
|
|
console->LogMessage(error);
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2012-11-08 05:24:27 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
nsObserverList* observerList = mObserverTopicTable.PutEntry(aTopic);
|
|
|
|
if (!observerList) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2006-03-09 06:14:32 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return observerList->AddObserver(aObserver, aOwnsWeak);
|
1999-04-20 18:50:47 +04:00
|
|
|
}
|
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 19:15:21 +04:00
|
|
|
nsObserverService::RemoveObserver(nsIObserver* aObserver, const char* aTopic)
|
1999-04-20 18:50:47 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
LOG(("nsObserverService::RemoveObserver(%p: %s)",
|
|
|
|
(void*)aObserver, aTopic));
|
|
|
|
NS_ENSURE_VALIDCALL
|
|
|
|
if (NS_WARN_IF(!aObserver) || NS_WARN_IF(!aTopic)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObserverList* observerList = mObserverTopicTable.GetEntry(aTopic);
|
|
|
|
if (!observerList) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This death grip is to protect against stupid consumers who call
|
|
|
|
RemoveObserver from their Destructor, see bug 485834/bug 325392. */
|
|
|
|
nsCOMPtr<nsIObserver> kungFuDeathGrip(aObserver);
|
|
|
|
return observerList->RemoveObserver(aObserver);
|
2006-03-09 06:14:32 +03:00
|
|
|
}
|
|
|
|
|
2006-03-21 17:43:56 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::EnumerateObservers(const char* aTopic,
|
|
|
|
nsISimpleEnumerator** anEnumerator)
|
2006-03-09 06:14:32 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_ENSURE_VALIDCALL
|
|
|
|
if (NS_WARN_IF(!anEnumerator) || NS_WARN_IF(!aTopic)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2006-03-09 06:14:32 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
nsObserverList* observerList = mObserverTopicTable.GetEntry(aTopic);
|
|
|
|
if (!observerList) {
|
|
|
|
return NS_NewEmptyEnumerator(anEnumerator);
|
|
|
|
}
|
2006-03-09 06:14:32 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return observerList->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.
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aSomeData)
|
2006-03-21 17:43:56 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
LOG(("nsObserverService::NotifyObservers(%s)", aTopic));
|
2006-11-23 11:43:28 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_ENSURE_VALIDCALL
|
|
|
|
if (NS_WARN_IF(!aTopic)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2005-01-14 14:25:05 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
nsObserverList* observerList = mObserverTopicTable.GetEntry(aTopic);
|
|
|
|
if (observerList) {
|
|
|
|
observerList->NotifyObservers(aSubject, aTopic, aSomeData);
|
|
|
|
}
|
2006-03-04 17:04:05 +03:00
|
|
|
|
2006-03-09 06:14:32 +03:00
|
|
|
#ifdef NOTIFY_GLOBAL_OBSERVERS
|
2014-07-09 19:15:21 +04:00
|
|
|
observerList = mObserverTopicTable.GetEntry("*");
|
|
|
|
if (observerList) {
|
|
|
|
observerList->NotifyObservers(aSubject, aTopic, aSomeData);
|
|
|
|
}
|
2006-03-09 06:14:32 +03:00
|
|
|
#endif
|
2006-03-21 17:43:56 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_OK;
|
1999-05-29 04:51:17 +04:00
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2012-04-30 23:01:11 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::UnmarkGrayStrongObservers()
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_ENSURE_VALIDCALL
|
2012-04-30 23:01:11 +04:00
|
|
|
|
2015-04-09 03:16:30 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
|
|
|
nsCOMArray<nsIObserver> strongObservers;
|
2015-07-10 02:49:32 +03:00
|
|
|
for (auto iter = mObserverTopicTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsObserverList* aObserverList = iter.Get();
|
|
|
|
if (aObserverList) {
|
|
|
|
aObserverList->AppendStrongObservers(strongObservers);
|
|
|
|
}
|
|
|
|
}
|
2015-04-09 03:16:30 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < strongObservers.Length(); ++i) {
|
|
|
|
xpc_TryUnmarkWrappedGrayObject(strongObservers[i]);
|
|
|
|
}
|
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2012-04-30 23:01:11 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_OK;
|
2012-04-30 23:01:11 +04:00
|
|
|
}
|