Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "core/timing/PerformanceObserver.h" | |
| 7 | |
| 8 #include "bindings/core/v8/ExceptionState.h" | |
| 9 #include "core/dom/ExceptionCode.h" | |
| 10 #include "core/inspector/InspectorInstrumentation.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" | |
| 18 #include <algorithm> | |
| 19 | |
| 20 namespace blink { | |
| 21 | |
| 22 class BLINK_EXPORT PerformanceObserverTimerCoordinator { | |
| 23 public: | |
| 24 PerformanceObserverTimerCoordinator(); | |
| 25 ~PerformanceObserverTimerCoordinator() {} | |
| 26 | |
| 27 void fired(Timer<PerformanceObserverTimerCoordinator>*); | |
| 28 PerformanceObserverSet& activePerformanceObservers() { return *m_activeObser vers; } | |
| 29 PerformanceObserverSet& suspendedPerformanceObservers() { return *m_suspende dObservers; } | |
| 30 private: | |
| 31 Persistent<PerformanceObserverSet> m_activeObservers; | |
| 32 Persistent<PerformanceObserverSet> m_suspendedObservers; | |
| 33 }; | |
| 34 | |
| 35 PerformanceObserverTimerCoordinator::PerformanceObserverTimerCoordinator() | |
| 36 : m_activeObservers(new PerformanceObserverSet()) | |
| 37 , m_suspendedObservers(new PerformanceObserverSet()) | |
| 38 { | |
| 39 } | |
| 40 | |
| 41 void PerformanceObserverTimerCoordinator::fired(Timer<PerformanceObserverTimerCo ordinator>*) | |
| 42 { | |
| 43 ASSERT(isMainThread()); | |
| 44 PerformanceObserverVector observers; | |
| 45 copyToVector(activePerformanceObservers(), observers); | |
| 46 activePerformanceObservers().clear(); | |
| 47 for (size_t i = 0; i < observers.size(); ++i) { | |
| 48 if (observers[i]->shouldBeSuspended()) | |
| 49 suspendedPerformanceObservers().add(observers[i]); | |
| 50 else | |
| 51 observers[i]->deliver(); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 static PerformanceObserverTimerCoordinator& timerCoordinator() | |
| 56 { | |
|
esprehn
2015/07/24 08:23:12
No static, PerformanceBase should own this. Each w
MikeB
2015/07/24 19:30:38
Done.
| |
| 57 DEFINE_STATIC_LOCAL(PerformanceObserverTimerCoordinator*, coordinator, (new PerformanceObserverTimerCoordinator)); | |
| 58 return *coordinator; | |
| 59 } | |
| 60 | |
| 61 static Timer<PerformanceObserverTimerCoordinator>& deliverObservationsTimer() | |
| 62 { | |
| 63 DEFINE_STATIC_LOCAL(Timer<PerformanceObserverTimerCoordinator>*, timer, (new Timer<PerformanceObserverTimerCoordinator>(&timerCoordinator(), &PerformanceObs erverTimerCoordinator::fired))); | |
|
esprehn
2015/07/24 08:23:12
static Timer is usually bad, I think you want Perf
MikeB
2015/07/24 19:30:38
Done.
| |
| 64 return *timer; | |
| 65 } | |
| 66 | |
| 67 PerformanceObserver* PerformanceObserver::create(PerformanceBase* performance, P erformanceObserverCallback* callback) | |
| 68 { | |
| 69 ASSERT(isMainThread()); | |
| 70 return new PerformanceObserver(performance, callback); | |
| 71 } | |
| 72 | |
| 73 PerformanceObserver::PerformanceObserver(PerformanceBase* performance, Performan ceObserverCallback* callback) | |
| 74 : m_callback(callback) | |
| 75 , m_performance(performance) | |
| 76 , m_filterOptions(PerformanceEntry::Invalid) | |
| 77 , m_isRegistered(false) | |
| 78 { | |
| 79 } | |
| 80 | |
| 81 PerformanceObserver::~PerformanceObserver() | |
| 82 { | |
| 83 if (!m_performanceEntries.isEmpty()) | |
| 84 InspectorInstrumentation::didClearAllPerformanceObservations(m_callback- >executionContext(), this); | |
| 85 } | |
| 86 | |
| 87 void PerformanceObserver::observe(const PerformanceObserverInit& observerInit, E xceptionState& exceptionState) | |
| 88 { | |
| 89 if (!m_performance) { | |
| 90 exceptionState.throwTypeError("Window may be destroyed? Performance targ et is invalid."); | |
| 91 return; | |
| 92 } | |
| 93 | |
| 94 PerformanceEntryType_t entryType = PerformanceEntry::Invalid; | |
| 95 if (observerInit.hasEntryType() && observerInit.entryType().size()) { | |
| 96 const Vector<String>& sequence = observerInit.entryType(); | |
| 97 for (unsigned i = 0; i < sequence.size(); ++i) | |
| 98 entryType |= PerformanceEntry::toEntryTypeEnum(sequence[i]); | |
| 99 } | |
| 100 if (entryType == PerformanceEntry::Invalid) { | |
| 101 exceptionState.throwTypeError("A Performance Observer MUST have a non-em pty entryType attribute."); | |
| 102 return; | |
| 103 } | |
| 104 if (m_isRegistered) | |
| 105 m_performance->unregisterPerformanceObserver(this); | |
| 106 m_filterOptions = entryType; | |
| 107 m_performance->registerPerformanceObserver(this); | |
| 108 m_isRegistered = true; | |
| 109 } | |
| 110 | |
| 111 void PerformanceObserver::disconnect() | |
| 112 { | |
| 113 m_performanceEntries.clear(); | |
| 114 InspectorInstrumentation::didClearAllPerformanceObservations(m_callback->exe cutionContext(), this); | |
| 115 if (m_performance) | |
| 116 m_performance->unregisterPerformanceObserver(this); | |
| 117 m_isRegistered = false; | |
| 118 } | |
| 119 | |
| 120 static void activateObserver(PerformanceObserver* observer) | |
| 121 { | |
| 122 if (timerCoordinator().activePerformanceObservers().isEmpty()) | |
| 123 deliverObservationsTimer().startOneShot(0, FROM_HERE); | |
| 124 | |
| 125 timerCoordinator().activePerformanceObservers().add(observer); | |
| 126 } | |
| 127 | |
| 128 void PerformanceObserver::enqueuePerformanceEntry(PerformanceEntry* entry) | |
| 129 { | |
| 130 ASSERT(isMainThread()); | |
| 131 m_performanceEntries.append(entry); | |
| 132 activateObserver(this); | |
| 133 InspectorInstrumentation::didEnqueuePerformanceObserverEntries(m_callback->e xecutionContext(), this); | |
| 134 } | |
| 135 | |
| 136 bool PerformanceObserver::shouldBeSuspended() const | |
| 137 { | |
| 138 return m_callback->executionContext() && m_callback->executionContext()->act iveDOMObjectsAreSuspended(); | |
| 139 } | |
| 140 | |
| 141 void PerformanceObserver::deliver() | |
| 142 { | |
| 143 ASSERT(!shouldBeSuspended()); | |
| 144 | |
| 145 if (m_performanceEntries.isEmpty()) | |
| 146 return; | |
| 147 | |
| 148 PerformanceEntryVector performanceEntries; | |
| 149 performanceEntries.swap(m_performanceEntries); | |
| 150 Member<PerformanceObserverEntryList> entryList(new PerformanceObserverEntryL ist(performanceEntries)); | |
| 151 | |
| 152 InspectorInstrumentation::willDeliverPerformanceObservations(m_callback->exe cutionContext(), this); | |
| 153 m_callback->handleEvent(entryList, this); | |
| 154 InspectorInstrumentation::didDeliverPerformanceObservations(m_callback->exec utionContext()); | |
| 155 } | |
| 156 | |
| 157 void PerformanceObserver::resumeSuspendedObservers() | |
| 158 { | |
| 159 ASSERT(isMainThread()); | |
| 160 if (timerCoordinator().suspendedPerformanceObservers().isEmpty()) | |
| 161 return; | |
| 162 | |
| 163 PerformanceObserverVector suspended; | |
| 164 copyToVector(timerCoordinator().suspendedPerformanceObservers(), suspended); | |
| 165 for (size_t i = 0; i < suspended.size(); ++i) { | |
| 166 if (!suspended[i]->shouldBeSuspended()) { | |
| 167 timerCoordinator().suspendedPerformanceObservers().remove(suspended[ i]); | |
| 168 activateObserver(suspended[i]); | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 | |
| 174 DEFINE_TRACE(PerformanceObserver) | |
| 175 { | |
| 176 visitor->trace(m_callback); | |
| 177 visitor->trace(m_performance); | |
| 178 visitor->trace(m_performanceEntries); | |
| 179 } | |
| 180 | |
| 181 } // namespace blink | |
| OLD | NEW |