Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(595)

Side by Side Diff: content/browser/download/download_item.cc

Issue 8404049: Added member data to classes to support download resumption. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rearranged structures for greater consistency. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 // database handles in incognito mode starting at -1 and progressively getting 124 // database handles in incognito mode starting at -1 and progressively getting
125 // more negative. 125 // more negative.
126 // static 126 // static
127 const int DownloadItem::kUninitializedHandle = 0; 127 const int DownloadItem::kUninitializedHandle = 0;
128 128
129 const char DownloadItem::kEmptyFileHash[] = ""; 129 const char DownloadItem::kEmptyFileHash[] = "";
130 130
131 // Constructor for reading from the history service. 131 // Constructor for reading from the history service.
132 DownloadItem::DownloadItem(DownloadManager* download_manager, 132 DownloadItem::DownloadItem(DownloadManager* download_manager,
133 const DownloadPersistentStoreInfo& info) 133 const DownloadPersistentStoreInfo& info)
134 : download_id_(download_manager->GetNextId()), 134 : state_info_(info.target_name, FilePath(), false,
135 content::PAGE_TRANSITION_LINK, false, 0,
136 DownloadStateInfo::NOT_DANGEROUS),
137 download_id_(download_manager->GetNextId()),
135 full_path_(info.path), 138 full_path_(info.path),
136 url_chain_(1, info.url), 139 url_chain_(1, info.url),
137 referrer_url_(info.referrer_url), 140 referrer_url_(info.referrer_url),
138 total_bytes_(info.total_bytes), 141 total_bytes_(info.total_bytes),
139 received_bytes_(info.received_bytes), 142 received_bytes_(info.received_bytes),
143 hash_state_(info.hash_state),
144 last_modified_time_(info.last_modified_time),
145 etag_(info.etag),
146 last_reason_(info.last_reason),
140 start_tick_(base::TimeTicks()), 147 start_tick_(base::TimeTicks()),
141 state_(static_cast<DownloadState>(info.state)), 148 state_(static_cast<DownloadState>(info.state)),
142 start_time_(info.start_time), 149 start_time_(info.start_time),
143 end_time_(info.end_time), 150 end_time_(info.end_time),
144 db_handle_(info.db_handle), 151 db_handle_(info.db_handle),
145 download_manager_(download_manager), 152 download_manager_(download_manager),
146 is_paused_(false), 153 is_paused_(false),
147 open_when_complete_(false), 154 open_when_complete_(false),
148 file_externally_removed_(false), 155 file_externally_removed_(false),
149 safety_state_(SAFE), 156 safety_state_(SAFE),
(...skipping 25 matching lines...) Expand all
175 full_path_(info.path), 182 full_path_(info.path),
176 url_chain_(info.url_chain), 183 url_chain_(info.url_chain),
177 referrer_url_(info.referrer_url), 184 referrer_url_(info.referrer_url),
178 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), 185 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)),
179 content_disposition_(info.content_disposition), 186 content_disposition_(info.content_disposition),
180 mime_type_(info.mime_type), 187 mime_type_(info.mime_type),
181 original_mime_type_(info.original_mime_type), 188 original_mime_type_(info.original_mime_type),
182 referrer_charset_(info.referrer_charset), 189 referrer_charset_(info.referrer_charset),
183 total_bytes_(info.total_bytes), 190 total_bytes_(info.total_bytes),
184 received_bytes_(0), 191 received_bytes_(0),
192 hash_(DownloadItem::kEmptyFileHash),
193 last_modified_time_(info.last_modified),
194 etag_(info.etag),
185 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), 195 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE),
186 start_tick_(base::TimeTicks::Now()), 196 start_tick_(base::TimeTicks::Now()),
187 state_(IN_PROGRESS), 197 state_(IN_PROGRESS),
188 start_time_(info.start_time), 198 start_time_(info.start_time),
189 db_handle_(DownloadItem::kUninitializedHandle), 199 db_handle_(DownloadItem::kUninitializedHandle),
190 download_manager_(download_manager), 200 download_manager_(download_manager),
191 is_paused_(false), 201 is_paused_(false),
192 open_when_complete_(false), 202 open_when_complete_(false),
193 file_externally_removed_(false), 203 file_externally_removed_(false),
194 safety_state_(SAFE), 204 safety_state_(SAFE),
(...skipping 13 matching lines...) Expand all
208 const GURL& url, 218 const GURL& url,
209 bool is_otr, 219 bool is_otr,
210 DownloadId download_id) 220 DownloadId download_id)
211 : request_handle_(new NullDownloadRequestHandle()), 221 : request_handle_(new NullDownloadRequestHandle()),
212 download_id_(download_id), 222 download_id_(download_id),
213 full_path_(path), 223 full_path_(path),
214 url_chain_(1, url), 224 url_chain_(1, url),
215 referrer_url_(GURL()), 225 referrer_url_(GURL()),
216 total_bytes_(0), 226 total_bytes_(0),
217 received_bytes_(0), 227 received_bytes_(0),
228 hash_(DownloadItem::kEmptyFileHash),
218 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), 229 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE),
219 start_tick_(base::TimeTicks::Now()), 230 start_tick_(base::TimeTicks::Now()),
220 state_(IN_PROGRESS), 231 state_(IN_PROGRESS),
221 start_time_(base::Time::Now()), 232 start_time_(base::Time::Now()),
222 db_handle_(DownloadItem::kUninitializedHandle), 233 db_handle_(DownloadItem::kUninitializedHandle),
223 download_manager_(download_manager), 234 download_manager_(download_manager),
224 is_paused_(false), 235 is_paused_(false),
225 open_when_complete_(false), 236 open_when_complete_(false),
226 file_externally_removed_(false), 237 file_externally_removed_(false),
227 safety_state_(SAFE), 238 safety_state_(SAFE),
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 344 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
334 345
335 received_bytes_ = bytes_so_far; 346 received_bytes_ = bytes_so_far;
336 347
337 // If we've received more data than we were expecting (bad server info?), 348 // If we've received more data than we were expecting (bad server info?),
338 // revert to 'unknown size mode'. 349 // revert to 'unknown size mode'.
339 if (received_bytes_ > total_bytes_) 350 if (received_bytes_ > total_bytes_)
340 total_bytes_ = 0; 351 total_bytes_ = 0;
341 } 352 }
342 353
354 void DownloadItem::UpdateSizeAndHashState(int64 bytes_so_far,
355 const std::string& hash_state) {
356 hash_state_ = hash_state;
357
358 UpdateSize(bytes_so_far);
359 }
360
343 // Updates from the download thread may have been posted while this download 361 // Updates from the download thread may have been posted while this download
344 // was being cancelled in the UI thread, so we'll accept them unless we're 362 // was being cancelled in the UI thread, so we'll accept them unless we're
345 // complete. 363 // complete.
346 void DownloadItem::Update(int64 bytes_so_far) { 364 void DownloadItem::Update(int64 bytes_so_far, const std::string& hash_state) {
347 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 365 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
348 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 366 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
349 367
350 if (!IsInProgress()) { 368 if (!IsInProgress()) {
351 NOTREACHED(); 369 NOTREACHED();
352 return; 370 return;
353 } 371 }
354 UpdateSize(bytes_so_far); 372 UpdateSizeAndHashState(bytes_so_far, hash_state);
355 UpdateObservers(); 373 UpdateObservers();
356 } 374 }
357 375
358 // Triggered by a user action. 376 // Triggered by a user action.
359 void DownloadItem::Cancel(bool user_cancel) { 377 void DownloadItem::Cancel(bool user_cancel) {
360 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 378 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
361 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 379 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
362 380
363 last_reason_ = user_cancel ? 381 last_reason_ = user_cancel ?
364 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : 382 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED :
(...skipping 26 matching lines...) Expand all
391 auto_opened_ = true; 409 auto_opened_ = true;
392 Completed(); 410 Completed();
393 } 411 }
394 412
395 void DownloadItem::OnAllDataSaved(int64 size, const std::string& final_hash) { 413 void DownloadItem::OnAllDataSaved(int64 size, const std::string& final_hash) {
396 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 414 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
397 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 415 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
398 416
399 DCHECK(!all_data_saved_); 417 DCHECK(!all_data_saved_);
400 all_data_saved_ = true; 418 all_data_saved_ = true;
419 hash_ = final_hash;
401 UpdateSize(size); 420 UpdateSize(size);
402 hash_ = final_hash;
403 } 421 }
404 422
405 void DownloadItem::OnDownloadedFileRemoved() { 423 void DownloadItem::OnDownloadedFileRemoved() {
406 file_externally_removed_ = true; 424 file_externally_removed_ = true;
407 UpdateObservers(); 425 UpdateObservers();
408 } 426 }
409 427
410 void DownloadItem::Completed() { 428 void DownloadItem::Completed() {
411 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 429 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
412 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 430 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 } 471 }
454 472
455 void DownloadItem::UpdateTarget() { 473 void DownloadItem::UpdateTarget() {
456 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 474 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
457 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 475 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
458 476
459 if (state_info_.target_name.value().empty()) 477 if (state_info_.target_name.value().empty())
460 state_info_.target_name = full_path_.BaseName(); 478 state_info_.target_name = full_path_.BaseName();
461 } 479 }
462 480
463 void DownloadItem::Interrupted(int64 size, InterruptReason reason) { 481 void DownloadItem::Interrupted(int64 size,
482 const std::string& hash_state,
483 InterruptReason reason) {
464 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 484 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
465 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 485 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
466 486
467 if (!IsInProgress()) 487 if (!IsInProgress())
468 return; 488 return;
469 489
470 last_reason_ = reason; 490 last_reason_ = reason;
471 UpdateSize(size); 491 UpdateSizeAndHashState(size, hash_state);
472 download_stats::RecordDownloadInterrupted(reason, 492 download_stats::RecordDownloadInterrupted(reason,
473 received_bytes_, 493 received_bytes_,
474 total_bytes_); 494 total_bytes_);
475 TransitionTo(INTERRUPTED); 495 TransitionTo(INTERRUPTED);
476 } 496 }
477 497
478 void DownloadItem::Delete(DeleteReason reason) { 498 void DownloadItem::Delete(DeleteReason reason) {
479 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 499 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
480 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 500 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
481 501
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 698
679 void DownloadItem::MarkContentDangerous() { 699 void DownloadItem::MarkContentDangerous() {
680 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 700 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
681 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 701 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
682 state_info_.danger = DownloadStateInfo::DANGEROUS_CONTENT; 702 state_info_.danger = DownloadStateInfo::DANGEROUS_CONTENT;
683 UpdateSafetyState(); 703 UpdateSafetyState();
684 } 704 }
685 705
686 DownloadPersistentStoreInfo DownloadItem::GetPersistentStoreInfo() const { 706 DownloadPersistentStoreInfo DownloadItem::GetPersistentStoreInfo() const {
687 return DownloadPersistentStoreInfo(full_path(), 707 return DownloadPersistentStoreInfo(full_path(),
708 target_name(),
688 GetURL(), 709 GetURL(),
689 referrer_url(), 710 referrer_url(),
690 start_time(), 711 start_time(),
691 end_time(), 712 end_time(),
692 received_bytes(), 713 received_bytes(),
693 total_bytes(), 714 total_bytes(),
694 state(), 715 state(),
695 db_handle(), 716 db_handle(),
696 opened()); 717 opened(),
718 hash_state_,
719 last_modified_time_,
720 etag_,
721 last_reason_);
697 } 722 }
698 723
699 TabContents* DownloadItem::GetTabContents() const { 724 TabContents* DownloadItem::GetTabContents() const {
700 // TODO(rdsmith): Remove null check after removing GetTabContents() from 725 // TODO(rdsmith): Remove null check after removing GetTabContents() from
701 // paths that might be used by DownloadItems created from history import. 726 // paths that might be used by DownloadItems created from history import.
702 // Currently such items have null request_handle_s, where other items 727 // Currently such items have null request_handle_s, where other items
703 // (regular and SavePackage downloads) have actual objects off the pointer. 728 // (regular and SavePackage downloads) have actual objects off the pointer.
704 if (request_handle_.get()) 729 if (request_handle_.get())
705 return request_handle_->GetTabContents(); 730 return request_handle_->GetTabContents();
706 return NULL; 731 return NULL;
(...skipping 24 matching lines...) Expand all
731 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 756 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
732 base::Bind(&DownloadFileManager::CancelDownload, 757 base::Bind(&DownloadFileManager::CancelDownload,
733 file_manager, global_id())); 758 file_manager, global_id()));
734 } 759 }
735 760
736 void DownloadItem::Init(bool active) { 761 void DownloadItem::Init(bool active) {
737 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 762 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
738 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 763 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
739 764
740 UpdateTarget(); 765 UpdateTarget();
741 if (active) { 766 if (active)
742 download_stats::RecordDownloadCount(download_stats::START_COUNT); 767 download_stats::RecordDownloadCount(download_stats::START_COUNT);
743 }
744 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); 768 VLOG(20) << __FUNCTION__ << "() " << DebugString(true);
745 } 769 }
746 770
747 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to 771 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to
748 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. 772 // |IsPartialDownload()|, when resuming interrupted downloads is implemented.
749 bool DownloadItem::IsPartialDownload() const { 773 bool DownloadItem::IsPartialDownload() const {
750 return (state_ == IN_PROGRESS); 774 return (state_ == IN_PROGRESS);
751 } 775 }
752 776
753 bool DownloadItem::IsInProgress() const { 777 bool DownloadItem::IsInProgress() const {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
794 } 818 }
795 819
796 if (verbose) { 820 if (verbose) {
797 description += base::StringPrintf( 821 description += base::StringPrintf(
798 " db_handle = %" PRId64 822 " db_handle = %" PRId64
799 " total_bytes = %" PRId64 823 " total_bytes = %" PRId64
800 " received_bytes = %" PRId64 824 " received_bytes = %" PRId64
801 " is_paused = %c" 825 " is_paused = %c"
802 " is_otr = %c" 826 " is_otr = %c"
803 " safety_state = %s" 827 " safety_state = %s"
828 " last_modified = '%s'"
829 " etag = '%s'"
804 " url_chain = \n\t\"%s\"\n\t" 830 " url_chain = \n\t\"%s\"\n\t"
805 " target_name = \"%" PRFilePath "\"" 831 " target_name = \"%" PRFilePath "\""
806 " full_path = \"%" PRFilePath "\"", 832 " full_path = \"%" PRFilePath "\"",
807 db_handle(), 833 db_handle(),
808 total_bytes(), 834 total_bytes(),
809 received_bytes(), 835 received_bytes(),
810 is_paused() ? 'T' : 'F', 836 is_paused() ? 'T' : 'F',
811 is_otr() ? 'T' : 'F', 837 is_otr() ? 'T' : 'F',
812 DebugSafetyStateString(safety_state()), 838 DebugSafetyStateString(safety_state()),
839 last_modified_time_.c_str(),
840 etag_.c_str(),
813 url_list.c_str(), 841 url_list.c_str(),
814 state_info_.target_name.value().c_str(), 842 state_info_.target_name.value().c_str(),
815 full_path().value().c_str()); 843 full_path().value().c_str());
816 } else { 844 } else {
817 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); 845 description += base::StringPrintf(" url = \"%s\"", url_list.c_str());
818 } 846 }
819 847
820 description += " }"; 848 description += " }";
821 849
822 return description; 850 return description;
823 } 851 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698