OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // OneShotTimer and RepeatingTimer provide a simple timer API. As the names | 5 // OneShotTimer and RepeatingTimer provide a simple timer API. As the names |
6 // suggest, OneShotTimer calls you back once after a time delay expires. | 6 // suggest, OneShotTimer calls you back once after a time delay expires. |
7 // RepeatingTimer on the other hand calls you back periodically with the | 7 // RepeatingTimer on the other hand calls you back periodically with the |
8 // prescribed time interval. | 8 // prescribed time interval. |
9 // | 9 // |
10 // OneShotTimer and RepeatingTimer both cancel the timer when they go out of | 10 // OneShotTimer and RepeatingTimer both cancel the timer when they go out of |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
48 // disabled ones) from timer_unittests.cc pass locally. Some are disabled | 48 // disabled ones) from timer_unittests.cc pass locally. Some are disabled |
49 // because they're flaky on the buildbot, but when you run them locally you | 49 // because they're flaky on the buildbot, but when you run them locally you |
50 // should be able to tell the difference. | 50 // should be able to tell the difference. |
51 | 51 |
52 #include "base/base_export.h" | 52 #include "base/base_export.h" |
53 #include "base/basictypes.h" | 53 #include "base/basictypes.h" |
54 #include "base/bind.h" | 54 #include "base/bind.h" |
55 #include "base/bind_helpers.h" | 55 #include "base/bind_helpers.h" |
56 #include "base/callback.h" | 56 #include "base/callback.h" |
57 #include "base/location.h" | 57 #include "base/location.h" |
58 #include "base/single_thread_task_runner.h" | |
danakj
2014/10/22 15:07:43
can this be forward declared in this header?
petrcermak
2014/10/23 10:59:37
Done.
| |
58 #include "base/time/time.h" | 59 #include "base/time/time.h" |
59 | 60 |
60 namespace base { | 61 namespace base { |
61 | 62 |
62 class BaseTimerTaskInternal; | 63 class BaseTimerTaskInternal; |
63 | 64 |
64 //----------------------------------------------------------------------------- | 65 //----------------------------------------------------------------------------- |
65 // This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating | 66 // This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating |
66 // tasks. It must be destructed on the same thread that starts tasks. There are | 67 // tasks. It must be destructed on the same thread that starts tasks. There are |
67 // DCHECKs in place to verify this. | 68 // DCHECKs in place to verify this. |
(...skipping 12 matching lines...) Expand all Loading... | |
80 bool is_repeating); | 81 bool is_repeating); |
81 | 82 |
82 virtual ~Timer(); | 83 virtual ~Timer(); |
83 | 84 |
84 // Returns true if the timer is running (i.e., not stopped). | 85 // Returns true if the timer is running (i.e., not stopped). |
85 virtual bool IsRunning() const; | 86 virtual bool IsRunning() const; |
86 | 87 |
87 // Returns the current delay for this timer. | 88 // Returns the current delay for this timer. |
88 virtual TimeDelta GetCurrentDelay() const; | 89 virtual TimeDelta GetCurrentDelay() const; |
89 | 90 |
91 // Set the task runner on which the task should be scheduled. This method can | |
92 // only be called before any tasks have been scheduled. | |
93 virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); | |
94 | |
90 // Start the timer to run at the given |delay| from now. If the timer is | 95 // Start the timer to run at the given |delay| from now. If the timer is |
91 // already running, it will be replaced to call the given |user_task|. | 96 // already running, it will be replaced to call the given |user_task|. |
92 virtual void Start(const tracked_objects::Location& posted_from, | 97 virtual void Start(const tracked_objects::Location& posted_from, |
93 TimeDelta delay, | 98 TimeDelta delay, |
94 const base::Closure& user_task); | 99 const base::Closure& user_task); |
95 | 100 |
96 // Call this method to stop and cancel the timer. It is a no-op if the timer | 101 // Call this method to stop and cancel the timer. It is a no-op if the timer |
97 // is not running. | 102 // is not running. |
98 virtual void Stop(); | 103 virtual void Stop(); |
99 | 104 |
(...skipping 15 matching lines...) Expand all Loading... | |
115 bool is_repeating() const { return is_repeating_; } | 120 bool is_repeating() const { return is_repeating_; } |
116 | 121 |
117 private: | 122 private: |
118 friend class BaseTimerTaskInternal; | 123 friend class BaseTimerTaskInternal; |
119 | 124 |
120 // Allocates a new scheduled_task_ and posts it on the current MessageLoop | 125 // Allocates a new scheduled_task_ and posts it on the current MessageLoop |
121 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ | 126 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ |
122 // and desired_run_time_ are reset to Now() + delay. | 127 // and desired_run_time_ are reset to Now() + delay. |
123 void PostNewScheduledTask(TimeDelta delay); | 128 void PostNewScheduledTask(TimeDelta delay); |
124 | 129 |
130 // Returns the task runner on which the task should be scheduled. If the | |
131 // corresponding task_runner_ field is null, the task runner for the current | |
132 // thread is returned. | |
133 scoped_refptr<SingleThreadTaskRunner> GetTaskRunner(); | |
134 | |
125 // Disable scheduled_task_ and abandon it so that it no longer refers back to | 135 // Disable scheduled_task_ and abandon it so that it no longer refers back to |
126 // this object. | 136 // this object. |
127 void AbandonScheduledTask(); | 137 void AbandonScheduledTask(); |
128 | 138 |
129 // Called by BaseTimerTaskInternal when the MessageLoop runs it. | 139 // Called by BaseTimerTaskInternal when the MessageLoop runs it. |
130 void RunScheduledTask(); | 140 void RunScheduledTask(); |
131 | 141 |
132 // Stop running task (if any) and abandon scheduled task (if any). | 142 // Stop running task (if any) and abandon scheduled task (if any). |
133 void StopAndAbandon() { | 143 void StopAndAbandon() { |
134 Stop(); | 144 Stop(); |
135 AbandonScheduledTask(); | 145 AbandonScheduledTask(); |
136 } | 146 } |
137 | 147 |
138 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call | 148 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call |
139 // RunScheduledTask() at scheduled_run_time_. | 149 // RunScheduledTask() at scheduled_run_time_. |
140 BaseTimerTaskInternal* scheduled_task_; | 150 BaseTimerTaskInternal* scheduled_task_; |
141 | 151 |
152 // The task runner on which the task should be scheduled. If it is null, the | |
153 // task runner for the current thread should be used. | |
154 scoped_refptr<SingleThreadTaskRunner> task_runner_; | |
155 | |
142 // Location in user code. | 156 // Location in user code. |
143 tracked_objects::Location posted_from_; | 157 tracked_objects::Location posted_from_; |
144 // Delay requested by user. | 158 // Delay requested by user. |
145 TimeDelta delay_; | 159 TimeDelta delay_; |
146 // user_task_ is what the user wants to be run at desired_run_time_. | 160 // user_task_ is what the user wants to be run at desired_run_time_. |
147 base::Closure user_task_; | 161 base::Closure user_task_; |
148 | 162 |
149 // The estimated time that the MessageLoop will run the scheduled_task_ that | 163 // The estimated time that the MessageLoop will run the scheduled_task_ that |
150 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the | 164 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the |
151 // task must be run immediately. | 165 // task must be run immediately. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 : Timer(posted_from, delay, | 252 : Timer(posted_from, delay, |
239 base::Bind(method, base::Unretained(receiver)), | 253 base::Bind(method, base::Unretained(receiver)), |
240 false) {} | 254 false) {} |
241 | 255 |
242 void Reset() { Timer::Reset(); } | 256 void Reset() { Timer::Reset(); } |
243 }; | 257 }; |
244 | 258 |
245 } // namespace base | 259 } // namespace base |
246 | 260 |
247 #endif // BASE_TIMER_TIMER_H_ | 261 #endif // BASE_TIMER_TIMER_H_ |
OLD | NEW |