| 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" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/debug/alias.h" | 12 #include "base/debug/alias.h" |
| 13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 20 #include "base/strings/sys_string_conversions.h" | 20 #include "base/strings/sys_string_conversions.h" |
| 21 #include "base/supports_user_data.h" | 21 #include "base/supports_user_data.h" |
| 22 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
| 23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "content/browser/byte_stream.h" | 24 #include "content/browser/byte_stream.h" |
| 25 #include "content/browser/child_process_security_policy_impl.h" |
| 25 #include "content/browser/download/download_create_info.h" | 26 #include "content/browser/download/download_create_info.h" |
| 26 #include "content/browser/download/download_file_factory.h" | 27 #include "content/browser/download/download_file_factory.h" |
| 27 #include "content/browser/download/download_item_factory.h" | 28 #include "content/browser/download/download_item_factory.h" |
| 28 #include "content/browser/download/download_item_impl.h" | 29 #include "content/browser/download/download_item_impl.h" |
| 29 #include "content/browser/download/download_stats.h" | 30 #include "content/browser/download/download_stats.h" |
| 30 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 31 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 32 #include "content/browser/loader/resource_request_info_impl.h" |
| 31 #include "content/browser/renderer_host/render_view_host_impl.h" | 33 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 32 #include "content/browser/web_contents/web_contents_impl.h" | 34 #include "content/browser/web_contents/web_contents_impl.h" |
| 33 #include "content/public/browser/browser_context.h" | 35 #include "content/public/browser/browser_context.h" |
| 34 #include "content/public/browser/content_browser_client.h" | 36 #include "content/public/browser/content_browser_client.h" |
| 35 #include "content/public/browser/download_interrupt_reasons.h" | 37 #include "content/public/browser/download_interrupt_reasons.h" |
| 36 #include "content/public/browser/download_manager_delegate.h" | 38 #include "content/public/browser/download_manager_delegate.h" |
| 37 #include "content/public/browser/download_url_parameters.h" | 39 #include "content/public/browser/download_url_parameters.h" |
| 38 #include "content/public/browser/notification_service.h" | 40 #include "content/public/browser/notification_service.h" |
| 39 #include "content/public/browser/notification_types.h" | 41 #include "content/public/browser/notification_types.h" |
| 40 #include "content/public/browser/render_process_host.h" | 42 #include "content/public/browser/render_process_host.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 66 if (blob_data_handle) { | 68 if (blob_data_handle) { |
| 67 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( | 69 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( |
| 68 url_request.get(), std::move(blob_data_handle)); | 70 url_request.get(), std::move(blob_data_handle)); |
| 69 } | 71 } |
| 70 | 72 |
| 71 // If there's a valid renderer process associated with the request, then the | 73 // If there's a valid renderer process associated with the request, then the |
| 72 // request should be driven by the ResourceLoader. Pass it over to the | 74 // request should be driven by the ResourceLoader. Pass it over to the |
| 73 // ResourceDispatcherHostImpl which will in turn pass it along to the | 75 // ResourceDispatcherHostImpl which will in turn pass it along to the |
| 74 // ResourceLoader. | 76 // ResourceLoader. |
| 75 if (params->render_process_host_id() >= 0) { | 77 if (params->render_process_host_id() >= 0) { |
| 76 DownloadInterruptReason reason = | 78 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest( |
| 77 ResourceDispatcherHostImpl::Get()->BeginDownload( | 79 std::move(url_request), params->referrer(), resource_context, |
| 78 std::move(url_request), params->referrer(), | 80 params->content_initiated(), params->render_process_host_id(), |
| 79 params->content_initiated(), resource_context, | 81 params->render_view_host_routing_id(), |
| 80 params->render_process_host_id(), | 82 params->render_frame_host_routing_id(), |
| 81 params->render_view_host_routing_id(), | 83 params->do_not_prompt_for_login()); |
| 82 params->render_frame_host_routing_id(), | |
| 83 params->do_not_prompt_for_login()); | |
| 84 | 84 |
| 85 // If the download was accepted, the DownloadResourceHandler is now | 85 // If the download was accepted, the DownloadResourceHandler is now |
| 86 // responsible for driving the request to completion. | 86 // responsible for driving the request to completion. |
| 87 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) | 87 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 88 return nullptr; | 88 return nullptr; |
| 89 | 89 |
| 90 // Otherwise, create an interrupted download. | 90 // Otherwise, create an interrupted download. |
| 91 std::unique_ptr<DownloadCreateInfo> failed_created_info( | 91 std::unique_ptr<DownloadCreateInfo> failed_created_info( |
| 92 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), | 92 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), |
| 93 base::WrapUnique(new DownloadSaveInfo))); | 93 base::WrapUnique(new DownloadSaveInfo))); |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { | 514 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { |
| 515 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); | 515 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); |
| 516 ++ptr) { | 516 ++ptr) { |
| 517 if (ptr->get() == downloader) { | 517 if (ptr->get() == downloader) { |
| 518 url_downloaders_.erase(ptr); | 518 url_downloaders_.erase(ptr); |
| 519 return; | 519 return; |
| 520 } | 520 } |
| 521 } | 521 } |
| 522 } | 522 } |
| 523 | 523 |
| 524 // static |
| 525 DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest( |
| 526 std::unique_ptr<net::URLRequest> url_request, |
| 527 const Referrer& referrer, |
| 528 ResourceContext* resource_context, |
| 529 bool is_content_initiated, |
| 530 int render_process_id, |
| 531 int render_view_route_id, |
| 532 int render_frame_route_id, |
| 533 bool do_not_prompt_for_login) { |
| 534 if (ResourceDispatcherHostImpl::Get()->is_shutdown()) |
| 535 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; |
| 536 |
| 537 // The URLRequest needs to be initialized with the referrer and other |
| 538 // information prior to issuing it. |
| 539 ResourceDispatcherHostImpl::Get()->InitializeURLRequest( |
| 540 url_request.get(), referrer, |
| 541 true, // download. |
| 542 render_process_id, render_view_route_id, render_frame_route_id, |
| 543 resource_context); |
| 544 |
| 545 // We treat a download as a main frame load, and thus update the policy URL on |
| 546 // redirects. |
| 547 // |
| 548 // TODO(davidben): Is this correct? If this came from a |
| 549 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set |
| 550 // appropriately? |
| 551 url_request->set_first_party_url_policy( |
| 552 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 553 |
| 554 const GURL& url = url_request->original_url(); |
| 555 |
| 556 // Check if the renderer is permitted to request the requested URL. |
| 557 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( |
| 558 render_process_id, url)) { |
| 559 DVLOG(1) << "Denied unauthorized download request for " |
| 560 << url.possibly_invalid_spec(); |
| 561 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; |
| 562 } |
| 563 |
| 564 const net::URLRequestContext* request_context = url_request->context(); |
| 565 if (!request_context->job_factory()->IsHandledURL(url)) { |
| 566 DVLOG(1) << "Download request for unsupported protocol: " |
| 567 << url.possibly_invalid_spec(); |
| 568 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; |
| 569 } |
| 570 |
| 571 // From this point forward, the |DownloadResourceHandler| is responsible for |
| 572 // |started_callback|. |
| 573 // TODO(ananta) |
| 574 // Find a better way to create the DownloadResourceHandler instance. |
| 575 std::unique_ptr<ResourceHandler> handler( |
| 576 DownloadResourceHandler::Create(url_request.get())); |
| 577 |
| 578 ResourceDispatcherHostImpl::Get()->BeginURLRequest( |
| 579 std::move(url_request), std::move(handler), true, // download |
| 580 is_content_initiated, do_not_prompt_for_login, resource_context); |
| 581 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 582 } |
| 583 |
| 524 namespace { | 584 namespace { |
| 525 | 585 |
| 526 bool EmptyFilter(const GURL& url) { | 586 bool EmptyFilter(const GURL& url) { |
| 527 return true; | 587 return true; |
| 528 } | 588 } |
| 529 | 589 |
| 530 bool RemoveDownloadByURLAndTime( | 590 bool RemoveDownloadByURLAndTime( |
| 531 const base::Callback<bool(const GURL&)>& url_filter, | 591 const base::Callback<bool(const GURL&)>& url_filter, |
| 532 base::Time remove_begin, | 592 base::Time remove_begin, |
| 533 base::Time remove_end, | 593 base::Time remove_end, |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 if (delegate_) | 755 if (delegate_) |
| 696 delegate_->OpenDownload(download); | 756 delegate_->OpenDownload(download); |
| 697 } | 757 } |
| 698 | 758 |
| 699 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 759 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
| 700 if (delegate_) | 760 if (delegate_) |
| 701 delegate_->ShowDownloadInShell(download); | 761 delegate_->ShowDownloadInShell(download); |
| 702 } | 762 } |
| 703 | 763 |
| 704 } // namespace content | 764 } // namespace content |
| OLD | NEW |