Merge "[CherryPick] Refactoring: Move the content of HTMLInputElement::subtreeHasChan...
[framework/web/webkit-efl.git] / Source / WebCore / page / PerformanceResourceTiming.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "PerformanceResourceTiming.h"
33
34 #if ENABLE(RESOURCE_TIMING)
35
36 #include "Document.h"
37 #include "DocumentLoadTiming.h"
38 #include "DocumentLoader.h"
39 #include "KURL.h"
40 #include "ResourceRequest.h"
41 #include "ResourceResponse.h"
42 #include "SecurityOrigin.h"
43 #include <wtf/Vector.h>
44
45 namespace WebCore {
46
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)
52 {
53 }
54
55 PerformanceResourceTiming::~PerformanceResourceTiming()
56 {
57 }
58
59 String PerformanceResourceTiming::initiatorType() const
60 {
61     // FIXME: This should be decided by the resource type.
62     return "other";
63 }
64
65 // FIXME: Need to enforce same-origin policy on these.
66
67 double PerformanceResourceTiming::redirectStart() const
68 {
69     // FIXME: Need to track and report redirects for resources.
70     return 0;
71 }
72
73 double PerformanceResourceTiming::redirectEnd() const
74 {
75     return 0;
76 }
77
78 double PerformanceResourceTiming::fetchStart() const
79 {
80     return monotonicTimeToDocumentMilliseconds(m_timing->requestTime);
81 }
82
83 double PerformanceResourceTiming::domainLookupStart() const
84 {
85     if (m_timing->dnsStart < 0)
86         return fetchStart();
87
88     return resourceTimeToDocumentMilliseconds(m_timing->dnsStart);
89 }
90
91 double PerformanceResourceTiming::domainLookupEnd() const
92 {
93     if (m_timing->dnsEnd < 0)
94         return domainLookupStart();
95
96     return resourceTimeToDocumentMilliseconds(m_timing->dnsEnd);
97 }
98
99 double PerformanceResourceTiming::connectStart() const
100 {
101     if (m_timing->connectStart < 0) // Connection was reused.
102         return domainLookupEnd();
103
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;
108
109     return resourceTimeToDocumentMilliseconds(connectStart);
110 }
111
112 double PerformanceResourceTiming::connectEnd() const
113 {
114     if (m_timing->connectEnd < 0) // Connection was reused.
115         return connectStart();
116
117     return resourceTimeToDocumentMilliseconds(m_timing->connectEnd);
118 }
119
120 double PerformanceResourceTiming::secureConnectionStart() const
121 {
122     if (m_timing->sslStart < 0) // Secure connection not negotiated.
123         return 0;
124
125     return resourceTimeToDocumentMilliseconds(m_timing->sslStart);
126 }
127
128 double PerformanceResourceTiming::requestStart() const
129 {
130     return resourceTimeToDocumentMilliseconds(m_timing->sendStart);
131 }
132
133 double PerformanceResourceTiming::responseStart() const
134 {
135     // FIXME: This number isn't exactly correct. See the notes in PerformanceTiming::responseStart().
136     return resourceTimeToDocumentMilliseconds(m_timing->receiveHeadersEnd);
137 }
138
139 double PerformanceResourceTiming::responseEnd() const
140 {
141     return monotonicTimeToDocumentMilliseconds(m_finishTime);
142 }
143
144 double PerformanceResourceTiming::monotonicTimeToDocumentMilliseconds(double seconds) const
145 {
146     ASSERT(seconds >= 0.0);
147     return m_requestingDocument->loader()->timing()->convertMonotonicTimeToDocumentTime(seconds) * 1000.0;
148 }
149
150 double PerformanceResourceTiming::resourceTimeToDocumentMilliseconds(int deltaMilliseconds) const
151 {
152     return monotonicTimeToDocumentMilliseconds(m_timing->requestTime) + deltaMilliseconds;
153 }
154
155 } // namespace WebCore
156
157 #endif // ENABLE(RESOURCE_TIMING)