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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
48 // disabled ones) from timer_unittests.cc pass locally. Some are disabled | 48 // disabled ones) from timer_unittests.cc pass locally. Some are disabled |
49 // because they're flaky on the buildbot, but when you run them locally you | 49 // because they're flaky on the buildbot, but when you run them locally you |
50 // should be able to tell the difference. | 50 // should be able to tell the difference. |
51 | 51 |
52 #include "base/base_export.h" | 52 #include "base/base_export.h" |
53 #include "base/basictypes.h" | 53 #include "base/basictypes.h" |
54 #include "base/bind.h" | 54 #include "base/bind.h" |
55 #include "base/bind_helpers.h" | 55 #include "base/bind_helpers.h" |
56 #include "base/callback.h" | 56 #include "base/callback.h" |
57 #include "base/location.h" | 57 #include "base/location.h" |
58 #include "base/single_thread_task_runner.h" | |
58 #include "base/time/time.h" | 59 #include "base/time/time.h" |
59 | 60 |
60 namespace base { | 61 namespace base { |
61 | 62 |
62 class BaseTimerTaskInternal; | 63 class BaseTimerTaskInternal; |
63 | 64 |
64 //----------------------------------------------------------------------------- | 65 //----------------------------------------------------------------------------- |
65 // This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating | 66 // This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating |
66 // tasks. It must be destructed on the same thread that starts tasks. There are | 67 // tasks. It must be destructed on the same thread that starts tasks. There are |
67 // DCHECKs in place to verify this. | 68 // DCHECKs in place to verify this. |
(...skipping 12 matching lines...) Expand all Loading... | |
80 bool is_repeating); | 81 bool is_repeating); |
81 | 82 |
82 virtual ~Timer(); | 83 virtual ~Timer(); |
83 | 84 |
84 // Returns true if the timer is running (i.e., not stopped). | 85 // Returns true if the timer is running (i.e., not stopped). |
85 virtual bool IsRunning() const; | 86 virtual bool IsRunning() const; |
86 | 87 |
87 // Returns the current delay for this timer. | 88 // Returns the current delay for this timer. |
88 virtual TimeDelta GetCurrentDelay() const; | 89 virtual TimeDelta GetCurrentDelay() const; |
89 | 90 |
91 // Set the task runner on which the task should be scheduled. | |
Sami
2014/10/13 17:00:11
Please mention that this can only be changed befor
petrcermak
2014/10/13 17:21:40
Done.
| |
92 virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); | |
93 | |
90 // Start the timer to run at the given |delay| from now. If the timer is | 94 // Start the timer to run at the given |delay| from now. If the timer is |
91 // already running, it will be replaced to call the given |user_task|. | 95 // already running, it will be replaced to call the given |user_task|. |
92 virtual void Start(const tracked_objects::Location& posted_from, | 96 virtual void Start(const tracked_objects::Location& posted_from, |
93 TimeDelta delay, | 97 TimeDelta delay, |
94 const base::Closure& user_task); | 98 const base::Closure& user_task); |
95 | 99 |
96 // Call this method to stop and cancel the timer. It is a no-op if the timer | 100 // Call this method to stop and cancel the timer. It is a no-op if the timer |
97 // is not running. | 101 // is not running. |
98 virtual void Stop(); | 102 virtual void Stop(); |
99 | 103 |
(...skipping 15 matching lines...) Expand all Loading... | |
115 bool is_repeating() const { return is_repeating_; } | 119 bool is_repeating() const { return is_repeating_; } |
116 | 120 |
117 private: | 121 private: |
118 friend class BaseTimerTaskInternal; | 122 friend class BaseTimerTaskInternal; |
119 | 123 |
120 // Allocates a new scheduled_task_ and posts it on the current MessageLoop | 124 // 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_ | 125 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ |
122 // and desired_run_time_ are reset to Now() + delay. | 126 // and desired_run_time_ are reset to Now() + delay. |
123 void PostNewScheduledTask(TimeDelta delay); | 127 void PostNewScheduledTask(TimeDelta delay); |
124 | 128 |
129 // Returns the task runner on which the task should be scheduled. If the | |
130 // corresponding task_runner_ field is NULL, the task runner for the current | |
131 // thread is returned. | |
132 scoped_refptr<SingleThreadTaskRunner> ResolveTaskRunner() const; | |
133 | |
125 // Disable scheduled_task_ and abandon it so that it no longer refers back to | 134 // Disable scheduled_task_ and abandon it so that it no longer refers back to |
126 // this object. | 135 // this object. |
127 void AbandonScheduledTask(); | 136 void AbandonScheduledTask(); |
128 | 137 |
129 // Called by BaseTimerTaskInternal when the MessageLoop runs it. | 138 // Called by BaseTimerTaskInternal when the MessageLoop runs it. |
130 void RunScheduledTask(); | 139 void RunScheduledTask(); |
131 | 140 |
132 // Stop running task (if any) and abandon scheduled task (if any). | 141 // Stop running task (if any) and abandon scheduled task (if any). |
133 void StopAndAbandon() { | 142 void StopAndAbandon() { |
134 Stop(); | 143 Stop(); |
135 AbandonScheduledTask(); | 144 AbandonScheduledTask(); |
136 } | 145 } |
137 | 146 |
138 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call | 147 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call |
139 // RunScheduledTask() at scheduled_run_time_. | 148 // RunScheduledTask() at scheduled_run_time_. |
140 BaseTimerTaskInternal* scheduled_task_; | 149 BaseTimerTaskInternal* scheduled_task_; |
141 | 150 |
151 // The task runner on which the task should be scheduled. If null, the | |
152 // task runner for the current thread should be used. | |
153 scoped_refptr<SingleThreadTaskRunner> task_runner_; | |
Sami
2014/10/13 17:00:10
Could we initialize this to be ThreadTaskRunnerHan
petrcermak
2014/10/13 17:21:40
As discussed, this cannot be done in the construct
| |
154 | |
142 // Location in user code. | 155 // Location in user code. |
143 tracked_objects::Location posted_from_; | 156 tracked_objects::Location posted_from_; |
144 // Delay requested by user. | 157 // Delay requested by user. |
145 TimeDelta delay_; | 158 TimeDelta delay_; |
146 // user_task_ is what the user wants to be run at desired_run_time_. | 159 // user_task_ is what the user wants to be run at desired_run_time_. |
147 base::Closure user_task_; | 160 base::Closure user_task_; |
148 | 161 |
149 // The estimated time that the MessageLoop will run the scheduled_task_ that | 162 // 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 | 163 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the |
151 // task must be run immediately. | 164 // task must be run immediately. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 : Timer(posted_from, delay, | 251 : Timer(posted_from, delay, |
239 base::Bind(method, base::Unretained(receiver)), | 252 base::Bind(method, base::Unretained(receiver)), |
240 false) {} | 253 false) {} |
241 | 254 |
242 void Reset() { Timer::Reset(); } | 255 void Reset() { Timer::Reset(); } |
243 }; | 256 }; |
244 | 257 |
245 } // namespace base | 258 } // namespace base |
246 | 259 |
247 #endif // BASE_TIMER_TIMER_H_ | 260 #endif // BASE_TIMER_TIMER_H_ |
OLD | NEW |