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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 opened_(false), | 184 opened_(false), |
184 open_enabled_(true) { | 185 open_enabled_(true) { |
185 Init(true /* actively downloading */); | 186 Init(true /* actively downloading */); |
186 } | 187 } |
187 | 188 |
188 // Constructing for the "Save Page As..." feature: | 189 // Constructing for the "Save Page As..." feature: |
189 DownloadItem::DownloadItem(DownloadManager* download_manager, | 190 DownloadItem::DownloadItem(DownloadManager* download_manager, |
190 const FilePath& path, | 191 const FilePath& path, |
191 const GURL& url, | 192 const GURL& url, |
192 bool is_otr, | 193 bool is_otr, |
193 int download_id) | 194 DownloadId download_id) |
194 : download_id_(download_id), | 195 : download_id_(download_id.local()), |
195 full_path_(path), | 196 full_path_(path), |
196 url_chain_(1, url), | 197 url_chain_(1, url), |
197 referrer_url_(GURL()), | 198 referrer_url_(GURL()), |
198 total_bytes_(0), | 199 total_bytes_(0), |
199 received_bytes_(0), | 200 received_bytes_(0), |
200 last_os_error_(0), | 201 last_os_error_(0), |
201 start_tick_(base::TimeTicks::Now()), | 202 start_tick_(base::TimeTicks::Now()), |
202 state_(IN_PROGRESS), | 203 state_(IN_PROGRESS), |
203 start_time_(base::Time::Now()), | 204 start_time_(base::Time::Now()), |
204 db_handle_(DownloadItem::kUninitializedHandle), | 205 db_handle_(DownloadItem::kUninitializedHandle), |
(...skipping 12 matching lines...) Expand all Loading... |
217 } | 218 } |
218 | 219 |
219 DownloadItem::~DownloadItem() { | 220 DownloadItem::~DownloadItem() { |
220 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 221 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
221 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 222 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
222 | 223 |
223 TransitionTo(REMOVING); | 224 TransitionTo(REMOVING); |
224 download_manager_->AssertQueueStateConsistent(this); | 225 download_manager_->AssertQueueStateConsistent(this); |
225 } | 226 } |
226 | 227 |
| 228 DownloadId DownloadItem::global_id() const { |
| 229 return DownloadId(download_manager_, id()); |
| 230 } |
| 231 |
227 void DownloadItem::AddObserver(Observer* observer) { | 232 void DownloadItem::AddObserver(Observer* observer) { |
228 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 233 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
229 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 234 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
230 | 235 |
231 observers_.AddObserver(observer); | 236 observers_.AddObserver(observer); |
232 } | 237 } |
233 | 238 |
234 void DownloadItem::RemoveObserver(Observer* observer) { | 239 void DownloadItem::RemoveObserver(Observer* observer) { |
235 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 240 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
236 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 241 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 | 573 |
569 VLOG(20) << __FUNCTION__ << "()" | 574 VLOG(20) << __FUNCTION__ << "()" |
570 << " needs rename = " << NeedsRename() | 575 << " needs rename = " << NeedsRename() |
571 << " " << DebugString(true); | 576 << " " << DebugString(true); |
572 DCHECK_NE(DANGEROUS, safety_state()); | 577 DCHECK_NE(DANGEROUS, safety_state()); |
573 DCHECK(file_manager); | 578 DCHECK(file_manager); |
574 | 579 |
575 if (NeedsRename()) { | 580 if (NeedsRename()) { |
576 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 581 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
577 NewRunnableMethod(file_manager, | 582 NewRunnableMethod(file_manager, |
578 &DownloadFileManager::RenameCompletingDownloadFile, id(), | 583 &DownloadFileManager::RenameCompletingDownloadFile, global_id(), |
579 GetTargetFilePath(), safety_state() == SAFE)); | 584 GetTargetFilePath(), safety_state() == SAFE)); |
580 return; | 585 return; |
581 } | 586 } |
582 | 587 |
583 Completed(); | 588 Completed(); |
584 | 589 |
585 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 590 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
586 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, | 591 file_manager, &DownloadFileManager::CompleteDownload, global_id())); |
587 id())); | |
588 } | 592 } |
589 | 593 |
590 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 594 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
591 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 595 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
592 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 596 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
593 | 597 |
594 VLOG(20) << __FUNCTION__ << "()" | 598 VLOG(20) << __FUNCTION__ << "()" |
595 << " full_path = \"" << full_path.value() << "\"" | 599 << " full_path = \"" << full_path.value() << "\"" |
596 << " needed rename = " << NeedsRename() | 600 << " needed rename = " << NeedsRename() |
597 << " " << DebugString(false); | 601 << " " << DebugString(false); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
703 GetTargetFilePath() : full_path_; | 707 GetTargetFilePath() : full_path_; |
704 } | 708 } |
705 | 709 |
706 void DownloadItem::OffThreadCancel(DownloadFileManager* file_manager) { | 710 void DownloadItem::OffThreadCancel(DownloadFileManager* file_manager) { |
707 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 711 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
708 request_handle_.CancelRequest(); | 712 request_handle_.CancelRequest(); |
709 | 713 |
710 BrowserThread::PostTask( | 714 BrowserThread::PostTask( |
711 BrowserThread::FILE, FROM_HERE, | 715 BrowserThread::FILE, FROM_HERE, |
712 NewRunnableMethod( | 716 NewRunnableMethod( |
713 file_manager, &DownloadFileManager::CancelDownload, download_id_)); | 717 file_manager, &DownloadFileManager::CancelDownload, global_id())); |
714 } | 718 } |
715 | 719 |
716 void DownloadItem::Init(bool active) { | 720 void DownloadItem::Init(bool active) { |
717 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 721 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
718 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 722 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
719 | 723 |
720 UpdateTarget(); | 724 UpdateTarget(); |
721 if (active) { | 725 if (active) { |
722 StartProgressTimer(); | 726 StartProgressTimer(); |
723 download_stats::RecordDownloadCount(download_stats::START_COUNT); | 727 download_stats::RecordDownloadCount(download_stats::START_COUNT); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 state_info_.target_name.value().c_str(), | 799 state_info_.target_name.value().c_str(), |
796 full_path().value().c_str()); | 800 full_path().value().c_str()); |
797 } else { | 801 } else { |
798 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 802 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
799 } | 803 } |
800 | 804 |
801 description += " }"; | 805 description += " }"; |
802 | 806 |
803 return description; | 807 return description; |
804 } | 808 } |
OLD | NEW |