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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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. |
| 43 | 43 |
| 44 #ifndef BASE_TIMER_TIMER_H_ | 44 #ifndef BASE_TIMER_TIMER_H_ |
| 45 #define BASE_TIMER_TIMER_H_ | 45 #define BASE_TIMER_TIMER_H_ |
| 46 | 46 |
| 47 // IMPORTANT: If you change timer code, make sure that all tests (including | 47 // IMPORTANT: If you change timer code, make sure that all tests (including |
| 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 <memory> | |
| 53 | |
| 52 #include "base/base_export.h" | 54 #include "base/base_export.h" |
| 53 #include "base/bind.h" | 55 #include "base/bind.h" |
| 54 #include "base/bind_helpers.h" | 56 #include "base/bind_helpers.h" |
| 55 #include "base/callback.h" | 57 #include "base/callback.h" |
| 56 #include "base/location.h" | 58 #include "base/location.h" |
| 57 #include "base/macros.h" | 59 #include "base/macros.h" |
| 58 #include "base/time/time.h" | 60 #include "base/time/time.h" |
| 59 | 61 |
| 60 namespace base { | 62 namespace base { |
| 61 | 63 |
| 62 class BaseTimerTaskInternal; | 64 class BaseTimerTaskInternal; |
| 63 class SingleThreadTaskRunner; | 65 class SingleThreadTaskRunner; |
| 66 class TickClock; | |
| 64 | 67 |
| 65 //----------------------------------------------------------------------------- | 68 //----------------------------------------------------------------------------- |
| 66 // This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating | 69 // This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating |
| 67 // 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 |
| 68 // DCHECKs in place to verify this. | 71 // DCHECKs in place to verify this. |
| 69 // | 72 // |
| 70 class BASE_EXPORT Timer { | 73 class BASE_EXPORT Timer { |
| 71 public: | 74 public: |
| 72 // 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 |
| 73 // 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 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 86 virtual bool IsRunning() const; | 89 virtual bool IsRunning() const; |
| 87 | 90 |
| 88 // Returns the current delay for this timer. | 91 // Returns the current delay for this timer. |
| 89 virtual TimeDelta GetCurrentDelay() const; | 92 virtual TimeDelta GetCurrentDelay() const; |
| 90 | 93 |
| 91 // Set the task runner on which the task should be scheduled. This method can | 94 // Set the task runner on which the task should be scheduled. This method can |
| 92 // only be called before any tasks have been scheduled. The task runner must | 95 // only be called before any tasks have been scheduled. The task runner must |
| 93 // run tasks on the same thread the timer is used on. | 96 // run tasks on the same thread the timer is used on. |
| 94 virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); | 97 virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); |
| 95 | 98 |
| 99 // Sets the tick clock used to calculate the run time for scheduled tasks. | |
| 100 virtual void SetTickClock(std::unique_ptr<TickClock> tick_clock); | |
|
danakj
2016/11/09 19:57:38
This class being virtual means subclasses need to
jameswest
2016/11/10 00:26:05
I'm fine with that idea, but it's currently possib
| |
| 101 | |
| 96 // Start the timer to run at the given |delay| from now. If the timer is | 102 // Start the timer to run at the given |delay| from now. If the timer is |
| 97 // already running, it will be replaced to call the given |user_task|. | 103 // already running, it will be replaced to call the given |user_task|. |
| 98 virtual void Start(const tracked_objects::Location& posted_from, | 104 virtual void Start(const tracked_objects::Location& posted_from, |
| 99 TimeDelta delay, | 105 TimeDelta delay, |
| 100 const base::Closure& user_task); | 106 const base::Closure& user_task); |
| 101 | 107 |
| 102 // Call this method to stop and cancel the timer. It is a no-op if the timer | 108 // Call this method to stop and cancel the timer. It is a no-op if the timer |
| 103 // is not running. | 109 // is not running. |
| 104 virtual void Stop(); | 110 virtual void Stop(); |
| 105 | 111 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 } | 159 } |
| 154 | 160 |
| 155 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call | 161 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call |
| 156 // RunScheduledTask() at scheduled_run_time_. | 162 // RunScheduledTask() at scheduled_run_time_. |
| 157 BaseTimerTaskInternal* scheduled_task_; | 163 BaseTimerTaskInternal* scheduled_task_; |
| 158 | 164 |
| 159 // The task runner on which the task should be scheduled. If it is null, the | 165 // The task runner on which the task should be scheduled. If it is null, the |
| 160 // task runner for the current thread should be used. | 166 // task runner for the current thread should be used. |
| 161 scoped_refptr<SingleThreadTaskRunner> task_runner_; | 167 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
| 162 | 168 |
| 169 // The tick clock used to calculate the run time for scheduled tasks. | |
| 170 std::unique_ptr<TickClock> tick_clock_; | |
| 171 | |
| 163 // Location in user code. | 172 // Location in user code. |
| 164 tracked_objects::Location posted_from_; | 173 tracked_objects::Location posted_from_; |
| 165 // Delay requested by user. | 174 // Delay requested by user. |
| 166 TimeDelta delay_; | 175 TimeDelta delay_; |
| 167 // user_task_ is what the user wants to be run at desired_run_time_. | 176 // user_task_ is what the user wants to be run at desired_run_time_. |
| 168 base::Closure user_task_; | 177 base::Closure user_task_; |
| 169 | 178 |
| 170 // The estimated time that the MessageLoop will run the scheduled_task_ that | 179 // The estimated time that the MessageLoop will run the scheduled_task_ that |
| 171 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the | 180 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the |
| 172 // task must be run immediately. | 181 // task must be run immediately. |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 // to link in MSVC. But clang-plugin does not allow inline definitions of | 279 // to link in MSVC. But clang-plugin does not allow inline definitions of |
| 271 // virtual methods, so the inline definition lives in the header file here | 280 // virtual methods, so the inline definition lives in the header file here |
| 272 // to satisfy both. | 281 // to satisfy both. |
| 273 inline void DelayTimer::Reset() { | 282 inline void DelayTimer::Reset() { |
| 274 Timer::Reset(); | 283 Timer::Reset(); |
| 275 } | 284 } |
| 276 | 285 |
| 277 } // namespace base | 286 } // namespace base |
| 278 | 287 |
| 279 #endif // BASE_TIMER_TIMER_H_ | 288 #endif // BASE_TIMER_TIMER_H_ |
| OLD | NEW |