Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // File method ordering: Methods in this file are in the same order as | 5 // File method ordering: Methods in this file are in the same order as |
| 6 // in download_item_impl.h, with the following exception: The public | 6 // in download_item_impl.h, with the following exception: The public |
| 7 // interface Start is placed in chronological order with the other | 7 // interface Start is placed in chronological order with the other |
| 8 // (private) routines that together define a DownloadItem's state | 8 // (private) routines that together define a DownloadItem's state |
| 9 // transitions as the download progresses. See "Download progression | 9 // transitions as the download progresses. See "Download progression |
| 10 // cascade" later in this file. | 10 // cascade" later in this file. |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 file_externally_removed_(false), | 153 file_externally_removed_(false), |
| 154 auto_opened_(false), | 154 auto_opened_(false), |
| 155 is_temporary_(false), | 155 is_temporary_(false), |
| 156 all_data_saved_(state == COMPLETE), | 156 all_data_saved_(state == COMPLETE), |
| 157 destination_error_(content::DOWNLOAD_INTERRUPT_REASON_NONE), | 157 destination_error_(content::DOWNLOAD_INTERRUPT_REASON_NONE), |
| 158 opened_(opened), | 158 opened_(opened), |
| 159 delegate_delayed_complete_(false), | 159 delegate_delayed_complete_(false), |
| 160 bound_net_log_(bound_net_log), | 160 bound_net_log_(bound_net_log), |
| 161 weak_ptr_factory_(this) { | 161 weak_ptr_factory_(this) { |
| 162 delegate_->Attach(); | 162 delegate_->Attach(); |
| 163 DCHECK_NE(IN_PROGRESS_INTERNAL, state_); | 163 DCHECK(state_ == COMPLETE_INTERNAL || state_ == INTERRUPTED_INTERNAL || |
| 164 state_ == CANCELLED_INTERNAL); | |
| 164 Init(false /* not actively downloading */, SRC_HISTORY_IMPORT); | 165 Init(false /* not actively downloading */, SRC_HISTORY_IMPORT); |
| 165 } | 166 } |
| 166 | 167 |
| 167 // Constructing for a regular download: | 168 // Constructing for a regular download: |
| 168 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate, | 169 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate, |
| 169 uint32_t download_id, | 170 uint32_t download_id, |
| 170 const DownloadCreateInfo& info, | 171 const DownloadCreateInfo& info, |
| 171 const net::BoundNetLog& bound_net_log) | 172 const net::BoundNetLog& bound_net_log) |
| 172 : is_save_package_download_(false), | 173 : is_save_package_download_(false), |
| 173 download_id_(download_id), | 174 download_id_(download_id), |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 186 mime_type_(info.mime_type), | 187 mime_type_(info.mime_type), |
| 187 original_mime_type_(info.original_mime_type), | 188 original_mime_type_(info.original_mime_type), |
| 188 remote_address_(info.remote_address), | 189 remote_address_(info.remote_address), |
| 189 total_bytes_(info.total_bytes), | 190 total_bytes_(info.total_bytes), |
| 190 received_bytes_(0), | 191 received_bytes_(0), |
| 191 bytes_per_sec_(0), | 192 bytes_per_sec_(0), |
| 192 last_modified_time_(info.last_modified), | 193 last_modified_time_(info.last_modified), |
| 193 etag_(info.etag), | 194 etag_(info.etag), |
| 194 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 195 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
| 195 start_tick_(base::TimeTicks::Now()), | 196 start_tick_(base::TimeTicks::Now()), |
| 196 state_(IN_PROGRESS_INTERNAL), | 197 state_(INITIAL_INTERNAL), |
| 197 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), | 198 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), |
| 198 start_time_(info.start_time), | 199 start_time_(info.start_time), |
| 199 delegate_(delegate), | 200 delegate_(delegate), |
| 200 is_paused_(false), | 201 is_paused_(false), |
| 201 auto_resume_count_(0), | 202 auto_resume_count_(0), |
| 202 open_when_complete_(false), | 203 open_when_complete_(false), |
| 203 file_externally_removed_(false), | 204 file_externally_removed_(false), |
| 204 auto_opened_(false), | 205 auto_opened_(false), |
| 205 is_temporary_(!info.save_info->file_path.empty()), | 206 is_temporary_(!info.save_info->file_path.empty()), |
| 206 all_data_saved_(false), | 207 all_data_saved_(false), |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 UpdateObservers(); | 321 UpdateObservers(); |
| 321 | 322 |
| 322 MaybeCompleteDownload(); | 323 MaybeCompleteDownload(); |
| 323 } | 324 } |
| 324 | 325 |
| 325 void DownloadItemImpl::StealDangerousDownload( | 326 void DownloadItemImpl::StealDangerousDownload( |
| 326 const AcquireFileCallback& callback) { | 327 const AcquireFileCallback& callback) { |
| 327 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 328 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 328 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 329 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 329 DCHECK(IsDangerous()); | 330 DCHECK(IsDangerous()); |
| 331 | |
| 330 if (download_file_) { | 332 if (download_file_) { |
| 331 BrowserThread::PostTaskAndReplyWithResult( | 333 BrowserThread::PostTaskAndReplyWithResult( |
| 332 BrowserThread::FILE, | 334 BrowserThread::FILE, |
| 333 FROM_HERE, | 335 FROM_HERE, |
| 334 base::Bind(&DownloadFileDetach, base::Passed(&download_file_)), | 336 base::Bind(&DownloadFileDetach, base::Passed(&download_file_)), |
| 335 callback); | 337 callback); |
| 336 } else { | 338 } else { |
| 337 callback.Run(current_path_); | 339 callback.Run(current_path_); |
| 338 } | 340 } |
| 339 current_path_.clear(); | 341 current_path_.clear(); |
| 340 Remove(); | 342 Remove(); |
| 341 // We have now been deleted. | 343 // We have now been deleted. |
| 342 } | 344 } |
| 343 | 345 |
| 344 void DownloadItemImpl::Pause() { | 346 void DownloadItemImpl::Pause() { |
| 345 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 347 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 346 | 348 |
| 347 // Ignore irrelevant states. | 349 // Ignore irrelevant states. |
| 348 if (state_ != IN_PROGRESS_INTERNAL || is_paused_) | 350 if (is_paused_) |
| 349 return; | 351 return; |
| 350 | 352 |
| 351 request_handle_->PauseRequest(); | 353 switch (state_) { |
| 352 is_paused_ = true; | 354 case INITIAL_INTERNAL: |
| 353 UpdateObservers(); | 355 case COMPLETING_INTERNAL: |
| 356 case COMPLETE_INTERNAL: | |
| 357 case INTERRUPTED_INTERNAL: | |
| 358 case CANCELLED_INTERNAL: | |
| 359 case RESUMING_INTERNAL: | |
| 360 // No active request. | |
| 361 return; | |
| 362 | |
| 363 case TARGET_PENDING_INTERNAL: | |
| 364 case TARGET_RESOLVED_INTERNAL: | |
| 365 case IN_PROGRESS_INTERNAL: | |
| 366 request_handle_->PauseRequest(); | |
| 367 is_paused_ = true; | |
| 368 UpdateObservers(); | |
| 369 return; | |
| 370 | |
| 371 case MAX_DOWNLOAD_INTERNAL_STATE: | |
| 372 NOTREACHED(); | |
| 373 } | |
| 354 } | 374 } |
| 355 | 375 |
| 356 void DownloadItemImpl::Resume() { | 376 void DownloadItemImpl::Resume() { |
| 357 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 377 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 358 switch (state_) { | 378 switch (state_) { |
| 379 case INITIAL_INTERNAL: | |
| 380 case COMPLETING_INTERNAL: | |
| 381 case COMPLETE_INTERNAL: | |
| 382 case CANCELLED_INTERNAL: | |
| 383 // Nothing to resume. | |
| 384 case RESUMING_INTERNAL: | |
| 385 // Resumption in progress. | |
| 386 return; | |
| 387 | |
| 388 case TARGET_PENDING_INTERNAL: | |
| 389 case TARGET_RESOLVED_INTERNAL: | |
| 359 case IN_PROGRESS_INTERNAL: | 390 case IN_PROGRESS_INTERNAL: |
| 360 if (!is_paused_) | 391 if (!is_paused_) |
| 361 return; | 392 return; |
| 362 request_handle_->ResumeRequest(); | 393 request_handle_->ResumeRequest(); |
| 363 is_paused_ = false; | 394 is_paused_ = false; |
| 364 UpdateObservers(); | 395 UpdateObservers(); |
| 365 return; | 396 return; |
| 366 | 397 |
| 367 case COMPLETING_INTERNAL: | |
| 368 case COMPLETE_INTERNAL: | |
| 369 case CANCELLED_INTERNAL: | |
| 370 case RESUMING_INTERNAL: | |
| 371 return; | |
| 372 | |
| 373 case INTERRUPTED_INTERNAL: | 398 case INTERRUPTED_INTERNAL: |
| 374 auto_resume_count_ = 0; // User input resets the counter. | 399 auto_resume_count_ = 0; // User input resets the counter. |
| 375 ResumeInterruptedDownload(); | 400 ResumeInterruptedDownload(); |
| 376 return; | 401 return; |
| 377 | 402 |
| 378 case MAX_DOWNLOAD_INTERNAL_STATE: | 403 case MAX_DOWNLOAD_INTERNAL_STATE: |
| 379 NOTREACHED(); | 404 NOTREACHED(); |
| 380 } | 405 } |
| 381 } | 406 } |
| 382 | 407 |
| 383 void DownloadItemImpl::Cancel(bool user_cancel) { | 408 void DownloadItemImpl::Cancel(bool user_cancel) { |
| 384 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 409 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 385 | |
| 386 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 410 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 387 if (state_ != IN_PROGRESS_INTERNAL && | 411 Interrupt(user_cancel ? DOWNLOAD_INTERRUPT_REASON_USER_CANCELED |
| 388 state_ != INTERRUPTED_INTERNAL && | 412 : DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN); |
| 389 state_ != RESUMING_INTERNAL) { | |
| 390 // Small downloads might be complete before this method has a chance to run. | |
| 391 return; | |
| 392 } | |
| 393 | |
| 394 if (IsDangerous()) { | |
| 395 RecordDangerousDownloadDiscard( | |
| 396 user_cancel ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION | |
| 397 : DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN, | |
| 398 GetDangerType(), | |
| 399 GetTargetFilePath()); | |
| 400 } | |
| 401 | |
| 402 last_reason_ = user_cancel ? DOWNLOAD_INTERRUPT_REASON_USER_CANCELED | |
| 403 : DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | |
| 404 | |
| 405 RecordDownloadCount(CANCELLED_COUNT); | |
| 406 | |
| 407 // TODO(rdsmith/benjhayden): Remove condition as part of | |
| 408 // |SavePackage| integration. | |
| 409 // |download_file_| can be NULL if Interrupt() is called after the | |
| 410 // download file has been released. | |
| 411 if (!is_save_package_download_ && download_file_) | |
| 412 ReleaseDownloadFile(true); | |
| 413 | |
| 414 if (state_ == IN_PROGRESS_INTERNAL) { | |
| 415 // Cancel the originating URL request unless it's already been cancelled | |
| 416 // by interrupt. | |
| 417 request_handle_->CancelRequest(); | |
| 418 } | |
| 419 | |
| 420 // Remove the intermediate file if we are cancelling an interrupted download. | |
| 421 // Continuable interruptions leave the intermediate file around. | |
| 422 if ((state_ == INTERRUPTED_INTERNAL || state_ == RESUMING_INTERNAL) && | |
| 423 !current_path_.empty()) { | |
| 424 BrowserThread::PostTask( | |
| 425 BrowserThread::FILE, FROM_HERE, | |
| 426 base::Bind(base::IgnoreResult(&DeleteDownloadedFile), current_path_)); | |
| 427 current_path_.clear(); | |
| 428 } | |
| 429 | |
| 430 TransitionTo(CANCELLED_INTERNAL, UPDATE_OBSERVERS); | |
| 431 } | 413 } |
| 432 | 414 |
| 433 void DownloadItemImpl::Remove() { | 415 void DownloadItemImpl::Remove() { |
| 434 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 416 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 435 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 417 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 436 | 418 |
| 437 delegate_->AssertStateConsistent(this); | 419 delegate_->AssertStateConsistent(this); |
| 438 Cancel(true); | 420 Interrupt(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); |
| 439 delegate_->AssertStateConsistent(this); | 421 delegate_->AssertStateConsistent(this); |
| 440 | 422 |
| 441 NotifyRemoved(); | 423 NotifyRemoved(); |
| 442 delegate_->DownloadRemoved(this); | 424 delegate_->DownloadRemoved(this); |
| 443 // We have now been deleted. | 425 // We have now been deleted. |
| 444 } | 426 } |
| 445 | 427 |
| 446 void DownloadItemImpl::OpenDownload() { | 428 void DownloadItemImpl::OpenDownload() { |
| 447 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 429 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 448 | 430 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 488 | 470 |
| 489 bool DownloadItemImpl::IsPaused() const { | 471 bool DownloadItemImpl::IsPaused() const { |
| 490 return is_paused_; | 472 return is_paused_; |
| 491 } | 473 } |
| 492 | 474 |
| 493 bool DownloadItemImpl::IsTemporary() const { | 475 bool DownloadItemImpl::IsTemporary() const { |
| 494 return is_temporary_; | 476 return is_temporary_; |
| 495 } | 477 } |
| 496 | 478 |
| 497 bool DownloadItemImpl::CanResume() const { | 479 bool DownloadItemImpl::CanResume() const { |
| 498 if ((GetState() == IN_PROGRESS) && IsPaused()) | 480 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 499 return true; | 481 switch (state_) { |
| 482 case INITIAL_INTERNAL: | |
| 483 case COMPLETING_INTERNAL: | |
| 484 case COMPLETE_INTERNAL: | |
| 485 case CANCELLED_INTERNAL: | |
| 486 case RESUMING_INTERNAL: | |
| 487 return false; | |
| 500 | 488 |
| 501 if (state_ != INTERRUPTED_INTERNAL) | 489 case TARGET_PENDING_INTERNAL: |
| 502 return false; | 490 case TARGET_RESOLVED_INTERNAL: |
| 491 case IN_PROGRESS_INTERNAL: | |
| 492 return is_paused_; | |
| 503 | 493 |
| 504 // We currently only support HTTP(S) requests for download resumption. | 494 case INTERRUPTED_INTERNAL: { |
| 505 if (!GetURL().SchemeIsHTTPOrHTTPS()) | 495 ResumeMode resume_mode = GetResumeMode(); |
| 506 return false; | 496 // Only allow Resume() calls if the resumption mode requires a user |
| 497 // action. | |
| 498 return IsDownloadResumptionEnabled() && | |
| 499 (resume_mode == RESUME_MODE_USER_RESTART || | |
| 500 resume_mode == RESUME_MODE_USER_CONTINUE); | |
| 501 } | |
| 507 | 502 |
| 508 ResumeMode resume_mode = GetResumeMode(); | 503 case MAX_DOWNLOAD_INTERNAL_STATE: |
| 509 return IsDownloadResumptionEnabled() && | 504 NOTREACHED(); |
| 510 (resume_mode == RESUME_MODE_USER_RESTART || | 505 } |
| 511 resume_mode == RESUME_MODE_USER_CONTINUE); | 506 return false; |
| 512 } | 507 } |
| 513 | 508 |
| 514 bool DownloadItemImpl::IsDone() const { | 509 bool DownloadItemImpl::IsDone() const { |
| 515 switch (state_) { | 510 switch (state_) { |
| 511 case INITIAL_INTERNAL: | |
| 512 case COMPLETING_INTERNAL: | |
| 513 case RESUMING_INTERNAL: | |
| 514 case TARGET_PENDING_INTERNAL: | |
| 515 case TARGET_RESOLVED_INTERNAL: | |
| 516 case IN_PROGRESS_INTERNAL: | 516 case IN_PROGRESS_INTERNAL: |
| 517 case COMPLETING_INTERNAL: | |
| 518 return false; | 517 return false; |
| 519 | 518 |
| 520 case COMPLETE_INTERNAL: | 519 case COMPLETE_INTERNAL: |
| 521 case CANCELLED_INTERNAL: | 520 case CANCELLED_INTERNAL: |
| 522 return true; | 521 return true; |
| 523 | 522 |
| 524 case INTERRUPTED_INTERNAL: | 523 case INTERRUPTED_INTERNAL: |
| 525 return !CanResume(); | 524 return !CanResume(); |
| 526 | 525 |
| 527 case RESUMING_INTERNAL: | |
| 528 return false; | |
| 529 | |
| 530 case MAX_DOWNLOAD_INTERNAL_STATE: | 526 case MAX_DOWNLOAD_INTERNAL_STATE: |
| 531 break; | 527 NOTREACHED(); |
| 532 } | 528 } |
| 533 NOTREACHED(); | 529 return false; |
| 534 return true; | |
| 535 } | 530 } |
| 536 | 531 |
| 537 const GURL& DownloadItemImpl::GetURL() const { | 532 const GURL& DownloadItemImpl::GetURL() const { |
| 538 return url_chain_.empty() ? GURL::EmptyGURL() : url_chain_.back(); | 533 return url_chain_.empty() ? GURL::EmptyGURL() : url_chain_.back(); |
| 539 } | 534 } |
| 540 | 535 |
| 541 const std::vector<GURL>& DownloadItemImpl::GetUrlChain() const { | 536 const std::vector<GURL>& DownloadItemImpl::GetUrlChain() const { |
| 542 return url_chain_; | 537 return url_chain_; |
| 543 } | 538 } |
| 544 | 539 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 763 // Currently such items have null request_handle_s, where other items | 758 // Currently such items have null request_handle_s, where other items |
| 764 // (regular and SavePackage downloads) have actual objects off the pointer. | 759 // (regular and SavePackage downloads) have actual objects off the pointer. |
| 765 if (request_handle_) | 760 if (request_handle_) |
| 766 return request_handle_->GetWebContents(); | 761 return request_handle_->GetWebContents(); |
| 767 return NULL; | 762 return NULL; |
| 768 } | 763 } |
| 769 | 764 |
| 770 void DownloadItemImpl::OnContentCheckCompleted(DownloadDangerType danger_type) { | 765 void DownloadItemImpl::OnContentCheckCompleted(DownloadDangerType danger_type) { |
| 771 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 766 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 772 DCHECK(AllDataSaved()); | 767 DCHECK(AllDataSaved()); |
| 768 | |
| 769 // Danger type is only allowed to be set on an active download after all data | |
| 770 // has been saved. This excludes all other states. In particular, | |
| 771 // OnContentCheckCompleted() isn't allowed on an INTERRUPTED download since | |
| 772 // such an interruption would need to happen between OnAllDataSaved() and | |
| 773 // OnContentCheckCompleted() during which no disk or network activity | |
| 774 // should've taken place. | |
| 775 DCHECK_EQ(state_, IN_PROGRESS_INTERNAL); | |
| 773 DVLOG(20) << __FUNCTION__ << " danger_type=" << danger_type | 776 DVLOG(20) << __FUNCTION__ << " danger_type=" << danger_type |
| 774 << " download=" << DebugString(true); | 777 << " download=" << DebugString(true); |
| 775 SetDangerType(danger_type); | 778 SetDangerType(danger_type); |
| 776 UpdateObservers(); | 779 UpdateObservers(); |
| 777 } | 780 } |
| 778 | 781 |
| 779 void DownloadItemImpl::SetOpenWhenComplete(bool open) { | 782 void DownloadItemImpl::SetOpenWhenComplete(bool open) { |
| 780 open_when_complete_ = open; | 783 open_when_complete_ = open; |
| 781 } | 784 } |
| 782 | 785 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 847 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 850 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
| 848 } | 851 } |
| 849 | 852 |
| 850 description += " }"; | 853 description += " }"; |
| 851 | 854 |
| 852 return description; | 855 return description; |
| 853 } | 856 } |
| 854 | 857 |
| 855 DownloadItemImpl::ResumeMode DownloadItemImpl::GetResumeMode() const { | 858 DownloadItemImpl::ResumeMode DownloadItemImpl::GetResumeMode() const { |
| 856 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 859 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 860 | |
| 861 if (!IsDownloadResumptionEnabled()) | |
| 862 return RESUME_MODE_INVALID; | |
| 863 | |
| 864 // Cnly support resumption for HTTP(S). | |
| 865 if (!GetURL().SchemeIsHTTPOrHTTPS()) | |
| 866 return RESUME_MODE_INVALID; | |
| 867 | |
| 857 // We can't continue without a handle on the intermediate file. | 868 // We can't continue without a handle on the intermediate file. |
| 858 // We also can't continue if we don't have some verifier to make sure | 869 // We also can't continue if we don't have some verifier to make sure |
| 859 // we're getting the same file. | 870 // we're getting the same file. |
| 860 const bool force_restart = | 871 const bool force_restart = |
| 861 (current_path_.empty() || (etag_.empty() && last_modified_time_.empty())); | 872 (current_path_.empty() || (etag_.empty() && last_modified_time_.empty())); |
| 862 | 873 |
| 863 // We won't auto-restart if we've used up our attempts or the | 874 // We won't auto-restart if we've used up our attempts or the |
| 864 // download has been paused by user action. | 875 // download has been paused by user action. |
| 865 const bool force_user = | 876 const bool force_user = |
| 866 (auto_resume_count_ >= kMaxAutoResumeAttempts || is_paused_); | 877 (auto_resume_count_ >= kMaxAutoResumeAttempts || is_paused_); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 987 const net::BoundNetLog& DownloadItemImpl::GetBoundNetLog() const { | 998 const net::BoundNetLog& DownloadItemImpl::GetBoundNetLog() const { |
| 988 return bound_net_log_; | 999 return bound_net_log_; |
| 989 } | 1000 } |
| 990 | 1001 |
| 991 void DownloadItemImpl::SetTotalBytes(int64_t total_bytes) { | 1002 void DownloadItemImpl::SetTotalBytes(int64_t total_bytes) { |
| 992 total_bytes_ = total_bytes; | 1003 total_bytes_ = total_bytes; |
| 993 } | 1004 } |
| 994 | 1005 |
| 995 void DownloadItemImpl::OnAllDataSaved(const std::string& final_hash) { | 1006 void DownloadItemImpl::OnAllDataSaved(const std::string& final_hash) { |
| 996 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1007 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 997 | 1008 DCHECK(state_ == TARGET_PENDING_INTERNAL || |
| 998 DCHECK_EQ(IN_PROGRESS_INTERNAL, state_); | 1009 state_ == TARGET_RESOLVED_INTERNAL || state_ == IN_PROGRESS_INTERNAL); |
| 999 DCHECK(!all_data_saved_); | 1010 DCHECK(!all_data_saved_); |
| 1000 all_data_saved_ = true; | 1011 all_data_saved_ = true; |
| 1001 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 1012 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
| 1002 | 1013 |
| 1003 // Store final hash and null out intermediate serialized hash state. | 1014 // Store final hash and null out intermediate serialized hash state. |
| 1004 hash_ = final_hash; | 1015 hash_ = final_hash; |
| 1005 hash_state_ = ""; | 1016 hash_state_ = ""; |
| 1006 | 1017 |
| 1007 UpdateObservers(); | 1018 UpdateObservers(); |
| 1008 } | 1019 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 1020 const std::string& hash_state) { | 1031 const std::string& hash_state) { |
| 1021 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1032 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1022 DVLOG(20) << __FUNCTION__ << " so_far=" << bytes_so_far | 1033 DVLOG(20) << __FUNCTION__ << " so_far=" << bytes_so_far |
| 1023 << " per_sec=" << bytes_per_sec << " download=" | 1034 << " per_sec=" << bytes_per_sec << " download=" |
| 1024 << DebugString(true); | 1035 << DebugString(true); |
| 1025 | 1036 |
| 1026 if (GetState() != IN_PROGRESS) { | 1037 if (GetState() != IN_PROGRESS) { |
| 1027 // Ignore if we're no longer in-progress. This can happen if we race a | 1038 // Ignore if we're no longer in-progress. This can happen if we race a |
| 1028 // Cancel on the UI thread with an update on the FILE thread. | 1039 // Cancel on the UI thread with an update on the FILE thread. |
| 1029 // | 1040 // |
| 1030 // TODO(rdsmith): Arguably we should let this go through, as this means | 1041 // Arguably we should let this go through, as this means the download really |
| 1031 // the download really did get further than we know before it was | 1042 // did get further than we know before it was cancelled. But the gain isn't |
| 1032 // cancelled. But the gain isn't very large, and the code is more | 1043 // very large, and the code is more fragile if it has to support in progress |
| 1033 // fragile if it has to support in progress updates in a non-in-progress | 1044 // updates in a non-in-progress state. This issue should be readdressed |
|
Randy Smith (Not in Mondays)
2016/02/11 17:19:18
The existence of a phrase "This issue should be re
asanka
2016/02/11 21:12:05
Done.
| |
| 1034 // state. This issue should be readdressed when we revamp performance | 1045 // when we revamp performance reporting. |
| 1035 // reporting. | |
| 1036 return; | 1046 return; |
| 1037 } | 1047 } |
| 1038 bytes_per_sec_ = bytes_per_sec; | 1048 bytes_per_sec_ = bytes_per_sec; |
| 1039 hash_state_ = hash_state; | 1049 hash_state_ = hash_state; |
| 1040 received_bytes_ = bytes_so_far; | 1050 received_bytes_ = bytes_so_far; |
| 1041 | 1051 |
| 1042 // If we've received more data than we were expecting (bad server info?), | 1052 // If we've received more data than we were expecting (bad server info?), |
| 1043 // revert to 'unknown size mode'. | 1053 // revert to 'unknown size mode'. |
| 1044 if (received_bytes_ > total_bytes_) | 1054 if (received_bytes_ > total_bytes_) |
| 1045 total_bytes_ = 0; | 1055 total_bytes_ = 0; |
| 1046 | 1056 |
| 1047 if (bound_net_log_.IsCapturing()) { | 1057 if (bound_net_log_.IsCapturing()) { |
| 1048 bound_net_log_.AddEvent( | 1058 bound_net_log_.AddEvent( |
| 1049 net::NetLog::TYPE_DOWNLOAD_ITEM_UPDATED, | 1059 net::NetLog::TYPE_DOWNLOAD_ITEM_UPDATED, |
| 1050 net::NetLog::Int64Callback("bytes_so_far", received_bytes_)); | 1060 net::NetLog::Int64Callback("bytes_so_far", received_bytes_)); |
| 1051 } | 1061 } |
| 1052 | 1062 |
| 1053 UpdateObservers(); | 1063 UpdateObservers(); |
| 1054 } | 1064 } |
| 1055 | 1065 |
| 1056 void DownloadItemImpl::DestinationError(DownloadInterruptReason reason) { | 1066 void DownloadItemImpl::DestinationError(DownloadInterruptReason reason) { |
| 1067 DCHECK(state_ == TARGET_PENDING_INTERNAL || | |
| 1068 state_ == TARGET_RESOLVED_INTERNAL || state_ == IN_PROGRESS_INTERNAL); | |
| 1069 DVLOG(20) << __FUNCTION__ | |
| 1070 << "() reason:" << DownloadInterruptReasonToString(reason); | |
| 1071 | |
| 1057 // Postpone recognition of this error until after file name determination | 1072 // Postpone recognition of this error until after file name determination |
| 1058 // has completed and the intermediate file has been renamed to simplify | 1073 // has completed and the intermediate file has been renamed to simplify |
| 1059 // resumption conditions. | 1074 // resumption conditions. |
| 1060 if (current_path_.empty() || target_path_.empty()) | 1075 if (state_ != IN_PROGRESS_INTERNAL) |
| 1061 destination_error_ = reason; | 1076 destination_error_ = reason; |
| 1062 else | 1077 else |
| 1063 Interrupt(reason); | 1078 Interrupt(reason); |
| 1064 } | 1079 } |
| 1065 | 1080 |
| 1066 void DownloadItemImpl::DestinationCompleted(const std::string& final_hash) { | 1081 void DownloadItemImpl::DestinationCompleted(const std::string& final_hash) { |
| 1067 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 1082 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
| 1068 if (GetState() != IN_PROGRESS) | |
| 1069 return; | |
| 1070 OnAllDataSaved(final_hash); | 1083 OnAllDataSaved(final_hash); |
| 1071 MaybeCompleteDownload(); | 1084 MaybeCompleteDownload(); |
| 1072 } | 1085 } |
| 1073 | 1086 |
| 1074 // **** Download progression cascade | 1087 // **** Download progression cascade |
| 1075 | 1088 |
| 1076 void DownloadItemImpl::Init(bool active, | 1089 void DownloadItemImpl::Init(bool active, |
| 1077 DownloadType download_type) { | 1090 DownloadType download_type) { |
| 1078 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1091 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1079 | 1092 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1109 } | 1122 } |
| 1110 | 1123 |
| 1111 // We're starting the download. | 1124 // We're starting the download. |
| 1112 void DownloadItemImpl::Start( | 1125 void DownloadItemImpl::Start( |
| 1113 scoped_ptr<DownloadFile> file, | 1126 scoped_ptr<DownloadFile> file, |
| 1114 scoped_ptr<DownloadRequestHandleInterface> req_handle) { | 1127 scoped_ptr<DownloadRequestHandleInterface> req_handle) { |
| 1115 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1128 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1116 DCHECK(!download_file_.get()); | 1129 DCHECK(!download_file_.get()); |
| 1117 DCHECK(file.get()); | 1130 DCHECK(file.get()); |
| 1118 DCHECK(req_handle.get()); | 1131 DCHECK(req_handle.get()); |
| 1132 DVLOG(20) << __FUNCTION__ << "() this=" << DebugString(true); | |
| 1119 | 1133 |
| 1120 download_file_ = std::move(file); | 1134 download_file_ = std::move(file); |
| 1121 request_handle_ = std::move(req_handle); | 1135 request_handle_ = std::move(req_handle); |
| 1122 | 1136 |
| 1123 if (GetState() == CANCELLED) { | 1137 if (state_ == CANCELLED_INTERNAL) { |
| 1124 // The download was in the process of resuming when it was cancelled. Don't | 1138 // The download was in the process of resuming when it was cancelled. Don't |
| 1125 // proceed. | 1139 // proceed. |
| 1126 ReleaseDownloadFile(true); | 1140 ReleaseDownloadFile(true); |
| 1127 request_handle_->CancelRequest(); | 1141 request_handle_->CancelRequest(); |
| 1128 return; | 1142 return; |
| 1129 } | 1143 } |
| 1130 | 1144 |
| 1131 TransitionTo(IN_PROGRESS_INTERNAL, UPDATE_OBSERVERS); | 1145 TransitionTo(TARGET_PENDING_INTERNAL, UPDATE_OBSERVERS); |
| 1132 | 1146 |
| 1133 BrowserThread::PostTask( | 1147 BrowserThread::PostTask( |
| 1134 BrowserThread::FILE, FROM_HERE, | 1148 BrowserThread::FILE, FROM_HERE, |
| 1135 base::Bind(&DownloadFile::Initialize, | 1149 base::Bind(&DownloadFile::Initialize, |
| 1136 // Safe because we control download file lifetime. | 1150 // Safe because we control download file lifetime. |
| 1137 base::Unretained(download_file_.get()), | 1151 base::Unretained(download_file_.get()), |
| 1138 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized, | 1152 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized, |
| 1139 weak_ptr_factory_.GetWeakPtr()))); | 1153 weak_ptr_factory_.GetWeakPtr()))); |
| 1140 } | 1154 } |
| 1141 | 1155 |
| 1142 void DownloadItemImpl::OnDownloadFileInitialized( | 1156 void DownloadItemImpl::OnDownloadFileInitialized( |
| 1143 DownloadInterruptReason result) { | 1157 DownloadInterruptReason result) { |
| 1144 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1158 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1159 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); | |
| 1160 DVLOG(20) << __FUNCTION__ | |
| 1161 << "() result:" << DownloadInterruptReasonToString(result); | |
| 1145 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) { | 1162 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 1163 // Transition out to IN_PROGRESS since this DownloadItem is skipping the | |
| 1164 // download target determination process. | |
|
Randy Smith (Not in Mondays)
2016/02/11 17:19:18
I don't follow "Transition out to IN_PROGRESS"; th
asanka
2016/02/11 21:12:05
Yup :) Done
| |
| 1165 TransitionTo(TARGET_RESOLVED_INTERNAL, DONT_UPDATE_OBSERVERS); | |
| 1146 Interrupt(result); | 1166 Interrupt(result); |
| 1147 // TODO(rdsmith/asanka): Arguably we should show this in the UI, but | 1167 // TODO(rdsmith/asanka): Arguably we should show this in the UI, but |
| 1148 // it's not at all clear what to show--we haven't done filename | 1168 // it's not at all clear what to show--we haven't done filename |
| 1149 // determination, so we don't know what name to display. OTOH, | 1169 // determination, so we don't know what name to display. OTOH, |
| 1150 // the failure mode of not showing the DI if the file initialization | 1170 // the failure mode of not showing the DI if the file initialization |
| 1151 // fails isn't a good one. Can we hack up a name based on the | 1171 // fails isn't a good one. Can we hack up a name based on the |
| 1152 // URLRequest? We'll need to make sure that initialization happens | 1172 // URLRequest? We'll need to make sure that initialization happens |
| 1153 // properly. Possibly the right thing is to have the UI handle | 1173 // properly. Possibly the right thing is to have the UI handle |
| 1154 // this case specially. | 1174 // this case specially. |
| 1155 return; | 1175 return; |
| 1156 } | 1176 } |
| 1157 | 1177 |
| 1158 delegate_->DetermineDownloadTarget( | 1178 delegate_->DetermineDownloadTarget( |
| 1159 this, base::Bind(&DownloadItemImpl::OnDownloadTargetDetermined, | 1179 this, base::Bind(&DownloadItemImpl::OnDownloadTargetDetermined, |
| 1160 weak_ptr_factory_.GetWeakPtr())); | 1180 weak_ptr_factory_.GetWeakPtr())); |
| 1161 } | 1181 } |
| 1162 | 1182 |
| 1163 // Called by delegate_ when the download target path has been | 1183 // Called by delegate_ when the download target path has been |
| 1164 // determined. | 1184 // determined. |
| 1165 void DownloadItemImpl::OnDownloadTargetDetermined( | 1185 void DownloadItemImpl::OnDownloadTargetDetermined( |
| 1166 const base::FilePath& target_path, | 1186 const base::FilePath& target_path, |
| 1167 TargetDisposition disposition, | 1187 TargetDisposition disposition, |
| 1168 DownloadDangerType danger_type, | 1188 DownloadDangerType danger_type, |
| 1169 const base::FilePath& intermediate_path) { | 1189 const base::FilePath& intermediate_path) { |
| 1170 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1190 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1191 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); | |
| 1171 | 1192 |
| 1172 // If the |target_path| is empty, then we consider this download to be | 1193 // If the |target_path| is empty, then we consider this download to be |
| 1173 // canceled. | 1194 // canceled. |
| 1174 if (target_path.empty()) { | 1195 if (target_path.empty()) { |
| 1175 Cancel(true); | 1196 Cancel(true); |
| 1176 return; | 1197 return; |
| 1177 } | 1198 } |
| 1178 | 1199 |
| 1179 // TODO(rdsmith,asanka): We are ignoring the possibility that the download | 1200 // TODO(rdsmith,asanka): We are ignoring the possibility that the download |
| 1180 // has been interrupted at this point until we finish the intermediate | 1201 // has been interrupted at this point until we finish the intermediate |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1224 base::Bind(&DownloadFile::RenameAndUniquify, | 1245 base::Bind(&DownloadFile::RenameAndUniquify, |
| 1225 // Safe because we control download file lifetime. | 1246 // Safe because we control download file lifetime. |
| 1226 base::Unretained(download_file_.get()), | 1247 base::Unretained(download_file_.get()), |
| 1227 intermediate_path, callback)); | 1248 intermediate_path, callback)); |
| 1228 } | 1249 } |
| 1229 | 1250 |
| 1230 void DownloadItemImpl::OnDownloadRenamedToIntermediateName( | 1251 void DownloadItemImpl::OnDownloadRenamedToIntermediateName( |
| 1231 DownloadInterruptReason reason, | 1252 DownloadInterruptReason reason, |
| 1232 const base::FilePath& full_path) { | 1253 const base::FilePath& full_path) { |
| 1233 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1254 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1255 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); | |
| 1234 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 1256 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
| 1257 TransitionTo(TARGET_RESOLVED_INTERNAL, DONT_UPDATE_OBSERVERS); | |
| 1235 | 1258 |
| 1236 if (DOWNLOAD_INTERRUPT_REASON_NONE != destination_error_) { | 1259 if (DOWNLOAD_INTERRUPT_REASON_NONE != destination_error_) { |
| 1237 // Process destination error. If both |reason| and |destination_error_| | 1260 // Process destination error. If both |reason| and |destination_error_| |
| 1238 // refer to actual errors, we want to use the |destination_error_| as the | 1261 // refer to actual errors, we want to use the |destination_error_| as the |
| 1239 // argument to the Interrupt() routine, as it happened first. | 1262 // argument to the Interrupt() routine, as it happened first. |
| 1240 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) | 1263 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 1241 SetFullPath(full_path); | 1264 SetFullPath(full_path); |
| 1242 Interrupt(destination_error_); | 1265 Interrupt(destination_error_); |
| 1243 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE; | 1266 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 1244 } else if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) { | 1267 } else if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) { |
| 1245 Interrupt(reason); | 1268 Interrupt(reason); |
| 1246 // All file errors result in file deletion above; no need to cleanup. The | 1269 // All file errors result in file deletion above; no need to cleanup. The |
| 1247 // current_path_ should be empty. Resuming this download will force a | 1270 // current_path_ should be empty. Resuming this download will force a |
| 1248 // restart and a re-doing of filename determination. | 1271 // restart and a re-doing of filename determination. |
| 1249 DCHECK(current_path_.empty()); | 1272 DCHECK(current_path_.empty()); |
| 1250 } else { | 1273 } else { |
| 1251 SetFullPath(full_path); | 1274 SetFullPath(full_path); |
| 1252 UpdateObservers(); | 1275 TransitionTo(IN_PROGRESS_INTERNAL, UPDATE_OBSERVERS); |
| 1253 MaybeCompleteDownload(); | 1276 MaybeCompleteDownload(); |
| 1254 } | 1277 } |
| 1255 } | 1278 } |
| 1256 | 1279 |
| 1257 // When SavePackage downloads MHTML to GData (see | 1280 // When SavePackage downloads MHTML to GData (see |
| 1258 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it | 1281 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it |
| 1259 // does for non-SavePackage downloads, but SavePackage downloads never satisfy | 1282 // does for non-SavePackage downloads, but SavePackage downloads never satisfy |
| 1260 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls | 1283 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls |
| 1261 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage | 1284 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage |
| 1262 // notices that the upload has completed and runs its normal Finish() pathway. | 1285 // notices that the upload has completed and runs its normal Finish() pathway. |
| 1263 // MaybeCompleteDownload() is never the mechanism by which SavePackage completes | 1286 // MaybeCompleteDownload() is never the mechanism by which SavePackage completes |
| 1264 // downloads. SavePackage always uses its own Finish() to mark downloads | 1287 // downloads. SavePackage always uses its own Finish() to mark downloads |
| 1265 // complete. | 1288 // complete. |
| 1266 void DownloadItemImpl::MaybeCompleteDownload() { | 1289 void DownloadItemImpl::MaybeCompleteDownload() { |
| 1267 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1290 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1268 DCHECK(!is_save_package_download_); | 1291 DCHECK(!is_save_package_download_); |
| 1269 | 1292 |
| 1270 if (!IsDownloadReadyForCompletion( | 1293 if (!IsDownloadReadyForCompletion( |
| 1271 base::Bind(&DownloadItemImpl::MaybeCompleteDownload, | 1294 base::Bind(&DownloadItemImpl::MaybeCompleteDownload, |
| 1272 weak_ptr_factory_.GetWeakPtr()))) | 1295 weak_ptr_factory_.GetWeakPtr()))) |
| 1273 return; | 1296 return; |
| 1274 | 1297 |
| 1275 // TODO(rdsmith): DCHECK that we only pass through this point | 1298 // Confirm we're in the proper set of states to be here; have all data, have a |
| 1276 // once per download. The natural way to do this is by a state | 1299 // history handle, (validated or safe). |
| 1277 // transition on the DownloadItem. | |
| 1278 | |
| 1279 // Confirm we're in the proper set of states to be here; | |
| 1280 // have all data, have a history handle, (validated or safe). | |
| 1281 DCHECK_EQ(IN_PROGRESS_INTERNAL, state_); | 1300 DCHECK_EQ(IN_PROGRESS_INTERNAL, state_); |
| 1282 DCHECK(!IsDangerous()); | 1301 DCHECK(!IsDangerous()); |
| 1283 DCHECK(all_data_saved_); | 1302 DCHECK(all_data_saved_); |
| 1284 | 1303 |
| 1285 OnDownloadCompleting(); | 1304 OnDownloadCompleting(); |
| 1286 } | 1305 } |
| 1287 | 1306 |
| 1288 // Called by MaybeCompleteDownload() when it has determined that the download | 1307 // Called by MaybeCompleteDownload() when it has determined that the download |
| 1289 // is ready for completion. | 1308 // is ready for completion. |
| 1290 void DownloadItemImpl::OnDownloadCompleting() { | 1309 void DownloadItemImpl::OnDownloadCompleting() { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1349 | 1368 |
| 1350 DCHECK(target_path_ == full_path); | 1369 DCHECK(target_path_ == full_path); |
| 1351 | 1370 |
| 1352 if (full_path != current_path_) { | 1371 if (full_path != current_path_) { |
| 1353 // full_path is now the current and target file path. | 1372 // full_path is now the current and target file path. |
| 1354 DCHECK(!full_path.empty()); | 1373 DCHECK(!full_path.empty()); |
| 1355 SetFullPath(full_path); | 1374 SetFullPath(full_path); |
| 1356 } | 1375 } |
| 1357 | 1376 |
| 1358 // Complete the download and release the DownloadFile. | 1377 // Complete the download and release the DownloadFile. |
| 1359 DCHECK(download_file_.get()); | 1378 DCHECK(download_file_); |
| 1360 ReleaseDownloadFile(false); | 1379 ReleaseDownloadFile(false); |
| 1361 | 1380 |
| 1362 // We're not completely done with the download item yet, but at this | 1381 // We're not completely done with the download item yet, but at this |
| 1363 // point we're committed to complete the download. Cancels (or Interrupts, | 1382 // point we're committed to complete the download. Cancels (or Interrupts, |
| 1364 // though it's not clear how they could happen) after this point will be | 1383 // though it's not clear how they could happen) after this point will be |
| 1365 // ignored. | 1384 // ignored. |
| 1366 TransitionTo(COMPLETING_INTERNAL, DONT_UPDATE_OBSERVERS); | 1385 TransitionTo(COMPLETING_INTERNAL, DONT_UPDATE_OBSERVERS); |
| 1367 | 1386 |
| 1368 if (delegate_->ShouldOpenDownload( | 1387 if (delegate_->ShouldOpenDownload( |
| 1369 this, base::Bind(&DownloadItemImpl::DelayedDownloadOpened, | 1388 this, base::Bind(&DownloadItemImpl::DelayedDownloadOpened, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1423 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); | 1442 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); |
| 1424 Interrupt(interrupt_reason); | 1443 Interrupt(interrupt_reason); |
| 1425 } | 1444 } |
| 1426 | 1445 |
| 1427 // **** End of Download progression cascade | 1446 // **** End of Download progression cascade |
| 1428 | 1447 |
| 1429 // An error occurred somewhere. | 1448 // An error occurred somewhere. |
| 1430 void DownloadItemImpl::Interrupt(DownloadInterruptReason reason) { | 1449 void DownloadItemImpl::Interrupt(DownloadInterruptReason reason) { |
| 1431 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1450 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1432 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, reason); | 1451 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, reason); |
| 1452 DVLOG(20) << __FUNCTION__ | |
| 1453 << "() reason:" << DownloadInterruptReasonToString(reason) | |
| 1454 << " this=" << DebugString(true); | |
| 1433 | 1455 |
| 1434 // Somewhat counter-intuitively, it is possible for us to receive an | 1456 // Somewhat counter-intuitively, it is possible for us to receive an |
| 1435 // interrupt after we've already been interrupted. The generation of | 1457 // interrupt after we've already been interrupted. The generation of |
| 1436 // interrupts from the file thread Renames and the generation of | 1458 // interrupts from the file thread Renames and the generation of |
| 1437 // interrupts from disk writes go through two different mechanisms (driven | 1459 // interrupts from disk writes go through two different mechanisms (driven |
| 1438 // by rename requests from UI thread and by write requests from IO thread, | 1460 // by rename requests from UI thread and by write requests from IO thread, |
| 1439 // respectively), and since we choose not to keep state on the File thread, | 1461 // respectively), and since we choose not to keep state on the File thread, |
| 1440 // this is the place where the races collide. It's also possible for | 1462 // this is the place where the races collide. It's also possible for |
| 1441 // interrupts to race with cancels. | 1463 // interrupts to race with cancels. |
| 1464 switch (state_) { | |
| 1465 case CANCELLED_INTERNAL: | |
| 1466 // If the download is already cancelled, then there's no point in | |
| 1467 // transitioning out to interrupted. | |
| 1468 case COMPLETING_INTERNAL: | |
| 1469 case COMPLETE_INTERNAL: | |
| 1470 // Already complete. | |
| 1471 return; | |
| 1442 | 1472 |
| 1443 // Whatever happens, the first one to hit the UI thread wins. | 1473 case TARGET_PENDING_INTERNAL: |
| 1444 if (state_ != IN_PROGRESS_INTERNAL && state_ != RESUMING_INTERNAL) | 1474 case TARGET_RESOLVED_INTERNAL: |
| 1445 return; | 1475 case IN_PROGRESS_INTERNAL: |
| 1476 break; | |
| 1477 | |
| 1478 case RESUMING_INTERNAL: | |
| 1479 case INTERRUPTED_INTERNAL: | |
| 1480 // The first non-cancel interrupt reason wins in cases where multiple | |
| 1481 // things go wrong. | |
| 1482 if (reason != DOWNLOAD_INTERRUPT_REASON_USER_CANCELED && | |
| 1483 reason != DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN) | |
| 1484 return; | |
| 1485 | |
| 1486 if (!current_path_.empty()) { | |
| 1487 BrowserThread::PostTask( | |
| 1488 BrowserThread::FILE, FROM_HERE, | |
| 1489 base::Bind(base::IgnoreResult(&DeleteDownloadedFile), | |
| 1490 current_path_)); | |
| 1491 current_path_.clear(); | |
| 1492 } | |
| 1493 break; | |
| 1494 | |
| 1495 case INITIAL_INTERNAL: | |
| 1496 case MAX_DOWNLOAD_INTERNAL_STATE: | |
| 1497 NOTREACHED(); | |
| 1498 return; | |
| 1499 } | |
| 1446 | 1500 |
| 1447 last_reason_ = reason; | 1501 last_reason_ = reason; |
| 1448 | 1502 if (download_file_) { |
| 1449 ResumeMode resume_mode = GetResumeMode(); | 1503 ResumeMode resume_mode = GetResumeMode(); |
| 1450 | 1504 ReleaseDownloadFile(resume_mode != RESUME_MODE_IMMEDIATE_CONTINUE && |
| 1451 if (state_ == IN_PROGRESS_INTERNAL) { | 1505 resume_mode != RESUME_MODE_USER_CONTINUE); |
| 1452 // Cancel (delete file) if: | |
| 1453 // 1) we're going to restart. | |
| 1454 // 2) Resumption isn't possible (download was cancelled or blocked due to | |
| 1455 // security restrictions). | |
| 1456 // 3) Resumption isn't enabled. | |
| 1457 // No point in leaving data around we aren't going to use. | |
| 1458 ReleaseDownloadFile(resume_mode == RESUME_MODE_IMMEDIATE_RESTART || | |
| 1459 resume_mode == RESUME_MODE_USER_RESTART || | |
| 1460 resume_mode == RESUME_MODE_INVALID || | |
| 1461 !IsDownloadResumptionEnabled()); | |
| 1462 | |
| 1463 // Cancel the originating URL request. | |
| 1464 request_handle_->CancelRequest(); | |
| 1465 } else { | |
| 1466 DCHECK(!download_file_.get()); | |
| 1467 } | 1506 } |
| 1468 | 1507 |
| 1469 // Reset all data saved, as even if we did save all the data we're going | 1508 // Reset all data saved, as even if we did save all the data we're going to go |
| 1470 // to go through another round of downloading when we resume. | 1509 // through another round of downloading when we resume. There's a potential |
| 1471 // There's a potential problem here in the abstract, as if we did download | 1510 // problem here in the abstract, as if we did download all the data and then |
| 1472 // all the data and then run into a continuable error, on resumption we | 1511 // run into a continuable error, on resumption we won't download any more |
| 1473 // won't download any more data. However, a) there are currently no | 1512 // data. However, a) there are currently no continuable errors that can occur |
| 1474 // continuable errors that can occur after we download all the data, and | 1513 // after we download all the data, and b) if there were, that would probably |
| 1475 // b) if there were, that would probably simply result in a null range | 1514 // simply result in a null range request, which would generate a |
| 1476 // request, which would generate a DestinationCompleted() notification | 1515 // DestinationCompleted() notification from the DownloadFile, which would |
| 1477 // from the DownloadFile, which would behave properly with setting | 1516 // behave properly with setting all_data_saved_ to false here. |
| 1478 // all_data_saved_ to false here. | |
| 1479 all_data_saved_ = false; | 1517 all_data_saved_ = false; |
| 1480 | 1518 |
| 1481 TransitionTo(INTERRUPTED_INTERNAL, DONT_UPDATE_OBSERVERS); | 1519 if (request_handle_) |
| 1482 RecordDownloadInterrupted(reason, received_bytes_, total_bytes_); | 1520 request_handle_->CancelRequest(); |
| 1483 if (!GetWebContents()) | |
| 1484 RecordDownloadCount(INTERRUPTED_WITHOUT_WEBCONTENTS); | |
| 1485 | 1521 |
| 1486 AutoResumeIfValid(); | 1522 if (reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED || |
| 1523 reason == DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN) { | |
| 1524 if (IsDangerous()) { | |
| 1525 RecordDangerousDownloadDiscard( | |
| 1526 reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED | |
| 1527 ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION | |
| 1528 : DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN, | |
| 1529 GetDangerType(), GetTargetFilePath()); | |
| 1530 } | |
| 1531 | |
| 1532 RecordDownloadCount(CANCELLED_COUNT); | |
| 1533 TransitionTo(CANCELLED_INTERNAL, DONT_UPDATE_OBSERVERS); | |
| 1534 } else { | |
| 1535 RecordDownloadInterrupted(reason, received_bytes_, total_bytes_); | |
| 1536 if (!GetWebContents()) | |
| 1537 RecordDownloadCount(INTERRUPTED_WITHOUT_WEBCONTENTS); | |
| 1538 TransitionTo(INTERRUPTED_INTERNAL, DONT_UPDATE_OBSERVERS); | |
| 1539 AutoResumeIfValid(); | |
| 1540 } | |
| 1541 | |
| 1487 UpdateObservers(); | 1542 UpdateObservers(); |
| 1488 } | 1543 } |
| 1489 | 1544 |
| 1490 void DownloadItemImpl::ReleaseDownloadFile(bool destroy_file) { | 1545 void DownloadItemImpl::ReleaseDownloadFile(bool destroy_file) { |
| 1491 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1546 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1547 DVLOG(20) << __FUNCTION__ << "() destroy_file:" << destroy_file; | |
| 1492 | 1548 |
| 1493 if (destroy_file) { | 1549 if (destroy_file) { |
| 1494 BrowserThread::PostTask( | 1550 BrowserThread::PostTask( |
| 1495 BrowserThread::FILE, FROM_HERE, | 1551 BrowserThread::FILE, FROM_HERE, |
| 1496 // Will be deleted at end of task execution. | 1552 // Will be deleted at end of task execution. |
| 1497 base::Bind(&DownloadFileCancel, base::Passed(&download_file_))); | 1553 base::Bind(&DownloadFileCancel, base::Passed(&download_file_))); |
| 1498 // Avoid attempting to reuse the intermediate file by clearing out | 1554 // Avoid attempting to reuse the intermediate file by clearing out |
| 1499 // current_path_. | 1555 // current_path_. |
| 1500 current_path_.clear(); | 1556 current_path_.clear(); |
| 1501 } else { | 1557 } else { |
| 1502 BrowserThread::PostTask( | 1558 BrowserThread::PostTask( |
| 1503 BrowserThread::FILE, | 1559 BrowserThread::FILE, |
| 1504 FROM_HERE, | 1560 FROM_HERE, |
| 1505 base::Bind(base::IgnoreResult(&DownloadFileDetach), | 1561 base::Bind(base::IgnoreResult(&DownloadFileDetach), |
| 1506 // Will be deleted at end of task execution. | 1562 // Will be deleted at end of task execution. |
| 1507 base::Passed(&download_file_))); | 1563 base::Passed(&download_file_))); |
| 1508 } | 1564 } |
| 1509 // Don't accept any more messages from the DownloadFile, and null | 1565 // Don't accept any more messages from the DownloadFile, and null |
| 1510 // out any previous "all data received". This also breaks links to | 1566 // out any previous "all data received". This also breaks links to |
| 1511 // other entities we've given out weak pointers to. | 1567 // other entities we've given out weak pointers to. |
| 1512 weak_ptr_factory_.InvalidateWeakPtrs(); | 1568 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 1513 } | 1569 } |
| 1514 | 1570 |
| 1515 bool DownloadItemImpl::IsDownloadReadyForCompletion( | 1571 bool DownloadItemImpl::IsDownloadReadyForCompletion( |
| 1516 const base::Closure& state_change_notification) { | 1572 const base::Closure& state_change_notification) { |
| 1573 // If the download hasn't progressed to the IN_PROGRESS state, then it's not | |
| 1574 // ready for completion. | |
| 1575 if (state_ != IN_PROGRESS_INTERNAL) | |
| 1576 return false; | |
| 1577 | |
| 1517 // If we don't have all the data, the download is not ready for | 1578 // If we don't have all the data, the download is not ready for |
| 1518 // completion. | 1579 // completion. |
| 1519 if (!AllDataSaved()) | 1580 if (!AllDataSaved()) |
| 1520 return false; | 1581 return false; |
| 1521 | 1582 |
| 1522 // If the download is dangerous, but not yet validated, it's not ready for | 1583 // If the download is dangerous, but not yet validated, it's not ready for |
| 1523 // completion. | 1584 // completion. |
| 1524 if (IsDangerous()) | 1585 if (IsDangerous()) |
| 1525 return false; | 1586 return false; |
| 1526 | 1587 |
| 1527 // If the download isn't active (e.g. has been cancelled) it's not | 1588 // Invariants for the IN_PROGRESS state. DCHECKs here verify that the |
| 1528 // ready for completion. | 1589 // invariants are still true. |
| 1529 if (state_ != IN_PROGRESS_INTERNAL) | 1590 DCHECK(!target_path_.empty()); |
| 1530 return false; | 1591 DCHECK(!current_path_.empty()); |
| 1531 | 1592 DCHECK(target_path_.DirName() == current_path_.DirName()); |
| 1532 // If the target filename hasn't been determined, then it's not ready for | |
| 1533 // completion. This is checked in ReadyForDownloadCompletionDone(). | |
| 1534 if (GetTargetFilePath().empty()) | |
| 1535 return false; | |
| 1536 | |
| 1537 // This is checked in NeedsRename(). Without this conditional, | |
| 1538 // browser_tests:DownloadTest.DownloadMimeType fails the DCHECK. | |
| 1539 if (target_path_.DirName() != current_path_.DirName()) | |
| 1540 return false; | |
| 1541 | 1593 |
| 1542 // Give the delegate a chance to hold up a stop sign. It'll call | 1594 // Give the delegate a chance to hold up a stop sign. It'll call |
| 1543 // use back through the passed callback if it does and that state changes. | 1595 // use back through the passed callback if it does and that state changes. |
| 1544 if (!delegate_->ShouldCompleteDownload(this, state_change_notification)) | 1596 if (!delegate_->ShouldCompleteDownload(this, state_change_notification)) |
| 1545 return false; | 1597 return false; |
| 1546 | 1598 |
| 1547 return true; | 1599 return true; |
| 1548 } | 1600 } |
| 1549 | 1601 |
| 1550 void DownloadItemImpl::TransitionTo(DownloadInternalState new_state, | 1602 void DownloadItemImpl::TransitionTo(DownloadInternalState new_state, |
| 1551 ShouldUpdateObservers notify_action) { | 1603 ShouldUpdateObservers notify_action) { |
| 1552 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1604 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1553 | 1605 |
| 1554 if (state_ == new_state) | 1606 if (state_ == new_state) |
| 1555 return; | 1607 return; |
| 1556 | 1608 |
| 1557 DownloadInternalState old_state = state_; | 1609 DownloadInternalState old_state = state_; |
| 1558 state_ = new_state; | 1610 state_ = new_state; |
| 1559 | 1611 |
| 1612 DCHECK(is_save_package_download_ | |
| 1613 ? IsValidSavePackageStateTransition(old_state, new_state) | |
| 1614 : IsValidStateTransition(old_state, new_state)) | |
| 1615 << "Invalid state transition from:" << DebugDownloadStateString(old_state) | |
| 1616 << " to:" << DebugDownloadStateString(new_state); | |
| 1617 | |
| 1560 switch (state_) { | 1618 switch (state_) { |
| 1619 case INITIAL_INTERNAL: | |
| 1620 NOTREACHED(); | |
| 1621 break; | |
| 1622 | |
| 1623 case TARGET_PENDING_INTERNAL: | |
| 1624 case TARGET_RESOLVED_INTERNAL: | |
| 1625 break; | |
| 1626 | |
| 1627 case IN_PROGRESS_INTERNAL: | |
| 1628 DCHECK(!current_path_.empty()) << "Current output path must be known."; | |
| 1629 DCHECK(!target_path_.empty()) << "Target path must be known."; | |
| 1630 DCHECK(current_path_.DirName() == target_path_.DirName()) | |
| 1631 << "Current output directory must match target directory."; | |
| 1632 DCHECK(download_file_) << "Output file must be owned by download item."; | |
| 1633 DCHECK(request_handle_) << "Download source must be active."; | |
| 1634 DCHECK(!is_paused_) << "At the time a download enters IN_PROGRESS state, " | |
| 1635 "it must not be paused."; | |
| 1636 break; | |
| 1637 | |
| 1561 case COMPLETING_INTERNAL: | 1638 case COMPLETING_INTERNAL: |
| 1639 DCHECK(all_data_saved_) << "All data must be saved prior to completion."; | |
| 1640 DCHECK(!download_file_) | |
| 1641 << "Download file must be released prior to completion."; | |
| 1642 DCHECK(!target_path_.empty()) << "Target path must be known."; | |
| 1643 DCHECK(current_path_ == target_path_) | |
| 1644 << "Current output path must match target path."; | |
| 1645 | |
| 1562 bound_net_log_.AddEvent( | 1646 bound_net_log_.AddEvent( |
| 1563 net::NetLog::TYPE_DOWNLOAD_ITEM_COMPLETING, | 1647 net::NetLog::TYPE_DOWNLOAD_ITEM_COMPLETING, |
| 1564 base::Bind(&ItemCompletingNetLogCallback, received_bytes_, &hash_)); | 1648 base::Bind(&ItemCompletingNetLogCallback, received_bytes_, &hash_)); |
| 1565 break; | 1649 break; |
| 1650 | |
| 1566 case COMPLETE_INTERNAL: | 1651 case COMPLETE_INTERNAL: |
| 1567 bound_net_log_.AddEvent( | 1652 bound_net_log_.AddEvent( |
| 1568 net::NetLog::TYPE_DOWNLOAD_ITEM_FINISHED, | 1653 net::NetLog::TYPE_DOWNLOAD_ITEM_FINISHED, |
| 1569 base::Bind(&ItemFinishedNetLogCallback, auto_opened_)); | 1654 base::Bind(&ItemFinishedNetLogCallback, auto_opened_)); |
| 1570 break; | 1655 break; |
| 1656 | |
| 1571 case INTERRUPTED_INTERNAL: | 1657 case INTERRUPTED_INTERNAL: |
| 1572 bound_net_log_.AddEvent( | 1658 bound_net_log_.AddEvent( |
| 1573 net::NetLog::TYPE_DOWNLOAD_ITEM_INTERRUPTED, | 1659 net::NetLog::TYPE_DOWNLOAD_ITEM_INTERRUPTED, |
| 1574 base::Bind(&ItemInterruptedNetLogCallback, last_reason_, | 1660 base::Bind(&ItemInterruptedNetLogCallback, last_reason_, |
| 1575 received_bytes_, &hash_state_)); | 1661 received_bytes_, &hash_state_)); |
| 1576 break; | 1662 break; |
| 1577 case IN_PROGRESS_INTERNAL: | 1663 |
| 1578 if (old_state == INTERRUPTED_INTERNAL) { | 1664 case RESUMING_INTERNAL: |
| 1579 bound_net_log_.AddEvent( | 1665 bound_net_log_.AddEvent( |
| 1580 net::NetLog::TYPE_DOWNLOAD_ITEM_RESUMED, | 1666 net::NetLog::TYPE_DOWNLOAD_ITEM_RESUMED, |
| 1581 base::Bind(&ItemResumingNetLogCallback, | 1667 base::Bind(&ItemResumingNetLogCallback, false, last_reason_, |
| 1582 false, last_reason_, received_bytes_, &hash_state_)); | 1668 received_bytes_, &hash_state_)); |
| 1583 } | |
| 1584 break; | 1669 break; |
| 1670 | |
| 1585 case CANCELLED_INTERNAL: | 1671 case CANCELLED_INTERNAL: |
| 1586 bound_net_log_.AddEvent( | 1672 bound_net_log_.AddEvent( |
| 1587 net::NetLog::TYPE_DOWNLOAD_ITEM_CANCELED, | 1673 net::NetLog::TYPE_DOWNLOAD_ITEM_CANCELED, |
| 1588 base::Bind(&ItemCanceledNetLogCallback, received_bytes_, | 1674 base::Bind(&ItemCanceledNetLogCallback, received_bytes_, |
| 1589 &hash_state_)); | 1675 &hash_state_)); |
| 1590 break; | 1676 break; |
| 1591 default: | 1677 |
| 1678 case MAX_DOWNLOAD_INTERNAL_STATE: | |
| 1679 NOTREACHED(); | |
| 1592 break; | 1680 break; |
| 1593 } | 1681 } |
| 1594 | 1682 |
| 1595 DVLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true) | 1683 DVLOG(20) << " " << __FUNCTION__ << "()" |
| 1596 << " " << InternalToExternalState(old_state) | 1684 << " from:" << DebugDownloadStateString(old_state) |
| 1597 << " " << InternalToExternalState(state_); | 1685 << " to:" << DebugDownloadStateString(state_) |
| 1598 | 1686 << " this = " << DebugString(true); |
| 1599 bool is_done = (state_ != IN_PROGRESS_INTERNAL && | 1687 bool is_done = |
| 1600 state_ != COMPLETING_INTERNAL); | 1688 (state_ != TARGET_PENDING_INTERNAL && |
| 1601 bool was_done = (old_state != IN_PROGRESS_INTERNAL && | 1689 state_ != TARGET_RESOLVED_INTERNAL && state_ != IN_PROGRESS_INTERNAL && |
| 1602 old_state != COMPLETING_INTERNAL); | 1690 state_ != COMPLETING_INTERNAL); |
| 1691 bool was_done = | |
| 1692 (old_state != TARGET_PENDING_INTERNAL && | |
| 1693 old_state != TARGET_RESOLVED_INTERNAL && | |
| 1694 old_state != IN_PROGRESS_INTERNAL && old_state != COMPLETING_INTERNAL); | |
| 1603 // Termination | 1695 // Termination |
| 1604 if (is_done && !was_done) | 1696 if (is_done && !was_done) |
| 1605 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE); | 1697 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE); |
| 1606 | 1698 |
| 1607 // Resumption | 1699 // Resumption |
| 1608 if (was_done && !is_done) { | 1700 if (was_done && !is_done) { |
| 1609 std::string file_name(target_path_.BaseName().AsUTF8Unsafe()); | 1701 std::string file_name(target_path_.BaseName().AsUTF8Unsafe()); |
| 1610 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, | 1702 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, |
| 1611 base::Bind(&ItemActivatedNetLogCallback, | 1703 base::Bind(&ItemActivatedNetLogCallback, |
| 1612 this, SRC_ACTIVE_DOWNLOAD, | 1704 this, SRC_ACTIVE_DOWNLOAD, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1697 | 1789 |
| 1698 download_params->set_file_path(GetFullPath()); | 1790 download_params->set_file_path(GetFullPath()); |
| 1699 download_params->set_offset(GetReceivedBytes()); | 1791 download_params->set_offset(GetReceivedBytes()); |
| 1700 download_params->set_hash_state(GetHashState()); | 1792 download_params->set_hash_state(GetHashState()); |
| 1701 download_params->set_last_modified(GetLastModifiedTime()); | 1793 download_params->set_last_modified(GetLastModifiedTime()); |
| 1702 download_params->set_etag(GetETag()); | 1794 download_params->set_etag(GetETag()); |
| 1703 download_params->set_callback( | 1795 download_params->set_callback( |
| 1704 base::Bind(&DownloadItemImpl::OnResumeRequestStarted, | 1796 base::Bind(&DownloadItemImpl::OnResumeRequestStarted, |
| 1705 weak_ptr_factory_.GetWeakPtr())); | 1797 weak_ptr_factory_.GetWeakPtr())); |
| 1706 | 1798 |
| 1799 TransitionTo(RESUMING_INTERNAL, DONT_UPDATE_OBSERVERS); | |
| 1707 delegate_->ResumeInterruptedDownload(std::move(download_params), GetId()); | 1800 delegate_->ResumeInterruptedDownload(std::move(download_params), GetId()); |
| 1708 // Just in case we were interrupted while paused. | 1801 // Just in case we were interrupted while paused. |
| 1709 is_paused_ = false; | 1802 is_paused_ = false; |
| 1710 | |
| 1711 TransitionTo(RESUMING_INTERNAL, DONT_UPDATE_OBSERVERS); | |
| 1712 } | 1803 } |
| 1713 | 1804 |
| 1714 // static | 1805 // static |
| 1715 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState( | 1806 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState( |
| 1716 DownloadInternalState internal_state) { | 1807 DownloadInternalState internal_state) { |
| 1717 switch (internal_state) { | 1808 switch (internal_state) { |
| 1809 case INITIAL_INTERNAL: | |
| 1810 case TARGET_PENDING_INTERNAL: | |
| 1811 case TARGET_RESOLVED_INTERNAL: | |
| 1812 // TODO(asanka): Introduce an externally visible state to distinguish | |
| 1813 // between the above states and IN_PROGRESS_INTERNAL. The latter (the | |
| 1814 // state where the download is active and has a known target) is the state | |
| 1815 // that most external users are interested in. | |
| 1718 case IN_PROGRESS_INTERNAL: | 1816 case IN_PROGRESS_INTERNAL: |
| 1719 return IN_PROGRESS; | 1817 return IN_PROGRESS; |
| 1720 case COMPLETING_INTERNAL: | 1818 case COMPLETING_INTERNAL: |
| 1721 return IN_PROGRESS; | 1819 return IN_PROGRESS; |
| 1722 case COMPLETE_INTERNAL: | 1820 case COMPLETE_INTERNAL: |
| 1723 return COMPLETE; | 1821 return COMPLETE; |
| 1724 case CANCELLED_INTERNAL: | 1822 case CANCELLED_INTERNAL: |
| 1725 return CANCELLED; | 1823 return CANCELLED; |
| 1726 case INTERRUPTED_INTERNAL: | 1824 case INTERRUPTED_INTERNAL: |
| 1727 return INTERRUPTED; | 1825 return INTERRUPTED; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 1746 case CANCELLED: | 1844 case CANCELLED: |
| 1747 return CANCELLED_INTERNAL; | 1845 return CANCELLED_INTERNAL; |
| 1748 case INTERRUPTED: | 1846 case INTERRUPTED: |
| 1749 return INTERRUPTED_INTERNAL; | 1847 return INTERRUPTED_INTERNAL; |
| 1750 default: | 1848 default: |
| 1751 NOTREACHED(); | 1849 NOTREACHED(); |
| 1752 } | 1850 } |
| 1753 return MAX_DOWNLOAD_INTERNAL_STATE; | 1851 return MAX_DOWNLOAD_INTERNAL_STATE; |
| 1754 } | 1852 } |
| 1755 | 1853 |
| 1854 #if DCHECK_IS_ON() | |
| 1855 // static | |
| 1856 bool DownloadItemImpl::IsValidSavePackageStateTransition( | |
| 1857 DownloadInternalState from, | |
| 1858 DownloadInternalState to) { | |
| 1859 switch (from) { | |
| 1860 case INITIAL_INTERNAL: | |
| 1861 case TARGET_PENDING_INTERNAL: | |
| 1862 case TARGET_RESOLVED_INTERNAL: | |
| 1863 case COMPLETING_INTERNAL: | |
| 1864 case COMPLETE_INTERNAL: | |
| 1865 case INTERRUPTED_INTERNAL: | |
| 1866 case RESUMING_INTERNAL: | |
| 1867 case CANCELLED_INTERNAL: | |
| 1868 return false; | |
| 1869 | |
| 1870 case IN_PROGRESS_INTERNAL: | |
| 1871 return to == CANCELLED_INTERNAL || COMPLETE_INTERNAL; | |
| 1872 | |
| 1873 case MAX_DOWNLOAD_INTERNAL_STATE: | |
| 1874 NOTREACHED(); | |
| 1875 } | |
| 1876 return false; | |
| 1877 } | |
| 1878 | |
| 1879 // static | |
| 1880 bool DownloadItemImpl::IsValidStateTransition(DownloadInternalState from, | |
| 1881 DownloadInternalState to) { | |
| 1882 switch (from) { | |
| 1883 case INITIAL_INTERNAL: | |
| 1884 return to == TARGET_PENDING_INTERNAL || to == INTERRUPTED_INTERNAL; | |
| 1885 | |
| 1886 case TARGET_PENDING_INTERNAL: | |
| 1887 return to == TARGET_RESOLVED_INTERNAL || to == CANCELLED_INTERNAL; | |
| 1888 | |
| 1889 case TARGET_RESOLVED_INTERNAL: | |
| 1890 return to == IN_PROGRESS_INTERNAL || to == INTERRUPTED_INTERNAL || | |
| 1891 to == CANCELLED_INTERNAL; | |
| 1892 | |
| 1893 case IN_PROGRESS_INTERNAL: | |
| 1894 return to == COMPLETING_INTERNAL || to == CANCELLED_INTERNAL || | |
| 1895 to == INTERRUPTED_INTERNAL; | |
| 1896 | |
| 1897 case COMPLETING_INTERNAL: | |
| 1898 return to == COMPLETE_INTERNAL; | |
| 1899 | |
| 1900 case COMPLETE_INTERNAL: | |
| 1901 return false; | |
| 1902 | |
| 1903 case INTERRUPTED_INTERNAL: | |
| 1904 return to == RESUMING_INTERNAL || to == CANCELLED_INTERNAL; | |
| 1905 | |
| 1906 case RESUMING_INTERNAL: | |
| 1907 return to == TARGET_PENDING_INTERNAL || to == CANCELLED_INTERNAL; | |
| 1908 | |
| 1909 case CANCELLED_INTERNAL: | |
| 1910 return false; | |
| 1911 | |
| 1912 case MAX_DOWNLOAD_INTERNAL_STATE: | |
| 1913 NOTREACHED(); | |
| 1914 } | |
| 1915 return false; | |
| 1916 } | |
| 1917 #endif // DCHECK_IS_ON() | |
| 1918 | |
| 1756 const char* DownloadItemImpl::DebugDownloadStateString( | 1919 const char* DownloadItemImpl::DebugDownloadStateString( |
| 1757 DownloadInternalState state) { | 1920 DownloadInternalState state) { |
| 1758 switch (state) { | 1921 switch (state) { |
| 1922 case INITIAL_INTERNAL: | |
| 1923 return "INITIAL"; | |
| 1924 case TARGET_PENDING_INTERNAL: | |
| 1925 return "TARGET_PENDING"; | |
| 1926 case TARGET_RESOLVED_INTERNAL: | |
| 1927 return "TARGET_RESOLVED"; | |
| 1759 case IN_PROGRESS_INTERNAL: | 1928 case IN_PROGRESS_INTERNAL: |
| 1760 return "IN_PROGRESS"; | 1929 return "IN_PROGRESS"; |
| 1761 case COMPLETING_INTERNAL: | 1930 case COMPLETING_INTERNAL: |
| 1762 return "COMPLETING"; | 1931 return "COMPLETING"; |
| 1763 case COMPLETE_INTERNAL: | 1932 case COMPLETE_INTERNAL: |
| 1764 return "COMPLETE"; | 1933 return "COMPLETE"; |
| 1765 case CANCELLED_INTERNAL: | 1934 case CANCELLED_INTERNAL: |
| 1766 return "CANCELLED"; | 1935 return "CANCELLED"; |
| 1767 case INTERRUPTED_INTERNAL: | 1936 case INTERRUPTED_INTERNAL: |
| 1768 return "INTERRUPTED"; | 1937 return "INTERRUPTED"; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 1786 case RESUME_MODE_USER_CONTINUE: | 1955 case RESUME_MODE_USER_CONTINUE: |
| 1787 return "USER_CONTINUE"; | 1956 return "USER_CONTINUE"; |
| 1788 case RESUME_MODE_USER_RESTART: | 1957 case RESUME_MODE_USER_RESTART: |
| 1789 return "USER_RESTART"; | 1958 return "USER_RESTART"; |
| 1790 } | 1959 } |
| 1791 NOTREACHED() << "Unknown resume mode " << mode; | 1960 NOTREACHED() << "Unknown resume mode " << mode; |
| 1792 return "unknown"; | 1961 return "unknown"; |
| 1793 } | 1962 } |
| 1794 | 1963 |
| 1795 } // namespace content | 1964 } // namespace content |
| OLD | NEW |