bd77252f5a2d2111d63a8cf2571ad6d16313f900
[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(RESOURCE_TIMING)
36
37 #include "Document.h"
38 #include "DocumentLoadTiming.h"
39 #include "DocumentLoader.h"
40 #include "KURL.h"
41 #include "ResourceRequest.h"
42 #include "ResourceResponse.h"
43 #include "SecurityOrigin.h"
44 #include <wtf/Vector.h>
45
46 namespace WebCore {
47
48 static double monotonicTimeToDocumentMilliseconds(Document* document, double seconds)
49 {
50     ASSERT(seconds >= 0.0);
51     return document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(seconds) * 1000.0;
52 }
53
54 static bool passesTimingAllowCheck(const ResourceResponse& response, Document* requestingDocument)
55 {
56     AtomicallyInitializedStatic(AtomicString&, timingAllowOrigin = *new AtomicString("timing-allow-origin"));
57
58     RefPtr<SecurityOrigin> resourceOrigin = SecurityOrigin::create(response.url());
59     if (resourceOrigin->isSameSchemeHostPort(requestingDocument->securityOrigin()))
60         return true;
61
62     const String& timingAllowOriginString = response.httpHeaderField(timingAllowOrigin);
63     if (timingAllowOriginString.isEmpty() || equalIgnoringCase(timingAllowOriginString, "null"))
64         return false;
65
66     if (timingAllowOriginString == "*")
67         return true;
68
69     const String& securityOrigin = requestingDocument->securityOrigin()->toString();
70     Vector<String> timingAllowOrigins;
71     timingAllowOriginString.split(" ", timingAllowOrigins);
72     for (size_t i = 0; i < timingAllowOrigins.size(); ++i)
73         if (timingAllowOrigins[i] == securityOrigin)
74             return true;
75
76     return false;
77 }
78
79 PerformanceResourceTiming::PerformanceResourceTiming(const AtomicString& initiatorType, const ResourceRequest& request, const ResourceResponse& response, double initiationTime, double finishTime, Document* requestingDocument)
80     : PerformanceEntry(request.url().string(), "resource", monotonicTimeToDocumentMilliseconds(requestingDocument, initiationTime), monotonicTimeToDocumentMilliseconds(requestingDocument, finishTime))
81     , m_initiatorType(initiatorType)
82     , m_timing(response.resourceLoadTiming())
83     , m_finishTime(finishTime)
84     , m_shouldReportDetails(passesTimingAllowCheck(response, requestingDocument))
85     , m_requestingDocument(requestingDocument)
86 {
87 }
88
89 PerformanceResourceTiming::~PerformanceResourceTiming()
90 {
91 }
92
93 AtomicString PerformanceResourceTiming::initiatorType() const
94 {
95     return m_initiatorType;
96 }
97
98 // FIXME: Need to enforce same-origin policy on these.
99
100 double PerformanceResourceTiming::redirectStart() const
101 {
102     // FIXME: Need to track and report redirects for resources.
103     if (!m_shouldReportDetails)
104         return 0.0;
105     return 0;
106 }
107
108 double PerformanceResourceTiming::redirectEnd() const
109 {
110     if (!m_shouldReportDetails)
111         return 0.0;
112     return 0;
113 }
114
115 double PerformanceResourceTiming::fetchStart() const
116 {
117     // FIXME: This should be different depending on redirects.
118     return (startTime());
119 }
120
121 double PerformanceResourceTiming::domainLookupStart() const
122 {
123     if (!m_shouldReportDetails)
124         return 0.0;
125
126     if (!m_timing || m_timing->dnsStart < 0)
127         return fetchStart();
128
129     return resourceTimeToDocumentMilliseconds(m_timing->dnsStart);
130 }
131
132 double PerformanceResourceTiming::domainLookupEnd() const
133 {
134     if (!m_shouldReportDetails)
135         return 0.0;
136
137     if (!m_timing || m_timing->dnsEnd < 0)
138         return domainLookupStart();
139
140     return resourceTimeToDocumentMilliseconds(m_timing->dnsEnd);
141 }
142
143 double PerformanceResourceTiming::connectStart() const
144 {
145     if (!m_shouldReportDetails)
146         return 0.0;
147
148     if (!m_timing || m_timing->connectStart < 0) // Connection was reused.
149         return domainLookupEnd();
150
151     // connectStart includes any DNS time, so we may need to trim that off.
152     int connectStart = m_timing->connectStart;
153     if (m_timing->dnsEnd >= 0)
154         connectStart = m_timing->dnsEnd;
155
156     return resourceTimeToDocumentMilliseconds(connectStart);
157 }
158
159 double PerformanceResourceTiming::connectEnd() const
160 {
161     if (!m_shouldReportDetails)
162         return 0.0;
163
164     if (!m_timing || m_timing->connectEnd < 0) // Connection was reused.
165         return connectStart();
166
167     return resourceTimeToDocumentMilliseconds(m_timing->connectEnd);
168 }
169
170 double PerformanceResourceTiming::secureConnectionStart() const
171 {
172     if (!m_shouldReportDetails)
173         return 0.0;
174
175     if (!m_timing || m_timing->sslStart < 0) // Secure connection not negotiated.
176         return 0.0;
177
178     return resourceTimeToDocumentMilliseconds(m_timing->sslStart);
179 }
180
181 double PerformanceResourceTiming::requestStart() const
182 {
183     if (!m_shouldReportDetails)
184         return 0.0;
185
186     if (!m_timing)
187         return connectEnd();
188
189     return resourceTimeToDocumentMilliseconds(m_timing->sendStart);
190 }
191
192 double PerformanceResourceTiming::responseStart() const
193 {
194     if (!m_shouldReportDetails)
195         return 0.0;
196
197     if (!m_timing)
198         return requestStart();
199     // FIXME: This number isn't exactly correct. See the notes in PerformanceTiming::responseStart().
200     return resourceTimeToDocumentMilliseconds(m_timing->receiveHeadersEnd);
201 }
202
203 double PerformanceResourceTiming::responseEnd() const
204 {
205     if (!m_shouldReportDetails)
206         return 0.0;
207
208     if (!m_timing)
209         return responseStart();
210
211     return monotonicTimeToDocumentMilliseconds(m_requestingDocument.get(), m_finishTime);
212 }
213
214 double PerformanceResourceTiming::resourceTimeToDocumentMilliseconds(int deltaMilliseconds) const
215 {
216     if (!deltaMilliseconds)
217         return 0.0;
218     return monotonicTimeToDocumentMilliseconds(m_requestingDocument.get(), m_timing->requestTime) + deltaMilliseconds;
219 }
220
221 } // namespace WebCore
222
223 #endif // ENABLE(RESOURCE_TIMING)