blob: 370b88f0b45200312e5f9b03510af98c0ea0ae7c [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 Qin3cbdb8d2017-08-25 06:23:0954#include "content/public/common/content_features.h"
megjabloncaf312f2017-01-12 18:47:4955#include "content/public/common/previews_state.h"
[email protected]8d68a3e02013-01-12 15:57:1056#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0757#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1458#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1959#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2860#include "net/base/upload_bytes_element_reader.h"
[email protected]eb795632012-09-01 00:19:3161#include "net/url_request/url_request_context.h"
asanka6150c522016-03-25 21:40:5762#include "storage/browser/blob/blob_url_request_job_factory.h"
ttr31481dc54b2015-08-06 20:11:2663#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2964
Antonio Gomes5986d9bca2017-08-30 22:15:5665#if defined(USE_X11)
thomasanderson1799a1222017-06-14 00:02:0266#include "base/nix/xdg_util.h"
67#endif
68
[email protected]35869622012-10-26 23:23:5569namespace content {
[email protected]a0ce3282011-08-19 20:49:5270namespace {
71
Min Qin99b2c57e2017-09-28 05:04:3772StoragePartitionImpl* GetStoragePartition(BrowserContext* context,
73 int render_process_id,
74 int render_frame_id) {
Min Qin3cbdb8d2017-08-25 06:23:0975 DCHECK_CURRENTLY_ON(BrowserThread::UI);
76
77 SiteInstance* site_instance = nullptr;
78 if (render_process_id >= 0) {
79 RenderFrameHost* render_frame_host_ =
80 RenderFrameHost::FromID(render_process_id, render_frame_id);
81 if (render_frame_host_)
82 site_instance = render_frame_host_->GetSiteInstance();
83 }
Min Qin99b2c57e2017-09-28 05:04:3784 return static_cast<StoragePartitionImpl*>(
Min Qin3cbdb8d2017-08-25 06:23:0985 BrowserContext::GetStoragePartition(context, site_instance));
Min Qin3cbdb8d2017-08-25 06:23:0986}
87
Min Qin4eb4ec432017-09-22 20:13:4488bool CanRequestURLFromRenderer(int render_process_id, GURL url) {
89 DCHECK_CURRENTLY_ON(BrowserThread::IO);
90 // Check if the renderer is permitted to request the requested URL.
91 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
92 render_process_id, url)) {
93 DVLOG(1) << "Denied unauthorized download request for "
94 << url.possibly_invalid_spec();
95 return false;
96 }
97 return true;
98}
99
100void CreateInterruptedDownload(
101 DownloadUrlParameters* params,
102 DownloadInterruptReason reason,
103 base::WeakPtr<DownloadManagerImpl> download_manager) {
104 DCHECK_CURRENTLY_ON(BrowserThread::IO);
105 std::unique_ptr<DownloadCreateInfo> failed_created_info(
Min Qin218f1a532017-11-09 01:35:01106 new DownloadCreateInfo(base::Time::Now(),
Min Qin4eb4ec432017-09-22 20:13:44107 base::WrapUnique(new DownloadSaveInfo)));
108 failed_created_info->url_chain.push_back(params->url());
109 failed_created_info->result = reason;
110 std::unique_ptr<ByteStreamReader> empty_byte_stream;
111 BrowserThread::PostTask(
112 BrowserThread::UI, FROM_HERE,
113 base::BindOnce(&DownloadManager::StartDownload, download_manager,
114 std::move(failed_created_info),
Jeremy Roman04f27c372017-10-27 15:20:55115 std::make_unique<DownloadManager::InputStream>(
Min Qin4eb4ec432017-09-22 20:13:44116 std::move(empty_byte_stream)),
117 params->callback()));
118}
119
Min Qin40b72172017-09-27 00:19:17120DownloadManagerImpl::UniqueUrlDownloadHandlerPtr BeginDownload(
dchengf6c40582016-04-09 00:05:13121 std::unique_ptr<DownloadUrlParameters> params,
asanka3b4be122016-05-11 04:27:57122 content::ResourceContext* resource_context,
avib7348942015-12-25 20:57:10123 uint32_t download_id,
svaldez6901c902015-12-10 18:57:33124 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:52125 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:10126
dchengf6c40582016-04-09 00:05:13127 std::unique_ptr<net::URLRequest> url_request =
Ramin Halavati03efa732017-06-12 09:28:20128 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
dchengf6c40582016-04-09 00:05:13129 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
asanka6150c522016-03-25 21:40:57130 params->GetBlobDataHandle();
131 if (blob_data_handle) {
132 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
133 url_request.get(), std::move(blob_data_handle));
134 }
[email protected]fc179dd12013-03-16 09:35:19135
asanka00b621f2016-02-19 18:09:23136 // If there's a valid renderer process associated with the request, then the
137 // request should be driven by the ResourceLoader. Pass it over to the
138 // ResourceDispatcherHostImpl which will in turn pass it along to the
139 // ResourceLoader.
asanka3b4be122016-05-11 04:27:57140 if (params->render_process_host_id() >= 0) {
anantac82dd5be2016-08-26 01:21:53141 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest(
142 std::move(url_request), params->referrer(), resource_context,
143 params->content_initiated(), params->render_process_host_id(),
144 params->render_view_host_routing_id(),
145 params->render_frame_host_routing_id(),
146 params->do_not_prompt_for_login());
asanka00b621f2016-02-19 18:09:23147
148 // If the download was accepted, the DownloadResourceHandler is now
149 // responsible for driving the request to completion.
150 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
151 return nullptr;
152
153 // Otherwise, create an interrupted download.
Min Qin4eb4ec432017-09-22 20:13:44154 CreateInterruptedDownload(params.get(), reason, download_manager);
svaldez6901c902015-12-10 18:57:33155 return nullptr;
156 }
asanka00b621f2016-02-19 18:09:23157
Min Qin40b72172017-09-27 00:19:17158 return DownloadManagerImpl::UniqueUrlDownloadHandlerPtr(
asanka00b621f2016-02-19 18:09:23159 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
qinmin462abbb2017-05-03 21:55:42160 params->referrer(), false)
svaldez6901c902015-12-10 18:57:33161 .release());
[email protected]a0ce3282011-08-19 20:49:52162}
163
Min Qin40b72172017-09-27 00:19:17164DownloadManagerImpl::UniqueUrlDownloadHandlerPtr BeginResourceDownload(
Min Qin3cbdb8d2017-08-25 06:23:09165 std::unique_ptr<DownloadUrlParameters> params,
166 std::unique_ptr<ResourceRequest> request,
167 scoped_refptr<URLLoaderFactoryGetter> url_loader_factory_getter,
Min Qin99b2c57e2017-09-28 05:04:37168 scoped_refptr<storage::FileSystemContext> file_system_context,
Min Qin3cbdb8d2017-08-25 06:23:09169 uint32_t download_id,
170 base::WeakPtr<DownloadManagerImpl> download_manager) {
171 DCHECK_CURRENTLY_ON(BrowserThread::IO);
172
Min Qin4eb4ec432017-09-22 20:13:44173 // Check if the renderer is permitted to request the requested URL.
174 if (params->render_process_host_id() >= 0 &&
175 !CanRequestURLFromRenderer(params->render_process_host_id(),
176 params->url())) {
177 CreateInterruptedDownload(params.get(),
178 DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST,
179 download_manager);
180 return nullptr;
181 }
182
Min Qin40b72172017-09-27 00:19:17183 return DownloadManagerImpl::UniqueUrlDownloadHandlerPtr(
184 ResourceDownloader::BeginDownload(
185 download_manager, std::move(params), std::move(request),
Min Qin99b2c57e2017-09-28 05:04:37186 url_loader_factory_getter, file_system_context, download_id, false)
Min Qin3cbdb8d2017-08-25 06:23:09187 .release());
188}
189
Min Qin40b72172017-09-27 00:19:17190// Creates a ResourceDownloader to own the URLLoader and intercept the response,
191// and passes it back to the DownloadManager.
192void InterceptNavigationResponse(
193 base::OnceCallback<void(DownloadManagerImpl::UniqueUrlDownloadHandlerPtr)>
194 callback,
195 base::WeakPtr<DownloadManagerImpl> download_manager,
196 const scoped_refptr<ResourceResponse>& response,
197 mojo::ScopedDataPipeConsumerHandle consumer_handle,
198 const SSLStatus& ssl_status,
Xing Liu18a8e0892017-11-15 06:16:13199 int frame_tree_node_id,
Min Qin40b72172017-09-27 00:19:17200 std::unique_ptr<ResourceRequest> resource_request,
201 std::unique_ptr<ThrottlingURLLoader> url_loader,
Min Qin83d07872017-10-26 23:22:41202 std::vector<GURL> url_chain,
Takashi Toyoshimaaa278662017-11-20 11:11:26203 base::Optional<network::URLLoaderCompletionStatus> status) {
Min Qin40b72172017-09-27 00:19:17204 DCHECK_CURRENTLY_ON(BrowserThread::IO);
205 DownloadManagerImpl::UniqueUrlDownloadHandlerPtr resource_downloader(
206 ResourceDownloader::InterceptNavigationResponse(
207 download_manager, std::move(resource_request), response,
Xing Liu18a8e0892017-11-15 06:16:13208 std::move(consumer_handle), ssl_status, frame_tree_node_id,
209 std::move(url_loader), std::move(url_chain), std::move(status))
Min Qin40b72172017-09-27 00:19:17210 .release());
211
212 BrowserThread::PostTask(
213 BrowserThread::UI, FROM_HERE,
214 base::BindOnce(std::move(callback), std::move(resource_downloader)));
215}
216
[email protected]35869622012-10-26 23:23:55217class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03218 public:
[email protected]b3756012013-03-06 17:43:02219 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58220 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03221
dchengc2282aa2014-10-21 12:07:58222 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02223 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11224 const std::string& guid,
avib7348942015-12-25 20:57:10225 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02226 const base::FilePath& current_path,
227 const base::FilePath& target_path,
228 const std::vector<GURL>& url_chain,
229 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57230 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28231 const GURL& tab_url,
232 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45233 const std::string& mime_type,
234 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23235 base::Time start_time,
236 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21237 const std::string& etag,
238 const std::string& last_modified,
avib7348942015-12-25 20:57:10239 int64_t received_bytes,
240 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57241 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02242 DownloadItem::DownloadState state,
243 DownloadDangerType danger_type,
244 DownloadInterruptReason interrupt_reason,
245 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23246 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10247 bool transient,
Min Qin218f1a532017-11-09 01:35:01248 const std::vector<DownloadItem::ReceivedSlice>& received_slices)
249 override {
asanka038d58d2016-04-14 00:29:28250 return new DownloadItemImpl(
251 delegate, guid, download_id, current_path, target_path, url_chain,
asanka3b4be122016-05-11 04:27:57252 referrer_url, site_url, tab_url, tab_refererr_url, mime_type,
253 original_mime_type, start_time, end_time, etag, last_modified,
254 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason,
Min Qin218f1a532017-11-09 01:35:01255 opened, last_access_time, transient, received_slices);
[email protected]b3756012013-03-06 17:43:02256 }
[email protected]d25fda12012-06-12 17:05:03257
Min Qin218f1a532017-11-09 01:35:01258 DownloadItemImpl* CreateActiveItem(DownloadItemImplDelegate* delegate,
259 uint32_t download_id,
260 const DownloadCreateInfo& info) override {
261 return new DownloadItemImpl(delegate, download_id, info);
[email protected]b3756012013-03-06 17:43:02262 }
[email protected]d25fda12012-06-12 17:05:03263
dchengc2282aa2014-10-21 12:07:58264 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02265 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10266 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02267 const base::FilePath& path,
268 const GURL& url,
[email protected]b3756012013-03-06 17:43:02269 const std::string& mime_type,
Min Qin218f1a532017-11-09 01:35:01270 std::unique_ptr<DownloadRequestHandleInterface> request_handle) override {
dcheng36b6aec92015-12-26 06:16:36271 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
Min Qin218f1a532017-11-09 01:35:01272 std::move(request_handle));
[email protected]b3756012013-03-06 17:43:02273 }
[email protected]d25fda12012-06-12 17:05:03274};
275
Antonio Gomes5986d9bca2017-08-30 22:15:56276#if defined(USE_X11)
thomasanderson1799a1222017-06-14 00:02:02277base::FilePath GetTemporaryDownloadDirectory() {
278 std::unique_ptr<base::Environment> env(base::Environment::Create());
279 return base::nix::GetXDGDirectory(env.get(), "XDG_DATA_HOME", ".local/share");
280}
281#endif
282
[email protected]a0ce3282011-08-19 20:49:52283} // namespace
284
Joy Mingda69d9a2017-11-13 20:22:22285// Responsible for persisting the in-progress metadata associated with a
286// download.
287class InProgressDownloadObserver : public DownloadItem::Observer {
288 public:
289 explicit InProgressDownloadObserver(
290 download::InProgressCache* in_progress_cache);
291 ~InProgressDownloadObserver() override;
292
293 private:
294 // DownloadItem::Observer
295 void OnDownloadUpdated(DownloadItem* download) override;
296 void OnDownloadRemoved(DownloadItem* download) override;
297
298 // The persistent cache to store in-progress metadata.
299 download::InProgressCache* in_progress_cache_;
300
301 DISALLOW_COPY_AND_ASSIGN(InProgressDownloadObserver);
302};
303
304InProgressDownloadObserver::InProgressDownloadObserver(
305 download::InProgressCache* in_progress_cache)
306 : in_progress_cache_(in_progress_cache) {}
307
308InProgressDownloadObserver::~InProgressDownloadObserver() = default;
309
310void InProgressDownloadObserver::OnDownloadUpdated(DownloadItem* download) {
311 // TODO(crbug.com/778425): Properly handle fail/resume/retry for downloads
312 // that are in the INTERRUPTED state for a long time.
313 switch (download->GetState()) {
314 case DownloadItem::DownloadState::COMPLETE:
315 case DownloadItem::DownloadState::CANCELLED:
316 if (in_progress_cache_)
317 in_progress_cache_->RemoveEntry(download->GetGuid());
318 break;
319 case DownloadItem::DownloadState::IN_PROGRESS:
320 // TODO(crbug.com/778425): After RetrieveEntry has been implemented, do a
321 // check to make sure the entry exists in the cache.
322 break;
323 default:
324 break;
325 }
326}
327
328void InProgressDownloadObserver::OnDownloadRemoved(DownloadItem* download) {
329 in_progress_cache_->RemoveEntry(download->GetGuid());
330}
331
Min Qinddb40ce2017-10-28 03:38:26332DownloadManagerImpl::DownloadManagerImpl(BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20333 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55334 file_factory_(new DownloadFileFactory()),
[email protected]16798692013-04-23 18:08:38335 shutdown_needed_(true),
xingliud64fde652017-05-24 07:04:33336 initialized_(false),
[email protected]16798692013-04-23 18:08:38337 browser_context_(browser_context),
avicebda7922016-10-21 17:37:54338 delegate_(nullptr),
[email protected]eba4a4d2013-05-29 02:18:06339 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38340 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29341}
342
[email protected]5656f8a2011-11-17 16:12:58343DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13344 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29345}
346
[email protected]89ec81f2013-05-15 19:20:02347DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10348 uint32_t id,
349 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52350 DCHECK_CURRENTLY_ON(BrowserThread::UI);
skyostil66bd67912016-08-12 12:33:11351 DCHECK(!base::ContainsKey(downloads_, id));
Min Qin218f1a532017-11-09 01:35:01352 DownloadItemImpl* download = item_factory_->CreateActiveItem(this, id, info);
avicebda7922016-10-21 17:37:54353 downloads_[id] = base::WrapUnique(download);
asankaeef62b02016-03-14 21:23:11354 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02355 return download;
[email protected]381af492013-03-28 01:56:22356}
357
[email protected]530047e2013-07-12 17:02:25358void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52359 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25360 if (delegate_) {
361 delegate_->GetNextId(callback);
362 return;
[email protected]491aaa62012-06-07 03:50:18363 }
avib7348942015-12-25 20:57:10364 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25365 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53366}
367
[email protected]53ac00e82012-10-18 20:59:20368void DownloadManagerImpl::DetermineDownloadTarget(
369 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
370 // Note that this next call relies on
371 // DownloadItemImplDelegate::DownloadTargetCallback and
372 // DownloadManagerDelegate::DownloadTargetCallback having the same
373 // type. If the types ever diverge, gasket code will need to
374 // be written here.
375 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34376 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20377 // TODO(asanka): Determine a useful path if |target_path| is empty.
Asanka Herath1ba0e9f2017-04-03 18:48:53378 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
379 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path,
380 DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]53ac00e82012-10-18 20:59:20381 }
[email protected]47665442012-07-27 02:31:22382}
383
[email protected]98299622013-01-03 22:26:50384bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43385 DownloadItemImpl* item, const base::Closure& complete_callback) {
386 if (!delegate_ ||
387 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50388 return true;
[email protected]18710a42a2012-10-17 19:50:43389 }
390 // Otherwise, the delegate has accepted responsibility to run the
391 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50392 return false;
[email protected]fc03de22011-12-06 23:28:12393}
394
[email protected]2dec8ec2013-02-07 19:20:34395bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
396 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18397 if (!delegate_)
398 return false;
399
[email protected]fc03de22011-12-06 23:28:12400 return delegate_->ShouldOpenFileBasedOnExtension(path);
401}
402
[email protected]bde0e4f2012-11-08 22:49:59403bool DownloadManagerImpl::ShouldOpenDownload(
404 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43405 if (!delegate_)
406 return true;
407
[email protected]bde0e4f2012-11-08 22:49:59408 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
409 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
410 // to have the same type :-}.
411 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43412}
413
[email protected]35869622012-10-26 23:23:55414void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57415 delegate_ = delegate;
416}
417
[email protected]35869622012-10-26 23:23:55418DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28419 return delegate_;
420}
421
[email protected]5656f8a2011-11-17 16:12:58422void DownloadManagerImpl::Shutdown() {
pkastingf5279482016-07-27 02:18:20423 DVLOG(20) << __func__ << "() shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13424 if (!shutdown_needed_)
425 return;
426 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29427
ericwilligers254597b2016-10-17 10:32:31428 for (auto& observer : observers_)
429 observer.ManagerGoingDown(this);
[email protected]fb4f8d902011-09-16 06:07:08430 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13431
[email protected]fe752272013-05-29 18:57:45432 // If there are in-progress downloads, cancel them. This also goes for
433 // dangerous downloads which will remain in history if they aren't explicitly
434 // accepted or discarded. Canceling will remove the intermediate download
435 // file.
lukasza921fabfa2016-02-05 19:51:48436 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54437 DownloadItemImpl* download = it.second.get();
[email protected]fe752272013-05-29 18:57:45438 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17439 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29440 }
avicebda7922016-10-21 17:37:54441 downloads_.clear();
asankaeef62b02016-03-14 21:23:11442 downloads_by_guid_.clear();
Min Qin3cbdb8d2017-08-25 06:23:09443 url_download_handlers_.clear();
initial.commit09911bf2008-07-26 23:55:29444
[email protected]41f558fb2012-01-09 22:59:58445 // We'll have nothing more to report to the observers after this point.
446 observers_.Clear();
447
[email protected]b441a8492012-06-06 14:55:57448 if (delegate_)
449 delegate_->Shutdown();
avicebda7922016-10-21 17:37:54450 delegate_ = nullptr;
initial.commit09911bf2008-07-26 23:55:29451}
452
[email protected]530047e2013-07-12 17:02:25453void DownloadManagerImpl::StartDownload(
dchengf6c40582016-04-09 00:05:13454 std::unique_ptr<DownloadCreateInfo> info,
Min Qin0e4d82a2017-09-01 20:40:31455 std::unique_ptr<DownloadManager::InputStream> stream,
[email protected]530047e2013-07-12 17:02:25456 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52457 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25458 DCHECK(info);
Min Qin0e4d82a2017-09-01 20:40:31459
asanka00b621f2016-02-19 18:09:23460 // |stream| is only non-nil if the download request was successful.
Min Qin0e4d82a2017-09-01 20:40:31461 DCHECK(
462 (info->result == DOWNLOAD_INTERRUPT_REASON_NONE && !stream->IsEmpty()) ||
463 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && stream->IsEmpty()));
pkastingf5279482016-07-27 02:18:20464 DVLOG(20) << __func__
465 << "() result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10466 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25467 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
jialiul82b8e502016-09-30 17:42:42468 if (new_download)
469 RecordDownloadConnectionSecurity(info->url(), info->url_chain);
avib7348942015-12-25 20:57:10470 base::Callback<void(uint32_t)> got_id(base::Bind(
471 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
472 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02473 if (new_download) {
[email protected]530047e2013-07-12 17:02:25474 GetNextId(got_id);
475 } else {
476 got_id.Run(download_id);
477 }
478}
479
480void DownloadManagerImpl::StartDownloadWithId(
dchengf6c40582016-04-09 00:05:13481 std::unique_ptr<DownloadCreateInfo> info,
Min Qin0e4d82a2017-09-01 20:40:31482 std::unique_ptr<DownloadManager::InputStream> stream,
[email protected]530047e2013-07-12 17:02:25483 const DownloadUrlParameters::OnStartedCallback& on_started,
484 bool new_download,
avib7348942015-12-25 20:57:10485 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52486 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25487 DCHECK_NE(content::DownloadItem::kInvalidId, id);
avicebda7922016-10-21 17:37:54488 DownloadItemImpl* download = nullptr;
[email protected]530047e2013-07-12 17:02:25489 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02490 download = CreateActiveItem(id, *info);
491 } else {
avicebda7922016-10-21 17:37:54492 auto item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02493 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37494 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47495 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02496 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37497 // removed after it was resumed. Ignore. If the download is cancelled
498 // while resuming, then also ignore the request.
Min Qin5b912dc42017-06-07 23:10:18499 info->request_handle->CancelRequest(true);
[email protected]530047e2013-07-12 17:02:25500 if (!on_started.is_null())
avicebda7922016-10-21 17:37:54501 on_started.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
siggi2b05ad102017-07-21 18:57:56502 // The ByteStreamReader lives and dies on the download sequence.
asanka00b621f2016-02-19 18:09:23503 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
siggi2b05ad102017-07-21 18:57:56504 GetDownloadTaskRunner()->DeleteSoon(FROM_HERE, stream.release());
[email protected]530047e2013-07-12 17:02:25505 return;
[email protected]89ec81f2013-05-15 19:20:02506 }
avicebda7922016-10-21 17:37:54507 download = item_iterator->second.get();
[email protected]89ec81f2013-05-15 19:20:02508 }
509
[email protected]2dec8ec2013-02-07 19:20:34510 base::FilePath default_download_directory;
Antonio Gomes5986d9bca2017-08-30 22:15:56511#if defined(USE_X11)
Tom Andersone9b9ba0e2017-11-14 21:28:06512 // TODO(thomasanderson,crbug.com/784010): Remove this when all Linux
513 // distros with versions of GTK lower than 3.14.7 are no longer
514 // supported. This should happen when support for Ubuntu Trusty and
515 // Debian Jessie are removed.
thomasanderson1799a1222017-06-14 00:02:02516 default_download_directory = GetTemporaryDownloadDirectory();
517#else
[email protected]96bfed052012-09-15 22:21:01518 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34519 base::FilePath website_save_directory; // Unused
520 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01521 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20522 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01523 }
thomasanderson1799a1222017-06-14 00:02:02524#endif
[email protected]96bfed052012-09-15 22:21:01525
Joy Mingda69d9a2017-11-13 20:22:22526 if (delegate_) {
527 if (!in_progress_download_observer_) {
528 in_progress_download_observer_.reset(
529 new InProgressDownloadObserver(delegate_->GetInProgressCache()));
530 }
531 // May already observe this item, remove observer first.
532 download->RemoveObserver(in_progress_download_observer_.get());
533 download->AddObserver(in_progress_download_observer_.get());
534 }
535
dchengf6c40582016-04-09 00:05:13536 std::unique_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48537
asanka00b621f2016-02-19 18:09:23538 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
539 DCHECK(stream.get());
tfarina42834112016-09-22 13:38:20540 download_file.reset(file_factory_->CreateFile(
541 std::move(info->save_info), default_download_directory,
Min Qin218f1a532017-11-09 01:35:01542 std::move(stream), id, download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48543 }
asanka4350f6a2016-03-15 02:40:57544 // It is important to leave info->save_info intact in the case of an interrupt
545 // so that the DownloadItem can salvage what it can out of a failed resumption
546 // attempt.
[email protected]492c0092013-08-10 13:49:48547
asanka00b621f2016-02-19 18:09:23548 download->Start(std::move(download_file), std::move(info->request_handle),
549 *info);
[email protected]96c3bdbaa2012-08-31 16:39:54550
[email protected]381af492013-03-28 01:56:22551 // For interrupted downloads, Start() will transition the state to
552 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
553 // For new downloads, we notify here, rather than earlier, so that
554 // the download_file is bound to download and all the usual
555 // setters (e.g. Cancel) work.
ericwilligers254597b2016-10-17 10:32:31556 if (new_download) {
557 for (auto& observer : observers_)
558 observer.OnDownloadCreated(this, download);
559 }
[email protected]96c3bdbaa2012-08-31 16:39:54560
[email protected]530047e2013-07-12 17:02:25561 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56562 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35563}
564
[email protected]5656f8a2011-11-17 16:12:58565void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52566 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lukasza921fabfa2016-02-05 19:51:48567 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54568 DownloadItemImpl* item = it.second.get();
[email protected]3d95e542012-11-20 00:52:08569 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48570 }
571}
572
[email protected]db1d8f72012-07-13 19:23:16573void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52574 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47575 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16576 !download_item->GetFileExternallyRemoved() &&
577 delegate_) {
578 delegate_->CheckForFileExistence(
579 download_item,
tzik5b521ad02017-08-18 07:49:19580 base::BindOnce(&DownloadManagerImpl::OnFileExistenceChecked,
581 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48582 }
583}
584
avib7348942015-12-25 20:57:10585void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id,
[email protected]4c544cd2013-01-25 07:54:16586 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52587 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16588 if (!result) { // File does not exist.
skyostil66bd67912016-08-12 12:33:11589 if (base::ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51590 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16591 }
[email protected]9fc114672011-06-15 08:17:48592}
593
asankadb9e50ad2016-10-03 15:00:29594std::string DownloadManagerImpl::GetApplicationClientIdForFileScanning() const {
595 if (delegate_)
596 return delegate_->ApplicationClientIdForFileScanning();
597 return std::string();
598}
599
[email protected]35869622012-10-26 23:23:55600BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58601 return browser_context_;
602}
603
[email protected]530047e2013-07-12 17:02:25604void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34605 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12606 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27607 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13608 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25609 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52610 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25611 GetNextId(base::Bind(
612 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
dcheng36b6aec92015-12-26 06:16:36613 weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type,
614 base::Passed(std::move(request_handle)), item_created));
[email protected]530047e2013-07-12 17:02:25615}
616
617void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
618 const base::FilePath& main_file_path,
619 const GURL& page_url,
620 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13621 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25622 const DownloadItemImplCreated& item_created,
avib7348942015-12-25 20:57:10623 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52624 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25625 DCHECK_NE(content::DownloadItem::kInvalidId, id);
skyostil66bd67912016-08-12 12:33:11626 DCHECK(!base::ContainsKey(downloads_, id));
[email protected]3d95e542012-11-20 00:52:08627 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
Min Qin218f1a532017-11-09 01:35:01628 this, id, main_file_path, page_url, mime_type, std::move(request_handle));
avicebda7922016-10-21 17:37:54629 downloads_[download_item->GetId()] = base::WrapUnique(download_item);
skyostil66bd67912016-08-12 12:33:11630 DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid()));
asankaeef62b02016-03-14 21:23:11631 downloads_by_guid_[download_item->GetGuid()] = download_item;
ericwilligers254597b2016-10-17 10:32:31632 for (auto& observer : observers_)
633 observer.OnDownloadCreated(this, download_item);
[email protected]530047e2013-07-12 17:02:25634 if (!item_created.is_null())
635 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12636}
637
[email protected]8d68a3e02013-01-12 15:57:10638// Resume a download of a specific URL. We send the request to the
639// ResourceDispatcherHost, and let it send us responses like a regular
640// download.
641void DownloadManagerImpl::ResumeInterruptedDownload(
dchengf6c40582016-04-09 00:05:13642 std::unique_ptr<content::DownloadUrlParameters> params,
avib7348942015-12-25 20:57:10643 uint32_t id) {
Min Qinc60045c2017-10-17 03:02:22644 BeginDownloadInternal(std::move(params), id);
[email protected]8d68a3e02013-01-12 15:57:10645}
646
Min Qinc60045c2017-10-17 03:02:22647
[email protected]53ac00e82012-10-18 20:59:20648void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
dchengf6c40582016-04-09 00:05:13649 std::unique_ptr<DownloadItemFactory> item_factory) {
dcheng36b6aec92015-12-26 06:16:36650 item_factory_ = std::move(item_factory);
[email protected]53ac00e82012-10-18 20:59:20651}
652
653void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
dchengf6c40582016-04-09 00:05:13654 std::unique_ptr<DownloadFileFactory> file_factory) {
dcheng36b6aec92015-12-26 06:16:36655 file_factory_ = std::move(file_factory);
[email protected]53ac00e82012-10-18 20:59:20656}
657
[email protected]35869622012-10-26 23:23:55658DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20659 return file_factory_.get();
660}
661
[email protected]db1d8f72012-07-13 19:23:16662void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28663 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40664 return;
665
asankaeef62b02016-03-14 21:23:11666 downloads_by_guid_.erase(download->GetGuid());
avicebda7922016-10-21 17:37:54667 downloads_.erase(download->GetId());
initial.commit09911bf2008-07-26 23:55:29668}
669
Min Qin3cbdb8d2017-08-25 06:23:09670void DownloadManagerImpl::AddUrlDownloadHandler(
Min Qin40b72172017-09-27 00:19:17671 UniqueUrlDownloadHandlerPtr downloader) {
svaldez6901c902015-12-10 18:57:33672 if (downloader)
Min Qin3cbdb8d2017-08-25 06:23:09673 url_download_handlers_.push_back(std::move(downloader));
svaldez6901c902015-12-10 18:57:33674}
675
anantac82dd5be2016-08-26 01:21:53676// static
677DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest(
678 std::unique_ptr<net::URLRequest> url_request,
679 const Referrer& referrer,
680 ResourceContext* resource_context,
681 bool is_content_initiated,
682 int render_process_id,
683 int render_view_route_id,
684 int render_frame_route_id,
685 bool do_not_prompt_for_login) {
Joy Mingda69d9a2017-11-13 20:22:22686 LOG(ERROR) << "BeginDownloadRequest";
anantac82dd5be2016-08-26 01:21:53687 if (ResourceDispatcherHostImpl::Get()->is_shutdown())
688 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
689
690 // The URLRequest needs to be initialized with the referrer and other
691 // information prior to issuing it.
692 ResourceDispatcherHostImpl::Get()->InitializeURLRequest(
693 url_request.get(), referrer,
694 true, // download.
695 render_process_id, render_view_route_id, render_frame_route_id,
megjabloncaf312f2017-01-12 18:47:49696 PREVIEWS_OFF, resource_context);
anantac82dd5be2016-08-26 01:21:53697
698 // We treat a download as a main frame load, and thus update the policy URL on
699 // redirects.
700 //
701 // TODO(davidben): Is this correct? If this came from a
702 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
703 // appropriately?
704 url_request->set_first_party_url_policy(
705 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
706
707 const GURL& url = url_request->original_url();
708
709 // Check if the renderer is permitted to request the requested URL.
Min Qin4eb4ec432017-09-22 20:13:44710 if (!CanRequestURLFromRenderer(render_process_id, url))
anantac82dd5be2016-08-26 01:21:53711 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
anantac82dd5be2016-08-26 01:21:53712
713 const net::URLRequestContext* request_context = url_request->context();
mmenke1918ae782017-04-03 20:10:20714 if (!request_context->job_factory()->IsHandledProtocol(url.scheme())) {
anantac82dd5be2016-08-26 01:21:53715 DVLOG(1) << "Download request for unsupported protocol: "
716 << url.possibly_invalid_spec();
717 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
718 }
719
720 // From this point forward, the |DownloadResourceHandler| is responsible for
721 // |started_callback|.
722 // TODO(ananta)
723 // Find a better way to create the DownloadResourceHandler instance.
724 std::unique_ptr<ResourceHandler> handler(
725 DownloadResourceHandler::Create(url_request.get()));
726
727 ResourceDispatcherHostImpl::Get()->BeginURLRequest(
728 std::move(url_request), std::move(handler), true, // download
729 is_content_initiated, do_not_prompt_for_login, resource_context);
730 return DOWNLOAD_INTERRUPT_REASON_NONE;
731}
732
Min Qin40b72172017-09-27 00:19:17733NavigationURLLoader::NavigationInterceptionCB
734DownloadManagerImpl::GetNavigationInterceptionCB(
735 const scoped_refptr<ResourceResponse>& response,
736 mojo::ScopedDataPipeConsumerHandle consumer_handle,
Xing Liu18a8e0892017-11-15 06:16:13737 const SSLStatus& ssl_status,
738 int frame_tree_node_id) {
Min Qin40b72172017-09-27 00:19:17739 return base::BindOnce(
740 &InterceptNavigationResponse,
741 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
742 weak_factory_.GetWeakPtr()),
743 weak_factory_.GetWeakPtr(), response, std::move(consumer_handle),
Xing Liu18a8e0892017-11-15 06:16:13744 ssl_status, frame_tree_node_id);
Min Qin40b72172017-09-27 00:19:17745}
746
asanka6381bd4e2017-01-05 22:08:37747int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
msramekd9c162a2016-02-23 11:17:21748 const base::Callback<bool(const GURL&)>& url_filter,
asanka6381bd4e2017-01-05 22:08:37749 base::Time remove_begin,
750 base::Time remove_end) {
[email protected]237af6d2013-03-15 23:45:28751 int count = 0;
avicebda7922016-10-21 17:37:54752 auto it = downloads_.begin();
[email protected]237af6d2013-03-15 23:45:28753 while (it != downloads_.end()) {
avicebda7922016-10-21 17:37:54754 DownloadItemImpl* download = it->second.get();
[email protected]237af6d2013-03-15 23:45:28755
756 // Increment done here to protect against invalidation below.
757 ++it;
758
ttr31481dc54b2015-08-06 20:11:26759 if (download->GetState() != DownloadItem::IN_PROGRESS &&
asanka6381bd4e2017-01-05 22:08:37760 url_filter.Run(download->GetURL()) &&
761 download->GetStartTime() >= remove_begin &&
762 (remove_end.is_null() || download->GetStartTime() < remove_end)) {
[email protected]237af6d2013-03-15 23:45:28763 download->Remove();
764 count++;
initial.commit09911bf2008-07-26 23:55:29765 }
initial.commit09911bf2008-07-26 23:55:29766 }
[email protected]237af6d2013-03-15 23:45:28767 return count;
initial.commit09911bf2008-07-26 23:55:29768}
769
[email protected]0d4e30c2012-01-28 00:47:53770void DownloadManagerImpl::DownloadUrl(
Ramin Halavati03efa732017-06-12 09:28:20771 std::unique_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14772 if (params->post_id() >= 0) {
773 // Check this here so that the traceback is more useful.
774 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43775 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14776 }
Min Qin3cbdb8d2017-08-25 06:23:09777
Min Qinc60045c2017-10-17 03:02:22778 BeginDownloadInternal(std::move(params), content::DownloadItem::kInvalidId);
initial.commit09911bf2008-07-26 23:55:29779}
780
[email protected]5656f8a2011-11-17 16:12:58781void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29782 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29783}
784
[email protected]5656f8a2011-11-17 16:12:58785void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29786 observers_.RemoveObserver(observer);
787}
788
[email protected]3d95e542012-11-20 00:52:08789DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11790 const std::string& guid,
avib7348942015-12-25 20:57:10791 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34792 const base::FilePath& current_path,
793 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15794 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08795 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57796 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28797 const GURL& tab_url,
798 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45799 const std::string& mime_type,
800 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23801 base::Time start_time,
802 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21803 const std::string& etag,
804 const std::string& last_modified,
avib7348942015-12-25 20:57:10805 int64_t received_bytes,
806 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57807 const std::string& hash,
[email protected]3d95e542012-11-20 00:52:08808 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15809 DownloadDangerType danger_type,
810 DownloadInterruptReason interrupt_reason,
qinmin23b28572017-02-25 00:24:28811 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23812 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10813 bool transient,
qinmin23b28572017-02-25 00:24:28814 const std::vector<DownloadItem::ReceivedSlice>& received_slices) {
skyostil66bd67912016-08-12 12:33:11815 if (base::ContainsKey(downloads_, id)) {
[email protected]6065748f2013-10-29 01:07:43816 NOTREACHED();
avicebda7922016-10-21 17:37:54817 return nullptr;
[email protected]6065748f2013-10-29 01:07:43818 }
skyostil66bd67912016-08-12 12:33:11819 DCHECK(!base::ContainsKey(downloads_by_guid_, guid));
[email protected]3d95e542012-11-20 00:52:08820 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
asanka038d58d2016-04-14 00:29:28821 this, guid, id, current_path, target_path, url_chain, referrer_url,
asanka3b4be122016-05-11 04:27:57822 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type,
823 start_time, end_time, etag, last_modified, received_bytes, total_bytes,
shaktisahu60eb97f2017-03-03 08:53:23824 hash, state, danger_type, interrupt_reason, opened, last_access_time,
Min Qin218f1a532017-11-09 01:35:01825 transient, received_slices);
avicebda7922016-10-21 17:37:54826 downloads_[id] = base::WrapUnique(item);
asankaeef62b02016-03-14 21:23:11827 downloads_by_guid_[guid] = item;
ericwilligers254597b2016-10-17 10:32:31828 for (auto& observer : observers_)
829 observer.OnDownloadCreated(this, item);
pkastingf5279482016-07-27 02:18:20830 DVLOG(20) << __func__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08831 return item;
initial.commit09911bf2008-07-26 23:55:29832}
833
xingliud64fde652017-05-24 07:04:33834void DownloadManagerImpl::PostInitialization() {
835 DCHECK(!initialized_);
836 initialized_ = true;
837 for (auto& observer : observers_)
838 observer.OnManagerInitialized();
839}
840
841bool DownloadManagerImpl::IsManagerInitialized() const {
842 return initialized_;
843}
844
[email protected]5656f8a2011-11-17 16:12:58845int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56846 int count = 0;
lukasza921fabfa2016-02-05 19:51:48847 for (const auto& it : downloads_) {
848 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56849 ++count;
850 }
851 return count;
[email protected]5656f8a2011-11-17 16:12:58852}
853
[email protected]422a7d12013-10-21 12:10:42854int DownloadManagerImpl::NonMaliciousInProgressCount() const {
855 int count = 0;
lukasza921fabfa2016-02-05 19:51:48856 for (const auto& it : downloads_) {
857 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
858 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
859 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
860 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
861 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59862 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42863 ++count;
864 }
865 }
866 return count;
867}
868
avib7348942015-12-25 20:57:10869DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
avicebda7922016-10-21 17:37:54870 return base::ContainsKey(downloads_, download_id)
871 ? downloads_[download_id].get()
872 : nullptr;
asankaeef62b02016-03-14 21:23:11873}
874
875DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
skyostil66bd67912016-08-12 12:33:11876 return base::ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
877 : nullptr;
[email protected]ba7895d2012-06-22 19:02:52878}
879
Min Qin3cbdb8d2017-08-25 06:23:09880void DownloadManagerImpl::OnUrlDownloadStarted(
xingliu468824d2017-02-28 02:59:25881 std::unique_ptr<DownloadCreateInfo> download_create_info,
Min Qin0e4d82a2017-09-01 20:40:31882 std::unique_ptr<DownloadManager::InputStream> stream,
xingliu468824d2017-02-28 02:59:25883 const DownloadUrlParameters::OnStartedCallback& callback) {
Min Qin0e4d82a2017-09-01 20:40:31884 StartDownload(std::move(download_create_info), std::move(stream), callback);
xingliu468824d2017-02-28 02:59:25885}
886
Min Qin3cbdb8d2017-08-25 06:23:09887void DownloadManagerImpl::OnUrlDownloadStopped(UrlDownloadHandler* downloader) {
888 for (auto ptr = url_download_handlers_.begin();
889 ptr != url_download_handlers_.end(); ++ptr) {
xingliu468824d2017-02-28 02:59:25890 if (ptr->get() == downloader) {
Min Qin3cbdb8d2017-08-25 06:23:09891 url_download_handlers_.erase(ptr);
xingliu468824d2017-02-28 02:59:25892 return;
893 }
894 }
895}
896
[email protected]9f1f4092012-09-10 21:18:04897void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48898 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54899 downloads->push_back(it.second.get());
[email protected]9f1f4092012-09-10 21:18:04900 }
901}
902
[email protected]ffc0c2d2013-01-21 15:32:12903void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06904 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48905 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54906 DownloadItemImpl* item = it.second.get();
[email protected]803036e02013-06-15 17:08:47907 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40908 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06909 ++num_unopened;
910 }
[email protected]35869622012-10-26 23:23:55911 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12912
[email protected]0baf5922013-01-30 13:55:18913 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12914 delegate_->OpenDownload(download);
915}
916
917void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
918 if (delegate_)
919 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06920}
[email protected]5656f8a2011-11-17 16:12:58921
Min Qinc60045c2017-10-17 03:02:22922void DownloadManagerImpl::BeginDownloadInternal(
923 std::unique_ptr<content::DownloadUrlParameters> params,
924 uint32_t id) {
Joy Mingda69d9a2017-11-13 20:22:22925 download::InProgressCache* in_progress_cache =
926 GetBrowserContext()->GetDownloadManagerDelegate()->GetInProgressCache();
927 if (in_progress_cache) {
928 in_progress_cache->AddOrReplaceEntry(download::DownloadEntry(
929 params.get()->guid(), params.get()->request_origin()));
930 }
931
Min Qinc60045c2017-10-17 03:02:22932 if (base::FeatureList::IsEnabled(features::kNetworkService)) {
933 std::unique_ptr<ResourceRequest> request = CreateResourceRequest(
934 params.get());
935 StoragePartitionImpl* storage_partition =
936 GetStoragePartition(browser_context_, params->render_process_host_id(),
937 params->render_frame_host_routing_id());
938 BrowserThread::PostTaskAndReplyWithResult(
939 BrowserThread::IO, FROM_HERE,
940 base::BindOnce(
941 &BeginResourceDownload, std::move(params), std::move(request),
942 storage_partition->url_loader_factory_getter(),
943 base::WrapRefCounted(storage_partition->GetFileSystemContext()),
944 id, weak_factory_.GetWeakPtr()),
945 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
946 weak_factory_.GetWeakPtr()));
947 } else {
948 BrowserThread::PostTaskAndReplyWithResult(
949 BrowserThread::IO, FROM_HERE,
950 base::BindOnce(&BeginDownload, std::move(params),
951 browser_context_->GetResourceContext(), id,
952 weak_factory_.GetWeakPtr()),
953 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
954 weak_factory_.GetWeakPtr()));
955 }
956}
957
[email protected]35869622012-10-26 23:23:55958} // namespace content