| 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" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "content/browser/byte_stream.h" | 15 #include "content/browser/byte_stream.h" |
| 16 #include "content/browser/download/download_create_info.h" | 16 #include "content/browser/download/download_create_info.h" |
| 17 #include "content/browser/download/download_destination_observer.h" | 17 #include "content/browser/download/download_destination_observer.h" |
| 18 #include "content/browser/download/download_interrupt_reasons_impl.h" | 18 #include "content/browser/download/download_interrupt_reasons_impl.h" |
| 19 #include "content/browser/download/download_net_log_parameters.h" | 19 #include "content/browser/download/download_net_log_parameters.h" |
| 20 #include "content/browser/download/download_stats.h" | 20 #include "content/browser/download/download_stats.h" |
| 21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "crypto/secure_hash.h" | 22 #include "crypto/secure_hash.h" |
| 23 #include "crypto/sha2.h" | 23 #include "crypto/sha2.h" |
| 24 #include "net/base/io_buffer.h" | 24 #include "net/base/io_buffer.h" |
| 25 #include "net/log/net_log_event_type.h" |
| 26 #include "net/log/net_log_source_type.h" |
| 25 | 27 |
| 26 namespace content { | 28 namespace content { |
| 27 | 29 |
| 28 const int kUpdatePeriodMs = 500; | 30 const int kUpdatePeriodMs = 500; |
| 29 const int kMaxTimeBlockingFileThreadMs = 1000; | 31 const int kMaxTimeBlockingFileThreadMs = 1000; |
| 30 | 32 |
| 31 // These constants control the default retry behavior for failing renames. Each | 33 // These constants control the default retry behavior for failing renames. Each |
| 32 // retry is performed after a delay that is twice the previous delay. The | 34 // retry is performed after a delay that is twice the previous delay. The |
| 33 // initial delay is specified by kInitialRenameRetryDelayMs. | 35 // initial delay is specified by kInitialRenameRetryDelayMs. |
| 34 const int kInitialRenameRetryDelayMs = 200; | 36 const int kInitialRenameRetryDelayMs = 200; |
| 35 | 37 |
| 36 // Number of times a failing rename is retried before giving up. | 38 // Number of times a failing rename is retried before giving up. |
| 37 const int kMaxRenameRetries = 3; | 39 const int kMaxRenameRetries = 3; |
| 38 | 40 |
| 39 DownloadFileImpl::DownloadFileImpl( | 41 DownloadFileImpl::DownloadFileImpl( |
| 40 std::unique_ptr<DownloadSaveInfo> save_info, | 42 std::unique_ptr<DownloadSaveInfo> save_info, |
| 41 const base::FilePath& default_download_directory, | 43 const base::FilePath& default_download_directory, |
| 42 std::unique_ptr<ByteStreamReader> stream, | 44 std::unique_ptr<ByteStreamReader> stream, |
| 43 const net::BoundNetLog& download_item_net_log, | 45 const net::BoundNetLog& download_item_net_log, |
| 44 base::WeakPtr<DownloadDestinationObserver> observer) | 46 base::WeakPtr<DownloadDestinationObserver> observer) |
| 45 : bound_net_log_(net::BoundNetLog::Make(download_item_net_log.net_log(), | 47 : bound_net_log_(net::BoundNetLog::Make(download_item_net_log.net_log(), |
| 46 net::NetLog::SOURCE_DOWNLOAD_FILE)), | 48 net::NetLogSourceType::DOWNLOAD_FILE)), |
| 47 file_(bound_net_log_), | 49 file_(bound_net_log_), |
| 48 save_info_(std::move(save_info)), | 50 save_info_(std::move(save_info)), |
| 49 default_download_directory_(default_download_directory), | 51 default_download_directory_(default_download_directory), |
| 50 stream_reader_(std::move(stream)), | 52 stream_reader_(std::move(stream)), |
| 51 bytes_seen_(0), | 53 bytes_seen_(0), |
| 52 observer_(observer), | 54 observer_(observer), |
| 53 weak_factory_(this) { | 55 weak_factory_(this) { |
| 54 download_item_net_log.AddEvent( | 56 download_item_net_log.AddEvent( |
| 55 net::NetLog::TYPE_DOWNLOAD_FILE_CREATED, | 57 net::NetLogEventType::DOWNLOAD_FILE_CREATED, |
| 56 bound_net_log_.source().ToEventParametersCallback()); | 58 bound_net_log_.source().ToEventParametersCallback()); |
| 57 bound_net_log_.BeginEvent( | 59 bound_net_log_.BeginEvent( |
| 58 net::NetLog::TYPE_DOWNLOAD_FILE_ACTIVE, | 60 net::NetLogEventType::DOWNLOAD_FILE_ACTIVE, |
| 59 download_item_net_log.source().ToEventParametersCallback()); | 61 download_item_net_log.source().ToEventParametersCallback()); |
| 60 } | 62 } |
| 61 | 63 |
| 62 DownloadFileImpl::~DownloadFileImpl() { | 64 DownloadFileImpl::~DownloadFileImpl() { |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 65 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 64 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_FILE_ACTIVE); | 66 bound_net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_ACTIVE); |
| 65 } | 67 } |
| 66 | 68 |
| 67 void DownloadFileImpl::Initialize(const InitializeCallback& callback) { | 69 void DownloadFileImpl::Initialize(const InitializeCallback& callback) { |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 70 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 69 | 71 |
| 70 update_timer_.reset(new base::RepeatingTimer()); | 72 update_timer_.reset(new base::RepeatingTimer()); |
| 71 DownloadInterruptReason result = | 73 DownloadInterruptReason result = |
| 72 file_.Initialize(save_info_->file_path, | 74 file_.Initialize(save_info_->file_path, |
| 73 default_download_directory_, | 75 default_download_directory_, |
| 74 std::move(save_info_->file), | 76 std::move(save_info_->file), |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 BrowserThread::PostTask( | 328 BrowserThread::PostTask( |
| 327 BrowserThread::UI, | 329 BrowserThread::UI, |
| 328 FROM_HERE, | 330 FROM_HERE, |
| 329 base::Bind(&DownloadDestinationObserver::DestinationCompleted, | 331 base::Bind(&DownloadDestinationObserver::DestinationCompleted, |
| 330 observer_, | 332 observer_, |
| 331 file_.bytes_so_far(), | 333 file_.bytes_so_far(), |
| 332 base::Passed(&hash_state))); | 334 base::Passed(&hash_state))); |
| 333 } | 335 } |
| 334 if (bound_net_log_.IsCapturing()) { | 336 if (bound_net_log_.IsCapturing()) { |
| 335 bound_net_log_.AddEvent( | 337 bound_net_log_.AddEvent( |
| 336 net::NetLog::TYPE_DOWNLOAD_STREAM_DRAINED, | 338 net::NetLogEventType::DOWNLOAD_STREAM_DRAINED, |
| 337 base::Bind(&FileStreamDrainedNetLogCallback, total_incoming_data_size, | 339 base::Bind(&FileStreamDrainedNetLogCallback, total_incoming_data_size, |
| 338 num_buffers)); | 340 num_buffers)); |
| 339 } | 341 } |
| 340 } | 342 } |
| 341 | 343 |
| 342 void DownloadFileImpl::SendUpdate() { | 344 void DownloadFileImpl::SendUpdate() { |
| 343 BrowserThread::PostTask( | 345 BrowserThread::PostTask( |
| 344 BrowserThread::UI, | 346 BrowserThread::UI, |
| 345 FROM_HERE, | 347 FROM_HERE, |
| 346 base::Bind(&DownloadDestinationObserver::DestinationUpdate, | 348 base::Bind(&DownloadDestinationObserver::DestinationUpdate, |
| 347 observer_, | 349 observer_, |
| 348 file_.bytes_so_far(), | 350 file_.bytes_so_far(), |
| 349 rate_estimator_.GetCountPerSecond())); | 351 rate_estimator_.GetCountPerSecond())); |
| 350 } | 352 } |
| 351 | 353 |
| 352 DownloadFileImpl::RenameParameters::RenameParameters( | 354 DownloadFileImpl::RenameParameters::RenameParameters( |
| 353 RenameOption option, | 355 RenameOption option, |
| 354 const base::FilePath& new_path, | 356 const base::FilePath& new_path, |
| 355 const RenameCompletionCallback& completion_callback) | 357 const RenameCompletionCallback& completion_callback) |
| 356 : option(option), | 358 : option(option), |
| 357 new_path(new_path), | 359 new_path(new_path), |
| 358 retries_left(kMaxRenameRetries), | 360 retries_left(kMaxRenameRetries), |
| 359 completion_callback(completion_callback) {} | 361 completion_callback(completion_callback) {} |
| 360 | 362 |
| 361 DownloadFileImpl::RenameParameters::~RenameParameters() {} | 363 DownloadFileImpl::RenameParameters::~RenameParameters() {} |
| 362 | 364 |
| 363 } // namespace content | 365 } // namespace content |
| OLD | NEW |