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

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

Issue 2498193003: [WIP not ready for review] ServiceWorker Download support.
Patch Set: rebase Created 4 years, 1 month 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"
(...skipping 14 matching lines...) Expand all
25 #include "content/browser/byte_stream.h" 25 #include "content/browser/byte_stream.h"
26 #include "content/browser/child_process_security_policy_impl.h" 26 #include "content/browser/child_process_security_policy_impl.h"
27 #include "content/browser/download/download_create_info.h" 27 #include "content/browser/download/download_create_info.h"
28 #include "content/browser/download/download_file_factory.h" 28 #include "content/browser/download/download_file_factory.h"
29 #include "content/browser/download/download_item_factory.h" 29 #include "content/browser/download/download_item_factory.h"
30 #include "content/browser/download/download_item_impl.h" 30 #include "content/browser/download/download_item_impl.h"
31 #include "content/browser/download/download_stats.h" 31 #include "content/browser/download/download_stats.h"
32 #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" 33 #include "content/browser/loader/resource_request_info_impl.h"
34 #include "content/browser/renderer_host/render_view_host_impl.h" 34 #include "content/browser/renderer_host/render_view_host_impl.h"
35 #include "content/browser/service_worker/service_worker_context_wrapper.h"
35 #include "content/browser/web_contents/web_contents_impl.h" 36 #include "content/browser/web_contents/web_contents_impl.h"
36 #include "content/public/browser/browser_context.h" 37 #include "content/public/browser/browser_context.h"
37 #include "content/public/browser/content_browser_client.h" 38 #include "content/public/browser/content_browser_client.h"
38 #include "content/public/browser/download_interrupt_reasons.h" 39 #include "content/public/browser/download_interrupt_reasons.h"
39 #include "content/public/browser/download_manager_delegate.h" 40 #include "content/public/browser/download_manager_delegate.h"
40 #include "content/public/browser/download_url_parameters.h" 41 #include "content/public/browser/download_url_parameters.h"
41 #include "content/public/browser/notification_service.h" 42 #include "content/public/browser/notification_service.h"
42 #include "content/public/browser/notification_types.h" 43 #include "content/public/browser/notification_types.h"
43 #include "content/public/browser/render_process_host.h" 44 #include "content/public/browser/render_process_host.h"
44 #include "content/public/browser/resource_context.h" 45 #include "content/public/browser/resource_context.h"
46 #include "content/public/browser/storage_partition.h"
45 #include "content/public/browser/web_contents_delegate.h" 47 #include "content/public/browser/web_contents_delegate.h"
46 #include "content/public/common/referrer.h" 48 #include "content/public/common/referrer.h"
47 #include "net/base/elements_upload_data_stream.h" 49 #include "net/base/elements_upload_data_stream.h"
48 #include "net/base/load_flags.h" 50 #include "net/base/load_flags.h"
49 #include "net/base/request_priority.h" 51 #include "net/base/request_priority.h"
50 #include "net/base/upload_bytes_element_reader.h" 52 #include "net/base/upload_bytes_element_reader.h"
51 #include "net/log/net_log_source_type.h" 53 #include "net/log/net_log_source_type.h"
52 #include "net/log/net_log_with_source.h" 54 #include "net/log/net_log_with_source.h"
53 #include "net/url_request/url_request_context.h" 55 #include "net/url_request/url_request_context.h"
54 #include "storage/browser/blob/blob_url_request_job_factory.h" 56 #include "storage/browser/blob/blob_url_request_job_factory.h"
55 #include "url/origin.h" 57 #include "url/origin.h"
56 58
57 namespace content { 59 namespace content {
58 namespace { 60 namespace {
59 61
60 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( 62 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload(
61 std::unique_ptr<DownloadUrlParameters> params, 63 std::unique_ptr<DownloadUrlParameters> params,
62 content::ResourceContext* resource_context, 64 content::ResourceContext* resource_context,
65 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
63 uint32_t download_id, 66 uint32_t download_id,
64 base::WeakPtr<DownloadManagerImpl> download_manager) { 67 base::WeakPtr<DownloadManagerImpl> download_manager) {
65 DCHECK_CURRENTLY_ON(BrowserThread::IO); 68 DCHECK_CURRENTLY_ON(BrowserThread::IO);
66 69
67 std::unique_ptr<net::URLRequest> url_request = 70 std::unique_ptr<net::URLRequest> url_request =
68 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); 71 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
69 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 72 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
70 params->GetBlobDataHandle(); 73 params->GetBlobDataHandle();
71 if (blob_data_handle) { 74 if (blob_data_handle) {
72 storage::BlobProtocolHandler::SetRequestedBlobDataHandle( 75 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
73 url_request.get(), std::move(blob_data_handle)); 76 url_request.get(), std::move(blob_data_handle));
74 } 77 }
75 78
76 // 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
77 // 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
78 // ResourceDispatcherHostImpl which will in turn pass it along to the 81 // ResourceDispatcherHostImpl which will in turn pass it along to the
79 // ResourceLoader. 82 // ResourceLoader.
80 if (params->render_process_host_id() >= 0) { 83 if (params->render_process_host_id() >= 0) {
81 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest( 84 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest(
82 std::move(url_request), params->referrer(), resource_context, 85 std::move(url_request), params->referrer(), resource_context,
83 params->content_initiated(), params->render_process_host_id(), 86 service_worker_context.get(), params->content_initiated(),
84 params->render_view_host_routing_id(), 87 params->render_process_host_id(), params->render_view_host_routing_id(),
85 params->render_frame_host_routing_id(), 88 params->render_frame_host_routing_id(),
89 params->service_worker_provider_id(),
86 params->do_not_prompt_for_login()); 90 params->do_not_prompt_for_login());
87 91
88 // If the download was accepted, the DownloadResourceHandler is now 92 // If the download was accepted, the DownloadResourceHandler is now
89 // responsible for driving the request to completion. 93 // responsible for driving the request to completion.
90 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) 94 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
91 return nullptr; 95 return nullptr;
92 96
93 // Otherwise, create an interrupted download. 97 // Otherwise, create an interrupted download.
94 std::unique_ptr<DownloadCreateInfo> failed_created_info( 98 std::unique_ptr<DownloadCreateInfo> failed_created_info(
95 new DownloadCreateInfo(base::Time::Now(), net::NetLogWithSource(), 99 new DownloadCreateInfo(base::Time::Now(), net::NetLogWithSource(),
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 480
477 // Resume a download of a specific URL. We send the request to the 481 // Resume a download of a specific URL. We send the request to the
478 // ResourceDispatcherHost, and let it send us responses like a regular 482 // ResourceDispatcherHost, and let it send us responses like a regular
479 // download. 483 // download.
480 void DownloadManagerImpl::ResumeInterruptedDownload( 484 void DownloadManagerImpl::ResumeInterruptedDownload(
481 std::unique_ptr<content::DownloadUrlParameters> params, 485 std::unique_ptr<content::DownloadUrlParameters> params,
482 uint32_t id) { 486 uint32_t id) {
483 BrowserThread::PostTaskAndReplyWithResult( 487 BrowserThread::PostTaskAndReplyWithResult(
484 BrowserThread::IO, FROM_HERE, 488 BrowserThread::IO, FROM_HERE,
485 base::Bind(&BeginDownload, base::Passed(&params), 489 base::Bind(&BeginDownload, base::Passed(&params),
486 browser_context_->GetResourceContext(), id, 490 browser_context_->GetResourceContext(),
491 scoped_refptr<ServiceWorkerContextWrapper>(), id,
487 weak_factory_.GetWeakPtr()), 492 weak_factory_.GetWeakPtr()),
488 base::Bind(&DownloadManagerImpl::AddUrlDownloader, 493 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
489 weak_factory_.GetWeakPtr())); 494 weak_factory_.GetWeakPtr()));
490 } 495 }
491 496
492 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( 497 void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
493 std::unique_ptr<DownloadItemFactory> item_factory) { 498 std::unique_ptr<DownloadItemFactory> item_factory) {
494 item_factory_ = std::move(item_factory); 499 item_factory_ = std::move(item_factory);
495 } 500 }
496 501
(...skipping 29 matching lines...) Expand all
526 return; 531 return;
527 } 532 }
528 } 533 }
529 } 534 }
530 535
531 // static 536 // static
532 DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest( 537 DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest(
533 std::unique_ptr<net::URLRequest> url_request, 538 std::unique_ptr<net::URLRequest> url_request,
534 const Referrer& referrer, 539 const Referrer& referrer,
535 ResourceContext* resource_context, 540 ResourceContext* resource_context,
541 ServiceWorkerContextWrapper* service_worker_context,
536 bool is_content_initiated, 542 bool is_content_initiated,
537 int render_process_id, 543 int render_process_id,
538 int render_view_route_id, 544 int render_view_route_id,
539 int render_frame_route_id, 545 int render_frame_route_id,
546 int service_worker_provider_id,
540 bool do_not_prompt_for_login) { 547 bool do_not_prompt_for_login) {
541 if (ResourceDispatcherHostImpl::Get()->is_shutdown()) 548 if (ResourceDispatcherHostImpl::Get()->is_shutdown())
542 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; 549 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
543 550
544 // The URLRequest needs to be initialized with the referrer and other 551 // The URLRequest needs to be initialized with the referrer and other
545 // information prior to issuing it. 552 // information prior to issuing it.
546 ResourceDispatcherHostImpl::Get()->InitializeURLRequest( 553 ResourceDispatcherHostImpl::Get()->InitializeURLRequest(
547 url_request.get(), referrer, 554 url_request.get(), referrer,
548 true, // download. 555 true, // download.
549 render_process_id, render_view_route_id, render_frame_route_id, 556 render_process_id, render_view_route_id, render_frame_route_id,
550 resource_context); 557 service_worker_provider_id, resource_context, service_worker_context);
551 558
552 // We treat a download as a main frame load, and thus update the policy URL on 559 // We treat a download as a main frame load, and thus update the policy URL on
553 // redirects. 560 // redirects.
554 // 561 //
555 // TODO(davidben): Is this correct? If this came from a 562 // TODO(davidben): Is this correct? If this came from a
556 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set 563 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
557 // appropriately? 564 // appropriately?
558 url_request->set_first_party_url_policy( 565 url_request->set_first_party_url_policy(
559 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); 566 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
560 567
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 base::Bind(&EmptyFilter); 645 base::Bind(&EmptyFilter);
639 // The null times make the date range unbounded. 646 // The null times make the date range unbounded.
640 int num_deleted = RemoveDownloadsByURLAndTime( 647 int num_deleted = RemoveDownloadsByURLAndTime(
641 empty_filter, base::Time(), base::Time()); 648 empty_filter, base::Time(), base::Time());
642 RecordClearAllSize(num_deleted); 649 RecordClearAllSize(num_deleted);
643 return num_deleted; 650 return num_deleted;
644 } 651 }
645 652
646 void DownloadManagerImpl::DownloadUrl( 653 void DownloadManagerImpl::DownloadUrl(
647 std::unique_ptr<DownloadUrlParameters> params) { 654 std::unique_ptr<DownloadUrlParameters> params) {
655 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context;
656 RenderProcessHost* render_process_host =
657 RenderProcessHost::FromID(params->render_process_host_id());
658 if (render_process_host) {
659 StoragePartition* partition = render_process_host->GetStoragePartition();
660 if (partition) {
661 service_worker_context = static_cast<ServiceWorkerContextWrapper*>(
662 partition->GetServiceWorkerContext());
663 }
664 }
648 if (params->post_id() >= 0) { 665 if (params->post_id() >= 0) {
649 // Check this here so that the traceback is more useful. 666 // Check this here so that the traceback is more useful.
650 DCHECK(params->prefer_cache()); 667 DCHECK(params->prefer_cache());
651 DCHECK_EQ("POST", params->method()); 668 DCHECK_EQ("POST", params->method());
652 } 669 }
653 BrowserThread::PostTaskAndReplyWithResult( 670 BrowserThread::PostTaskAndReplyWithResult(
654 BrowserThread::IO, FROM_HERE, 671 BrowserThread::IO, FROM_HERE,
655 base::Bind(&BeginDownload, base::Passed(&params), 672 base::Bind(&BeginDownload, base::Passed(&params),
656 browser_context_->GetResourceContext(), 673 browser_context_->GetResourceContext(),
674 std::move(service_worker_context),
657 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()), 675 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()),
658 base::Bind(&DownloadManagerImpl::AddUrlDownloader, 676 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
659 weak_factory_.GetWeakPtr())); 677 weak_factory_.GetWeakPtr()));
660 } 678 }
661 679
662 void DownloadManagerImpl::AddObserver(Observer* observer) { 680 void DownloadManagerImpl::AddObserver(Observer* observer) {
663 observers_.AddObserver(observer); 681 observers_.AddObserver(observer);
664 } 682 }
665 683
666 void DownloadManagerImpl::RemoveObserver(Observer* observer) { 684 void DownloadManagerImpl::RemoveObserver(Observer* observer) {
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 if (delegate_) 782 if (delegate_)
765 delegate_->OpenDownload(download); 783 delegate_->OpenDownload(download);
766 } 784 }
767 785
768 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { 786 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
769 if (delegate_) 787 if (delegate_)
770 delegate_->ShowDownloadInShell(download); 788 delegate_->ShowDownloadInShell(download);
771 } 789 }
772 790
773 } // namespace content 791 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/download_manager_impl.h ('k') | content/browser/download/save_file_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698