OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/delayed_callback_runner.h" |
| 6 |
| 7 #include "base/location.h" |
| 8 |
| 9 namespace safe_browsing { |
| 10 |
| 11 DelayedCallbackRunner::DelayedCallbackRunner( |
| 12 base::TimeDelta delay, |
| 13 base::TaskRunner* task_runner) |
| 14 : task_runner_(task_runner), |
| 15 next_callback_(callbacks_.end()), |
| 16 timer_(FROM_HERE, delay, this, &DelayedCallbackRunner::OnTimer) { |
| 17 } |
| 18 |
| 19 DelayedCallbackRunner::~DelayedCallbackRunner() { |
| 20 } |
| 21 |
| 22 void DelayedCallbackRunner::RegisterCallback(const base::Closure& callback) { |
| 23 callbacks_.push_back(callback); |
| 24 } |
| 25 |
| 26 void DelayedCallbackRunner::Start() { |
| 27 // Nothing to do if the runner is already running or nothing has been added. |
| 28 if (next_callback_ != callbacks_.end() || callbacks_.empty()) |
| 29 return; |
| 30 |
| 31 // Prime the system with the first callback. |
| 32 next_callback_ = callbacks_.begin(); |
| 33 |
| 34 // Point the starter pistol in the air and pull the trigger. |
| 35 timer_.Reset(); |
| 36 } |
| 37 |
| 38 void DelayedCallbackRunner::OnTimer() { |
| 39 // Run the next callback on the task runner. |
| 40 task_runner_->PostTask(FROM_HERE, *next_callback_); |
| 41 |
| 42 // Get ready for the next callback if there is one. |
| 43 if (++next_callback_ != callbacks_.end()) |
| 44 timer_.Reset(); |
| 45 } |
| 46 |
| 47 } // namespace safe_browsing |
OLD | NEW |