| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/sequenced_task_runner.h" | 8 #include "base/sequenced_task_runner.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "net/base/escape.h" | 11 #include "net/base/escape.h" |
| 12 #include "net/url_request/url_request_context.h" | 12 #include "net/url_request/url_request_context.h" |
| 13 #include "webkit/blob/shareable_file_reference.h" | 13 #include "webkit/blob/shareable_file_reference.h" |
| 14 #include "webkit/fileapi/file_system_context.h" | 14 #include "webkit/fileapi/file_system_context.h" |
| 15 #include "webkit/fileapi/file_system_file_util_proxy.h" | 15 #include "webkit/fileapi/file_system_file_util_proxy.h" |
| 16 #include "webkit/fileapi/file_system_mount_point_provider.h" | 16 #include "webkit/fileapi/file_system_mount_point_provider.h" |
| 17 #include "webkit/fileapi/file_system_operation_context.h" | 17 #include "webkit/fileapi/file_system_operation_context.h" |
| 18 #include "webkit/fileapi/file_system_quota_util.h" | 18 #include "webkit/fileapi/file_system_quota_util.h" |
| 19 #include "webkit/fileapi/file_system_types.h" | 19 #include "webkit/fileapi/file_system_types.h" |
| 20 #include "webkit/fileapi/file_system_util.h" | 20 #include "webkit/fileapi/file_system_util.h" |
| 21 #include "webkit/fileapi/file_writer_delegate.h" | 21 #include "webkit/fileapi/file_writer_delegate.h" |
| 22 #include "webkit/fileapi/sandbox_file_writer.h" |
| 22 #include "webkit/quota/quota_manager.h" | 23 #include "webkit/quota/quota_manager.h" |
| 23 #include "webkit/quota/quota_types.h" | 24 #include "webkit/quota/quota_types.h" |
| 24 | 25 |
| 25 namespace fileapi { | 26 namespace fileapi { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 void GetMetadataForSnapshot( | 30 void GetMetadataForSnapshot( |
| 30 const FileSystemOperationInterface::SnapshotFileCallback& callback, | 31 const FileSystemOperationInterface::SnapshotFileCallback& callback, |
| 31 base::PlatformFileError result, | 32 base::PlatformFileError result, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 } | 73 } |
| 73 } | 74 } |
| 74 | 75 |
| 75 FileSystemOperation::TaskParamsForDidGetQuota::TaskParamsForDidGetQuota() | 76 FileSystemOperation::TaskParamsForDidGetQuota::TaskParamsForDidGetQuota() |
| 76 : type(kFileSystemTypeUnknown) { | 77 : type(kFileSystemTypeUnknown) { |
| 77 } | 78 } |
| 78 | 79 |
| 79 FileSystemOperation::TaskParamsForDidGetQuota::~TaskParamsForDidGetQuota() {} | 80 FileSystemOperation::TaskParamsForDidGetQuota::~TaskParamsForDidGetQuota() {} |
| 80 | 81 |
| 81 FileSystemOperation::~FileSystemOperation() { | 82 FileSystemOperation::~FileSystemOperation() { |
| 82 if (file_writer_delegate_.get()) { | |
| 83 FileSystemOperationContext* c = | |
| 84 new FileSystemOperationContext(operation_context_); | |
| 85 base::FileUtilProxy::RelayClose( | |
| 86 file_system_context()->file_task_runner(), | |
| 87 base::Bind(&FileSystemFileUtil::Close, | |
| 88 base::Unretained(src_util_), | |
| 89 base::Owned(c)), | |
| 90 file_writer_delegate_->file(), | |
| 91 base::FileUtilProxy::StatusCallback()); | |
| 92 } | |
| 93 } | 83 } |
| 94 | 84 |
| 95 void FileSystemOperation::CreateFile(const GURL& path_url, | 85 void FileSystemOperation::CreateFile(const GURL& path_url, |
| 96 bool exclusive, | 86 bool exclusive, |
| 97 const StatusCallback& callback) { | 87 const StatusCallback& callback) { |
| 98 DCHECK(SetPendingOperationType(kOperationCreateFile)); | 88 DCHECK(SetPendingOperationType(kOperationCreateFile)); |
| 99 | 89 |
| 100 base::PlatformFileError result = SetUpFileSystemPath( | 90 base::PlatformFileError result = SetUpFileSystemPath( |
| 101 path_url, &src_path_, &src_util_, PATH_FOR_CREATE); | 91 path_url, &src_path_, &src_util_, PATH_FOR_CREATE); |
| 102 if (result != base::PLATFORM_FILE_OK) { | 92 if (result != base::PLATFORM_FILE_OK) { |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 | 271 |
| 282 base::PlatformFileError result = SetUpFileSystemPath( | 272 base::PlatformFileError result = SetUpFileSystemPath( |
| 283 path_url, &src_path_, &src_util_, PATH_FOR_WRITE); | 273 path_url, &src_path_, &src_util_, PATH_FOR_WRITE); |
| 284 if (result != base::PLATFORM_FILE_OK) { | 274 if (result != base::PLATFORM_FILE_OK) { |
| 285 callback.Run(result, 0, false); | 275 callback.Run(result, 0, false); |
| 286 delete this; | 276 delete this; |
| 287 return; | 277 return; |
| 288 } | 278 } |
| 289 DCHECK(blob_url.is_valid()); | 279 DCHECK(blob_url.is_valid()); |
| 290 file_writer_delegate_.reset(new FileWriterDelegate( | 280 file_writer_delegate_.reset(new FileWriterDelegate( |
| 291 this, src_path_, offset)); | 281 base::Bind(&FileSystemOperation::DidWrite, weak_factory_.GetWeakPtr()), |
| 282 scoped_ptr<FileWriter>( |
| 283 new SandboxFileWriter(file_system_context(), path_url, offset)))); |
| 292 set_write_callback(callback); | 284 set_write_callback(callback); |
| 293 scoped_ptr<net::URLRequest> blob_request( | 285 scoped_ptr<net::URLRequest> blob_request( |
| 294 new net::URLRequest(blob_url, file_writer_delegate_.get())); | 286 new net::URLRequest(blob_url, file_writer_delegate_.get())); |
| 295 blob_request->set_context(url_request_context); | 287 blob_request->set_context(url_request_context); |
| 296 | 288 |
| 297 GetUsageAndQuotaThenRunTask( | 289 file_writer_delegate_->Start(blob_request.Pass()); |
| 298 src_path_.origin(), src_path_.type(), | |
| 299 base::Bind(&FileSystemOperation::DoWrite, weak_factory_.GetWeakPtr(), | |
| 300 base::Passed(&blob_request)), | |
| 301 base::Bind(callback, base::PLATFORM_FILE_ERROR_FAILED, 0, true)); | |
| 302 } | 290 } |
| 303 | 291 |
| 304 void FileSystemOperation::Truncate(const GURL& path_url, int64 length, | 292 void FileSystemOperation::Truncate(const GURL& path_url, int64 length, |
| 305 const StatusCallback& callback) { | 293 const StatusCallback& callback) { |
| 306 DCHECK(SetPendingOperationType(kOperationTruncate)); | 294 DCHECK(SetPendingOperationType(kOperationTruncate)); |
| 307 | 295 |
| 308 base::PlatformFileError result = SetUpFileSystemPath( | 296 base::PlatformFileError result = SetUpFileSystemPath( |
| 309 path_url, &src_path_, &src_util_, PATH_FOR_WRITE); | 297 path_url, &src_path_, &src_util_, PATH_FOR_WRITE); |
| 310 if (result != base::PLATFORM_FILE_OK) { | 298 if (result != base::PLATFORM_FILE_OK) { |
| 311 callback.Run(result); | 299 callback.Run(result); |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 | 523 |
| 536 void FileSystemOperation::DoMove(const StatusCallback& callback) { | 524 void FileSystemOperation::DoMove(const StatusCallback& callback) { |
| 537 FileSystemFileUtilProxy::Move( | 525 FileSystemFileUtilProxy::Move( |
| 538 &operation_context_, | 526 &operation_context_, |
| 539 src_util_, dest_util_, | 527 src_util_, dest_util_, |
| 540 src_path_, dest_path_, | 528 src_path_, dest_path_, |
| 541 base::Bind(&FileSystemOperation::DidFinishFileOperation, | 529 base::Bind(&FileSystemOperation::DidFinishFileOperation, |
| 542 base::Owned(this), callback)); | 530 base::Owned(this), callback)); |
| 543 } | 531 } |
| 544 | 532 |
| 545 void FileSystemOperation::DoWrite(scoped_ptr<net::URLRequest> blob_request) { | |
| 546 int file_flags = base::PLATFORM_FILE_OPEN | | |
| 547 base::PLATFORM_FILE_WRITE | | |
| 548 base::PLATFORM_FILE_ASYNC; | |
| 549 | |
| 550 // We may get deleted on the way so allocate a new operation context | |
| 551 // to keep it alive. | |
| 552 FileSystemOperationContext* write_context = new FileSystemOperationContext( | |
| 553 operation_context_); | |
| 554 FileSystemFileUtilProxy::CreateOrOpen( | |
| 555 write_context, src_util_, src_path_, file_flags, | |
| 556 base::Bind(&FileSystemOperation::OnFileOpenedForWrite, | |
| 557 weak_factory_.GetWeakPtr(), | |
| 558 base::Passed(&blob_request), | |
| 559 base::Owned(write_context))); | |
| 560 } | |
| 561 | |
| 562 void FileSystemOperation::DoTruncate(const StatusCallback& callback, | 533 void FileSystemOperation::DoTruncate(const StatusCallback& callback, |
| 563 int64 length) { | 534 int64 length) { |
| 564 FileSystemFileUtilProxy::Truncate( | 535 FileSystemFileUtilProxy::Truncate( |
| 565 &operation_context_, src_util_, src_path_, length, | 536 &operation_context_, src_util_, src_path_, length, |
| 566 base::Bind(&FileSystemOperation::DidFinishFileOperation, | 537 base::Bind(&FileSystemOperation::DidFinishFileOperation, |
| 567 base::Owned(this), callback)); | 538 base::Owned(this), callback)); |
| 568 } | 539 } |
| 569 | 540 |
| 570 void FileSystemOperation::DoOpenFile(const OpenFileCallback& callback, | 541 void FileSystemOperation::DoOpenFile(const OpenFileCallback& callback, |
| 571 int file_flags) { | 542 int file_flags) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 void FileSystemOperation::DidOpenFile( | 635 void FileSystemOperation::DidOpenFile( |
| 665 const OpenFileCallback& callback, | 636 const OpenFileCallback& callback, |
| 666 base::PlatformFileError rv, | 637 base::PlatformFileError rv, |
| 667 base::PassPlatformFile file, | 638 base::PassPlatformFile file, |
| 668 bool unused) { | 639 bool unused) { |
| 669 if (rv == base::PLATFORM_FILE_OK) | 640 if (rv == base::PLATFORM_FILE_OK) |
| 670 CHECK_NE(base::kNullProcessHandle, peer_handle_); | 641 CHECK_NE(base::kNullProcessHandle, peer_handle_); |
| 671 callback.Run(rv, file.ReleaseValue(), peer_handle_); | 642 callback.Run(rv, file.ReleaseValue(), peer_handle_); |
| 672 } | 643 } |
| 673 | 644 |
| 674 void FileSystemOperation::OnFileOpenedForWrite( | |
| 675 scoped_ptr<net::URLRequest> blob_request, | |
| 676 FileSystemOperationContext* unused, | |
| 677 base::PlatformFileError rv, | |
| 678 base::PassPlatformFile file, | |
| 679 bool created) { | |
| 680 if (rv != base::PLATFORM_FILE_OK) { | |
| 681 if (!write_callback_.is_null()) | |
| 682 write_callback_.Run(rv, 0, false); | |
| 683 delete this; | |
| 684 return; | |
| 685 } | |
| 686 file_writer_delegate_->Start(file.ReleaseValue(), blob_request.Pass()); | |
| 687 } | |
| 688 | |
| 689 base::PlatformFileError FileSystemOperation::SetUpFileSystemPath( | 645 base::PlatformFileError FileSystemOperation::SetUpFileSystemPath( |
| 690 const GURL& path_url, | 646 const GURL& path_url, |
| 691 FileSystemPath* file_system_path, | 647 FileSystemPath* file_system_path, |
| 692 FileSystemFileUtil** file_util, | 648 FileSystemFileUtil** file_util, |
| 693 SetUpPathMode mode) { | 649 SetUpPathMode mode) { |
| 694 DCHECK(file_system_path); | 650 DCHECK(file_system_path); |
| 695 GURL origin_url; | 651 GURL origin_url; |
| 696 FileSystemType type; | 652 FileSystemType type; |
| 697 FilePath cracked_path; | 653 FilePath cracked_path; |
| 698 if (!CrackFileSystemURL(path_url, &origin_url, &type, &cracked_path)) | 654 if (!CrackFileSystemURL(path_url, &origin_url, &type, &cracked_path)) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 746 } | 702 } |
| 747 | 703 |
| 748 bool FileSystemOperation::SetPendingOperationType(OperationType type) { | 704 bool FileSystemOperation::SetPendingOperationType(OperationType type) { |
| 749 if (pending_operation_ != kOperationNone) | 705 if (pending_operation_ != kOperationNone) |
| 750 return false; | 706 return false; |
| 751 pending_operation_ = type; | 707 pending_operation_ = type; |
| 752 return true; | 708 return true; |
| 753 } | 709 } |
| 754 | 710 |
| 755 } // namespace fileapi | 711 } // namespace fileapi |
| OLD | NEW |