| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_ |
| 6 #define COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 | 12 |
| 13 namespace offline_pages { | 13 namespace offline_pages { |
| 14 | 14 |
| 15 // Task interface for consumers of the TaskQueue. Implements a mechanism for | 15 // Task interface for consumers of the TaskQueue. Implements a mechanism for |
| 16 // task completion. | 16 // task completion. |
| 17 // | 17 // |
| 18 // To create your own Task: | 18 // To create your own Task: |
| 19 // * Derive your new task from offline_pages::Task. | 19 // * Derive your new task from offline_pages::Task. |
| 20 // * Implement your task with as many async operations on the controlled | 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.) | 21 // resource as is required. (In general the smaller the task the better.) |
| 22 // * Whenever the task is terminated, call |Complete|. | 22 // * Whenever the task is terminated, call |Task::TaskComplete|. This step is |
| 23 // mandatory to ensure |TaskQueue| can pick another task. It should be called |
| 24 // once, but in every situation when task is exited. |
| 23 // * To schedule task for execution call |TaskQueue::AddTask|. | 25 // * To schedule task for execution call |TaskQueue::AddTask|. |
| 24 // | 26 // |
| 25 // If there is a chance that a task callback will come after the task is | 27 // 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 | 28 // destroyed, it is up to the task to actually implement mechanism to deal with |
| 27 // that, such as using a |base::WeakPtrFactory|. | 29 // that, such as using a |base::WeakPtrFactory|. |
| 28 class Task { | 30 class Task { |
| 29 public: | 31 public: |
| 30 // Signature of the method to be called by a task, when its work is done. | 32 // Signature of the method to be called by a task, when its work is done. |
| 31 typedef base::Callback<void(Task*)> TaskCompletionCallback; | 33 typedef base::Callback<void(Task*)> TaskCompletionCallback; |
| 32 | 34 |
| 33 Task(); | 35 Task(); |
| 34 virtual ~Task(); | 36 virtual ~Task(); |
| 35 | 37 |
| 36 // Entry point to the task. This is used by the queue to start the task, and | 38 // 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. | 39 // first step of the task should be implemented by overloading this method. |
| 38 // The task should define an additional method for every asynchronous step, | 40 // The task should define an additional method for every asynchronous step, |
| 39 // with each step setting up the next step as a callback. | 41 // 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; | 42 virtual void Run() = 0; |
| 43 | 43 |
| 44 // Sets the callback normally used by |TaskQueue| for testing. See | 44 // Sets the callback normally used by |TaskQueue| for testing. See |
| 45 // |SetTaskCompletionCallback| for details. | 45 // |SetTaskCompletionCallback| for details. |
| 46 void SetTaskCompletionCallbackForTesting( | 46 void SetTaskCompletionCallbackForTesting( |
| 47 scoped_refptr<base::SingleThreadTaskRunner> task_completion_runner, | 47 scoped_refptr<base::SingleThreadTaskRunner> task_completion_runner, |
| 48 const TaskCompletionCallback& task_completion_callback); | 48 const TaskCompletionCallback& task_completion_callback); |
| 49 | 49 |
| 50 protected: | 50 protected: |
| 51 // Call |TaskComplete| as the last call, before the task is terminated. This | 51 // Call |TaskComplete| as the last call, before the task is terminated. This |
| (...skipping 21 matching lines...) Expand all Loading... |
| 73 // Task runner for calling completion callback. Set by | 73 // Task runner for calling completion callback. Set by |
| 74 // |SetTaskCompletionCallback|. | 74 // |SetTaskCompletionCallback|. |
| 75 scoped_refptr<base::SingleThreadTaskRunner> task_completion_runner_; | 75 scoped_refptr<base::SingleThreadTaskRunner> task_completion_runner_; |
| 76 | 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(Task); | 77 DISALLOW_COPY_AND_ASSIGN(Task); |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 } // namespace offline_pages | 80 } // namespace offline_pages |
| 81 | 81 |
| 82 #endif // COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_ | 82 #endif // COMPONENTS_OFFLINE_PAGES_CORE_TASK_H_ |
| OLD | NEW |