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_timeout_h
8 #define mozilla_dom_timeout_h
10 #include "mozilla/dom/PopupBlocker.h"
11 #include "mozilla/dom/TimeoutHandler.h"
12 #include "mozilla/LinkedList.h"
13 #include "mozilla/TimeStamp.h"
14 #include "nsGlobalWindowInner.h"
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsTHashMap.h"
17 #include "GeckoProfiler.h"
19 namespace mozilla::dom
{
22 * Timeout struct that holds information about each script
23 * timeout. Holds a strong reference to an nsITimeoutHandler, which
24 * abstracts the language specific cruft.
26 class Timeout final
: protected LinkedListElement
<RefPtr
<Timeout
>> {
30 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(Timeout
)
31 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(Timeout
)
33 enum class Reason
: uint8_t {
37 eDelayedWebTaskTimeout
,
40 struct TimeoutIdAndReason
{
45 class TimeoutHashKey
: public PLDHashEntryHdr
{
47 typedef const TimeoutIdAndReason
& KeyType
;
48 typedef const TimeoutIdAndReason
* KeyTypePointer
;
50 explicit TimeoutHashKey(KeyTypePointer aKey
) : mValue(*aKey
) {}
51 TimeoutHashKey(TimeoutHashKey
&& aOther
)
52 : PLDHashEntryHdr(std::move(aOther
)),
53 mValue(std::move(aOther
.mValue
)) {}
54 ~TimeoutHashKey() = default;
56 KeyType
GetKey() const { return mValue
; }
57 bool KeyEquals(KeyTypePointer aKey
) const {
58 return aKey
->mId
== mValue
.mId
&& aKey
->mReason
== mValue
.mReason
;
61 static KeyTypePointer
KeyToPointer(KeyType aKey
) { return &aKey
; }
62 static PLDHashNumber
HashKey(KeyTypePointer aKey
) {
63 return aKey
->mId
| (static_cast<uint8_t>(aKey
->mReason
) << 31);
65 enum { ALLOW_MEMMOVE
= true };
68 const TimeoutIdAndReason mValue
;
71 class TimeoutSet
: public nsTHashMap
<TimeoutHashKey
, Timeout
*> {
73 NS_INLINE_DECL_REFCOUNTING(TimeoutSet
);
76 ~TimeoutSet() = default;
79 void SetWhenOrTimeRemaining(const TimeStamp
& aBaseTime
,
80 const TimeDuration
& aDelay
);
82 // Can only be called when not frozen.
83 const TimeStamp
& When() const;
85 const TimeStamp
& SubmitTime() const;
87 // Can only be called when frozen.
88 const TimeDuration
& TimeRemaining() const;
90 void SetTimeoutContainer(TimeoutSet
* aTimeouts
) {
91 MOZ_ASSERT(mTimeoutId
!= 0);
92 TimeoutIdAndReason key
= {mTimeoutId
, mReason
};
94 mTimeouts
->Remove(key
);
96 mTimeouts
= aTimeouts
;
98 mTimeouts
->InsertOrUpdate(key
, this);
102 // Override some LinkedListElement methods so that remove()
103 // calls can call SetTimeoutContainer.
104 Timeout
* getNext() { return LinkedListElement
<RefPtr
<Timeout
>>::getNext(); }
106 void setNext(Timeout
* aNext
) {
107 return LinkedListElement
<RefPtr
<Timeout
>>::setNext(aNext
);
110 Timeout
* getPrevious() {
111 return LinkedListElement
<RefPtr
<Timeout
>>::getPrevious();
115 SetTimeoutContainer(nullptr);
116 LinkedListElement
<RefPtr
<Timeout
>>::remove();
119 UniquePtr
<ProfileChunkedBuffer
> TakeProfilerBacktrace() {
120 return std::move(mCause
);
124 // mWhen and mTimeRemaining can't be in a union, sadly, because they
125 // have constructors.
126 // Nominal time to run this timeout. Use only when timeouts are not
130 // Remaining time to wait. Used only when timeouts are frozen.
131 TimeDuration mTimeRemaining
;
133 // Time that the timeout started, restarted, or was frozen. Useful for
134 // logging time from (virtual) start of a timer until the time it fires
135 // (or is cancelled, etc)
136 TimeStamp mSubmitTime
;
138 ~Timeout() { SetTimeoutContainer(nullptr); }
141 // Public member variables in this section. Please don't add to this list
142 // or mix methods with these. The interleaving public/private sections
143 // is necessary as we migrate members to private while still trying to
144 // keep decent binary packing.
146 // Window for which this timeout fires
147 RefPtr
<nsGlobalWindowInner
> mWindow
;
149 // The language-specific information about the callback.
150 RefPtr
<TimeoutHandler
> mScriptHandler
;
152 RefPtr
<TimeoutSet
> mTimeouts
;
155 TimeDuration mInterval
;
157 UniquePtr
<ProfileChunkedBuffer
> mCause
;
159 // Returned as value of setTimeout()
162 // Identifies which firing level this Timeout is being processed in
163 // when sync loops trigger nested firing.
167 int64_t mFiringIndex
;
170 // The popup state at timeout creation time if not created from
172 PopupBlocker::PopupControlState mPopupState
;
174 // Used to allow several reasons for setting a timeout, where each
175 // 'Reason' value is using a possibly overlapping set of id:s.
178 // Between 0 and DOM_CLAMP_TIMEOUT_NESTING_LEVEL. Currently we don't
179 // care about nesting levels beyond that value.
180 uint8_t mNestingLevel
;
182 // True if the timeout was cleared
185 // True if this is one of the timeouts that are currently running
188 // True if this is a repeating/interval timer
192 friend class LinkedList
<RefPtr
<Timeout
>>;
193 friend class LinkedListElement
<RefPtr
<Timeout
>>;
196 } // namespace mozilla::dom
198 #endif // mozilla_dom_timeout_h