wall_clock_timer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_UTIL_TIMER_WALL_CLOCK_TIMER_H_
  5. #define BASE_UTIL_TIMER_WALL_CLOCK_TIMER_H_
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/location.h"
  9. #include "base/power_monitor/power_observer.h"
  10. #include "base/time/default_clock.h"
  11. #include "base/time/time.h"
  12. #include "base/timer/timer.h"
  13. namespace base {
  14. class Clock;
  15. class TickClock;
  16. } // namespace base
  17. namespace util {
  18. // WallClockTimer is based on OneShotTimer and provides a simple timer API
  19. // which is mostly similar to OneShotTimer's API. The main difference is that
  20. // WallClockTimer is using Time (which is system-dependent) to schedule task.
  21. // WallClockTimer calls you back once scheduled time has come.
  22. //
  23. // Comparison with OneShotTimer: WallClockTimer runs |user_task_| after |delay_|
  24. // expires according to usual time, while OneShotTimer runs |user_task_| after
  25. // |delay_| expires according to TimeTicks which may freeze on some platforms
  26. // when power suspends (desktop falls asleep). On platforms where TimeTicks
  27. // don't freeze, the WallClockTimer has the same behavior as OneShotTimer.
  28. //
  29. // The API is not thread safe. All methods must be called from the same
  30. // sequence (not necessarily the construction sequence), except for the
  31. // destructor.
  32. // - The destructor may be called from any sequence when the timer is not
  33. // running and there is no scheduled task active.
  34. class WallClockTimer : public base::PowerObserver {
  35. public:
  36. // Constructs a timer. Start() must be called later to start the timer.
  37. // If |clock| is provided, it's used instead of
  38. // base::DefaultClock::GetInstance() to calulate timer's delay.
  39. // If |tick_clock| is provided, it's used instead of base::TimeTicks::Now() to
  40. // get base::TimeTicks when scheduling tasks.
  41. WallClockTimer();
  42. WallClockTimer(const base::Clock* clock, const base::TickClock* tick_clock);
  43. WallClockTimer(const WallClockTimer&) = delete;
  44. WallClockTimer& operator=(const WallClockTimer&) = delete;
  45. ~WallClockTimer() override;
  46. // Starts the timer to run at the given |desired_run_time|. If the timer is
  47. // already running, it will be replaced to call the given |user_task|.
  48. virtual void Start(const base::Location& posted_from,
  49. base::Time desired_run_time,
  50. base::OnceClosure user_task);
  51. // Starts the timer to run at the given |desired_run_time|. If the timer is
  52. // already running, it will be replaced to call a task formed from
  53. // |receiver|->*|method|.
  54. template <class Receiver>
  55. void Start(const base::Location& posted_from,
  56. base::Time desired_run_time,
  57. Receiver* receiver,
  58. void (Receiver::*method)()) {
  59. Start(posted_from, desired_run_time,
  60. base::BindOnce(method, base::Unretained(receiver)));
  61. }
  62. // Stops the timer. It is a no-op if the timer is not running.
  63. void Stop();
  64. // Returns true if the timer is running.
  65. bool IsRunning() const;
  66. // base::PowerObserver:
  67. void OnResume() override;
  68. base::Time desired_run_time() const { return desired_run_time_; }
  69. private:
  70. void AddObserver();
  71. void RemoveObserver();
  72. // Actually run scheduled task
  73. void RunUserTask();
  74. // Returns the current time count.
  75. base::Time Now() const;
  76. bool observer_added_ = false;
  77. // Location in user code.
  78. base::Location posted_from_;
  79. // The desired run time of |user_task_|.
  80. base::Time desired_run_time_;
  81. base::OnceClosure user_task_;
  82. // Timer which should notify to run task in the period while system awake
  83. base::OneShotTimer timer_;
  84. // The clock used to calculate the run time for scheduled tasks.
  85. const base::Clock* const clock_ = base::DefaultClock::GetInstance();
  86. };
  87. } // namespace util
  88. #endif // BASE_UTIL_TIMER_WALL_CLOCK_TIMER_H_