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 |
55 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( | 63 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( |
56 std::unique_ptr<DownloadUrlParameters> params, | 64 std::unique_ptr<DownloadUrlParameters> params, |
57 content::ResourceContext* resource_context, | 65 content::ResourceContext* resource_context, |
58 uint32_t download_id, | 66 uint32_t download_id, |
59 base::WeakPtr<DownloadManagerImpl> download_manager) { | 67 base::WeakPtr<DownloadManagerImpl> download_manager) { |
60 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 68 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
61 | 69 |
62 std::unique_ptr<net::URLRequest> url_request = | 70 std::unique_ptr<net::URLRequest> url_request = |
63 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); | 71 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); |
64 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = | 72 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = |
65 params->GetBlobDataHandle(); | 73 params->GetBlobDataHandle(); |
66 if (blob_data_handle) { | 74 if (blob_data_handle) { |
67 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( | 75 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( |
68 url_request.get(), std::move(blob_data_handle)); | 76 url_request.get(), std::move(blob_data_handle)); |
69 } | 77 } |
70 | 78 |
71 // If there's a valid renderer process associated with the request, then the | 79 // 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 | 80 // request should be driven by the ResourceLoader. Pass it over to the |
73 // ResourceDispatcherHostImpl which will in turn pass it along to the | 81 // ResourceDispatcherHostImpl which will in turn pass it along to the |
74 // ResourceLoader. | 82 // ResourceLoader. |
75 if (params->render_process_host_id() >= 0) { | 83 if (params->render_process_host_id() >= 0) { |
76 DownloadInterruptReason reason = | 84 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest( |
77 ResourceDispatcherHostImpl::Get()->BeginDownload( | 85 std::move(url_request), params->referrer(), resource_context, |
78 std::move(url_request), params->referrer(), | 86 params->content_initiated(), params->render_process_host_id(), |
79 params->content_initiated(), resource_context, | 87 params->render_view_host_routing_id(), |
80 params->render_process_host_id(), | 88 params->render_frame_host_routing_id(), |
81 params->render_view_host_routing_id(), | 89 params->do_not_prompt_for_login()); |
82 params->render_frame_host_routing_id(), | |
83 params->do_not_prompt_for_login()); | |
84 | 90 |
85 // If the download was accepted, the DownloadResourceHandler is now | 91 // If the download was accepted, the DownloadResourceHandler is now |
86 // responsible for driving the request to completion. | 92 // responsible for driving the request to completion. |
87 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) | 93 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
88 return nullptr; | 94 return nullptr; |
89 | 95 |
90 // Otherwise, create an interrupted download. | 96 // Otherwise, create an interrupted download. |
91 std::unique_ptr<DownloadCreateInfo> failed_created_info( | 97 std::unique_ptr<DownloadCreateInfo> failed_created_info( |
92 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), | 98 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), |
93 base::WrapUnique(new DownloadSaveInfo))); | 99 base::WrapUnique(new DownloadSaveInfo))); |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
514 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { | 520 void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { |
515 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); | 521 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); |
516 ++ptr) { | 522 ++ptr) { |
517 if (ptr->get() == downloader) { | 523 if (ptr->get() == downloader) { |
518 url_downloaders_.erase(ptr); | 524 url_downloaders_.erase(ptr); |
519 return; | 525 return; |
520 } | 526 } |
521 } | 527 } |
522 } | 528 } |
523 | 529 |
530 // static | |
531 DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest( | |
532 std::unique_ptr<net::URLRequest> url_request, | |
533 const Referrer& referrer, | |
534 ResourceContext* resource_context, | |
535 bool is_content_initiated, | |
536 int render_process_id, | |
537 int render_view_route_id, | |
538 int render_frame_route_id, | |
539 bool do_not_prompt_for_login) { | |
540 if (ResourceDispatcherHostImpl::Get()->is_shutdown()) | |
541 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; | |
542 | |
543 // The URLRequest needs to be initialized with the referrer and other | |
544 // information prior to issuing it. | |
545 ResourceDispatcherHostImpl::Get()->InitializeURLRequest( | |
546 url_request.get(), referrer, | |
547 true, // download. | |
548 render_process_id, render_view_route_id, render_frame_route_id, | |
549 resource_context); | |
550 | |
551 // We treat a download as a main frame load, and thus update the policy URL on | |
552 // redirects. | |
553 // | |
554 // TODO(davidben): Is this correct? If this came from a | |
555 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set | |
556 // appropriately? | |
557 url_request->set_first_party_url_policy( | |
558 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); | |
559 | |
560 const GURL& url = url_request->original_url(); | |
561 | |
562 // Check if the renderer is permitted to request the requested URL. | |
563 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( | |
564 render_process_id, url)) { | |
565 DVLOG(1) << "Denied unauthorized download request for " | |
566 << url.possibly_invalid_spec(); | |
567 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; | |
568 } | |
569 | |
570 const net::URLRequestContext* request_context = url_request->context(); | |
571 if (!request_context->job_factory()->IsHandledURL(url)) { | |
572 DVLOG(1) << "Download request for unsupported protocol: " | |
573 << url.possibly_invalid_spec(); | |
574 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; | |
575 } | |
576 | |
577 if (url.SchemeIs(url::kBlobScheme) && | |
578 !storage::BlobProtocolHandler::GetRequestBlobDataHandle( | |
579 url_request.get())) { | |
580 ChromeBlobStorageContext* blob_context = | |
581 GetChromeBlobStorageContextForResourceContext(resource_context); | |
582 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( | |
583 url_request.get(), | |
584 blob_context->context()->GetBlobDataFromPublicURL(url)); | |
585 } | |
Randy Smith (Not in Mondays)
2016/08/24 14:34:21
Is this not common code with the other initiation
ananta
2016/08/24 18:52:03
The code which is common between the SaveFileManag
mmenke
2016/08/25 19:10:25
And what about blob logic? The download system re
Randy Smith (Not in Mondays)
2016/08/25 19:12:38
Yes, I was specifically referring to the blob logi
ananta
2016/08/25 20:03:01
Yes. It makes sense for that code to move back int
ananta
2016/08/25 20:03:01
Done.
| |
586 | |
587 // From this point forward, the |DownloadResourceHandler| is responsible for | |
588 // |started_callback|. | |
589 // TODO(ananta) | |
590 // Find a better way to create the DownloadResourceHandler instance. | |
591 std::unique_ptr<ResourceHandler> handler( | |
592 DownloadResourceHandler::Create(url_request.get())); | |
593 | |
594 ResourceDispatcherHostImpl::Get()->BeginURLRequest( | |
595 std::move(url_request), std::move(handler), true, // download | |
596 is_content_initiated, do_not_prompt_for_login); | |
597 return DOWNLOAD_INTERRUPT_REASON_NONE; | |
598 } | |
599 | |
524 namespace { | 600 namespace { |
525 | 601 |
526 bool EmptyFilter(const GURL& url) { | 602 bool EmptyFilter(const GURL& url) { |
527 return true; | 603 return true; |
528 } | 604 } |
529 | 605 |
530 bool RemoveDownloadByURLAndTime( | 606 bool RemoveDownloadByURLAndTime( |
531 const base::Callback<bool(const GURL&)>& url_filter, | 607 const base::Callback<bool(const GURL&)>& url_filter, |
532 base::Time remove_begin, | 608 base::Time remove_begin, |
533 base::Time remove_end, | 609 base::Time remove_end, |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
695 if (delegate_) | 771 if (delegate_) |
696 delegate_->OpenDownload(download); | 772 delegate_->OpenDownload(download); |
697 } | 773 } |
698 | 774 |
699 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 775 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
700 if (delegate_) | 776 if (delegate_) |
701 delegate_->ShowDownloadInShell(download); | 777 delegate_->ShowDownloadInShell(download); |
702 } | 778 } |
703 | 779 |
704 } // namespace content | 780 } // namespace content |
OLD | NEW |