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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 75 |
76 // Construct a timer with retained task info. | 76 // Construct a timer with retained task info. |
77 Timer(const tracked_objects::Location& posted_from, | 77 Timer(const tracked_objects::Location& posted_from, |
78 TimeDelta delay, | 78 TimeDelta delay, |
79 const base::Closure& user_task, | 79 const base::Closure& user_task, |
80 bool is_repeating); | 80 bool is_repeating); |
81 | 81 |
82 virtual ~Timer(); | 82 virtual ~Timer(); |
83 | 83 |
84 // Returns true if the timer is running (i.e., not stopped). | 84 // Returns true if the timer is running (i.e., not stopped). |
85 bool IsRunning() const { | 85 virtual bool IsRunning() const; |
86 return is_running_; | |
87 } | |
88 | 86 |
89 // Returns the current delay for this timer. | 87 // Returns the current delay for this timer. |
90 TimeDelta GetCurrentDelay() const { | 88 virtual TimeDelta GetCurrentDelay() const; |
91 return delay_; | |
92 } | |
93 | 89 |
94 // Start the timer to run at the given |delay| from now. If the timer is | 90 // Start the timer to run at the given |delay| from now. If the timer is |
95 // already running, it will be replaced to call the given |user_task|. | 91 // already running, it will be replaced to call the given |user_task|. |
96 void Start(const tracked_objects::Location& posted_from, | 92 virtual void Start(const tracked_objects::Location& posted_from, |
97 TimeDelta delay, | 93 TimeDelta delay, |
98 const base::Closure& user_task); | 94 const base::Closure& user_task); |
99 | 95 |
100 // Call this method to stop and cancel the timer. It is a no-op if the timer | 96 // Call this method to stop and cancel the timer. It is a no-op if the timer |
101 // is not running. | 97 // is not running. |
102 void Stop(); | 98 virtual void Stop(); |
103 | 99 |
104 // Call this method to reset the timer delay. The user_task_ must be set. If | 100 // Call this method to reset the timer delay. The user_task_ must be set. If |
105 // the timer is not running, this will start it by posting a task. | 101 // the timer is not running, this will start it by posting a task. |
106 void Reset(); | 102 virtual void Reset(); |
107 | 103 |
108 const base::Closure& user_task() const { return user_task_; } | 104 const base::Closure& user_task() const { return user_task_; } |
109 const TimeTicks& desired_run_time() const { return desired_run_time_; } | 105 const TimeTicks& desired_run_time() const { return desired_run_time_; } |
110 | 106 |
111 protected: | 107 protected: |
112 // 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 |
113 // scheduled_task_ if it is non-null. | 109 // scheduled_task_ if it is non-null. |
114 void SetTaskInfo(const tracked_objects::Location& posted_from, | 110 void SetTaskInfo(const tracked_objects::Location& posted_from, |
115 TimeDelta delay, | 111 TimeDelta delay, |
116 const base::Closure& user_task); | 112 const base::Closure& user_task); |
117 | 113 |
| 114 bool retain_user_task() const { return retain_user_task_; } |
| 115 bool is_repeating() const { return is_repeating_; } |
| 116 |
118 private: | 117 private: |
119 friend class BaseTimerTaskInternal; | 118 friend class BaseTimerTaskInternal; |
120 | 119 |
121 // Allocates a new scheduled_task_ and posts it on the current MessageLoop | 120 // Allocates a new scheduled_task_ and posts it on the current MessageLoop |
122 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ | 121 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ |
123 // and desired_run_time_ are reset to Now() + delay. | 122 // and desired_run_time_ are reset to Now() + delay. |
124 void PostNewScheduledTask(TimeDelta delay); | 123 void PostNewScheduledTask(TimeDelta delay); |
125 | 124 |
126 // Disable scheduled_task_ and abandon it so that it no longer refers back to | 125 // Disable scheduled_task_ and abandon it so that it no longer refers back to |
127 // this object. | 126 // this object. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below | 189 // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below |
191 // and convert callers to use the base::Closure version in Timer::Start, | 190 // and convert callers to use the base::Closure version in Timer::Start, |
192 // see bug 148832. | 191 // see bug 148832. |
193 using Timer::Start; | 192 using Timer::Start; |
194 | 193 |
195 BaseTimerMethodPointer() : Timer(kIsRepeating, kIsRepeating) {} | 194 BaseTimerMethodPointer() : Timer(kIsRepeating, kIsRepeating) {} |
196 | 195 |
197 // Start the timer to run at the given |delay| from now. If the timer is | 196 // Start the timer to run at the given |delay| from now. If the timer is |
198 // already running, it will be replaced to call a task formed from | 197 // already running, it will be replaced to call a task formed from |
199 // |reviewer->*method|. | 198 // |reviewer->*method|. |
200 void Start(const tracked_objects::Location& posted_from, | 199 virtual void Start(const tracked_objects::Location& posted_from, |
201 TimeDelta delay, | 200 TimeDelta delay, |
202 Receiver* receiver, | 201 Receiver* receiver, |
203 ReceiverMethod method) { | 202 ReceiverMethod method) { |
204 Timer::Start(posted_from, delay, | 203 Timer::Start(posted_from, delay, |
205 base::Bind(method, base::Unretained(receiver))); | 204 base::Bind(method, base::Unretained(receiver))); |
206 } | 205 } |
207 }; | 206 }; |
208 | 207 |
209 //----------------------------------------------------------------------------- | 208 //----------------------------------------------------------------------------- |
210 // A simple, one-shot timer. See usage notes at the top of the file. | 209 // A simple, one-shot timer. See usage notes at the top of the file. |
(...skipping 28 matching lines...) Expand all Loading... |
239 : Timer(posted_from, delay, | 238 : Timer(posted_from, delay, |
240 base::Bind(method, base::Unretained(receiver)), | 239 base::Bind(method, base::Unretained(receiver)), |
241 false) {} | 240 false) {} |
242 | 241 |
243 void Reset() { Timer::Reset(); } | 242 void Reset() { Timer::Reset(); } |
244 }; | 243 }; |
245 | 244 |
246 } // namespace base | 245 } // namespace base |
247 | 246 |
248 #endif // BASE_TIMER_TIMER_H_ | 247 #endif // BASE_TIMER_TIMER_H_ |
OLD | NEW |