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"
8 #include "webkit/browser/fileapi/file_system_context.h"
9 #include "webkit/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() {
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_
);
40 ++inflight_operations_
;
43 base::Bind(&RecursiveOperationDelegate::DidTryProcessFile
,
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.
63 (error
!= base::PLATFORM_FILE_ERROR_NOT_A_FILE
&&
64 error
!= base::PLATFORM_FILE_ERROR_SECURITY
)) {
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_
;
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
) {
102 const FileSystemURL
& parent
= pending_directory_stack_
.top().front();
103 pending_directory_stack_
.push(std::queue
<FileSystemURL
>());
104 operation_runner()->ReadDirectory(
106 base::Bind(&RecursiveOperationDelegate::DidReadDirectory
,
107 AsWeakPtr(), parent
));
110 void RecursiveOperationDelegate::DidReadDirectory(
111 const FileSystemURL
& parent
,
112 base::PlatformFileError error
,
113 const FileEntryList
& entries
,
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
) {
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::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(
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::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
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::PLATFORM_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::PLATFORM_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::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
) {
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
);
237 callback_
.Run(error
);
241 } // namespace fileapi