Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / sync_task_token.cc
blobb4d58bf862d2713c45ebfbb6ef3a6333a88c6878
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.
5 #include "chrome/browser/sync_file_system/drive_backend/sync_task_token.h"
7 #include "base/bind.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "base/trace_event/trace_event.h"
10 #include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h"
11 #include "chrome/browser/sync_file_system/drive_backend/task_dependency_manager.h"
13 namespace sync_file_system {
14 namespace drive_backend {
16 const int64 SyncTaskToken::kTestingTaskTokenID = -1;
17 const int64 SyncTaskToken::kForegroundTaskTokenID = 0;
18 const int64 SyncTaskToken::kMinimumBackgroundTaskTokenID = 1;
20 // static
21 scoped_ptr<SyncTaskToken> SyncTaskToken::CreateForTesting(
22 const SyncStatusCallback& callback) {
23 return make_scoped_ptr(new SyncTaskToken(
24 base::WeakPtr<SyncTaskManager>(),
25 base::ThreadTaskRunnerHandle::Get(),
26 kTestingTaskTokenID,
27 nullptr, // task_blocker
28 callback));
31 // static
32 scoped_ptr<SyncTaskToken> SyncTaskToken::CreateForForegroundTask(
33 const base::WeakPtr<SyncTaskManager>& manager,
34 base::SequencedTaskRunner* task_runner) {
35 return make_scoped_ptr(new SyncTaskToken(
36 manager,
37 task_runner,
38 kForegroundTaskTokenID,
39 nullptr, // task_blocker
40 SyncStatusCallback()));
43 // static
44 scoped_ptr<SyncTaskToken> SyncTaskToken::CreateForBackgroundTask(
45 const base::WeakPtr<SyncTaskManager>& manager,
46 base::SequencedTaskRunner* task_runner,
47 int64 token_id,
48 scoped_ptr<TaskBlocker> task_blocker) {
49 return make_scoped_ptr(new SyncTaskToken(
50 manager,
51 task_runner,
52 token_id,
53 task_blocker.Pass(),
54 SyncStatusCallback()));
57 void SyncTaskToken::UpdateTask(const tracked_objects::Location& location,
58 const SyncStatusCallback& callback) {
59 DCHECK(callback_.is_null());
60 location_ = location;
61 callback_ = callback;
62 DVLOG(2) << "Token updated: " << location_.ToString();
65 SyncTaskToken::~SyncTaskToken() {
66 // All task on Client must hold TaskToken instance to ensure
67 // no other tasks are running. Also, as soon as a task finishes to work,
68 // it must return the token to TaskManager.
69 // Destroying a token with valid |client| indicates the token was
70 // dropped by a task without returning.
71 if (task_runner_.get() && task_runner_->RunsTasksOnCurrentThread() &&
72 manager_ && manager_->IsRunningTask(token_id_)) {
73 if (!manager_->ShouldTrackTaskToken())
74 return;
76 NOTREACHED()
77 << "Unexpected TaskToken deletion from: " << location_.ToString();
79 // Reinitializes the token.
80 SyncTaskManager::NotifyTaskDone(
81 make_scoped_ptr(new SyncTaskToken(manager_,
82 task_runner_.get(),
83 token_id_,
84 task_blocker_.Pass(),
85 SyncStatusCallback())),
86 SYNC_STATUS_OK);
90 // static
91 SyncStatusCallback SyncTaskToken::WrapToCallback(
92 scoped_ptr<SyncTaskToken> token) {
93 return base::Bind(&SyncTaskManager::NotifyTaskDone, base::Passed(&token));
96 void SyncTaskToken::set_task_blocker(
97 scoped_ptr<TaskBlocker> task_blocker) {
98 task_blocker_ = task_blocker.Pass();
101 const TaskBlocker* SyncTaskToken::task_blocker() const {
102 return task_blocker_.get();
105 void SyncTaskToken::clear_task_blocker() {
106 task_blocker_.reset();
109 void SyncTaskToken::InitializeTaskLog(const std::string& task_description) {
110 task_log_.reset(new TaskLogger::TaskLog);
111 task_log_->start_time = base::TimeTicks::Now();
112 task_log_->task_description = task_description;
114 TRACE_EVENT_ASYNC_BEGIN1(
115 TRACE_DISABLED_BY_DEFAULT("SyncFileSystem"),
116 "SyncTask", task_log_->log_id,
117 "task_description", task_description);
120 void SyncTaskToken::FinalizeTaskLog(const std::string& result_description) {
121 TRACE_EVENT_ASYNC_END1(
122 TRACE_DISABLED_BY_DEFAULT("SyncFileSystem"),
123 "SyncTask", task_log_->log_id,
124 "result_description", result_description);
126 DCHECK(task_log_);
127 task_log_->result_description = result_description;
128 task_log_->end_time = base::TimeTicks::Now();
131 void SyncTaskToken::RecordLog(const std::string& message) {
132 DCHECK(task_log_);
133 task_log_->details.push_back(message);
136 void SyncTaskToken::SetTaskLog(scoped_ptr<TaskLogger::TaskLog> task_log) {
137 task_log_ = task_log.Pass();
140 scoped_ptr<TaskLogger::TaskLog> SyncTaskToken::PassTaskLog() {
141 return task_log_.Pass();
144 SyncTaskToken::SyncTaskToken(
145 const base::WeakPtr<SyncTaskManager>& manager,
146 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
147 int64 token_id,
148 scoped_ptr<TaskBlocker> task_blocker,
149 const SyncStatusCallback& callback)
150 : manager_(manager),
151 task_runner_(task_runner),
152 token_id_(token_id),
153 callback_(callback),
154 task_blocker_(task_blocker.Pass()) {
157 } // namespace drive_backend
158 } // namespace sync_file_system