| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/url_request/url_request_context.h" | 8 #include "net/url_request/url_request_context.h" |
| 9 #include "webkit/fileapi/file_system_callback_dispatcher.h" | 9 #include "webkit/fileapi/file_system_callback_dispatcher.h" |
| 10 #include "webkit/fileapi/file_system_context.h" | 10 #include "webkit/fileapi/file_system_context.h" |
| 11 #include "webkit/fileapi/file_system_file_util_proxy.h" |
| 12 #include "webkit/fileapi/file_system_operation_context.h" |
| 11 #include "webkit/fileapi/file_system_path_manager.h" | 13 #include "webkit/fileapi/file_system_path_manager.h" |
| 12 #include "webkit/fileapi/file_system_quota_manager.h" | 14 #include "webkit/fileapi/file_system_quota_manager.h" |
| 13 #include "webkit/fileapi/file_writer_delegate.h" | 15 #include "webkit/fileapi/file_writer_delegate.h" |
| 14 | 16 |
| 15 namespace fileapi { | 17 namespace fileapi { |
| 16 | 18 |
| 17 FileSystemOperation::FileSystemOperation( | 19 FileSystemOperation::FileSystemOperation( |
| 18 FileSystemCallbackDispatcher* dispatcher, | 20 FileSystemCallbackDispatcher* dispatcher, |
| 19 scoped_refptr<base::MessageLoopProxy> proxy, | 21 scoped_refptr<base::MessageLoopProxy> proxy, |
| 20 FileSystemContext* file_system_context) | 22 FileSystemContext* file_system_context) |
| 21 : proxy_(proxy), | 23 : proxy_(proxy), |
| 22 dispatcher_(dispatcher), | 24 dispatcher_(dispatcher), |
| 23 file_system_context_(file_system_context), | 25 file_system_context_(file_system_context), |
| 26 file_system_operation_context_(new FileSystemOperationContext( |
| 27 FileSystemFileUtil::GetInstance())), |
| 24 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 28 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 25 DCHECK(dispatcher); | 29 DCHECK(dispatcher); |
| 26 #ifndef NDEBUG | 30 #ifndef NDEBUG |
| 27 pending_operation_ = kOperationNone; | 31 pending_operation_ = kOperationNone; |
| 28 #endif | 32 #endif |
| 29 } | 33 } |
| 30 | 34 |
| 31 FileSystemOperation::~FileSystemOperation() { | 35 FileSystemOperation::~FileSystemOperation() { |
| 32 if (file_writer_delegate_.get()) | 36 if (file_writer_delegate_.get()) |
| 33 base::FileUtilProxy::Close(proxy_, file_writer_delegate_->file(), NULL); | 37 FileSystemFileUtilProxy::Close( |
| 38 *file_system_operation_context_, |
| 39 proxy_, file_writer_delegate_->file(), NULL); |
| 34 } | 40 } |
| 35 | 41 |
| 36 void FileSystemOperation::OpenFileSystem( | 42 void FileSystemOperation::OpenFileSystem( |
| 37 const GURL& origin_url, fileapi::FileSystemType type, bool create) { | 43 const GURL& origin_url, fileapi::FileSystemType type, bool create) { |
| 38 #ifndef NDEBUG | 44 #ifndef NDEBUG |
| 39 DCHECK(kOperationNone == pending_operation_); | 45 DCHECK(kOperationNone == pending_operation_); |
| 40 pending_operation_ = static_cast<FileSystemOperation::OperationType>( | 46 pending_operation_ = static_cast<FileSystemOperation::OperationType>( |
| 41 kOperationOpenFileSystem); | 47 kOperationOpenFileSystem); |
| 42 #endif | 48 #endif |
| 43 | 49 |
| 44 DCHECK(file_system_context_.get()); | 50 DCHECK(file_system_context_.get()); |
| 45 file_system_context_->path_manager()->GetFileSystemRootPath( | 51 file_system_context_->path_manager()->GetFileSystemRootPath( |
| 46 origin_url, type, create, | 52 origin_url, type, create, |
| 47 callback_factory_.NewCallback(&FileSystemOperation::DidGetRootPath)); | 53 callback_factory_.NewCallback(&FileSystemOperation::DidGetRootPath)); |
| 48 } | 54 } |
| 49 | 55 |
| 50 void FileSystemOperation::CreateFile(const FilePath& path, | 56 void FileSystemOperation::CreateFile(const FilePath& path, |
| 51 bool exclusive) { | 57 bool exclusive) { |
| 52 #ifndef NDEBUG | 58 #ifndef NDEBUG |
| 53 DCHECK(kOperationNone == pending_operation_); | 59 DCHECK(kOperationNone == pending_operation_); |
| 54 pending_operation_ = kOperationCreateFile; | 60 pending_operation_ = kOperationCreateFile; |
| 55 #endif | 61 #endif |
| 56 | 62 |
| 57 if (!VerifyFileSystemPathForWrite(path, true /* create */, 0)) { | 63 if (!VerifyFileSystemPathForWrite(path, true /* create */, 0)) { |
| 58 delete this; | 64 delete this; |
| 59 return; | 65 return; |
| 60 } | 66 } |
| 61 base::FileUtilProxy::EnsureFileExists( | 67 FileSystemFileUtilProxy::EnsureFileExists( |
| 62 proxy_, path, callback_factory_.NewCallback( | 68 *file_system_operation_context_, |
| 63 exclusive ? &FileSystemOperation::DidEnsureFileExistsExclusive | 69 proxy_, path, callback_factory_.NewCallback( |
| 64 : &FileSystemOperation::DidEnsureFileExistsNonExclusive)); | 70 exclusive ? &FileSystemOperation::DidEnsureFileExistsExclusive |
| 71 : &FileSystemOperation::DidEnsureFileExistsNonExclusive)); |
| 65 } | 72 } |
| 66 | 73 |
| 67 void FileSystemOperation::CreateDirectory(const FilePath& path, | 74 void FileSystemOperation::CreateDirectory(const FilePath& path, |
| 68 bool exclusive, | 75 bool exclusive, |
| 69 bool recursive) { | 76 bool unused) { |
| 70 #ifndef NDEBUG | 77 #ifndef NDEBUG |
| 71 DCHECK(kOperationNone == pending_operation_); | 78 DCHECK(kOperationNone == pending_operation_); |
| 72 pending_operation_ = kOperationCreateDirectory; | 79 pending_operation_ = kOperationCreateDirectory; |
| 73 #endif | 80 #endif |
| 81 DCHECK(!unused); |
| 74 | 82 |
| 75 if (!VerifyFileSystemPathForWrite(path, true /* create */, 0)) { | 83 if (!VerifyFileSystemPathForWrite(path, true /* create */, 0)) { |
| 76 delete this; | 84 delete this; |
| 77 return; | 85 return; |
| 78 } | 86 } |
| 79 base::FileUtilProxy::CreateDirectory( | 87 FileSystemFileUtilProxy::CreateDirectory( |
| 80 proxy_, path, exclusive, recursive, callback_factory_.NewCallback( | 88 *file_system_operation_context_, |
| 89 proxy_, path, exclusive, callback_factory_.NewCallback( |
| 81 &FileSystemOperation::DidFinishFileOperation)); | 90 &FileSystemOperation::DidFinishFileOperation)); |
| 82 } | 91 } |
| 83 | 92 |
| 84 void FileSystemOperation::Copy(const FilePath& src_path, | 93 void FileSystemOperation::Copy(const FilePath& src_path, |
| 85 const FilePath& dest_path) { | 94 const FilePath& dest_path) { |
| 86 #ifndef NDEBUG | 95 #ifndef NDEBUG |
| 87 DCHECK(kOperationNone == pending_operation_); | 96 DCHECK(kOperationNone == pending_operation_); |
| 88 pending_operation_ = kOperationCopy; | 97 pending_operation_ = kOperationCopy; |
| 89 #endif | 98 #endif |
| 90 | 99 |
| 91 if (!VerifyFileSystemPathForRead(src_path) || | 100 if (!VerifyFileSystemPathForRead(src_path) || |
| 92 !VerifyFileSystemPathForWrite(dest_path, true /* create */, | 101 !VerifyFileSystemPathForWrite(dest_path, true /* create */, |
| 93 FileSystemQuotaManager::kUnknownSize)) { | 102 FileSystemQuotaManager::kUnknownSize)) { |
| 94 delete this; | 103 delete this; |
| 95 return; | 104 return; |
| 96 } | 105 } |
| 97 base::FileUtilProxy::Copy(proxy_, src_path, dest_path, | 106 FileSystemFileUtilProxy::Copy( |
| 98 callback_factory_.NewCallback( | 107 *file_system_operation_context_, |
| 108 proxy_, src_path, dest_path, callback_factory_.NewCallback( |
| 99 &FileSystemOperation::DidFinishFileOperation)); | 109 &FileSystemOperation::DidFinishFileOperation)); |
| 100 } | 110 } |
| 101 | 111 |
| 102 void FileSystemOperation::Move(const FilePath& src_path, | 112 void FileSystemOperation::Move(const FilePath& src_path, |
| 103 const FilePath& dest_path) { | 113 const FilePath& dest_path) { |
| 104 #ifndef NDEBUG | 114 #ifndef NDEBUG |
| 105 DCHECK(kOperationNone == pending_operation_); | 115 DCHECK(kOperationNone == pending_operation_); |
| 106 pending_operation_ = kOperationMove; | 116 pending_operation_ = kOperationMove; |
| 107 #endif | 117 #endif |
| 108 | 118 |
| 109 if (!VerifyFileSystemPathForRead(src_path) || | 119 if (!VerifyFileSystemPathForRead(src_path) || |
| 110 !VerifyFileSystemPathForWrite(dest_path, true /* create */, | 120 !VerifyFileSystemPathForWrite(dest_path, true /* create */, |
| 111 FileSystemQuotaManager::kUnknownSize)) { | 121 FileSystemQuotaManager::kUnknownSize)) { |
| 112 delete this; | 122 delete this; |
| 113 return; | 123 return; |
| 114 } | 124 } |
| 115 base::FileUtilProxy::Move(proxy_, src_path, dest_path, | 125 FileSystemFileUtilProxy::Move( |
| 116 callback_factory_.NewCallback( | 126 *file_system_operation_context_, |
| 127 proxy_, src_path, dest_path, callback_factory_.NewCallback( |
| 117 &FileSystemOperation::DidFinishFileOperation)); | 128 &FileSystemOperation::DidFinishFileOperation)); |
| 118 } | 129 } |
| 119 | 130 |
| 120 void FileSystemOperation::DirectoryExists(const FilePath& path) { | 131 void FileSystemOperation::DirectoryExists(const FilePath& path) { |
| 121 #ifndef NDEBUG | 132 #ifndef NDEBUG |
| 122 DCHECK(kOperationNone == pending_operation_); | 133 DCHECK(kOperationNone == pending_operation_); |
| 123 pending_operation_ = kOperationDirectoryExists; | 134 pending_operation_ = kOperationDirectoryExists; |
| 124 #endif | 135 #endif |
| 125 | 136 |
| 126 if (!VerifyFileSystemPathForRead(path)) { | 137 if (!VerifyFileSystemPathForRead(path)) { |
| 127 delete this; | 138 delete this; |
| 128 return; | 139 return; |
| 129 } | 140 } |
| 130 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( | 141 FileSystemFileUtilProxy::GetFileInfo( |
| 131 &FileSystemOperation::DidDirectoryExists)); | 142 *file_system_operation_context_, |
| 143 proxy_, path, callback_factory_.NewCallback( |
| 144 &FileSystemOperation::DidDirectoryExists)); |
| 132 } | 145 } |
| 133 | 146 |
| 134 void FileSystemOperation::FileExists(const FilePath& path) { | 147 void FileSystemOperation::FileExists(const FilePath& path) { |
| 135 #ifndef NDEBUG | 148 #ifndef NDEBUG |
| 136 DCHECK(kOperationNone == pending_operation_); | 149 DCHECK(kOperationNone == pending_operation_); |
| 137 pending_operation_ = kOperationFileExists; | 150 pending_operation_ = kOperationFileExists; |
| 138 #endif | 151 #endif |
| 139 | 152 |
| 140 if (!VerifyFileSystemPathForRead(path)) { | 153 if (!VerifyFileSystemPathForRead(path)) { |
| 141 delete this; | 154 delete this; |
| 142 return; | 155 return; |
| 143 } | 156 } |
| 144 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( | 157 FileSystemFileUtilProxy::GetFileInfo( |
| 145 &FileSystemOperation::DidFileExists)); | 158 *file_system_operation_context_, |
| 159 proxy_, path, callback_factory_.NewCallback( |
| 160 &FileSystemOperation::DidFileExists)); |
| 146 } | 161 } |
| 147 | 162 |
| 148 void FileSystemOperation::GetMetadata(const FilePath& path) { | 163 void FileSystemOperation::GetMetadata(const FilePath& path) { |
| 149 #ifndef NDEBUG | 164 #ifndef NDEBUG |
| 150 DCHECK(kOperationNone == pending_operation_); | 165 DCHECK(kOperationNone == pending_operation_); |
| 151 pending_operation_ = kOperationGetMetadata; | 166 pending_operation_ = kOperationGetMetadata; |
| 152 #endif | 167 #endif |
| 153 | 168 |
| 154 if (!VerifyFileSystemPathForRead(path)) { | 169 if (!VerifyFileSystemPathForRead(path)) { |
| 155 delete this; | 170 delete this; |
| 156 return; | 171 return; |
| 157 } | 172 } |
| 158 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( | 173 FileSystemFileUtilProxy::GetFileInfo( |
| 159 &FileSystemOperation::DidGetMetadata)); | 174 *file_system_operation_context_, |
| 175 proxy_, path, callback_factory_.NewCallback( |
| 176 &FileSystemOperation::DidGetMetadata)); |
| 160 } | 177 } |
| 161 | 178 |
| 162 void FileSystemOperation::ReadDirectory(const FilePath& path) { | 179 void FileSystemOperation::ReadDirectory(const FilePath& path) { |
| 163 #ifndef NDEBUG | 180 #ifndef NDEBUG |
| 164 DCHECK(kOperationNone == pending_operation_); | 181 DCHECK(kOperationNone == pending_operation_); |
| 165 pending_operation_ = kOperationReadDirectory; | 182 pending_operation_ = kOperationReadDirectory; |
| 166 #endif | 183 #endif |
| 167 | 184 |
| 168 if (!VerifyFileSystemPathForRead(path)) { | 185 if (!VerifyFileSystemPathForRead(path)) { |
| 169 delete this; | 186 delete this; |
| 170 return; | 187 return; |
| 171 } | 188 } |
| 172 base::FileUtilProxy::ReadDirectory(proxy_, path, | 189 FileSystemFileUtilProxy::ReadDirectory( |
| 173 callback_factory_.NewCallback( | 190 *file_system_operation_context_, |
| 191 proxy_, path, callback_factory_.NewCallback( |
| 174 &FileSystemOperation::DidReadDirectory)); | 192 &FileSystemOperation::DidReadDirectory)); |
| 175 } | 193 } |
| 176 | 194 |
| 177 void FileSystemOperation::Remove(const FilePath& path, bool recursive) { | 195 void FileSystemOperation::Remove(const FilePath& path, bool recursive) { |
| 178 #ifndef NDEBUG | 196 #ifndef NDEBUG |
| 179 DCHECK(kOperationNone == pending_operation_); | 197 DCHECK(kOperationNone == pending_operation_); |
| 180 pending_operation_ = kOperationRemove; | 198 pending_operation_ = kOperationRemove; |
| 181 #endif | 199 #endif |
| 182 | 200 |
| 183 if (!VerifyFileSystemPathForWrite(path, false /* create */, 0)) { | 201 if (!VerifyFileSystemPathForWrite(path, false /* create */, 0)) { |
| 184 delete this; | 202 delete this; |
| 185 return; | 203 return; |
| 186 } | 204 } |
| 187 base::FileUtilProxy::Delete(proxy_, path, recursive, | 205 FileSystemFileUtilProxy::Delete( |
| 188 callback_factory_.NewCallback( | 206 *file_system_operation_context_, |
| 207 proxy_, path, recursive, callback_factory_.NewCallback( |
| 189 &FileSystemOperation::DidFinishFileOperation)); | 208 &FileSystemOperation::DidFinishFileOperation)); |
| 190 } | 209 } |
| 191 | 210 |
| 192 void FileSystemOperation::Write( | 211 void FileSystemOperation::Write( |
| 193 scoped_refptr<net::URLRequestContext> url_request_context, | 212 scoped_refptr<net::URLRequestContext> url_request_context, |
| 194 const FilePath& path, | 213 const FilePath& path, |
| 195 const GURL& blob_url, | 214 const GURL& blob_url, |
| 196 int64 offset) { | 215 int64 offset) { |
| 197 #ifndef NDEBUG | 216 #ifndef NDEBUG |
| 198 DCHECK(kOperationNone == pending_operation_); | 217 DCHECK(kOperationNone == pending_operation_); |
| 199 pending_operation_ = kOperationWrite; | 218 pending_operation_ = kOperationWrite; |
| 200 #endif | 219 #endif |
| 201 if (!VerifyFileSystemPathForWrite(path, true /* create */, | 220 if (!VerifyFileSystemPathForWrite(path, true /* create */, |
| 202 FileSystemQuotaManager::kUnknownSize)) { | 221 FileSystemQuotaManager::kUnknownSize)) { |
| 203 delete this; | 222 delete this; |
| 204 return; | 223 return; |
| 205 } | 224 } |
| 206 DCHECK(blob_url.is_valid()); | 225 DCHECK(blob_url.is_valid()); |
| 207 file_writer_delegate_.reset(new FileWriterDelegate(this, offset)); | 226 file_writer_delegate_.reset(new FileWriterDelegate(this, offset)); |
| 208 blob_request_.reset( | 227 blob_request_.reset( |
| 209 new net::URLRequest(blob_url, file_writer_delegate_.get())); | 228 new net::URLRequest(blob_url, file_writer_delegate_.get())); |
| 210 blob_request_->set_context(url_request_context); | 229 blob_request_->set_context(url_request_context); |
| 211 base::FileUtilProxy::CreateOrOpen( | 230 FileSystemFileUtilProxy::CreateOrOpen( |
| 231 *file_system_operation_context_, |
| 212 proxy_, | 232 proxy_, |
| 213 path, | 233 path, |
| 214 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | | 234 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | |
| 215 base::PLATFORM_FILE_ASYNC, | 235 base::PLATFORM_FILE_ASYNC, |
| 216 callback_factory_.NewCallback( | 236 callback_factory_.NewCallback( |
| 217 &FileSystemOperation::OnFileOpenedForWrite)); | 237 &FileSystemOperation::OnFileOpenedForWrite)); |
| 218 } | 238 } |
| 219 | 239 |
| 220 void FileSystemOperation::Truncate(const FilePath& path, int64 length) { | 240 void FileSystemOperation::Truncate(const FilePath& path, int64 length) { |
| 221 #ifndef NDEBUG | 241 #ifndef NDEBUG |
| 222 DCHECK(kOperationNone == pending_operation_); | 242 DCHECK(kOperationNone == pending_operation_); |
| 223 pending_operation_ = kOperationTruncate; | 243 pending_operation_ = kOperationTruncate; |
| 224 #endif | 244 #endif |
| 225 if (!VerifyFileSystemPathForWrite(path, false /* create */, 0)) { | 245 if (!VerifyFileSystemPathForWrite(path, false /* create */, 0)) { |
| 226 delete this; | 246 delete this; |
| 227 return; | 247 return; |
| 228 } | 248 } |
| 229 base::FileUtilProxy::Truncate(proxy_, path, length, | 249 FileSystemFileUtilProxy::Truncate( |
| 230 callback_factory_.NewCallback( | 250 *file_system_operation_context_, |
| 251 proxy_, path, length, callback_factory_.NewCallback( |
| 231 &FileSystemOperation::DidFinishFileOperation)); | 252 &FileSystemOperation::DidFinishFileOperation)); |
| 232 } | 253 } |
| 233 | 254 |
| 234 void FileSystemOperation::TouchFile(const FilePath& path, | 255 void FileSystemOperation::TouchFile(const FilePath& path, |
| 235 const base::Time& last_access_time, | 256 const base::Time& last_access_time, |
| 236 const base::Time& last_modified_time) { | 257 const base::Time& last_modified_time) { |
| 237 #ifndef NDEBUG | 258 #ifndef NDEBUG |
| 238 DCHECK(kOperationNone == pending_operation_); | 259 DCHECK(kOperationNone == pending_operation_); |
| 239 pending_operation_ = kOperationTouchFile; | 260 pending_operation_ = kOperationTouchFile; |
| 240 #endif | 261 #endif |
| 241 | 262 |
| 242 if (!VerifyFileSystemPathForWrite(path, true /* create */, 0)) { | 263 if (!VerifyFileSystemPathForWrite(path, true /* create */, 0)) { |
| 243 delete this; | 264 delete this; |
| 244 return; | 265 return; |
| 245 } | 266 } |
| 246 base::FileUtilProxy::Touch( | 267 FileSystemFileUtilProxy::Touch( |
| 268 *file_system_operation_context_, |
| 247 proxy_, path, last_access_time, last_modified_time, | 269 proxy_, path, last_access_time, last_modified_time, |
| 248 callback_factory_.NewCallback(&FileSystemOperation::DidTouchFile)); | 270 callback_factory_.NewCallback(&FileSystemOperation::DidTouchFile)); |
| 249 } | 271 } |
| 250 | 272 |
| 251 // We can only get here on a write or truncate that's not yet completed. | 273 // We can only get here on a write or truncate that's not yet completed. |
| 252 // We don't support cancelling any other operation at this time. | 274 // We don't support cancelling any other operation at this time. |
| 253 void FileSystemOperation::Cancel(FileSystemOperation* cancel_operation_ptr) { | 275 void FileSystemOperation::Cancel(FileSystemOperation* cancel_operation_ptr) { |
| 254 scoped_ptr<FileSystemOperation> cancel_operation(cancel_operation_ptr); | 276 scoped_ptr<FileSystemOperation> cancel_operation(cancel_operation_ptr); |
| 255 if (file_writer_delegate_.get()) { | 277 if (file_writer_delegate_.get()) { |
| 256 #ifndef NDEBUG | 278 #ifndef NDEBUG |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 // need to resolve what amount of size it's going to write. | 466 // need to resolve what amount of size it's going to write. |
| 445 if (!file_system_context_->quota_manager()->CheckOriginQuota( | 467 if (!file_system_context_->quota_manager()->CheckOriginQuota( |
| 446 origin_url, growth)) { | 468 origin_url, growth)) { |
| 447 dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_NO_SPACE); | 469 dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_NO_SPACE); |
| 448 return false; | 470 return false; |
| 449 } | 471 } |
| 450 return true; | 472 return true; |
| 451 } | 473 } |
| 452 | 474 |
| 453 } // namespace fileapi | 475 } // namespace fileapi |
| OLD | NEW |