| 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) { |
| 19 DCHECK(dispatcher); | 20 DCHECK(dispatcher); |
| 20 #ifndef NDEBUG | 21 #ifndef NDEBUG |
| 21 operation_pending_ = false; | 22 pending_operation_ = kOperationNone; |
| 22 #endif | 23 #endif |
| 23 } | 24 } |
| 24 | 25 |
| 25 FileSystemOperation::~FileSystemOperation() { | 26 FileSystemOperation::~FileSystemOperation() { |
| 26 } | 27 } |
| 27 | 28 |
| 28 void FileSystemOperation::CreateFile(const FilePath& path, | 29 void FileSystemOperation::CreateFile(const FilePath& path, |
| 29 bool exclusive) { | 30 bool exclusive) { |
| 30 #ifndef NDEBUG | 31 #ifndef NDEBUG |
| 31 DCHECK(!operation_pending_); | 32 DCHECK(kOperationNone == pending_operation_); |
| 32 operation_pending_ = true; | 33 pending_operation_ = kOperationCreateFile; |
| 33 #endif | 34 #endif |
| 34 | 35 |
| 35 base::FileUtilProxy::CreateOrOpen( | 36 base::FileUtilProxy::CreateOrOpen( |
| 36 proxy_, path, base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, | 37 proxy_, path, base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, |
| 37 callback_factory_.NewCallback( | 38 callback_factory_.NewCallback( |
| 38 exclusive ? &FileSystemOperation::DidCreateFileExclusive | 39 exclusive ? &FileSystemOperation::DidCreateFileExclusive |
| 39 : &FileSystemOperation::DidCreateFileNonExclusive)); | 40 : &FileSystemOperation::DidCreateFileNonExclusive)); |
| 40 } | 41 } |
| 41 | 42 |
| 42 void FileSystemOperation::CreateDirectory(const FilePath& path, | 43 void FileSystemOperation::CreateDirectory(const FilePath& path, |
| 43 bool exclusive, | 44 bool exclusive, |
| 44 bool recursive) { | 45 bool recursive) { |
| 45 #ifndef NDEBUG | 46 #ifndef NDEBUG |
| 46 DCHECK(!operation_pending_); | 47 DCHECK(kOperationNone == pending_operation_); |
| 47 operation_pending_ = true; | 48 pending_operation_ = kOperationCreateDirectory; |
| 48 #endif | 49 #endif |
| 49 | 50 |
| 50 base::FileUtilProxy::CreateDirectory( | 51 base::FileUtilProxy::CreateDirectory( |
| 51 proxy_, path, exclusive, recursive, callback_factory_.NewCallback( | 52 proxy_, path, exclusive, recursive, callback_factory_.NewCallback( |
| 52 &FileSystemOperation::DidFinishFileOperation)); | 53 &FileSystemOperation::DidFinishFileOperation)); |
| 53 } | 54 } |
| 54 | 55 |
| 55 void FileSystemOperation::Copy(const FilePath& src_path, | 56 void FileSystemOperation::Copy(const FilePath& src_path, |
| 56 const FilePath& dest_path) { | 57 const FilePath& dest_path) { |
| 57 #ifndef NDEBUG | 58 #ifndef NDEBUG |
| 58 DCHECK(!operation_pending_); | 59 DCHECK(kOperationNone == pending_operation_); |
| 59 operation_pending_ = true; | 60 pending_operation_ = kOperationCopy; |
| 60 #endif | 61 #endif |
| 61 | 62 |
| 62 base::FileUtilProxy::Copy(proxy_, src_path, dest_path, | 63 base::FileUtilProxy::Copy(proxy_, src_path, dest_path, |
| 63 callback_factory_.NewCallback( | 64 callback_factory_.NewCallback( |
| 64 &FileSystemOperation::DidFinishFileOperation)); | 65 &FileSystemOperation::DidFinishFileOperation)); |
| 65 } | 66 } |
| 66 | 67 |
| 67 void FileSystemOperation::Move(const FilePath& src_path, | 68 void FileSystemOperation::Move(const FilePath& src_path, |
| 68 const FilePath& dest_path) { | 69 const FilePath& dest_path) { |
| 69 #ifndef NDEBUG | 70 #ifndef NDEBUG |
| 70 DCHECK(!operation_pending_); | 71 DCHECK(kOperationNone == pending_operation_); |
| 71 operation_pending_ = true; | 72 pending_operation_ = kOperationMove; |
| 72 #endif | 73 #endif |
| 73 | 74 |
| 74 base::FileUtilProxy::Move(proxy_, src_path, dest_path, | 75 base::FileUtilProxy::Move(proxy_, src_path, dest_path, |
| 75 callback_factory_.NewCallback( | 76 callback_factory_.NewCallback( |
| 76 &FileSystemOperation::DidFinishFileOperation)); | 77 &FileSystemOperation::DidFinishFileOperation)); |
| 77 } | 78 } |
| 78 | 79 |
| 79 void FileSystemOperation::DirectoryExists(const FilePath& path) { | 80 void FileSystemOperation::DirectoryExists(const FilePath& path) { |
| 80 #ifndef NDEBUG | 81 #ifndef NDEBUG |
| 81 DCHECK(!operation_pending_); | 82 DCHECK(kOperationNone == pending_operation_); |
| 82 operation_pending_ = true; | 83 pending_operation_ = kOperationDirectoryExists; |
| 83 #endif | 84 #endif |
| 84 | 85 |
| 85 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( | 86 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( |
| 86 &FileSystemOperation::DidDirectoryExists)); | 87 &FileSystemOperation::DidDirectoryExists)); |
| 87 } | 88 } |
| 88 | 89 |
| 89 void FileSystemOperation::FileExists(const FilePath& path) { | 90 void FileSystemOperation::FileExists(const FilePath& path) { |
| 90 #ifndef NDEBUG | 91 #ifndef NDEBUG |
| 91 DCHECK(!operation_pending_); | 92 DCHECK(kOperationNone == pending_operation_); |
| 92 operation_pending_ = true; | 93 pending_operation_ = kOperationFileExists; |
| 93 #endif | 94 #endif |
| 94 | 95 |
| 95 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( | 96 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( |
| 96 &FileSystemOperation::DidFileExists)); | 97 &FileSystemOperation::DidFileExists)); |
| 97 } | 98 } |
| 98 | 99 |
| 99 void FileSystemOperation::GetMetadata(const FilePath& path) { | 100 void FileSystemOperation::GetMetadata(const FilePath& path) { |
| 100 #ifndef NDEBUG | 101 #ifndef NDEBUG |
| 101 DCHECK(!operation_pending_); | 102 DCHECK(kOperationNone == pending_operation_); |
| 102 operation_pending_ = true; | 103 pending_operation_ = kOperationGetMetadata; |
| 103 #endif | 104 #endif |
| 104 | 105 |
| 105 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( | 106 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( |
| 106 &FileSystemOperation::DidGetMetadata)); | 107 &FileSystemOperation::DidGetMetadata)); |
| 107 } | 108 } |
| 108 | 109 |
| 109 void FileSystemOperation::ReadDirectory(const FilePath& path) { | 110 void FileSystemOperation::ReadDirectory(const FilePath& path) { |
| 110 #ifndef NDEBUG | 111 #ifndef NDEBUG |
| 111 DCHECK(!operation_pending_); | 112 DCHECK(kOperationNone == pending_operation_); |
| 112 operation_pending_ = true; | 113 pending_operation_ = kOperationReadDirectory; |
| 113 #endif | 114 #endif |
| 114 | 115 |
| 115 base::FileUtilProxy::ReadDirectory(proxy_, path, | 116 base::FileUtilProxy::ReadDirectory(proxy_, path, |
| 116 callback_factory_.NewCallback( | 117 callback_factory_.NewCallback( |
| 117 &FileSystemOperation::DidReadDirectory)); | 118 &FileSystemOperation::DidReadDirectory)); |
| 118 } | 119 } |
| 119 | 120 |
| 120 void FileSystemOperation::Remove(const FilePath& path) { | 121 void FileSystemOperation::Remove(const FilePath& path) { |
| 121 #ifndef NDEBUG | 122 #ifndef NDEBUG |
| 122 DCHECK(!operation_pending_); | 123 DCHECK(kOperationNone == pending_operation_); |
| 123 operation_pending_ = true; | 124 pending_operation_ = kOperationRemove; |
| 124 #endif | 125 #endif |
| 125 | 126 |
| 126 base::FileUtilProxy::Delete(proxy_, path, callback_factory_.NewCallback( | 127 base::FileUtilProxy::Delete(proxy_, path, callback_factory_.NewCallback( |
| 127 &FileSystemOperation::DidFinishFileOperation)); | 128 &FileSystemOperation::DidFinishFileOperation)); |
| 128 } | 129 } |
| 129 | 130 |
| 130 void FileSystemOperation::Write( | 131 void FileSystemOperation::Write( |
| 131 const FilePath&, | 132 const FilePath& path, |
| 132 const GURL&, | 133 const GURL& blob_url, |
| 133 int64) { | 134 int64 offset) { |
| 134 #ifndef NDEBUG | 135 #ifndef NDEBUG |
| 135 DCHECK(!operation_pending_); | 136 DCHECK(kOperationNone == pending_operation_); |
| 136 operation_pending_ = true; | 137 pending_operation_ = kOperationWrite; |
| 137 #endif | 138 #endif |
| 138 NOTREACHED(); | 139 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. | |
| 144 } | 140 } |
| 145 | 141 |
| 146 void FileSystemOperation::Truncate(const FilePath& path, int64 length) { | 142 void FileSystemOperation::Truncate(const FilePath& path, int64 length) { |
| 147 #ifndef NDEBUG | 143 #ifndef NDEBUG |
| 148 DCHECK(!operation_pending_); | 144 DCHECK(kOperationNone == pending_operation_); |
| 149 operation_pending_ = true; | 145 pending_operation_ = kOperationTruncate; |
| 150 #endif | 146 #endif |
| 151 // TODO(ericu): | 147 base::FileUtilProxy::Truncate(proxy_, path, length, |
| 152 NOTREACHED(); | 148 callback_factory_.NewCallback( |
| 149 &FileSystemOperation::DidFinishFileOperation)); |
| 153 } | 150 } |
| 154 | 151 |
| 155 void FileSystemOperation::TouchFile(const FilePath& path, | 152 void FileSystemOperation::TouchFile(const FilePath& path, |
| 156 const base::Time& last_access_time, | 153 const base::Time& last_access_time, |
| 157 const base::Time& last_modified_time) { | 154 const base::Time& last_modified_time) { |
| 158 #ifndef NDEBUG | 155 #ifndef NDEBUG |
| 159 DCHECK(!operation_pending_); | 156 DCHECK(kOperationNone == pending_operation_); |
| 160 operation_pending_ = true; | 157 pending_operation_ = kOperationTouchFile; |
| 161 #endif | 158 #endif |
| 162 | 159 |
| 163 base::FileUtilProxy::Touch( | 160 base::FileUtilProxy::Touch( |
| 164 proxy_, path, last_access_time, last_modified_time, | 161 proxy_, path, last_access_time, last_modified_time, |
| 165 callback_factory_.NewCallback(&FileSystemOperation::DidTouchFile)); | 162 callback_factory_.NewCallback(&FileSystemOperation::DidTouchFile)); |
| 166 } | 163 } |
| 167 | 164 |
| 168 void FileSystemOperation::Cancel() { | 165 // We can only get here on a write or truncate that's not yet completed. |
| 166 // We don't support cancelling any other operation at this time. |
| 167 void FileSystemOperation::Cancel(FileSystemOperation* cancel_operation) { |
| 169 #ifndef NDEBUG | 168 #ifndef NDEBUG |
| 170 DCHECK(operation_pending_); | 169 DCHECK(kOperationTruncate == pending_operation_); |
| 170 // FIXME(ericu): Cancelling for writes coming soon. |
| 171 #endif | 171 #endif |
| 172 NOTREACHED(); | 172 // We're cancelling a truncate operation, but we can't actually stop it |
| 173 // TODO(ericu): | 173 // since it's been proxied to another thread. We need to save the |
| 174 // Make sure this was done on a FileSystemOperation used for a Write. | 174 // cancel_operation so that when the truncate returns, it can see that it's |
| 175 // Then set a flag that ensures that the Write loop will exit without | 175 // been cancelled, report it, and report that the cancel has succeeded. |
| 176 // reporting any more progress, with a failure notification. | 176 cancel_operation_ = cancel_operation; |
| 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 (rv == base::PLATFORM_FILE_OK) | 200 if (cancel_operation_) { |
| 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) { |
| 201 dispatcher_->DidSucceed(); | 209 dispatcher_->DidSucceed(); |
| 202 else | 210 } else { |
| 203 dispatcher_->DidFail(rv); | 211 dispatcher_->DidFail(rv); |
| 212 } |
| 204 } | 213 } |
| 205 | 214 |
| 206 void FileSystemOperation::DidDirectoryExists( | 215 void FileSystemOperation::DidDirectoryExists( |
| 207 base::PlatformFileError rv, | 216 base::PlatformFileError rv, |
| 208 const base::PlatformFileInfo& file_info) { | 217 const base::PlatformFileInfo& file_info) { |
| 209 if (rv == base::PLATFORM_FILE_OK) { | 218 if (rv == base::PLATFORM_FILE_OK) { |
| 210 if (file_info.is_directory) | 219 if (file_info.is_directory) |
| 211 dispatcher_->DidSucceed(); | 220 dispatcher_->DidSucceed(); |
| 212 else | 221 else |
| 213 dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_FAILED); | 222 dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_FAILED); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 243 dispatcher_->DidReadDirectory(entries, false /* has_more */); | 252 dispatcher_->DidReadDirectory(entries, false /* has_more */); |
| 244 else | 253 else |
| 245 dispatcher_->DidFail(rv); | 254 dispatcher_->DidFail(rv); |
| 246 } | 255 } |
| 247 | 256 |
| 248 void FileSystemOperation::DidWrite( | 257 void FileSystemOperation::DidWrite( |
| 249 base::PlatformFileError rv, | 258 base::PlatformFileError rv, |
| 250 int64 bytes, | 259 int64 bytes, |
| 251 bool complete) { | 260 bool complete) { |
| 252 if (rv == base::PLATFORM_FILE_OK) | 261 if (rv == base::PLATFORM_FILE_OK) |
| 253 /* dispatcher_->DidWrite(bytes, complete) TODO(ericu): Coming soon. */ {} | 262 dispatcher_->DidWrite(bytes, complete); |
| 254 else | 263 else |
| 255 dispatcher_->DidFail(rv); | 264 dispatcher_->DidFail(rv); |
| 256 } | 265 } |
| 257 | 266 |
| 258 void FileSystemOperation::DidTouchFile(base::PlatformFileError rv) { | 267 void FileSystemOperation::DidTouchFile(base::PlatformFileError rv) { |
| 259 if (rv == base::PLATFORM_FILE_OK) | 268 if (rv == base::PLATFORM_FILE_OK) |
| 260 dispatcher_->DidSucceed(); | 269 dispatcher_->DidSucceed(); |
| 261 else | 270 else |
| 262 dispatcher_->DidFail(rv); | 271 dispatcher_->DidFail(rv); |
| 263 } | 272 } |
| 264 | 273 |
| 265 } // namespace fileapi | 274 } // namespace fileapi |
| OLD | NEW |