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_PerformanceResourceTiming_h___
8 #define mozilla_dom_PerformanceResourceTiming_h___
10 #include "mozilla/UniquePtr.h"
12 #include "Performance.h"
13 #include "PerformanceEntry.h"
14 #include "PerformanceServerTiming.h"
15 #include "PerformanceTiming.h"
17 namespace mozilla::dom
{
18 #define IMPL_RESOURCE_TIMING_TAO_PROTECTED_TIMING_PROP(name) \
19 DOMHighResTimeStamp name(nsIPrincipal& aSubjectPrincipal) const { \
20 bool allowed = !mTimingData->RedirectCountReal() \
21 ? TimingAllowedForCaller(aSubjectPrincipal) \
22 : ReportRedirectForCaller(aSubjectPrincipal, false); \
23 return allowed ? mTimingData->name##HighRes(mPerformance) : 0; \
26 #define IMPL_RESOURCE_TIMING_TAO_PROTECTED_SIZE_PROP(name) \
27 uint64_t name(nsIPrincipal& aSubjectPrincipal) const { \
28 bool allowed = !mTimingData->RedirectCountReal() \
29 ? TimingAllowedForCaller(aSubjectPrincipal) \
30 : ReportRedirectForCaller(aSubjectPrincipal, false); \
31 return allowed ? mTimingData->name() : 0; \
34 // http://www.w3.org/TR/resource-timing/#performanceresourcetiming
35 class PerformanceResourceTiming
: public PerformanceEntry
{
37 using TimeStamp
= mozilla::TimeStamp
;
39 NS_DECL_ISUPPORTS_INHERITED
40 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
41 PerformanceResourceTiming
, PerformanceEntry
)
43 // aPerformanceTimingData and aPerformance must be non-null
44 PerformanceResourceTiming(
45 UniquePtr
<PerformanceTimingData
>&& aPerformanceTimingData
,
46 Performance
* aPerformance
, const nsAString
& aName
);
48 virtual JSObject
* WrapObject(JSContext
* aCx
,
49 JS::Handle
<JSObject
*> aGivenProto
) override
;
51 virtual DOMHighResTimeStamp
StartTime() const override
;
53 virtual DOMHighResTimeStamp
Duration() const override
{
54 return ResponseEnd() - StartTime();
57 void GetInitiatorType(nsAString
& aInitiatorType
) const {
58 aInitiatorType
= mInitiatorType
;
61 void SetInitiatorType(const nsAString
& aInitiatorType
) {
62 mInitiatorType
= aInitiatorType
;
65 void GetNextHopProtocol(nsAString
& aNextHopProtocol
) const {
66 if (mTimingData
->TimingAllowed()) {
67 aNextHopProtocol
= mTimingData
->NextHopProtocol();
71 DOMHighResTimeStamp
WorkerStart() const {
72 return mTimingData
->WorkerStartHighRes(mPerformance
);
75 DOMHighResTimeStamp
FetchStart() const;
77 DOMHighResTimeStamp
RedirectStart(nsIPrincipal
& aSubjectPrincipal
,
78 bool aEnsureSameOriginAndIgnoreTAO
) const {
79 // We have to check if all the redirect URIs whether had the same origin or
80 // different origins with TAO headers set (since there is no check in
81 // RedirectStartHighRes())
82 return ReportRedirectForCaller(aSubjectPrincipal
,
83 aEnsureSameOriginAndIgnoreTAO
)
84 ? mTimingData
->RedirectStartHighRes(mPerformance
)
88 virtual DOMHighResTimeStamp
RedirectStart(
89 nsIPrincipal
& aSubjectPrincipal
) const {
90 return RedirectStart(aSubjectPrincipal
,
91 false /* aEnsureSameOriginAndIgnoreTAO */);
94 DOMHighResTimeStamp
RedirectEnd(nsIPrincipal
& aSubjectPrincipal
,
95 bool aEnsureSameOriginAndIgnoreTAO
) const {
96 // We have to check if all the redirect URIs whether had the same origin or
97 // different origins with TAO headers set (since there is no check in
98 // RedirectEndHighRes())
99 return ReportRedirectForCaller(aSubjectPrincipal
,
100 aEnsureSameOriginAndIgnoreTAO
)
101 ? mTimingData
->RedirectEndHighRes(mPerformance
)
105 virtual DOMHighResTimeStamp
RedirectEnd(
106 nsIPrincipal
& aSubjectPrincipal
) const {
107 return RedirectEnd(aSubjectPrincipal
,
108 false /* aEnsureSameOriginAndIgnoreTAO */);
111 IMPL_RESOURCE_TIMING_TAO_PROTECTED_TIMING_PROP(DomainLookupStart
)
113 IMPL_RESOURCE_TIMING_TAO_PROTECTED_TIMING_PROP(DomainLookupEnd
)
115 IMPL_RESOURCE_TIMING_TAO_PROTECTED_TIMING_PROP(ConnectStart
)
117 IMPL_RESOURCE_TIMING_TAO_PROTECTED_TIMING_PROP(ConnectEnd
)
119 IMPL_RESOURCE_TIMING_TAO_PROTECTED_TIMING_PROP(RequestStart
)
121 IMPL_RESOURCE_TIMING_TAO_PROTECTED_TIMING_PROP(ResponseStart
)
123 DOMHighResTimeStamp
ResponseEnd() const {
124 return mTimingData
->ResponseEndHighRes(mPerformance
);
127 IMPL_RESOURCE_TIMING_TAO_PROTECTED_TIMING_PROP(SecureConnectionStart
)
129 virtual const PerformanceResourceTiming
* ToResourceTiming() const override
{
133 IMPL_RESOURCE_TIMING_TAO_PROTECTED_SIZE_PROP(TransferSize
)
135 IMPL_RESOURCE_TIMING_TAO_PROTECTED_SIZE_PROP(EncodedBodySize
)
137 IMPL_RESOURCE_TIMING_TAO_PROTECTED_SIZE_PROP(DecodedBodySize
)
139 void GetServerTiming(nsTArray
<RefPtr
<PerformanceServerTiming
>>& aRetval
,
140 nsIPrincipal
& aSubjectPrincipal
);
142 size_t SizeOfIncludingThis(
143 mozilla::MallocSizeOf aMallocSizeOf
) const override
;
146 virtual ~PerformanceResourceTiming();
148 size_t SizeOfExcludingThis(
149 mozilla::MallocSizeOf aMallocSizeOf
) const override
;
151 // Check if caller has access to cross-origin timings, either by the rules
152 // from the spec, or based on addon permissions.
153 bool TimingAllowedForCaller(nsIPrincipal
& aCaller
) const;
155 // Check if cross-origin redirects should be reported to the caller.
156 bool ReportRedirectForCaller(nsIPrincipal
& aCaller
,
157 bool aEnsureSameOriginAndIgnoreTAO
) const;
159 nsString mInitiatorType
;
160 const UniquePtr
<PerformanceTimingData
> mTimingData
; // always non-null
161 RefPtr
<Performance
> mPerformance
;
163 // The same initial requested URI as the `name` attribute.
164 nsCOMPtr
<nsIURI
> mOriginalURI
;
167 mutable Maybe
<DOMHighResTimeStamp
> mCachedStartTime
;
170 } // namespace mozilla::dom
172 #endif /* mozilla_dom_PerformanceResourceTiming_h___ */