| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/download/download_item.h" | 5 #include "content/browser/download/download_item.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 346 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 347 | 347 |
| 348 if (!IsInProgress()) { | 348 if (!IsInProgress()) { |
| 349 NOTREACHED(); | 349 NOTREACHED(); |
| 350 return; | 350 return; |
| 351 } | 351 } |
| 352 UpdateSize(bytes_so_far); | 352 UpdateSize(bytes_so_far); |
| 353 UpdateObservers(); | 353 UpdateObservers(); |
| 354 } | 354 } |
| 355 | 355 |
| 356 // Triggered by a user action. | 356 void DownloadItem::Cancel() { |
| 357 void DownloadItem::Cancel(bool update_history) { | |
| 358 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 357 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 359 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 358 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 359 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 360 | 360 |
| 361 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 361 // Small downloads might be complete before we have a chance to run. |
| 362 if (!IsPartialDownload()) { | 362 if (!IsInProgress()) |
| 363 // Small downloads might be complete before this method has | |
| 364 // a chance to run. | |
| 365 return; | 363 return; |
| 366 } | 364 |
| 365 TransitionTo(CANCELLED); |
| 367 | 366 |
| 368 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); | 367 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); |
| 369 | 368 |
| 370 TransitionTo(CANCELLED); | 369 // History insertion is the point at which we have finalized download |
| 371 StopProgressTimer(); | 370 // details and persist them if something goes wrong. Before history |
| 372 if (update_history) | 371 // insertion, interrupt or cancel results in download removal. |
| 373 download_manager_->DownloadCancelledInternal(this); | 372 if (db_handle() == DownloadItem::kUninitializedHandle) { |
| 373 download_manager_->RemoveDownload(this); |
| 374 // We are now deleted; no further code should be executed on this |
| 375 // object. |
| 376 } |
| 374 } | 377 } |
| 375 | 378 |
| 376 void DownloadItem::MarkAsComplete() { | 379 void DownloadItem::MarkAsComplete() { |
| 377 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 380 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 378 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 381 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 379 | 382 |
| 380 DCHECK(all_data_saved_); | 383 DCHECK(all_data_saved_); |
| 381 TransitionTo(COMPLETE); | 384 TransitionTo(COMPLETE); |
| 382 } | 385 } |
| 383 | 386 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 // download shelf. | 425 // download shelf. |
| 423 if (!is_temporary()) | 426 if (!is_temporary()) |
| 424 OpenDownload(); | 427 OpenDownload(); |
| 425 | 428 |
| 426 auto_opened_ = true; | 429 auto_opened_ = true; |
| 427 UpdateObservers(); | 430 UpdateObservers(); |
| 428 } | 431 } |
| 429 } | 432 } |
| 430 | 433 |
| 431 void DownloadItem::TransitionTo(DownloadState new_state) { | 434 void DownloadItem::TransitionTo(DownloadState new_state) { |
| 432 if (state_ == new_state) | 435 DownloadState old_state = state_; |
| 436 if (old_state == new_state) |
| 433 return; | 437 return; |
| 434 | 438 |
| 439 // Check for disallowed state transitions. |
| 440 CHECK(!(old_state == IN_PROGRESS && new_state == REMOVING)); |
| 441 |
| 435 state_ = new_state; | 442 state_ = new_state; |
| 443 |
| 444 // Do special operations for transitions from an active state. |
| 445 if (old_state == IN_PROGRESS && |
| 446 (new_state == CANCELLED || new_state == INTERRUPTED)) { |
| 447 download_manager_->DownloadStopped(this); |
| 448 StopProgressTimer(); |
| 449 } |
| 450 |
| 436 UpdateObservers(); | 451 UpdateObservers(); |
| 437 } | 452 } |
| 438 | 453 |
| 439 void DownloadItem::UpdateSafetyState() { | 454 void DownloadItem::UpdateSafetyState() { |
| 440 SafetyState updated_value( | 455 SafetyState updated_value( |
| 441 GetSafetyState(state_info_.is_dangerous_file, | 456 GetSafetyState(state_info_.is_dangerous_file, |
| 442 state_info_.is_dangerous_url)); | 457 state_info_.is_dangerous_url)); |
| 443 if (updated_value != safety_state_) { | 458 if (updated_value != safety_state_) { |
| 444 safety_state_ = updated_value; | 459 safety_state_ = updated_value; |
| 445 UpdateObservers(); | 460 UpdateObservers(); |
| 446 } | 461 } |
| 447 } | 462 } |
| 448 | 463 |
| 449 void DownloadItem::UpdateTarget() { | 464 void DownloadItem::UpdateTarget() { |
| 450 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 465 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 451 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 466 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 452 | 467 |
| 453 if (state_info_.target_name.value().empty()) | 468 if (state_info_.target_name.value().empty()) |
| 454 state_info_.target_name = full_path_.BaseName(); | 469 state_info_.target_name = full_path_.BaseName(); |
| 455 } | 470 } |
| 456 | 471 |
| 457 void DownloadItem::Interrupted(int64 size, net::Error net_error) { | 472 void DownloadItem::Interrupt(int64 size, net::Error net_error) { |
| 458 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 473 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 459 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 474 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 475 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
| 460 | 476 |
| 477 // Small downloads might be complete before we have a chance to run. |
| 461 if (!IsInProgress()) | 478 if (!IsInProgress()) |
| 462 return; | 479 return; |
| 463 | 480 |
| 481 UpdateSize(size); |
| 464 last_error_ = net_error; | 482 last_error_ = net_error; |
| 465 UpdateSize(size); | 483 |
| 466 StopProgressTimer(); | 484 TransitionTo(INTERRUPTED); |
| 485 |
| 467 download_stats::RecordDownloadInterrupted(net_error, | 486 download_stats::RecordDownloadInterrupted(net_error, |
| 468 received_bytes_, | 487 received_bytes_, |
| 469 total_bytes_); | 488 total_bytes_); |
| 470 TransitionTo(INTERRUPTED); | 489 |
| 490 // History insertion is the point at which we have finalized download |
| 491 // details and persist them if something goes wrong. Before history |
| 492 // insertion, interrupt or cancel results in download removal. |
| 493 if (db_handle() == DownloadItem::kUninitializedHandle) { |
| 494 download_manager_->RemoveDownload(this); |
| 495 // We are now deleted; no further code should be executed on this |
| 496 // object. |
| 497 } |
| 471 } | 498 } |
| 472 | 499 |
| 473 void DownloadItem::Delete(DeleteReason reason) { | 500 void DownloadItem::Delete(DeleteReason reason) { |
| 474 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 501 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 475 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 502 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 476 | 503 |
| 477 switch (reason) { | 504 switch (reason) { |
| 478 case DELETE_DUE_TO_USER_DISCARD: | 505 case DELETE_DUE_TO_USER_DISCARD: |
| 479 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), | 506 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), |
| 480 DANGEROUS_TYPE_MAX); | 507 DANGEROUS_TYPE_MAX); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 491 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); | 518 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); |
| 492 Remove(); | 519 Remove(); |
| 493 // We have now been deleted. | 520 // We have now been deleted. |
| 494 } | 521 } |
| 495 | 522 |
| 496 void DownloadItem::Remove() { | 523 void DownloadItem::Remove() { |
| 497 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 524 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 498 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 525 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 499 | 526 |
| 500 download_manager_->AssertQueueStateConsistent(this); | 527 download_manager_->AssertQueueStateConsistent(this); |
| 501 Cancel(true); | 528 if (IsInProgress()) { |
| 529 TransitionTo(CANCELLED); |
| 530 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); |
| 531 } |
| 502 download_manager_->AssertQueueStateConsistent(this); | 532 download_manager_->AssertQueueStateConsistent(this); |
| 533 download_stats::RecordDownloadCount(download_stats::REMOVED_COUNT); |
| 503 | 534 |
| 504 TransitionTo(REMOVING); | 535 download_manager_->RemoveDownload(this); |
| 505 download_manager_->RemoveDownload(db_handle_); | |
| 506 // We have now been deleted. | 536 // We have now been deleted. |
| 507 } | 537 } |
| 508 | 538 |
| 509 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { | 539 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { |
| 510 if (total_bytes_ <= 0) | 540 if (total_bytes_ <= 0) |
| 511 return false; // We never received the content_length for this download. | 541 return false; // We never received the content_length for this download. |
| 512 | 542 |
| 513 int64 speed = CurrentSpeed(); | 543 int64 speed = CurrentSpeed(); |
| 514 if (speed == 0) | 544 if (speed == 0) |
| 515 return false; | 545 return false; |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 793 state_info_.target_name.value().c_str(), | 823 state_info_.target_name.value().c_str(), |
| 794 full_path().value().c_str()); | 824 full_path().value().c_str()); |
| 795 } else { | 825 } else { |
| 796 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 826 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
| 797 } | 827 } |
| 798 | 828 |
| 799 description += " }"; | 829 description += " }"; |
| 800 | 830 |
| 801 return description; | 831 return description; |
| 802 } | 832 } |
| OLD | NEW |