| 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_TIMERS_ALARM_TIMER_CHROMEOS_H_ | 5 #ifndef COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ |
| 6 #define COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ | 6 #define COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/files/file_descriptor_watcher_posix.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/sequenced_task_runner_handle.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 class MessageLoop; | |
| 19 struct PendingTask; | 19 struct PendingTask; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace timers { | 22 namespace timers { |
| 23 // The class implements a timer that is capable of waking the system up from a | 23 // The class implements a timer that is capable of waking the system up from a |
| 24 // suspended state. For example, this is useful for running tasks that are | 24 // suspended state. For example, this is useful for running tasks that are |
| 25 // needed for maintaining network connectivity, like sending heartbeat messages. | 25 // needed for maintaining network connectivity, like sending heartbeat messages. |
| 26 // Currently, this feature is only available on Chrome OS systems running linux | 26 // Currently, this feature is only available on Chrome OS systems running linux |
| 27 // version 3.11 or higher. On all other platforms, the AlarmTimer behaves | 27 // version 3.11 or higher. On all other platforms, the AlarmTimer behaves |
| 28 // exactly the same way as a regular Timer. | 28 // exactly the same way as a regular Timer. |
| 29 // |
| 30 // An AlarmTimer instance can only be used from the sequence on which it was |
| 31 // instantiated. Start() and Stop() must be called from a thread that supports |
| 32 // FileDescriptorWatcher. |
| 29 class AlarmTimer : public base::Timer { | 33 class AlarmTimer : public base::Timer { |
| 30 public: | 34 public: |
| 31 ~AlarmTimer() override; | 35 ~AlarmTimer() override; |
| 32 | 36 |
| 33 bool can_wake_from_suspend() const { return can_wake_from_suspend_; } | |
| 34 | |
| 35 // Sets a hook that will be called when the timer fires and a task has been | |
| 36 // queued on |origin_message_loop_|. Used by tests to wait until a task is | |
| 37 // pending in the MessageLoop. | |
| 38 void SetTimerFiredCallbackForTest(base::Closure test_callback); | |
| 39 | |
| 40 // Timer overrides. | 37 // Timer overrides. |
| 41 void Stop() override; | 38 void Stop() override; |
| 42 void Reset() override; | 39 void Reset() override; |
| 43 | 40 |
| 44 protected: | 41 protected: |
| 45 // The constructors for this class are protected because consumers should | |
| 46 // instantiate one of the specialized sub-classes defined below instead. | |
| 47 AlarmTimer(bool retain_user_task, bool is_repeating); | 42 AlarmTimer(bool retain_user_task, bool is_repeating); |
| 48 AlarmTimer(const tracked_objects::Location& posted_from, | |
| 49 base::TimeDelta delay, | |
| 50 const base::Closure& user_task, | |
| 51 bool is_repeating); | |
| 52 | 43 |
| 53 private: | 44 private: |
| 54 // Common initialization that must be performed by both constructors. This | 45 // Called when |alarm_fd_| is readable without blocking. Reads data from |
| 55 // really should live in a delegated constructor but the way base::Timer's | 46 // |alarm_fd_| and calls OnTimerFired(). |
| 56 // constructors are written makes it really hard to do so. | 47 void OnAlarmFdReadableWithoutBlocking(); |
| 57 void Init(); | |
| 58 | 48 |
| 59 // Will be called by the delegate to indicate that the timer has fired and | 49 // Called when the timer fires. Runs the callback. |
| 60 // that the user task should be run. | |
| 61 void OnTimerFired(); | 50 void OnTimerFired(); |
| 62 | 51 |
| 63 // Called when |origin_message_loop_| will be destroyed. | 52 // Tracks whether the timer has the ability to wake the system up from |
| 64 void WillDestroyCurrentMessageLoop(); | 53 // suspend. This is a runtime check because we won't know if the system |
| 54 // supports being woken up from suspend until the constructor actually tries |
| 55 // to set it up. |
| 56 bool CanWakeFromSuspend() const; |
| 65 | 57 |
| 66 // Delegate that will manage actually setting the timer. | 58 // Timer file descriptor. |
| 67 class Delegate; | 59 const int alarm_fd_; |
| 68 scoped_refptr<Delegate> delegate_; | |
| 69 | 60 |
| 70 // Keeps track of the user task we want to run. A new one is constructed | 61 // Watches |alarm_fd_|. |
| 71 // every time Reset() is called. | 62 std::unique_ptr<base::FileDescriptorWatcher::Controller> alarm_fd_watcher_; |
| 63 |
| 64 // Posts tasks to the sequence on which this AlarmTimer was instantiated. |
| 65 const scoped_refptr<base::SequencedTaskRunner> origin_task_runner_ = |
| 66 base::SequencedTaskRunnerHandle::Get(); |
| 67 |
| 68 // Keeps track of the user task we want to run. A new one is constructed every |
| 69 // time Reset() is called. |
| 72 std::unique_ptr<base::PendingTask> pending_task_; | 70 std::unique_ptr<base::PendingTask> pending_task_; |
| 73 | 71 |
| 74 // Tracks whether the timer has the ability to wake the system up from | 72 // Used to invalidate pending callbacks. |
| 75 // suspend. This is a runtime check because we won't know if the system | |
| 76 // supports being woken up from suspend until the delegate actually tries to | |
| 77 // set it up. | |
| 78 bool can_wake_from_suspend_; | |
| 79 | |
| 80 // Pointer to the message loop that started the timer. Used to track the | |
| 81 // destruction of that message loop. | |
| 82 base::MessageLoop* origin_message_loop_; | |
| 83 | |
| 84 // Observes |origin_message_loop_| and informs this class if it will be | |
| 85 // destroyed. | |
| 86 class MessageLoopObserver; | |
| 87 std::unique_ptr<MessageLoopObserver> message_loop_observer_; | |
| 88 | |
| 89 base::WeakPtrFactory<AlarmTimer> weak_factory_; | 73 base::WeakPtrFactory<AlarmTimer> weak_factory_; |
| 90 | 74 |
| 91 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); | 75 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); |
| 92 }; | 76 }; |
| 93 | 77 |
| 94 // As its name suggests, a OneShotAlarmTimer runs a given task once. It does | 78 // As its name suggests, a OneShotAlarmTimer runs a given task once. It does |
| 95 // not remember the task that was given to it after it has fired and does not | 79 // not remember the task that was given to it after it has fired and does not |
| 96 // repeat. Useful for fire-and-forget tasks. | 80 // repeat. Useful for fire-and-forget tasks. |
| 97 class OneShotAlarmTimer : public AlarmTimer { | 81 class OneShotAlarmTimer : public AlarmTimer { |
| 98 public: | 82 public: |
| 99 // Constructs a basic OneShotAlarmTimer. An AlarmTimer constructed this way | |
| 100 // requires that Start() is called before Reset() is called. | |
| 101 OneShotAlarmTimer(); | 83 OneShotAlarmTimer(); |
| 102 ~OneShotAlarmTimer() override; | 84 ~OneShotAlarmTimer() override; |
| 103 }; | 85 }; |
| 104 | 86 |
| 105 // A RepeatingAlarmTimer takes a task and delay and repeatedly runs the task | 87 // A RepeatingAlarmTimer takes a task and delay and repeatedly runs the task |
| 106 // using the specified delay as an interval between the runs until it is | 88 // using the specified delay as an interval between the runs until it is |
| 107 // explicitly stopped. It remembers both the task and the delay it was given | 89 // explicitly stopped. It remembers both the task and the delay it was given |
| 108 // after it fires. | 90 // after it fires. |
| 109 class RepeatingAlarmTimer : public AlarmTimer { | 91 class RepeatingAlarmTimer : public AlarmTimer { |
| 110 public: | 92 public: |
| 111 // Constructs a basic RepeatingAlarmTimer. An AlarmTimer constructed this way | |
| 112 // requires that Start() is called before Reset() is called. | |
| 113 RepeatingAlarmTimer(); | 93 RepeatingAlarmTimer(); |
| 114 | |
| 115 // Constructs a RepeatingAlarmTimer with pre-populated parameters but does not | |
| 116 // start it. Useful if |user_task| or |delay| are not going to change. | |
| 117 // Reset() can be called immediately after constructing an AlarmTimer in this | |
| 118 // way. | |
| 119 RepeatingAlarmTimer(const tracked_objects::Location& posted_from, | |
| 120 base::TimeDelta delay, | |
| 121 const base::Closure& user_task); | |
| 122 | |
| 123 ~RepeatingAlarmTimer() override; | 94 ~RepeatingAlarmTimer() override; |
| 124 }; | 95 }; |
| 125 | 96 |
| 126 // A SimpleAlarmTimer only fires once but remembers the task that it was given | 97 // A SimpleAlarmTimer only fires once but remembers the task that it was given |
| 127 // even after it has fired. Useful if you want to run the same task multiple | 98 // even after it has fired. Useful if you want to run the same task multiple |
| 128 // times but not at a regular interval. | 99 // times but not at a regular interval. |
| 129 class SimpleAlarmTimer : public AlarmTimer { | 100 class SimpleAlarmTimer : public AlarmTimer { |
| 130 public: | 101 public: |
| 131 // Constructs a basic SimpleAlarmTimer. An AlarmTimer constructed this way | |
| 132 // requires that Start() is called before Reset() is called. | |
| 133 SimpleAlarmTimer(); | 102 SimpleAlarmTimer(); |
| 134 | |
| 135 // Constructs a SimpleAlarmTimer with pre-populated parameters but does not | |
| 136 // start it. Useful if |user_task| or |delay| are not going to change. | |
| 137 // Reset() can be called immediately after constructing an AlarmTimer in this | |
| 138 // way. | |
| 139 SimpleAlarmTimer(const tracked_objects::Location& posted_from, | |
| 140 base::TimeDelta delay, | |
| 141 const base::Closure& user_task); | |
| 142 | |
| 143 ~SimpleAlarmTimer() override; | 103 ~SimpleAlarmTimer() override; |
| 144 }; | 104 }; |
| 145 | 105 |
| 146 } // namespace timers | 106 } // namespace timers |
| 147 | 107 |
| 148 #endif // COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ | 108 #endif // COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ |
| OLD | NEW |