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..e0e9b47563e265effc507b19c8a709498fef38af |
--- /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 entryType = PerformanceEntry::Invalid; |
+ if (observerInit.hasEntryType() && observerInit.entryType().size()) { |
+ const Vector<String>& sequence = observerInit.entryType(); |
+ for (unsigned i = 0; i < sequence.size(); ++i) |
+ entryType |= PerformanceEntry::toEntryTypeEnum(sequence[i]); |
+ } |
+ if (entryType == PerformanceEntry::Invalid) { |
+ exceptionState.throwTypeError("A Performance Observer MUST have a non-empty entryType attribute."); |
+ return; |
+ } |
+ if (m_isRegistered) |
+ m_performance->unregisterPerformanceObserver(*this); |
+ m_filterOptions = entryType; |
+ 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 |