OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_TIMER_ALARM_TIMER_H_ | 5 #ifndef COMPONENTS_TIMER_ALARM_TIMER_H_ |
6 #define COMPONENTS_TIMER_ALARM_TIMER_H_ | 6 #define COMPONENTS_TIMER_ALARM_TIMER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 struct PendingTask; | 17 struct PendingTask; |
18 } | 18 } |
19 | 19 |
20 namespace timers { | 20 namespace timers { |
21 // The class implements a timer that is capable of waking the system up from a | 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 | 22 // suspended state. For example, this is useful for running tasks that are |
23 // needed for maintaining network connectivity, like sending heartbeat messages. | 23 // needed for maintaining network connectivity, like sending heartbeat messages. |
24 // Currently, this feature is only available on Chrome OS systems running linux | 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 | 25 // version 3.11 or higher. On all other platforms, the AlarmTimer behaves |
26 // exactly the same way as a regular Timer. | 26 // exactly the same way as a regular Timer. |
27 class AlarmTimer : public base::Timer, | 27 class AlarmTimer : public base::Timer, |
28 public base::MessageLoop::DestructionObserver { | 28 public base::MessageLoop::DestructionObserver { |
29 public: | 29 public: |
30 // The delegate is responsible for managing the system level details for | |
31 // actually setting up and monitoring a timer that is capable of waking the | |
32 // system from suspend. This class is reference counted because it may need | |
33 // to outlive the timer in order to clean up after itself. | |
34 class Delegate : public base::RefCountedThreadSafe<Delegate> { | |
35 public: | |
36 // Initializes the timer. Should return true iff the system has timers that | |
37 // can wake it up from suspend. Will only be called once. | |
38 virtual bool Init(base::WeakPtr<AlarmTimer> timer) = 0; | |
39 | |
40 // Stops the currently running timer. It should be safe to call this more | |
41 // than once. | |
42 virtual void Stop() = 0; | |
43 | |
44 // Resets the timer to fire after |delay| has passed. Cancels any | |
45 // pre-existing delay. | |
46 virtual void Reset(base::TimeDelta delay) = 0; | |
47 | |
48 protected: | |
49 virtual ~Delegate() {} | |
50 | |
51 private: | |
52 friend class base::RefCountedThreadSafe<Delegate>; | |
53 }; | |
54 | |
55 AlarmTimer(bool retain_user_task, bool is_repeating); | |
56 | |
57 AlarmTimer(const tracked_objects::Location& posted_from, | |
58 base::TimeDelta delay, | |
59 const base::Closure& user_task, | |
60 bool is_repeating); | |
61 | |
62 ~AlarmTimer() override; | 30 ~AlarmTimer() override; |
63 | 31 |
64 bool can_wake_from_suspend() const { return can_wake_from_suspend_; } | 32 bool can_wake_from_suspend() const { return can_wake_from_suspend_; } |
65 | 33 |
66 // Must be called by the delegate to indicate that the timer has fired and | 34 // Must be called by the delegate to indicate that the timer has fired and |
gromer
2015/02/10 23:47:10
If only the delegate needs to call it, could it be
Chirantan Ekbote
2015/02/12 01:49:59
Done.
| |
67 // that the user task should be run. | 35 // that the user task should be run. |
68 void OnTimerFired(); | 36 void OnTimerFired(); |
69 | 37 |
70 // Timer overrides. | 38 // Timer overrides. |
71 void Stop() override; | 39 void Stop() override; |
72 void Reset() override; | 40 void Reset() override; |
73 | 41 |
74 // MessageLoop::DestructionObserver overrides. | 42 // MessageLoop::DestructionObserver overrides. |
75 void WillDestroyCurrentMessageLoop() override; | 43 void WillDestroyCurrentMessageLoop() override; |
76 | 44 |
45 protected: | |
46 // The constructors for this class are protected because consumers should | |
47 // instantiate one of the specialized sub-classes defined below instead. | |
48 AlarmTimer(bool retain_user_task, bool is_repeating); | |
49 AlarmTimer(const tracked_objects::Location& posted_from, | |
50 base::TimeDelta delay, | |
51 const base::Closure& user_task, | |
52 bool is_repeating); | |
53 | |
77 private: | 54 private: |
78 // Initializes the timer with the appropriate delegate. | 55 // Initializes the timer with the appropriate delegate. |
79 void Init(); | 56 void Init(); |
80 | 57 |
81 // Delegate that will manage actually setting the timer. | 58 // Delegate that will manage actually setting the timer. |
59 class Delegate; | |
82 scoped_refptr<Delegate> delegate_; | 60 scoped_refptr<Delegate> delegate_; |
83 | 61 |
84 // Keeps track of the user task we want to run. A new one is constructed | 62 // Keeps track of the user task we want to run. A new one is constructed |
85 // every time Reset() is called. | 63 // every time Reset() is called. |
86 scoped_ptr<base::PendingTask> pending_task_; | 64 scoped_ptr<base::PendingTask> pending_task_; |
87 | 65 |
88 // Tracks whether the timer has the ability to wake the system up from | 66 // Tracks whether the timer has the ability to wake the system up from |
89 // suspend. This is a runtime check because we won't know if the system | 67 // suspend. This is a runtime check because we won't know if the system |
90 // supports being woken up from suspend until the delegate actually tries to | 68 // supports being woken up from suspend until the delegate actually tries to |
91 // set it up. | 69 // set it up. |
92 bool can_wake_from_suspend_; | 70 bool can_wake_from_suspend_; |
93 | 71 |
94 // Pointer to the message loop that started the timer. Used to track the | 72 // Pointer to the message loop that started the timer. Used to track the |
95 // destruction of that message loop. | 73 // destruction of that message loop. |
96 base::MessageLoop* origin_message_loop_; | 74 base::MessageLoop* origin_message_loop_; |
97 | 75 |
98 base::WeakPtrFactory<AlarmTimer> weak_factory_; | 76 base::WeakPtrFactory<AlarmTimer> weak_factory_; |
99 | 77 |
100 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); | 78 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); |
101 }; | 79 }; |
102 | 80 |
81 // As its name suggests, a OneShotAlarmTimer runs a given task once. It does | |
82 // not remember the task that was given to it after it has fired and does not | |
83 // repeat. Useful for fire-and-forget tasks. | |
84 class OneShotAlarmTimer : public AlarmTimer { | |
85 public: | |
86 // Constructs a basic OneShotAlarmTimer. An AlarmTimer constructed this way | |
87 // requires that Start() is called before Reset() is called. | |
88 OneShotAlarmTimer() : AlarmTimer(false, false) {} | |
89 }; | |
90 | |
91 // A RepeatingAlarmTimer takes a task and delay and repeatedly runs the task | |
92 // using the specified delay as an interval between the runs until it is | |
93 // explicitly stopped. It remembers both the task and the delay it was given | |
94 // after it fires. | |
95 class RepeatingAlarmTimer : public AlarmTimer { | |
96 public: | |
97 // Constructs a basic RepeatingAlarmTimer. An AlarmTimer constructed this way | |
98 // requires that Start() is called before Reset() is called. | |
99 RepeatingAlarmTimer() : AlarmTimer(true, true) {} | |
100 | |
101 // Constructs a RepeatingAlarmTimer with pre-populated parameters but does not | |
102 // start it. Useful if |user_task| or |delay| are not going to change. | |
103 // Reset() can be called immediately after constructing an AlarmTimer in this | |
104 // way. | |
105 RepeatingAlarmTimer(const tracked_objects::Location& posted_from, | |
106 base::TimeDelta delay, | |
107 const base::Closure& user_task) | |
108 : AlarmTimer(posted_from, delay, user_task, true) {} | |
109 }; | |
110 | |
111 // A SimpleAlarmTimer only fires once but remembers the task that it was given | |
112 // even after it has fired. Useful if you want to run the same task multiple | |
113 // times but not at a regular interval. | |
114 class SimpleAlarmTimer : public AlarmTimer { | |
115 public: | |
116 // Constructs a basic SimpleAlarmTimer. An AlarmTimer constructed this way | |
117 // requires that Start() is called before Reset() is called. | |
118 SimpleAlarmTimer() : AlarmTimer(true, false) {} | |
119 | |
120 // Constructs a SimpleAlarmTimer with pre-populated parameters but does not | |
121 // start it. Useful if |user_task| or |delay| are not going to change. | |
122 // Reset() can be called immediately after constructing an AlarmTimer in this | |
123 // way. | |
124 SimpleAlarmTimer(const tracked_objects::Location& posted_from, | |
125 base::TimeDelta delay, | |
126 const base::Closure& user_task) | |
127 : AlarmTimer(posted_from, delay, user_task, false) {} | |
128 }; | |
129 | |
103 } // namespace timers | 130 } // namespace timers |
104 | 131 |
105 #endif // COMPONENTS_TIMER_ALARM_TIMER_H_ | 132 #endif // COMPONENTS_TIMER_ALARM_TIMER_H_ |
OLD | NEW |