[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 1 | // Copyright 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/browser/fileapi/file_system_operation_runner.h" |
| 6 | |
| 7 | #include "base/bind.h" |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 8 | #include "base/message_loop/message_loop_proxy.h" |
| 9 | #include "base/stl_util.h" |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 10 | #include "net/url_request/url_request_context.h" |
[email protected] | 8438889 | 2013-09-07 04:20:18 | [diff] [blame] | 11 | #include "webkit/browser/blob/blob_url_request_job_factory.h" |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 12 | #include "webkit/browser/fileapi/file_observers.h" |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 13 | #include "webkit/browser/fileapi/file_stream_writer.h" |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 14 | #include "webkit/browser/fileapi/file_system_context.h" |
[email protected] | bd69f57 | 2013-09-09 06:15:23 | [diff] [blame] | 15 | #include "webkit/browser/fileapi/file_system_operation.h" |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 16 | #include "webkit/browser/fileapi/file_writer_delegate.h" |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 17 | #include "webkit/common/blob/shareable_file_reference.h" |
| 18 | |
| 19 | namespace fileapi { |
| 20 | |
| 21 | typedef FileSystemOperationRunner::OperationID OperationID; |
| 22 | |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 23 | class FileSystemOperationRunner::BeginOperationScoper |
| 24 | : public base::SupportsWeakPtr< |
| 25 | FileSystemOperationRunner::BeginOperationScoper> { |
| 26 | public: |
| 27 | BeginOperationScoper() {} |
| 28 | private: |
| 29 | DISALLOW_COPY_AND_ASSIGN(BeginOperationScoper); |
| 30 | }; |
| 31 | |
| 32 | FileSystemOperationRunner::OperationHandle::OperationHandle() {} |
| 33 | FileSystemOperationRunner::OperationHandle::~OperationHandle() {} |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 34 | |
| 35 | FileSystemOperationRunner::~FileSystemOperationRunner() { |
| 36 | } |
| 37 | |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 38 | void FileSystemOperationRunner::Shutdown() { |
| 39 | operations_.Clear(); |
| 40 | } |
| 41 | |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 42 | OperationID FileSystemOperationRunner::CreateFile( |
| 43 | const FileSystemURL& url, |
| 44 | bool exclusive, |
| 45 | const StatusCallback& callback) { |
| 46 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 47 | FileSystemOperation* operation = |
| 48 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 49 | |
| 50 | BeginOperationScoper scope; |
| 51 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 52 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 53 | DidFinish(handle, callback, error); |
| 54 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 55 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 56 | PrepareForWrite(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 57 | operation->CreateFile( |
| 58 | url, exclusive, |
| 59 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 60 | handle, callback)); |
| 61 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | OperationID FileSystemOperationRunner::CreateDirectory( |
| 65 | const FileSystemURL& url, |
| 66 | bool exclusive, |
| 67 | bool recursive, |
| 68 | const StatusCallback& callback) { |
| 69 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 70 | FileSystemOperation* operation = |
| 71 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 72 | BeginOperationScoper scope; |
| 73 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 74 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 75 | DidFinish(handle, callback, error); |
| 76 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 77 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 78 | PrepareForWrite(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 79 | operation->CreateDirectory( |
| 80 | url, exclusive, recursive, |
| 81 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 82 | handle, callback)); |
| 83 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | OperationID FileSystemOperationRunner::Copy( |
| 87 | const FileSystemURL& src_url, |
| 88 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 89 | CopyOrMoveOption option, |
[email protected] | af34956 | 2013-09-09 14:18:53 | [diff] [blame] | 90 | const CopyProgressCallback& progress_callback, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 91 | const StatusCallback& callback) { |
| 92 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 93 | FileSystemOperation* operation = |
| 94 | file_system_context_->CreateFileSystemOperation(dest_url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 95 | BeginOperationScoper scope; |
| 96 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 97 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 98 | DidFinish(handle, callback, error); |
| 99 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 100 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 101 | PrepareForWrite(handle.id, dest_url); |
| 102 | PrepareForRead(handle.id, src_url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 103 | operation->Copy( |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 104 | src_url, dest_url, option, |
[email protected] | d307243 | 2013-09-12 17:39:29 | [diff] [blame] | 105 | progress_callback.is_null() ? |
| 106 | CopyProgressCallback() : |
| 107 | base::Bind(&FileSystemOperationRunner::OnCopyProgress, AsWeakPtr(), |
| 108 | handle, progress_callback), |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 109 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 110 | handle, callback)); |
| 111 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | OperationID FileSystemOperationRunner::Move( |
| 115 | const FileSystemURL& src_url, |
| 116 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 117 | CopyOrMoveOption option, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 118 | const StatusCallback& callback) { |
| 119 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 120 | FileSystemOperation* operation = |
| 121 | file_system_context_->CreateFileSystemOperation(dest_url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 122 | BeginOperationScoper scope; |
| 123 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 124 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 125 | DidFinish(handle, callback, error); |
| 126 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 127 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 128 | PrepareForWrite(handle.id, dest_url); |
| 129 | PrepareForWrite(handle.id, src_url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 130 | operation->Move( |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 131 | src_url, dest_url, option, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 132 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 133 | handle, callback)); |
| 134 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | OperationID FileSystemOperationRunner::DirectoryExists( |
| 138 | const FileSystemURL& url, |
| 139 | const StatusCallback& callback) { |
| 140 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 141 | FileSystemOperation* operation = |
| 142 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 143 | BeginOperationScoper scope; |
| 144 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 145 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 146 | DidFinish(handle, callback, error); |
| 147 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 148 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 149 | PrepareForRead(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 150 | operation->DirectoryExists( |
| 151 | url, |
| 152 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 153 | handle, callback)); |
| 154 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | OperationID FileSystemOperationRunner::FileExists( |
| 158 | const FileSystemURL& url, |
| 159 | const StatusCallback& callback) { |
| 160 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 161 | FileSystemOperation* operation = |
| 162 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 163 | BeginOperationScoper scope; |
| 164 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 165 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 166 | DidFinish(handle, callback, error); |
| 167 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 168 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 169 | PrepareForRead(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 170 | operation->FileExists( |
| 171 | url, |
| 172 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 173 | handle, callback)); |
| 174 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | OperationID FileSystemOperationRunner::GetMetadata( |
| 178 | const FileSystemURL& url, |
| 179 | const GetMetadataCallback& callback) { |
| 180 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 181 | FileSystemOperation* operation = |
| 182 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 183 | BeginOperationScoper scope; |
| 184 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 185 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 186 | DidGetMetadata(handle, callback, error, base::PlatformFileInfo()); |
| 187 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 188 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 189 | PrepareForRead(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 190 | operation->GetMetadata( |
| 191 | url, |
| 192 | base::Bind(&FileSystemOperationRunner::DidGetMetadata, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 193 | handle, callback)); |
| 194 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | OperationID FileSystemOperationRunner::ReadDirectory( |
| 198 | const FileSystemURL& url, |
| 199 | const ReadDirectoryCallback& callback) { |
| 200 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 201 | FileSystemOperation* operation = |
| 202 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 203 | BeginOperationScoper scope; |
| 204 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 205 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 206 | DidReadDirectory(handle, callback, error, std::vector<DirectoryEntry>(), |
| 207 | false); |
| 208 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 209 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 210 | PrepareForRead(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 211 | operation->ReadDirectory( |
| 212 | url, |
| 213 | base::Bind(&FileSystemOperationRunner::DidReadDirectory, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 214 | handle, callback)); |
| 215 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | OperationID FileSystemOperationRunner::Remove( |
| 219 | const FileSystemURL& url, bool recursive, |
| 220 | const StatusCallback& callback) { |
| 221 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 222 | FileSystemOperation* operation = |
| 223 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 224 | BeginOperationScoper scope; |
| 225 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 226 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 227 | DidFinish(handle, callback, error); |
| 228 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 229 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 230 | PrepareForWrite(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 231 | operation->Remove( |
| 232 | url, recursive, |
| 233 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 234 | handle, callback)); |
| 235 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | OperationID FileSystemOperationRunner::Write( |
| 239 | const net::URLRequestContext* url_request_context, |
| 240 | const FileSystemURL& url, |
[email protected] | 8438889 | 2013-09-07 04:20:18 | [diff] [blame] | 241 | scoped_ptr<webkit_blob::BlobDataHandle> blob, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 242 | int64 offset, |
| 243 | const WriteCallback& callback) { |
| 244 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 245 | FileSystemOperation* operation = |
| 246 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 247 | |
| 248 | BeginOperationScoper scope; |
| 249 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 250 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 251 | DidWrite(handle, callback, error, 0, true); |
| 252 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 253 | } |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 254 | |
| 255 | scoped_ptr<FileStreamWriter> writer( |
| 256 | file_system_context_->CreateFileStreamWriter(url, offset)); |
| 257 | if (!writer) { |
| 258 | // Write is not supported. |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 259 | DidWrite(handle, callback, base::PLATFORM_FILE_ERROR_SECURITY, 0, true); |
| 260 | return handle.id; |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 263 | scoped_ptr<FileWriterDelegate> writer_delegate( |
| 264 | new FileWriterDelegate(writer.Pass())); |
[email protected] | 8438889 | 2013-09-07 04:20:18 | [diff] [blame] | 265 | |
| 266 | scoped_ptr<net::URLRequest> blob_request( |
| 267 | webkit_blob::BlobProtocolHandler::CreateBlobRequest( |
| 268 | blob.Pass(), |
| 269 | url_request_context, |
| 270 | writer_delegate.get())); |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 271 | |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 272 | PrepareForWrite(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 273 | operation->Write( |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 274 | url, writer_delegate.Pass(), blob_request.Pass(), |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 275 | base::Bind(&FileSystemOperationRunner::DidWrite, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 276 | handle, callback)); |
| 277 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | OperationID FileSystemOperationRunner::Truncate( |
| 281 | const FileSystemURL& url, int64 length, |
| 282 | const StatusCallback& callback) { |
| 283 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 284 | FileSystemOperation* operation = |
| 285 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 286 | BeginOperationScoper scope; |
| 287 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 288 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 289 | DidFinish(handle, callback, error); |
| 290 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 291 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 292 | PrepareForWrite(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 293 | operation->Truncate( |
| 294 | url, length, |
| 295 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 296 | handle, callback)); |
| 297 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void FileSystemOperationRunner::Cancel( |
| 301 | OperationID id, |
| 302 | const StatusCallback& callback) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 303 | if (ContainsKey(finished_operations_, id)) { |
| 304 | DCHECK(!ContainsKey(stray_cancel_callbacks_, id)); |
| 305 | stray_cancel_callbacks_[id] = callback; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 306 | return; |
| 307 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 308 | FileSystemOperation* operation = operations_.Lookup(id); |
[email protected] | 4f976f761 | 2013-09-13 15:51:27 | [diff] [blame] | 309 | if (!operation) { |
| 310 | // There is no operation with |id|. |
| 311 | callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); |
| 312 | return; |
| 313 | } |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 314 | operation->Cancel(callback); |
| 315 | } |
| 316 | |
| 317 | OperationID FileSystemOperationRunner::TouchFile( |
| 318 | const FileSystemURL& url, |
| 319 | const base::Time& last_access_time, |
| 320 | const base::Time& last_modified_time, |
| 321 | const StatusCallback& callback) { |
| 322 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 323 | FileSystemOperation* operation = |
| 324 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 325 | BeginOperationScoper scope; |
| 326 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 327 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 328 | DidFinish(handle, callback, error); |
| 329 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 330 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 331 | PrepareForWrite(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 332 | operation->TouchFile( |
| 333 | url, last_access_time, last_modified_time, |
| 334 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 335 | handle, callback)); |
| 336 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | OperationID FileSystemOperationRunner::OpenFile( |
| 340 | const FileSystemURL& url, |
| 341 | int file_flags, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 342 | const OpenFileCallback& callback) { |
| 343 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 344 | FileSystemOperation* operation = |
| 345 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 346 | BeginOperationScoper scope; |
| 347 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 348 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 349 | DidOpenFile(handle, callback, error, base::kInvalidPlatformFileValue, |
[email protected] | 10c3922 | 2013-11-13 20:09:25 | [diff] [blame] | 350 | base::Closure()); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 351 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 352 | } |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 353 | if (file_flags & |
| 354 | (base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_OPEN_ALWAYS | |
| 355 | base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_TRUNCATED | |
| 356 | base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE | |
| 357 | base::PLATFORM_FILE_DELETE_ON_CLOSE | |
| 358 | base::PLATFORM_FILE_WRITE_ATTRIBUTES)) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 359 | PrepareForWrite(handle.id, url); |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 360 | } else { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 361 | PrepareForRead(handle.id, url); |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 362 | } |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 363 | operation->OpenFile( |
[email protected] | 10c3922 | 2013-11-13 20:09:25 | [diff] [blame] | 364 | url, file_flags, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 365 | base::Bind(&FileSystemOperationRunner::DidOpenFile, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 366 | handle, callback)); |
| 367 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | OperationID FileSystemOperationRunner::CreateSnapshotFile( |
| 371 | const FileSystemURL& url, |
| 372 | const SnapshotFileCallback& callback) { |
| 373 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 374 | FileSystemOperation* operation = |
| 375 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 376 | BeginOperationScoper scope; |
| 377 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 378 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 379 | DidCreateSnapshot(handle, callback, error, base::PlatformFileInfo(), |
| 380 | base::FilePath(), NULL); |
| 381 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 382 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 383 | PrepareForRead(handle.id, url); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 384 | operation->CreateSnapshotFile( |
| 385 | url, |
| 386 | base::Bind(&FileSystemOperationRunner::DidCreateSnapshot, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 387 | handle, callback)); |
| 388 | return handle.id; |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 389 | } |
| 390 | |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 391 | OperationID FileSystemOperationRunner::CopyInForeignFile( |
| 392 | const base::FilePath& src_local_disk_path, |
| 393 | const FileSystemURL& dest_url, |
| 394 | const StatusCallback& callback) { |
| 395 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 396 | FileSystemOperation* operation = |
| 397 | file_system_context_->CreateFileSystemOperation(dest_url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 398 | BeginOperationScoper scope; |
| 399 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 400 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 401 | DidFinish(handle, callback, error); |
| 402 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 403 | } |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 404 | operation->CopyInForeignFile( |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 405 | src_local_disk_path, dest_url, |
| 406 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 407 | handle, callback)); |
| 408 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | OperationID FileSystemOperationRunner::RemoveFile( |
| 412 | const FileSystemURL& url, |
| 413 | const StatusCallback& callback) { |
| 414 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 415 | FileSystemOperation* operation = |
| 416 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 417 | BeginOperationScoper scope; |
| 418 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 419 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 420 | DidFinish(handle, callback, error); |
| 421 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 422 | } |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 423 | operation->RemoveFile( |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 424 | url, |
| 425 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 426 | handle, callback)); |
| 427 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | OperationID FileSystemOperationRunner::RemoveDirectory( |
| 431 | const FileSystemURL& url, |
| 432 | const StatusCallback& callback) { |
| 433 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 434 | FileSystemOperation* operation = |
| 435 | file_system_context_->CreateFileSystemOperation(url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 436 | BeginOperationScoper scope; |
| 437 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 438 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 439 | DidFinish(handle, callback, error); |
| 440 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 441 | } |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 442 | operation->RemoveDirectory( |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 443 | url, |
| 444 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 445 | handle, callback)); |
| 446 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | OperationID FileSystemOperationRunner::CopyFileLocal( |
| 450 | const FileSystemURL& src_url, |
| 451 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 452 | CopyOrMoveOption option, |
[email protected] | af34956 | 2013-09-09 14:18:53 | [diff] [blame] | 453 | const CopyFileProgressCallback& progress_callback, |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 454 | const StatusCallback& callback) { |
| 455 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 456 | FileSystemOperation* operation = |
| 457 | file_system_context_->CreateFileSystemOperation(src_url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 458 | BeginOperationScoper scope; |
| 459 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 460 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 461 | DidFinish(handle, callback, error); |
| 462 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 463 | } |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 464 | operation->CopyFileLocal( |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 465 | src_url, dest_url, option, progress_callback, |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 466 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 467 | handle, callback)); |
| 468 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | OperationID FileSystemOperationRunner::MoveFileLocal( |
| 472 | const FileSystemURL& src_url, |
| 473 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 474 | CopyOrMoveOption option, |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 475 | const StatusCallback& callback) { |
| 476 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 477 | FileSystemOperation* operation = |
| 478 | file_system_context_->CreateFileSystemOperation(src_url, &error); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 479 | BeginOperationScoper scope; |
| 480 | OperationHandle handle = BeginOperation(operation, scope.AsWeakPtr()); |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 481 | if (!operation) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 482 | DidFinish(handle, callback, error); |
| 483 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 484 | } |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 485 | operation->MoveFileLocal( |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 486 | src_url, dest_url, option, |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 487 | base::Bind(&FileSystemOperationRunner::DidFinish, AsWeakPtr(), |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 488 | handle, callback)); |
| 489 | return handle.id; |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 490 | } |
| 491 | |
[email protected] | 3a7bf22 | 2013-06-09 14:14:12 | [diff] [blame] | 492 | base::PlatformFileError FileSystemOperationRunner::SyncGetPlatformPath( |
| 493 | const FileSystemURL& url, |
| 494 | base::FilePath* platform_path) { |
| 495 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | ee38793 | 2013-10-28 19:46:09 | [diff] [blame] | 496 | scoped_ptr<FileSystemOperation> operation( |
| 497 | file_system_context_->CreateFileSystemOperation(url, &error)); |
| 498 | if (!operation.get()) |
[email protected] | 3a7bf22 | 2013-06-09 14:14:12 | [diff] [blame] | 499 | return error; |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 500 | return operation->SyncGetPlatformPath(url, platform_path); |
[email protected] | 3a7bf22 | 2013-06-09 14:14:12 | [diff] [blame] | 501 | } |
| 502 | |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 503 | FileSystemOperationRunner::FileSystemOperationRunner( |
| 504 | FileSystemContext* file_system_context) |
| 505 | : file_system_context_(file_system_context) {} |
| 506 | |
| 507 | void FileSystemOperationRunner::DidFinish( |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 508 | const OperationHandle& handle, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 509 | const StatusCallback& callback, |
| 510 | base::PlatformFileError rv) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 511 | if (handle.scope) { |
| 512 | finished_operations_.insert(handle.id); |
| 513 | base::MessageLoopProxy::current()->PostTask( |
| 514 | FROM_HERE, base::Bind(&FileSystemOperationRunner::DidFinish, |
| 515 | AsWeakPtr(), handle, callback, rv)); |
| 516 | return; |
| 517 | } |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 518 | callback.Run(rv); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 519 | FinishOperation(handle.id); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void FileSystemOperationRunner::DidGetMetadata( |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 523 | const OperationHandle& handle, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 524 | const GetMetadataCallback& callback, |
| 525 | base::PlatformFileError rv, |
[email protected] | 86b74ce | 2013-06-14 06:57:10 | [diff] [blame] | 526 | const base::PlatformFileInfo& file_info) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 527 | if (handle.scope) { |
| 528 | finished_operations_.insert(handle.id); |
| 529 | base::MessageLoopProxy::current()->PostTask( |
| 530 | FROM_HERE, base::Bind(&FileSystemOperationRunner::DidGetMetadata, |
| 531 | AsWeakPtr(), handle, callback, rv, file_info)); |
| 532 | return; |
| 533 | } |
[email protected] | 86b74ce | 2013-06-14 06:57:10 | [diff] [blame] | 534 | callback.Run(rv, file_info); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 535 | FinishOperation(handle.id); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void FileSystemOperationRunner::DidReadDirectory( |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 539 | const OperationHandle& handle, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 540 | const ReadDirectoryCallback& callback, |
| 541 | base::PlatformFileError rv, |
| 542 | const std::vector<DirectoryEntry>& entries, |
| 543 | bool has_more) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 544 | if (handle.scope) { |
| 545 | finished_operations_.insert(handle.id); |
| 546 | base::MessageLoopProxy::current()->PostTask( |
| 547 | FROM_HERE, base::Bind(&FileSystemOperationRunner::DidReadDirectory, |
| 548 | AsWeakPtr(), handle, callback, rv, |
| 549 | entries, has_more)); |
| 550 | return; |
| 551 | } |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 552 | callback.Run(rv, entries, has_more); |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 553 | if (rv != base::PLATFORM_FILE_OK || !has_more) |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 554 | FinishOperation(handle.id); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void FileSystemOperationRunner::DidWrite( |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 558 | const OperationHandle& handle, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 559 | const WriteCallback& callback, |
| 560 | base::PlatformFileError rv, |
| 561 | int64 bytes, |
| 562 | bool complete) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 563 | if (handle.scope) { |
| 564 | finished_operations_.insert(handle.id); |
| 565 | base::MessageLoopProxy::current()->PostTask( |
| 566 | FROM_HERE, base::Bind(&FileSystemOperationRunner::DidWrite, AsWeakPtr(), |
| 567 | handle, callback, rv, bytes, complete)); |
| 568 | return; |
| 569 | } |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 570 | callback.Run(rv, bytes, complete); |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 571 | if (rv != base::PLATFORM_FILE_OK || complete) |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 572 | FinishOperation(handle.id); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void FileSystemOperationRunner::DidOpenFile( |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 576 | const OperationHandle& handle, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 577 | const OpenFileCallback& callback, |
| 578 | base::PlatformFileError rv, |
| 579 | base::PlatformFile file, |
[email protected] | 10c3922 | 2013-11-13 20:09:25 | [diff] [blame] | 580 | const base::Closure& on_close_callback) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 581 | if (handle.scope) { |
| 582 | finished_operations_.insert(handle.id); |
| 583 | base::MessageLoopProxy::current()->PostTask( |
| 584 | FROM_HERE, base::Bind(&FileSystemOperationRunner::DidOpenFile, |
| 585 | AsWeakPtr(), handle, callback, rv, file, |
[email protected] | 10c3922 | 2013-11-13 20:09:25 | [diff] [blame] | 586 | on_close_callback)); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 587 | return; |
| 588 | } |
[email protected] | 10c3922 | 2013-11-13 20:09:25 | [diff] [blame] | 589 | callback.Run(rv, file, on_close_callback); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 590 | FinishOperation(handle.id); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void FileSystemOperationRunner::DidCreateSnapshot( |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 594 | const OperationHandle& handle, |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 595 | const SnapshotFileCallback& callback, |
| 596 | base::PlatformFileError rv, |
| 597 | const base::PlatformFileInfo& file_info, |
| 598 | const base::FilePath& platform_path, |
| 599 | const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 600 | if (handle.scope) { |
| 601 | finished_operations_.insert(handle.id); |
| 602 | base::MessageLoopProxy::current()->PostTask( |
| 603 | FROM_HERE, base::Bind(&FileSystemOperationRunner::DidCreateSnapshot, |
| 604 | AsWeakPtr(), handle, callback, rv, file_info, |
| 605 | platform_path, file_ref)); |
| 606 | return; |
| 607 | } |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 608 | callback.Run(rv, file_info, platform_path, file_ref); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 609 | FinishOperation(handle.id); |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 610 | } |
| 611 | |
[email protected] | d307243 | 2013-09-12 17:39:29 | [diff] [blame] | 612 | void FileSystemOperationRunner::OnCopyProgress( |
| 613 | const OperationHandle& handle, |
| 614 | const CopyProgressCallback& callback, |
| 615 | FileSystemOperation::CopyProgressType type, |
[email protected] | 9def556 | 2013-09-20 11:24:45 | [diff] [blame] | 616 | const FileSystemURL& source_url, |
| 617 | const FileSystemURL& dest_url, |
[email protected] | d307243 | 2013-09-12 17:39:29 | [diff] [blame] | 618 | int64 size) { |
| 619 | if (handle.scope) { |
| 620 | base::MessageLoopProxy::current()->PostTask( |
[email protected] | 9def556 | 2013-09-20 11:24:45 | [diff] [blame] | 621 | FROM_HERE, base::Bind( |
| 622 | &FileSystemOperationRunner::OnCopyProgress, |
| 623 | AsWeakPtr(), handle, callback, type, source_url, dest_url, size)); |
[email protected] | d307243 | 2013-09-12 17:39:29 | [diff] [blame] | 624 | return; |
| 625 | } |
[email protected] | 9def556 | 2013-09-20 11:24:45 | [diff] [blame] | 626 | callback.Run(type, source_url, dest_url, size); |
[email protected] | d307243 | 2013-09-12 17:39:29 | [diff] [blame] | 627 | } |
| 628 | |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 629 | void FileSystemOperationRunner::PrepareForWrite(OperationID id, |
| 630 | const FileSystemURL& url) { |
| 631 | if (file_system_context_->GetUpdateObservers(url.type())) { |
| 632 | file_system_context_->GetUpdateObservers(url.type())->Notify( |
| 633 | &FileUpdateObserver::OnStartUpdate, MakeTuple(url)); |
| 634 | } |
| 635 | write_target_urls_[id].insert(url); |
| 636 | } |
| 637 | |
| 638 | void FileSystemOperationRunner::PrepareForRead(OperationID id, |
| 639 | const FileSystemURL& url) { |
| 640 | if (file_system_context_->GetAccessObservers(url.type())) { |
| 641 | file_system_context_->GetAccessObservers(url.type())->Notify( |
| 642 | &FileAccessObserver::OnAccess, MakeTuple(url)); |
| 643 | } |
| 644 | } |
| 645 | |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 646 | FileSystemOperationRunner::OperationHandle |
| 647 | FileSystemOperationRunner::BeginOperation( |
| 648 | FileSystemOperation* operation, |
| 649 | base::WeakPtr<BeginOperationScoper> scope) { |
| 650 | OperationHandle handle; |
| 651 | handle.id = operations_.Add(operation); |
| 652 | handle.scope = scope; |
| 653 | return handle; |
| 654 | } |
| 655 | |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 656 | void FileSystemOperationRunner::FinishOperation(OperationID id) { |
| 657 | OperationToURLSet::iterator found = write_target_urls_.find(id); |
| 658 | if (found != write_target_urls_.end()) { |
| 659 | const FileSystemURLSet& urls = found->second; |
| 660 | for (FileSystemURLSet::const_iterator iter = urls.begin(); |
| 661 | iter != urls.end(); ++iter) { |
| 662 | if (file_system_context_->GetUpdateObservers(iter->type())) { |
| 663 | file_system_context_->GetUpdateObservers(iter->type())->Notify( |
| 664 | &FileUpdateObserver::OnEndUpdate, MakeTuple(*iter)); |
| 665 | } |
| 666 | } |
| 667 | write_target_urls_.erase(found); |
| 668 | } |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 669 | |
| 670 | // IDMap::Lookup fails if the operation is NULL, so we don't check |
| 671 | // operations_.Lookup(id) here. |
| 672 | |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 673 | operations_.Remove(id); |
[email protected] | 33f5a4e | 2013-09-05 11:24:19 | [diff] [blame] | 674 | finished_operations_.erase(id); |
| 675 | |
| 676 | // Dispatch stray cancel callback if exists. |
| 677 | std::map<OperationID, StatusCallback>::iterator found_cancel = |
| 678 | stray_cancel_callbacks_.find(id); |
| 679 | if (found_cancel != stray_cancel_callbacks_.end()) { |
| 680 | // This cancel has been requested after the operation has finished, |
| 681 | // so report that we failed to stop it. |
| 682 | found_cancel->second.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); |
| 683 | stray_cancel_callbacks_.erase(found_cancel); |
| 684 | } |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 685 | } |
| 686 | |
[email protected] | 6fb9264 | 2013-06-05 13:05:16 | [diff] [blame] | 687 | } // namespace fileapi |