| 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), |
| 26 failed_some_operations_(false) { |
| 25 } | 27 } |
| 26 | 28 |
| 27 RecursiveOperationDelegate::~RecursiveOperationDelegate() { | 29 RecursiveOperationDelegate::~RecursiveOperationDelegate() { |
| 28 } | 30 } |
| 29 | 31 |
| 30 void RecursiveOperationDelegate::Cancel() { | 32 void RecursiveOperationDelegate::Cancel() { |
| 31 canceled_ = true; | 33 canceled_ = true; |
| 32 OnCancel(); | 34 OnCancel(); |
| 33 } | 35 } |
| 34 | 36 |
| 35 void RecursiveOperationDelegate::StartRecursiveOperation( | 37 void RecursiveOperationDelegate::StartRecursiveOperation( |
| 36 const FileSystemURL& root, | 38 const FileSystemURL& root, |
| 37 const StatusCallback& callback) { | 39 const StatusCallback& callback) { |
| 38 DCHECK(pending_directory_stack_.empty()); | 40 DCHECK(pending_directory_stack_.empty()); |
| 39 DCHECK(pending_files_.empty()); | 41 DCHECK(pending_files_.empty()); |
| 40 DCHECK_EQ(0, inflight_operations_); | 42 DCHECK_EQ(0, inflight_operations_); |
| 41 | 43 |
| 42 callback_ = callback; | 44 callback_ = callback; |
| 45 |
| 46 TryProcessFile(root); |
| 47 } |
| 48 |
| 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) { |
| 43 ++inflight_operations_; | 65 ++inflight_operations_; |
| 44 ProcessFile( | 66 ProcessFile(root, base::Bind(&RecursiveOperationDelegate::DidTryProcessFile, |
| 45 root, | 67 AsWeakPtr(), root)); |
| 46 base::Bind(&RecursiveOperationDelegate::DidTryProcessFile, | |
| 47 AsWeakPtr(), root)); | |
| 48 } | 68 } |
| 49 | 69 |
| 50 FileSystemOperationRunner* RecursiveOperationDelegate::operation_runner() { | 70 FileSystemOperationRunner* RecursiveOperationDelegate::operation_runner() { |
| 51 return file_system_context_->operation_runner(); | 71 return file_system_context_->operation_runner(); |
| 52 } | 72 } |
| 53 | 73 |
| 54 void RecursiveOperationDelegate::OnCancel() { | 74 void RecursiveOperationDelegate::OnCancel() { |
| 55 } | 75 } |
| 56 | 76 |
| 57 void RecursiveOperationDelegate::DidTryProcessFile( | 77 void RecursiveOperationDelegate::DidTryProcessFile( |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 return; | 172 return; |
| 153 | 173 |
| 154 // Run ProcessFile in parallel (upto kMaxInflightOperations). | 174 // Run ProcessFile in parallel (upto kMaxInflightOperations). |
| 155 scoped_refptr<base::SingleThreadTaskRunner> current_task_runner = | 175 scoped_refptr<base::SingleThreadTaskRunner> current_task_runner = |
| 156 base::ThreadTaskRunnerHandle::Get(); | 176 base::ThreadTaskRunnerHandle::Get(); |
| 157 while (!pending_files_.empty() && | 177 while (!pending_files_.empty() && |
| 158 inflight_operations_ < kMaxInflightOperations) { | 178 inflight_operations_ < kMaxInflightOperations) { |
| 159 ++inflight_operations_; | 179 ++inflight_operations_; |
| 160 current_task_runner->PostTask( | 180 current_task_runner->PostTask( |
| 161 FROM_HERE, | 181 FROM_HERE, |
| 162 base::Bind(&RecursiveOperationDelegate::ProcessFile, | 182 base::Bind(&RecursiveOperationDelegate::ProcessFile, AsWeakPtr(), |
| 163 AsWeakPtr(), pending_files_.front(), | 183 pending_files_.front(), |
| 164 base::Bind(&RecursiveOperationDelegate::DidProcessFile, | 184 base::Bind(&RecursiveOperationDelegate::DidProcessFile, |
| 165 AsWeakPtr()))); | 185 AsWeakPtr(), pending_files_.front()))); |
| 166 pending_files_.pop(); | 186 pending_files_.pop(); |
| 167 } | 187 } |
| 168 } | 188 } |
| 169 | 189 |
| 170 void RecursiveOperationDelegate::DidProcessFile( | 190 void RecursiveOperationDelegate::DidProcessFile(const FileSystemURL& url, |
| 171 base::File::Error error) { | 191 base::File::Error error) { |
| 172 --inflight_operations_; | 192 --inflight_operations_; |
| 193 |
| 173 if (error != base::File::FILE_OK) { | 194 if (error != base::File::FILE_OK) { |
| 174 // If an error occurs, invoke Done immediately (even if there remain | 195 if (!ignore_error_) { |
| 175 // running operations). It is because in the callback, this instance is | 196 // If an error occurs, invoke Done immediately (even if there remain |
| 176 // deleted. | 197 // running operations). It is because in the callback, this instance is |
| 177 Done(error); | 198 // deleted. |
| 178 return; | 199 Done(error); |
| 200 return; |
| 201 } else { |
| 202 failed_some_operations_ = true; |
| 203 |
| 204 if (!error_callback_.is_null()) |
| 205 error_callback_.Run(url, error); |
| 206 } |
| 179 } | 207 } |
| 180 | 208 |
| 181 ProcessPendingFiles(); | 209 ProcessPendingFiles(); |
| 182 } | 210 } |
| 183 | 211 |
| 184 void RecursiveOperationDelegate::ProcessSubDirectory() { | 212 void RecursiveOperationDelegate::ProcessSubDirectory() { |
| 185 DCHECK(pending_files_.empty()); | 213 DCHECK(pending_files_.empty()); |
| 186 DCHECK(!pending_directory_stack_.empty()); | 214 DCHECK(!pending_directory_stack_.empty()); |
| 187 DCHECK_EQ(0, inflight_operations_); | 215 DCHECK_EQ(0, inflight_operations_); |
| 188 | 216 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 return; | 255 return; |
| 228 } | 256 } |
| 229 | 257 |
| 230 ProcessSubDirectory(); | 258 ProcessSubDirectory(); |
| 231 } | 259 } |
| 232 | 260 |
| 233 void RecursiveOperationDelegate::Done(base::File::Error error) { | 261 void RecursiveOperationDelegate::Done(base::File::Error error) { |
| 234 if (canceled_ && error == base::File::FILE_OK) { | 262 if (canceled_ && error == base::File::FILE_OK) { |
| 235 callback_.Run(base::File::FILE_ERROR_ABORT); | 263 callback_.Run(base::File::FILE_ERROR_ABORT); |
| 236 } else { | 264 } else { |
| 237 callback_.Run(error); | 265 if (ignore_error_ && failed_some_operations_) |
| 266 callback_.Run(base::File::FILE_ERROR_FAILED); |
| 267 else |
| 268 callback_.Run(error); |
| 238 } | 269 } |
| 239 } | 270 } |
| 240 | 271 |
| 241 } // namespace storage | 272 } // namespace storage |
| OLD | NEW |