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

Unified 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 feedback from Dmitry Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/offline_pages/core/task_queue.h ('k') | components/offline_pages/core/task_queue_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/offline_pages/core/task_queue.cc
diff --git a/components/offline_pages/core/task_queue.cc b/components/offline_pages/core/task_queue.cc
new file mode 100644
index 0000000000000000000000000000000000000000..115c509af1a266f463628f6cf8712c658c34bda4
--- /dev/null
+++ b/components/offline_pages/core/task_queue.cc
@@ -0,0 +1,49 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/offline_pages/core/task_queue.h"
+
+#include "base/bind.h"
+#include "base/threading/thread_task_runner_handle.h"
+
+namespace offline_pages {
+
+TaskQueue::TaskQueue() : weak_ptr_factory_(this) {}
+
+TaskQueue::~TaskQueue() {}
+
+void TaskQueue::AddTask(std::unique_ptr<Task> task) {
+ task->SetTaskCompletionCallback(
+ base::ThreadTaskRunnerHandle::Get(),
+ base::Bind(&TaskQueue::TaskCompleted, weak_ptr_factory_.GetWeakPtr()));
+ tasks_.push(std::move(task));
+ MaybeStartTask();
+}
+
+bool TaskQueue::HasPendingTasks() const {
+ return !tasks_.empty() || HasRunningTask();
+}
+
+bool TaskQueue::HasRunningTask() const {
+ return current_task_.get() != nullptr;
+}
+
+void TaskQueue::MaybeStartTask() {
+ if (HasRunningTask() || !HasPendingTasks())
+ return;
+
+ current_task_ = std::move(tasks_.front());
+ tasks_.pop();
+ current_task_->Run();
+}
+
+void TaskQueue::TaskCompleted(Task* task) {
+ DCHECK_EQ(task, current_task_.get());
+ if (task == current_task_.get()) {
+ current_task_.reset(nullptr);
+ MaybeStartTask();
+ }
+}
+
+} // namespace offline_pages
« no previous file with comments | « components/offline_pages/core/task_queue.h ('k') | components/offline_pages/core/task_queue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698