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