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 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 download->Cancel(false); | 310 download->Cancel(false); |
311 if (delegate_) | 311 if (delegate_) |
312 delegate_->UpdateItemInPersistentStore(download); | 312 delegate_->UpdateItemInPersistentStore(download); |
313 } | 313 } |
314 } | 314 } |
315 | 315 |
316 // At this point, all dangerous downloads have had their files removed | 316 // At this point, all dangerous downloads have had their files removed |
317 // and all in progress downloads have been cancelled. We can now delete | 317 // and all in progress downloads have been cancelled. We can now delete |
318 // anything left. | 318 // anything left. |
319 | 319 |
320 // Copy downloads_ to separate container so as not to set off checks | |
321 // in DownloadItem destruction. | |
322 DownloadMap downloads_to_delete; | |
323 downloads_to_delete.swap(downloads_); | |
324 | |
325 active_downloads_.clear(); | 320 active_downloads_.clear(); |
326 STLDeleteValues(&downloads_to_delete); | 321 STLDeleteValues(&downloads_); |
| 322 downloads_.clear(); |
327 | 323 |
328 // We'll have nothing more to report to the observers after this point. | 324 // We'll have nothing more to report to the observers after this point. |
329 observers_.Clear(); | 325 observers_.Clear(); |
330 | 326 |
331 file_manager_ = NULL; | 327 file_manager_ = NULL; |
332 if (delegate_) | 328 if (delegate_) |
333 delegate_->Shutdown(); | 329 delegate_->Shutdown(); |
334 } | 330 } |
335 | 331 |
336 void DownloadManagerImpl::GetTemporaryDownloads( | 332 void DownloadManagerImpl::GetTemporaryDownloads( |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 if (active_downloads_.count(download_id) == 0) | 625 if (active_downloads_.count(download_id) == 0) |
630 return; | 626 return; |
631 | 627 |
632 DownloadItemImpl* download = active_downloads_[download_id]; | 628 DownloadItemImpl* download = active_downloads_[download_id]; |
633 download->OnAllDataSaved(size, hash); | 629 download->OnAllDataSaved(size, hash); |
634 MaybeCompleteDownload(download); | 630 MaybeCompleteDownload(download); |
635 } | 631 } |
636 | 632 |
637 void DownloadManagerImpl::AssertStateConsistent( | 633 void DownloadManagerImpl::AssertStateConsistent( |
638 DownloadItemImpl* download) const { | 634 DownloadItemImpl* download) const { |
639 if (download->GetState() == DownloadItem::REMOVING) { | |
640 DCHECK(!ContainsKey(downloads_, download->GetId())); | |
641 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | |
642 return; | |
643 } | |
644 | |
645 // Should be in downloads_ if we're not REMOVING. | |
646 CHECK(ContainsKey(downloads_, download->GetId())); | 635 CHECK(ContainsKey(downloads_, download->GetId())); |
647 | 636 |
648 int64 state = download->GetState(); | 637 int64 state = download->GetState(); |
649 base::debug::Alias(&state); | 638 base::debug::Alias(&state); |
650 if (ContainsKey(active_downloads_, download->GetId())) { | 639 if (ContainsKey(active_downloads_, download->GetId())) { |
651 if (download->IsPersisted()) | 640 if (download->IsPersisted()) |
652 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 641 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
653 if (DownloadItem::IN_PROGRESS != download->GetState()) | 642 if (DownloadItem::IN_PROGRESS != download->GetState()) |
654 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle()); | 643 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle()); |
655 } | 644 } |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
794 return delegate_ && delegate_->GenerateFileHash(); | 783 return delegate_ && delegate_->GenerateFileHash(); |
795 } | 784 } |
796 | 785 |
797 int DownloadManagerImpl::RemoveDownloadItems( | 786 int DownloadManagerImpl::RemoveDownloadItems( |
798 const DownloadItemImplVector& pending_deletes) { | 787 const DownloadItemImplVector& pending_deletes) { |
799 if (pending_deletes.empty()) | 788 if (pending_deletes.empty()) |
800 return 0; | 789 return 0; |
801 | 790 |
802 // Delete from internal maps. | 791 // Delete from internal maps. |
803 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 792 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
804 it != pending_deletes.end(); | 793 it != pending_deletes.end(); |
805 ++it) { | 794 ++it) { |
806 DownloadItemImpl* download = *it; | 795 DownloadItemImpl* download = *it; |
807 DCHECK(download); | 796 DCHECK(download); |
808 downloads_.erase(download->GetId()); | 797 int32 download_id = download->GetId(); |
| 798 delete download; |
| 799 downloads_.erase(download_id); |
809 } | 800 } |
810 | |
811 // Tell observers to refresh their views. | |
812 NotifyModelChanged(); | 801 NotifyModelChanged(); |
813 | 802 return static_cast<int>(pending_deletes.size()); |
814 // Delete the download items themselves. | |
815 const int num_deleted = static_cast<int>(pending_deletes.size()); | |
816 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end()); | |
817 return num_deleted; | |
818 } | 803 } |
819 | 804 |
820 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { | 805 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
821 if (!download || | 806 if (!download || |
822 downloads_.find(download->GetId()) == downloads_.end()) | 807 downloads_.find(download->GetId()) == downloads_.end()) |
823 return; | 808 return; |
824 | 809 |
825 // TODO(benjhayden,rdsmith): Remove this. | 810 // TODO(benjhayden,rdsmith): Remove this. |
826 if (!download->IsPersisted()) | 811 if (!download->IsPersisted()) |
827 return; | 812 return; |
828 | 813 |
829 // Make history update. | 814 // Make history update. |
830 if (delegate_) | 815 if (delegate_) |
831 delegate_->RemoveItemFromPersistentStore(download); | 816 delegate_->RemoveItemFromPersistentStore(download); |
832 | 817 |
833 // Remove from our tables and delete. | 818 // Remove from our tables and delete. |
834 int downloads_count = | 819 int downloads_count = |
835 RemoveDownloadItems(DownloadItemImplVector(1, download)); | 820 RemoveDownloadItems(DownloadItemImplVector(1, download)); |
836 DCHECK_EQ(1, downloads_count); | 821 DCHECK_EQ(1, downloads_count); |
837 } | 822 } |
838 | 823 |
839 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, | 824 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, |
840 base::Time remove_end) { | 825 base::Time remove_end) { |
841 if (delegate_) | 826 if (delegate_) |
842 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); | 827 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); |
843 | 828 |
844 // All downloads visible to the user will be in the history, | |
845 // so scan that map. | |
846 DownloadItemImplVector pending_deletes; | 829 DownloadItemImplVector pending_deletes; |
847 for (DownloadMap::const_iterator it = downloads_.begin(); | 830 for (DownloadMap::const_iterator it = downloads_.begin(); |
848 it != downloads_.end(); | 831 it != downloads_.end(); |
849 ++it) { | 832 ++it) { |
850 DownloadItemImpl* download = it->second; | 833 DownloadItemImpl* download = it->second; |
851 if (download->IsPersisted() && | 834 if (download->IsPersisted() && |
852 download->GetStartTime() >= remove_begin && | 835 download->GetStartTime() >= remove_begin && |
853 (remove_end.is_null() || download->GetStartTime() < remove_end) && | 836 (remove_end.is_null() || download->GetStartTime() < remove_end) && |
854 (download->IsComplete() || download->IsCancelled())) { | 837 (download->IsComplete() || download->IsCancelled())) { |
855 AssertStateConsistent(download); | 838 AssertStateConsistent(download); |
| 839 download->NotifyRemoved(); |
856 pending_deletes.push_back(download); | 840 pending_deletes.push_back(download); |
857 } | 841 } |
858 } | 842 } |
859 return RemoveDownloadItems(pending_deletes); | 843 return RemoveDownloadItems(pending_deletes); |
860 } | 844 } |
861 | 845 |
862 int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) { | 846 int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) { |
863 return RemoveDownloadsBetween(remove_begin, base::Time()); | 847 return RemoveDownloadsBetween(remove_begin, base::Time()); |
864 } | 848 } |
865 | 849 |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1169 void DownloadManagerImpl::DownloadRenamedToFinalName( | 1153 void DownloadManagerImpl::DownloadRenamedToFinalName( |
1170 DownloadItemImpl* download) { | 1154 DownloadItemImpl* download) { |
1171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1172 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 1156 // If the rename failed, we receive an OnDownloadInterrupted() call before we |
1173 // receive the DownloadRenamedToFinalName() call. | 1157 // receive the DownloadRenamedToFinalName() call. |
1174 if (delegate_) { | 1158 if (delegate_) { |
1175 delegate_->UpdatePathForItemInPersistentStore( | 1159 delegate_->UpdatePathForItemInPersistentStore( |
1176 download, download->GetFullPath()); | 1160 download, download->GetFullPath()); |
1177 } | 1161 } |
1178 } | 1162 } |
OLD | NEW |