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 46namespace WebCore { 47 48static double monotonicTimeToDocumentMilliseconds(Document* document, double seconds) 49{ 50 ASSERT(seconds >= 0.0); 51 return document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(seconds) * 1000.0; 52} 53 54static 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 79PerformanceResourceTiming::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_didReuseConnection(response.connectionReused()) 85 , m_shouldReportDetails(passesTimingAllowCheck(response, requestingDocument)) 86 , m_requestingDocument(requestingDocument) 87{ 88} 89 90PerformanceResourceTiming::~PerformanceResourceTiming() 91{ 92} 93 94AtomicString PerformanceResourceTiming::initiatorType() const 95{ 96 return m_initiatorType; 97} 98 99double PerformanceResourceTiming::redirectStart() const 100{ 101 // FIXME: Need to track and report redirects for resources. 102 if (!m_shouldReportDetails) 103 return 0.0; 104 return 0; 105} 106 107double PerformanceResourceTiming::redirectEnd() const 108{ 109 if (!m_shouldReportDetails) 110 return 0.0; 111 return 0; 112} 113 114double PerformanceResourceTiming::fetchStart() const 115{ 116 // FIXME: This should be different depending on redirects. 117 return (startTime()); 118} 119 120double PerformanceResourceTiming::domainLookupStart() const 121{ 122 if (!m_shouldReportDetails) 123 return 0.0; 124 125 if (!m_timing || m_timing->dnsStart < 0) 126 return fetchStart(); 127 128 return resourceTimeToDocumentMilliseconds(m_timing->dnsStart); 129} 130 131double PerformanceResourceTiming::domainLookupEnd() const 132{ 133 if (!m_shouldReportDetails) 134 return 0.0; 135 136 if (!m_timing || m_timing->dnsEnd < 0) 137 return domainLookupStart(); 138 139 return resourceTimeToDocumentMilliseconds(m_timing->dnsEnd); 140} 141 142double 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 || m_timing->connectStart < 0 || m_didReuseConnection) 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 159double 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 || m_timing->connectEnd < 0 || m_didReuseConnection) 166 return connectStart(); 167 168 return resourceTimeToDocumentMilliseconds(m_timing->connectEnd); 169} 170 171double PerformanceResourceTiming::secureConnectionStart() const 172{ 173 if (!m_shouldReportDetails) 174 return 0.0; 175 176 if (!m_timing || m_timing->sslStart < 0) // Secure connection not negotiated. 177 return 0.0; 178 179 return resourceTimeToDocumentMilliseconds(m_timing->sslStart); 180} 181 182double PerformanceResourceTiming::requestStart() const 183{ 184 if (!m_shouldReportDetails) 185 return 0.0; 186 187 if (!m_timing) 188 return connectEnd(); 189 190 return resourceTimeToDocumentMilliseconds(m_timing->sendStart); 191} 192 193double PerformanceResourceTiming::responseStart() const 194{ 195 if (!m_shouldReportDetails) 196 return 0.0; 197 198 if (!m_timing) 199 return requestStart(); 200 // FIXME: This number isn't exactly correct. See the notes in PerformanceTiming::responseStart(). 201 return resourceTimeToDocumentMilliseconds(m_timing->receiveHeadersEnd); 202} 203 204double PerformanceResourceTiming::responseEnd() const 205{ 206 if (!m_finishTime) 207 return responseStart(); 208 209 return monotonicTimeToDocumentMilliseconds(m_requestingDocument.get(), m_finishTime); 210} 211 212double PerformanceResourceTiming::resourceTimeToDocumentMilliseconds(int deltaMilliseconds) const 213{ 214 if (!deltaMilliseconds) 215 return 0.0; 216 return monotonicTimeToDocumentMilliseconds(m_requestingDocument.get(), m_timing->requestTime) + deltaMilliseconds; 217} 218 219} // namespace WebCore 220 221#endif // ENABLE(RESOURCE_TIMING) 222