1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "core/timing/PerformanceObserver.h"
8 #include "bindings/core/v8/ExceptionState.h"
9 #include "core/dom/ExceptionCode.h"
10 #include "core/dom/ExecutionContext.h"
11 #include "core/timing/PerformanceBase.h"
12 #include "core/timing/PerformanceEntry.h"
13 #include "core/timing/PerformanceObserverCallback.h"
14 #include "core/timing/PerformanceObserverEntryList.h"
15 #include "core/timing/PerformanceObserverInit.h"
16 #include "platform/Timer.h"
17 #include "wtf/MainThread.h"
22 PerformanceObserver
* PerformanceObserver::create(PerformanceBase
* performance
, PerformanceObserverCallback
* callback
)
24 ASSERT(isMainThread());
25 return new PerformanceObserver(performance
, callback
);
28 PerformanceObserver::PerformanceObserver(PerformanceBase
* performance
, PerformanceObserverCallback
* callback
)
29 : m_callback(callback
)
30 , m_performance(performance
)
31 , m_filterOptions(PerformanceEntry::Invalid
)
32 , m_isRegistered(false)
36 PerformanceObserver::~PerformanceObserver()
40 void PerformanceObserver::observe(const PerformanceObserverInit
& observerInit
, ExceptionState
& exceptionState
)
43 exceptionState
.throwTypeError("Window may be destroyed? Performance target is invalid.");
47 PerformanceEntryTypeMask entryTypes
= PerformanceEntry::Invalid
;
48 if (observerInit
.hasEntryTypes() && observerInit
.entryTypes().size()) {
49 const Vector
<String
>& sequence
= observerInit
.entryTypes();
50 for (const auto& entryTypeString
: sequence
)
51 entryTypes
|= PerformanceEntry::toEntryTypeEnum(entryTypeString
);
53 if (entryTypes
== PerformanceEntry::Invalid
) {
54 exceptionState
.throwTypeError("A Performance Observer MUST have a non-empty entryTypes attribute.");
57 m_filterOptions
= entryTypes
;
59 m_performance
->updatePerformanceObserverFilterOptions();
61 m_performance
->registerPerformanceObserver(*this);
62 m_isRegistered
= true;
65 void PerformanceObserver::disconnect()
67 m_performanceEntries
.clear();
69 m_performance
->unregisterPerformanceObserver(*this);
70 m_isRegistered
= false;
73 void PerformanceObserver::enqueuePerformanceEntry(PerformanceEntry
& entry
)
75 ASSERT(isMainThread());
76 m_performanceEntries
.append(&entry
);
78 m_performance
->activateObserver(*this);
81 bool PerformanceObserver::shouldBeSuspended() const
83 return m_callback
->executionContext() && m_callback
->executionContext()->activeDOMObjectsAreSuspended();
86 void PerformanceObserver::deliver()
88 ASSERT(!shouldBeSuspended());
90 if (m_performanceEntries
.isEmpty())
93 PerformanceEntryVector performanceEntries
;
94 performanceEntries
.swap(m_performanceEntries
);
95 Member
<PerformanceObserverEntryList
> entryList(new PerformanceObserverEntryList(performanceEntries
));
97 m_callback
->handleEvent(entryList
, this);
100 DEFINE_TRACE(PerformanceObserver
)
102 visitor
->trace(m_callback
);
103 visitor
->trace(m_performance
);
104 visitor
->trace(m_performanceEntries
);