| 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 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 base::TimeDelta delta( | 400 base::TimeDelta delta( |
| 401 base::TimeDelta::FromMilliseconds(kMaxTimeBlockingFileThreadMs)); | 401 base::TimeDelta::FromMilliseconds(kMaxTimeBlockingFileThreadMs)); |
| 402 | 402 |
| 403 // Take care of any file local activity required. | 403 // Take care of any file local activity required. |
| 404 do { | 404 do { |
| 405 state = source_stream->stream_reader()->Read(&incoming_data, | 405 state = source_stream->stream_reader()->Read(&incoming_data, |
| 406 &incoming_data_size); | 406 &incoming_data_size); |
| 407 | 407 |
| 408 switch (state) { | 408 switch (state) { |
| 409 case ByteStreamReader::STREAM_EMPTY: | 409 case ByteStreamReader::STREAM_EMPTY: |
| 410 should_terminate = (source_stream->length() == kNoBytesToWrite); |
| 410 break; | 411 break; |
| 411 case ByteStreamReader::STREAM_HAS_DATA: | 412 case ByteStreamReader::STREAM_HAS_DATA: |
| 412 { | 413 { |
| 413 ++num_buffers; | 414 ++num_buffers; |
| 414 base::TimeTicks write_start(base::TimeTicks::Now()); | 415 base::TimeTicks write_start(base::TimeTicks::Now()); |
| 415 should_terminate = CalculateBytesToWrite( | 416 should_terminate = CalculateBytesToWrite( |
| 416 source_stream, incoming_data_size, &bytes_to_write); | 417 source_stream, incoming_data_size, &bytes_to_write); |
| 417 DCHECK_GE(incoming_data_size, bytes_to_write); | 418 DCHECK_GE(incoming_data_size, bytes_to_write); |
| 418 reason = WriteDataToFile( | 419 reason = WriteDataToFile( |
| 419 source_stream->offset() + source_stream->bytes_written(), | 420 source_stream->offset() + source_stream->bytes_written(), |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 ByteStreamReader* stream_reader = source_stream->stream_reader(); | 520 ByteStreamReader* stream_reader = source_stream->stream_reader(); |
| 520 if (stream_reader) { | 521 if (stream_reader) { |
| 521 stream_reader->RegisterCallback(base::Bind(&DownloadFileImpl::StreamActive, | 522 stream_reader->RegisterCallback(base::Bind(&DownloadFileImpl::StreamActive, |
| 522 weak_factory_.GetWeakPtr(), | 523 weak_factory_.GetWeakPtr(), |
| 523 source_stream)); | 524 source_stream)); |
| 524 // Truncate |source_stream|'s length if necessary. | 525 // Truncate |source_stream|'s length if necessary. |
| 525 for (const auto& received_slice : received_slices_) { | 526 for (const auto& received_slice : received_slices_) { |
| 526 source_stream->TruncateLengthWithWrittenDataBlock( | 527 source_stream->TruncateLengthWithWrittenDataBlock( |
| 527 received_slice.offset, received_slice.received_bytes); | 528 received_slice.offset, received_slice.received_bytes); |
| 528 } | 529 } |
| 530 num_active_streams_++; |
| 529 StreamActive(source_stream); | 531 StreamActive(source_stream); |
| 530 num_active_streams_++; | |
| 531 } | 532 } |
| 532 } | 533 } |
| 533 | 534 |
| 534 int64_t DownloadFileImpl::TotalBytesReceived() const { | 535 int64_t DownloadFileImpl::TotalBytesReceived() const { |
| 535 return file_.bytes_so_far(); | 536 return file_.bytes_so_far(); |
| 536 } | 537 } |
| 537 | 538 |
| 538 void DownloadFileImpl::SendUpdate() { | 539 void DownloadFileImpl::SendUpdate() { |
| 539 // TODO(qinmin): For each active stream, add the slice it has written so | 540 // TODO(qinmin): For each active stream, add the slice it has written so |
| 540 // far along with received_slices_. | 541 // far along with received_slices_. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 return TotalBytesReceived() == potential_file_length_; | 604 return TotalBytesReceived() == potential_file_length_; |
| 604 } | 605 } |
| 605 | 606 |
| 606 void DownloadFileImpl::HandleStreamError(SourceStream* source_stream, | 607 void DownloadFileImpl::HandleStreamError(SourceStream* source_stream, |
| 607 DownloadInterruptReason reason) { | 608 DownloadInterruptReason reason) { |
| 608 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 609 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 609 source_stream->stream_reader()->RegisterCallback(base::Closure()); | 610 source_stream->stream_reader()->RegisterCallback(base::Closure()); |
| 610 source_stream->set_finished(true); | 611 source_stream->set_finished(true); |
| 611 num_active_streams_--; | 612 num_active_streams_--; |
| 612 | 613 |
| 613 bool can_recover_from_error = false; | 614 bool can_recover_from_error = (source_stream->length() == kNoBytesToWrite); |
| 614 | 615 |
| 615 if (IsSparseFile() && source_stream->length() != kNoBytesToWrite) { | 616 if (IsSparseFile() && !can_recover_from_error) { |
| 616 // If a neighboring stream request is available, check if it can help | 617 // If a neighboring stream request is available, check if it can help |
| 617 // download all the data left by |source stream| or has already done so. We | 618 // download all the data left by |source stream| or has already done so. We |
| 618 // want to avoid the situation that a server always fail additional requests | 619 // want to avoid the situation that a server always fail additional requests |
| 619 // from the client thus causing the initial request and the download going | 620 // from the client thus causing the initial request and the download going |
| 620 // nowhere. | 621 // nowhere. |
| 621 // TODO(qinmin): make all streams half open so that they can recover | 622 // TODO(qinmin): make all streams half open so that they can recover |
| 622 // failures from their neighbors. | 623 // failures from their neighbors. |
| 623 SourceStream* preceding_neighbor = FindPrecedingNeighbor(source_stream); | 624 SourceStream* preceding_neighbor = FindPrecedingNeighbor(source_stream); |
| 624 while (preceding_neighbor) { | 625 while (preceding_neighbor) { |
| 625 int64_t upper_range = source_stream->offset() + source_stream->length(); | 626 int64_t upper_range = source_stream->offset() + source_stream->length(); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 const base::FilePath& new_path, | 716 const base::FilePath& new_path, |
| 716 const RenameCompletionCallback& completion_callback) | 717 const RenameCompletionCallback& completion_callback) |
| 717 : option(option), | 718 : option(option), |
| 718 new_path(new_path), | 719 new_path(new_path), |
| 719 retries_left(kMaxRenameRetries), | 720 retries_left(kMaxRenameRetries), |
| 720 completion_callback(completion_callback) {} | 721 completion_callback(completion_callback) {} |
| 721 | 722 |
| 722 DownloadFileImpl::RenameParameters::~RenameParameters() {} | 723 DownloadFileImpl::RenameParameters::~RenameParameters() {} |
| 723 | 724 |
| 724 } // namespace content | 725 } // namespace content |
| OLD | NEW |