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

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

Issue 2394333002: Revert of Use FileDescriptorWatcher in AlarmTimer. (Closed)
Patch Set: Created 4 years, 2 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
« no previous file with comments | « no previous file | components/timers/alarm_timer_chromeos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_TIMERS_ALARM_TIMER_CHROMEOS_H_ 5 #ifndef COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_
6 #define COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ 6 #define COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/files/file_descriptor_watcher_posix.h" 10 #include "base/callback.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/sequenced_task_runner_handle.h"
15 #include "base/time/time.h" 14 #include "base/time/time.h"
16 #include "base/timer/timer.h" 15 #include "base/timer/timer.h"
17 16
18 namespace base { 17 namespace base {
18 class MessageLoop;
19 struct PendingTask; 19 struct PendingTask;
20 } 20 }
21 21
22 namespace timers { 22 namespace timers {
23 // The class implements a timer that is capable of waking the system up from a 23 // The class implements a timer that is capable of waking the system up from a
24 // suspended state. For example, this is useful for running tasks that are 24 // suspended state. For example, this is useful for running tasks that are
25 // needed for maintaining network connectivity, like sending heartbeat messages. 25 // needed for maintaining network connectivity, like sending heartbeat messages.
26 // Currently, this feature is only available on Chrome OS systems running linux 26 // Currently, this feature is only available on Chrome OS systems running linux
27 // version 3.11 or higher. On all other platforms, the AlarmTimer behaves 27 // version 3.11 or higher. On all other platforms, the AlarmTimer behaves
28 // exactly the same way as a regular Timer. 28 // exactly the same way as a regular Timer.
29 //
30 // An AlarmTimer instance can only be used from the sequence on which it was
31 // instantiated. Start() and Stop() must be called from a thread that supports
32 // FileDescriptorWatcher.
33 class AlarmTimer : public base::Timer { 29 class AlarmTimer : public base::Timer {
34 public: 30 public:
35 ~AlarmTimer() override; 31 ~AlarmTimer() override;
36 32
33 bool can_wake_from_suspend() const { return can_wake_from_suspend_; }
34
35 // Sets a hook that will be called when the timer fires and a task has been
36 // queued on |origin_message_loop_|. Used by tests to wait until a task is
37 // pending in the MessageLoop.
38 void SetTimerFiredCallbackForTest(base::Closure test_callback);
39
37 // Timer overrides. 40 // Timer overrides.
38 void Stop() override; 41 void Stop() override;
39 void Reset() override; 42 void Reset() override;
40 43
41 protected: 44 protected:
45 // The constructors for this class are protected because consumers should
46 // instantiate one of the specialized sub-classes defined below instead.
42 AlarmTimer(bool retain_user_task, bool is_repeating); 47 AlarmTimer(bool retain_user_task, bool is_repeating);
48 AlarmTimer(const tracked_objects::Location& posted_from,
49 base::TimeDelta delay,
50 const base::Closure& user_task,
51 bool is_repeating);
43 52
44 private: 53 private:
45 // Called when |alarm_fd_| is readable without blocking. Reads data from 54 // Common initialization that must be performed by both constructors. This
46 // |alarm_fd_| and calls OnTimerFired(). 55 // really should live in a delegated constructor but the way base::Timer's
47 void OnAlarmFdReadableWithoutBlocking(); 56 // constructors are written makes it really hard to do so.
57 void Init();
48 58
49 // Called when the timer fires. Runs the callback. 59 // Will be called by the delegate to indicate that the timer has fired and
60 // that the user task should be run.
50 void OnTimerFired(); 61 void OnTimerFired();
51 62
52 // Tracks whether the timer has the ability to wake the system up from 63 // Called when |origin_message_loop_| will be destroyed.
53 // suspend. This is a runtime check because we won't know if the system 64 void WillDestroyCurrentMessageLoop();
54 // supports being woken up from suspend until the constructor actually tries
55 // to set it up.
56 bool CanWakeFromSuspend() const;
57 65
58 // Timer file descriptor. 66 // Delegate that will manage actually setting the timer.
59 const int alarm_fd_; 67 class Delegate;
68 scoped_refptr<Delegate> delegate_;
60 69
61 // Watches |alarm_fd_|. 70 // Keeps track of the user task we want to run. A new one is constructed
62 std::unique_ptr<base::FileDescriptorWatcher::Controller> alarm_fd_watcher_; 71 // every time Reset() is called.
63
64 // Posts tasks to the sequence on which this AlarmTimer was instantiated.
65 const scoped_refptr<base::SequencedTaskRunner> origin_task_runner_ =
66 base::SequencedTaskRunnerHandle::Get();
67
68 // Keeps track of the user task we want to run. A new one is constructed every
69 // time Reset() is called.
70 std::unique_ptr<base::PendingTask> pending_task_; 72 std::unique_ptr<base::PendingTask> pending_task_;
71 73
72 // Used to invalidate pending callbacks. 74 // Tracks whether the timer has the ability to wake the system up from
75 // suspend. This is a runtime check because we won't know if the system
76 // supports being woken up from suspend until the delegate actually tries to
77 // set it up.
78 bool can_wake_from_suspend_;
79
80 // Pointer to the message loop that started the timer. Used to track the
81 // destruction of that message loop.
82 base::MessageLoop* origin_message_loop_;
83
84 // Observes |origin_message_loop_| and informs this class if it will be
85 // destroyed.
86 class MessageLoopObserver;
87 std::unique_ptr<MessageLoopObserver> message_loop_observer_;
88
73 base::WeakPtrFactory<AlarmTimer> weak_factory_; 89 base::WeakPtrFactory<AlarmTimer> weak_factory_;
74 90
75 DISALLOW_COPY_AND_ASSIGN(AlarmTimer); 91 DISALLOW_COPY_AND_ASSIGN(AlarmTimer);
76 }; 92 };
77 93
78 // As its name suggests, a OneShotAlarmTimer runs a given task once. It does 94 // As its name suggests, a OneShotAlarmTimer runs a given task once. It does
79 // not remember the task that was given to it after it has fired and does not 95 // not remember the task that was given to it after it has fired and does not
80 // repeat. Useful for fire-and-forget tasks. 96 // repeat. Useful for fire-and-forget tasks.
81 class OneShotAlarmTimer : public AlarmTimer { 97 class OneShotAlarmTimer : public AlarmTimer {
82 public: 98 public:
99 // Constructs a basic OneShotAlarmTimer. An AlarmTimer constructed this way
100 // requires that Start() is called before Reset() is called.
83 OneShotAlarmTimer(); 101 OneShotAlarmTimer();
84 ~OneShotAlarmTimer() override; 102 ~OneShotAlarmTimer() override;
85 }; 103 };
86 104
87 // A RepeatingAlarmTimer takes a task and delay and repeatedly runs the task 105 // A RepeatingAlarmTimer takes a task and delay and repeatedly runs the task
88 // using the specified delay as an interval between the runs until it is 106 // using the specified delay as an interval between the runs until it is
89 // explicitly stopped. It remembers both the task and the delay it was given 107 // explicitly stopped. It remembers both the task and the delay it was given
90 // after it fires. 108 // after it fires.
91 class RepeatingAlarmTimer : public AlarmTimer { 109 class RepeatingAlarmTimer : public AlarmTimer {
92 public: 110 public:
111 // Constructs a basic RepeatingAlarmTimer. An AlarmTimer constructed this way
112 // requires that Start() is called before Reset() is called.
93 RepeatingAlarmTimer(); 113 RepeatingAlarmTimer();
114
115 // Constructs a RepeatingAlarmTimer with pre-populated parameters but does not
116 // start it. Useful if |user_task| or |delay| are not going to change.
117 // Reset() can be called immediately after constructing an AlarmTimer in this
118 // way.
119 RepeatingAlarmTimer(const tracked_objects::Location& posted_from,
120 base::TimeDelta delay,
121 const base::Closure& user_task);
122
94 ~RepeatingAlarmTimer() override; 123 ~RepeatingAlarmTimer() override;
95 }; 124 };
96 125
97 // A SimpleAlarmTimer only fires once but remembers the task that it was given 126 // A SimpleAlarmTimer only fires once but remembers the task that it was given
98 // even after it has fired. Useful if you want to run the same task multiple 127 // even after it has fired. Useful if you want to run the same task multiple
99 // times but not at a regular interval. 128 // times but not at a regular interval.
100 class SimpleAlarmTimer : public AlarmTimer { 129 class SimpleAlarmTimer : public AlarmTimer {
101 public: 130 public:
131 // Constructs a basic SimpleAlarmTimer. An AlarmTimer constructed this way
132 // requires that Start() is called before Reset() is called.
102 SimpleAlarmTimer(); 133 SimpleAlarmTimer();
134
135 // Constructs a SimpleAlarmTimer with pre-populated parameters but does not
136 // start it. Useful if |user_task| or |delay| are not going to change.
137 // Reset() can be called immediately after constructing an AlarmTimer in this
138 // way.
139 SimpleAlarmTimer(const tracked_objects::Location& posted_from,
140 base::TimeDelta delay,
141 const base::Closure& user_task);
142
103 ~SimpleAlarmTimer() override; 143 ~SimpleAlarmTimer() override;
104 }; 144 };
105 145
106 } // namespace timers 146 } // namespace timers
107 147
108 #endif // COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_ 148 #endif // COMPONENTS_TIMERS_ALARM_TIMER_CHROMEOS_H_
OLDNEW
« no previous file with comments | « no previous file | components/timers/alarm_timer_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698