| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/browser/fileapi/copy_or_move_operation_delegate.h" | 5 #include "webkit/browser/fileapi/copy_or_move_operation_delegate.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "webkit/browser/blob/file_stream_reader.h" | 11 #include "webkit/browser/blob/file_stream_reader.h" |
| 12 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | 12 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
| 13 #include "webkit/browser/fileapi/file_stream_writer.h" | 13 #include "webkit/browser/fileapi/file_stream_writer.h" |
| 14 #include "webkit/browser/fileapi/file_system_context.h" | 14 #include "webkit/browser/fileapi/file_system_context.h" |
| 15 #include "webkit/browser/fileapi/file_system_operation_runner.h" | 15 #include "webkit/browser/fileapi/file_system_operation_runner.h" |
| 16 #include "webkit/browser/fileapi/file_system_url.h" | 16 #include "webkit/browser/fileapi/file_system_url.h" |
| 17 #include "webkit/browser/fileapi/recursive_operation_delegate.h" | 17 #include "webkit/browser/fileapi/recursive_operation_delegate.h" |
| 18 #include "webkit/common/blob/shareable_file_reference.h" | 18 #include "webkit/common/blob/shareable_file_reference.h" |
| 19 #include "webkit/common/fileapi/file_system_util.h" | 19 #include "webkit/common/fileapi/file_system_util.h" |
| 20 | 20 |
| 21 namespace fileapi { | 21 namespace fileapi { |
| 22 | 22 |
| 23 const int64 kFlushIntervalInBytes = 10 << 20; // 10MB. |
| 24 |
| 23 class CopyOrMoveOperationDelegate::CopyOrMoveImpl { | 25 class CopyOrMoveOperationDelegate::CopyOrMoveImpl { |
| 24 public: | 26 public: |
| 25 virtual ~CopyOrMoveImpl() {} | 27 virtual ~CopyOrMoveImpl() {} |
| 26 virtual void Run( | 28 virtual void Run( |
| 27 const CopyOrMoveOperationDelegate::StatusCallback& callback) = 0; | 29 const CopyOrMoveOperationDelegate::StatusCallback& callback) = 0; |
| 28 virtual void Cancel() = 0; | 30 virtual void Cancel() = 0; |
| 29 | 31 |
| 30 protected: | 32 protected: |
| 31 CopyOrMoveImpl() {} | 33 CopyOrMoveImpl() {} |
| 32 | 34 |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 int buffer_size, | 538 int buffer_size, |
| 537 const FileSystemOperation::CopyFileProgressCallback& | 539 const FileSystemOperation::CopyFileProgressCallback& |
| 538 file_progress_callback, | 540 file_progress_callback, |
| 539 const base::TimeDelta& min_progress_callback_invocation_span) | 541 const base::TimeDelta& min_progress_callback_invocation_span) |
| 540 : reader_(reader.Pass()), | 542 : reader_(reader.Pass()), |
| 541 writer_(writer.Pass()), | 543 writer_(writer.Pass()), |
| 542 need_flush_(need_flush), | 544 need_flush_(need_flush), |
| 543 file_progress_callback_(file_progress_callback), | 545 file_progress_callback_(file_progress_callback), |
| 544 io_buffer_(new net::IOBufferWithSize(buffer_size)), | 546 io_buffer_(new net::IOBufferWithSize(buffer_size)), |
| 545 num_copied_bytes_(0), | 547 num_copied_bytes_(0), |
| 548 previous_flush_(0), |
| 546 min_progress_callback_invocation_span_( | 549 min_progress_callback_invocation_span_( |
| 547 min_progress_callback_invocation_span), | 550 min_progress_callback_invocation_span), |
| 548 cancel_requested_(false), | 551 cancel_requested_(false), |
| 549 weak_factory_(this) { | 552 weak_factory_(this) { |
| 550 } | 553 } |
| 551 | 554 |
| 552 CopyOrMoveOperationDelegate::StreamCopyHelper::~StreamCopyHelper() { | 555 CopyOrMoveOperationDelegate::StreamCopyHelper::~StreamCopyHelper() { |
| 553 } | 556 } |
| 554 | 557 |
| 555 void CopyOrMoveOperationDelegate::StreamCopyHelper::Run( | 558 void CopyOrMoveOperationDelegate::StreamCopyHelper::Run( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 581 } | 584 } |
| 582 | 585 |
| 583 if (result < 0) { | 586 if (result < 0) { |
| 584 callback.Run(NetErrorToPlatformFileError(result)); | 587 callback.Run(NetErrorToPlatformFileError(result)); |
| 585 return; | 588 return; |
| 586 } | 589 } |
| 587 | 590 |
| 588 if (result == 0) { | 591 if (result == 0) { |
| 589 // Here is the EOF. | 592 // Here is the EOF. |
| 590 if (need_flush_) | 593 if (need_flush_) |
| 591 Flush(callback); | 594 Flush(callback, true /* is_eof */); |
| 592 else | 595 else |
| 593 callback.Run(base::PLATFORM_FILE_OK); | 596 callback.Run(base::PLATFORM_FILE_OK); |
| 594 return; | 597 return; |
| 595 } | 598 } |
| 596 | 599 |
| 597 Write(callback, new net::DrainableIOBuffer(io_buffer_.get(), result)); | 600 Write(callback, new net::DrainableIOBuffer(io_buffer_.get(), result)); |
| 598 } | 601 } |
| 599 | 602 |
| 600 void CopyOrMoveOperationDelegate::StreamCopyHelper::Write( | 603 void CopyOrMoveOperationDelegate::StreamCopyHelper::Write( |
| 601 const StatusCallback& callback, | 604 const StatusCallback& callback, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 min_progress_callback_invocation_span_) { | 636 min_progress_callback_invocation_span_) { |
| 634 file_progress_callback_.Run(num_copied_bytes_); | 637 file_progress_callback_.Run(num_copied_bytes_); |
| 635 last_progress_callback_invocation_time_ = now; | 638 last_progress_callback_invocation_time_ = now; |
| 636 } | 639 } |
| 637 | 640 |
| 638 if (buffer->BytesRemaining() > 0) { | 641 if (buffer->BytesRemaining() > 0) { |
| 639 Write(callback, buffer); | 642 Write(callback, buffer); |
| 640 return; | 643 return; |
| 641 } | 644 } |
| 642 | 645 |
| 643 Read(callback); | 646 if (need_flush_ && |
| 647 (num_copied_bytes_ - previous_flush_) > kFlushIntervalInBytes) { |
| 648 Flush(callback, false /* not is_eof */); |
| 649 } else { |
| 650 Read(callback); |
| 651 } |
| 644 } | 652 } |
| 645 | 653 |
| 646 void CopyOrMoveOperationDelegate::StreamCopyHelper::Flush( | 654 void CopyOrMoveOperationDelegate::StreamCopyHelper::Flush( |
| 647 const StatusCallback& callback) { | 655 const StatusCallback& callback, bool is_eof) { |
| 648 int result = writer_->Flush( | 656 int result = writer_->Flush( |
| 649 base::Bind(&StreamCopyHelper::DidFlush, | 657 base::Bind(&StreamCopyHelper::DidFlush, |
| 650 weak_factory_.GetWeakPtr(), callback)); | 658 weak_factory_.GetWeakPtr(), callback, is_eof)); |
| 651 if (result != net::ERR_IO_PENDING) | 659 if (result != net::ERR_IO_PENDING) |
| 652 DidFlush(callback, result); | 660 DidFlush(callback, is_eof, result); |
| 653 } | 661 } |
| 654 | 662 |
| 655 void CopyOrMoveOperationDelegate::StreamCopyHelper::DidFlush( | 663 void CopyOrMoveOperationDelegate::StreamCopyHelper::DidFlush( |
| 656 const StatusCallback& callback, | 664 const StatusCallback& callback, bool is_eof, int result) { |
| 657 int result) { | |
| 658 if (cancel_requested_) { | 665 if (cancel_requested_) { |
| 659 callback.Run(base::PLATFORM_FILE_ERROR_ABORT); | 666 callback.Run(base::PLATFORM_FILE_ERROR_ABORT); |
| 660 return; | 667 return; |
| 661 } | 668 } |
| 662 | 669 |
| 663 callback.Run(NetErrorToPlatformFileError(result)); | 670 previous_flush_ = num_copied_bytes_; |
| 671 if (is_eof) |
| 672 callback.Run(NetErrorToPlatformFileError(result)); |
| 673 else |
| 674 Read(callback); |
| 664 } | 675 } |
| 665 | 676 |
| 666 CopyOrMoveOperationDelegate::CopyOrMoveOperationDelegate( | 677 CopyOrMoveOperationDelegate::CopyOrMoveOperationDelegate( |
| 667 FileSystemContext* file_system_context, | 678 FileSystemContext* file_system_context, |
| 668 const FileSystemURL& src_root, | 679 const FileSystemURL& src_root, |
| 669 const FileSystemURL& dest_root, | 680 const FileSystemURL& dest_root, |
| 670 OperationType operation_type, | 681 OperationType operation_type, |
| 671 CopyOrMoveOption option, | 682 CopyOrMoveOption option, |
| 672 const CopyProgressCallback& progress_callback, | 683 const CopyProgressCallback& progress_callback, |
| 673 const StatusCallback& callback) | 684 const StatusCallback& callback) |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 base::FilePath relative = dest_root_.virtual_path(); | 953 base::FilePath relative = dest_root_.virtual_path(); |
| 943 src_root_.virtual_path().AppendRelativePath(src_url.virtual_path(), | 954 src_root_.virtual_path().AppendRelativePath(src_url.virtual_path(), |
| 944 &relative); | 955 &relative); |
| 945 return file_system_context()->CreateCrackedFileSystemURL( | 956 return file_system_context()->CreateCrackedFileSystemURL( |
| 946 dest_root_.origin(), | 957 dest_root_.origin(), |
| 947 dest_root_.mount_type(), | 958 dest_root_.mount_type(), |
| 948 relative); | 959 relative); |
| 949 } | 960 } |
| 950 | 961 |
| 951 } // namespace fileapi | 962 } // namespace fileapi |
| OLD | NEW |