Bug 1261702 - Make nsPerformanceStatsService::Dispose() idempotent,r=froydnj

Although I haven't been able to pinpoint why, it looks like
nsPerformanceStatsService::Dispose() may be called twice, which in
turn causes crashes. This patch makes sure that calling the method
twice is idempotent.

Also, just in case this was due to a typo in
AddObserver/RemoveObserver, this patch replaces the literal strings
used in both with constants.

MozReview-Commit-ID: 8fXO20r5xvO

--HG--
extra : rebase_source : 490f1a5186a426a41ab567e564cdbd46080262ec
This commit is contained in:
David Rajchenbach-Teller 2016-06-07 10:45:44 +02:00
Родитель 9ca9e93794
Коммит b9cd80d622
2 изменённых файлов: 25 добавлений и 8 удалений

Просмотреть файл

@ -136,6 +136,13 @@ GenerateUniqueGroupId(const JSRuntime* rt, uint64_t uid, uint64_t processId, nsA
groupId.AppendInt(uid);
}
static const char* TOPICS[] = {
"profile-before-change",
"quit-application",
"quit-application-granted",
"xpcom-will-shutdown"
};
} // namespace
/* ------------------------------------------------------
@ -638,6 +645,7 @@ NS_IMPL_ISUPPORTS(nsPerformanceStatsService, nsIPerformanceStatsService, nsIObse
nsPerformanceStatsService::nsPerformanceStatsService()
: mIsAvailable(false)
, mDisposed(false)
#if defined(XP_WIN)
, mProcessId(GetCurrentProcessId())
#else
@ -705,13 +713,18 @@ nsPerformanceStatsService::Dispose()
RefPtr<nsPerformanceStatsService> kungFuDeathGrip(this);
mIsAvailable = false;
if (mDisposed) {
// Make sure that we don't double-dispose.
return;
}
mDisposed = true;
// Disconnect from nsIObserverService.
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->RemoveObserver(this, "profile-before-change");
obs->RemoveObserver(this, "quit-application");
obs->RemoveObserver(this, "quit-application-granted");
obs->RemoveObserver(this, "xpcom-will-shutdown");
for (size_t i = 0; i < mozilla::ArrayLength(TOPICS); ++i) {
mozilla::Unused << obs->RemoveObserver(this, TOPICS[i]);
}
}
// Clear up and disconnect from JSAPI.
@ -780,10 +793,9 @@ nsPerformanceStatsService::InitInternal()
// regular shutdown order.
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->AddObserver(this, "profile-before-change", false);
obs->AddObserver(this, "quit-application-granted", false);
obs->AddObserver(this, "quit-application", false);
obs->AddObserver(this, "xpcom-will-shutdown", false);
for (size_t i = 0; i < mozilla::ArrayLength(TOPICS); ++i) {
mozilla::Unused << obs->AddObserver(this, TOPICS[i], false);
}
}
// Connect to JSAPI.

Просмотреть файл

@ -142,6 +142,11 @@ protected:
*/
bool mIsAvailable;
/**
* `true` once we have called `Dispose()`.
*/
bool mDisposed;
/**
* A unique identifier for the process.
*