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 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 68 std::unique_ptr<ResourceHandler> handler( |
| 69 new DownloadResourceHandler(request)); |
| 70 return handler; |
| 71 } |
| 72 |
55 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( | 73 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( |
56 std::unique_ptr<DownloadUrlParameters> params, | 74 std::unique_ptr<DownloadUrlParameters> params, |
57 content::ResourceContext* resource_context, | 75 content::ResourceContext* resource_context, |
58 uint32_t download_id, | 76 uint32_t download_id, |
59 base::WeakPtr<DownloadManagerImpl> download_manager) { | 77 base::WeakPtr<DownloadManagerImpl> download_manager) { |
60 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
61 | 79 |
62 std::unique_ptr<net::URLRequest> url_request = | 80 std::unique_ptr<net::URLRequest> url_request = |
63 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); | 81 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); |
64 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = | 82 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = |
65 params->GetBlobDataHandle(); | 83 params->GetBlobDataHandle(); |
66 if (blob_data_handle) { | 84 if (blob_data_handle) { |
67 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( | 85 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( |
68 url_request.get(), std::move(blob_data_handle)); | 86 url_request.get(), std::move(blob_data_handle)); |
69 } | 87 } |
70 | 88 |
71 // If there's a valid renderer process associated with the request, then the | 89 // 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 | 90 // request should be driven by the ResourceLoader. Pass it over to the |
73 // ResourceDispatcherHostImpl which will in turn pass it along to the | 91 // ResourceDispatcherHostImpl which will in turn pass it along to the |
74 // ResourceLoader. | 92 // ResourceLoader. |
75 if (params->render_process_host_id() >= 0) { | 93 if (params->render_process_host_id() >= 0) { |
76 DownloadInterruptReason reason = | 94 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest( |
77 ResourceDispatcherHostImpl::Get()->BeginDownload( | 95 std::move(url_request), params->referrer(), resource_context, |
78 std::move(url_request), params->referrer(), | 96 params->content_initiated(), params->render_process_host_id(), |
79 params->content_initiated(), resource_context, | 97 params->render_view_host_routing_id(), |
80 params->render_process_host_id(), | 98 params->render_frame_host_routing_id(), |
81 params->render_view_host_routing_id(), | 99 params->do_not_prompt_for_login()); |
82 params->render_frame_host_routing_id(), | |
83 params->do_not_prompt_for_login()); | |
84 | 100 |
85 // If the download was accepted, the DownloadResourceHandler is now | 101 // If the download was accepted, the DownloadResourceHandler is now |
86 // responsible for driving the request to completion. | 102 // responsible for driving the request to completion. |
87 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) | 103 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
88 return nullptr; | 104 return nullptr; |
89 | 105 |
90 // Otherwise, create an interrupted download. | 106 // Otherwise, create an interrupted download. |
91 std::unique_ptr<DownloadCreateInfo> failed_created_info( | 107 std::unique_ptr<DownloadCreateInfo> failed_created_info( |
92 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), | 108 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), |
93 base::WrapUnique(new DownloadSaveInfo))); | 109 base::WrapUnique(new DownloadSaveInfo))); |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { | 530 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { |
515 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); | 531 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); |
516 ++ptr) { | 532 ++ptr) { |
517 if (ptr->get() == downloader) { | 533 if (ptr->get() == downloader) { |
518 url_downloaders_.erase(ptr); | 534 url_downloaders_.erase(ptr); |
519 return; | 535 return; |
520 } | 536 } |
521 } | 537 } |
522 } | 538 } |
523 | 539 |
| 540 // static |
| 541 void DownloadManagerImpl::ResourceDispatcherHostCreated() { |
| 542 ResourceDispatcherHostImpl::Get()->RegisterCreateDownloadHandlerInterceptor( |
| 543 base::Bind(&CreateResourceHandlerForDownload)); |
| 544 } |
| 545 |
| 546 // static |
| 547 DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest( |
| 548 std::unique_ptr<net::URLRequest> url_request, |
| 549 const Referrer& referrer, |
| 550 ResourceContext* resource_context, |
| 551 bool is_content_initiated, |
| 552 int render_process_id, |
| 553 int render_view_route_id, |
| 554 int render_frame_route_id, |
| 555 bool do_not_prompt_for_login) { |
| 556 // We treat a download as a main frame load, and thus update the policy URL on |
| 557 // redirects. |
| 558 // |
| 559 // TODO(davidben): Is this correct? If this came from a |
| 560 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set |
| 561 // appropriately? |
| 562 url_request->set_first_party_url_policy( |
| 563 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 564 |
| 565 const GURL& url = url_request->original_url(); |
| 566 |
| 567 // Check if the renderer is permitted to request the requested URL. |
| 568 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( |
| 569 render_process_id, url)) { |
| 570 DVLOG(1) << "Denied unauthorized download request for " |
| 571 << url.possibly_invalid_spec(); |
| 572 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; |
| 573 } |
| 574 |
| 575 const net::URLRequestContext* request_context = url_request->context(); |
| 576 if (!request_context->job_factory()->IsHandledURL(url)) { |
| 577 DVLOG(1) << "Download request for unsupported protocol: " |
| 578 << url.possibly_invalid_spec(); |
| 579 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; |
| 580 } |
| 581 |
| 582 if (url.SchemeIs(url::kBlobScheme) && |
| 583 !storage::BlobProtocolHandler::GetRequestBlobDataHandle( |
| 584 url_request.get())) { |
| 585 ChromeBlobStorageContext* blob_context = |
| 586 GetChromeBlobStorageContextForResourceContext(resource_context); |
| 587 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( |
| 588 url_request.get(), |
| 589 blob_context->context()->GetBlobDataFromPublicURL(url)); |
| 590 } |
| 591 |
| 592 // From this point forward, the |DownloadResourceHandler| is responsible for |
| 593 // |started_callback|. |
| 594 // TODO(ananta) |
| 595 // Find a better way to create the DownloadResourceHandler instance. |
| 596 std::unique_ptr<ResourceHandler> handler( |
| 597 CreateResourceHandlerForDownload(url_request.get())); |
| 598 |
| 599 ResourceDispatcherHostImpl::URLRequestStatus ret = |
| 600 ResourceDispatcherHostImpl::Get()->BeginURLRequest( |
| 601 std::move(url_request), std::move(handler), referrer, |
| 602 true, // download |
| 603 is_content_initiated, do_not_prompt_for_login, render_process_id, |
| 604 render_view_route_id, render_frame_route_id, resource_context); |
| 605 if (ret == ResourceDispatcherHostImpl::URL_REQUEST_FAILED_SHUTDOWN) |
| 606 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; |
| 607 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 608 } |
| 609 |
524 namespace { | 610 namespace { |
525 | 611 |
526 bool EmptyFilter(const GURL& url) { | 612 bool EmptyFilter(const GURL& url) { |
527 return true; | 613 return true; |
528 } | 614 } |
529 | 615 |
530 bool RemoveDownloadByURLAndTime( | 616 bool RemoveDownloadByURLAndTime( |
531 const base::Callback<bool(const GURL&)>& url_filter, | 617 const base::Callback<bool(const GURL&)>& url_filter, |
532 base::Time remove_begin, | 618 base::Time remove_begin, |
533 base::Time remove_end, | 619 base::Time remove_end, |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 if (delegate_) | 781 if (delegate_) |
696 delegate_->OpenDownload(download); | 782 delegate_->OpenDownload(download); |
697 } | 783 } |
698 | 784 |
699 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 785 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
700 if (delegate_) | 786 if (delegate_) |
701 delegate_->ShowDownloadInShell(download); | 787 delegate_->ShowDownloadInShell(download); |
702 } | 788 } |
703 | 789 |
704 } // namespace content | 790 } // namespace content |
OLD | NEW |