Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 65561ac6868abf412d599037f5475d219461cae9 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2018 The Chromium Authors
Roman Aleksandrov9f6594c2019-01-04 12:12:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Albert J. Wong276d6ff2021-07-26 17:47:495#ifndef BASE_TIMER_WALL_CLOCK_TIMER_H_
6#define BASE_TIMER_WALL_CLOCK_TIMER_H_
Roman Aleksandrov9f6594c2019-01-04 12:12:397
Albert J. Wong276d6ff2021-07-26 17:47:498#include "base/base_export.h"
Avi Drissman63e1f992023-01-13 18:54:439#include "base/functional/bind.h"
10#include "base/functional/callback.h"
Roman Aleksandrov9f6594c2019-01-04 12:12:3911#include "base/location.h"
Keishi Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
Roman Aleksandrov9f6594c2019-01-04 12:12:3913#include "base/power_monitor/power_observer.h"
Peter Kasting47b4de142025-01-02 20:48:4614#include "base/sequence_checker.h"
Roman Aleksandrov9f6594c2019-01-04 12:12:3915#include "base/time/time.h"
16#include "base/timer/timer.h"
17
18namespace base {
Peter Kasting99791752025-01-02 20:48:5919
Roman Aleksandrov9f6594c2019-01-04 12:12:3920class Clock;
21class TickClock;
Owen Min782b8932020-04-06 14:31:0622
Peter Kasting99791752025-01-02 20:48:5923// `WallClockTimer` is based on `OneShotTimer` and provides a similar API.
24// Where `OneShotTimer` uses `TimeTicks`, however, `WallClockTimer` uses `Time`.
25// On some platforms, `TimeTicks` freezes during suspend; `Time` does not.
26// `WallClockTimer` recomputes the desired delay after resuming from suspend, so
27// on these platforms, suspends will "delay" `OneShotTimer` but not
28// `WallClockTimer`.
Roman Aleksandrov9f6594c2019-01-04 12:12:3929//
Peter Kasting99791752025-01-02 20:48:5930// This does not attempt to observe and accommodate other `Time` changes, e.g.
31// `Time` moving backwards in response to a DST change. `WallClockTimer` will
32// only notice such changes if the system is subsequently suspended (which will
33// cause a recalculation on resume that will coincidentally take them into
34// account).
Roman Aleksandrov9f6594c2019-01-04 12:12:3935//
Peter Kasting47b4de142025-01-02 20:48:4636// After construction, the timer will become bound to the first sequence any
37// method is called on. All subsequent calls must happen on that sequence until
38// the task runs or is canceled via `Stop()`, after which the timer may be
39// destroyed or restarted on another sequence.
Albert J. Wong276d6ff2021-07-26 17:47:4940class BASE_EXPORT WallClockTimer : public PowerSuspendObserver {
Roman Aleksandrov9f6594c2019-01-04 12:12:3941 public:
Owen Min782b8932020-04-06 14:31:0642 // Constructs a timer. Start() must be called later to start the timer.
43 // If |clock| is provided, it's used instead of
Albert J. Wong276d6ff2021-07-26 17:47:4944 // DefaultClock::GetInstance() to calulate timer's delay. If |tick_clock|
45 // is provided, it's used instead of TimeTicks::Now() to get TimeTicks when
46 // scheduling tasks.
Roman Aleksandrov9f6594c2019-01-04 12:12:3947 WallClockTimer();
Albert J. Wong276d6ff2021-07-26 17:47:4948 WallClockTimer(const Clock* clock, const TickClock* tick_clock);
Owen Min782b8932020-04-06 14:31:0649 WallClockTimer(const WallClockTimer&) = delete;
50 WallClockTimer& operator=(const WallClockTimer&) = delete;
Roman Aleksandrov9f6594c2019-01-04 12:12:3951 ~WallClockTimer() override;
52
Owen Min5eda9bf2020-04-09 14:20:2753 // Starts the timer to run at the given |desired_run_time|. If the timer is
Roman Aleksandrov9f6594c2019-01-04 12:12:3954 // already running, it will be replaced to call the given |user_task|.
Albert J. Wong276d6ff2021-07-26 17:47:4955 virtual void Start(const Location& posted_from,
56 Time desired_run_time,
57 OnceClosure user_task);
Roman Aleksandrov9f6594c2019-01-04 12:12:3958
Owen Min5eda9bf2020-04-09 14:20:2759 // Starts the timer to run at the given |desired_run_time|. If the timer is
Roman Aleksandrov9f6594c2019-01-04 12:12:3960 // already running, it will be replaced to call a task formed from
Owen Min5eda9bf2020-04-09 14:20:2761 // |receiver|->*|method|.
Roman Aleksandrov9f6594c2019-01-04 12:12:3962 template <class Receiver>
Albert J. Wong276d6ff2021-07-26 17:47:4963 void Start(const Location& posted_from,
64 Time desired_run_time,
Roman Aleksandrov9f6594c2019-01-04 12:12:3965 Receiver* receiver,
66 void (Receiver::*method)()) {
Roman Aleksandrov7ed8e1042019-01-16 12:15:3267 Start(posted_from, desired_run_time,
Albert J. Wong276d6ff2021-07-26 17:47:4968 BindOnce(method, Unretained(receiver)));
Roman Aleksandrov9f6594c2019-01-04 12:12:3969 }
70
Peter Kasting99791752025-01-02 20:48:5971 // Stops the timer. No-op if the timer is not running.
Roman Aleksandrov9f6594c2019-01-04 12:12:3972 void Stop();
73
Peter Kasting99791752025-01-02 20:48:5974 // Returns whether the timer is running.
Owen Min491d0dc2020-01-22 19:42:3775 bool IsRunning() const;
Roman Aleksandrov9f6594c2019-01-04 12:12:3976
Albert J. Wong276d6ff2021-07-26 17:47:4977 // PowerSuspendObserver:
Roman Aleksandrov9f6594c2019-01-04 12:12:3978 void OnResume() override;
79
Peter Kasting47b4de142025-01-02 20:48:4680 Time desired_run_time() const {
81 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
82 return desired_run_time_;
83 }
Roman Aleksandrov9f6594c2019-01-04 12:12:3984
85 private:
Roman Aleksandrov9f6594c2019-01-04 12:12:3986 void RunUserTask();
87
Peter Kasting47b4de142025-01-02 20:48:4688 SEQUENCE_CHECKER(sequence_checker_);
89
Roman Aleksandrov9f6594c2019-01-04 12:12:3990 // Location in user code.
Peter Kasting47b4de142025-01-02 20:48:4691 Location posted_from_ GUARDED_BY_CONTEXT(sequence_checker_);
Roman Aleksandrov9f6594c2019-01-04 12:12:3992
Roman Aleksandrov9f6594c2019-01-04 12:12:3993 // The desired run time of |user_task_|.
Peter Kasting47b4de142025-01-02 20:48:4694 Time desired_run_time_ GUARDED_BY_CONTEXT(sequence_checker_);
Roman Aleksandrov9f6594c2019-01-04 12:12:3995
Peter Kasting47b4de142025-01-02 20:48:4696 OnceClosure user_task_ GUARDED_BY_CONTEXT(sequence_checker_);
Roman Aleksandrov9f6594c2019-01-04 12:12:3997
Albert J. Wong276d6ff2021-07-26 17:47:4998 OneShotTimer timer_;
Roman Aleksandrov9f6594c2019-01-04 12:12:3999
100 // The clock used to calculate the run time for scheduled tasks.
Peter Kasting47b4de142025-01-02 20:48:46101 const raw_ptr<const Clock> clock_;
Roman Aleksandrov9f6594c2019-01-04 12:12:39102};
103
Albert J. Wong276d6ff2021-07-26 17:47:49104} // namespace base
Owen Min782b8932020-04-06 14:31:06105
Albert J. Wong276d6ff2021-07-26 17:47:49106#endif // BASE_TIMER_WALL_CLOCK_TIMER_H_