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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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/time/time.h" | 58 #include "base/time/time.h" |
59 | 59 |
60 namespace base { | 60 namespace base { |
61 | 61 |
62 class BaseTimerTaskInternal; | 62 class BaseTimerTaskInternal; |
| 63 class SingleThreadTaskRunner; |
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. |
68 // | 69 // |
69 class BASE_EXPORT Timer { | 70 class BASE_EXPORT Timer { |
70 public: | 71 public: |
71 // Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must | 72 // Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must |
72 // be called later to set task info. |retain_user_task| determines whether the | 73 // be called later to set task info. |retain_user_task| determines whether the |
73 // user_task is retained or reset when it runs or stops. | 74 // user_task is retained or reset when it runs or stops. |
74 Timer(bool retain_user_task, bool is_repeating); | 75 Timer(bool retain_user_task, bool is_repeating); |
75 | 76 |
76 // Construct a timer with retained task info. | 77 // Construct a timer with retained task info. |
77 Timer(const tracked_objects::Location& posted_from, | 78 Timer(const tracked_objects::Location& posted_from, |
78 TimeDelta delay, | 79 TimeDelta delay, |
79 const base::Closure& user_task, | 80 const base::Closure& user_task, |
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. This method can |
| 92 // only be called before any tasks have been scheduled. |
| 93 virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); |
| 94 |
90 // Start the timer to run at the given |delay| from now. If the timer is | 95 // 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|. | 96 // already running, it will be replaced to call the given |user_task|. |
92 virtual void Start(const tracked_objects::Location& posted_from, | 97 virtual void Start(const tracked_objects::Location& posted_from, |
93 TimeDelta delay, | 98 TimeDelta delay, |
94 const base::Closure& user_task); | 99 const base::Closure& user_task); |
95 | 100 |
96 // Call this method to stop and cancel the timer. It is a no-op if the timer | 101 // Call this method to stop and cancel the timer. It is a no-op if the timer |
97 // is not running. | 102 // is not running. |
98 virtual void Stop(); | 103 virtual void Stop(); |
99 | 104 |
(...skipping 21 matching lines...) Expand all Loading... |
121 bool is_running() const { return is_running_; } | 126 bool is_running() const { return is_running_; } |
122 | 127 |
123 private: | 128 private: |
124 friend class BaseTimerTaskInternal; | 129 friend class BaseTimerTaskInternal; |
125 | 130 |
126 // Allocates a new scheduled_task_ and posts it on the current MessageLoop | 131 // Allocates a new scheduled_task_ and posts it on the current MessageLoop |
127 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ | 132 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ |
128 // and desired_run_time_ are reset to Now() + delay. | 133 // and desired_run_time_ are reset to Now() + delay. |
129 void PostNewScheduledTask(TimeDelta delay); | 134 void PostNewScheduledTask(TimeDelta delay); |
130 | 135 |
| 136 // Returns the task runner on which the task should be scheduled. If the |
| 137 // corresponding task_runner_ field is null, the task runner for the current |
| 138 // thread is returned. |
| 139 scoped_refptr<SingleThreadTaskRunner> GetTaskRunner(); |
| 140 |
131 // Disable scheduled_task_ and abandon it so that it no longer refers back to | 141 // Disable scheduled_task_ and abandon it so that it no longer refers back to |
132 // this object. | 142 // this object. |
133 void AbandonScheduledTask(); | 143 void AbandonScheduledTask(); |
134 | 144 |
135 // Called by BaseTimerTaskInternal when the MessageLoop runs it. | 145 // Called by BaseTimerTaskInternal when the MessageLoop runs it. |
136 void RunScheduledTask(); | 146 void RunScheduledTask(); |
137 | 147 |
138 // Stop running task (if any) and abandon scheduled task (if any). | 148 // Stop running task (if any) and abandon scheduled task (if any). |
139 void StopAndAbandon() { | 149 void StopAndAbandon() { |
140 Stop(); | 150 Stop(); |
141 AbandonScheduledTask(); | 151 AbandonScheduledTask(); |
142 } | 152 } |
143 | 153 |
144 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call | 154 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call |
145 // RunScheduledTask() at scheduled_run_time_. | 155 // RunScheduledTask() at scheduled_run_time_. |
146 BaseTimerTaskInternal* scheduled_task_; | 156 BaseTimerTaskInternal* scheduled_task_; |
147 | 157 |
| 158 // The task runner on which the task should be scheduled. If it is null, the |
| 159 // task runner for the current thread should be used. |
| 160 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
| 161 |
148 // Location in user code. | 162 // Location in user code. |
149 tracked_objects::Location posted_from_; | 163 tracked_objects::Location posted_from_; |
150 // Delay requested by user. | 164 // Delay requested by user. |
151 TimeDelta delay_; | 165 TimeDelta delay_; |
152 // user_task_ is what the user wants to be run at desired_run_time_. | 166 // user_task_ is what the user wants to be run at desired_run_time_. |
153 base::Closure user_task_; | 167 base::Closure user_task_; |
154 | 168 |
155 // The estimated time that the MessageLoop will run the scheduled_task_ that | 169 // The estimated time that the MessageLoop will run the scheduled_task_ that |
156 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the | 170 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the |
157 // task must be run immediately. | 171 // task must be run immediately. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 : Timer(posted_from, delay, | 258 : Timer(posted_from, delay, |
245 base::Bind(method, base::Unretained(receiver)), | 259 base::Bind(method, base::Unretained(receiver)), |
246 false) {} | 260 false) {} |
247 | 261 |
248 void Reset() { Timer::Reset(); } | 262 void Reset() { Timer::Reset(); } |
249 }; | 263 }; |
250 | 264 |
251 } // namespace base | 265 } // namespace base |
252 | 266 |
253 #endif // BASE_TIMER_TIMER_H_ | 267 #endif // BASE_TIMER_TIMER_H_ |
OLD | NEW |