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/files/file_descriptor_watcher_posix.h" 11 #include "base/macros.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/threading/sequenced_task_runner_handle.h" 15 #include "base/time/time.h" 16 #include "base/timer/timer.h" 17 18 namespace base { 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 // 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. 33 class AlarmTimer : public base::Timer { 34 public: 35 ~AlarmTimer() override; 36 37 // Timer overrides. 38 void Stop() override; 39 void Reset() override; 40 41 protected: 42 AlarmTimer(bool retain_user_task, bool is_repeating); 43 44 private: 45 // Called when |alarm_fd_| is readable without blocking. Reads data from 46 // |alarm_fd_| and calls OnTimerFired(). 47 void OnAlarmFdReadableWithoutBlocking(); 48 49 // Called when the timer fires. Runs the callback. 50 void OnTimerFired(); 51 52 // Tracks whether the timer has the ability to wake the system up from 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; 57 58 // Timer file descriptor. 59 const int alarm_fd_; 60 61 // Watches |alarm_fd_|. 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. 70 std::unique_ptr<base::PendingTask> pending_task_; 71 72 // Used to invalidate pending callbacks. 73 base::WeakPtrFactory<AlarmTimer> weak_factory_; 74 75 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); 76 }; 77 78 // As its name suggests, a OneShotAlarmTimer runs a given task once. It does 79 // not remember the task that was given to it after it has fired and does not 80 // repeat. Useful for fire-and-forget tasks. 81 class OneShotAlarmTimer : public AlarmTimer { 82 public: 83 OneShotAlarmTimer(); 84 ~OneShotAlarmTimer() override; 85 }; 86 87 // A RepeatingAlarmTimer takes a task and delay and repeatedly runs the task 88 // using the specified delay as an interval between the runs until it is 89 // explicitly stopped. It remembers both the task and the delay it was given 90 // after it fires. 91 class RepeatingAlarmTimer : public AlarmTimer { 92 public: 93 RepeatingAlarmTimer(); 94 ~RepeatingAlarmTimer() override; 95 }; 96 97 // A SimpleAlarmTimer only fires once but remembers the task that it was given 98 // even after it has fired. Useful if you want to run the same task multiple 99 // times but not at a regular interval. 100 class SimpleAlarmTimer : public AlarmTimer { 101 public: 102 SimpleAlarmTimer(); 103 ~SimpleAlarmTimer() override; 104 }; 105 106 } // namespace timers 107 108 #endif // COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ 109