Bumping manifests a=b2g-bump
[gecko.git] / dom / base / nsPerformance.h
blob09697b055bc176dd9c39193e063813bc1bf57959
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 namespace dom {
25 class PerformanceEntry;
29 // Script "performance.timing" object
30 class nsPerformanceTiming MOZ_FINAL : public nsWrapperCache
32 public:
33 typedef mozilla::TimeStamp TimeStamp;
35 /**
36 * @param aPerformance
37 * The performance object (the JS parent).
38 * This will allow access to "window.performance.timing" attribute for
39 * the navigation timing (can't be null).
40 * @param aChannel
41 * An nsITimedChannel used to gather all the networking timings by both
42 * the navigation timing and the resource timing (can't be null).
43 * @param aHttpChannel
44 * An nsIHttpChannel (the resource's http channel).
45 * This will be used by the resource timing cross-domain check
46 * algorithm.
47 * Argument is null for the navigation timing (navigation timing uses
48 * another algorithm for the cross-domain redirects).
49 * @param aZeroTime
50 * The offset that will be added to the timestamp of each event. This
51 * argument should be equal to performance.navigationStart for
52 * navigation timing and "0" for the resource timing.
54 nsPerformanceTiming(nsPerformance* aPerformance,
55 nsITimedChannel* aChannel,
56 nsIHttpChannel* aHttpChannel,
57 DOMHighResTimeStamp aZeroTime);
58 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(nsPerformanceTiming)
59 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(nsPerformanceTiming)
61 nsDOMNavigationTiming* GetDOMTiming() const;
63 nsPerformance* GetParentObject() const
65 return mPerformance;
68 /**
69 * @param aStamp
70 * The TimeStamp recorded for a specific event. This TimeStamp can
71 * be null.
72 * @return the duration of an event with a given TimeStamp, relative to the
73 * navigationStart TimeStamp (the moment the user landed on the
74 * page), if the given TimeStamp is valid. Otherwise, it will return
75 * the FetchStart timing value.
77 inline DOMHighResTimeStamp TimeStampToDOMHighResOrFetchStart(TimeStamp aStamp)
79 return (!aStamp.IsNull())
80 ? TimeStampToDOMHighRes(aStamp)
81 : FetchStartHighRes();
84 /**
85 * The nsITimedChannel records an absolute timestamp for each event.
86 * The nsDOMNavigationTiming will record the moment when the user landed on
87 * the page. This is a window.performance unique timestamp, so it can be used
88 * for all the events (navigation timing and resource timing events).
90 * The algorithm operates in 2 steps:
91 * 1. The first step is to subtract the two timestamps: the argument (the
92 * envet's timesramp) and the navigation start timestamp. This will result in
93 * a relative timestamp of the event (relative to the navigation start -
94 * window.performance.timing.navigationStart).
95 * 2. The second step is to add any required offset (the mZeroTime). For now,
96 * this offset value is either 0 (for the resource timing), or equal to
97 * "performance.navigationStart" (for navigation timing).
98 * For the resource timing, mZeroTime is set to 0, causing the result to be a
99 * relative time.
100 * For the navigation timing, mZeroTime is set to "performance.navigationStart"
101 * causing the result be an absolute time.
103 * @param aStamp
104 * The TimeStamp recorded for a specific event. This TimeStamp can't
105 * be null.
106 * @return number of milliseconds value as one of:
107 * - relative to the navigation start time, time the user has landed on the
108 * page
109 * - an absolute wall clock time since the unix epoch
111 inline DOMHighResTimeStamp TimeStampToDOMHighRes(TimeStamp aStamp) const
113 MOZ_ASSERT(!aStamp.IsNull());
114 mozilla::TimeDuration duration =
115 aStamp - GetDOMTiming()->GetNavigationStartTimeStamp();
116 return duration.ToMilliseconds() + mZeroTime;
119 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
121 // PerformanceNavigation WebIDL methods
122 DOMTimeMilliSec NavigationStart() const {
123 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
124 return 0;
126 return GetDOMTiming()->GetNavigationStart();
128 DOMTimeMilliSec UnloadEventStart() {
129 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
130 return 0;
132 return GetDOMTiming()->GetUnloadEventStart();
134 DOMTimeMilliSec UnloadEventEnd() {
135 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
136 return 0;
138 return GetDOMTiming()->GetUnloadEventEnd();
141 uint16_t GetRedirectCount() const;
142 bool IsSameOriginAsReferral() const;
143 void CheckRedirectCrossOrigin(nsIHttpChannel* aResourceChannel);
145 // High resolution (used by resource timing)
146 DOMHighResTimeStamp FetchStartHighRes();
147 DOMHighResTimeStamp RedirectStartHighRes();
148 DOMHighResTimeStamp RedirectEndHighRes();
149 DOMHighResTimeStamp DomainLookupStartHighRes();
150 DOMHighResTimeStamp DomainLookupEndHighRes();
151 DOMHighResTimeStamp ConnectStartHighRes();
152 DOMHighResTimeStamp ConnectEndHighRes();
153 DOMHighResTimeStamp RequestStartHighRes();
154 DOMHighResTimeStamp ResponseStartHighRes();
155 DOMHighResTimeStamp ResponseEndHighRes();
157 // Low resolution (used by navigation timing)
158 DOMTimeMilliSec FetchStart();
159 DOMTimeMilliSec RedirectStart();
160 DOMTimeMilliSec RedirectEnd();
161 DOMTimeMilliSec DomainLookupStart();
162 DOMTimeMilliSec DomainLookupEnd();
163 DOMTimeMilliSec ConnectStart();
164 DOMTimeMilliSec ConnectEnd();
165 DOMTimeMilliSec RequestStart();
166 DOMTimeMilliSec ResponseStart();
167 DOMTimeMilliSec ResponseEnd();
169 DOMTimeMilliSec DomLoading() {
170 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
171 return 0;
173 return GetDOMTiming()->GetDomLoading();
175 DOMTimeMilliSec DomInteractive() const {
176 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
177 return 0;
179 return GetDOMTiming()->GetDomInteractive();
181 DOMTimeMilliSec DomContentLoadedEventStart() const {
182 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
183 return 0;
185 return GetDOMTiming()->GetDomContentLoadedEventStart();
187 DOMTimeMilliSec DomContentLoadedEventEnd() const {
188 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
189 return 0;
191 return GetDOMTiming()->GetDomContentLoadedEventEnd();
193 DOMTimeMilliSec DomComplete() const {
194 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
195 return 0;
197 return GetDOMTiming()->GetDomComplete();
199 DOMTimeMilliSec LoadEventStart() const {
200 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
201 return 0;
203 return GetDOMTiming()->GetLoadEventStart();
205 DOMTimeMilliSec LoadEventEnd() const {
206 if (!nsContentUtils::IsPerformanceTimingEnabled()) {
207 return 0;
209 return GetDOMTiming()->GetLoadEventEnd();
212 private:
213 ~nsPerformanceTiming();
214 bool IsInitialized() const;
215 nsRefPtr<nsPerformance> mPerformance;
216 nsCOMPtr<nsITimedChannel> mChannel;
217 DOMHighResTimeStamp mFetchStart;
218 // This is an offset that will be added to each timing ([ms] resolution).
219 // There are only 2 possible values: (1) logicaly equal to navigationStart
220 // TimeStamp (results are absolute timstamps - wallclock); (2) "0" (results
221 // are relative to the navigation start).
222 DOMHighResTimeStamp mZeroTime;
223 bool mReportCrossOriginResources;
226 // Script "performance.navigation" object
227 class nsPerformanceNavigation MOZ_FINAL : public nsWrapperCache
229 public:
230 explicit nsPerformanceNavigation(nsPerformance* aPerformance);
231 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(nsPerformanceNavigation)
232 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(nsPerformanceNavigation)
234 nsDOMNavigationTiming* GetDOMTiming() const;
235 nsPerformanceTiming* GetPerformanceTiming() const;
237 nsPerformance* GetParentObject() const
239 return mPerformance;
242 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
244 // PerformanceNavigation WebIDL methods
245 uint16_t Type() const {
246 return GetDOMTiming()->GetType();
248 uint16_t RedirectCount() const {
249 return GetPerformanceTiming()->GetRedirectCount();
252 private:
253 ~nsPerformanceNavigation();
254 nsRefPtr<nsPerformance> mPerformance;
257 // Script "performance" object
258 class nsPerformance MOZ_FINAL : public mozilla::DOMEventTargetHelper
260 public:
261 typedef mozilla::dom::PerformanceEntry PerformanceEntry;
262 nsPerformance(nsPIDOMWindow* aWindow,
263 nsDOMNavigationTiming* aDOMTiming,
264 nsITimedChannel* aChannel,
265 nsPerformance* aParentPerformance);
267 NS_DECL_ISUPPORTS_INHERITED
268 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsPerformance, DOMEventTargetHelper)
270 nsDOMNavigationTiming* GetDOMTiming() const
272 return mDOMTiming;
275 nsITimedChannel* GetChannel() const
277 return mChannel;
280 nsPerformance* GetParentPerformance() const
282 return mParentPerformance;
285 nsPIDOMWindow* GetParentObject() const
287 return mWindow.get();
290 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
292 // Performance WebIDL methods
293 DOMHighResTimeStamp Now();
294 nsPerformanceTiming* Timing();
295 nsPerformanceNavigation* Navigation();
297 void GetEntries(nsTArray<nsRefPtr<PerformanceEntry> >& retval);
298 void GetEntriesByType(const nsAString& entryType,
299 nsTArray<nsRefPtr<PerformanceEntry> >& retval);
300 void GetEntriesByName(const nsAString& name,
301 const mozilla::dom::Optional< nsAString >& entryType,
302 nsTArray<nsRefPtr<PerformanceEntry> >& retval);
303 void AddEntry(nsIHttpChannel* channel,
304 nsITimedChannel* timedChannel);
305 void ClearResourceTimings();
306 void SetResourceTimingBufferSize(uint64_t maxSize);
307 IMPL_EVENT_HANDLER(resourcetimingbufferfull)
309 private:
310 ~nsPerformance();
311 void DispatchBufferFullEvent();
313 nsCOMPtr<nsPIDOMWindow> mWindow;
314 nsRefPtr<nsDOMNavigationTiming> mDOMTiming;
315 nsCOMPtr<nsITimedChannel> mChannel;
316 nsRefPtr<nsPerformanceTiming> mTiming;
317 nsRefPtr<nsPerformanceNavigation> mNavigation;
318 nsTArray<nsRefPtr<PerformanceEntry> > mEntries;
319 nsRefPtr<nsPerformance> mParentPerformance;
320 uint64_t mPrimaryBufferSize;
322 static const uint64_t kDefaultBufferSize = 150;
324 // Helper classes
325 class PerformanceEntryComparator {
326 public:
327 bool Equals(const PerformanceEntry* aElem1,
328 const PerformanceEntry* aElem2) const;
329 bool LessThan(const PerformanceEntry* aElem1,
330 const PerformanceEntry* aElem2) const;
334 inline nsDOMNavigationTiming*
335 nsPerformanceNavigation::GetDOMTiming() const
337 return mPerformance->GetDOMTiming();
340 inline nsPerformanceTiming*
341 nsPerformanceNavigation::GetPerformanceTiming() const
343 return mPerformance->Timing();
346 inline nsDOMNavigationTiming*
347 nsPerformanceTiming::GetDOMTiming() const
349 return mPerformance->GetDOMTiming();
352 #endif /* nsPerformance_h___ */