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 storage { |
22 | 22 |
23 const int64 kFlushIntervalInBytes = 10 << 20; // 10MB. | 23 const int64 kFlushIntervalInBytes = 10 << 20; // 10MB. |
24 | 24 |
25 class CopyOrMoveOperationDelegate::CopyOrMoveImpl { | 25 class CopyOrMoveOperationDelegate::CopyOrMoveImpl { |
26 public: | 26 public: |
27 virtual ~CopyOrMoveImpl() {} | 27 virtual ~CopyOrMoveImpl() {} |
28 virtual void Run( | 28 virtual void Run( |
29 const CopyOrMoveOperationDelegate::StatusCallback& callback) = 0; | 29 const CopyOrMoveOperationDelegate::StatusCallback& callback) = 0; |
30 virtual void Cancel() = 0; | 30 virtual void Cancel() = 0; |
31 | 31 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 virtual void Cancel() OVERRIDE { | 124 virtual void Cancel() OVERRIDE { |
125 cancel_requested_ = true; | 125 cancel_requested_ = true; |
126 } | 126 } |
127 | 127 |
128 private: | 128 private: |
129 void RunAfterCreateSnapshot( | 129 void RunAfterCreateSnapshot( |
130 const CopyOrMoveOperationDelegate::StatusCallback& callback, | 130 const CopyOrMoveOperationDelegate::StatusCallback& callback, |
131 base::File::Error error, | 131 base::File::Error error, |
132 const base::File::Info& file_info, | 132 const base::File::Info& file_info, |
133 const base::FilePath& platform_path, | 133 const base::FilePath& platform_path, |
134 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { | 134 const scoped_refptr<storage::ShareableFileReference>& file_ref) { |
135 if (cancel_requested_) | 135 if (cancel_requested_) |
136 error = base::File::FILE_ERROR_ABORT; | 136 error = base::File::FILE_ERROR_ABORT; |
137 | 137 |
138 if (error != base::File::FILE_OK) { | 138 if (error != base::File::FILE_OK) { |
139 callback.Run(error); | 139 callback.Run(error); |
140 return; | 140 return; |
141 } | 141 } |
142 | 142 |
143 // For now we assume CreateSnapshotFile always return a valid local file | 143 // For now we assume CreateSnapshotFile always return a valid local file |
144 // path. | 144 // path. |
(...skipping 10 matching lines...) Expand all Loading... |
155 PreWriteValidation( | 155 PreWriteValidation( |
156 platform_path, | 156 platform_path, |
157 base::Bind(&SnapshotCopyOrMoveImpl::RunAfterPreWriteValidation, | 157 base::Bind(&SnapshotCopyOrMoveImpl::RunAfterPreWriteValidation, |
158 weak_factory_.GetWeakPtr(), | 158 weak_factory_.GetWeakPtr(), |
159 platform_path, file_info, file_ref, callback)); | 159 platform_path, file_info, file_ref, callback)); |
160 } | 160 } |
161 | 161 |
162 void RunAfterPreWriteValidation( | 162 void RunAfterPreWriteValidation( |
163 const base::FilePath& platform_path, | 163 const base::FilePath& platform_path, |
164 const base::File::Info& file_info, | 164 const base::File::Info& file_info, |
165 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref, | 165 const scoped_refptr<storage::ShareableFileReference>& file_ref, |
166 const CopyOrMoveOperationDelegate::StatusCallback& callback, | 166 const CopyOrMoveOperationDelegate::StatusCallback& callback, |
167 base::File::Error error) { | 167 base::File::Error error) { |
168 if (cancel_requested_) | 168 if (cancel_requested_) |
169 error = base::File::FILE_ERROR_ABORT; | 169 error = base::File::FILE_ERROR_ABORT; |
170 | 170 |
171 if (error != base::File::FILE_OK) { | 171 if (error != base::File::FILE_OK) { |
172 callback.Run(error); | 172 callback.Run(error); |
173 return; | 173 return; |
174 } | 174 } |
175 | 175 |
176 // |file_ref| is unused but necessary to keep the file alive until | 176 // |file_ref| is unused but necessary to keep the file alive until |
177 // CopyInForeignFile() is completed. | 177 // CopyInForeignFile() is completed. |
178 operation_runner_->CopyInForeignFile( | 178 operation_runner_->CopyInForeignFile( |
179 platform_path, dest_url_, | 179 platform_path, dest_url_, |
180 base::Bind(&SnapshotCopyOrMoveImpl::RunAfterCopyInForeignFile, | 180 base::Bind(&SnapshotCopyOrMoveImpl::RunAfterCopyInForeignFile, |
181 weak_factory_.GetWeakPtr(), file_info, file_ref, callback)); | 181 weak_factory_.GetWeakPtr(), file_info, file_ref, callback)); |
182 } | 182 } |
183 | 183 |
184 void RunAfterCopyInForeignFile( | 184 void RunAfterCopyInForeignFile( |
185 const base::File::Info& file_info, | 185 const base::File::Info& file_info, |
186 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref, | 186 const scoped_refptr<storage::ShareableFileReference>& file_ref, |
187 const CopyOrMoveOperationDelegate::StatusCallback& callback, | 187 const CopyOrMoveOperationDelegate::StatusCallback& callback, |
188 base::File::Error error) { | 188 base::File::Error error) { |
189 if (cancel_requested_) | 189 if (cancel_requested_) |
190 error = base::File::FILE_ERROR_ABORT; | 190 error = base::File::FILE_ERROR_ABORT; |
191 | 191 |
192 if (error != base::File::FILE_OK) { | 192 if (error != base::File::FILE_OK) { |
193 callback.Run(error); | 193 callback.Run(error); |
194 return; | 194 return; |
195 } | 195 } |
196 | 196 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 base::Bind( | 303 base::Bind( |
304 &SnapshotCopyOrMoveImpl::PostWriteValidationAfterCreateSnapshotFile, | 304 &SnapshotCopyOrMoveImpl::PostWriteValidationAfterCreateSnapshotFile, |
305 weak_factory_.GetWeakPtr(), callback)); | 305 weak_factory_.GetWeakPtr(), callback)); |
306 } | 306 } |
307 | 307 |
308 void PostWriteValidationAfterCreateSnapshotFile( | 308 void PostWriteValidationAfterCreateSnapshotFile( |
309 const CopyOrMoveOperationDelegate::StatusCallback& callback, | 309 const CopyOrMoveOperationDelegate::StatusCallback& callback, |
310 base::File::Error error, | 310 base::File::Error error, |
311 const base::File::Info& file_info, | 311 const base::File::Info& file_info, |
312 const base::FilePath& platform_path, | 312 const base::FilePath& platform_path, |
313 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { | 313 const scoped_refptr<storage::ShareableFileReference>& file_ref) { |
314 if (cancel_requested_) | 314 if (cancel_requested_) |
315 error = base::File::FILE_ERROR_ABORT; | 315 error = base::File::FILE_ERROR_ABORT; |
316 | 316 |
317 if (error != base::File::FILE_OK) { | 317 if (error != base::File::FILE_OK) { |
318 callback.Run(error); | 318 callback.Run(error); |
319 return; | 319 return; |
320 } | 320 } |
321 | 321 |
322 DCHECK(validator_); | 322 DCHECK(validator_); |
323 // Note: file_ref passed here to keep the file alive until after | 323 // Note: file_ref passed here to keep the file alive until after |
324 // the StartPostWriteValidation operation finishes. | 324 // the StartPostWriteValidation operation finishes. |
325 validator_->StartPostWriteValidation( | 325 validator_->StartPostWriteValidation( |
326 platform_path, | 326 platform_path, |
327 base::Bind(&SnapshotCopyOrMoveImpl::DidPostWriteValidation, | 327 base::Bind(&SnapshotCopyOrMoveImpl::DidPostWriteValidation, |
328 weak_factory_.GetWeakPtr(), file_ref, callback)); | 328 weak_factory_.GetWeakPtr(), file_ref, callback)); |
329 } | 329 } |
330 | 330 |
331 // |file_ref| is unused; it is passed here to make sure the reference is | 331 // |file_ref| is unused; it is passed here to make sure the reference is |
332 // alive until after post-write validation is complete. | 332 // alive until after post-write validation is complete. |
333 void DidPostWriteValidation( | 333 void DidPostWriteValidation( |
334 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref, | 334 const scoped_refptr<storage::ShareableFileReference>& file_ref, |
335 const CopyOrMoveOperationDelegate::StatusCallback& callback, | 335 const CopyOrMoveOperationDelegate::StatusCallback& callback, |
336 base::File::Error error) { | 336 base::File::Error error) { |
337 callback.Run(error); | 337 callback.Run(error); |
338 } | 338 } |
339 | 339 |
340 FileSystemOperationRunner* operation_runner_; | 340 FileSystemOperationRunner* operation_runner_; |
341 CopyOrMoveOperationDelegate::OperationType operation_type_; | 341 CopyOrMoveOperationDelegate::OperationType operation_type_; |
342 FileSystemURL src_url_; | 342 FileSystemURL src_url_; |
343 FileSystemURL dest_url_; | 343 FileSystemURL dest_url_; |
344 | 344 |
(...skipping 18 matching lines...) Expand all Loading... |
363 // necessary SnapshotCopyOrMoveImpl should be used. | 363 // necessary SnapshotCopyOrMoveImpl should be used. |
364 class StreamCopyOrMoveImpl | 364 class StreamCopyOrMoveImpl |
365 : public CopyOrMoveOperationDelegate::CopyOrMoveImpl { | 365 : public CopyOrMoveOperationDelegate::CopyOrMoveImpl { |
366 public: | 366 public: |
367 StreamCopyOrMoveImpl( | 367 StreamCopyOrMoveImpl( |
368 FileSystemOperationRunner* operation_runner, | 368 FileSystemOperationRunner* operation_runner, |
369 CopyOrMoveOperationDelegate::OperationType operation_type, | 369 CopyOrMoveOperationDelegate::OperationType operation_type, |
370 const FileSystemURL& src_url, | 370 const FileSystemURL& src_url, |
371 const FileSystemURL& dest_url, | 371 const FileSystemURL& dest_url, |
372 CopyOrMoveOperationDelegate::CopyOrMoveOption option, | 372 CopyOrMoveOperationDelegate::CopyOrMoveOption option, |
373 scoped_ptr<webkit_blob::FileStreamReader> reader, | 373 scoped_ptr<storage::FileStreamReader> reader, |
374 scoped_ptr<FileStreamWriter> writer, | 374 scoped_ptr<FileStreamWriter> writer, |
375 const FileSystemOperation::CopyFileProgressCallback& | 375 const FileSystemOperation::CopyFileProgressCallback& |
376 file_progress_callback) | 376 file_progress_callback) |
377 : operation_runner_(operation_runner), | 377 : operation_runner_(operation_runner), |
378 operation_type_(operation_type), | 378 operation_type_(operation_type), |
379 src_url_(src_url), | 379 src_url_(src_url), |
380 dest_url_(dest_url), | 380 dest_url_(dest_url), |
381 option_(option), | 381 option_(option), |
382 reader_(reader.Pass()), | 382 reader_(reader.Pass()), |
383 writer_(writer.Pass()), | 383 writer_(writer.Pass()), |
384 file_progress_callback_(file_progress_callback), | 384 file_progress_callback_(file_progress_callback), |
385 cancel_requested_(false), | 385 cancel_requested_(false), |
386 weak_factory_(this) { | 386 weak_factory_(this) {} |
387 } | |
388 | 387 |
389 virtual void Run( | 388 virtual void Run( |
390 const CopyOrMoveOperationDelegate::StatusCallback& callback) OVERRIDE { | 389 const CopyOrMoveOperationDelegate::StatusCallback& callback) OVERRIDE { |
391 // Reader can be created even if the entry does not exist or the entry is | 390 // Reader can be created even if the entry does not exist or the entry is |
392 // a directory. To check errors before destination file creation, | 391 // a directory. To check errors before destination file creation, |
393 // check metadata first. | 392 // check metadata first. |
394 operation_runner_->GetMetadata( | 393 operation_runner_->GetMetadata( |
395 src_url_, | 394 src_url_, |
396 base::Bind(&StreamCopyOrMoveImpl::RunAfterGetMetadataForSource, | 395 base::Bind(&StreamCopyOrMoveImpl::RunAfterGetMetadataForSource, |
397 weak_factory_.GetWeakPtr(), callback)); | 396 weak_factory_.GetWeakPtr(), callback)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 base::File::Error error) { | 435 base::File::Error error) { |
437 if (cancel_requested_) | 436 if (cancel_requested_) |
438 error = base::File::FILE_ERROR_ABORT; | 437 error = base::File::FILE_ERROR_ABORT; |
439 | 438 |
440 if (error != base::File::FILE_OK) { | 439 if (error != base::File::FILE_OK) { |
441 callback.Run(error); | 440 callback.Run(error); |
442 return; | 441 return; |
443 } | 442 } |
444 | 443 |
445 const bool need_flush = dest_url_.mount_option().copy_sync_option() == | 444 const bool need_flush = dest_url_.mount_option().copy_sync_option() == |
446 fileapi::COPY_SYNC_OPTION_SYNC; | 445 storage::COPY_SYNC_OPTION_SYNC; |
447 | 446 |
448 DCHECK(!copy_helper_); | 447 DCHECK(!copy_helper_); |
449 copy_helper_.reset( | 448 copy_helper_.reset( |
450 new CopyOrMoveOperationDelegate::StreamCopyHelper( | 449 new CopyOrMoveOperationDelegate::StreamCopyHelper( |
451 reader_.Pass(), writer_.Pass(), | 450 reader_.Pass(), writer_.Pass(), |
452 need_flush, | 451 need_flush, |
453 kReadBufferSize, | 452 kReadBufferSize, |
454 file_progress_callback_, | 453 file_progress_callback_, |
455 base::TimeDelta::FromMilliseconds( | 454 base::TimeDelta::FromMilliseconds( |
456 kMinProgressCallbackInvocationSpanInMilliseconds))); | 455 kMinProgressCallbackInvocationSpanInMilliseconds))); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 if (error == base::File::FILE_ERROR_NOT_FOUND) | 512 if (error == base::File::FILE_ERROR_NOT_FOUND) |
514 error = base::File::FILE_OK; | 513 error = base::File::FILE_OK; |
515 callback.Run(error); | 514 callback.Run(error); |
516 } | 515 } |
517 | 516 |
518 FileSystemOperationRunner* operation_runner_; | 517 FileSystemOperationRunner* operation_runner_; |
519 CopyOrMoveOperationDelegate::OperationType operation_type_; | 518 CopyOrMoveOperationDelegate::OperationType operation_type_; |
520 FileSystemURL src_url_; | 519 FileSystemURL src_url_; |
521 FileSystemURL dest_url_; | 520 FileSystemURL dest_url_; |
522 CopyOrMoveOperationDelegate::CopyOrMoveOption option_; | 521 CopyOrMoveOperationDelegate::CopyOrMoveOption option_; |
523 scoped_ptr<webkit_blob::FileStreamReader> reader_; | 522 scoped_ptr<storage::FileStreamReader> reader_; |
524 scoped_ptr<FileStreamWriter> writer_; | 523 scoped_ptr<FileStreamWriter> writer_; |
525 FileSystemOperation::CopyFileProgressCallback file_progress_callback_; | 524 FileSystemOperation::CopyFileProgressCallback file_progress_callback_; |
526 scoped_ptr<CopyOrMoveOperationDelegate::StreamCopyHelper> copy_helper_; | 525 scoped_ptr<CopyOrMoveOperationDelegate::StreamCopyHelper> copy_helper_; |
527 bool cancel_requested_; | 526 bool cancel_requested_; |
528 base::WeakPtrFactory<StreamCopyOrMoveImpl> weak_factory_; | 527 base::WeakPtrFactory<StreamCopyOrMoveImpl> weak_factory_; |
529 DISALLOW_COPY_AND_ASSIGN(StreamCopyOrMoveImpl); | 528 DISALLOW_COPY_AND_ASSIGN(StreamCopyOrMoveImpl); |
530 }; | 529 }; |
531 | 530 |
532 } // namespace | 531 } // namespace |
533 | 532 |
534 CopyOrMoveOperationDelegate::StreamCopyHelper::StreamCopyHelper( | 533 CopyOrMoveOperationDelegate::StreamCopyHelper::StreamCopyHelper( |
535 scoped_ptr<webkit_blob::FileStreamReader> reader, | 534 scoped_ptr<storage::FileStreamReader> reader, |
536 scoped_ptr<FileStreamWriter> writer, | 535 scoped_ptr<FileStreamWriter> writer, |
537 bool need_flush, | 536 bool need_flush, |
538 int buffer_size, | 537 int buffer_size, |
539 const FileSystemOperation::CopyFileProgressCallback& | 538 const FileSystemOperation::CopyFileProgressCallback& file_progress_callback, |
540 file_progress_callback, | |
541 const base::TimeDelta& min_progress_callback_invocation_span) | 539 const base::TimeDelta& min_progress_callback_invocation_span) |
542 : reader_(reader.Pass()), | 540 : reader_(reader.Pass()), |
543 writer_(writer.Pass()), | 541 writer_(writer.Pass()), |
544 need_flush_(need_flush), | 542 need_flush_(need_flush), |
545 file_progress_callback_(file_progress_callback), | 543 file_progress_callback_(file_progress_callback), |
546 io_buffer_(new net::IOBufferWithSize(buffer_size)), | 544 io_buffer_(new net::IOBufferWithSize(buffer_size)), |
547 num_copied_bytes_(0), | 545 num_copied_bytes_(0), |
548 previous_flush_offset_(0), | 546 previous_flush_offset_(0), |
549 min_progress_callback_invocation_span_( | 547 min_progress_callback_invocation_span_( |
550 min_progress_callback_invocation_span), | 548 min_progress_callback_invocation_span), |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
750 base::File::Error error = base::File::FILE_ERROR_FAILED; | 748 base::File::Error error = base::File::FILE_ERROR_FAILED; |
751 CopyOrMoveFileValidatorFactory* validator_factory = | 749 CopyOrMoveFileValidatorFactory* validator_factory = |
752 file_system_context()->GetCopyOrMoveFileValidatorFactory( | 750 file_system_context()->GetCopyOrMoveFileValidatorFactory( |
753 dest_root_.type(), &error); | 751 dest_root_.type(), &error); |
754 if (error != base::File::FILE_OK) { | 752 if (error != base::File::FILE_OK) { |
755 callback.Run(error); | 753 callback.Run(error); |
756 return; | 754 return; |
757 } | 755 } |
758 | 756 |
759 if (!validator_factory) { | 757 if (!validator_factory) { |
760 scoped_ptr<webkit_blob::FileStreamReader> reader = | 758 scoped_ptr<storage::FileStreamReader> reader = |
761 file_system_context()->CreateFileStreamReader( | 759 file_system_context()->CreateFileStreamReader( |
762 src_url, 0, base::Time()); | 760 src_url, 0, base::Time()); |
763 scoped_ptr<FileStreamWriter> writer = | 761 scoped_ptr<FileStreamWriter> writer = |
764 file_system_context()->CreateFileStreamWriter(dest_url, 0); | 762 file_system_context()->CreateFileStreamWriter(dest_url, 0); |
765 if (reader && writer) { | 763 if (reader && writer) { |
766 impl = new StreamCopyOrMoveImpl( | 764 impl = new StreamCopyOrMoveImpl( |
767 operation_runner(), operation_type_, src_url, dest_url, option_, | 765 operation_runner(), operation_type_, src_url, dest_url, option_, |
768 reader.Pass(), writer.Pass(), | 766 reader.Pass(), writer.Pass(), |
769 base::Bind(&CopyOrMoveOperationDelegate::OnCopyFileProgress, | 767 base::Bind(&CopyOrMoveOperationDelegate::OnCopyFileProgress, |
770 weak_factory_.GetWeakPtr(), src_url)); | 768 weak_factory_.GetWeakPtr(), src_url)); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 | 956 |
959 base::FilePath relative = dest_root_.virtual_path(); | 957 base::FilePath relative = dest_root_.virtual_path(); |
960 src_root_.virtual_path().AppendRelativePath(src_url.virtual_path(), | 958 src_root_.virtual_path().AppendRelativePath(src_url.virtual_path(), |
961 &relative); | 959 &relative); |
962 return file_system_context()->CreateCrackedFileSystemURL( | 960 return file_system_context()->CreateCrackedFileSystemURL( |
963 dest_root_.origin(), | 961 dest_root_.origin(), |
964 dest_root_.mount_type(), | 962 dest_root_.mount_type(), |
965 relative); | 963 relative); |
966 } | 964 } |
967 | 965 |
968 } // namespace fileapi | 966 } // namespace storage |
OLD | NEW |