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 "chrome/browser/download/download_item.h" | 5 #include "chrome/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 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 334 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
335 | 335 |
336 received_bytes_ = bytes_so_far; | 336 received_bytes_ = bytes_so_far; |
337 | 337 |
338 // If we've received more data than we were expecting (bad server info?), | 338 // If we've received more data than we were expecting (bad server info?), |
339 // revert to 'unknown size mode'. | 339 // revert to 'unknown size mode'. |
340 if (received_bytes_ > total_bytes_) | 340 if (received_bytes_ > total_bytes_) |
341 total_bytes_ = 0; | 341 total_bytes_ = 0; |
342 } | 342 } |
343 | 343 |
344 void DownloadItem::StopInternal(DownloadState target_state) { | |
345 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | |
346 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
347 | |
348 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
349 DCHECK(IsInProgress()); | |
350 | |
351 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); | |
Randy Smith (Not in Mondays)
2011/06/30 23:05:13
This recording is incorrect--it should be only in
Randy Smith (Not in Mondays)
2011/07/05 20:28:44
Done.
| |
352 | |
353 state_ = target_state; | |
354 download_manager_->DownloadStopped(download_id_); | |
355 UpdateObservers(); | |
356 StopProgressTimer(); | |
357 } | |
358 | |
344 void DownloadItem::StartProgressTimer() { | 359 void DownloadItem::StartProgressTimer() { |
345 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 360 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
346 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 361 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
347 | 362 |
348 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, | 363 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, |
349 &DownloadItem::UpdateObservers); | 364 &DownloadItem::UpdateObservers); |
350 } | 365 } |
351 | 366 |
352 void DownloadItem::StopProgressTimer() { | 367 void DownloadItem::StopProgressTimer() { |
353 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 368 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
(...skipping 10 matching lines...) Expand all Loading... | |
364 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 379 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
365 | 380 |
366 if (!IsInProgress()) { | 381 if (!IsInProgress()) { |
367 NOTREACHED(); | 382 NOTREACHED(); |
368 return; | 383 return; |
369 } | 384 } |
370 UpdateSize(bytes_so_far); | 385 UpdateSize(bytes_so_far); |
371 UpdateObservers(); | 386 UpdateObservers(); |
372 } | 387 } |
373 | 388 |
374 // Triggered by a user action. | 389 |
375 void DownloadItem::Cancel(bool update_history) { | 390 // May be triggered by user action or because of various kinds of error. |
391 // Note that a cancel that occurs before a downoad item is persisted | |
Randy Smith (Not in Mondays)
2011/06/30 23:05:13
"download"
Randy Smith (Not in Mondays)
2011/07/05 20:28:44
Done.
| |
392 // into the history system will result in a removal of the Download | |
393 // from the system. | |
394 void DownloadItem::Cancel() { | |
376 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 395 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
377 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 396 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
397 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
378 | 398 |
379 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 399 // Small downloads might be complete before we have a chance to run. |
380 if (!IsPartialDownload()) { | 400 if (!IsInProgress()) |
381 // Small downloads might be complete before this method has | |
382 // a chance to run. | |
383 return; | 401 return; |
384 } | 402 |
403 StopInternal(CANCELLED); | |
385 | 404 |
386 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); | 405 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); |
387 | 406 |
388 state_ = CANCELLED; | 407 // History insertion is the point at which we have finalized download |
389 UpdateObservers(); | 408 // details and persist them if something goes wrong. Before history |
390 StopProgressTimer(); | 409 // insertion, interrupt or cancel results in download removal. |
391 if (update_history) | 410 if (db_handle() == DownloadHistory::kUninitializedHandle) { |
392 download_manager_->DownloadCancelled(download_id_); | 411 download_manager_->RemoveDownload(this); |
412 // We are now deleted. | |
413 return; | |
414 } | |
393 } | 415 } |
394 | 416 |
417 | |
395 void DownloadItem::MarkAsComplete() { | 418 void DownloadItem::MarkAsComplete() { |
396 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 419 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
397 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 420 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
398 | 421 |
399 DCHECK(all_data_saved_); | 422 DCHECK(all_data_saved_); |
400 state_ = COMPLETE; | 423 state_ = COMPLETE; |
401 UpdateObservers(); | 424 UpdateObservers(); |
402 } | 425 } |
403 | 426 |
404 void DownloadItem::OnAllDataSaved(int64 size) { | 427 void DownloadItem::OnAllDataSaved(int64 size) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
484 registrar_.Remove(this, | 507 registrar_.Remove(this, |
485 NotificationType::CRX_INSTALLER_DONE, | 508 NotificationType::CRX_INSTALLER_DONE, |
486 source); | 509 source); |
487 | 510 |
488 auto_opened_ = true; | 511 auto_opened_ = true; |
489 DCHECK(all_data_saved_); | 512 DCHECK(all_data_saved_); |
490 | 513 |
491 Completed(); | 514 Completed(); |
492 } | 515 } |
493 | 516 |
494 void DownloadItem::Interrupted(int64 size, int os_error) { | 517 void DownloadItem::Interrupt(int64 size, int os_error) { |
495 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 518 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
496 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 519 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
520 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
497 | 521 |
522 // Small downloads might be complete before we have a chance to run. | |
498 if (!IsInProgress()) | 523 if (!IsInProgress()) |
499 return; | 524 return; |
500 state_ = INTERRUPTED; | 525 |
526 UpdateSize(size); | |
501 last_os_error_ = os_error; | 527 last_os_error_ = os_error; |
502 UpdateSize(size); | 528 |
503 StopProgressTimer(); | 529 StopInternal(INTERRUPTED); |
530 | |
504 download_util::RecordDownloadInterrupted(os_error, | 531 download_util::RecordDownloadInterrupted(os_error, |
505 received_bytes_, | 532 received_bytes_, |
506 total_bytes_); | 533 total_bytes_); |
507 UpdateObservers(); | 534 |
535 // History insertion is the point at which we have finalized download | |
536 // details and persist them if something goes wrong. Before history | |
537 // insertion, interrupt or cancel results in download removal. | |
538 if (db_handle() == DownloadHistory::kUninitializedHandle) { | |
539 download_manager_->RemoveDownload(this); | |
540 // We are now deleted. | |
541 return; | |
542 } | |
508 } | 543 } |
509 | 544 |
510 void DownloadItem::Delete(DeleteReason reason) { | 545 void DownloadItem::Delete(DeleteReason reason) { |
511 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 546 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
512 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 547 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
513 | 548 |
514 switch (reason) { | 549 switch (reason) { |
515 case DELETE_DUE_TO_USER_DISCARD: | 550 case DELETE_DUE_TO_USER_DISCARD: |
516 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), | 551 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), |
517 DANGEROUS_TYPE_MAX); | 552 DANGEROUS_TYPE_MAX); |
518 break; | 553 break; |
519 case DELETE_DUE_TO_BROWSER_SHUTDOWN: | 554 case DELETE_DUE_TO_BROWSER_SHUTDOWN: |
520 UMA_HISTOGRAM_ENUMERATION("Download.Discard", GetDangerType(), | 555 UMA_HISTOGRAM_ENUMERATION("Download.Discard", GetDangerType(), |
521 DANGEROUS_TYPE_MAX); | 556 DANGEROUS_TYPE_MAX); |
522 break; | 557 break; |
523 default: | 558 default: |
524 NOTREACHED(); | 559 NOTREACHED(); |
525 } | 560 } |
526 | 561 |
527 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 562 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
528 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); | 563 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); |
529 Remove(); | 564 Remove(); |
530 // We have now been deleted. | 565 // We have now been deleted. |
531 } | 566 } |
532 | 567 |
533 void DownloadItem::Remove() { | 568 void DownloadItem::Remove() { |
Randy Smith (Not in Mondays)
2011/06/30 23:05:13
We need a new UMA count here for removals (or mayb
Randy Smith (Not in Mondays)
2011/07/05 20:28:44
Done.
| |
534 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 569 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
535 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 570 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
536 | 571 |
537 download_manager_->AssertQueueStateConsistent(this); | 572 download_manager_->AssertQueueStateConsistent(this); |
538 Cancel(true); | 573 if (IsInProgress()) |
574 StopInternal(CANCELLED); | |
539 download_manager_->AssertQueueStateConsistent(this); | 575 download_manager_->AssertQueueStateConsistent(this); |
540 | 576 |
541 state_ = REMOVING; | 577 download_manager_->RemoveDownload(this); |
542 download_manager_->RemoveDownload(db_handle_); | |
543 // We have now been deleted. | 578 // We have now been deleted. |
544 } | 579 } |
545 | 580 |
546 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { | 581 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { |
547 if (total_bytes_ <= 0) | 582 if (total_bytes_ <= 0) |
548 return false; // We never received the content_length for this download. | 583 return false; // We never received the content_length for this download. |
549 | 584 |
550 int64 speed = CurrentSpeed(); | 585 int64 speed = CurrentSpeed(); |
551 if (speed == 0) | 586 if (speed == 0) |
552 return false; | 587 return false; |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
836 state_info_.target_name.value().c_str(), | 871 state_info_.target_name.value().c_str(), |
837 full_path().value().c_str()); | 872 full_path().value().c_str()); |
838 } else { | 873 } else { |
839 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 874 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
840 } | 875 } |
841 | 876 |
842 description += " }"; | 877 description += " }"; |
843 | 878 |
844 return description; | 879 return description; |
845 } | 880 } |
OLD | NEW |