| 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 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
| 12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
| 18 #include "base/supports_user_data.h" | 19 #include "base/supports_user_data.h" |
| 19 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
| 20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 21 #include "content/browser/byte_stream.h" | 22 #include "content/browser/byte_stream.h" |
| 22 #include "content/browser/download/download_create_info.h" | 23 #include "content/browser/download/download_create_info.h" |
| 23 #include "content/browser/download/download_file_factory.h" | 24 #include "content/browser/download/download_file_factory.h" |
| 24 #include "content/browser/download/download_item_factory.h" | 25 #include "content/browser/download/download_item_factory.h" |
| 25 #include "content/browser/download/download_item_impl.h" | 26 #include "content/browser/download/download_item_impl.h" |
| 26 #include "content/browser/download/download_stats.h" | 27 #include "content/browser/download/download_stats.h" |
| 27 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 28 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 28 #include "content/browser/renderer_host/render_view_host_impl.h" | 29 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 29 #include "content/browser/web_contents/web_contents_impl.h" | 30 #include "content/browser/web_contents/web_contents_impl.h" |
| 30 #include "content/public/browser/browser_context.h" | 31 #include "content/public/browser/browser_context.h" |
| 31 #include "content/public/browser/browser_thread.h" | |
| 32 #include "content/public/browser/content_browser_client.h" | 32 #include "content/public/browser/content_browser_client.h" |
| 33 #include "content/public/browser/download_interrupt_reasons.h" | 33 #include "content/public/browser/download_interrupt_reasons.h" |
| 34 #include "content/public/browser/download_manager_delegate.h" | 34 #include "content/public/browser/download_manager_delegate.h" |
| 35 #include "content/public/browser/download_url_parameters.h" | 35 #include "content/public/browser/download_url_parameters.h" |
| 36 #include "content/public/browser/notification_service.h" | 36 #include "content/public/browser/notification_service.h" |
| 37 #include "content/public/browser/notification_types.h" | 37 #include "content/public/browser/notification_types.h" |
| 38 #include "content/public/browser/render_process_host.h" | 38 #include "content/public/browser/render_process_host.h" |
| 39 #include "content/public/browser/resource_context.h" | 39 #include "content/public/browser/resource_context.h" |
| 40 #include "content/public/browser/web_contents_delegate.h" | 40 #include "content/public/browser/web_contents_delegate.h" |
| 41 #include "content/public/common/referrer.h" | 41 #include "content/public/common/referrer.h" |
| 42 #include "net/base/elements_upload_data_stream.h" | 42 #include "net/base/elements_upload_data_stream.h" |
| 43 #include "net/base/load_flags.h" | 43 #include "net/base/load_flags.h" |
| 44 #include "net/base/request_priority.h" | 44 #include "net/base/request_priority.h" |
| 45 #include "net/base/upload_bytes_element_reader.h" | 45 #include "net/base/upload_bytes_element_reader.h" |
| 46 #include "net/url_request/url_request_context.h" | 46 #include "net/url_request/url_request_context.h" |
| 47 #include "url/origin.h" | 47 #include "url/origin.h" |
| 48 | 48 |
| 49 namespace content { | 49 namespace content { |
| 50 namespace { | 50 namespace { |
| 51 | 51 |
| 52 void BeginDownload(scoped_ptr<DownloadUrlParameters> params, | 52 scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( |
| 53 uint32 download_id) { | 53 scoped_ptr<DownloadUrlParameters> params, |
| 54 uint32 download_id, |
| 55 base::WeakPtr<DownloadManagerImpl> download_manager) { |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 56 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 55 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and | 57 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and |
| 56 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so | 58 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so |
| 57 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. | 59 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. |
| 58 scoped_ptr<net::URLRequest> request( | 60 scoped_ptr<net::URLRequest> request( |
| 59 params->resource_context()->GetRequestContext()->CreateRequest( | 61 params->resource_context()->GetRequestContext()->CreateRequest( |
| 60 params->url(), net::DEFAULT_PRIORITY, NULL)); | 62 params->url(), net::DEFAULT_PRIORITY, NULL)); |
| 61 request->set_method(params->method()); | 63 request->set_method(params->method()); |
| 62 if (!params->post_body().empty()) { | 64 if (!params->post_body().empty()) { |
| 63 const std::string& body = params->post_body(); | 65 const std::string& body = params->post_body(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 } | 115 } |
| 114 | 116 |
| 115 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); | 117 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); |
| 116 save_info->file_path = params->file_path(); | 118 save_info->file_path = params->file_path(); |
| 117 save_info->suggested_name = params->suggested_name(); | 119 save_info->suggested_name = params->suggested_name(); |
| 118 save_info->offset = params->offset(); | 120 save_info->offset = params->offset(); |
| 119 save_info->hash_state = params->hash_state(); | 121 save_info->hash_state = params->hash_state(); |
| 120 save_info->prompt_for_save_location = params->prompt(); | 122 save_info->prompt_for_save_location = params->prompt(); |
| 121 save_info->file = params->GetFile(); | 123 save_info->file = params->GetFile(); |
| 122 | 124 |
| 123 ResourceDispatcherHost::Get()->BeginDownload( | 125 if (params->render_process_host_id() != -1) { |
| 124 request.Pass(), | 126 ResourceDispatcherHost::Get()->BeginDownload( |
| 125 params->referrer(), | 127 request.Pass(), params->referrer(), params->content_initiated(), |
| 126 params->content_initiated(), | 128 params->resource_context(), params->render_process_host_id(), |
| 127 params->resource_context(), | 129 params->render_view_host_routing_id(), |
| 128 params->render_process_host_id(), | 130 params->render_frame_host_routing_id(), params->prefer_cache(), |
| 129 params->render_view_host_routing_id(), | 131 params->do_not_prompt_for_login(), save_info.Pass(), download_id, |
| 130 params->render_frame_host_routing_id(), | 132 params->callback()); |
| 131 params->prefer_cache(), | 133 return nullptr; |
| 132 params->do_not_prompt_for_login(), | 134 } |
| 133 save_info.Pass(), | 135 return scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>( |
| 134 download_id, | 136 UrlDownloader::BeginDownload( |
| 135 params->callback()); | 137 download_manager, request.Pass(), params->referrer(), false, |
| 138 params->prefer_cache(), true, save_info.Pass(), download_id, |
| 139 params->callback()) |
| 140 .release()); |
| 136 } | 141 } |
| 137 | 142 |
| 138 class DownloadItemFactoryImpl : public DownloadItemFactory { | 143 class DownloadItemFactoryImpl : public DownloadItemFactory { |
| 139 public: | 144 public: |
| 140 DownloadItemFactoryImpl() {} | 145 DownloadItemFactoryImpl() {} |
| 141 ~DownloadItemFactoryImpl() override {} | 146 ~DownloadItemFactoryImpl() override {} |
| 142 | 147 |
| 143 DownloadItemImpl* CreatePersistedItem( | 148 DownloadItemImpl* CreatePersistedItem( |
| 144 DownloadItemImplDelegate* delegate, | 149 DownloadItemImplDelegate* delegate, |
| 145 uint32 download_id, | 150 uint32 download_id, |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 OnSavePackageSuccessfullyFinished(this, download_item)); | 510 OnSavePackageSuccessfullyFinished(this, download_item)); |
| 506 } | 511 } |
| 507 | 512 |
| 508 // Resume a download of a specific URL. We send the request to the | 513 // Resume a download of a specific URL. We send the request to the |
| 509 // ResourceDispatcherHost, and let it send us responses like a regular | 514 // ResourceDispatcherHost, and let it send us responses like a regular |
| 510 // download. | 515 // download. |
| 511 void DownloadManagerImpl::ResumeInterruptedDownload( | 516 void DownloadManagerImpl::ResumeInterruptedDownload( |
| 512 scoped_ptr<content::DownloadUrlParameters> params, | 517 scoped_ptr<content::DownloadUrlParameters> params, |
| 513 uint32 id) { | 518 uint32 id) { |
| 514 RecordDownloadSource(INITIATED_BY_RESUMPTION); | 519 RecordDownloadSource(INITIATED_BY_RESUMPTION); |
| 515 BrowserThread::PostTask( | 520 BrowserThread::PostTaskAndReplyWithResult( |
| 516 BrowserThread::IO, | 521 BrowserThread::IO, FROM_HERE, |
| 517 FROM_HERE, | 522 base::Bind(&BeginDownload, base::Passed(¶ms), id, |
| 518 base::Bind(&BeginDownload, base::Passed(¶ms), id)); | 523 weak_factory_.GetWeakPtr()), |
| 524 base::Bind(&DownloadManagerImpl::AddUrlDownloader, |
| 525 weak_factory_.GetWeakPtr())); |
| 519 } | 526 } |
| 520 | 527 |
| 521 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( | 528 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
| 522 scoped_ptr<DownloadItemFactory> item_factory) { | 529 scoped_ptr<DownloadItemFactory> item_factory) { |
| 523 item_factory_ = item_factory.Pass(); | 530 item_factory_ = item_factory.Pass(); |
| 524 } | 531 } |
| 525 | 532 |
| 526 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( | 533 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
| 527 scoped_ptr<DownloadFileFactory> file_factory) { | 534 scoped_ptr<DownloadFileFactory> file_factory) { |
| 528 file_factory_ = file_factory.Pass(); | 535 file_factory_ = file_factory.Pass(); |
| 529 } | 536 } |
| 530 | 537 |
| 531 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { | 538 DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
| 532 return file_factory_.get(); | 539 return file_factory_.get(); |
| 533 } | 540 } |
| 534 | 541 |
| 535 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { | 542 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
| 536 if (!download) | 543 if (!download) |
| 537 return; | 544 return; |
| 538 | 545 |
| 539 uint32 download_id = download->GetId(); | 546 uint32 download_id = download->GetId(); |
| 540 if (downloads_.erase(download_id) == 0) | 547 if (downloads_.erase(download_id) == 0) |
| 541 return; | 548 return; |
| 542 delete download; | 549 delete download; |
| 543 } | 550 } |
| 544 | 551 |
| 552 void DownloadManagerImpl::AddUrlDownloader( |
| 553 scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> downloader) { |
| 554 if (downloader) |
| 555 url_downloaders_.push_back(downloader.Pass()); |
| 556 } |
| 557 |
| 558 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { |
| 559 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); |
| 560 ++ptr) { |
| 561 if (ptr->get() == downloader) { |
| 562 url_downloaders_.erase(ptr); |
| 563 return; |
| 564 } |
| 565 } |
| 566 } |
| 567 |
| 545 namespace { | 568 namespace { |
| 546 | 569 |
| 547 bool RemoveDownloadBetween(base::Time remove_begin, | 570 bool RemoveDownloadBetween(base::Time remove_begin, |
| 548 base::Time remove_end, | 571 base::Time remove_end, |
| 549 const DownloadItemImpl* download_item) { | 572 const DownloadItemImpl* download_item) { |
| 550 return download_item->GetStartTime() >= remove_begin && | 573 return download_item->GetStartTime() >= remove_begin && |
| 551 (remove_end.is_null() || download_item->GetStartTime() < remove_end); | 574 (remove_end.is_null() || download_item->GetStartTime() < remove_end); |
| 552 } | 575 } |
| 553 | 576 |
| 554 bool RemoveDownloadByOriginAndTime(const url::Origin& origin, | 577 bool RemoveDownloadByOriginAndTime(const url::Origin& origin, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 return num_deleted; | 628 return num_deleted; |
| 606 } | 629 } |
| 607 | 630 |
| 608 void DownloadManagerImpl::DownloadUrl( | 631 void DownloadManagerImpl::DownloadUrl( |
| 609 scoped_ptr<DownloadUrlParameters> params) { | 632 scoped_ptr<DownloadUrlParameters> params) { |
| 610 if (params->post_id() >= 0) { | 633 if (params->post_id() >= 0) { |
| 611 // Check this here so that the traceback is more useful. | 634 // Check this here so that the traceback is more useful. |
| 612 DCHECK(params->prefer_cache()); | 635 DCHECK(params->prefer_cache()); |
| 613 DCHECK_EQ("POST", params->method()); | 636 DCHECK_EQ("POST", params->method()); |
| 614 } | 637 } |
| 615 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( | 638 BrowserThread::PostTaskAndReplyWithResult( |
| 616 &BeginDownload, base::Passed(¶ms), | 639 BrowserThread::IO, FROM_HERE, |
| 617 content::DownloadItem::kInvalidId)); | 640 base::Bind(&BeginDownload, base::Passed(¶ms), |
| 641 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()), |
| 642 base::Bind(&DownloadManagerImpl::AddUrlDownloader, |
| 643 weak_factory_.GetWeakPtr())); |
| 618 } | 644 } |
| 619 | 645 |
| 620 void DownloadManagerImpl::AddObserver(Observer* observer) { | 646 void DownloadManagerImpl::AddObserver(Observer* observer) { |
| 621 observers_.AddObserver(observer); | 647 observers_.AddObserver(observer); |
| 622 } | 648 } |
| 623 | 649 |
| 624 void DownloadManagerImpl::RemoveObserver(Observer* observer) { | 650 void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
| 625 observers_.RemoveObserver(observer); | 651 observers_.RemoveObserver(observer); |
| 626 } | 652 } |
| 627 | 653 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 if (delegate_) | 750 if (delegate_) |
| 725 delegate_->OpenDownload(download); | 751 delegate_->OpenDownload(download); |
| 726 } | 752 } |
| 727 | 753 |
| 728 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 754 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
| 729 if (delegate_) | 755 if (delegate_) |
| 730 delegate_->ShowDownloadInShell(download); | 756 delegate_->ShowDownloadInShell(download); |
| 731 } | 757 } |
| 732 | 758 |
| 733 } // namespace content | 759 } // namespace content |
| OLD | NEW |