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