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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 protected: | 107 protected: |
| 108 // Used to initiate a new delayed task. This has the side-effect of disabling | 108 // Used to initiate a new delayed task. This has the side-effect of disabling |
| 109 // scheduled_task_ if it is non-null. | 109 // scheduled_task_ if it is non-null. |
| 110 void SetTaskInfo(const tracked_objects::Location& posted_from, | 110 void SetTaskInfo(const tracked_objects::Location& posted_from, |
| 111 TimeDelta delay, | 111 TimeDelta delay, |
| 112 const base::Closure& user_task); | 112 const base::Closure& user_task); |
| 113 | 113 |
| 114 bool retain_user_task() const { return retain_user_task_; } | 114 bool retain_user_task() const { return retain_user_task_; } |
| 115 bool is_repeating() const { return is_repeating_; } | 115 bool is_repeating() const { return is_repeating_; } |
| 116 | 116 |
| 117 // Location in user code. | |
| 118 tracked_objects::Location posted_from_; | |
| 119 // Delay requested by user. | |
| 120 TimeDelta delay_; | |
| 121 // user_task_ is what the user wants to be run at desired_run_time_. | |
| 122 base::Closure user_task_; | |
| 123 | |
| 124 // The desired run time of user_task_. The user may update this at any time, | |
| 125 // even if their previous request has not run yet. If desired_run_time_ is | |
| 126 // greater than scheduled_run_time_, a continuation task will be posted to | |
| 127 // wait for the remaining time. This allows us to reuse the pending task so as | |
| 128 // not to flood the MessageLoop with orphaned tasks when the user code | |
| 129 // excessively Stops and Starts the timer. This time can be a "zero" TimeTicks | |
| 130 // if the task must be run immediately. | |
| 131 TimeTicks desired_run_time_; | |
| 132 | |
| 133 // Repeating timers automatically post the task again before calling the task | |
| 134 // callback. | |
| 135 const bool is_repeating_; | |
| 136 | |
| 137 // If true, hold on to the user_task_ closure object for reuse. | |
|
Daniel Erat
2014/10/15 15:04:22
nit: s/the user_task_/|user_task_|/
Chirantan Ekbote
2014/10/16 21:26:38
Done.
| |
| 138 const bool retain_user_task_; | |
| 139 | |
| 140 // If true, user_task_ is scheduled to run sometime in the future. | |
|
Daniel Erat
2014/10/15 15:04:22
nit: |user_task_|
Chirantan Ekbote
2014/10/16 21:26:38
Done.
| |
| 141 bool is_running_; | |
| 142 | |
| 117 private: | 143 private: |
| 118 friend class BaseTimerTaskInternal; | 144 friend class BaseTimerTaskInternal; |
| 119 | 145 |
| 120 // Allocates a new scheduled_task_ and posts it on the current MessageLoop | 146 // 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_ | 147 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ |
| 122 // and desired_run_time_ are reset to Now() + delay. | 148 // and desired_run_time_ are reset to Now() + delay. |
| 123 void PostNewScheduledTask(TimeDelta delay); | 149 void PostNewScheduledTask(TimeDelta delay); |
| 124 | 150 |
| 125 // Disable scheduled_task_ and abandon it so that it no longer refers back to | 151 // Disable scheduled_task_ and abandon it so that it no longer refers back to |
| 126 // this object. | 152 // this object. |
| 127 void AbandonScheduledTask(); | 153 void AbandonScheduledTask(); |
| 128 | 154 |
| 129 // Called by BaseTimerTaskInternal when the MessageLoop runs it. | 155 // Called by BaseTimerTaskInternal when the MessageLoop runs it. |
| 130 void RunScheduledTask(); | 156 void RunScheduledTask(); |
| 131 | 157 |
| 132 // Stop running task (if any) and abandon scheduled task (if any). | 158 // Stop running task (if any) and abandon scheduled task (if any). |
| 133 void StopAndAbandon() { | 159 void StopAndAbandon() { |
| 134 Stop(); | 160 Stop(); |
| 135 AbandonScheduledTask(); | 161 AbandonScheduledTask(); |
| 136 } | 162 } |
| 137 | 163 |
| 138 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call | 164 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call |
| 139 // RunScheduledTask() at scheduled_run_time_. | 165 // RunScheduledTask() at scheduled_run_time_. |
| 140 BaseTimerTaskInternal* scheduled_task_; | 166 BaseTimerTaskInternal* scheduled_task_; |
| 141 | 167 |
| 142 // Location in user code. | |
| 143 tracked_objects::Location posted_from_; | |
| 144 // Delay requested by user. | |
| 145 TimeDelta delay_; | |
| 146 // user_task_ is what the user wants to be run at desired_run_time_. | |
| 147 base::Closure user_task_; | |
| 148 | |
| 149 // The estimated time that the MessageLoop will run the scheduled_task_ that | 168 // 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 | 169 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the |
| 151 // task must be run immediately. | 170 // task must be run immediately. |
| 152 TimeTicks scheduled_run_time_; | 171 TimeTicks scheduled_run_time_; |
| 153 | 172 |
| 154 // The desired run time of user_task_. The user may update this at any time, | |
| 155 // even if their previous request has not run yet. If desired_run_time_ is | |
| 156 // greater than scheduled_run_time_, a continuation task will be posted to | |
| 157 // wait for the remaining time. This allows us to reuse the pending task so as | |
| 158 // not to flood the MessageLoop with orphaned tasks when the user code | |
| 159 // excessively Stops and Starts the timer. This time can be a "zero" TimeTicks | |
| 160 // if the task must be run immediately. | |
| 161 TimeTicks desired_run_time_; | |
| 162 | |
| 163 // Thread ID of current MessageLoop for verifying single-threaded usage. | 173 // Thread ID of current MessageLoop for verifying single-threaded usage. |
| 164 int thread_id_; | 174 int thread_id_; |
| 165 | 175 |
| 166 // Repeating timers automatically post the task again before calling the task | |
| 167 // callback. | |
| 168 const bool is_repeating_; | |
| 169 | |
| 170 // If true, hold on to the user_task_ closure object for reuse. | |
| 171 const bool retain_user_task_; | |
| 172 | |
| 173 // If true, user_task_ is scheduled to run sometime in the future. | |
| 174 bool is_running_; | |
| 175 | |
| 176 DISALLOW_COPY_AND_ASSIGN(Timer); | 176 DISALLOW_COPY_AND_ASSIGN(Timer); |
| 177 }; | 177 }; |
| 178 | 178 |
| 179 //----------------------------------------------------------------------------- | 179 //----------------------------------------------------------------------------- |
| 180 // This class is an implementation detail of OneShotTimer and RepeatingTimer. | 180 // This class is an implementation detail of OneShotTimer and RepeatingTimer. |
| 181 // Please do not use this class directly. | 181 // Please do not use this class directly. |
| 182 template <class Receiver, bool kIsRepeating> | 182 template <class Receiver, bool kIsRepeating> |
| 183 class BaseTimerMethodPointer : public Timer { | 183 class BaseTimerMethodPointer : public Timer { |
| 184 public: | 184 public: |
| 185 typedef void (Receiver::*ReceiverMethod)(); | 185 typedef void (Receiver::*ReceiverMethod)(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 : Timer(posted_from, delay, | 238 : Timer(posted_from, delay, |
| 239 base::Bind(method, base::Unretained(receiver)), | 239 base::Bind(method, base::Unretained(receiver)), |
| 240 false) {} | 240 false) {} |
| 241 | 241 |
| 242 void Reset() { Timer::Reset(); } | 242 void Reset() { Timer::Reset(); } |
| 243 }; | 243 }; |
| 244 | 244 |
| 245 } // namespace base | 245 } // namespace base |
| 246 | 246 |
| 247 #endif // BASE_TIMER_TIMER_H_ | 247 #endif // BASE_TIMER_TIMER_H_ |
| OLD | NEW |