1 // Copyright 2017 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_DOWNLOAD_INTERNAL_COMMON_RESOURCE_DOWNLOADER_H_ 6 #define COMPONENTS_DOWNLOAD_INTERNAL_COMMON_RESOURCE_DOWNLOADER_H_ 7 8 #include "base/callback.h" 9 #include "components/download/public/common/download_export.h" 10 #include "components/download/public/common/download_response_handler.h" 11 #include "components/download/public/common/download_utils.h" 12 #include "components/download/public/common/url_download_handler.h" 13 #include "mojo/public/cpp/bindings/pending_remote.h" 14 #include "mojo/public/cpp/bindings/receiver.h" 15 #include "mojo/public/cpp/bindings/remote.h" 16 #include "net/cert/cert_status_flags.h" 17 #include "services/device/public/mojom/wake_lock.mojom.h" 18 #include "services/device/public/mojom/wake_lock_provider.mojom.h" 19 #include "services/network/public/cpp/resource_request.h" 20 #include "services/network/public/mojom/url_loader.mojom.h" 21 22 namespace download { 23 // Class for handing the download of a url. Lives on IO thread. 24 class COMPONENTS_DOWNLOAD_EXPORT ResourceDownloader 25 : public UrlDownloadHandler, 26 public DownloadResponseHandler::Delegate { 27 public: 28 // Called to start a download, must be called on IO thread. 29 static std::unique_ptr<ResourceDownloader> BeginDownload( 30 base::WeakPtr<download::UrlDownloadHandler::Delegate> delegate, 31 std::unique_ptr<download::DownloadUrlParameters> download_url_parameters, 32 std::unique_ptr<network::ResourceRequest> request, 33 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 34 const URLSecurityPolicy& url_security_policy, 35 const GURL& site_url, 36 const GURL& tab_url, 37 const GURL& tab_referrer_url, 38 bool is_new_download, 39 bool is_parallel_request, 40 mojo::PendingRemote<device::mojom::WakeLockProvider> wake_lock_provider, 41 bool is_background_mode, 42 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); 43 44 // Create a ResourceDownloader from a navigation that turns to be a download. 45 // No URLLoader is created, but the URLLoaderClient implementation is 46 // transferred. 47 static void InterceptNavigationResponse( 48 base::WeakPtr<UrlDownloadHandler::Delegate> delegate, 49 std::unique_ptr<network::ResourceRequest> resource_request, 50 int render_process_id, 51 int render_frame_id, 52 const GURL& site_url, 53 const GURL& tab_url, 54 const GURL& tab_referrer_url, 55 std::vector<GURL> url_chain, 56 net::CertStatus cert_status, 57 network::mojom::URLResponseHeadPtr response_head, 58 mojo::ScopedDataPipeConsumerHandle response_body, 59 network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints, 60 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 61 const URLSecurityPolicy& url_security_policy, 62 mojo::PendingRemote<device::mojom::WakeLockProvider> wake_lock_provider, 63 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); 64 65 ResourceDownloader( 66 base::WeakPtr<UrlDownloadHandler::Delegate> delegate, 67 std::unique_ptr<network::ResourceRequest> resource_request, 68 int render_process_id, 69 int render_frame_id, 70 const GURL& site_url, 71 const GURL& tab_url, 72 const GURL& tab_referrer_url, 73 bool is_new_download, 74 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 75 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 76 const URLSecurityPolicy& url_security_policy, 77 mojo::PendingRemote<device::mojom::WakeLockProvider> wake_lock_provider); 78 ~ResourceDownloader() override; 79 80 // DownloadResponseHandler::Delegate 81 void OnResponseStarted( 82 std::unique_ptr<download::DownloadCreateInfo> download_create_info, 83 mojom::DownloadStreamHandlePtr stream_handle) override; 84 void OnReceiveRedirect() override; 85 void OnResponseCompleted() override; 86 bool CanRequestURL(const GURL& url) override; 87 void OnUploadProgress(uint64_t bytes_uploaded) override; 88 89 private: 90 // Helper method to start the network request. 91 void Start( 92 std::unique_ptr<download::DownloadUrlParameters> download_url_parameters, 93 bool is_parallel_request, 94 bool is_background_mode); 95 96 // Intercepts the navigation response. 97 void InterceptResponse( 98 std::vector<GURL> url_chain, 99 net::CertStatus cert_status, 100 network::mojom::URLResponseHeadPtr response_head, 101 mojo::ScopedDataPipeConsumerHandle response_body, 102 network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints); 103 104 // Ask the |delegate_| to destroy this object. 105 void Destroy(); 106 107 // Requests the wake lock using |provider|. 108 void RequestWakeLock(device::mojom::WakeLockProvider* provider); 109 110 base::WeakPtr<download::UrlDownloadHandler::Delegate> delegate_; 111 112 // The ResourceRequest for this object. 113 std::unique_ptr<network::ResourceRequest> resource_request_; 114 115 // Object that will handle the response. 116 std::unique_ptr<network::mojom::URLLoaderClient> url_loader_client_; 117 118 // URLLoaderClient receiver. It sends any requests to the 119 // |url_loader_client_|. 120 std::unique_ptr<mojo::Receiver<network::mojom::URLLoaderClient>> 121 url_loader_client_receiver_; 122 123 // URLLoader for sending out the request. 124 mojo::Remote<network::mojom::URLLoader> url_loader_; 125 126 // Whether this is a new download. 127 bool is_new_download_; 128 129 // GUID of the download, or empty if this is a new download. 130 std::string guid_; 131 132 // Callback to run after download starts. 133 download::DownloadUrlParameters::OnStartedCallback callback_; 134 135 // Callback to run with upload updates. 136 DownloadUrlParameters::UploadProgressCallback upload_callback_; 137 138 // Frame and process id associated with the request. 139 int render_process_id_; 140 int render_frame_id_; 141 142 // Site URL for the site instance that initiated the download. 143 GURL site_url_; 144 145 // The URL of the tab that started us. 146 GURL tab_url_; 147 148 // The referrer URL of the tab that started us. 149 GURL tab_referrer_url_; 150 151 // URLLoader status when intercepting the navigation request. 152 base::Optional<network::URLLoaderCompletionStatus> url_loader_status_; 153 154 // TaskRunner to post callbacks to the |delegate_| 155 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_; 156 157 // URLLoaderFactory for issuing network requests. 158 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; 159 160 // Used to check if the URL is safe to request. 161 URLSecurityPolicy url_security_policy_; 162 163 // Whether download is initated by the content on the page. 164 bool is_content_initiated_; 165 166 // Used to keep the system from sleeping while a download is ongoing. If the 167 // system enters power saving mode while a download is alive, it can cause 168 // download to be interrupted. 169 mojo::Remote<device::mojom::WakeLock> wake_lock_; 170 171 DISALLOW_COPY_AND_ASSIGN(ResourceDownloader); 172 }; 173 174 } // namespace download 175 176 #endif // COMPONENTS_DOWNLOAD_INTERNAL_COMMON_RESOURCE_DOWNLOADER_H_ 177