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