| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "content/browser/download/download_item.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/timer.h" | 14 #include "base/timer.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
| 17 #include "content/browser/browser_thread.h" | 17 #include "content/browser/browser_thread.h" |
| 18 #include "content/browser/content_browser_client.h" | 18 #include "content/browser/content_browser_client.h" |
| 19 #include "content/browser/download/download_file.h" | 19 #include "content/browser/download/download_file.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_manager.h" | 21 #include "content/browser/download/download_file_manager.h" |
| 22 #include "content/browser/download/download_id.h" |
| 22 #include "content/browser/download/download_manager.h" | 23 #include "content/browser/download/download_manager.h" |
| 23 #include "content/browser/download/download_manager_delegate.h" | 24 #include "content/browser/download/download_manager_delegate.h" |
| 24 #include "content/browser/download/download_persistent_store_info.h" | 25 #include "content/browser/download/download_persistent_store_info.h" |
| 25 #include "content/browser/download/download_request_handle.h" | 26 #include "content/browser/download/download_request_handle.h" |
| 26 #include "content/browser/download/download_stats.h" | 27 #include "content/browser/download/download_stats.h" |
| 27 | 28 |
| 28 // A DownloadItem normally goes through the following states: | 29 // A DownloadItem normally goes through the following states: |
| 29 // * Created (when download starts) | 30 // * Created (when download starts) |
| 30 // * Made visible to consumers (e.g. Javascript) after the | 31 // * Made visible to consumers (e.g. Javascript) after the |
| 31 // destination file has been determined. | 32 // destination file has been determined. |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 opened_(false), | 185 opened_(false), |
| 185 open_enabled_(true) { | 186 open_enabled_(true) { |
| 186 Init(true /* actively downloading */); | 187 Init(true /* actively downloading */); |
| 187 } | 188 } |
| 188 | 189 |
| 189 // Constructing for the "Save Page As..." feature: | 190 // Constructing for the "Save Page As..." feature: |
| 190 DownloadItem::DownloadItem(DownloadManager* download_manager, | 191 DownloadItem::DownloadItem(DownloadManager* download_manager, |
| 191 const FilePath& path, | 192 const FilePath& path, |
| 192 const GURL& url, | 193 const GURL& url, |
| 193 bool is_otr, | 194 bool is_otr, |
| 194 int download_id) | 195 DownloadId download_id) |
| 195 : download_id_(download_id), | 196 : download_id_(download_id.local()), |
| 196 full_path_(path), | 197 full_path_(path), |
| 197 url_chain_(1, url), | 198 url_chain_(1, url), |
| 198 referrer_url_(GURL()), | 199 referrer_url_(GURL()), |
| 199 total_bytes_(0), | 200 total_bytes_(0), |
| 200 received_bytes_(0), | 201 received_bytes_(0), |
| 201 last_error_(net::OK), | 202 last_error_(net::OK), |
| 202 start_tick_(base::TimeTicks::Now()), | 203 start_tick_(base::TimeTicks::Now()), |
| 203 state_(IN_PROGRESS), | 204 state_(IN_PROGRESS), |
| 204 start_time_(base::Time::Now()), | 205 start_time_(base::Time::Now()), |
| 205 db_handle_(DownloadItem::kUninitializedHandle), | 206 db_handle_(DownloadItem::kUninitializedHandle), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 218 } | 219 } |
| 219 | 220 |
| 220 DownloadItem::~DownloadItem() { | 221 DownloadItem::~DownloadItem() { |
| 221 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 222 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 222 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 223 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 223 | 224 |
| 224 TransitionTo(REMOVING); | 225 TransitionTo(REMOVING); |
| 225 download_manager_->AssertQueueStateConsistent(this); | 226 download_manager_->AssertQueueStateConsistent(this); |
| 226 } | 227 } |
| 227 | 228 |
| 229 DownloadId DownloadItem::global_id() const { |
| 230 return DownloadId(download_manager_, id()); |
| 231 } |
| 232 |
| 228 void DownloadItem::AddObserver(Observer* observer) { | 233 void DownloadItem::AddObserver(Observer* observer) { |
| 229 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 234 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 230 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 235 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 231 | 236 |
| 232 observers_.AddObserver(observer); | 237 observers_.AddObserver(observer); |
| 233 } | 238 } |
| 234 | 239 |
| 235 void DownloadItem::RemoveObserver(Observer* observer) { | 240 void DownloadItem::RemoveObserver(Observer* observer) { |
| 236 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 241 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 237 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 242 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 | 575 |
| 571 VLOG(20) << __FUNCTION__ << "()" | 576 VLOG(20) << __FUNCTION__ << "()" |
| 572 << " needs rename = " << NeedsRename() | 577 << " needs rename = " << NeedsRename() |
| 573 << " " << DebugString(true); | 578 << " " << DebugString(true); |
| 574 DCHECK_NE(DANGEROUS, safety_state()); | 579 DCHECK_NE(DANGEROUS, safety_state()); |
| 575 DCHECK(file_manager); | 580 DCHECK(file_manager); |
| 576 | 581 |
| 577 if (NeedsRename()) { | 582 if (NeedsRename()) { |
| 578 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 583 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 579 NewRunnableMethod(file_manager, | 584 NewRunnableMethod(file_manager, |
| 580 &DownloadFileManager::RenameCompletingDownloadFile, id(), | 585 &DownloadFileManager::RenameCompletingDownloadFile, global_id(), |
| 581 GetTargetFilePath(), safety_state() == SAFE)); | 586 GetTargetFilePath(), safety_state() == SAFE)); |
| 582 return; | 587 return; |
| 583 } | 588 } |
| 584 | 589 |
| 585 Completed(); | 590 Completed(); |
| 586 | 591 |
| 587 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 592 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
| 588 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, | 593 file_manager, &DownloadFileManager::CompleteDownload, global_id())); |
| 589 id())); | |
| 590 } | 594 } |
| 591 | 595 |
| 592 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 596 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
| 593 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 597 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 594 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 598 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 595 | 599 |
| 596 VLOG(20) << __FUNCTION__ << "()" | 600 VLOG(20) << __FUNCTION__ << "()" |
| 597 << " full_path = \"" << full_path.value() << "\"" | 601 << " full_path = \"" << full_path.value() << "\"" |
| 598 << " needed rename = " << NeedsRename() | 602 << " needed rename = " << NeedsRename() |
| 599 << " " << DebugString(false); | 603 << " " << DebugString(false); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 705 GetTargetFilePath() : full_path_; | 709 GetTargetFilePath() : full_path_; |
| 706 } | 710 } |
| 707 | 711 |
| 708 void DownloadItem::OffThreadCancel(DownloadFileManager* file_manager) { | 712 void DownloadItem::OffThreadCancel(DownloadFileManager* file_manager) { |
| 709 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 713 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 710 request_handle_.CancelRequest(); | 714 request_handle_.CancelRequest(); |
| 711 | 715 |
| 712 BrowserThread::PostTask( | 716 BrowserThread::PostTask( |
| 713 BrowserThread::FILE, FROM_HERE, | 717 BrowserThread::FILE, FROM_HERE, |
| 714 NewRunnableMethod( | 718 NewRunnableMethod( |
| 715 file_manager, &DownloadFileManager::CancelDownload, download_id_)); | 719 file_manager, &DownloadFileManager::CancelDownload, global_id())); |
| 716 } | 720 } |
| 717 | 721 |
| 718 void DownloadItem::Init(bool active) { | 722 void DownloadItem::Init(bool active) { |
| 719 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 723 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 720 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 724 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 721 | 725 |
| 722 UpdateTarget(); | 726 UpdateTarget(); |
| 723 if (active) { | 727 if (active) { |
| 724 StartProgressTimer(); | 728 StartProgressTimer(); |
| 725 download_stats::RecordDownloadCount(download_stats::START_COUNT); | 729 download_stats::RecordDownloadCount(download_stats::START_COUNT); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 state_info_.target_name.value().c_str(), | 801 state_info_.target_name.value().c_str(), |
| 798 full_path().value().c_str()); | 802 full_path().value().c_str()); |
| 799 } else { | 803 } else { |
| 800 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 804 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
| 801 } | 805 } |
| 802 | 806 |
| 803 description += " }"; | 807 description += " }"; |
| 804 | 808 |
| 805 return description; | 809 return description; |
| 806 } | 810 } |
| OLD | NEW |