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

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

Issue 1781983002: [Downloads] Introduce GUIDs for downloads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
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 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 .release()); 99 .release());
100 } 100 }
101 101
102 class DownloadItemFactoryImpl : public DownloadItemFactory { 102 class DownloadItemFactoryImpl : public DownloadItemFactory {
103 public: 103 public:
104 DownloadItemFactoryImpl() {} 104 DownloadItemFactoryImpl() {}
105 ~DownloadItemFactoryImpl() override {} 105 ~DownloadItemFactoryImpl() override {}
106 106
107 DownloadItemImpl* CreatePersistedItem( 107 DownloadItemImpl* CreatePersistedItem(
108 DownloadItemImplDelegate* delegate, 108 DownloadItemImplDelegate* delegate,
109 const std::string& guid,
109 uint32_t download_id, 110 uint32_t download_id,
110 const base::FilePath& current_path, 111 const base::FilePath& current_path,
111 const base::FilePath& target_path, 112 const base::FilePath& target_path,
112 const std::vector<GURL>& url_chain, 113 const std::vector<GURL>& url_chain,
113 const GURL& referrer_url, 114 const GURL& referrer_url,
114 const std::string& mime_type, 115 const std::string& mime_type,
115 const std::string& original_mime_type, 116 const std::string& original_mime_type,
116 const base::Time& start_time, 117 const base::Time& start_time,
117 const base::Time& end_time, 118 const base::Time& end_time,
118 const std::string& etag, 119 const std::string& etag,
119 const std::string& last_modified, 120 const std::string& last_modified,
120 int64_t received_bytes, 121 int64_t received_bytes,
121 int64_t total_bytes, 122 int64_t total_bytes,
122 DownloadItem::DownloadState state, 123 DownloadItem::DownloadState state,
123 DownloadDangerType danger_type, 124 DownloadDangerType danger_type,
124 DownloadInterruptReason interrupt_reason, 125 DownloadInterruptReason interrupt_reason,
125 bool opened, 126 bool opened,
126 const net::BoundNetLog& bound_net_log) override { 127 const net::BoundNetLog& bound_net_log) override {
127 return new DownloadItemImpl( 128 return new DownloadItemImpl(
128 delegate, 129 delegate, guid, download_id, current_path, target_path, url_chain,
129 download_id, 130 referrer_url, mime_type, original_mime_type, start_time, end_time, etag,
130 current_path, 131 last_modified, received_bytes, total_bytes, state, danger_type,
131 target_path, 132 interrupt_reason, opened, bound_net_log);
132 url_chain,
133 referrer_url,
134 mime_type,
135 original_mime_type,
136 start_time,
137 end_time,
138 etag,
139 last_modified,
140 received_bytes,
141 total_bytes,
142 state,
143 danger_type,
144 interrupt_reason,
145 opened,
146 bound_net_log);
147 } 133 }
148 134
149 DownloadItemImpl* CreateActiveItem( 135 DownloadItemImpl* CreateActiveItem(
150 DownloadItemImplDelegate* delegate, 136 DownloadItemImplDelegate* delegate,
151 uint32_t download_id, 137 uint32_t download_id,
152 const DownloadCreateInfo& info, 138 const DownloadCreateInfo& info,
153 const net::BoundNetLog& bound_net_log) override { 139 const net::BoundNetLog& bound_net_log) override {
154 return new DownloadItemImpl(delegate, download_id, info, bound_net_log); 140 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
155 } 141 }
156 142
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( 176 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
191 uint32_t id, 177 uint32_t id,
192 const DownloadCreateInfo& info) { 178 const DownloadCreateInfo& info) {
193 DCHECK_CURRENTLY_ON(BrowserThread::UI); 179 DCHECK_CURRENTLY_ON(BrowserThread::UI);
194 DCHECK(!ContainsKey(downloads_, id)); 180 DCHECK(!ContainsKey(downloads_, id));
195 net::BoundNetLog bound_net_log = 181 net::BoundNetLog bound_net_log =
196 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); 182 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
197 DownloadItemImpl* download = 183 DownloadItemImpl* download =
198 item_factory_->CreateActiveItem(this, id, info, bound_net_log); 184 item_factory_->CreateActiveItem(this, id, info, bound_net_log);
199 downloads_[id] = download; 185 downloads_[id] = download;
186 downloads_by_guid_[download->GetGuid()] = download;
200 return download; 187 return download;
201 } 188 }
202 189
203 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) { 190 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
204 DCHECK_CURRENTLY_ON(BrowserThread::UI); 191 DCHECK_CURRENTLY_ON(BrowserThread::UI);
205 if (delegate_) { 192 if (delegate_) {
206 delegate_->GetNextId(callback); 193 delegate_->GetNextId(callback);
207 return; 194 return;
208 } 195 }
209 static uint32_t next_id = content::DownloadItem::kInvalidId + 1; 196 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 // If there are in-progress downloads, cancel them. This also goes for 265 // If there are in-progress downloads, cancel them. This also goes for
279 // dangerous downloads which will remain in history if they aren't explicitly 266 // dangerous downloads which will remain in history if they aren't explicitly
280 // accepted or discarded. Canceling will remove the intermediate download 267 // accepted or discarded. Canceling will remove the intermediate download
281 // file. 268 // file.
282 for (const auto& it : downloads_) { 269 for (const auto& it : downloads_) {
283 DownloadItemImpl* download = it.second; 270 DownloadItemImpl* download = it.second;
284 if (download->GetState() == DownloadItem::IN_PROGRESS) 271 if (download->GetState() == DownloadItem::IN_PROGRESS)
285 download->Cancel(false); 272 download->Cancel(false);
286 } 273 }
287 STLDeleteValues(&downloads_); 274 STLDeleteValues(&downloads_);
275 downloads_by_guid_.clear();
288 url_downloaders_.clear(); 276 url_downloaders_.clear();
289 277
290 // We'll have nothing more to report to the observers after this point. 278 // We'll have nothing more to report to the observers after this point.
291 observers_.Clear(); 279 observers_.Clear();
292 280
293 if (delegate_) 281 if (delegate_)
294 delegate_->Shutdown(); 282 delegate_->Shutdown();
295 delegate_ = NULL; 283 delegate_ = NULL;
296 } 284 }
297 285
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 uint32_t id) { 431 uint32_t id) {
444 DCHECK_CURRENTLY_ON(BrowserThread::UI); 432 DCHECK_CURRENTLY_ON(BrowserThread::UI);
445 DCHECK_NE(content::DownloadItem::kInvalidId, id); 433 DCHECK_NE(content::DownloadItem::kInvalidId, id);
446 DCHECK(!ContainsKey(downloads_, id)); 434 DCHECK(!ContainsKey(downloads_, id));
447 net::BoundNetLog bound_net_log = 435 net::BoundNetLog bound_net_log =
448 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); 436 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
449 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( 437 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
450 this, id, main_file_path, page_url, mime_type, std::move(request_handle), 438 this, id, main_file_path, page_url, mime_type, std::move(request_handle),
451 bound_net_log); 439 bound_net_log);
452 downloads_[download_item->GetId()] = download_item; 440 downloads_[download_item->GetId()] = download_item;
441 DCHECK(!ContainsKey(downloads_by_guid_, download_item->GetGuid()));
442 downloads_by_guid_[download_item->GetGuid()] = download_item;
453 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( 443 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
454 this, download_item)); 444 this, download_item));
455 if (!item_created.is_null()) 445 if (!item_created.is_null())
456 item_created.Run(download_item); 446 item_created.Run(download_item);
457 } 447 }
458 448
459 void DownloadManagerImpl::OnSavePackageSuccessfullyFinished( 449 void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
460 DownloadItem* download_item) { 450 DownloadItem* download_item) {
461 FOR_EACH_OBSERVER(Observer, observers_, 451 FOR_EACH_OBSERVER(Observer, observers_,
462 OnSavePackageSuccessfullyFinished(this, download_item)); 452 OnSavePackageSuccessfullyFinished(this, download_item));
(...skipping 25 matching lines...) Expand all
488 } 478 }
489 479
490 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { 480 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
491 return file_factory_.get(); 481 return file_factory_.get();
492 } 482 }
493 483
494 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { 484 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
495 if (!download) 485 if (!download)
496 return; 486 return;
497 487
488 downloads_by_guid_.erase(download->GetGuid());
489
498 uint32_t download_id = download->GetId(); 490 uint32_t download_id = download->GetId();
499 if (downloads_.erase(download_id) == 0) 491 if (downloads_.erase(download_id) == 0)
500 return; 492 return;
501 delete download; 493 delete download;
502 } 494 }
503 495
504 void DownloadManagerImpl::AddUrlDownloader( 496 void DownloadManagerImpl::AddUrlDownloader(
505 scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> downloader) { 497 scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> downloader) {
506 if (downloader) 498 if (downloader)
507 url_downloaders_.push_back(std::move(downloader)); 499 url_downloaders_.push_back(std::move(downloader));
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 581
590 void DownloadManagerImpl::AddObserver(Observer* observer) { 582 void DownloadManagerImpl::AddObserver(Observer* observer) {
591 observers_.AddObserver(observer); 583 observers_.AddObserver(observer);
592 } 584 }
593 585
594 void DownloadManagerImpl::RemoveObserver(Observer* observer) { 586 void DownloadManagerImpl::RemoveObserver(Observer* observer) {
595 observers_.RemoveObserver(observer); 587 observers_.RemoveObserver(observer);
596 } 588 }
597 589
598 DownloadItem* DownloadManagerImpl::CreateDownloadItem( 590 DownloadItem* DownloadManagerImpl::CreateDownloadItem(
591 const std::string& guid,
599 uint32_t id, 592 uint32_t id,
600 const base::FilePath& current_path, 593 const base::FilePath& current_path,
601 const base::FilePath& target_path, 594 const base::FilePath& target_path,
602 const std::vector<GURL>& url_chain, 595 const std::vector<GURL>& url_chain,
603 const GURL& referrer_url, 596 const GURL& referrer_url,
604 const std::string& mime_type, 597 const std::string& mime_type,
605 const std::string& original_mime_type, 598 const std::string& original_mime_type,
606 const base::Time& start_time, 599 const base::Time& start_time,
607 const base::Time& end_time, 600 const base::Time& end_time,
608 const std::string& etag, 601 const std::string& etag,
609 const std::string& last_modified, 602 const std::string& last_modified,
610 int64_t received_bytes, 603 int64_t received_bytes,
611 int64_t total_bytes, 604 int64_t total_bytes,
612 DownloadItem::DownloadState state, 605 DownloadItem::DownloadState state,
613 DownloadDangerType danger_type, 606 DownloadDangerType danger_type,
614 DownloadInterruptReason interrupt_reason, 607 DownloadInterruptReason interrupt_reason,
615 bool opened) { 608 bool opened) {
616 if (ContainsKey(downloads_, id)) { 609 if (ContainsKey(downloads_, id)) {
617 NOTREACHED(); 610 NOTREACHED();
618 return NULL; 611 return NULL;
619 } 612 }
613 DCHECK(!ContainsKey(downloads_by_guid_, guid));
620 DownloadItemImpl* item = item_factory_->CreatePersistedItem( 614 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
621 this, 615 this, guid, id, current_path, target_path, url_chain, referrer_url,
622 id, 616 mime_type, original_mime_type, start_time, end_time, etag, last_modified,
623 current_path, 617 received_bytes, total_bytes, state, danger_type, interrupt_reason, opened,
624 target_path,
625 url_chain,
626 referrer_url,
627 mime_type,
628 original_mime_type,
629 start_time,
630 end_time,
631 etag,
632 last_modified,
633 received_bytes,
634 total_bytes,
635 state,
636 danger_type,
637 interrupt_reason,
638 opened,
639 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD)); 618 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
640 downloads_[id] = item; 619 downloads_[id] = item;
620 downloads_by_guid_[guid] = item;
641 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); 621 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
642 DVLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true); 622 DVLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
643 return item; 623 return item;
644 } 624 }
645 625
646 int DownloadManagerImpl::InProgressCount() const { 626 int DownloadManagerImpl::InProgressCount() const {
647 int count = 0; 627 int count = 0;
648 for (const auto& it : downloads_) { 628 for (const auto& it : downloads_) {
649 if (it.second->GetState() == DownloadItem::IN_PROGRESS) 629 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
650 ++count; 630 ++count;
(...skipping 10 matching lines...) Expand all
661 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST && 641 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
662 it.second->GetDangerType() != 642 it.second->GetDangerType() !=
663 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) { 643 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
664 ++count; 644 ++count;
665 } 645 }
666 } 646 }
667 return count; 647 return count;
668 } 648 }
669 649
670 DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) { 650 DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
671 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL; 651 return ContainsKey(downloads_, download_id) ? downloads_[download_id]
652 : nullptr;
653 }
654
655 DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
656 DCHECK(guid == base::ToUpperASCII(guid));
657 return ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
658 : nullptr;
672 } 659 }
673 660
674 void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) { 661 void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
675 for (const auto& it : downloads_) { 662 for (const auto& it : downloads_) {
676 downloads->push_back(it.second); 663 downloads->push_back(it.second);
677 } 664 }
678 } 665 }
679 666
680 void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) { 667 void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
681 int num_unopened = 0; 668 int num_unopened = 0;
682 for (const auto& it : downloads_) { 669 for (const auto& it : downloads_) {
683 DownloadItemImpl* item = it.second; 670 DownloadItemImpl* item = it.second;
684 if ((item->GetState() == DownloadItem::COMPLETE) && 671 if ((item->GetState() == DownloadItem::COMPLETE) &&
685 !item->GetOpened()) 672 !item->GetOpened())
686 ++num_unopened; 673 ++num_unopened;
687 } 674 }
688 RecordOpensOutstanding(num_unopened); 675 RecordOpensOutstanding(num_unopened);
689 676
690 if (delegate_) 677 if (delegate_)
691 delegate_->OpenDownload(download); 678 delegate_->OpenDownload(download);
692 } 679 }
693 680
694 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { 681 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
695 if (delegate_) 682 if (delegate_)
696 delegate_->ShowDownloadInShell(download); 683 delegate_->ShowDownloadInShell(download);
697 } 684 }
698 685
699 } // namespace content 686 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698