2 * Copyright (C) 2012 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 "PerformanceResourceTiming.h"
34 #if ENABLE(RESOURCE_TIMING)
37 #include "DocumentLoadTiming.h"
38 #include "DocumentLoader.h"
40 #include "ResourceRequest.h"
41 #include "ResourceResponse.h"
42 #include "SecurityOrigin.h"
43 #include <wtf/Vector.h>
47 PerformanceResourceTiming::PerformanceResourceTiming(const ResourceRequest& request, const ResourceResponse& response, double finishTime, Document* requestingDocument)
48 : PerformanceEntry(request.url().string(), "resource", response.resourceLoadTiming()->requestTime, finishTime)
49 , m_timing(response.resourceLoadTiming())
50 , m_finishTime(finishTime)
51 , m_requestingDocument(requestingDocument)
55 PerformanceResourceTiming::~PerformanceResourceTiming()
59 String PerformanceResourceTiming::initiatorType() const
61 // FIXME: This should be decided by the resource type.
65 // FIXME: Need to enforce same-origin policy on these.
67 double PerformanceResourceTiming::redirectStart() const
69 // FIXME: Need to track and report redirects for resources.
73 double PerformanceResourceTiming::redirectEnd() const
78 double PerformanceResourceTiming::fetchStart() const
80 return monotonicTimeToDocumentMilliseconds(m_timing->requestTime);
83 double PerformanceResourceTiming::domainLookupStart() const
85 if (m_timing->dnsStart < 0)
88 return resourceTimeToDocumentMilliseconds(m_timing->dnsStart);
91 double PerformanceResourceTiming::domainLookupEnd() const
93 if (m_timing->dnsEnd < 0)
94 return domainLookupStart();
96 return resourceTimeToDocumentMilliseconds(m_timing->dnsEnd);
99 double PerformanceResourceTiming::connectStart() const
101 if (m_timing->connectStart < 0) // Connection was reused.
102 return domainLookupEnd();
104 // connectStart includes any DNS time, so we may need to trim that off.
105 int connectStart = m_timing->connectStart;
106 if (m_timing->dnsEnd >= 0)
107 connectStart = m_timing->dnsEnd;
109 return resourceTimeToDocumentMilliseconds(connectStart);
112 double PerformanceResourceTiming::connectEnd() const
114 if (m_timing->connectEnd < 0) // Connection was reused.
115 return connectStart();
117 return resourceTimeToDocumentMilliseconds(m_timing->connectEnd);
120 double PerformanceResourceTiming::secureConnectionStart() const
122 if (m_timing->sslStart < 0) // Secure connection not negotiated.
125 return resourceTimeToDocumentMilliseconds(m_timing->sslStart);
128 double PerformanceResourceTiming::requestStart() const
130 return resourceTimeToDocumentMilliseconds(m_timing->sendStart);
133 double PerformanceResourceTiming::responseStart() const
135 // FIXME: This number isn't exactly correct. See the notes in PerformanceTiming::responseStart().
136 return resourceTimeToDocumentMilliseconds(m_timing->receiveHeadersEnd);
139 double PerformanceResourceTiming::responseEnd() const
141 return monotonicTimeToDocumentMilliseconds(m_finishTime);
144 double PerformanceResourceTiming::monotonicTimeToDocumentMilliseconds(double seconds) const
146 ASSERT(seconds >= 0.0);
147 return m_requestingDocument->loader()->timing()->convertMonotonicTimeToDocumentTime(seconds) * 1000.0;
150 double PerformanceResourceTiming::resourceTimeToDocumentMilliseconds(int deltaMilliseconds) const
152 return monotonicTimeToDocumentMilliseconds(m_timing->requestTime) + deltaMilliseconds;
155 } // namespace WebCore
157 #endif // ENABLE(RESOURCE_TIMING)