2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "PerformanceTiming.h"
34 #if ENABLE(WEB_TIMING)
37 #include "DocumentLoadTiming.h"
38 #include "DocumentLoader.h"
39 #include "DocumentTiming.h"
41 #include "ResourceLoadTiming.h"
42 #include "ResourceResponse.h"
43 #include <wtf/CurrentTime.h>
47 static unsigned long long toIntegerMilliseconds(double seconds)
50 return static_cast<unsigned long long>(seconds * 1000.0);
53 PerformanceTiming::PerformanceTiming(Frame* frame)
54 : DOMWindowProperty(frame)
58 unsigned long long PerformanceTiming::navigationStart() const
60 DocumentLoadTiming* timing = documentLoadTiming();
64 return toIntegerMilliseconds(timing->navigationStart());
67 unsigned long long PerformanceTiming::unloadEventStart() const
69 DocumentLoadTiming* timing = documentLoadTiming();
73 if (timing->hasCrossOriginRedirect() || !timing->hasSameOriginAsPreviousDocument())
76 return toIntegerMilliseconds(timing->unloadEventStart());
79 unsigned long long PerformanceTiming::unloadEventEnd() const
81 DocumentLoadTiming* timing = documentLoadTiming();
85 if (timing->hasCrossOriginRedirect() || !timing->hasSameOriginAsPreviousDocument())
88 return toIntegerMilliseconds(timing->unloadEventEnd());
91 unsigned long long PerformanceTiming::redirectStart() const
93 DocumentLoadTiming* timing = documentLoadTiming();
97 if (timing->hasCrossOriginRedirect())
100 return toIntegerMilliseconds(timing->redirectStart());
103 unsigned long long PerformanceTiming::redirectEnd() const
105 DocumentLoadTiming* timing = documentLoadTiming();
109 if (timing->hasCrossOriginRedirect())
112 return toIntegerMilliseconds(timing->redirectEnd());
115 unsigned long long PerformanceTiming::fetchStart() const
117 DocumentLoadTiming* timing = documentLoadTiming();
121 return toIntegerMilliseconds(timing->fetchStart());
124 unsigned long long PerformanceTiming::domainLookupStart() const
126 ResourceLoadTiming* timing = resourceLoadTiming();
130 // This will be -1 when a DNS request is not performed.
131 // Rather than exposing a special value that indicates no DNS, we "backfill" with fetchStart.
132 int dnsStart = timing->dnsStart;
136 return resourceLoadTimeRelativeToAbsolute(dnsStart);
139 unsigned long long PerformanceTiming::domainLookupEnd() const
141 ResourceLoadTiming* timing = resourceLoadTiming();
143 return domainLookupStart();
145 // This will be -1 when a DNS request is not performed.
146 // Rather than exposing a special value that indicates no DNS, we "backfill" with domainLookupStart.
147 int dnsEnd = timing->dnsEnd;
149 return domainLookupStart();
151 return resourceLoadTimeRelativeToAbsolute(dnsEnd);
154 unsigned long long PerformanceTiming::connectStart() const
156 DocumentLoader* loader = documentLoader();
158 return domainLookupEnd();
160 ResourceLoadTiming* timing = loader->response().resourceLoadTiming();
162 return domainLookupEnd();
164 // connectStart will be -1 when a network request is not made.
165 // Rather than exposing a special value that indicates no new connection, we "backfill" with domainLookupEnd.
166 int connectStart = timing->connectStart;
167 if (connectStart < 0 || loader->response().connectionReused())
168 return domainLookupEnd();
170 // ResourceLoadTiming's connect phase includes DNS, however Navigation Timing's
171 // connect phase should not. So if there is DNS time, trim it from the start.
172 if (timing->dnsEnd >= 0 && timing->dnsEnd > connectStart)
173 connectStart = timing->dnsEnd;
175 return resourceLoadTimeRelativeToAbsolute(connectStart);
178 unsigned long long PerformanceTiming::connectEnd() const
180 DocumentLoader* loader = documentLoader();
182 return connectStart();
184 ResourceLoadTiming* timing = loader->response().resourceLoadTiming();
186 return connectStart();
188 // connectEnd will be -1 when a network request is not made.
189 // Rather than exposing a special value that indicates no new connection, we "backfill" with connectStart.
190 int connectEnd = timing->connectEnd;
191 if (connectEnd < 0 || loader->response().connectionReused())
192 return connectStart();
194 return resourceLoadTimeRelativeToAbsolute(connectEnd);
197 unsigned long long PerformanceTiming::secureConnectionStart() const
199 DocumentLoader* loader = documentLoader();
203 ResourceLoadTiming* timing = loader->response().resourceLoadTiming();
207 int sslStart = timing->sslStart;
211 return resourceLoadTimeRelativeToAbsolute(sslStart);
214 unsigned long long PerformanceTiming::requestStart() const
216 ResourceLoadTiming* timing = resourceLoadTiming();
220 ASSERT(timing->sendStart >= 0);
221 return resourceLoadTimeRelativeToAbsolute(timing->sendStart);
224 unsigned long long PerformanceTiming::responseStart() const
226 ResourceLoadTiming* timing = resourceLoadTiming();
228 return requestStart();
230 // FIXME: Response start needs to be the time of the first received byte.
231 // However, the ResourceLoadTiming API currently only supports the time
232 // the last header byte was received. For many responses with reasonable
233 // sized cookies, the HTTP headers fit into a single packet so this time
234 // is basically equivalent. But for some responses, particularly those with
235 // headers larger than a single packet, this time will be too late.
236 ASSERT(timing->receiveHeadersEnd >= 0);
237 return resourceLoadTimeRelativeToAbsolute(timing->receiveHeadersEnd);
240 unsigned long long PerformanceTiming::responseEnd() const
242 DocumentLoadTiming* timing = documentLoadTiming();
246 return toIntegerMilliseconds(timing->responseEnd());
249 unsigned long long PerformanceTiming::domLoading() const
251 const DocumentTiming* timing = documentTiming();
255 return monotonicTimeToIntegerMilliseconds(timing->domLoading);
258 unsigned long long PerformanceTiming::domInteractive() const
260 const DocumentTiming* timing = documentTiming();
264 return monotonicTimeToIntegerMilliseconds(timing->domInteractive);
267 unsigned long long PerformanceTiming::domContentLoadedEventStart() const
269 const DocumentTiming* timing = documentTiming();
273 return monotonicTimeToIntegerMilliseconds(timing->domContentLoadedEventStart);
276 unsigned long long PerformanceTiming::domContentLoadedEventEnd() const
278 const DocumentTiming* timing = documentTiming();
282 return monotonicTimeToIntegerMilliseconds(timing->domContentLoadedEventEnd);
285 unsigned long long PerformanceTiming::domComplete() const
287 const DocumentTiming* timing = documentTiming();
291 return monotonicTimeToIntegerMilliseconds(timing->domComplete);
294 unsigned long long PerformanceTiming::loadEventStart() const
296 DocumentLoadTiming* timing = documentLoadTiming();
300 return toIntegerMilliseconds(timing->loadEventStart());
303 unsigned long long PerformanceTiming::loadEventEnd() const
305 DocumentLoadTiming* timing = documentLoadTiming();
309 return toIntegerMilliseconds(timing->loadEventEnd());
312 DocumentLoader* PerformanceTiming::documentLoader() const
317 return m_frame->loader()->documentLoader();
320 const DocumentTiming* PerformanceTiming::documentTiming() const
325 Document* document = m_frame->document();
329 return document->timing();
332 DocumentLoadTiming* PerformanceTiming::documentLoadTiming() const
334 DocumentLoader* loader = documentLoader();
338 return loader->timing();
341 ResourceLoadTiming* PerformanceTiming::resourceLoadTiming() const
343 DocumentLoader* loader = documentLoader();
347 return loader->response().resourceLoadTiming();
350 unsigned long long PerformanceTiming::resourceLoadTimeRelativeToAbsolute(int relativeMilliseconds) const
352 ASSERT(relativeMilliseconds >= 0);
353 ResourceLoadTiming* resourceTiming = resourceLoadTiming();
354 ASSERT(resourceTiming);
355 return toIntegerMilliseconds(resourceTiming->convertResourceLoadTimeToDocumentTime(documentLoadTiming(), relativeMilliseconds));
358 unsigned long long PerformanceTiming::monotonicTimeToIntegerMilliseconds(double monotonicSeconds) const
360 ASSERT(monotonicSeconds >= 0);
361 const DocumentLoadTiming* timing = documentLoadTiming();
363 return toIntegerMilliseconds(timing->convertMonotonicTimeToDocumentTime(monotonicSeconds));
366 } // namespace WebCore
368 #endif // ENABLE(WEB_TIMING)