| 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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 full_path_(info.path), | 168 full_path_(info.path), |
| 169 url_chain_(info.url_chain), | 169 url_chain_(info.url_chain), |
| 170 referrer_url_(info.referrer_url), | 170 referrer_url_(info.referrer_url), |
| 171 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), | 171 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), |
| 172 content_disposition_(info.content_disposition), | 172 content_disposition_(info.content_disposition), |
| 173 mime_type_(info.mime_type), | 173 mime_type_(info.mime_type), |
| 174 original_mime_type_(info.original_mime_type), | 174 original_mime_type_(info.original_mime_type), |
| 175 referrer_charset_(info.referrer_charset), | 175 referrer_charset_(info.referrer_charset), |
| 176 total_bytes_(info.total_bytes), | 176 total_bytes_(info.total_bytes), |
| 177 received_bytes_(0), | 177 received_bytes_(0), |
| 178 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 178 last_modified_time_(info.last_modified), |
| 179 etag_(info.etag), |
| 180 last_reason_(info.server_interrupt_reason), |
| 179 start_tick_(base::TimeTicks::Now()), | 181 start_tick_(base::TimeTicks::Now()), |
| 180 state_(IN_PROGRESS), | 182 state_(IN_PROGRESS), |
| 181 start_time_(info.start_time), | 183 start_time_(info.start_time), |
| 182 db_handle_(DownloadItem::kUninitializedHandle), | 184 db_handle_(DownloadItem::kUninitializedHandle), |
| 183 download_manager_(download_manager), | 185 download_manager_(download_manager), |
| 184 is_paused_(false), | 186 is_paused_(false), |
| 185 open_when_complete_(false), | 187 open_when_complete_(false), |
| 186 file_externally_removed_(false), | 188 file_externally_removed_(false), |
| 187 safety_state_(SAFE), | 189 safety_state_(SAFE), |
| 188 auto_opened_(false), | 190 auto_opened_(false), |
| 189 is_otr_(is_otr), | 191 is_otr_(is_otr), |
| 190 is_temporary_(!info.save_info.file_path.empty()), | 192 is_temporary_(!info.save_info.file_path.empty() && |
| 193 !info.continued_download), |
| 191 all_data_saved_(false), | 194 all_data_saved_(false), |
| 192 opened_(false), | 195 opened_(false), |
| 193 open_enabled_(true), | 196 open_enabled_(true), |
| 194 delegate_delayed_complete_(false) { | 197 delegate_delayed_complete_(false) { |
| 195 Init(true /* actively downloading */); | 198 Init(true /* actively downloading */); |
| 196 } | 199 } |
| 197 | 200 |
| 198 // Constructing for the "Save Page As..." feature: | 201 // Constructing for the "Save Page As..." feature: |
| 199 DownloadItem::DownloadItem(DownloadManager* download_manager, | 202 DownloadItem::DownloadItem(DownloadManager* download_manager, |
| 200 const FilePath& path, | 203 const FilePath& path, |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 328 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 326 | 329 |
| 327 received_bytes_ = bytes_so_far; | 330 received_bytes_ = bytes_so_far; |
| 328 | 331 |
| 329 // If we've received more data than we were expecting (bad server info?), | 332 // If we've received more data than we were expecting (bad server info?), |
| 330 // revert to 'unknown size mode'. | 333 // revert to 'unknown size mode'. |
| 331 if (received_bytes_ > total_bytes_) | 334 if (received_bytes_ > total_bytes_) |
| 332 total_bytes_ = 0; | 335 total_bytes_ = 0; |
| 333 } | 336 } |
| 334 | 337 |
| 338 void DownloadItem::UpdateHash(const std::string& partial_hash) { |
| 339 if (!partial_hash.empty() && !BaseFile::IsEmptySha256Hash(partial_hash)) |
| 340 hash_ = partial_hash; |
| 341 } |
| 342 |
| 335 // Updates from the download thread may have been posted while this download | 343 // Updates from the download thread may have been posted while this download |
| 336 // was being cancelled in the UI thread, so we'll accept them unless we're | 344 // was being cancelled in the UI thread, so we'll accept them unless we're |
| 337 // complete. | 345 // complete. |
| 338 void DownloadItem::Update(int64 bytes_so_far) { | 346 void DownloadItem::Update(int64 bytes_so_far, const std::string& partial_hash) { |
| 339 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 347 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 340 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 348 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 341 | 349 |
| 342 if (!IsInProgress()) { | 350 if (!IsInProgress()) { |
| 343 NOTREACHED(); | 351 NOTREACHED(); |
| 344 return; | 352 return; |
| 345 } | 353 } |
| 346 UpdateSize(bytes_so_far); | 354 UpdateSize(bytes_so_far); |
| 355 UpdateHash(partial_hash); |
| 347 UpdateObservers(); | 356 UpdateObservers(); |
| 348 } | 357 } |
| 349 | 358 |
| 350 // Triggered by a user action. | 359 // Triggered by a user action. |
| 351 void DownloadItem::Cancel(bool user_cancel) { | 360 void DownloadItem::Cancel(bool user_cancel) { |
| 352 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 361 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 353 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 362 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 354 | 363 |
| 355 last_reason_ = user_cancel ? | 364 last_reason_ = user_cancel ? |
| 356 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : | 365 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : |
| (...skipping 27 matching lines...) Expand all Loading... |
| 384 Completed(); | 393 Completed(); |
| 385 } | 394 } |
| 386 | 395 |
| 387 void DownloadItem::OnAllDataSaved(int64 size, const std::string& final_hash) { | 396 void DownloadItem::OnAllDataSaved(int64 size, const std::string& final_hash) { |
| 388 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 397 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 389 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 398 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 390 | 399 |
| 391 DCHECK(!all_data_saved_); | 400 DCHECK(!all_data_saved_); |
| 392 all_data_saved_ = true; | 401 all_data_saved_ = true; |
| 393 UpdateSize(size); | 402 UpdateSize(size); |
| 394 hash_ = final_hash; | 403 UpdateHash(final_hash); |
| 395 } | 404 } |
| 396 | 405 |
| 397 void DownloadItem::OnDownloadedFileRemoved() { | 406 void DownloadItem::OnDownloadedFileRemoved() { |
| 398 file_externally_removed_ = true; | 407 file_externally_removed_ = true; |
| 399 UpdateObservers(); | 408 UpdateObservers(); |
| 400 } | 409 } |
| 401 | 410 |
| 402 void DownloadItem::Completed() { | 411 void DownloadItem::Completed() { |
| 403 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 412 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 404 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 413 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 } | 455 } |
| 447 | 456 |
| 448 void DownloadItem::UpdateTarget() { | 457 void DownloadItem::UpdateTarget() { |
| 449 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 458 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 450 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 459 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 451 | 460 |
| 452 if (state_info_.target_name.value().empty()) | 461 if (state_info_.target_name.value().empty()) |
| 453 state_info_.target_name = full_path_.BaseName(); | 462 state_info_.target_name = full_path_.BaseName(); |
| 454 } | 463 } |
| 455 | 464 |
| 456 void DownloadItem::Interrupted(int64 size, InterruptReason reason) { | 465 void DownloadItem::Interrupted(int64 size, |
| 466 const std::string partial_hash, |
| 467 InterruptReason reason) { |
| 457 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 468 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 458 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 469 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 459 | 470 |
| 460 if (!IsInProgress()) | 471 if (!IsInProgress()) |
| 461 return; | 472 return; |
| 462 | 473 |
| 463 last_reason_ = reason; | 474 last_reason_ = reason; |
| 464 UpdateSize(size); | 475 UpdateSize(size); |
| 476 UpdateHash(partial_hash); |
| 465 download_stats::RecordDownloadInterrupted(reason, | 477 download_stats::RecordDownloadInterrupted(reason, |
| 466 received_bytes_, | 478 received_bytes_, |
| 467 total_bytes_); | 479 total_bytes_); |
| 468 TransitionTo(INTERRUPTED); | 480 TransitionTo(INTERRUPTED); |
| 469 } | 481 } |
| 470 | 482 |
| 471 void DownloadItem::Delete(DeleteReason reason) { | 483 void DownloadItem::Delete(DeleteReason reason) { |
| 472 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 484 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 473 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 485 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 474 | 486 |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 726 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 715 base::Bind(&DownloadFileManager::CancelDownload, | 727 base::Bind(&DownloadFileManager::CancelDownload, |
| 716 file_manager, global_id())); | 728 file_manager, global_id())); |
| 717 } | 729 } |
| 718 | 730 |
| 719 void DownloadItem::Init(bool active) { | 731 void DownloadItem::Init(bool active) { |
| 720 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 732 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 721 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 733 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 722 | 734 |
| 723 UpdateTarget(); | 735 UpdateTarget(); |
| 724 if (active) { | 736 if (active) |
| 725 download_stats::RecordDownloadCount(download_stats::START_COUNT); | 737 download_stats::RecordDownloadCount(download_stats::START_COUNT); |
| 726 } | |
| 727 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); | 738 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); |
| 728 } | 739 } |
| 729 | 740 |
| 730 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to | 741 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to |
| 731 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. | 742 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. |
| 732 bool DownloadItem::IsPartialDownload() const { | 743 bool DownloadItem::IsPartialDownload() const { |
| 733 return (state_ == IN_PROGRESS); | 744 return (state_ == IN_PROGRESS); |
| 734 } | 745 } |
| 735 | 746 |
| 736 bool DownloadItem::IsInProgress() const { | 747 bool DownloadItem::IsInProgress() const { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 } | 788 } |
| 778 | 789 |
| 779 if (verbose) { | 790 if (verbose) { |
| 780 description += base::StringPrintf( | 791 description += base::StringPrintf( |
| 781 " db_handle = %" PRId64 | 792 " db_handle = %" PRId64 |
| 782 " total_bytes = %" PRId64 | 793 " total_bytes = %" PRId64 |
| 783 " received_bytes = %" PRId64 | 794 " received_bytes = %" PRId64 |
| 784 " is_paused = %c" | 795 " is_paused = %c" |
| 785 " is_otr = %c" | 796 " is_otr = %c" |
| 786 " safety_state = %s" | 797 " safety_state = %s" |
| 798 " last_modified = '%s'" |
| 799 " etag = '%s'" |
| 787 " url_chain = \n\t\"%s\"\n\t" | 800 " url_chain = \n\t\"%s\"\n\t" |
| 788 " target_name = \"%" PRFilePath "\"" | 801 " target_name = \"%" PRFilePath "\"" |
| 789 " full_path = \"%" PRFilePath "\"", | 802 " full_path = \"%" PRFilePath "\"", |
| 790 db_handle(), | 803 db_handle(), |
| 791 total_bytes(), | 804 total_bytes(), |
| 792 received_bytes(), | 805 received_bytes(), |
| 793 is_paused() ? 'T' : 'F', | 806 is_paused() ? 'T' : 'F', |
| 794 is_otr() ? 'T' : 'F', | 807 is_otr() ? 'T' : 'F', |
| 795 DebugSafetyStateString(safety_state()), | 808 DebugSafetyStateString(safety_state()), |
| 809 last_modified_time_.c_str(), |
| 810 etag_.c_str(), |
| 796 url_list.c_str(), | 811 url_list.c_str(), |
| 797 state_info_.target_name.value().c_str(), | 812 state_info_.target_name.value().c_str(), |
| 798 full_path().value().c_str()); | 813 full_path().value().c_str()); |
| 799 } else { | 814 } else { |
| 800 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 815 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
| 801 } | 816 } |
| 802 | 817 |
| 803 description += " }"; | 818 description += " }"; |
| 804 | 819 |
| 805 return description; | 820 return description; |
| 806 } | 821 } |
| OLD | NEW |