1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2012 Intel Inc. All rights reserved.
4 * Copyright (C) 2016 Apple Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#pragma once
34
35#include "ContextDestructionObserver.h"
36#include "DOMHighResTimeStamp.h"
37#include "EventTarget.h"
38#include "ExceptionOr.h"
39#include "GenericTaskQueue.h"
40#include <wtf/ListHashSet.h>
41
42namespace WebCore {
43
44class LoadTiming;
45class PerformanceEntry;
46class PerformanceNavigation;
47class PerformanceObserver;
48class PerformanceTiming;
49class ResourceResponse;
50class ResourceTiming;
51class ScriptExecutionContext;
52class UserTiming;
53
54class Performance final : public RefCounted<Performance>, public ContextDestructionObserver, public EventTargetWithInlineData {
55 WTF_MAKE_ISO_ALLOCATED(Performance);
56public:
57 static Ref<Performance> create(ScriptExecutionContext* context, MonotonicTime timeOrigin) { return adoptRef(*new Performance(context, timeOrigin)); }
58 ~Performance();
59
60 DOMHighResTimeStamp now() const;
61
62 PerformanceNavigation* navigation();
63 PerformanceTiming* timing();
64
65 Vector<RefPtr<PerformanceEntry>> getEntries() const;
66 Vector<RefPtr<PerformanceEntry>> getEntriesByType(const String& entryType) const;
67 Vector<RefPtr<PerformanceEntry>> getEntriesByName(const String& name, const String& entryType) const;
68
69 void clearResourceTimings();
70 void setResourceTimingBufferSize(unsigned);
71
72 ExceptionOr<void> mark(const String& markName);
73 void clearMarks(const String& markName);
74
75 ExceptionOr<void> measure(const String& measureName, const String& startMark, const String& endMark);
76 void clearMeasures(const String& measureName);
77
78 void addResourceTiming(ResourceTiming&&);
79
80 void removeAllObservers();
81 void registerPerformanceObserver(PerformanceObserver&);
82 void unregisterPerformanceObserver(PerformanceObserver&);
83
84 static Seconds reduceTimeResolution(Seconds);
85
86 DOMHighResTimeStamp relativeTimeFromTimeOriginInReducedResolution(MonotonicTime) const;
87
88 ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
89
90 using RefCounted::ref;
91 using RefCounted::deref;
92
93private:
94 Performance(ScriptExecutionContext*, MonotonicTime timeOrigin);
95
96 void contextDestroyed() override;
97
98 EventTargetInterface eventTargetInterface() const final { return PerformanceEventTargetInterfaceType; }
99
100 void refEventTarget() final { ref(); }
101 void derefEventTarget() final { deref(); }
102
103 bool isResourceTimingBufferFull() const;
104 void resourceTimingBufferFullTimerFired();
105
106 void queueEntry(PerformanceEntry&);
107
108 mutable RefPtr<PerformanceNavigation> m_navigation;
109 mutable RefPtr<PerformanceTiming> m_timing;
110
111 // https://w3c.github.io/resource-timing/#extensions-performance-interface recommends size of 150.
112 Vector<RefPtr<PerformanceEntry>> m_resourceTimingBuffer;
113 unsigned m_resourceTimingBufferSize { 150 };
114
115 Timer m_resourceTimingBufferFullTimer;
116 Vector<RefPtr<PerformanceEntry>> m_backupResourceTimingBuffer;
117
118 // https://w3c.github.io/resource-timing/#dfn-resource-timing-buffer-full-flag
119 bool m_resourceTimingBufferFullFlag { false };
120 bool m_waitingForBackupBufferToBeProcessed { false };
121
122 MonotonicTime m_timeOrigin;
123
124 std::unique_ptr<UserTiming> m_userTiming;
125
126 GenericTaskQueue<ScriptExecutionContext> m_performanceTimelineTaskQueue;
127 ListHashSet<RefPtr<PerformanceObserver>> m_observers;
128};
129
130}
131