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/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" |
| 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 } |
| 39 |
| 40 void PerformanceObserver::observe(const PerformanceObserverInit& observerInit, E
xceptionState& exceptionState) |
| 41 { |
| 42 if (!m_performance) { |
| 43 exceptionState.throwTypeError("Window may be destroyed? Performance targ
et is invalid."); |
| 44 return; |
| 45 } |
| 46 |
| 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); |
| 52 } |
| 53 if (entryTypes == PerformanceEntry::Invalid) { |
| 54 exceptionState.throwTypeError("A Performance Observer MUST have a non-em
pty entryTypes attribute."); |
| 55 return; |
| 56 } |
| 57 m_filterOptions = entryTypes; |
| 58 if (m_isRegistered) |
| 59 m_performance->updatePerformanceObserverFilterOptions(); |
| 60 else |
| 61 m_performance->registerPerformanceObserver(*this); |
| 62 m_isRegistered = true; |
| 63 } |
| 64 |
| 65 void PerformanceObserver::disconnect() |
| 66 { |
| 67 m_performanceEntries.clear(); |
| 68 if (m_performance) |
| 69 m_performance->unregisterPerformanceObserver(*this); |
| 70 m_isRegistered = false; |
| 71 } |
| 72 |
| 73 void PerformanceObserver::enqueuePerformanceEntry(PerformanceEntry& entry) |
| 74 { |
| 75 ASSERT(isMainThread()); |
| 76 m_performanceEntries.append(&entry); |
| 77 if (m_performance) |
| 78 m_performance->activateObserver(*this); |
| 79 } |
| 80 |
| 81 bool PerformanceObserver::shouldBeSuspended() const |
| 82 { |
| 83 return m_callback->executionContext() && m_callback->executionContext()->act
iveDOMObjectsAreSuspended(); |
| 84 } |
| 85 |
| 86 void PerformanceObserver::deliver() |
| 87 { |
| 88 ASSERT(!shouldBeSuspended()); |
| 89 |
| 90 if (m_performanceEntries.isEmpty()) |
| 91 return; |
| 92 |
| 93 PerformanceEntryVector performanceEntries; |
| 94 performanceEntries.swap(m_performanceEntries); |
| 95 Member<PerformanceObserverEntryList> entryList(new PerformanceObserverEntryL
ist(performanceEntries)); |
| 96 |
| 97 m_callback->handleEvent(entryList, this); |
| 98 } |
| 99 |
| 100 DEFINE_TRACE(PerformanceObserver) |
| 101 { |
| 102 visitor->trace(m_callback); |
| 103 visitor->trace(m_performance); |
| 104 visitor->trace(m_performanceEntries); |
| 105 } |
| 106 |
| 107 } // namespace blink |
OLD | NEW |