2015-04-09 17:25:05 +00: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 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
|
|
|
|
2015-05-19 18:15:34 +00:00
|
|
|
#include "mozilla/Logging.h"
|
2006-03-21 14:43:56 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2015-06-16 18:58:04 +00:00
|
|
|
#include "nsIConsoleService.h"
|
1999-04-06 20:54:09 +00:00
|
|
|
#include "nsIObserverService.h"
|
2005-11-30 18:51:27 +00:00
|
|
|
#include "nsIObserver.h"
|
2015-06-16 18:58:04 +00:00
|
|
|
#include "nsIScriptError.h"
|
1999-04-06 20:54:09 +00:00
|
|
|
#include "nsObserverService.h"
|
|
|
|
#include "nsObserverList.h"
|
2015-06-16 18:58:04 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2006-08-08 18:18:50 +00:00
|
|
|
#include "nsEnumeratorUtils.h"
|
2015-04-11 01:25:58 +00:00
|
|
|
#include "xpcpublic.h"
|
2012-11-08 01:24:27 +00:00
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
2018-11-12 10:04:17 +00:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
2017-05-29 19:42:42 +00:00
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "nsString.h"
|
2017-06-14 22:58:25 +00:00
|
|
|
#include "GeckoProfiler.h"
|
2001-10-20 02:07:33 +00:00
|
|
|
|
2017-05-29 19:42:42 +00:00
|
|
|
static const uint32_t kMinTelemetryNotifyObserversLatencyMs = 1;
|
|
|
|
|
2001-10-22 22:01:27 +00:00
|
|
|
// Log module for nsObserverService logging...
|
|
|
|
//
|
|
|
|
// To enable logging (see prlog.h for full details):
|
|
|
|
//
|
2016-05-25 22:26:10 +00:00
|
|
|
// set MOZ_LOG=ObserverService:5
|
|
|
|
// set MOZ_LOG_FILE=service.log
|
2001-10-22 22:01:27 +00:00
|
|
|
//
|
2016-05-25 22:26:10 +00:00
|
|
|
// This enables LogLevel::Debug level information and places all output in
|
|
|
|
// the file service.log.
|
2015-10-19 19:50:14 +00:00
|
|
|
static mozilla::LazyLogModule sObserverServiceLog("ObserverService");
|
|
|
|
#define LOG(x) MOZ_LOG(sObserverServiceLog, mozilla::LogLevel::Debug, x)
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2013-11-07 05:35:30 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-07-09 23:49:32 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aAnonymize) {
|
|
|
|
struct SuspectObserver {
|
|
|
|
SuspectObserver(const char* aTopic, size_t aReferentCount)
|
|
|
|
: mTopic(aTopic), mReferentCount(aReferentCount) {}
|
|
|
|
const char* mTopic;
|
|
|
|
size_t mReferentCount;
|
|
|
|
};
|
2014-07-09 15:15:21 +00:00
|
|
|
|
2015-07-12 22:55:27 +00:00
|
|
|
size_t totalNumStrong = 0;
|
|
|
|
size_t totalNumWeakAlive = 0;
|
|
|
|
size_t totalNumWeakDead = 0;
|
2015-07-09 23:49:32 +00:00
|
|
|
nsTArray<SuspectObserver> suspectObservers;
|
|
|
|
|
|
|
|
for (auto iter = mObserverTopicTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsObserverList* observerList = iter.Get();
|
|
|
|
if (!observerList) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-09 15:15:21 +00:00
|
|
|
|
2015-07-12 22:55:27 +00:00
|
|
|
size_t topicNumStrong = 0;
|
|
|
|
size_t topicNumWeakAlive = 0;
|
|
|
|
size_t topicNumWeakDead = 0;
|
|
|
|
|
2018-11-12 10:04:17 +00:00
|
|
|
nsMaybeWeakPtrArray<nsIObserver>& observers = observerList->mObservers;
|
2015-07-09 23:49:32 +00:00
|
|
|
for (uint32_t i = 0; i < observers.Length(); i++) {
|
2018-11-12 10:04:17 +00:00
|
|
|
if (observers[i].IsWeak()) {
|
|
|
|
nsCOMPtr<nsIObserver> ref = observers[i].GetValue();
|
|
|
|
if (ref) {
|
2015-07-12 22:55:27 +00:00
|
|
|
topicNumWeakAlive++;
|
2015-07-09 23:49:32 +00:00
|
|
|
} else {
|
2015-07-12 22:55:27 +00:00
|
|
|
topicNumWeakDead++;
|
2015-07-09 23:49:32 +00:00
|
|
|
}
|
2014-07-09 15:15:21 +00:00
|
|
|
} else {
|
2015-07-12 22:55:27 +00:00
|
|
|
topicNumStrong++;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2013-08-26 18:56:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-12 22:55:27 +00:00
|
|
|
totalNumStrong += topicNumStrong;
|
|
|
|
totalNumWeakAlive += topicNumWeakAlive;
|
|
|
|
totalNumWeakDead += topicNumWeakDead;
|
|
|
|
|
2015-07-09 23:49:32 +00:00
|
|
|
// Keep track of topics that have a suspiciously large number
|
|
|
|
// of referents (symptom of leaks).
|
2015-07-12 22:55:27 +00:00
|
|
|
size_t topicTotal = topicNumStrong + topicNumWeakAlive + topicNumWeakDead;
|
|
|
|
if (topicTotal > kSuspectReferentCount) {
|
|
|
|
SuspectObserver suspect(observerList->GetKey(), topicTotal);
|
2015-07-09 23:49:32 +00:00
|
|
|
suspectObservers.AppendElement(suspect);
|
|
|
|
}
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2013-08-26 18:56:23 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
// These aren't privacy-sensitive and so don't need anonymizing.
|
2015-07-09 23:49:32 +00:00
|
|
|
for (uint32_t i = 0; i < suspectObservers.Length(); i++) {
|
|
|
|
SuspectObserver& suspect = suspectObservers[i];
|
|
|
|
nsPrintfCString suspectPath("observer-service-suspect/referent(topic=%s)",
|
|
|
|
suspect.mTopic);
|
2016-08-24 05:23:45 +00:00
|
|
|
aHandleReport->Callback(
|
2015-07-09 23:49:32 +00:00
|
|
|
/* process */ EmptyCString(), suspectPath, KIND_OTHER, UNITS_COUNT,
|
|
|
|
suspect.mReferentCount,
|
2014-07-09 15:15:21 +00: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);
|
|
|
|
}
|
|
|
|
|
2016-08-24 05:23:45 +00:00
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"observer-service/referent/strong", KIND_OTHER, UNITS_COUNT,
|
|
|
|
totalNumStrong,
|
|
|
|
"The number of strong references held by the observer service.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"observer-service/referent/weak/alive", KIND_OTHER, UNITS_COUNT,
|
|
|
|
totalNumWeakAlive,
|
|
|
|
"The number of weak references held by the observer service that are "
|
|
|
|
"still alive.");
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"observer-service/referent/weak/dead", KIND_OTHER, UNITS_COUNT,
|
|
|
|
totalNumWeakDead,
|
|
|
|
"The number of weak references held by the observer service that are "
|
|
|
|
"dead.");
|
2014-07-09 15:15:21 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2013-08-26 18:56:23 +00:00
|
|
|
}
|
|
|
|
|
1999-04-06 20:54:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsObserverService Implementation
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsObserverService, nsIObserverService, nsObserverService,
|
|
|
|
nsIMemoryReporter)
|
1999-04-06 20:54:09 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsObserverService::nsObserverService() : mShuttingDown(false) {}
|
1999-04-06 20:54:09 +00:00
|
|
|
|
|
|
|
nsObserverService::~nsObserverService(void) { Shutdown(); }
|
2006-03-21 14:43:56 +00:00
|
|
|
|
2013-08-26 18:56:23 +00:00
|
|
|
void nsObserverService::RegisterReporter() { RegisterWeakMemoryReporter(this); }
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
void nsObserverService::Shutdown() {
|
2018-11-12 10:04:17 +00:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-26 18:56:23 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
mShuttingDown = true;
|
2018-11-12 10:04:17 +00:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
2014-07-09 15:15:21 +00:00
|
|
|
mObserverTopicTable.Clear();
|
1999-04-06 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsresult nsObserverService::Create(nsISupports* aOuter, const nsIID& aIID,
|
|
|
|
void** aInstancePtr) {
|
|
|
|
LOG(("nsObserverService::Create()"));
|
2001-10-22 22:01:27 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsObserverService> os = new nsObserverService();
|
2006-03-21 14:43:56 +00:00
|
|
|
|
2014-07-09 15:15:21 +00: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.
|
2017-06-12 19:34:10 +00:00
|
|
|
NS_DispatchToCurrentThread(
|
|
|
|
NewRunnableMethod("nsObserverService::RegisterReporter", os,
|
|
|
|
&nsObserverService::RegisterReporter));
|
2013-08-26 18:56:23 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
return os->QueryInterface(aIID, aInstancePtr);
|
1999-07-30 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 10:04:17 +00:00
|
|
|
nsresult nsObserverService::EnsureValidCall() const {
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
MOZ_CRASH("Using observer service off the main thread!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2006-11-23 08:43:28 +00:00
|
|
|
|
2018-11-12 10:04:17 +00:00
|
|
|
if (mShuttingDown) {
|
|
|
|
NS_ERROR("Using observer service after XPCOM shutdown!");
|
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
1999-04-20 14:50:47 +00:00
|
|
|
|
2018-11-12 10:04:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsObserverService::FilterHttpOnTopics(const char* aTopic) {
|
2017-09-01 09:55:37 +00:00
|
|
|
// Specifically allow http-on-opening-request and http-on-stop-request in the
|
|
|
|
// child process; see bug 1269765.
|
2016-05-29 23:24:00 +00:00
|
|
|
if (mozilla::net::IsNeckoChild() && !strncmp(aTopic, "http-on-", 8) &&
|
2019-04-26 10:59:41 +00:00
|
|
|
strcmp(aTopic, "http-on-failed-opening-request") &&
|
2017-09-01 09:55:37 +00:00
|
|
|
strcmp(aTopic, "http-on-opening-request") &&
|
|
|
|
strcmp(aTopic, "http-on-stop-request")) {
|
2015-06-16 18:58:04 +00: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,
|
2019-04-01 22:42:34 +00:00
|
|
|
"chrome javascript", false /* from private window */,
|
|
|
|
true /* from chrome context */);
|
2015-06-16 18:58:04 +00:00
|
|
|
console->LogMessage(error);
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2012-11-08 01:24:27 +00:00
|
|
|
|
2018-11-12 10:04:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::AddObserver(nsIObserver* aObserver, const char* aTopic,
|
|
|
|
bool aOwnsWeak) {
|
|
|
|
LOG(("nsObserverService::AddObserver(%p: %s, %s)", (void*)aObserver, aTopic,
|
|
|
|
aOwnsWeak ? "weak" : "strong"));
|
|
|
|
|
|
|
|
MOZ_TRY(EnsureValidCall());
|
|
|
|
if (NS_WARN_IF(!aObserver) || NS_WARN_IF(!aTopic)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_TRY(FilterHttpOnTopics(aTopic));
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsObserverList* observerList = mObserverTopicTable.PutEntry(aTopic);
|
|
|
|
if (!observerList) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2006-03-09 03:14:32 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
return observerList->AddObserver(aObserver, aOwnsWeak);
|
1999-04-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 14:43:56 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsObserverService::RemoveObserver(nsIObserver* aObserver, const char* aTopic) {
|
|
|
|
LOG(("nsObserverService::RemoveObserver(%p: %s)", (void*)aObserver, aTopic));
|
2018-11-12 10:04:17 +00:00
|
|
|
|
2019-03-06 08:55:35 +00:00
|
|
|
if (mShuttingDown) {
|
|
|
|
// The service is shutting down. Let's ignore this call.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-12 10:04:17 +00:00
|
|
|
MOZ_TRY(EnsureValidCall());
|
2014-07-09 15:15:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-12 10:04:17 +00:00
|
|
|
/* This death grip is needed to protect against consumers who call
|
|
|
|
* RemoveObserver from their Destructor thus potentially thus causing
|
|
|
|
* infinite recursion, see bug 485834/bug 325392. */
|
2014-07-09 15:15:21 +00:00
|
|
|
nsCOMPtr<nsIObserver> kungFuDeathGrip(aObserver);
|
|
|
|
return observerList->RemoveObserver(aObserver);
|
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) {
|
2018-11-12 10:04:17 +00:00
|
|
|
LOG(("nsObserverService::EnumerateObservers(%s)", aTopic));
|
|
|
|
|
|
|
|
MOZ_TRY(EnsureValidCall());
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_WARN_IF(!anEnumerator) || NS_WARN_IF(!aTopic)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2006-03-09 03:14:32 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsObserverList* observerList = mObserverTopicTable.GetEntry(aTopic);
|
|
|
|
if (!observerList) {
|
|
|
|
return NS_NewEmptyEnumerator(anEnumerator);
|
|
|
|
}
|
2006-03-09 03:14:32 +00:00
|
|
|
|
2016-04-28 07:11:17 +00:00
|
|
|
observerList->GetObserverList(anEnumerator);
|
|
|
|
return NS_OK;
|
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.
|
2014-07-09 15:15:21 +00:00
|
|
|
NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aSomeData) {
|
|
|
|
LOG(("nsObserverService::NotifyObservers(%s)", aTopic));
|
2006-11-23 08:43:28 +00:00
|
|
|
|
2018-11-12 10:04:17 +00:00
|
|
|
MOZ_TRY(EnsureValidCall());
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_WARN_IF(!aTopic)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2005-01-14 11:25:05 +00:00
|
|
|
|
2017-05-29 19:42:42 +00:00
|
|
|
mozilla::TimeStamp start = TimeStamp::Now();
|
|
|
|
|
2019-02-14 05:10:14 +00:00
|
|
|
AUTO_PROFILER_TEXT_MARKER_CAUSE("NotifyObservers", nsDependentCString(aTopic),
|
|
|
|
OTHER, profiler_get_backtrace());
|
2017-10-13 05:12:57 +00:00
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_CSTR("nsObserverService::NotifyObservers", OTHER,
|
|
|
|
aTopic);
|
2017-06-14 22:58:25 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsObserverList* observerList = mObserverTopicTable.GetEntry(aTopic);
|
|
|
|
if (observerList) {
|
|
|
|
observerList->NotifyObservers(aSubject, aTopic, aSomeData);
|
|
|
|
}
|
2006-03-04 14:04:05 +00:00
|
|
|
|
2017-05-29 19:42:42 +00:00
|
|
|
uint32_t latencyMs = round((TimeStamp::Now() - start).ToMilliseconds());
|
|
|
|
if (latencyMs >= kMinTelemetryNotifyObserversLatencyMs) {
|
|
|
|
Telemetry::Accumulate(Telemetry::NOTIFY_OBSERVERS_LATENCY_MS,
|
|
|
|
nsDependentCString(aTopic), latencyMs);
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +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
|
|
|
NS_IMETHODIMP
|
|
|
|
nsObserverService::UnmarkGrayStrongObservers() {
|
2018-11-12 10:04:17 +00:00
|
|
|
MOZ_TRY(EnsureValidCall());
|
2012-04-30 19:01:11 +00:00
|
|
|
|
2015-04-09 00:16:30 +00:00
|
|
|
nsCOMArray<nsIObserver> strongObservers;
|
2015-07-09 23:49:32 +00:00
|
|
|
for (auto iter = mObserverTopicTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsObserverList* aObserverList = iter.Get();
|
|
|
|
if (aObserverList) {
|
|
|
|
aObserverList->AppendStrongObservers(strongObservers);
|
|
|
|
}
|
|
|
|
}
|
2015-04-09 00:16:30 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < strongObservers.Length(); ++i) {
|
|
|
|
xpc_TryUnmarkWrappedGrayObject(strongObservers[i]);
|
|
|
|
}
|
2012-04-30 19:01:11 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
return NS_OK;
|
2012-04-30 19:01:11 +00:00
|
|
|
}
|