| 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_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 14 #include "base/i18n/string_search.h" | 14 #include "base/i18n/string_search.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "content/browser/download/download_create_info.h" | 20 #include "content/browser/download/download_create_info.h" |
| 21 #include "content/browser/download/download_file.h" | 21 #include "content/browser/download/download_file.h" |
| 22 #include "content/browser/download/download_file_manager.h" | 22 #include "content/browser/download/download_file_manager.h" |
| 23 #include "content/browser/download/download_interrupt_reasons_impl.h" |
| 23 #include "content/browser/download/download_persistent_store_info.h" | 24 #include "content/browser/download/download_persistent_store_info.h" |
| 24 #include "content/browser/download/download_request_handle.h" | 25 #include "content/browser/download/download_request_handle.h" |
| 25 #include "content/browser/download/download_stats.h" | 26 #include "content/browser/download/download_stats.h" |
| 26 #include "content/browser/download/interrupt_reasons.h" | |
| 27 #include "content/browser/tab_contents/tab_contents.h" | 27 #include "content/browser/tab_contents/tab_contents.h" |
| 28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/content_browser_client.h" | 29 #include "content/public/browser/content_browser_client.h" |
| 30 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
| 31 | 31 |
| 32 using content::BrowserThread; | 32 using content::BrowserThread; |
| 33 using content::DownloadFile; | 33 using content::DownloadFile; |
| 34 using content::DownloadId; | 34 using content::DownloadId; |
| 35 using content::DownloadItem; | 35 using content::DownloadItem; |
| 36 using content::DownloadManager; | 36 using content::DownloadManager; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 DownloadId download_id, | 162 DownloadId download_id, |
| 163 const DownloadPersistentStoreInfo& info, | 163 const DownloadPersistentStoreInfo& info, |
| 164 const net::BoundNetLog& bound_net_log) | 164 const net::BoundNetLog& bound_net_log) |
| 165 : download_id_(download_id), | 165 : download_id_(download_id), |
| 166 full_path_(info.path), | 166 full_path_(info.path), |
| 167 url_chain_(1, info.url), | 167 url_chain_(1, info.url), |
| 168 referrer_url_(info.referrer_url), | 168 referrer_url_(info.referrer_url), |
| 169 total_bytes_(info.total_bytes), | 169 total_bytes_(info.total_bytes), |
| 170 received_bytes_(info.received_bytes), | 170 received_bytes_(info.received_bytes), |
| 171 bytes_per_sec_(0), | 171 bytes_per_sec_(0), |
| 172 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 172 last_reason_(content::DOWNLOAD_INTERRUPT_REASON_NONE), |
| 173 start_tick_(base::TimeTicks()), | 173 start_tick_(base::TimeTicks()), |
| 174 state_(static_cast<DownloadState>(info.state)), | 174 state_(static_cast<DownloadState>(info.state)), |
| 175 start_time_(info.start_time), | 175 start_time_(info.start_time), |
| 176 end_time_(info.end_time), | 176 end_time_(info.end_time), |
| 177 db_handle_(info.db_handle), | 177 db_handle_(info.db_handle), |
| 178 delegate_(delegate), | 178 delegate_(delegate), |
| 179 is_paused_(false), | 179 is_paused_(false), |
| 180 open_when_complete_(false), | 180 open_when_complete_(false), |
| 181 file_externally_removed_(false), | 181 file_externally_removed_(false), |
| 182 safety_state_(SAFE), | 182 safety_state_(SAFE), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 referrer_url_(info.referrer_url), | 214 referrer_url_(info.referrer_url), |
| 215 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), | 215 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), |
| 216 content_disposition_(info.content_disposition), | 216 content_disposition_(info.content_disposition), |
| 217 mime_type_(info.mime_type), | 217 mime_type_(info.mime_type), |
| 218 original_mime_type_(info.original_mime_type), | 218 original_mime_type_(info.original_mime_type), |
| 219 referrer_charset_(info.referrer_charset), | 219 referrer_charset_(info.referrer_charset), |
| 220 remote_address_(info.remote_address), | 220 remote_address_(info.remote_address), |
| 221 total_bytes_(info.total_bytes), | 221 total_bytes_(info.total_bytes), |
| 222 received_bytes_(0), | 222 received_bytes_(0), |
| 223 bytes_per_sec_(0), | 223 bytes_per_sec_(0), |
| 224 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 224 last_reason_(content::DOWNLOAD_INTERRUPT_REASON_NONE), |
| 225 start_tick_(base::TimeTicks::Now()), | 225 start_tick_(base::TimeTicks::Now()), |
| 226 state_(IN_PROGRESS), | 226 state_(IN_PROGRESS), |
| 227 start_time_(info.start_time), | 227 start_time_(info.start_time), |
| 228 db_handle_(DownloadItem::kUninitializedHandle), | 228 db_handle_(DownloadItem::kUninitializedHandle), |
| 229 delegate_(delegate), | 229 delegate_(delegate), |
| 230 is_paused_(false), | 230 is_paused_(false), |
| 231 open_when_complete_(false), | 231 open_when_complete_(false), |
| 232 file_externally_removed_(false), | 232 file_externally_removed_(false), |
| 233 safety_state_(SAFE), | 233 safety_state_(SAFE), |
| 234 auto_opened_(false), | 234 auto_opened_(false), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 DownloadId download_id, | 266 DownloadId download_id, |
| 267 const net::BoundNetLog& bound_net_log) | 267 const net::BoundNetLog& bound_net_log) |
| 268 : request_handle_(new NullDownloadRequestHandle()), | 268 : request_handle_(new NullDownloadRequestHandle()), |
| 269 download_id_(download_id), | 269 download_id_(download_id), |
| 270 full_path_(path), | 270 full_path_(path), |
| 271 url_chain_(1, url), | 271 url_chain_(1, url), |
| 272 referrer_url_(GURL()), | 272 referrer_url_(GURL()), |
| 273 total_bytes_(0), | 273 total_bytes_(0), |
| 274 received_bytes_(0), | 274 received_bytes_(0), |
| 275 bytes_per_sec_(0), | 275 bytes_per_sec_(0), |
| 276 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 276 last_reason_(content::DOWNLOAD_INTERRUPT_REASON_NONE), |
| 277 start_tick_(base::TimeTicks::Now()), | 277 start_tick_(base::TimeTicks::Now()), |
| 278 state_(IN_PROGRESS), | 278 state_(IN_PROGRESS), |
| 279 start_time_(base::Time::Now()), | 279 start_time_(base::Time::Now()), |
| 280 db_handle_(DownloadItem::kUninitializedHandle), | 280 db_handle_(DownloadItem::kUninitializedHandle), |
| 281 delegate_(delegate), | 281 delegate_(delegate), |
| 282 is_paused_(false), | 282 is_paused_(false), |
| 283 open_when_complete_(false), | 283 open_when_complete_(false), |
| 284 file_externally_removed_(false), | 284 file_externally_removed_(false), |
| 285 safety_state_(SAFE), | 285 safety_state_(SAFE), |
| 286 auto_opened_(false), | 286 auto_opened_(false), |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 UpdateProgress(bytes_so_far, hash_state); | 450 UpdateProgress(bytes_so_far, hash_state); |
| 451 UpdateObservers(); | 451 UpdateObservers(); |
| 452 } | 452 } |
| 453 | 453 |
| 454 // Triggered by a user action. | 454 // Triggered by a user action. |
| 455 void DownloadItemImpl::Cancel(bool user_cancel) { | 455 void DownloadItemImpl::Cancel(bool user_cancel) { |
| 456 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 456 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 457 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 457 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 458 | 458 |
| 459 last_reason_ = user_cancel ? | 459 last_reason_ = user_cancel ? |
| 460 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : | 460 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : |
| 461 DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | 461 content::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; |
| 462 | 462 |
| 463 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 463 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 464 if (!IsPartialDownload()) { | 464 if (!IsPartialDownload()) { |
| 465 // Small downloads might be complete before this method has | 465 // Small downloads might be complete before this method has |
| 466 // a chance to run. | 466 // a chance to run. |
| 467 return; | 467 return; |
| 468 } | 468 } |
| 469 | 469 |
| 470 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); | 470 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); |
| 471 | 471 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 void DownloadItemImpl::UpdateTarget() { | 596 void DownloadItemImpl::UpdateTarget() { |
| 597 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 597 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 598 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 598 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 599 | 599 |
| 600 if (state_info_.target_name.value().empty()) | 600 if (state_info_.target_name.value().empty()) |
| 601 state_info_.target_name = full_path_.BaseName(); | 601 state_info_.target_name = full_path_.BaseName(); |
| 602 } | 602 } |
| 603 | 603 |
| 604 void DownloadItemImpl::Interrupted(int64 size, | 604 void DownloadItemImpl::Interrupted(int64 size, |
| 605 const std::string& hash_state, | 605 const std::string& hash_state, |
| 606 InterruptReason reason) { | 606 content::DownloadInterruptReason reason) { |
| 607 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 607 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 608 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 608 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 609 | 609 |
| 610 if (!IsInProgress()) | 610 if (!IsInProgress()) |
| 611 return; | 611 return; |
| 612 | 612 |
| 613 last_reason_ = reason; | 613 last_reason_ = reason; |
| 614 UpdateProgress(size, hash_state); | 614 UpdateProgress(size, hash_state); |
| 615 download_stats::RecordDownloadInterrupted(reason, | 615 download_stats::RecordDownloadInterrupted(reason, |
| 616 received_bytes_, | 616 received_bytes_, |
| (...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1105 const FilePath& DownloadItemImpl::GetSuggestedPath() const { | 1105 const FilePath& DownloadItemImpl::GetSuggestedPath() const { |
| 1106 return state_info_.suggested_path; | 1106 return state_info_.suggested_path; |
| 1107 } | 1107 } |
| 1108 bool DownloadItemImpl::IsTemporary() const { return is_temporary_; } | 1108 bool DownloadItemImpl::IsTemporary() const { return is_temporary_; } |
| 1109 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } | 1109 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } |
| 1110 bool DownloadItemImpl::GetOpened() const { return opened_; } | 1110 bool DownloadItemImpl::GetOpened() const { return opened_; } |
| 1111 const std::string& DownloadItemImpl::GetLastModifiedTime() const { | 1111 const std::string& DownloadItemImpl::GetLastModifiedTime() const { |
| 1112 return last_modified_time_; | 1112 return last_modified_time_; |
| 1113 } | 1113 } |
| 1114 const std::string& DownloadItemImpl::GetETag() const { return etag_; } | 1114 const std::string& DownloadItemImpl::GetETag() const { return etag_; } |
| 1115 InterruptReason DownloadItemImpl::GetLastReason() const { | 1115 content::DownloadInterruptReason DownloadItemImpl::GetLastReason() const { |
| 1116 return last_reason_; | 1116 return last_reason_; |
| 1117 } | 1117 } |
| 1118 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } | 1118 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } |
| 1119 bool DownloadItemImpl::NeedsRename() const { | 1119 bool DownloadItemImpl::NeedsRename() const { |
| 1120 return state_info_.target_name != full_path_.BaseName(); | 1120 return state_info_.target_name != full_path_.BaseName(); |
| 1121 } | 1121 } |
| 1122 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } | 1122 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } |
| 1123 | 1123 |
| 1124 DownloadItem::ExternalData* | 1124 DownloadItem::ExternalData* |
| 1125 DownloadItemImpl::GetExternalData(const void* key) { | 1125 DownloadItemImpl::GetExternalData(const void* key) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1141 std::map<const void*, ExternalData*>::iterator it = | 1141 std::map<const void*, ExternalData*>::iterator it = |
| 1142 external_data_map_.find(key); | 1142 external_data_map_.find(key); |
| 1143 | 1143 |
| 1144 if (it == external_data_map_.end()) { | 1144 if (it == external_data_map_.end()) { |
| 1145 external_data_map_[key] = data; | 1145 external_data_map_[key] = data; |
| 1146 } else if (it->second != data) { | 1146 } else if (it->second != data) { |
| 1147 delete it->second; | 1147 delete it->second; |
| 1148 it->second = data; | 1148 it->second = data; |
| 1149 } | 1149 } |
| 1150 } | 1150 } |
| OLD | NEW |