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

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

Issue 2359933007: [Offline pages] Introduces TaskQueue to serialize tasks that asynchronously access SQLStore (Closed)
Patch Set: Addressing CR feedback 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_H_
6 #define COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_
7
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/single_thread_task_runner.h"
12
13 namespace offline_pages {
14
15 // Task interface for consumers of the TaskQueue. Implements a mechanism for
16 // task completion.
17 //
18 // To create your own Task:
19 // * Derive your new task from offline_pages::Task.
20 // * Implement your task with as many async operations on the controlled
21 // resource as is required. (In general the smaller the task the better.)
22 // * Whenever the task is terminated, call |Complete|.
23 // * To schedule task for execution call |TaskQueue::AddTask|.
24 //
25 // If there is a chance that a task callback will come after the task is
26 // destroyed, it is up to the task to actually implement mechanism to deal with
27 // that, such as using a |base::WeakPtrFactory|.
28 class Task {
29 public:
30 // Signature of the method to be called by a task, when its work is done.
31 typedef base::Callback<void(Task*)> TaskCompletionCallback;
32
33 Task();
34 virtual ~Task();
35
36 // Entry point to the task. This is used by the queue to start the task, and
37 // first step of the task should be implemented by overloading this method.
38 // The task should define an additional method for every asynchronous step,
39 // with each step setting up the next step as a callback.
40 // TODO(fgorski): Consider alternative: protected RunImpl(), so that we can
41 // add things like UMA in the Run method.
42 virtual void Run() = 0;
43
44 // Sets the |task_completion_callback| and single thread task
45 // |task_completion_runner| that will be used to inform the task owner that
46 // the task is done.
47 //
48 // In production the task owner should be a |TaskQueue|.
49 //
50 // Should only be called once, by |TaskQueue|. Should not be called from
51 // within the |Task|.
52 //
53 // If the task is run outside of the |TaskQueue| and completion callback is
54 // not set, it will also work.
55 //
56 // When defining a new task, this is not the callback to be used to inform the
57 // caller of task completion, it is only used between the |TaskQueue| and the
58 // |Task| to make sure it can be properly cleaned up.
59 //
60 // Left public for testing.
61 void SetTaskCompletionCallback(
62 scoped_refptr<base::SingleThreadTaskRunner> task_completion_runner,
63 const TaskCompletionCallback& task_completion_callback);
64
65 protected:
66 // Call |TaskComplete| as the last call, before the task is terminated. This
67 // ensures that |TaskQueue| can pick up another task.
68 // |task_completion_callback_| will be scheduled on the provided
69 // |task_completion_runner_|, which means task code is no longer going to be
70 // on stack, when the next call is made.
71 void TaskComplete();
dewittj 2016/09/29 18:40:55 Feels like TaskQueue should be a TaskDelegate with
fgorski 2016/09/30 17:33:12 As we discussed, it may be harder to work with if
72
73 private:
74 // Completion callback for this task set by |SetTaskCompletionCallback|.
75 TaskCompletionCallback task_completion_callback_;
76 // Task runner for calling completion callback. Set by
77 // |SetTaskCompletionCallback|.
78 scoped_refptr<base::SingleThreadTaskRunner> task_completion_runner_;
79
80 DISALLOW_COPY_AND_ASSIGN(Task);
81 };
82
83 } // namespace offline_pages
84
85 #endif // COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698