| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "webkit/fileapi/recursive_operation_delegate.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "webkit/fileapi/file_system_context.h" |
| 9 #include "webkit/fileapi/file_system_operation_context.h" |
| 10 #include "webkit/fileapi/local_file_system_operation.h" |
| 11 |
| 12 namespace fileapi { |
| 13 |
| 14 RecursiveOperationDelegate::RecursiveOperationDelegate( |
| 15 LocalFileSystemOperation* original_operation) |
| 16 : original_operation_(original_operation), |
| 17 inflight_operations_(0) { |
| 18 } |
| 19 |
| 20 RecursiveOperationDelegate::~RecursiveOperationDelegate() {} |
| 21 |
| 22 void RecursiveOperationDelegate::StartRecursiveOperation( |
| 23 const FileSystemURL& root, |
| 24 const StatusCallback& callback) { |
| 25 callback_ = callback; |
| 26 pending_directories_.push(root); |
| 27 ProcessNextDirectory(base::PLATFORM_FILE_OK); |
| 28 } |
| 29 |
| 30 LocalFileSystemOperation* RecursiveOperationDelegate::NewOperation( |
| 31 const FileSystemURL& url, |
| 32 base::PlatformFileError* error_out) { |
| 33 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 34 FileSystemOperation* operation = original_operation_->file_system_context()-> |
| 35 CreateFileSystemOperation(url, &error); |
| 36 if (error != base::PLATFORM_FILE_OK) { |
| 37 if (error_out) |
| 38 *error_out = error; |
| 39 return NULL; |
| 40 } |
| 41 LocalFileSystemOperation* local_operation = |
| 42 operation->AsLocalFileSystemOperation(); |
| 43 DCHECK(local_operation); |
| 44 |
| 45 // Let the new operation inherit from the original operation. |
| 46 local_operation->set_overriding_operation_context( |
| 47 original_operation_->operation_context()); |
| 48 if (error_out) |
| 49 *error_out = base::PLATFORM_FILE_OK; |
| 50 return local_operation; |
| 51 } |
| 52 |
| 53 FileSystemContext* RecursiveOperationDelegate::file_system_context() { |
| 54 return original_operation_->file_system_context(); |
| 55 } |
| 56 |
| 57 void RecursiveOperationDelegate::ProcessNextDirectory( |
| 58 base::PlatformFileError error) { |
| 59 if (error != base::PLATFORM_FILE_OK) { |
| 60 callback_.Run(error); |
| 61 return; |
| 62 } |
| 63 if (inflight_operations_ > 0) |
| 64 return; |
| 65 if (pending_directories_.empty()) { |
| 66 callback_.Run(error); |
| 67 return; |
| 68 } |
| 69 FileSystemURL url = pending_directories_.front(); |
| 70 pending_directories_.pop(); |
| 71 inflight_operations_++; |
| 72 ProcessDirectory( |
| 73 url, base::Bind(&RecursiveOperationDelegate::DidProcessDirectory, |
| 74 AsWeakPtr(), url)); |
| 75 } |
| 76 |
| 77 void RecursiveOperationDelegate::DidProcessFile(base::PlatformFileError error) { |
| 78 inflight_operations_--; |
| 79 DCHECK_GE(inflight_operations_, 0); |
| 80 ProcessNextDirectory(error); |
| 81 } |
| 82 |
| 83 void RecursiveOperationDelegate::DidProcessDirectory( |
| 84 const FileSystemURL& url, |
| 85 base::PlatformFileError error) { |
| 86 if (error != base::PLATFORM_FILE_OK) { |
| 87 callback_.Run(error); |
| 88 return; |
| 89 } |
| 90 LocalFileSystemOperation* operation = NewOperation(url, &error); |
| 91 if (!operation) { |
| 92 callback_.Run(error); |
| 93 return; |
| 94 } |
| 95 operation->ReadDirectory( |
| 96 url, base::Bind(&RecursiveOperationDelegate::DidReadDirectory, |
| 97 AsWeakPtr(), url)); |
| 98 } |
| 99 |
| 100 void RecursiveOperationDelegate::DidReadDirectory( |
| 101 const FileSystemURL& parent, |
| 102 base::PlatformFileError error, |
| 103 const FileEntryList& entries, |
| 104 bool has_more) { |
| 105 if (error != base::PLATFORM_FILE_OK) { |
| 106 if (error == base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY) { |
| 107 // The given path may have been a file, so try RemoveFile now. |
| 108 ProcessFile(parent, |
| 109 base::Bind(&RecursiveOperationDelegate::DidTryProcessFile, |
| 110 AsWeakPtr(), error)); |
| 111 return; |
| 112 } |
| 113 callback_.Run(error); |
| 114 return; |
| 115 } |
| 116 for (size_t i = 0; i < entries.size(); i++) { |
| 117 FileSystemURL url = parent.WithPath(parent.path().Append(entries[i].name)); |
| 118 if (entries[i].is_directory) { |
| 119 pending_directories_.push(url); |
| 120 continue; |
| 121 } |
| 122 inflight_operations_++; |
| 123 ProcessFile(url, base::Bind(&RecursiveOperationDelegate::DidProcessFile, |
| 124 AsWeakPtr())); |
| 125 } |
| 126 if (has_more) |
| 127 return; |
| 128 |
| 129 inflight_operations_--; |
| 130 DCHECK_GE(inflight_operations_, 0); |
| 131 ProcessNextDirectory(base::PLATFORM_FILE_OK); |
| 132 } |
| 133 |
| 134 void RecursiveOperationDelegate::DidTryProcessFile( |
| 135 base::PlatformFileError previous_error, |
| 136 base::PlatformFileError error) { |
| 137 if (error == base::PLATFORM_FILE_ERROR_NOT_A_FILE) { |
| 138 // It wasn't a file either; returns with the previous error. |
| 139 callback_.Run(previous_error); |
| 140 return; |
| 141 } |
| 142 DidProcessFile(error); |
| 143 } |
| 144 |
| 145 } // namespace fileapi |
| OLD | NEW |