Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / storage / browser / fileapi / recursive_operation_delegate.cc
blob609df31f8f03610c82137b26cc54e95ab73ccf79
1 // Copyright (c) 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 "storage/browser/fileapi/recursive_operation_delegate.h"
7 #include "base/bind.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "storage/browser/fileapi/file_system_context.h"
11 #include "storage/browser/fileapi/file_system_operation_runner.h"
13 namespace storage {
15 namespace {
16 // Don't start too many inflight operations.
17 const int kMaxInflightOperations = 5;
20 RecursiveOperationDelegate::RecursiveOperationDelegate(
21 FileSystemContext* file_system_context)
22 : file_system_context_(file_system_context),
23 inflight_operations_(0),
24 canceled_(false) {
27 RecursiveOperationDelegate::~RecursiveOperationDelegate() {
30 void RecursiveOperationDelegate::Cancel() {
31 canceled_ = true;
32 OnCancel();
35 void RecursiveOperationDelegate::StartRecursiveOperation(
36 const FileSystemURL& root,
37 const StatusCallback& callback) {
38 DCHECK(pending_directory_stack_.empty());
39 DCHECK(pending_files_.empty());
40 DCHECK_EQ(0, inflight_operations_);
42 callback_ = callback;
43 ++inflight_operations_;
44 ProcessFile(
45 root,
46 base::Bind(&RecursiveOperationDelegate::DidTryProcessFile,
47 AsWeakPtr(), root));
50 FileSystemOperationRunner* RecursiveOperationDelegate::operation_runner() {
51 return file_system_context_->operation_runner();
54 void RecursiveOperationDelegate::OnCancel() {
57 void RecursiveOperationDelegate::DidTryProcessFile(
58 const FileSystemURL& root,
59 base::File::Error error) {
60 DCHECK(pending_directory_stack_.empty());
61 DCHECK(pending_files_.empty());
62 DCHECK_EQ(1, inflight_operations_);
64 --inflight_operations_;
65 if (canceled_ || error != base::File::FILE_ERROR_NOT_A_FILE) {
66 Done(error);
67 return;
70 pending_directory_stack_.push(std::queue<FileSystemURL>());
71 pending_directory_stack_.top().push(root);
72 ProcessNextDirectory();
75 void RecursiveOperationDelegate::ProcessNextDirectory() {
76 DCHECK(pending_files_.empty());
77 DCHECK(!pending_directory_stack_.empty());
78 DCHECK(!pending_directory_stack_.top().empty());
79 DCHECK_EQ(0, inflight_operations_);
81 const FileSystemURL& url = pending_directory_stack_.top().front();
83 ++inflight_operations_;
84 ProcessDirectory(
85 url,
86 base::Bind(
87 &RecursiveOperationDelegate::DidProcessDirectory, AsWeakPtr()));
90 void RecursiveOperationDelegate::DidProcessDirectory(
91 base::File::Error error) {
92 DCHECK(pending_files_.empty());
93 DCHECK(!pending_directory_stack_.empty());
94 DCHECK(!pending_directory_stack_.top().empty());
95 DCHECK_EQ(1, inflight_operations_);
97 --inflight_operations_;
98 if (canceled_ || error != base::File::FILE_OK) {
99 Done(error);
100 return;
103 const FileSystemURL& parent = pending_directory_stack_.top().front();
104 pending_directory_stack_.push(std::queue<FileSystemURL>());
105 operation_runner()->ReadDirectory(
106 parent,
107 base::Bind(&RecursiveOperationDelegate::DidReadDirectory,
108 AsWeakPtr(), parent));
111 void RecursiveOperationDelegate::DidReadDirectory(
112 const FileSystemURL& parent,
113 base::File::Error error,
114 const FileEntryList& entries,
115 bool has_more) {
116 DCHECK(!pending_directory_stack_.empty());
117 DCHECK_EQ(0, inflight_operations_);
119 if (canceled_ || error != base::File::FILE_OK) {
120 Done(error);
121 return;
124 for (size_t i = 0; i < entries.size(); i++) {
125 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL(
126 parent.origin(),
127 parent.mount_type(),
128 parent.virtual_path().Append(entries[i].name));
129 if (entries[i].is_directory)
130 pending_directory_stack_.top().push(url);
131 else
132 pending_files_.push(url);
135 // Wait for next entries.
136 if (has_more)
137 return;
139 ProcessPendingFiles();
142 void RecursiveOperationDelegate::ProcessPendingFiles() {
143 DCHECK(!pending_directory_stack_.empty());
145 if ((pending_files_.empty() || canceled_) && inflight_operations_ == 0) {
146 ProcessSubDirectory();
147 return;
150 // Do not post any new tasks.
151 if (canceled_)
152 return;
154 // Run ProcessFile in parallel (upto kMaxInflightOperations).
155 scoped_refptr<base::SingleThreadTaskRunner> current_task_runner =
156 base::ThreadTaskRunnerHandle::Get();
157 while (!pending_files_.empty() &&
158 inflight_operations_ < kMaxInflightOperations) {
159 ++inflight_operations_;
160 current_task_runner->PostTask(
161 FROM_HERE,
162 base::Bind(&RecursiveOperationDelegate::ProcessFile,
163 AsWeakPtr(), pending_files_.front(),
164 base::Bind(&RecursiveOperationDelegate::DidProcessFile,
165 AsWeakPtr())));
166 pending_files_.pop();
170 void RecursiveOperationDelegate::DidProcessFile(
171 base::File::Error error) {
172 --inflight_operations_;
173 if (error != base::File::FILE_OK) {
174 // If an error occurs, invoke Done immediately (even if there remain
175 // running operations). It is because in the callback, this instance is
176 // deleted.
177 Done(error);
178 return;
181 ProcessPendingFiles();
184 void RecursiveOperationDelegate::ProcessSubDirectory() {
185 DCHECK(pending_files_.empty());
186 DCHECK(!pending_directory_stack_.empty());
187 DCHECK_EQ(0, inflight_operations_);
189 if (canceled_) {
190 Done(base::File::FILE_ERROR_ABORT);
191 return;
194 if (!pending_directory_stack_.top().empty()) {
195 // There remain some sub directories. Process them first.
196 ProcessNextDirectory();
197 return;
200 // All subdirectories are processed.
201 pending_directory_stack_.pop();
202 if (pending_directory_stack_.empty()) {
203 // All files/directories are processed.
204 Done(base::File::FILE_OK);
205 return;
208 DCHECK(!pending_directory_stack_.top().empty());
209 ++inflight_operations_;
210 PostProcessDirectory(
211 pending_directory_stack_.top().front(),
212 base::Bind(&RecursiveOperationDelegate::DidPostProcessDirectory,
213 AsWeakPtr()));
216 void RecursiveOperationDelegate::DidPostProcessDirectory(
217 base::File::Error error) {
218 DCHECK(pending_files_.empty());
219 DCHECK(!pending_directory_stack_.empty());
220 DCHECK(!pending_directory_stack_.top().empty());
221 DCHECK_EQ(1, inflight_operations_);
223 --inflight_operations_;
224 pending_directory_stack_.top().pop();
225 if (canceled_ || error != base::File::FILE_OK) {
226 Done(error);
227 return;
230 ProcessSubDirectory();
233 void RecursiveOperationDelegate::Done(base::File::Error error) {
234 if (canceled_ && error == base::File::FILE_OK) {
235 callback_.Run(base::File::FILE_ERROR_ABORT);
236 } else {
237 callback_.Run(error);
241 } // namespace storage