Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / timing / PerformanceObserver.cpp
blobba81f131d808df3c2711a9028f04c5cd49a847d8
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.
5 #include "config.h"
6 #include "core/timing/PerformanceObserver.h"
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>
20 namespace blink {
22 PerformanceObserver* PerformanceObserver::create(PerformanceBase* performance, PerformanceObserverCallback* callback)
24 ASSERT(isMainThread());
25 return new PerformanceObserver(performance, callback);
28 PerformanceObserver::PerformanceObserver(PerformanceBase* performance, PerformanceObserverCallback* callback)
29 : m_callback(callback)
30 , m_performance(performance)
31 , m_filterOptions(PerformanceEntry::Invalid)
32 , m_isRegistered(false)
36 PerformanceObserver::~PerformanceObserver()
40 void PerformanceObserver::observe(const PerformanceObserverInit& observerInit, ExceptionState& exceptionState)
42 if (!m_performance) {
43 exceptionState.throwTypeError("Window may be destroyed? Performance target is invalid.");
44 return;
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);
53 if (entryTypes == PerformanceEntry::Invalid) {
54 exceptionState.throwTypeError("A Performance Observer MUST have a non-empty entryTypes attribute.");
55 return;
57 m_filterOptions = entryTypes;
58 if (m_isRegistered)
59 m_performance->updatePerformanceObserverFilterOptions();
60 else
61 m_performance->registerPerformanceObserver(*this);
62 m_isRegistered = true;
65 void PerformanceObserver::disconnect()
67 m_performanceEntries.clear();
68 if (m_performance)
69 m_performance->unregisterPerformanceObserver(*this);
70 m_isRegistered = false;
73 void PerformanceObserver::enqueuePerformanceEntry(PerformanceEntry& entry)
75 ASSERT(isMainThread());
76 m_performanceEntries.append(&entry);
77 if (m_performance)
78 m_performance->activateObserver(*this);
81 bool PerformanceObserver::shouldBeSuspended() const
83 return m_callback->executionContext() && m_callback->executionContext()->activeDOMObjectsAreSuspended();
86 void PerformanceObserver::deliver()
88 ASSERT(!shouldBeSuspended());
90 if (m_performanceEntries.isEmpty())
91 return;
93 PerformanceEntryVector performanceEntries;
94 performanceEntries.swap(m_performanceEntries);
95 Member<PerformanceObserverEntryList> entryList(new PerformanceObserverEntryList(performanceEntries));
97 m_callback->handleEvent(entryList, this);
100 DEFINE_TRACE(PerformanceObserver)
102 visitor->trace(m_callback);
103 visitor->trace(m_performance);
104 visitor->trace(m_performanceEntries);
107 } // namespace blink