Files
palemoon27/xpcom/ds/nsObserverService.cpp
T
roytam1 e1c4d9e76f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1077318 - call out to C++ for ARMv6 sub-word atomics. r=luke (2810c3f93)
- Bug 1142593 - factor atomics into the platform layer. r=waldo (f5088b2af)
- Bug 1173642 - Import unimplemented ARM64 Ion components. r=efaust (2567975a1)
- Bug 1170107 part 1 - Call GetProperty/CallProperty from the interpreter and Baseline. r=bhackett (fe45a446a)
- Bug 1170107 part 2 - Don't allocate a wrapper object for (pure) primitive.foo operations. r=bhackett (8793d7d2d)
- Bug 1152079 - Remove Purify support files. r=froydnj (69836c373)
- Bug 1152079 - Remove references to deleted Purify files from moz.build. r=bustage (ebd68a5fa)
- Bug 1126089 - Assert we don't Destroy nsFrameLoader in destructor (r=smaug) (c0894ed2b)
- Bug 1250871 - only send messages to child scripts when the docShell is still around. r=smaug (ac2562f55)
- Bug 1168750 - SharedStubs: (part3) Make split between baseline only and shared stubs, r=jandem (21c89cffe)
- Bug 1168750 - SharedStubs: (part4) Track for which engine a stub needs to get compiled, r=jandem (a36eb943b)
- Bug 1168753 - SharedStubs: Simplification to push BaselineFrame* in stubs, r=jandem (4a26b318d)
- pointer style (e20a50c7e)
- Bug 1169391 - Use a ReservedRooted class for optimized Rooted use in vm/Interpreter.cpp, r=terrence (1b80a857d)
- Bug 1182865 - Relax assertion on the allowed class for home objects, r=efaust (33009a9fe)
- Bug 1154391 - Update import declarations to current ES6 spec r=shu (ad7042d54)
- Bug 1154391 - Update parsing of export declarations to match current ES6 spec r=shu (4b509d9ec)
- Bug 1169171 - Split PNK_DELETE into several different kinds for each of the syntactically distinct modes of |delete| operation, depending upon the syntax of the operand. r=efaust (5667265fc)
- Bug 1169511 - Split PNK_TYPEOF into one kind for application to names one kind for application to other expressions. r=efaust (91a1086d8)
- pointer style (0546201d8)
- Bug 1163851 - Remove the remaining two tests of |pc->parsingForInit|, replacing them with yet another argument provided by the caller. r=efaust (919903e1e)
- Bug 1163851 - |pc->parsingForInit| is dead. Long live |InHandling|! r=efaust (bf35fb405)
- Bug 1167030 - Suppress MSVC warning C4661 in js/src. r=luke (1d28ed1d2)
- Bug 1167845 - Rejigger {in,de}crement/assignment parsing to vet targets primarily through a single Parser::isValidSimpleAssignmentTarget method, so that this method can also be called outside the parser. r=efaust (87484d3fc)
- Bug 1166950 - Introduce a new FunctionKind for class-constructors. r=efaust (e9dd197e1)
- Bug 1166950 - Make generator methods constructors. r=efaust (e076cacd6)
- Bug 1167845 - Rename ParseHandler::isName to ParseHandler::maybeName. r=efaust (1e090741b)
- Bug 1141865 - Part 3: Parse new.target, add Reflect support, and tests. (r=jorendorff, r=shu) (9e0282870)
- Bug 1139759 - Self-host %TypedArray%.prototype.copyWithin. r=till (8beea5f7c)
- pointer style (0da72f30a)
- Bug 1139769 - Add TypedArrayBuffer, TypedArrayByteOffset, and TypedArrayElementShift intrinsics to safely get the relevant information for a typed array. r=till (b5847a441)
- pointer style (5486dd33d)
- Bug 1139769 - Self-host %TypedArray%.prototype.subarray. r=t (b10c55e93)
- pointer style (335ab6c87)
- Bug 1140752 - Land code to self-host %TypedArray%.prototype.set, but don't enable it yet, pending perf-testing. This shouldn't make any changes to the build, except in terms of adding more code to it and affecting binary layout. r=jandem, also much feedback from till that effectively amounts to a review as well (98050308d)
- Bug 1144692: Fix --disable-ion builds; r=h4writer (3be32046c)
- Bug 1148970 - Check for possibly incomplete type sets when double checking the correctness of argument type set information, r=jandem. (934130c2e)
- bug 1171125 - Fix nsStackWalk to build for iOS. r=froydnj (c076c4d22)
- Bug 1119980 - Use 'snprintf' instead of 'sprintf' to avoid a warning on Lollipop-based builds. r=froydnj (a000af66c)
- Bug 1146597 - Avoid unnecessary calls to maybeSweep under hasUnanalyzedPreliminaryObjects, r=jandem. (335ba3fc3)
- Bug 1162134 - IonMonkey MIPS: Fix build failure on MIPS (js/src/jit/CodeGenerator.cpp:6740:28: error: `obj' was not declared in this scope). r=bhackett (56824379b)
- Bug 1160884 followup - Add testcase. r=me (5495ad016)
- Bug 1141865 - Part 4: Remove LCallDirectEvalV in anticipation of register pressure with new.target in eval frames. (r=jandem) (cbb706356)
- Bug 1162242 - Part 1: Remove instances of #ifdef PR_LOGGING. r=froydnj PR_LOGGING is now always defined, we can remove #ifdefs checking for it. (878131a4a)
- revert pointer style for patch (a263375f1)
- Bug 1144097. Fix yet another is<ScopeObject>() assert. r=shu (96d516117)
- Bug 1145282. Reverse the sense of IsValidTerminatingScope and rename it to IsSyntacticScope. r=luke (3681fc831)
- Bug 1149897 - PerformanceStatsService data is not monotonic. r=jandem (927639ac6)
- Bug 1142457 - Compute stopwatch durations per thread on MacOS X. r=jandem, r=areinald (a52c388be)
2020-10-02 18:15:14 +08:00

362 lines
10 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
#include "mozilla/Logging.h"
#include "nsAutoPtr.h"
#include "nsIObserverService.h"
#include "nsIObserver.h"
#include "nsObserverService.h"
#include "nsObserverList.h"
#include "nsThreadUtils.h"
#include "nsEnumeratorUtils.h"
#include "xpcpublic.h"
#include "mozilla/net/NeckoCommon.h"
#include "mozilla/Services.h"
#define NOTIFY_GLOBAL_OBSERVERS
// 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
static PRLogModuleInfo*
GetObserverServiceLog()
{
static PRLogModuleInfo* sLog;
if (!sLog) {
sLog = PR_NewLogModule("ObserverService");
}
return sLog;
}
#define LOG(x) PR_LOG(GetObserverServiceLog(), PR_LOG_DEBUG, x)
namespace mozilla {
struct SuspectObserver
{
SuspectObserver(const char* aTopic, size_t aReferentCount)
: topic(aTopic)
, referentCount(aReferentCount)
{
}
const char* topic;
size_t referentCount;
};
struct ObserverServiceReferentCount
{
ObserverServiceReferentCount()
: numStrong(0)
, numWeakAlive(0)
, numWeakDead(0)
{
}
size_t numStrong;
size_t numWeakAlive;
size_t numWeakDead;
nsTArray<SuspectObserver> suspectObservers;
};
} // namespace mozilla
using namespace mozilla;
PLDHashOperator
nsObserverService::CountReferents(nsObserverList* aObserverList,
void* aClosure)
{
if (!aObserverList) {
return PL_DHASH_NEXT;
}
ObserverServiceReferentCount* referentCount =
static_cast<ObserverServiceReferentCount*>(aClosure);
size_t numStrong = 0;
size_t numWeakAlive = 0;
size_t numWeakDead = 0;
nsTArray<ObserverRef>& observers = aObserverList->mObservers;
for (uint32_t i = 0; i < observers.Length(); i++) {
if (observers[i].isWeakRef) {
nsCOMPtr<nsIObserver> observerRef(
do_QueryReferent(observers[i].asWeak()));
if (observerRef) {
numWeakAlive++;
} else {
numWeakDead++;
}
} else {
numStrong++;
}
}
referentCount->numStrong += numStrong;
referentCount->numWeakAlive += numWeakAlive;
referentCount->numWeakDead += numWeakDead;
// Keep track of topics that have a suspiciously large number
// of referents (symptom of leaks).
size_t total = numStrong + numWeakAlive + numWeakDead;
if (total > kSuspectReferentCount) {
SuspectObserver suspect(aObserverList->GetKey(), total);
referentCount->suspectObservers.AppendElement(suspect);
}
return PL_DHASH_NEXT;
}
NS_IMETHODIMP
nsObserverService::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize)
{
ObserverServiceReferentCount referentCount;
mObserverTopicTable.EnumerateEntries(CountReferents, &referentCount);
// These aren't privacy-sensitive and so don't need anonymizing.
nsresult rv;
for (uint32_t i = 0; i < referentCount.suspectObservers.Length(); i++) {
SuspectObserver& suspect = referentCount.suspectObservers[i];
nsPrintfCString suspectPath("observer-service-suspect/"
"referent(topic=%s)",
suspect.topic);
rv = aHandleReport->Callback(
/* process */ EmptyCString(),
suspectPath, KIND_OTHER, UNITS_COUNT, suspect.referentCount,
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;
}
}
rv = aHandleReport->Callback(
/* process */ EmptyCString(),
NS_LITERAL_CSTRING("observer-service/referent/strong"),
KIND_OTHER, UNITS_COUNT, referentCount.numStrong,
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"),
KIND_OTHER, UNITS_COUNT, referentCount.numWeakAlive,
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"),
KIND_OTHER, UNITS_COUNT, referentCount.numWeakDead,
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;
}
////////////////////////////////////////////////////////////////////////////////
// nsObserverService Implementation
NS_IMPL_ISUPPORTS(nsObserverService,
nsIObserverService,
nsObserverService,
nsIMemoryReporter)
nsObserverService::nsObserverService()
: mShuttingDown(false)
{
}
nsObserverService::~nsObserverService(void)
{
Shutdown();
}
void
nsObserverService::RegisterReporter()
{
#if !defined(MOZILLA_XPCOMRT_API)
RegisterWeakMemoryReporter(this);
#endif // !defined(MOZILLA_XPCOMRT_API)
}
void
nsObserverService::Shutdown()
{
#if !defined(MOZILLA_XPCOMRT_API)
UnregisterWeakMemoryReporter(this);
#endif // !defined(MOZILLA_XPCOMRT_API)
mShuttingDown = true;
mObserverTopicTable.Clear();
}
nsresult
nsObserverService::Create(nsISupports* aOuter, const nsIID& aIID,
void** aInstancePtr)
{
LOG(("nsObserverService::Create()"));
nsRefPtr<nsObserverService> os = new nsObserverService();
if (!os) {
return NS_ERROR_OUT_OF_MEMORY;
}
// The memory reporter can not be immediately registered here because
// the nsMemoryReporterManager may attempt to get the nsObserverService
// during initialization, causing a recursive GetService.
nsRefPtr<nsRunnableMethod<nsObserverService>> registerRunnable =
NS_NewRunnableMethod(os, &nsObserverService::RegisterReporter);
NS_DispatchToCurrentThread(registerRunnable);
return os->QueryInterface(aIID, aInstancePtr);
}
#define NS_ENSURE_VALIDCALL \
if (!NS_IsMainThread()) { \
MOZ_CRASH("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; \
}
NS_IMETHODIMP
nsObserverService::AddObserver(nsIObserver* aObserver, const char* aTopic,
bool aOwnsWeak)
{
LOG(("nsObserverService::AddObserver(%p: %s)",
(void*)aObserver, aTopic));
NS_ENSURE_VALIDCALL
if (NS_WARN_IF(!aObserver) || NS_WARN_IF(!aTopic)) {
return NS_ERROR_INVALID_ARG;
}
if (mozilla::net::IsNeckoChild() && !strncmp(aTopic, "http-on-", 8)) {
return NS_ERROR_NOT_IMPLEMENTED;
}
nsObserverList* observerList = mObserverTopicTable.PutEntry(aTopic);
if (!observerList) {
return NS_ERROR_OUT_OF_MEMORY;
}
return observerList->AddObserver(aObserver, aOwnsWeak);
}
NS_IMETHODIMP
nsObserverService::RemoveObserver(nsIObserver* aObserver, const char* aTopic)
{
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);
}
NS_IMETHODIMP
nsObserverService::EnumerateObservers(const char* aTopic,
nsISimpleEnumerator** anEnumerator)
{
NS_ENSURE_VALIDCALL
if (NS_WARN_IF(!anEnumerator) || NS_WARN_IF(!aTopic)) {
return NS_ERROR_INVALID_ARG;
}
nsObserverList* observerList = mObserverTopicTable.GetEntry(aTopic);
if (!observerList) {
return NS_NewEmptyEnumerator(anEnumerator);
}
return observerList->GetObserverList(anEnumerator);
}
// Enumerate observers of aTopic and call Observe on each.
NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports* aSubject,
const char* aTopic,
const char16_t* aSomeData)
{
LOG(("nsObserverService::NotifyObservers(%s)", aTopic));
NS_ENSURE_VALIDCALL
if (NS_WARN_IF(!aTopic)) {
return NS_ERROR_INVALID_ARG;
}
nsObserverList* observerList = mObserverTopicTable.GetEntry(aTopic);
if (observerList) {
observerList->NotifyObservers(aSubject, aTopic, aSomeData);
}
#ifdef NOTIFY_GLOBAL_OBSERVERS
observerList = mObserverTopicTable.GetEntry("*");
if (observerList) {
observerList->NotifyObservers(aSubject, aTopic, aSomeData);
}
#endif
return NS_OK;
}
static PLDHashOperator
UnmarkGrayObserverEntry(nsObserverList* aObserverList, void* aClosure)
{
if (aObserverList) {
aObserverList->UnmarkGrayStrongObservers();
}
return PL_DHASH_NEXT;
}
NS_IMETHODIMP
nsObserverService::UnmarkGrayStrongObservers()
{
NS_ENSURE_VALIDCALL
mObserverTopicTable.EnumerateEntries(UnmarkGrayObserverEntry, nullptr);
return NS_OK;
}