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 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_ | |
| 6 #define COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <queue> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "components/offline_pages/core/task.h" | |
| 16 | |
| 17 namespace offline_pages { | |
| 18 | |
| 19 // 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.
| |
| 20 // and put multiple tasks in a message loop, with the goal to only run a single | |
| 21 // operation like that at once and postpone all the other work until the current | |
| 22 // task is finished. | |
| 23 // | |
| 24 // Consumers of this class should create an instance of TaskQueue and implement | |
| 25 // 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
| |
| 26 // when the previous one calls complete. | |
| 27 class TaskQueue { | |
| 28 public: | |
| 29 TaskQueue(); | |
| 30 ~TaskQueue(); | |
| 31 | |
| 32 // Adds a task to the queue. Queue takes ownership of the task. | |
| 33 void AddTask(std::unique_ptr<Task> task); | |
| 34 // Whether the task queue has any tasks (running or waiting). | |
| 35 bool HasTasks() const; | |
| 36 // Whether there is a task currently running. Used for testing. | |
| 37 bool CurrentlyRunning() const; | |
| 38 | |
| 39 private: | |
| 40 // Checks whether there are any tasks to run, as well as whether no task is | |
| 41 // currently running. When both are met, it will start the next task in the | |
| 42 // queue. | |
| 43 void MaybeStartTask(); | |
| 44 | |
| 45 // Callback for informing the queue that a task was completed. | |
| 46 void TaskCompleted(Task* task); | |
| 47 | |
| 48 // Currently running tasks. | |
| 49 std::unique_ptr<Task> current_task_; | |
| 50 | |
| 51 // A FIFO queue of tasks that will be run using this task queue. | |
| 52 std::queue<std::unique_ptr<Task>> tasks_; | |
| 53 | |
| 54 base::WeakPtrFactory<TaskQueue> weak_ptr_factory_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(TaskQueue); | |
| 57 }; | |
| 58 | |
| 59 } // namespace offline_pages | |
| 60 | |
| 61 #endif // COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_ | |
| OLD | NEW |