| 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 // File method ordering: Methods in this file are in the same order as | 5 // File method ordering: Methods in this file are in the same order as |
| 6 // in download_item_impl.h, with the following exception: The public | 6 // in download_item_impl.h, with the following exception: The public |
| 7 // interface Start is placed in chronological order with the other | 7 // interface Start is placed in chronological order with the other |
| 8 // (private) routines that together define a DownloadItem's state | 8 // (private) routines that together define a DownloadItem's state |
| 9 // transitions as the download progresses. See "Download progression | 9 // transitions as the download progresses. See "Download progression |
| 10 // cascade" later in this file. | 10 // cascade" later in this file. |
| 11 | 11 |
| 12 // A regular DownloadItem (created for a download in this session of the | 12 // A regular DownloadItem (created for a download in this session of the |
| 13 // browser) normally goes through the following states: | 13 // browser) normally goes through the following states: |
| 14 // * Created (when download starts) | 14 // * Created (when download starts) |
| 15 // * Destination filename determined | 15 // * Destination filename determined |
| 16 // * Entered into the history database. | 16 // * Entered into the history database. |
| 17 // * Made visible in the download shelf. | 17 // * Made visible in the download shelf. |
| 18 // * All the data is saved. Note that the actual data download occurs | 18 // * All the data is saved. Note that the actual data download occurs |
| 19 // in parallel with the above steps, but until those steps are | 19 // in parallel with the above steps, but until those steps are |
| 20 // complete, the state of the data save will be ignored. | 20 // complete, the state of the data save will be ignored. |
| 21 // * Download file is renamed to its final name, and possibly | 21 // * Download file is renamed to its final name, and possibly |
| 22 // auto-opened. | 22 // auto-opened. |
| 23 | 23 |
| 24 #include "content/browser/download/download_item_impl.h" | 24 #include "content/browser/download/download_item_impl.h" |
| 25 | 25 |
| 26 #include <vector> | 26 #include <vector> |
| 27 | 27 |
| 28 #include "base/basictypes.h" | |
| 29 #include "base/bind.h" | 28 #include "base/bind.h" |
| 30 #include "base/files/file_util.h" | 29 #include "base/files/file_util.h" |
| 31 #include "base/format_macros.h" | 30 #include "base/format_macros.h" |
| 32 #include "base/logging.h" | 31 #include "base/logging.h" |
| 33 #include "base/metrics/histogram.h" | 32 #include "base/metrics/histogram.h" |
| 34 #include "base/stl_util.h" | 33 #include "base/stl_util.h" |
| 35 #include "base/strings/stringprintf.h" | 34 #include "base/strings/stringprintf.h" |
| 36 #include "base/strings/utf_string_conversions.h" | 35 #include "base/strings/utf_string_conversions.h" |
| 37 #include "content/browser/download/download_create_info.h" | 36 #include "content/browser/download/download_create_info.h" |
| 38 #include "content/browser/download/download_file.h" | 37 #include "content/browser/download/download_file.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 89 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 91 download_file->Cancel(); | 90 download_file->Cancel(); |
| 92 } | 91 } |
| 93 | 92 |
| 94 bool IsDownloadResumptionEnabled() { | 93 bool IsDownloadResumptionEnabled() { |
| 95 return base::FeatureList::IsEnabled(features::kDownloadResumption); | 94 return base::FeatureList::IsEnabled(features::kDownloadResumption); |
| 96 } | 95 } |
| 97 | 96 |
| 98 } // namespace | 97 } // namespace |
| 99 | 98 |
| 100 const uint32 DownloadItem::kInvalidId = 0; | 99 const uint32_t DownloadItem::kInvalidId = 0; |
| 101 | 100 |
| 102 const char DownloadItem::kEmptyFileHash[] = ""; | 101 const char DownloadItem::kEmptyFileHash[] = ""; |
| 103 | 102 |
| 104 // The maximum number of attempts we will make to resume automatically. | 103 // The maximum number of attempts we will make to resume automatically. |
| 105 const int DownloadItemImpl::kMaxAutoResumeAttempts = 5; | 104 const int DownloadItemImpl::kMaxAutoResumeAttempts = 5; |
| 106 | 105 |
| 107 // Constructor for reading from the history service. | 106 // Constructor for reading from the history service. |
| 108 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate, | 107 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate, |
| 109 uint32 download_id, | 108 uint32_t download_id, |
| 110 const base::FilePath& current_path, | 109 const base::FilePath& current_path, |
| 111 const base::FilePath& target_path, | 110 const base::FilePath& target_path, |
| 112 const std::vector<GURL>& url_chain, | 111 const std::vector<GURL>& url_chain, |
| 113 const GURL& referrer_url, | 112 const GURL& referrer_url, |
| 114 const std::string& mime_type, | 113 const std::string& mime_type, |
| 115 const std::string& original_mime_type, | 114 const std::string& original_mime_type, |
| 116 const base::Time& start_time, | 115 const base::Time& start_time, |
| 117 const base::Time& end_time, | 116 const base::Time& end_time, |
| 118 const std::string& etag, | 117 const std::string& etag, |
| 119 const std::string& last_modified, | 118 const std::string& last_modified, |
| 120 int64 received_bytes, | 119 int64_t received_bytes, |
| 121 int64 total_bytes, | 120 int64_t total_bytes, |
| 122 DownloadItem::DownloadState state, | 121 DownloadItem::DownloadState state, |
| 123 DownloadDangerType danger_type, | 122 DownloadDangerType danger_type, |
| 124 DownloadInterruptReason interrupt_reason, | 123 DownloadInterruptReason interrupt_reason, |
| 125 bool opened, | 124 bool opened, |
| 126 const net::BoundNetLog& bound_net_log) | 125 const net::BoundNetLog& bound_net_log) |
| 127 : is_save_package_download_(false), | 126 : is_save_package_download_(false), |
| 128 download_id_(download_id), | 127 download_id_(download_id), |
| 129 current_path_(current_path), | 128 current_path_(current_path), |
| 130 target_path_(target_path), | 129 target_path_(target_path), |
| 131 target_disposition_(TARGET_DISPOSITION_OVERWRITE), | 130 target_disposition_(TARGET_DISPOSITION_OVERWRITE), |
| (...skipping 26 matching lines...) Expand all Loading... |
| 158 opened_(opened), | 157 opened_(opened), |
| 159 delegate_delayed_complete_(false), | 158 delegate_delayed_complete_(false), |
| 160 bound_net_log_(bound_net_log), | 159 bound_net_log_(bound_net_log), |
| 161 weak_ptr_factory_(this) { | 160 weak_ptr_factory_(this) { |
| 162 delegate_->Attach(); | 161 delegate_->Attach(); |
| 163 DCHECK_NE(IN_PROGRESS_INTERNAL, state_); | 162 DCHECK_NE(IN_PROGRESS_INTERNAL, state_); |
| 164 Init(false /* not actively downloading */, SRC_HISTORY_IMPORT); | 163 Init(false /* not actively downloading */, SRC_HISTORY_IMPORT); |
| 165 } | 164 } |
| 166 | 165 |
| 167 // Constructing for a regular download: | 166 // Constructing for a regular download: |
| 168 DownloadItemImpl::DownloadItemImpl( | 167 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate, |
| 169 DownloadItemImplDelegate* delegate, | 168 uint32_t download_id, |
| 170 uint32 download_id, | 169 const DownloadCreateInfo& info, |
| 171 const DownloadCreateInfo& info, | 170 const net::BoundNetLog& bound_net_log) |
| 172 const net::BoundNetLog& bound_net_log) | |
| 173 : is_save_package_download_(false), | 171 : is_save_package_download_(false), |
| 174 download_id_(download_id), | 172 download_id_(download_id), |
| 175 target_disposition_( | 173 target_disposition_((info.save_info->prompt_for_save_location) |
| 176 (info.save_info->prompt_for_save_location) ? | 174 ? TARGET_DISPOSITION_PROMPT |
| 177 TARGET_DISPOSITION_PROMPT : TARGET_DISPOSITION_OVERWRITE), | 175 : TARGET_DISPOSITION_OVERWRITE), |
| 178 url_chain_(info.url_chain), | 176 url_chain_(info.url_chain), |
| 179 referrer_url_(info.referrer_url), | 177 referrer_url_(info.referrer_url), |
| 180 tab_url_(info.tab_url), | 178 tab_url_(info.tab_url), |
| 181 tab_referrer_url_(info.tab_referrer_url), | 179 tab_referrer_url_(info.tab_referrer_url), |
| 182 suggested_filename_(base::UTF16ToUTF8(info.save_info->suggested_name)), | 180 suggested_filename_(base::UTF16ToUTF8(info.save_info->suggested_name)), |
| 183 forced_file_path_(info.save_info->file_path), | 181 forced_file_path_(info.save_info->file_path), |
| 184 transition_type_(info.transition_type), | 182 transition_type_(info.transition_type), |
| 185 has_user_gesture_(info.has_user_gesture), | 183 has_user_gesture_(info.has_user_gesture), |
| 186 content_disposition_(info.content_disposition), | 184 content_disposition_(info.content_disposition), |
| 187 mime_type_(info.mime_type), | 185 mime_type_(info.mime_type), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 info.request_bound_net_log.source().ToEventParametersCallback()); | 217 info.request_bound_net_log.source().ToEventParametersCallback()); |
| 220 | 218 |
| 221 info.request_bound_net_log.AddEvent( | 219 info.request_bound_net_log.AddEvent( |
| 222 net::NetLog::TYPE_DOWNLOAD_STARTED, | 220 net::NetLog::TYPE_DOWNLOAD_STARTED, |
| 223 bound_net_log_.source().ToEventParametersCallback()); | 221 bound_net_log_.source().ToEventParametersCallback()); |
| 224 } | 222 } |
| 225 | 223 |
| 226 // Constructing for the "Save Page As..." feature: | 224 // Constructing for the "Save Page As..." feature: |
| 227 DownloadItemImpl::DownloadItemImpl( | 225 DownloadItemImpl::DownloadItemImpl( |
| 228 DownloadItemImplDelegate* delegate, | 226 DownloadItemImplDelegate* delegate, |
| 229 uint32 download_id, | 227 uint32_t download_id, |
| 230 const base::FilePath& path, | 228 const base::FilePath& path, |
| 231 const GURL& url, | 229 const GURL& url, |
| 232 const std::string& mime_type, | 230 const std::string& mime_type, |
| 233 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 231 scoped_ptr<DownloadRequestHandleInterface> request_handle, |
| 234 const net::BoundNetLog& bound_net_log) | 232 const net::BoundNetLog& bound_net_log) |
| 235 : is_save_package_download_(true), | 233 : is_save_package_download_(true), |
| 236 request_handle_(request_handle.Pass()), | 234 request_handle_(request_handle.Pass()), |
| 237 download_id_(download_id), | 235 download_id_(download_id), |
| 238 current_path_(path), | 236 current_path_(path), |
| 239 target_path_(path), | 237 target_path_(path), |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); | 466 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); |
| 469 delegate_->OpenDownload(this); | 467 delegate_->OpenDownload(this); |
| 470 } | 468 } |
| 471 | 469 |
| 472 void DownloadItemImpl::ShowDownloadInShell() { | 470 void DownloadItemImpl::ShowDownloadInShell() { |
| 473 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 471 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 474 | 472 |
| 475 delegate_->ShowDownloadInShell(this); | 473 delegate_->ShowDownloadInShell(this); |
| 476 } | 474 } |
| 477 | 475 |
| 478 uint32 DownloadItemImpl::GetId() const { | 476 uint32_t DownloadItemImpl::GetId() const { |
| 479 return download_id_; | 477 return download_id_; |
| 480 } | 478 } |
| 481 | 479 |
| 482 DownloadItem::DownloadState DownloadItemImpl::GetState() const { | 480 DownloadItem::DownloadState DownloadItemImpl::GetState() const { |
| 483 return InternalToExternalState(state_); | 481 return InternalToExternalState(state_); |
| 484 } | 482 } |
| 485 | 483 |
| 486 DownloadInterruptReason DownloadItemImpl::GetLastReason() const { | 484 DownloadInterruptReason DownloadItemImpl::GetLastReason() const { |
| 487 return last_reason_; | 485 return last_reason_; |
| 488 } | 486 } |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 } | 670 } |
| 673 | 671 |
| 674 DownloadDangerType DownloadItemImpl::GetDangerType() const { | 672 DownloadDangerType DownloadItemImpl::GetDangerType() const { |
| 675 return danger_type_; | 673 return danger_type_; |
| 676 } | 674 } |
| 677 | 675 |
| 678 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { | 676 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { |
| 679 if (total_bytes_ <= 0) | 677 if (total_bytes_ <= 0) |
| 680 return false; // We never received the content_length for this download. | 678 return false; // We never received the content_length for this download. |
| 681 | 679 |
| 682 int64 speed = CurrentSpeed(); | 680 int64_t speed = CurrentSpeed(); |
| 683 if (speed == 0) | 681 if (speed == 0) |
| 684 return false; | 682 return false; |
| 685 | 683 |
| 686 *remaining = base::TimeDelta::FromSeconds( | 684 *remaining = base::TimeDelta::FromSeconds( |
| 687 (total_bytes_ - received_bytes_) / speed); | 685 (total_bytes_ - received_bytes_) / speed); |
| 688 return true; | 686 return true; |
| 689 } | 687 } |
| 690 | 688 |
| 691 int64 DownloadItemImpl::CurrentSpeed() const { | 689 int64_t DownloadItemImpl::CurrentSpeed() const { |
| 692 if (is_paused_) | 690 if (is_paused_) |
| 693 return 0; | 691 return 0; |
| 694 return bytes_per_sec_; | 692 return bytes_per_sec_; |
| 695 } | 693 } |
| 696 | 694 |
| 697 int DownloadItemImpl::PercentComplete() const { | 695 int DownloadItemImpl::PercentComplete() const { |
| 698 // If the delegate is delaying completion of the download, then we have no | 696 // If the delegate is delaying completion of the download, then we have no |
| 699 // idea how long it will take. | 697 // idea how long it will take. |
| 700 if (delegate_delayed_complete_ || total_bytes_ <= 0) | 698 if (delegate_delayed_complete_ || total_bytes_ <= 0) |
| 701 return -1; | 699 return -1; |
| 702 | 700 |
| 703 return static_cast<int>(received_bytes_ * 100.0 / total_bytes_); | 701 return static_cast<int>(received_bytes_ * 100.0 / total_bytes_); |
| 704 } | 702 } |
| 705 | 703 |
| 706 bool DownloadItemImpl::AllDataSaved() const { | 704 bool DownloadItemImpl::AllDataSaved() const { |
| 707 return all_data_saved_; | 705 return all_data_saved_; |
| 708 } | 706 } |
| 709 | 707 |
| 710 int64 DownloadItemImpl::GetTotalBytes() const { | 708 int64_t DownloadItemImpl::GetTotalBytes() const { |
| 711 return total_bytes_; | 709 return total_bytes_; |
| 712 } | 710 } |
| 713 | 711 |
| 714 int64 DownloadItemImpl::GetReceivedBytes() const { | 712 int64_t DownloadItemImpl::GetReceivedBytes() const { |
| 715 return received_bytes_; | 713 return received_bytes_; |
| 716 } | 714 } |
| 717 | 715 |
| 718 base::Time DownloadItemImpl::GetStartTime() const { | 716 base::Time DownloadItemImpl::GetStartTime() const { |
| 719 return start_time_; | 717 return start_time_; |
| 720 } | 718 } |
| 721 | 719 |
| 722 base::Time DownloadItemImpl::GetEndTime() const { | 720 base::Time DownloadItemImpl::GetEndTime() const { |
| 723 return end_time_; | 721 return end_time_; |
| 724 } | 722 } |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 982 | 980 |
| 983 base::WeakPtr<DownloadDestinationObserver> | 981 base::WeakPtr<DownloadDestinationObserver> |
| 984 DownloadItemImpl::DestinationObserverAsWeakPtr() { | 982 DownloadItemImpl::DestinationObserverAsWeakPtr() { |
| 985 return weak_ptr_factory_.GetWeakPtr(); | 983 return weak_ptr_factory_.GetWeakPtr(); |
| 986 } | 984 } |
| 987 | 985 |
| 988 const net::BoundNetLog& DownloadItemImpl::GetBoundNetLog() const { | 986 const net::BoundNetLog& DownloadItemImpl::GetBoundNetLog() const { |
| 989 return bound_net_log_; | 987 return bound_net_log_; |
| 990 } | 988 } |
| 991 | 989 |
| 992 void DownloadItemImpl::SetTotalBytes(int64 total_bytes) { | 990 void DownloadItemImpl::SetTotalBytes(int64_t total_bytes) { |
| 993 total_bytes_ = total_bytes; | 991 total_bytes_ = total_bytes; |
| 994 } | 992 } |
| 995 | 993 |
| 996 void DownloadItemImpl::OnAllDataSaved(const std::string& final_hash) { | 994 void DownloadItemImpl::OnAllDataSaved(const std::string& final_hash) { |
| 997 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 995 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 998 | 996 |
| 999 DCHECK_EQ(IN_PROGRESS_INTERNAL, state_); | 997 DCHECK_EQ(IN_PROGRESS_INTERNAL, state_); |
| 1000 DCHECK(!all_data_saved_); | 998 DCHECK(!all_data_saved_); |
| 1001 all_data_saved_ = true; | 999 all_data_saved_ = true; |
| 1002 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 1000 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
| 1003 | 1001 |
| 1004 // Store final hash and null out intermediate serialized hash state. | 1002 // Store final hash and null out intermediate serialized hash state. |
| 1005 hash_ = final_hash; | 1003 hash_ = final_hash; |
| 1006 hash_state_ = ""; | 1004 hash_state_ = ""; |
| 1007 | 1005 |
| 1008 UpdateObservers(); | 1006 UpdateObservers(); |
| 1009 } | 1007 } |
| 1010 | 1008 |
| 1011 void DownloadItemImpl::MarkAsComplete() { | 1009 void DownloadItemImpl::MarkAsComplete() { |
| 1012 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1010 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1013 | 1011 |
| 1014 DCHECK(all_data_saved_); | 1012 DCHECK(all_data_saved_); |
| 1015 end_time_ = base::Time::Now(); | 1013 end_time_ = base::Time::Now(); |
| 1016 TransitionTo(COMPLETE_INTERNAL, UPDATE_OBSERVERS); | 1014 TransitionTo(COMPLETE_INTERNAL, UPDATE_OBSERVERS); |
| 1017 } | 1015 } |
| 1018 | 1016 |
| 1019 void DownloadItemImpl::DestinationUpdate(int64 bytes_so_far, | 1017 void DownloadItemImpl::DestinationUpdate(int64_t bytes_so_far, |
| 1020 int64 bytes_per_sec, | 1018 int64_t bytes_per_sec, |
| 1021 const std::string& hash_state) { | 1019 const std::string& hash_state) { |
| 1022 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1020 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1023 DVLOG(20) << __FUNCTION__ << " so_far=" << bytes_so_far | 1021 DVLOG(20) << __FUNCTION__ << " so_far=" << bytes_so_far |
| 1024 << " per_sec=" << bytes_per_sec << " download=" | 1022 << " per_sec=" << bytes_per_sec << " download=" |
| 1025 << DebugString(true); | 1023 << DebugString(true); |
| 1026 | 1024 |
| 1027 if (GetState() != IN_PROGRESS) { | 1025 if (GetState() != IN_PROGRESS) { |
| 1028 // Ignore if we're no longer in-progress. This can happen if we race a | 1026 // Ignore if we're no longer in-progress. This can happen if we race a |
| 1029 // Cancel on the UI thread with an update on the FILE thread. | 1027 // Cancel on the UI thread with an update on the FILE thread. |
| 1030 // | 1028 // |
| (...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1787 case RESUME_MODE_USER_CONTINUE: | 1785 case RESUME_MODE_USER_CONTINUE: |
| 1788 return "USER_CONTINUE"; | 1786 return "USER_CONTINUE"; |
| 1789 case RESUME_MODE_USER_RESTART: | 1787 case RESUME_MODE_USER_RESTART: |
| 1790 return "USER_RESTART"; | 1788 return "USER_RESTART"; |
| 1791 } | 1789 } |
| 1792 NOTREACHED() << "Unknown resume mode " << mode; | 1790 NOTREACHED() << "Unknown resume mode " << mode; |
| 1793 return "unknown"; | 1791 return "unknown"; |
| 1794 } | 1792 } |
| 1795 | 1793 |
| 1796 } // namespace content | 1794 } // namespace content |
| OLD | NEW |