OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 private: | 128 private: |
129 typedef BaseTimer<Receiver, kIsRepeating> SelfType; | 129 typedef BaseTimer<Receiver, kIsRepeating> SelfType; |
130 | 130 |
131 class TimerTask : public BaseTimer_Helper::TimerTask { | 131 class TimerTask : public BaseTimer_Helper::TimerTask { |
132 public: | 132 public: |
133 TimerTask(TimeDelta delay, Receiver* receiver, ReceiverMethod method) | 133 TimerTask(TimeDelta delay, Receiver* receiver, ReceiverMethod method) |
134 : BaseTimer_Helper::TimerTask(delay), | 134 : BaseTimer_Helper::TimerTask(delay), |
135 receiver_(receiver), | 135 receiver_(receiver), |
136 method_(method) { | 136 method_(method) { |
137 } | 137 } |
138 | |
139 virtual ~TimerTask() { | |
140 ClearBaseTimer(); | |
141 } | |
142 | |
143 virtual void Run() { | 138 virtual void Run() { |
144 if (!timer_) // timer_ is null if we were orphaned. | 139 if (!timer_) // timer_ is null if we were orphaned. |
145 return; | 140 return; |
146 if (kIsRepeating) | 141 SelfType* self = static_cast<SelfType*>(timer_); |
147 ResetBaseTimer(); | 142 if (kIsRepeating) { |
148 else | 143 self->Reset(); |
149 ClearBaseTimer(); | 144 } else { |
| 145 self->delayed_task_ = NULL; |
| 146 } |
150 DispatchToMethod(receiver_, method_, Tuple0()); | 147 DispatchToMethod(receiver_, method_, Tuple0()); |
151 } | 148 } |
152 | |
153 TimerTask* Clone() const { | 149 TimerTask* Clone() const { |
154 return new TimerTask(delay_, receiver_, method_); | 150 return new TimerTask(delay_, receiver_, method_); |
155 } | 151 } |
156 | |
157 private: | 152 private: |
158 // Inform the Base that the timer is no longer active. | |
159 void ClearBaseTimer() { | |
160 if (timer_) { | |
161 SelfType* self = static_cast<SelfType*>(timer_); | |
162 self->delayed_task_ = NULL; | |
163 } | |
164 } | |
165 | |
166 // Inform the Base that we're resetting the timer. | |
167 void ResetBaseTimer() { | |
168 DCHECK(timer_); | |
169 DCHECK(kIsRepeating); | |
170 SelfType* self = static_cast<SelfType*>(timer_); | |
171 self->Reset(); | |
172 } | |
173 | |
174 Receiver* receiver_; | 153 Receiver* receiver_; |
175 ReceiverMethod method_; | 154 ReceiverMethod method_; |
176 }; | 155 }; |
177 }; | 156 }; |
178 | 157 |
179 //----------------------------------------------------------------------------- | 158 //----------------------------------------------------------------------------- |
180 // A simple, one-shot timer. See usage notes at the top of the file. | 159 // A simple, one-shot timer. See usage notes at the top of the file. |
181 template <class Receiver> | 160 template <class Receiver> |
182 class OneShotTimer : public BaseTimer<Receiver, false> {}; | 161 class OneShotTimer : public BaseTimer<Receiver, false> {}; |
183 | 162 |
184 //----------------------------------------------------------------------------- | 163 //----------------------------------------------------------------------------- |
185 // A simple, repeating timer. See usage notes at the top of the file. | 164 // A simple, repeating timer. See usage notes at the top of the file. |
186 template <class Receiver> | 165 template <class Receiver> |
187 class RepeatingTimer : public BaseTimer<Receiver, true> {}; | 166 class RepeatingTimer : public BaseTimer<Receiver, true> {}; |
188 | 167 |
189 } // namespace base | 168 } // namespace base |
190 | 169 |
191 #endif // BASE_TIMER_H_ | 170 #endif // BASE_TIMER_H_ |
OLD | NEW |