Performance Timeline: Prepare for Worker support
[WebKit-https.git] / Source / WebCore / page / PerformanceResourceTiming.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  * Copyright (C) 2012 Intel Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "PerformanceResourceTiming.h"
34
35 #if ENABLE(WEB_TIMING)
36
37 #include "Document.h"
38 #include "DocumentLoader.h"
39 #include "HTTPHeaderNames.h"
40 #include "LoadTiming.h"
41 #include "URL.h"
42 #include "ResourceResponse.h"
43 #include "SecurityOrigin.h"
44 #include <wtf/Vector.h>
45
46 namespace WebCore {
47
48 static double monotonicTimeToDOMHighResTimeStamp(double timeOrigin, double seconds)
49 {
50     ASSERT(seconds >= 0.0);
51     if (!seconds || !timeOrigin)
52         return 0;
53     return Performance::reduceTimeResolution(seconds - timeOrigin) * 1000.0;
54 }
55
56 static bool passesTimingAllowCheck(const ResourceResponse& response, const SecurityOrigin& initiatorSecurityOrigin)
57 {
58     Ref<SecurityOrigin> resourceOrigin = SecurityOrigin::create(response.url());
59     if (resourceOrigin->isSameSchemeHostPort(initiatorSecurityOrigin))
60         return true;
61
62     const String& timingAllowOriginString = response.httpHeaderField(HTTPHeaderName::TimingAllowOrigin);
63     if (timingAllowOriginString.isEmpty() || equalLettersIgnoringASCIICase(timingAllowOriginString, "null"))
64         return false;
65
66     if (timingAllowOriginString == "*")
67         return true;
68
69     const String& securityOrigin = initiatorSecurityOrigin.toString();
70     Vector<String> timingAllowOrigins;
71     timingAllowOriginString.split(' ', timingAllowOrigins);
72     for (auto& origin : timingAllowOrigins) {
73         if (origin == securityOrigin)
74             return true;
75     }
76
77     return false;
78 }
79
80 PerformanceResourceTiming::PerformanceResourceTiming(const AtomicString& initiatorType, const URL& originalURL, const ResourceResponse& response, const SecurityOrigin& initiatorSecurityOrigin, double timeOrigin, LoadTiming loadTiming)
81     : PerformanceEntry(PerformanceEntry::Type::Resource, originalURL.string(), ASCIILiteral("resource"), monotonicTimeToDOMHighResTimeStamp(timeOrigin, loadTiming.startTime()), monotonicTimeToDOMHighResTimeStamp(timeOrigin, loadTiming.responseEnd()))
82     , m_initiatorType(initiatorType)
83     , m_timing(response.networkLoadTiming())
84     , m_loadTiming(loadTiming)
85     , m_shouldReportDetails(passesTimingAllowCheck(response, initiatorSecurityOrigin))
86     , m_timeOrigin(timeOrigin)
87 {
88 }
89
90 PerformanceResourceTiming::~PerformanceResourceTiming()
91 {
92 }
93
94 double PerformanceResourceTiming::workerStart() const
95 {
96     return 0.0;
97 }
98
99 double PerformanceResourceTiming::redirectStart() const
100 {
101     if (!m_shouldReportDetails)
102         return 0.0;
103
104     return monotonicTimeToDOMHighResTimeStamp(m_timeOrigin, m_loadTiming.redirectStart());
105 }
106
107 double PerformanceResourceTiming::redirectEnd() const
108 {
109     if (!m_shouldReportDetails)
110         return 0.0;
111
112     return monotonicTimeToDOMHighResTimeStamp(m_timeOrigin, m_loadTiming.redirectEnd());
113 }
114
115 double PerformanceResourceTiming::fetchStart() const
116 {
117     return monotonicTimeToDOMHighResTimeStamp(m_timeOrigin, m_loadTiming.fetchStart());
118 }
119
120 double PerformanceResourceTiming::domainLookupStart() const
121 {
122     if (!m_shouldReportDetails)
123         return 0.0;
124
125     if (m_timing.domainLookupStart <= 0)
126         return fetchStart();
127
128     return networkLoadTimeToDOMHighResTimeStamp(m_timing.domainLookupStart);
129 }
130
131 double PerformanceResourceTiming::domainLookupEnd() const
132 {
133     if (!m_shouldReportDetails)
134         return 0.0;
135
136     if (m_timing.domainLookupEnd <= 0)
137         return domainLookupStart();
138
139     return networkLoadTimeToDOMHighResTimeStamp(m_timing.domainLookupEnd);
140 }
141
142 double PerformanceResourceTiming::connectStart() const
143 {
144     if (!m_shouldReportDetails)
145         return 0.0;
146
147     // connectStart will be -1 when a network request is not made.
148     if (m_timing.connectStart <= 0)
149         return domainLookupEnd();
150
151     // connectStart includes any DNS time, so we may need to trim that off.
152     double connectStart = m_timing.connectStart;
153     if (m_timing.domainLookupEnd >= 0)
154         connectStart = m_timing.domainLookupEnd;
155
156     return networkLoadTimeToDOMHighResTimeStamp(connectStart);
157 }
158
159 double PerformanceResourceTiming::connectEnd() const
160 {
161     if (!m_shouldReportDetails)
162         return 0.0;
163
164     // connectStart will be -1 when a network request is not made.
165     if (m_timing.connectEnd <= 0)
166         return connectStart();
167
168     return networkLoadTimeToDOMHighResTimeStamp(m_timing.connectEnd);
169 }
170
171 double PerformanceResourceTiming::secureConnectionStart() const
172 {
173     if (!m_shouldReportDetails)
174         return 0.0;
175
176     if (m_timing.secureConnectionStart < 0) // Secure connection not negotiated.
177         return 0.0;
178
179     return networkLoadTimeToDOMHighResTimeStamp(m_timing.secureConnectionStart);
180 }
181
182 double PerformanceResourceTiming::requestStart() const
183 {
184     if (!m_shouldReportDetails)
185         return 0.0;
186
187     return networkLoadTimeToDOMHighResTimeStamp(m_timing.requestStart);
188 }
189
190 double PerformanceResourceTiming::responseStart() const
191 {
192     if (!m_shouldReportDetails)
193         return 0.0;
194
195     return networkLoadTimeToDOMHighResTimeStamp(m_timing.responseStart);
196 }
197
198 double PerformanceResourceTiming::responseEnd() const
199 {
200     if (!m_shouldReportDetails)
201         return 0.0;
202
203     return monotonicTimeToDOMHighResTimeStamp(m_timeOrigin, m_loadTiming.responseEnd());
204 }
205
206 double PerformanceResourceTiming::networkLoadTimeToDOMHighResTimeStamp(double deltaMilliseconds) const
207 {
208     if (!deltaMilliseconds)
209         return 0.0;
210
211     double seconds = m_loadTiming.fetchStart() + (deltaMilliseconds / 1000.0);
212     return Performance::reduceTimeResolution(seconds - m_timeOrigin) * 1000.0;
213 }
214
215 } // namespace WebCore
216
217 #endif // ENABLE(WEB_TIMING)