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"
20 // http://www.w3.org/TR/resource-timing/#performanceresourcetiming
21 class PerformanceResourceTiming
: public PerformanceEntry
{
23 using TimeStamp
= mozilla::TimeStamp
;
25 NS_DECL_ISUPPORTS_INHERITED
26 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
27 PerformanceResourceTiming
, PerformanceEntry
)
29 // aPerformanceTimingData and aPerformance must be non-null
30 PerformanceResourceTiming(
31 UniquePtr
<PerformanceTimingData
>&& aPerformanceTimingData
,
32 Performance
* aPerformance
, const nsAString
& aName
);
34 virtual JSObject
* WrapObject(JSContext
* aCx
,
35 JS::Handle
<JSObject
*> aGivenProto
) override
;
37 virtual DOMHighResTimeStamp
StartTime() const override
;
39 virtual DOMHighResTimeStamp
Duration() const override
{
40 return ResponseEnd() - StartTime();
43 void GetInitiatorType(nsAString
& aInitiatorType
) const {
44 aInitiatorType
= mInitiatorType
;
47 void SetInitiatorType(const nsAString
& aInitiatorType
) {
48 mInitiatorType
= aInitiatorType
;
51 void GetNextHopProtocol(nsAString
& aNextHopProtocol
) const {
52 aNextHopProtocol
= mTimingData
->NextHopProtocol();
55 DOMHighResTimeStamp
WorkerStart() const {
56 return mTimingData
->WorkerStartHighRes(mPerformance
);
59 DOMHighResTimeStamp
FetchStart() const {
60 return mTimingData
->FetchStartHighRes(mPerformance
);
63 DOMHighResTimeStamp
RedirectStart(Maybe
<nsIPrincipal
*>& aSubjectPrincipal
,
64 bool aEnsureSameOriginAndIgnoreTAO
) const {
65 // We have to check if all the redirect URIs whether had the same origin or
66 // different origins with TAO headers set (since there is no check in
67 // RedirectStartHighRes())
68 return ReportRedirectForCaller(aSubjectPrincipal
,
69 aEnsureSameOriginAndIgnoreTAO
)
70 ? mTimingData
->RedirectStartHighRes(mPerformance
)
74 virtual DOMHighResTimeStamp
RedirectStart(
75 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
76 return RedirectStart(aSubjectPrincipal
,
77 false /* aEnsureSameOriginAndIgnoreTAO */);
80 DOMHighResTimeStamp
RedirectEnd(Maybe
<nsIPrincipal
*>& aSubjectPrincipal
,
81 bool aEnsureSameOriginAndIgnoreTAO
) const {
82 // We have to check if all the redirect URIs whether had the same origin or
83 // different origins with TAO headers set (since there is no check in
84 // RedirectEndHighRes())
85 return ReportRedirectForCaller(aSubjectPrincipal
,
86 aEnsureSameOriginAndIgnoreTAO
)
87 ? mTimingData
->RedirectEndHighRes(mPerformance
)
91 virtual DOMHighResTimeStamp
RedirectEnd(
92 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
93 return RedirectEnd(aSubjectPrincipal
,
94 false /* aEnsureSameOriginAndIgnoreTAO */);
97 DOMHighResTimeStamp
DomainLookupStart(
98 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
99 return TimingAllowedForCaller(aSubjectPrincipal
)
100 ? mTimingData
->DomainLookupStartHighRes(mPerformance
)
104 DOMHighResTimeStamp
DomainLookupEnd(
105 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
106 return TimingAllowedForCaller(aSubjectPrincipal
)
107 ? mTimingData
->DomainLookupEndHighRes(mPerformance
)
111 DOMHighResTimeStamp
ConnectStart(
112 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
113 return TimingAllowedForCaller(aSubjectPrincipal
)
114 ? mTimingData
->ConnectStartHighRes(mPerformance
)
118 DOMHighResTimeStamp
ConnectEnd(
119 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
120 return TimingAllowedForCaller(aSubjectPrincipal
)
121 ? mTimingData
->ConnectEndHighRes(mPerformance
)
125 DOMHighResTimeStamp
RequestStart(
126 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
127 return TimingAllowedForCaller(aSubjectPrincipal
)
128 ? mTimingData
->RequestStartHighRes(mPerformance
)
132 DOMHighResTimeStamp
ResponseStart(
133 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
134 return TimingAllowedForCaller(aSubjectPrincipal
)
135 ? mTimingData
->ResponseStartHighRes(mPerformance
)
139 DOMHighResTimeStamp
ResponseEnd() const {
140 return mTimingData
->ResponseEndHighRes(mPerformance
);
143 DOMHighResTimeStamp
SecureConnectionStart(
144 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
145 return TimingAllowedForCaller(aSubjectPrincipal
)
146 ? mTimingData
->SecureConnectionStartHighRes(mPerformance
)
150 virtual const PerformanceResourceTiming
* ToResourceTiming() const override
{
154 uint64_t TransferSize(Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
155 return TimingAllowedForCaller(aSubjectPrincipal
)
156 ? mTimingData
->TransferSize()
160 uint64_t EncodedBodySize(Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
161 return TimingAllowedForCaller(aSubjectPrincipal
)
162 ? mTimingData
->EncodedBodySize()
166 uint64_t DecodedBodySize(Maybe
<nsIPrincipal
*>& aSubjectPrincipal
) const {
167 return TimingAllowedForCaller(aSubjectPrincipal
)
168 ? mTimingData
->DecodedBodySize()
172 void GetServerTiming(nsTArray
<RefPtr
<PerformanceServerTiming
>>& aRetval
,
173 Maybe
<nsIPrincipal
*>& aSubjectPrincipal
);
175 size_t SizeOfIncludingThis(
176 mozilla::MallocSizeOf aMallocSizeOf
) const override
;
179 virtual ~PerformanceResourceTiming();
181 size_t SizeOfExcludingThis(
182 mozilla::MallocSizeOf aMallocSizeOf
) const override
;
184 // Check if caller has access to cross-origin timings, either by the rules
185 // from the spec, or based on addon permissions.
186 bool TimingAllowedForCaller(Maybe
<nsIPrincipal
*>& aCaller
) const;
188 // Check if cross-origin redirects should be reported to the caller.
189 bool ReportRedirectForCaller(Maybe
<nsIPrincipal
*>& aCaller
,
190 bool aEnsureSameOriginAndIgnoreTAO
) const;
192 nsString mInitiatorType
;
193 const UniquePtr
<PerformanceTimingData
> mTimingData
; // always non-null
194 RefPtr
<Performance
> mPerformance
;
196 // The same initial requested URI as the `name` attribute.
197 nsCOMPtr
<nsIURI
> mOriginalURI
;
200 mutable Maybe
<DOMHighResTimeStamp
> mCachedStartTime
;
204 } // namespace mozilla
206 #endif /* mozilla_dom_PerformanceResourceTiming_h___ */