Refactor RecursiveOperationDelegate to support PostProcessDirectory.
[chromium-blink-merge.git] / webkit / browser / fileapi / recursive_operation_delegate.cc
blobe2bcdc4c6f3ac717cb062052e4d05057ab23647f
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 "webkit/browser/fileapi/recursive_operation_delegate.h"
7 #include "base/bind.h"
8 #include "webkit/browser/fileapi/file_system_context.h"
9 #include "webkit/browser/fileapi/file_system_operation_runner.h"
11 namespace fileapi {
13 namespace {
14 // Don't start too many inflight operations.
15 const int kMaxInflightOperations = 5;
18 RecursiveOperationDelegate::RecursiveOperationDelegate(
19 FileSystemContext* file_system_context)
20 : file_system_context_(file_system_context),
21 inflight_operations_(0),
22 canceled_(false) {
25 RecursiveOperationDelegate::~RecursiveOperationDelegate() {
28 void RecursiveOperationDelegate::Cancel() {
29 canceled_ = true;
32 void RecursiveOperationDelegate::StartRecursiveOperation(
33 const FileSystemURL& root,
34 const StatusCallback& callback) {
35 DCHECK(pending_directory_stack_.empty());
36 DCHECK(pending_files_.empty());
37 DCHECK_EQ(0, inflight_operations_);
39 callback_ = callback;
40 ++inflight_operations_;
41 ProcessFile(
42 root,
43 base::Bind(&RecursiveOperationDelegate::DidTryProcessFile,
44 AsWeakPtr(), root));
47 FileSystemOperationRunner* RecursiveOperationDelegate::operation_runner() {
48 return file_system_context_->operation_runner();
51 void RecursiveOperationDelegate::DidTryProcessFile(
52 const FileSystemURL& root,
53 base::PlatformFileError error) {
54 DCHECK(pending_directory_stack_.empty());
55 DCHECK(pending_files_.empty());
56 DCHECK_EQ(1, inflight_operations_);
58 --inflight_operations_;
59 // If the operation for a file is not permitted, the operation may fail
60 // with SECURITY, even if the path points a directory and the operation is
61 // permitted for a directory.
62 if (canceled_ ||
63 (error != base::PLATFORM_FILE_ERROR_NOT_A_FILE &&
64 error != base::PLATFORM_FILE_ERROR_SECURITY)) {
65 Done(error);
66 return;
69 pending_directory_stack_.push(std::queue<FileSystemURL>());
70 pending_directory_stack_.top().push(root);
71 ProcessNextDirectory();
74 void RecursiveOperationDelegate::ProcessNextDirectory() {
75 DCHECK(pending_files_.empty());
76 DCHECK(!pending_directory_stack_.empty());
77 DCHECK(!pending_directory_stack_.top().empty());
78 DCHECK_EQ(0, inflight_operations_);
80 const FileSystemURL& url = pending_directory_stack_.top().front();
82 ++inflight_operations_;
83 ProcessDirectory(
84 url,
85 base::Bind(
86 &RecursiveOperationDelegate::DidProcessDirectory, AsWeakPtr()));
89 void RecursiveOperationDelegate::DidProcessDirectory(
90 base::PlatformFileError error) {
91 DCHECK(pending_files_.empty());
92 DCHECK(!pending_directory_stack_.empty());
93 DCHECK(!pending_directory_stack_.top().empty());
94 DCHECK_EQ(1, inflight_operations_);
96 --inflight_operations_;
97 if (canceled_ || error != base::PLATFORM_FILE_OK) {
98 Done(error);
99 return;
102 const FileSystemURL& parent = pending_directory_stack_.top().front();
103 pending_directory_stack_.push(std::queue<FileSystemURL>());
104 operation_runner()->ReadDirectory(
105 parent,
106 base::Bind(&RecursiveOperationDelegate::DidReadDirectory,
107 AsWeakPtr(), parent));
110 void RecursiveOperationDelegate::DidReadDirectory(
111 const FileSystemURL& parent,
112 base::PlatformFileError error,
113 const FileEntryList& entries,
114 bool has_more) {
115 DCHECK(pending_files_.empty());
116 DCHECK(!pending_directory_stack_.empty());
117 DCHECK_EQ(0, inflight_operations_);
119 if (canceled_ || error != base::PLATFORM_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::MessageLoopProxy> current_message_loop =
156 base::MessageLoopProxy::current();
157 while (!pending_files_.empty() &&
158 inflight_operations_ < kMaxInflightOperations) {
159 ++inflight_operations_;
160 current_message_loop->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::PlatformFileError error) {
172 --inflight_operations_;
173 if (error != base::PLATFORM_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::PLATFORM_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::PLATFORM_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::PlatformFileError 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::PLATFORM_FILE_OK) {
226 Done(error);
227 return;
230 ProcessSubDirectory();
233 void RecursiveOperationDelegate::Done(base::PlatformFileError error) {
234 if (canceled_ && error == base::PLATFORM_FILE_OK) {
235 callback_.Run(base::PLATFORM_FILE_ERROR_ABORT);
236 } else {
237 callback_.Run(error);
241 } // namespace fileapi