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 : 8b2eada3f9c80fe9feff880e985739a368e7b997
This commit is contained in:
David Rajchenbach-Teller 2016-05-30 12:24:50 +02:00
Родитель 5f15a2d19b
Коммит 784f035bea
2 изменённых файлов: 26 добавлений и 8 удалений

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

@ -136,6 +136,14 @@ 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",
"content-child-shutdown",
"xpcom-will-shutdown"
};
} // namespace
/* ------------------------------------------------------
@ -638,6 +646,7 @@ NS_IMPL_ISUPPORTS(nsPerformanceStatsService, nsIPerformanceStatsService, nsIObse
nsPerformanceStatsService::nsPerformanceStatsService()
: mIsAvailable(false)
, mDisposed(false)
#if defined(XP_WIN)
, mProcessId(GetCurrentProcessId())
#else
@ -705,13 +714,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 +794,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.
*