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),
27 RecursiveOperationDelegate::~RecursiveOperationDelegate() {
30 void RecursiveOperationDelegate::Cancel() {
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_
);
43 ++inflight_operations_
;
46 base::Bind(&RecursiveOperationDelegate::DidTryProcessFile
,
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
) {
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_
;
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
) {
103 const FileSystemURL
& parent
= pending_directory_stack_
.top().front();
104 pending_directory_stack_
.push(std::queue
<FileSystemURL
>());
105 operation_runner()->ReadDirectory(
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
,
116 DCHECK(!pending_directory_stack_
.empty());
117 DCHECK_EQ(0, inflight_operations_
);
119 if (canceled_
|| error
!= base::File::FILE_OK
) {
124 for (size_t i
= 0; i
< entries
.size(); i
++) {
125 FileSystemURL url
= file_system_context_
->CreateCrackedFileSystemURL(
128 parent
.virtual_path().Append(entries
[i
].name
));
129 if (entries
[i
].is_directory
)
130 pending_directory_stack_
.top().push(url
);
132 pending_files_
.push(url
);
135 // Wait for next entries.
139 ProcessPendingFiles();
142 void RecursiveOperationDelegate::ProcessPendingFiles() {
143 DCHECK(!pending_directory_stack_
.empty());
145 if ((pending_files_
.empty() || canceled_
) && inflight_operations_
== 0) {
146 ProcessSubDirectory();
150 // Do not post any new tasks.
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(
162 base::Bind(&RecursiveOperationDelegate::ProcessFile
,
163 AsWeakPtr(), pending_files_
.front(),
164 base::Bind(&RecursiveOperationDelegate::DidProcessFile
,
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
181 ProcessPendingFiles();
184 void RecursiveOperationDelegate::ProcessSubDirectory() {
185 DCHECK(pending_files_
.empty());
186 DCHECK(!pending_directory_stack_
.empty());
187 DCHECK_EQ(0, inflight_operations_
);
190 Done(base::File::FILE_ERROR_ABORT
);
194 if (!pending_directory_stack_
.top().empty()) {
195 // There remain some sub directories. Process them first.
196 ProcessNextDirectory();
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
);
208 DCHECK(!pending_directory_stack_
.top().empty());
209 ++inflight_operations_
;
210 PostProcessDirectory(
211 pending_directory_stack_
.top().front(),
212 base::Bind(&RecursiveOperationDelegate::DidPostProcessDirectory
,
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
) {
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
);
237 callback_
.Run(error
);
241 } // namespace storage