TimeoutExecutor.h (2964B)
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/. */ 6 7 #ifndef mozilla_dom_timeoutexecutor_h 8 #define mozilla_dom_timeoutexecutor_h 9 10 #include "mozilla/TimeStamp.h" 11 #include "nsCOMPtr.h" 12 #include "nsINamed.h" 13 #include "nsIRunnable.h" 14 #include "nsITimer.h" 15 16 namespace mozilla::dom { 17 18 class TimeoutManager; 19 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; 28 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; 34 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 52 }; 53 54 Mode mMode; 55 56 ~TimeoutExecutor(); 57 58 nsresult ScheduleImmediate(const TimeStamp& aDeadline, const TimeStamp& aNow); 59 60 nsresult ScheduleDelayed(const TimeStamp& aDeadline, const TimeStamp& aNow, 61 const TimeDuration& aMinDelay); 62 63 nsresult Schedule(const TimeStamp& aDeadline, const TimeDuration& aMinDelay); 64 65 nsresult MaybeReschedule(const TimeStamp& aDeadline, 66 const TimeDuration& aMinDelay); 67 68 MOZ_CAN_RUN_SCRIPT void MaybeExecute(); 69 70 public: 71 TimeoutExecutor(TimeoutManager* aOwner, bool aIsIdleQueue, 72 uint32_t aMaxIdleDeferMS); 73 74 void Shutdown(); 75 76 nsresult MaybeSchedule(const TimeStamp& aDeadline, 77 const TimeDuration& aMinDelay); 78 79 void Cancel(); 80 81 NS_DECL_ISUPPORTS 82 NS_DECL_NSIRUNNABLE 83 NS_DECL_NSITIMERCALLBACK 84 NS_DECL_NSINAMED 85 }; 86 87 } // namespace mozilla::dom 88 89 #endif // mozilla_dom_timeoutexecutor_h