sync_task_token.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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
69void SyncTaskToken::set_blocking_factor(
70    scoped_ptr<BlockingFactor> blocking_factor) {
71  blocking_factor_ = blocking_factor.Pass();
72}
73
74const BlockingFactor* SyncTaskToken::blocking_factor() const {
75  return blocking_factor_.get();
76}
77
78void SyncTaskToken::clear_blocking_factor() {
79  blocking_factor_.reset();
80}
81
82SyncTaskToken::SyncTaskToken(const base::WeakPtr<SyncTaskManager>& manager,
83                             int64 token_id,
84                             scoped_ptr<BlockingFactor> blocking_factor)
85    : manager_(manager),
86      token_id_(token_id),
87      blocking_factor_(blocking_factor.Pass()) {
88}
89
90}  // namespace drive_backend
91}  // namespace sync_file_system
92