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

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

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