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

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: Incorporated Randy's comments. 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/bind.h" 9 #include "base/bind.h"
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 123
124 // Constructor for reading from the history service. 124 // Constructor for reading from the history service.
125 DownloadItem::DownloadItem(DownloadManager* download_manager, 125 DownloadItem::DownloadItem(DownloadManager* download_manager,
126 const DownloadPersistentStoreInfo& info) 126 const DownloadPersistentStoreInfo& info)
127 : download_id_(download_manager->GetNextId()), 127 : download_id_(download_manager->GetNextId()),
128 full_path_(info.path), 128 full_path_(info.path),
129 url_chain_(1, info.url), 129 url_chain_(1, info.url),
130 referrer_url_(info.referrer_url), 130 referrer_url_(info.referrer_url),
131 total_bytes_(info.total_bytes), 131 total_bytes_(info.total_bytes),
132 received_bytes_(info.received_bytes), 132 received_bytes_(info.received_bytes),
133 hash_(info.hash),
134 last_modified_time_(info.last_modified_time),
135 etag_(info.etag),
136 last_reason_(info.last_reason),
133 start_tick_(base::TimeTicks()), 137 start_tick_(base::TimeTicks()),
134 state_(static_cast<DownloadState>(info.state)), 138 state_(static_cast<DownloadState>(info.state)),
135 start_time_(info.start_time), 139 start_time_(info.start_time),
136 end_time_(info.end_time), 140 end_time_(info.end_time),
137 db_handle_(info.db_handle), 141 db_handle_(info.db_handle),
138 download_manager_(download_manager), 142 download_manager_(download_manager),
139 is_paused_(false), 143 is_paused_(false),
140 open_when_complete_(false), 144 open_when_complete_(false),
141 file_externally_removed_(false), 145 file_externally_removed_(false),
142 safety_state_(SAFE), 146 safety_state_(SAFE),
(...skipping 25 matching lines...) Expand all
168 full_path_(info.path), 172 full_path_(info.path),
169 url_chain_(info.url_chain), 173 url_chain_(info.url_chain),
170 referrer_url_(info.referrer_url), 174 referrer_url_(info.referrer_url),
171 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), 175 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)),
172 content_disposition_(info.content_disposition), 176 content_disposition_(info.content_disposition),
173 mime_type_(info.mime_type), 177 mime_type_(info.mime_type),
174 original_mime_type_(info.original_mime_type), 178 original_mime_type_(info.original_mime_type),
175 referrer_charset_(info.referrer_charset), 179 referrer_charset_(info.referrer_charset),
176 total_bytes_(info.total_bytes), 180 total_bytes_(info.total_bytes),
177 received_bytes_(0), 181 received_bytes_(0),
182 hash_(DownloadItem::kEmptyFileHash),
183 last_modified_time_(info.last_modified),
184 etag_(info.etag),
178 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), 185 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE),
179 start_tick_(base::TimeTicks::Now()), 186 start_tick_(base::TimeTicks::Now()),
180 state_(IN_PROGRESS), 187 state_(IN_PROGRESS),
181 start_time_(info.start_time), 188 start_time_(info.start_time),
182 db_handle_(DownloadItem::kUninitializedHandle), 189 db_handle_(DownloadItem::kUninitializedHandle),
183 download_manager_(download_manager), 190 download_manager_(download_manager),
184 is_paused_(false), 191 is_paused_(false),
185 open_when_complete_(false), 192 open_when_complete_(false),
186 file_externally_removed_(false), 193 file_externally_removed_(false),
187 safety_state_(SAFE), 194 safety_state_(SAFE),
188 auto_opened_(false), 195 auto_opened_(false),
189 is_otr_(is_otr), 196 is_otr_(is_otr),
190 is_temporary_(!info.save_info.file_path.empty()), 197 is_temporary_(!info.save_info.file_path.empty() &&
198 (info.received_bytes == 0)),
Randy Smith (Not in Mondays) 2011/11/16 21:40:25 What's the use case for adding this test? I would
ahendrickson 2011/11/19 20:18:04 Reverted. This is not a path that resuming downlo
191 all_data_saved_(false), 199 all_data_saved_(false),
192 opened_(false), 200 opened_(false),
193 open_enabled_(true), 201 open_enabled_(true),
194 delegate_delayed_complete_(false) { 202 delegate_delayed_complete_(false) {
195 Init(true /* actively downloading */); 203 Init(true /* actively downloading */);
196 } 204 }
197 205
198 // Constructing for the "Save Page As..." feature: 206 // Constructing for the "Save Page As..." feature:
199 DownloadItem::DownloadItem(DownloadManager* download_manager, 207 DownloadItem::DownloadItem(DownloadManager* download_manager,
200 const FilePath& path, 208 const FilePath& path,
201 const GURL& url, 209 const GURL& url,
202 bool is_otr, 210 bool is_otr,
203 DownloadId download_id) 211 DownloadId download_id)
204 : download_id_(download_id), 212 : download_id_(download_id),
205 full_path_(path), 213 full_path_(path),
206 url_chain_(1, url), 214 url_chain_(1, url),
207 referrer_url_(GURL()), 215 referrer_url_(GURL()),
208 total_bytes_(0), 216 total_bytes_(0),
209 received_bytes_(0), 217 received_bytes_(0),
218 hash_(DownloadItem::kEmptyFileHash),
210 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), 219 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE),
211 start_tick_(base::TimeTicks::Now()), 220 start_tick_(base::TimeTicks::Now()),
212 state_(IN_PROGRESS), 221 state_(IN_PROGRESS),
213 start_time_(base::Time::Now()), 222 start_time_(base::Time::Now()),
214 db_handle_(DownloadItem::kUninitializedHandle), 223 db_handle_(DownloadItem::kUninitializedHandle),
215 download_manager_(download_manager), 224 download_manager_(download_manager),
216 is_paused_(false), 225 is_paused_(false),
217 open_when_complete_(false), 226 open_when_complete_(false),
218 file_externally_removed_(false), 227 file_externally_removed_(false),
219 safety_state_(SAFE), 228 safety_state_(SAFE),
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", 322 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated",
314 GetDangerType(), 323 GetDangerType(),
315 DANGEROUS_TYPE_MAX); 324 DANGEROUS_TYPE_MAX);
316 325
317 safety_state_ = DANGEROUS_BUT_VALIDATED; 326 safety_state_ = DANGEROUS_BUT_VALIDATED;
318 UpdateObservers(); 327 UpdateObservers();
319 328
320 download_manager_->MaybeCompleteDownload(this); 329 download_manager_->MaybeCompleteDownload(this);
321 } 330 }
322 331
323 void DownloadItem::UpdateSize(int64 bytes_so_far) { 332 void DownloadItem::UpdateSizeAndHash(int64 bytes_so_far,
333 const std::string& partial_hash) {
324 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 334 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
325 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 335 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
326 336
337 if (!partial_hash.empty() && !BaseFile::IsEmptySha256Hash(partial_hash))
338 hash_ = partial_hash;
Randy Smith (Not in Mondays) 2011/11/16 21:40:25 Why not unilaterally assign?
ahendrickson 2011/11/19 20:18:04 Done.
339
327 received_bytes_ = bytes_so_far; 340 received_bytes_ = bytes_so_far;
328 341
329 // If we've received more data than we were expecting (bad server info?), 342 // If we've received more data than we were expecting (bad server info?),
330 // revert to 'unknown size mode'. 343 // revert to 'unknown size mode'.
331 if (received_bytes_ > total_bytes_) 344 if (received_bytes_ > total_bytes_)
332 total_bytes_ = 0; 345 total_bytes_ = 0;
333 } 346 }
334 347
335 // Updates from the download thread may have been posted while this download 348 // 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 349 // was being cancelled in the UI thread, so we'll accept them unless we're
337 // complete. 350 // complete.
338 void DownloadItem::Update(int64 bytes_so_far) { 351 void DownloadItem::Update(int64 bytes_so_far, const std::string& partial_hash) {
339 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 352 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
340 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 353 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
341 354
342 if (!IsInProgress()) { 355 if (!IsInProgress()) {
343 NOTREACHED(); 356 NOTREACHED();
344 return; 357 return;
345 } 358 }
346 UpdateSize(bytes_so_far); 359 UpdateSizeAndHash(bytes_so_far, partial_hash);
347 UpdateObservers(); 360 UpdateObservers();
348 } 361 }
349 362
350 // Triggered by a user action. 363 // Triggered by a user action.
351 void DownloadItem::Cancel(bool user_cancel) { 364 void DownloadItem::Cancel(bool user_cancel) {
352 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 365 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
353 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 366 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
354 367
355 last_reason_ = user_cancel ? 368 last_reason_ = user_cancel ?
356 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED : 369 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED :
(...skipping 26 matching lines...) Expand all
383 auto_opened_ = true; 396 auto_opened_ = true;
384 Completed(); 397 Completed();
385 } 398 }
386 399
387 void DownloadItem::OnAllDataSaved(int64 size, const std::string& final_hash) { 400 void DownloadItem::OnAllDataSaved(int64 size, const std::string& final_hash) {
388 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 401 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
389 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 402 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
390 403
391 DCHECK(!all_data_saved_); 404 DCHECK(!all_data_saved_);
392 all_data_saved_ = true; 405 all_data_saved_ = true;
393 UpdateSize(size); 406 UpdateSizeAndHash(size, final_hash);
394 hash_ = final_hash;
395 } 407 }
396 408
397 void DownloadItem::OnDownloadedFileRemoved() { 409 void DownloadItem::OnDownloadedFileRemoved() {
398 file_externally_removed_ = true; 410 file_externally_removed_ = true;
399 UpdateObservers(); 411 UpdateObservers();
400 } 412 }
401 413
402 void DownloadItem::Completed() { 414 void DownloadItem::Completed() {
403 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 415 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
404 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 416 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 } 458 }
447 459
448 void DownloadItem::UpdateTarget() { 460 void DownloadItem::UpdateTarget() {
449 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 461 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
450 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 462 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
451 463
452 if (state_info_.target_name.value().empty()) 464 if (state_info_.target_name.value().empty())
453 state_info_.target_name = full_path_.BaseName(); 465 state_info_.target_name = full_path_.BaseName();
454 } 466 }
455 467
456 void DownloadItem::Interrupted(int64 size, InterruptReason reason) { 468 void DownloadItem::Interrupted(int64 size,
469 const std::string partial_hash,
470 InterruptReason reason) {
457 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 471 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
458 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 472 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
459 473
460 if (!IsInProgress()) 474 if (!IsInProgress())
461 return; 475 return;
462 476
463 last_reason_ = reason; 477 last_reason_ = reason;
464 UpdateSize(size); 478 UpdateSizeAndHash(size, partial_hash);
465 download_stats::RecordDownloadInterrupted(reason, 479 download_stats::RecordDownloadInterrupted(reason,
466 received_bytes_, 480 received_bytes_,
467 total_bytes_); 481 total_bytes_);
468 TransitionTo(INTERRUPTED); 482 TransitionTo(INTERRUPTED);
469 } 483 }
470 484
471 void DownloadItem::Delete(DeleteReason reason) { 485 void DownloadItem::Delete(DeleteReason reason) {
472 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 486 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
473 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 487 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
474 488
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 DownloadPersistentStoreInfo DownloadItem::GetPersistentStoreInfo() const { 687 DownloadPersistentStoreInfo DownloadItem::GetPersistentStoreInfo() const {
674 return DownloadPersistentStoreInfo(full_path(), 688 return DownloadPersistentStoreInfo(full_path(),
675 GetURL(), 689 GetURL(),
676 referrer_url(), 690 referrer_url(),
677 start_time(), 691 start_time(),
678 end_time(), 692 end_time(),
679 received_bytes(), 693 received_bytes(),
680 total_bytes(), 694 total_bytes(),
681 state(), 695 state(),
682 db_handle(), 696 db_handle(),
683 opened()); 697 opened(),
698 hash_,
699 last_modified_time_,
700 etag_,
701 last_reason_);
684 } 702 }
685 703
686 TabContents* DownloadItem::GetTabContents() const { 704 TabContents* DownloadItem::GetTabContents() const {
687 if (request_handle_.get()) 705 if (request_handle_.get())
688 return request_handle_->GetTabContents(); 706 return request_handle_->GetTabContents();
689 return NULL; 707 return NULL;
690 } 708 }
691 709
692 FilePath DownloadItem::GetTargetFilePath() const { 710 FilePath DownloadItem::GetTargetFilePath() const {
693 return full_path_.DirName().Append(state_info_.target_name); 711 return full_path_.DirName().Append(state_info_.target_name);
(...skipping 20 matching lines...) Expand all
714 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 732 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
715 base::Bind(&DownloadFileManager::CancelDownload, 733 base::Bind(&DownloadFileManager::CancelDownload,
716 file_manager, global_id())); 734 file_manager, global_id()));
717 } 735 }
718 736
719 void DownloadItem::Init(bool active) { 737 void DownloadItem::Init(bool active) {
720 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 738 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
721 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 739 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
722 740
723 UpdateTarget(); 741 UpdateTarget();
724 if (active) { 742 if (active)
725 download_stats::RecordDownloadCount(download_stats::START_COUNT); 743 download_stats::RecordDownloadCount(download_stats::START_COUNT);
726 }
727 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); 744 VLOG(20) << __FUNCTION__ << "() " << DebugString(true);
728 } 745 }
729 746
730 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to 747 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to
731 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. 748 // |IsPartialDownload()|, when resuming interrupted downloads is implemented.
732 bool DownloadItem::IsPartialDownload() const { 749 bool DownloadItem::IsPartialDownload() const {
733 return (state_ == IN_PROGRESS); 750 return (state_ == IN_PROGRESS);
734 } 751 }
735 752
736 bool DownloadItem::IsInProgress() const { 753 bool DownloadItem::IsInProgress() const {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
777 } 794 }
778 795
779 if (verbose) { 796 if (verbose) {
780 description += base::StringPrintf( 797 description += base::StringPrintf(
781 " db_handle = %" PRId64 798 " db_handle = %" PRId64
782 " total_bytes = %" PRId64 799 " total_bytes = %" PRId64
783 " received_bytes = %" PRId64 800 " received_bytes = %" PRId64
784 " is_paused = %c" 801 " is_paused = %c"
785 " is_otr = %c" 802 " is_otr = %c"
786 " safety_state = %s" 803 " safety_state = %s"
804 " last_modified = '%s'"
805 " etag = '%s'"
787 " url_chain = \n\t\"%s\"\n\t" 806 " url_chain = \n\t\"%s\"\n\t"
788 " target_name = \"%" PRFilePath "\"" 807 " target_name = \"%" PRFilePath "\""
789 " full_path = \"%" PRFilePath "\"", 808 " full_path = \"%" PRFilePath "\"",
790 db_handle(), 809 db_handle(),
791 total_bytes(), 810 total_bytes(),
792 received_bytes(), 811 received_bytes(),
793 is_paused() ? 'T' : 'F', 812 is_paused() ? 'T' : 'F',
794 is_otr() ? 'T' : 'F', 813 is_otr() ? 'T' : 'F',
795 DebugSafetyStateString(safety_state()), 814 DebugSafetyStateString(safety_state()),
815 last_modified_time_.c_str(),
816 etag_.c_str(),
796 url_list.c_str(), 817 url_list.c_str(),
797 state_info_.target_name.value().c_str(), 818 state_info_.target_name.value().c_str(),
798 full_path().value().c_str()); 819 full_path().value().c_str());
799 } else { 820 } else {
800 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); 821 description += base::StringPrintf(" url = \"%s\"", url_list.c_str());
801 } 822 }
802 823
803 description += " }"; 824 description += " }";
804 825
805 return description; 826 return description;
806 } 827 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698