OLD | NEW |
---|---|
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" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
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 { | 28 public base::MessageLoop::DestructionObserver { |
gromer
2014/12/05 20:13:41
https://engdoc.corp.google.com/eng/doc/devguide/cp
Chirantan Ekbote
2014/12/31 00:58:29
MessageLoop::DestructionObserver _is_ a pure inter
gromer
2015/02/10 23:47:10
"In order to ensure that they remain pure interfac
Chirantan Ekbote
2015/02/12 01:49:58
I had no say when this interface was created and i
| |
29 public: | 29 public: |
30 // The delegate is responsible for managing the system level details for | 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 | 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 | 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. | 33 // to outlive the timer in order to clean up after itself. |
34 class Delegate : public base::RefCountedThreadSafe<Delegate> { | 34 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
gromer
2014/12/05 20:13:40
Why not use shared_ptr for thread-safe refcounting
gromer
2014/12/05 20:13:40
Why is this in the public section? It seems like a
gromer
2014/12/05 20:13:40
RtcAlarm is the only implementation of Delegate th
Lei Zhang
2014/12/05 20:58:12
We don't use shared_ptrs or unique_ptrs in Chromiu
Chirantan Ekbote
2014/12/31 00:58:29
As I mentioned above, it's abstracted out to make
Chirantan Ekbote
2014/12/31 00:58:29
The Delegate provides all the platform-specific fu
| |
35 public: | 35 public: |
36 // Initializes the timer. Should return true iff the system has timers that | 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. | 37 // can wake it up from suspend. Will only be called once. |
38 virtual bool Init(base::WeakPtr<AlarmTimer> timer) = 0; | 38 virtual bool Init(base::WeakPtr<AlarmTimer> timer) = 0; |
gromer
2014/12/05 20:13:40
Init() methods are bad news; see http://go/totw:42
Chirantan Ekbote
2014/12/31 00:58:29
So this Init function exists to break a dependency
| |
39 | 39 |
40 // Stops the currently running timer. It should be safe to call this more | 40 // Stops the currently running timer. It should be safe to call this more |
41 // than once. | 41 // than once. |
42 virtual void Stop() = 0; | 42 virtual void Stop() = 0; |
43 | 43 |
44 // Resets the timer to fire after |delay| has passed. Cancels any | 44 // Resets the timer to fire after |delay| has passed. Cancels any |
45 // pre-existing delay. | 45 // pre-existing delay. |
46 virtual void Reset(base::TimeDelta delay) = 0; | 46 virtual void Reset(base::TimeDelta delay) = 0; |
47 | 47 |
48 protected: | 48 protected: |
49 virtual ~Delegate() {} | 49 virtual ~Delegate() {} |
50 | 50 |
51 private: | 51 private: |
52 friend class base::RefCountedThreadSafe<Delegate>; | 52 friend class base::RefCountedThreadSafe<Delegate>; |
gromer
2014/12/05 20:13:41
Why is this needed?
Chirantan Ekbote
2014/12/31 00:58:29
It's recommended by the docstring for base::RefCou
| |
53 }; | 53 }; |
54 | 54 |
55 AlarmTimer(bool retain_user_task, bool is_repeating); | 55 AlarmTimer(bool retain_user_task, bool is_repeating); |
gromer
2014/12/05 20:13:41
https://engdoc.corp.google.com/eng/doc/devguide/cp
gromer
2014/12/05 20:13:41
bool parameters are a readability hazard, especial
Chirantan Ekbote
2014/12/31 00:58:29
I agree wholeheartedly. I was already bit by this
| |
56 | 56 |
57 AlarmTimer(const tracked_objects::Location& posted_from, | 57 AlarmTimer(const tracked_objects::Location& posted_from, |
58 base::TimeDelta delay, | 58 base::TimeDelta delay, |
59 const base::Closure& user_task, | 59 const base::Closure& user_task, |
60 bool is_repeating); | 60 bool is_repeating); |
61 | 61 |
62 ~AlarmTimer() override; | 62 ~AlarmTimer() override; |
63 | 63 |
64 bool can_wake_from_suspend() { return can_wake_from_suspend_; } | 64 bool can_wake_from_suspend() { return can_wake_from_suspend_; } |
gromer
2014/12/05 20:13:40
https://engdoc.corp.google.com/eng/doc/devguide/cp
Chirantan Ekbote
2014/12/31 00:58:29
Done. This was cleaned up in a separate CL.
| |
65 | 65 |
66 // Timer overrides. | 66 // Timer overrides. |
67 void Stop() override; | 67 void Stop() override; |
68 void Reset() override; | 68 void Reset() override; |
69 | 69 |
70 // MessageLoop::DestructionObserver overrides. | 70 // MessageLoop::DestructionObserver overrides. |
71 void WillDestroyCurrentMessageLoop() override; | 71 void WillDestroyCurrentMessageLoop() override; |
72 | 72 |
73 // Must be called by the delegate to indicate that the timer has fired and | 73 // Must be called by the delegate to indicate that the timer has fired and |
74 // that the user task should be run. | 74 // that the user task should be run. |
75 void OnTimerFired(); | 75 void OnTimerFired(); |
76 | 76 |
77 private: | 77 private: |
78 // Initializes the timer with the appropriate delegate. | 78 // Initializes the timer with the appropriate delegate. |
79 void Init(); | 79 void Init(); |
80 | 80 |
81 // Delegate that will manage actually setting the timer. | 81 // Delegate that will manage actually setting the timer. |
82 scoped_refptr<Delegate> delegate_; | 82 scoped_refptr<Delegate> delegate_; |
83 | 83 |
84 // Keeps track of the user task we want to run. A new one is constructed | 84 // Keeps track of the user task we want to run. A new one is constructed |
85 // every time Reset() is called. | 85 // every time Reset() is called. |
86 scoped_ptr<base::PendingTask> pending_task_; | 86 scoped_ptr<base::PendingTask> pending_task_; |
gromer
2014/12/05 20:13:40
Why not unique_ptr?
Chirantan Ekbote
2014/12/31 00:58:29
As Lei mentioned, chromium doesn't use unique_ptr
| |
87 | 87 |
88 // Tracks whether the timer has the ability to wake the system up from | 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 | 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 | 90 // supports being woken up from suspend until the delegate actually tries to |
91 // set it up. | 91 // set it up. |
92 bool can_wake_from_suspend_; | 92 bool can_wake_from_suspend_; |
93 | 93 |
94 // Pointer to the message loop that started the timer. Used to track the | 94 // Pointer to the message loop that started the timer. Used to track the |
95 // destruction of that message loop. | 95 // destruction of that message loop. |
96 base::MessageLoop* origin_message_loop_; | 96 base::MessageLoop* origin_message_loop_; |
97 | 97 |
98 base::WeakPtrFactory<AlarmTimer> weak_factory_; | 98 base::WeakPtrFactory<AlarmTimer> weak_factory_; |
99 | 99 |
100 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); | 100 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); |
101 }; | 101 }; |
102 | 102 |
103 } // namespace timers | 103 } // namespace timers |
104 | 104 |
105 | |
105 #endif // COMPONENTS_TIMER_ALARM_TIMER_H_ | 106 #endif // COMPONENTS_TIMER_ALARM_TIMER_H_ |
OLD | NEW |