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

Unified Diff: components/offline_pages/core/task.h

Issue 2359933007: [Offline pages] Introduces TaskQueue to serialize tasks that asynchronously access SQLStore (Closed)
Patch Set: Addressing gn problems, adding missing test file to build 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
Index: components/offline_pages/core/task.h
diff --git a/components/offline_pages/core/task.h b/components/offline_pages/core/task.h
new file mode 100644
index 0000000000000000000000000000000000000000..2817510e4073abe6c80c882cf709fab3f2cb82d2
--- /dev/null
+++ b/components/offline_pages/core/task.h
@@ -0,0 +1,68 @@
+// 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_H_
+#define COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_
+
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/single_thread_task_runner.h"
+
+namespace offline_pages {
+
+// Task interface for consumers of the TaskQueue. Implements a mechanism for
+// task completion.
+//
+// To use TaskQueue:
Pete Williamson 2016/09/29 00:54:43 Maybe this would be better as "To use a Task" *
fgorski 2016/09/29 17:37:37 Done.
+// * Implement your task with as many async operations on the controlled
+// resource as is required. (In general the smaller the task the better.)
+// * Whenever the task is terminated, call |Complete|.
+//
+// If there is a chance that a task callback will come after the task is
+// destroyed, it is up to the task to actually implement mechanism to deal with
+// that, such as using a |base::WeakPtrFactory|.
+class Task {
+ public:
+ typedef base::Callback<void(Task*)> CompletionCallback;
dougarnett 2016/09/29 15:40:04 TaskCompletionCallback?
fgorski 2016/09/29 17:37:37 Done.
+
+ Task();
+ virtual ~Task();
+
+ // Entry point to the task. This is used by the queue to start the task, and
+ // first step of the task should be implemented by overloading this method.
+ // TODO(fgorski): Consider alternative: protected RunImpl(), so that we can
+ // add things like UMA in the Run method.
Pete Williamson 2016/09/29 00:54:44 maybe add: // The task will have an additional f
fgorski 2016/09/29 17:37:36 Done.
+ virtual void Run() = 0;
+
+ // Sets the completion callback and whatever is set on it before the task is
Pete Williamson 2016/09/29 00:54:44 We have lots of different kinds of completions - l
fgorski 2016/09/29 17:37:36 I went with Doug's suggestion above and added to d
+ // put in the queue, will be overwritten. Setting the runner and callback here
dougarnett 2016/09/29 15:40:05 wording seems a bit confusing wrt overwritten and
fgorski 2016/09/29 17:37:37 Actually this raises a valid point. I'll add DCHEC
+ // allows task owner to inject completion callback and specify appropriate
Pete Williamson 2016/09/29 00:54:44 Naive user question: How does the Task client get
fgorski 2016/09/29 17:37:37 That is actually left for the caller to work out.
+ // thread/runner on which to call it.
+ // If the task is run outside of the queue and completion callback is not
+ // set, it will also work.
+ // Left public for testing.
+ void SetCompletionCallback(scoped_refptr<base::SingleThreadTaskRunner> runner,
+ const CompletionCallback& completion_callback);
+
+ protected:
+ // Call |Complete| at every point when the task is finished so that task queue
dougarnett 2016/09/29 15:40:05 what does "at every point" mean? Is it finished mo
fgorski 2016/09/29 17:37:36 Attempted clarification. Let me know.
+ // can pick up another task.
+ // |completion_callback_| will be scheduled on the provided
+ // |completion_runner_|, which means task code is no longer going to be on
+ // stack, when the next call is made.
+ void Complete();
Pete Williamson 2016/09/29 00:54:44 Maybe TaskComplete() to differentiate it a bit mor
fgorski 2016/09/29 17:37:37 Done.
+
+ private:
+ // Completion callback for this task set by |SetCompletionCallback|.
+ CompletionCallback completion_callback_;
+ // Task runner for calling completion callback.
+ scoped_refptr<base::SingleThreadTaskRunner> completion_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(Task);
Pete Williamson 2016/09/29 00:54:43 You've disallowed copy and assign, should you also
fgorski 2016/09/29 17:37:36 Cool. It got me thinking. I don't think that is c
+};
+
+} // namespace offline_pages
+
+#endif // COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_

Powered by Google App Engine
This is Rietveld 408576698