Bumping gaia.json for 8 gaia revision(s) a=gaia-bump
[gecko.git] / dom / base / nsPerformance.h
blob4d79b9e27328169ccb2a6700897c27638cc58b73
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsPerformance_h___
6 #define nsPerformance_h___
8 #include "nsCOMPtr.h"
9 #include "nsAutoPtr.h"
10 #include "mozilla/Attributes.h"
11 #include "nsWrapperCache.h"
12 #include "nsDOMNavigationTiming.h"
13 #include "nsContentUtils.h"
14 #include "nsPIDOMWindow.h"
15 #include "js/TypeDecls.h"
16 #include "mozilla/dom/BindingDeclarations.h"
17 #include "mozilla/DOMEventTargetHelper.h"
19 class nsITimedChannel;
20 class nsPerformance;
21 class nsIHttpChannel;
23 namespace mozilla {
24 class ErrorResult;
25 namespace dom {
26 class PerformanceEntry;
30 // Script "performance.timing" object
31 class nsPerformanceTiming MOZ_FINAL : public nsWrapperCache
33 public:
34 typedef mozilla::TimeStamp TimeStamp;
36 /**
37 * @param aPerformance
38 * The performance object (the JS parent).
39 * This will allow access to "window.performance.timing" attribute for
40 * the navigation timing (can't be null).
41 * @param aChannel
42 * An nsITimedChannel used to gather all the networking timings by both
43 * the navigation timing and the resource timing (can't be null).
44 * @param aHttpChannel
45 * An nsIHttpChannel (the resource's http channel).
46 * This will be used by the resource timing cross-domain check
47 * algorithm.
48 * Argument is null for the navigation timing (navigation timing uses
49 * another algorithm for the cross-domain redirects).
50 * @param aZeroTime
51 * The offset that will be added to the timestamp of each event. This
52 * argument should be equal to performance.navigationStart for
53 * navigation timing and "0" for the resource timing.
55 nsPerformanceTiming(nsPerformance* aPerformance,
56 nsITimedChannel* aChannel,
57 nsIHttpChannel* aHttpChannel,
58 DOMHighResTimeStamp aZeroTime);
59 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(nsPerformanceTiming)
60 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(nsPerformanceTiming)
62 nsDOMNavigationTiming* GetDOMTiming() const;
64 nsPerformance* GetParentObject() const
66 return mPerformance;
69 /**
70 * @param aStamp
71 * The TimeStamp recorded for a specific event. This TimeStamp can
72 * be null.
73 * @return the duration of an event with a given TimeStamp, relative to the
74 * navigationStart TimeStamp (the moment the user landed on the
75 * page), if the given TimeStamp is valid. Otherwise, it will return
76 * the FetchStart timing value.
78 inline DOMHighResTimeStamp TimeStampToDOMHighResOrFetchStart(TimeStamp aStamp)
80 return (!aStamp.IsNull())
81 ? TimeStampToDOMHighRes(aStamp)
82 : FetchStartHighRes();
85 /**
86 * The nsITimedChannel records an absolute timestamp for each event.
87 * The nsDOMNavigationTiming will record the moment when the user landed on
88 * the page. This is a window.performance unique timestamp, so it can be used
89 * for all the events (navigation timing and resource timing events).
91 * The algorithm operates in 2 steps:
92 * 1. The first step is to subtract the two timestamps: the argument (the
93 * envet's timesramp) and the navigation start timestamp. This will result in
94 * a relative timestamp of the event (relative to the navigation start -
95 * window.performance.timing.navigationStart).
96 * 2. The second step is to add any required offset (the mZeroTime). For now,
97 * this offset value is either 0 (for the resource timing), or equal to
98 * "performance.navigationStart" (for navigation timing).
99 * For the resource timing, mZeroTime is set to 0, causing the result to be a
100 * relative time.
101 * For the navigation timing, mZeroTime is set to "performance.navigationStart"
102 * causing the result be an absolute time.
104 * @param aStamp
105 * The TimeStamp recorded for a specific event. This TimeStamp can't
106 * be null.
107 * @return number of milliseconds value as one of:
108 * - relative to the navigation start time, time the user has landed on the
109 * page
110 * - an absolute wall clock time since the unix epoch
112 inline DOMHighResTimeStamp TimeStampToDOMHighRes(TimeStamp aStamp) const
114 MOZ_ASSERT(!aStamp.IsNull());
115 mozilla::TimeDuration duration =
116 aStamp - GetDOMTiming()->GetNavigationStartTimeStamp();
117 return duration.ToMilliseconds() + mZeroTime;
120 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
122 // PerformanceNavigation WebIDL methods
123 DOMTimeMilliSec NavigationStart() const {
124 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
125 return 0;
127 return GetDOMTiming()->GetNavigationStart();
129 DOMTimeMilliSec UnloadEventStart() {
130 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
131 return 0;
133 return GetDOMTiming()->GetUnloadEventStart();
135 DOMTimeMilliSec UnloadEventEnd() {
136 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
137 return 0;
139 return GetDOMTiming()->GetUnloadEventEnd();
142 uint16_t GetRedirectCount() const;
143 // Checks if the resource is either same origin as the page that started
144 // the load, or if the response contains the Timing-Allow-Origin header
145 // with a value of * or matching the domain of the loading Principal
146 bool CheckAllowedOrigin(nsIHttpChannel* aResourceChannel, nsITimedChannel* aChannel);
147 // Cached result of CheckAllowedOrigin. If false, security sensitive
148 // attributes of the resourceTiming object will be set to 0
149 bool TimingAllowed() const;
151 // If this is false the values of redirectStart/End will be 0
152 // This is false if no redirects occured, or if any of the responses failed
153 // the timing-allow-origin check in HttpBaseChannel::TimingAllowCheck
154 bool ShouldReportCrossOriginRedirect() const;
156 // High resolution (used by resource timing)
157 DOMHighResTimeStamp FetchStartHighRes();
158 DOMHighResTimeStamp RedirectStartHighRes();
159 DOMHighResTimeStamp RedirectEndHighRes();
160 DOMHighResTimeStamp DomainLookupStartHighRes();
161 DOMHighResTimeStamp DomainLookupEndHighRes();
162 DOMHighResTimeStamp ConnectStartHighRes();
163 DOMHighResTimeStamp ConnectEndHighRes();
164 DOMHighResTimeStamp RequestStartHighRes();
165 DOMHighResTimeStamp ResponseStartHighRes();
166 DOMHighResTimeStamp ResponseEndHighRes();
168 // Low resolution (used by navigation timing)
169 DOMTimeMilliSec FetchStart();
170 DOMTimeMilliSec RedirectStart();
171 DOMTimeMilliSec RedirectEnd();
172 DOMTimeMilliSec DomainLookupStart();
173 DOMTimeMilliSec DomainLookupEnd();
174 DOMTimeMilliSec ConnectStart();
175 DOMTimeMilliSec ConnectEnd();
176 DOMTimeMilliSec RequestStart();
177 DOMTimeMilliSec ResponseStart();
178 DOMTimeMilliSec ResponseEnd();
180 DOMTimeMilliSec DomLoading() {
181 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
182 return 0;
184 return GetDOMTiming()->GetDomLoading();
186 DOMTimeMilliSec DomInteractive() const {
187 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
188 return 0;
190 return GetDOMTiming()->GetDomInteractive();
192 DOMTimeMilliSec DomContentLoadedEventStart() const {
193 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
194 return 0;
196 return GetDOMTiming()->GetDomContentLoadedEventStart();
198 DOMTimeMilliSec DomContentLoadedEventEnd() const {
199 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
200 return 0;
202 return GetDOMTiming()->GetDomContentLoadedEventEnd();
204 DOMTimeMilliSec DomComplete() const {
205 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
206 return 0;
208 return GetDOMTiming()->GetDomComplete();
210 DOMTimeMilliSec LoadEventStart() const {
211 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
212 return 0;
214 return GetDOMTiming()->GetLoadEventStart();
216 DOMTimeMilliSec LoadEventEnd() const {
217 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
218 return 0;
220 return GetDOMTiming()->GetLoadEventEnd();
223 private:
224 ~nsPerformanceTiming();
225 bool IsInitialized() const;
226 void InitializeTimingInfo(nsITimedChannel* aChannel);
227 nsRefPtr<nsPerformance> mPerformance;
228 DOMHighResTimeStamp mFetchStart;
229 // This is an offset that will be added to each timing ([ms] resolution).
230 // There are only 2 possible values: (1) logicaly equal to navigationStart
231 // TimeStamp (results are absolute timstamps - wallclock); (2) "0" (results
232 // are relative to the navigation start).
233 DOMHighResTimeStamp mZeroTime;
235 TimeStamp mAsyncOpen;
236 TimeStamp mRedirectStart;
237 TimeStamp mRedirectEnd;
238 TimeStamp mDomainLookupStart;
239 TimeStamp mDomainLookupEnd;
240 TimeStamp mConnectStart;
241 TimeStamp mConnectEnd;
242 TimeStamp mRequestStart;
243 TimeStamp mResponseStart;
244 TimeStamp mCacheReadStart;
245 TimeStamp mResponseEnd;
246 TimeStamp mCacheReadEnd;
247 uint16_t mRedirectCount;
248 bool mTimingAllowed;
249 bool mAllRedirectsSameOrigin;
250 bool mInitialized;
252 // If the resourceTiming object should have non-zero redirectStart and
253 // redirectEnd attributes. It is false if there were no redirects, or if
254 // any of the responses didn't pass the timing-allow-check
255 bool mReportCrossOriginRedirect;
258 // Script "performance.navigation" object
259 class nsPerformanceNavigation MOZ_FINAL : public nsWrapperCache
261 public:
262 explicit nsPerformanceNavigation(nsPerformance* aPerformance);
263 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(nsPerformanceNavigation)
264 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(nsPerformanceNavigation)
266 nsDOMNavigationTiming* GetDOMTiming() const;
267 nsPerformanceTiming* GetPerformanceTiming() const;
269 nsPerformance* GetParentObject() const
271 return mPerformance;
274 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
276 // PerformanceNavigation WebIDL methods
277 uint16_t Type() const {
278 return GetDOMTiming()->GetType();
280 uint16_t RedirectCount() const {
281 return GetPerformanceTiming()->GetRedirectCount();
284 private:
285 ~nsPerformanceNavigation();
286 nsRefPtr<nsPerformance> mPerformance;
289 // Script "performance" object
290 class nsPerformance MOZ_FINAL : public mozilla::DOMEventTargetHelper
292 public:
293 typedef mozilla::dom::PerformanceEntry PerformanceEntry;
294 nsPerformance(nsPIDOMWindow* aWindow,
295 nsDOMNavigationTiming* aDOMTiming,
296 nsITimedChannel* aChannel,
297 nsPerformance* aParentPerformance);
299 NS_DECL_ISUPPORTS_INHERITED
300 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsPerformance, DOMEventTargetHelper)
302 nsDOMNavigationTiming* GetDOMTiming() const
304 return mDOMTiming;
307 nsITimedChannel* GetChannel() const
309 return mChannel;
312 nsPerformance* GetParentPerformance() const
314 return mParentPerformance;
317 nsPIDOMWindow* GetParentObject() const
319 return mWindow.get();
322 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
324 // Performance WebIDL methods
325 DOMHighResTimeStamp Now();
326 nsPerformanceTiming* Timing();
327 nsPerformanceNavigation* Navigation();
329 void GetEntries(nsTArray<nsRefPtr<PerformanceEntry> >& retval);
330 void GetEntriesByType(const nsAString& entryType,
331 nsTArray<nsRefPtr<PerformanceEntry> >& retval);
332 void GetEntriesByName(const nsAString& name,
333 const mozilla::dom::Optional< nsAString >& entryType,
334 nsTArray<nsRefPtr<PerformanceEntry> >& retval);
335 void AddEntry(nsIHttpChannel* channel,
336 nsITimedChannel* timedChannel);
337 void ClearResourceTimings();
338 void SetResourceTimingBufferSize(uint64_t maxSize);
339 void Mark(const nsAString& aName, mozilla::ErrorResult& aRv);
340 void ClearMarks(const mozilla::dom::Optional<nsAString>& aName);
341 void Measure(const nsAString& aName,
342 const mozilla::dom::Optional<nsAString>& aStartMark,
343 const mozilla::dom::Optional<nsAString>& aEndMark,
344 mozilla::ErrorResult& aRv);
345 void ClearMeasures(const mozilla::dom::Optional<nsAString>& aName);
347 IMPL_EVENT_HANDLER(resourcetimingbufferfull)
349 private:
350 ~nsPerformance();
351 bool IsPerformanceTimingAttribute(const nsAString& aName);
352 DOMHighResTimeStamp ResolveTimestampFromName(const nsAString& aName, mozilla::ErrorResult& aRv);
353 DOMTimeMilliSec GetPerformanceTimingFromString(const nsAString& aTimingName);
354 DOMHighResTimeStamp ConvertDOMMilliSecToHighRes(const DOMTimeMilliSec aTime);
355 void DispatchBufferFullEvent();
356 void InsertPerformanceEntry(PerformanceEntry* aEntry, bool aShouldPrint);
357 void ClearEntries(const mozilla::dom::Optional<nsAString>& aEntryName,
358 const nsAString& aEntryType);
359 nsCOMPtr<nsPIDOMWindow> mWindow;
360 nsRefPtr<nsDOMNavigationTiming> mDOMTiming;
361 nsCOMPtr<nsITimedChannel> mChannel;
362 nsRefPtr<nsPerformanceTiming> mTiming;
363 nsRefPtr<nsPerformanceNavigation> mNavigation;
364 nsTArray<nsRefPtr<PerformanceEntry> > mEntries;
365 nsRefPtr<nsPerformance> mParentPerformance;
366 uint64_t mPrimaryBufferSize;
368 static const uint64_t kDefaultBufferSize = 150;
370 // Helper classes
371 class PerformanceEntryComparator {
372 public:
373 bool Equals(const PerformanceEntry* aElem1,
374 const PerformanceEntry* aElem2) const;
375 bool LessThan(const PerformanceEntry* aElem1,
376 const PerformanceEntry* aElem2) const;
380 inline nsDOMNavigationTiming*
381 nsPerformanceNavigation::GetDOMTiming() const
383 return mPerformance->GetDOMTiming();
386 inline nsPerformanceTiming*
387 nsPerformanceNavigation::GetPerformanceTiming() const
389 return mPerformance->Timing();
392 inline nsDOMNavigationTiming*
393 nsPerformanceTiming::GetDOMTiming() const
395 return mPerformance->GetDOMTiming();
398 #endif /* nsPerformance_h___ */