| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // tasks. It must be destructed on the same thread that starts tasks. There are | 70 // tasks. It must be destructed on the same thread that starts tasks. There are |
| 71 // DCHECKs in place to verify this. | 71 // DCHECKs in place to verify this. |
| 72 // | 72 // |
| 73 class BASE_EXPORT Timer { | 73 class BASE_EXPORT Timer { |
| 74 public: | 74 public: |
| 75 // Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must | 75 // Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must |
| 76 // be called later to set task info. |retain_user_task| determines whether the | 76 // be called later to set task info. |retain_user_task| determines whether the |
| 77 // user_task is retained or reset when it runs or stops. If |tick_clock| is | 77 // user_task is retained or reset when it runs or stops. If |tick_clock| is |
| 78 // provided, it is used instead of TimeTicks::Now() to get TimeTicks when | 78 // provided, it is used instead of TimeTicks::Now() to get TimeTicks when |
| 79 // scheduling tasks. | 79 // scheduling tasks. |
| 80 Timer(bool retain_user_task, bool is_repeating); | 80 Timer(bool retain_user_task, |
| 81 Timer(bool retain_user_task, bool is_repeating, TickClock* tick_clock); | 81 bool is_repeating, |
| 82 TickClock* tick_clock = nullptr); |
| 82 | 83 |
| 83 // Construct a timer with retained task info. If |tick_clock| is provided, it | 84 // Construct a timer with retained task info. If |tick_clock| is provided, it |
| 84 // is used instead of TimeTicks::Now() to get TimeTicks when scheduling tasks. | 85 // is used instead of TimeTicks::Now() to get TimeTicks when scheduling tasks. |
| 85 Timer(const tracked_objects::Location& posted_from, | 86 Timer(const tracked_objects::Location& posted_from, |
| 86 TimeDelta delay, | 87 TimeDelta delay, |
| 87 const base::Closure& user_task, | 88 const base::Closure& user_task, |
| 88 bool is_repeating); | |
| 89 Timer(const tracked_objects::Location& posted_from, | |
| 90 TimeDelta delay, | |
| 91 const base::Closure& user_task, | |
| 92 bool is_repeating, | 89 bool is_repeating, |
| 93 TickClock* tick_clock); | 90 TickClock* tick_clock = nullptr); |
| 94 | 91 |
| 95 virtual ~Timer(); | 92 virtual ~Timer(); |
| 96 | 93 |
| 97 // Returns true if the timer is running (i.e., not stopped). | 94 // Returns true if the timer is running (i.e., not stopped). |
| 98 virtual bool IsRunning() const; | 95 virtual bool IsRunning() const; |
| 99 | 96 |
| 100 // Returns the current delay for this timer. | 97 // Returns the current delay for this timer. |
| 101 virtual TimeDelta GetCurrentDelay() const; | 98 virtual TimeDelta GetCurrentDelay() const; |
| 102 | 99 |
| 103 // Set the task runner on which the task should be scheduled. This method can | 100 // Set the task runner on which the task should be scheduled. This method can |
| 104 // only be called before any tasks have been scheduled. The task runner must | 101 // only be called before any tasks have been scheduled. The task runner must |
| 105 // run tasks on the same thread the timer is used on. | 102 // run tasks on the same thread the timer is used on. |
| 106 virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); | 103 virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); |
| 107 | 104 |
| 108 // Start the timer to run at the given |delay| from now. If the timer is | 105 // Start the timer to run at the given |delay| from now. If the timer is |
| 109 // already running, it will be replaced to call the given |user_task|. | 106 // already running, it will be replaced to call the given |user_task|. |
| 110 virtual void Start(const tracked_objects::Location& posted_from, | 107 virtual void Start(const tracked_objects::Location& posted_from, |
| 111 TimeDelta delay, | 108 TimeDelta delay, |
| 112 const base::Closure& user_task); | 109 const base::Closure& user_task); |
| 113 | 110 |
| 111 // Start the timer to run at the given |delay| from now. If the timer is |
| 112 // already running, it will be replaced to call a task formed from |
| 113 // |reviewer->*method|. |
| 114 template <class Receiver> |
| 115 void Start(const tracked_objects::Location& posted_from, |
| 116 TimeDelta delay, |
| 117 Receiver* receiver, |
| 118 void (Receiver::*method)()) { |
| 119 Timer::Start(posted_from, delay, |
| 120 base::Bind(method, base::Unretained(receiver))); |
| 121 } |
| 122 |
| 114 // Call this method to stop and cancel the timer. It is a no-op if the timer | 123 // Call this method to stop and cancel the timer. It is a no-op if the timer |
| 115 // is not running. | 124 // is not running. |
| 116 virtual void Stop(); | 125 virtual void Stop(); |
| 117 | 126 |
| 118 // Call this method to reset the timer delay. The user_task_ must be set. If | 127 // Call this method to reset the timer delay. The user_task_ must be set. If |
| 119 // the timer is not running, this will start it by posting a task. | 128 // the timer is not running, this will start it by posting a task. |
| 120 virtual void Reset(); | 129 virtual void Reset(); |
| 121 | 130 |
| 122 const base::Closure& user_task() const { return user_task_; } | 131 const base::Closure& user_task() const { return user_task_; } |
| 123 const TimeTicks& desired_run_time() const { return desired_run_time_; } | 132 const TimeTicks& desired_run_time() const { return desired_run_time_; } |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 // The tick clock used to calculate the run time for scheduled tasks. | 220 // The tick clock used to calculate the run time for scheduled tasks. |
| 212 TickClock* const tick_clock_; | 221 TickClock* const tick_clock_; |
| 213 | 222 |
| 214 // If true, user_task_ is scheduled to run sometime in the future. | 223 // If true, user_task_ is scheduled to run sometime in the future. |
| 215 bool is_running_; | 224 bool is_running_; |
| 216 | 225 |
| 217 DISALLOW_COPY_AND_ASSIGN(Timer); | 226 DISALLOW_COPY_AND_ASSIGN(Timer); |
| 218 }; | 227 }; |
| 219 | 228 |
| 220 //----------------------------------------------------------------------------- | 229 //----------------------------------------------------------------------------- |
| 221 // This class is an implementation detail of OneShotTimer and RepeatingTimer. | |
| 222 // Please do not use this class directly. | |
| 223 class BaseTimerMethodPointer : public Timer { | |
| 224 public: | |
| 225 // This is here to work around the fact that Timer::Start is "hidden" by the | |
| 226 // Start definition below, rather than being overloaded. | |
| 227 // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below | |
| 228 // and convert callers to use the base::Closure version in Timer::Start, | |
| 229 // see bug 148832. | |
| 230 using Timer::Start; | |
| 231 | |
| 232 enum RepeatMode { ONE_SHOT, REPEATING }; | |
| 233 BaseTimerMethodPointer(RepeatMode mode, TickClock* tick_clock) | |
| 234 : Timer(mode == REPEATING, mode == REPEATING, tick_clock) {} | |
| 235 | |
| 236 // Start the timer to run at the given |delay| from now. If the timer is | |
| 237 // already running, it will be replaced to call a task formed from | |
| 238 // |reviewer->*method|. | |
| 239 template <class Receiver> | |
| 240 void Start(const tracked_objects::Location& posted_from, | |
| 241 TimeDelta delay, | |
| 242 Receiver* receiver, | |
| 243 void (Receiver::*method)()) { | |
| 244 Timer::Start(posted_from, delay, | |
| 245 base::Bind(method, base::Unretained(receiver))); | |
| 246 } | |
| 247 }; | |
| 248 | |
| 249 //----------------------------------------------------------------------------- | |
| 250 // 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. |
| 251 class OneShotTimer : public BaseTimerMethodPointer { | 231 class OneShotTimer : public Timer { |
| 252 public: | 232 public: |
| 253 OneShotTimer() : OneShotTimer(nullptr) {} | 233 explicit OneShotTimer(TickClock* tick_clock = nullptr) |
| 254 explicit OneShotTimer(TickClock* tick_clock) | 234 : Timer(false, false, tick_clock) {} |
| 255 : BaseTimerMethodPointer(ONE_SHOT, tick_clock) {} | |
| 256 }; | 235 }; |
| 257 | 236 |
| 258 //----------------------------------------------------------------------------- | 237 //----------------------------------------------------------------------------- |
| 259 // A simple, repeating timer. See usage notes at the top of the file. | 238 // A simple, repeating timer. See usage notes at the top of the file. |
| 260 class RepeatingTimer : public BaseTimerMethodPointer { | 239 class RepeatingTimer : public Timer { |
| 261 public: | 240 public: |
| 262 RepeatingTimer() : RepeatingTimer(nullptr) {} | 241 explicit RepeatingTimer(TickClock* tick_clock = nullptr) |
| 263 explicit RepeatingTimer(TickClock* tick_clock) | 242 : Timer(true, true, tick_clock) {} |
| 264 : BaseTimerMethodPointer(REPEATING, tick_clock) {} | |
| 265 }; | 243 }; |
| 266 | 244 |
| 267 //----------------------------------------------------------------------------- | 245 //----------------------------------------------------------------------------- |
| 268 // A Delay timer is like The Button from Lost. Once started, you have to keep | 246 // A Delay timer is like The Button from Lost. Once started, you have to keep |
| 269 // calling Reset otherwise it will call the given method in the MessageLoop | 247 // calling Reset otherwise it will call the given method in the MessageLoop |
| 270 // thread. | 248 // thread. |
| 271 // | 249 // |
| 272 // Once created, it is inactive until Reset is called. Once |delay| seconds have | 250 // Once created, it is inactive until Reset is called. Once |delay| seconds have |
| 273 // passed since the last call to Reset, the callback is made. Once the callback | 251 // passed since the last call to Reset, the callback is made. Once the callback |
| 274 // has been made, it's inactive until Reset is called again. | 252 // has been made, it's inactive until Reset is called again. |
| 275 // | 253 // |
| 276 // If destroyed, the timeout is canceled and will not occur even if already | 254 // If destroyed, the timeout is canceled and will not occur even if already |
| 277 // inflight. | 255 // inflight. |
| 278 class DelayTimer : protected Timer { | 256 class DelayTimer { |
| 279 public: | 257 public: |
| 280 template <class Receiver> | 258 template <class Receiver> |
| 281 DelayTimer(const tracked_objects::Location& posted_from, | 259 DelayTimer(const tracked_objects::Location& posted_from, |
| 282 TimeDelta delay, | 260 TimeDelta delay, |
| 283 Receiver* receiver, | 261 Receiver* receiver, |
| 284 void (Receiver::*method)()) | 262 void (Receiver::*method)(), |
| 285 : DelayTimer(posted_from, delay, receiver, method, nullptr) {} | 263 TickClock* tick_clock = nullptr) |
| 264 : timer_(posted_from, |
| 265 delay, |
| 266 base::Bind(method, base::Unretained(receiver)), |
| 267 false, |
| 268 tick_clock) {} |
| 286 | 269 |
| 287 template <class Receiver> | 270 void Reset() { timer_.Reset(); } |
| 288 DelayTimer(const tracked_objects::Location& posted_from, | |
| 289 TimeDelta delay, | |
| 290 Receiver* receiver, | |
| 291 void (Receiver::*method)(), | |
| 292 TickClock* tick_clock) | |
| 293 : Timer(posted_from, | |
| 294 delay, | |
| 295 base::Bind(method, base::Unretained(receiver)), | |
| 296 false, | |
| 297 tick_clock) {} | |
| 298 | 271 |
| 299 void Reset() override; | 272 private: |
| 273 Timer timer_; |
| 300 }; | 274 }; |
| 301 | 275 |
| 302 // This class has a templated method so it can not be exported without failing | |
| 303 // to link in MSVC. But clang-plugin does not allow inline definitions of | |
| 304 // virtual methods, so the inline definition lives in the header file here | |
| 305 // to satisfy both. | |
| 306 inline void DelayTimer::Reset() { | |
| 307 Timer::Reset(); | |
| 308 } | |
| 309 | |
| 310 } // namespace base | 276 } // namespace base |
| 311 | 277 |
| 312 #endif // BASE_TIMER_TIMER_H_ | 278 #endif // BASE_TIMER_TIMER_H_ |
| OLD | NEW |