xref: /aosp_15_r20/external/libchrome/base/timer/timer.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker // OneShotTimer and RepeatingTimer provide a simple timer API.  As the names
6*635a8641SAndroid Build Coastguard Worker // suggest, OneShotTimer calls you back once after a time delay expires.
7*635a8641SAndroid Build Coastguard Worker // RepeatingTimer on the other hand calls you back periodically with the
8*635a8641SAndroid Build Coastguard Worker // prescribed time interval.
9*635a8641SAndroid Build Coastguard Worker //
10*635a8641SAndroid Build Coastguard Worker // OneShotTimer and RepeatingTimer both cancel the timer when they go out of
11*635a8641SAndroid Build Coastguard Worker // scope, which makes it easy to ensure that you do not get called when your
12*635a8641SAndroid Build Coastguard Worker // object has gone out of scope.  Just instantiate a OneShotTimer or
13*635a8641SAndroid Build Coastguard Worker // RepeatingTimer as a member variable of the class for which you wish to
14*635a8641SAndroid Build Coastguard Worker // receive timer events.
15*635a8641SAndroid Build Coastguard Worker //
16*635a8641SAndroid Build Coastguard Worker // Sample RepeatingTimer usage:
17*635a8641SAndroid Build Coastguard Worker //
18*635a8641SAndroid Build Coastguard Worker //   class MyClass {
19*635a8641SAndroid Build Coastguard Worker //    public:
20*635a8641SAndroid Build Coastguard Worker //     void StartDoingStuff() {
21*635a8641SAndroid Build Coastguard Worker //       timer_.Start(FROM_HERE, TimeDelta::FromSeconds(1),
22*635a8641SAndroid Build Coastguard Worker //                    this, &MyClass::DoStuff);
23*635a8641SAndroid Build Coastguard Worker //     }
24*635a8641SAndroid Build Coastguard Worker //     void StopDoingStuff() {
25*635a8641SAndroid Build Coastguard Worker //       timer_.Stop();
26*635a8641SAndroid Build Coastguard Worker //     }
27*635a8641SAndroid Build Coastguard Worker //    private:
28*635a8641SAndroid Build Coastguard Worker //     void DoStuff() {
29*635a8641SAndroid Build Coastguard Worker //       // This method is called every second to do stuff.
30*635a8641SAndroid Build Coastguard Worker //       ...
31*635a8641SAndroid Build Coastguard Worker //     }
32*635a8641SAndroid Build Coastguard Worker //     base::RepeatingTimer timer_;
33*635a8641SAndroid Build Coastguard Worker //   };
34*635a8641SAndroid Build Coastguard Worker //
35*635a8641SAndroid Build Coastguard Worker // Both OneShotTimer and RepeatingTimer also support a Reset method, which
36*635a8641SAndroid Build Coastguard Worker // allows you to easily defer the timer event until the timer delay passes once
37*635a8641SAndroid Build Coastguard Worker // again.  So, in the above example, if 0.5 seconds have already passed,
38*635a8641SAndroid Build Coastguard Worker // calling Reset on |timer_| would postpone DoStuff by another 1 second.  In
39*635a8641SAndroid Build Coastguard Worker // other words, Reset is shorthand for calling Stop and then Start again with
40*635a8641SAndroid Build Coastguard Worker // the same arguments.
41*635a8641SAndroid Build Coastguard Worker //
42*635a8641SAndroid Build Coastguard Worker // These APIs are not thread safe. All methods must be called from the same
43*635a8641SAndroid Build Coastguard Worker // sequence (not necessarily the construction sequence), except for the
44*635a8641SAndroid Build Coastguard Worker // destructor and SetTaskRunner().
45*635a8641SAndroid Build Coastguard Worker // - The destructor may be called from any sequence when the timer is not
46*635a8641SAndroid Build Coastguard Worker // running and there is no scheduled task active, i.e. when Start() has never
47*635a8641SAndroid Build Coastguard Worker // been called or after AbandonAndStop() has been called.
48*635a8641SAndroid Build Coastguard Worker // - SetTaskRunner() may be called from any sequence when the timer is not
49*635a8641SAndroid Build Coastguard Worker // running, i.e. when Start() has never been called or Stop() has been called
50*635a8641SAndroid Build Coastguard Worker // since the last Start().
51*635a8641SAndroid Build Coastguard Worker //
52*635a8641SAndroid Build Coastguard Worker // By default, the scheduled tasks will be run on the same sequence that the
53*635a8641SAndroid Build Coastguard Worker // Timer was *started on*, but this can be changed *prior* to Start() via
54*635a8641SAndroid Build Coastguard Worker // SetTaskRunner().
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TIMER_TIMER_H_
57*635a8641SAndroid Build Coastguard Worker #define BASE_TIMER_TIMER_H_
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker // IMPORTANT: If you change timer code, make sure that all tests (including
60*635a8641SAndroid Build Coastguard Worker // disabled ones) from timer_unittests.cc pass locally. Some are disabled
61*635a8641SAndroid Build Coastguard Worker // because they're flaky on the buildbot, but when you run them locally you
62*635a8641SAndroid Build Coastguard Worker // should be able to tell the difference.
63*635a8641SAndroid Build Coastguard Worker 
64*635a8641SAndroid Build Coastguard Worker #include <memory>
65*635a8641SAndroid Build Coastguard Worker 
66*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
67*635a8641SAndroid Build Coastguard Worker #include "base/bind.h"
68*635a8641SAndroid Build Coastguard Worker #include "base/bind_helpers.h"
69*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
70*635a8641SAndroid Build Coastguard Worker #include "base/location.h"
71*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
72*635a8641SAndroid Build Coastguard Worker #include "base/sequence_checker_impl.h"
73*635a8641SAndroid Build Coastguard Worker #include "base/sequenced_task_runner.h"
74*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker namespace base {
77*635a8641SAndroid Build Coastguard Worker 
78*635a8641SAndroid Build Coastguard Worker class BaseTimerTaskInternal;
79*635a8641SAndroid Build Coastguard Worker class TickClock;
80*635a8641SAndroid Build Coastguard Worker 
81*635a8641SAndroid Build Coastguard Worker //-----------------------------------------------------------------------------
82*635a8641SAndroid Build Coastguard Worker // This class wraps TaskRunner::PostDelayedTask to manage delayed and repeating
83*635a8641SAndroid Build Coastguard Worker // tasks. See meta comment above for thread-safety requirements.
84*635a8641SAndroid Build Coastguard Worker //
85*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT Timer {
86*635a8641SAndroid Build Coastguard Worker  public:
87*635a8641SAndroid Build Coastguard Worker   // Construct a timer in repeating or one-shot mode. Start must be called later
88*635a8641SAndroid Build Coastguard Worker   // to set task info. |retain_user_task| determines whether the user_task is
89*635a8641SAndroid Build Coastguard Worker   // retained or reset when it runs or stops. If |tick_clock| is provided, it is
90*635a8641SAndroid Build Coastguard Worker   // used instead of TimeTicks::Now() to get TimeTicks when scheduling tasks.
91*635a8641SAndroid Build Coastguard Worker   Timer(bool retain_user_task, bool is_repeating);
92*635a8641SAndroid Build Coastguard Worker   Timer(bool retain_user_task, bool is_repeating, const TickClock* tick_clock);
93*635a8641SAndroid Build Coastguard Worker 
94*635a8641SAndroid Build Coastguard Worker   // Construct a timer with retained task info. If |tick_clock| is provided, it
95*635a8641SAndroid Build Coastguard Worker   // is used instead of TimeTicks::Now() to get TimeTicks when scheduling tasks.
96*635a8641SAndroid Build Coastguard Worker   Timer(const Location& posted_from,
97*635a8641SAndroid Build Coastguard Worker         TimeDelta delay,
98*635a8641SAndroid Build Coastguard Worker         const base::Closure& user_task,
99*635a8641SAndroid Build Coastguard Worker         bool is_repeating);
100*635a8641SAndroid Build Coastguard Worker   Timer(const Location& posted_from,
101*635a8641SAndroid Build Coastguard Worker         TimeDelta delay,
102*635a8641SAndroid Build Coastguard Worker         const base::Closure& user_task,
103*635a8641SAndroid Build Coastguard Worker         bool is_repeating,
104*635a8641SAndroid Build Coastguard Worker         const TickClock* tick_clock);
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker   virtual ~Timer();
107*635a8641SAndroid Build Coastguard Worker 
108*635a8641SAndroid Build Coastguard Worker   // Returns true if the timer is running (i.e., not stopped).
109*635a8641SAndroid Build Coastguard Worker   bool IsRunning() const;
110*635a8641SAndroid Build Coastguard Worker 
111*635a8641SAndroid Build Coastguard Worker   // Returns the current delay for this timer.
112*635a8641SAndroid Build Coastguard Worker   TimeDelta GetCurrentDelay() const;
113*635a8641SAndroid Build Coastguard Worker 
114*635a8641SAndroid Build Coastguard Worker   // Set the task runner on which the task should be scheduled. This method can
115*635a8641SAndroid Build Coastguard Worker   // only be called before any tasks have been scheduled. If |task_runner| runs
116*635a8641SAndroid Build Coastguard Worker   // tasks on a different sequence than the sequence owning this Timer,
117*635a8641SAndroid Build Coastguard Worker   // |user_task_| will be posted to it when the Timer fires (note that this
118*635a8641SAndroid Build Coastguard Worker   // means |user_task_| can run after ~Timer() and should support that).
119*635a8641SAndroid Build Coastguard Worker   virtual void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner);
120*635a8641SAndroid Build Coastguard Worker 
121*635a8641SAndroid Build Coastguard Worker   // Start the timer to run at the given |delay| from now. If the timer is
122*635a8641SAndroid Build Coastguard Worker   // already running, it will be replaced to call the given |user_task|.
123*635a8641SAndroid Build Coastguard Worker   virtual void Start(const Location& posted_from,
124*635a8641SAndroid Build Coastguard Worker                      TimeDelta delay,
125*635a8641SAndroid Build Coastguard Worker                      const base::Closure& user_task);
126*635a8641SAndroid Build Coastguard Worker 
127*635a8641SAndroid Build Coastguard Worker   // Start the timer to run at the given |delay| from now. If the timer is
128*635a8641SAndroid Build Coastguard Worker   // already running, it will be replaced to call a task formed from
129*635a8641SAndroid Build Coastguard Worker   // |reviewer->*method|.
130*635a8641SAndroid Build Coastguard Worker   template <class Receiver>
Start(const Location & posted_from,TimeDelta delay,Receiver * receiver,void (Receiver::* method)())131*635a8641SAndroid Build Coastguard Worker   void Start(const Location& posted_from,
132*635a8641SAndroid Build Coastguard Worker              TimeDelta delay,
133*635a8641SAndroid Build Coastguard Worker              Receiver* receiver,
134*635a8641SAndroid Build Coastguard Worker              void (Receiver::*method)()) {
135*635a8641SAndroid Build Coastguard Worker     Start(posted_from, delay,
136*635a8641SAndroid Build Coastguard Worker           base::BindRepeating(method, base::Unretained(receiver)));
137*635a8641SAndroid Build Coastguard Worker   }
138*635a8641SAndroid Build Coastguard Worker 
139*635a8641SAndroid Build Coastguard Worker   // Call this method to stop and cancel the timer.  It is a no-op if the timer
140*635a8641SAndroid Build Coastguard Worker   // is not running.
141*635a8641SAndroid Build Coastguard Worker   virtual void Stop();
142*635a8641SAndroid Build Coastguard Worker 
143*635a8641SAndroid Build Coastguard Worker   // Stop running task (if any) and abandon scheduled task (if any).
AbandonAndStop()144*635a8641SAndroid Build Coastguard Worker   void AbandonAndStop() {
145*635a8641SAndroid Build Coastguard Worker     AbandonScheduledTask();
146*635a8641SAndroid Build Coastguard Worker 
147*635a8641SAndroid Build Coastguard Worker     Stop();
148*635a8641SAndroid Build Coastguard Worker     // No more member accesses here: |this| could be deleted at this point.
149*635a8641SAndroid Build Coastguard Worker   }
150*635a8641SAndroid Build Coastguard Worker 
151*635a8641SAndroid Build Coastguard Worker   // Call this method to reset the timer delay. The |user_task_| must be set. If
152*635a8641SAndroid Build Coastguard Worker   // the timer is not running, this will start it by posting a task.
153*635a8641SAndroid Build Coastguard Worker   virtual void Reset();
154*635a8641SAndroid Build Coastguard Worker 
user_task()155*635a8641SAndroid Build Coastguard Worker   const base::Closure& user_task() const { return user_task_; }
desired_run_time()156*635a8641SAndroid Build Coastguard Worker   const TimeTicks& desired_run_time() const { return desired_run_time_; }
157*635a8641SAndroid Build Coastguard Worker 
158*635a8641SAndroid Build Coastguard Worker  protected:
159*635a8641SAndroid Build Coastguard Worker   // Returns the current tick count.
160*635a8641SAndroid Build Coastguard Worker   TimeTicks Now() const;
161*635a8641SAndroid Build Coastguard Worker 
set_user_task(const Closure & task)162*635a8641SAndroid Build Coastguard Worker   void set_user_task(const Closure& task) { user_task_ = task; }
set_desired_run_time(TimeTicks desired)163*635a8641SAndroid Build Coastguard Worker   void set_desired_run_time(TimeTicks desired) { desired_run_time_ = desired; }
set_is_running(bool running)164*635a8641SAndroid Build Coastguard Worker   void set_is_running(bool running) { is_running_ = running; }
165*635a8641SAndroid Build Coastguard Worker 
posted_from()166*635a8641SAndroid Build Coastguard Worker   const Location& posted_from() const { return posted_from_; }
167*635a8641SAndroid Build Coastguard Worker 
168*635a8641SAndroid Build Coastguard Worker   // The task runner on which the task should be scheduled. If it is null, the
169*635a8641SAndroid Build Coastguard Worker   // task runner for the current sequence will be used.
170*635a8641SAndroid Build Coastguard Worker   scoped_refptr<SequencedTaskRunner> task_runner_;
171*635a8641SAndroid Build Coastguard Worker 
172*635a8641SAndroid Build Coastguard Worker   // Timer isn't thread-safe and must only be used on its origin sequence
173*635a8641SAndroid Build Coastguard Worker   // (sequence on which it was started). Once fully Stop()'ed it may be
174*635a8641SAndroid Build Coastguard Worker   // destroyed or restarted on another sequence.
175*635a8641SAndroid Build Coastguard Worker   SequenceChecker origin_sequence_checker_;
176*635a8641SAndroid Build Coastguard Worker 
177*635a8641SAndroid Build Coastguard Worker  private:
178*635a8641SAndroid Build Coastguard Worker   friend class BaseTimerTaskInternal;
179*635a8641SAndroid Build Coastguard Worker 
180*635a8641SAndroid Build Coastguard Worker   // Allocates a new |scheduled_task_| and posts it on the current sequence with
181*635a8641SAndroid Build Coastguard Worker   // the given |delay|. |scheduled_task_| must be null. |scheduled_run_time_|
182*635a8641SAndroid Build Coastguard Worker   // and |desired_run_time_| are reset to Now() + delay.
183*635a8641SAndroid Build Coastguard Worker   void PostNewScheduledTask(TimeDelta delay);
184*635a8641SAndroid Build Coastguard Worker 
185*635a8641SAndroid Build Coastguard Worker   // Returns the task runner on which the task should be scheduled. If the
186*635a8641SAndroid Build Coastguard Worker   // corresponding |task_runner_| field is null, the task runner for the current
187*635a8641SAndroid Build Coastguard Worker   // sequence is returned.
188*635a8641SAndroid Build Coastguard Worker   scoped_refptr<SequencedTaskRunner> GetTaskRunner();
189*635a8641SAndroid Build Coastguard Worker 
190*635a8641SAndroid Build Coastguard Worker   // Disable |scheduled_task_| and abandon it so that it no longer refers back
191*635a8641SAndroid Build Coastguard Worker   // to this object.
192*635a8641SAndroid Build Coastguard Worker   void AbandonScheduledTask();
193*635a8641SAndroid Build Coastguard Worker 
194*635a8641SAndroid Build Coastguard Worker   // Called by BaseTimerTaskInternal when the delayed task fires.
195*635a8641SAndroid Build Coastguard Worker   void RunScheduledTask();
196*635a8641SAndroid Build Coastguard Worker 
197*635a8641SAndroid Build Coastguard Worker   // When non-null, the |scheduled_task_| was posted to call RunScheduledTask()
198*635a8641SAndroid Build Coastguard Worker   // at |scheduled_run_time_|.
199*635a8641SAndroid Build Coastguard Worker   BaseTimerTaskInternal* scheduled_task_;
200*635a8641SAndroid Build Coastguard Worker 
201*635a8641SAndroid Build Coastguard Worker   // Location in user code.
202*635a8641SAndroid Build Coastguard Worker   Location posted_from_;
203*635a8641SAndroid Build Coastguard Worker   // Delay requested by user.
204*635a8641SAndroid Build Coastguard Worker   TimeDelta delay_;
205*635a8641SAndroid Build Coastguard Worker   // |user_task_| is what the user wants to be run at |desired_run_time_|.
206*635a8641SAndroid Build Coastguard Worker   base::Closure user_task_;
207*635a8641SAndroid Build Coastguard Worker 
208*635a8641SAndroid Build Coastguard Worker   // The time at which |scheduled_task_| is expected to fire. This time can be a
209*635a8641SAndroid Build Coastguard Worker   // "zero" TimeTicks if the task must be run immediately.
210*635a8641SAndroid Build Coastguard Worker   TimeTicks scheduled_run_time_;
211*635a8641SAndroid Build Coastguard Worker 
212*635a8641SAndroid Build Coastguard Worker   // The desired run time of |user_task_|. The user may update this at any time,
213*635a8641SAndroid Build Coastguard Worker   // even if their previous request has not run yet. If |desired_run_time_| is
214*635a8641SAndroid Build Coastguard Worker   // greater than |scheduled_run_time_|, a continuation task will be posted to
215*635a8641SAndroid Build Coastguard Worker   // wait for the remaining time. This allows us to reuse the pending task so as
216*635a8641SAndroid Build Coastguard Worker   // not to flood the delayed queues with orphaned tasks when the user code
217*635a8641SAndroid Build Coastguard Worker   // excessively Stops and Starts the timer. This time can be a "zero" TimeTicks
218*635a8641SAndroid Build Coastguard Worker   // if the task must be run immediately.
219*635a8641SAndroid Build Coastguard Worker   TimeTicks desired_run_time_;
220*635a8641SAndroid Build Coastguard Worker 
221*635a8641SAndroid Build Coastguard Worker   // Repeating timers automatically post the task again before calling the task
222*635a8641SAndroid Build Coastguard Worker   // callback.
223*635a8641SAndroid Build Coastguard Worker   const bool is_repeating_;
224*635a8641SAndroid Build Coastguard Worker 
225*635a8641SAndroid Build Coastguard Worker   // If true, hold on to the |user_task_| closure object for reuse.
226*635a8641SAndroid Build Coastguard Worker   const bool retain_user_task_;
227*635a8641SAndroid Build Coastguard Worker 
228*635a8641SAndroid Build Coastguard Worker   // The tick clock used to calculate the run time for scheduled tasks.
229*635a8641SAndroid Build Coastguard Worker   const TickClock* const tick_clock_;
230*635a8641SAndroid Build Coastguard Worker 
231*635a8641SAndroid Build Coastguard Worker   // If true, |user_task_| is scheduled to run sometime in the future.
232*635a8641SAndroid Build Coastguard Worker   bool is_running_;
233*635a8641SAndroid Build Coastguard Worker 
234*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(Timer);
235*635a8641SAndroid Build Coastguard Worker };
236*635a8641SAndroid Build Coastguard Worker 
237*635a8641SAndroid Build Coastguard Worker //-----------------------------------------------------------------------------
238*635a8641SAndroid Build Coastguard Worker // A simple, one-shot timer.  See usage notes at the top of the file.
239*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT OneShotTimer : public Timer {
240*635a8641SAndroid Build Coastguard Worker  public:
OneShotTimer()241*635a8641SAndroid Build Coastguard Worker   OneShotTimer() : OneShotTimer(nullptr) {}
OneShotTimer(const TickClock * tick_clock)242*635a8641SAndroid Build Coastguard Worker   explicit OneShotTimer(const TickClock* tick_clock)
243*635a8641SAndroid Build Coastguard Worker       : Timer(false, false, tick_clock) {}
244*635a8641SAndroid Build Coastguard Worker 
245*635a8641SAndroid Build Coastguard Worker   // Run the scheduled task immediately, and stop the timer. The timer needs to
246*635a8641SAndroid Build Coastguard Worker   // be running.
247*635a8641SAndroid Build Coastguard Worker   void FireNow();
248*635a8641SAndroid Build Coastguard Worker };
249*635a8641SAndroid Build Coastguard Worker 
250*635a8641SAndroid Build Coastguard Worker //-----------------------------------------------------------------------------
251*635a8641SAndroid Build Coastguard Worker // A simple, repeating timer.  See usage notes at the top of the file.
252*635a8641SAndroid Build Coastguard Worker class RepeatingTimer : public Timer {
253*635a8641SAndroid Build Coastguard Worker  public:
RepeatingTimer()254*635a8641SAndroid Build Coastguard Worker   RepeatingTimer() : RepeatingTimer(nullptr) {}
RepeatingTimer(const TickClock * tick_clock)255*635a8641SAndroid Build Coastguard Worker   explicit RepeatingTimer(const TickClock* tick_clock)
256*635a8641SAndroid Build Coastguard Worker       : Timer(true, true, tick_clock) {}
257*635a8641SAndroid Build Coastguard Worker 
RepeatingTimer(const Location & posted_from,TimeDelta delay,RepeatingClosure user_task)258*635a8641SAndroid Build Coastguard Worker   RepeatingTimer(const Location& posted_from,
259*635a8641SAndroid Build Coastguard Worker                  TimeDelta delay,
260*635a8641SAndroid Build Coastguard Worker                  RepeatingClosure user_task)
261*635a8641SAndroid Build Coastguard Worker       : Timer(posted_from, delay, std::move(user_task), true) {}
RepeatingTimer(const Location & posted_from,TimeDelta delay,RepeatingClosure user_task,const TickClock * tick_clock)262*635a8641SAndroid Build Coastguard Worker   RepeatingTimer(const Location& posted_from,
263*635a8641SAndroid Build Coastguard Worker                  TimeDelta delay,
264*635a8641SAndroid Build Coastguard Worker                  RepeatingClosure user_task,
265*635a8641SAndroid Build Coastguard Worker                  const TickClock* tick_clock)
266*635a8641SAndroid Build Coastguard Worker       : Timer(posted_from, delay, std::move(user_task), true, tick_clock) {}
267*635a8641SAndroid Build Coastguard Worker };
268*635a8641SAndroid Build Coastguard Worker 
269*635a8641SAndroid Build Coastguard Worker //-----------------------------------------------------------------------------
270*635a8641SAndroid Build Coastguard Worker // A simple, one-shot timer with the retained user task.  See usage notes at the
271*635a8641SAndroid Build Coastguard Worker // top of the file.
272*635a8641SAndroid Build Coastguard Worker class RetainingOneShotTimer : public Timer {
273*635a8641SAndroid Build Coastguard Worker  public:
RetainingOneShotTimer()274*635a8641SAndroid Build Coastguard Worker   RetainingOneShotTimer() : RetainingOneShotTimer(nullptr) {}
RetainingOneShotTimer(const TickClock * tick_clock)275*635a8641SAndroid Build Coastguard Worker   explicit RetainingOneShotTimer(const TickClock* tick_clock)
276*635a8641SAndroid Build Coastguard Worker       : Timer(true, false, tick_clock) {}
277*635a8641SAndroid Build Coastguard Worker 
RetainingOneShotTimer(const Location & posted_from,TimeDelta delay,RepeatingClosure user_task)278*635a8641SAndroid Build Coastguard Worker   RetainingOneShotTimer(const Location& posted_from,
279*635a8641SAndroid Build Coastguard Worker                         TimeDelta delay,
280*635a8641SAndroid Build Coastguard Worker                         RepeatingClosure user_task)
281*635a8641SAndroid Build Coastguard Worker       : Timer(posted_from, delay, std::move(user_task), false) {}
RetainingOneShotTimer(const Location & posted_from,TimeDelta delay,RepeatingClosure user_task,const TickClock * tick_clock)282*635a8641SAndroid Build Coastguard Worker   RetainingOneShotTimer(const Location& posted_from,
283*635a8641SAndroid Build Coastguard Worker                         TimeDelta delay,
284*635a8641SAndroid Build Coastguard Worker                         RepeatingClosure user_task,
285*635a8641SAndroid Build Coastguard Worker                         const TickClock* tick_clock)
286*635a8641SAndroid Build Coastguard Worker       : Timer(posted_from, delay, std::move(user_task), false, tick_clock) {}
287*635a8641SAndroid Build Coastguard Worker };
288*635a8641SAndroid Build Coastguard Worker 
289*635a8641SAndroid Build Coastguard Worker //-----------------------------------------------------------------------------
290*635a8641SAndroid Build Coastguard Worker // A Delay timer is like The Button from Lost. Once started, you have to keep
291*635a8641SAndroid Build Coastguard Worker // calling Reset otherwise it will call the given method on the sequence it was
292*635a8641SAndroid Build Coastguard Worker // initially Reset() from.
293*635a8641SAndroid Build Coastguard Worker //
294*635a8641SAndroid Build Coastguard Worker // Once created, it is inactive until Reset is called. Once |delay| seconds have
295*635a8641SAndroid Build Coastguard Worker // passed since the last call to Reset, the callback is made. Once the callback
296*635a8641SAndroid Build Coastguard Worker // has been made, it's inactive until Reset is called again.
297*635a8641SAndroid Build Coastguard Worker //
298*635a8641SAndroid Build Coastguard Worker // If destroyed, the timeout is canceled and will not occur even if already
299*635a8641SAndroid Build Coastguard Worker // inflight.
300*635a8641SAndroid Build Coastguard Worker class DelayTimer {
301*635a8641SAndroid Build Coastguard Worker  public:
302*635a8641SAndroid Build Coastguard Worker   template <class Receiver>
DelayTimer(const Location & posted_from,TimeDelta delay,Receiver * receiver,void (Receiver::* method)())303*635a8641SAndroid Build Coastguard Worker   DelayTimer(const Location& posted_from,
304*635a8641SAndroid Build Coastguard Worker              TimeDelta delay,
305*635a8641SAndroid Build Coastguard Worker              Receiver* receiver,
306*635a8641SAndroid Build Coastguard Worker              void (Receiver::*method)())
307*635a8641SAndroid Build Coastguard Worker       : DelayTimer(posted_from, delay, receiver, method, nullptr) {}
308*635a8641SAndroid Build Coastguard Worker 
309*635a8641SAndroid Build Coastguard Worker   template <class Receiver>
DelayTimer(const Location & posted_from,TimeDelta delay,Receiver * receiver,void (Receiver::* method)(),const TickClock * tick_clock)310*635a8641SAndroid Build Coastguard Worker   DelayTimer(const Location& posted_from,
311*635a8641SAndroid Build Coastguard Worker              TimeDelta delay,
312*635a8641SAndroid Build Coastguard Worker              Receiver* receiver,
313*635a8641SAndroid Build Coastguard Worker              void (Receiver::*method)(),
314*635a8641SAndroid Build Coastguard Worker              const TickClock* tick_clock)
315*635a8641SAndroid Build Coastguard Worker       : timer_(posted_from,
316*635a8641SAndroid Build Coastguard Worker                delay,
317*635a8641SAndroid Build Coastguard Worker                BindRepeating(method, Unretained(receiver)),
318*635a8641SAndroid Build Coastguard Worker                tick_clock) {}
319*635a8641SAndroid Build Coastguard Worker 
Reset()320*635a8641SAndroid Build Coastguard Worker   void Reset() { timer_.Reset(); }
321*635a8641SAndroid Build Coastguard Worker 
322*635a8641SAndroid Build Coastguard Worker  private:
323*635a8641SAndroid Build Coastguard Worker   RetainingOneShotTimer timer_;
324*635a8641SAndroid Build Coastguard Worker 
325*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(DelayTimer);
326*635a8641SAndroid Build Coastguard Worker };
327*635a8641SAndroid Build Coastguard Worker 
328*635a8641SAndroid Build Coastguard Worker }  // namespace base
329*635a8641SAndroid Build Coastguard Worker 
330*635a8641SAndroid Build Coastguard Worker #endif  // BASE_TIMER_TIMER_H_
331