| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/fileapi/file_system_operation.h" | 5 #include "webkit/fileapi/file_system_operation.h" |
| 6 | 6 |
| 7 #include "base/time.h" | 7 #include "base/time.h" |
| 8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
| 9 #include "webkit/fileapi/file_system_callback_dispatcher.h" | 9 #include "webkit/fileapi/file_system_callback_dispatcher.h" |
| 10 | 10 |
| 11 namespace fileapi { | 11 namespace fileapi { |
| 12 | 12 |
| 13 FileSystemOperation::FileSystemOperation( | 13 FileSystemOperation::FileSystemOperation( |
| 14 FileSystemCallbackDispatcher* dispatcher, | 14 FileSystemCallbackDispatcher* dispatcher, |
| 15 scoped_refptr<base::MessageLoopProxy> proxy) | 15 scoped_refptr<base::MessageLoopProxy> proxy) |
| 16 : proxy_(proxy), | 16 : proxy_(proxy), |
| 17 dispatcher_(dispatcher), | 17 dispatcher_(dispatcher), |
| 18 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 18 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 19 cancel_operation_(NULL) { | |
| 20 DCHECK(dispatcher); | 19 DCHECK(dispatcher); |
| 21 #ifndef NDEBUG | 20 #ifndef NDEBUG |
| 22 pending_operation_ = kOperationNone; | 21 operation_pending_ = false; |
| 23 #endif | 22 #endif |
| 24 } | 23 } |
| 25 | 24 |
| 26 FileSystemOperation::~FileSystemOperation() { | 25 FileSystemOperation::~FileSystemOperation() { |
| 27 } | 26 } |
| 28 | 27 |
| 29 void FileSystemOperation::CreateFile(const FilePath& path, | 28 void FileSystemOperation::CreateFile(const FilePath& path, |
| 30 bool exclusive) { | 29 bool exclusive) { |
| 31 #ifndef NDEBUG | 30 #ifndef NDEBUG |
| 32 DCHECK(kOperationNone == pending_operation_); | 31 DCHECK(!operation_pending_); |
| 33 pending_operation_ = kOperationCreateFile; | 32 operation_pending_ = true; |
| 34 #endif | 33 #endif |
| 35 | 34 |
| 36 base::FileUtilProxy::CreateOrOpen( | 35 base::FileUtilProxy::CreateOrOpen( |
| 37 proxy_, path, base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, | 36 proxy_, path, base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, |
| 38 callback_factory_.NewCallback( | 37 callback_factory_.NewCallback( |
| 39 exclusive ? &FileSystemOperation::DidCreateFileExclusive | 38 exclusive ? &FileSystemOperation::DidCreateFileExclusive |
| 40 : &FileSystemOperation::DidCreateFileNonExclusive)); | 39 : &FileSystemOperation::DidCreateFileNonExclusive)); |
| 41 } | 40 } |
| 42 | 41 |
| 43 void FileSystemOperation::CreateDirectory(const FilePath& path, | 42 void FileSystemOperation::CreateDirectory(const FilePath& path, |
| 44 bool exclusive, | 43 bool exclusive, |
| 45 bool recursive) { | 44 bool recursive) { |
| 46 #ifndef NDEBUG | 45 #ifndef NDEBUG |
| 47 DCHECK(kOperationNone == pending_operation_); | 46 DCHECK(!operation_pending_); |
| 48 pending_operation_ = kOperationCreateDirectory; | 47 operation_pending_ = true; |
| 49 #endif | 48 #endif |
| 50 | 49 |
| 51 base::FileUtilProxy::CreateDirectory( | 50 base::FileUtilProxy::CreateDirectory( |
| 52 proxy_, path, exclusive, recursive, callback_factory_.NewCallback( | 51 proxy_, path, exclusive, recursive, callback_factory_.NewCallback( |
| 53 &FileSystemOperation::DidFinishFileOperation)); | 52 &FileSystemOperation::DidFinishFileOperation)); |
| 54 } | 53 } |
| 55 | 54 |
| 56 void FileSystemOperation::Copy(const FilePath& src_path, | 55 void FileSystemOperation::Copy(const FilePath& src_path, |
| 57 const FilePath& dest_path) { | 56 const FilePath& dest_path) { |
| 58 #ifndef NDEBUG | 57 #ifndef NDEBUG |
| 59 DCHECK(kOperationNone == pending_operation_); | 58 DCHECK(!operation_pending_); |
| 60 pending_operation_ = kOperationCopy; | 59 operation_pending_ = true; |
| 61 #endif | 60 #endif |
| 62 | 61 |
| 63 base::FileUtilProxy::Copy(proxy_, src_path, dest_path, | 62 base::FileUtilProxy::Copy(proxy_, src_path, dest_path, |
| 64 callback_factory_.NewCallback( | 63 callback_factory_.NewCallback( |
| 65 &FileSystemOperation::DidFinishFileOperation)); | 64 &FileSystemOperation::DidFinishFileOperation)); |
| 66 } | 65 } |
| 67 | 66 |
| 68 void FileSystemOperation::Move(const FilePath& src_path, | 67 void FileSystemOperation::Move(const FilePath& src_path, |
| 69 const FilePath& dest_path) { | 68 const FilePath& dest_path) { |
| 70 #ifndef NDEBUG | 69 #ifndef NDEBUG |
| 71 DCHECK(kOperationNone == pending_operation_); | 70 DCHECK(!operation_pending_); |
| 72 pending_operation_ = kOperationMove; | 71 operation_pending_ = true; |
| 73 #endif | 72 #endif |
| 74 | 73 |
| 75 base::FileUtilProxy::Move(proxy_, src_path, dest_path, | 74 base::FileUtilProxy::Move(proxy_, src_path, dest_path, |
| 76 callback_factory_.NewCallback( | 75 callback_factory_.NewCallback( |
| 77 &FileSystemOperation::DidFinishFileOperation)); | 76 &FileSystemOperation::DidFinishFileOperation)); |
| 78 } | 77 } |
| 79 | 78 |
| 80 void FileSystemOperation::DirectoryExists(const FilePath& path) { | 79 void FileSystemOperation::DirectoryExists(const FilePath& path) { |
| 81 #ifndef NDEBUG | 80 #ifndef NDEBUG |
| 82 DCHECK(kOperationNone == pending_operation_); | 81 DCHECK(!operation_pending_); |
| 83 pending_operation_ = kOperationDirectoryExists; | 82 operation_pending_ = true; |
| 84 #endif | 83 #endif |
| 85 | 84 |
| 86 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( | 85 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( |
| 87 &FileSystemOperation::DidDirectoryExists)); | 86 &FileSystemOperation::DidDirectoryExists)); |
| 88 } | 87 } |
| 89 | 88 |
| 90 void FileSystemOperation::FileExists(const FilePath& path) { | 89 void FileSystemOperation::FileExists(const FilePath& path) { |
| 91 #ifndef NDEBUG | 90 #ifndef NDEBUG |
| 92 DCHECK(kOperationNone == pending_operation_); | 91 DCHECK(!operation_pending_); |
| 93 pending_operation_ = kOperationFileExists; | 92 operation_pending_ = true; |
| 94 #endif | 93 #endif |
| 95 | 94 |
| 96 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( | 95 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( |
| 97 &FileSystemOperation::DidFileExists)); | 96 &FileSystemOperation::DidFileExists)); |
| 98 } | 97 } |
| 99 | 98 |
| 100 void FileSystemOperation::GetMetadata(const FilePath& path) { | 99 void FileSystemOperation::GetMetadata(const FilePath& path) { |
| 101 #ifndef NDEBUG | 100 #ifndef NDEBUG |
| 102 DCHECK(kOperationNone == pending_operation_); | 101 DCHECK(!operation_pending_); |
| 103 pending_operation_ = kOperationGetMetadata; | 102 operation_pending_ = true; |
| 104 #endif | 103 #endif |
| 105 | 104 |
| 106 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( | 105 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( |
| 107 &FileSystemOperation::DidGetMetadata)); | 106 &FileSystemOperation::DidGetMetadata)); |
| 108 } | 107 } |
| 109 | 108 |
| 110 void FileSystemOperation::ReadDirectory(const FilePath& path) { | 109 void FileSystemOperation::ReadDirectory(const FilePath& path) { |
| 111 #ifndef NDEBUG | 110 #ifndef NDEBUG |
| 112 DCHECK(kOperationNone == pending_operation_); | 111 DCHECK(!operation_pending_); |
| 113 pending_operation_ = kOperationReadDirectory; | 112 operation_pending_ = true; |
| 114 #endif | 113 #endif |
| 115 | 114 |
| 116 base::FileUtilProxy::ReadDirectory(proxy_, path, | 115 base::FileUtilProxy::ReadDirectory(proxy_, path, |
| 117 callback_factory_.NewCallback( | 116 callback_factory_.NewCallback( |
| 118 &FileSystemOperation::DidReadDirectory)); | 117 &FileSystemOperation::DidReadDirectory)); |
| 119 } | 118 } |
| 120 | 119 |
| 121 void FileSystemOperation::Remove(const FilePath& path) { | 120 void FileSystemOperation::Remove(const FilePath& path) { |
| 122 #ifndef NDEBUG | 121 #ifndef NDEBUG |
| 123 DCHECK(kOperationNone == pending_operation_); | 122 DCHECK(!operation_pending_); |
| 124 pending_operation_ = kOperationRemove; | 123 operation_pending_ = true; |
| 125 #endif | 124 #endif |
| 126 | 125 |
| 127 base::FileUtilProxy::Delete(proxy_, path, callback_factory_.NewCallback( | 126 base::FileUtilProxy::Delete(proxy_, path, callback_factory_.NewCallback( |
| 128 &FileSystemOperation::DidFinishFileOperation)); | 127 &FileSystemOperation::DidFinishFileOperation)); |
| 129 } | 128 } |
| 130 | 129 |
| 131 void FileSystemOperation::Write( | 130 void FileSystemOperation::Write( |
| 132 const FilePath& path, | 131 const FilePath&, |
| 133 const GURL& blob_url, | 132 const GURL&, |
| 134 int64 offset) { | 133 int64) { |
| 135 #ifndef NDEBUG | 134 #ifndef NDEBUG |
| 136 DCHECK(kOperationNone == pending_operation_); | 135 DCHECK(!operation_pending_); |
| 137 pending_operation_ = kOperationWrite; | 136 operation_pending_ = true; |
| 138 #endif | 137 #endif |
| 139 NOTREACHED(); | 138 NOTREACHED(); |
| 139 // TODO(ericu): |
| 140 // Set up a loop that, via multiple callback invocations, reads from a |
| 141 // URLRequest wrapping blob_url, writes the bytes to the file, reports |
| 142 // progress events no more frequently than some set rate, and periodically |
| 143 // checks to see if it's been cancelled. |
| 140 } | 144 } |
| 141 | 145 |
| 142 void FileSystemOperation::Truncate(const FilePath& path, int64 length) { | 146 void FileSystemOperation::Truncate(const FilePath& path, int64 length) { |
| 143 #ifndef NDEBUG | 147 #ifndef NDEBUG |
| 144 DCHECK(kOperationNone == pending_operation_); | 148 DCHECK(!operation_pending_); |
| 145 pending_operation_ = kOperationTruncate; | 149 operation_pending_ = true; |
| 146 #endif | 150 #endif |
| 147 base::FileUtilProxy::Truncate(proxy_, path, length, | 151 // TODO(ericu): |
| 148 callback_factory_.NewCallback( | 152 NOTREACHED(); |
| 149 &FileSystemOperation::DidFinishFileOperation)); | |
| 150 } | 153 } |
| 151 | 154 |
| 152 void FileSystemOperation::TouchFile(const FilePath& path, | 155 void FileSystemOperation::TouchFile(const FilePath& path, |
| 153 const base::Time& last_access_time, | 156 const base::Time& last_access_time, |
| 154 const base::Time& last_modified_time) { | 157 const base::Time& last_modified_time) { |
| 155 #ifndef NDEBUG | 158 #ifndef NDEBUG |
| 156 DCHECK(kOperationNone == pending_operation_); | 159 DCHECK(!operation_pending_); |
| 157 pending_operation_ = kOperationTouchFile; | 160 operation_pending_ = true; |
| 158 #endif | 161 #endif |
| 159 | 162 |
| 160 base::FileUtilProxy::Touch( | 163 base::FileUtilProxy::Touch( |
| 161 proxy_, path, last_access_time, last_modified_time, | 164 proxy_, path, last_access_time, last_modified_time, |
| 162 callback_factory_.NewCallback(&FileSystemOperation::DidTouchFile)); | 165 callback_factory_.NewCallback(&FileSystemOperation::DidTouchFile)); |
| 163 } | 166 } |
| 164 | 167 |
| 165 // We can only get here on a write or truncate that's not yet completed. | 168 void FileSystemOperation::Cancel() { |
| 166 // We don't support cancelling any other operation at this time. | |
| 167 void FileSystemOperation::Cancel(FileSystemOperation* cancel_operation) { | |
| 168 #ifndef NDEBUG | 169 #ifndef NDEBUG |
| 169 DCHECK(kOperationTruncate == pending_operation_); | 170 DCHECK(operation_pending_); |
| 170 // FIXME(ericu): Cancelling for writes coming soon. | |
| 171 #endif | 171 #endif |
| 172 // We're cancelling a truncate operation, but we can't actually stop it | 172 NOTREACHED(); |
| 173 // since it's been proxied to another thread. We need to save the | 173 // TODO(ericu): |
| 174 // cancel_operation so that when the truncate returns, it can see that it's | 174 // Make sure this was done on a FileSystemOperation used for a Write. |
| 175 // been cancelled, report it, and report that the cancel has succeeded. | 175 // Then set a flag that ensures that the Write loop will exit without |
| 176 cancel_operation_ = cancel_operation; | 176 // reporting any more progress, with a failure notification. |
| 177 } | 177 } |
| 178 | 178 |
| 179 void FileSystemOperation::DidCreateFileExclusive( | 179 void FileSystemOperation::DidCreateFileExclusive( |
| 180 base::PlatformFileError rv, | 180 base::PlatformFileError rv, |
| 181 base::PassPlatformFile file, | 181 base::PassPlatformFile file, |
| 182 bool created) { | 182 bool created) { |
| 183 DidFinishFileOperation(rv); | 183 DidFinishFileOperation(rv); |
| 184 } | 184 } |
| 185 | 185 |
| 186 void FileSystemOperation::DidCreateFileNonExclusive( | 186 void FileSystemOperation::DidCreateFileNonExclusive( |
| 187 base::PlatformFileError rv, | 187 base::PlatformFileError rv, |
| 188 base::PassPlatformFile file, | 188 base::PassPlatformFile file, |
| 189 bool created) { | 189 bool created) { |
| 190 // Suppress the already exists error and report success. | 190 // Suppress the already exists error and report success. |
| 191 if (rv == base::PLATFORM_FILE_OK || | 191 if (rv == base::PLATFORM_FILE_OK || |
| 192 rv == base::PLATFORM_FILE_ERROR_EXISTS) | 192 rv == base::PLATFORM_FILE_ERROR_EXISTS) |
| 193 dispatcher_->DidSucceed(); | 193 dispatcher_->DidSucceed(); |
| 194 else | 194 else |
| 195 dispatcher_->DidFail(rv); | 195 dispatcher_->DidFail(rv); |
| 196 } | 196 } |
| 197 | 197 |
| 198 void FileSystemOperation::DidFinishFileOperation( | 198 void FileSystemOperation::DidFinishFileOperation( |
| 199 base::PlatformFileError rv) { | 199 base::PlatformFileError rv) { |
| 200 if (cancel_operation_) { | 200 if (rv == base::PLATFORM_FILE_OK) |
| 201 #ifndef NDEBUG | |
| 202 DCHECK(kOperationTruncate == pending_operation_); | |
| 203 #endif | |
| 204 FileSystemOperation *cancel_op = cancel_operation_; | |
| 205 // This call deletes us, so we have to extract cancel_op first. | |
| 206 dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_ABORT); | |
| 207 cancel_op->dispatcher_->DidSucceed(); | |
| 208 } else if (rv == base::PLATFORM_FILE_OK) { | |
| 209 dispatcher_->DidSucceed(); | 201 dispatcher_->DidSucceed(); |
| 210 } else { | 202 else |
| 211 dispatcher_->DidFail(rv); | 203 dispatcher_->DidFail(rv); |
| 212 } | |
| 213 } | 204 } |
| 214 | 205 |
| 215 void FileSystemOperation::DidDirectoryExists( | 206 void FileSystemOperation::DidDirectoryExists( |
| 216 base::PlatformFileError rv, | 207 base::PlatformFileError rv, |
| 217 const base::PlatformFileInfo& file_info) { | 208 const base::PlatformFileInfo& file_info) { |
| 218 if (rv == base::PLATFORM_FILE_OK) { | 209 if (rv == base::PLATFORM_FILE_OK) { |
| 219 if (file_info.is_directory) | 210 if (file_info.is_directory) |
| 220 dispatcher_->DidSucceed(); | 211 dispatcher_->DidSucceed(); |
| 221 else | 212 else |
| 222 dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_FAILED); | 213 dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_FAILED); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 252 dispatcher_->DidReadDirectory(entries, false /* has_more */); | 243 dispatcher_->DidReadDirectory(entries, false /* has_more */); |
| 253 else | 244 else |
| 254 dispatcher_->DidFail(rv); | 245 dispatcher_->DidFail(rv); |
| 255 } | 246 } |
| 256 | 247 |
| 257 void FileSystemOperation::DidWrite( | 248 void FileSystemOperation::DidWrite( |
| 258 base::PlatformFileError rv, | 249 base::PlatformFileError rv, |
| 259 int64 bytes, | 250 int64 bytes, |
| 260 bool complete) { | 251 bool complete) { |
| 261 if (rv == base::PLATFORM_FILE_OK) | 252 if (rv == base::PLATFORM_FILE_OK) |
| 262 dispatcher_->DidWrite(bytes, complete); | 253 /* dispatcher_->DidWrite(bytes, complete) TODO(ericu): Coming soon. */ {} |
| 263 else | 254 else |
| 264 dispatcher_->DidFail(rv); | 255 dispatcher_->DidFail(rv); |
| 265 } | 256 } |
| 266 | 257 |
| 267 void FileSystemOperation::DidTouchFile(base::PlatformFileError rv) { | 258 void FileSystemOperation::DidTouchFile(base::PlatformFileError rv) { |
| 268 if (rv == base::PLATFORM_FILE_OK) | 259 if (rv == base::PLATFORM_FILE_OK) |
| 269 dispatcher_->DidSucceed(); | 260 dispatcher_->DidSucceed(); |
| 270 else | 261 else |
| 271 dispatcher_->DidFail(rv); | 262 dispatcher_->DidFail(rv); |
| 272 } | 263 } |
| 273 | 264 |
| 274 } // namespace fileapi | 265 } // namespace fileapi |
| OLD | NEW |