| 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_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 callback normally used by |TaskQueue| for testing. See |
| 45 // |SetTaskCompletionCallback| for details. |
| 46 void SetTaskCompletionCallbackForTesting( |
| 47 scoped_refptr<base::SingleThreadTaskRunner> task_completion_runner, |
| 48 const TaskCompletionCallback& task_completion_callback); |
| 49 |
| 50 protected: |
| 51 // Call |TaskComplete| as the last call, before the task is terminated. This |
| 52 // ensures that |TaskQueue| can pick up another task. |
| 53 // |task_completion_callback_| will be scheduled on the provided |
| 54 // |task_completion_runner_|, which means task code is no longer going to be |
| 55 // on stack, when the next call is made. |
| 56 void TaskComplete(); |
| 57 |
| 58 private: |
| 59 friend class TaskQueue; |
| 60 |
| 61 // Allows task queue to Set the |task_completion_callback| and single thread |
| 62 // task |task_completion_runner| that will be used to inform the |TaskQueue| |
| 63 // when the task is done. |
| 64 // |
| 65 // If the task is run outside of the |TaskQueue| and completion callback is |
| 66 // not set, it will also work. |
| 67 void SetTaskCompletionCallback( |
| 68 scoped_refptr<base::SingleThreadTaskRunner> task_completion_runner, |
| 69 const TaskCompletionCallback& task_completion_callback); |
| 70 |
| 71 // Completion callback for this task set by |SetTaskCompletionCallback|. |
| 72 TaskCompletionCallback task_completion_callback_; |
| 73 // Task runner for calling completion callback. Set by |
| 74 // |SetTaskCompletionCallback|. |
| 75 scoped_refptr<base::SingleThreadTaskRunner> task_completion_runner_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(Task); |
| 78 }; |
| 79 |
| 80 } // namespace offline_pages |
| 81 |
| 82 #endif // COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_ |
| OLD | NEW |