[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 5 | #include "webkit/browser/fileapi/file_system_operation_impl.h" |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 6 | |
[email protected] | 30c1eea | 2011-10-17 18:40:30 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | dc57ec8 | 2012-08-07 03:50:10 | [diff] [blame] | 8 | #include "base/single_thread_task_runner.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 0a8ebe1 | 2013-06-28 15:23:23 | [diff] [blame] | 10 | #include "base/time/time.h" |
[email protected] | 41e88943 | 2011-04-13 19:53:30 | [diff] [blame] | 11 | #include "net/base/escape.h" |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 12 | #include "net/url_request/url_request.h" |
[email protected] | c6f9203a | 2013-05-28 02:08:07 | [diff] [blame] | 13 | #include "webkit/browser/fileapi/async_file_util.h" |
[email protected] | f17d6a1 | 2013-06-04 06:57:07 | [diff] [blame] | 14 | #include "webkit/browser/fileapi/copy_or_move_operation_delegate.h" |
[email protected] | f25e113 | 2013-05-24 13:58:04 | [diff] [blame] | 15 | #include "webkit/browser/fileapi/file_observers.h" |
[email protected] | f19bbf6 | 2013-07-09 01:22:32 | [diff] [blame] | 16 | #include "webkit/browser/fileapi/file_system_backend.h" |
[email protected] | c6f9203a | 2013-05-28 02:08:07 | [diff] [blame] | 17 | #include "webkit/browser/fileapi/file_system_context.h" |
[email protected] | 5a20d04 | 2013-05-22 12:54:18 | [diff] [blame] | 18 | #include "webkit/browser/fileapi/file_system_file_util.h" |
[email protected] | c6f9203a | 2013-05-28 02:08:07 | [diff] [blame] | 19 | #include "webkit/browser/fileapi/file_system_operation_context.h" |
[email protected] | c6f9203a | 2013-05-28 02:08:07 | [diff] [blame] | 20 | #include "webkit/browser/fileapi/file_system_url.h" |
| 21 | #include "webkit/browser/fileapi/file_writer_delegate.h" |
| 22 | #include "webkit/browser/fileapi/remove_operation_delegate.h" |
[email protected] | 57acf8a | 2013-08-30 00:06:47 | [diff] [blame] | 23 | #include "webkit/browser/fileapi/sandbox_file_system_backend.h" |
[email protected] | 844a7f8 | 2014-01-17 16:59:45 | [diff] [blame] | 24 | #include "webkit/browser/quota/quota_manager_proxy.h" |
[email protected] | b76b556d | 2013-05-29 03:09:43 | [diff] [blame] | 25 | #include "webkit/common/blob/shareable_file_reference.h" |
[email protected] | 61d271f3 | 2013-05-28 04:59:23 | [diff] [blame] | 26 | #include "webkit/common/fileapi/file_system_types.h" |
| 27 | #include "webkit/common/fileapi/file_system_util.h" |
[email protected] | 7660ec9 | 2013-05-30 05:12:39 | [diff] [blame] | 28 | #include "webkit/common/quota/quota_types.h" |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 29 | |
[email protected] | 7ab45cfa8 | 2013-04-26 07:13:20 | [diff] [blame] | 30 | using webkit_blob::ScopedFile; |
[email protected] | 826df82 | 2012-08-04 01:23:56 | [diff] [blame] | 31 | |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 32 | namespace fileapi { |
| 33 | |
[email protected] | bd69f57 | 2013-09-09 06:15:23 | [diff] [blame] | 34 | FileSystemOperation* FileSystemOperation::Create( |
[email protected] | 5dfa47c | 2013-06-10 04:57:15 | [diff] [blame] | 35 | const FileSystemURL& url, |
[email protected] | 84780b4 | 2013-04-19 14:56:11 | [diff] [blame] | 36 | FileSystemContext* file_system_context, |
[email protected] | bd69f57 | 2013-09-09 06:15:23 | [diff] [blame] | 37 | scoped_ptr<FileSystemOperationContext> operation_context) { |
| 38 | return new FileSystemOperationImpl(url, file_system_context, |
| 39 | operation_context.Pass()); |
[email protected] | 84780b4 | 2013-04-19 14:56:11 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 42 | FileSystemOperationImpl::~FileSystemOperationImpl() { |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 45 | void FileSystemOperationImpl::CreateFile(const FileSystemURL& url, |
| 46 | bool exclusive, |
| 47 | const StatusCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 48 | DCHECK(SetPendingOperationType(kOperationCreateFile)); |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 49 | GetUsageAndQuotaThenRunTask( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 50 | url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 51 | base::Bind(&FileSystemOperationImpl::DoCreateFile, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 52 | weak_factory_.GetWeakPtr(), url, callback, exclusive), |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 53 | base::Bind(callback, base::File::FILE_ERROR_FAILED)); |
[email protected] | ffac1ac4 | 2011-07-25 15:03:15 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 56 | void FileSystemOperationImpl::CreateDirectory(const FileSystemURL& url, |
| 57 | bool exclusive, |
| 58 | bool recursive, |
| 59 | const StatusCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 60 | DCHECK(SetPendingOperationType(kOperationCreateDirectory)); |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 61 | GetUsageAndQuotaThenRunTask( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 62 | url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 63 | base::Bind(&FileSystemOperationImpl::DoCreateDirectory, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 64 | weak_factory_.GetWeakPtr(), url, callback, |
| 65 | exclusive, recursive), |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 66 | base::Bind(callback, base::File::FILE_ERROR_FAILED)); |
[email protected] | ffac1ac4 | 2011-07-25 15:03:15 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | af34956 | 2013-09-09 14:18:53 | [diff] [blame] | 69 | void FileSystemOperationImpl::Copy( |
| 70 | const FileSystemURL& src_url, |
| 71 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 72 | CopyOrMoveOption option, |
[email protected] | af34956 | 2013-09-09 14:18:53 | [diff] [blame] | 73 | const CopyProgressCallback& progress_callback, |
| 74 | const StatusCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 75 | DCHECK(SetPendingOperationType(kOperationCopy)); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 76 | DCHECK(!recursive_operation_delegate_); |
[email protected] | af34956 | 2013-09-09 14:18:53 | [diff] [blame] | 77 | |
| 78 | // TODO(hidehiko): Support |progress_callback|. (crbug.com/278038). |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 79 | recursive_operation_delegate_.reset( |
[email protected] | f17d6a1 | 2013-06-04 06:57:07 | [diff] [blame] | 80 | new CopyOrMoveOperationDelegate( |
[email protected] | a85efc3 | 2013-03-04 10:45:51 | [diff] [blame] | 81 | file_system_context(), |
[email protected] | a85efc3 | 2013-03-04 10:45:51 | [diff] [blame] | 82 | src_url, dest_url, |
[email protected] | f17d6a1 | 2013-06-04 06:57:07 | [diff] [blame] | 83 | CopyOrMoveOperationDelegate::OPERATION_COPY, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 84 | option, |
[email protected] | b2ef40c | 2013-09-12 13:07:42 | [diff] [blame] | 85 | progress_callback, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 86 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 87 | weak_factory_.GetWeakPtr(), callback))); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 88 | recursive_operation_delegate_->RunRecursively(); |
[email protected] | b97b5d3 | 2011-05-10 11:20:56 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 91 | void FileSystemOperationImpl::Move(const FileSystemURL& src_url, |
| 92 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 93 | CopyOrMoveOption option, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 94 | const StatusCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 95 | DCHECK(SetPendingOperationType(kOperationMove)); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 96 | DCHECK(!recursive_operation_delegate_); |
| 97 | recursive_operation_delegate_.reset( |
[email protected] | f17d6a1 | 2013-06-04 06:57:07 | [diff] [blame] | 98 | new CopyOrMoveOperationDelegate( |
[email protected] | a85efc3 | 2013-03-04 10:45:51 | [diff] [blame] | 99 | file_system_context(), |
[email protected] | a85efc3 | 2013-03-04 10:45:51 | [diff] [blame] | 100 | src_url, dest_url, |
[email protected] | f17d6a1 | 2013-06-04 06:57:07 | [diff] [blame] | 101 | CopyOrMoveOperationDelegate::OPERATION_MOVE, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 102 | option, |
[email protected] | b2ef40c | 2013-09-12 13:07:42 | [diff] [blame] | 103 | FileSystemOperation::CopyProgressCallback(), |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 104 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 105 | weak_factory_.GetWeakPtr(), callback))); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 106 | recursive_operation_delegate_->RunRecursively(); |
[email protected] | b97b5d3 | 2011-05-10 11:20:56 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 109 | void FileSystemOperationImpl::DirectoryExists(const FileSystemURL& url, |
| 110 | const StatusCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 111 | DCHECK(SetPendingOperationType(kOperationDirectoryExists)); |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 112 | async_file_util_->GetFileInfo( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 113 | operation_context_.Pass(), url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 114 | base::Bind(&FileSystemOperationImpl::DidDirectoryExists, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 115 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 118 | void FileSystemOperationImpl::FileExists(const FileSystemURL& url, |
| 119 | const StatusCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 120 | DCHECK(SetPendingOperationType(kOperationFileExists)); |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 121 | async_file_util_->GetFileInfo( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 122 | operation_context_.Pass(), url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 123 | base::Bind(&FileSystemOperationImpl::DidFileExists, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 124 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 127 | void FileSystemOperationImpl::GetMetadata( |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 128 | const FileSystemURL& url, const GetMetadataCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 129 | DCHECK(SetPendingOperationType(kOperationGetMetadata)); |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 130 | async_file_util_->GetFileInfo(operation_context_.Pass(), url, callback); |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 133 | void FileSystemOperationImpl::ReadDirectory( |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 134 | const FileSystemURL& url, const ReadDirectoryCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 135 | DCHECK(SetPendingOperationType(kOperationReadDirectory)); |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 136 | async_file_util_->ReadDirectory( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 137 | operation_context_.Pass(), url, callback); |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 140 | void FileSystemOperationImpl::Remove(const FileSystemURL& url, |
| 141 | bool recursive, |
| 142 | const StatusCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 143 | DCHECK(SetPendingOperationType(kOperationRemove)); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 144 | DCHECK(!recursive_operation_delegate_); |
[email protected] | ef1cc9f | 2013-07-01 20:15:36 | [diff] [blame] | 145 | |
| 146 | if (recursive) { |
| 147 | // For recursive removal, try to delegate the operation to AsyncFileUtil |
| 148 | // first. If not supported, it is delegated to RemoveOperationDelegate |
| 149 | // in DidDeleteRecursively. |
| 150 | async_file_util_->DeleteRecursively( |
| 151 | operation_context_.Pass(), url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 152 | base::Bind(&FileSystemOperationImpl::DidDeleteRecursively, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 153 | weak_factory_.GetWeakPtr(), url, callback)); |
[email protected] | ef1cc9f | 2013-07-01 20:15:36 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 157 | recursive_operation_delegate_.reset( |
| 158 | new RemoveOperationDelegate( |
[email protected] | 3c631b2 | 2013-06-05 16:13:34 | [diff] [blame] | 159 | file_system_context(), url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 160 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 161 | weak_factory_.GetWeakPtr(), callback))); |
[email protected] | ef1cc9f | 2013-07-01 20:15:36 | [diff] [blame] | 162 | recursive_operation_delegate_->Run(); |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 165 | void FileSystemOperationImpl::Write( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 166 | const FileSystemURL& url, |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 167 | scoped_ptr<FileWriterDelegate> writer_delegate, |
| 168 | scoped_ptr<net::URLRequest> blob_request, |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 169 | const WriteCallback& callback) { |
[email protected] | 1a6e390 | 2013-06-13 07:09:40 | [diff] [blame] | 170 | DCHECK(SetPendingOperationType(kOperationWrite)); |
| 171 | file_writer_delegate_ = writer_delegate.Pass(); |
| 172 | file_writer_delegate_->Start( |
| 173 | blob_request.Pass(), |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 174 | base::Bind(&FileSystemOperationImpl::DidWrite, |
| 175 | weak_factory_.GetWeakPtr(), url, callback)); |
[email protected] | b97b5d3 | 2011-05-10 11:20:56 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 178 | void FileSystemOperationImpl::Truncate(const FileSystemURL& url, int64 length, |
| 179 | const StatusCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 180 | DCHECK(SetPendingOperationType(kOperationTruncate)); |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 181 | GetUsageAndQuotaThenRunTask( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 182 | url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 183 | base::Bind(&FileSystemOperationImpl::DoTruncate, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 184 | weak_factory_.GetWeakPtr(), url, callback, length), |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 185 | base::Bind(callback, base::File::FILE_ERROR_FAILED)); |
[email protected] | b97b5d3 | 2011-05-10 11:20:56 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 188 | void FileSystemOperationImpl::TouchFile(const FileSystemURL& url, |
| 189 | const base::Time& last_access_time, |
| 190 | const base::Time& last_modified_time, |
| 191 | const StatusCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 192 | DCHECK(SetPendingOperationType(kOperationTouchFile)); |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 193 | async_file_util_->Touch( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 194 | operation_context_.Pass(), url, |
[email protected] | 65c337e0 | 2012-02-29 04:31:10 | [diff] [blame] | 195 | last_access_time, last_modified_time, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 196 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 197 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | ed7ae17 | 2010-09-27 21:22:01 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 200 | void FileSystemOperationImpl::OpenFile(const FileSystemURL& url, |
| 201 | int file_flags, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 202 | const OpenFileCallback& callback) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 203 | DCHECK(SetPendingOperationType(kOperationOpenFile)); |
[email protected] | 49b67d2 | 2011-10-26 11:18:26 | [diff] [blame] | 204 | |
[email protected] | 45d5c60 | 2013-10-07 18:33:22 | [diff] [blame] | 205 | if (file_flags & |
| 206 | (base::PLATFORM_FILE_TEMPORARY | base::PLATFORM_FILE_HIDDEN)) { |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 207 | callback.Run(base::File::FILE_ERROR_FAILED, |
[email protected] | 1aa9e15d | 2013-04-26 13:51:20 | [diff] [blame] | 208 | base::kInvalidPlatformFileValue, |
[email protected] | 10c3922 | 2013-11-13 20:09:25 | [diff] [blame] | 209 | base::Closure()); |
[email protected] | 07c3d21 | 2011-04-16 00:02:53 | [diff] [blame] | 210 | return; |
| 211 | } |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 212 | GetUsageAndQuotaThenRunTask( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 213 | url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 214 | base::Bind(&FileSystemOperationImpl::DoOpenFile, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 215 | weak_factory_.GetWeakPtr(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 216 | url, callback, file_flags), |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 217 | base::Bind(callback, base::File::FILE_ERROR_FAILED, |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 218 | base::kInvalidPlatformFileValue, |
[email protected] | 10c3922 | 2013-11-13 20:09:25 | [diff] [blame] | 219 | base::Closure())); |
[email protected] | ffac1ac4 | 2011-07-25 15:03:15 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | 7eb68a2 | 2010-10-05 02:26:47 | [diff] [blame] | 222 | // We can only get here on a write or truncate that's not yet completed. |
| 223 | // We don't support cancelling any other operation at this time. |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 224 | void FileSystemOperationImpl::Cancel(const StatusCallback& cancel_callback) { |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 225 | DCHECK(cancel_callback_.is_null()); |
| 226 | cancel_callback_ = cancel_callback; |
| 227 | |
| 228 | if (file_writer_delegate_.get()) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 229 | DCHECK_EQ(kOperationWrite, pending_operation_); |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 230 | // This will call DidWrite() with ABORT status code. |
| 231 | file_writer_delegate_->Cancel(); |
[email protected] | 4f976f761 | 2013-09-13 15:51:27 | [diff] [blame] | 232 | } else if (recursive_operation_delegate_) { |
| 233 | // This will call DidFinishOperation() with ABORT status code. |
| 234 | recursive_operation_delegate_->Cancel(); |
[email protected] | bd15238 | 2010-10-07 23:24:38 | [diff] [blame] | 235 | } else { |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 236 | // For truncate we have no way to cancel the inflight operation (for now). |
| 237 | // Let it just run and dispatch cancel callback later. |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 238 | DCHECK_EQ(kOperationTruncate, pending_operation_); |
[email protected] | bd15238 | 2010-10-07 23:24:38 | [diff] [blame] | 239 | } |
[email protected] | ac71626 | 2010-09-24 01:06:00 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 242 | void FileSystemOperationImpl::CreateSnapshotFile( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 243 | const FileSystemURL& url, |
[email protected] | 4737387 | 2012-02-28 06:27:30 | [diff] [blame] | 244 | const SnapshotFileCallback& callback) { |
[email protected] | d0a1f037 | 2012-07-19 11:17:37 | [diff] [blame] | 245 | DCHECK(SetPendingOperationType(kOperationCreateSnapshotFile)); |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 246 | async_file_util_->CreateSnapshotFile( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 247 | operation_context_.Pass(), url, callback); |
[email protected] | 4737387 | 2012-02-28 06:27:30 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 250 | void FileSystemOperationImpl::CopyInForeignFile( |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 251 | const base::FilePath& src_local_disk_file_path, |
[email protected] | d954389 | 2012-10-12 06:49:56 | [diff] [blame] | 252 | const FileSystemURL& dest_url, |
| 253 | const StatusCallback& callback) { |
| 254 | DCHECK(SetPendingOperationType(kOperationCopyInForeignFile)); |
[email protected] | d954389 | 2012-10-12 06:49:56 | [diff] [blame] | 255 | GetUsageAndQuotaThenRunTask( |
| 256 | dest_url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 257 | base::Bind(&FileSystemOperationImpl::DoCopyInForeignFile, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 258 | weak_factory_.GetWeakPtr(), src_local_disk_file_path, dest_url, |
[email protected] | d954389 | 2012-10-12 06:49:56 | [diff] [blame] | 259 | callback), |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 260 | base::Bind(callback, base::File::FILE_ERROR_FAILED)); |
[email protected] | d954389 | 2012-10-12 06:49:56 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 263 | void FileSystemOperationImpl::RemoveFile( |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 264 | const FileSystemURL& url, |
| 265 | const StatusCallback& callback) { |
| 266 | DCHECK(SetPendingOperationType(kOperationRemove)); |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 267 | async_file_util_->DeleteFile( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 268 | operation_context_.Pass(), url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 269 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 270 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 271 | } |
| 272 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 273 | void FileSystemOperationImpl::RemoveDirectory( |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 274 | const FileSystemURL& url, |
| 275 | const StatusCallback& callback) { |
| 276 | DCHECK(SetPendingOperationType(kOperationRemove)); |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 277 | async_file_util_->DeleteDirectory( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 278 | operation_context_.Pass(), url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 279 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 280 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 283 | void FileSystemOperationImpl::CopyFileLocal( |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 284 | const FileSystemURL& src_url, |
| 285 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 286 | CopyOrMoveOption option, |
[email protected] | af34956 | 2013-09-09 14:18:53 | [diff] [blame] | 287 | const CopyFileProgressCallback& progress_callback, |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 288 | const StatusCallback& callback) { |
| 289 | DCHECK(SetPendingOperationType(kOperationCopy)); |
[email protected] | 4c413de0 | 2013-05-27 06:00:59 | [diff] [blame] | 290 | DCHECK(src_url.IsInSameFileSystem(dest_url)); |
[email protected] | af34956 | 2013-09-09 14:18:53 | [diff] [blame] | 291 | |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 292 | GetUsageAndQuotaThenRunTask( |
| 293 | dest_url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 294 | base::Bind(&FileSystemOperationImpl::DoCopyFileLocal, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 295 | weak_factory_.GetWeakPtr(), src_url, dest_url, option, |
[email protected] | 9ebe8790 | 2013-09-12 01:31:56 | [diff] [blame] | 296 | progress_callback, callback), |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 297 | base::Bind(callback, base::File::FILE_ERROR_FAILED)); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 300 | void FileSystemOperationImpl::MoveFileLocal( |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 301 | const FileSystemURL& src_url, |
| 302 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 303 | CopyOrMoveOption option, |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 304 | const StatusCallback& callback) { |
| 305 | DCHECK(SetPendingOperationType(kOperationMove)); |
[email protected] | 4c413de0 | 2013-05-27 06:00:59 | [diff] [blame] | 306 | DCHECK(src_url.IsInSameFileSystem(dest_url)); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 307 | GetUsageAndQuotaThenRunTask( |
| 308 | dest_url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 309 | base::Bind(&FileSystemOperationImpl::DoMoveFileLocal, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 310 | weak_factory_.GetWeakPtr(), |
| 311 | src_url, dest_url, option, callback), |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 312 | base::Bind(callback, base::File::FILE_ERROR_FAILED)); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 315 | base::File::Error FileSystemOperationImpl::SyncGetPlatformPath( |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 316 | const FileSystemURL& url, |
| 317 | base::FilePath* platform_path) { |
| 318 | DCHECK(SetPendingOperationType(kOperationGetLocalPath)); |
[email protected] | 57acf8a | 2013-08-30 00:06:47 | [diff] [blame] | 319 | if (!file_system_context()->IsSandboxFileSystem(url.type())) |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 320 | return base::File::FILE_ERROR_INVALID_OPERATION; |
[email protected] | 57acf8a | 2013-08-30 00:06:47 | [diff] [blame] | 321 | FileSystemFileUtil* file_util = |
| 322 | file_system_context()->sandbox_delegate()->sync_file_util(); |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 323 | file_util->GetLocalFilePath(operation_context_.get(), url, platform_path); |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 324 | return base::File::FILE_OK; |
[email protected] | 5d4d7a6 | 2013-08-27 07:43:31 | [diff] [blame] | 325 | } |
| 326 | |
[email protected] | bd69f57 | 2013-09-09 06:15:23 | [diff] [blame] | 327 | FileSystemOperationImpl::FileSystemOperationImpl( |
| 328 | const FileSystemURL& url, |
| 329 | FileSystemContext* file_system_context, |
| 330 | scoped_ptr<FileSystemOperationContext> operation_context) |
| 331 | : file_system_context_(file_system_context), |
| 332 | operation_context_(operation_context.Pass()), |
| 333 | async_file_util_(NULL), |
[email protected] | bd69f57 | 2013-09-09 06:15:23 | [diff] [blame] | 334 | pending_operation_(kOperationNone), |
| 335 | weak_factory_(this) { |
| 336 | DCHECK(operation_context_.get()); |
| 337 | operation_context_->DetachUserDataThread(); |
| 338 | async_file_util_ = file_system_context_->GetAsyncFileUtil(url.type()); |
| 339 | DCHECK(async_file_util_); |
| 340 | } |
| 341 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 342 | void FileSystemOperationImpl::GetUsageAndQuotaThenRunTask( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 343 | const FileSystemURL& url, |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 344 | const base::Closure& task, |
| 345 | const base::Closure& error_callback) { |
[email protected] | b97b5d3 | 2011-05-10 11:20:56 | [diff] [blame] | 346 | quota::QuotaManagerProxy* quota_manager_proxy = |
| 347 | file_system_context()->quota_manager_proxy(); |
[email protected] | 81484d7 | 2011-09-20 07:21:07 | [diff] [blame] | 348 | if (!quota_manager_proxy || |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 349 | !file_system_context()->GetQuotaUtil(url.type())) { |
[email protected] | 81484d7 | 2011-09-20 07:21:07 | [diff] [blame] | 350 | // If we don't have the quota manager or the requested filesystem type |
| 351 | // does not support quota, we should be able to let it go. |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 352 | operation_context_->set_allowed_bytes_growth(kint64max); |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 353 | task.Run(); |
[email protected] | 81484d7 | 2011-09-20 07:21:07 | [diff] [blame] | 354 | return; |
[email protected] | b97b5d3 | 2011-05-10 11:20:56 | [diff] [blame] | 355 | } |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 356 | |
[email protected] | 81484d7 | 2011-09-20 07:21:07 | [diff] [blame] | 357 | DCHECK(quota_manager_proxy); |
| 358 | DCHECK(quota_manager_proxy->quota_manager()); |
| 359 | quota_manager_proxy->quota_manager()->GetUsageAndQuota( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 360 | url.origin(), |
| 361 | FileSystemTypeToQuotaStorageType(url.type()), |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 362 | base::Bind(&FileSystemOperationImpl::DidGetUsageAndQuotaAndRunTask, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 363 | weak_factory_.GetWeakPtr(), task, error_callback)); |
[email protected] | b97b5d3 | 2011-05-10 11:20:56 | [diff] [blame] | 364 | } |
| 365 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 366 | void FileSystemOperationImpl::DidGetUsageAndQuotaAndRunTask( |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 367 | const base::Closure& task, |
| 368 | const base::Closure& error_callback, |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 369 | quota::QuotaStatusCode status, |
| 370 | int64 usage, int64 quota) { |
| 371 | if (status != quota::kQuotaStatusOk) { |
| 372 | LOG(WARNING) << "Got unexpected quota error : " << status; |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 373 | error_callback.Run(); |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 377 | operation_context_->set_allowed_bytes_growth(quota - usage); |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 378 | task.Run(); |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 379 | } |
| 380 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 381 | void FileSystemOperationImpl::DoCreateFile( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 382 | const FileSystemURL& url, |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 383 | const StatusCallback& callback, |
| 384 | bool exclusive) { |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 385 | async_file_util_->EnsureFileExists( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 386 | operation_context_.Pass(), url, |
[email protected] | 787a399e | 2012-02-28 01:05:54 | [diff] [blame] | 387 | base::Bind( |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 388 | exclusive ? |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 389 | &FileSystemOperationImpl::DidEnsureFileExistsExclusive : |
| 390 | &FileSystemOperationImpl::DidEnsureFileExistsNonExclusive, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 391 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | 787a399e | 2012-02-28 01:05:54 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 394 | void FileSystemOperationImpl::DoCreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 395 | const FileSystemURL& url, |
[email protected] | 787a399e | 2012-02-28 01:05:54 | [diff] [blame] | 396 | const StatusCallback& callback, |
[email protected] | 2a50446 | 2012-02-28 18:55:22 | [diff] [blame] | 397 | bool exclusive, bool recursive) { |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 398 | async_file_util_->CreateDirectory( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 399 | operation_context_.Pass(), |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 400 | url, exclusive, recursive, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 401 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 402 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | 787a399e | 2012-02-28 01:05:54 | [diff] [blame] | 403 | } |
| 404 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 405 | void FileSystemOperationImpl::DoCopyFileLocal( |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 406 | const FileSystemURL& src_url, |
| 407 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 408 | CopyOrMoveOption option, |
[email protected] | 9ebe8790 | 2013-09-12 01:31:56 | [diff] [blame] | 409 | const CopyFileProgressCallback& progress_callback, |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 410 | const StatusCallback& callback) { |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 411 | async_file_util_->CopyFileLocal( |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 412 | operation_context_.Pass(), src_url, dest_url, option, progress_callback, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 413 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 414 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 417 | void FileSystemOperationImpl::DoMoveFileLocal( |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 418 | const FileSystemURL& src_url, |
| 419 | const FileSystemURL& dest_url, |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 420 | CopyOrMoveOption option, |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 421 | const StatusCallback& callback) { |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 422 | async_file_util_->MoveFileLocal( |
[email protected] | 824d389 | 2013-09-25 13:00:30 | [diff] [blame] | 423 | operation_context_.Pass(), src_url, dest_url, option, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 424 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 425 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | 787a399e | 2012-02-28 01:05:54 | [diff] [blame] | 426 | } |
| 427 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 428 | void FileSystemOperationImpl::DoCopyInForeignFile( |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 429 | const base::FilePath& src_local_disk_file_path, |
[email protected] | dd68456 | 2012-09-14 04:30:02 | [diff] [blame] | 430 | const FileSystemURL& dest_url, |
| 431 | const StatusCallback& callback) { |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 432 | async_file_util_->CopyInForeignFile( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 433 | operation_context_.Pass(), |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 434 | src_local_disk_file_path, dest_url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 435 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 436 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | 787a399e | 2012-02-28 01:05:54 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 439 | void FileSystemOperationImpl::DoTruncate(const FileSystemURL& url, |
| 440 | const StatusCallback& callback, |
| 441 | int64 length) { |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 442 | async_file_util_->Truncate( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 443 | operation_context_.Pass(), url, length, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 444 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 445 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | 787a399e | 2012-02-28 01:05:54 | [diff] [blame] | 446 | } |
| 447 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 448 | void FileSystemOperationImpl::DoOpenFile(const FileSystemURL& url, |
| 449 | const OpenFileCallback& callback, |
| 450 | int file_flags) { |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 451 | async_file_util_->CreateOrOpen( |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 452 | operation_context_.Pass(), url, file_flags, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 453 | base::Bind(&FileSystemOperationImpl::DidOpenFile, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 454 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | 787a399e | 2012-02-28 01:05:54 | [diff] [blame] | 455 | } |
| 456 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 457 | void FileSystemOperationImpl::DidEnsureFileExistsExclusive( |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 458 | const StatusCallback& callback, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 459 | base::File::Error rv, bool created) { |
| 460 | if (rv == base::File::FILE_OK && !created) { |
| 461 | callback.Run(base::File::FILE_ERROR_EXISTS); |
[email protected] | ed514e8 | 2011-05-25 09:15:43 | [diff] [blame] | 462 | } else { |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 463 | DidFinishOperation(callback, rv); |
[email protected] | ed514e8 | 2011-05-25 09:15:43 | [diff] [blame] | 464 | } |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 465 | } |
| 466 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 467 | void FileSystemOperationImpl::DidEnsureFileExistsNonExclusive( |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 468 | const StatusCallback& callback, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 469 | base::File::Error rv, bool /* created */) { |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 470 | DidFinishOperation(callback, rv); |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 471 | } |
| 472 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 473 | void FileSystemOperationImpl::DidFinishOperation( |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 474 | const StatusCallback& callback, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 475 | base::File::Error rv) { |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 476 | if (!cancel_callback_.is_null()) { |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 477 | StatusCallback cancel_callback = cancel_callback_; |
[email protected] | 2022894 | 2013-09-05 15:52:41 | [diff] [blame] | 478 | callback.Run(rv); |
| 479 | |
| 480 | // Return OK only if we succeeded to stop the operation. |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 481 | cancel_callback.Run(rv == base::File::FILE_ERROR_ABORT ? |
| 482 | base::File::FILE_OK : |
| 483 | base::File::FILE_ERROR_INVALID_OPERATION); |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 484 | } else { |
| 485 | callback.Run(rv); |
[email protected] | 7eb68a2 | 2010-10-05 02:26:47 | [diff] [blame] | 486 | } |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 487 | } |
| 488 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 489 | void FileSystemOperationImpl::DidDirectoryExists( |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 490 | const StatusCallback& callback, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 491 | base::File::Error rv, |
| 492 | const base::File::Info& file_info) { |
| 493 | if (rv == base::File::FILE_OK && !file_info.is_directory) |
| 494 | rv = base::File::FILE_ERROR_NOT_A_DIRECTORY; |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 495 | callback.Run(rv); |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 496 | } |
| 497 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 498 | void FileSystemOperationImpl::DidFileExists( |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 499 | const StatusCallback& callback, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 500 | base::File::Error rv, |
| 501 | const base::File::Info& file_info) { |
| 502 | if (rv == base::File::FILE_OK && file_info.is_directory) |
| 503 | rv = base::File::FILE_ERROR_NOT_A_FILE; |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 504 | callback.Run(rv); |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 505 | } |
| 506 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 507 | void FileSystemOperationImpl::DidDeleteRecursively( |
[email protected] | ef1cc9f | 2013-07-01 20:15:36 | [diff] [blame] | 508 | const FileSystemURL& url, |
| 509 | const StatusCallback& callback, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 510 | base::File::Error rv) { |
| 511 | if (rv == base::File::FILE_ERROR_INVALID_OPERATION) { |
[email protected] | ef1cc9f | 2013-07-01 20:15:36 | [diff] [blame] | 512 | // Recursive removal is not supported on this platform. |
| 513 | DCHECK(!recursive_operation_delegate_); |
| 514 | recursive_operation_delegate_.reset( |
| 515 | new RemoveOperationDelegate( |
| 516 | file_system_context(), url, |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 517 | base::Bind(&FileSystemOperationImpl::DidFinishOperation, |
[email protected] | c420e4c | 2013-08-28 06:28:37 | [diff] [blame] | 518 | weak_factory_.GetWeakPtr(), callback))); |
[email protected] | ef1cc9f | 2013-07-01 20:15:36 | [diff] [blame] | 519 | recursive_operation_delegate_->RunRecursively(); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | callback.Run(rv); |
| 524 | } |
| 525 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 526 | void FileSystemOperationImpl::DidWrite( |
[email protected] | ca47448 | 2012-09-26 11:21:21 | [diff] [blame] | 527 | const FileSystemURL& url, |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 528 | const WriteCallback& write_callback, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 529 | base::File::Error rv, |
[email protected] | ac71626 | 2010-09-24 01:06:00 | [diff] [blame] | 530 | int64 bytes, |
[email protected] | ca47448 | 2012-09-26 11:21:21 | [diff] [blame] | 531 | FileWriterDelegate::WriteProgressStatus write_status) { |
[email protected] | ca47448 | 2012-09-26 11:21:21 | [diff] [blame] | 532 | const bool complete = ( |
| 533 | write_status != FileWriterDelegate::SUCCESS_IO_PENDING); |
| 534 | if (complete && write_status != FileWriterDelegate::ERROR_WRITE_NOT_STARTED) { |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 535 | DCHECK(operation_context_); |
| 536 | operation_context_->change_observers()->Notify( |
[email protected] | ca47448 | 2012-09-26 11:21:21 | [diff] [blame] | 537 | &FileChangeObserver::OnModifyFile, MakeTuple(url)); |
| 538 | } |
| 539 | |
[email protected] | 2c02c8d | 2013-06-10 17:52:43 | [diff] [blame] | 540 | StatusCallback cancel_callback = cancel_callback_; |
| 541 | write_callback.Run(rv, bytes, complete); |
| 542 | if (!cancel_callback.is_null()) |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 543 | cancel_callback.Run(base::File::FILE_OK); |
[email protected] | ed7ae17 | 2010-09-27 21:22:01 | [diff] [blame] | 544 | } |
| 545 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 546 | void FileSystemOperationImpl::DidOpenFile( |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 547 | const OpenFileCallback& callback, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 548 | base::File::Error rv, |
[email protected] | 969cbbe | 2013-07-10 07:55:18 | [diff] [blame] | 549 | base::PassPlatformFile file, |
| 550 | const base::Closure& on_close_callback) { |
[email protected] | 10c3922 | 2013-11-13 20:09:25 | [diff] [blame] | 551 | callback.Run(rv, file.ReleaseValue(), on_close_callback); |
[email protected] | 07c3d21 | 2011-04-16 00:02:53 | [diff] [blame] | 552 | } |
| 553 | |
[email protected] | 61b48e0 | 2013-08-01 15:00:10 | [diff] [blame] | 554 | bool FileSystemOperationImpl::SetPendingOperationType(OperationType type) { |
[email protected] | f7fdd4d | 2012-02-16 11:32:45 | [diff] [blame] | 555 | if (pending_operation_ != kOperationNone) |
| 556 | return false; |
| 557 | pending_operation_ = type; |
| 558 | return true; |
| 559 | } |
| 560 | |
[email protected] | f05ca65 | 2010-09-16 04:46:14 | [diff] [blame] | 561 | } // namespace fileapi |