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 #include "base/timer/timer.h" | 5 #include "base/timer/timer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 86 } |
87 | 87 |
88 bool Timer::IsRunning() const { | 88 bool Timer::IsRunning() const { |
89 return is_running_; | 89 return is_running_; |
90 } | 90 } |
91 | 91 |
92 TimeDelta Timer::GetCurrentDelay() const { | 92 TimeDelta Timer::GetCurrentDelay() const { |
93 return delay_; | 93 return delay_; |
94 } | 94 } |
95 | 95 |
| 96 void Timer::SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { |
| 97 // Do not allow changing the task runner once something has been scheduled. |
| 98 DCHECK_EQ(thread_id_, 0); |
| 99 task_runner_.swap(task_runner); |
| 100 } |
| 101 |
96 void Timer::Start(const tracked_objects::Location& posted_from, | 102 void Timer::Start(const tracked_objects::Location& posted_from, |
97 TimeDelta delay, | 103 TimeDelta delay, |
98 const base::Closure& user_task) { | 104 const base::Closure& user_task) { |
99 SetTaskInfo(posted_from, delay, user_task); | 105 SetTaskInfo(posted_from, delay, user_task); |
100 Reset(); | 106 Reset(); |
101 } | 107 } |
102 | 108 |
103 void Timer::Stop() { | 109 void Timer::Stop() { |
104 is_running_ = false; | 110 is_running_ = false; |
105 if (!retain_user_task_) | 111 if (!retain_user_task_) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 posted_from_ = posted_from; | 145 posted_from_ = posted_from; |
140 delay_ = delay; | 146 delay_ = delay; |
141 user_task_ = user_task; | 147 user_task_ = user_task; |
142 } | 148 } |
143 | 149 |
144 void Timer::PostNewScheduledTask(TimeDelta delay) { | 150 void Timer::PostNewScheduledTask(TimeDelta delay) { |
145 DCHECK(scheduled_task_ == NULL); | 151 DCHECK(scheduled_task_ == NULL); |
146 is_running_ = true; | 152 is_running_ = true; |
147 scheduled_task_ = new BaseTimerTaskInternal(this); | 153 scheduled_task_ = new BaseTimerTaskInternal(this); |
148 if (delay > TimeDelta::FromMicroseconds(0)) { | 154 if (delay > TimeDelta::FromMicroseconds(0)) { |
149 ThreadTaskRunnerHandle::Get()->PostDelayedTask(posted_from_, | 155 GetTaskRunner()->PostDelayedTask(posted_from_, |
150 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)), | 156 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)), |
151 delay); | 157 delay); |
152 scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay; | 158 scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay; |
153 } else { | 159 } else { |
154 ThreadTaskRunnerHandle::Get()->PostTask(posted_from_, | 160 GetTaskRunner()->PostTask(posted_from_, |
155 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_))); | 161 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_))); |
156 scheduled_run_time_ = desired_run_time_ = TimeTicks(); | 162 scheduled_run_time_ = desired_run_time_ = TimeTicks(); |
157 } | 163 } |
158 // Remember the thread ID that posts the first task -- this will be verified | 164 // Remember the thread ID that posts the first task -- this will be verified |
159 // later when the task is abandoned to detect misuse from multiple threads. | 165 // later when the task is abandoned to detect misuse from multiple threads. |
160 if (!thread_id_) | 166 if (!thread_id_) |
161 thread_id_ = static_cast<int>(PlatformThread::CurrentId()); | 167 thread_id_ = static_cast<int>(PlatformThread::CurrentId()); |
162 } | 168 } |
163 | 169 |
| 170 scoped_refptr<SingleThreadTaskRunner> Timer::GetTaskRunner() { |
| 171 return task_runner_.get() ? task_runner_ : ThreadTaskRunnerHandle::Get(); |
| 172 } |
| 173 |
164 void Timer::AbandonScheduledTask() { | 174 void Timer::AbandonScheduledTask() { |
165 DCHECK(thread_id_ == 0 || | 175 DCHECK(thread_id_ == 0 || |
166 thread_id_ == static_cast<int>(PlatformThread::CurrentId())); | 176 thread_id_ == static_cast<int>(PlatformThread::CurrentId())); |
167 if (scheduled_task_) { | 177 if (scheduled_task_) { |
168 scheduled_task_->Abandon(); | 178 scheduled_task_->Abandon(); |
169 scheduled_task_ = NULL; | 179 scheduled_task_ = NULL; |
170 } | 180 } |
171 } | 181 } |
172 | 182 |
173 void Timer::RunScheduledTask() { | 183 void Timer::RunScheduledTask() { |
(...skipping 23 matching lines...) Expand all Loading... |
197 PostNewScheduledTask(delay_); | 207 PostNewScheduledTask(delay_); |
198 else | 208 else |
199 Stop(); | 209 Stop(); |
200 | 210 |
201 task.Run(); | 211 task.Run(); |
202 | 212 |
203 // No more member accesses here: *this could be deleted at this point. | 213 // No more member accesses here: *this could be deleted at this point. |
204 } | 214 } |
205 | 215 |
206 } // namespace base | 216 } // namespace base |
OLD | NEW |