Home | History | Annotate | Download | only in drive_backend
      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 "base/debug/trace_event.h"
      9 #include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h"
     10 #include "chrome/browser/sync_file_system/drive_backend/task_dependency_manager.h"
     11 
     12 namespace sync_file_system {
     13 namespace drive_backend {
     14 
     15 const int64 SyncTaskToken::kTestingTaskTokenID = -1;
     16 const int64 SyncTaskToken::kForegroundTaskTokenID = 0;
     17 const int64 SyncTaskToken::kMinimumBackgroundTaskTokenID = 1;
     18 
     19 // static
     20 scoped_ptr<SyncTaskToken> SyncTaskToken::CreateForTesting(
     21     const SyncStatusCallback& callback) {
     22   return make_scoped_ptr(new SyncTaskToken(
     23       base::WeakPtr<SyncTaskManager>(),
     24       kTestingTaskTokenID,
     25       scoped_ptr<BlockingFactor>(),
     26       callback));
     27 }
     28 
     29 // static
     30 scoped_ptr<SyncTaskToken> SyncTaskToken::CreateForForegroundTask(
     31     const base::WeakPtr<SyncTaskManager>& manager) {
     32   return make_scoped_ptr(new SyncTaskToken(
     33       manager,
     34       kForegroundTaskTokenID,
     35       scoped_ptr<BlockingFactor>(),
     36       SyncStatusCallback()));
     37 }
     38 
     39 // static
     40 scoped_ptr<SyncTaskToken> SyncTaskToken::CreateForBackgroundTask(
     41     const base::WeakPtr<SyncTaskManager>& manager,
     42     int64 token_id,
     43     scoped_ptr<BlockingFactor> blocking_factor) {
     44   return make_scoped_ptr(new SyncTaskToken(
     45       manager,
     46       token_id,
     47       blocking_factor.Pass(),
     48       SyncStatusCallback()));
     49 }
     50 
     51 void SyncTaskToken::UpdateTask(const tracked_objects::Location& location,
     52                                const SyncStatusCallback& callback) {
     53   DCHECK(callback_.is_null());
     54   location_ = location;
     55   callback_ = callback;
     56   DVLOG(2) << "Token updated: " << location_.ToString();
     57 }
     58 
     59 SyncTaskToken::~SyncTaskToken() {
     60   // All task on Client must hold TaskToken instance to ensure
     61   // no other tasks are running. Also, as soon as a task finishes to work,
     62   // it must return the token to TaskManager.
     63   // Destroying a token with valid |client| indicates the token was
     64   // dropped by a task without returning.
     65   if (manager_.get() && manager_->IsRunningTask(token_id_)) {
     66     NOTREACHED()
     67         << "Unexpected TaskToken deletion from: " << location_.ToString();
     68 
     69     // Reinitializes the token.
     70     SyncTaskManager::NotifyTaskDone(
     71         make_scoped_ptr(new SyncTaskToken(
     72             manager_, token_id_, blocking_factor_.Pass(),
     73             SyncStatusCallback())),
     74         SYNC_STATUS_OK);
     75   }
     76 }
     77 
     78 // static
     79 SyncStatusCallback SyncTaskToken::WrapToCallback(
     80     scoped_ptr<SyncTaskToken> token) {
     81   return base::Bind(&SyncTaskManager::NotifyTaskDone, base::Passed(&token));
     82 }
     83 
     84 void SyncTaskToken::set_blocking_factor(
     85     scoped_ptr<BlockingFactor> blocking_factor) {
     86   blocking_factor_ = blocking_factor.Pass();
     87 }
     88 
     89 const BlockingFactor* SyncTaskToken::blocking_factor() const {
     90   return blocking_factor_.get();
     91 }
     92 
     93 void SyncTaskToken::clear_blocking_factor() {
     94   blocking_factor_.reset();
     95 }
     96 
     97 void SyncTaskToken::InitializeTaskLog(const std::string& task_description) {
     98   task_log_.reset(new TaskLogger::TaskLog);
     99   task_log_->start_time = base::TimeTicks::Now();
    100   task_log_->task_description = task_description;
    101 
    102   TRACE_EVENT_ASYNC_BEGIN1(
    103       TRACE_DISABLED_BY_DEFAULT("SyncFileSystem"),
    104       "SyncTask", task_log_->log_id,
    105       "task_description", task_description);
    106 }
    107 
    108 void SyncTaskToken::FinalizeTaskLog(const std::string& result_description) {
    109   TRACE_EVENT_ASYNC_END1(
    110       TRACE_DISABLED_BY_DEFAULT("SyncFileSystem"),
    111       "SyncTask", task_log_->log_id,
    112       "result_description", result_description);
    113 
    114   DCHECK(task_log_);
    115   task_log_->result_description = result_description;
    116   task_log_->end_time = base::TimeTicks::Now();
    117 }
    118 
    119 void SyncTaskToken::RecordLog(const std::string& message) {
    120   DCHECK(task_log_);
    121   task_log_->details.push_back(message);
    122 }
    123 
    124 void SyncTaskToken::SetTaskLog(scoped_ptr<TaskLogger::TaskLog> task_log) {
    125   task_log_ = task_log.Pass();
    126 }
    127 
    128 scoped_ptr<TaskLogger::TaskLog> SyncTaskToken::PassTaskLog() {
    129   return task_log_.Pass();
    130 }
    131 
    132 SyncTaskToken::SyncTaskToken(const base::WeakPtr<SyncTaskManager>& manager,
    133                              int64 token_id,
    134                              scoped_ptr<BlockingFactor> blocking_factor,
    135                              const SyncStatusCallback& callback)
    136     : manager_(manager),
    137       token_id_(token_id),
    138       callback_(callback),
    139       blocking_factor_(blocking_factor.Pass()) {
    140 }
    141 
    142 }  // namespace drive_backend
    143 }  // namespace sync_file_system
    144