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 enum RepeatMode { ONE_SHOT, REPEATING }; |
| 213 BaseTimerMethodPointer(RepeatMode mode) | |
| 214 : Timer(mode == REPEATING, mode == REPEATING) {} | |
| 216 | 215 |
| 217 // Start the timer to run at the given |delay| from now. If the timer is | 216 // 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 | 217 // already running, it will be replaced to call a task formed from |
| 219 // |reviewer->*method|. | 218 // |reviewer->*method|. |
| 220 virtual void Start(const tracked_objects::Location& posted_from, | 219 template <class Receiver> |
| 221 TimeDelta delay, | 220 void Start(const tracked_objects::Location& posted_from, |
| 222 Receiver* receiver, | 221 TimeDelta delay, |
| 223 ReceiverMethod method) { | 222 Receiver* receiver, |
| 223 void (Receiver::*method)()) { | |
| 224 Timer::Start(posted_from, delay, | 224 Timer::Start(posted_from, delay, |
| 225 base::Bind(method, base::Unretained(receiver))); | 225 base::Bind(method, base::Unretained(receiver))); |
| 226 } | 226 } |
| 227 }; | 227 }; |
| 228 | 228 |
| 229 //----------------------------------------------------------------------------- | 229 //----------------------------------------------------------------------------- |
| 230 // A simple, one-shot timer. See usage notes at the top of the file. | 230 // A simple, one-shot timer. See usage notes at the top of the file. |
| 231 template <class Receiver> | 231 class OneShotTimer : public BaseTimerMethodPointer { |
| 232 class OneShotTimer : public BaseTimerMethodPointer<Receiver, false> {}; | 232 public: |
| 233 OneShotTimer() : BaseTimerMethodPointer(ONE_SHOT) {} | |
| 234 }; | |
| 233 | 235 |
| 234 //----------------------------------------------------------------------------- | 236 //----------------------------------------------------------------------------- |
| 235 // A simple, repeating timer. See usage notes at the top of the file. | 237 // A simple, repeating timer. See usage notes at the top of the file. |
| 236 template <class Receiver> | 238 class RepeatingTimer : public BaseTimerMethodPointer { |
| 237 class RepeatingTimer : public BaseTimerMethodPointer<Receiver, true> {}; | 239 public: |
| 240 RepeatingTimer() : BaseTimerMethodPointer(REPEATING) {} | |
| 241 }; | |
| 238 | 242 |
| 239 //----------------------------------------------------------------------------- | 243 //----------------------------------------------------------------------------- |
| 240 // A Delay timer is like The Button from Lost. Once started, you have to keep | 244 // 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 | 245 // calling Reset otherwise it will call the given method in the MessageLoop |
| 242 // thread. | 246 // thread. |
| 243 // | 247 // |
| 244 // Once created, it is inactive until Reset is called. Once |delay| seconds have | 248 // 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 | 249 // 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. | 250 // has been made, it's inactive until Reset is called again. |
| 247 // | 251 // |
| 248 // If destroyed, the timeout is canceled and will not occur even if already | 252 // If destroyed, the timeout is canceled and will not occur even if already |
| 249 // inflight. | 253 // inflight. |
| 250 template <class Receiver> | 254 class BASE_EXPORT DelayTimer : protected Timer { |
| 251 class DelayTimer : protected Timer { | |
| 252 public: | 255 public: |
| 253 typedef void (Receiver::*ReceiverMethod)(); | 256 template <class Receiver> |
| 254 | 257 BASE_EXPORT DelayTimer(const tracked_objects::Location& posted_from, |
|
Nico
2015/09/23 00:21:25
Huh, that's really needed? Surprising, given that
danakj
2015/09/23 19:00:32
Ya.. compiler complains both with and without this
| |
| 255 DelayTimer(const tracked_objects::Location& posted_from, | 258 TimeDelta delay, |
| 256 TimeDelta delay, | 259 Receiver* receiver, |
| 257 Receiver* receiver, | 260 void (Receiver::*method)()) |
| 258 ReceiverMethod method) | 261 : Timer(posted_from, |
| 259 : Timer(posted_from, delay, | 262 delay, |
| 260 base::Bind(method, base::Unretained(receiver)), | 263 base::Bind(method, base::Unretained(receiver)), |
| 261 false) {} | 264 false) {} |
| 262 | 265 |
| 263 void Reset() override { Timer::Reset(); } | 266 void Reset() override; |
| 264 }; | 267 }; |
| 265 | 268 |
| 266 } // namespace base | 269 } // namespace base |
| 267 | 270 |
| 268 #endif // BASE_TIMER_TIMER_H_ | 271 #endif // BASE_TIMER_TIMER_H_ |
| OLD | NEW |