Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / timing / PerformanceUserTiming.cpp
1 /*
2  * Copyright (C) 2012 Intel 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "core/timing/PerformanceUserTiming.h"
28
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/dom/ExceptionCode.h"
31 #include "core/timing/Performance.h"
32 #include "core/timing/PerformanceMark.h"
33 #include "core/timing/PerformanceMeasure.h"
34 #include "public/platform/Platform.h"
35
36 namespace blink {
37
38 namespace {
39
40 using RestrictedKeyMap = HashMap<String, NavigationTimingFunction>;
41 static RestrictedKeyMap restrictedKeyMap()
42 {
43     DEFINE_STATIC_LOCAL(RestrictedKeyMap, map, ());
44     if (map.isEmpty()) {
45         map.add("navigationStart", &PerformanceTiming::navigationStart);
46         map.add("unloadEventStart", &PerformanceTiming::unloadEventStart);
47         map.add("unloadEventEnd", &PerformanceTiming::unloadEventEnd);
48         map.add("redirectStart", &PerformanceTiming::redirectStart);
49         map.add("redirectEnd", &PerformanceTiming::redirectEnd);
50         map.add("fetchStart", &PerformanceTiming::fetchStart);
51         map.add("domainLookupStart", &PerformanceTiming::domainLookupStart);
52         map.add("domainLookupEnd", &PerformanceTiming::domainLookupEnd);
53         map.add("connectStart", &PerformanceTiming::connectStart);
54         map.add("connectEnd", &PerformanceTiming::connectEnd);
55         map.add("secureConnectionStart", &PerformanceTiming::secureConnectionStart);
56         map.add("requestStart", &PerformanceTiming::requestStart);
57         map.add("responseStart", &PerformanceTiming::responseStart);
58         map.add("responseEnd", &PerformanceTiming::responseEnd);
59         map.add("domLoading", &PerformanceTiming::domLoading);
60         map.add("domInteractive", &PerformanceTiming::domInteractive);
61         map.add("domContentLoadedEventStart", &PerformanceTiming::domContentLoadedEventStart);
62         map.add("domContentLoadedEventEnd", &PerformanceTiming::domContentLoadedEventEnd);
63         map.add("domComplete", &PerformanceTiming::domComplete);
64         map.add("loadEventStart", &PerformanceTiming::loadEventStart);
65         map.add("loadEventEnd", &PerformanceTiming::loadEventEnd);
66     }
67     return map;
68 }
69
70 } // namespace anonymous
71
72 UserTiming::UserTiming(Performance* performance)
73     : m_performance(performance)
74 {
75 }
76
77 static void insertPerformanceEntry(PerformanceEntryMap& performanceEntryMap, PassRefPtrWillBeRawPtr<PerformanceEntry> performanceEntry)
78 {
79     RefPtrWillBeRawPtr<PerformanceEntry> entry = performanceEntry;
80     PerformanceEntryMap::iterator it = performanceEntryMap.find(entry->name());
81     if (it != performanceEntryMap.end())
82         it->value.append(entry);
83     else {
84         PerformanceEntryVector vector(1);
85         vector[0] = entry;
86         performanceEntryMap.set(entry->name(), vector);
87     }
88 }
89
90 static void clearPeformanceEntries(PerformanceEntryMap& performanceEntryMap, const String& name)
91 {
92     if (name.isNull()) {
93         performanceEntryMap.clear();
94         return;
95     }
96
97     if (performanceEntryMap.contains(name))
98         performanceEntryMap.remove(name);
99 }
100
101 void UserTiming::mark(const String& markName, ExceptionState& exceptionState)
102 {
103     if (restrictedKeyMap().contains(markName)) {
104         exceptionState.throwDOMException(SyntaxError, "'" + markName + "' is part of the PerformanceTiming interface, and cannot be used as a mark name.");
105         return;
106     }
107
108     double startTime = m_performance->now();
109     insertPerformanceEntry(m_marksMap, PerformanceMark::create(markName, startTime));
110     blink::Platform::current()->histogramCustomCounts("PLT.UserTiming_Mark", static_cast<int>(startTime), 0, 600000, 100);
111 }
112
113 void UserTiming::clearMarks(const String& markName)
114 {
115     clearPeformanceEntries(m_marksMap, markName);
116 }
117
118 double UserTiming::findExistingMarkStartTime(const String& markName, ExceptionState& exceptionState)
119 {
120     if (m_marksMap.contains(markName))
121         return m_marksMap.get(markName).last()->startTime();
122
123     if (restrictedKeyMap().contains(markName)) {
124         double value = static_cast<double>((m_performance->timing()->*(restrictedKeyMap().get(markName)))());
125         if (!value) {
126             exceptionState.throwDOMException(InvalidAccessError, "'" + markName + "' is empty: either the event hasn't happened yet, or it would provide cross-origin timing information.");
127             return 0.0;
128         }
129         return value - m_performance->timing()->navigationStart();
130     }
131
132     exceptionState.throwDOMException(SyntaxError, "The mark '" + markName + "' does not exist.");
133     return 0.0;
134 }
135
136 void UserTiming::measure(const String& measureName, const String& startMark, const String& endMark, ExceptionState& exceptionState)
137 {
138     double startTime = 0.0;
139     double endTime = 0.0;
140
141     if (startMark.isNull())
142         endTime = m_performance->now();
143     else if (endMark.isNull()) {
144         endTime = m_performance->now();
145         startTime = findExistingMarkStartTime(startMark, exceptionState);
146         if (exceptionState.hadException())
147             return;
148     } else {
149         endTime = findExistingMarkStartTime(endMark, exceptionState);
150         if (exceptionState.hadException())
151             return;
152         startTime = findExistingMarkStartTime(startMark, exceptionState);
153         if (exceptionState.hadException())
154             return;
155     }
156
157     insertPerformanceEntry(m_measuresMap, PerformanceMeasure::create(measureName, startTime, endTime));
158     if (endTime >= startTime)
159         blink::Platform::current()->histogramCustomCounts("PLT.UserTiming_MeasureDuration", static_cast<int>(endTime - startTime), 0, 600000, 100);
160 }
161
162 void UserTiming::clearMeasures(const String& measureName)
163 {
164     clearPeformanceEntries(m_measuresMap, measureName);
165 }
166
167 static PerformanceEntryVector convertToEntrySequence(const PerformanceEntryMap& performanceEntryMap)
168 {
169     PerformanceEntryVector entries;
170
171     for (const auto& entry : performanceEntryMap)
172         entries.appendVector(entry.value);
173
174     return entries;
175 }
176
177 static PerformanceEntryVector getEntrySequenceByName(const PerformanceEntryMap& performanceEntryMap, const String& name)
178 {
179     PerformanceEntryVector entries;
180
181     PerformanceEntryMap::const_iterator it = performanceEntryMap.find(name);
182     if (it != performanceEntryMap.end())
183         entries.appendVector(it->value);
184
185     return entries;
186 }
187
188 PerformanceEntryVector UserTiming::getMarks() const
189 {
190     return convertToEntrySequence(m_marksMap);
191 }
192
193 PerformanceEntryVector UserTiming::getMarks(const String& name) const
194 {
195     return getEntrySequenceByName(m_marksMap, name);
196 }
197
198 PerformanceEntryVector UserTiming::getMeasures() const
199 {
200     return convertToEntrySequence(m_measuresMap);
201 }
202
203 PerformanceEntryVector UserTiming::getMeasures(const String& name) const
204 {
205     return getEntrySequenceByName(m_measuresMap, name);
206 }
207
208 void UserTiming::trace(Visitor* visitor)
209 {
210 #if ENABLE(OILPAN)
211     visitor->trace(m_performance);
212     visitor->trace(m_marksMap);
213     visitor->trace(m_measuresMap);
214 #endif
215 }
216
217 } // namespace blink