Chromium Code Reviews| 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() { | |
|
darin (slow to review)
2008/12/02 01:37:22
It is nice that delayed Tasks have the guarantee o
| |
| 140 ClearBaseTimer(); | |
| 141 } | |
| 142 | |
| 138 virtual void Run() { | 143 virtual void Run() { |
| 139 if (!timer_) // timer_ is null if we were orphaned. | 144 if (!timer_) // timer_ is null if we were orphaned. |
| 140 return; | 145 return; |
| 141 SelfType* self = static_cast<SelfType*>(timer_); | 146 if (kIsRepeating) |
| 142 if (kIsRepeating) { | 147 ResetBaseTimer(); |
| 143 self->Reset(); | 148 else |
| 144 } else { | 149 ClearBaseTimer(); |
| 145 self->delayed_task_ = NULL; | |
| 146 } | |
| 147 DispatchToMethod(receiver_, method_, Tuple0()); | 150 DispatchToMethod(receiver_, method_, Tuple0()); |
| 148 } | 151 } |
| 152 | |
| 149 TimerTask* Clone() const { | 153 TimerTask* Clone() const { |
| 150 return new TimerTask(delay_, receiver_, method_); | 154 return new TimerTask(delay_, receiver_, method_); |
| 151 } | 155 } |
| 156 | |
| 152 private: | 157 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 | |
| 153 Receiver* receiver_; | 174 Receiver* receiver_; |
| 154 ReceiverMethod method_; | 175 ReceiverMethod method_; |
| 155 }; | 176 }; |
| 156 }; | 177 }; |
| 157 | 178 |
| 158 //----------------------------------------------------------------------------- | 179 //----------------------------------------------------------------------------- |
| 159 // A simple, one-shot timer. See usage notes at the top of the file. | 180 // A simple, one-shot timer. See usage notes at the top of the file. |
| 160 template <class Receiver> | 181 template <class Receiver> |
| 161 class OneShotTimer : public BaseTimer<Receiver, false> {}; | 182 class OneShotTimer : public BaseTimer<Receiver, false> {}; |
| 162 | 183 |
| 163 //----------------------------------------------------------------------------- | 184 //----------------------------------------------------------------------------- |
| 164 // A simple, repeating timer. See usage notes at the top of the file. | 185 // A simple, repeating timer. See usage notes at the top of the file. |
| 165 template <class Receiver> | 186 template <class Receiver> |
| 166 class RepeatingTimer : public BaseTimer<Receiver, true> {}; | 187 class RepeatingTimer : public BaseTimer<Receiver, true> {}; |
| 167 | 188 |
| 168 } // namespace base | 189 } // namespace base |
| 169 | 190 |
| 170 #endif // BASE_TIMER_H_ | 191 #endif // BASE_TIMER_H_ |
| OLD | NEW |