OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 #pragma once | 43 #pragma once |
44 | 44 |
45 // IMPORTANT: If you change timer code, make sure that all tests (including | 45 // IMPORTANT: If you change timer code, make sure that all tests (including |
46 // disabled ones) from timer_unittests.cc pass locally. Some are disabled | 46 // disabled ones) from timer_unittests.cc pass locally. Some are disabled |
47 // because they're flaky on the buildbot, but when you run them locally you | 47 // because they're flaky on the buildbot, but when you run them locally you |
48 // should be able to tell the difference. | 48 // should be able to tell the difference. |
49 | 49 |
50 #include "base/base_export.h" | 50 #include "base/base_export.h" |
51 #include "base/location.h" | 51 #include "base/location.h" |
52 #include "base/logging.h" | 52 #include "base/logging.h" |
53 #include "base/task.h" | |
54 #include "base/time.h" | 53 #include "base/time.h" |
55 | 54 |
56 class MessageLoop; | 55 class MessageLoop; |
57 | 56 |
58 namespace base { | 57 namespace base { |
59 | 58 |
60 //----------------------------------------------------------------------------- | 59 //----------------------------------------------------------------------------- |
61 // This class is an implementation detail of OneShotTimer and RepeatingTimer. | 60 // This class is an implementation detail of OneShotTimer and RepeatingTimer. |
62 // Please do not use this class directly. | 61 // Please do not use this class directly. |
63 // | 62 // |
(...skipping 15 matching lines...) Expand all Loading... |
79 // the timer is running! | 78 // the timer is running! |
80 TimeDelta GetCurrentDelay() const { | 79 TimeDelta GetCurrentDelay() const { |
81 DCHECK(IsRunning()); | 80 DCHECK(IsRunning()); |
82 return delayed_task_->delay_; | 81 return delayed_task_->delay_; |
83 } | 82 } |
84 | 83 |
85 protected: | 84 protected: |
86 BaseTimer_Helper() : delayed_task_(NULL) {} | 85 BaseTimer_Helper() : delayed_task_(NULL) {} |
87 | 86 |
88 // We have access to the timer_ member so we can orphan this task. | 87 // We have access to the timer_ member so we can orphan this task. |
89 class TimerTask : public Task { | 88 class TimerTask { |
90 public: | 89 public: |
91 TimerTask(const tracked_objects::Location& posted_from, | 90 TimerTask(const tracked_objects::Location& posted_from, |
92 TimeDelta delay) | 91 TimeDelta delay) |
93 : posted_from_(posted_from), | 92 : posted_from_(posted_from), |
94 timer_(NULL), | 93 timer_(NULL), |
95 delay_(delay) { | 94 delay_(delay) { |
96 } | 95 } |
97 virtual ~TimerTask() {} | 96 virtual ~TimerTask() {} |
| 97 virtual void Run() = 0; |
98 tracked_objects::Location posted_from_; | 98 tracked_objects::Location posted_from_; |
99 BaseTimer_Helper* timer_; | 99 BaseTimer_Helper* timer_; |
100 TimeDelta delay_; | 100 TimeDelta delay_; |
101 }; | 101 }; |
102 | 102 |
103 // Used to orphan delayed_task_ so that when it runs it does nothing. | 103 // Used to orphan delayed_task_ so that when it runs it does nothing. |
104 void OrphanDelayedTask(); | 104 void OrphanDelayedTask(); |
105 | 105 |
106 // Used to initiated a new delayed task. This has the side-effect of | 106 // Used to initiated a new delayed task. This has the side-effect of |
107 // orphaning delayed_task_ if it is non-null. | 107 // orphaning delayed_task_ if it is non-null. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 ClearBaseTimer(); | 163 ClearBaseTimer(); |
164 } | 164 } |
165 | 165 |
166 virtual void Run() { | 166 virtual void Run() { |
167 if (!timer_) // timer_ is null if we were orphaned. | 167 if (!timer_) // timer_ is null if we were orphaned. |
168 return; | 168 return; |
169 if (kIsRepeating) | 169 if (kIsRepeating) |
170 ResetBaseTimer(); | 170 ResetBaseTimer(); |
171 else | 171 else |
172 ClearBaseTimer(); | 172 ClearBaseTimer(); |
173 DispatchToMethod(receiver_, method_, Tuple0()); | 173 (receiver_->*method_)(); |
174 } | 174 } |
175 | 175 |
176 TimerTask* Clone() const { | 176 TimerTask* Clone() const { |
177 return new TimerTask(posted_from_, delay_, receiver_, method_); | 177 return new TimerTask(posted_from_, delay_, receiver_, method_); |
178 } | 178 } |
179 | 179 |
180 private: | 180 private: |
181 // Inform the Base that the timer is no longer active. | 181 // Inform the Base that the timer is no longer active. |
182 void ClearBaseTimer() { | 182 void ClearBaseTimer() { |
183 if (timer_) { | 183 if (timer_) { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 const ReceiverMethod method_; | 280 const ReceiverMethod method_; |
281 const TimeDelta delay_; | 281 const TimeDelta delay_; |
282 | 282 |
283 OneShotTimer<DelayTimer<Receiver> > timer_; | 283 OneShotTimer<DelayTimer<Receiver> > timer_; |
284 TimeTicks trigger_time_; | 284 TimeTicks trigger_time_; |
285 }; | 285 }; |
286 | 286 |
287 } // namespace base | 287 } // namespace base |
288 | 288 |
289 #endif // BASE_TIMER_H_ | 289 #endif // BASE_TIMER_H_ |
OLD | NEW |