| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h" |
| 6 |
| 5 #include <stdint.h> | 7 #include <stdint.h> |
| 6 | |
| 7 #include <deque> | 8 #include <deque> |
| 8 #include <string> | 9 #include <string> |
| 10 #include <utility> |
| 9 | 11 |
| 10 #include "base/bind.h" | 12 #include "base/bind.h" |
| 11 #include "base/location.h" | 13 #include "base/location.h" |
| 12 #include "base/macros.h" | 14 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 16 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 17 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
| 18 #include "chrome/browser/sync_file_system/drive_backend/sync_task.h" | 20 #include "chrome/browser/sync_file_system/drive_backend/sync_task.h" |
| 19 #include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h" | |
| 20 #include "chrome/browser/sync_file_system/drive_backend/sync_task_token.h" | 21 #include "chrome/browser/sync_file_system/drive_backend/sync_task_token.h" |
| 21 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" | 22 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" |
| 22 #include "storage/common/fileapi/file_system_util.h" | 23 #include "storage/common/fileapi/file_system_util.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 #define MAKE_PATH(path) \ | 26 #define MAKE_PATH(path) \ |
| 26 base::FilePath(storage::VirtualPath::GetNormalizedFilePath( \ | 27 base::FilePath(storage::VirtualPath::GetNormalizedFilePath( \ |
| 27 base::FilePath(FILE_PATH_LITERAL(path)))) | 28 base::FilePath(FILE_PATH_LITERAL(path)))) |
| 28 | 29 |
| 29 namespace sync_file_system { | 30 namespace sync_file_system { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 } | 192 } |
| 192 | 193 |
| 193 ~BackgroundTask() override {} | 194 ~BackgroundTask() override {} |
| 194 | 195 |
| 195 void RunPreflight(scoped_ptr<SyncTaskToken> token) override { | 196 void RunPreflight(scoped_ptr<SyncTaskToken> token) override { |
| 196 scoped_ptr<TaskBlocker> task_blocker(new TaskBlocker); | 197 scoped_ptr<TaskBlocker> task_blocker(new TaskBlocker); |
| 197 task_blocker->app_id = app_id_; | 198 task_blocker->app_id = app_id_; |
| 198 task_blocker->paths.push_back(path_); | 199 task_blocker->paths.push_back(path_); |
| 199 | 200 |
| 200 SyncTaskManager::UpdateTaskBlocker( | 201 SyncTaskManager::UpdateTaskBlocker( |
| 201 token.Pass(), task_blocker.Pass(), | 202 std::move(token), std::move(task_blocker), |
| 202 base::Bind(&BackgroundTask::RunAsBackgroundTask, | 203 base::Bind(&BackgroundTask::RunAsBackgroundTask, |
| 203 weak_ptr_factory_.GetWeakPtr())); | 204 weak_ptr_factory_.GetWeakPtr())); |
| 204 } | 205 } |
| 205 | 206 |
| 206 private: | 207 private: |
| 207 void RunAsBackgroundTask(scoped_ptr<SyncTaskToken> token) { | 208 void RunAsBackgroundTask(scoped_ptr<SyncTaskToken> token) { |
| 208 ++(stats_->running_background_task); | 209 ++(stats_->running_background_task); |
| 209 if (stats_->max_parallel_task < stats_->running_background_task) | 210 if (stats_->max_parallel_task < stats_->running_background_task) |
| 210 stats_->max_parallel_task = stats_->running_background_task; | 211 stats_->max_parallel_task = stats_->running_background_task; |
| 211 | 212 |
| 212 base::ThreadTaskRunnerHandle::Get()->PostTask( | 213 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 213 FROM_HERE, | 214 FROM_HERE, |
| 214 base::Bind(&BackgroundTask::CompleteTask, | 215 base::Bind(&BackgroundTask::CompleteTask, |
| 215 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token))); | 216 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token))); |
| 216 } | 217 } |
| 217 | 218 |
| 218 void CompleteTask(scoped_ptr<SyncTaskToken> token) { | 219 void CompleteTask(scoped_ptr<SyncTaskToken> token) { |
| 219 ++(stats_->finished_task); | 220 ++(stats_->finished_task); |
| 220 --(stats_->running_background_task); | 221 --(stats_->running_background_task); |
| 221 SyncTaskManager::NotifyTaskDone(token.Pass(), SYNC_STATUS_OK); | 222 SyncTaskManager::NotifyTaskDone(std::move(token), SYNC_STATUS_OK); |
| 222 } | 223 } |
| 223 | 224 |
| 224 std::string app_id_; | 225 std::string app_id_; |
| 225 base::FilePath path_; | 226 base::FilePath path_; |
| 226 Stats* stats_; | 227 Stats* stats_; |
| 227 | 228 |
| 228 base::WeakPtrFactory<BackgroundTask> weak_ptr_factory_; | 229 base::WeakPtrFactory<BackgroundTask> weak_ptr_factory_; |
| 229 | 230 |
| 230 DISALLOW_COPY_AND_ASSIGN(BackgroundTask); | 231 DISALLOW_COPY_AND_ASSIGN(BackgroundTask); |
| 231 }; | 232 }; |
| 232 | 233 |
| 233 class BlockerUpdateTestHelper : public SyncTask { | 234 class BlockerUpdateTestHelper : public SyncTask { |
| 234 public: | 235 public: |
| 235 typedef std::vector<std::string> Log; | 236 typedef std::vector<std::string> Log; |
| 236 | 237 |
| 237 BlockerUpdateTestHelper(const std::string& name, | 238 BlockerUpdateTestHelper(const std::string& name, |
| 238 const std::string& app_id, | 239 const std::string& app_id, |
| 239 const std::vector<std::string>& paths, | 240 const std::vector<std::string>& paths, |
| 240 Log* log) | 241 Log* log) |
| 241 : name_(name), | 242 : name_(name), |
| 242 app_id_(app_id), | 243 app_id_(app_id), |
| 243 paths_(paths.begin(), paths.end()), | 244 paths_(paths.begin(), paths.end()), |
| 244 log_(log), | 245 log_(log), |
| 245 weak_ptr_factory_(this) { | 246 weak_ptr_factory_(this) { |
| 246 } | 247 } |
| 247 | 248 |
| 248 ~BlockerUpdateTestHelper() override {} | 249 ~BlockerUpdateTestHelper() override {} |
| 249 | 250 |
| 250 void RunPreflight(scoped_ptr<SyncTaskToken> token) override { | 251 void RunPreflight(scoped_ptr<SyncTaskToken> token) override { |
| 251 UpdateBlocker(token.Pass()); | 252 UpdateBlocker(std::move(token)); |
| 252 } | 253 } |
| 253 | 254 |
| 254 private: | 255 private: |
| 255 void UpdateBlocker(scoped_ptr<SyncTaskToken> token) { | 256 void UpdateBlocker(scoped_ptr<SyncTaskToken> token) { |
| 256 if (paths_.empty()) { | 257 if (paths_.empty()) { |
| 257 log_->push_back(name_ + ": finished"); | 258 log_->push_back(name_ + ": finished"); |
| 258 SyncTaskManager::NotifyTaskDone(token.Pass(), SYNC_STATUS_OK); | 259 SyncTaskManager::NotifyTaskDone(std::move(token), SYNC_STATUS_OK); |
| 259 return; | 260 return; |
| 260 } | 261 } |
| 261 | 262 |
| 262 std::string updating_to = paths_.front(); | 263 std::string updating_to = paths_.front(); |
| 263 paths_.pop_front(); | 264 paths_.pop_front(); |
| 264 | 265 |
| 265 log_->push_back(name_ + ": updating to " + updating_to); | 266 log_->push_back(name_ + ": updating to " + updating_to); |
| 266 | 267 |
| 267 scoped_ptr<TaskBlocker> task_blocker(new TaskBlocker); | 268 scoped_ptr<TaskBlocker> task_blocker(new TaskBlocker); |
| 268 task_blocker->app_id = app_id_; | 269 task_blocker->app_id = app_id_; |
| 269 task_blocker->paths.push_back( | 270 task_blocker->paths.push_back( |
| 270 base::FilePath(storage::VirtualPath::GetNormalizedFilePath( | 271 base::FilePath(storage::VirtualPath::GetNormalizedFilePath( |
| 271 base::FilePath::FromUTF8Unsafe(updating_to)))); | 272 base::FilePath::FromUTF8Unsafe(updating_to)))); |
| 272 | 273 |
| 273 SyncTaskManager::UpdateTaskBlocker( | 274 SyncTaskManager::UpdateTaskBlocker( |
| 274 token.Pass(), task_blocker.Pass(), | 275 std::move(token), std::move(task_blocker), |
| 275 base::Bind(&BlockerUpdateTestHelper::UpdateBlockerSoon, | 276 base::Bind(&BlockerUpdateTestHelper::UpdateBlockerSoon, |
| 276 weak_ptr_factory_.GetWeakPtr(), | 277 weak_ptr_factory_.GetWeakPtr(), updating_to)); |
| 277 updating_to)); | |
| 278 } | 278 } |
| 279 | 279 |
| 280 void UpdateBlockerSoon(const std::string& updated_to, | 280 void UpdateBlockerSoon(const std::string& updated_to, |
| 281 scoped_ptr<SyncTaskToken> token) { | 281 scoped_ptr<SyncTaskToken> token) { |
| 282 log_->push_back(name_ + ": updated to " + updated_to); | 282 log_->push_back(name_ + ": updated to " + updated_to); |
| 283 base::ThreadTaskRunnerHandle::Get()->PostTask( | 283 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 284 FROM_HERE, | 284 FROM_HERE, |
| 285 base::Bind(&BlockerUpdateTestHelper::UpdateBlocker, | 285 base::Bind(&BlockerUpdateTestHelper::UpdateBlocker, |
| 286 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token))); | 286 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token))); |
| 287 } | 287 } |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 | 672 |
| 673 EXPECT_EQ("task1: finished", log[i++]); | 673 EXPECT_EQ("task1: finished", log[i++]); |
| 674 | 674 |
| 675 EXPECT_EQ("task2: updating to /hoge/fuga/piyo", log[i++]); | 675 EXPECT_EQ("task2: updating to /hoge/fuga/piyo", log[i++]); |
| 676 EXPECT_EQ("task2: updated to /hoge/fuga/piyo", log[i++]); | 676 EXPECT_EQ("task2: updated to /hoge/fuga/piyo", log[i++]); |
| 677 EXPECT_EQ("task2: finished", log[i++]); | 677 EXPECT_EQ("task2: finished", log[i++]); |
| 678 } | 678 } |
| 679 | 679 |
| 680 } // namespace drive_backend | 680 } // namespace drive_backend |
| 681 } // namespace sync_file_system | 681 } // namespace sync_file_system |
| OLD | NEW |