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. 4 5 #ifndef COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ 6 #define COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ 7 8 #include <memory> 9 10 #include "base/callback.h" 11 #include "base/macros.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/time/time.h" 15 #include "base/timer/timer.h" 16 17 namespace base { 18 class MessageLoop; 19 struct PendingTask; 20 } 21 22 namespace timers { 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 25 // needed for maintaining network connectivity, like sending heartbeat messages. 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 28 // exactly the same way as a regular Timer. 29 class AlarmTimer : public base::Timer { 30 public: 31 ~AlarmTimer() override; 32 can_wake_from_suspend()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. 41 void Stop() override; 42 void Reset() override; 43 44 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); 48 AlarmTimer(const tracked_objects::Location& posted_from, 49 base::TimeDelta delay, 50 const base::Closure& user_task, 51 bool is_repeating); 52 53 private: 54 // Common initialization that must be performed by both constructors. This 55 // really should live in a delegated constructor but the way base::Timer's 56 // constructors are written makes it really hard to do so. 57 void Init(); 58 59 // Will be called by the delegate to indicate that the timer has fired and 60 // that the user task should be run. 61 void OnTimerFired(); 62 63 // Called when |origin_message_loop_| will be destroyed. 64 void WillDestroyCurrentMessageLoop(); 65 66 // Delegate that will manage actually setting the timer. 67 class Delegate; 68 scoped_refptr<Delegate> delegate_; 69 70 // Keeps track of the user task we want to run. A new one is constructed 71 // every time Reset() is called. 72 std::unique_ptr<base::PendingTask> pending_task_; 73 74 // Tracks whether the timer has the ability to wake the system up from 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_; 90 91 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); 92 }; 93 94 // 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 96 // repeat. Useful for fire-and-forget tasks. 97 class OneShotAlarmTimer : public AlarmTimer { 98 public: 99 // Constructs a basic OneShotAlarmTimer. An AlarmTimer constructed this way 100 // requires that Start() is called before Reset() is called. 101 OneShotAlarmTimer(); 102 ~OneShotAlarmTimer() override; 103 }; 104 105 // 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 107 // explicitly stopped. It remembers both the task and the delay it was given 108 // after it fires. 109 class RepeatingAlarmTimer : public AlarmTimer { 110 public: 111 // Constructs a basic RepeatingAlarmTimer. An AlarmTimer constructed this way 112 // requires that Start() is called before Reset() is called. 113 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; 124 }; 125 126 // 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 128 // times but not at a regular interval. 129 class SimpleAlarmTimer : public AlarmTimer { 130 public: 131 // Constructs a basic SimpleAlarmTimer. An AlarmTimer constructed this way 132 // requires that Start() is called before Reset() is called. 133 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; 144 }; 145 146 } // namespace timers 147 148 #endif // COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ 149