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 #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 Loading... |
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(delegate, |
128 delegate, | 129 guid, |
129 download_id, | 130 download_id, |
130 current_path, | 131 current_path, |
131 target_path, | 132 target_path, |
132 url_chain, | 133 url_chain, |
133 referrer_url, | 134 referrer_url, |
134 mime_type, | 135 mime_type, |
135 original_mime_type, | 136 original_mime_type, |
136 start_time, | 137 start_time, |
137 end_time, | 138 end_time, |
138 etag, | 139 etag, |
139 last_modified, | 140 last_modified, |
140 received_bytes, | 141 received_bytes, |
141 total_bytes, | 142 total_bytes, |
142 state, | 143 state, |
143 danger_type, | 144 danger_type, |
144 interrupt_reason, | 145 interrupt_reason, |
145 opened, | 146 opened, |
146 bound_net_log); | 147 bound_net_log); |
147 } | 148 } |
148 | 149 |
149 DownloadItemImpl* CreateActiveItem( | 150 DownloadItemImpl* CreateActiveItem( |
150 DownloadItemImplDelegate* delegate, | 151 DownloadItemImplDelegate* delegate, |
151 uint32_t download_id, | 152 uint32_t download_id, |
152 const DownloadCreateInfo& info, | 153 const DownloadCreateInfo& info, |
153 const net::BoundNetLog& bound_net_log) override { | 154 const net::BoundNetLog& bound_net_log) override { |
154 return new DownloadItemImpl(delegate, download_id, info, bound_net_log); | 155 return new DownloadItemImpl(delegate, download_id, info, bound_net_log); |
155 } | 156 } |
156 | 157 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( | 191 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( |
191 uint32_t id, | 192 uint32_t id, |
192 const DownloadCreateInfo& info) { | 193 const DownloadCreateInfo& info) { |
193 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 194 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
194 DCHECK(!ContainsKey(downloads_, id)); | 195 DCHECK(!ContainsKey(downloads_, id)); |
195 net::BoundNetLog bound_net_log = | 196 net::BoundNetLog bound_net_log = |
196 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 197 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
197 DownloadItemImpl* download = | 198 DownloadItemImpl* download = |
198 item_factory_->CreateActiveItem(this, id, info, bound_net_log); | 199 item_factory_->CreateActiveItem(this, id, info, bound_net_log); |
199 downloads_[id] = download; | 200 downloads_[id] = download; |
| 201 downloads_by_guid_[download->GetGuid()] = download; |
200 return download; | 202 return download; |
201 } | 203 } |
202 | 204 |
203 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) { | 205 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) { |
204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 206 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
205 if (delegate_) { | 207 if (delegate_) { |
206 delegate_->GetNextId(callback); | 208 delegate_->GetNextId(callback); |
207 return; | 209 return; |
208 } | 210 } |
209 static uint32_t next_id = content::DownloadItem::kInvalidId + 1; | 211 static uint32_t next_id = content::DownloadItem::kInvalidId + 1; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 // If there are in-progress downloads, cancel them. This also goes for | 280 // 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 | 281 // dangerous downloads which will remain in history if they aren't explicitly |
280 // accepted or discarded. Canceling will remove the intermediate download | 282 // accepted or discarded. Canceling will remove the intermediate download |
281 // file. | 283 // file. |
282 for (const auto& it : downloads_) { | 284 for (const auto& it : downloads_) { |
283 DownloadItemImpl* download = it.second; | 285 DownloadItemImpl* download = it.second; |
284 if (download->GetState() == DownloadItem::IN_PROGRESS) | 286 if (download->GetState() == DownloadItem::IN_PROGRESS) |
285 download->Cancel(false); | 287 download->Cancel(false); |
286 } | 288 } |
287 STLDeleteValues(&downloads_); | 289 STLDeleteValues(&downloads_); |
| 290 downloads_by_guid_.clear(); |
288 url_downloaders_.clear(); | 291 url_downloaders_.clear(); |
289 | 292 |
290 // We'll have nothing more to report to the observers after this point. | 293 // We'll have nothing more to report to the observers after this point. |
291 observers_.Clear(); | 294 observers_.Clear(); |
292 | 295 |
293 if (delegate_) | 296 if (delegate_) |
294 delegate_->Shutdown(); | 297 delegate_->Shutdown(); |
295 delegate_ = NULL; | 298 delegate_ = NULL; |
296 } | 299 } |
297 | 300 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 uint32_t id) { | 446 uint32_t id) { |
444 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 447 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
445 DCHECK_NE(content::DownloadItem::kInvalidId, id); | 448 DCHECK_NE(content::DownloadItem::kInvalidId, id); |
446 DCHECK(!ContainsKey(downloads_, id)); | 449 DCHECK(!ContainsKey(downloads_, id)); |
447 net::BoundNetLog bound_net_log = | 450 net::BoundNetLog bound_net_log = |
448 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 451 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
449 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( | 452 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( |
450 this, id, main_file_path, page_url, mime_type, std::move(request_handle), | 453 this, id, main_file_path, page_url, mime_type, std::move(request_handle), |
451 bound_net_log); | 454 bound_net_log); |
452 downloads_[download_item->GetId()] = download_item; | 455 downloads_[download_item->GetId()] = download_item; |
| 456 DCHECK(!ContainsKey(downloads_by_guid_, download_item->GetGuid())); |
| 457 downloads_by_guid_[download_item->GetGuid()] = download_item; |
453 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( | 458 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( |
454 this, download_item)); | 459 this, download_item)); |
455 if (!item_created.is_null()) | 460 if (!item_created.is_null()) |
456 item_created.Run(download_item); | 461 item_created.Run(download_item); |
457 } | 462 } |
458 | 463 |
459 void DownloadManagerImpl::OnSavePackageSuccessfullyFinished( | 464 void DownloadManagerImpl::OnSavePackageSuccessfullyFinished( |
460 DownloadItem* download_item) { | 465 DownloadItem* download_item) { |
461 FOR_EACH_OBSERVER(Observer, observers_, | 466 FOR_EACH_OBSERVER(Observer, observers_, |
462 OnSavePackageSuccessfullyFinished(this, download_item)); | 467 OnSavePackageSuccessfullyFinished(this, download_item)); |
(...skipping 25 matching lines...) Expand all Loading... |
488 } | 493 } |
489 | 494 |
490 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { | 495 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
491 return file_factory_.get(); | 496 return file_factory_.get(); |
492 } | 497 } |
493 | 498 |
494 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { | 499 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
495 if (!download) | 500 if (!download) |
496 return; | 501 return; |
497 | 502 |
| 503 downloads_by_guid_.erase(download->GetGuid()); |
| 504 |
498 uint32_t download_id = download->GetId(); | 505 uint32_t download_id = download->GetId(); |
499 if (downloads_.erase(download_id) == 0) | 506 if (downloads_.erase(download_id) == 0) |
500 return; | 507 return; |
501 delete download; | 508 delete download; |
502 } | 509 } |
503 | 510 |
504 void DownloadManagerImpl::AddUrlDownloader( | 511 void DownloadManagerImpl::AddUrlDownloader( |
505 scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> downloader) { | 512 scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> downloader) { |
506 if (downloader) | 513 if (downloader) |
507 url_downloaders_.push_back(std::move(downloader)); | 514 url_downloaders_.push_back(std::move(downloader)); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 | 596 |
590 void DownloadManagerImpl::AddObserver(Observer* observer) { | 597 void DownloadManagerImpl::AddObserver(Observer* observer) { |
591 observers_.AddObserver(observer); | 598 observers_.AddObserver(observer); |
592 } | 599 } |
593 | 600 |
594 void DownloadManagerImpl::RemoveObserver(Observer* observer) { | 601 void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
595 observers_.RemoveObserver(observer); | 602 observers_.RemoveObserver(observer); |
596 } | 603 } |
597 | 604 |
598 DownloadItem* DownloadManagerImpl::CreateDownloadItem( | 605 DownloadItem* DownloadManagerImpl::CreateDownloadItem( |
| 606 const std::string& guid, |
599 uint32_t id, | 607 uint32_t id, |
600 const base::FilePath& current_path, | 608 const base::FilePath& current_path, |
601 const base::FilePath& target_path, | 609 const base::FilePath& target_path, |
602 const std::vector<GURL>& url_chain, | 610 const std::vector<GURL>& url_chain, |
603 const GURL& referrer_url, | 611 const GURL& referrer_url, |
604 const std::string& mime_type, | 612 const std::string& mime_type, |
605 const std::string& original_mime_type, | 613 const std::string& original_mime_type, |
606 const base::Time& start_time, | 614 const base::Time& start_time, |
607 const base::Time& end_time, | 615 const base::Time& end_time, |
608 const std::string& etag, | 616 const std::string& etag, |
609 const std::string& last_modified, | 617 const std::string& last_modified, |
610 int64_t received_bytes, | 618 int64_t received_bytes, |
611 int64_t total_bytes, | 619 int64_t total_bytes, |
612 DownloadItem::DownloadState state, | 620 DownloadItem::DownloadState state, |
613 DownloadDangerType danger_type, | 621 DownloadDangerType danger_type, |
614 DownloadInterruptReason interrupt_reason, | 622 DownloadInterruptReason interrupt_reason, |
615 bool opened) { | 623 bool opened) { |
616 if (ContainsKey(downloads_, id)) { | 624 if (ContainsKey(downloads_, id)) { |
617 NOTREACHED(); | 625 NOTREACHED(); |
618 return NULL; | 626 return NULL; |
619 } | 627 } |
| 628 DCHECK(!ContainsKey(downloads_by_guid_, guid)); |
620 DownloadItemImpl* item = item_factory_->CreatePersistedItem( | 629 DownloadItemImpl* item = item_factory_->CreatePersistedItem( |
621 this, | 630 this, |
| 631 guid, |
622 id, | 632 id, |
623 current_path, | 633 current_path, |
624 target_path, | 634 target_path, |
625 url_chain, | 635 url_chain, |
626 referrer_url, | 636 referrer_url, |
627 mime_type, | 637 mime_type, |
628 original_mime_type, | 638 original_mime_type, |
629 start_time, | 639 start_time, |
630 end_time, | 640 end_time, |
631 etag, | 641 etag, |
632 last_modified, | 642 last_modified, |
633 received_bytes, | 643 received_bytes, |
634 total_bytes, | 644 total_bytes, |
635 state, | 645 state, |
636 danger_type, | 646 danger_type, |
637 interrupt_reason, | 647 interrupt_reason, |
638 opened, | 648 opened, |
639 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD)); | 649 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD)); |
640 downloads_[id] = item; | 650 downloads_[id] = item; |
| 651 downloads_by_guid_[guid] = item; |
641 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); | 652 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); |
642 DVLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true); | 653 DVLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true); |
643 return item; | 654 return item; |
644 } | 655 } |
645 | 656 |
646 int DownloadManagerImpl::InProgressCount() const { | 657 int DownloadManagerImpl::InProgressCount() const { |
647 int count = 0; | 658 int count = 0; |
648 for (const auto& it : downloads_) { | 659 for (const auto& it : downloads_) { |
649 if (it.second->GetState() == DownloadItem::IN_PROGRESS) | 660 if (it.second->GetState() == DownloadItem::IN_PROGRESS) |
650 ++count; | 661 ++count; |
(...skipping 10 matching lines...) Expand all Loading... |
661 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST && | 672 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST && |
662 it.second->GetDangerType() != | 673 it.second->GetDangerType() != |
663 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) { | 674 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) { |
664 ++count; | 675 ++count; |
665 } | 676 } |
666 } | 677 } |
667 return count; | 678 return count; |
668 } | 679 } |
669 | 680 |
670 DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) { | 681 DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) { |
671 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL; | 682 return ContainsKey(downloads_, download_id) ? downloads_[download_id] |
| 683 : nullptr; |
| 684 } |
| 685 |
| 686 DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) { |
| 687 DCHECK(guid == base::ToUpperASCII(guid)); |
| 688 return ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid] |
| 689 : nullptr; |
672 } | 690 } |
673 | 691 |
674 void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) { | 692 void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) { |
675 for (const auto& it : downloads_) { | 693 for (const auto& it : downloads_) { |
676 downloads->push_back(it.second); | 694 downloads->push_back(it.second); |
677 } | 695 } |
678 } | 696 } |
679 | 697 |
680 void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) { | 698 void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) { |
681 int num_unopened = 0; | 699 int num_unopened = 0; |
682 for (const auto& it : downloads_) { | 700 for (const auto& it : downloads_) { |
683 DownloadItemImpl* item = it.second; | 701 DownloadItemImpl* item = it.second; |
684 if ((item->GetState() == DownloadItem::COMPLETE) && | 702 if ((item->GetState() == DownloadItem::COMPLETE) && |
685 !item->GetOpened()) | 703 !item->GetOpened()) |
686 ++num_unopened; | 704 ++num_unopened; |
687 } | 705 } |
688 RecordOpensOutstanding(num_unopened); | 706 RecordOpensOutstanding(num_unopened); |
689 | 707 |
690 if (delegate_) | 708 if (delegate_) |
691 delegate_->OpenDownload(download); | 709 delegate_->OpenDownload(download); |
692 } | 710 } |
693 | 711 |
694 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 712 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
695 if (delegate_) | 713 if (delegate_) |
696 delegate_->ShowDownloadInShell(download); | 714 delegate_->ShowDownloadInShell(download); |
697 } | 715 } |
698 | 716 |
699 } // namespace content | 717 } // namespace content |
OLD | NEW |