Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Side by Side Diff: components/timers/alarm_timer.h

Issue 706993003: C++ readability review (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reorder Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_TIMER_ALARM_TIMER_H_ 5 #ifndef COMPONENTS_TIMER_ALARM_TIMER_H_
6 #define COMPONENTS_TIMER_ALARM_TIMER_H_ 6 #define COMPONENTS_TIMER_ALARM_TIMER_H_
7 7
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/time/time.h" 12 #include "base/time/time.h"
14 #include "base/timer/timer.h" 13 #include "base/timer/timer.h"
15 14
16 namespace base { 15 namespace base {
16 class MessageLoop;
17 struct PendingTask; 17 struct PendingTask;
18 } 18 }
19 19
20 namespace timers { 20 namespace timers {
21 // The class implements a timer that is capable of waking the system up from a 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 22 // suspended state. For example, this is useful for running tasks that are
23 // needed for maintaining network connectivity, like sending heartbeat messages. 23 // needed for maintaining network connectivity, like sending heartbeat messages.
24 // Currently, this feature is only available on Chrome OS systems running linux 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 25 // version 3.11 or higher. On all other platforms, the AlarmTimer behaves
26 // exactly the same way as a regular Timer. 26 // exactly the same way as a regular Timer.
27 class AlarmTimer : public base::Timer, 27 class AlarmTimer : public base::Timer {
28 public base::MessageLoop::DestructionObserver {
29 public: 28 public:
30 // The delegate is responsible for managing the system level details for 29 ~AlarmTimer() override;
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 30
40 // Stops the currently running timer. It should be safe to call this more 31 bool can_wake_from_suspend() const { return can_wake_from_suspend_; }
41 // than once.
42 virtual void Stop() = 0;
43 32
44 // Resets the timer to fire after |delay| has passed. Cancels any 33 // Timer overrides.
45 // pre-existing delay. 34 void Stop() override;
46 virtual void Reset(base::TimeDelta delay) = 0; 35 void Reset() override;
47 36
48 protected: 37 protected:
49 virtual ~Delegate() {} 38 // The constructors for this class are protected because consumers should
50 39 // instantiate one of the specialized sub-classes defined below instead.
51 private:
52 friend class base::RefCountedThreadSafe<Delegate>;
53 };
54
55 AlarmTimer(bool retain_user_task, bool is_repeating); 40 AlarmTimer(bool retain_user_task, bool is_repeating);
56
57 AlarmTimer(const tracked_objects::Location& posted_from, 41 AlarmTimer(const tracked_objects::Location& posted_from,
58 base::TimeDelta delay, 42 base::TimeDelta delay,
59 const base::Closure& user_task, 43 const base::Closure& user_task,
60 bool is_repeating); 44 bool is_repeating);
61 45
62 ~AlarmTimer() override; 46 private:
47 // Common initialization that must be performed by both constructors. This
48 // really should live in a delegated constructor but the way base::Timer's
49 // constructors are written makes it really hard to do so.
50 void Init();
63 51
64 bool can_wake_from_suspend() const { return can_wake_from_suspend_; } 52 // Will be called by the delegate to indicate that the timer has fired and
65
66 // Must be called by the delegate to indicate that the timer has fired and
67 // that the user task should be run. 53 // that the user task should be run.
68 void OnTimerFired(); 54 void OnTimerFired();
69 55
70 // Timer overrides. 56 // Called when |origin_message_loop_| will be destroyed.
71 void Stop() override; 57 void WillDestroyCurrentMessageLoop();
72 void Reset() override;
73
74 // MessageLoop::DestructionObserver overrides.
75 void WillDestroyCurrentMessageLoop() override;
76
77 private:
78 // Initializes the timer with the appropriate delegate.
79 void Init();
80 58
81 // Delegate that will manage actually setting the timer. 59 // Delegate that will manage actually setting the timer.
60 class Delegate;
82 scoped_refptr<Delegate> delegate_; 61 scoped_refptr<Delegate> delegate_;
83 62
84 // Keeps track of the user task we want to run. A new one is constructed 63 // Keeps track of the user task we want to run. A new one is constructed
85 // every time Reset() is called. 64 // every time Reset() is called.
86 scoped_ptr<base::PendingTask> pending_task_; 65 scoped_ptr<base::PendingTask> pending_task_;
87 66
88 // Tracks whether the timer has the ability to wake the system up from 67 // 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 68 // 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 69 // supports being woken up from suspend until the delegate actually tries to
91 // set it up. 70 // set it up.
92 bool can_wake_from_suspend_; 71 bool can_wake_from_suspend_;
93 72
94 // Pointer to the message loop that started the timer. Used to track the 73 // Pointer to the message loop that started the timer. Used to track the
95 // destruction of that message loop. 74 // destruction of that message loop.
96 base::MessageLoop* origin_message_loop_; 75 base::MessageLoop* origin_message_loop_;
97 76
77 // Observes |origin_message_loop_| and informs this class if it will be
78 // destroyed.
79 class MessageLoopObserver;
80 scoped_ptr<MessageLoopObserver> message_loop_observer_;
81
98 base::WeakPtrFactory<AlarmTimer> weak_factory_; 82 base::WeakPtrFactory<AlarmTimer> weak_factory_;
99 83
100 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); 84 DISALLOW_COPY_AND_ASSIGN(AlarmTimer);
101 }; 85 };
102 86
87 // As its name suggests, a OneShotAlarmTimer runs a given task once. It does
88 // not remember the task that was given to it after it has fired and does not
89 // repeat. Useful for fire-and-forget tasks.
90 class OneShotAlarmTimer : public AlarmTimer {
91 public:
92 // Constructs a basic OneShotAlarmTimer. An AlarmTimer constructed this way
93 // requires that Start() is called before Reset() is called.
94 OneShotAlarmTimer() : AlarmTimer(false, false) {}
95 };
96
97 // A RepeatingAlarmTimer takes a task and delay and repeatedly runs the task
98 // using the specified delay as an interval between the runs until it is
99 // explicitly stopped. It remembers both the task and the delay it was given
100 // after it fires.
101 class RepeatingAlarmTimer : public AlarmTimer {
102 public:
103 // Constructs a basic RepeatingAlarmTimer. An AlarmTimer constructed this way
104 // requires that Start() is called before Reset() is called.
105 RepeatingAlarmTimer() : AlarmTimer(true, true) {}
106
107 // Constructs a RepeatingAlarmTimer with pre-populated parameters but does not
108 // start it. Useful if |user_task| or |delay| are not going to change.
109 // Reset() can be called immediately after constructing an AlarmTimer in this
110 // way.
111 RepeatingAlarmTimer(const tracked_objects::Location& posted_from,
112 base::TimeDelta delay,
113 const base::Closure& user_task)
114 : AlarmTimer(posted_from, delay, user_task, true) {}
115 };
116
117 // A SimpleAlarmTimer only fires once but remembers the task that it was given
118 // even after it has fired. Useful if you want to run the same task multiple
119 // times but not at a regular interval.
120 class SimpleAlarmTimer : public AlarmTimer {
121 public:
122 // Constructs a basic SimpleAlarmTimer. An AlarmTimer constructed this way
123 // requires that Start() is called before Reset() is called.
124 SimpleAlarmTimer() : AlarmTimer(true, false) {}
125
126 // Constructs a SimpleAlarmTimer with pre-populated parameters but does not
127 // start it. Useful if |user_task| or |delay| are not going to change.
128 // Reset() can be called immediately after constructing an AlarmTimer in this
129 // way.
130 SimpleAlarmTimer(const tracked_objects::Location& posted_from,
131 base::TimeDelta delay,
132 const base::Closure& user_task)
133 : AlarmTimer(posted_from, delay, user_task, false) {}
134 };
135
103 } // namespace timers 136 } // namespace timers
104 137
105 #endif // COMPONENTS_TIMER_ALARM_TIMER_H_ 138 #endif // COMPONENTS_TIMER_ALARM_TIMER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698