Chromium Code Reviews| Index: Source/core/timing/PerformanceObserver.cpp |
| diff --git a/Source/core/timing/PerformanceObserver.cpp b/Source/core/timing/PerformanceObserver.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..162496da44193b4ab981710f280ecd3eb937fded |
| --- /dev/null |
| +++ b/Source/core/timing/PerformanceObserver.cpp |
| @@ -0,0 +1,112 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "core/timing/PerformanceObserver.h" |
| + |
| +#include "bindings/core/v8/ExceptionState.h" |
| +#include "core/dom/ExceptionCode.h" |
| +#include "core/inspector/InspectorInstrumentation.h" |
| +#include "core/timing/PerformanceBase.h" |
| +#include "core/timing/PerformanceEntry.h" |
| +#include "core/timing/PerformanceObserverCallback.h" |
| +#include "core/timing/PerformanceObserverEntryList.h" |
| +#include "core/timing/PerformanceObserverInit.h" |
| +#include "platform/Timer.h" |
| +#include "wtf/MainThread.h" |
| +#include <algorithm> |
| + |
| +namespace blink { |
| + |
| +PerformanceObserver* PerformanceObserver::create(PerformanceBase* performance, PerformanceObserverCallback* callback) |
| +{ |
| + ASSERT(isMainThread()); |
| + return new PerformanceObserver(performance, callback); |
| +} |
| + |
| +PerformanceObserver::PerformanceObserver(PerformanceBase* performance, PerformanceObserverCallback* callback) |
| + : m_callback(callback) |
| + , m_performance(performance) |
| + , m_filterOptions(PerformanceEntry::Invalid) |
| + , m_isRegistered(false) |
| +{ |
| +} |
| + |
| +PerformanceObserver::~PerformanceObserver() |
| +{ |
| + if (!m_performanceEntries.isEmpty()) |
| + InspectorInstrumentation::didClearAllPerformanceObservations(m_callback->executionContext(), this); |
| +} |
| + |
| +void PerformanceObserver::observe(const PerformanceObserverInit& observerInit, ExceptionState& exceptionState) |
| +{ |
| + if (!m_performance) { |
| + exceptionState.throwTypeError("Window may be destroyed? Performance target is invalid."); |
| + return; |
| + } |
| + |
| + PerformanceEntryTypeMask entryTypes = PerformanceEntry::Invalid; |
| + if (observerInit.hasEntryTypes() && observerInit.entryTypes().size()) { |
| + const Vector<String>& sequence = observerInit.entryTypes(); |
| + for (unsigned i = 0; i < sequence.size(); ++i) |
|
esprehn
2015/08/20 08:33:48
You can use range loops.
for (const auto& item :
MikeB
2015/08/25 19:42:42
Done.
|
| + entryTypes |= PerformanceEntry::toEntryTypeEnum(sequence[i]); |
| + } |
| + if (entryTypes == PerformanceEntry::Invalid) { |
| + exceptionState.throwTypeError("A Performance Observer MUST have a non-empty entryTypes attribute."); |
| + return; |
| + } |
| + if (m_isRegistered) |
| + m_performance->unregisterPerformanceObserver(*this); |
|
esprehn
2015/08/20 08:33:48
Why unregister? I think you just want to only regi
MikeB
2015/08/25 19:42:42
Done.
|
| + m_filterOptions = entryTypes; |
| + m_performance->registerPerformanceObserver(*this); |
| + m_isRegistered = true; |
| +} |
| + |
| +void PerformanceObserver::disconnect() |
| +{ |
| + m_performanceEntries.clear(); |
| + InspectorInstrumentation::didClearAllPerformanceObservations(m_callback->executionContext(), this); |
| + if (m_performance) |
| + m_performance->unregisterPerformanceObserver(*this); |
| + m_isRegistered = false; |
| +} |
| + |
| +void PerformanceObserver::enqueuePerformanceEntry(PerformanceEntry& entry) |
| +{ |
| + ASSERT(isMainThread()); |
| + m_performanceEntries.append(&entry); |
| + if (m_performance) |
| + m_performance->activateObserver(*this); |
| + InspectorInstrumentation::didEnqueuePerformanceObserverEntries(m_callback->executionContext(), this); |
| +} |
| + |
| +bool PerformanceObserver::shouldBeSuspended() const |
| +{ |
| + return m_callback->executionContext() && m_callback->executionContext()->activeDOMObjectsAreSuspended(); |
| +} |
| + |
| +void PerformanceObserver::deliver() |
| +{ |
| + ASSERT(!shouldBeSuspended()); |
| + |
| + if (m_performanceEntries.isEmpty()) |
| + return; |
| + |
| + PerformanceEntryVector performanceEntries; |
| + performanceEntries.swap(m_performanceEntries); |
| + Member<PerformanceObserverEntryList> entryList(new PerformanceObserverEntryList(performanceEntries)); |
| + |
| + InspectorInstrumentation::willDeliverPerformanceObservations(m_callback->executionContext(), this); |
| + m_callback->handleEvent(entryList, this); |
| + InspectorInstrumentation::didDeliverPerformanceObservations(m_callback->executionContext()); |
| +} |
| + |
| +DEFINE_TRACE(PerformanceObserver) |
| +{ |
| + visitor->trace(m_callback); |
| + visitor->trace(m_performance); |
| + visitor->trace(m_performanceEntries); |
| +} |
| + |
| +} // namespace blink |