| 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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 has_user_gesture_(info.has_user_gesture), | 185 has_user_gesture_(info.has_user_gesture), |
| 186 content_disposition_(info.content_disposition), | 186 content_disposition_(info.content_disposition), |
| 187 mime_type_(info.mime_type), | 187 mime_type_(info.mime_type), |
| 188 original_mime_type_(info.original_mime_type), | 188 original_mime_type_(info.original_mime_type), |
| 189 remote_address_(info.remote_address), | 189 remote_address_(info.remote_address), |
| 190 total_bytes_(info.total_bytes), | 190 total_bytes_(info.total_bytes), |
| 191 received_bytes_(0), | 191 received_bytes_(0), |
| 192 bytes_per_sec_(0), | 192 bytes_per_sec_(0), |
| 193 last_modified_time_(info.last_modified), | 193 last_modified_time_(info.last_modified), |
| 194 etag_(info.etag), | 194 etag_(info.etag), |
| 195 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 195 last_reason_(info.result), |
| 196 start_tick_(base::TimeTicks::Now()), | 196 start_tick_(base::TimeTicks::Now()), |
| 197 state_(INITIAL_INTERNAL), | 197 state_(INITIAL_INTERNAL), |
| 198 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), | 198 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), |
| 199 start_time_(info.start_time), | 199 start_time_(info.start_time), |
| 200 delegate_(delegate), | 200 delegate_(delegate), |
| 201 is_paused_(false), | 201 is_paused_(false), |
| 202 auto_resume_count_(0), | 202 auto_resume_count_(0), |
| 203 open_when_complete_(false), | 203 open_when_complete_(false), |
| 204 file_externally_removed_(false), | 204 file_externally_removed_(false), |
| 205 auto_opened_(false), | 205 auto_opened_(false), |
| 206 is_temporary_(!info.save_info->file_path.empty()), | 206 is_temporary_(!info.save_info->file_path.empty()), |
| 207 all_data_saved_(false), | 207 all_data_saved_(false), |
| 208 destination_error_(content::DOWNLOAD_INTERRUPT_REASON_NONE), | 208 destination_error_(DOWNLOAD_INTERRUPT_REASON_NONE), |
| 209 opened_(false), | 209 opened_(false), |
| 210 delegate_delayed_complete_(false), | 210 delegate_delayed_complete_(false), |
| 211 bound_net_log_(bound_net_log), | 211 bound_net_log_(bound_net_log), |
| 212 weak_ptr_factory_(this) { | 212 weak_ptr_factory_(this) { |
| 213 delegate_->Attach(); | 213 delegate_->Attach(); |
| 214 Init(true /* actively downloading */, SRC_ACTIVE_DOWNLOAD); | 214 Init(true /* actively downloading */, SRC_ACTIVE_DOWNLOAD); |
| 215 | 215 |
| 216 // Link the event sources. | 216 // Link the event sources. |
| 217 bound_net_log_.AddEvent( | 217 bound_net_log_.AddEvent( |
| 218 net::NetLog::TYPE_DOWNLOAD_URL_REQUEST, | 218 net::NetLog::TYPE_DOWNLOAD_URL_REQUEST, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 } | 288 } |
| 289 | 289 |
| 290 void DownloadItemImpl::RemoveObserver(Observer* observer) { | 290 void DownloadItemImpl::RemoveObserver(Observer* observer) { |
| 291 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 291 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 292 | 292 |
| 293 observers_.RemoveObserver(observer); | 293 observers_.RemoveObserver(observer); |
| 294 } | 294 } |
| 295 | 295 |
| 296 void DownloadItemImpl::UpdateObservers() { | 296 void DownloadItemImpl::UpdateObservers() { |
| 297 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 297 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 298 DVLOG(20) << __FUNCTION__ << "()"; |
| 298 | 299 |
| 299 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); | 300 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); |
| 300 } | 301 } |
| 301 | 302 |
| 302 void DownloadItemImpl::ValidateDangerousDownload() { | 303 void DownloadItemImpl::ValidateDangerousDownload() { |
| 303 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 304 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 304 DCHECK(!IsDone()); | 305 DCHECK(!IsDone()); |
| 305 DCHECK(IsDangerous()); | 306 DCHECK(IsDangerous()); |
| 306 | 307 |
| 307 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 308 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
| 308 | 309 |
| 309 if (IsDone() || !IsDangerous()) | 310 if (IsDone() || !IsDangerous()) |
| 310 return; | 311 return; |
| 311 | 312 |
| 312 RecordDangerousDownloadAccept(GetDangerType(), | 313 RecordDangerousDownloadAccept(GetDangerType(), |
| 313 GetTargetFilePath()); | 314 GetTargetFilePath()); |
| 314 | 315 |
| 315 danger_type_ = DOWNLOAD_DANGER_TYPE_USER_VALIDATED; | 316 danger_type_ = DOWNLOAD_DANGER_TYPE_USER_VALIDATED; |
| 316 | 317 |
| 317 bound_net_log_.AddEvent( | 318 bound_net_log_.AddEvent( |
| 318 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED, | 319 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED, |
| 319 base::Bind(&ItemCheckedNetLogCallback, GetDangerType())); | 320 base::Bind(&ItemCheckedNetLogCallback, GetDangerType())); |
| 320 | 321 |
| 321 UpdateObservers(); | 322 UpdateObservers(); // TODO(asanka): This is potentially unsafe. The download |
| 323 // may not be in a consistent state or around at all after |
| 324 // invoking observers. http://crbug.com/586610 |
| 322 | 325 |
| 323 MaybeCompleteDownload(); | 326 MaybeCompleteDownload(); |
| 324 } | 327 } |
| 325 | 328 |
| 326 void DownloadItemImpl::StealDangerousDownload( | 329 void DownloadItemImpl::StealDangerousDownload( |
| 327 const AcquireFileCallback& callback) { | 330 const AcquireFileCallback& callback) { |
| 328 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 331 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 329 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 332 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 330 DCHECK(IsDangerous()); | 333 DCHECK(IsDangerous()); |
| 331 | 334 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 344 } | 347 } |
| 345 | 348 |
| 346 void DownloadItemImpl::Pause() { | 349 void DownloadItemImpl::Pause() { |
| 347 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 350 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 348 | 351 |
| 349 // Ignore irrelevant states. | 352 // Ignore irrelevant states. |
| 350 if (is_paused_) | 353 if (is_paused_) |
| 351 return; | 354 return; |
| 352 | 355 |
| 353 switch (state_) { | 356 switch (state_) { |
| 357 case CANCELLED_INTERNAL: |
| 358 case COMPLETE_INTERNAL: |
| 359 case COMPLETING_INTERNAL: |
| 354 case INITIAL_INTERNAL: | 360 case INITIAL_INTERNAL: |
| 355 case COMPLETING_INTERNAL: | |
| 356 case COMPLETE_INTERNAL: | |
| 357 case INTERRUPTED_INTERNAL: | 361 case INTERRUPTED_INTERNAL: |
| 358 case CANCELLED_INTERNAL: | 362 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 359 case RESUMING_INTERNAL: | 363 case RESUMING_INTERNAL: |
| 360 // No active request. | 364 // No active request. |
| 365 // TODO(asanka): In the case of RESUMING_INTERNAL, consider setting |
| 366 // is_paused_ even if there's no request currently associated with this |
| 367 // DII. When a request is assigned (due to a resumption, for example) we |
| 368 // can honor the is_paused_ setting. |
| 361 return; | 369 return; |
| 362 | 370 |
| 371 case IN_PROGRESS_INTERNAL: |
| 363 case TARGET_PENDING_INTERNAL: | 372 case TARGET_PENDING_INTERNAL: |
| 364 case IN_PROGRESS_INTERNAL: | |
| 365 request_handle_->PauseRequest(); | 373 request_handle_->PauseRequest(); |
| 366 is_paused_ = true; | 374 is_paused_ = true; |
| 367 UpdateObservers(); | 375 UpdateObservers(); |
| 368 return; | 376 return; |
| 369 | 377 |
| 370 case MAX_DOWNLOAD_INTERNAL_STATE: | 378 case MAX_DOWNLOAD_INTERNAL_STATE: |
| 371 case TARGET_RESOLVED_INTERNAL: | 379 case TARGET_RESOLVED_INTERNAL: |
| 372 NOTREACHED(); | 380 NOTREACHED(); |
| 373 } | 381 } |
| 374 } | 382 } |
| 375 | 383 |
| 376 void DownloadItemImpl::Resume() { | 384 void DownloadItemImpl::Resume() { |
| 377 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 385 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 386 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 378 switch (state_) { | 387 switch (state_) { |
| 388 case CANCELLED_INTERNAL: // Nothing to resume. |
| 389 case COMPLETE_INTERNAL: |
| 390 case COMPLETING_INTERNAL: |
| 379 case INITIAL_INTERNAL: | 391 case INITIAL_INTERNAL: |
| 380 case COMPLETING_INTERNAL: | 392 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 381 case COMPLETE_INTERNAL: | 393 case RESUMING_INTERNAL: // Resumption in progress. |
| 382 case CANCELLED_INTERNAL: | |
| 383 // Nothing to resume. | |
| 384 case RESUMING_INTERNAL: | |
| 385 // Resumption in progress. | |
| 386 DCHECK(!is_paused_); | |
| 387 return; | 394 return; |
| 388 | 395 |
| 389 case TARGET_PENDING_INTERNAL: | 396 case TARGET_PENDING_INTERNAL: |
| 390 case IN_PROGRESS_INTERNAL: | 397 case IN_PROGRESS_INTERNAL: |
| 391 if (!is_paused_) | 398 if (!is_paused_) |
| 392 return; | 399 return; |
| 393 request_handle_->ResumeRequest(); | 400 request_handle_->ResumeRequest(); |
| 394 is_paused_ = false; | 401 is_paused_ = false; |
| 395 UpdateObservers(); | 402 UpdateObservers(); |
| 396 return; | 403 return; |
| 397 | 404 |
| 398 case INTERRUPTED_INTERNAL: | 405 case INTERRUPTED_INTERNAL: |
| 399 auto_resume_count_ = 0; // User input resets the counter. | 406 auto_resume_count_ = 0; // User input resets the counter. |
| 400 ResumeInterruptedDownload(); | 407 ResumeInterruptedDownload(); |
| 408 UpdateObservers(); |
| 401 return; | 409 return; |
| 402 | 410 |
| 403 case MAX_DOWNLOAD_INTERNAL_STATE: | 411 case MAX_DOWNLOAD_INTERNAL_STATE: |
| 404 case TARGET_RESOLVED_INTERNAL: | 412 case TARGET_RESOLVED_INTERNAL: |
| 405 NOTREACHED(); | 413 NOTREACHED(); |
| 406 } | 414 } |
| 407 } | 415 } |
| 408 | 416 |
| 409 void DownloadItemImpl::Cancel(bool user_cancel) { | 417 void DownloadItemImpl::Cancel(bool user_cancel) { |
| 410 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 418 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 411 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 419 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 412 Interrupt(user_cancel ? DOWNLOAD_INTERRUPT_REASON_USER_CANCELED | 420 Interrupt(user_cancel ? DOWNLOAD_INTERRUPT_REASON_USER_CANCELED |
| 413 : DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN); | 421 : DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN); |
| 422 UpdateObservers(); |
| 414 } | 423 } |
| 415 | 424 |
| 416 void DownloadItemImpl::Remove() { | 425 void DownloadItemImpl::Remove() { |
| 417 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 426 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 418 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 427 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 419 | 428 |
| 420 delegate_->AssertStateConsistent(this); | 429 delegate_->AssertStateConsistent(this); |
| 421 Interrupt(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); | 430 Interrupt(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); |
| 431 UpdateObservers(); |
| 422 delegate_->AssertStateConsistent(this); | 432 delegate_->AssertStateConsistent(this); |
| 423 | 433 |
| 424 NotifyRemoved(); | 434 NotifyRemoved(); |
| 425 delegate_->DownloadRemoved(this); | 435 delegate_->DownloadRemoved(this); |
| 426 // We have now been deleted. | 436 // We have now been deleted. |
| 427 } | 437 } |
| 428 | 438 |
| 429 void DownloadItemImpl::OpenDownload() { | 439 void DownloadItemImpl::OpenDownload() { |
| 430 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 440 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 431 | 441 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 } | 488 } |
| 479 | 489 |
| 480 bool DownloadItemImpl::CanResume() const { | 490 bool DownloadItemImpl::CanResume() const { |
| 481 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 491 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 482 switch (state_) { | 492 switch (state_) { |
| 483 case INITIAL_INTERNAL: | 493 case INITIAL_INTERNAL: |
| 484 case COMPLETING_INTERNAL: | 494 case COMPLETING_INTERNAL: |
| 485 case COMPLETE_INTERNAL: | 495 case COMPLETE_INTERNAL: |
| 486 case CANCELLED_INTERNAL: | 496 case CANCELLED_INTERNAL: |
| 487 case RESUMING_INTERNAL: | 497 case RESUMING_INTERNAL: |
| 498 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 488 return false; | 499 return false; |
| 489 | 500 |
| 490 case TARGET_PENDING_INTERNAL: | 501 case TARGET_PENDING_INTERNAL: |
| 491 case TARGET_RESOLVED_INTERNAL: | 502 case TARGET_RESOLVED_INTERNAL: |
| 492 case IN_PROGRESS_INTERNAL: | 503 case IN_PROGRESS_INTERNAL: |
| 493 return is_paused_; | 504 return is_paused_; |
| 494 | 505 |
| 495 case INTERRUPTED_INTERNAL: { | 506 case INTERRUPTED_INTERNAL: { |
| 496 ResumeMode resume_mode = GetResumeMode(); | 507 ResumeMode resume_mode = GetResumeMode(); |
| 497 // Only allow Resume() calls if the resumption mode requires a user | 508 // Only allow Resume() calls if the resumption mode requires a user |
| 498 // action. | 509 // action. |
| 499 return IsDownloadResumptionEnabled() && | 510 return IsDownloadResumptionEnabled() && |
| 500 (resume_mode == RESUME_MODE_USER_RESTART || | 511 (resume_mode == RESUME_MODE_USER_RESTART || |
| 501 resume_mode == RESUME_MODE_USER_CONTINUE); | 512 resume_mode == RESUME_MODE_USER_CONTINUE); |
| 502 } | 513 } |
| 503 | 514 |
| 504 case MAX_DOWNLOAD_INTERNAL_STATE: | 515 case MAX_DOWNLOAD_INTERNAL_STATE: |
| 505 NOTREACHED(); | 516 NOTREACHED(); |
| 506 } | 517 } |
| 507 return false; | 518 return false; |
| 508 } | 519 } |
| 509 | 520 |
| 510 bool DownloadItemImpl::IsDone() const { | 521 bool DownloadItemImpl::IsDone() const { |
| 511 switch (state_) { | 522 switch (state_) { |
| 512 case INITIAL_INTERNAL: | 523 case INITIAL_INTERNAL: |
| 513 case COMPLETING_INTERNAL: | 524 case COMPLETING_INTERNAL: |
| 514 case RESUMING_INTERNAL: | 525 case RESUMING_INTERNAL: |
| 515 case TARGET_PENDING_INTERNAL: | 526 case TARGET_PENDING_INTERNAL: |
| 527 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 516 case TARGET_RESOLVED_INTERNAL: | 528 case TARGET_RESOLVED_INTERNAL: |
| 517 case IN_PROGRESS_INTERNAL: | 529 case IN_PROGRESS_INTERNAL: |
| 518 return false; | 530 return false; |
| 519 | 531 |
| 520 case COMPLETE_INTERNAL: | 532 case COMPLETE_INTERNAL: |
| 521 case CANCELLED_INTERNAL: | 533 case CANCELLED_INTERNAL: |
| 522 return true; | 534 return true; |
| 523 | 535 |
| 524 case INTERRUPTED_INTERNAL: | 536 case INTERRUPTED_INTERNAL: |
| 525 return !CanResume(); | 537 return !CanResume(); |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 case DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT: | 908 case DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT: |
| 897 if (force_user) | 909 if (force_user) |
| 898 mode = RESUME_MODE_USER_RESTART; | 910 mode = RESUME_MODE_USER_RESTART; |
| 899 else | 911 else |
| 900 mode = RESUME_MODE_IMMEDIATE_RESTART; | 912 mode = RESUME_MODE_IMMEDIATE_RESTART; |
| 901 break; | 913 break; |
| 902 | 914 |
| 903 case DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED: | 915 case DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED: |
| 904 case DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED: | 916 case DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED: |
| 905 case DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN: | 917 case DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN: |
| 906 case DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST: | |
| 907 case DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED: | 918 case DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED: |
| 908 case DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN: | 919 case DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN: |
| 909 case DOWNLOAD_INTERRUPT_REASON_CRASH: | 920 case DOWNLOAD_INTERRUPT_REASON_CRASH: |
| 910 if (force_restart) | 921 if (force_restart) |
| 911 mode = RESUME_MODE_USER_RESTART; | 922 mode = RESUME_MODE_USER_RESTART; |
| 912 else | 923 else |
| 913 mode = RESUME_MODE_USER_CONTINUE; | 924 mode = RESUME_MODE_USER_CONTINUE; |
| 914 break; | 925 break; |
| 915 | 926 |
| 916 case DOWNLOAD_INTERRUPT_REASON_FILE_FAILED: | 927 case DOWNLOAD_INTERRUPT_REASON_FILE_FAILED: |
| 917 case DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: | 928 case DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: |
| 918 case DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: | 929 case DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: |
| 919 case DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG: | 930 case DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG: |
| 920 case DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE: | 931 case DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE: |
| 921 mode = RESUME_MODE_USER_RESTART; | 932 mode = RESUME_MODE_USER_RESTART; |
| 922 break; | 933 break; |
| 923 | 934 |
| 924 case DOWNLOAD_INTERRUPT_REASON_NONE: | 935 case DOWNLOAD_INTERRUPT_REASON_NONE: |
| 936 case DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST: |
| 925 case DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED: | 937 case DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED: |
| 926 case DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT: | 938 case DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT: |
| 927 case DOWNLOAD_INTERRUPT_REASON_USER_CANCELED: | 939 case DOWNLOAD_INTERRUPT_REASON_USER_CANCELED: |
| 928 case DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED: | 940 case DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED: |
| 929 case DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED: | 941 case DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED: |
| 930 case DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED: | 942 case DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED: |
| 931 case DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM: | 943 case DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM: |
| 932 case DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN: | 944 case DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN: |
| 933 mode = RESUME_MODE_INVALID; | 945 mode = RESUME_MODE_INVALID; |
| 934 break; | 946 break; |
| 935 } | 947 } |
| 936 | 948 |
| 937 return mode; | 949 return mode; |
| 938 } | 950 } |
| 939 | 951 |
| 940 void DownloadItemImpl::MergeOriginInfoOnResume( | 952 void DownloadItemImpl::UpdateValidatorsOnResumption( |
| 941 const DownloadCreateInfo& new_create_info) { | 953 const DownloadCreateInfo& new_create_info) { |
| 942 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 954 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 943 DCHECK_EQ(RESUMING_INTERNAL, state_); | 955 DCHECK_EQ(RESUMING_INTERNAL, state_); |
| 944 DCHECK(!new_create_info.url_chain.empty()); | 956 DCHECK(!new_create_info.url_chain.empty()); |
| 945 | 957 |
| 946 // We are going to tack on any new redirects to our list of redirects. | 958 // We are going to tack on any new redirects to our list of redirects. |
| 947 // When a download is resumed, the URL used for the resumption request is the | 959 // When a download is resumed, the URL used for the resumption request is the |
| 948 // one at the end of the previous redirect chain. Tacking additional redirects | 960 // one at the end of the previous redirect chain. Tacking additional redirects |
| 949 // to the end of this chain ensures that: | 961 // to the end of this chain ensures that: |
| 950 // - If the download needs to be resumed again, the ETag/Last-Modified headers | 962 // - If the download needs to be resumed again, the ETag/Last-Modified headers |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 hash_state_ = ""; | 1027 hash_state_ = ""; |
| 1016 | 1028 |
| 1017 UpdateObservers(); | 1029 UpdateObservers(); |
| 1018 } | 1030 } |
| 1019 | 1031 |
| 1020 void DownloadItemImpl::MarkAsComplete() { | 1032 void DownloadItemImpl::MarkAsComplete() { |
| 1021 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1033 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1022 | 1034 |
| 1023 DCHECK(all_data_saved_); | 1035 DCHECK(all_data_saved_); |
| 1024 end_time_ = base::Time::Now(); | 1036 end_time_ = base::Time::Now(); |
| 1025 TransitionTo(COMPLETE_INTERNAL, UPDATE_OBSERVERS); | 1037 TransitionTo(COMPLETE_INTERNAL); |
| 1038 UpdateObservers(); |
| 1026 } | 1039 } |
| 1027 | 1040 |
| 1028 void DownloadItemImpl::DestinationUpdate(int64_t bytes_so_far, | 1041 void DownloadItemImpl::DestinationUpdate(int64_t bytes_so_far, |
| 1029 int64_t bytes_per_sec, | 1042 int64_t bytes_per_sec, |
| 1030 const std::string& hash_state) { | 1043 const std::string& hash_state) { |
| 1031 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1044 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1032 // If the download is in any other state we don't expect any | 1045 // If the download is in any other state we don't expect any |
| 1033 // DownloadDestinationObserver callbacks. An interruption or a cancellation | 1046 // DownloadDestinationObserver callbacks. An interruption or a cancellation |
| 1034 // results in a call to ReleaseDownloadFile which invalidates the weak | 1047 // results in a call to ReleaseDownloadFile which invalidates the weak |
| 1035 // reference held by the DownloadFile and hence cuts off any pending | 1048 // reference held by the DownloadFile and hence cuts off any pending |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1064 // results in a call to ReleaseDownloadFile which invalidates the weak | 1077 // results in a call to ReleaseDownloadFile which invalidates the weak |
| 1065 // reference held by the DownloadFile and hence cuts off any pending | 1078 // reference held by the DownloadFile and hence cuts off any pending |
| 1066 // callbacks. | 1079 // callbacks. |
| 1067 DCHECK(state_ == TARGET_PENDING_INTERNAL || state_ == IN_PROGRESS_INTERNAL); | 1080 DCHECK(state_ == TARGET_PENDING_INTERNAL || state_ == IN_PROGRESS_INTERNAL); |
| 1068 DVLOG(20) << __FUNCTION__ | 1081 DVLOG(20) << __FUNCTION__ |
| 1069 << "() reason:" << DownloadInterruptReasonToString(reason); | 1082 << "() reason:" << DownloadInterruptReasonToString(reason); |
| 1070 | 1083 |
| 1071 // Postpone recognition of this error until after file name determination | 1084 // Postpone recognition of this error until after file name determination |
| 1072 // has completed and the intermediate file has been renamed to simplify | 1085 // has completed and the intermediate file has been renamed to simplify |
| 1073 // resumption conditions. | 1086 // resumption conditions. |
| 1074 if (state_ != IN_PROGRESS_INTERNAL) | 1087 if (state_ == TARGET_PENDING_INTERNAL) { |
| 1075 destination_error_ = reason; | 1088 destination_error_ = reason; |
| 1076 else | 1089 return; |
| 1077 Interrupt(reason); | 1090 } |
| 1091 Interrupt(reason); |
| 1092 UpdateObservers(); |
| 1078 } | 1093 } |
| 1079 | 1094 |
| 1080 void DownloadItemImpl::DestinationCompleted(const std::string& final_hash) { | 1095 void DownloadItemImpl::DestinationCompleted(const std::string& final_hash) { |
| 1081 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1096 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1082 // If the download is in any other state we don't expect any | 1097 // If the download is in any other state we don't expect any |
| 1083 // DownloadDestinationObserver callbacks. An interruption or a cancellation | 1098 // DownloadDestinationObserver callbacks. An interruption or a cancellation |
| 1084 // results in a call to ReleaseDownloadFile which invalidates the weak | 1099 // results in a call to ReleaseDownloadFile which invalidates the weak |
| 1085 // reference held by the DownloadFile and hence cuts off any pending | 1100 // reference held by the DownloadFile and hence cuts off any pending |
| 1086 // callbacks. | 1101 // callbacks. |
| 1087 DCHECK(state_ == TARGET_PENDING_INTERNAL || state_ == IN_PROGRESS_INTERNAL); | 1102 DCHECK(state_ == TARGET_PENDING_INTERNAL || state_ == IN_PROGRESS_INTERNAL); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1124 bound_net_log_.AddEvent( | 1139 bound_net_log_.AddEvent( |
| 1125 net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data); | 1140 net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data); |
| 1126 } | 1141 } |
| 1127 | 1142 |
| 1128 DVLOG(20) << __FUNCTION__ << "() " << DebugString(true); | 1143 DVLOG(20) << __FUNCTION__ << "() " << DebugString(true); |
| 1129 } | 1144 } |
| 1130 | 1145 |
| 1131 // We're starting the download. | 1146 // We're starting the download. |
| 1132 void DownloadItemImpl::Start( | 1147 void DownloadItemImpl::Start( |
| 1133 scoped_ptr<DownloadFile> file, | 1148 scoped_ptr<DownloadFile> file, |
| 1134 scoped_ptr<DownloadRequestHandleInterface> req_handle) { | 1149 scoped_ptr<DownloadRequestHandleInterface> req_handle, |
| 1150 const DownloadCreateInfo& new_create_info) { |
| 1135 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1151 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1136 DCHECK(!download_file_.get()); | 1152 DCHECK(!download_file_.get()); |
| 1137 DCHECK(file.get()); | |
| 1138 DCHECK(req_handle.get()); | |
| 1139 DVLOG(20) << __FUNCTION__ << "() this=" << DebugString(true); | 1153 DVLOG(20) << __FUNCTION__ << "() this=" << DebugString(true); |
| 1140 | 1154 |
| 1141 download_file_ = std::move(file); | 1155 download_file_ = std::move(file); |
| 1142 request_handle_ = std::move(req_handle); | 1156 request_handle_ = std::move(req_handle); |
| 1157 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 1143 | 1158 |
| 1144 if (state_ == CANCELLED_INTERNAL) { | 1159 if (state_ == CANCELLED_INTERNAL) { |
| 1145 // The download was in the process of resuming when it was cancelled. Don't | 1160 // The download was in the process of resuming when it was cancelled. Don't |
| 1146 // proceed. | 1161 // proceed. |
| 1147 ReleaseDownloadFile(true); | 1162 ReleaseDownloadFile(true); |
| 1148 request_handle_->CancelRequest(); | 1163 if (request_handle_) |
| 1164 request_handle_->CancelRequest(); |
| 1149 return; | 1165 return; |
| 1150 } | 1166 } |
| 1151 | 1167 |
| 1152 TransitionTo(TARGET_PENDING_INTERNAL, UPDATE_OBSERVERS); | 1168 // The state could be one of the following: |
| 1169 // |
| 1170 // INITIAL_INTERNAL: A normal download attempt. |
| 1171 // |
| 1172 // RESUMING_INTERNAL: A resumption attempt. May or may not have been |
| 1173 // successful. |
| 1174 DCHECK(state_ == INITIAL_INTERNAL || state_ == RESUMING_INTERNAL); |
| 1175 |
| 1176 // If the state_ is INITIAL_INTERNAL, then the target path must be empty. |
| 1177 DCHECK(state_ != INITIAL_INTERNAL || target_path_.empty()); |
| 1178 |
| 1179 // If a resumption attempted failed, or if the download was DOA, then the |
| 1180 // download should go back to being interrupted. |
| 1181 if (new_create_info.result != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 1182 DCHECK(!download_file_.get()); |
| 1183 |
| 1184 // Interrupted downloads also need a target path. |
| 1185 if (target_path_.empty()) { |
| 1186 TransitionTo(INTERRUPTED_TARGET_PENDING_INTERNAL); |
| 1187 destination_error_ = new_create_info.result; |
| 1188 DetermineDownloadTarget(); |
| 1189 return; |
| 1190 } |
| 1191 |
| 1192 // Otherwise, this was a resumption attempt which ended with an |
| 1193 // interruption. Continue with current target path. |
| 1194 TransitionTo(TARGET_RESOLVED_INTERNAL); |
| 1195 Interrupt(new_create_info.result); |
| 1196 UpdateObservers(); |
| 1197 return; |
| 1198 } |
| 1199 |
| 1200 // Successful download start. |
| 1201 DCHECK(download_file_.get()); |
| 1202 DCHECK(request_handle_.get()); |
| 1203 |
| 1204 if (state_ == RESUMING_INTERNAL) |
| 1205 UpdateValidatorsOnResumption(new_create_info); |
| 1206 |
| 1207 TransitionTo(TARGET_PENDING_INTERNAL); |
| 1153 | 1208 |
| 1154 BrowserThread::PostTask( | 1209 BrowserThread::PostTask( |
| 1155 BrowserThread::FILE, FROM_HERE, | 1210 BrowserThread::FILE, FROM_HERE, |
| 1156 base::Bind(&DownloadFile::Initialize, | 1211 base::Bind(&DownloadFile::Initialize, |
| 1157 // Safe because we control download file lifetime. | 1212 // Safe because we control download file lifetime. |
| 1158 base::Unretained(download_file_.get()), | 1213 base::Unretained(download_file_.get()), |
| 1159 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized, | 1214 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized, |
| 1160 weak_ptr_factory_.GetWeakPtr()))); | 1215 weak_ptr_factory_.GetWeakPtr()))); |
| 1161 } | 1216 } |
| 1162 | 1217 |
| 1163 void DownloadItemImpl::OnDownloadFileInitialized( | 1218 void DownloadItemImpl::OnDownloadFileInitialized( |
| 1164 DownloadInterruptReason result) { | 1219 DownloadInterruptReason result) { |
| 1165 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1220 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1166 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); | 1221 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); |
| 1167 DVLOG(20) << __FUNCTION__ | 1222 DVLOG(20) << __FUNCTION__ |
| 1168 << "() result:" << DownloadInterruptReasonToString(result); | 1223 << "() result:" << DownloadInterruptReasonToString(result); |
| 1169 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) { | 1224 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 1170 // Transition out to TARGET_RESOLVED_INTERNAL since this DownloadItem is | 1225 // Whoops. That didn't work. Proceed as an interrupted download. |
| 1171 // skipping the download target determination process. | 1226 destination_error_ = result; |
| 1172 TransitionTo(TARGET_RESOLVED_INTERNAL, DONT_UPDATE_OBSERVERS); | 1227 TransitionTo(INTERRUPTED_TARGET_PENDING_INTERNAL); |
| 1173 Interrupt(result); | |
| 1174 // TODO(rdsmith/asanka): Arguably we should show this in the UI, but | |
| 1175 // it's not at all clear what to show--we haven't done filename | |
| 1176 // determination, so we don't know what name to display. OTOH, | |
| 1177 // the failure mode of not showing the DI if the file initialization | |
| 1178 // fails isn't a good one. Can we hack up a name based on the | |
| 1179 // URLRequest? We'll need to make sure that initialization happens | |
| 1180 // properly. Possibly the right thing is to have the UI handle | |
| 1181 // this case specially. | |
| 1182 return; | |
| 1183 } | 1228 } |
| 1184 | 1229 |
| 1230 DetermineDownloadTarget(); |
| 1231 } |
| 1232 |
| 1233 void DownloadItemImpl::DetermineDownloadTarget() { |
| 1234 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1235 DVLOG(20) << __FUNCTION__ << "() " << DebugString(true); |
| 1236 |
| 1185 delegate_->DetermineDownloadTarget( | 1237 delegate_->DetermineDownloadTarget( |
| 1186 this, base::Bind(&DownloadItemImpl::OnDownloadTargetDetermined, | 1238 this, base::Bind(&DownloadItemImpl::OnDownloadTargetDetermined, |
| 1187 weak_ptr_factory_.GetWeakPtr())); | 1239 weak_ptr_factory_.GetWeakPtr())); |
| 1188 } | 1240 } |
| 1189 | 1241 |
| 1190 // Called by delegate_ when the download target path has been | 1242 // Called by delegate_ when the download target path has been determined. |
| 1191 // determined. | |
| 1192 void DownloadItemImpl::OnDownloadTargetDetermined( | 1243 void DownloadItemImpl::OnDownloadTargetDetermined( |
| 1193 const base::FilePath& target_path, | 1244 const base::FilePath& target_path, |
| 1194 TargetDisposition disposition, | 1245 TargetDisposition disposition, |
| 1195 DownloadDangerType danger_type, | 1246 DownloadDangerType danger_type, |
| 1196 const base::FilePath& intermediate_path) { | 1247 const base::FilePath& intermediate_path) { |
| 1197 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1248 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1198 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); | 1249 DCHECK(state_ == TARGET_PENDING_INTERNAL || |
| 1250 state_ == INTERRUPTED_TARGET_PENDING_INTERNAL); |
| 1199 | 1251 |
| 1200 // If the |target_path| is empty, then we consider this download to be | 1252 // If the |target_path| is empty, then we consider this download to be |
| 1201 // canceled. | 1253 // canceled. |
| 1202 if (target_path.empty()) { | 1254 if (target_path.empty()) { |
| 1203 Cancel(true); | 1255 Cancel(true); |
| 1204 return; | 1256 return; |
| 1205 } | 1257 } |
| 1206 | 1258 |
| 1207 // TODO(rdsmith,asanka): We are ignoring the possibility that the download | 1259 DVLOG(20) << __FUNCTION__ << "() target_path:" << target_path.value() |
| 1208 // has been interrupted at this point until we finish the intermediate | 1260 << " disposition:" << disposition << " danger_type:" << danger_type |
| 1209 // rename and set the full path. That's dangerous, because we might race | 1261 << " this:" << DebugString(true); |
| 1210 // with resumption, either manual (because the interrupt is visible to the | |
| 1211 // UI) or automatic. If we keep the "ignore an error on download until file | |
| 1212 // name determination complete" semantics, we need to make sure that the | |
| 1213 // error is kept completely invisible until that point. | |
| 1214 | |
| 1215 DVLOG(20) << __FUNCTION__ << " " << target_path.value() << " " << disposition | |
| 1216 << " " << danger_type << " " << DebugString(true); | |
| 1217 | 1262 |
| 1218 target_path_ = target_path; | 1263 target_path_ = target_path; |
| 1219 target_disposition_ = disposition; | 1264 target_disposition_ = disposition; |
| 1220 SetDangerType(danger_type); | 1265 SetDangerType(danger_type); |
| 1221 | 1266 |
| 1267 // This was an interrupted download that was looking for a filename. Now that |
| 1268 // it has one, transition to interrupted. |
| 1269 if (state_ == INTERRUPTED_TARGET_PENDING_INTERNAL) { |
| 1270 Interrupt(destination_error_); |
| 1271 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 1272 UpdateObservers(); |
| 1273 return; |
| 1274 } |
| 1275 |
| 1222 // We want the intermediate and target paths to refer to the same directory so | 1276 // We want the intermediate and target paths to refer to the same directory so |
| 1223 // that they are both on the same device and subject to same | 1277 // that they are both on the same device and subject to same |
| 1224 // space/permission/availability constraints. | 1278 // space/permission/availability constraints. |
| 1225 DCHECK(intermediate_path.DirName() == target_path.DirName()); | 1279 DCHECK(intermediate_path.DirName() == target_path.DirName()); |
| 1226 | 1280 |
| 1227 // During resumption, we may choose to proceed with the same intermediate | 1281 // During resumption, we may choose to proceed with the same intermediate |
| 1228 // file. No rename is necessary if our intermediate file already has the | 1282 // file. No rename is necessary if our intermediate file already has the |
| 1229 // correct name. | 1283 // correct name. |
| 1230 // | 1284 // |
| 1231 // The intermediate name may change from its original value during filename | 1285 // The intermediate name may change from its original value during filename |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1254 base::Unretained(download_file_.get()), | 1308 base::Unretained(download_file_.get()), |
| 1255 intermediate_path, callback)); | 1309 intermediate_path, callback)); |
| 1256 } | 1310 } |
| 1257 | 1311 |
| 1258 void DownloadItemImpl::OnDownloadRenamedToIntermediateName( | 1312 void DownloadItemImpl::OnDownloadRenamedToIntermediateName( |
| 1259 DownloadInterruptReason reason, | 1313 DownloadInterruptReason reason, |
| 1260 const base::FilePath& full_path) { | 1314 const base::FilePath& full_path) { |
| 1261 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1315 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1262 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); | 1316 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); |
| 1263 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 1317 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
| 1264 TransitionTo(TARGET_RESOLVED_INTERNAL, DONT_UPDATE_OBSERVERS); | 1318 TransitionTo(TARGET_RESOLVED_INTERNAL); |
| 1265 | 1319 |
| 1266 if (DOWNLOAD_INTERRUPT_REASON_NONE != destination_error_) { | 1320 if (DOWNLOAD_INTERRUPT_REASON_NONE != destination_error_) { |
| 1267 // Process destination error. If both |reason| and |destination_error_| | 1321 // Process destination error. If both |reason| and |destination_error_| |
| 1268 // refer to actual errors, we want to use the |destination_error_| as the | 1322 // refer to actual errors, we want to use the |destination_error_| as the |
| 1269 // argument to the Interrupt() routine, as it happened first. | 1323 // argument to the Interrupt() routine, as it happened first. |
| 1270 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) | 1324 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 1271 SetFullPath(full_path); | 1325 SetFullPath(full_path); |
| 1272 Interrupt(destination_error_); | 1326 Interrupt(destination_error_); |
| 1273 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE; | 1327 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 1328 UpdateObservers(); |
| 1274 } else if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) { | 1329 } else if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) { |
| 1275 Interrupt(reason); | 1330 Interrupt(reason); |
| 1276 // All file errors result in file deletion above; no need to cleanup. The | 1331 // All file errors result in file deletion above; no need to cleanup. The |
| 1277 // current_path_ should be empty. Resuming this download will force a | 1332 // current_path_ should be empty. Resuming this download will force a |
| 1278 // restart and a re-doing of filename determination. | 1333 // restart and a re-doing of filename determination. |
| 1279 DCHECK(current_path_.empty()); | 1334 DCHECK(current_path_.empty()); |
| 1335 UpdateObservers(); |
| 1280 } else { | 1336 } else { |
| 1281 SetFullPath(full_path); | 1337 SetFullPath(full_path); |
| 1282 TransitionTo(IN_PROGRESS_INTERNAL, UPDATE_OBSERVERS); | 1338 TransitionTo(IN_PROGRESS_INTERNAL); |
| 1339 // TODO(asanka): Calling UpdateObservers() prior to MaybeCompleteDownload() |
| 1340 // is not safe. The download could be in an underminate state after invoking |
| 1341 // observers. http://crbug.com/586610 |
| 1342 UpdateObservers(); |
| 1283 MaybeCompleteDownload(); | 1343 MaybeCompleteDownload(); |
| 1284 } | 1344 } |
| 1285 } | 1345 } |
| 1286 | 1346 |
| 1287 // When SavePackage downloads MHTML to GData (see | 1347 // When SavePackage downloads MHTML to GData (see |
| 1288 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it | 1348 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it |
| 1289 // does for non-SavePackage downloads, but SavePackage downloads never satisfy | 1349 // does for non-SavePackage downloads, but SavePackage downloads never satisfy |
| 1290 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls | 1350 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls |
| 1291 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage | 1351 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage |
| 1292 // notices that the upload has completed and runs its normal Finish() pathway. | 1352 // notices that the upload has completed and runs its normal Finish() pathway. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1320 return; | 1380 return; |
| 1321 | 1381 |
| 1322 DVLOG(20) << __FUNCTION__ << "()" | 1382 DVLOG(20) << __FUNCTION__ << "()" |
| 1323 << " " << DebugString(true); | 1383 << " " << DebugString(true); |
| 1324 DCHECK(!GetTargetFilePath().empty()); | 1384 DCHECK(!GetTargetFilePath().empty()); |
| 1325 DCHECK(!IsDangerous()); | 1385 DCHECK(!IsDangerous()); |
| 1326 | 1386 |
| 1327 // TODO(rdsmith/benjhayden): Remove as part of SavePackage integration. | 1387 // TODO(rdsmith/benjhayden): Remove as part of SavePackage integration. |
| 1328 if (is_save_package_download_) { | 1388 if (is_save_package_download_) { |
| 1329 // Avoid doing anything on the file thread; there's nothing we control | 1389 // Avoid doing anything on the file thread; there's nothing we control |
| 1330 // there. | 1390 // there. Strictly speaking, this skips giving the embedder a chance to |
| 1331 // Strictly speaking, this skips giving the embedder a chance to open | 1391 // open the download. But on a save package download, there's no real |
| 1332 // the download. But on a save package download, there's no real | |
| 1333 // concept of opening. | 1392 // concept of opening. |
| 1334 Completed(); | 1393 Completed(); |
| 1335 return; | 1394 return; |
| 1336 } | 1395 } |
| 1337 | 1396 |
| 1338 DCHECK(download_file_.get()); | 1397 DCHECK(download_file_.get()); |
| 1339 // Unilaterally rename; even if it already has the right name, | 1398 // Unilaterally rename; even if it already has the right name, |
| 1340 // we need theannotation. | 1399 // we need theannotation. |
| 1341 DownloadFile::RenameCompletionCallback callback = | 1400 DownloadFile::RenameCompletionCallback callback = |
| 1342 base::Bind(&DownloadItemImpl::OnDownloadRenamedToFinalName, | 1401 base::Bind(&DownloadItemImpl::OnDownloadRenamedToFinalName, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1363 DVLOG(20) << __FUNCTION__ << "()" | 1422 DVLOG(20) << __FUNCTION__ << "()" |
| 1364 << " full_path = \"" << full_path.value() << "\"" | 1423 << " full_path = \"" << full_path.value() << "\"" |
| 1365 << " " << DebugString(false); | 1424 << " " << DebugString(false); |
| 1366 | 1425 |
| 1367 if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) { | 1426 if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) { |
| 1368 Interrupt(reason); | 1427 Interrupt(reason); |
| 1369 | 1428 |
| 1370 // All file errors should have resulted in in file deletion above. On | 1429 // All file errors should have resulted in in file deletion above. On |
| 1371 // resumption we will need to re-do filename determination. | 1430 // resumption we will need to re-do filename determination. |
| 1372 DCHECK(current_path_.empty()); | 1431 DCHECK(current_path_.empty()); |
| 1432 UpdateObservers(); |
| 1373 return; | 1433 return; |
| 1374 } | 1434 } |
| 1375 | 1435 |
| 1376 DCHECK(target_path_ == full_path); | 1436 DCHECK(target_path_ == full_path); |
| 1377 | 1437 |
| 1378 if (full_path != current_path_) { | 1438 if (full_path != current_path_) { |
| 1379 // full_path is now the current and target file path. | 1439 // full_path is now the current and target file path. |
| 1380 DCHECK(!full_path.empty()); | 1440 DCHECK(!full_path.empty()); |
| 1381 SetFullPath(full_path); | 1441 SetFullPath(full_path); |
| 1382 } | 1442 } |
| 1383 | 1443 |
| 1384 // Complete the download and release the DownloadFile. | 1444 // Complete the download and release the DownloadFile. |
| 1385 DCHECK(download_file_); | 1445 DCHECK(download_file_); |
| 1386 ReleaseDownloadFile(false); | 1446 ReleaseDownloadFile(false); |
| 1387 | 1447 |
| 1388 // We're not completely done with the download item yet, but at this | 1448 // We're not completely done with the download item yet, but at this |
| 1389 // point we're committed to complete the download. Cancels (or Interrupts, | 1449 // point we're committed to complete the download. Cancels (or Interrupts, |
| 1390 // though it's not clear how they could happen) after this point will be | 1450 // though it's not clear how they could happen) after this point will be |
| 1391 // ignored. | 1451 // ignored. |
| 1392 TransitionTo(COMPLETING_INTERNAL, DONT_UPDATE_OBSERVERS); | 1452 TransitionTo(COMPLETING_INTERNAL); |
| 1393 | 1453 |
| 1394 if (delegate_->ShouldOpenDownload( | 1454 if (delegate_->ShouldOpenDownload( |
| 1395 this, base::Bind(&DownloadItemImpl::DelayedDownloadOpened, | 1455 this, base::Bind(&DownloadItemImpl::DelayedDownloadOpened, |
| 1396 weak_ptr_factory_.GetWeakPtr()))) { | 1456 weak_ptr_factory_.GetWeakPtr()))) { |
| 1397 Completed(); | 1457 Completed(); |
| 1398 } else { | 1458 } else { |
| 1399 delegate_delayed_complete_ = true; | 1459 delegate_delayed_complete_ = true; |
| 1400 UpdateObservers(); | 1460 UpdateObservers(); |
| 1401 } | 1461 } |
| 1402 } | 1462 } |
| 1403 | 1463 |
| 1404 void DownloadItemImpl::DelayedDownloadOpened(bool auto_opened) { | 1464 void DownloadItemImpl::DelayedDownloadOpened(bool auto_opened) { |
| 1405 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1465 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1406 | 1466 |
| 1407 auto_opened_ = auto_opened; | 1467 auto_opened_ = auto_opened; |
| 1408 Completed(); | 1468 Completed(); |
| 1409 } | 1469 } |
| 1410 | 1470 |
| 1411 void DownloadItemImpl::Completed() { | 1471 void DownloadItemImpl::Completed() { |
| 1412 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1472 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1413 | 1473 |
| 1414 DVLOG(20) << __FUNCTION__ << "() " << DebugString(false); | 1474 DVLOG(20) << __FUNCTION__ << "() " << DebugString(false); |
| 1415 | 1475 |
| 1416 DCHECK(all_data_saved_); | 1476 DCHECK(all_data_saved_); |
| 1417 end_time_ = base::Time::Now(); | 1477 end_time_ = base::Time::Now(); |
| 1418 TransitionTo(COMPLETE_INTERNAL, UPDATE_OBSERVERS); | 1478 TransitionTo(COMPLETE_INTERNAL); |
| 1419 RecordDownloadCompleted(start_tick_, received_bytes_); | 1479 RecordDownloadCompleted(start_tick_, received_bytes_); |
| 1420 | 1480 |
| 1421 if (auto_opened_) { | 1481 if (auto_opened_) { |
| 1422 // If it was already handled by the delegate, do nothing. | 1482 // If it was already handled by the delegate, do nothing. |
| 1423 } else if (GetOpenWhenComplete() || | 1483 } else if (GetOpenWhenComplete() || |
| 1424 ShouldOpenFileBasedOnExtension() || | 1484 ShouldOpenFileBasedOnExtension() || |
| 1425 IsTemporary()) { | 1485 IsTemporary()) { |
| 1426 // If the download is temporary, like in drag-and-drop, do not open it but | 1486 // If the download is temporary, like in drag-and-drop, do not open it but |
| 1427 // we still need to set it auto-opened so that it can be removed from the | 1487 // we still need to set it auto-opened so that it can be removed from the |
| 1428 // download shelf. | 1488 // download shelf. |
| 1429 if (!IsTemporary()) | 1489 if (!IsTemporary()) |
| 1430 OpenDownload(); | 1490 OpenDownload(); |
| 1431 | 1491 |
| 1432 auto_opened_ = true; | 1492 auto_opened_ = true; |
| 1433 UpdateObservers(); | |
| 1434 } | 1493 } |
| 1435 } | 1494 UpdateObservers(); |
| 1436 | |
| 1437 void DownloadItemImpl::OnResumeRequestStarted( | |
| 1438 DownloadItem* item, | |
| 1439 DownloadInterruptReason interrupt_reason) { | |
| 1440 // If |item| is not NULL, then Start() has been called already, and nothing | |
| 1441 // more needs to be done here. | |
| 1442 if (item) { | |
| 1443 DCHECK_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); | |
| 1444 DCHECK_EQ(static_cast<DownloadItem*>(this), item); | |
| 1445 return; | |
| 1446 } | |
| 1447 // Otherwise, the request failed without passing through | |
| 1448 // DownloadResourceHandler::OnResponseStarted. | |
| 1449 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); | |
| 1450 Interrupt(interrupt_reason); | |
| 1451 } | 1495 } |
| 1452 | 1496 |
| 1453 // **** End of Download progression cascade | 1497 // **** End of Download progression cascade |
| 1454 | 1498 |
| 1455 // An error occurred somewhere. | 1499 // An error occurred somewhere. |
| 1456 void DownloadItemImpl::Interrupt(DownloadInterruptReason reason) { | 1500 void DownloadItemImpl::Interrupt(DownloadInterruptReason reason) { |
| 1457 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1501 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1458 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, reason); | 1502 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, reason); |
| 1459 DVLOG(20) << __FUNCTION__ | 1503 DVLOG(20) << __FUNCTION__ |
| 1460 << "() reason:" << DownloadInterruptReasonToString(reason) | 1504 << "() reason:" << DownloadInterruptReasonToString(reason) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1475 case COMPLETING_INTERNAL: | 1519 case COMPLETING_INTERNAL: |
| 1476 case COMPLETE_INTERNAL: | 1520 case COMPLETE_INTERNAL: |
| 1477 // Already complete. | 1521 // Already complete. |
| 1478 return; | 1522 return; |
| 1479 | 1523 |
| 1480 case INITIAL_INTERNAL: | 1524 case INITIAL_INTERNAL: |
| 1481 case MAX_DOWNLOAD_INTERNAL_STATE: | 1525 case MAX_DOWNLOAD_INTERNAL_STATE: |
| 1482 NOTREACHED(); | 1526 NOTREACHED(); |
| 1483 return; | 1527 return; |
| 1484 | 1528 |
| 1529 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 1530 case IN_PROGRESS_INTERNAL: |
| 1485 case TARGET_PENDING_INTERNAL: | 1531 case TARGET_PENDING_INTERNAL: |
| 1486 case TARGET_RESOLVED_INTERNAL: | 1532 case TARGET_RESOLVED_INTERNAL: |
| 1487 case IN_PROGRESS_INTERNAL: | |
| 1488 // last_reason_ needs to be set for GetResumeMode() to work. | 1533 // last_reason_ needs to be set for GetResumeMode() to work. |
| 1489 last_reason_ = reason; | 1534 last_reason_ = reason; |
| 1490 | 1535 |
| 1491 if (download_file_) { | 1536 if (download_file_) { |
| 1492 ResumeMode resume_mode = GetResumeMode(); | 1537 ResumeMode resume_mode = GetResumeMode(); |
| 1493 ReleaseDownloadFile(resume_mode != RESUME_MODE_IMMEDIATE_CONTINUE && | 1538 ReleaseDownloadFile(resume_mode != RESUME_MODE_IMMEDIATE_CONTINUE && |
| 1494 resume_mode != RESUME_MODE_USER_CONTINUE); | 1539 resume_mode != RESUME_MODE_USER_CONTINUE); |
| 1495 } | 1540 } |
| 1496 break; | 1541 break; |
| 1497 | 1542 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1535 reason == DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN) { | 1580 reason == DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN) { |
| 1536 if (IsDangerous()) { | 1581 if (IsDangerous()) { |
| 1537 RecordDangerousDownloadDiscard( | 1582 RecordDangerousDownloadDiscard( |
| 1538 reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED | 1583 reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED |
| 1539 ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION | 1584 ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION |
| 1540 : DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN, | 1585 : DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN, |
| 1541 GetDangerType(), GetTargetFilePath()); | 1586 GetDangerType(), GetTargetFilePath()); |
| 1542 } | 1587 } |
| 1543 | 1588 |
| 1544 RecordDownloadCount(CANCELLED_COUNT); | 1589 RecordDownloadCount(CANCELLED_COUNT); |
| 1545 TransitionTo(CANCELLED_INTERNAL, DONT_UPDATE_OBSERVERS); | 1590 TransitionTo(CANCELLED_INTERNAL); |
| 1546 } else { | 1591 return; |
| 1547 RecordDownloadInterrupted(reason, received_bytes_, total_bytes_); | |
| 1548 if (!GetWebContents()) | |
| 1549 RecordDownloadCount(INTERRUPTED_WITHOUT_WEBCONTENTS); | |
| 1550 TransitionTo(INTERRUPTED_INTERNAL, DONT_UPDATE_OBSERVERS); | |
| 1551 AutoResumeIfValid(); | |
| 1552 } | 1592 } |
| 1553 | 1593 |
| 1554 UpdateObservers(); | 1594 RecordDownloadInterrupted(reason, received_bytes_, total_bytes_); |
| 1595 if (!GetWebContents()) |
| 1596 RecordDownloadCount(INTERRUPTED_WITHOUT_WEBCONTENTS); |
| 1597 |
| 1598 TransitionTo(INTERRUPTED_INTERNAL); |
| 1599 AutoResumeIfValid(); |
| 1555 } | 1600 } |
| 1556 | 1601 |
| 1557 void DownloadItemImpl::ReleaseDownloadFile(bool destroy_file) { | 1602 void DownloadItemImpl::ReleaseDownloadFile(bool destroy_file) { |
| 1558 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1603 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1559 DVLOG(20) << __FUNCTION__ << "() destroy_file:" << destroy_file; | 1604 DVLOG(20) << __FUNCTION__ << "() destroy_file:" << destroy_file; |
| 1560 | 1605 |
| 1561 if (destroy_file) { | 1606 if (destroy_file) { |
| 1562 BrowserThread::PostTask( | 1607 BrowserThread::PostTask( |
| 1563 BrowserThread::FILE, FROM_HERE, | 1608 BrowserThread::FILE, FROM_HERE, |
| 1564 // Will be deleted at end of task execution. | 1609 // Will be deleted at end of task execution. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1590 // If we don't have all the data, the download is not ready for | 1635 // If we don't have all the data, the download is not ready for |
| 1591 // completion. | 1636 // completion. |
| 1592 if (!AllDataSaved()) | 1637 if (!AllDataSaved()) |
| 1593 return false; | 1638 return false; |
| 1594 | 1639 |
| 1595 // If the download is dangerous, but not yet validated, it's not ready for | 1640 // If the download is dangerous, but not yet validated, it's not ready for |
| 1596 // completion. | 1641 // completion. |
| 1597 if (IsDangerous()) | 1642 if (IsDangerous()) |
| 1598 return false; | 1643 return false; |
| 1599 | 1644 |
| 1600 // Invariants for the IN_PROGRESS state. DCHECKs here verify that the | 1645 // Check for consistency before invoking delegate. Since there are no pending |
| 1601 // invariants are still true. | 1646 // target determination calls and the download is in progress, both the target |
| 1647 // and current paths should be non-empty and they should point to the same |
| 1648 // directory. |
| 1602 DCHECK(!target_path_.empty()); | 1649 DCHECK(!target_path_.empty()); |
| 1603 DCHECK(!current_path_.empty()); | 1650 DCHECK(!current_path_.empty()); |
| 1604 DCHECK(target_path_.DirName() == current_path_.DirName()); | 1651 DCHECK(target_path_.DirName() == current_path_.DirName()); |
| 1605 | 1652 |
| 1606 // Give the delegate a chance to hold up a stop sign. It'll call | 1653 // Give the delegate a chance to hold up a stop sign. It'll call |
| 1607 // use back through the passed callback if it does and that state changes. | 1654 // use back through the passed callback if it does and that state changes. |
| 1608 if (!delegate_->ShouldCompleteDownload(this, state_change_notification)) | 1655 if (!delegate_->ShouldCompleteDownload(this, state_change_notification)) |
| 1609 return false; | 1656 return false; |
| 1610 | 1657 |
| 1611 return true; | 1658 return true; |
| 1612 } | 1659 } |
| 1613 | 1660 |
| 1614 void DownloadItemImpl::TransitionTo(DownloadInternalState new_state, | 1661 void DownloadItemImpl::TransitionTo(DownloadInternalState new_state) { |
| 1615 ShouldUpdateObservers notify_action) { | |
| 1616 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1662 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1617 | 1663 |
| 1618 if (state_ == new_state) | 1664 if (state_ == new_state) |
| 1619 return; | 1665 return; |
| 1620 | 1666 |
| 1621 DownloadInternalState old_state = state_; | 1667 DownloadInternalState old_state = state_; |
| 1622 state_ = new_state; | 1668 state_ = new_state; |
| 1623 | 1669 |
| 1624 DCHECK(is_save_package_download_ | 1670 DCHECK(is_save_package_download_ |
| 1625 ? IsValidSavePackageStateTransition(old_state, new_state) | 1671 ? IsValidSavePackageStateTransition(old_state, new_state) |
| 1626 : IsValidStateTransition(old_state, new_state)) | 1672 : IsValidStateTransition(old_state, new_state)) |
| 1627 << "Invalid state transition from:" << DebugDownloadStateString(old_state) | 1673 << "Invalid state transition from:" << DebugDownloadStateString(old_state) |
| 1628 << " to:" << DebugDownloadStateString(new_state); | 1674 << " to:" << DebugDownloadStateString(new_state); |
| 1629 | 1675 |
| 1630 switch (state_) { | 1676 switch (state_) { |
| 1631 case INITIAL_INTERNAL: | 1677 case INITIAL_INTERNAL: |
| 1632 NOTREACHED(); | 1678 NOTREACHED(); |
| 1633 break; | 1679 break; |
| 1634 | 1680 |
| 1635 case TARGET_PENDING_INTERNAL: | 1681 case TARGET_PENDING_INTERNAL: |
| 1636 case TARGET_RESOLVED_INTERNAL: | 1682 case TARGET_RESOLVED_INTERNAL: |
| 1683 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 1637 break; | 1684 break; |
| 1638 | 1685 |
| 1639 case IN_PROGRESS_INTERNAL: | 1686 case IN_PROGRESS_INTERNAL: |
| 1640 DCHECK(!current_path_.empty()) << "Current output path must be known."; | 1687 DCHECK(!current_path_.empty()) << "Current output path must be known."; |
| 1641 DCHECK(!target_path_.empty()) << "Target path must be known."; | 1688 DCHECK(!target_path_.empty()) << "Target path must be known."; |
| 1642 DCHECK(current_path_.DirName() == target_path_.DirName()) | 1689 DCHECK(current_path_.DirName() == target_path_.DirName()) |
| 1643 << "Current output directory must match target directory."; | 1690 << "Current output directory must match target directory."; |
| 1644 DCHECK(download_file_) << "Output file must be owned by download item."; | 1691 DCHECK(download_file_) << "Output file must be owned by download item."; |
| 1645 DCHECK(request_handle_) << "Download source must be active."; | 1692 DCHECK(request_handle_) << "Download source must be active."; |
| 1646 DCHECK(!is_paused_) << "At the time a download enters IN_PROGRESS state, " | 1693 DCHECK(!is_paused_) << "At the time a download enters IN_PROGRESS state, " |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1708 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE); | 1755 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE); |
| 1709 | 1756 |
| 1710 // Resumption | 1757 // Resumption |
| 1711 if (was_done && !is_done) { | 1758 if (was_done && !is_done) { |
| 1712 std::string file_name(target_path_.BaseName().AsUTF8Unsafe()); | 1759 std::string file_name(target_path_.BaseName().AsUTF8Unsafe()); |
| 1713 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, | 1760 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, |
| 1714 base::Bind(&ItemActivatedNetLogCallback, | 1761 base::Bind(&ItemActivatedNetLogCallback, |
| 1715 this, SRC_ACTIVE_DOWNLOAD, | 1762 this, SRC_ACTIVE_DOWNLOAD, |
| 1716 &file_name)); | 1763 &file_name)); |
| 1717 } | 1764 } |
| 1718 | |
| 1719 if (notify_action == UPDATE_OBSERVERS) | |
| 1720 UpdateObservers(); | |
| 1721 } | 1765 } |
| 1722 | 1766 |
| 1723 void DownloadItemImpl::SetDangerType(DownloadDangerType danger_type) { | 1767 void DownloadItemImpl::SetDangerType(DownloadDangerType danger_type) { |
| 1724 if (danger_type != danger_type_) { | 1768 if (danger_type != danger_type_) { |
| 1725 bound_net_log_.AddEvent( | 1769 bound_net_log_.AddEvent( |
| 1726 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED, | 1770 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED, |
| 1727 base::Bind(&ItemCheckedNetLogCallback, danger_type)); | 1771 base::Bind(&ItemCheckedNetLogCallback, danger_type)); |
| 1728 } | 1772 } |
| 1729 // Only record the Malicious UMA stat if it's going from {not malicious} -> | 1773 // Only record the Malicious UMA stat if it's going from {not malicious} -> |
| 1730 // {malicious}. | 1774 // {malicious}. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1772 | 1816 |
| 1773 void DownloadItemImpl::ResumeInterruptedDownload() { | 1817 void DownloadItemImpl::ResumeInterruptedDownload() { |
| 1774 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1818 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1775 if (!IsDownloadResumptionEnabled()) | 1819 if (!IsDownloadResumptionEnabled()) |
| 1776 return; | 1820 return; |
| 1777 | 1821 |
| 1778 // If we're not interrupted, ignore the request; our caller is drunk. | 1822 // If we're not interrupted, ignore the request; our caller is drunk. |
| 1779 if (state_ != INTERRUPTED_INTERNAL) | 1823 if (state_ != INTERRUPTED_INTERNAL) |
| 1780 return; | 1824 return; |
| 1781 | 1825 |
| 1826 // We are starting a new request. Shake off all pending operations. |
| 1827 DCHECK(!download_file_); |
| 1828 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 1829 |
| 1782 // Reset the appropriate state if restarting. | 1830 // Reset the appropriate state if restarting. |
| 1783 ResumeMode mode = GetResumeMode(); | 1831 ResumeMode mode = GetResumeMode(); |
| 1784 if (mode == RESUME_MODE_IMMEDIATE_RESTART || | 1832 if (mode == RESUME_MODE_IMMEDIATE_RESTART || |
| 1785 mode == RESUME_MODE_USER_RESTART) { | 1833 mode == RESUME_MODE_USER_RESTART) { |
| 1786 received_bytes_ = 0; | 1834 received_bytes_ = 0; |
| 1787 hash_state_ = ""; | 1835 hash_state_ = ""; |
| 1788 last_modified_time_ = ""; | 1836 last_modified_time_ = ""; |
| 1789 etag_ = ""; | 1837 etag_ = ""; |
| 1790 } | 1838 } |
| 1791 | 1839 |
| 1792 scoped_ptr<DownloadUrlParameters> download_params; | 1840 // Avoid using the WebContents even if it's still around. Resumption requests |
| 1793 if (GetWebContents()) { | 1841 // are consistently routed through the no-renderer code paths so that the |
| 1794 download_params = | 1842 // request will not be dropped if the WebContents (and by extension, the |
| 1795 DownloadUrlParameters::FromWebContents(GetWebContents(), GetURL()); | 1843 // associated renderer) goes away before a response is received. |
| 1796 } else { | 1844 scoped_ptr<DownloadUrlParameters> download_params(new DownloadUrlParameters( |
| 1797 download_params = make_scoped_ptr(new DownloadUrlParameters( | 1845 GetURL(), -1, -1, -1, GetBrowserContext()->GetResourceContext())); |
| 1798 GetURL(), -1, -1, -1, GetBrowserContext()->GetResourceContext())); | |
| 1799 } | |
| 1800 | |
| 1801 download_params->set_file_path(GetFullPath()); | 1846 download_params->set_file_path(GetFullPath()); |
| 1802 download_params->set_offset(GetReceivedBytes()); | 1847 download_params->set_offset(GetReceivedBytes()); |
| 1803 download_params->set_hash_state(GetHashState()); | 1848 download_params->set_hash_state(GetHashState()); |
| 1804 download_params->set_last_modified(GetLastModifiedTime()); | 1849 download_params->set_last_modified(GetLastModifiedTime()); |
| 1805 download_params->set_etag(GetETag()); | 1850 download_params->set_etag(GetETag()); |
| 1806 download_params->set_callback( | |
| 1807 base::Bind(&DownloadItemImpl::OnResumeRequestStarted, | |
| 1808 weak_ptr_factory_.GetWeakPtr())); | |
| 1809 | 1851 |
| 1810 TransitionTo(RESUMING_INTERNAL, DONT_UPDATE_OBSERVERS); | 1852 TransitionTo(RESUMING_INTERNAL); |
| 1811 delegate_->ResumeInterruptedDownload(std::move(download_params), GetId()); | 1853 delegate_->ResumeInterruptedDownload(std::move(download_params), GetId()); |
| 1812 // Just in case we were interrupted while paused. | 1854 // Just in case we were interrupted while paused. |
| 1813 is_paused_ = false; | 1855 is_paused_ = false; |
| 1814 } | 1856 } |
| 1815 | 1857 |
| 1816 // static | 1858 // static |
| 1817 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState( | 1859 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState( |
| 1818 DownloadInternalState internal_state) { | 1860 DownloadInternalState internal_state) { |
| 1819 switch (internal_state) { | 1861 switch (internal_state) { |
| 1820 case INITIAL_INTERNAL: | 1862 case INITIAL_INTERNAL: |
| 1821 case TARGET_PENDING_INTERNAL: | 1863 case TARGET_PENDING_INTERNAL: |
| 1822 case TARGET_RESOLVED_INTERNAL: | 1864 case TARGET_RESOLVED_INTERNAL: |
| 1865 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 1823 // TODO(asanka): Introduce an externally visible state to distinguish | 1866 // TODO(asanka): Introduce an externally visible state to distinguish |
| 1824 // between the above states and IN_PROGRESS_INTERNAL. The latter (the | 1867 // between the above states and IN_PROGRESS_INTERNAL. The latter (the |
| 1825 // state where the download is active and has a known target) is the state | 1868 // state where the download is active and has a known target) is the state |
| 1826 // that most external users are interested in. | 1869 // that most external users are interested in. |
| 1827 case IN_PROGRESS_INTERNAL: | 1870 case IN_PROGRESS_INTERNAL: |
| 1828 return IN_PROGRESS; | 1871 return IN_PROGRESS; |
| 1829 case COMPLETING_INTERNAL: | 1872 case COMPLETING_INTERNAL: |
| 1830 return IN_PROGRESS; | 1873 return IN_PROGRESS; |
| 1831 case COMPLETE_INTERNAL: | 1874 case COMPLETE_INTERNAL: |
| 1832 return COMPLETE; | 1875 return COMPLETE; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1863 } | 1906 } |
| 1864 | 1907 |
| 1865 // static | 1908 // static |
| 1866 bool DownloadItemImpl::IsValidSavePackageStateTransition( | 1909 bool DownloadItemImpl::IsValidSavePackageStateTransition( |
| 1867 DownloadInternalState from, | 1910 DownloadInternalState from, |
| 1868 DownloadInternalState to) { | 1911 DownloadInternalState to) { |
| 1869 #if DCHECK_IS_ON() | 1912 #if DCHECK_IS_ON() |
| 1870 switch (from) { | 1913 switch (from) { |
| 1871 case INITIAL_INTERNAL: | 1914 case INITIAL_INTERNAL: |
| 1872 case TARGET_PENDING_INTERNAL: | 1915 case TARGET_PENDING_INTERNAL: |
| 1916 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 1873 case TARGET_RESOLVED_INTERNAL: | 1917 case TARGET_RESOLVED_INTERNAL: |
| 1874 case COMPLETING_INTERNAL: | 1918 case COMPLETING_INTERNAL: |
| 1875 case COMPLETE_INTERNAL: | 1919 case COMPLETE_INTERNAL: |
| 1876 case INTERRUPTED_INTERNAL: | 1920 case INTERRUPTED_INTERNAL: |
| 1877 case RESUMING_INTERNAL: | 1921 case RESUMING_INTERNAL: |
| 1878 case CANCELLED_INTERNAL: | 1922 case CANCELLED_INTERNAL: |
| 1879 return false; | 1923 return false; |
| 1880 | 1924 |
| 1881 case IN_PROGRESS_INTERNAL: | 1925 case IN_PROGRESS_INTERNAL: |
| 1882 return to == CANCELLED_INTERNAL || to == COMPLETE_INTERNAL; | 1926 return to == CANCELLED_INTERNAL || to == COMPLETE_INTERNAL; |
| 1883 | 1927 |
| 1884 case MAX_DOWNLOAD_INTERNAL_STATE: | 1928 case MAX_DOWNLOAD_INTERNAL_STATE: |
| 1885 NOTREACHED(); | 1929 NOTREACHED(); |
| 1886 } | 1930 } |
| 1887 return false; | 1931 return false; |
| 1888 #else | 1932 #else |
| 1889 return true; | 1933 return true; |
| 1890 #endif | 1934 #endif |
| 1891 } | 1935 } |
| 1892 | 1936 |
| 1893 // static | 1937 // static |
| 1894 bool DownloadItemImpl::IsValidStateTransition(DownloadInternalState from, | 1938 bool DownloadItemImpl::IsValidStateTransition(DownloadInternalState from, |
| 1895 DownloadInternalState to) { | 1939 DownloadInternalState to) { |
| 1896 #if DCHECK_IS_ON() | 1940 #if DCHECK_IS_ON() |
| 1897 switch (from) { | 1941 switch (from) { |
| 1898 case INITIAL_INTERNAL: | 1942 case INITIAL_INTERNAL: |
| 1899 return to == TARGET_PENDING_INTERNAL || to == INTERRUPTED_INTERNAL; | 1943 return to == TARGET_PENDING_INTERNAL || |
| 1944 to == INTERRUPTED_TARGET_PENDING_INTERNAL; |
| 1900 | 1945 |
| 1901 case TARGET_PENDING_INTERNAL: | 1946 case TARGET_PENDING_INTERNAL: |
| 1902 return to == TARGET_RESOLVED_INTERNAL || to == CANCELLED_INTERNAL; | 1947 return to == INTERRUPTED_TARGET_PENDING_INTERNAL || |
| 1948 to == TARGET_RESOLVED_INTERNAL || to == CANCELLED_INTERNAL; |
| 1949 |
| 1950 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 1951 return to == INTERRUPTED_INTERNAL || to == CANCELLED_INTERNAL; |
| 1903 | 1952 |
| 1904 case TARGET_RESOLVED_INTERNAL: | 1953 case TARGET_RESOLVED_INTERNAL: |
| 1905 return to == IN_PROGRESS_INTERNAL || to == INTERRUPTED_INTERNAL || | 1954 return to == IN_PROGRESS_INTERNAL || to == INTERRUPTED_INTERNAL || |
| 1906 to == CANCELLED_INTERNAL; | 1955 to == CANCELLED_INTERNAL; |
| 1907 | 1956 |
| 1908 case IN_PROGRESS_INTERNAL: | 1957 case IN_PROGRESS_INTERNAL: |
| 1909 return to == COMPLETING_INTERNAL || to == CANCELLED_INTERNAL || | 1958 return to == COMPLETING_INTERNAL || to == CANCELLED_INTERNAL || |
| 1910 to == INTERRUPTED_INTERNAL; | 1959 to == INTERRUPTED_INTERNAL; |
| 1911 | 1960 |
| 1912 case COMPLETING_INTERNAL: | 1961 case COMPLETING_INTERNAL: |
| 1913 return to == COMPLETE_INTERNAL; | 1962 return to == COMPLETE_INTERNAL; |
| 1914 | 1963 |
| 1915 case COMPLETE_INTERNAL: | 1964 case COMPLETE_INTERNAL: |
| 1916 return false; | 1965 return false; |
| 1917 | 1966 |
| 1918 case INTERRUPTED_INTERNAL: | 1967 case INTERRUPTED_INTERNAL: |
| 1919 return to == RESUMING_INTERNAL || to == CANCELLED_INTERNAL; | 1968 return to == RESUMING_INTERNAL || to == CANCELLED_INTERNAL; |
| 1920 | 1969 |
| 1921 case RESUMING_INTERNAL: | 1970 case RESUMING_INTERNAL: |
| 1922 return to == TARGET_PENDING_INTERNAL || to == CANCELLED_INTERNAL; | 1971 return to == TARGET_PENDING_INTERNAL || |
| 1972 to == INTERRUPTED_TARGET_PENDING_INTERNAL || |
| 1973 to == TARGET_RESOLVED_INTERNAL || to == CANCELLED_INTERNAL; |
| 1923 | 1974 |
| 1924 case CANCELLED_INTERNAL: | 1975 case CANCELLED_INTERNAL: |
| 1925 return false; | 1976 return false; |
| 1926 | 1977 |
| 1927 case MAX_DOWNLOAD_INTERNAL_STATE: | 1978 case MAX_DOWNLOAD_INTERNAL_STATE: |
| 1928 NOTREACHED(); | 1979 NOTREACHED(); |
| 1929 } | 1980 } |
| 1930 return false; | 1981 return false; |
| 1931 #else | 1982 #else |
| 1932 return true; | 1983 return true; |
| 1933 #endif // DCHECK_IS_ON() | 1984 #endif // DCHECK_IS_ON() |
| 1934 } | 1985 } |
| 1935 | 1986 |
| 1936 const char* DownloadItemImpl::DebugDownloadStateString( | 1987 const char* DownloadItemImpl::DebugDownloadStateString( |
| 1937 DownloadInternalState state) { | 1988 DownloadInternalState state) { |
| 1938 switch (state) { | 1989 switch (state) { |
| 1939 case INITIAL_INTERNAL: | 1990 case INITIAL_INTERNAL: |
| 1940 return "INITIAL"; | 1991 return "INITIAL"; |
| 1941 case TARGET_PENDING_INTERNAL: | 1992 case TARGET_PENDING_INTERNAL: |
| 1942 return "TARGET_PENDING"; | 1993 return "TARGET_PENDING"; |
| 1994 case INTERRUPTED_TARGET_PENDING_INTERNAL: |
| 1995 return "INTERRUPTED_TARGET_PENDING"; |
| 1943 case TARGET_RESOLVED_INTERNAL: | 1996 case TARGET_RESOLVED_INTERNAL: |
| 1944 return "TARGET_RESOLVED"; | 1997 return "TARGET_RESOLVED"; |
| 1945 case IN_PROGRESS_INTERNAL: | 1998 case IN_PROGRESS_INTERNAL: |
| 1946 return "IN_PROGRESS"; | 1999 return "IN_PROGRESS"; |
| 1947 case COMPLETING_INTERNAL: | 2000 case COMPLETING_INTERNAL: |
| 1948 return "COMPLETING"; | 2001 return "COMPLETING"; |
| 1949 case COMPLETE_INTERNAL: | 2002 case COMPLETE_INTERNAL: |
| 1950 return "COMPLETE"; | 2003 return "COMPLETE"; |
| 1951 case CANCELLED_INTERNAL: | 2004 case CANCELLED_INTERNAL: |
| 1952 return "CANCELLED"; | 2005 return "CANCELLED"; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1972 case RESUME_MODE_USER_CONTINUE: | 2025 case RESUME_MODE_USER_CONTINUE: |
| 1973 return "USER_CONTINUE"; | 2026 return "USER_CONTINUE"; |
| 1974 case RESUME_MODE_USER_RESTART: | 2027 case RESUME_MODE_USER_RESTART: |
| 1975 return "USER_RESTART"; | 2028 return "USER_RESTART"; |
| 1976 } | 2029 } |
| 1977 NOTREACHED() << "Unknown resume mode " << mode; | 2030 NOTREACHED() << "Unknown resume mode " << mode; |
| 1978 return "unknown"; | 2031 return "unknown"; |
| 1979 } | 2032 } |
| 1980 | 2033 |
| 1981 } // namespace content | 2034 } // namespace content |
| OLD | NEW |