2 * Copyright (C) 2012 Intel 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
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.
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.
27 #include "core/timing/PerformanceUserTiming.h"
29 #include "bindings/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 #include "wtf/text/WTFString.h"
41 typedef HashMap<String, NavigationTimingFunction> RestrictedKeyMap;
42 static RestrictedKeyMap restrictedKeyMap()
44 DEFINE_STATIC_LOCAL(RestrictedKeyMap, map, ());
46 map.add("navigationStart", &PerformanceTiming::navigationStart);
47 map.add("unloadEventStart", &PerformanceTiming::unloadEventStart);
48 map.add("unloadEventEnd", &PerformanceTiming::unloadEventEnd);
49 map.add("redirectStart", &PerformanceTiming::redirectStart);
50 map.add("redirectEnd", &PerformanceTiming::redirectEnd);
51 map.add("fetchStart", &PerformanceTiming::fetchStart);
52 map.add("domainLookupStart", &PerformanceTiming::domainLookupStart);
53 map.add("domainLookupEnd", &PerformanceTiming::domainLookupEnd);
54 map.add("connectStart", &PerformanceTiming::connectStart);
55 map.add("connectEnd", &PerformanceTiming::connectEnd);
56 map.add("secureConnectionStart", &PerformanceTiming::secureConnectionStart);
57 map.add("requestStart", &PerformanceTiming::requestStart);
58 map.add("responseStart", &PerformanceTiming::responseStart);
59 map.add("responseEnd", &PerformanceTiming::responseEnd);
60 map.add("domLoading", &PerformanceTiming::domLoading);
61 map.add("domInteractive", &PerformanceTiming::domInteractive);
62 map.add("domContentLoadedEventStart", &PerformanceTiming::domContentLoadedEventStart);
63 map.add("domContentLoadedEventEnd", &PerformanceTiming::domContentLoadedEventEnd);
64 map.add("domComplete", &PerformanceTiming::domComplete);
65 map.add("loadEventStart", &PerformanceTiming::loadEventStart);
66 map.add("loadEventEnd", &PerformanceTiming::loadEventEnd);
71 } // namespace anonymous
73 UserTiming::UserTiming(Performance* performance)
74 : m_performance(performance)
78 static void insertPerformanceEntry(PerformanceEntryMap& performanceEntryMap, PassRefPtr<PerformanceEntry> performanceEntry)
80 RefPtr<PerformanceEntry> entry = performanceEntry;
81 PerformanceEntryMap::iterator it = performanceEntryMap.find(entry->name());
82 if (it != performanceEntryMap.end())
83 it->value.append(entry);
85 Vector<RefPtr<PerformanceEntry> > v(1);
87 performanceEntryMap.set(entry->name(), v);
91 static void clearPeformanceEntries(PerformanceEntryMap& performanceEntryMap, const String& name)
94 performanceEntryMap.clear();
98 if (performanceEntryMap.contains(name))
99 performanceEntryMap.remove(name);
102 void UserTiming::mark(const String& markName, ExceptionState& es)
104 if (restrictedKeyMap().contains(markName)) {
105 es.throwDOMException(SyntaxError, "'" + markName + "' is part of the PerformanceTiming interface, and cannot be used as a mark name.");
109 double startTime = m_performance->now();
110 insertPerformanceEntry(m_marksMap, PerformanceMark::create(markName, startTime));
111 WebKit::Platform::current()->histogramCustomCounts("PLT.UserTiming_Mark", static_cast<int>(startTime), 0, 600000, 100);
114 void UserTiming::clearMarks(const String& markName)
116 clearPeformanceEntries(m_marksMap, markName);
119 double UserTiming::findExistingMarkStartTime(const String& markName, ExceptionState& es)
121 if (m_marksMap.contains(markName))
122 return m_marksMap.get(markName).last()->startTime();
124 if (restrictedKeyMap().contains(markName)) {
125 double value = static_cast<double>((m_performance->timing()->*(restrictedKeyMap().get(markName)))());
127 es.throwDOMException(InvalidAccessError, "'" + markName + "' is empty: either the event hasn't happened yet, or it would provide cross-origin timing information.");
130 return value - m_performance->timing()->navigationStart();
133 es.throwDOMException(SyntaxError, "The mark '" + markName + "' does not exist.");
137 void UserTiming::measure(const String& measureName, const String& startMark, const String& endMark, ExceptionState& es)
139 double startTime = 0.0;
140 double endTime = 0.0;
142 if (startMark.isNull())
143 endTime = m_performance->now();
144 else if (endMark.isNull()) {
145 endTime = m_performance->now();
146 startTime = findExistingMarkStartTime(startMark, es);
147 if (es.hadException())
150 endTime = findExistingMarkStartTime(endMark, es);
151 if (es.hadException())
153 startTime = findExistingMarkStartTime(startMark, es);
154 if (es.hadException())
158 insertPerformanceEntry(m_measuresMap, PerformanceMeasure::create(measureName, startTime, endTime));
159 if (endTime >= startTime)
160 WebKit::Platform::current()->histogramCustomCounts("PLT.UserTiming_MeasureDuration", static_cast<int>(endTime - startTime), 0, 600000, 100);
163 void UserTiming::clearMeasures(const String& measureName)
165 clearPeformanceEntries(m_measuresMap, measureName);
168 static Vector<RefPtr<PerformanceEntry> > convertToEntrySequence(const PerformanceEntryMap& performanceEntryMap)
170 Vector<RefPtr<PerformanceEntry> > entries;
172 for (PerformanceEntryMap::const_iterator it = performanceEntryMap.begin(); it != performanceEntryMap.end(); ++it)
173 entries.append(it->value);
178 static Vector<RefPtr<PerformanceEntry> > getEntrySequenceByName(const PerformanceEntryMap& performanceEntryMap, const String& name)
180 Vector<RefPtr<PerformanceEntry> > entries;
182 PerformanceEntryMap::const_iterator it = performanceEntryMap.find(name);
183 if (it != performanceEntryMap.end())
184 entries.append(it->value);
189 Vector<RefPtr<PerformanceEntry> > UserTiming::getMarks() const
191 return convertToEntrySequence(m_marksMap);
194 Vector<RefPtr<PerformanceEntry> > UserTiming::getMarks(const String& name) const
196 return getEntrySequenceByName(m_marksMap, name);
199 Vector<RefPtr<PerformanceEntry> > UserTiming::getMeasures() const
201 return convertToEntrySequence(m_measuresMap);
204 Vector<RefPtr<PerformanceEntry> > UserTiming::getMeasures(const String& name) const
206 return getEntrySequenceByName(m_measuresMap, name);
209 } // namespace WebCore