Chromium Code Reviews| Index: components/offline_pages/core/task_queue.h |
| diff --git a/components/offline_pages/core/task_queue.h b/components/offline_pages/core/task_queue.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..36727fa10c110d6582d50222345ef1ba5ca72a62 |
| --- /dev/null |
| +++ b/components/offline_pages/core/task_queue.h |
| @@ -0,0 +1,61 @@ |
| +// 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. |
| + |
| +#ifndef COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_ |
| +#define COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_ |
| + |
| +#include <memory> |
| +#include <queue> |
| + |
| +#include "base/callback.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "components/offline_pages/core/task.h" |
| + |
| +namespace offline_pages { |
| + |
| +// Class for coordinating sets of asynchronous operations, which switch threads |
|
dougarnett
2016/09/29 15:40:05
wording seems confusing to me - not sure if the "s
fgorski
2016/09/29 17:37:37
Done.
|
| +// and put multiple tasks in a message loop, with the goal to only run a single |
| +// operation like that at once and postpone all the other work until the current |
| +// task is finished. |
| +// |
| +// Consumers of this class should create an instance of TaskQueue and implement |
| +// the tasks, that need to be run sequentially. New task will only be started |
|
Pete Williamson
2016/09/29 00:54:44
This will read smoother without the comma.
In thi
fgorski
2016/09/29 17:37:37
Background component is the consumer, therefore th
|
| +// when the previous one calls complete. |
| +class TaskQueue { |
| + public: |
| + TaskQueue(); |
| + ~TaskQueue(); |
| + |
| + // Adds a task to the queue. Queue takes ownership of the task. |
| + void AddTask(std::unique_ptr<Task> task); |
| + // Whether the task queue has any tasks (running or waiting). |
| + bool HasTasks() const; |
| + // Whether there is a task currently running. Used for testing. |
| + bool CurrentlyRunning() const; |
| + |
| + private: |
| + // Checks whether there are any tasks to run, as well as whether no task is |
| + // currently running. When both are met, it will start the next task in the |
| + // queue. |
| + void MaybeStartTask(); |
| + |
| + // Callback for informing the queue that a task was completed. |
| + void TaskCompleted(Task* task); |
| + |
| + // Currently running tasks. |
| + std::unique_ptr<Task> current_task_; |
| + |
| + // A FIFO queue of tasks that will be run using this task queue. |
| + std::queue<std::unique_ptr<Task>> tasks_; |
| + |
| + base::WeakPtrFactory<TaskQueue> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
| +}; |
| + |
| +} // namespace offline_pages |
| + |
| +#endif // COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_ |