| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_TASK_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_TASK_MANAGER_H_ |
| 6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_TASK_MANAGER_H_ | 6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_TASK_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <memory> |
| 11 #include <queue> | 12 #include <queue> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/callback.h" | 15 #include "base/callback.h" |
| 15 #include "base/containers/scoped_ptr_hash_map.h" | 16 #include "base/containers/scoped_ptr_hash_map.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/sequence_checker.h" | 19 #include "base/sequence_checker.h" |
| 20 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
| 21 #include "chrome/browser/sync_file_system/drive_backend/task_dependency_manager.
h" | 21 #include "chrome/browser/sync_file_system/drive_backend/task_dependency_manager.
h" |
| 22 #include "chrome/browser/sync_file_system/sync_callbacks.h" | 22 #include "chrome/browser/sync_file_system/sync_callbacks.h" |
| 23 #include "chrome/browser/sync_file_system/sync_status_code.h" | 23 #include "chrome/browser/sync_file_system/sync_status_code.h" |
| 24 #include "chrome/browser/sync_file_system/task_logger.h" | 24 #include "chrome/browser/sync_file_system/task_logger.h" |
| 25 | 25 |
| 26 namespace base { | 26 namespace base { |
| 27 class SequencedTaskRunner; | 27 class SequencedTaskRunner; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 41 // This class manages asynchronous tasks for Sync FileSystem. Each task must be | 41 // This class manages asynchronous tasks for Sync FileSystem. Each task must be |
| 42 // either a Task or a SyncTask. | 42 // either a Task or a SyncTask. |
| 43 // The instance runs single task as the foreground task, and multiple tasks as | 43 // The instance runs single task as the foreground task, and multiple tasks as |
| 44 // background tasks. Running background task has a TaskBlocker that | 44 // background tasks. Running background task has a TaskBlocker that |
| 45 // describes which task can run in parallel. When a task start running as a | 45 // describes which task can run in parallel. When a task start running as a |
| 46 // background task, SyncTaskManager checks if any running background task | 46 // background task, SyncTaskManager checks if any running background task |
| 47 // doesn't block the new background task, and queues it up if it can't run. | 47 // doesn't block the new background task, and queues it up if it can't run. |
| 48 class SyncTaskManager { | 48 class SyncTaskManager { |
| 49 public: | 49 public: |
| 50 typedef base::Callback<void(const SyncStatusCallback& callback)> Task; | 50 typedef base::Callback<void(const SyncStatusCallback& callback)> Task; |
| 51 typedef base::Callback<void(scoped_ptr<SyncTaskToken> token)> Continuation; | 51 typedef base::Callback<void(std::unique_ptr<SyncTaskToken> token)> |
| 52 Continuation; |
| 52 | 53 |
| 53 enum Priority { | 54 enum Priority { |
| 54 PRIORITY_LOW, | 55 PRIORITY_LOW, |
| 55 PRIORITY_MED, | 56 PRIORITY_MED, |
| 56 PRIORITY_HIGH, | 57 PRIORITY_HIGH, |
| 57 }; | 58 }; |
| 58 | 59 |
| 59 class Client { | 60 class Client { |
| 60 public: | 61 public: |
| 61 virtual ~Client() {} | 62 virtual ~Client() {} |
| 62 | 63 |
| 63 // Called when the manager is idle. | 64 // Called when the manager is idle. |
| 64 virtual void MaybeScheduleNextTask() = 0; | 65 virtual void MaybeScheduleNextTask() = 0; |
| 65 | 66 |
| 66 // Called when the manager is notified a task is done. | 67 // Called when the manager is notified a task is done. |
| 67 virtual void NotifyLastOperationStatus( | 68 virtual void NotifyLastOperationStatus( |
| 68 SyncStatusCode last_operation_status, | 69 SyncStatusCode last_operation_status, |
| 69 bool last_operation_used_network) = 0; | 70 bool last_operation_used_network) = 0; |
| 70 | 71 |
| 71 virtual void RecordTaskLog(scoped_ptr<TaskLogger::TaskLog> task_log) = 0; | 72 virtual void RecordTaskLog( |
| 73 std::unique_ptr<TaskLogger::TaskLog> task_log) = 0; |
| 72 }; | 74 }; |
| 73 | 75 |
| 74 // Runs at most |maximum_background_tasks| parallel as background tasks. | 76 // Runs at most |maximum_background_tasks| parallel as background tasks. |
| 75 // If |maximum_background_tasks| is zero, all task runs as foreground task. | 77 // If |maximum_background_tasks| is zero, all task runs as foreground task. |
| 76 SyncTaskManager(base::WeakPtr<Client> client, | 78 SyncTaskManager(base::WeakPtr<Client> client, |
| 77 size_t maximum_background_task, | 79 size_t maximum_background_task, |
| 78 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 80 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 79 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); | 81 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); |
| 80 virtual ~SyncTaskManager(); | 82 virtual ~SyncTaskManager(); |
| 81 | 83 |
| 82 // This needs to be called to start task scheduling. | 84 // This needs to be called to start task scheduling. |
| 83 // If |status| is not SYNC_STATUS_OK calling this may change the | 85 // If |status| is not SYNC_STATUS_OK calling this may change the |
| 84 // service status. This should not be called more than once. | 86 // service status. This should not be called more than once. |
| 85 void Initialize(SyncStatusCode status); | 87 void Initialize(SyncStatusCode status); |
| 86 | 88 |
| 87 // Schedules a task at the given priority. | 89 // Schedules a task at the given priority. |
| 88 void ScheduleTask(const tracked_objects::Location& from_here, | 90 void ScheduleTask(const tracked_objects::Location& from_here, |
| 89 const Task& task, | 91 const Task& task, |
| 90 Priority priority, | 92 Priority priority, |
| 91 const SyncStatusCallback& callback); | 93 const SyncStatusCallback& callback); |
| 92 void ScheduleSyncTask(const tracked_objects::Location& from_here, | 94 void ScheduleSyncTask(const tracked_objects::Location& from_here, |
| 93 scoped_ptr<SyncTask> task, | 95 std::unique_ptr<SyncTask> task, |
| 94 Priority priority, | 96 Priority priority, |
| 95 const SyncStatusCallback& callback); | 97 const SyncStatusCallback& callback); |
| 96 | 98 |
| 97 // Runs the posted task only when we're idle. Returns true if tha task is | 99 // Runs the posted task only when we're idle. Returns true if tha task is |
| 98 // scheduled. | 100 // scheduled. |
| 99 bool ScheduleTaskIfIdle(const tracked_objects::Location& from_here, | 101 bool ScheduleTaskIfIdle(const tracked_objects::Location& from_here, |
| 100 const Task& task, | 102 const Task& task, |
| 101 const SyncStatusCallback& callback); | 103 const SyncStatusCallback& callback); |
| 102 bool ScheduleSyncTaskIfIdle(const tracked_objects::Location& from_here, | 104 bool ScheduleSyncTaskIfIdle(const tracked_objects::Location& from_here, |
| 103 scoped_ptr<SyncTask> task, | 105 std::unique_ptr<SyncTask> task, |
| 104 const SyncStatusCallback& callback); | 106 const SyncStatusCallback& callback); |
| 105 | 107 |
| 106 // Notifies SyncTaskManager that the task associated to |token| has finished | 108 // Notifies SyncTaskManager that the task associated to |token| has finished |
| 107 // with |status|. | 109 // with |status|. |
| 108 static void NotifyTaskDone(scoped_ptr<SyncTaskToken> token, | 110 static void NotifyTaskDone(std::unique_ptr<SyncTaskToken> token, |
| 109 SyncStatusCode status); | 111 SyncStatusCode status); |
| 110 | 112 |
| 111 // Updates |task_blocker| associated to the current task by specified | 113 // Updates |task_blocker| associated to the current task by specified |
| 112 // |task_blocker| and turns the current task to a background task if | 114 // |task_blocker| and turns the current task to a background task if |
| 113 // the current task is running as a foreground task. | 115 // the current task is running as a foreground task. |
| 114 // If specified |task_blocker| is blocked by any other blocking factor | 116 // If specified |task_blocker| is blocked by any other blocking factor |
| 115 // associated to an existing background task, this function waits for the | 117 // associated to an existing background task, this function waits for the |
| 116 // existing background task to finish. | 118 // existing background task to finish. |
| 117 // Upon the task is ready to run as a background task, calls |continuation| | 119 // Upon the task is ready to run as a background task, calls |continuation| |
| 118 // with new SyncTaskToken. | 120 // with new SyncTaskToken. |
| 119 // Note that this function once releases previous |task_blocker| before | 121 // Note that this function once releases previous |task_blocker| before |
| 120 // applying new |task_blocker|. So, any other task may be run before | 122 // applying new |task_blocker|. So, any other task may be run before |
| 121 // invocation of |continuation|. | 123 // invocation of |continuation|. |
| 122 static void UpdateTaskBlocker(scoped_ptr<SyncTaskToken> current_task_token, | 124 static void UpdateTaskBlocker( |
| 123 scoped_ptr<TaskBlocker> task_blocker, | 125 std::unique_ptr<SyncTaskToken> current_task_token, |
| 124 const Continuation& continuation); | 126 std::unique_ptr<TaskBlocker> task_blocker, |
| 127 const Continuation& continuation); |
| 125 | 128 |
| 126 bool IsRunningTask(int64_t task_token_id) const; | 129 bool IsRunningTask(int64_t task_token_id) const; |
| 127 | 130 |
| 128 void DetachFromSequence(); | 131 void DetachFromSequence(); |
| 129 bool ShouldTrackTaskToken() const; | 132 bool ShouldTrackTaskToken() const; |
| 130 | 133 |
| 131 private: | 134 private: |
| 132 struct PendingTask { | 135 struct PendingTask { |
| 133 base::Closure task; | 136 base::Closure task; |
| 134 Priority priority; | 137 Priority priority; |
| 135 int64_t seq; | 138 int64_t seq; |
| 136 | 139 |
| 137 PendingTask(); | 140 PendingTask(); |
| 138 PendingTask(const base::Closure& task, Priority pri, int seq); | 141 PendingTask(const base::Closure& task, Priority pri, int seq); |
| 139 PendingTask(const PendingTask& other); | 142 PendingTask(const PendingTask& other); |
| 140 ~PendingTask(); | 143 ~PendingTask(); |
| 141 }; | 144 }; |
| 142 | 145 |
| 143 struct PendingTaskComparator { | 146 struct PendingTaskComparator { |
| 144 bool operator()(const PendingTask& left, | 147 bool operator()(const PendingTask& left, |
| 145 const PendingTask& right) const; | 148 const PendingTask& right) const; |
| 146 }; | 149 }; |
| 147 | 150 |
| 148 // Non-static version of NotifyTaskDone. | 151 // Non-static version of NotifyTaskDone. |
| 149 void NotifyTaskDoneBody(scoped_ptr<SyncTaskToken> token, | 152 void NotifyTaskDoneBody(std::unique_ptr<SyncTaskToken> token, |
| 150 SyncStatusCode status); | 153 SyncStatusCode status); |
| 151 | 154 |
| 152 // Non-static version of UpdateTaskBlocker. | 155 // Non-static version of UpdateTaskBlocker. |
| 153 void UpdateTaskBlockerBody(scoped_ptr<SyncTaskToken> foreground_task_token, | 156 void UpdateTaskBlockerBody( |
| 154 scoped_ptr<SyncTaskToken> background_task_token, | 157 std::unique_ptr<SyncTaskToken> foreground_task_token, |
| 155 scoped_ptr<TaskLogger::TaskLog> task_log, | 158 std::unique_ptr<SyncTaskToken> background_task_token, |
| 156 scoped_ptr<TaskBlocker> task_blocker, | 159 std::unique_ptr<TaskLogger::TaskLog> task_log, |
| 157 const Continuation& continuation); | 160 std::unique_ptr<TaskBlocker> task_blocker, |
| 161 const Continuation& continuation); |
| 158 | 162 |
| 159 // This should be called when an async task needs to get a task token. | 163 // This should be called when an async task needs to get a task token. |
| 160 scoped_ptr<SyncTaskToken> GetToken(const tracked_objects::Location& from_here, | 164 std::unique_ptr<SyncTaskToken> GetToken( |
| 161 const SyncStatusCallback& callback); | 165 const tracked_objects::Location& from_here, |
| 166 const SyncStatusCallback& callback); |
| 162 | 167 |
| 163 scoped_ptr<SyncTaskToken> GetTokenForBackgroundTask( | 168 std::unique_ptr<SyncTaskToken> GetTokenForBackgroundTask( |
| 164 const tracked_objects::Location& from_here, | 169 const tracked_objects::Location& from_here, |
| 165 const SyncStatusCallback& callback, | 170 const SyncStatusCallback& callback, |
| 166 scoped_ptr<TaskBlocker> task_blocker); | 171 std::unique_ptr<TaskBlocker> task_blocker); |
| 167 | 172 |
| 168 void PushPendingTask(const base::Closure& closure, Priority priority); | 173 void PushPendingTask(const base::Closure& closure, Priority priority); |
| 169 | 174 |
| 170 void RunTask(scoped_ptr<SyncTaskToken> token, | 175 void RunTask(std::unique_ptr<SyncTaskToken> token, |
| 171 scoped_ptr<SyncTask> task); | 176 std::unique_ptr<SyncTask> task); |
| 172 | 177 |
| 173 // Runs a pending task as a foreground task if possible. | 178 // Runs a pending task as a foreground task if possible. |
| 174 // If |token| is non-nullptr, put |token| back to |token_| beforehand. | 179 // If |token| is non-nullptr, put |token| back to |token_| beforehand. |
| 175 void MaybeStartNextForegroundTask(scoped_ptr<SyncTaskToken> token); | 180 void MaybeStartNextForegroundTask(std::unique_ptr<SyncTaskToken> token); |
| 176 | 181 |
| 177 base::WeakPtr<Client> client_; | 182 base::WeakPtr<Client> client_; |
| 178 | 183 |
| 179 // Owns running SyncTask to cancel the task on SyncTaskManager deletion. | 184 // Owns running SyncTask to cancel the task on SyncTaskManager deletion. |
| 180 scoped_ptr<SyncTask> running_foreground_task_; | 185 std::unique_ptr<SyncTask> running_foreground_task_; |
| 181 | 186 |
| 182 // Owns running backgrounded SyncTask to cancel the task on SyncTaskManager | 187 // Owns running backgrounded SyncTask to cancel the task on SyncTaskManager |
| 183 // deletion. | 188 // deletion. |
| 184 base::ScopedPtrHashMap<int64_t, scoped_ptr<SyncTask>> | 189 base::ScopedPtrHashMap<int64_t, std::unique_ptr<SyncTask>> |
| 185 running_background_tasks_; | 190 running_background_tasks_; |
| 186 | 191 |
| 187 size_t maximum_background_task_; | 192 size_t maximum_background_task_; |
| 188 | 193 |
| 189 // Holds pending continuation to move task to background. | 194 // Holds pending continuation to move task to background. |
| 190 base::Closure pending_backgrounding_task_; | 195 base::Closure pending_backgrounding_task_; |
| 191 | 196 |
| 192 std::priority_queue<PendingTask, std::vector<PendingTask>, | 197 std::priority_queue<PendingTask, std::vector<PendingTask>, |
| 193 PendingTaskComparator> pending_tasks_; | 198 PendingTaskComparator> pending_tasks_; |
| 194 int64_t pending_task_seq_; | 199 int64_t pending_task_seq_; |
| 195 int64_t task_token_seq_; | 200 int64_t task_token_seq_; |
| 196 | 201 |
| 197 // Absence of |token_| implies a task is running. Incoming tasks should | 202 // Absence of |token_| implies a task is running. Incoming tasks should |
| 198 // wait for the task to finish in |pending_tasks_| if |token_| is null. | 203 // wait for the task to finish in |pending_tasks_| if |token_| is null. |
| 199 // Each task must take TaskToken instance from |token_| and must hold it | 204 // Each task must take TaskToken instance from |token_| and must hold it |
| 200 // until it finished. And the task must return the instance through | 205 // until it finished. And the task must return the instance through |
| 201 // NotifyTaskDone when the task finished. | 206 // NotifyTaskDone when the task finished. |
| 202 scoped_ptr<SyncTaskToken> token_; | 207 std::unique_ptr<SyncTaskToken> token_; |
| 203 | 208 |
| 204 TaskDependencyManager dependency_manager_; | 209 TaskDependencyManager dependency_manager_; |
| 205 | 210 |
| 206 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 211 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 207 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 212 scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
| 208 base::SequenceChecker sequence_checker_; | 213 base::SequenceChecker sequence_checker_; |
| 209 | 214 |
| 210 base::WeakPtrFactory<SyncTaskManager> weak_ptr_factory_; | 215 base::WeakPtrFactory<SyncTaskManager> weak_ptr_factory_; |
| 211 | 216 |
| 212 DISALLOW_COPY_AND_ASSIGN(SyncTaskManager); | 217 DISALLOW_COPY_AND_ASSIGN(SyncTaskManager); |
| 213 }; | 218 }; |
| 214 | 219 |
| 215 } // namespace drive_backend | 220 } // namespace drive_backend |
| 216 } // namespace sync_file_system | 221 } // namespace sync_file_system |
| 217 | 222 |
| 218 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_TASK_MANAGER_H_ | 223 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_TASK_MANAGER_H_ |
| OLD | NEW |