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"
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"
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),
25 error_behavior_(FileSystemOperation::ERROR_BEHAVIOR_ABORT
),
26 failed_some_operations_(false) {
29 RecursiveOperationDelegate::~RecursiveOperationDelegate() {
32 void RecursiveOperationDelegate::Cancel() {
37 void RecursiveOperationDelegate::StartRecursiveOperation(
38 const FileSystemURL
& root
,
39 ErrorBehavior error_behavior
,
40 const StatusCallback
& callback
) {
41 DCHECK(pending_directory_stack_
.empty());
42 DCHECK(pending_files_
.empty());
43 DCHECK_EQ(0, inflight_operations_
);
45 error_behavior_
= error_behavior
;
51 void RecursiveOperationDelegate::TryProcessFile(const FileSystemURL
& root
) {
52 ++inflight_operations_
;
53 ProcessFile(root
, base::Bind(&RecursiveOperationDelegate::DidTryProcessFile
,
57 FileSystemOperationRunner
* RecursiveOperationDelegate::operation_runner() {
58 return file_system_context_
->operation_runner();
61 void RecursiveOperationDelegate::OnCancel() {
64 void RecursiveOperationDelegate::DidTryProcessFile(
65 const FileSystemURL
& root
,
66 base::File::Error error
) {
67 DCHECK(pending_directory_stack_
.empty());
68 DCHECK(pending_files_
.empty());
69 DCHECK_EQ(1, inflight_operations_
);
71 --inflight_operations_
;
72 if (canceled_
|| error
!= base::File::FILE_ERROR_NOT_A_FILE
) {
77 pending_directory_stack_
.push(std::queue
<FileSystemURL
>());
78 pending_directory_stack_
.top().push(root
);
79 ProcessNextDirectory();
82 void RecursiveOperationDelegate::ProcessNextDirectory() {
83 DCHECK(pending_files_
.empty());
84 DCHECK(!pending_directory_stack_
.empty());
85 DCHECK(!pending_directory_stack_
.top().empty());
86 DCHECK_EQ(0, inflight_operations_
);
88 const FileSystemURL
& url
= pending_directory_stack_
.top().front();
90 ++inflight_operations_
;
94 &RecursiveOperationDelegate::DidProcessDirectory
, AsWeakPtr()));
97 void RecursiveOperationDelegate::DidProcessDirectory(
98 base::File::Error error
) {
99 DCHECK(pending_files_
.empty());
100 DCHECK(!pending_directory_stack_
.empty());
101 DCHECK(!pending_directory_stack_
.top().empty());
102 DCHECK_EQ(1, inflight_operations_
);
104 --inflight_operations_
;
105 if (canceled_
|| error
!= base::File::FILE_OK
) {
110 const FileSystemURL
& parent
= pending_directory_stack_
.top().front();
111 pending_directory_stack_
.push(std::queue
<FileSystemURL
>());
112 operation_runner()->ReadDirectory(
114 base::Bind(&RecursiveOperationDelegate::DidReadDirectory
,
115 AsWeakPtr(), parent
));
118 void RecursiveOperationDelegate::DidReadDirectory(
119 const FileSystemURL
& parent
,
120 base::File::Error error
,
121 const FileEntryList
& entries
,
123 DCHECK(!pending_directory_stack_
.empty());
124 DCHECK_EQ(0, inflight_operations_
);
126 if (canceled_
|| error
!= base::File::FILE_OK
) {
131 for (size_t i
= 0; i
< entries
.size(); i
++) {
132 FileSystemURL url
= file_system_context_
->CreateCrackedFileSystemURL(
135 parent
.virtual_path().Append(entries
[i
].name
));
136 if (entries
[i
].is_directory
)
137 pending_directory_stack_
.top().push(url
);
139 pending_files_
.push(url
);
142 // Wait for next entries.
146 ProcessPendingFiles();
149 void RecursiveOperationDelegate::ProcessPendingFiles() {
150 DCHECK(!pending_directory_stack_
.empty());
152 if ((pending_files_
.empty() || canceled_
) && inflight_operations_
== 0) {
153 ProcessSubDirectory();
157 // Do not post any new tasks.
161 // Run ProcessFile in parallel (upto kMaxInflightOperations).
162 scoped_refptr
<base::SingleThreadTaskRunner
> current_task_runner
=
163 base::ThreadTaskRunnerHandle::Get();
164 while (!pending_files_
.empty() &&
165 inflight_operations_
< kMaxInflightOperations
) {
166 ++inflight_operations_
;
167 current_task_runner
->PostTask(
169 base::Bind(&RecursiveOperationDelegate::ProcessFile
, AsWeakPtr(),
170 pending_files_
.front(),
171 base::Bind(&RecursiveOperationDelegate::DidProcessFile
,
172 AsWeakPtr(), pending_files_
.front())));
173 pending_files_
.pop();
177 void RecursiveOperationDelegate::DidProcessFile(const FileSystemURL
& url
,
178 base::File::Error error
) {
179 --inflight_operations_
;
181 if (error
!= base::File::FILE_OK
) {
182 if (error_behavior_
== FileSystemOperation::ERROR_BEHAVIOR_ABORT
) {
183 // If an error occurs, invoke Done immediately (even if there remain
184 // running operations). It is because in the callback, this instance is
190 failed_some_operations_
= true;
193 ProcessPendingFiles();
196 void RecursiveOperationDelegate::ProcessSubDirectory() {
197 DCHECK(pending_files_
.empty());
198 DCHECK(!pending_directory_stack_
.empty());
199 DCHECK_EQ(0, inflight_operations_
);
202 Done(base::File::FILE_ERROR_ABORT
);
206 if (!pending_directory_stack_
.top().empty()) {
207 // There remain some sub directories. Process them first.
208 ProcessNextDirectory();
212 // All subdirectories are processed.
213 pending_directory_stack_
.pop();
214 if (pending_directory_stack_
.empty()) {
215 // All files/directories are processed.
216 Done(base::File::FILE_OK
);
220 DCHECK(!pending_directory_stack_
.top().empty());
221 ++inflight_operations_
;
222 PostProcessDirectory(
223 pending_directory_stack_
.top().front(),
224 base::Bind(&RecursiveOperationDelegate::DidPostProcessDirectory
,
228 void RecursiveOperationDelegate::DidPostProcessDirectory(
229 base::File::Error error
) {
230 DCHECK(pending_files_
.empty());
231 DCHECK(!pending_directory_stack_
.empty());
232 DCHECK(!pending_directory_stack_
.top().empty());
233 DCHECK_EQ(1, inflight_operations_
);
235 --inflight_operations_
;
236 pending_directory_stack_
.top().pop();
237 if (canceled_
|| error
!= base::File::FILE_OK
) {
242 ProcessSubDirectory();
245 void RecursiveOperationDelegate::Done(base::File::Error error
) {
246 if (canceled_
&& error
== base::File::FILE_OK
) {
247 callback_
.Run(base::File::FILE_ERROR_ABORT
);
249 if (error_behavior_
== FileSystemOperation::ERROR_BEHAVIOR_SKIP
&&
250 failed_some_operations_
)
251 callback_
.Run(base::File::FILE_ERROR_FAILED
);
253 callback_
.Run(error
);
257 } // namespace storage