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

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

Issue 2359933007: [Offline pages] Introduces TaskQueue to serialize tasks that asynchronously access SQLStore (Closed)
Patch Set: Addressing gn problems, adding missing test file to build Created 4 years, 2 months 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
OLDNEW
(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::TaskQueue() : weak_ptr_factory_(this) {}
13
14 TaskQueue::~TaskQueue() {}
15
16 void TaskQueue::AddTask(std::unique_ptr<Task> task) {
17 task->SetCompletionCallback(
dougarnett 2016/09/29 15:40:05 ah, so this is never meant to be used by the Task
fgorski 2016/09/29 17:37:37 Done.
18 base::ThreadTaskRunnerHandle::Get(),
19 base::Bind(&TaskQueue::TaskCompleted, weak_ptr_factory_.GetWeakPtr()));
20 tasks_.push(std::move(task));
21 MaybeStartTask();
22 }
23
24 bool TaskQueue::HasTasks() const {
25 return !tasks_.empty() || CurrentlyRunning();
26 }
27
28 bool TaskQueue::CurrentlyRunning() const {
29 return current_task_.get() != nullptr;
30 }
31
32 void TaskQueue::MaybeStartTask() {
33 if (CurrentlyRunning())
34 return;
35 if (!HasTasks())
36 return;
37
38 current_task_ = std::move(tasks_.front());
39 tasks_.pop();
40 current_task_->Run();
41 }
42
43 void TaskQueue::TaskCompleted(Task* task) {
44 DCHECK_EQ(task, current_task_.get());
45 if (task == current_task_.get()) {
46 current_task_.reset(nullptr);
47 MaybeStartTask();
48 }
49 }
50
51 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698