Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: components/offline_pages/core/task_queue.cc

Issue 2535273003: [Offline pages] TaskQueue polishing (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/offline_pages/core/task_queue.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 "components/offline_pages/core/task_queue.h" 5 #include "components/offline_pages/core/task_queue.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/threading/thread_task_runner_handle.h" 8 #include "base/threading/thread_task_runner_handle.h"
9 9
10 namespace offline_pages { 10 namespace offline_pages {
11 11
12 TaskQueue::TaskQueue() : weak_ptr_factory_(this) {} 12 TaskQueue::TaskQueue() : weak_ptr_factory_(this) {}
13 13
14 TaskQueue::~TaskQueue() {} 14 TaskQueue::~TaskQueue() {}
15 15
16 void TaskQueue::AddTask(std::unique_ptr<Task> task) { 16 void TaskQueue::AddTask(std::unique_ptr<Task> task) {
17 task->SetTaskCompletionCallback( 17 task->SetTaskCompletionCallback(
18 base::ThreadTaskRunnerHandle::Get(), 18 base::ThreadTaskRunnerHandle::Get(),
19 base::Bind(&TaskQueue::TaskCompleted, weak_ptr_factory_.GetWeakPtr())); 19 base::Bind(&TaskQueue::TaskCompleted, weak_ptr_factory_.GetWeakPtr()));
20 tasks_.push(std::move(task)); 20 tasks_.push(std::move(task));
21 MaybeStartTask(); 21 StartTaskIfAvailable();
22 } 22 }
23 23
24 bool TaskQueue::HasPendingTasks() const { 24 bool TaskQueue::HasPendingTasks() const {
25 return !tasks_.empty() || HasRunningTask(); 25 return !tasks_.empty() || HasRunningTask();
26 } 26 }
27 27
28 bool TaskQueue::HasRunningTask() const { 28 bool TaskQueue::HasRunningTask() const {
29 return current_task_.get() != nullptr; 29 return current_task_.get() != nullptr;
30 } 30 }
31 31
32 void TaskQueue::MaybeStartTask() { 32 void TaskQueue::StartTaskIfAvailable() {
33 DVLOG(2) << "running? " << HasRunningTask() << ", pending? " 33 DVLOG(2) << "running? " << HasRunningTask() << ", pending? "
34 << HasPendingTasks() << " " << __func__; 34 << HasPendingTasks() << " " << __func__;
35 if (HasRunningTask() || !HasPendingTasks()) 35 if (HasRunningTask() || !HasPendingTasks())
36 return; 36 return;
37 37
38 current_task_ = std::move(tasks_.front()); 38 current_task_ = std::move(tasks_.front());
39 tasks_.pop(); 39 tasks_.pop();
40 current_task_->Run(); 40 current_task_->Run();
41 } 41 }
42 42
43 void TaskQueue::TaskCompleted(Task* task) { 43 void TaskQueue::TaskCompleted(Task* task) {
44 DCHECK_EQ(task, current_task_.get()); 44 DCHECK_EQ(task, current_task_.get());
45 if (task == current_task_.get()) { 45 if (task == current_task_.get()) {
46 current_task_.reset(nullptr); 46 current_task_.reset(nullptr);
47 MaybeStartTask(); 47 StartTaskIfAvailable();
48 } 48 }
49 } 49 }
50 50
51 } // namespace offline_pages 51 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/core/task_queue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698