Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: content/browser/download/download_manager_impl.cc

Issue 2251643003: Remove the BeginSaveFile and BeginDownload methods from ResourceDispatcherHostImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Initialize tab_info_ in OnStart instead of OnResponseStart because we need a central place where al… Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // We register an interceptor on the ResourceDispatcherHostImpl instance to
543 // intercept requests to create handlers for download requests.
544 // TODO(ananta)
545 // Find a better way to achieve this. Ideally we don't want knowledge of
546 // downloads in ResourceDispatcherHostImpl.
547 ResourceDispatcherHostImpl::Get()->RegisterCreateDownloadHandlerInterceptor(
548 base::Bind(&CreateResourceHandlerForDownload));
549 }
550
551 // static
552 DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest(
553 std::unique_ptr<net::URLRequest> url_request,
554 const Referrer& referrer,
555 ResourceContext* resource_context,
556 bool is_content_initiated,
557 int render_process_id,
558 int render_view_route_id,
559 int render_frame_route_id,
560 bool do_not_prompt_for_login) {
561 // We treat a download as a main frame load, and thus update the policy URL on
562 // redirects.
563 //
564 // TODO(davidben): Is this correct? If this came from a
565 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
566 // appropriately?
567 url_request->set_first_party_url_policy(
568 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
569
570 const GURL& url = url_request->original_url();
571
572 // Check if the renderer is permitted to request the requested URL.
573 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
574 render_process_id, url)) {
575 DVLOG(1) << "Denied unauthorized download request for "
576 << url.possibly_invalid_spec();
577 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
578 }
579
580 const net::URLRequestContext* request_context = url_request->context();
581 if (!request_context->job_factory()->IsHandledURL(url)) {
582 DVLOG(1) << "Download request for unsupported protocol: "
583 << url.possibly_invalid_spec();
584 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
585 }
586
587 if (url.SchemeIs(url::kBlobScheme) &&
588 !storage::BlobProtocolHandler::GetRequestBlobDataHandle(
589 url_request.get())) {
590 ChromeBlobStorageContext* blob_context =
591 GetChromeBlobStorageContextForResourceContext(resource_context);
592 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
593 url_request.get(),
594 blob_context->context()->GetBlobDataFromPublicURL(url));
595 }
596
597 // From this point forward, the |DownloadResourceHandler| is responsible for
598 // |started_callback|.
599 // TODO(ananta)
600 // Find a better way to create the DownloadResourceHandler instance.
601 std::unique_ptr<ResourceHandler> handler(
602 CreateResourceHandlerForDownload(url_request.get()));
603
604 ResourceDispatcherHostImpl::URLRequestStatus ret =
605 ResourceDispatcherHostImpl::Get()->BeginURLRequest(
606 std::move(url_request), std::move(handler), referrer,
607 true, // download
608 is_content_initiated, do_not_prompt_for_login, render_process_id,
609 render_view_route_id, render_frame_route_id, resource_context);
610 if (ret == ResourceDispatcherHostImpl::URL_REQUEST_FAILED_SHUTDOWN)
611 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
612 return DOWNLOAD_INTERRUPT_REASON_NONE;
613 }
614
524 namespace { 615 namespace {
525 616
526 bool EmptyFilter(const GURL& url) { 617 bool EmptyFilter(const GURL& url) {
527 return true; 618 return true;
528 } 619 }
529 620
530 bool RemoveDownloadByURLAndTime( 621 bool RemoveDownloadByURLAndTime(
531 const base::Callback<bool(const GURL&)>& url_filter, 622 const base::Callback<bool(const GURL&)>& url_filter,
532 base::Time remove_begin, 623 base::Time remove_begin,
533 base::Time remove_end, 624 base::Time remove_end,
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 if (delegate_) 786 if (delegate_)
696 delegate_->OpenDownload(download); 787 delegate_->OpenDownload(download);
697 } 788 }
698 789
699 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { 790 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
700 if (delegate_) 791 if (delegate_)
701 delegate_->ShowDownloadInShell(download); 792 delegate_->ShowDownloadInShell(download);
702 } 793 }
703 794
704 } // namespace content 795 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698