Bug 1686668 [wpt PR 27185] - Update wpt metadata, a=testonly
[gecko.git] / dom / base / Timeout.h
blob5216dbb12b101c77750d776279846b9294e5068b
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 "nsDataHashtable.h"
18 #ifdef MOZ_GECKO_PROFILER
19 # include "mozilla/ProfileChunkedBuffer.h"
20 #endif
22 namespace mozilla {
23 namespace dom {
26 * Timeout struct that holds information about each script
27 * timeout. Holds a strong reference to an nsITimeoutHandler, which
28 * abstracts the language specific cruft.
30 class Timeout final : protected LinkedListElement<RefPtr<Timeout>> {
31 public:
32 Timeout();
34 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(Timeout)
35 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(Timeout)
37 enum class Reason : uint8_t {
38 eTimeoutOrInterval,
39 eIdleCallbackTimeout,
42 struct TimeoutIdAndReason {
43 uint32_t mId;
44 Reason mReason;
47 class TimeoutHashKey : public PLDHashEntryHdr {
48 public:
49 typedef const TimeoutIdAndReason& KeyType;
50 typedef const TimeoutIdAndReason* KeyTypePointer;
52 explicit TimeoutHashKey(KeyTypePointer aKey) : mValue(*aKey) {}
53 TimeoutHashKey(TimeoutHashKey&& aOther)
54 : PLDHashEntryHdr(std::move(aOther)),
55 mValue(std::move(aOther.mValue)) {}
56 ~TimeoutHashKey() = default;
58 KeyType GetKey() const { return mValue; }
59 bool KeyEquals(KeyTypePointer aKey) const {
60 return aKey->mId == mValue.mId && aKey->mReason == mValue.mReason;
63 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
64 static PLDHashNumber HashKey(KeyTypePointer aKey) {
65 return aKey->mId | (static_cast<uint8_t>(aKey->mReason) << 31);
67 enum { ALLOW_MEMMOVE = true };
69 private:
70 const TimeoutIdAndReason mValue;
73 class TimeoutSet : public nsDataHashtable<TimeoutHashKey, Timeout*> {
74 public:
75 NS_INLINE_DECL_REFCOUNTING(TimeoutSet);
77 private:
78 ~TimeoutSet() = default;
81 void SetWhenOrTimeRemaining(const TimeStamp& aBaseTime,
82 const TimeDuration& aDelay);
84 // Can only be called when not frozen.
85 const TimeStamp& When() const;
87 const TimeStamp& SubmitTime() const;
89 // Can only be called when frozen.
90 const TimeDuration& TimeRemaining() const;
92 void SetTimeoutContainer(TimeoutSet* aTimeouts) {
93 MOZ_ASSERT(mTimeoutId != 0);
94 TimeoutIdAndReason key = {mTimeoutId, mReason};
95 if (mTimeouts) {
96 mTimeouts->Remove(key);
98 mTimeouts = aTimeouts;
99 if (mTimeouts) {
100 mTimeouts->Put(key, this);
104 // Override some LinkedListElement methods so that remove()
105 // calls can call SetTimeoutContainer.
106 Timeout* getNext() { return LinkedListElement<RefPtr<Timeout>>::getNext(); }
108 void setNext(Timeout* aNext) {
109 return LinkedListElement<RefPtr<Timeout>>::setNext(aNext);
112 Timeout* getPrevious() {
113 return LinkedListElement<RefPtr<Timeout>>::getPrevious();
116 void remove() {
117 SetTimeoutContainer(nullptr);
118 LinkedListElement<RefPtr<Timeout>>::remove();
121 #ifdef MOZ_GECKO_PROFILER
122 UniquePtr<ProfileChunkedBuffer> TakeProfilerBacktrace() {
123 return std::move(mCause);
125 #endif
127 private:
128 // mWhen and mTimeRemaining can't be in a union, sadly, because they
129 // have constructors.
130 // Nominal time to run this timeout. Use only when timeouts are not
131 // frozen.
132 TimeStamp mWhen;
134 // Remaining time to wait. Used only when timeouts are frozen.
135 TimeDuration mTimeRemaining;
137 // Time that the timeout started, restarted, or was frozen. Useful for
138 // logging time from (virtual) start of a timer until the time it fires
139 // (or is cancelled, etc)
140 TimeStamp mSubmitTime;
142 ~Timeout() { SetTimeoutContainer(nullptr); }
144 public:
145 // Public member variables in this section. Please don't add to this list
146 // or mix methods with these. The interleaving public/private sections
147 // is necessary as we migrate members to private while still trying to
148 // keep decent binary packing.
150 // Window for which this timeout fires
151 RefPtr<nsGlobalWindowInner> mWindow;
153 // The language-specific information about the callback.
154 RefPtr<TimeoutHandler> mScriptHandler;
156 RefPtr<TimeoutSet> mTimeouts;
158 // Interval
159 TimeDuration mInterval;
161 #ifdef MOZ_GECKO_PROFILER
162 UniquePtr<ProfileChunkedBuffer> mCause;
163 #endif
165 // Returned as value of setTimeout()
166 uint32_t mTimeoutId;
168 // Identifies which firing level this Timeout is being processed in
169 // when sync loops trigger nested firing.
170 uint32_t mFiringId;
172 #ifdef DEBUG
173 int64_t mFiringIndex;
174 #endif
176 // The popup state at timeout creation time if not created from
177 // another timeout
178 PopupBlocker::PopupControlState mPopupState;
180 // Used to allow several reasons for setting a timeout, where each
181 // 'Reason' value is using a possibly overlapping set of id:s.
182 Reason mReason;
184 // Between 0 and DOM_CLAMP_TIMEOUT_NESTING_LEVEL. Currently we don't
185 // care about nesting levels beyond that value.
186 uint8_t mNestingLevel;
188 // True if the timeout was cleared
189 bool mCleared;
191 // True if this is one of the timeouts that are currently running
192 bool mRunning;
194 // True if this is a repeating/interval timer
195 bool mIsInterval;
197 protected:
198 friend class LinkedList<RefPtr<Timeout>>;
199 friend class LinkedListElement<RefPtr<Timeout>>;
202 } // namespace dom
203 } // namespace mozilla
205 #endif // mozilla_dom_timeout_h