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

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

Issue 2359933007: [Offline pages] Introduces TaskQueue to serialize tasks that asynchronously access SQLStore (Closed)
Patch Set: 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 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_
6 #define COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_
7
8 #include <stdint.h>
9
10 #include <memory>
11 #include <queue>
12
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17
18 namespace offline_pages {
19
20 // Class for coordinating sets of asynchronous operations, which switch threads
21 // and put multiple tasks in a message loop, with the goal to only run a single
22 // operation like that at once and postpone all the other work until the current
23 // task is finished.
24 //
25 // Consumers of this class should create an instance of TaskQueue and implement
26 // the tasks, that need to be run sequentially. New task will only be started,
Pete Williamson 2016/09/27 23:49:47 nit - clearer without the comma after "started"
fgorski 2016/09/28 22:19:28 Done.
27 // when the previous one calls complete.
28 //
29 // If there is a chance that a task callback will come after the task is
30 // destroyed, it is up to the task to actually implement mechanism to deal with
31 // that, such as using a base::WeakPtrFactory.
32 class TaskQueue {
33 public:
34 class Task;
35
36 typedef base::Callback<void(Task*)> CompletionCallback;
37
38 // Task interface for consumers of the TaskQueue. Implements a mechanism for
39 // task completion.
40 class Task {
Pete Williamson 2016/09/27 23:49:47 Would it make the code less complicated to move ta
fgorski 2016/09/28 22:19:29 This is mixing together task implementation guidan
41 public:
42 virtual ~Task();
43
44 // Entry point to the task. This is used by the queue to start the task, and
45 // first step of the task should be implemented by overloading this method.
46 // TODO(fgorski): Consider alternative: protected RunImpl(), so that we can
47 // add things like UMA in the Run method.
48 virtual void Run() = 0;
49
50 // Sets the completion callback and whatever is set on it before the task is
51 // put in the queue, will be overwritten. Left public for testing.
52 // If the task is run outside of the queue and completion callback is not
53 // set, it will also work.
54 void SetCompletionCallback(
55 scoped_refptr<base::SingleThreadTaskRunner> runner,
56 const CompletionCallback& completion_callback);
57
58 protected:
59 // Complete call, which should be made at every point, where the task is
Pete Williamson 2016/09/27 23:49:47 nit - clearer without comma.
fgorski 2016/09/28 22:19:28 Done.
60 // finished, so that task queue can pick up another task.
61 // |completion_callback_| will be scheduled on the provided
62 // |completion_runner_|, which means task code is no longer going to be on
63 // stack, when the next call is made.
64 void Complete();
65
66 private:
67 // Completion callback for this task set by |SetCompletionCallback|.
68 CompletionCallback completion_callback_;
69 // Task runner for calling completion callback.
70 scoped_refptr<base::SingleThreadTaskRunner> completion_runner_;
71 };
72
73 TaskQueue();
74 ~TaskQueue();
75
76 // Adds a task to the queue. Queue takes ownership of the task.
77 void AddTask(std::unique_ptr<Task> task);
78 // Whether the task queue has any tasks (running or waiting).
79 bool HasTasks() const;
80 // Whether there is a task currently running. Used for testing.
81 bool CurrentlyRunning() const;
82
83 private:
84 // Checks whether there are any tasks to run, as well as whether no task is
85 // currently running. When both are met, it will start the next task in the
86 // queue.
87 void MaybeStartTask();
88
89 // Callback for informing the queue that a task was completed.
90 void TaskCompleted(Task* task);
91
92 // Currently running tasks.
93 std::unique_ptr<Task> current_task_;
94
95 // A FIFO queue of tasks that will be run using this task queue.
96 std::queue<std::unique_ptr<Task>> tasks_;
Pete Williamson 2016/09/27 23:49:47 Is FIFO the right policy? For example, if we abor
fgorski 2016/09/28 22:19:29 RC has full control over in what order the tasks a
97
98 base::WeakPtrFactory<TaskQueue> weak_ptr_factory_;
99
100 DISALLOW_COPY_AND_ASSIGN(TaskQueue);
101 };
102
103 } // namespace offline_pages
104
105 #endif // COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698