Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebCore / page / Performance.h
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
42 namespace WebCore {
43
44 class LoadTiming;
45 class PerformanceEntry;
46 class PerformanceNavigation;
47 class PerformanceObserver;
48 class PerformanceTiming;
49 class ResourceResponse;
50 class ResourceTiming;
51 class ScriptExecutionContext;
52 class UserTiming;
53
54 class Performance final : public RefCounted<Performance>, public ContextDestructionObserver, public EventTargetWithInlineData {
55 public:
56     static Ref<Performance> create(ScriptExecutionContext* context, MonotonicTime timeOrigin) { return adoptRef(*new Performance(context, timeOrigin)); }
57     ~Performance();
58
59     DOMHighResTimeStamp now() const;
60
61     PerformanceNavigation* navigation();
62     PerformanceTiming* timing();
63
64     Vector<RefPtr<PerformanceEntry>> getEntries() const;
65     Vector<RefPtr<PerformanceEntry>> getEntriesByType(const String& entryType) const;
66     Vector<RefPtr<PerformanceEntry>> getEntriesByName(const String& name, const String& entryType) const;
67
68     void clearResourceTimings();
69     void setResourceTimingBufferSize(unsigned);
70
71     ExceptionOr<void> mark(const String& markName);
72     void clearMarks(const String& markName);
73
74     ExceptionOr<void> measure(const String& measureName, const String& startMark, const String& endMark);
75     void clearMeasures(const String& measureName);
76
77     void addResourceTiming(ResourceTiming&&);
78
79     void removeAllObservers();
80     void registerPerformanceObserver(PerformanceObserver&);
81     void unregisterPerformanceObserver(PerformanceObserver&);
82
83     static Seconds reduceTimeResolution(Seconds);
84
85     DOMHighResTimeStamp relativeTimeFromTimeOriginInReducedResolution(MonotonicTime) const;
86
87     ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
88
89     using RefCounted::ref;
90     using RefCounted::deref;
91
92 private:
93     Performance(ScriptExecutionContext*, MonotonicTime timeOrigin);
94
95     void contextDestroyed() override;
96
97     EventTargetInterface eventTargetInterface() const final { return PerformanceEventTargetInterfaceType; }
98
99     void refEventTarget() final { ref(); }
100     void derefEventTarget() final { deref(); }
101
102     bool isResourceTimingBufferFull() const;
103     void resourceTimingBufferFullTimerFired();
104
105     void queueEntry(PerformanceEntry&);
106
107     mutable RefPtr<PerformanceNavigation> m_navigation;
108     mutable RefPtr<PerformanceTiming> m_timing;
109
110     // https://w3c.github.io/resource-timing/#extensions-performance-interface recommends size of 150.
111     Vector<RefPtr<PerformanceEntry>> m_resourceTimingBuffer;
112     unsigned m_resourceTimingBufferSize { 150 };
113
114     Timer m_resourceTimingBufferFullTimer;
115     Vector<RefPtr<PerformanceEntry>> m_backupResourceTimingBuffer;
116
117     // https://w3c.github.io/resource-timing/#dfn-resource-timing-buffer-full-flag
118     bool m_resourceTimingBufferFullFlag { false };
119     bool m_waitingForBackupBufferToBeProcessed { false };
120
121     MonotonicTime m_timeOrigin;
122
123     std::unique_ptr<UserTiming> m_userTiming;
124
125     GenericTaskQueue<ScriptExecutionContext> m_performanceTimelineTaskQueue;
126     ListHashSet<RefPtr<PerformanceObserver>> m_observers;
127 };
128
129 }