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 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 << " size = " << size; | 541 << " size = " << size; |
542 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 542 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
543 | 543 |
544 // If it's not in active_downloads_, that means it was cancelled; just | 544 // If it's not in active_downloads_, that means it was cancelled; just |
545 // ignore the notification. | 545 // ignore the notification. |
546 if (active_downloads_.count(download_id) == 0) | 546 if (active_downloads_.count(download_id) == 0) |
547 return; | 547 return; |
548 | 548 |
549 DownloadItem* download = active_downloads_[download_id]; | 549 DownloadItem* download = active_downloads_[download_id]; |
550 download->OnAllDataSaved(size, hash); | 550 download->OnAllDataSaved(size, hash); |
551 | 551 MaybeCompleteDownload(download); |
552 download->MaybeCompleteDownload(); | |
553 } | 552 } |
554 | 553 |
555 void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const { | 554 void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const { |
556 if (download->GetState() == DownloadItem::REMOVING) { | 555 if (download->GetState() == DownloadItem::REMOVING) { |
557 DCHECK(!ContainsKey(downloads_, download)); | 556 DCHECK(!ContainsKey(downloads_, download)); |
558 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 557 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
559 DCHECK(!ContainsKey(in_progress_, download->GetId())); | 558 DCHECK(!ContainsKey(in_progress_, download->GetId())); |
560 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle())); | 559 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle())); |
561 return; | 560 return; |
562 } | 561 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 // transition on the DownloadItem. | 633 // transition on the DownloadItem. |
635 | 634 |
636 // Confirm we're in the proper set of states to be here; | 635 // Confirm we're in the proper set of states to be here; |
637 // in in_progress_, have all data, have a history handle, (validated or safe). | 636 // in in_progress_, have all data, have a history handle, (validated or safe). |
638 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); | 637 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); |
639 DCHECK_EQ(1u, in_progress_.count(download->GetId())); | 638 DCHECK_EQ(1u, in_progress_.count(download->GetId())); |
640 DCHECK(download->AllDataSaved()); | 639 DCHECK(download->AllDataSaved()); |
641 DCHECK(download->IsPersisted()); | 640 DCHECK(download->IsPersisted()); |
642 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle())); | 641 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle())); |
643 | 642 |
644 // Give the delegate a chance to override. | 643 // Give the delegate a chance to override. It's ok to keep re-setting the |
645 if (!delegate_->ShouldCompleteDownload(download)) | 644 // delegate's |complete_callback| cb as long as there isn't another call-point |
| 645 // trying to set it to a different cb. TODO(benjhayden): Change the callback |
| 646 // to point directly to the item instead of |this| when DownloadItem supports |
| 647 // weak-ptrs. |
| 648 if (!delegate_->ShouldCompleteDownload(download, base::Bind( |
| 649 &DownloadManagerImpl::MaybeCompleteDownloadById, |
| 650 this, download->GetId()))) |
646 return; | 651 return; |
647 | 652 |
648 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " | 653 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
649 << download->DebugString(false); | 654 << download->DebugString(false); |
650 | 655 |
651 // Remove the id from in_progress | 656 // Remove the id from in_progress |
652 in_progress_.erase(download->GetId()); | 657 in_progress_.erase(download->GetId()); |
653 | 658 |
654 delegate_->UpdateItemInPersistentStore(download); | 659 delegate_->UpdateItemInPersistentStore(download); |
655 | 660 |
656 // Finish the download. | 661 // Finish the download. |
657 download->OnDownloadCompleting(file_manager_); | 662 download->OnDownloadCompleting(file_manager_); |
658 } | 663 } |
659 | 664 |
| 665 void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { |
| 666 DownloadItem* download_item = GetActiveDownload(download_id); |
| 667 if (download_item != NULL) |
| 668 MaybeCompleteDownload(download_item); |
| 669 } |
| 670 |
660 void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) { | 671 void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) { |
661 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 672 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
662 DCHECK(download); | 673 DCHECK(download); |
663 delegate_->UpdateItemInPersistentStore(download); | 674 delegate_->UpdateItemInPersistentStore(download); |
664 active_downloads_.erase(download->GetId()); | 675 active_downloads_.erase(download->GetId()); |
665 AssertStateConsistent(download); | 676 AssertStateConsistent(download); |
666 } | 677 } |
667 | 678 |
668 void DownloadManagerImpl::OnDownloadRenamedToFinalName( | 679 void DownloadManagerImpl::OnDownloadRenamedToFinalName( |
669 int download_id, | 680 int download_id, |
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1169 if (it->second->IsComplete() && !it->second->GetOpened()) | 1180 if (it->second->IsComplete() && !it->second->GetOpened()) |
1170 ++num_unopened; | 1181 ++num_unopened; |
1171 } | 1182 } |
1172 download_stats::RecordOpensOutstanding(num_unopened); | 1183 download_stats::RecordOpensOutstanding(num_unopened); |
1173 } | 1184 } |
1174 | 1185 |
1175 void DownloadManagerImpl::SetFileManagerForTesting( | 1186 void DownloadManagerImpl::SetFileManagerForTesting( |
1176 DownloadFileManager* file_manager) { | 1187 DownloadFileManager* file_manager) { |
1177 file_manager_ = file_manager; | 1188 file_manager_ = file_manager; |
1178 } | 1189 } |
OLD | NEW |