Unreviewed, rolling out r212944.
[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 "LoadTiming.h"
40 #include "ResourceResponse.h"
41 #include "ResourceTiming.h"
42 #include "URL.h"
43
44 namespace WebCore {
45
46 static double monotonicTimeToDOMHighResTimeStamp(MonotonicTime timeOrigin, MonotonicTime timeStamp)
47 {
48     ASSERT(timeStamp.secondsSinceEpoch().seconds() >= 0);
49     if (!timeStamp || !timeOrigin)
50         return 0;
51
52     Seconds seconds = timeStamp - timeOrigin;
53     return Performance::reduceTimeResolution(seconds).milliseconds();
54 }
55
56 Ref<PerformanceResourceTiming> PerformanceResourceTiming::create(MonotonicTime timeOrigin, ResourceTiming&& resourceTiming)
57 {
58     return adoptRef(*new PerformanceResourceTiming(timeOrigin, WTFMove(resourceTiming)));
59 }
60
61 PerformanceResourceTiming::PerformanceResourceTiming(MonotonicTime timeOrigin, ResourceTiming&& resourceTiming)
62     : PerformanceEntry(PerformanceEntry::Type::Resource, resourceTiming.url().string(), ASCIILiteral("resource"), monotonicTimeToDOMHighResTimeStamp(timeOrigin, resourceTiming.loadTiming().startTime()), monotonicTimeToDOMHighResTimeStamp(timeOrigin, resourceTiming.loadTiming().responseEnd()))
63     , m_initiatorType(resourceTiming.initiator())
64     , m_timeOrigin(timeOrigin)
65     , m_loadTiming(resourceTiming.loadTiming())
66     , m_networkLoadTiming(resourceTiming.networkLoadTiming())
67     , m_shouldReportDetails(resourceTiming.allowTimingDetails())
68 {
69 }
70
71 PerformanceResourceTiming::~PerformanceResourceTiming()
72 {
73 }
74
75 double PerformanceResourceTiming::workerStart() const
76 {
77     return 0.0;
78 }
79
80 double PerformanceResourceTiming::redirectStart() const
81 {
82     if (!m_shouldReportDetails)
83         return 0.0;
84
85     return monotonicTimeToDOMHighResTimeStamp(m_timeOrigin, m_loadTiming.redirectStart());
86 }
87
88 double PerformanceResourceTiming::redirectEnd() const
89 {
90     if (!m_shouldReportDetails)
91         return 0.0;
92
93     return monotonicTimeToDOMHighResTimeStamp(m_timeOrigin, m_loadTiming.redirectEnd());
94 }
95
96 double PerformanceResourceTiming::fetchStart() const
97 {
98     // fetchStart is a required property.
99     ASSERT(m_loadTiming.fetchStart());
100
101     return monotonicTimeToDOMHighResTimeStamp(m_timeOrigin, m_loadTiming.fetchStart());
102 }
103
104 double PerformanceResourceTiming::domainLookupStart() const
105 {
106     if (!m_shouldReportDetails)
107         return 0.0;
108
109     if (m_networkLoadTiming.domainLookupStart <= 0)
110         return fetchStart();
111
112     return networkLoadTimeToDOMHighResTimeStamp(m_networkLoadTiming.domainLookupStart);
113 }
114
115 double PerformanceResourceTiming::domainLookupEnd() const
116 {
117     if (!m_shouldReportDetails)
118         return 0.0;
119
120     if (m_networkLoadTiming.domainLookupEnd <= 0)
121         return domainLookupStart();
122
123     return networkLoadTimeToDOMHighResTimeStamp(m_networkLoadTiming.domainLookupEnd);
124 }
125
126 double PerformanceResourceTiming::connectStart() const
127 {
128     if (!m_shouldReportDetails)
129         return 0.0;
130
131     // connectStart will be -1 when a network request is not made.
132     if (m_networkLoadTiming.connectStart <= 0)
133         return domainLookupEnd();
134
135     // connectStart includes any DNS time, so we may need to trim that off.
136     double connectStart = m_networkLoadTiming.connectStart;
137     if (m_networkLoadTiming.domainLookupEnd >= 0)
138         connectStart = m_networkLoadTiming.domainLookupEnd;
139
140     return networkLoadTimeToDOMHighResTimeStamp(connectStart);
141 }
142
143 double PerformanceResourceTiming::connectEnd() const
144 {
145     if (!m_shouldReportDetails)
146         return 0.0;
147
148     // connectStart will be -1 when a network request is not made.
149     if (m_networkLoadTiming.connectEnd <= 0)
150         return connectStart();
151
152     return networkLoadTimeToDOMHighResTimeStamp(m_networkLoadTiming.connectEnd);
153 }
154
155 double PerformanceResourceTiming::secureConnectionStart() const
156 {
157     if (!m_shouldReportDetails)
158         return 0.0;
159
160     if (m_networkLoadTiming.secureConnectionStart < 0) // Secure connection not negotiated.
161         return 0.0;
162
163     return networkLoadTimeToDOMHighResTimeStamp(m_networkLoadTiming.secureConnectionStart);
164 }
165
166 double PerformanceResourceTiming::requestStart() const
167 {
168     if (!m_shouldReportDetails)
169         return 0.0;
170
171     // requestStart is 0 when a network request is not made.
172     if (m_networkLoadTiming.requestStart <= 0)
173         return connectEnd();
174
175     return networkLoadTimeToDOMHighResTimeStamp(m_networkLoadTiming.requestStart);
176 }
177
178 double PerformanceResourceTiming::responseStart() const
179 {
180     if (!m_shouldReportDetails)
181         return 0.0;
182
183     // responseStart is 0 when a network request is not made.
184     if (m_networkLoadTiming.responseStart <= 0)
185         return requestStart();
186
187     return networkLoadTimeToDOMHighResTimeStamp(m_networkLoadTiming.responseStart);
188 }
189
190 double PerformanceResourceTiming::responseEnd() const
191 {
192     // responseEnd is a required property.
193     ASSERT(m_loadTiming.responseEnd());
194
195     return monotonicTimeToDOMHighResTimeStamp(m_timeOrigin, m_loadTiming.responseEnd());
196 }
197
198 double PerformanceResourceTiming::networkLoadTimeToDOMHighResTimeStamp(double deltaMilliseconds) const
199 {
200     ASSERT(deltaMilliseconds);
201     MonotonicTime combined = m_loadTiming.fetchStart() + Seconds::fromMilliseconds(deltaMilliseconds);
202     Seconds delta = combined - m_timeOrigin;
203     return Performance::reduceTimeResolution(delta).milliseconds();
204 }
205
206 } // namespace WebCore
207
208 #endif // ENABLE(WEB_TIMING)