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

Side by Side Diff: content/browser/download/download_manager_impl.cc

Issue 10704026: Reland DownloadItem::Observer::OnDownloadDestroyed() replaces DownloadItem::REMOVING (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: lint Created 8 years, 4 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
OLDNEW
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 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 download->Cancel(false); 315 download->Cancel(false);
316 if (delegate_) 316 if (delegate_)
317 delegate_->UpdateItemInPersistentStore(download); 317 delegate_->UpdateItemInPersistentStore(download);
318 } 318 }
319 } 319 }
320 320
321 // At this point, all dangerous downloads have had their files removed 321 // At this point, all dangerous downloads have had their files removed
322 // and all in progress downloads have been cancelled. We can now delete 322 // and all in progress downloads have been cancelled. We can now delete
323 // anything left. 323 // anything left.
324 324
325 // Copy downloads_ to separate container so as not to set off checks
326 // in DownloadItem destruction.
327 DownloadMap downloads_to_delete;
328 downloads_to_delete.swap(downloads_);
329
330 active_downloads_.clear(); 325 active_downloads_.clear();
331 STLDeleteValues(&downloads_to_delete); 326 STLDeleteValues(&downloads_);
327 downloads_.clear();
332 328
333 // We'll have nothing more to report to the observers after this point. 329 // We'll have nothing more to report to the observers after this point.
334 observers_.Clear(); 330 observers_.Clear();
335 331
336 file_manager_ = NULL; 332 file_manager_ = NULL;
337 if (delegate_) 333 if (delegate_)
338 delegate_->Shutdown(); 334 delegate_->Shutdown();
339 delegate_ = NULL; 335 delegate_ = NULL;
340 } 336 }
341 337
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 if (active_downloads_.count(download_id) == 0) 607 if (active_downloads_.count(download_id) == 0)
612 return; 608 return;
613 609
614 DownloadItemImpl* download = active_downloads_[download_id]; 610 DownloadItemImpl* download = active_downloads_[download_id];
615 download->OnAllDataSaved(size, hash); 611 download->OnAllDataSaved(size, hash);
616 MaybeCompleteDownload(download); 612 MaybeCompleteDownload(download);
617 } 613 }
618 614
619 void DownloadManagerImpl::AssertStateConsistent( 615 void DownloadManagerImpl::AssertStateConsistent(
620 DownloadItemImpl* download) const { 616 DownloadItemImpl* download) const {
621 if (download->GetState() == DownloadItem::REMOVING) {
622 DCHECK(!ContainsKey(downloads_, download->GetId()));
623 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
624 return;
625 }
626
627 // Should be in downloads_ if we're not REMOVING.
628 CHECK(ContainsKey(downloads_, download->GetId())); 617 CHECK(ContainsKey(downloads_, download->GetId()));
629 618
630 int64 state = download->GetState(); 619 int64 state = download->GetState();
631 base::debug::Alias(&state); 620 base::debug::Alias(&state);
632 if (ContainsKey(active_downloads_, download->GetId())) { 621 if (ContainsKey(active_downloads_, download->GetId())) {
633 if (download->IsPersisted()) 622 if (download->IsPersisted())
634 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 623 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
635 if (DownloadItem::IN_PROGRESS != download->GetState()) 624 if (DownloadItem::IN_PROGRESS != download->GetState())
636 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle()); 625 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle());
637 } 626 }
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 return delegate_ && delegate_->GenerateFileHash(); 765 return delegate_ && delegate_->GenerateFileHash();
777 } 766 }
778 767
779 int DownloadManagerImpl::RemoveDownloadItems( 768 int DownloadManagerImpl::RemoveDownloadItems(
780 const DownloadItemImplVector& pending_deletes) { 769 const DownloadItemImplVector& pending_deletes) {
781 if (pending_deletes.empty()) 770 if (pending_deletes.empty())
782 return 0; 771 return 0;
783 772
784 // Delete from internal maps. 773 // Delete from internal maps.
785 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); 774 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin();
786 it != pending_deletes.end(); 775 it != pending_deletes.end();
787 ++it) { 776 ++it) {
788 DownloadItemImpl* download = *it; 777 DownloadItemImpl* download = *it;
789 DCHECK(download); 778 DCHECK(download);
790 downloads_.erase(download->GetId()); 779 int32 download_id = download->GetId();
780 delete download;
781 downloads_.erase(download_id);
791 } 782 }
792
793 // Tell observers to refresh their views.
794 NotifyModelChanged(); 783 NotifyModelChanged();
795 784 return static_cast<int>(pending_deletes.size());
796 // Delete the download items themselves.
797 const int num_deleted = static_cast<int>(pending_deletes.size());
798 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
799 return num_deleted;
800 } 785 }
801 786
802 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { 787 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
803 if (!download || 788 if (!download ||
804 downloads_.find(download->GetId()) == downloads_.end()) 789 downloads_.find(download->GetId()) == downloads_.end())
805 return; 790 return;
806 791
807 // TODO(benjhayden,rdsmith): Remove this. 792 // TODO(benjhayden,rdsmith): Remove this.
808 if (!download->IsPersisted()) 793 if (!download->IsPersisted())
809 return; 794 return;
810 795
811 // Make history update. 796 // Make history update.
812 if (delegate_) 797 if (delegate_)
813 delegate_->RemoveItemFromPersistentStore(download); 798 delegate_->RemoveItemFromPersistentStore(download);
814 799
815 // Remove from our tables and delete. 800 // Remove from our tables and delete.
816 int downloads_count = 801 int downloads_count =
817 RemoveDownloadItems(DownloadItemImplVector(1, download)); 802 RemoveDownloadItems(DownloadItemImplVector(1, download));
818 DCHECK_EQ(1, downloads_count); 803 DCHECK_EQ(1, downloads_count);
819 } 804 }
820 805
821 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, 806 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
822 base::Time remove_end) { 807 base::Time remove_end) {
823 if (delegate_) 808 if (delegate_)
824 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); 809 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
825 810
826 // All downloads visible to the user will be in the history,
827 // so scan that map.
828 DownloadItemImplVector pending_deletes; 811 DownloadItemImplVector pending_deletes;
829 for (DownloadMap::const_iterator it = downloads_.begin(); 812 for (DownloadMap::const_iterator it = downloads_.begin();
830 it != downloads_.end(); 813 it != downloads_.end();
831 ++it) { 814 ++it) {
832 DownloadItemImpl* download = it->second; 815 DownloadItemImpl* download = it->second;
833 if (download->IsPersisted() && 816 if (download->IsPersisted() &&
834 download->GetStartTime() >= remove_begin && 817 download->GetStartTime() >= remove_begin &&
835 (remove_end.is_null() || download->GetStartTime() < remove_end) && 818 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
836 (download->IsComplete() || download->IsCancelled())) { 819 (download->IsComplete() || download->IsCancelled())) {
837 AssertStateConsistent(download); 820 AssertStateConsistent(download);
821 download->NotifyRemoved();
838 pending_deletes.push_back(download); 822 pending_deletes.push_back(download);
839 } 823 }
840 } 824 }
841 return RemoveDownloadItems(pending_deletes); 825 return RemoveDownloadItems(pending_deletes);
842 } 826 }
843 827
844 int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) { 828 int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
845 return RemoveDownloadsBetween(remove_begin, base::Time()); 829 return RemoveDownloadsBetween(remove_begin, base::Time());
846 } 830 }
847 831
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 void DownloadManagerImpl::DownloadRenamedToFinalName( 1100 void DownloadManagerImpl::DownloadRenamedToFinalName(
1117 DownloadItemImpl* download) { 1101 DownloadItemImpl* download) {
1118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1119 // If the rename failed, we receive an OnDownloadInterrupted() call before we 1103 // If the rename failed, we receive an OnDownloadInterrupted() call before we
1120 // receive the DownloadRenamedToFinalName() call. 1104 // receive the DownloadRenamedToFinalName() call.
1121 if (delegate_) { 1105 if (delegate_) {
1122 delegate_->UpdatePathForItemInPersistentStore( 1106 delegate_->UpdatePathForItemInPersistentStore(
1123 download, download->GetFullPath()); 1107 download, download->GetFullPath());
1124 } 1108 }
1125 } 1109 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698