Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / local / syncable_file_operation_runner.cc
blob11053a2f7d0e9e90701459af74f492f18096ed65
1 // Copyright 2013 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/local/syncable_file_operation_runner.h"
7 #include <algorithm>
8 #include <functional>
10 #include "base/callback.h"
11 #include "base/stl_util.h"
13 using storage::FileSystemURL;
15 namespace sync_file_system {
17 // SyncableFileOperationRunner::Task -------------------------------------------
19 // static
20 void SyncableFileOperationRunner::Task::CancelAndDelete(
21 SyncableFileOperationRunner::Task* task) {
22 task->Cancel();
23 delete task;
26 bool SyncableFileOperationRunner::Task::IsRunnable(
27 LocalFileSyncStatus* status) const {
28 for (size_t i = 0; i < target_paths().size(); ++i) {
29 if (!status->IsWritable(target_paths()[i]))
30 return false;
32 return true;
35 void SyncableFileOperationRunner::Task::Start(LocalFileSyncStatus* status) {
36 for (size_t i = 0; i < target_paths().size(); ++i) {
37 DCHECK(status->IsWritable(target_paths()[i]));
38 status->StartWriting(target_paths()[i]);
40 Run();
43 // SyncableFileOperationRunner -------------------------------------------------
45 SyncableFileOperationRunner::SyncableFileOperationRunner(
46 int64 max_inflight_tasks,
47 LocalFileSyncStatus* sync_status)
48 : sync_status_(sync_status),
49 max_inflight_tasks_(max_inflight_tasks),
50 num_inflight_tasks_(0) {
51 DCHECK(CalledOnValidThread());
52 sync_status_->AddObserver(this);
55 SyncableFileOperationRunner::~SyncableFileOperationRunner() {
56 DCHECK(CalledOnValidThread());
57 for_each(pending_tasks_.begin(), pending_tasks_.end(),
58 SyncableFileOperationRunner::Task::CancelAndDelete);
61 void SyncableFileOperationRunner::OnSyncEnabled(const FileSystemURL& url) {
64 void SyncableFileOperationRunner::OnWriteEnabled(const FileSystemURL& url) {
65 DCHECK(CalledOnValidThread());
66 RunNextRunnableTask();
69 void SyncableFileOperationRunner::PostOperationTask(scoped_ptr<Task> task) {
70 DCHECK(CalledOnValidThread());
71 pending_tasks_.push_back(task.release());
72 RunNextRunnableTask();
75 void SyncableFileOperationRunner::RunNextRunnableTask() {
76 DCHECK(CalledOnValidThread());
77 for (std::list<Task*>::iterator iter = pending_tasks_.begin();
78 iter != pending_tasks_.end() && ShouldStartMoreTasks();) {
79 if ((*iter)->IsRunnable(sync_status())) {
80 ++num_inflight_tasks_;
81 DCHECK_GE(num_inflight_tasks_, 1);
82 scoped_ptr<Task> task(*iter);
83 pending_tasks_.erase(iter++);
84 task->Start(sync_status());
85 continue;
87 ++iter;
91 void SyncableFileOperationRunner::OnOperationCompleted(
92 const std::vector<FileSystemURL>& target_paths) {
93 --num_inflight_tasks_;
94 DCHECK_GE(num_inflight_tasks_, 0);
95 for (size_t i = 0; i < target_paths.size(); ++i) {
96 DCHECK(sync_status()->IsWriting(target_paths[i]));
97 sync_status()->EndWriting(target_paths[i]);
99 RunNextRunnableTask();
102 bool SyncableFileOperationRunner::ShouldStartMoreTasks() const {
103 return num_inflight_tasks_ < max_inflight_tasks_;
106 } // namespace sync_file_system