OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "storage/browser/fileapi/recursive_operation_delegate.h" | 5 #include "storage/browser/fileapi/recursive_operation_delegate.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
9 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
10 #include "storage/browser/fileapi/file_system_context.h" | 10 #include "storage/browser/fileapi/file_system_context.h" |
11 #include "storage/browser/fileapi/file_system_operation_runner.h" | 11 #include "storage/browser/fileapi/file_system_operation_runner.h" |
12 | 12 |
13 namespace storage { | 13 namespace storage { |
14 | 14 |
15 namespace { | 15 namespace { |
16 // Don't start too many inflight operations. | 16 // Don't start too many inflight operations. |
17 const int kMaxInflightOperations = 5; | 17 const int kMaxInflightOperations = 5; |
18 } | 18 } |
19 | 19 |
20 RecursiveOperationDelegate::RecursiveOperationDelegate( | 20 RecursiveOperationDelegate::RecursiveOperationDelegate( |
21 FileSystemContext* file_system_context) | 21 FileSystemContext* file_system_context) |
22 : file_system_context_(file_system_context), | 22 : file_system_context_(file_system_context), |
23 inflight_operations_(0), | 23 inflight_operations_(0), |
24 canceled_(false), | 24 canceled_(false), |
25 ignore_error_(false), | 25 error_behavior_(FileSystemOperation::ERROR_BEHAVIOR_ABORT), |
26 failed_some_operations_(false) { | 26 failed_some_operations_(false) { |
27 } | 27 } |
28 | 28 |
29 RecursiveOperationDelegate::~RecursiveOperationDelegate() { | 29 RecursiveOperationDelegate::~RecursiveOperationDelegate() { |
30 } | 30 } |
31 | 31 |
32 void RecursiveOperationDelegate::Cancel() { | 32 void RecursiveOperationDelegate::Cancel() { |
33 canceled_ = true; | 33 canceled_ = true; |
34 OnCancel(); | 34 OnCancel(); |
35 } | 35 } |
36 | 36 |
37 void RecursiveOperationDelegate::StartRecursiveOperation( | 37 void RecursiveOperationDelegate::StartRecursiveOperation( |
38 const FileSystemURL& root, | 38 const FileSystemURL& root, |
| 39 ErrorBehavior error_behavior, |
39 const StatusCallback& callback) { | 40 const StatusCallback& callback) { |
40 DCHECK(pending_directory_stack_.empty()); | 41 DCHECK(pending_directory_stack_.empty()); |
41 DCHECK(pending_files_.empty()); | 42 DCHECK(pending_files_.empty()); |
42 DCHECK_EQ(0, inflight_operations_); | 43 DCHECK_EQ(0, inflight_operations_); |
43 | 44 |
| 45 error_behavior_ = error_behavior; |
44 callback_ = callback; | 46 callback_ = callback; |
45 | 47 |
46 TryProcessFile(root); | 48 TryProcessFile(root); |
47 } | 49 } |
48 | 50 |
49 void RecursiveOperationDelegate::StartRecursiveOperationWithIgnoringError( | |
50 const FileSystemURL& root, | |
51 const ErrorCallback& error_callback, | |
52 const StatusCallback& status_callback) { | |
53 DCHECK(pending_directory_stack_.empty()); | |
54 DCHECK(pending_files_.empty()); | |
55 DCHECK_EQ(0, inflight_operations_); | |
56 | |
57 error_callback_ = error_callback; | |
58 callback_ = status_callback; | |
59 ignore_error_ = true; | |
60 | |
61 TryProcessFile(root); | |
62 } | |
63 | |
64 void RecursiveOperationDelegate::TryProcessFile(const FileSystemURL& root) { | 51 void RecursiveOperationDelegate::TryProcessFile(const FileSystemURL& root) { |
65 ++inflight_operations_; | 52 ++inflight_operations_; |
66 ProcessFile(root, base::Bind(&RecursiveOperationDelegate::DidTryProcessFile, | 53 ProcessFile(root, base::Bind(&RecursiveOperationDelegate::DidTryProcessFile, |
67 AsWeakPtr(), root)); | 54 AsWeakPtr(), root)); |
68 } | 55 } |
69 | 56 |
70 FileSystemOperationRunner* RecursiveOperationDelegate::operation_runner() { | 57 FileSystemOperationRunner* RecursiveOperationDelegate::operation_runner() { |
71 return file_system_context_->operation_runner(); | 58 return file_system_context_->operation_runner(); |
72 } | 59 } |
73 | 60 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 AsWeakPtr(), pending_files_.front()))); | 172 AsWeakPtr(), pending_files_.front()))); |
186 pending_files_.pop(); | 173 pending_files_.pop(); |
187 } | 174 } |
188 } | 175 } |
189 | 176 |
190 void RecursiveOperationDelegate::DidProcessFile(const FileSystemURL& url, | 177 void RecursiveOperationDelegate::DidProcessFile(const FileSystemURL& url, |
191 base::File::Error error) { | 178 base::File::Error error) { |
192 --inflight_operations_; | 179 --inflight_operations_; |
193 | 180 |
194 if (error != base::File::FILE_OK) { | 181 if (error != base::File::FILE_OK) { |
195 if (!ignore_error_) { | 182 if (error_behavior_ == FileSystemOperation::ERROR_BEHAVIOR_ABORT) { |
196 // If an error occurs, invoke Done immediately (even if there remain | 183 // If an error occurs, invoke Done immediately (even if there remain |
197 // running operations). It is because in the callback, this instance is | 184 // running operations). It is because in the callback, this instance is |
198 // deleted. | 185 // deleted. |
199 Done(error); | 186 Done(error); |
200 return; | 187 return; |
201 } else { | 188 } |
202 failed_some_operations_ = true; | |
203 | 189 |
204 if (!error_callback_.is_null()) | 190 failed_some_operations_ = true; |
205 error_callback_.Run(url, error); | |
206 } | |
207 } | 191 } |
208 | 192 |
209 ProcessPendingFiles(); | 193 ProcessPendingFiles(); |
210 } | 194 } |
211 | 195 |
212 void RecursiveOperationDelegate::ProcessSubDirectory() { | 196 void RecursiveOperationDelegate::ProcessSubDirectory() { |
213 DCHECK(pending_files_.empty()); | 197 DCHECK(pending_files_.empty()); |
214 DCHECK(!pending_directory_stack_.empty()); | 198 DCHECK(!pending_directory_stack_.empty()); |
215 DCHECK_EQ(0, inflight_operations_); | 199 DCHECK_EQ(0, inflight_operations_); |
216 | 200 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 return; | 239 return; |
256 } | 240 } |
257 | 241 |
258 ProcessSubDirectory(); | 242 ProcessSubDirectory(); |
259 } | 243 } |
260 | 244 |
261 void RecursiveOperationDelegate::Done(base::File::Error error) { | 245 void RecursiveOperationDelegate::Done(base::File::Error error) { |
262 if (canceled_ && error == base::File::FILE_OK) { | 246 if (canceled_ && error == base::File::FILE_OK) { |
263 callback_.Run(base::File::FILE_ERROR_ABORT); | 247 callback_.Run(base::File::FILE_ERROR_ABORT); |
264 } else { | 248 } else { |
265 if (ignore_error_ && failed_some_operations_) | 249 if (error_behavior_ == FileSystemOperation::ERROR_BEHAVIOR_SKIP && |
| 250 failed_some_operations_) |
266 callback_.Run(base::File::FILE_ERROR_FAILED); | 251 callback_.Run(base::File::FILE_ERROR_FAILED); |
267 else | 252 else |
268 callback_.Run(error); | 253 callback_.Run(error); |
269 } | 254 } |
270 } | 255 } |
271 | 256 |
272 } // namespace storage | 257 } // namespace storage |
OLD | NEW |