Chromium Code Reviews| 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 #include "base/single_thread_task_runner.h" | |
| 9 #include "base/thread_task_runner_handle.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // Runs |callback| on the thread to which |thread_runner| belongs. The callback | |
| 14 // is run immediately if this function is called on |thread_runner|'s thread. | |
| 15 void RunCallbackOnOriginThread( | |
| 16 const base::Closure& callback, | |
| 17 scoped_refptr<base::SingleThreadTaskRunner> thread_runner) { | |
| 18 if (thread_runner->BelongsToCurrentThread()) | |
| 19 callback.Run(); | |
| 20 else | |
| 21 thread_runner->PostTask(FROM_HERE, callback); | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 namespace safe_browsing { | |
| 27 | |
| 28 DelayedCallbackRunner::DelayedCallbackRunner( | |
| 29 base::TimeDelta delay, | |
| 30 scoped_refptr<base::TaskRunner> task_runner) | |
| 31 : task_runner_(task_runner), | |
| 32 next_callback_(callbacks_.end()), | |
| 33 timer_(FROM_HERE, delay, this, &DelayedCallbackRunner::OnTimer) { | |
| 34 } | |
| 35 | |
| 36 DelayedCallbackRunner::~DelayedCallbackRunner() { | |
| 37 } | |
| 38 | |
| 39 void DelayedCallbackRunner::RegisterCallback(const base::Closure& callback) { | |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 41 callbacks_.push_back(base::Bind(&RunCallbackOnOriginThread, | |
|
grt (UTC plus 2)
2014/08/04 14:16:29
oops, this is totally wrong.
| |
| 42 callback, | |
| 43 base::ThreadTaskRunnerHandle::Get())); | |
| 44 } | |
| 45 | |
| 46 void DelayedCallbackRunner::Start() { | |
| 47 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 48 | |
| 49 // Nothing to do if the runner is already running or nothing has been added. | |
| 50 if (next_callback_ != callbacks_.end() || callbacks_.empty()) | |
| 51 return; | |
| 52 | |
| 53 // Prime the system with the first callback. | |
| 54 next_callback_ = callbacks_.begin(); | |
| 55 | |
| 56 // Point the starter pistol in the air and pull the trigger. | |
| 57 timer_.Reset(); | |
| 58 } | |
| 59 | |
| 60 void DelayedCallbackRunner::OnTimer() { | |
| 61 // Run the next callback on the task runner. | |
| 62 task_runner_->PostTask(FROM_HERE, *next_callback_); | |
| 63 | |
| 64 // Remove this callback and get ready for the next if there is one. | |
| 65 next_callback_ = callbacks_.erase(next_callback_); | |
| 66 if (next_callback_ != callbacks_.end()) | |
| 67 timer_.Reset(); | |
| 68 } | |
| 69 | |
| 70 } // namespace safe_browsing | |
| OLD | NEW |