- add third_party src.
[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/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"
36
37 namespace WebCore {
38
39 namespace {
40
41 typedef HashMap<String, NavigationTimingFunction> RestrictedKeyMap;
42 static RestrictedKeyMap restrictedKeyMap()
43 {
44     DEFINE_STATIC_LOCAL(RestrictedKeyMap, map, ());
45     if (map.isEmpty()) {
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);
67     }
68     return map;
69 }
70
71 } // namespace anonymous
72
73 UserTiming::UserTiming(Performance* performance)
74     : m_performance(performance)
75 {
76 }
77
78 static void insertPerformanceEntry(PerformanceEntryMap& performanceEntryMap, PassRefPtr<PerformanceEntry> performanceEntry)
79 {
80     RefPtr<PerformanceEntry> entry = performanceEntry;
81     PerformanceEntryMap::iterator it = performanceEntryMap.find(entry->name());
82     if (it != performanceEntryMap.end())
83         it->value.append(entry);
84     else {
85         Vector<RefPtr<PerformanceEntry> > v(1);
86         v[0] = entry;
87         performanceEntryMap.set(entry->name(), v);
88     }
89 }
90
91 static void clearPeformanceEntries(PerformanceEntryMap& performanceEntryMap, const String& name)
92 {
93     if (name.isNull()) {
94         performanceEntryMap.clear();
95         return;
96     }
97
98     if (performanceEntryMap.contains(name))
99         performanceEntryMap.remove(name);
100 }
101
102 void UserTiming::mark(const String& markName, ExceptionState& es)
103 {
104     if (restrictedKeyMap().contains(markName)) {
105         es.throwDOMException(SyntaxError, "'" + markName + "' is part of the PerformanceTiming interface, and cannot be used as a mark name.");
106         return;
107     }
108
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);
112 }
113
114 void UserTiming::clearMarks(const String& markName)
115 {
116     clearPeformanceEntries(m_marksMap, markName);
117 }
118
119 double UserTiming::findExistingMarkStartTime(const String& markName, ExceptionState& es)
120 {
121     if (m_marksMap.contains(markName))
122         return m_marksMap.get(markName).last()->startTime();
123
124     if (restrictedKeyMap().contains(markName)) {
125         double value = static_cast<double>((m_performance->timing()->*(restrictedKeyMap().get(markName)))());
126         if (!value) {
127             es.throwDOMException(InvalidAccessError, "'" + markName + "' is empty: either the event hasn't happened yet, or it would provide cross-origin timing information.");
128             return 0.0;
129         }
130         return value - m_performance->timing()->navigationStart();
131     }
132
133     es.throwDOMException(SyntaxError, "The mark '" + markName + "' does not exist.");
134     return 0.0;
135 }
136
137 void UserTiming::measure(const String& measureName, const String& startMark, const String& endMark, ExceptionState& es)
138 {
139     double startTime = 0.0;
140     double endTime = 0.0;
141
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())
148             return;
149     } else {
150         endTime = findExistingMarkStartTime(endMark, es);
151         if (es.hadException())
152             return;
153         startTime = findExistingMarkStartTime(startMark, es);
154         if (es.hadException())
155             return;
156     }
157
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);
161 }
162
163 void UserTiming::clearMeasures(const String& measureName)
164 {
165     clearPeformanceEntries(m_measuresMap, measureName);
166 }
167
168 static Vector<RefPtr<PerformanceEntry> > convertToEntrySequence(const PerformanceEntryMap& performanceEntryMap)
169 {
170     Vector<RefPtr<PerformanceEntry> > entries;
171
172     for (PerformanceEntryMap::const_iterator it = performanceEntryMap.begin(); it != performanceEntryMap.end(); ++it)
173         entries.append(it->value);
174
175     return entries;
176 }
177
178 static Vector<RefPtr<PerformanceEntry> > getEntrySequenceByName(const PerformanceEntryMap& performanceEntryMap, const String& name)
179 {
180     Vector<RefPtr<PerformanceEntry> > entries;
181
182     PerformanceEntryMap::const_iterator it = performanceEntryMap.find(name);
183     if (it != performanceEntryMap.end())
184         entries.append(it->value);
185
186     return entries;
187 }
188
189 Vector<RefPtr<PerformanceEntry> > UserTiming::getMarks() const
190 {
191     return convertToEntrySequence(m_marksMap);
192 }
193
194 Vector<RefPtr<PerformanceEntry> > UserTiming::getMarks(const String& name) const
195 {
196     return getEntrySequenceByName(m_marksMap, name);
197 }
198
199 Vector<RefPtr<PerformanceEntry> > UserTiming::getMeasures() const
200 {
201     return convertToEntrySequence(m_measuresMap);
202 }
203
204 Vector<RefPtr<PerformanceEntry> > UserTiming::getMeasures(const String& name) const
205 {
206     return getEntrySequenceByName(m_measuresMap, name);
207 }
208
209 } // namespace WebCore