Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "components/offline_pages/core/task_queue.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/threading/thread_task_runner_handle.h" | |
| 9 | |
| 10 namespace offline_pages { | |
| 11 | |
| 12 TaskQueue::Task::~Task() {} | |
| 13 | |
| 14 void TaskQueue::Task::SetCompletionCallback( | |
|
Pete Williamson
2016/09/27 23:49:47
Why does this set both the runner and the callback
fgorski
2016/09/28 22:19:28
Because we want the completion to be executed on t
| |
| 15 scoped_refptr<base::SingleThreadTaskRunner> runner, | |
| 16 const TaskQueue::CompletionCallback& callback) { | |
| 17 DCHECK(runner); | |
| 18 DCHECK(!callback.is_null()); | |
| 19 completion_runner_ = runner; | |
| 20 completion_callback_ = callback; | |
| 21 } | |
| 22 | |
| 23 void TaskQueue::Task::Complete() { | |
| 24 if (completion_callback_.is_null() || !completion_runner_) | |
| 25 return; | |
| 26 | |
| 27 completion_runner_->PostTask(FROM_HERE, | |
| 28 base::Bind(completion_callback_, this)); | |
| 29 } | |
| 30 | |
| 31 TaskQueue::TaskQueue() : weak_ptr_factory_(this) {} | |
| 32 | |
| 33 TaskQueue::~TaskQueue() {} | |
| 34 | |
| 35 void TaskQueue::AddTask(std::unique_ptr<Task> task) { | |
| 36 task->SetCompletionCallback( | |
| 37 base::ThreadTaskRunnerHandle::Get(), | |
| 38 base::Bind(&TaskQueue::TaskCompleted, weak_ptr_factory_.GetWeakPtr())); | |
| 39 tasks_.push(std::move(task)); | |
|
Pete Williamson
2016/09/27 23:49:47
Should this be persistent? I'm guessing not, but
fgorski
2016/09/28 22:19:28
You are correct. It shouldn't be persisted. This i
| |
| 40 MaybeStartTask(); | |
| 41 } | |
| 42 | |
| 43 bool TaskQueue::HasTasks() const { | |
| 44 return !tasks_.empty() || CurrentlyRunning(); | |
| 45 } | |
| 46 | |
| 47 bool TaskQueue::CurrentlyRunning() const { | |
| 48 return current_task_.get() != nullptr; | |
| 49 } | |
| 50 | |
| 51 void TaskQueue::MaybeStartTask() { | |
| 52 if (CurrentlyRunning()) | |
| 53 return; | |
| 54 if (!HasTasks()) | |
| 55 return; | |
| 56 | |
| 57 current_task_ = std::move(tasks_.front()); | |
| 58 tasks_.pop(); | |
| 59 current_task_->Run(); | |
| 60 } | |
| 61 | |
| 62 void TaskQueue::TaskCompleted(Task* task) { | |
| 63 DCHECK_EQ(task, current_task_.get()); | |
|
Pete Williamson
2016/09/27 23:49:47
This doesn't seem to call the completion for the t
fgorski
2016/09/28 22:19:28
line 38
| |
| 64 if (task == current_task_.get()) { | |
| 65 current_task_.reset(nullptr); | |
| 66 MaybeStartTask(); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 } // namespace offline_pages | |
| OLD | NEW |