1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ 6*635a8641SAndroid Build Coastguard Worker #define COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <memory> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include "base/files/file_descriptor_watcher_posix.h" 11*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 12*635a8641SAndroid Build Coastguard Worker #include "base/memory/scoped_refptr.h" 13*635a8641SAndroid Build Coastguard Worker #include "base/memory/weak_ptr.h" 14*635a8641SAndroid Build Coastguard Worker #include "base/threading/sequenced_task_runner_handle.h" 15*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h" 16*635a8641SAndroid Build Coastguard Worker #include "base/timer/timer.h" 17*635a8641SAndroid Build Coastguard Worker 18*635a8641SAndroid Build Coastguard Worker namespace base { 19*635a8641SAndroid Build Coastguard Worker struct PendingTask; 20*635a8641SAndroid Build Coastguard Worker } 21*635a8641SAndroid Build Coastguard Worker 22*635a8641SAndroid Build Coastguard Worker namespace timers { 23*635a8641SAndroid Build Coastguard Worker // The class implements a timer that is capable of waking the system up from a 24*635a8641SAndroid Build Coastguard Worker // suspended state. For example, this is useful for running tasks that are 25*635a8641SAndroid Build Coastguard Worker // needed for maintaining network connectivity, like sending heartbeat messages. 26*635a8641SAndroid Build Coastguard Worker // Currently, this feature is only available on Chrome OS systems running linux 27*635a8641SAndroid Build Coastguard Worker // version 3.11 or higher. On all other platforms, the SimpleAlarmTimer behaves 28*635a8641SAndroid Build Coastguard Worker // exactly the same way as a regular Timer. 29*635a8641SAndroid Build Coastguard Worker // 30*635a8641SAndroid Build Coastguard Worker // A SimpleAlarmTimer instance can only be used from the sequence on which it 31*635a8641SAndroid Build Coastguard Worker // was instantiated. Start() and Stop() must be called from a thread that 32*635a8641SAndroid Build Coastguard Worker // supports FileDescriptorWatcher. 33*635a8641SAndroid Build Coastguard Worker // 34*635a8641SAndroid Build Coastguard Worker // A SimpleAlarmTimer only fires once but remembers the task that it was given 35*635a8641SAndroid Build Coastguard Worker // even after it has fired. Useful if you want to run the same task multiple 36*635a8641SAndroid Build Coastguard Worker // times but not at a regular interval. 37*635a8641SAndroid Build Coastguard Worker class SimpleAlarmTimer : public base::RetainingOneShotTimer { 38*635a8641SAndroid Build Coastguard Worker public: 39*635a8641SAndroid Build Coastguard Worker SimpleAlarmTimer(); 40*635a8641SAndroid Build Coastguard Worker ~SimpleAlarmTimer() override; 41*635a8641SAndroid Build Coastguard Worker 42*635a8641SAndroid Build Coastguard Worker // Timer overrides. 43*635a8641SAndroid Build Coastguard Worker void Stop() override; 44*635a8641SAndroid Build Coastguard Worker void Reset() override; 45*635a8641SAndroid Build Coastguard Worker 46*635a8641SAndroid Build Coastguard Worker private: 47*635a8641SAndroid Build Coastguard Worker // Called when |alarm_fd_| is readable without blocking. Reads data from 48*635a8641SAndroid Build Coastguard Worker // |alarm_fd_| and calls OnTimerFired(). 49*635a8641SAndroid Build Coastguard Worker void OnAlarmFdReadableWithoutBlocking(); 50*635a8641SAndroid Build Coastguard Worker 51*635a8641SAndroid Build Coastguard Worker // Called when the timer fires. Runs the callback. 52*635a8641SAndroid Build Coastguard Worker void OnTimerFired(); 53*635a8641SAndroid Build Coastguard Worker 54*635a8641SAndroid Build Coastguard Worker // Tracks whether the timer has the ability to wake the system up from 55*635a8641SAndroid Build Coastguard Worker // suspend. This is a runtime check because we won't know if the system 56*635a8641SAndroid Build Coastguard Worker // supports being woken up from suspend until the constructor actually tries 57*635a8641SAndroid Build Coastguard Worker // to set it up. 58*635a8641SAndroid Build Coastguard Worker bool CanWakeFromSuspend() const; 59*635a8641SAndroid Build Coastguard Worker 60*635a8641SAndroid Build Coastguard Worker // Timer file descriptor. 61*635a8641SAndroid Build Coastguard Worker const int alarm_fd_; 62*635a8641SAndroid Build Coastguard Worker 63*635a8641SAndroid Build Coastguard Worker // Watches |alarm_fd_|. 64*635a8641SAndroid Build Coastguard Worker std::unique_ptr<base::FileDescriptorWatcher::Controller> alarm_fd_watcher_; 65*635a8641SAndroid Build Coastguard Worker 66*635a8641SAndroid Build Coastguard Worker // Posts tasks to the sequence on which this AlarmTimer was instantiated. 67*635a8641SAndroid Build Coastguard Worker const scoped_refptr<base::SequencedTaskRunner> origin_task_runner_ = 68*635a8641SAndroid Build Coastguard Worker base::SequencedTaskRunnerHandle::Get(); 69*635a8641SAndroid Build Coastguard Worker 70*635a8641SAndroid Build Coastguard Worker // Keeps track of the user task we want to run. A new one is constructed every 71*635a8641SAndroid Build Coastguard Worker // time Reset() is called. 72*635a8641SAndroid Build Coastguard Worker std::unique_ptr<base::PendingTask> pending_task_; 73*635a8641SAndroid Build Coastguard Worker 74*635a8641SAndroid Build Coastguard Worker // Used to invalidate pending callbacks. 75*635a8641SAndroid Build Coastguard Worker base::WeakPtrFactory<SimpleAlarmTimer> weak_factory_; 76*635a8641SAndroid Build Coastguard Worker 77*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(SimpleAlarmTimer); 78*635a8641SAndroid Build Coastguard Worker }; 79*635a8641SAndroid Build Coastguard Worker 80*635a8641SAndroid Build Coastguard Worker } // namespace timers 81*635a8641SAndroid Build Coastguard Worker 82*635a8641SAndroid Build Coastguard Worker #endif // COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ 83