| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 params->render_frame_host_routing_id(), | 83 params->render_frame_host_routing_id(), |
| 84 params->do_not_prompt_for_login()); | 84 params->do_not_prompt_for_login()); |
| 85 | 85 |
| 86 // If the download was accepted, the DownloadResourceHandler is now | 86 // If the download was accepted, the DownloadResourceHandler is now |
| 87 // responsible for driving the request to completion. | 87 // responsible for driving the request to completion. |
| 88 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) | 88 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 89 return nullptr; | 89 return nullptr; |
| 90 | 90 |
| 91 // Otherwise, create an interrupted download. | 91 // Otherwise, create an interrupted download. |
| 92 std::unique_ptr<DownloadCreateInfo> failed_created_info( | 92 std::unique_ptr<DownloadCreateInfo> failed_created_info( |
| 93 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), | 93 new DownloadCreateInfo(base::Time::Now(), net::NetLogWithSource(), |
| 94 base::WrapUnique(new DownloadSaveInfo))); | 94 base::WrapUnique(new DownloadSaveInfo))); |
| 95 failed_created_info->url_chain.push_back(params->url()); | 95 failed_created_info->url_chain.push_back(params->url()); |
| 96 failed_created_info->result = reason; | 96 failed_created_info->result = reason; |
| 97 std::unique_ptr<ByteStreamReader> empty_byte_stream; | 97 std::unique_ptr<ByteStreamReader> empty_byte_stream; |
| 98 BrowserThread::PostTask( | 98 BrowserThread::PostTask( |
| 99 BrowserThread::UI, FROM_HERE, | 99 BrowserThread::UI, FROM_HERE, |
| 100 base::Bind(&DownloadManager::StartDownload, download_manager, | 100 base::Bind(&DownloadManager::StartDownload, download_manager, |
| 101 base::Passed(&failed_created_info), | 101 base::Passed(&failed_created_info), |
| 102 base::Passed(&empty_byte_stream), params->callback())); | 102 base::Passed(&empty_byte_stream), params->callback())); |
| 103 return nullptr; | 103 return nullptr; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 131 const base::Time& end_time, | 131 const base::Time& end_time, |
| 132 const std::string& etag, | 132 const std::string& etag, |
| 133 const std::string& last_modified, | 133 const std::string& last_modified, |
| 134 int64_t received_bytes, | 134 int64_t received_bytes, |
| 135 int64_t total_bytes, | 135 int64_t total_bytes, |
| 136 const std::string& hash, | 136 const std::string& hash, |
| 137 DownloadItem::DownloadState state, | 137 DownloadItem::DownloadState state, |
| 138 DownloadDangerType danger_type, | 138 DownloadDangerType danger_type, |
| 139 DownloadInterruptReason interrupt_reason, | 139 DownloadInterruptReason interrupt_reason, |
| 140 bool opened, | 140 bool opened, |
| 141 const net::BoundNetLog& bound_net_log) override { | 141 const net::NetLogWithSource& net_log) override { |
| 142 return new DownloadItemImpl( | 142 return new DownloadItemImpl( |
| 143 delegate, guid, download_id, current_path, target_path, url_chain, | 143 delegate, guid, download_id, current_path, target_path, url_chain, |
| 144 referrer_url, site_url, tab_url, tab_refererr_url, mime_type, | 144 referrer_url, site_url, tab_url, tab_refererr_url, mime_type, |
| 145 original_mime_type, start_time, end_time, etag, last_modified, | 145 original_mime_type, start_time, end_time, etag, last_modified, |
| 146 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason, | 146 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason, |
| 147 opened, bound_net_log); | 147 opened, net_log); |
| 148 } | 148 } |
| 149 | 149 |
| 150 DownloadItemImpl* CreateActiveItem( | 150 DownloadItemImpl* CreateActiveItem( |
| 151 DownloadItemImplDelegate* delegate, | 151 DownloadItemImplDelegate* delegate, |
| 152 uint32_t download_id, | 152 uint32_t download_id, |
| 153 const DownloadCreateInfo& info, | 153 const DownloadCreateInfo& info, |
| 154 const net::BoundNetLog& bound_net_log) override { | 154 const net::NetLogWithSource& net_log) override { |
| 155 return new DownloadItemImpl(delegate, download_id, info, bound_net_log); | 155 return new DownloadItemImpl(delegate, download_id, info, net_log); |
| 156 } | 156 } |
| 157 | 157 |
| 158 DownloadItemImpl* CreateSavePageItem( | 158 DownloadItemImpl* CreateSavePageItem( |
| 159 DownloadItemImplDelegate* delegate, | 159 DownloadItemImplDelegate* delegate, |
| 160 uint32_t download_id, | 160 uint32_t download_id, |
| 161 const base::FilePath& path, | 161 const base::FilePath& path, |
| 162 const GURL& url, | 162 const GURL& url, |
| 163 const std::string& mime_type, | 163 const std::string& mime_type, |
| 164 std::unique_ptr<DownloadRequestHandleInterface> request_handle, | 164 std::unique_ptr<DownloadRequestHandleInterface> request_handle, |
| 165 const net::BoundNetLog& bound_net_log) override { | 165 const net::NetLogWithSource& net_log) override { |
| 166 return new DownloadItemImpl(delegate, download_id, path, url, mime_type, | 166 return new DownloadItemImpl(delegate, download_id, path, url, mime_type, |
| 167 std::move(request_handle), bound_net_log); | 167 std::move(request_handle), net_log); |
| 168 } | 168 } |
| 169 }; | 169 }; |
| 170 | 170 |
| 171 } // namespace | 171 } // namespace |
| 172 | 172 |
| 173 DownloadManagerImpl::DownloadManagerImpl( | 173 DownloadManagerImpl::DownloadManagerImpl( |
| 174 net::NetLog* net_log, | 174 net::NetLog* net_log, |
| 175 BrowserContext* browser_context) | 175 BrowserContext* browser_context) |
| 176 : item_factory_(new DownloadItemFactoryImpl()), | 176 : item_factory_(new DownloadItemFactoryImpl()), |
| 177 file_factory_(new DownloadFileFactory()), | 177 file_factory_(new DownloadFileFactory()), |
| 178 shutdown_needed_(true), | 178 shutdown_needed_(true), |
| 179 browser_context_(browser_context), | 179 browser_context_(browser_context), |
| 180 delegate_(NULL), | 180 delegate_(NULL), |
| 181 net_log_(net_log), | 181 net_log_(net_log), |
| 182 weak_factory_(this) { | 182 weak_factory_(this) { |
| 183 DCHECK(browser_context); | 183 DCHECK(browser_context); |
| 184 } | 184 } |
| 185 | 185 |
| 186 DownloadManagerImpl::~DownloadManagerImpl() { | 186 DownloadManagerImpl::~DownloadManagerImpl() { |
| 187 DCHECK(!shutdown_needed_); | 187 DCHECK(!shutdown_needed_); |
| 188 } | 188 } |
| 189 | 189 |
| 190 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( | 190 DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( |
| 191 uint32_t id, | 191 uint32_t id, |
| 192 const DownloadCreateInfo& info) { | 192 const DownloadCreateInfo& info) { |
| 193 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 193 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 194 DCHECK(!base::ContainsKey(downloads_, id)); | 194 DCHECK(!base::ContainsKey(downloads_, id)); |
| 195 net::BoundNetLog bound_net_log = | 195 net::NetLogWithSource net_log = |
| 196 net::BoundNetLog::Make(net_log_, net::NetLogSourceType::DOWNLOAD); | 196 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD); |
| 197 DownloadItemImpl* download = | 197 DownloadItemImpl* download = |
| 198 item_factory_->CreateActiveItem(this, id, info, bound_net_log); | 198 item_factory_->CreateActiveItem(this, id, info, net_log); |
| 199 downloads_[id] = download; | 199 downloads_[id] = download; |
| 200 downloads_by_guid_[download->GetGuid()] = download; | 200 downloads_by_guid_[download->GetGuid()] = download; |
| 201 return download; | 201 return download; |
| 202 } | 202 } |
| 203 | 203 |
| 204 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) { | 204 void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) { |
| 205 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 205 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 206 if (delegate_) { | 206 if (delegate_) { |
| 207 delegate_->GetNextId(callback); | 207 delegate_->GetNextId(callback); |
| 208 return; | 208 return; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 base::FilePath website_save_directory; // Unused | 355 base::FilePath website_save_directory; // Unused |
| 356 bool skip_dir_check = false; // Unused | 356 bool skip_dir_check = false; // Unused |
| 357 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, | 357 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
| 358 &default_download_directory, &skip_dir_check); | 358 &default_download_directory, &skip_dir_check); |
| 359 } | 359 } |
| 360 | 360 |
| 361 std::unique_ptr<DownloadFile> download_file; | 361 std::unique_ptr<DownloadFile> download_file; |
| 362 | 362 |
| 363 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) { | 363 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 364 DCHECK(stream.get()); | 364 DCHECK(stream.get()); |
| 365 download_file.reset( | 365 download_file.reset(file_factory_->CreateFile( |
| 366 file_factory_->CreateFile(std::move(info->save_info), | 366 std::move(info->save_info), default_download_directory, |
| 367 default_download_directory, | 367 std::move(stream), download->GetNetLogWithSource(), |
| 368 std::move(stream), | 368 download->DestinationObserverAsWeakPtr())); |
| 369 download->GetBoundNetLog(), | |
| 370 download->DestinationObserverAsWeakPtr())); | |
| 371 } | 369 } |
| 372 // It is important to leave info->save_info intact in the case of an interrupt | 370 // It is important to leave info->save_info intact in the case of an interrupt |
| 373 // so that the DownloadItem can salvage what it can out of a failed resumption | 371 // so that the DownloadItem can salvage what it can out of a failed resumption |
| 374 // attempt. | 372 // attempt. |
| 375 | 373 |
| 376 download->Start(std::move(download_file), std::move(info->request_handle), | 374 download->Start(std::move(download_file), std::move(info->request_handle), |
| 377 *info); | 375 *info); |
| 378 | 376 |
| 379 // For interrupted downloads, Start() will transition the state to | 377 // For interrupted downloads, Start() will transition the state to |
| 380 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated(). | 378 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated(). |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 void DownloadManagerImpl::CreateSavePackageDownloadItemWithId( | 435 void DownloadManagerImpl::CreateSavePackageDownloadItemWithId( |
| 438 const base::FilePath& main_file_path, | 436 const base::FilePath& main_file_path, |
| 439 const GURL& page_url, | 437 const GURL& page_url, |
| 440 const std::string& mime_type, | 438 const std::string& mime_type, |
| 441 std::unique_ptr<DownloadRequestHandleInterface> request_handle, | 439 std::unique_ptr<DownloadRequestHandleInterface> request_handle, |
| 442 const DownloadItemImplCreated& item_created, | 440 const DownloadItemImplCreated& item_created, |
| 443 uint32_t id) { | 441 uint32_t id) { |
| 444 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 442 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 445 DCHECK_NE(content::DownloadItem::kInvalidId, id); | 443 DCHECK_NE(content::DownloadItem::kInvalidId, id); |
| 446 DCHECK(!base::ContainsKey(downloads_, id)); | 444 DCHECK(!base::ContainsKey(downloads_, id)); |
| 447 net::BoundNetLog bound_net_log = | 445 net::NetLogWithSource net_log = |
| 448 net::BoundNetLog::Make(net_log_, net::NetLogSourceType::DOWNLOAD); | 446 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD); |
| 449 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( | 447 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( |
| 450 this, id, main_file_path, page_url, mime_type, std::move(request_handle), | 448 this, id, main_file_path, page_url, mime_type, std::move(request_handle), |
| 451 bound_net_log); | 449 net_log); |
| 452 downloads_[download_item->GetId()] = download_item; | 450 downloads_[download_item->GetId()] = download_item; |
| 453 DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid())); | 451 DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid())); |
| 454 downloads_by_guid_[download_item->GetGuid()] = download_item; | 452 downloads_by_guid_[download_item->GetGuid()] = download_item; |
| 455 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( | 453 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( |
| 456 this, download_item)); | 454 this, download_item)); |
| 457 if (!item_created.is_null()) | 455 if (!item_created.is_null()) |
| 458 item_created.Run(download_item); | 456 item_created.Run(download_item); |
| 459 } | 457 } |
| 460 | 458 |
| 461 void DownloadManagerImpl::OnSavePackageSuccessfullyFinished( | 459 void DownloadManagerImpl::OnSavePackageSuccessfullyFinished( |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 if (base::ContainsKey(downloads_, id)) { | 685 if (base::ContainsKey(downloads_, id)) { |
| 688 NOTREACHED(); | 686 NOTREACHED(); |
| 689 return NULL; | 687 return NULL; |
| 690 } | 688 } |
| 691 DCHECK(!base::ContainsKey(downloads_by_guid_, guid)); | 689 DCHECK(!base::ContainsKey(downloads_by_guid_, guid)); |
| 692 DownloadItemImpl* item = item_factory_->CreatePersistedItem( | 690 DownloadItemImpl* item = item_factory_->CreatePersistedItem( |
| 693 this, guid, id, current_path, target_path, url_chain, referrer_url, | 691 this, guid, id, current_path, target_path, url_chain, referrer_url, |
| 694 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type, | 692 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type, |
| 695 start_time, end_time, etag, last_modified, received_bytes, total_bytes, | 693 start_time, end_time, etag, last_modified, received_bytes, total_bytes, |
| 696 hash, state, danger_type, interrupt_reason, opened, | 694 hash, state, danger_type, interrupt_reason, opened, |
| 697 net::BoundNetLog::Make(net_log_, net::NetLogSourceType::DOWNLOAD)); | 695 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD)); |
| 698 downloads_[id] = item; | 696 downloads_[id] = item; |
| 699 downloads_by_guid_[guid] = item; | 697 downloads_by_guid_[guid] = item; |
| 700 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); | 698 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); |
| 701 DVLOG(20) << __func__ << "() download = " << item->DebugString(true); | 699 DVLOG(20) << __func__ << "() download = " << item->DebugString(true); |
| 702 return item; | 700 return item; |
| 703 } | 701 } |
| 704 | 702 |
| 705 int DownloadManagerImpl::InProgressCount() const { | 703 int DownloadManagerImpl::InProgressCount() const { |
| 706 int count = 0; | 704 int count = 0; |
| 707 for (const auto& it : downloads_) { | 705 for (const auto& it : downloads_) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 if (delegate_) | 754 if (delegate_) |
| 757 delegate_->OpenDownload(download); | 755 delegate_->OpenDownload(download); |
| 758 } | 756 } |
| 759 | 757 |
| 760 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 758 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
| 761 if (delegate_) | 759 if (delegate_) |
| 762 delegate_->ShowDownloadInShell(download); | 760 delegate_->ShowDownloadInShell(download); |
| 763 } | 761 } |
| 764 | 762 |
| 765 } // namespace content | 763 } // namespace content |
| OLD | NEW |