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