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) { | |
69 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
70 | |
71 std::unique_ptr<ResourceHandler> handler( | |
72 new DownloadResourceHandler(request)); | |
73 if (ResourceDispatcherHostImpl::Get()->delegate()) { | |
Randy Smith (Not in Mondays)
2016/08/18 17:38:57
I don't think it makes sense to move this call and
ananta
2016/08/18 22:27:09
Please correct me if I am wrong here. The Resource
Randy Smith (Not in Mondays)
2016/08/21 23:32:57
No, that's right. Another way of putting this is
ananta
2016/08/22 19:14:57
It does seem plausible that we may want to have Do
ananta
2016/08/23 04:27:38
Discussed this a bit with jam. The goal of the net
Randy Smith (Not in Mondays)
2016/08/23 19:54:45
It's ok with me--I just want to have a clear idea
Randy Smith (Not in Mondays)
2016/08/23 19:54:45
So this strikes me as orthogonal to the issues we
| |
74 const ResourceRequestInfoImpl* request_info( | |
75 ResourceRequestInfoImpl::ForRequest(request)); | |
76 | |
77 ScopedVector<ResourceThrottle> throttles; | |
78 ResourceDispatcherHostImpl::Get()->delegate()->DownloadStarting( | |
79 request, request_info->GetContext(), request_info->GetChildID(), | |
80 request_info->GetRouteID(), is_content_initiated, must_download, | |
81 &throttles); | |
82 if (!throttles.empty()) { | |
83 handler.reset(new ThrottlingResourceHandler(std::move(handler), request, | |
84 std::move(throttles))); | |
85 } | |
86 } | |
87 return handler; | |
88 } | |
89 | |
55 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( | 90 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( |
56 std::unique_ptr<DownloadUrlParameters> params, | 91 std::unique_ptr<DownloadUrlParameters> params, |
57 content::ResourceContext* resource_context, | 92 content::ResourceContext* resource_context, |
58 uint32_t download_id, | 93 uint32_t download_id, |
59 base::WeakPtr<DownloadManagerImpl> download_manager) { | 94 base::WeakPtr<DownloadManagerImpl> download_manager) { |
60 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 95 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
61 | 96 |
62 std::unique_ptr<net::URLRequest> url_request = | 97 std::unique_ptr<net::URLRequest> url_request = |
63 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); | 98 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); |
64 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = | 99 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = |
65 params->GetBlobDataHandle(); | 100 params->GetBlobDataHandle(); |
66 if (blob_data_handle) { | 101 if (blob_data_handle) { |
67 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( | 102 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( |
68 url_request.get(), std::move(blob_data_handle)); | 103 url_request.get(), std::move(blob_data_handle)); |
69 } | 104 } |
70 | 105 |
71 // If there's a valid renderer process associated with the request, then the | 106 // 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 | 107 // request should be driven by the ResourceLoader. Pass it over to the |
73 // ResourceDispatcherHostImpl which will in turn pass it along to the | 108 // ResourceDispatcherHostImpl which will in turn pass it along to the |
74 // ResourceLoader. | 109 // ResourceLoader. |
75 if (params->render_process_host_id() >= 0) { | 110 if (params->render_process_host_id() >= 0) { |
76 DownloadInterruptReason reason = | 111 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest( |
77 ResourceDispatcherHostImpl::Get()->BeginDownload( | 112 std::move(url_request), |
78 std::move(url_request), params->referrer(), | 113 params->referrer(), |
79 params->content_initiated(), resource_context, | 114 resource_context, |
80 params->render_process_host_id(), | 115 params->content_initiated(), |
81 params->render_view_host_routing_id(), | 116 params->render_process_host_id(), |
82 params->render_frame_host_routing_id(), | 117 params->render_view_host_routing_id(), |
83 params->do_not_prompt_for_login()); | 118 params->render_frame_host_routing_id(), |
119 params->do_not_prompt_for_login()); | |
84 | 120 |
85 // If the download was accepted, the DownloadResourceHandler is now | 121 // If the download was accepted, the DownloadResourceHandler is now |
86 // responsible for driving the request to completion. | 122 // responsible for driving the request to completion. |
87 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) | 123 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
88 return nullptr; | 124 return nullptr; |
89 | 125 |
90 // Otherwise, create an interrupted download. | 126 // Otherwise, create an interrupted download. |
91 std::unique_ptr<DownloadCreateInfo> failed_created_info( | 127 std::unique_ptr<DownloadCreateInfo> failed_created_info( |
92 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), | 128 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), |
93 base::WrapUnique(new DownloadSaveInfo))); | 129 base::WrapUnique(new DownloadSaveInfo))); |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
514 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { | 550 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { |
515 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); | 551 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); |
516 ++ptr) { | 552 ++ptr) { |
517 if (ptr->get() == downloader) { | 553 if (ptr->get() == downloader) { |
518 url_downloaders_.erase(ptr); | 554 url_downloaders_.erase(ptr); |
519 return; | 555 return; |
520 } | 556 } |
521 } | 557 } |
522 } | 558 } |
523 | 559 |
560 // static | |
561 void DownloadManagerImpl::ResourceDispatcherHostCreated() { | |
562 ResourceDispatcherHostImpl::Get()->RegisterCreateDownloadHandlerInterceptor( | |
563 base::Bind(&CreateResourceHandlerForDownload)); | |
564 } | |
565 | |
566 // static | |
567 DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest( | |
568 std::unique_ptr<net::URLRequest> url_request, | |
569 const Referrer& referrer, | |
570 ResourceContext* resource_context, | |
571 bool is_content_initiated, | |
572 int render_process_id, | |
573 int render_view_route_id, | |
574 int render_frame_route_id, | |
575 bool do_not_prompt_for_login) { | |
576 if (ResourceDispatcherHostImpl::Get()->is_shutdown()) | |
Randy Smith (Not in Mondays)
2016/08/18 17:38:57
Can this be pulled down into RDHI::BeginURLRequest
ananta
2016/08/18 22:27:09
Done.
Randy Smith (Not in Mondays)
2016/08/21 23:32:57
I apologize, but I'd like to take this request bac
ananta
2016/08/22 19:14:57
Sure thing. Reverted to BeginURLRequest being a vo
| |
577 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | |
578 | |
579 // We treat a download as a main frame load, and thus update the policy URL on | |
580 // redirects. | |
581 // | |
582 // TODO(davidben): Is this correct? If this came from a | |
583 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set | |
584 // appropriately? | |
585 url_request->set_first_party_url_policy( | |
586 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); | |
587 | |
588 const GURL& url = url_request->original_url(); | |
589 | |
590 // Check if the renderer is permitted to request the requested URL. | |
591 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( | |
592 render_process_id, url)) { | |
593 DVLOG(1) << "Denied unauthorized download request for " | |
594 << url.possibly_invalid_spec(); | |
595 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; | |
596 } | |
597 | |
598 ResourceRequestInfoImpl* extra_info = | |
599 ResourceDispatcherHostImpl::Get()->CreateRequestInfo( | |
600 render_process_id, | |
601 render_view_route_id, | |
602 render_frame_route_id, | |
603 true, | |
604 resource_context); | |
Randy Smith (Not in Mondays)
2016/08/18 17:38:57
Can these simply be added to the interface for Beg
ananta
2016/08/18 22:27:09
Done. I left the core of the logic in BeginURLRequ
| |
605 extra_info->set_do_not_prompt_for_login(do_not_prompt_for_login); | |
606 // Request takes ownership. | |
607 extra_info->AssociateWithRequest(url_request.get()); | |
608 | |
609 if (url.SchemeIs(url::kBlobScheme) && | |
610 !storage::BlobProtocolHandler::GetRequestBlobDataHandle( | |
611 url_request.get())) { | |
612 ChromeBlobStorageContext* blob_context = | |
613 GetChromeBlobStorageContextForResourceContext(resource_context); | |
614 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( | |
615 url_request.get(), | |
616 blob_context->context()->GetBlobDataFromPublicURL(url)); | |
617 } | |
618 | |
619 // From this point forward, the |DownloadResourceHandler| is responsible for | |
620 // |started_callback|. | |
621 std::unique_ptr<ResourceHandler> handler( | |
622 ResourceDispatcherHostImpl::Get()->CreateResourceHandlerForDownload( | |
623 url_request.get(), is_content_initiated, true)); | |
Randy Smith (Not in Mondays)
2016/08/18 17:38:57
It feels broken for download code to delegate crea
ananta
2016/08/18 22:27:09
Added a TODO here. The CreateResourceHandlerForDow
| |
624 | |
625 ResourceDispatcherHostImpl::Get()->BeginURLRequest(std::move(url_request), | |
626 referrer, | |
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 |