Chromium Code Reviews| Index: content/browser/download/download_file_impl.cc |
| diff --git a/content/browser/download/download_file_impl.cc b/content/browser/download/download_file_impl.cc |
| index 869c4e08f95663425c5dd5d37e1fc54c0c556ce3..b87b3d3f6146ddff6fc25a9dd1fc841812f90f36 100644 |
| --- a/content/browser/download/download_file_impl.cc |
| +++ b/content/browser/download/download_file_impl.cc |
| @@ -19,6 +19,7 @@ |
| #include "content/browser/download/download_interrupt_reasons_impl.h" |
| #include "content/browser/download/download_net_log_parameters.h" |
| #include "content/browser/download/download_stats.h" |
| +#include "content/browser/download/parallel_download_utils.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "crypto/secure_hash.h" |
| #include "crypto/sha2.h" |
| @@ -42,7 +43,11 @@ const int kInitialRenameRetryDelayMs = 200; |
| const int kMaxRenameRetries = 3; |
| DownloadFileImpl::SourceStream::SourceStream(int64_t offset, int64_t length) |
| - : offset_(offset), length_(length), bytes_written_(0), finished_(false) {} |
| + : offset_(offset), |
| + length_(length), |
| + bytes_written_(0), |
| + finished_(false), |
| + index_(0) {} |
| DownloadFileImpl::SourceStream::~SourceStream() = default; |
| @@ -312,8 +317,20 @@ void DownloadFileImpl::StreamActive(SourceStream* source_stream) { |
| disk_writes_time_ += (base::TimeTicks::Now() - write_start); |
| bytes_seen_ += incoming_data_size; |
| total_incoming_data_size += incoming_data_size; |
| - if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
| + if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| + int64_t prev_bytes_written = source_stream->bytes_written(); |
| source_stream->OnWriteBytesToDisk(incoming_data_size); |
| + // If the write operation creates a new slice, add it to the |
| + // |received_slices_| and update all the entries in |
| + // |source_streams_|. |
| + int64_t current_bytes_written = source_stream->bytes_written(); |
| + if (current_bytes_written > 0 && prev_bytes_written == 0) { |
| + AddNewSlice(source_stream->offset(), current_bytes_written); |
| + } else { |
| + received_slices_[source_stream->index()].received_bytes = |
| + current_bytes_written; |
| + } |
| + } |
| } |
| break; |
| case ByteStreamReader::STREAM_COMPLETE: |
| @@ -431,6 +448,29 @@ void DownloadFileImpl::WillWriteToDisk(size_t data_len) { |
| rate_estimator_.Increment(data_len); |
| } |
| +void DownloadFileImpl::AddNewSlice(int64_t offset, int64_t length) { |
|
xingliu
2017/03/07 22:18:13
This in general sgtm.
Question: after adding a ne
qinmin
2017/03/08 18:46:33
Yes, we will. We can merge some of the slices tog
xingliu
2017/03/08 19:45:18
Maybe we should test this in very poor network con
|
| + if (!is_sparse_file_) |
| + return; |
| + int index = AddReceivedSliceToSortedArray( |
|
xingliu
2017/03/08 19:45:18
nit%: Just something to consider here, is it possi
qinmin
2017/03/08 21:59:04
This shouldn't happen. A slice will not be added t
|
| + DownloadItem::ReceivedSlice(offset, length), received_slices_); |
| + // Update the index of exising SourceStreams. |
| + for (auto& stream : source_streams_) { |
| + SourceStream* source_stream = stream.second.get(); |
| + if (source_stream->offset() > offset) { |
| + if (source_stream->bytes_written() > 0) |
| + source_stream->set_index(source_stream->index() + 1); |
| + } else if (source_stream->offset() == offset) { |
| + source_stream->set_index(index); |
| + } else if (source_stream->length() == |
| + DownloadSaveInfo::kLengthFullContent || |
| + source_stream->length() > offset - source_stream->offset()) { |
| + // The newly introduced slice will impact the length of the SourceStreams |
| + // preceding it. |
| + source_stream->set_length(offset - source_stream->offset()); |
| + } |
| + } |
| +} |
| + |
| DownloadFileImpl::RenameParameters::RenameParameters( |
| RenameOption option, |
| const base::FilePath& new_path, |