sync_task_token.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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 3// found in the LICENSE file. 4 5#include "chrome/browser/sync_file_system/drive_backend/sync_task_token.h" 6 7#include "base/bind.h" 8#include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h" 9#include "chrome/browser/sync_file_system/drive_backend/task_dependency_manager.h" 10 11namespace sync_file_system { 12namespace drive_backend { 13 14const int64 SyncTaskToken::kForegroundTaskTokenID = 0; 15const int64 SyncTaskToken::kMinimumBackgroundTaskTokenID = 1; 16 17// static 18scoped_ptr<SyncTaskToken> SyncTaskToken::CreateForForegroundTask( 19 const base::WeakPtr<SyncTaskManager>& manager) { 20 return make_scoped_ptr(new SyncTaskToken( 21 manager, 22 kForegroundTaskTokenID, 23 scoped_ptr<BlockingFactor>())); 24} 25 26// static 27scoped_ptr<SyncTaskToken> SyncTaskToken::CreateForBackgroundTask( 28 const base::WeakPtr<SyncTaskManager>& manager, 29 int64 token_id, 30 scoped_ptr<BlockingFactor> blocking_factor) { 31 return make_scoped_ptr(new SyncTaskToken( 32 manager, 33 token_id, 34 blocking_factor.Pass())); 35} 36 37void SyncTaskToken::UpdateTask(const tracked_objects::Location& location, 38 const SyncStatusCallback& callback) { 39 DCHECK(callback_.is_null()); 40 location_ = location; 41 callback_ = callback; 42 DVLOG(2) << "Token updated: " << location_.ToString(); 43} 44 45SyncTaskToken::~SyncTaskToken() { 46 // All task on Client must hold TaskToken instance to ensure 47 // no other tasks are running. Also, as soon as a task finishes to work, 48 // it must return the token to TaskManager. 49 // Destroying a token with valid |client| indicates the token was 50 // dropped by a task without returning. 51 if (manager_.get() && manager_->IsRunningTask(token_id_)) { 52 NOTREACHED() 53 << "Unexpected TaskToken deletion from: " << location_.ToString(); 54 55 // Reinitializes the token. 56 SyncTaskManager::NotifyTaskDone( 57 make_scoped_ptr(new SyncTaskToken( 58 manager_, token_id_, blocking_factor_.Pass())), 59 SYNC_STATUS_OK); 60 } 61} 62 63// static 64SyncStatusCallback SyncTaskToken::WrapToCallback( 65 scoped_ptr<SyncTaskToken> token) { 66 return base::Bind(&SyncTaskManager::NotifyTaskDone, base::Passed(&token)); 67} 68 69const BlockingFactor* SyncTaskToken::blocking_factor() const { 70 return blocking_factor_.get(); 71} 72 73void SyncTaskToken::clear_blocking_factor() { 74 blocking_factor_.reset(); 75} 76 77SyncTaskToken::SyncTaskToken(const base::WeakPtr<SyncTaskManager>& manager, 78 int64 token_id, 79 scoped_ptr<BlockingFactor> blocking_factor) 80 : manager_(manager), 81 token_id_(token_id), 82 blocking_factor_(blocking_factor.Pass()) { 83} 84 85} // namespace drive_backend 86} // namespace sync_file_system 87