| 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 net::NetLogEventType::DOWNLOAD_FILE_ACTIVE, | 121 net::NetLogEventType::DOWNLOAD_FILE_ACTIVE, |
| 122 download_item_net_log.source().ToEventParametersCallback()); | 122 download_item_net_log.source().ToEventParametersCallback()); |
| 123 } | 123 } |
| 124 | 124 |
| 125 DownloadFileImpl::~DownloadFileImpl() { | 125 DownloadFileImpl::~DownloadFileImpl() { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 126 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 127 net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_ACTIVE); | 127 net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_ACTIVE); |
| 128 } | 128 } |
| 129 | 129 |
| 130 void DownloadFileImpl::Initialize( | 130 void DownloadFileImpl::Initialize( |
| 131 const InitializeCallback& callback, | 131 const InitializeCallback& initialize_callback, |
| 132 const CancelRequestCallback& cancel_request_callback, |
| 132 const DownloadItem::ReceivedSlices& received_slices) { | 133 const DownloadItem::ReceivedSlices& received_slices) { |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 134 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 134 | 135 |
| 135 update_timer_.reset(new base::RepeatingTimer()); | 136 update_timer_.reset(new base::RepeatingTimer()); |
| 136 int64_t bytes_so_far = 0; | 137 int64_t bytes_so_far = 0; |
| 138 cancel_request_callback_ = cancel_request_callback; |
| 137 received_slices_ = received_slices; | 139 received_slices_ = received_slices; |
| 138 if (IsSparseFile()) { | 140 if (IsSparseFile()) { |
| 139 for (const auto& received_slice : received_slices_) { | 141 for (const auto& received_slice : received_slices_) { |
| 140 bytes_so_far += received_slice.received_bytes; | 142 bytes_so_far += received_slice.received_bytes; |
| 141 } | 143 } |
| 142 } else { | 144 } else { |
| 143 bytes_so_far = save_info_->offset; | 145 bytes_so_far = save_info_->offset; |
| 144 } | 146 } |
| 145 DownloadInterruptReason result = file_.Initialize( | 147 DownloadInterruptReason result = file_.Initialize( |
| 146 save_info_->file_path, default_download_directory_, | 148 save_info_->file_path, default_download_directory_, |
| 147 std::move(save_info_->file), bytes_so_far, | 149 std::move(save_info_->file), bytes_so_far, |
| 148 save_info_->hash_of_partial_file, std::move(save_info_->hash_state), | 150 save_info_->hash_of_partial_file, std::move(save_info_->hash_state), |
| 149 IsSparseFile()); | 151 IsSparseFile()); |
| 150 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) { | 152 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 151 BrowserThread::PostTask( | 153 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 152 BrowserThread::UI, FROM_HERE, base::Bind(callback, result)); | 154 base::Bind(initialize_callback, result)); |
| 153 return; | 155 return; |
| 154 } | 156 } |
| 155 | 157 |
| 156 download_start_ = base::TimeTicks::Now(); | 158 download_start_ = base::TimeTicks::Now(); |
| 157 last_update_time_ = download_start_; | 159 last_update_time_ = download_start_; |
| 158 | 160 |
| 159 // Primarily to make reset to zero in restart visible to owner. | 161 // Primarily to make reset to zero in restart visible to owner. |
| 160 SendUpdate(); | 162 SendUpdate(); |
| 161 | 163 |
| 162 BrowserThread::PostTask( | 164 BrowserThread::PostTask( |
| 163 BrowserThread::UI, FROM_HERE, base::Bind( | 165 BrowserThread::UI, FROM_HERE, |
| 164 callback, DOWNLOAD_INTERRUPT_REASON_NONE)); | 166 base::Bind(initialize_callback, DOWNLOAD_INTERRUPT_REASON_NONE)); |
| 165 | 167 |
| 166 // Initial pull from the straw from all source streams. | 168 // Initial pull from the straw from all source streams. |
| 167 for (auto& source_stream : source_streams_) | 169 for (auto& source_stream : source_streams_) |
| 168 RegisterAndActivateStream(source_stream.second.get()); | 170 RegisterAndActivateStream(source_stream.second.get()); |
| 169 } | 171 } |
| 170 | 172 |
| 171 void DownloadFileImpl::AddByteStream( | 173 void DownloadFileImpl::AddByteStream( |
| 172 std::unique_ptr<ByteStreamReader> stream_reader, | 174 std::unique_ptr<ByteStreamReader> stream_reader, |
| 173 int64_t offset, | 175 int64_t offset, |
| 174 int64_t length) { | 176 int64_t length) { |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 parameters->source_url, | 323 parameters->source_url, |
| 322 parameters->referrer_url); | 324 parameters->referrer_url); |
| 323 } | 325 } |
| 324 | 326 |
| 325 if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) { | 327 if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 326 // Make sure our information is updated, since we're about to | 328 // Make sure our information is updated, since we're about to |
| 327 // error out. | 329 // error out. |
| 328 SendUpdate(); | 330 SendUpdate(); |
| 329 | 331 |
| 330 // Null out callback so that we don't do any more stream processing. | 332 // Null out callback so that we don't do any more stream processing. |
| 333 // The request that writes to the pipe should be canceled after |
| 334 // the download being interrupted. |
| 331 for (auto& stream : source_streams_) { | 335 for (auto& stream : source_streams_) { |
| 332 ByteStreamReader* stream_reader = stream.second->stream_reader(); | 336 ByteStreamReader* stream_reader = stream.second->stream_reader(); |
| 333 if (stream_reader) | 337 if (stream_reader) |
| 334 stream_reader->RegisterCallback(base::Closure()); | 338 stream_reader->RegisterCallback(base::Closure()); |
| 335 } | 339 } |
| 336 | 340 |
| 337 new_path.clear(); | 341 new_path.clear(); |
| 338 } | 342 } |
| 339 | 343 |
| 340 BrowserThread::PostTask( | 344 BrowserThread::PostTask( |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 | 466 |
| 463 RecordContiguousWriteTime(now - start); | 467 RecordContiguousWriteTime(now - start); |
| 464 | 468 |
| 465 // Take care of communication with our observer. | 469 // Take care of communication with our observer. |
| 466 if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) { | 470 if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 467 HandleStreamError(source_stream, reason); | 471 HandleStreamError(source_stream, reason); |
| 468 } else if (state == ByteStreamReader::STREAM_COMPLETE || should_terminate) { | 472 } else if (state == ByteStreamReader::STREAM_COMPLETE || should_terminate) { |
| 469 // Signal successful completion or termination of the current stream. | 473 // Signal successful completion or termination of the current stream. |
| 470 source_stream->stream_reader()->RegisterCallback(base::Closure()); | 474 source_stream->stream_reader()->RegisterCallback(base::Closure()); |
| 471 source_stream->set_finished(true); | 475 source_stream->set_finished(true); |
| 476 if (should_terminate) |
| 477 CancelRequestOnUIThread(source_stream->offset()); |
| 472 if (source_stream->length() == DownloadSaveInfo::kLengthFullContent) { | 478 if (source_stream->length() == DownloadSaveInfo::kLengthFullContent) { |
| 473 SetPotentialFileLength(source_stream->offset() + | 479 SetPotentialFileLength(source_stream->offset() + |
| 474 source_stream->bytes_written()); | 480 source_stream->bytes_written()); |
| 475 } | 481 } |
| 476 num_active_streams_--; | 482 num_active_streams_--; |
| 477 | 483 |
| 478 // Inform observers. | 484 // Inform observers. |
| 479 SendUpdate(); | 485 SendUpdate(); |
| 480 | 486 |
| 481 // All the stream reader are completed, shut down file IO processing. | 487 // All the stream reader are completed, shut down file IO processing. |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 if (can_recover_from_error) { | 640 if (can_recover_from_error) { |
| 635 // Since the neighbor stream will download all data downloading from its | 641 // Since the neighbor stream will download all data downloading from its |
| 636 // offset to source_stream->offset(). Close all other streams in the | 642 // offset to source_stream->offset(). Close all other streams in the |
| 637 // middle. | 643 // middle. |
| 638 for (auto& stream : source_streams_) { | 644 for (auto& stream : source_streams_) { |
| 639 if (stream.second->offset() < source_stream->offset() && | 645 if (stream.second->offset() < source_stream->offset() && |
| 640 stream.second->offset() > preceding_neighbor->offset()) { | 646 stream.second->offset() > preceding_neighbor->offset()) { |
| 641 DCHECK_EQ(stream.second->bytes_written(), 0); | 647 DCHECK_EQ(stream.second->bytes_written(), 0); |
| 642 stream.second->stream_reader()->RegisterCallback(base::Closure()); | 648 stream.second->stream_reader()->RegisterCallback(base::Closure()); |
| 643 stream.second->set_finished(true); | 649 stream.second->set_finished(true); |
| 650 CancelRequestOnUIThread(stream.second->offset()); |
| 644 num_active_streams_--; | 651 num_active_streams_--; |
| 645 } | 652 } |
| 646 } | 653 } |
| 647 } | 654 } |
| 648 } | 655 } |
| 649 | 656 |
| 650 SendUpdate(); // Make info up to date before error. | 657 SendUpdate(); // Make info up to date before error. |
| 651 | 658 |
| 652 if (!can_recover_from_error) { | 659 if (!can_recover_from_error) { |
| 653 // Error case for both upstream source and file write. | 660 // Error case for both upstream source and file write. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 673 for (auto& stream : source_streams_) { | 680 for (auto& stream : source_streams_) { |
| 674 int64_t offset = stream.second->offset(); | 681 int64_t offset = stream.second->offset(); |
| 675 if (offset < source_stream->offset() && offset >= max_preceding_offset) { | 682 if (offset < source_stream->offset() && offset >= max_preceding_offset) { |
| 676 ret = stream.second.get(); | 683 ret = stream.second.get(); |
| 677 max_preceding_offset = offset; | 684 max_preceding_offset = offset; |
| 678 } | 685 } |
| 679 } | 686 } |
| 680 return ret; | 687 return ret; |
| 681 } | 688 } |
| 682 | 689 |
| 690 void DownloadFileImpl::CancelRequestOnUIThread(int64_t offset) { |
| 691 if (!cancel_request_callback_.is_null()) { |
| 692 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 693 base::Bind(cancel_request_callback_, offset)); |
| 694 } |
| 695 } |
| 696 |
| 683 void DownloadFileImpl::DebugStates() const { | 697 void DownloadFileImpl::DebugStates() const { |
| 684 DVLOG(1) << "### Debugging DownloadFile states:"; | 698 DVLOG(1) << "### Debugging DownloadFile states:"; |
| 685 DVLOG(1) << "Total source stream count = " << source_streams_.size(); | 699 DVLOG(1) << "Total source stream count = " << source_streams_.size(); |
| 686 for (const auto& stream : source_streams_) { | 700 for (const auto& stream : source_streams_) { |
| 687 DVLOG(1) << "Source stream, offset = " << stream.second->offset() | 701 DVLOG(1) << "Source stream, offset = " << stream.second->offset() |
| 688 << " , bytes_written = " << stream.second->bytes_written() | 702 << " , bytes_written = " << stream.second->bytes_written() |
| 689 << " , is_finished = " << stream.second->is_finished() | 703 << " , is_finished = " << stream.second->is_finished() |
| 690 << " , length = " << stream.second->length(); | 704 << " , length = " << stream.second->length(); |
| 691 } | 705 } |
| 692 | 706 |
| 693 DebugSlicesInfo(received_slices_); | 707 DebugSlicesInfo(received_slices_); |
| 694 } | 708 } |
| 695 | 709 |
| 696 DownloadFileImpl::RenameParameters::RenameParameters( | 710 DownloadFileImpl::RenameParameters::RenameParameters( |
| 697 RenameOption option, | 711 RenameOption option, |
| 698 const base::FilePath& new_path, | 712 const base::FilePath& new_path, |
| 699 const RenameCompletionCallback& completion_callback) | 713 const RenameCompletionCallback& completion_callback) |
| 700 : option(option), | 714 : option(option), |
| 701 new_path(new_path), | 715 new_path(new_path), |
| 702 retries_left(kMaxRenameRetries), | 716 retries_left(kMaxRenameRetries), |
| 703 completion_callback(completion_callback) {} | 717 completion_callback(completion_callback) {} |
| 704 | 718 |
| 705 DownloadFileImpl::RenameParameters::~RenameParameters() {} | 719 DownloadFileImpl::RenameParameters::~RenameParameters() {} |
| 706 | 720 |
| 707 } // namespace content | 721 } // namespace content |
| OLD | NEW |