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

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

Issue 10263019: DownloadManagerDelegate::ShouldCompleteDownload(callback) (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: complete_callback is the droids you're looking for. Created 8 years, 7 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 536 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 << " size = " << size; 547 << " size = " << size;
548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
549 549
550 // If it's not in active_downloads_, that means it was cancelled; just 550 // If it's not in active_downloads_, that means it was cancelled; just
551 // ignore the notification. 551 // ignore the notification.
552 if (active_downloads_.count(download_id) == 0) 552 if (active_downloads_.count(download_id) == 0)
553 return; 553 return;
554 554
555 DownloadItem* download = active_downloads_[download_id]; 555 DownloadItem* download = active_downloads_[download_id];
556 download->OnAllDataSaved(size, hash); 556 download->OnAllDataSaved(size, hash);
557 557 MaybeCompleteDownload(download);
558 download->MaybeCompleteDownload();
559 } 558 }
560 559
561 void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const { 560 void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const {
562 // TODO(rdsmith): Change to DCHECK after http://crbug.com/96627 resolved. 561 // TODO(rdsmith): Change to DCHECK after http://crbug.com/96627 resolved.
563 if (download->GetState() == DownloadItem::REMOVING) { 562 if (download->GetState() == DownloadItem::REMOVING) {
564 CHECK(!ContainsKey(downloads_, download)); 563 CHECK(!ContainsKey(downloads_, download));
565 CHECK(!ContainsKey(active_downloads_, download->GetId())); 564 CHECK(!ContainsKey(active_downloads_, download->GetId()));
566 CHECK(!ContainsKey(in_progress_, download->GetId())); 565 CHECK(!ContainsKey(in_progress_, download->GetId()));
567 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle())); 566 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
568 return; 567 return;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 // transition on the DownloadItem. 630 // transition on the DownloadItem.
632 631
633 // Confirm we're in the proper set of states to be here; 632 // Confirm we're in the proper set of states to be here;
634 // in in_progress_, have all data, have a history handle, (validated or safe). 633 // in in_progress_, have all data, have a history handle, (validated or safe).
635 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); 634 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState());
636 DCHECK_EQ(1u, in_progress_.count(download->GetId())); 635 DCHECK_EQ(1u, in_progress_.count(download->GetId()));
637 DCHECK(download->AllDataSaved()); 636 DCHECK(download->AllDataSaved());
638 DCHECK(download->IsPersisted()); 637 DCHECK(download->IsPersisted());
639 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle())); 638 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle()));
640 639
641 // Give the delegate a chance to override. 640 // Give the delegate a chance to override. It's ok to keep re-setting the
642 if (!delegate_->ShouldCompleteDownload(download)) 641 // delegate's |complete_callback| cb as long as there isn't another call-point
642 // trying to set it to a different cb. TODO(benjhayden): Change the callback
643 // to point directly to the item instead of |this| when DownloadItem supports
644 // weak-ptrs.
645 if (!delegate_->ShouldCompleteDownload(download, base::Bind(
646 &DownloadManagerImpl::MaybeCompleteDownloadById,
647 this, download->GetId())))
643 return; 648 return;
644 649
645 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " 650 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
646 << download->DebugString(false); 651 << download->DebugString(false);
647 652
648 // Remove the id from in_progress 653 // Remove the id from in_progress
649 in_progress_.erase(download->GetId()); 654 in_progress_.erase(download->GetId());
650 655
651 delegate_->UpdateItemInPersistentStore(download); 656 delegate_->UpdateItemInPersistentStore(download);
652 657
653 // Finish the download. 658 // Finish the download.
654 download->OnDownloadCompleting(file_manager_); 659 download->OnDownloadCompleting(file_manager_);
655 } 660 }
656 661
662 void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) {
663 DownloadItem* download_item = GetActiveDownload(download_id);
664 if (download_item != NULL)
665 MaybeCompleteDownload(download_item);
666 }
667
657 void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) { 668 void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) {
658 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 669 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
659 DCHECK(download); 670 DCHECK(download);
660 delegate_->UpdateItemInPersistentStore(download); 671 delegate_->UpdateItemInPersistentStore(download);
661 active_downloads_.erase(download->GetId()); 672 active_downloads_.erase(download->GetId());
662 AssertStateConsistent(download); 673 AssertStateConsistent(download);
663 } 674 }
664 675
665 void DownloadManagerImpl::OnDownloadRenamedToFinalName( 676 void DownloadManagerImpl::OnDownloadRenamedToFinalName(
666 int download_id, 677 int download_id,
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after
1208 if (it->second->IsComplete() && !it->second->GetOpened()) 1219 if (it->second->IsComplete() && !it->second->GetOpened())
1209 ++num_unopened; 1220 ++num_unopened;
1210 } 1221 }
1211 download_stats::RecordOpensOutstanding(num_unopened); 1222 download_stats::RecordOpensOutstanding(num_unopened);
1212 } 1223 }
1213 1224
1214 void DownloadManagerImpl::SetFileManagerForTesting( 1225 void DownloadManagerImpl::SetFileManagerForTesting(
1215 DownloadFileManager* file_manager) { 1226 DownloadFileManager* file_manager) {
1216 file_manager_ = file_manager; 1227 file_manager_ = file_manager;
1217 } 1228 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698