Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / timers / alarm_timer.h
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 timers {
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   ~AlarmTimer() override;
63
64   bool can_wake_from_suspend() { return can_wake_from_suspend_; }
65
66   // Timer overrides.
67   void Stop() override;
68   void Reset() override;
69
70   // MessageLoop::DestructionObserver overrides.
71   void WillDestroyCurrentMessageLoop() override;
72
73   // Must be called by the delegate to indicate that the timer has fired and
74   // that the user task should be run.
75   void OnTimerFired();
76
77  private:
78   // Initializes the timer with the appropriate delegate.
79   void Init();
80
81   // Delegate that will manage actually setting the timer.
82   scoped_refptr<Delegate> delegate_;
83
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_;
87
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
91   // set it up.
92   bool can_wake_from_suspend_;
93
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_;
97
98   base::WeakPtrFactory<AlarmTimer> weak_factory_;
99
100   DISALLOW_COPY_AND_ASSIGN(AlarmTimer);
101 };
102
103 }  // namespace timers
104
105 #endif  // COMPONENTS_TIMER_ALARM_TIMER_H_