1 // Copyright 2014 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.
5 #ifndef COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_
6 #define COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
21 // The class implements a timer that is capable of waking the system up from a
22 // suspended state. For example, this is useful for running tasks that are
23 // needed for maintaining network connectivity, like sending heartbeat messages.
24 // Currently, this feature is only available on Chrome OS systems running linux
25 // version 3.11 or higher. On all other platforms, the AlarmTimer behaves
26 // exactly the same way as a regular Timer.
27 class AlarmTimer
: public base::Timer
{
29 ~AlarmTimer() override
;
31 bool can_wake_from_suspend() const { return can_wake_from_suspend_
; }
33 // Sets a hook that will be called when the timer fires and a task has been
34 // queued on |origin_message_loop_|. Used by tests to wait until a task is
35 // pending in the MessageLoop.
36 void SetTimerFiredCallbackForTest(base::Closure test_callback
);
40 void Reset() override
;
43 // The constructors for this class are protected because consumers should
44 // instantiate one of the specialized sub-classes defined below instead.
45 AlarmTimer(bool retain_user_task
, bool is_repeating
);
46 AlarmTimer(const tracked_objects::Location
& posted_from
,
47 base::TimeDelta delay
,
48 const base::Closure
& user_task
,
52 // Common initialization that must be performed by both constructors. This
53 // really should live in a delegated constructor but the way base::Timer's
54 // constructors are written makes it really hard to do so.
57 // Will be called by the delegate to indicate that the timer has fired and
58 // that the user task should be run.
61 // Called when |origin_message_loop_| will be destroyed.
62 void WillDestroyCurrentMessageLoop();
64 // Delegate that will manage actually setting the timer.
66 scoped_refptr
<Delegate
> delegate_
;
68 // Keeps track of the user task we want to run. A new one is constructed
69 // every time Reset() is called.
70 scoped_ptr
<base::PendingTask
> pending_task_
;
72 // Tracks whether the timer has the ability to wake the system up from
73 // suspend. This is a runtime check because we won't know if the system
74 // supports being woken up from suspend until the delegate actually tries to
76 bool can_wake_from_suspend_
;
78 // Pointer to the message loop that started the timer. Used to track the
79 // destruction of that message loop.
80 base::MessageLoop
* origin_message_loop_
;
82 // Observes |origin_message_loop_| and informs this class if it will be
84 class MessageLoopObserver
;
85 scoped_ptr
<MessageLoopObserver
> message_loop_observer_
;
87 base::WeakPtrFactory
<AlarmTimer
> weak_factory_
;
89 DISALLOW_COPY_AND_ASSIGN(AlarmTimer
);
92 // As its name suggests, a OneShotAlarmTimer runs a given task once. It does
93 // not remember the task that was given to it after it has fired and does not
94 // repeat. Useful for fire-and-forget tasks.
95 class OneShotAlarmTimer
: public AlarmTimer
{
97 // Constructs a basic OneShotAlarmTimer. An AlarmTimer constructed this way
98 // requires that Start() is called before Reset() is called.
100 ~OneShotAlarmTimer() override
;
103 // A RepeatingAlarmTimer takes a task and delay and repeatedly runs the task
104 // using the specified delay as an interval between the runs until it is
105 // explicitly stopped. It remembers both the task and the delay it was given
107 class RepeatingAlarmTimer
: public AlarmTimer
{
109 // Constructs a basic RepeatingAlarmTimer. An AlarmTimer constructed this way
110 // requires that Start() is called before Reset() is called.
111 RepeatingAlarmTimer();
113 // Constructs a RepeatingAlarmTimer with pre-populated parameters but does not
114 // start it. Useful if |user_task| or |delay| are not going to change.
115 // Reset() can be called immediately after constructing an AlarmTimer in this
117 RepeatingAlarmTimer(const tracked_objects::Location
& posted_from
,
118 base::TimeDelta delay
,
119 const base::Closure
& user_task
);
121 ~RepeatingAlarmTimer() override
;
124 // A SimpleAlarmTimer only fires once but remembers the task that it was given
125 // even after it has fired. Useful if you want to run the same task multiple
126 // times but not at a regular interval.
127 class SimpleAlarmTimer
: public AlarmTimer
{
129 // Constructs a basic SimpleAlarmTimer. An AlarmTimer constructed this way
130 // requires that Start() is called before Reset() is called.
133 // Constructs a SimpleAlarmTimer with pre-populated parameters but does not
134 // start it. Useful if |user_task| or |delay| are not going to change.
135 // Reset() can be called immediately after constructing an AlarmTimer in this
137 SimpleAlarmTimer(const tracked_objects::Location
& posted_from
,
138 base::TimeDelta delay
,
139 const base::Closure
& user_task
);
141 ~SimpleAlarmTimer() override
;
144 } // namespace timers
146 #endif // COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_