Home | History | Annotate | Download | only in timers
      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/scoped_refptr.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 SimpleAlarmTimer behaves
     28 // exactly the same way as a regular Timer.
     29 //
     30 // A SimpleAlarmTimer instance can only be used from the sequence on which it
     31 // was instantiated. Start() and Stop() must be called from a thread that
     32 // supports FileDescriptorWatcher.
     33 //
     34 // A SimpleAlarmTimer only fires once but remembers the task that it was given
     35 // even after it has fired.  Useful if you want to run the same task multiple
     36 // times but not at a regular interval.
     37 class SimpleAlarmTimer : public base::RetainingOneShotTimer {
     38  public:
     39   SimpleAlarmTimer();
     40   ~SimpleAlarmTimer() override;
     41 
     42   // Timer overrides.
     43   void Stop() override;
     44   void Reset() override;
     45 
     46  private:
     47   // Called when |alarm_fd_| is readable without blocking. Reads data from
     48   // |alarm_fd_| and calls OnTimerFired().
     49   void OnAlarmFdReadableWithoutBlocking();
     50 
     51   // Called when the timer fires. Runs the callback.
     52   void OnTimerFired();
     53 
     54   // Tracks whether the timer has the ability to wake the system up from
     55   // suspend. This is a runtime check because we won't know if the system
     56   // supports being woken up from suspend until the constructor actually tries
     57   // to set it up.
     58   bool CanWakeFromSuspend() const;
     59 
     60   // Timer file descriptor.
     61   const int alarm_fd_;
     62 
     63   // Watches |alarm_fd_|.
     64   std::unique_ptr<base::FileDescriptorWatcher::Controller> alarm_fd_watcher_;
     65 
     66   // Posts tasks to the sequence on which this AlarmTimer was instantiated.
     67   const scoped_refptr<base::SequencedTaskRunner> origin_task_runner_ =
     68       base::SequencedTaskRunnerHandle::Get();
     69 
     70   // Keeps track of the user task we want to run. A new one is constructed every
     71   // time Reset() is called.
     72   std::unique_ptr<base::PendingTask> pending_task_;
     73 
     74   // Used to invalidate pending callbacks.
     75   base::WeakPtrFactory<SimpleAlarmTimer> weak_factory_;
     76 
     77   DISALLOW_COPY_AND_ASSIGN(SimpleAlarmTimer);
     78 };
     79 
     80 }  // namespace timers
     81 
     82 #endif  // COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_
     83