1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_PerformanceTiming_h
8 #define mozilla_dom_PerformanceTiming_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/BasePrincipal.h"
12 #include "mozilla/StaticPrefs_dom.h"
13 #include "nsContentUtils.h"
14 #include "nsDOMNavigationTiming.h"
15 #include "nsRFPService.h"
16 #include "nsWrapperCache.h"
17 #include "CacheablePerformanceTimingData.h"
18 #include "Performance.h"
19 #include "nsITimedChannel.h"
20 #include "mozilla/dom/PerformanceTimingTypes.h"
21 #include "mozilla/ipc/IPDLParamTraits.h"
22 #include "ipc/IPCMessageUtils.h"
23 #include "ipc/IPCMessageUtilsSpecializations.h"
24 #include "mozilla/net/nsServerTiming.h"
28 namespace mozilla::dom
{
30 class PerformanceTiming
;
31 enum class RenderBlockingStatusType
: uint8_t;
33 class PerformanceTimingData final
: public CacheablePerformanceTimingData
{
34 friend class PerformanceTiming
;
35 friend struct mozilla::ipc::IPDLParamTraits
<
36 mozilla::dom::PerformanceTimingData
>;
38 // https://w3c.github.io/resource-timing/#dom-performanceresourcetiming-transfersize
39 // The transferSize getter steps are to perform the following steps:
40 // 1. If this's cache mode is "local", then return 0.
41 static constexpr uint64_t kLocalCacheTransferSize
= 0;
44 PerformanceTimingData() = default; // For deserialization
45 // This can return null.
46 static PerformanceTimingData
* Create(nsITimedChannel
* aChannel
,
47 nsIHttpChannel
* aHttpChannel
,
48 DOMHighResTimeStamp aZeroTime
,
49 nsAString
& aInitiatorType
,
50 nsAString
& aEntryName
);
52 PerformanceTimingData(nsITimedChannel
* aChannel
, nsIHttpChannel
* aHttpChannel
,
53 DOMHighResTimeStamp aZeroTime
);
55 static PerformanceTimingData
* Create(
56 const CacheablePerformanceTimingData
& aCachedData
,
57 DOMHighResTimeStamp aZeroTime
, TimeStamp aStartTime
, TimeStamp aEndTime
,
58 RenderBlockingStatusType aRenderBlockingStatus
);
61 PerformanceTimingData(const CacheablePerformanceTimingData
& aCachedData
,
62 DOMHighResTimeStamp aZeroTime
, TimeStamp aStartTime
,
64 RenderBlockingStatusType aRenderBlockingStatus
);
67 explicit PerformanceTimingData(const IPCPerformanceTimingData
& aIPCData
);
69 IPCPerformanceTimingData
ToIPC();
71 void SetPropertiesFromHttpChannel(nsIHttpChannel
* aHttpChannel
,
72 nsITimedChannel
* aChannel
);
75 void SetTransferSizeFromHttpChannel(nsIHttpChannel
* aHttpChannel
);
78 uint64_t TransferSize() const { return mTransferSize
; }
82 * The TimeStamp recorded for a specific event. This TimeStamp can
84 * @return the duration of an event with a given TimeStamp, relative to the
85 * navigationStart TimeStamp (the moment the user landed on the
86 * page), if the given TimeStamp is valid. Otherwise, it will return
87 * the FetchStart timing value.
89 inline DOMHighResTimeStamp
TimeStampToReducedDOMHighResOrFetchStart(
90 Performance
* aPerformance
, TimeStamp aStamp
) {
91 MOZ_ASSERT(aPerformance
);
93 if (aStamp
.IsNull()) {
94 return FetchStartHighRes(aPerformance
);
97 DOMHighResTimeStamp rawTimestamp
=
98 TimeStampToDOMHighRes(aPerformance
, aStamp
);
100 return nsRFPService::ReduceTimePrecisionAsMSecs(
101 rawTimestamp
, aPerformance
->GetRandomTimelineSeed(),
102 aPerformance
->GetRTPCallerType());
106 * The nsITimedChannel records an absolute timestamp for each event.
107 * The nsDOMNavigationTiming will record the moment when the user landed on
108 * the page. This is a window.performance unique timestamp, so it can be used
109 * for all the events (navigation timing and resource timing events).
111 * The algorithm operates in 2 steps:
112 * 1. The first step is to subtract the two timestamps: the argument (the
113 * event's timestamp) and the navigation start timestamp. This will result in
114 * a relative timestamp of the event (relative to the navigation start -
115 * window.performance.timing.navigationStart).
116 * 2. The second step is to add any required offset (the mZeroTime). For now,
117 * this offset value is either 0 (for the resource timing), or equal to
118 * "performance.navigationStart" (for navigation timing).
119 * For the resource timing, mZeroTime is set to 0, causing the result to be a
121 * For the navigation timing, mZeroTime is set to
122 * "performance.navigationStart" causing the result be an absolute time.
125 * The TimeStamp recorded for a specific event. This TimeStamp can't
127 * @return number of milliseconds value as one of:
128 * - relative to the navigation start time, time the user has landed on the
130 * - an absolute wall clock time since the unix epoch
132 inline DOMHighResTimeStamp
TimeStampToDOMHighRes(Performance
* aPerformance
,
133 TimeStamp aStamp
) const {
134 MOZ_ASSERT(aPerformance
);
135 MOZ_ASSERT(!aStamp
.IsNull());
137 TimeDuration duration
= aStamp
- aPerformance
->CreationTimeStamp();
138 return duration
.ToMilliseconds() + mZeroTime
;
141 // The last channel's AsyncOpen time. This may occur before the FetchStart
143 DOMHighResTimeStamp
AsyncOpenHighRes(Performance
* aPerformance
);
145 // High resolution (used by resource timing)
146 DOMHighResTimeStamp
WorkerStartHighRes(Performance
* aPerformance
);
147 DOMHighResTimeStamp
FetchStartHighRes(Performance
* aPerformance
);
148 DOMHighResTimeStamp
RedirectStartHighRes(Performance
* aPerformance
);
149 DOMHighResTimeStamp
RedirectEndHighRes(Performance
* aPerformance
);
150 DOMHighResTimeStamp
DomainLookupStartHighRes(Performance
* aPerformance
);
151 DOMHighResTimeStamp
DomainLookupEndHighRes(Performance
* aPerformance
);
152 DOMHighResTimeStamp
ConnectStartHighRes(Performance
* aPerformance
);
153 DOMHighResTimeStamp
SecureConnectionStartHighRes(Performance
* aPerformance
);
154 DOMHighResTimeStamp
ConnectEndHighRes(Performance
* aPerformance
);
155 DOMHighResTimeStamp
RequestStartHighRes(Performance
* aPerformance
);
156 DOMHighResTimeStamp
ResponseStartHighRes(Performance
* aPerformance
);
157 DOMHighResTimeStamp
ResponseEndHighRes(Performance
* aPerformance
);
159 DOMHighResTimeStamp
ZeroTime() const { return mZeroTime
; }
161 // If this is false the values of redirectStart/End will be 0 This is false if
162 // no redirects occured, or if any of the responses failed the
163 // timing-allow-origin check in HttpBaseChannel::TimingAllowCheck
165 // If aEnsureSameOriginAndIgnoreTAO is false, it checks if all redirects pass
166 // TAO. When it is true, it checks if all redirects are same-origin and
167 // ignores the result of TAO.
168 bool ShouldReportCrossOriginRedirect(
169 bool aEnsureSameOriginAndIgnoreTAO
) const;
171 RenderBlockingStatusType
RenderBlockingStatus() const {
172 return mRenderBlockingStatus
;
176 TimeStamp mAsyncOpen
;
177 TimeStamp mRedirectStart
;
178 TimeStamp mRedirectEnd
;
179 TimeStamp mDomainLookupStart
;
180 TimeStamp mDomainLookupEnd
;
181 TimeStamp mConnectStart
;
182 TimeStamp mSecureConnectionStart
;
183 TimeStamp mConnectEnd
;
184 TimeStamp mRequestStart
;
185 TimeStamp mResponseStart
;
186 TimeStamp mCacheReadStart
;
187 TimeStamp mResponseEnd
;
188 TimeStamp mCacheReadEnd
;
190 // ServiceWorker interception timing information
191 TimeStamp mWorkerStart
;
192 TimeStamp mWorkerRequestStart
;
193 TimeStamp mWorkerResponseEnd
;
195 // This is an offset that will be added to each timing ([ms] resolution).
196 // There are only 2 possible values: (1) logicaly equal to navigationStart
197 // TimeStamp (results are absolute timstamps - wallclock); (2) "0" (results
198 // are relative to the navigation start).
199 DOMHighResTimeStamp mZeroTime
= 0;
201 DOMHighResTimeStamp mFetchStart
= 0;
203 uint64_t mTransferSize
= 0;
205 RenderBlockingStatusType mRenderBlockingStatus
;
208 // Script "performance.timing" object
209 class PerformanceTiming final
: public nsWrapperCache
{
212 * @param aPerformance
213 * The performance object (the JS parent).
214 * This will allow access to "window.performance.timing" attribute
215 * for the navigation timing (can't be null).
217 * An nsITimedChannel used to gather all the networking timings by
218 * both the navigation timing and the resource timing (can't be null).
219 * @param aHttpChannel
220 * An nsIHttpChannel (the resource's http channel).
221 * This will be used by the resource timing cross-domain check
223 * Argument is null for the navigation timing (navigation timing uses
224 * another algorithm for the cross-domain redirects).
226 * The offset that will be added to the timestamp of each event. This
227 * argument should be equal to performance.navigationStart for
228 * navigation timing and "0" for the resource timing.
230 PerformanceTiming(Performance
* aPerformance
, nsITimedChannel
* aChannel
,
231 nsIHttpChannel
* aHttpChannel
,
232 DOMHighResTimeStamp aZeroTime
);
233 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(PerformanceTiming
)
234 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(PerformanceTiming
)
236 nsDOMNavigationTiming
* GetDOMTiming() const {
237 return mPerformance
->GetDOMTiming();
240 Performance
* GetParentObject() const { return mPerformance
; }
242 virtual JSObject
* WrapObject(JSContext
* cx
,
243 JS::Handle
<JSObject
*> aGivenProto
) override
;
245 // PerformanceNavigation WebIDL methods
246 DOMTimeMilliSec
NavigationStart() const {
247 if (!StaticPrefs::dom_enable_performance()) {
250 return nsRFPService::ReduceTimePrecisionAsMSecs(
251 GetDOMTiming()->GetNavigationStart(),
252 mPerformance
->GetRandomTimelineSeed(),
253 mPerformance
->GetRTPCallerType());
256 DOMTimeMilliSec
UnloadEventStart() {
257 if (!StaticPrefs::dom_enable_performance()) {
260 return nsRFPService::ReduceTimePrecisionAsMSecs(
261 GetDOMTiming()->GetUnloadEventStart(),
262 mPerformance
->GetRandomTimelineSeed(),
263 mPerformance
->GetRTPCallerType());
266 DOMTimeMilliSec
UnloadEventEnd() {
267 if (!StaticPrefs::dom_enable_performance()) {
270 return nsRFPService::ReduceTimePrecisionAsMSecs(
271 GetDOMTiming()->GetUnloadEventEnd(),
272 mPerformance
->GetRandomTimelineSeed(),
273 mPerformance
->GetRTPCallerType());
276 // Low resolution (used by navigation timing)
277 DOMTimeMilliSec
FetchStart();
278 DOMTimeMilliSec
RedirectStart();
279 DOMTimeMilliSec
RedirectEnd();
280 DOMTimeMilliSec
DomainLookupStart();
281 DOMTimeMilliSec
DomainLookupEnd();
282 DOMTimeMilliSec
ConnectStart();
283 DOMTimeMilliSec
SecureConnectionStart();
284 DOMTimeMilliSec
ConnectEnd();
285 DOMTimeMilliSec
RequestStart();
286 DOMTimeMilliSec
ResponseStart();
287 DOMTimeMilliSec
ResponseEnd();
289 DOMTimeMilliSec
DomLoading() {
290 if (!StaticPrefs::dom_enable_performance()) {
293 return nsRFPService::ReduceTimePrecisionAsMSecs(
294 GetDOMTiming()->GetDomLoading(), mPerformance
->GetRandomTimelineSeed(),
295 mPerformance
->GetRTPCallerType());
298 DOMTimeMilliSec
DomInteractive() const {
299 if (!StaticPrefs::dom_enable_performance()) {
302 return nsRFPService::ReduceTimePrecisionAsMSecs(
303 GetDOMTiming()->GetDomInteractive(),
304 mPerformance
->GetRandomTimelineSeed(),
305 mPerformance
->GetRTPCallerType());
308 DOMTimeMilliSec
DomContentLoadedEventStart() const {
309 if (!StaticPrefs::dom_enable_performance()) {
312 return nsRFPService::ReduceTimePrecisionAsMSecs(
313 GetDOMTiming()->GetDomContentLoadedEventStart(),
314 mPerformance
->GetRandomTimelineSeed(),
315 mPerformance
->GetRTPCallerType());
318 DOMTimeMilliSec
DomContentLoadedEventEnd() const {
319 if (!StaticPrefs::dom_enable_performance()) {
322 return nsRFPService::ReduceTimePrecisionAsMSecs(
323 GetDOMTiming()->GetDomContentLoadedEventEnd(),
324 mPerformance
->GetRandomTimelineSeed(),
325 mPerformance
->GetRTPCallerType());
328 DOMTimeMilliSec
DomComplete() const {
329 if (!StaticPrefs::dom_enable_performance()) {
332 return nsRFPService::ReduceTimePrecisionAsMSecs(
333 GetDOMTiming()->GetDomComplete(), mPerformance
->GetRandomTimelineSeed(),
334 mPerformance
->GetRTPCallerType());
337 DOMTimeMilliSec
LoadEventStart() const {
338 if (!StaticPrefs::dom_enable_performance()) {
341 return nsRFPService::ReduceTimePrecisionAsMSecs(
342 GetDOMTiming()->GetLoadEventStart(),
343 mPerformance
->GetRandomTimelineSeed(),
344 mPerformance
->GetRTPCallerType());
347 DOMTimeMilliSec
LoadEventEnd() const {
348 if (!StaticPrefs::dom_enable_performance()) {
351 return nsRFPService::ReduceTimePrecisionAsMSecs(
352 GetDOMTiming()->GetLoadEventEnd(),
353 mPerformance
->GetRandomTimelineSeed(),
354 mPerformance
->GetRTPCallerType());
357 DOMTimeMilliSec
TimeToNonBlankPaint() const {
358 if (!StaticPrefs::dom_enable_performance()) {
361 return nsRFPService::ReduceTimePrecisionAsMSecs(
362 GetDOMTiming()->GetTimeToNonBlankPaint(),
363 mPerformance
->GetRandomTimelineSeed(),
364 mPerformance
->GetRTPCallerType());
367 DOMTimeMilliSec
TimeToContentfulPaint() const {
368 if (!StaticPrefs::dom_enable_performance()) {
371 return nsRFPService::ReduceTimePrecisionAsMSecs(
372 GetDOMTiming()->GetTimeToContentfulComposite(),
373 mPerformance
->GetRandomTimelineSeed(),
374 mPerformance
->GetRTPCallerType());
377 DOMTimeMilliSec
TimeToFirstInteractive() const {
378 if (!StaticPrefs::dom_enable_performance()) {
381 return nsRFPService::ReduceTimePrecisionAsMSecs(
382 GetDOMTiming()->GetTimeToTTFI(), mPerformance
->GetRandomTimelineSeed(),
383 mPerformance
->GetRTPCallerType());
386 PerformanceTimingData
* Data() const { return mTimingData
.get(); }
389 ~PerformanceTiming();
391 bool IsTopLevelContentDocument() const;
393 RefPtr
<Performance
> mPerformance
;
395 UniquePtr
<PerformanceTimingData
> mTimingData
;
398 } // namespace mozilla::dom
400 namespace mozilla::ipc
{
403 struct IPDLParamTraits
<mozilla::dom::PerformanceTimingData
> {
404 using paramType
= mozilla::dom::PerformanceTimingData
;
405 static void Write(IPC::MessageWriter
* aWriter
, IProtocol
* aActor
,
406 const paramType
& aParam
) {
407 WriteIPDLParam(aWriter
, aActor
, aParam
.mServerTiming
);
408 WriteIPDLParam(aWriter
, aActor
, aParam
.mNextHopProtocol
);
409 WriteIPDLParam(aWriter
, aActor
, aParam
.mAsyncOpen
);
410 WriteIPDLParam(aWriter
, aActor
, aParam
.mRedirectStart
);
411 WriteIPDLParam(aWriter
, aActor
, aParam
.mRedirectEnd
);
412 WriteIPDLParam(aWriter
, aActor
, aParam
.mDomainLookupStart
);
413 WriteIPDLParam(aWriter
, aActor
, aParam
.mDomainLookupEnd
);
414 WriteIPDLParam(aWriter
, aActor
, aParam
.mConnectStart
);
415 WriteIPDLParam(aWriter
, aActor
, aParam
.mSecureConnectionStart
);
416 WriteIPDLParam(aWriter
, aActor
, aParam
.mConnectEnd
);
417 WriteIPDLParam(aWriter
, aActor
, aParam
.mRequestStart
);
418 WriteIPDLParam(aWriter
, aActor
, aParam
.mResponseStart
);
419 WriteIPDLParam(aWriter
, aActor
, aParam
.mCacheReadStart
);
420 WriteIPDLParam(aWriter
, aActor
, aParam
.mResponseEnd
);
421 WriteIPDLParam(aWriter
, aActor
, aParam
.mCacheReadEnd
);
422 WriteIPDLParam(aWriter
, aActor
, aParam
.mWorkerStart
);
423 WriteIPDLParam(aWriter
, aActor
, aParam
.mWorkerRequestStart
);
424 WriteIPDLParam(aWriter
, aActor
, aParam
.mWorkerResponseEnd
);
425 WriteIPDLParam(aWriter
, aActor
, aParam
.mZeroTime
);
426 WriteIPDLParam(aWriter
, aActor
, aParam
.mFetchStart
);
427 WriteIPDLParam(aWriter
, aActor
, aParam
.mEncodedBodySize
);
428 WriteIPDLParam(aWriter
, aActor
, aParam
.mTransferSize
);
429 WriteIPDLParam(aWriter
, aActor
, aParam
.mDecodedBodySize
);
430 WriteIPDLParam(aWriter
, aActor
, aParam
.mResponseStatus
);
431 WriteIPDLParam(aWriter
, aActor
, aParam
.mRedirectCount
);
432 WriteIPDLParam(aWriter
, aActor
, aParam
.mContentType
);
433 WriteIPDLParam(aWriter
, aActor
, aParam
.mAllRedirectsSameOrigin
);
434 WriteIPDLParam(aWriter
, aActor
, aParam
.mAllRedirectsPassTAO
);
435 WriteIPDLParam(aWriter
, aActor
, aParam
.mSecureConnection
);
436 WriteIPDLParam(aWriter
, aActor
, aParam
.mBodyInfoAccessAllowed
);
437 WriteIPDLParam(aWriter
, aActor
, aParam
.mTimingAllowed
);
438 WriteIPDLParam(aWriter
, aActor
, aParam
.mInitialized
);
441 static bool Read(IPC::MessageReader
* aReader
, IProtocol
* aActor
,
442 paramType
* aResult
) {
443 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mServerTiming
)) {
446 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mNextHopProtocol
)) {
449 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mAsyncOpen
)) {
452 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mRedirectStart
)) {
455 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mRedirectEnd
)) {
458 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mDomainLookupStart
)) {
461 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mDomainLookupEnd
)) {
464 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mConnectStart
)) {
467 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mSecureConnectionStart
)) {
470 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mConnectEnd
)) {
473 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mRequestStart
)) {
476 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mResponseStart
)) {
479 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mCacheReadStart
)) {
482 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mResponseEnd
)) {
485 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mCacheReadEnd
)) {
488 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mWorkerStart
)) {
491 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mWorkerRequestStart
)) {
494 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mWorkerResponseEnd
)) {
497 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mZeroTime
)) {
500 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mFetchStart
)) {
503 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mEncodedBodySize
)) {
506 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mTransferSize
)) {
509 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mDecodedBodySize
)) {
512 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mResponseStatus
)) {
515 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mRedirectCount
)) {
518 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mContentType
)) {
521 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mAllRedirectsSameOrigin
)) {
524 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mAllRedirectsPassTAO
)) {
527 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mSecureConnection
)) {
530 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mBodyInfoAccessAllowed
)) {
533 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mTimingAllowed
)) {
536 if (!ReadIPDLParam(aReader
, aActor
, &aResult
->mInitialized
)) {
544 struct IPDLParamTraits
<nsIServerTiming
*> {
545 static void Write(IPC::MessageWriter
* aWriter
, IProtocol
* aActor
,
546 nsIServerTiming
* aParam
) {
548 Unused
<< aParam
->GetName(name
);
550 Unused
<< aParam
->GetDuration(&duration
);
551 nsAutoCString description
;
552 Unused
<< aParam
->GetDescription(description
);
553 WriteIPDLParam(aWriter
, aActor
, name
);
554 WriteIPDLParam(aWriter
, aActor
, duration
);
555 WriteIPDLParam(aWriter
, aActor
, description
);
558 static bool Read(IPC::MessageReader
* aReader
, IProtocol
* aActor
,
559 RefPtr
<nsIServerTiming
>* aResult
) {
562 nsAutoCString description
;
563 if (!ReadIPDLParam(aReader
, aActor
, &name
)) {
566 if (!ReadIPDLParam(aReader
, aActor
, &duration
)) {
569 if (!ReadIPDLParam(aReader
, aActor
, &description
)) {
573 RefPtr
<nsServerTiming
> timing
= new nsServerTiming();
574 timing
->SetName(name
);
575 timing
->SetDuration(duration
);
576 timing
->SetDescription(description
);
577 *aResult
= timing
.forget();
582 } // namespace mozilla::ipc
584 #endif // mozilla_dom_PerformanceTiming_h