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 PerformanceObserver* PerformanceObserver::create(PerformanceBase* performance, P
erformanceObserverCallback* callback) |
| 23 { |
| 24 ASSERT(isMainThread()); |
| 25 return new PerformanceObserver(performance, callback); |
| 26 } |
| 27 |
| 28 PerformanceObserver::PerformanceObserver(PerformanceBase* performance, Performan
ceObserverCallback* callback) |
| 29 : m_callback(callback) |
| 30 , m_performance(performance) |
| 31 , m_filterOptions(PerformanceEntry::Invalid) |
| 32 , m_isRegistered(false) |
| 33 { |
| 34 } |
| 35 |
| 36 PerformanceObserver::~PerformanceObserver() |
| 37 { |
| 38 if (!m_performanceEntries.isEmpty()) |
| 39 InspectorInstrumentation::didClearAllPerformanceObservations(m_callback-
>executionContext(), this); |
| 40 } |
| 41 |
| 42 void PerformanceObserver::observe(const PerformanceObserverInit& observerInit, E
xceptionState& exceptionState) |
| 43 { |
| 44 if (!m_performance) { |
| 45 exceptionState.throwTypeError("Window may be destroyed? Performance targ
et is invalid."); |
| 46 return; |
| 47 } |
| 48 |
| 49 PerformanceEntryTypeMask entryTypes = PerformanceEntry::Invalid; |
| 50 if (observerInit.hasEntryTypes() && observerInit.entryTypes().size()) { |
| 51 const Vector<String>& sequence = observerInit.entryTypes(); |
| 52 for (const auto& entryTypeString : sequence) |
| 53 entryTypes |= PerformanceEntry::toEntryTypeEnum(entryTypeString); |
| 54 } |
| 55 if (entryTypes == PerformanceEntry::Invalid) { |
| 56 exceptionState.throwTypeError("A Performance Observer MUST have a non-em
pty entryTypes attribute."); |
| 57 return; |
| 58 } |
| 59 m_filterOptions = entryTypes; |
| 60 if (m_isRegistered) |
| 61 m_performance->updatePerformanceObserverFilterOptions(); |
| 62 else |
| 63 m_performance->registerPerformanceObserver(*this); |
| 64 m_isRegistered = true; |
| 65 } |
| 66 |
| 67 void PerformanceObserver::disconnect() |
| 68 { |
| 69 m_performanceEntries.clear(); |
| 70 InspectorInstrumentation::didClearAllPerformanceObservations(m_callback->exe
cutionContext(), this); |
| 71 if (m_performance) |
| 72 m_performance->unregisterPerformanceObserver(*this); |
| 73 m_isRegistered = false; |
| 74 } |
| 75 |
| 76 void PerformanceObserver::enqueuePerformanceEntry(PerformanceEntry& entry) |
| 77 { |
| 78 ASSERT(isMainThread()); |
| 79 m_performanceEntries.append(&entry); |
| 80 if (m_performance) |
| 81 m_performance->activateObserver(*this); |
| 82 InspectorInstrumentation::didEnqueuePerformanceObserverEntries(m_callback->e
xecutionContext(), this); |
| 83 } |
| 84 |
| 85 bool PerformanceObserver::shouldBeSuspended() const |
| 86 { |
| 87 return m_callback->executionContext() && m_callback->executionContext()->act
iveDOMObjectsAreSuspended(); |
| 88 } |
| 89 |
| 90 void PerformanceObserver::deliver() |
| 91 { |
| 92 ASSERT(!shouldBeSuspended()); |
| 93 |
| 94 if (m_performanceEntries.isEmpty()) |
| 95 return; |
| 96 |
| 97 PerformanceEntryVector performanceEntries; |
| 98 performanceEntries.swap(m_performanceEntries); |
| 99 Member<PerformanceObserverEntryList> entryList(new PerformanceObserverEntryL
ist(performanceEntries)); |
| 100 |
| 101 InspectorInstrumentation::willDeliverPerformanceObservations(m_callback->exe
cutionContext(), this); |
| 102 m_callback->handleEvent(entryList, this); |
| 103 InspectorInstrumentation::didDeliverPerformanceObservations(m_callback->exec
utionContext()); |
| 104 } |
| 105 |
| 106 DEFINE_TRACE(PerformanceObserver) |
| 107 { |
| 108 visitor->trace(m_callback); |
| 109 visitor->trace(m_performance); |
| 110 visitor->trace(m_performanceEntries); |
| 111 } |
| 112 |
| 113 } // namespace blink |
OLD | NEW |