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 "storage/browser/fileapi/file_system_context.h"
9 #include "storage/browser/fileapi/file_system_operation_runner.h"
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),
25 RecursiveOperationDelegate::~RecursiveOperationDelegate() {
28 void RecursiveOperationDelegate::Cancel() {
33 void RecursiveOperationDelegate::StartRecursiveOperation(
34 const FileSystemURL
& root
,
35 const StatusCallback
& callback
) {
36 DCHECK(pending_directory_stack_
.empty());
37 DCHECK(pending_files_
.empty());
38 DCHECK_EQ(0, inflight_operations_
);
41 ++inflight_operations_
;
44 base::Bind(&RecursiveOperationDelegate::DidTryProcessFile
,
48 FileSystemOperationRunner
* RecursiveOperationDelegate::operation_runner() {
49 return file_system_context_
->operation_runner();
52 void RecursiveOperationDelegate::OnCancel() {
55 void RecursiveOperationDelegate::DidTryProcessFile(
56 const FileSystemURL
& root
,
57 base::File::Error error
) {
58 DCHECK(pending_directory_stack_
.empty());
59 DCHECK(pending_files_
.empty());
60 DCHECK_EQ(1, inflight_operations_
);
62 --inflight_operations_
;
63 if (canceled_
|| error
!= base::File::FILE_ERROR_NOT_A_FILE
) {
68 pending_directory_stack_
.push(std::queue
<FileSystemURL
>());
69 pending_directory_stack_
.top().push(root
);
70 ProcessNextDirectory();
73 void RecursiveOperationDelegate::ProcessNextDirectory() {
74 DCHECK(pending_files_
.empty());
75 DCHECK(!pending_directory_stack_
.empty());
76 DCHECK(!pending_directory_stack_
.top().empty());
77 DCHECK_EQ(0, inflight_operations_
);
79 const FileSystemURL
& url
= pending_directory_stack_
.top().front();
81 ++inflight_operations_
;
85 &RecursiveOperationDelegate::DidProcessDirectory
, AsWeakPtr()));
88 void RecursiveOperationDelegate::DidProcessDirectory(
89 base::File::Error error
) {
90 DCHECK(pending_files_
.empty());
91 DCHECK(!pending_directory_stack_
.empty());
92 DCHECK(!pending_directory_stack_
.top().empty());
93 DCHECK_EQ(1, inflight_operations_
);
95 --inflight_operations_
;
96 if (canceled_
|| error
!= base::File::FILE_OK
) {
101 const FileSystemURL
& parent
= pending_directory_stack_
.top().front();
102 pending_directory_stack_
.push(std::queue
<FileSystemURL
>());
103 operation_runner()->ReadDirectory(
105 base::Bind(&RecursiveOperationDelegate::DidReadDirectory
,
106 AsWeakPtr(), parent
));
109 void RecursiveOperationDelegate::DidReadDirectory(
110 const FileSystemURL
& parent
,
111 base::File::Error error
,
112 const FileEntryList
& entries
,
114 DCHECK(!pending_directory_stack_
.empty());
115 DCHECK_EQ(0, inflight_operations_
);
117 if (canceled_
|| error
!= base::File::FILE_OK
) {
122 for (size_t i
= 0; i
< entries
.size(); i
++) {
123 FileSystemURL url
= file_system_context_
->CreateCrackedFileSystemURL(
126 parent
.virtual_path().Append(entries
[i
].name
));
127 if (entries
[i
].is_directory
)
128 pending_directory_stack_
.top().push(url
);
130 pending_files_
.push(url
);
133 // Wait for next entries.
137 ProcessPendingFiles();
140 void RecursiveOperationDelegate::ProcessPendingFiles() {
141 DCHECK(!pending_directory_stack_
.empty());
143 if ((pending_files_
.empty() || canceled_
) && inflight_operations_
== 0) {
144 ProcessSubDirectory();
148 // Do not post any new tasks.
152 // Run ProcessFile in parallel (upto kMaxInflightOperations).
153 scoped_refptr
<base::MessageLoopProxy
> current_message_loop
=
154 base::MessageLoopProxy::current();
155 while (!pending_files_
.empty() &&
156 inflight_operations_
< kMaxInflightOperations
) {
157 ++inflight_operations_
;
158 current_message_loop
->PostTask(
160 base::Bind(&RecursiveOperationDelegate::ProcessFile
,
161 AsWeakPtr(), pending_files_
.front(),
162 base::Bind(&RecursiveOperationDelegate::DidProcessFile
,
164 pending_files_
.pop();
168 void RecursiveOperationDelegate::DidProcessFile(
169 base::File::Error error
) {
170 --inflight_operations_
;
171 if (error
!= base::File::FILE_OK
) {
172 // If an error occurs, invoke Done immediately (even if there remain
173 // running operations). It is because in the callback, this instance is
179 ProcessPendingFiles();
182 void RecursiveOperationDelegate::ProcessSubDirectory() {
183 DCHECK(pending_files_
.empty());
184 DCHECK(!pending_directory_stack_
.empty());
185 DCHECK_EQ(0, inflight_operations_
);
188 Done(base::File::FILE_ERROR_ABORT
);
192 if (!pending_directory_stack_
.top().empty()) {
193 // There remain some sub directories. Process them first.
194 ProcessNextDirectory();
198 // All subdirectories are processed.
199 pending_directory_stack_
.pop();
200 if (pending_directory_stack_
.empty()) {
201 // All files/directories are processed.
202 Done(base::File::FILE_OK
);
206 DCHECK(!pending_directory_stack_
.top().empty());
207 ++inflight_operations_
;
208 PostProcessDirectory(
209 pending_directory_stack_
.top().front(),
210 base::Bind(&RecursiveOperationDelegate::DidPostProcessDirectory
,
214 void RecursiveOperationDelegate::DidPostProcessDirectory(
215 base::File::Error error
) {
216 DCHECK(pending_files_
.empty());
217 DCHECK(!pending_directory_stack_
.empty());
218 DCHECK(!pending_directory_stack_
.top().empty());
219 DCHECK_EQ(1, inflight_operations_
);
221 --inflight_operations_
;
222 pending_directory_stack_
.top().pop();
223 if (canceled_
|| error
!= base::File::FILE_OK
) {
228 ProcessSubDirectory();
231 void RecursiveOperationDelegate::Done(base::File::Error error
) {
232 if (canceled_
&& error
== base::File::FILE_OK
) {
233 callback_
.Run(base::File::FILE_ERROR_ABORT
);
235 callback_
.Run(error
);
239 } // namespace storage