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_Performance_h
8 #define mozilla_dom_Performance_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/DOMEventTargetHelper.h"
13 #include "nsContentUtils.h"
14 #include "nsDOMNavigationTiming.h"
15 #include "nsTObserverArray.h"
17 class nsITimedChannel
;
25 class OwningStringOrDouble
;
26 class StringOrPerformanceMeasureOptions
;
27 class PerformanceEntry
;
28 class PerformanceMark
;
29 struct PerformanceMarkOptions
;
30 struct PerformanceMeasureOptions
;
31 class PerformanceMeasure
;
32 class PerformanceNavigation
;
33 class PerformancePaintTiming
;
34 class PerformanceObserver
;
35 class PerformanceService
;
36 class PerformanceStorage
;
37 class PerformanceTiming
;
38 class PerformanceEventTiming
;
39 class WorkerGlobalScope
;
42 // Base class for main-thread and worker Performance API
43 class Performance
: public DOMEventTargetHelper
{
45 NS_DECL_ISUPPORTS_INHERITED
46 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Performance
, DOMEventTargetHelper
)
48 static bool IsObserverEnabled(JSContext
* aCx
, JSObject
* aGlobal
);
50 static already_AddRefed
<Performance
> CreateForMainThread(
51 nsPIDOMWindowInner
* aWindow
, nsIPrincipal
* aPrincipal
,
52 nsDOMNavigationTiming
* aDOMTiming
, nsITimedChannel
* aChannel
);
54 static already_AddRefed
<Performance
> CreateForWorker(
55 WorkerGlobalScope
* aGlobalScope
);
57 // This will return nullptr if called outside of a Window or Worker.
58 static already_AddRefed
<Performance
> Get(JSContext
* aCx
,
59 nsIGlobalObject
* aGlobal
);
61 JSObject
* WrapObject(JSContext
* cx
,
62 JS::Handle
<JSObject
*> aGivenProto
) override
;
64 virtual void GetEntries(nsTArray
<RefPtr
<PerformanceEntry
>>& aRetval
);
66 virtual void GetEntriesByType(const nsAString
& aEntryType
,
67 nsTArray
<RefPtr
<PerformanceEntry
>>& aRetval
);
69 virtual void GetEntriesByTypeForObserver(
70 const nsAString
& aEntryType
, nsTArray
<RefPtr
<PerformanceEntry
>>& aRetval
);
72 virtual void GetEntriesByName(const nsAString
& aName
,
73 const Optional
<nsAString
>& aEntryType
,
74 nsTArray
<RefPtr
<PerformanceEntry
>>& aRetval
);
76 virtual PerformanceStorage
* AsPerformanceStorage() = 0;
78 void ClearResourceTimings();
80 DOMHighResTimeStamp
Now();
82 DOMHighResTimeStamp
NowUnclamped() const;
84 DOMHighResTimeStamp
TimeOrigin();
86 already_AddRefed
<PerformanceMark
> Mark(
87 JSContext
* aCx
, const nsAString
& aName
,
88 const PerformanceMarkOptions
& aMarkOptions
, ErrorResult
& aRv
);
90 void ClearMarks(const Optional
<nsAString
>& aName
);
92 already_AddRefed
<PerformanceMeasure
> Measure(
93 JSContext
* aCx
, const nsAString
& aName
,
94 const StringOrPerformanceMeasureOptions
& aStartOrMeasureOptions
,
95 const Optional
<nsAString
>& aEndMark
, ErrorResult
& aRv
);
97 void ClearMeasures(const Optional
<nsAString
>& aName
);
99 void SetResourceTimingBufferSize(uint64_t aMaxSize
);
101 void AddObserver(PerformanceObserver
* aObserver
);
102 void RemoveObserver(PerformanceObserver
* aObserver
);
103 MOZ_CAN_RUN_SCRIPT
void NotifyObservers();
104 void CancelNotificationObservers();
106 virtual PerformanceTiming
* Timing() = 0;
108 virtual PerformanceNavigation
* Navigation() = 0;
110 virtual void SetFCPTimingEntry(PerformancePaintTiming
* aEntry
) = 0;
112 IMPL_EVENT_HANDLER(resourcetimingbufferfull
)
114 virtual void GetMozMemory(JSContext
* aCx
,
115 JS::MutableHandle
<JSObject
*> aObj
) = 0;
117 virtual nsDOMNavigationTiming
* GetDOMTiming() const = 0;
119 virtual nsITimedChannel
* GetChannel() const = 0;
121 virtual TimeStamp
CreationTimeStamp() const = 0;
123 RTPCallerType
GetRTPCallerType() const { return mRTPCallerType
; }
125 bool CrossOriginIsolated() const { return mCrossOriginIsolated
; }
126 bool ShouldResistFingerprinting() const {
127 return mShouldResistFingerprinting
;
130 DOMHighResTimeStamp
TimeStampToDOMHighResForRendering(TimeStamp
) const;
132 virtual uint64_t GetRandomTimelineSeed() = 0;
134 void MemoryPressure();
136 size_t SizeOfUserEntries(mozilla::MallocSizeOf aMallocSizeOf
) const;
137 size_t SizeOfResourceEntries(mozilla::MallocSizeOf aMallocSizeOf
) const;
138 virtual size_t SizeOfEventEntries(mozilla::MallocSizeOf aMallocSizeOf
) const {
142 void InsertResourceEntry(PerformanceEntry
* aEntry
);
144 virtual void InsertEventTimingEntry(PerformanceEventTiming
* aEntry
) = 0;
146 virtual void BufferEventTimingEntryIfNeeded(
147 PerformanceEventTiming
* aEntry
) = 0;
149 virtual class EventCounts
* EventCounts() = 0;
151 virtual void QueueNavigationTimingEntry() = 0;
153 virtual void UpdateNavigationTimingEntry() = 0;
155 virtual void DispatchPendingEventTimingEntries() = 0;
157 void QueueNotificationObserversTask();
159 bool IsPerformanceTimingAttribute(const nsAString
& aName
) const;
161 virtual bool IsGlobalObjectWindow() const { return false; };
164 Performance(nsIGlobalObject
* aGlobal
);
165 Performance(nsPIDOMWindowInner
* aWindow
);
167 virtual ~Performance();
169 virtual void InsertUserEntry(PerformanceEntry
* aEntry
);
171 void ClearUserEntries(const Optional
<nsAString
>& aEntryName
,
172 const nsAString
& aEntryType
);
174 virtual void DispatchBufferFullEvent() = 0;
176 virtual DOMHighResTimeStamp
CreationTime() const = 0;
178 virtual DOMHighResTimeStamp
GetPerformanceTimingFromString(
179 const nsAString
& aTimingName
) {
183 void LogEntry(PerformanceEntry
* aEntry
, const nsACString
& aOwner
) const;
184 void TimingNotification(PerformanceEntry
* aEntry
, const nsACString
& aOwner
,
185 const double aEpoch
);
187 void RunNotificationObserversTask();
188 void QueueEntry(PerformanceEntry
* aEntry
);
190 nsTObserverArray
<RefPtr
<PerformanceObserver
>> mObservers
;
193 static const uint64_t kDefaultResourceTimingBufferSize
= 250;
195 // When kDefaultResourceTimingBufferSize is increased or removed, these should
196 // be changed to use SegmentedVector
197 AutoTArray
<RefPtr
<PerformanceEntry
>, kDefaultResourceTimingBufferSize
>
199 AutoTArray
<RefPtr
<PerformanceEntry
>, kDefaultResourceTimingBufferSize
>
201 AutoTArray
<RefPtr
<PerformanceEntry
>, kDefaultResourceTimingBufferSize
>
202 mSecondaryResourceEntries
;
204 uint64_t mResourceTimingBufferSize
;
205 bool mPendingNotificationObserversTask
;
207 bool mPendingResourceTimingBufferFullEvent
;
209 RefPtr
<PerformanceService
> mPerformanceService
;
211 const RTPCallerType mRTPCallerType
;
212 const bool mCrossOriginIsolated
;
213 const bool mShouldResistFingerprinting
;
216 MOZ_ALWAYS_INLINE
bool CanAddResourceTimingEntry();
218 void MaybeEmitExternalProfilerMarker(
219 const nsAString
& aName
, Maybe
<const PerformanceMeasureOptions
&> aOptions
,
220 Maybe
<const nsAString
&> aStartMark
, const Optional
<nsAString
>& aEndMark
);
222 // The attributes of a PerformanceMeasureOptions that we call
223 // ResolveTimestamp* on.
224 enum class ResolveTimestampAttribute
;
226 DOMHighResTimeStamp
ConvertMarkToTimestampWithString(const nsAString
& aName
,
228 bool aReturnUnclamped
);
229 DOMHighResTimeStamp
ConvertMarkToTimestampWithDOMHighResTimeStamp(
230 const ResolveTimestampAttribute aAttribute
, const double aTimestamp
,
232 DOMHighResTimeStamp
ConvertMarkToTimestamp(
233 const ResolveTimestampAttribute aAttribute
,
234 const OwningStringOrDouble
& aMarkNameOrTimestamp
, ErrorResult
& aRv
,
235 bool aReturnUnclamped
);
237 DOMHighResTimeStamp
ConvertNameToTimestamp(const nsAString
& aName
,
240 DOMHighResTimeStamp
ResolveEndTimeForMeasure(
241 const Optional
<nsAString
>& aEndMark
,
242 const Maybe
<const PerformanceMeasureOptions
&>& aOptions
, ErrorResult
& aRv
,
243 bool aReturnUnclamped
);
244 DOMHighResTimeStamp
ResolveStartTimeForMeasure(
245 const Maybe
<const nsAString
&>& aStartMark
,
246 const Maybe
<const PerformanceMeasureOptions
&>& aOptions
, ErrorResult
& aRv
,
247 bool aReturnUnclamped
);
249 std::pair
<TimeStamp
, TimeStamp
> GetTimeStampsForMarker(
250 const Maybe
<const nsAString
&>& aStartMark
,
251 const Optional
<nsAString
>& aEndMark
,
252 const Maybe
<const PerformanceMeasureOptions
&>& aOptions
,
257 } // namespace mozilla
259 #endif // mozilla_dom_Performance_h