Bug 1812499 [wpt PR 38184] - Simplify handling of name-to-subdir mapping in canvas...
[gecko.git] / dom / base / TimeoutExecutor.h
blob991ff8fe7f3b360857f03a4a71711755f5ef298e
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_timeoutexecutor_h
8 #define mozilla_dom_timeoutexecutor_h
10 #include "mozilla/TimeStamp.h"
11 #include "nsCOMPtr.h"
12 #include "nsIRunnable.h"
13 #include "nsITimer.h"
14 #include "nsINamed.h"
16 namespace mozilla::dom {
18 class TimeoutManager;
20 class TimeoutExecutor final : public nsIRunnable,
21 public nsITimerCallback,
22 public nsINamed {
23 TimeoutManager* mOwner;
24 bool mIsIdleQueue;
25 nsCOMPtr<nsITimer> mTimer;
26 TimeStamp mDeadline;
27 uint32_t mMaxIdleDeferMS;
29 // Limits how far we allow timers to fire into the future from their
30 // deadline. Starts off at zero, but is then adjusted when we start
31 // using nsITimer. The nsITimer implementation may sometimes fire
32 // early and we should allow that to minimize additional wakeups.
33 TimeDuration mAllowedEarlyFiringTime;
35 // The TimeoutExecutor is repeatedly scheduled by the TimeoutManager
36 // to fire for the next soonest Timeout. Since the executor is re-used
37 // it needs to handle switching between a few states.
38 enum class Mode {
39 // None indicates the executor is idle. It may be scheduled or shutdown.
40 None,
41 // Immediate means the executor is scheduled to run a Timeout with a
42 // deadline that has already expired.
43 Immediate,
44 // Delayed means the executor is scheduled to run a Timeout with a
45 // deadline in the future.
46 Delayed,
47 // Shutdown means the TimeoutManager has been destroyed. Once this
48 // state is reached the executor cannot be scheduled again. If the
49 // executor is already dispatched as a runnable or held by a timer then
50 // we may still get a Run()/Notify() call which will be ignored.
51 Shutdown
54 Mode mMode;
56 ~TimeoutExecutor();
58 nsresult ScheduleImmediate(const TimeStamp& aDeadline, const TimeStamp& aNow);
60 nsresult ScheduleDelayed(const TimeStamp& aDeadline, const TimeStamp& aNow,
61 const TimeDuration& aMinDelay);
63 nsresult Schedule(const TimeStamp& aDeadline, const TimeDuration& aMinDelay);
65 nsresult MaybeReschedule(const TimeStamp& aDeadline,
66 const TimeDuration& aMinDelay);
68 MOZ_CAN_RUN_SCRIPT void MaybeExecute();
70 public:
71 TimeoutExecutor(TimeoutManager* aOwner, bool aIsIdleQueue,
72 uint32_t aMaxIdleDeferMS);
74 void Shutdown();
76 nsresult MaybeSchedule(const TimeStamp& aDeadline,
77 const TimeDuration& aMinDelay);
79 void Cancel();
81 NS_DECL_ISUPPORTS
82 NS_DECL_NSIRUNNABLE
83 NS_DECL_NSITIMERCALLBACK
84 NS_DECL_NSINAMED
87 } // namespace mozilla::dom
89 #endif // mozilla_dom_timeoutexecutor_h