Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: chrome/browser/download/download_item.cc

Issue 7294013: Modified cancel and interrupt processing to avoid race with history. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged to TOT. Again :-}. Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/download/download_item.h ('k') | chrome/browser/download/download_item_model.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 335 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
336 336
337 received_bytes_ = bytes_so_far; 337 received_bytes_ = bytes_so_far;
338 338
339 // If we've received more data than we were expecting (bad server info?), 339 // If we've received more data than we were expecting (bad server info?),
340 // revert to 'unknown size mode'. 340 // revert to 'unknown size mode'.
341 if (received_bytes_ > total_bytes_) 341 if (received_bytes_ > total_bytes_)
342 total_bytes_ = 0; 342 total_bytes_ = 0;
343 } 343 }
344 344
345 void DownloadItem::StopInternal(DownloadState target_state) {
346 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
347 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
348 DCHECK(target_state == CANCELLED || target_state == INTERRUPTED);
349
350 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
351 DCHECK(IsInProgress());
352
353 state_ = target_state;
354 download_manager_->DownloadStopped(download_id_);
355 UpdateObservers();
356 StopProgressTimer();
357 }
358
345 void DownloadItem::StartProgressTimer() { 359 void DownloadItem::StartProgressTimer() {
346 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 360 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
347 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 361 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
348 362
349 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, 363 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this,
350 &DownloadItem::UpdateObservers); 364 &DownloadItem::UpdateObservers);
351 } 365 }
352 366
353 void DownloadItem::StopProgressTimer() { 367 void DownloadItem::StopProgressTimer() {
354 // 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
365 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 379 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
366 380
367 if (!IsInProgress()) { 381 if (!IsInProgress()) {
368 NOTREACHED(); 382 NOTREACHED();
369 return; 383 return;
370 } 384 }
371 UpdateSize(bytes_so_far); 385 UpdateSize(bytes_so_far);
372 UpdateObservers(); 386 UpdateObservers();
373 } 387 }
374 388
375 // Triggered by a user action. 389
376 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 download item is persisted
392 // into the history system will result in a removal of the Download
393 // from the system.
394 void DownloadItem::Cancel() {
377 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 395 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
378 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 396 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
397 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
379 398
380 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); 399 // Small downloads might be complete before we have a chance to run.
381 if (!IsPartialDownload()) { 400 if (!IsInProgress())
382 // Small downloads might be complete before this method has
383 // a chance to run.
384 return; 401 return;
385 } 402
403 StopInternal(CANCELLED);
386 404
387 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); 405 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT);
388 406
389 state_ = CANCELLED; 407 // History insertion is the point at which we have finalized download
390 UpdateObservers(); 408 // details and persist them if something goes wrong. Before history
391 StopProgressTimer(); 409 // insertion, interrupt or cancel results in download removal.
392 if (update_history) 410 if (db_handle() == DownloadHistory::kUninitializedHandle) {
393 download_manager_->DownloadCancelled(download_id_); 411 download_manager_->RemoveDownload(this);
412 // We are now deleted; no further code should be executed on this
413 // object.
414 }
394 } 415 }
395 416
417
396 void DownloadItem::MarkAsComplete() { 418 void DownloadItem::MarkAsComplete() {
397 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 419 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
398 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 420 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
399 421
400 DCHECK(all_data_saved_); 422 DCHECK(all_data_saved_);
401 state_ = COMPLETE; 423 state_ = COMPLETE;
402 UpdateObservers(); 424 UpdateObservers();
403 } 425 }
404 426
405 void DownloadItem::OnAllDataSaved(int64 size) { 427 void DownloadItem::OnAllDataSaved(int64 size) {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 registrar_.Remove(this, 507 registrar_.Remove(this,
486 chrome::NOTIFICATION_CRX_INSTALLER_DONE, 508 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
487 source); 509 source);
488 510
489 auto_opened_ = true; 511 auto_opened_ = true;
490 DCHECK(all_data_saved_); 512 DCHECK(all_data_saved_);
491 513
492 Completed(); 514 Completed();
493 } 515 }
494 516
495 void DownloadItem::Interrupted(int64 size, int os_error) { 517 void DownloadItem::Interrupt(int64 size, int os_error) {
496 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 518 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
497 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 519 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
520 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
498 521
522 // Small downloads might be complete before we have a chance to run.
499 if (!IsInProgress()) 523 if (!IsInProgress())
500 return; 524 return;
501 state_ = INTERRUPTED; 525
526 UpdateSize(size);
502 last_os_error_ = os_error; 527 last_os_error_ = os_error;
503 UpdateSize(size); 528
504 StopProgressTimer(); 529 StopInternal(INTERRUPTED);
530
505 download_util::RecordDownloadInterrupted(os_error, 531 download_util::RecordDownloadInterrupted(os_error,
506 received_bytes_, 532 received_bytes_,
507 total_bytes_); 533 total_bytes_);
508 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; no further code should be executed on this
541 // object.
542 }
509 } 543 }
510 544
511 void DownloadItem::Delete(DeleteReason reason) { 545 void DownloadItem::Delete(DeleteReason reason) {
512 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 546 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
513 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 547 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
514 548
515 switch (reason) { 549 switch (reason) {
516 case DELETE_DUE_TO_USER_DISCARD: 550 case DELETE_DUE_TO_USER_DISCARD:
517 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(), 551 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", GetDangerType(),
518 DANGEROUS_TYPE_MAX); 552 DANGEROUS_TYPE_MAX);
(...skipping 10 matching lines...) Expand all
529 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); 563 NewRunnableFunction(&DeleteDownloadedFile, full_path_));
530 Remove(); 564 Remove();
531 // We have now been deleted. 565 // We have now been deleted.
532 } 566 }
533 567
534 void DownloadItem::Remove() { 568 void DownloadItem::Remove() {
535 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 569 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
536 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 570 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
537 571
538 download_manager_->AssertQueueStateConsistent(this); 572 download_manager_->AssertQueueStateConsistent(this);
539 Cancel(true); 573 if (IsInProgress()) {
574 StopInternal(CANCELLED);
575 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT);
576 }
540 download_manager_->AssertQueueStateConsistent(this); 577 download_manager_->AssertQueueStateConsistent(this);
578 download_util::RecordDownloadCount(download_util::REMOVED_COUNT);
541 579
542 state_ = REMOVING; 580 download_manager_->RemoveDownload(this);
543 download_manager_->RemoveDownload(db_handle_);
544 // We have now been deleted. 581 // We have now been deleted.
545 } 582 }
546 583
547 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { 584 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const {
548 if (total_bytes_ <= 0) 585 if (total_bytes_ <= 0)
549 return false; // We never received the content_length for this download. 586 return false; // We never received the content_length for this download.
550 587
551 int64 speed = CurrentSpeed(); 588 int64 speed = CurrentSpeed();
552 if (speed == 0) 589 if (speed == 0)
553 return false; 590 return false;
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 state_info_.target_name.value().c_str(), 874 state_info_.target_name.value().c_str(),
838 full_path().value().c_str()); 875 full_path().value().c_str());
839 } else { 876 } else {
840 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); 877 description += base::StringPrintf(" url = \"%s\"", url_list.c_str());
841 } 878 }
842 879
843 description += " }"; 880 description += " }";
844 881
845 return description; 882 return description;
846 } 883 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_item.h ('k') | chrome/browser/download/download_item_model.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698