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_PerformanceEventTiming_h___
8 #define mozilla_dom_PerformanceEventTiming_h___
10 #include "mozilla/dom/PerformanceEntry.h"
11 #include "mozilla/EventForwards.h"
12 #include "nsRFPService.h"
13 #include "Performance.h"
14 #include "nsIWeakReferenceUtils.h"
21 class PerformanceEventTiming final
22 : public PerformanceEntry
,
23 public LinkedListElement
<RefPtr
<PerformanceEventTiming
>> {
25 NS_DECL_ISUPPORTS_INHERITED
27 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PerformanceEventTiming
,
30 static already_AddRefed
<PerformanceEventTiming
> TryGenerateEventTiming(
31 const EventTarget
* aTarget
, const WidgetEvent
* aEvent
);
33 already_AddRefed
<PerformanceEventTiming
> Clone() {
34 RefPtr
<PerformanceEventTiming
> eventTiming
=
35 new PerformanceEventTiming(*this);
36 return eventTiming
.forget();
39 JSObject
* WrapObject(JSContext
* cx
,
40 JS::Handle
<JSObject
*> aGivenProto
) override
;
42 DOMHighResTimeStamp
ProcessingStart() const {
43 if (mCachedProcessingStart
.isNothing()) {
44 mCachedProcessingStart
.emplace(nsRFPService::ReduceTimePrecisionAsMSecs(
45 mProcessingStart
, mPerformance
->GetRandomTimelineSeed(),
46 mPerformance
->GetRTPCallerType()));
48 return mCachedProcessingStart
.value();
51 DOMHighResTimeStamp
ProcessingEnd() const {
52 if (mCachedProcessingEnd
.isNothing()) {
53 mCachedProcessingEnd
.emplace(nsRFPService::ReduceTimePrecisionAsMSecs(
54 mProcessingEnd
, mPerformance
->GetRandomTimelineSeed(),
55 mPerformance
->GetRTPCallerType()));
57 return mCachedProcessingEnd
.value();
60 bool Cancelable() const { return mCancelable
; }
62 nsINode
* GetTarget() const;
64 void SetDuration(const DOMHighResTimeStamp aDuration
) {
65 mDuration
= aDuration
;
68 // nsRFPService::ReduceTimePrecisionAsMSecs might causes
69 // some memory overhead, using the raw timestamp internally
70 // to avoid calling in unnecessarily.
71 DOMHighResTimeStamp
RawDuration() const { return mDuration
; }
73 DOMHighResTimeStamp
Duration() const override
{
74 if (mCachedDuration
.isNothing()) {
75 mCachedDuration
.emplace(nsRFPService::ReduceTimePrecisionAsMSecs(
76 mDuration
, mPerformance
->GetRandomTimelineSeed(),
77 mPerformance
->GetRTPCallerType()));
79 return mCachedDuration
.value();
82 // Similar as RawDuration; Used to avoid calling
83 // nsRFPService::ReduceTimePrecisionAsMSecs unnecessarily.
84 DOMHighResTimeStamp
RawStartTime() const { return mStartTime
; }
86 DOMHighResTimeStamp
StartTime() const override
{
87 if (mCachedStartTime
.isNothing()) {
88 mCachedStartTime
.emplace(nsRFPService::ReduceTimePrecisionAsMSecs(
89 mStartTime
, mPerformance
->GetRandomTimelineSeed(),
90 mPerformance
->GetRTPCallerType()));
92 return mCachedStartTime
.value();
95 bool ShouldAddEntryToBuffer(double aDuration
) const;
96 bool ShouldAddEntryToObserverBuffer(PerformanceObserverInit
&) const override
;
98 void BufferEntryIfNeeded() override
;
100 void FinalizeEventTiming(EventTarget
* aTarget
);
102 EventMessage
GetMessage() const { return mMessage
; }
105 PerformanceEventTiming(Performance
* aPerformance
, const nsAString
& aName
,
106 const TimeStamp
& aStartTime
, bool aIsCacelable
,
107 EventMessage aMessage
);
109 PerformanceEventTiming(const PerformanceEventTiming
& aEventTimingEntry
);
111 ~PerformanceEventTiming() = default;
113 RefPtr
<Performance
> mPerformance
;
115 DOMHighResTimeStamp mProcessingStart
;
116 mutable Maybe
<DOMHighResTimeStamp
> mCachedProcessingStart
;
118 DOMHighResTimeStamp mProcessingEnd
;
119 mutable Maybe
<DOMHighResTimeStamp
> mCachedProcessingEnd
;
123 DOMHighResTimeStamp mStartTime
;
124 mutable Maybe
<DOMHighResTimeStamp
> mCachedStartTime
;
126 DOMHighResTimeStamp mDuration
;
127 mutable Maybe
<DOMHighResTimeStamp
> mCachedDuration
;
131 EventMessage mMessage
;
134 } // namespace mozilla