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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 } | 144 } |
145 | 145 |
146 DownloadInterruptReason DownloadFileImpl::WriteDataToFile(int64_t offset, | 146 DownloadInterruptReason DownloadFileImpl::WriteDataToFile(int64_t offset, |
147 const char* data, | 147 const char* data, |
148 size_t data_len) { | 148 size_t data_len) { |
149 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 149 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
150 WillWriteToDisk(data_len); | 150 WillWriteToDisk(data_len); |
151 return file_.WriteDataToFile(offset, data, data_len); | 151 return file_.WriteDataToFile(offset, data, data_len); |
152 } | 152 } |
153 | 153 |
154 bool DownloadFileImpl::CalculateBytesToWrite( | |
155 SourceStream* source_stream, | |
156 size_t bytes_read, | |
157 size_t *bytes_to_write) { | |
158 // If a new slice find that its target position has already been written, | |
159 // terminate the stream. | |
160 if (source_stream->bytes_written() == 0) { | |
161 for (const auto& received_slice : received_slices_) { | |
162 if (received_slice.offset <= source_stream->offset() && | |
163 received_slice.offset + received_slice.received_bytes > | |
164 source_stream->offset()) { | |
165 *bytes_to_write = 0; | |
166 return true; | |
167 } | |
168 } | |
169 } | |
170 | |
171 if (source_stream->length() != DownloadSaveInfo::kLengthFullContent && | |
xingliu
2017/03/11 00:11:46
nit%: Maybe add a comment for this block.
Write a
qinmin
2017/03/14 23:24:50
Done.
| |
172 source_stream->bytes_written() + | |
173 static_cast<int64_t>(bytes_read) >= source_stream->length()) { | |
174 *bytes_to_write = source_stream->length() - source_stream->bytes_written(); | |
175 return true; | |
176 } | |
177 | |
178 *bytes_to_write = bytes_read; | |
179 return false; | |
180 } | |
181 | |
154 void DownloadFileImpl::RenameAndUniquify( | 182 void DownloadFileImpl::RenameAndUniquify( |
155 const base::FilePath& full_path, | 183 const base::FilePath& full_path, |
156 const RenameCompletionCallback& callback) { | 184 const RenameCompletionCallback& callback) { |
157 std::unique_ptr<RenameParameters> parameters( | 185 std::unique_ptr<RenameParameters> parameters( |
158 new RenameParameters(UNIQUIFY, full_path, callback)); | 186 new RenameParameters(UNIQUIFY, full_path, callback)); |
159 RenameWithRetryInternal(std::move(parameters)); | 187 RenameWithRetryInternal(std::move(parameters)); |
160 } | 188 } |
161 | 189 |
162 void DownloadFileImpl::RenameAndAnnotate( | 190 void DownloadFileImpl::RenameAndAnnotate( |
163 const base::FilePath& full_path, | 191 const base::FilePath& full_path, |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 } | 308 } |
281 | 309 |
282 void DownloadFileImpl::StreamActive(SourceStream* source_stream) { | 310 void DownloadFileImpl::StreamActive(SourceStream* source_stream) { |
283 DCHECK(source_stream->stream_reader()); | 311 DCHECK(source_stream->stream_reader()); |
284 base::TimeTicks start(base::TimeTicks::Now()); | 312 base::TimeTicks start(base::TimeTicks::Now()); |
285 base::TimeTicks now; | 313 base::TimeTicks now; |
286 scoped_refptr<net::IOBuffer> incoming_data; | 314 scoped_refptr<net::IOBuffer> incoming_data; |
287 size_t incoming_data_size = 0; | 315 size_t incoming_data_size = 0; |
288 size_t total_incoming_data_size = 0; | 316 size_t total_incoming_data_size = 0; |
289 size_t num_buffers = 0; | 317 size_t num_buffers = 0; |
318 size_t bytes_to_write = 0; | |
290 bool should_terminate = false; | 319 bool should_terminate = false; |
291 ByteStreamReader::StreamState state(ByteStreamReader::STREAM_EMPTY); | 320 ByteStreamReader::StreamState state(ByteStreamReader::STREAM_EMPTY); |
292 DownloadInterruptReason reason = DOWNLOAD_INTERRUPT_REASON_NONE; | 321 DownloadInterruptReason reason = DOWNLOAD_INTERRUPT_REASON_NONE; |
293 base::TimeDelta delta( | 322 base::TimeDelta delta( |
294 base::TimeDelta::FromMilliseconds(kMaxTimeBlockingFileThreadMs)); | 323 base::TimeDelta::FromMilliseconds(kMaxTimeBlockingFileThreadMs)); |
295 | 324 |
296 // Take care of any file local activity required. | 325 // Take care of any file local activity required. |
297 do { | 326 do { |
298 state = source_stream->stream_reader()->Read(&incoming_data, | 327 state = source_stream->stream_reader()->Read(&incoming_data, |
299 &incoming_data_size); | 328 &incoming_data_size); |
300 | 329 |
301 switch (state) { | 330 switch (state) { |
302 case ByteStreamReader::STREAM_EMPTY: | 331 case ByteStreamReader::STREAM_EMPTY: |
303 break; | 332 break; |
304 case ByteStreamReader::STREAM_HAS_DATA: | 333 case ByteStreamReader::STREAM_HAS_DATA: |
305 { | 334 { |
306 ++num_buffers; | 335 ++num_buffers; |
307 base::TimeTicks write_start(base::TimeTicks::Now()); | 336 base::TimeTicks write_start(base::TimeTicks::Now()); |
308 // Stop the stream if it writes more bytes than expected. | 337 should_terminate = CalculateBytesToWrite( |
309 if (source_stream->length() != DownloadSaveInfo::kLengthFullContent && | 338 source_stream, incoming_data_size, &bytes_to_write); |
310 source_stream->bytes_written() + | 339 |
311 static_cast<int64_t>(incoming_data_size) >= | |
312 source_stream->length()) { | |
313 should_terminate = true; | |
314 incoming_data_size = | |
315 source_stream->length() - source_stream->bytes_written(); | |
316 } | |
317 reason = WriteDataToFile( | 340 reason = WriteDataToFile( |
318 source_stream->offset() + source_stream->bytes_written(), | 341 source_stream->offset() + source_stream->bytes_written(), |
319 incoming_data.get()->data(), incoming_data_size); | 342 incoming_data.get()->data(), bytes_to_write); |
320 disk_writes_time_ += (base::TimeTicks::Now() - write_start); | 343 disk_writes_time_ += (base::TimeTicks::Now() - write_start); |
321 bytes_seen_ += incoming_data_size; | 344 bytes_seen_ += bytes_to_write; |
322 total_incoming_data_size += incoming_data_size; | 345 total_incoming_data_size += bytes_to_write; |
323 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) | 346 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
324 source_stream->OnWriteBytesToDisk(incoming_data_size); | 347 source_stream->OnWriteBytesToDisk(bytes_to_write); |
325 } | 348 } |
326 break; | 349 break; |
327 case ByteStreamReader::STREAM_COMPLETE: | 350 case ByteStreamReader::STREAM_COMPLETE: |
328 { | 351 { |
329 reason = static_cast<DownloadInterruptReason>( | 352 reason = static_cast<DownloadInterruptReason>( |
330 source_stream->stream_reader()->GetStatus()); | 353 source_stream->stream_reader()->GetStatus()); |
331 SendUpdate(); | 354 SendUpdate(); |
332 } | 355 } |
333 break; | 356 break; |
334 default: | 357 default: |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
445 const base::FilePath& new_path, | 468 const base::FilePath& new_path, |
446 const RenameCompletionCallback& completion_callback) | 469 const RenameCompletionCallback& completion_callback) |
447 : option(option), | 470 : option(option), |
448 new_path(new_path), | 471 new_path(new_path), |
449 retries_left(kMaxRenameRetries), | 472 retries_left(kMaxRenameRetries), |
450 completion_callback(completion_callback) {} | 473 completion_callback(completion_callback) {} |
451 | 474 |
452 DownloadFileImpl::RenameParameters::~RenameParameters() {} | 475 DownloadFileImpl::RenameParameters::~RenameParameters() {} |
453 | 476 |
454 } // namespace content | 477 } // namespace content |
OLD | NEW |