| 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 #include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 } | 72 } |
| 73 | 73 |
| 74 SyncTaskManager::~SyncTaskManager() { | 74 SyncTaskManager::~SyncTaskManager() { |
| 75 weak_ptr_factory_.InvalidateWeakPtrs(); | 75 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 76 | 76 |
| 77 client_.reset(); | 77 client_.reset(); |
| 78 token_.reset(); | 78 token_.reset(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void SyncTaskManager::Initialize(SyncStatusCode status) { | 81 void SyncTaskManager::Initialize(SyncStatusCode status) { |
| 82 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 82 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 83 DCHECK(!token_); | 83 DCHECK(!token_); |
| 84 NotifyTaskDone( | 84 NotifyTaskDone( |
| 85 SyncTaskToken::CreateForForegroundTask( | 85 SyncTaskToken::CreateForForegroundTask( |
| 86 weak_ptr_factory_.GetWeakPtr(), task_runner_.get()), | 86 weak_ptr_factory_.GetWeakPtr(), task_runner_.get()), |
| 87 status); | 87 status); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void SyncTaskManager::ScheduleTask( | 90 void SyncTaskManager::ScheduleTask( |
| 91 const tracked_objects::Location& from_here, | 91 const tracked_objects::Location& from_here, |
| 92 const Task& task, | 92 const Task& task, |
| 93 Priority priority, | 93 Priority priority, |
| 94 const SyncStatusCallback& callback) { | 94 const SyncStatusCallback& callback) { |
| 95 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 95 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 96 | 96 |
| 97 ScheduleSyncTask(from_here, | 97 ScheduleSyncTask(from_here, |
| 98 std::unique_ptr<SyncTask>(new SyncTaskAdapter(task)), | 98 std::unique_ptr<SyncTask>(new SyncTaskAdapter(task)), |
| 99 priority, callback); | 99 priority, callback); |
| 100 } | 100 } |
| 101 | 101 |
| 102 void SyncTaskManager::ScheduleSyncTask( | 102 void SyncTaskManager::ScheduleSyncTask( |
| 103 const tracked_objects::Location& from_here, | 103 const tracked_objects::Location& from_here, |
| 104 std::unique_ptr<SyncTask> task, | 104 std::unique_ptr<SyncTask> task, |
| 105 Priority priority, | 105 Priority priority, |
| 106 const SyncStatusCallback& callback) { | 106 const SyncStatusCallback& callback) { |
| 107 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 107 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 108 | 108 |
| 109 std::unique_ptr<SyncTaskToken> token(GetToken(from_here, callback)); | 109 std::unique_ptr<SyncTaskToken> token(GetToken(from_here, callback)); |
| 110 if (!token) { | 110 if (!token) { |
| 111 PushPendingTask( | 111 PushPendingTask( |
| 112 base::Bind(&SyncTaskManager::ScheduleSyncTask, | 112 base::Bind(&SyncTaskManager::ScheduleSyncTask, |
| 113 weak_ptr_factory_.GetWeakPtr(), from_here, | 113 weak_ptr_factory_.GetWeakPtr(), from_here, |
| 114 base::Passed(&task), priority, callback), | 114 base::Passed(&task), priority, callback), |
| 115 priority); | 115 priority); |
| 116 return; | 116 return; |
| 117 } | 117 } |
| 118 RunTask(std::move(token), std::move(task)); | 118 RunTask(std::move(token), std::move(task)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 bool SyncTaskManager::ScheduleTaskIfIdle( | 121 bool SyncTaskManager::ScheduleTaskIfIdle( |
| 122 const tracked_objects::Location& from_here, | 122 const tracked_objects::Location& from_here, |
| 123 const Task& task, | 123 const Task& task, |
| 124 const SyncStatusCallback& callback) { | 124 const SyncStatusCallback& callback) { |
| 125 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 125 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 126 | 126 |
| 127 return ScheduleSyncTaskIfIdle( | 127 return ScheduleSyncTaskIfIdle( |
| 128 from_here, std::unique_ptr<SyncTask>(new SyncTaskAdapter(task)), | 128 from_here, std::unique_ptr<SyncTask>(new SyncTaskAdapter(task)), |
| 129 callback); | 129 callback); |
| 130 } | 130 } |
| 131 | 131 |
| 132 bool SyncTaskManager::ScheduleSyncTaskIfIdle( | 132 bool SyncTaskManager::ScheduleSyncTaskIfIdle( |
| 133 const tracked_objects::Location& from_here, | 133 const tracked_objects::Location& from_here, |
| 134 std::unique_ptr<SyncTask> task, | 134 std::unique_ptr<SyncTask> task, |
| 135 const SyncStatusCallback& callback) { | 135 const SyncStatusCallback& callback) { |
| 136 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 136 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 137 | 137 |
| 138 std::unique_ptr<SyncTaskToken> token(GetToken(from_here, callback)); | 138 std::unique_ptr<SyncTaskToken> token(GetToken(from_here, callback)); |
| 139 if (!token) | 139 if (!token) |
| 140 return false; | 140 return false; |
| 141 RunTask(std::move(token), std::move(task)); | 141 RunTask(std::move(token), std::move(task)); |
| 142 return true; | 142 return true; |
| 143 } | 143 } |
| 144 | 144 |
| 145 // static | 145 // static |
| 146 void SyncTaskManager::NotifyTaskDone(std::unique_ptr<SyncTaskToken> token, | 146 void SyncTaskManager::NotifyTaskDone(std::unique_ptr<SyncTaskToken> token, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 foreground_task_token = std::move(current_task_token); | 185 foreground_task_token = std::move(current_task_token); |
| 186 else | 186 else |
| 187 background_task_token = std::move(current_task_token); | 187 background_task_token = std::move(current_task_token); |
| 188 | 188 |
| 189 manager->UpdateTaskBlockerBody( | 189 manager->UpdateTaskBlockerBody( |
| 190 std::move(foreground_task_token), std::move(background_task_token), | 190 std::move(foreground_task_token), std::move(background_task_token), |
| 191 std::move(task_log), std::move(task_blocker), continuation); | 191 std::move(task_log), std::move(task_blocker), continuation); |
| 192 } | 192 } |
| 193 | 193 |
| 194 bool SyncTaskManager::IsRunningTask(int64_t token_id) const { | 194 bool SyncTaskManager::IsRunningTask(int64_t token_id) const { |
| 195 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 195 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 196 | 196 |
| 197 // If the client is gone, all task should be aborted. | 197 // If the client is gone, all task should be aborted. |
| 198 if (!client_) | 198 if (!client_) |
| 199 return false; | 199 return false; |
| 200 | 200 |
| 201 if (token_id == SyncTaskToken::kForegroundTaskTokenID) | 201 if (token_id == SyncTaskToken::kForegroundTaskTokenID) |
| 202 return true; | 202 return true; |
| 203 | 203 |
| 204 return ContainsKey(running_background_tasks_, token_id); | 204 return ContainsKey(running_background_tasks_, token_id); |
| 205 } | 205 } |
| 206 | 206 |
| 207 void SyncTaskManager::DetachFromSequence() { | 207 void SyncTaskManager::DetachFromSequence() { |
| 208 sequence_checker_.DetachFromSequence(); | 208 sequence_checker_.DetachFromSequence(); |
| 209 } | 209 } |
| 210 | 210 |
| 211 bool SyncTaskManager::ShouldTrackTaskToken() const { | 211 bool SyncTaskManager::ShouldTrackTaskToken() const { |
| 212 return !worker_pool_ || !worker_pool_->IsShutdownInProgress(); | 212 return !worker_pool_ || !worker_pool_->IsShutdownInProgress(); |
| 213 } | 213 } |
| 214 | 214 |
| 215 void SyncTaskManager::NotifyTaskDoneBody(std::unique_ptr<SyncTaskToken> token, | 215 void SyncTaskManager::NotifyTaskDoneBody(std::unique_ptr<SyncTaskToken> token, |
| 216 SyncStatusCode status) { | 216 SyncStatusCode status) { |
| 217 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 217 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 218 DCHECK(token); | 218 DCHECK(token); |
| 219 | 219 |
| 220 DVLOG(3) << "NotifyTaskDone: " << "finished with status=" << status | 220 DVLOG(3) << "NotifyTaskDone: " << "finished with status=" << status |
| 221 << " (" << SyncStatusCodeToString(status) << ")" | 221 << " (" << SyncStatusCodeToString(status) << ")" |
| 222 << " " << token->location().ToString(); | 222 << " " << token->location().ToString(); |
| 223 | 223 |
| 224 if (token->task_blocker()) { | 224 if (token->task_blocker()) { |
| 225 dependency_manager_.Erase(token->task_blocker()); | 225 dependency_manager_.Erase(token->task_blocker()); |
| 226 token->clear_task_blocker(); | 226 token->clear_task_blocker(); |
| 227 } | 227 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 base::Bind(&SyncTaskManager::MaybeStartNextForegroundTask, | 263 base::Bind(&SyncTaskManager::MaybeStartNextForegroundTask, |
| 264 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token))); | 264 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token))); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void SyncTaskManager::UpdateTaskBlockerBody( | 267 void SyncTaskManager::UpdateTaskBlockerBody( |
| 268 std::unique_ptr<SyncTaskToken> foreground_task_token, | 268 std::unique_ptr<SyncTaskToken> foreground_task_token, |
| 269 std::unique_ptr<SyncTaskToken> background_task_token, | 269 std::unique_ptr<SyncTaskToken> background_task_token, |
| 270 std::unique_ptr<TaskLogger::TaskLog> task_log, | 270 std::unique_ptr<TaskLogger::TaskLog> task_log, |
| 271 std::unique_ptr<TaskBlocker> task_blocker, | 271 std::unique_ptr<TaskBlocker> task_blocker, |
| 272 const Continuation& continuation) { | 272 const Continuation& continuation) { |
| 273 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 273 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 274 | 274 |
| 275 // Run the task directly if the parallelization is disabled. | 275 // Run the task directly if the parallelization is disabled. |
| 276 if (!maximum_background_task_) { | 276 if (!maximum_background_task_) { |
| 277 DCHECK(foreground_task_token); | 277 DCHECK(foreground_task_token); |
| 278 DCHECK(!background_task_token); | 278 DCHECK(!background_task_token); |
| 279 foreground_task_token->SetTaskLog(std::move(task_log)); | 279 foreground_task_token->SetTaskLog(std::move(task_log)); |
| 280 continuation.Run(std::move(foreground_task_token)); | 280 continuation.Run(std::move(foreground_task_token)); |
| 281 return; | 281 return; |
| 282 } | 282 } |
| 283 | 283 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 | 349 |
| 350 token_ = std::move(foreground_task_token); | 350 token_ = std::move(foreground_task_token); |
| 351 MaybeStartNextForegroundTask(nullptr); | 351 MaybeStartNextForegroundTask(nullptr); |
| 352 background_task_token->SetTaskLog(std::move(task_log)); | 352 background_task_token->SetTaskLog(std::move(task_log)); |
| 353 continuation.Run(std::move(background_task_token)); | 353 continuation.Run(std::move(background_task_token)); |
| 354 } | 354 } |
| 355 | 355 |
| 356 std::unique_ptr<SyncTaskToken> SyncTaskManager::GetToken( | 356 std::unique_ptr<SyncTaskToken> SyncTaskManager::GetToken( |
| 357 const tracked_objects::Location& from_here, | 357 const tracked_objects::Location& from_here, |
| 358 const SyncStatusCallback& callback) { | 358 const SyncStatusCallback& callback) { |
| 359 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 359 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 360 | 360 |
| 361 if (!token_) | 361 if (!token_) |
| 362 return nullptr; | 362 return nullptr; |
| 363 token_->UpdateTask(from_here, callback); | 363 token_->UpdateTask(from_here, callback); |
| 364 return std::move(token_); | 364 return std::move(token_); |
| 365 } | 365 } |
| 366 | 366 |
| 367 void SyncTaskManager::PushPendingTask( | 367 void SyncTaskManager::PushPendingTask( |
| 368 const base::Closure& closure, Priority priority) { | 368 const base::Closure& closure, Priority priority) { |
| 369 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 369 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 370 | 370 |
| 371 pending_tasks_.push(PendingTask(closure, priority, pending_task_seq_++)); | 371 pending_tasks_.push(PendingTask(closure, priority, pending_task_seq_++)); |
| 372 } | 372 } |
| 373 | 373 |
| 374 void SyncTaskManager::RunTask(std::unique_ptr<SyncTaskToken> token, | 374 void SyncTaskManager::RunTask(std::unique_ptr<SyncTaskToken> token, |
| 375 std::unique_ptr<SyncTask> task) { | 375 std::unique_ptr<SyncTask> task) { |
| 376 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 376 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 377 DCHECK(!running_foreground_task_); | 377 DCHECK(!running_foreground_task_); |
| 378 | 378 |
| 379 running_foreground_task_ = std::move(task); | 379 running_foreground_task_ = std::move(task); |
| 380 running_foreground_task_->RunPreflight(std::move(token)); | 380 running_foreground_task_->RunPreflight(std::move(token)); |
| 381 } | 381 } |
| 382 | 382 |
| 383 void SyncTaskManager::MaybeStartNextForegroundTask( | 383 void SyncTaskManager::MaybeStartNextForegroundTask( |
| 384 std::unique_ptr<SyncTaskToken> token) { | 384 std::unique_ptr<SyncTaskToken> token) { |
| 385 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 385 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 386 | 386 |
| 387 if (token) { | 387 if (token) { |
| 388 DCHECK(!token_); | 388 DCHECK(!token_); |
| 389 token_ = std::move(token); | 389 token_ = std::move(token); |
| 390 } | 390 } |
| 391 | 391 |
| 392 if (!pending_backgrounding_task_.is_null()) { | 392 if (!pending_backgrounding_task_.is_null()) { |
| 393 base::Closure closure = pending_backgrounding_task_; | 393 base::Closure closure = pending_backgrounding_task_; |
| 394 pending_backgrounding_task_.Reset(); | 394 pending_backgrounding_task_.Reset(); |
| 395 closure.Run(); | 395 closure.Run(); |
| 396 return; | 396 return; |
| 397 } | 397 } |
| 398 | 398 |
| 399 if (!token_) | 399 if (!token_) |
| 400 return; | 400 return; |
| 401 | 401 |
| 402 if (!pending_tasks_.empty()) { | 402 if (!pending_tasks_.empty()) { |
| 403 base::Closure closure = pending_tasks_.top().task; | 403 base::Closure closure = pending_tasks_.top().task; |
| 404 pending_tasks_.pop(); | 404 pending_tasks_.pop(); |
| 405 closure.Run(); | 405 closure.Run(); |
| 406 return; | 406 return; |
| 407 } | 407 } |
| 408 | 408 |
| 409 if (client_) | 409 if (client_) |
| 410 client_->MaybeScheduleNextTask(); | 410 client_->MaybeScheduleNextTask(); |
| 411 } | 411 } |
| 412 | 412 |
| 413 } // namespace drive_backend | 413 } // namespace drive_backend |
| 414 } // namespace sync_file_system | 414 } // namespace sync_file_system |
| OLD | NEW |