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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 case DownloadItem::REMOVING: | 84 case DownloadItem::REMOVING: |
85 return "REMOVING"; | 85 return "REMOVING"; |
86 case DownloadItem::INTERRUPTED: | 86 case DownloadItem::INTERRUPTED: |
87 return "INTERRUPTED"; | 87 return "INTERRUPTED"; |
88 default: | 88 default: |
89 NOTREACHED() << "Unknown download state " << state; | 89 NOTREACHED() << "Unknown download state " << state; |
90 return "unknown"; | 90 return "unknown"; |
91 }; | 91 }; |
92 } | 92 } |
93 | 93 |
94 DownloadItem::SafetyState GetSafetyState(bool dangerous_file, | |
95 bool dangerous_url) { | |
96 return (dangerous_url || dangerous_file) ? | |
97 DownloadItem::DANGEROUS : DownloadItem::SAFE; | |
98 } | |
99 | |
100 // Note: When a download has both |dangerous_file| and |dangerous_url| set, | |
101 // danger type is set to DANGEROUS_URL since the risk of dangerous URL | |
102 // overweights that of dangerous file type. | |
103 DownloadItem::DangerType GetDangerType(bool dangerous_file, | |
104 bool dangerous_url) { | |
105 if (dangerous_url) { | |
106 // dangerous URL overweights dangerous file. We check dangerous URL first. | |
107 return DownloadItem::DANGEROUS_URL; | |
108 } | |
109 return dangerous_file ? | |
110 DownloadItem::DANGEROUS_FILE : DownloadItem::NOT_DANGEROUS; | |
111 } | |
112 | |
113 // Classes to null out request handle calls (for SavePage DownloadItems, which | 94 // Classes to null out request handle calls (for SavePage DownloadItems, which |
114 // may have, e.g., Cancel() called on them without it doing anything) | 95 // may have, e.g., Cancel() called on them without it doing anything) |
115 // and to DCHECK on them (for history DownloadItems, which should never have | 96 // and to DCHECK on them (for history DownloadItems, which should never have |
116 // any operation that implies an off-thread component, since they don't | 97 // any operation that implies an off-thread component, since they don't |
117 // have any). | 98 // have any). |
118 class NullDownloadRequestHandle : public DownloadRequestHandleInterface { | 99 class NullDownloadRequestHandle : public DownloadRequestHandleInterface { |
119 public: | 100 public: |
120 NullDownloadRequestHandle() {} | 101 NullDownloadRequestHandle() {} |
121 | 102 |
122 // DownloadRequestHandleInterface calls | 103 // DownloadRequestHandleInterface calls |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 161 } |
181 | 162 |
182 // Constructing for a regular download: | 163 // Constructing for a regular download: |
183 DownloadItem::DownloadItem(DownloadManager* download_manager, | 164 DownloadItem::DownloadItem(DownloadManager* download_manager, |
184 const DownloadCreateInfo& info, | 165 const DownloadCreateInfo& info, |
185 DownloadRequestHandleInterface* request_handle, | 166 DownloadRequestHandleInterface* request_handle, |
186 bool is_otr) | 167 bool is_otr) |
187 : state_info_(info.original_name, info.save_info.file_path, | 168 : state_info_(info.original_name, info.save_info.file_path, |
188 info.has_user_gesture, info.transition_type, | 169 info.has_user_gesture, info.transition_type, |
189 info.prompt_user_for_save_location, info.path_uniquifier, | 170 info.prompt_user_for_save_location, info.path_uniquifier, |
190 false, false), | 171 DownloadStateInfo::NOT_DANGEROUS), |
191 request_handle_(request_handle), | 172 request_handle_(request_handle), |
192 download_id_(info.download_id), | 173 download_id_(info.download_id), |
193 full_path_(info.path), | 174 full_path_(info.path), |
194 url_chain_(info.url_chain), | 175 url_chain_(info.url_chain), |
195 referrer_url_(info.referrer_url), | 176 referrer_url_(info.referrer_url), |
196 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), | 177 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), |
197 content_disposition_(info.content_disposition), | 178 content_disposition_(info.content_disposition), |
198 mime_type_(info.mime_type), | 179 mime_type_(info.mime_type), |
199 original_mime_type_(info.original_mime_type), | 180 original_mime_type_(info.original_mime_type), |
200 referrer_charset_(info.referrer_charset), | 181 referrer_charset_(info.referrer_charset), |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 | 312 |
332 content::GetContentClient()->browser()->ShowItemInFolder(full_path()); | 313 content::GetContentClient()->browser()->ShowItemInFolder(full_path()); |
333 } | 314 } |
334 | 315 |
335 void DownloadItem::DangerousDownloadValidated() { | 316 void DownloadItem::DangerousDownloadValidated() { |
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
337 DCHECK_EQ(DANGEROUS, safety_state()); | 318 DCHECK_EQ(DANGEROUS, safety_state()); |
338 | 319 |
339 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 320 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", |
340 GetDangerType(), | 321 GetDangerType(), |
341 DANGEROUS_TYPE_MAX); | 322 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
342 | 323 |
343 safety_state_ = DANGEROUS_BUT_VALIDATED; | 324 safety_state_ = DANGEROUS_BUT_VALIDATED; |
344 UpdateObservers(); | 325 UpdateObservers(); |
345 | 326 |
346 download_manager_->MaybeCompleteDownload(this); | 327 download_manager_->MaybeCompleteDownload(this); |
347 } | 328 } |
348 | 329 |
349 void DownloadItem::UpdateSize(int64 bytes_so_far) { | 330 void DownloadItem::UpdateSize(int64 bytes_so_far) { |
350 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 331 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
351 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 332 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 | 436 |
456 void DownloadItem::TransitionTo(DownloadState new_state) { | 437 void DownloadItem::TransitionTo(DownloadState new_state) { |
457 if (state_ == new_state) | 438 if (state_ == new_state) |
458 return; | 439 return; |
459 | 440 |
460 state_ = new_state; | 441 state_ = new_state; |
461 UpdateObservers(); | 442 UpdateObservers(); |
462 } | 443 } |
463 | 444 |
464 void DownloadItem::UpdateSafetyState() { | 445 void DownloadItem::UpdateSafetyState() { |
465 SafetyState updated_value( | 446 SafetyState updated_value = state_info_.IsDangerous() ? |
466 GetSafetyState(state_info_.is_dangerous_file, | 447 DownloadItem::DANGEROUS : DownloadItem::SAFE; |
467 state_info_.is_dangerous_url)); | |
468 if (updated_value != safety_state_) { | 448 if (updated_value != safety_state_) { |
469 safety_state_ = updated_value; | 449 safety_state_ = updated_value; |
470 UpdateObservers(); | 450 UpdateObservers(); |
471 } | 451 } |
472 } | 452 } |
473 | 453 |
474 void DownloadItem::UpdateTarget() { | 454 void DownloadItem::UpdateTarget() { |
475 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 455 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
476 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 456 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
477 | 457 |
(...skipping 16 matching lines...) Expand all Loading... |
494 TransitionTo(INTERRUPTED); | 474 TransitionTo(INTERRUPTED); |
495 } | 475 } |
496 | 476 |
497 void DownloadItem::Delete(DeleteReason reason) { | 477 void DownloadItem::Delete(DeleteReason reason) { |
498 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 478 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
499 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 479 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
500 | 480 |
501 switch (reason) { | 481 switch (reason) { |
502 case DELETE_DUE_TO_USER_DISCARD: | 482 case DELETE_DUE_TO_USER_DISCARD: |
503 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), | 483 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), |
504 DANGEROUS_TYPE_MAX); | 484 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
505 break; | 485 break; |
506 case DELETE_DUE_TO_BROWSER_SHUTDOWN: | 486 case DELETE_DUE_TO_BROWSER_SHUTDOWN: |
507 UMA_HISTOGRAM_ENUMERATION("Download.Discard", GetDangerType(), | 487 UMA_HISTOGRAM_ENUMERATION("Download.Discard", GetDangerType(), |
508 DANGEROUS_TYPE_MAX); | 488 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
509 break; | 489 break; |
510 default: | 490 default: |
511 NOTREACHED(); | 491 NOTREACHED(); |
512 } | 492 } |
513 | 493 |
514 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 494 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
515 base::Bind(&DeleteDownloadedFile, full_path_)); | 495 base::Bind(&DeleteDownloadedFile, full_path_)); |
516 Remove(); | 496 Remove(); |
517 // We have now been deleted. | 497 // We have now been deleted. |
518 } | 498 } |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 644 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
665 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 645 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
666 | 646 |
667 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); | 647 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
668 state_info_ = state; | 648 state_info_ = state; |
669 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); | 649 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
670 | 650 |
671 UpdateSafetyState(); | 651 UpdateSafetyState(); |
672 } | 652 } |
673 | 653 |
674 DownloadItem::DangerType DownloadItem::GetDangerType() const { | 654 DownloadStateInfo::DangerType DownloadItem::GetDangerType() const { |
675 return ::GetDangerType(state_info_.is_dangerous_file, | 655 return state_info_.danger; |
676 state_info_.is_dangerous_url); | |
677 } | 656 } |
678 | 657 |
679 bool DownloadItem::IsDangerous() const { | 658 bool DownloadItem::IsDangerous() const { |
680 return GetDangerType() != DownloadItem::NOT_DANGEROUS; | 659 return state_info_.IsDangerous(); |
681 } | 660 } |
682 | 661 |
683 void DownloadItem::MarkFileDangerous() { | 662 void DownloadItem::MarkFileDangerous() { |
684 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 663 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
685 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 664 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
686 | 665 state_info_.danger = DownloadStateInfo::DANGEROUS_FILE; |
687 state_info_.is_dangerous_file = true; | |
688 UpdateSafetyState(); | 666 UpdateSafetyState(); |
689 } | 667 } |
690 | 668 |
691 void DownloadItem::MarkUrlDangerous() { | 669 void DownloadItem::MarkUrlDangerous() { |
692 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 670 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
693 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 671 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
694 | 672 state_info_.danger = DownloadStateInfo::DANGEROUS_URL; |
695 state_info_.is_dangerous_url = true; | |
696 UpdateSafetyState(); | 673 UpdateSafetyState(); |
697 } | 674 } |
698 | 675 |
| 676 void DownloadItem::MarkContentDangerous() { |
| 677 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 678 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 679 state_info_.danger = DownloadStateInfo::DANGEROUS_CONTENT; |
| 680 UpdateSafetyState(); |
| 681 } |
| 682 |
699 DownloadPersistentStoreInfo DownloadItem::GetPersistentStoreInfo() const { | 683 DownloadPersistentStoreInfo DownloadItem::GetPersistentStoreInfo() const { |
700 return DownloadPersistentStoreInfo(full_path(), | 684 return DownloadPersistentStoreInfo(full_path(), |
701 GetURL(), | 685 GetURL(), |
702 referrer_url(), | 686 referrer_url(), |
703 start_time(), | 687 start_time(), |
704 end_time(), | 688 end_time(), |
705 received_bytes(), | 689 received_bytes(), |
706 total_bytes(), | 690 total_bytes(), |
707 state(), | 691 state(), |
708 db_handle(), | 692 db_handle(), |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 state_info_.target_name.value().c_str(), | 811 state_info_.target_name.value().c_str(), |
828 full_path().value().c_str()); | 812 full_path().value().c_str()); |
829 } else { | 813 } else { |
830 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 814 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
831 } | 815 } |
832 | 816 |
833 description += " }"; | 817 description += " }"; |
834 | 818 |
835 return description; | 819 return description; |
836 } | 820 } |
OLD | NEW |