author Sylvestre Ledru <>
Fri, 30 Nov 2018 11:46:48 +0100
changeset 448947 6f3709b3878117466168c40affa7bca0b60cf75b
parent 388518 cb123c84818a3c491fb9d63f00891d23f515cf6f
child 455557 7f8594d21c51ba9966c85b1bacbf0d245e45395f
permissions -rw-r--r--
Bug 1511181 - Reformat everything to the Google coding style r=ehsan a=clang-format # ignore-this-changeset

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at */

#ifndef mozilla_dom_timeoutexecutor_h
#define mozilla_dom_timeoutexecutor_h

#include "mozilla/TimeStamp.h"
#include "nsCOMPtr.h"
#include "nsIRunnable.h"
#include "nsITimer.h"
#include "nsINamed.h"

namespace mozilla {
namespace dom {

class TimeoutManager;

class TimeoutExecutor final : public nsIRunnable,
                              public nsITimerCallback,
                              public nsINamed {
  TimeoutManager* mOwner;
  nsCOMPtr<nsITimer> mTimer;
  TimeStamp mDeadline;

  // Limits how far we allow timers to fire into the future from their
  // deadline.  Starts off at zero, but is then adjusted when we start
  // using nsITimer.  The nsITimer implementation may sometimes fire
  // early and we should allow that to minimize additional wakeups.
  TimeDuration mAllowedEarlyFiringTime;

  // The TimeoutExecutor is repeatedly scheduled by the TimeoutManager
  // to fire for the next soonest Timeout.  Since the executor is re-used
  // it needs to handle switching between a few states.
  enum class Mode {
    // None indicates the executor is idle.  It may be scheduled or shutdown.
    // Immediate means the executor is scheduled to run a Timeout with a
    // deadline that has already expired.
    // Delayed means the executor is scheduled to run a Timeout with a
    // deadline in the future.
    // Shutdown means the TimeoutManager has been destroyed.  Once this
    // state is reached the executor cannot be scheduled again.  If the
    // executor is already dispatched as a runnable or held by a timer then
    // we may still get a Run()/Notify() call which will be ignored.

  Mode mMode;


  nsresult ScheduleImmediate(const TimeStamp& aDeadline, const TimeStamp& aNow);

  nsresult ScheduleDelayed(const TimeStamp& aDeadline, const TimeStamp& aNow,
                           const TimeDuration& aMinDelay);

  nsresult Schedule(const TimeStamp& aDeadline, const TimeDuration& aMinDelay);

  nsresult MaybeReschedule(const TimeStamp& aDeadline,
                           const TimeDuration& aMinDelay);

  void MaybeExecute();

  explicit TimeoutExecutor(TimeoutManager* aOwner);

  void Shutdown();

  nsresult MaybeSchedule(const TimeStamp& aDeadline,
                         const TimeDuration& aMinDelay);

  void Cancel();


}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_timeoutexecutor_h