blob: 0992aad5fe6c82c75207a4c3686bd55391592c2e [file] [log] [blame]
[email protected]0afff032012-01-06 20:55:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]5656f8a2011-11-17 16:12:585#include "content/browser/download/download_manager_impl.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]e7557f172011-08-19 23:42:017#include <iterator>
dcheng36b6aec92015-12-26 06:16:368#include <utility>
[email protected]e7557f172011-08-19 23:42:019
[email protected]eda58402011-09-21 19:32:0210#include "base/bind.h"
[email protected]2041cf342010-02-19 03:15:5911#include "base/callback.h"
[email protected]c6944272012-01-06 22:12:2812#include "base/debug/alias.h"
[email protected]503d03872011-05-06 08:36:2613#include "base/i18n/case_conversion.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/logging.h"
dchengf6c40582016-04-09 00:05:1315#include "base/memory/ptr_util.h"
svaldez6901c902015-12-10 18:57:3316#include "base/memory/weak_ptr.h"
[email protected]95f861e2013-07-18 02:07:1717#include "base/message_loop/message_loop.h"
jialiul82b8e502016-09-30 17:42:4218#include "base/metrics/histogram_macros.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]10994d132013-06-11 07:16:1820#include "base/strings/stringprintf.h"
[email protected]40d11e02013-03-28 17:43:1421#include "base/strings/sys_string_conversions.h"
[email protected]d59d40c2012-08-08 18:18:3722#include "base/supports_user_data.h"
[email protected]eda58402011-09-21 19:32:0223#include "base/synchronization/lock.h"
[email protected]d2a8fb72010-01-21 05:31:4224#include "build/build_config.h"
Joy Mingda69d9a2017-11-13 20:22:2225#include "components/download/downloader/in_progress/download_entry.h"
26#include "components/download/downloader/in_progress/in_progress_cache.h"
[email protected]f8e92b5d2013-03-21 18:35:4627#include "content/browser/byte_stream.h"
anantac82dd5be2016-08-26 01:21:5328#include "content/browser/child_process_security_policy_impl.h"
[email protected]71bf3f5e2011-08-15 21:05:2229#include "content/browser/download/download_create_info.h"
[email protected]53ac00e82012-10-18 20:59:2030#include "content/browser/download/download_file_factory.h"
31#include "content/browser/download/download_item_factory.h"
[email protected]c09a8fd2011-11-21 19:54:5032#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0633#include "content/browser/download/download_stats.h"
siggi2b05ad102017-07-21 18:57:5634#include "content/browser/download/download_task_runner.h"
Min Qin3cbdb8d2017-08-25 06:23:0935#include "content/browser/download/download_utils.h"
36#include "content/browser/download/resource_downloader.h"
37#include "content/browser/download/url_downloader.h"
[email protected]678c0362012-12-05 08:02:4438#include "content/browser/loader/resource_dispatcher_host_impl.h"
anantac82dd5be2016-08-26 01:21:5339#include "content/browser/loader/resource_request_info_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3240#include "content/browser/renderer_host/render_view_host_impl.h"
Min Qin3cbdb8d2017-08-25 06:23:0941#include "content/browser/storage_partition_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2942#include "content/browser/web_contents/web_contents_impl.h"
Min Qin40b72172017-09-27 00:19:1743#include "content/common/throttling_url_loader.h"
[email protected]ccb797302011-12-15 16:55:1144#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4445#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3446#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1747#include "content/public/browser/download_manager_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1448#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0749#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1650#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3051#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1352#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2753#include "content/public/browser/web_contents_delegate.h"
Min Qinc3c194652017-11-28 00:02:5754#include "content/public/common/browser_side_navigation_policy.h"
Min Qin3cbdb8d2017-08-25 06:23:0955#include "content/public/common/content_features.h"
megjabloncaf312f2017-01-12 18:47:4956#include "content/public/common/previews_state.h"
[email protected]8d68a3e02013-01-12 15:57:1057#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0758#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1459#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1960#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2861#include "net/base/upload_bytes_element_reader.h"
[email protected]eb795632012-09-01 00:19:3162#include "net/url_request/url_request_context.h"
asanka6150c522016-03-25 21:40:5763#include "storage/browser/blob/blob_url_request_job_factory.h"
ttr31481dc54b2015-08-06 20:11:2664#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2965
Antonio Gomes5986d9bca2017-08-30 22:15:5666#if defined(USE_X11)
thomasanderson1799a1222017-06-14 00:02:0267#include "base/nix/xdg_util.h"
68#endif
69
[email protected]35869622012-10-26 23:23:5570namespace content {
[email protected]a0ce3282011-08-19 20:49:5271namespace {
72
Min Qinc3c194652017-11-28 00:02:5773WebContents* GetWebContents(int render_process_id,
74 int render_frame_id,
75 int frame_tree_node_id) {
76 DCHECK(IsBrowserSideNavigationEnabled());
77
78 WebContents* web_contents = WebContents::FromRenderFrameHost(
79 RenderFrameHost::FromID(render_process_id, render_frame_id));
80 if (web_contents)
81 return web_contents;
82
83 return WebContents::FromFrameTreeNodeId(frame_tree_node_id);
84}
85
Min Qin99b2c57e2017-09-28 05:04:3786StoragePartitionImpl* GetStoragePartition(BrowserContext* context,
87 int render_process_id,
88 int render_frame_id) {
Min Qin3cbdb8d2017-08-25 06:23:0989 DCHECK_CURRENTLY_ON(BrowserThread::UI);
90
91 SiteInstance* site_instance = nullptr;
92 if (render_process_id >= 0) {
93 RenderFrameHost* render_frame_host_ =
94 RenderFrameHost::FromID(render_process_id, render_frame_id);
95 if (render_frame_host_)
96 site_instance = render_frame_host_->GetSiteInstance();
97 }
Min Qin99b2c57e2017-09-28 05:04:3798 return static_cast<StoragePartitionImpl*>(
Min Qin3cbdb8d2017-08-25 06:23:0999 BrowserContext::GetStoragePartition(context, site_instance));
Min Qin3cbdb8d2017-08-25 06:23:09100}
101
Min Qin4eb4ec432017-09-22 20:13:44102bool CanRequestURLFromRenderer(int render_process_id, GURL url) {
103 DCHECK_CURRENTLY_ON(BrowserThread::IO);
104 // Check if the renderer is permitted to request the requested URL.
105 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
106 render_process_id, url)) {
107 DVLOG(1) << "Denied unauthorized download request for "
108 << url.possibly_invalid_spec();
109 return false;
110 }
111 return true;
112}
113
114void CreateInterruptedDownload(
115 DownloadUrlParameters* params,
116 DownloadInterruptReason reason,
117 base::WeakPtr<DownloadManagerImpl> download_manager) {
118 DCHECK_CURRENTLY_ON(BrowserThread::IO);
119 std::unique_ptr<DownloadCreateInfo> failed_created_info(
Min Qin218f1a532017-11-09 01:35:01120 new DownloadCreateInfo(base::Time::Now(),
Min Qin4eb4ec432017-09-22 20:13:44121 base::WrapUnique(new DownloadSaveInfo)));
122 failed_created_info->url_chain.push_back(params->url());
123 failed_created_info->result = reason;
124 std::unique_ptr<ByteStreamReader> empty_byte_stream;
125 BrowserThread::PostTask(
126 BrowserThread::UI, FROM_HERE,
127 base::BindOnce(&DownloadManager::StartDownload, download_manager,
128 std::move(failed_created_info),
Jeremy Roman04f27c372017-10-27 15:20:55129 std::make_unique<DownloadManager::InputStream>(
Min Qin4eb4ec432017-09-22 20:13:44130 std::move(empty_byte_stream)),
131 params->callback()));
132}
133
Min Qin40b72172017-09-27 00:19:17134DownloadManagerImpl::UniqueUrlDownloadHandlerPtr BeginDownload(
dchengf6c40582016-04-09 00:05:13135 std::unique_ptr<DownloadUrlParameters> params,
asanka3b4be122016-05-11 04:27:57136 content::ResourceContext* resource_context,
avib7348942015-12-25 20:57:10137 uint32_t download_id,
svaldez6901c902015-12-10 18:57:33138 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:52139 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:10140
dchengf6c40582016-04-09 00:05:13141 std::unique_ptr<net::URLRequest> url_request =
Ramin Halavati03efa732017-06-12 09:28:20142 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
dchengf6c40582016-04-09 00:05:13143 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
asanka6150c522016-03-25 21:40:57144 params->GetBlobDataHandle();
145 if (blob_data_handle) {
146 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
147 url_request.get(), std::move(blob_data_handle));
148 }
[email protected]fc179dd12013-03-16 09:35:19149
asanka00b621f2016-02-19 18:09:23150 // If there's a valid renderer process associated with the request, then the
151 // request should be driven by the ResourceLoader. Pass it over to the
152 // ResourceDispatcherHostImpl which will in turn pass it along to the
153 // ResourceLoader.
asanka3b4be122016-05-11 04:27:57154 if (params->render_process_host_id() >= 0) {
anantac82dd5be2016-08-26 01:21:53155 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest(
156 std::move(url_request), params->referrer(), resource_context,
157 params->content_initiated(), params->render_process_host_id(),
158 params->render_view_host_routing_id(),
159 params->render_frame_host_routing_id(),
160 params->do_not_prompt_for_login());
asanka00b621f2016-02-19 18:09:23161
162 // If the download was accepted, the DownloadResourceHandler is now
163 // responsible for driving the request to completion.
164 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
165 return nullptr;
166
167 // Otherwise, create an interrupted download.
Min Qin4eb4ec432017-09-22 20:13:44168 CreateInterruptedDownload(params.get(), reason, download_manager);
svaldez6901c902015-12-10 18:57:33169 return nullptr;
170 }
asanka00b621f2016-02-19 18:09:23171
Min Qin40b72172017-09-27 00:19:17172 return DownloadManagerImpl::UniqueUrlDownloadHandlerPtr(
asanka00b621f2016-02-19 18:09:23173 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
qinmin462abbb2017-05-03 21:55:42174 params->referrer(), false)
svaldez6901c902015-12-10 18:57:33175 .release());
[email protected]a0ce3282011-08-19 20:49:52176}
177
Min Qin40b72172017-09-27 00:19:17178DownloadManagerImpl::UniqueUrlDownloadHandlerPtr BeginResourceDownload(
Min Qin3cbdb8d2017-08-25 06:23:09179 std::unique_ptr<DownloadUrlParameters> params,
180 std::unique_ptr<ResourceRequest> request,
181 scoped_refptr<URLLoaderFactoryGetter> url_loader_factory_getter,
Min Qin99b2c57e2017-09-28 05:04:37182 scoped_refptr<storage::FileSystemContext> file_system_context,
Min Qin3cbdb8d2017-08-25 06:23:09183 uint32_t download_id,
184 base::WeakPtr<DownloadManagerImpl> download_manager) {
185 DCHECK_CURRENTLY_ON(BrowserThread::IO);
186
Min Qin4eb4ec432017-09-22 20:13:44187 // Check if the renderer is permitted to request the requested URL.
188 if (params->render_process_host_id() >= 0 &&
189 !CanRequestURLFromRenderer(params->render_process_host_id(),
190 params->url())) {
191 CreateInterruptedDownload(params.get(),
192 DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST,
193 download_manager);
194 return nullptr;
195 }
196
Nick Carter62855fe82017-11-29 00:17:17197 ResourceRequestInfo::WebContentsGetter getter =
198 base::Bind(&GetWebContents, params->render_process_host_id(),
199 params->render_frame_host_routing_id(), -1);
Min Qinc3c194652017-11-28 00:02:57200 // TODO(qinmin): Check the storage permission before creating the URLLoader.
201 // This is already done for context menu download, but it is missing for
202 // download service and download resumption.
Min Qin40b72172017-09-27 00:19:17203 return DownloadManagerImpl::UniqueUrlDownloadHandlerPtr(
204 ResourceDownloader::BeginDownload(
205 download_manager, std::move(params), std::move(request),
Min Qin4a567352017-11-28 19:14:48206 url_loader_factory_getter, file_system_context, getter, download_id,
207 false)
Min Qin3cbdb8d2017-08-25 06:23:09208 .release());
209}
210
Min Qin40b72172017-09-27 00:19:17211// Creates a ResourceDownloader to own the URLLoader and intercept the response,
212// and passes it back to the DownloadManager.
213void InterceptNavigationResponse(
Min Qin40b72172017-09-27 00:19:17214 base::WeakPtr<DownloadManagerImpl> download_manager,
215 const scoped_refptr<ResourceResponse>& response,
216 mojo::ScopedDataPipeConsumerHandle consumer_handle,
John Abd-El-Malekf36e05f2017-11-30 16:17:52217 net::CertStatus cert_status,
Xing Liu18a8e0892017-11-15 06:16:13218 int frame_tree_node_id,
Min Qin40b72172017-09-27 00:19:17219 std::unique_ptr<ResourceRequest> resource_request,
220 std::unique_ptr<ThrottlingURLLoader> url_loader,
Min Qin83d07872017-10-26 23:22:41221 std::vector<GURL> url_chain,
Takashi Toyoshimaaa278662017-11-20 11:11:26222 base::Optional<network::URLLoaderCompletionStatus> status) {
Min Qin40b72172017-09-27 00:19:17223 DCHECK_CURRENTLY_ON(BrowserThread::IO);
Min Qinc3c194652017-11-28 00:02:57224 GURL url = resource_request->url;
225 std::string method = resource_request->method;
226 ResourceRequestInfo::WebContentsGetter getter =
Nick Carter62855fe82017-11-29 00:17:17227 base::Bind(&GetWebContents, ChildProcessHost::kInvalidUniqueID,
228 MSG_ROUTING_NONE, frame_tree_node_id);
Min Qinc3c194652017-11-28 00:02:57229 std::unique_ptr<ResourceDownloader> resource_downloader =
230 ResourceDownloader::CreateWithURLLoader(
231 download_manager, std::move(resource_request), getter,
232 std::move(url_loader), std::move(status));
233
234 // Use Unretained() is safe as |resource_downloader| will be deleted on
235 // the IO thread.
236 base::OnceClosure start_interception_cb = base::BindOnce(
237 &ResourceDownloader::StartNavigationInterception,
238 base::Unretained(resource_downloader.get()), response,
John Abd-El-Malekf36e05f2017-11-30 16:17:52239 std::move(consumer_handle), cert_status, std::move(url_chain));
Min Qin40b72172017-09-27 00:19:17240
241 BrowserThread::PostTask(
242 BrowserThread::UI, FROM_HERE,
Min Qinc3c194652017-11-28 00:02:57243 base::BindOnce(&DownloadManagerImpl::CheckDownloadAllowed,
244 download_manager, getter, url, method,
245 DownloadManagerImpl::UniqueUrlDownloadHandlerPtr(
246 resource_downloader.release()),
247 std::move(start_interception_cb)));
Min Qin40b72172017-09-27 00:19:17248}
249
[email protected]35869622012-10-26 23:23:55250class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03251 public:
[email protected]b3756012013-03-06 17:43:02252 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58253 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03254
dchengc2282aa2014-10-21 12:07:58255 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02256 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11257 const std::string& guid,
avib7348942015-12-25 20:57:10258 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02259 const base::FilePath& current_path,
260 const base::FilePath& target_path,
261 const std::vector<GURL>& url_chain,
262 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57263 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28264 const GURL& tab_url,
265 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45266 const std::string& mime_type,
267 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23268 base::Time start_time,
269 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21270 const std::string& etag,
271 const std::string& last_modified,
avib7348942015-12-25 20:57:10272 int64_t received_bytes,
273 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57274 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02275 DownloadItem::DownloadState state,
276 DownloadDangerType danger_type,
277 DownloadInterruptReason interrupt_reason,
278 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23279 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10280 bool transient,
Min Qin218f1a532017-11-09 01:35:01281 const std::vector<DownloadItem::ReceivedSlice>& received_slices)
282 override {
asanka038d58d2016-04-14 00:29:28283 return new DownloadItemImpl(
284 delegate, guid, download_id, current_path, target_path, url_chain,
asanka3b4be122016-05-11 04:27:57285 referrer_url, site_url, tab_url, tab_refererr_url, mime_type,
286 original_mime_type, start_time, end_time, etag, last_modified,
287 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason,
Min Qin218f1a532017-11-09 01:35:01288 opened, last_access_time, transient, received_slices);
[email protected]b3756012013-03-06 17:43:02289 }
[email protected]d25fda12012-06-12 17:05:03290
Min Qin218f1a532017-11-09 01:35:01291 DownloadItemImpl* CreateActiveItem(DownloadItemImplDelegate* delegate,
292 uint32_t download_id,
293 const DownloadCreateInfo& info) override {
294 return new DownloadItemImpl(delegate, download_id, info);
[email protected]b3756012013-03-06 17:43:02295 }
[email protected]d25fda12012-06-12 17:05:03296
dchengc2282aa2014-10-21 12:07:58297 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02298 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10299 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02300 const base::FilePath& path,
301 const GURL& url,
[email protected]b3756012013-03-06 17:43:02302 const std::string& mime_type,
Min Qin218f1a532017-11-09 01:35:01303 std::unique_ptr<DownloadRequestHandleInterface> request_handle) override {
dcheng36b6aec92015-12-26 06:16:36304 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
Min Qin218f1a532017-11-09 01:35:01305 std::move(request_handle));
[email protected]b3756012013-03-06 17:43:02306 }
[email protected]d25fda12012-06-12 17:05:03307};
308
Antonio Gomes5986d9bca2017-08-30 22:15:56309#if defined(USE_X11)
thomasanderson1799a1222017-06-14 00:02:02310base::FilePath GetTemporaryDownloadDirectory() {
311 std::unique_ptr<base::Environment> env(base::Environment::Create());
312 return base::nix::GetXDGDirectory(env.get(), "XDG_DATA_HOME", ".local/share");
313}
314#endif
315
[email protected]a0ce3282011-08-19 20:49:52316} // namespace
317
Joy Mingda69d9a2017-11-13 20:22:22318// Responsible for persisting the in-progress metadata associated with a
319// download.
320class InProgressDownloadObserver : public DownloadItem::Observer {
321 public:
322 explicit InProgressDownloadObserver(
323 download::InProgressCache* in_progress_cache);
324 ~InProgressDownloadObserver() override;
325
326 private:
327 // DownloadItem::Observer
328 void OnDownloadUpdated(DownloadItem* download) override;
329 void OnDownloadRemoved(DownloadItem* download) override;
330
331 // The persistent cache to store in-progress metadata.
332 download::InProgressCache* in_progress_cache_;
333
334 DISALLOW_COPY_AND_ASSIGN(InProgressDownloadObserver);
335};
336
337InProgressDownloadObserver::InProgressDownloadObserver(
338 download::InProgressCache* in_progress_cache)
339 : in_progress_cache_(in_progress_cache) {}
340
341InProgressDownloadObserver::~InProgressDownloadObserver() = default;
342
343void InProgressDownloadObserver::OnDownloadUpdated(DownloadItem* download) {
344 // TODO(crbug.com/778425): Properly handle fail/resume/retry for downloads
345 // that are in the INTERRUPTED state for a long time.
346 switch (download->GetState()) {
347 case DownloadItem::DownloadState::COMPLETE:
348 case DownloadItem::DownloadState::CANCELLED:
349 if (in_progress_cache_)
350 in_progress_cache_->RemoveEntry(download->GetGuid());
351 break;
352 case DownloadItem::DownloadState::IN_PROGRESS:
353 // TODO(crbug.com/778425): After RetrieveEntry has been implemented, do a
354 // check to make sure the entry exists in the cache.
355 break;
356 default:
357 break;
358 }
359}
360
361void InProgressDownloadObserver::OnDownloadRemoved(DownloadItem* download) {
362 in_progress_cache_->RemoveEntry(download->GetGuid());
363}
364
Min Qinddb40ce2017-10-28 03:38:26365DownloadManagerImpl::DownloadManagerImpl(BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20366 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55367 file_factory_(new DownloadFileFactory()),
[email protected]16798692013-04-23 18:08:38368 shutdown_needed_(true),
xingliud64fde652017-05-24 07:04:33369 initialized_(false),
[email protected]16798692013-04-23 18:08:38370 browser_context_(browser_context),
avicebda7922016-10-21 17:37:54371 delegate_(nullptr),
[email protected]eba4a4d2013-05-29 02:18:06372 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38373 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29374}
375
[email protected]5656f8a2011-11-17 16:12:58376DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13377 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29378}
379
[email protected]89ec81f2013-05-15 19:20:02380DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10381 uint32_t id,
382 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52383 DCHECK_CURRENTLY_ON(BrowserThread::UI);
skyostil66bd67912016-08-12 12:33:11384 DCHECK(!base::ContainsKey(downloads_, id));
Min Qin218f1a532017-11-09 01:35:01385 DownloadItemImpl* download = item_factory_->CreateActiveItem(this, id, info);
avicebda7922016-10-21 17:37:54386 downloads_[id] = base::WrapUnique(download);
asankaeef62b02016-03-14 21:23:11387 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02388 return download;
[email protected]381af492013-03-28 01:56:22389}
390
[email protected]530047e2013-07-12 17:02:25391void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52392 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25393 if (delegate_) {
394 delegate_->GetNextId(callback);
395 return;
[email protected]491aaa62012-06-07 03:50:18396 }
avib7348942015-12-25 20:57:10397 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25398 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53399}
400
[email protected]53ac00e82012-10-18 20:59:20401void DownloadManagerImpl::DetermineDownloadTarget(
402 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
403 // Note that this next call relies on
404 // DownloadItemImplDelegate::DownloadTargetCallback and
405 // DownloadManagerDelegate::DownloadTargetCallback having the same
406 // type. If the types ever diverge, gasket code will need to
407 // be written here.
408 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34409 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20410 // TODO(asanka): Determine a useful path if |target_path| is empty.
Asanka Herath1ba0e9f2017-04-03 18:48:53411 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
412 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path,
413 DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]53ac00e82012-10-18 20:59:20414 }
[email protected]47665442012-07-27 02:31:22415}
416
[email protected]98299622013-01-03 22:26:50417bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43418 DownloadItemImpl* item, const base::Closure& complete_callback) {
419 if (!delegate_ ||
420 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50421 return true;
[email protected]18710a42a2012-10-17 19:50:43422 }
423 // Otherwise, the delegate has accepted responsibility to run the
424 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50425 return false;
[email protected]fc03de22011-12-06 23:28:12426}
427
[email protected]2dec8ec2013-02-07 19:20:34428bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
429 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18430 if (!delegate_)
431 return false;
432
[email protected]fc03de22011-12-06 23:28:12433 return delegate_->ShouldOpenFileBasedOnExtension(path);
434}
435
[email protected]bde0e4f2012-11-08 22:49:59436bool DownloadManagerImpl::ShouldOpenDownload(
437 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43438 if (!delegate_)
439 return true;
440
[email protected]bde0e4f2012-11-08 22:49:59441 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
442 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
443 // to have the same type :-}.
444 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43445}
446
[email protected]35869622012-10-26 23:23:55447void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57448 delegate_ = delegate;
449}
450
[email protected]35869622012-10-26 23:23:55451DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28452 return delegate_;
453}
454
[email protected]5656f8a2011-11-17 16:12:58455void DownloadManagerImpl::Shutdown() {
pkastingf5279482016-07-27 02:18:20456 DVLOG(20) << __func__ << "() shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13457 if (!shutdown_needed_)
458 return;
459 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29460
ericwilligers254597b2016-10-17 10:32:31461 for (auto& observer : observers_)
462 observer.ManagerGoingDown(this);
[email protected]fb4f8d902011-09-16 06:07:08463 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13464
[email protected]fe752272013-05-29 18:57:45465 // If there are in-progress downloads, cancel them. This also goes for
466 // dangerous downloads which will remain in history if they aren't explicitly
467 // accepted or discarded. Canceling will remove the intermediate download
468 // file.
lukasza921fabfa2016-02-05 19:51:48469 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54470 DownloadItemImpl* download = it.second.get();
[email protected]fe752272013-05-29 18:57:45471 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17472 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29473 }
avicebda7922016-10-21 17:37:54474 downloads_.clear();
asankaeef62b02016-03-14 21:23:11475 downloads_by_guid_.clear();
Min Qin3cbdb8d2017-08-25 06:23:09476 url_download_handlers_.clear();
initial.commit09911bf2008-07-26 23:55:29477
[email protected]41f558fb2012-01-09 22:59:58478 // We'll have nothing more to report to the observers after this point.
479 observers_.Clear();
480
[email protected]b441a8492012-06-06 14:55:57481 if (delegate_)
482 delegate_->Shutdown();
avicebda7922016-10-21 17:37:54483 delegate_ = nullptr;
initial.commit09911bf2008-07-26 23:55:29484}
485
[email protected]530047e2013-07-12 17:02:25486void DownloadManagerImpl::StartDownload(
dchengf6c40582016-04-09 00:05:13487 std::unique_ptr<DownloadCreateInfo> info,
Min Qin0e4d82a2017-09-01 20:40:31488 std::unique_ptr<DownloadManager::InputStream> stream,
[email protected]530047e2013-07-12 17:02:25489 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52490 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25491 DCHECK(info);
Min Qin0e4d82a2017-09-01 20:40:31492
asanka00b621f2016-02-19 18:09:23493 // |stream| is only non-nil if the download request was successful.
Min Qin0e4d82a2017-09-01 20:40:31494 DCHECK(
495 (info->result == DOWNLOAD_INTERRUPT_REASON_NONE && !stream->IsEmpty()) ||
496 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && stream->IsEmpty()));
pkastingf5279482016-07-27 02:18:20497 DVLOG(20) << __func__
498 << "() result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10499 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25500 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
jialiul82b8e502016-09-30 17:42:42501 if (new_download)
502 RecordDownloadConnectionSecurity(info->url(), info->url_chain);
avib7348942015-12-25 20:57:10503 base::Callback<void(uint32_t)> got_id(base::Bind(
504 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
505 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02506 if (new_download) {
[email protected]530047e2013-07-12 17:02:25507 GetNextId(got_id);
508 } else {
509 got_id.Run(download_id);
510 }
511}
512
513void DownloadManagerImpl::StartDownloadWithId(
dchengf6c40582016-04-09 00:05:13514 std::unique_ptr<DownloadCreateInfo> info,
Min Qin0e4d82a2017-09-01 20:40:31515 std::unique_ptr<DownloadManager::InputStream> stream,
[email protected]530047e2013-07-12 17:02:25516 const DownloadUrlParameters::OnStartedCallback& on_started,
517 bool new_download,
avib7348942015-12-25 20:57:10518 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52519 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25520 DCHECK_NE(content::DownloadItem::kInvalidId, id);
avicebda7922016-10-21 17:37:54521 DownloadItemImpl* download = nullptr;
[email protected]530047e2013-07-12 17:02:25522 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02523 download = CreateActiveItem(id, *info);
524 } else {
avicebda7922016-10-21 17:37:54525 auto item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02526 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37527 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47528 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02529 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37530 // removed after it was resumed. Ignore. If the download is cancelled
531 // while resuming, then also ignore the request.
Min Qin5b912dc42017-06-07 23:10:18532 info->request_handle->CancelRequest(true);
[email protected]530047e2013-07-12 17:02:25533 if (!on_started.is_null())
avicebda7922016-10-21 17:37:54534 on_started.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
siggi2b05ad102017-07-21 18:57:56535 // The ByteStreamReader lives and dies on the download sequence.
asanka00b621f2016-02-19 18:09:23536 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
siggi2b05ad102017-07-21 18:57:56537 GetDownloadTaskRunner()->DeleteSoon(FROM_HERE, stream.release());
[email protected]530047e2013-07-12 17:02:25538 return;
[email protected]89ec81f2013-05-15 19:20:02539 }
avicebda7922016-10-21 17:37:54540 download = item_iterator->second.get();
[email protected]89ec81f2013-05-15 19:20:02541 }
542
[email protected]2dec8ec2013-02-07 19:20:34543 base::FilePath default_download_directory;
Antonio Gomes5986d9bca2017-08-30 22:15:56544#if defined(USE_X11)
Tom Andersone9b9ba0e2017-11-14 21:28:06545 // TODO(thomasanderson,crbug.com/784010): Remove this when all Linux
546 // distros with versions of GTK lower than 3.14.7 are no longer
547 // supported. This should happen when support for Ubuntu Trusty and
548 // Debian Jessie are removed.
thomasanderson1799a1222017-06-14 00:02:02549 default_download_directory = GetTemporaryDownloadDirectory();
550#else
[email protected]96bfed052012-09-15 22:21:01551 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34552 base::FilePath website_save_directory; // Unused
553 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01554 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20555 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01556 }
thomasanderson1799a1222017-06-14 00:02:02557#endif
[email protected]96bfed052012-09-15 22:21:01558
Joy Mingda69d9a2017-11-13 20:22:22559 if (delegate_) {
560 if (!in_progress_download_observer_) {
561 in_progress_download_observer_.reset(
562 new InProgressDownloadObserver(delegate_->GetInProgressCache()));
563 }
564 // May already observe this item, remove observer first.
565 download->RemoveObserver(in_progress_download_observer_.get());
566 download->AddObserver(in_progress_download_observer_.get());
567 }
568
dchengf6c40582016-04-09 00:05:13569 std::unique_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48570
asanka00b621f2016-02-19 18:09:23571 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
572 DCHECK(stream.get());
tfarina42834112016-09-22 13:38:20573 download_file.reset(file_factory_->CreateFile(
574 std::move(info->save_info), default_download_directory,
Min Qin218f1a532017-11-09 01:35:01575 std::move(stream), id, download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48576 }
asanka4350f6a2016-03-15 02:40:57577 // It is important to leave info->save_info intact in the case of an interrupt
578 // so that the DownloadItem can salvage what it can out of a failed resumption
579 // attempt.
[email protected]492c0092013-08-10 13:49:48580
asanka00b621f2016-02-19 18:09:23581 download->Start(std::move(download_file), std::move(info->request_handle),
582 *info);
[email protected]96c3bdbaa2012-08-31 16:39:54583
[email protected]381af492013-03-28 01:56:22584 // For interrupted downloads, Start() will transition the state to
585 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
586 // For new downloads, we notify here, rather than earlier, so that
587 // the download_file is bound to download and all the usual
588 // setters (e.g. Cancel) work.
ericwilligers254597b2016-10-17 10:32:31589 if (new_download) {
590 for (auto& observer : observers_)
591 observer.OnDownloadCreated(this, download);
592 }
[email protected]96c3bdbaa2012-08-31 16:39:54593
[email protected]530047e2013-07-12 17:02:25594 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56595 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35596}
597
[email protected]5656f8a2011-11-17 16:12:58598void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52599 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lukasza921fabfa2016-02-05 19:51:48600 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54601 DownloadItemImpl* item = it.second.get();
[email protected]3d95e542012-11-20 00:52:08602 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48603 }
604}
605
[email protected]db1d8f72012-07-13 19:23:16606void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52607 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47608 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16609 !download_item->GetFileExternallyRemoved() &&
610 delegate_) {
611 delegate_->CheckForFileExistence(
612 download_item,
tzik5b521ad02017-08-18 07:49:19613 base::BindOnce(&DownloadManagerImpl::OnFileExistenceChecked,
614 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48615 }
616}
617
avib7348942015-12-25 20:57:10618void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id,
[email protected]4c544cd2013-01-25 07:54:16619 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52620 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16621 if (!result) { // File does not exist.
skyostil66bd67912016-08-12 12:33:11622 if (base::ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51623 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16624 }
[email protected]9fc114672011-06-15 08:17:48625}
626
asankadb9e50ad2016-10-03 15:00:29627std::string DownloadManagerImpl::GetApplicationClientIdForFileScanning() const {
628 if (delegate_)
629 return delegate_->ApplicationClientIdForFileScanning();
630 return std::string();
631}
632
[email protected]35869622012-10-26 23:23:55633BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58634 return browser_context_;
635}
636
[email protected]530047e2013-07-12 17:02:25637void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34638 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12639 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27640 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13641 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25642 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52643 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25644 GetNextId(base::Bind(
645 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
dcheng36b6aec92015-12-26 06:16:36646 weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type,
647 base::Passed(std::move(request_handle)), item_created));
[email protected]530047e2013-07-12 17:02:25648}
649
650void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
651 const base::FilePath& main_file_path,
652 const GURL& page_url,
653 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13654 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25655 const DownloadItemImplCreated& item_created,
avib7348942015-12-25 20:57:10656 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52657 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25658 DCHECK_NE(content::DownloadItem::kInvalidId, id);
skyostil66bd67912016-08-12 12:33:11659 DCHECK(!base::ContainsKey(downloads_, id));
[email protected]3d95e542012-11-20 00:52:08660 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
Min Qin218f1a532017-11-09 01:35:01661 this, id, main_file_path, page_url, mime_type, std::move(request_handle));
avicebda7922016-10-21 17:37:54662 downloads_[download_item->GetId()] = base::WrapUnique(download_item);
skyostil66bd67912016-08-12 12:33:11663 DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid()));
asankaeef62b02016-03-14 21:23:11664 downloads_by_guid_[download_item->GetGuid()] = download_item;
ericwilligers254597b2016-10-17 10:32:31665 for (auto& observer : observers_)
666 observer.OnDownloadCreated(this, download_item);
[email protected]530047e2013-07-12 17:02:25667 if (!item_created.is_null())
668 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12669}
670
[email protected]8d68a3e02013-01-12 15:57:10671// Resume a download of a specific URL. We send the request to the
672// ResourceDispatcherHost, and let it send us responses like a regular
673// download.
674void DownloadManagerImpl::ResumeInterruptedDownload(
dchengf6c40582016-04-09 00:05:13675 std::unique_ptr<content::DownloadUrlParameters> params,
avib7348942015-12-25 20:57:10676 uint32_t id) {
Min Qinc60045c2017-10-17 03:02:22677 BeginDownloadInternal(std::move(params), id);
[email protected]8d68a3e02013-01-12 15:57:10678}
679
Min Qinc60045c2017-10-17 03:02:22680
[email protected]53ac00e82012-10-18 20:59:20681void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
dchengf6c40582016-04-09 00:05:13682 std::unique_ptr<DownloadItemFactory> item_factory) {
dcheng36b6aec92015-12-26 06:16:36683 item_factory_ = std::move(item_factory);
[email protected]53ac00e82012-10-18 20:59:20684}
685
686void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
dchengf6c40582016-04-09 00:05:13687 std::unique_ptr<DownloadFileFactory> file_factory) {
dcheng36b6aec92015-12-26 06:16:36688 file_factory_ = std::move(file_factory);
[email protected]53ac00e82012-10-18 20:59:20689}
690
[email protected]35869622012-10-26 23:23:55691DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20692 return file_factory_.get();
693}
694
[email protected]db1d8f72012-07-13 19:23:16695void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28696 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40697 return;
698
asankaeef62b02016-03-14 21:23:11699 downloads_by_guid_.erase(download->GetGuid());
avicebda7922016-10-21 17:37:54700 downloads_.erase(download->GetId());
initial.commit09911bf2008-07-26 23:55:29701}
702
Min Qin3cbdb8d2017-08-25 06:23:09703void DownloadManagerImpl::AddUrlDownloadHandler(
Min Qin40b72172017-09-27 00:19:17704 UniqueUrlDownloadHandlerPtr downloader) {
svaldez6901c902015-12-10 18:57:33705 if (downloader)
Min Qin3cbdb8d2017-08-25 06:23:09706 url_download_handlers_.push_back(std::move(downloader));
svaldez6901c902015-12-10 18:57:33707}
708
anantac82dd5be2016-08-26 01:21:53709// static
710DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest(
711 std::unique_ptr<net::URLRequest> url_request,
712 const Referrer& referrer,
713 ResourceContext* resource_context,
714 bool is_content_initiated,
715 int render_process_id,
716 int render_view_route_id,
717 int render_frame_route_id,
718 bool do_not_prompt_for_login) {
anantac82dd5be2016-08-26 01:21:53719 if (ResourceDispatcherHostImpl::Get()->is_shutdown())
720 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
721
722 // The URLRequest needs to be initialized with the referrer and other
723 // information prior to issuing it.
724 ResourceDispatcherHostImpl::Get()->InitializeURLRequest(
725 url_request.get(), referrer,
726 true, // download.
727 render_process_id, render_view_route_id, render_frame_route_id,
megjabloncaf312f2017-01-12 18:47:49728 PREVIEWS_OFF, resource_context);
anantac82dd5be2016-08-26 01:21:53729
730 // We treat a download as a main frame load, and thus update the policy URL on
731 // redirects.
732 //
733 // TODO(davidben): Is this correct? If this came from a
734 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
735 // appropriately?
736 url_request->set_first_party_url_policy(
737 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
738
739 const GURL& url = url_request->original_url();
740
741 // Check if the renderer is permitted to request the requested URL.
Min Qin4eb4ec432017-09-22 20:13:44742 if (!CanRequestURLFromRenderer(render_process_id, url))
anantac82dd5be2016-08-26 01:21:53743 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
anantac82dd5be2016-08-26 01:21:53744
745 const net::URLRequestContext* request_context = url_request->context();
mmenke1918ae782017-04-03 20:10:20746 if (!request_context->job_factory()->IsHandledProtocol(url.scheme())) {
anantac82dd5be2016-08-26 01:21:53747 DVLOG(1) << "Download request for unsupported protocol: "
748 << url.possibly_invalid_spec();
749 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
750 }
751
752 // From this point forward, the |DownloadResourceHandler| is responsible for
753 // |started_callback|.
754 // TODO(ananta)
755 // Find a better way to create the DownloadResourceHandler instance.
756 std::unique_ptr<ResourceHandler> handler(
757 DownloadResourceHandler::Create(url_request.get()));
758
759 ResourceDispatcherHostImpl::Get()->BeginURLRequest(
760 std::move(url_request), std::move(handler), true, // download
761 is_content_initiated, do_not_prompt_for_login, resource_context);
762 return DOWNLOAD_INTERRUPT_REASON_NONE;
763}
764
Min Qin40b72172017-09-27 00:19:17765NavigationURLLoader::NavigationInterceptionCB
766DownloadManagerImpl::GetNavigationInterceptionCB(
767 const scoped_refptr<ResourceResponse>& response,
768 mojo::ScopedDataPipeConsumerHandle consumer_handle,
John Abd-El-Malekf36e05f2017-11-30 16:17:52769 net::CertStatus cert_status,
Xing Liu18a8e0892017-11-15 06:16:13770 int frame_tree_node_id) {
Min Qin40b72172017-09-27 00:19:17771 return base::BindOnce(
John Abd-El-Malekf36e05f2017-11-30 16:17:52772 &InterceptNavigationResponse, weak_factory_.GetWeakPtr(), response,
773 std::move(consumer_handle), cert_status, frame_tree_node_id);
Min Qin40b72172017-09-27 00:19:17774}
775
Min Qinc3c194652017-11-28 00:02:57776void DownloadManagerImpl::CheckDownloadAllowed(
777 const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter,
778 const GURL& url,
779 const std::string& request_method,
780 UniqueUrlDownloadHandlerPtr downloader,
781 base::OnceClosure callback) {
782 if (delegate_) {
783 delegate_->CheckDownloadAllowed(
784 web_contents_getter, url, request_method,
785 base::BindOnce(&DownloadManagerImpl::OnDownloadAllowedCheckComplete,
786 weak_factory_.GetWeakPtr(), std::move(downloader),
787 std::move(callback)));
788 }
789}
790
791void DownloadManagerImpl::OnDownloadAllowedCheckComplete(
792 UniqueUrlDownloadHandlerPtr downloader,
793 base::OnceClosure callback,
794 bool allow) {
795 if (!allow)
796 return;
797 AddUrlDownloadHandler(std::move(downloader));
798 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, std::move(callback));
799}
800
asanka6381bd4e2017-01-05 22:08:37801int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
msramekd9c162a2016-02-23 11:17:21802 const base::Callback<bool(const GURL&)>& url_filter,
asanka6381bd4e2017-01-05 22:08:37803 base::Time remove_begin,
804 base::Time remove_end) {
[email protected]237af6d2013-03-15 23:45:28805 int count = 0;
avicebda7922016-10-21 17:37:54806 auto it = downloads_.begin();
[email protected]237af6d2013-03-15 23:45:28807 while (it != downloads_.end()) {
avicebda7922016-10-21 17:37:54808 DownloadItemImpl* download = it->second.get();
[email protected]237af6d2013-03-15 23:45:28809
810 // Increment done here to protect against invalidation below.
811 ++it;
812
ttr31481dc54b2015-08-06 20:11:26813 if (download->GetState() != DownloadItem::IN_PROGRESS &&
asanka6381bd4e2017-01-05 22:08:37814 url_filter.Run(download->GetURL()) &&
815 download->GetStartTime() >= remove_begin &&
816 (remove_end.is_null() || download->GetStartTime() < remove_end)) {
[email protected]237af6d2013-03-15 23:45:28817 download->Remove();
818 count++;
initial.commit09911bf2008-07-26 23:55:29819 }
initial.commit09911bf2008-07-26 23:55:29820 }
[email protected]237af6d2013-03-15 23:45:28821 return count;
initial.commit09911bf2008-07-26 23:55:29822}
823
[email protected]0d4e30c2012-01-28 00:47:53824void DownloadManagerImpl::DownloadUrl(
Ramin Halavati03efa732017-06-12 09:28:20825 std::unique_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14826 if (params->post_id() >= 0) {
827 // Check this here so that the traceback is more useful.
828 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43829 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14830 }
Min Qin3cbdb8d2017-08-25 06:23:09831
Min Qinc60045c2017-10-17 03:02:22832 BeginDownloadInternal(std::move(params), content::DownloadItem::kInvalidId);
initial.commit09911bf2008-07-26 23:55:29833}
834
[email protected]5656f8a2011-11-17 16:12:58835void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29836 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29837}
838
[email protected]5656f8a2011-11-17 16:12:58839void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29840 observers_.RemoveObserver(observer);
841}
842
[email protected]3d95e542012-11-20 00:52:08843DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11844 const std::string& guid,
avib7348942015-12-25 20:57:10845 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34846 const base::FilePath& current_path,
847 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15848 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08849 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57850 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28851 const GURL& tab_url,
852 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45853 const std::string& mime_type,
854 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23855 base::Time start_time,
856 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21857 const std::string& etag,
858 const std::string& last_modified,
avib7348942015-12-25 20:57:10859 int64_t received_bytes,
860 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57861 const std::string& hash,
[email protected]3d95e542012-11-20 00:52:08862 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15863 DownloadDangerType danger_type,
864 DownloadInterruptReason interrupt_reason,
qinmin23b28572017-02-25 00:24:28865 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23866 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10867 bool transient,
qinmin23b28572017-02-25 00:24:28868 const std::vector<DownloadItem::ReceivedSlice>& received_slices) {
skyostil66bd67912016-08-12 12:33:11869 if (base::ContainsKey(downloads_, id)) {
[email protected]6065748f2013-10-29 01:07:43870 NOTREACHED();
avicebda7922016-10-21 17:37:54871 return nullptr;
[email protected]6065748f2013-10-29 01:07:43872 }
skyostil66bd67912016-08-12 12:33:11873 DCHECK(!base::ContainsKey(downloads_by_guid_, guid));
[email protected]3d95e542012-11-20 00:52:08874 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
asanka038d58d2016-04-14 00:29:28875 this, guid, id, current_path, target_path, url_chain, referrer_url,
asanka3b4be122016-05-11 04:27:57876 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type,
877 start_time, end_time, etag, last_modified, received_bytes, total_bytes,
shaktisahu60eb97f2017-03-03 08:53:23878 hash, state, danger_type, interrupt_reason, opened, last_access_time,
Min Qin218f1a532017-11-09 01:35:01879 transient, received_slices);
avicebda7922016-10-21 17:37:54880 downloads_[id] = base::WrapUnique(item);
asankaeef62b02016-03-14 21:23:11881 downloads_by_guid_[guid] = item;
ericwilligers254597b2016-10-17 10:32:31882 for (auto& observer : observers_)
883 observer.OnDownloadCreated(this, item);
pkastingf5279482016-07-27 02:18:20884 DVLOG(20) << __func__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08885 return item;
initial.commit09911bf2008-07-26 23:55:29886}
887
xingliud64fde652017-05-24 07:04:33888void DownloadManagerImpl::PostInitialization() {
889 DCHECK(!initialized_);
890 initialized_ = true;
891 for (auto& observer : observers_)
892 observer.OnManagerInitialized();
893}
894
895bool DownloadManagerImpl::IsManagerInitialized() const {
896 return initialized_;
897}
898
[email protected]5656f8a2011-11-17 16:12:58899int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56900 int count = 0;
lukasza921fabfa2016-02-05 19:51:48901 for (const auto& it : downloads_) {
902 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56903 ++count;
904 }
905 return count;
[email protected]5656f8a2011-11-17 16:12:58906}
907
[email protected]422a7d12013-10-21 12:10:42908int DownloadManagerImpl::NonMaliciousInProgressCount() const {
909 int count = 0;
lukasza921fabfa2016-02-05 19:51:48910 for (const auto& it : downloads_) {
911 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
912 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
913 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
914 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
915 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59916 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42917 ++count;
918 }
919 }
920 return count;
921}
922
avib7348942015-12-25 20:57:10923DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
avicebda7922016-10-21 17:37:54924 return base::ContainsKey(downloads_, download_id)
925 ? downloads_[download_id].get()
926 : nullptr;
asankaeef62b02016-03-14 21:23:11927}
928
929DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
skyostil66bd67912016-08-12 12:33:11930 return base::ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
931 : nullptr;
[email protected]ba7895d2012-06-22 19:02:52932}
933
Min Qin3cbdb8d2017-08-25 06:23:09934void DownloadManagerImpl::OnUrlDownloadStarted(
xingliu468824d2017-02-28 02:59:25935 std::unique_ptr<DownloadCreateInfo> download_create_info,
Min Qin0e4d82a2017-09-01 20:40:31936 std::unique_ptr<DownloadManager::InputStream> stream,
xingliu468824d2017-02-28 02:59:25937 const DownloadUrlParameters::OnStartedCallback& callback) {
Min Qin0e4d82a2017-09-01 20:40:31938 StartDownload(std::move(download_create_info), std::move(stream), callback);
xingliu468824d2017-02-28 02:59:25939}
940
Min Qin3cbdb8d2017-08-25 06:23:09941void DownloadManagerImpl::OnUrlDownloadStopped(UrlDownloadHandler* downloader) {
942 for (auto ptr = url_download_handlers_.begin();
943 ptr != url_download_handlers_.end(); ++ptr) {
xingliu468824d2017-02-28 02:59:25944 if (ptr->get() == downloader) {
Min Qin3cbdb8d2017-08-25 06:23:09945 url_download_handlers_.erase(ptr);
xingliu468824d2017-02-28 02:59:25946 return;
947 }
948 }
949}
950
[email protected]9f1f4092012-09-10 21:18:04951void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48952 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54953 downloads->push_back(it.second.get());
[email protected]9f1f4092012-09-10 21:18:04954 }
955}
956
[email protected]ffc0c2d2013-01-21 15:32:12957void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06958 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48959 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54960 DownloadItemImpl* item = it.second.get();
[email protected]803036e02013-06-15 17:08:47961 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40962 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06963 ++num_unopened;
964 }
[email protected]35869622012-10-26 23:23:55965 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12966
[email protected]0baf5922013-01-30 13:55:18967 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12968 delegate_->OpenDownload(download);
969}
970
971void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
972 if (delegate_)
973 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06974}
[email protected]5656f8a2011-11-17 16:12:58975
Min Qinc60045c2017-10-17 03:02:22976void DownloadManagerImpl::BeginDownloadInternal(
977 std::unique_ptr<content::DownloadUrlParameters> params,
978 uint32_t id) {
Joy Mingda69d9a2017-11-13 20:22:22979 download::InProgressCache* in_progress_cache =
980 GetBrowserContext()->GetDownloadManagerDelegate()->GetInProgressCache();
981 if (in_progress_cache) {
Xing Liu3a08c5c2017-11-29 00:39:55982 in_progress_cache->AddOrReplaceEntry(
983 download::DownloadEntry(params->guid(), params->request_origin(),
984 ToDownloadSource(params->download_source())));
Joy Mingda69d9a2017-11-13 20:22:22985 }
986
Min Qinc60045c2017-10-17 03:02:22987 if (base::FeatureList::IsEnabled(features::kNetworkService)) {
988 std::unique_ptr<ResourceRequest> request = CreateResourceRequest(
989 params.get());
990 StoragePartitionImpl* storage_partition =
991 GetStoragePartition(browser_context_, params->render_process_host_id(),
992 params->render_frame_host_routing_id());
993 BrowserThread::PostTaskAndReplyWithResult(
994 BrowserThread::IO, FROM_HERE,
995 base::BindOnce(
996 &BeginResourceDownload, std::move(params), std::move(request),
997 storage_partition->url_loader_factory_getter(),
998 base::WrapRefCounted(storage_partition->GetFileSystemContext()),
999 id, weak_factory_.GetWeakPtr()),
1000 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
1001 weak_factory_.GetWeakPtr()));
1002 } else {
1003 BrowserThread::PostTaskAndReplyWithResult(
1004 BrowserThread::IO, FROM_HERE,
1005 base::BindOnce(&BeginDownload, std::move(params),
1006 browser_context_->GetResourceContext(), id,
1007 weak_factory_.GetWeakPtr()),
1008 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
1009 weak_factory_.GetWeakPtr()));
1010 }
1011}
1012
[email protected]35869622012-10-26 23:23:551013} // namespace content