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.
5 #ifndef COMPONENTS_TIMER_ALARM_TIMER_H_
6 #define COMPONENTS_TIMER_ALARM_TIMER_H_
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"
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
{
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
> {
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;
40 // Stops the currently running timer. It should be safe to call this more
42 virtual void Stop() = 0;
44 // Resets the timer to fire after |delay| has passed. Cancels any
45 // pre-existing delay.
46 virtual void Reset(base::TimeDelta delay
) = 0;
49 virtual ~Delegate() {}
52 friend class base::RefCountedThreadSafe
<Delegate
>;
55 AlarmTimer(bool retain_user_task
, bool is_repeating
);
57 AlarmTimer(const tracked_objects::Location
& posted_from
,
58 base::TimeDelta delay
,
59 const base::Closure
& user_task
,
62 ~AlarmTimer() override
;
64 bool can_wake_from_suspend() const { return can_wake_from_suspend_
; }
66 // Must be called by the delegate to indicate that the timer has fired and
67 // that the user task should be run.
72 void Reset() override
;
74 // MessageLoop::DestructionObserver overrides.
75 void WillDestroyCurrentMessageLoop() override
;
78 // Initializes the timer with the appropriate delegate.
81 // Delegate that will manage actually setting the timer.
82 scoped_refptr
<Delegate
> delegate_
;
84 // Keeps track of the user task we want to run. A new one is constructed
85 // every time Reset() is called.
86 scoped_ptr
<base::PendingTask
> pending_task_
;
88 // Tracks whether the timer has the ability to wake the system up from
89 // suspend. This is a runtime check because we won't know if the system
90 // supports being woken up from suspend until the delegate actually tries to
92 bool can_wake_from_suspend_
;
94 // Pointer to the message loop that started the timer. Used to track the
95 // destruction of that message loop.
96 base::MessageLoop
* origin_message_loop_
;
98 base::WeakPtrFactory
<AlarmTimer
> weak_factory_
;
100 DISALLOW_COPY_AND_ASSIGN(AlarmTimer
);
103 } // namespace timers
105 #endif // COMPONENTS_TIMER_ALARM_TIMER_H_