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

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

Issue 2742093002: Glue parallel download job and download file together. (Closed)
Patch Set: Work on feedback. 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"
(...skipping 23 matching lines...) Expand all
34 const int kMaxTimeBlockingFileThreadMs = 1000; 34 const int kMaxTimeBlockingFileThreadMs = 1000;
35 35
36 // These constants control the default retry behavior for failing renames. Each 36 // 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 37 // retry is performed after a delay that is twice the previous delay. The
38 // initial delay is specified by kInitialRenameRetryDelayMs. 38 // initial delay is specified by kInitialRenameRetryDelayMs.
39 const int kInitialRenameRetryDelayMs = 200; 39 const int kInitialRenameRetryDelayMs = 200;
40 40
41 // Number of times a failing rename is retried before giving up. 41 // Number of times a failing rename is retried before giving up.
42 const int kMaxRenameRetries = 3; 42 const int kMaxRenameRetries = 3;
43 43
44 DownloadFileImpl::SourceStream::SourceStream(int64_t offset, int64_t length) 44 DownloadFileImpl::SourceStream::SourceStream(
45 : offset_(offset), length_(length), bytes_written_(0), finished_(false) {} 45 int64_t offset,
46 int64_t length,
47 std::unique_ptr<ByteStreamReader> stream_reader)
48 : offset_(offset),
49 length_(length),
50 bytes_written_(0),
51 finished_(false),
52 stream_reader_(std::move(stream_reader)) {}
46 53
47 DownloadFileImpl::SourceStream::~SourceStream() = default; 54 DownloadFileImpl::SourceStream::~SourceStream() = default;
48 55
49 void DownloadFileImpl::SourceStream::SetByteStream(
50 std::unique_ptr<ByteStreamReader> stream_reader) {
51 stream_reader_ = std::move(stream_reader);
52 }
53
54 void DownloadFileImpl::SourceStream::OnWriteBytesToDisk(int64_t bytes_write) { 56 void DownloadFileImpl::SourceStream::OnWriteBytesToDisk(int64_t bytes_write) {
55 bytes_written_ += bytes_write; 57 bytes_written_ += bytes_write;
56 } 58 }
57 59
58 DownloadFileImpl::DownloadFileImpl( 60 DownloadFileImpl::DownloadFileImpl(
59 std::unique_ptr<DownloadSaveInfo> save_info, 61 std::unique_ptr<DownloadSaveInfo> save_info,
60 const base::FilePath& default_download_directory, 62 const base::FilePath& default_download_directory,
61 std::unique_ptr<ByteStreamReader> stream_reader, 63 std::unique_ptr<ByteStreamReader> stream_reader,
62 const std::vector<DownloadItem::ReceivedSlice>& received_slices, 64 const std::vector<DownloadItem::ReceivedSlice>& received_slices,
63 const net::NetLogWithSource& download_item_net_log, 65 const net::NetLogWithSource& download_item_net_log,
64 bool is_sparse_file, 66 bool is_sparse_file,
65 base::WeakPtr<DownloadDestinationObserver> observer) 67 base::WeakPtr<DownloadDestinationObserver> observer)
66 : net_log_( 68 : net_log_(
67 net::NetLogWithSource::Make(download_item_net_log.net_log(), 69 net::NetLogWithSource::Make(download_item_net_log.net_log(),
68 net::NetLogSourceType::DOWNLOAD_FILE)), 70 net::NetLogSourceType::DOWNLOAD_FILE)),
69 file_(net_log_), 71 file_(net_log_),
70 save_info_(std::move(save_info)), 72 save_info_(std::move(save_info)),
71 default_download_directory_(default_download_directory), 73 default_download_directory_(default_download_directory),
72 is_sparse_file_(is_sparse_file), 74 is_sparse_file_(is_sparse_file),
73 bytes_seen_(0), 75 bytes_seen_(0),
74 received_slices_(received_slices), 76 received_slices_(received_slices),
75 observer_(observer), 77 observer_(observer),
76 weak_factory_(this) { 78 weak_factory_(this) {
77 source_streams_[save_info_->offset] = base::MakeUnique<SourceStream>( 79 source_streams_[save_info_->offset] = base::MakeUnique<SourceStream>(
78 save_info_->offset, DownloadSaveInfo::kLengthFullContent); 80 save_info_->offset, DownloadSaveInfo::kLengthFullContent,
qinmin 2017/03/17 07:54:36 actually, this is no longer true. For resumption,
xingliu 2017/03/17 19:08:25 Good point, done.
79 DCHECK(source_streams_.size() == static_cast<size_t>(1)); 81 std::move(stream_reader));
80 source_streams_[save_info_->offset]->SetByteStream(std::move(stream_reader));
81 82
82 download_item_net_log.AddEvent( 83 download_item_net_log.AddEvent(
83 net::NetLogEventType::DOWNLOAD_FILE_CREATED, 84 net::NetLogEventType::DOWNLOAD_FILE_CREATED,
84 net_log_.source().ToEventParametersCallback()); 85 net_log_.source().ToEventParametersCallback());
85 net_log_.BeginEvent( 86 net_log_.BeginEvent(
86 net::NetLogEventType::DOWNLOAD_FILE_ACTIVE, 87 net::NetLogEventType::DOWNLOAD_FILE_ACTIVE,
87 download_item_net_log.source().ToEventParametersCallback()); 88 download_item_net_log.source().ToEventParametersCallback());
88 } 89 }
89 90
90 DownloadFileImpl::~DownloadFileImpl() { 91 DownloadFileImpl::~DownloadFileImpl() {
(...skipping 22 matching lines...) Expand all
113 BrowserThread::PostTask( 114 BrowserThread::PostTask(
114 BrowserThread::UI, FROM_HERE, base::Bind(callback, result)); 115 BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
115 return; 116 return;
116 } 117 }
117 118
118 download_start_ = base::TimeTicks::Now(); 119 download_start_ = base::TimeTicks::Now();
119 120
120 // Primarily to make reset to zero in restart visible to owner. 121 // Primarily to make reset to zero in restart visible to owner.
121 SendUpdate(); 122 SendUpdate();
122 123
123 // Initial pull from the straw. 124 // Initial pull from the straw from all source streams.
124 for (auto& source_stream : source_streams_) 125 for (auto& source_stream : source_streams_)
125 RegisterAndActivateStream(source_stream.second.get()); 126 RegisterAndActivateStream(source_stream.second.get());
126 127
127 BrowserThread::PostTask( 128 BrowserThread::PostTask(
128 BrowserThread::UI, FROM_HERE, base::Bind( 129 BrowserThread::UI, FROM_HERE, base::Bind(
129 callback, DOWNLOAD_INTERRUPT_REASON_NONE)); 130 callback, DOWNLOAD_INTERRUPT_REASON_NONE));
asanka 2017/03/17 17:10:09 (not for this CL, but since you are in the neighbo
xingliu 2017/03/17 19:08:25 Done, Oh, I see, thanks for this suggestion.
130 } 131 }
131 132
132 void DownloadFileImpl::AddByteStream( 133 void DownloadFileImpl::AddByteStream(
133 std::unique_ptr<ByteStreamReader> stream_reader, 134 std::unique_ptr<ByteStreamReader> stream_reader,
134 int64_t offset) { 135 int64_t offset,
136 int64_t length) {
135 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 137 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
138 DCHECK(source_streams_.find(offset) == source_streams_.end());
136 139
137 // The |source_streams_| must have an existing entry for the stream reader. 140 source_streams_[offset] =
138 auto current_source_stream = source_streams_.find(offset); 141 base::MakeUnique<SourceStream>(offset, length, std::move(stream_reader));
139 DCHECK(current_source_stream != source_streams_.end());
140 SourceStream* stream = current_source_stream->second.get();
141 stream->SetByteStream(std::move(stream_reader));
142 142
143 RegisterAndActivateStream(stream); 143 // If the file is initialized, start to write data, or wait until file opened.
144 if (file_.in_progress())
145 RegisterAndActivateStream(source_streams_[offset].get());
144 } 146 }
145 147
146 DownloadInterruptReason DownloadFileImpl::WriteDataToFile(int64_t offset, 148 DownloadInterruptReason DownloadFileImpl::WriteDataToFile(int64_t offset,
147 const char* data, 149 const char* data,
148 size_t data_len) { 150 size_t data_len) {
149 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 151 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
150 WillWriteToDisk(data_len); 152 WillWriteToDisk(data_len);
151 return file_.WriteDataToFile(offset, data, data_len); 153 return file_.WriteDataToFile(offset, data, data_len);
152 } 154 }
153 155
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 const base::FilePath& new_path, 447 const base::FilePath& new_path,
446 const RenameCompletionCallback& completion_callback) 448 const RenameCompletionCallback& completion_callback)
447 : option(option), 449 : option(option),
448 new_path(new_path), 450 new_path(new_path),
449 retries_left(kMaxRenameRetries), 451 retries_left(kMaxRenameRetries),
450 completion_callback(completion_callback) {} 452 completion_callback(completion_callback) {}
451 453
452 DownloadFileImpl::RenameParameters::~RenameParameters() {} 454 DownloadFileImpl::RenameParameters::~RenameParameters() {}
453 455
454 } // namespace content 456 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698