Bug 1195700 - Disconnect performance observer before being destroyed to avoid crash. r=baku

This commit is contained in:
Hiroyuki Ikezoe 2015-08-20 14:33:54 -07:00
parent 4ed420a354
commit bd4e472575
4 changed files with 19 additions and 1 deletions

View File

@ -38,6 +38,7 @@ PerformanceObserver::PerformanceObserver(nsPIDOMWindow* aOwner,
PerformanceObserverCallback& aCb)
: mOwner(aOwner)
, mCallback(&aCb)
, mConnected(false)
{
MOZ_ASSERT(mOwner);
mPerformance = aOwner->GetPerformance();
@ -46,6 +47,7 @@ PerformanceObserver::PerformanceObserver(nsPIDOMWindow* aOwner,
PerformanceObserver::PerformanceObserver(WorkerPrivate* aWorkerPrivate,
PerformanceObserverCallback& aCb)
: mCallback(&aCb)
, mConnected(false)
{
MOZ_ASSERT(aWorkerPrivate);
mPerformance = aWorkerPrivate->GlobalScope()->GetPerformance();
@ -53,6 +55,8 @@ PerformanceObserver::PerformanceObserver(WorkerPrivate* aWorkerPrivate,
PerformanceObserver::~PerformanceObserver()
{
Disconnect();
MOZ_ASSERT(!mConnected);
}
// static
@ -145,10 +149,15 @@ PerformanceObserver::Observe(const PerformanceObserverInit& aOptions,
mEntryTypes = validEntryTypes;
mPerformance->AddObserver(this);
mConnected = true;
}
void
PerformanceObserver::Disconnect()
{
mPerformance->RemoveObserver(this);
if (mConnected) {
MOZ_ASSERT(mPerformance);
mPerformance->RemoveObserver(this);
mConnected = false;
}
}

View File

@ -68,6 +68,7 @@ private:
nsRefPtr<PerformanceObserverCallback> mCallback;
nsRefPtr<PerformanceBase> mPerformance;
nsTArray<nsString> mEntryTypes;
bool mConnected;
};
} // namespace dom

View File

@ -48,6 +48,7 @@ async_test(t => {
observedEntryList = list;
});
observer.observe({entryTypes: ['resource']});
t.add_cleanup(() => observer.disconnect());
assert_equals(observedEntries.length, 0);

View File

@ -44,6 +44,7 @@ test(t => {
list.getEntries().forEach(entry => observedEntries.push(entry));
});
observer.observe({entryTypes: ['mark', 'measure']});
t.add_cleanup(() => observer.disconnect());
assert_equals(observedEntries.length, 0,
"User timing entries should never be observed.");
@ -107,6 +108,7 @@ test(t => {
observedEntryList = list;
});
observer.observe({entryTypes: ['mark']});
t.add_cleanup(() => observer.disconnect());
performance.mark("test");
assert_array_equals(observedEntryList.getEntries({"entryType": "mark"}),
@ -150,6 +152,7 @@ test(t => {
observedEntryList = list;
});
observer.observe({entryTypes: ['mark', 'measure']});
t.add_cleanup(() => observer.disconnect());
performance.mark("test");
assert_array_equals(observedEntryList.getEntriesByType("mark"),
@ -169,6 +172,7 @@ test(t => {
observedEntryList = list;
});
observer.observe({entryTypes: ['mark', 'measure']});
t.add_cleanup(() => observer.disconnect());
performance.mark("test");
assert_array_equals(observedEntryList.getEntriesByName("test"),
@ -190,6 +194,7 @@ test(t => {
observer.observe({entryTypes: ['mark', 'measure']});
observer.observe({entryTypes: ['mark', 'measure']});
t.add_cleanup(() => observer.disconnect());
performance.mark("test-start");
performance.mark("test-end");
@ -210,6 +215,7 @@ test(t => {
observer.observe({entryTypes: ['mark', 'measure']});
observer.observe({entryTypes: ['mark']});
t.add_cleanup(() => observer.disconnect());
performance.mark("test-start");
performance.mark("test-end");
@ -230,6 +236,7 @@ test(t => {
observer.observe({entryTypes: ['mark']});
observer.observe({entryTypes: ['measure']});
t.add_cleanup(() => observer.disconnect());
performance.mark("test-start");
performance.mark("test-end");