Chromium Code Reviews| 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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 314 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 315 | 315 |
| 316 received_bytes_ = bytes_so_far; | 316 received_bytes_ = bytes_so_far; |
| 317 | 317 |
| 318 // If we've received more data than we were expecting (bad server info?), | 318 // If we've received more data than we were expecting (bad server info?), |
| 319 // revert to 'unknown size mode'. | 319 // revert to 'unknown size mode'. |
| 320 if (received_bytes_ > total_bytes_) | 320 if (received_bytes_ > total_bytes_) |
| 321 total_bytes_ = 0; | 321 total_bytes_ = 0; |
| 322 } | 322 } |
| 323 | 323 |
| 324 void DownloadItem::StopInternal(DownloadState target_state) { | |
|
benjhayden
2011/09/12 18:13:38
Would you rather do this stuff inside TransitionTo
Randy Smith (Not in Mondays)
2011/09/12 19:43:23
Hmmm. I'm torn. I had originally conceptualized
| |
| 325 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | |
| 326 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 327 DCHECK(target_state == CANCELLED || target_state == INTERRUPTED); | |
|
benjhayden
2011/09/12 18:13:38
Should this be CHECK?
Randy Smith (Not in Mondays)
2011/09/12 19:43:23
Moot (since I'm nuking StopInternal) but I'm not i
Randy Smith (Not in Mondays)
2011/09/12 19:44:36
Sorry, mistyped--I meant "I'm not inclined to make
| |
| 328 | |
| 329 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
| 330 DCHECK(IsInProgress()); | |
|
benjhayden
2011/09/12 18:13:38
Should this be CHECK?
Randy Smith (Not in Mondays)
2011/09/12 19:43:23
See above.
| |
| 331 | |
| 332 TransitionTo(target_state); | |
| 333 download_manager_->DownloadStopped(this); | |
| 334 StopProgressTimer(); | |
| 335 } | |
| 336 | |
| 324 void DownloadItem::StartProgressTimer() { | 337 void DownloadItem::StartProgressTimer() { |
| 325 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 338 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 326 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 339 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 327 | 340 |
| 328 update_timer_.Start(FROM_HERE, | 341 update_timer_.Start(FROM_HERE, |
| 329 base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, | 342 base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, |
| 330 &DownloadItem::UpdateObservers); | 343 &DownloadItem::UpdateObservers); |
| 331 } | 344 } |
| 332 | 345 |
| 333 void DownloadItem::StopProgressTimer() { | 346 void DownloadItem::StopProgressTimer() { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 345 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 358 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 346 | 359 |
| 347 if (!IsInProgress()) { | 360 if (!IsInProgress()) { |
| 348 NOTREACHED(); | 361 NOTREACHED(); |
| 349 return; | 362 return; |
| 350 } | 363 } |
| 351 UpdateSize(bytes_so_far); | 364 UpdateSize(bytes_so_far); |
| 352 UpdateObservers(); | 365 UpdateObservers(); |
| 353 } | 366 } |
| 354 | 367 |
| 355 // Triggered by a user action. | 368 // May be triggered by user action or because of various kinds of error. |
|
benjhayden
2011/09/12 18:13:38
What kinds of errors would go through here instead
Randy Smith (Not in Mondays)
2011/09/12 19:43:23
I think this comment was based on a previous incar
| |
| 356 void DownloadItem::Cancel(bool update_history) { | 369 void DownloadItem::Cancel() { |
| 357 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 370 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 358 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 371 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 372 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
| 359 | 373 |
| 360 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 374 // Small downloads might be complete before we have a chance to run. |
| 361 if (!IsPartialDownload()) { | 375 if (!IsInProgress()) |
| 362 // Small downloads might be complete before this method has | |
| 363 // a chance to run. | |
| 364 return; | 376 return; |
| 365 } | 377 |
| 378 StopInternal(CANCELLED); | |
| 366 | 379 |
| 367 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); | 380 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); |
| 368 | 381 |
| 369 TransitionTo(CANCELLED); | 382 // History insertion is the point at which we have finalized download |
| 370 StopProgressTimer(); | 383 // details and persist them if something goes wrong. Before history |
| 371 if (update_history) | 384 // insertion, interrupt or cancel results in download removal. |
| 372 download_manager_->DownloadCancelledInternal(this); | 385 if (db_handle() == DownloadItem::kUninitializedHandle) { |
| 386 download_manager_->RemoveDownload(this); | |
| 387 // We are now deleted; no further code should be executed on this | |
| 388 // object. | |
| 389 } | |
| 373 } | 390 } |
| 374 | 391 |
| 375 void DownloadItem::MarkAsComplete() { | 392 void DownloadItem::MarkAsComplete() { |
| 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)); |
| 378 | 395 |
| 379 DCHECK(all_data_saved_); | 396 DCHECK(all_data_saved_); |
| 380 TransitionTo(COMPLETE); | 397 TransitionTo(COMPLETE); |
| 381 } | 398 } |
| 382 | 399 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 446 } | 463 } |
| 447 | 464 |
| 448 void DownloadItem::UpdateTarget() { | 465 void DownloadItem::UpdateTarget() { |
| 449 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 466 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 450 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 467 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 451 | 468 |
| 452 if (state_info_.target_name.value().empty()) | 469 if (state_info_.target_name.value().empty()) |
| 453 state_info_.target_name = full_path_.BaseName(); | 470 state_info_.target_name = full_path_.BaseName(); |
| 454 } | 471 } |
| 455 | 472 |
| 456 void DownloadItem::Interrupted(int64 size, net::Error net_error) { | 473 void DownloadItem::Interrupt(int64 size, net::Error net_error) { |
| 457 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 474 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 458 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 475 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 476 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | |
| 459 | 477 |
| 478 // Small downloads might be complete before we have a chance to run. | |
| 460 if (!IsInProgress()) | 479 if (!IsInProgress()) |
| 461 return; | 480 return; |
| 462 | 481 |
| 482 UpdateSize(size); | |
| 463 last_error_ = net_error; | 483 last_error_ = net_error; |
| 464 UpdateSize(size); | 484 |
| 465 StopProgressTimer(); | 485 StopInternal(INTERRUPTED); |
| 486 | |
| 466 download_stats::RecordDownloadInterrupted(net_error, | 487 download_stats::RecordDownloadInterrupted(net_error, |
| 467 received_bytes_, | 488 received_bytes_, |
| 468 total_bytes_); | 489 total_bytes_); |
| 469 TransitionTo(INTERRUPTED); | 490 |
| 491 // History insertion is the point at which we have finalized download | |
| 492 // details and persist them if something goes wrong. Before history | |
| 493 // insertion, interrupt or cancel results in download removal. | |
| 494 if (db_handle() == DownloadItem::kUninitializedHandle) { | |
| 495 download_manager_->RemoveDownload(this); | |
| 496 // We are now deleted; no further code should be executed on this | |
| 497 // object. | |
| 498 } | |
| 470 } | 499 } |
| 471 | 500 |
| 472 void DownloadItem::Delete(DeleteReason reason) { | 501 void DownloadItem::Delete(DeleteReason reason) { |
| 473 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 502 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 474 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 503 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 475 | 504 |
| 476 switch (reason) { | 505 switch (reason) { |
| 477 case DELETE_DUE_TO_USER_DISCARD: | 506 case DELETE_DUE_TO_USER_DISCARD: |
| 478 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), | 507 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), |
| 479 DANGEROUS_TYPE_MAX); | 508 DANGEROUS_TYPE_MAX); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 490 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); | 519 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); |
| 491 Remove(); | 520 Remove(); |
| 492 // We have now been deleted. | 521 // We have now been deleted. |
| 493 } | 522 } |
| 494 | 523 |
| 495 void DownloadItem::Remove() { | 524 void DownloadItem::Remove() { |
| 496 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 525 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 497 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 526 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 498 | 527 |
| 499 download_manager_->AssertQueueStateConsistent(this); | 528 download_manager_->AssertQueueStateConsistent(this); |
| 500 Cancel(true); | 529 if (IsInProgress()) { |
| 530 StopInternal(CANCELLED); | |
| 531 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); | |
|
benjhayden
2011/09/12 18:13:38
Want to do this in StopInternal()?
Randy Smith (Not in Mondays)
2011/09/12 19:43:23
I'm disinclined. I'd have to have a conditional (
| |
| 532 } | |
| 501 download_manager_->AssertQueueStateConsistent(this); | 533 download_manager_->AssertQueueStateConsistent(this); |
| 534 download_stats::RecordDownloadCount(download_stats::REMOVED_COUNT); | |
| 502 | 535 |
| 503 TransitionTo(REMOVING); | 536 download_manager_->RemoveDownload(this); |
| 504 download_manager_->RemoveDownload(db_handle_); | |
| 505 // We have now been deleted. | 537 // We have now been deleted. |
| 506 } | 538 } |
| 507 | 539 |
| 508 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { | 540 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { |
| 509 if (total_bytes_ <= 0) | 541 if (total_bytes_ <= 0) |
| 510 return false; // We never received the content_length for this download. | 542 return false; // We never received the content_length for this download. |
| 511 | 543 |
| 512 int64 speed = CurrentSpeed(); | 544 int64 speed = CurrentSpeed(); |
| 513 if (speed == 0) | 545 if (speed == 0) |
| 514 return false; | 546 return false; |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 797 state_info_.target_name.value().c_str(), | 829 state_info_.target_name.value().c_str(), |
| 798 full_path().value().c_str()); | 830 full_path().value().c_str()); |
| 799 } else { | 831 } else { |
| 800 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 832 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
| 801 } | 833 } |
| 802 | 834 |
| 803 description += " }"; | 835 description += " }"; |
| 804 | 836 |
| 805 return description; | 837 return description; |
| 806 } | 838 } |
| OLD | NEW |