Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_TIMER_ALARM_TIMER_H_ | |
| 6 #define COMPONENTS_TIMER_ALARM_TIMER_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "base/timer/timer.h" | |
| 15 | |
| 16 namespace base { | |
| 17 struct PendingTask; | |
| 18 } | |
| 19 | |
| 20 namespace timer { | |
| 21 // The class implements a timer that is capable of waking the system up from a | |
| 22 // suspended state. For example, this is useful for running tasks that are | |
| 23 // needed for maintaining network connectivity, like sending heartbeat messages. | |
| 24 // Currently, this feature is only available on Chrome OS systems running linux | |
| 25 // version 3.11 or higher. On all other platforms, the AlarmTimer behaves | |
| 26 // exactly the same way as a regular Timer. | |
| 27 class AlarmTimer : public base::Timer, | |
| 28 public base::MessageLoop::DestructionObserver { | |
| 29 public: | |
| 30 // The delegate is responsible for managing the system level details for | |
| 31 // actually setting up and monitoring a timer that is capable of waking the | |
| 32 // system from suspend. This class is reference counted because it may need | |
| 33 // to outlive the timer in order to clean up after itself. | |
| 34 class Delegate : public base::RefCountedThreadSafe<Delegate> { | |
| 35 public: | |
| 36 // Initializes the timer. Should return true iff the system has timers that | |
| 37 // can wake it up from suspend. Will only be called once. | |
| 38 virtual bool Init(base::WeakPtr<AlarmTimer> timer) = 0; | |
| 39 | |
| 40 // Stops the currently running timer. It should be safe to call this more | |
| 41 // than once. | |
| 42 virtual void Stop() = 0; | |
| 43 | |
| 44 // Resets the timer to fire after |delay| has passed. Cancels any | |
| 45 // pre-existing delay. | |
| 46 virtual void Reset(base::TimeDelta delay) = 0; | |
| 47 | |
| 48 protected: | |
| 49 virtual ~Delegate() {} | |
| 50 | |
| 51 private: | |
| 52 friend class base::RefCountedThreadSafe<Delegate>; | |
| 53 }; | |
| 54 | |
| 55 AlarmTimer(bool retain_user_task, bool is_repeating); | |
| 56 | |
| 57 AlarmTimer(const tracked_objects::Location& posted_from, | |
| 58 base::TimeDelta delay, | |
| 59 const base::Closure& user_task, | |
| 60 bool is_repeating); | |
| 61 | |
| 62 virtual ~AlarmTimer(); | |
| 63 | |
| 64 // Timer overrides. | |
| 65 void Stop() override; | |
| 66 void Reset() override; | |
| 67 | |
| 68 // MessageLoop::DestructionObserver overrides. | |
| 69 void WillDestroyCurrentMessageLoop() override; | |
| 70 | |
| 71 // Must be called by the delegate to indicate that the timer has fired and | |
| 72 // that the user task should be run. | |
| 73 void OnTimerFired(); | |
| 74 | |
| 75 private: | |
| 76 // Initializes the timer with the appropriate delegate. | |
| 77 void Init(); | |
| 78 | |
| 79 // Delegate that will manage actually setting the timer. | |
| 80 scoped_refptr<Delegate> delegate_; | |
| 81 | |
| 82 // Keeps track of the user task we want to run. A new one is constructed | |
| 83 // every timer Reset() is called. | |
|
Daniel Erat
2014/10/23 03:44:54
nit: s/timer/time/
Chirantan Ekbote
2014/10/23 23:53:30
Done.
| |
| 84 scoped_ptr<base::PendingTask> pending_task_; | |
| 85 | |
| 86 // Tracks whether the timer has the ability to wake the system up from | |
| 87 // suspend. This is a runtime check because we won't know if the system | |
| 88 // supports being woken up from suspend until the delegate actually tries to | |
| 89 // set it up. | |
| 90 bool can_wake_from_suspend_; | |
| 91 | |
| 92 // Pointer to the message loop that started the timer. Used to track the | |
| 93 // destruction of that message loop. | |
| 94 base::MessageLoop* origin_message_loop_; | |
| 95 | |
| 96 base::WeakPtrFactory<AlarmTimer> weak_factory_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); | |
| 99 }; | |
| 100 | |
| 101 } // namespace timer | |
| 102 | |
| 103 #endif // COMPONENTS_TIMER_ALARM_TIMER_H_ | |
| OLD | NEW |