OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_manager_impl.h" | 5 #include "content/browser/download/download_manager_impl.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 << " size = " << size; | 535 << " size = " << size; |
536 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 536 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
537 | 537 |
538 // If it's not in active_downloads_, that means it was cancelled; just | 538 // If it's not in active_downloads_, that means it was cancelled; just |
539 // ignore the notification. | 539 // ignore the notification. |
540 if (active_downloads_.count(download_id) == 0) | 540 if (active_downloads_.count(download_id) == 0) |
541 return; | 541 return; |
542 | 542 |
543 DownloadItem* download = active_downloads_[download_id]; | 543 DownloadItem* download = active_downloads_[download_id]; |
544 download->OnAllDataSaved(size, hash); | 544 download->OnAllDataSaved(size, hash); |
545 | 545 MaybeCompleteDownload(download); |
546 download->MaybeCompleteDownload(); | |
547 } | 546 } |
548 | 547 |
549 void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const { | 548 void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const { |
550 if (download->GetState() == DownloadItem::REMOVING) { | 549 if (download->GetState() == DownloadItem::REMOVING) { |
551 DCHECK(!ContainsKey(downloads_, download)); | 550 DCHECK(!ContainsKey(downloads_, download)); |
552 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 551 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
553 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle())); | 552 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle())); |
554 return; | 553 return; |
555 } | 554 } |
556 | 555 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 // transition on the DownloadItem. | 626 // transition on the DownloadItem. |
628 | 627 |
629 // Confirm we're in the proper set of states to be here; | 628 // Confirm we're in the proper set of states to be here; |
630 // have all data, have a history handle, (validated or safe). | 629 // have all data, have a history handle, (validated or safe). |
631 DCHECK(download->IsInProgress()); | 630 DCHECK(download->IsInProgress()); |
632 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); | 631 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); |
633 DCHECK(download->AllDataSaved()); | 632 DCHECK(download->AllDataSaved()); |
634 DCHECK(download->IsPersisted()); | 633 DCHECK(download->IsPersisted()); |
635 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle())); | 634 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle())); |
636 | 635 |
637 // Give the delegate a chance to override. | 636 // Give the delegate a chance to override. It's ok to keep re-setting the |
638 if (!delegate_->ShouldCompleteDownload(download)) | 637 // delegate's |complete_callback| cb as long as there isn't another call-point |
| 638 // trying to set it to a different cb. TODO(benjhayden): Change the callback |
| 639 // to point directly to the item instead of |this| when DownloadItem supports |
| 640 // weak-ptrs. |
| 641 if (!delegate_->ShouldCompleteDownload(download, base::Bind( |
| 642 &DownloadManagerImpl::MaybeCompleteDownloadById, |
| 643 this, download->GetId()))) |
639 return; | 644 return; |
640 | 645 |
641 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " | 646 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
642 << download->DebugString(false); | 647 << download->DebugString(false); |
643 | 648 |
644 delegate_->UpdateItemInPersistentStore(download); | 649 delegate_->UpdateItemInPersistentStore(download); |
645 | 650 |
646 // Finish the download. | 651 // Finish the download. |
647 download->OnDownloadCompleting(file_manager_); | 652 download->OnDownloadCompleting(file_manager_); |
648 } | 653 } |
649 | 654 |
| 655 void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { |
| 656 DownloadItem* download_item = GetActiveDownload(download_id); |
| 657 if (download_item != NULL) |
| 658 MaybeCompleteDownload(download_item); |
| 659 } |
| 660 |
650 void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) { | 661 void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) { |
651 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 662 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
652 DCHECK(download); | 663 DCHECK(download); |
653 delegate_->UpdateItemInPersistentStore(download); | 664 delegate_->UpdateItemInPersistentStore(download); |
654 active_downloads_.erase(download->GetId()); | 665 active_downloads_.erase(download->GetId()); |
655 AssertStateConsistent(download); | 666 AssertStateConsistent(download); |
656 } | 667 } |
657 | 668 |
658 void DownloadManagerImpl::OnDownloadRenamedToFinalName( | 669 void DownloadManagerImpl::OnDownloadRenamedToFinalName( |
659 int download_id, | 670 int download_id, |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1157 if (it->second->IsComplete() && !it->second->GetOpened()) | 1168 if (it->second->IsComplete() && !it->second->GetOpened()) |
1158 ++num_unopened; | 1169 ++num_unopened; |
1159 } | 1170 } |
1160 download_stats::RecordOpensOutstanding(num_unopened); | 1171 download_stats::RecordOpensOutstanding(num_unopened); |
1161 } | 1172 } |
1162 | 1173 |
1163 void DownloadManagerImpl::SetFileManagerForTesting( | 1174 void DownloadManagerImpl::SetFileManagerForTesting( |
1164 DownloadFileManager* file_manager) { | 1175 DownloadFileManager* file_manager) { |
1165 file_manager_ = file_manager; | 1176 file_manager_ = file_manager; |
1166 } | 1177 } |
OLD | NEW |