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 "storage/browser/fileapi/copy_or_move_operation_delegate.h" | 5 #include "storage/browser/fileapi/copy_or_move_operation_delegate.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <tuple> | 10 #include <tuple> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" |
16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
18 #include "storage/browser/blob/shareable_file_reference.h" | 19 #include "storage/browser/blob/shareable_file_reference.h" |
19 #include "storage/browser/fileapi/copy_or_move_file_validator.h" | 20 #include "storage/browser/fileapi/copy_or_move_file_validator.h" |
20 #include "storage/browser/fileapi/file_observers.h" | 21 #include "storage/browser/fileapi/file_observers.h" |
21 #include "storage/browser/fileapi/file_stream_reader.h" | 22 #include "storage/browser/fileapi/file_stream_reader.h" |
22 #include "storage/browser/fileapi/file_stream_writer.h" | 23 #include "storage/browser/fileapi/file_stream_writer.h" |
23 #include "storage/browser/fileapi/file_system_context.h" | 24 #include "storage/browser/fileapi/file_system_context.h" |
24 #include "storage/browser/fileapi/file_system_operation_runner.h" | 25 #include "storage/browser/fileapi/file_system_operation_runner.h" |
25 #include "storage/browser/fileapi/file_system_url.h" | 26 #include "storage/browser/fileapi/file_system_url.h" |
(...skipping 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
747 operation_type_(operation_type), | 748 operation_type_(operation_type), |
748 option_(option), | 749 option_(option), |
749 error_behavior_(error_behavior), | 750 error_behavior_(error_behavior), |
750 progress_callback_(progress_callback), | 751 progress_callback_(progress_callback), |
751 callback_(callback), | 752 callback_(callback), |
752 weak_factory_(this) { | 753 weak_factory_(this) { |
753 same_file_system_ = src_root_.IsInSameFileSystem(dest_root_); | 754 same_file_system_ = src_root_.IsInSameFileSystem(dest_root_); |
754 } | 755 } |
755 | 756 |
756 CopyOrMoveOperationDelegate::~CopyOrMoveOperationDelegate() { | 757 CopyOrMoveOperationDelegate::~CopyOrMoveOperationDelegate() { |
757 base::STLDeleteElements(&running_copy_set_); | |
758 } | 758 } |
759 | 759 |
760 void CopyOrMoveOperationDelegate::Run() { | 760 void CopyOrMoveOperationDelegate::Run() { |
761 // Not supported; this should never be called. | 761 // Not supported; this should never be called. |
762 NOTREACHED(); | 762 NOTREACHED(); |
763 } | 763 } |
764 | 764 |
765 void CopyOrMoveOperationDelegate::RunRecursively() { | 765 void CopyOrMoveOperationDelegate::RunRecursively() { |
766 // Perform light-weight checks first. | 766 // Perform light-weight checks first. |
767 | 767 |
(...skipping 20 matching lines...) Expand all Loading... |
788 | 788 |
789 void CopyOrMoveOperationDelegate::ProcessFile( | 789 void CopyOrMoveOperationDelegate::ProcessFile( |
790 const FileSystemURL& src_url, | 790 const FileSystemURL& src_url, |
791 const StatusCallback& callback) { | 791 const StatusCallback& callback) { |
792 if (!progress_callback_.is_null()) { | 792 if (!progress_callback_.is_null()) { |
793 progress_callback_.Run( | 793 progress_callback_.Run( |
794 FileSystemOperation::BEGIN_COPY_ENTRY, src_url, FileSystemURL(), 0); | 794 FileSystemOperation::BEGIN_COPY_ENTRY, src_url, FileSystemURL(), 0); |
795 } | 795 } |
796 | 796 |
797 FileSystemURL dest_url = CreateDestURL(src_url); | 797 FileSystemURL dest_url = CreateDestURL(src_url); |
798 CopyOrMoveImpl* impl = NULL; | 798 std::unique_ptr<CopyOrMoveImpl> impl; |
799 if (same_file_system_ && | 799 if (same_file_system_ && |
800 (file_system_context() | 800 (file_system_context() |
801 ->GetFileSystemBackend(src_url.type()) | 801 ->GetFileSystemBackend(src_url.type()) |
802 ->HasInplaceCopyImplementation(src_url.type()) || | 802 ->HasInplaceCopyImplementation(src_url.type()) || |
803 operation_type_ == OPERATION_MOVE)) { | 803 operation_type_ == OPERATION_MOVE)) { |
804 impl = new CopyOrMoveOnSameFileSystemImpl( | 804 impl = base::MakeUnique<CopyOrMoveOnSameFileSystemImpl>( |
805 operation_runner(), operation_type_, src_url, dest_url, option_, | 805 operation_runner(), operation_type_, src_url, dest_url, option_, |
806 base::Bind(&CopyOrMoveOperationDelegate::OnCopyFileProgress, | 806 base::Bind(&CopyOrMoveOperationDelegate::OnCopyFileProgress, |
807 weak_factory_.GetWeakPtr(), src_url)); | 807 weak_factory_.GetWeakPtr(), src_url)); |
808 } else { | 808 } else { |
809 // Cross filesystem case. | 809 // Cross filesystem case. |
810 base::File::Error error = base::File::FILE_ERROR_FAILED; | 810 base::File::Error error = base::File::FILE_ERROR_FAILED; |
811 CopyOrMoveFileValidatorFactory* validator_factory = | 811 CopyOrMoveFileValidatorFactory* validator_factory = |
812 file_system_context()->GetCopyOrMoveFileValidatorFactory( | 812 file_system_context()->GetCopyOrMoveFileValidatorFactory( |
813 dest_root_.type(), &error); | 813 dest_root_.type(), &error); |
814 if (error != base::File::FILE_OK) { | 814 if (error != base::File::FILE_OK) { |
815 if (!progress_callback_.is_null()) | 815 if (!progress_callback_.is_null()) |
816 progress_callback_.Run(FileSystemOperation::ERROR_COPY_ENTRY, src_url, | 816 progress_callback_.Run(FileSystemOperation::ERROR_COPY_ENTRY, src_url, |
817 dest_url, 0); | 817 dest_url, 0); |
818 | 818 |
819 callback.Run(error); | 819 callback.Run(error); |
820 return; | 820 return; |
821 } | 821 } |
822 | 822 |
823 if (!validator_factory) { | 823 if (!validator_factory) { |
824 std::unique_ptr<storage::FileStreamReader> reader = | 824 std::unique_ptr<storage::FileStreamReader> reader = |
825 file_system_context()->CreateFileStreamReader( | 825 file_system_context()->CreateFileStreamReader( |
826 src_url, 0 /* offset */, storage::kMaximumLength, base::Time()); | 826 src_url, 0 /* offset */, storage::kMaximumLength, base::Time()); |
827 std::unique_ptr<FileStreamWriter> writer = | 827 std::unique_ptr<FileStreamWriter> writer = |
828 file_system_context()->CreateFileStreamWriter(dest_url, 0); | 828 file_system_context()->CreateFileStreamWriter(dest_url, 0); |
829 if (reader && writer) { | 829 if (reader && writer) { |
830 impl = new StreamCopyOrMoveImpl( | 830 impl = base::MakeUnique<StreamCopyOrMoveImpl>( |
831 operation_runner(), file_system_context(), operation_type_, src_url, | 831 operation_runner(), file_system_context(), operation_type_, src_url, |
832 dest_url, option_, std::move(reader), std::move(writer), | 832 dest_url, option_, std::move(reader), std::move(writer), |
833 base::Bind(&CopyOrMoveOperationDelegate::OnCopyFileProgress, | 833 base::Bind(&CopyOrMoveOperationDelegate::OnCopyFileProgress, |
834 weak_factory_.GetWeakPtr(), src_url)); | 834 weak_factory_.GetWeakPtr(), src_url)); |
835 } | 835 } |
836 } | 836 } |
837 | 837 |
838 if (!impl) { | 838 if (!impl) { |
839 impl = new SnapshotCopyOrMoveImpl( | 839 impl = base::MakeUnique<SnapshotCopyOrMoveImpl>( |
840 operation_runner(), operation_type_, src_url, dest_url, option_, | 840 operation_runner(), operation_type_, src_url, dest_url, option_, |
841 validator_factory, | 841 validator_factory, |
842 base::Bind(&CopyOrMoveOperationDelegate::OnCopyFileProgress, | 842 base::Bind(&CopyOrMoveOperationDelegate::OnCopyFileProgress, |
843 weak_factory_.GetWeakPtr(), src_url)); | 843 weak_factory_.GetWeakPtr(), src_url)); |
844 } | 844 } |
845 } | 845 } |
846 | 846 |
847 // Register the running task. | 847 // Register the running task. |
848 running_copy_set_.insert(impl); | 848 |
849 impl->Run(base::Bind( | 849 CopyOrMoveImpl* impl_ptr = impl.get(); |
850 &CopyOrMoveOperationDelegate::DidCopyOrMoveFile, | 850 running_copy_set_[impl_ptr] = std::move(impl); |
851 weak_factory_.GetWeakPtr(), src_url, dest_url, callback, impl)); | 851 impl_ptr->Run(base::Bind(&CopyOrMoveOperationDelegate::DidCopyOrMoveFile, |
| 852 weak_factory_.GetWeakPtr(), src_url, dest_url, |
| 853 callback, impl_ptr)); |
852 } | 854 } |
853 | 855 |
854 void CopyOrMoveOperationDelegate::ProcessDirectory( | 856 void CopyOrMoveOperationDelegate::ProcessDirectory( |
855 const FileSystemURL& src_url, | 857 const FileSystemURL& src_url, |
856 const StatusCallback& callback) { | 858 const StatusCallback& callback) { |
857 if (src_url == src_root_) { | 859 if (src_url == src_root_) { |
858 // The src_root_ looks to be a directory. | 860 // The src_root_ looks to be a directory. |
859 // Try removing the dest_root_ to see if it exists and/or it is an | 861 // Try removing the dest_root_ to see if it exists and/or it is an |
860 // empty directory. | 862 // empty directory. |
861 // We do not invoke |progress_callback_| for source root, because it is | 863 // We do not invoke |progress_callback_| for source root, because it is |
(...skipping 24 matching lines...) Expand all Loading... |
886 | 888 |
887 operation_runner()->GetMetadata( | 889 operation_runner()->GetMetadata( |
888 src_url, FileSystemOperation::GET_METADATA_FIELD_LAST_MODIFIED, | 890 src_url, FileSystemOperation::GET_METADATA_FIELD_LAST_MODIFIED, |
889 base::Bind( | 891 base::Bind( |
890 &CopyOrMoveOperationDelegate::PostProcessDirectoryAfterGetMetadata, | 892 &CopyOrMoveOperationDelegate::PostProcessDirectoryAfterGetMetadata, |
891 weak_factory_.GetWeakPtr(), src_url, callback)); | 893 weak_factory_.GetWeakPtr(), src_url, callback)); |
892 } | 894 } |
893 | 895 |
894 void CopyOrMoveOperationDelegate::OnCancel() { | 896 void CopyOrMoveOperationDelegate::OnCancel() { |
895 // Request to cancel all running Copy/Move file. | 897 // Request to cancel all running Copy/Move file. |
896 for (std::set<CopyOrMoveImpl*>::iterator iter = running_copy_set_.begin(); | 898 for (auto& job : running_copy_set_) |
897 iter != running_copy_set_.end(); ++iter) | 899 job.first->Cancel(); |
898 (*iter)->Cancel(); | |
899 } | 900 } |
900 | 901 |
901 void CopyOrMoveOperationDelegate::DidCopyOrMoveFile( | 902 void CopyOrMoveOperationDelegate::DidCopyOrMoveFile( |
902 const FileSystemURL& src_url, | 903 const FileSystemURL& src_url, |
903 const FileSystemURL& dest_url, | 904 const FileSystemURL& dest_url, |
904 const StatusCallback& callback, | 905 const StatusCallback& callback, |
905 CopyOrMoveImpl* impl, | 906 CopyOrMoveImpl* impl, |
906 base::File::Error error) { | 907 base::File::Error error) { |
907 running_copy_set_.erase(impl); | 908 running_copy_set_.erase(impl); |
908 delete impl; | |
909 | 909 |
910 if (!progress_callback_.is_null() && error != base::File::FILE_OK && | 910 if (!progress_callback_.is_null() && error != base::File::FILE_OK && |
911 error != base::File::FILE_ERROR_NOT_A_FILE) | 911 error != base::File::FILE_ERROR_NOT_A_FILE) |
912 progress_callback_.Run(FileSystemOperation::ERROR_COPY_ENTRY, src_url, | 912 progress_callback_.Run(FileSystemOperation::ERROR_COPY_ENTRY, src_url, |
913 dest_url, 0); | 913 dest_url, 0); |
914 | 914 |
915 if (!progress_callback_.is_null() && error == base::File::FILE_OK) { | 915 if (!progress_callback_.is_null() && error == base::File::FILE_OK) { |
916 progress_callback_.Run( | 916 progress_callback_.Run( |
917 FileSystemOperation::END_COPY_ENTRY, src_url, dest_url, 0); | 917 FileSystemOperation::END_COPY_ENTRY, src_url, dest_url, 0); |
918 } | 918 } |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1029 base::FilePath relative = dest_root_.virtual_path(); | 1029 base::FilePath relative = dest_root_.virtual_path(); |
1030 src_root_.virtual_path().AppendRelativePath(src_url.virtual_path(), | 1030 src_root_.virtual_path().AppendRelativePath(src_url.virtual_path(), |
1031 &relative); | 1031 &relative); |
1032 return file_system_context()->CreateCrackedFileSystemURL( | 1032 return file_system_context()->CreateCrackedFileSystemURL( |
1033 dest_root_.origin(), | 1033 dest_root_.origin(), |
1034 dest_root_.mount_type(), | 1034 dest_root_.mount_type(), |
1035 relative); | 1035 relative); |
1036 } | 1036 } |
1037 | 1037 |
1038 } // namespace storage | 1038 } // namespace storage |
OLD | NEW |