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/blob_storage/chrome_blob_storage_context.h" | |
24 #include "content/browser/byte_stream.h" | 25 #include "content/browser/byte_stream.h" |
26 #include "content/browser/child_process_security_policy_impl.h" | |
25 #include "content/browser/download/download_create_info.h" | 27 #include "content/browser/download/download_create_info.h" |
26 #include "content/browser/download/download_file_factory.h" | 28 #include "content/browser/download/download_file_factory.h" |
27 #include "content/browser/download/download_item_factory.h" | 29 #include "content/browser/download/download_item_factory.h" |
28 #include "content/browser/download/download_item_impl.h" | 30 #include "content/browser/download/download_item_impl.h" |
29 #include "content/browser/download/download_stats.h" | 31 #include "content/browser/download/download_stats.h" |
30 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 32 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
33 #include "content/browser/loader/resource_request_info_impl.h" | |
34 #include "content/browser/loader/throttling_resource_handler.h" | |
31 #include "content/browser/renderer_host/render_view_host_impl.h" | 35 #include "content/browser/renderer_host/render_view_host_impl.h" |
36 #include "content/browser/resource_context_impl.h" | |
32 #include "content/browser/web_contents/web_contents_impl.h" | 37 #include "content/browser/web_contents/web_contents_impl.h" |
33 #include "content/public/browser/browser_context.h" | 38 #include "content/public/browser/browser_context.h" |
34 #include "content/public/browser/content_browser_client.h" | 39 #include "content/public/browser/content_browser_client.h" |
35 #include "content/public/browser/download_interrupt_reasons.h" | 40 #include "content/public/browser/download_interrupt_reasons.h" |
36 #include "content/public/browser/download_manager_delegate.h" | 41 #include "content/public/browser/download_manager_delegate.h" |
37 #include "content/public/browser/download_url_parameters.h" | 42 #include "content/public/browser/download_url_parameters.h" |
38 #include "content/public/browser/notification_service.h" | 43 #include "content/public/browser/notification_service.h" |
39 #include "content/public/browser/notification_types.h" | 44 #include "content/public/browser/notification_types.h" |
40 #include "content/public/browser/render_process_host.h" | 45 #include "content/public/browser/render_process_host.h" |
41 #include "content/public/browser/resource_context.h" | 46 #include "content/public/browser/resource_context.h" |
47 #include "content/public/browser/resource_dispatcher_host_delegate.h" | |
48 #include "content/public/browser/resource_throttle.h" | |
42 #include "content/public/browser/web_contents_delegate.h" | 49 #include "content/public/browser/web_contents_delegate.h" |
43 #include "content/public/common/referrer.h" | 50 #include "content/public/common/referrer.h" |
44 #include "net/base/elements_upload_data_stream.h" | 51 #include "net/base/elements_upload_data_stream.h" |
45 #include "net/base/load_flags.h" | 52 #include "net/base/load_flags.h" |
46 #include "net/base/request_priority.h" | 53 #include "net/base/request_priority.h" |
47 #include "net/base/upload_bytes_element_reader.h" | 54 #include "net/base/upload_bytes_element_reader.h" |
48 #include "net/url_request/url_request_context.h" | 55 #include "net/url_request/url_request_context.h" |
56 #include "storage/browser/blob/blob_storage_context.h" | |
49 #include "storage/browser/blob/blob_url_request_job_factory.h" | 57 #include "storage/browser/blob/blob_url_request_job_factory.h" |
50 #include "url/origin.h" | 58 #include "url/origin.h" |
51 | 59 |
52 namespace content { | 60 namespace content { |
53 namespace { | 61 namespace { |
54 | 62 |
63 // Helper function to create an instance of the DownloadResourceHandler for | |
64 // handling download requests. | |
65 std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload( | |
66 net::URLRequest* request, | |
67 bool is_content_initiated, | |
68 bool must_download) { | |
Randy Smith (Not in Mondays)
2016/08/17 15:53:54
I believe this function needs to be called on the
ananta
2016/08/17 20:31:36
Added DCHECK's
| |
69 std::unique_ptr<ResourceHandler> handler( | |
70 new DownloadResourceHandler(request)); | |
71 if (ResourceDispatcherHostImpl::Get()->delegate()) { | |
72 const ResourceRequestInfoImpl* request_info( | |
73 ResourceRequestInfoImpl::ForRequest(request)); | |
74 | |
75 ScopedVector<ResourceThrottle> throttles; | |
76 ResourceDispatcherHostImpl::Get()->delegate()->DownloadStarting( | |
77 request, request_info->GetContext(), request_info->GetChildID(), | |
78 request_info->GetRouteID(), is_content_initiated, must_download, | |
79 &throttles); | |
80 if (!throttles.empty()) { | |
81 handler.reset(new ThrottlingResourceHandler(std::move(handler), request, | |
82 std::move(throttles))); | |
83 } | |
84 } | |
85 return handler; | |
86 } | |
87 | |
55 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( | 88 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( |
56 std::unique_ptr<DownloadUrlParameters> params, | 89 std::unique_ptr<DownloadUrlParameters> params, |
57 content::ResourceContext* resource_context, | 90 content::ResourceContext* resource_context, |
58 uint32_t download_id, | 91 uint32_t download_id, |
59 base::WeakPtr<DownloadManagerImpl> download_manager) { | 92 base::WeakPtr<DownloadManagerImpl> download_manager) { |
60 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
61 | 94 |
62 std::unique_ptr<net::URLRequest> url_request = | 95 std::unique_ptr<net::URLRequest> url_request = |
63 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); | 96 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); |
64 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = | 97 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = |
65 params->GetBlobDataHandle(); | 98 params->GetBlobDataHandle(); |
66 if (blob_data_handle) { | 99 if (blob_data_handle) { |
67 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( | 100 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( |
68 url_request.get(), std::move(blob_data_handle)); | 101 url_request.get(), std::move(blob_data_handle)); |
69 } | 102 } |
70 | 103 |
71 // If there's a valid renderer process associated with the request, then the | 104 // 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 | 105 // request should be driven by the ResourceLoader. Pass it over to the |
73 // ResourceDispatcherHostImpl which will in turn pass it along to the | 106 // ResourceDispatcherHostImpl which will in turn pass it along to the |
74 // ResourceLoader. | 107 // ResourceLoader. |
75 if (params->render_process_host_id() >= 0) { | 108 if (params->render_process_host_id() >= 0) { |
76 DownloadInterruptReason reason = | 109 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest( |
Randy Smith (Not in Mondays)
2016/08/17 15:53:54
If this is the only place this function is called
ananta
2016/08/18 01:53:25
This function is also called from resource_dispatc
Randy Smith (Not in Mondays)
2016/08/18 15:33:57
Given that the function is no longer in rdhi.cc, s
| |
77 ResourceDispatcherHostImpl::Get()->BeginDownload( | 110 std::move(url_request), |
78 std::move(url_request), params->referrer(), | 111 params->referrer(), |
79 params->content_initiated(), resource_context, | 112 resource_context, |
80 params->render_process_host_id(), | 113 params->content_initiated(), |
81 params->render_view_host_routing_id(), | 114 params->render_process_host_id(), |
82 params->render_frame_host_routing_id(), | 115 params->render_view_host_routing_id(), |
83 params->do_not_prompt_for_login()); | 116 params->render_frame_host_routing_id(), |
117 params->do_not_prompt_for_login()); | |
84 | 118 |
85 // If the download was accepted, the DownloadResourceHandler is now | 119 // If the download was accepted, the DownloadResourceHandler is now |
86 // responsible for driving the request to completion. | 120 // responsible for driving the request to completion. |
87 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) | 121 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
88 return nullptr; | 122 return nullptr; |
89 | 123 |
90 // Otherwise, create an interrupted download. | 124 // Otherwise, create an interrupted download. |
91 std::unique_ptr<DownloadCreateInfo> failed_created_info( | 125 std::unique_ptr<DownloadCreateInfo> failed_created_info( |
92 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), | 126 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), |
93 base::WrapUnique(new DownloadSaveInfo))); | 127 base::WrapUnique(new DownloadSaveInfo))); |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
514 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { | 548 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { |
515 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); | 549 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); |
516 ++ptr) { | 550 ++ptr) { |
517 if (ptr->get() == downloader) { | 551 if (ptr->get() == downloader) { |
518 url_downloaders_.erase(ptr); | 552 url_downloaders_.erase(ptr); |
519 return; | 553 return; |
520 } | 554 } |
521 } | 555 } |
522 } | 556 } |
523 | 557 |
558 // static | |
559 void DownloadManagerImpl::ResourceDispatcherHostCreated() { | |
560 ResourceDispatcherHostImpl::Get()->RegisterCreateDownloadHandlerInterceptor( | |
561 base::Bind(&CreateResourceHandlerForDownload)); | |
562 } | |
563 | |
564 // static | |
565 DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest( | |
566 std::unique_ptr<net::URLRequest> url_request, | |
567 const Referrer& referrer, | |
568 ResourceContext* resource_context, | |
569 bool is_content_initiated, | |
570 int render_process_id, | |
571 int render_view_route_id, | |
572 int render_frame_route_id, | |
573 bool do_not_prompt_for_login) { | |
574 if (ResourceDispatcherHostImpl::Get()->is_shutdown()) | |
575 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | |
576 | |
577 if (referrer.url.is_valid()) | |
578 url_request->SetReferrer(referrer.url.spec()); | |
579 | |
580 // We treat a download as a main frame load, and thus update the policy URL on | |
581 // redirects. | |
582 // | |
583 // TODO(davidben): Is this correct? If this came from a | |
584 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set | |
585 // appropriately? | |
586 url_request->set_first_party_url_policy( | |
587 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); | |
588 | |
589 const GURL& url = url_request->original_url(); | |
590 | |
591 // Check if the renderer is permitted to request the requested URL. | |
592 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( | |
593 render_process_id, url)) { | |
594 DVLOG(1) << "Denied unauthorized download request for " | |
595 << url.possibly_invalid_spec(); | |
596 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; | |
597 } | |
598 | |
599 ResourceRequestInfoImpl* extra_info = | |
600 ResourceDispatcherHostImpl::Get()->CreateRequestInfo( | |
601 render_process_id, | |
602 render_view_route_id, | |
603 render_frame_route_id, | |
604 true, | |
605 resource_context); | |
606 extra_info->set_do_not_prompt_for_login(do_not_prompt_for_login); | |
607 // Request takes ownership. | |
608 extra_info->AssociateWithRequest(url_request.get()); | |
609 | |
610 if (url.SchemeIs(url::kBlobScheme) && | |
611 !storage::BlobProtocolHandler::GetRequestBlobDataHandle( | |
612 url_request.get())) { | |
613 ChromeBlobStorageContext* blob_context = | |
614 GetChromeBlobStorageContextForResourceContext(resource_context); | |
615 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( | |
616 url_request.get(), | |
617 blob_context->context()->GetBlobDataFromPublicURL(url)); | |
618 } | |
619 | |
620 // From this point forward, the |DownloadResourceHandler| is responsible for | |
621 // |started_callback|. | |
622 std::unique_ptr<ResourceHandler> handler( | |
623 ResourceDispatcherHostImpl::Get()->CreateResourceHandlerForDownload( | |
624 url_request.get(), is_content_initiated, true)); | |
625 | |
626 ResourceDispatcherHostImpl::Get()->BeginURLRequest(std::move(url_request), | |
627 std::move(handler)); | |
628 return DOWNLOAD_INTERRUPT_REASON_NONE; | |
629 } | |
630 | |
524 namespace { | 631 namespace { |
525 | 632 |
526 bool EmptyFilter(const GURL& url) { | 633 bool EmptyFilter(const GURL& url) { |
527 return true; | 634 return true; |
528 } | 635 } |
529 | 636 |
530 bool RemoveDownloadByURLAndTime( | 637 bool RemoveDownloadByURLAndTime( |
531 const base::Callback<bool(const GURL&)>& url_filter, | 638 const base::Callback<bool(const GURL&)>& url_filter, |
532 base::Time remove_begin, | 639 base::Time remove_begin, |
533 base::Time remove_end, | 640 base::Time remove_end, |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
695 if (delegate_) | 802 if (delegate_) |
696 delegate_->OpenDownload(download); | 803 delegate_->OpenDownload(download); |
697 } | 804 } |
698 | 805 |
699 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 806 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
700 if (delegate_) | 807 if (delegate_) |
701 delegate_->ShowDownloadInShell(download); | 808 delegate_->ShowDownloadInShell(download); |
702 } | 809 } |
703 | 810 |
704 } // namespace content | 811 } // namespace content |
OLD | NEW |