Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: content/browser/download/download_file_impl.cc

Issue 2737033002: Update the received slices vector when stream is written to disk (Closed)
Patch Set: nit Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 #include "content/browser/byte_stream.h" 16 #include "content/browser/byte_stream.h"
17 #include "content/browser/download/download_create_info.h" 17 #include "content/browser/download/download_create_info.h"
18 #include "content/browser/download/download_destination_observer.h" 18 #include "content/browser/download/download_destination_observer.h"
19 #include "content/browser/download/download_interrupt_reasons_impl.h" 19 #include "content/browser/download/download_interrupt_reasons_impl.h"
20 #include "content/browser/download/download_net_log_parameters.h" 20 #include "content/browser/download/download_net_log_parameters.h"
21 #include "content/browser/download/download_stats.h" 21 #include "content/browser/download/download_stats.h"
22 #include "content/browser/download/parallel_download_utils.h"
22 #include "content/public/browser/browser_thread.h" 23 #include "content/public/browser/browser_thread.h"
23 #include "crypto/secure_hash.h" 24 #include "crypto/secure_hash.h"
24 #include "crypto/sha2.h" 25 #include "crypto/sha2.h"
25 #include "net/base/io_buffer.h" 26 #include "net/base/io_buffer.h"
26 #include "net/log/net_log.h" 27 #include "net/log/net_log.h"
27 #include "net/log/net_log_event_type.h" 28 #include "net/log/net_log_event_type.h"
28 #include "net/log/net_log_source.h" 29 #include "net/log/net_log_source.h"
29 #include "net/log/net_log_source_type.h" 30 #include "net/log/net_log_source_type.h"
30 31
31 namespace content { 32 namespace content {
32 33
33 const int kUpdatePeriodMs = 500; 34 const int kUpdatePeriodMs = 500;
34 const int kMaxTimeBlockingFileThreadMs = 1000; 35 const int kMaxTimeBlockingFileThreadMs = 1000;
35 36
36 // These constants control the default retry behavior for failing renames. Each 37 // These constants control the default retry behavior for failing renames. Each
37 // retry is performed after a delay that is twice the previous delay. The 38 // retry is performed after a delay that is twice the previous delay. The
38 // initial delay is specified by kInitialRenameRetryDelayMs. 39 // initial delay is specified by kInitialRenameRetryDelayMs.
39 const int kInitialRenameRetryDelayMs = 200; 40 const int kInitialRenameRetryDelayMs = 200;
40 41
41 // Number of times a failing rename is retried before giving up. 42 // Number of times a failing rename is retried before giving up.
42 const int kMaxRenameRetries = 3; 43 const int kMaxRenameRetries = 3;
43 44
44 DownloadFileImpl::SourceStream::SourceStream(int64_t offset, int64_t length) 45 DownloadFileImpl::SourceStream::SourceStream(int64_t offset, int64_t length)
45 : offset_(offset), length_(length), bytes_written_(0), finished_(false) {} 46 : offset_(offset),
47 length_(length),
48 bytes_written_(0),
49 finished_(false),
50 index_(0u) {}
46 51
47 DownloadFileImpl::SourceStream::~SourceStream() = default; 52 DownloadFileImpl::SourceStream::~SourceStream() = default;
48 53
49 void DownloadFileImpl::SourceStream::SetByteStream( 54 void DownloadFileImpl::SourceStream::SetByteStream(
50 std::unique_ptr<ByteStreamReader> stream_reader) { 55 std::unique_ptr<ByteStreamReader> stream_reader) {
51 stream_reader_ = std::move(stream_reader); 56 stream_reader_ = std::move(stream_reader);
52 } 57 }
53 58
54 void DownloadFileImpl::SourceStream::OnWriteBytesToDisk(int64_t bytes_write) { 59 void DownloadFileImpl::SourceStream::OnWriteBytesToDisk(int64_t bytes_write) {
55 bytes_written_ += bytes_write; 60 bytes_written_ += bytes_write;
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 should_terminate = true; 318 should_terminate = true;
314 incoming_data_size = 319 incoming_data_size =
315 source_stream->length() - source_stream->bytes_written(); 320 source_stream->length() - source_stream->bytes_written();
316 } 321 }
317 reason = WriteDataToFile( 322 reason = WriteDataToFile(
318 source_stream->offset() + source_stream->bytes_written(), 323 source_stream->offset() + source_stream->bytes_written(),
319 incoming_data.get()->data(), incoming_data_size); 324 incoming_data.get()->data(), incoming_data_size);
320 disk_writes_time_ += (base::TimeTicks::Now() - write_start); 325 disk_writes_time_ += (base::TimeTicks::Now() - write_start);
321 bytes_seen_ += incoming_data_size; 326 bytes_seen_ += incoming_data_size;
322 total_incoming_data_size += incoming_data_size; 327 total_incoming_data_size += incoming_data_size;
323 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) 328 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) {
329 int64_t prev_bytes_written = source_stream->bytes_written();
324 source_stream->OnWriteBytesToDisk(incoming_data_size); 330 source_stream->OnWriteBytesToDisk(incoming_data_size);
331 if (!is_sparse_file_)
332 break;
333 // If the write operation creates a new slice, add it to the
334 // |received_slices_| and update all the entries in
335 // |source_streams_|.
336 if (incoming_data_size > 0 && prev_bytes_written == 0) {
337 AddNewSlice(source_stream->offset(), incoming_data_size);
338 } else {
339 received_slices_[source_stream->index()].received_bytes +=
340 incoming_data_size;
341 }
342 }
325 } 343 }
326 break; 344 break;
327 case ByteStreamReader::STREAM_COMPLETE: 345 case ByteStreamReader::STREAM_COMPLETE:
328 { 346 {
329 reason = static_cast<DownloadInterruptReason>( 347 reason = static_cast<DownloadInterruptReason>(
330 source_stream->stream_reader()->GetStatus()); 348 source_stream->stream_reader()->GetStatus());
331 SendUpdate(); 349 SendUpdate();
332 } 350 }
333 break; 351 break;
334 default: 352 default:
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 451
434 void DownloadFileImpl::WillWriteToDisk(size_t data_len) { 452 void DownloadFileImpl::WillWriteToDisk(size_t data_len) {
435 if (!update_timer_->IsRunning()) { 453 if (!update_timer_->IsRunning()) {
436 update_timer_->Start(FROM_HERE, 454 update_timer_->Start(FROM_HERE,
437 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), 455 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs),
438 this, &DownloadFileImpl::SendUpdate); 456 this, &DownloadFileImpl::SendUpdate);
439 } 457 }
440 rate_estimator_.Increment(data_len); 458 rate_estimator_.Increment(data_len);
441 } 459 }
442 460
461 void DownloadFileImpl::AddNewSlice(int64_t offset, int64_t length) {
462 if (!is_sparse_file_)
463 return;
464 size_t index = AddOrMergeReceivedSliceIntoSortedArray(
465 DownloadItem::ReceivedSlice(offset, length), received_slices_);
466 // Check if the slice is added as a new slice, or merged with an existing one.
467 bool slice_added = (offset == received_slices_[index].offset);
468 // Update the index of exising SourceStreams.
469 for (auto& stream : source_streams_) {
470 SourceStream* source_stream = stream.second.get();
471 if (source_stream->offset() > offset) {
472 if (slice_added && source_stream->bytes_written() > 0)
473 source_stream->set_index(source_stream->index() + 1);
474 } else if (source_stream->offset() == offset) {
475 source_stream->set_index(index);
476 } else if (source_stream->length() ==
477 DownloadSaveInfo::kLengthFullContent ||
478 source_stream->length() > offset - source_stream->offset()) {
479 // The newly introduced slice will impact the length of the SourceStreams
480 // preceding it.
481 source_stream->set_length(offset - source_stream->offset());
482 }
483 }
484 }
485
443 DownloadFileImpl::RenameParameters::RenameParameters( 486 DownloadFileImpl::RenameParameters::RenameParameters(
444 RenameOption option, 487 RenameOption option,
445 const base::FilePath& new_path, 488 const base::FilePath& new_path,
446 const RenameCompletionCallback& completion_callback) 489 const RenameCompletionCallback& completion_callback)
447 : option(option), 490 : option(option),
448 new_path(new_path), 491 new_path(new_path),
449 retries_left(kMaxRenameRetries), 492 retries_left(kMaxRenameRetries),
450 completion_callback(completion_callback) {} 493 completion_callback(completion_callback) {}
451 494
452 DownloadFileImpl::RenameParameters::~RenameParameters() {} 495 DownloadFileImpl::RenameParameters::~RenameParameters() {}
453 496
454 } // namespace content 497 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/download_file_impl.h ('k') | content/browser/download/download_file_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698