| 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 "content/browser/download/download_file_impl.h" | 5 #include "content/browser/download/download_file_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 base::Bind(&DownloadDestinationObserver::DestinationError, observer_, | 386 base::Bind(&DownloadDestinationObserver::DestinationError, observer_, |
| 387 reason, TotalBytesReceived(), base::Passed(&hash_state))); | 387 reason, TotalBytesReceived(), base::Passed(&hash_state))); |
| 388 } else if (state == ByteStreamReader::STREAM_COMPLETE || should_terminate) { | 388 } else if (state == ByteStreamReader::STREAM_COMPLETE || should_terminate) { |
| 389 // Signal successful completion or termination of the current stream. | 389 // Signal successful completion or termination of the current stream. |
| 390 source_stream->stream_reader()->RegisterCallback(base::Closure()); | 390 source_stream->stream_reader()->RegisterCallback(base::Closure()); |
| 391 source_stream->set_finished(true); | 391 source_stream->set_finished(true); |
| 392 | 392 |
| 393 // Inform observers. | 393 // Inform observers. |
| 394 SendUpdate(); | 394 SendUpdate(); |
| 395 | 395 |
| 396 // TODO(xingliu): Use slice info to determine if the file is fully | |
| 397 // downloaded. | |
| 398 bool all_stream_complete = true; | |
| 399 for (auto& stream : source_streams_) { | |
| 400 if (!stream.second->is_finished()) { | |
| 401 all_stream_complete = false; | |
| 402 break; | |
| 403 } | |
| 404 } | |
| 405 | |
| 406 // All the stream reader are completed, shut down file IO processing. | 396 // All the stream reader are completed, shut down file IO processing. |
| 407 if (all_stream_complete) { | 397 if (IsDownloadCompleted()) { |
| 408 RecordFileBandwidth(bytes_seen_, disk_writes_time_, | 398 RecordFileBandwidth(bytes_seen_, disk_writes_time_, |
| 409 base::TimeTicks::Now() - download_start_); | 399 base::TimeTicks::Now() - download_start_); |
| 410 weak_factory_.InvalidateWeakPtrs(); | 400 weak_factory_.InvalidateWeakPtrs(); |
| 411 std::unique_ptr<crypto::SecureHash> hash_state = file_.Finish(); | 401 std::unique_ptr<crypto::SecureHash> hash_state = file_.Finish(); |
| 412 update_timer_.reset(); | 402 update_timer_.reset(); |
| 413 BrowserThread::PostTask( | 403 BrowserThread::PostTask( |
| 414 BrowserThread::UI, FROM_HERE, | 404 BrowserThread::UI, FROM_HERE, |
| 415 base::Bind(&DownloadDestinationObserver::DestinationCompleted, | 405 base::Bind(&DownloadDestinationObserver::DestinationCompleted, |
| 416 observer_, TotalBytesReceived(), | 406 observer_, TotalBytesReceived(), |
| 417 base::Passed(&hash_state))); | 407 base::Passed(&hash_state))); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 } else if (source_stream->length() == | 466 } else if (source_stream->length() == |
| 477 DownloadSaveInfo::kLengthFullContent || | 467 DownloadSaveInfo::kLengthFullContent || |
| 478 source_stream->length() > offset - source_stream->offset()) { | 468 source_stream->length() > offset - source_stream->offset()) { |
| 479 // The newly introduced slice will impact the length of the SourceStreams | 469 // The newly introduced slice will impact the length of the SourceStreams |
| 480 // preceding it. | 470 // preceding it. |
| 481 source_stream->set_length(offset - source_stream->offset()); | 471 source_stream->set_length(offset - source_stream->offset()); |
| 482 } | 472 } |
| 483 } | 473 } |
| 484 } | 474 } |
| 485 | 475 |
| 476 bool DownloadFileImpl::IsDownloadCompleted() { |
| 477 SourceStream* stream_for_last_slice = nullptr; |
| 478 int64_t last_slice_offset = 0; |
| 479 for (auto& stream : source_streams_) { |
| 480 SourceStream* source_stream = stream.second.get(); |
| 481 if (source_stream->offset() >= last_slice_offset && |
| 482 source_stream->bytes_written() > 0) { |
| 483 stream_for_last_slice = source_stream; |
| 484 last_slice_offset = source_stream->offset(); |
| 485 } |
| 486 if (!source_stream->is_finished()) |
| 487 return false; |
| 488 } |
| 489 |
| 490 if (!is_sparse_file_) |
| 491 return true; |
| 492 |
| 493 // Verify that all the file slices have been downloaded. |
| 494 std::vector<DownloadItem::ReceivedSlice> slices_to_download = |
| 495 FindSlicesToDownload(received_slices_); |
| 496 if (slices_to_download.size() > 1) { |
| 497 // If there are 1 or more holes in the file, download is not finished. |
| 498 // Some streams might not have been added to |source_streams_| yet. |
| 499 return false; |
| 500 } |
| 501 if (stream_for_last_slice) { |
| 502 DCHECK_EQ(slices_to_download[0].received_bytes, |
| 503 DownloadSaveInfo::kLengthFullContent); |
| 504 // The last stream should not have a length limit. If it has, it might |
| 505 // not reach the end of the file. |
| 506 if (stream_for_last_slice->length() != |
| 507 DownloadSaveInfo::kLengthFullContent) { |
| 508 return false; |
| 509 } |
| 510 DCHECK_EQ(slices_to_download[0].offset, |
| 511 stream_for_last_slice->offset() + |
| 512 stream_for_last_slice->bytes_written()); |
| 513 } |
| 514 |
| 515 return true; |
| 516 } |
| 517 |
| 486 DownloadFileImpl::RenameParameters::RenameParameters( | 518 DownloadFileImpl::RenameParameters::RenameParameters( |
| 487 RenameOption option, | 519 RenameOption option, |
| 488 const base::FilePath& new_path, | 520 const base::FilePath& new_path, |
| 489 const RenameCompletionCallback& completion_callback) | 521 const RenameCompletionCallback& completion_callback) |
| 490 : option(option), | 522 : option(option), |
| 491 new_path(new_path), | 523 new_path(new_path), |
| 492 retries_left(kMaxRenameRetries), | 524 retries_left(kMaxRenameRetries), |
| 493 completion_callback(completion_callback) {} | 525 completion_callback(completion_callback) {} |
| 494 | 526 |
| 495 DownloadFileImpl::RenameParameters::~RenameParameters() {} | 527 DownloadFileImpl::RenameParameters::~RenameParameters() {} |
| 496 | 528 |
| 497 } // namespace content | 529 } // namespace content |
| OLD | NEW |