| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 } | 52 } |
| 53 | 53 |
| 54 void DownloadFileImpl::SourceStream::OnWriteBytesToDisk(int64_t bytes_write) { | 54 void DownloadFileImpl::SourceStream::OnWriteBytesToDisk(int64_t bytes_write) { |
| 55 bytes_written_ += bytes_write; | 55 bytes_written_ += bytes_write; |
| 56 } | 56 } |
| 57 | 57 |
| 58 DownloadFileImpl::DownloadFileImpl( | 58 DownloadFileImpl::DownloadFileImpl( |
| 59 std::unique_ptr<DownloadSaveInfo> save_info, | 59 std::unique_ptr<DownloadSaveInfo> save_info, |
| 60 const base::FilePath& default_download_directory, | 60 const base::FilePath& default_download_directory, |
| 61 std::unique_ptr<ByteStreamReader> stream_reader, | 61 std::unique_ptr<ByteStreamReader> stream_reader, |
| 62 const std::vector<DownloadItem::ReceivedSlice>& received_slices, |
| 62 const net::NetLogWithSource& download_item_net_log, | 63 const net::NetLogWithSource& download_item_net_log, |
| 63 bool is_sparse_file, | 64 bool is_sparse_file, |
| 64 base::WeakPtr<DownloadDestinationObserver> observer) | 65 base::WeakPtr<DownloadDestinationObserver> observer) |
| 65 : net_log_( | 66 : net_log_( |
| 66 net::NetLogWithSource::Make(download_item_net_log.net_log(), | 67 net::NetLogWithSource::Make(download_item_net_log.net_log(), |
| 67 net::NetLogSourceType::DOWNLOAD_FILE)), | 68 net::NetLogSourceType::DOWNLOAD_FILE)), |
| 68 file_(net_log_), | 69 file_(net_log_), |
| 69 save_info_(std::move(save_info)), | 70 save_info_(std::move(save_info)), |
| 70 default_download_directory_(default_download_directory), | 71 default_download_directory_(default_download_directory), |
| 71 is_sparse_file_(is_sparse_file), | 72 is_sparse_file_(is_sparse_file), |
| 72 bytes_seen_(0), | 73 bytes_seen_(0), |
| 74 received_slices_(received_slices), |
| 73 observer_(observer), | 75 observer_(observer), |
| 74 weak_factory_(this) { | 76 weak_factory_(this) { |
| 75 source_streams_[save_info_->offset] = base::MakeUnique<SourceStream>( | 77 source_streams_[save_info_->offset] = base::MakeUnique<SourceStream>( |
| 76 save_info_->offset, DownloadSaveInfo::kLengthFullContent); | 78 save_info_->offset, DownloadSaveInfo::kLengthFullContent); |
| 77 DCHECK(source_streams_.size() == static_cast<size_t>(1)); | 79 DCHECK(source_streams_.size() == static_cast<size_t>(1)); |
| 78 source_streams_[save_info_->offset]->SetByteStream(std::move(stream_reader)); | 80 source_streams_[save_info_->offset]->SetByteStream(std::move(stream_reader)); |
| 79 | 81 |
| 80 download_item_net_log.AddEvent( | 82 download_item_net_log.AddEvent( |
| 81 net::NetLogEventType::DOWNLOAD_FILE_CREATED, | 83 net::NetLogEventType::DOWNLOAD_FILE_CREATED, |
| 82 net_log_.source().ToEventParametersCallback()); | 84 net_log_.source().ToEventParametersCallback()); |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 StreamActive(source_stream); | 406 StreamActive(source_stream); |
| 405 } | 407 } |
| 406 } | 408 } |
| 407 | 409 |
| 408 int64_t DownloadFileImpl::TotalBytesReceived() const { | 410 int64_t DownloadFileImpl::TotalBytesReceived() const { |
| 409 // TODO(xingliu): Use slice info to figure out total bytes received. | 411 // TODO(xingliu): Use slice info to figure out total bytes received. |
| 410 return file_.bytes_so_far(); | 412 return file_.bytes_so_far(); |
| 411 } | 413 } |
| 412 | 414 |
| 413 void DownloadFileImpl::SendUpdate() { | 415 void DownloadFileImpl::SendUpdate() { |
| 414 // TODO(xingliu): Update slice info to observer to update history db. | 416 // TODO(qinmin): For each active stream, add the slice it has written so |
| 417 // far along with received_slices_. |
| 415 BrowserThread::PostTask( | 418 BrowserThread::PostTask( |
| 416 BrowserThread::UI, FROM_HERE, | 419 BrowserThread::UI, FROM_HERE, |
| 417 base::Bind(&DownloadDestinationObserver::DestinationUpdate, observer_, | 420 base::Bind(&DownloadDestinationObserver::DestinationUpdate, observer_, |
| 418 TotalBytesReceived(), rate_estimator_.GetCountPerSecond(), | 421 TotalBytesReceived(), rate_estimator_.GetCountPerSecond(), |
| 419 std::vector<DownloadItem::ReceivedSlice>())); | 422 received_slices_)); |
| 420 } | 423 } |
| 421 | 424 |
| 422 void DownloadFileImpl::WillWriteToDisk(size_t data_len) { | 425 void DownloadFileImpl::WillWriteToDisk(size_t data_len) { |
| 423 if (!update_timer_->IsRunning()) { | 426 if (!update_timer_->IsRunning()) { |
| 424 update_timer_->Start(FROM_HERE, | 427 update_timer_->Start(FROM_HERE, |
| 425 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), | 428 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), |
| 426 this, &DownloadFileImpl::SendUpdate); | 429 this, &DownloadFileImpl::SendUpdate); |
| 427 } | 430 } |
| 428 rate_estimator_.Increment(data_len); | 431 rate_estimator_.Increment(data_len); |
| 429 } | 432 } |
| 430 | 433 |
| 431 DownloadFileImpl::RenameParameters::RenameParameters( | 434 DownloadFileImpl::RenameParameters::RenameParameters( |
| 432 RenameOption option, | 435 RenameOption option, |
| 433 const base::FilePath& new_path, | 436 const base::FilePath& new_path, |
| 434 const RenameCompletionCallback& completion_callback) | 437 const RenameCompletionCallback& completion_callback) |
| 435 : option(option), | 438 : option(option), |
| 436 new_path(new_path), | 439 new_path(new_path), |
| 437 retries_left(kMaxRenameRetries), | 440 retries_left(kMaxRenameRetries), |
| 438 completion_callback(completion_callback) {} | 441 completion_callback(completion_callback) {} |
| 439 | 442 |
| 440 DownloadFileImpl::RenameParameters::~RenameParameters() {} | 443 DownloadFileImpl::RenameParameters::~RenameParameters() {} |
| 441 | 444 |
| 442 } // namespace content | 445 } // namespace content |
| OLD | NEW |