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