Bug 1660051 [wpt PR 25111] - Origin isolation: expand getter test coverage, a=testonly
[gecko.git] / dom / base / Timeout.h
blob6ef0c85f64519002b43ff3b322a3b39e7319c03c
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 "nsCOMPtr.h"
15 #include "nsGlobalWindowInner.h"
16 #include "nsCycleCollectionParticipant.h"
17 #include "GeckoProfiler.h"
18 #include "nsDataHashtable.h"
20 class nsIEventTarget;
21 class nsIPrincipal;
22 class nsIEventTarget;
24 namespace mozilla {
25 namespace dom {
28 * Timeout struct that holds information about each script
29 * timeout. Holds a strong reference to an nsITimeoutHandler, which
30 * abstracts the language specific cruft.
32 class Timeout final : protected LinkedListElement<RefPtr<Timeout>> {
33 public:
34 Timeout();
36 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(Timeout)
37 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(Timeout)
39 enum class Reason : uint8_t {
40 eTimeoutOrInterval,
41 eIdleCallbackTimeout,
44 struct TimeoutIdAndReason {
45 uint32_t mId;
46 Reason mReason;
49 class TimeoutHashKey : public PLDHashEntryHdr {
50 public:
51 typedef const TimeoutIdAndReason& KeyType;
52 typedef const TimeoutIdAndReason* KeyTypePointer;
54 explicit TimeoutHashKey(KeyTypePointer aKey) : mValue(*aKey) {}
55 TimeoutHashKey(TimeoutHashKey&& aOther)
56 : PLDHashEntryHdr(std::move(aOther)),
57 mValue(std::move(aOther.mValue)) {}
58 ~TimeoutHashKey() = default;
60 KeyType GetKey() const { return mValue; }
61 bool KeyEquals(KeyTypePointer aKey) const {
62 return aKey->mId == mValue.mId && aKey->mReason == mValue.mReason;
65 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
66 static PLDHashNumber HashKey(KeyTypePointer aKey) {
67 return aKey->mId | (static_cast<uint8_t>(aKey->mReason) << 31);
69 enum { ALLOW_MEMMOVE = true };
71 private:
72 const TimeoutIdAndReason mValue;
75 class TimeoutSet : public nsDataHashtable<TimeoutHashKey, Timeout*> {
76 public:
77 NS_INLINE_DECL_REFCOUNTING(TimeoutSet);
79 private:
80 ~TimeoutSet() = default;
83 void SetWhenOrTimeRemaining(const TimeStamp& aBaseTime,
84 const TimeDuration& aDelay);
86 // Can only be called when not frozen.
87 const TimeStamp& When() const;
89 const TimeStamp& SubmitTime() const;
91 // Can only be called when frozen.
92 const TimeDuration& TimeRemaining() const;
94 void SetTimeoutContainer(TimeoutSet* aTimeouts) {
95 MOZ_ASSERT(mTimeoutId != 0);
96 TimeoutIdAndReason key = {mTimeoutId, mReason};
97 if (mTimeouts) {
98 mTimeouts->Remove(key);
100 mTimeouts = aTimeouts;
101 if (mTimeouts) {
102 mTimeouts->Put(key, this);
106 // Override some LinkedListElement methods so that remove()
107 // calls can call SetTimeoutContainer.
108 Timeout* getNext() { return LinkedListElement<RefPtr<Timeout>>::getNext(); }
110 void setNext(Timeout* aNext) {
111 return LinkedListElement<RefPtr<Timeout>>::setNext(aNext);
114 Timeout* getPrevious() {
115 return LinkedListElement<RefPtr<Timeout>>::getPrevious();
118 void remove() {
119 SetTimeoutContainer(nullptr);
120 LinkedListElement<RefPtr<Timeout>>::remove();
123 #ifdef MOZ_GECKO_PROFILER
124 UniqueProfilerBacktrace TakeProfilerBacktrace() { 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 UniqueProfilerBacktrace 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