Chromium Code Reviews| 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 11 matching lines...) Expand all Loading... | |
| 22 // this, &MyClass::DoStuff); | 22 // this, &MyClass::DoStuff); |
| 23 // } | 23 // } |
| 24 // void StopDoingStuff() { | 24 // void StopDoingStuff() { |
| 25 // timer_.Stop(); | 25 // timer_.Stop(); |
| 26 // } | 26 // } |
| 27 // private: | 27 // private: |
| 28 // void DoStuff() { | 28 // void DoStuff() { |
| 29 // // This method is called every second to do stuff. | 29 // // This method is called every second to do stuff. |
| 30 // ... | 30 // ... |
| 31 // } | 31 // } |
| 32 // base::RepeatingTimer<MyClass> timer_; | 32 // base::RepeatingTimer timer_; |
| 33 // }; | 33 // }; |
| 34 // | 34 // |
| 35 // Both OneShotTimer and RepeatingTimer also support a Reset method, which | 35 // Both OneShotTimer and RepeatingTimer also support a Reset method, which |
| 36 // allows you to easily defer the timer event until the timer delay passes once | 36 // allows you to easily defer the timer event until the timer delay passes once |
| 37 // again. So, in the above example, if 0.5 seconds have already passed, | 37 // again. So, in the above example, if 0.5 seconds have already passed, |
| 38 // calling Reset on timer_ would postpone DoStuff by another 1 second. In | 38 // calling Reset on timer_ would postpone DoStuff by another 1 second. In |
| 39 // other words, Reset is shorthand for calling Stop and then Start again with | 39 // other words, Reset is shorthand for calling Stop and then Start again with |
| 40 // the same arguments. | 40 // the same arguments. |
| 41 // | 41 // |
| 42 // NOTE: These APIs are not thread safe. Always call from the same thread. | 42 // NOTE: These APIs are not thread safe. Always call from the same thread. |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 | 193 |
| 194 // If true, user_task_ is scheduled to run sometime in the future. | 194 // If true, user_task_ is scheduled to run sometime in the future. |
| 195 bool is_running_; | 195 bool is_running_; |
| 196 | 196 |
| 197 DISALLOW_COPY_AND_ASSIGN(Timer); | 197 DISALLOW_COPY_AND_ASSIGN(Timer); |
| 198 }; | 198 }; |
| 199 | 199 |
| 200 //----------------------------------------------------------------------------- | 200 //----------------------------------------------------------------------------- |
| 201 // This class is an implementation detail of OneShotTimer and RepeatingTimer. | 201 // This class is an implementation detail of OneShotTimer and RepeatingTimer. |
| 202 // Please do not use this class directly. | 202 // Please do not use this class directly. |
| 203 template <class Receiver, bool kIsRepeating> | |
| 204 class BaseTimerMethodPointer : public Timer { | 203 class BaseTimerMethodPointer : public Timer { |
| 205 public: | 204 public: |
| 206 typedef void (Receiver::*ReceiverMethod)(); | |
| 207 | |
| 208 // This is here to work around the fact that Timer::Start is "hidden" by the | 205 // This is here to work around the fact that Timer::Start is "hidden" by the |
| 209 // Start definition below, rather than being overloaded. | 206 // Start definition below, rather than being overloaded. |
| 210 // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below | 207 // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below |
| 211 // and convert callers to use the base::Closure version in Timer::Start, | 208 // and convert callers to use the base::Closure version in Timer::Start, |
| 212 // see bug 148832. | 209 // see bug 148832. |
| 213 using Timer::Start; | 210 using Timer::Start; |
| 214 | 211 |
| 215 BaseTimerMethodPointer() : Timer(kIsRepeating, kIsRepeating) {} | 212 BaseTimerMethodPointer(bool repeats) : Timer(repeats, repeats) {} |
|
Nico
2015/09/22 01:16:22
nit:
enum RepeatMode { kOneShot, kRepeating };
danakj
2015/09/22 18:31:14
Done.
| |
| 216 | 213 |
| 217 // Start the timer to run at the given |delay| from now. If the timer is | 214 // Start the timer to run at the given |delay| from now. If the timer is |
| 218 // already running, it will be replaced to call a task formed from | 215 // already running, it will be replaced to call a task formed from |
| 219 // |reviewer->*method|. | 216 // |reviewer->*method|. |
| 220 virtual void Start(const tracked_objects::Location& posted_from, | 217 template <class Receiver> |
| 221 TimeDelta delay, | 218 void Start(const tracked_objects::Location& posted_from, |
| 222 Receiver* receiver, | 219 TimeDelta delay, |
| 223 ReceiverMethod method) { | 220 Receiver* receiver, |
| 221 void (Receiver::*method)()) { | |
| 224 Timer::Start(posted_from, delay, | 222 Timer::Start(posted_from, delay, |
| 225 base::Bind(method, base::Unretained(receiver))); | 223 base::Bind(method, base::Unretained(receiver))); |
| 226 } | 224 } |
| 227 }; | 225 }; |
| 228 | 226 |
| 229 //----------------------------------------------------------------------------- | 227 //----------------------------------------------------------------------------- |
| 230 // A simple, one-shot timer. See usage notes at the top of the file. | 228 // A simple, one-shot timer. See usage notes at the top of the file. |
| 231 template <class Receiver> | 229 class OneShotTimer : public BaseTimerMethodPointer { |
| 232 class OneShotTimer : public BaseTimerMethodPointer<Receiver, false> {}; | 230 public: |
| 231 OneShotTimer() : BaseTimerMethodPointer(false) {} | |
| 232 }; | |
| 233 | 233 |
| 234 //----------------------------------------------------------------------------- | 234 //----------------------------------------------------------------------------- |
| 235 // A simple, repeating timer. See usage notes at the top of the file. | 235 // A simple, repeating timer. See usage notes at the top of the file. |
| 236 template <class Receiver> | 236 class RepeatingTimer : public BaseTimerMethodPointer { |
| 237 class RepeatingTimer : public BaseTimerMethodPointer<Receiver, true> {}; | 237 public: |
| 238 RepeatingTimer() : BaseTimerMethodPointer(true) {} | |
| 239 }; | |
| 238 | 240 |
| 239 //----------------------------------------------------------------------------- | 241 //----------------------------------------------------------------------------- |
| 240 // A Delay timer is like The Button from Lost. Once started, you have to keep | 242 // A Delay timer is like The Button from Lost. Once started, you have to keep |
| 241 // calling Reset otherwise it will call the given method in the MessageLoop | 243 // calling Reset otherwise it will call the given method in the MessageLoop |
| 242 // thread. | 244 // thread. |
| 243 // | 245 // |
| 244 // Once created, it is inactive until Reset is called. Once |delay| seconds have | 246 // Once created, it is inactive until Reset is called. Once |delay| seconds have |
| 245 // passed since the last call to Reset, the callback is made. Once the callback | 247 // passed since the last call to Reset, the callback is made. Once the callback |
| 246 // has been made, it's inactive until Reset is called again. | 248 // has been made, it's inactive until Reset is called again. |
| 247 // | 249 // |
| 248 // If destroyed, the timeout is canceled and will not occur even if already | 250 // If destroyed, the timeout is canceled and will not occur even if already |
| 249 // inflight. | 251 // inflight. |
| 250 template <class Receiver> | |
| 251 class DelayTimer : protected Timer { | 252 class DelayTimer : protected Timer { |
| 252 public: | 253 public: |
| 253 typedef void (Receiver::*ReceiverMethod)(); | 254 template <class Receiver> |
| 254 | |
| 255 DelayTimer(const tracked_objects::Location& posted_from, | 255 DelayTimer(const tracked_objects::Location& posted_from, |
| 256 TimeDelta delay, | 256 TimeDelta delay, |
| 257 Receiver* receiver, | 257 Receiver* receiver, |
| 258 ReceiverMethod method) | 258 void (Receiver::*method)()) |
| 259 : Timer(posted_from, delay, | 259 : Timer(posted_from, |
| 260 delay, | |
| 260 base::Bind(method, base::Unretained(receiver)), | 261 base::Bind(method, base::Unretained(receiver)), |
| 261 false) {} | 262 false) {} |
| 262 | 263 |
| 263 void Reset() override { Timer::Reset(); } | 264 void Reset() override; |
| 264 }; | 265 }; |
| 265 | 266 |
| 266 } // namespace base | 267 } // namespace base |
| 267 | 268 |
| 268 #endif // BASE_TIMER_TIMER_H_ | 269 #endif // BASE_TIMER_TIMER_H_ |
| OLD | NEW |