blob: 675b08564341c8bed660a7686ed08b68a731faf2 [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"
[email protected]f8e92b5d2013-03-21 18:35:4625#include "content/browser/byte_stream.h"
anantac82dd5be2016-08-26 01:21:5326#include "content/browser/child_process_security_policy_impl.h"
[email protected]71bf3f5e2011-08-15 21:05:2227#include "content/browser/download/download_create_info.h"
[email protected]53ac00e82012-10-18 20:59:2028#include "content/browser/download/download_file_factory.h"
29#include "content/browser/download/download_item_factory.h"
[email protected]c09a8fd2011-11-21 19:54:5030#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0631#include "content/browser/download/download_stats.h"
siggi2b05ad102017-07-21 18:57:5632#include "content/browser/download/download_task_runner.h"
Min Qin3cbdb8d2017-08-25 06:23:0933#include "content/browser/download/download_utils.h"
34#include "content/browser/download/resource_downloader.h"
35#include "content/browser/download/url_downloader.h"
[email protected]678c0362012-12-05 08:02:4436#include "content/browser/loader/resource_dispatcher_host_impl.h"
anantac82dd5be2016-08-26 01:21:5337#include "content/browser/loader/resource_request_info_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3238#include "content/browser/renderer_host/render_view_host_impl.h"
Min Qin3cbdb8d2017-08-25 06:23:0939#include "content/browser/storage_partition_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2940#include "content/browser/web_contents/web_contents_impl.h"
Min Qin40b72172017-09-27 00:19:1741#include "content/common/throttling_url_loader.h"
[email protected]ccb797302011-12-15 16:55:1142#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4443#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3444#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1745#include "content/public/browser/download_manager_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1446#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0747#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1648#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3049#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1350#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2751#include "content/public/browser/web_contents_delegate.h"
Min Qin3cbdb8d2017-08-25 06:23:0952#include "content/public/common/content_features.h"
megjabloncaf312f2017-01-12 18:47:4953#include "content/public/common/previews_state.h"
[email protected]8d68a3e02013-01-12 15:57:1054#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0755#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1456#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1957#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2858#include "net/base/upload_bytes_element_reader.h"
[email protected]eb795632012-09-01 00:19:3159#include "net/url_request/url_request_context.h"
asanka6150c522016-03-25 21:40:5760#include "storage/browser/blob/blob_url_request_job_factory.h"
ttr31481dc54b2015-08-06 20:11:2661#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2962
Antonio Gomes5986d9bca2017-08-30 22:15:5663#if defined(USE_X11)
thomasanderson1799a1222017-06-14 00:02:0264#include "base/nix/xdg_util.h"
65#endif
66
[email protected]35869622012-10-26 23:23:5567namespace content {
[email protected]a0ce3282011-08-19 20:49:5268namespace {
69
Min Qin99b2c57e2017-09-28 05:04:3770StoragePartitionImpl* GetStoragePartition(BrowserContext* context,
71 int render_process_id,
72 int render_frame_id) {
Min Qin3cbdb8d2017-08-25 06:23:0973 DCHECK_CURRENTLY_ON(BrowserThread::UI);
74
75 SiteInstance* site_instance = nullptr;
76 if (render_process_id >= 0) {
77 RenderFrameHost* render_frame_host_ =
78 RenderFrameHost::FromID(render_process_id, render_frame_id);
79 if (render_frame_host_)
80 site_instance = render_frame_host_->GetSiteInstance();
81 }
Min Qin99b2c57e2017-09-28 05:04:3782 return static_cast<StoragePartitionImpl*>(
Min Qin3cbdb8d2017-08-25 06:23:0983 BrowserContext::GetStoragePartition(context, site_instance));
Min Qin3cbdb8d2017-08-25 06:23:0984}
85
Min Qin4eb4ec432017-09-22 20:13:4486bool CanRequestURLFromRenderer(int render_process_id, GURL url) {
87 DCHECK_CURRENTLY_ON(BrowserThread::IO);
88 // Check if the renderer is permitted to request the requested URL.
89 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
90 render_process_id, url)) {
91 DVLOG(1) << "Denied unauthorized download request for "
92 << url.possibly_invalid_spec();
93 return false;
94 }
95 return true;
96}
97
98void CreateInterruptedDownload(
99 DownloadUrlParameters* params,
100 DownloadInterruptReason reason,
101 base::WeakPtr<DownloadManagerImpl> download_manager) {
102 DCHECK_CURRENTLY_ON(BrowserThread::IO);
103 std::unique_ptr<DownloadCreateInfo> failed_created_info(
Min Qin218f1a532017-11-09 01:35:01104 new DownloadCreateInfo(base::Time::Now(),
Min Qin4eb4ec432017-09-22 20:13:44105 base::WrapUnique(new DownloadSaveInfo)));
106 failed_created_info->url_chain.push_back(params->url());
107 failed_created_info->result = reason;
108 std::unique_ptr<ByteStreamReader> empty_byte_stream;
109 BrowserThread::PostTask(
110 BrowserThread::UI, FROM_HERE,
111 base::BindOnce(&DownloadManager::StartDownload, download_manager,
112 std::move(failed_created_info),
Jeremy Roman04f27c372017-10-27 15:20:55113 std::make_unique<DownloadManager::InputStream>(
Min Qin4eb4ec432017-09-22 20:13:44114 std::move(empty_byte_stream)),
115 params->callback()));
116}
117
Min Qin40b72172017-09-27 00:19:17118DownloadManagerImpl::UniqueUrlDownloadHandlerPtr BeginDownload(
dchengf6c40582016-04-09 00:05:13119 std::unique_ptr<DownloadUrlParameters> params,
asanka3b4be122016-05-11 04:27:57120 content::ResourceContext* resource_context,
avib7348942015-12-25 20:57:10121 uint32_t download_id,
svaldez6901c902015-12-10 18:57:33122 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:52123 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:10124
dchengf6c40582016-04-09 00:05:13125 std::unique_ptr<net::URLRequest> url_request =
Ramin Halavati03efa732017-06-12 09:28:20126 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
dchengf6c40582016-04-09 00:05:13127 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
asanka6150c522016-03-25 21:40:57128 params->GetBlobDataHandle();
129 if (blob_data_handle) {
130 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
131 url_request.get(), std::move(blob_data_handle));
132 }
[email protected]fc179dd12013-03-16 09:35:19133
asanka00b621f2016-02-19 18:09:23134 // If there's a valid renderer process associated with the request, then the
135 // request should be driven by the ResourceLoader. Pass it over to the
136 // ResourceDispatcherHostImpl which will in turn pass it along to the
137 // ResourceLoader.
asanka3b4be122016-05-11 04:27:57138 if (params->render_process_host_id() >= 0) {
anantac82dd5be2016-08-26 01:21:53139 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest(
140 std::move(url_request), params->referrer(), resource_context,
141 params->content_initiated(), params->render_process_host_id(),
142 params->render_view_host_routing_id(),
143 params->render_frame_host_routing_id(),
144 params->do_not_prompt_for_login());
asanka00b621f2016-02-19 18:09:23145
146 // If the download was accepted, the DownloadResourceHandler is now
147 // responsible for driving the request to completion.
148 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
149 return nullptr;
150
151 // Otherwise, create an interrupted download.
Min Qin4eb4ec432017-09-22 20:13:44152 CreateInterruptedDownload(params.get(), reason, download_manager);
svaldez6901c902015-12-10 18:57:33153 return nullptr;
154 }
asanka00b621f2016-02-19 18:09:23155
Min Qin40b72172017-09-27 00:19:17156 return DownloadManagerImpl::UniqueUrlDownloadHandlerPtr(
asanka00b621f2016-02-19 18:09:23157 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
qinmin462abbb2017-05-03 21:55:42158 params->referrer(), false)
svaldez6901c902015-12-10 18:57:33159 .release());
[email protected]a0ce3282011-08-19 20:49:52160}
161
Min Qin40b72172017-09-27 00:19:17162DownloadManagerImpl::UniqueUrlDownloadHandlerPtr BeginResourceDownload(
Min Qin3cbdb8d2017-08-25 06:23:09163 std::unique_ptr<DownloadUrlParameters> params,
164 std::unique_ptr<ResourceRequest> request,
165 scoped_refptr<URLLoaderFactoryGetter> url_loader_factory_getter,
Min Qin99b2c57e2017-09-28 05:04:37166 scoped_refptr<storage::FileSystemContext> file_system_context,
Min Qin3cbdb8d2017-08-25 06:23:09167 uint32_t download_id,
168 base::WeakPtr<DownloadManagerImpl> download_manager) {
169 DCHECK_CURRENTLY_ON(BrowserThread::IO);
170
Min Qin4eb4ec432017-09-22 20:13:44171 // Check if the renderer is permitted to request the requested URL.
172 if (params->render_process_host_id() >= 0 &&
173 !CanRequestURLFromRenderer(params->render_process_host_id(),
174 params->url())) {
175 CreateInterruptedDownload(params.get(),
176 DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST,
177 download_manager);
178 return nullptr;
179 }
180
Min Qin40b72172017-09-27 00:19:17181 return DownloadManagerImpl::UniqueUrlDownloadHandlerPtr(
182 ResourceDownloader::BeginDownload(
183 download_manager, std::move(params), std::move(request),
Min Qin99b2c57e2017-09-28 05:04:37184 url_loader_factory_getter, file_system_context, download_id, false)
Min Qin3cbdb8d2017-08-25 06:23:09185 .release());
186}
187
Min Qin40b72172017-09-27 00:19:17188// Creates a ResourceDownloader to own the URLLoader and intercept the response,
189// and passes it back to the DownloadManager.
190void InterceptNavigationResponse(
191 base::OnceCallback<void(DownloadManagerImpl::UniqueUrlDownloadHandlerPtr)>
192 callback,
193 base::WeakPtr<DownloadManagerImpl> download_manager,
194 const scoped_refptr<ResourceResponse>& response,
195 mojo::ScopedDataPipeConsumerHandle consumer_handle,
196 const SSLStatus& ssl_status,
197 std::unique_ptr<ResourceRequest> resource_request,
198 std::unique_ptr<ThrottlingURLLoader> url_loader,
Min Qin83d07872017-10-26 23:22:41199 std::vector<GURL> url_chain,
Min Qin40b72172017-09-27 00:19:17200 base::Optional<ResourceRequestCompletionStatus> completion_status) {
201 DCHECK_CURRENTLY_ON(BrowserThread::IO);
202 DownloadManagerImpl::UniqueUrlDownloadHandlerPtr resource_downloader(
203 ResourceDownloader::InterceptNavigationResponse(
204 download_manager, std::move(resource_request), response,
205 std::move(consumer_handle), ssl_status, std::move(url_loader),
Min Qin83d07872017-10-26 23:22:41206 std::move(url_chain), std::move(completion_status))
Min Qin40b72172017-09-27 00:19:17207 .release());
208
209 BrowserThread::PostTask(
210 BrowserThread::UI, FROM_HERE,
211 base::BindOnce(std::move(callback), std::move(resource_downloader)));
212}
213
[email protected]35869622012-10-26 23:23:55214class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03215 public:
[email protected]b3756012013-03-06 17:43:02216 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58217 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03218
dchengc2282aa2014-10-21 12:07:58219 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02220 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11221 const std::string& guid,
avib7348942015-12-25 20:57:10222 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02223 const base::FilePath& current_path,
224 const base::FilePath& target_path,
225 const std::vector<GURL>& url_chain,
226 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57227 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28228 const GURL& tab_url,
229 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45230 const std::string& mime_type,
231 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23232 base::Time start_time,
233 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21234 const std::string& etag,
235 const std::string& last_modified,
avib7348942015-12-25 20:57:10236 int64_t received_bytes,
237 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57238 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02239 DownloadItem::DownloadState state,
240 DownloadDangerType danger_type,
241 DownloadInterruptReason interrupt_reason,
242 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23243 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10244 bool transient,
Min Qin218f1a532017-11-09 01:35:01245 const std::vector<DownloadItem::ReceivedSlice>& received_slices)
246 override {
asanka038d58d2016-04-14 00:29:28247 return new DownloadItemImpl(
248 delegate, guid, download_id, current_path, target_path, url_chain,
asanka3b4be122016-05-11 04:27:57249 referrer_url, site_url, tab_url, tab_refererr_url, mime_type,
250 original_mime_type, start_time, end_time, etag, last_modified,
251 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason,
Min Qin218f1a532017-11-09 01:35:01252 opened, last_access_time, transient, received_slices);
[email protected]b3756012013-03-06 17:43:02253 }
[email protected]d25fda12012-06-12 17:05:03254
Min Qin218f1a532017-11-09 01:35:01255 DownloadItemImpl* CreateActiveItem(DownloadItemImplDelegate* delegate,
256 uint32_t download_id,
257 const DownloadCreateInfo& info) override {
258 return new DownloadItemImpl(delegate, download_id, info);
[email protected]b3756012013-03-06 17:43:02259 }
[email protected]d25fda12012-06-12 17:05:03260
dchengc2282aa2014-10-21 12:07:58261 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02262 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10263 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02264 const base::FilePath& path,
265 const GURL& url,
[email protected]b3756012013-03-06 17:43:02266 const std::string& mime_type,
Min Qin218f1a532017-11-09 01:35:01267 std::unique_ptr<DownloadRequestHandleInterface> request_handle) override {
dcheng36b6aec92015-12-26 06:16:36268 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
Min Qin218f1a532017-11-09 01:35:01269 std::move(request_handle));
[email protected]b3756012013-03-06 17:43:02270 }
[email protected]d25fda12012-06-12 17:05:03271};
272
Antonio Gomes5986d9bca2017-08-30 22:15:56273#if defined(USE_X11)
thomasanderson1799a1222017-06-14 00:02:02274base::FilePath GetTemporaryDownloadDirectory() {
275 std::unique_ptr<base::Environment> env(base::Environment::Create());
276 return base::nix::GetXDGDirectory(env.get(), "XDG_DATA_HOME", ".local/share");
277}
278#endif
279
[email protected]a0ce3282011-08-19 20:49:52280} // namespace
281
Min Qinddb40ce2017-10-28 03:38:26282DownloadManagerImpl::DownloadManagerImpl(BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20283 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55284 file_factory_(new DownloadFileFactory()),
[email protected]16798692013-04-23 18:08:38285 shutdown_needed_(true),
xingliud64fde652017-05-24 07:04:33286 initialized_(false),
[email protected]16798692013-04-23 18:08:38287 browser_context_(browser_context),
avicebda7922016-10-21 17:37:54288 delegate_(nullptr),
[email protected]eba4a4d2013-05-29 02:18:06289 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38290 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29291}
292
[email protected]5656f8a2011-11-17 16:12:58293DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13294 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29295}
296
[email protected]89ec81f2013-05-15 19:20:02297DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10298 uint32_t id,
299 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52300 DCHECK_CURRENTLY_ON(BrowserThread::UI);
skyostil66bd67912016-08-12 12:33:11301 DCHECK(!base::ContainsKey(downloads_, id));
Min Qin218f1a532017-11-09 01:35:01302 DownloadItemImpl* download = item_factory_->CreateActiveItem(this, id, info);
avicebda7922016-10-21 17:37:54303 downloads_[id] = base::WrapUnique(download);
asankaeef62b02016-03-14 21:23:11304 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02305 return download;
[email protected]381af492013-03-28 01:56:22306}
307
[email protected]530047e2013-07-12 17:02:25308void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52309 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25310 if (delegate_) {
311 delegate_->GetNextId(callback);
312 return;
[email protected]491aaa62012-06-07 03:50:18313 }
avib7348942015-12-25 20:57:10314 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25315 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53316}
317
[email protected]53ac00e82012-10-18 20:59:20318void DownloadManagerImpl::DetermineDownloadTarget(
319 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
320 // Note that this next call relies on
321 // DownloadItemImplDelegate::DownloadTargetCallback and
322 // DownloadManagerDelegate::DownloadTargetCallback having the same
323 // type. If the types ever diverge, gasket code will need to
324 // be written here.
325 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34326 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20327 // TODO(asanka): Determine a useful path if |target_path| is empty.
Asanka Herath1ba0e9f2017-04-03 18:48:53328 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
329 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path,
330 DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]53ac00e82012-10-18 20:59:20331 }
[email protected]47665442012-07-27 02:31:22332}
333
[email protected]98299622013-01-03 22:26:50334bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43335 DownloadItemImpl* item, const base::Closure& complete_callback) {
336 if (!delegate_ ||
337 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50338 return true;
[email protected]18710a42a2012-10-17 19:50:43339 }
340 // Otherwise, the delegate has accepted responsibility to run the
341 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50342 return false;
[email protected]fc03de22011-12-06 23:28:12343}
344
[email protected]2dec8ec2013-02-07 19:20:34345bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
346 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18347 if (!delegate_)
348 return false;
349
[email protected]fc03de22011-12-06 23:28:12350 return delegate_->ShouldOpenFileBasedOnExtension(path);
351}
352
[email protected]bde0e4f2012-11-08 22:49:59353bool DownloadManagerImpl::ShouldOpenDownload(
354 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43355 if (!delegate_)
356 return true;
357
[email protected]bde0e4f2012-11-08 22:49:59358 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
359 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
360 // to have the same type :-}.
361 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43362}
363
[email protected]35869622012-10-26 23:23:55364void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57365 delegate_ = delegate;
366}
367
[email protected]35869622012-10-26 23:23:55368DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28369 return delegate_;
370}
371
[email protected]5656f8a2011-11-17 16:12:58372void DownloadManagerImpl::Shutdown() {
pkastingf5279482016-07-27 02:18:20373 DVLOG(20) << __func__ << "() shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13374 if (!shutdown_needed_)
375 return;
376 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29377
ericwilligers254597b2016-10-17 10:32:31378 for (auto& observer : observers_)
379 observer.ManagerGoingDown(this);
[email protected]fb4f8d902011-09-16 06:07:08380 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13381
[email protected]fe752272013-05-29 18:57:45382 // If there are in-progress downloads, cancel them. This also goes for
383 // dangerous downloads which will remain in history if they aren't explicitly
384 // accepted or discarded. Canceling will remove the intermediate download
385 // file.
lukasza921fabfa2016-02-05 19:51:48386 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54387 DownloadItemImpl* download = it.second.get();
[email protected]fe752272013-05-29 18:57:45388 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17389 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29390 }
avicebda7922016-10-21 17:37:54391 downloads_.clear();
asankaeef62b02016-03-14 21:23:11392 downloads_by_guid_.clear();
Min Qin3cbdb8d2017-08-25 06:23:09393 url_download_handlers_.clear();
initial.commit09911bf2008-07-26 23:55:29394
[email protected]41f558fb2012-01-09 22:59:58395 // We'll have nothing more to report to the observers after this point.
396 observers_.Clear();
397
[email protected]b441a8492012-06-06 14:55:57398 if (delegate_)
399 delegate_->Shutdown();
avicebda7922016-10-21 17:37:54400 delegate_ = nullptr;
initial.commit09911bf2008-07-26 23:55:29401}
402
[email protected]530047e2013-07-12 17:02:25403void DownloadManagerImpl::StartDownload(
dchengf6c40582016-04-09 00:05:13404 std::unique_ptr<DownloadCreateInfo> info,
Min Qin0e4d82a2017-09-01 20:40:31405 std::unique_ptr<DownloadManager::InputStream> stream,
[email protected]530047e2013-07-12 17:02:25406 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52407 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25408 DCHECK(info);
Min Qin0e4d82a2017-09-01 20:40:31409
asanka00b621f2016-02-19 18:09:23410 // |stream| is only non-nil if the download request was successful.
Min Qin0e4d82a2017-09-01 20:40:31411 DCHECK(
412 (info->result == DOWNLOAD_INTERRUPT_REASON_NONE && !stream->IsEmpty()) ||
413 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && stream->IsEmpty()));
pkastingf5279482016-07-27 02:18:20414 DVLOG(20) << __func__
415 << "() result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10416 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25417 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
jialiul82b8e502016-09-30 17:42:42418 if (new_download)
419 RecordDownloadConnectionSecurity(info->url(), info->url_chain);
avib7348942015-12-25 20:57:10420 base::Callback<void(uint32_t)> got_id(base::Bind(
421 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
422 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02423 if (new_download) {
[email protected]530047e2013-07-12 17:02:25424 GetNextId(got_id);
425 } else {
426 got_id.Run(download_id);
427 }
428}
429
430void DownloadManagerImpl::StartDownloadWithId(
dchengf6c40582016-04-09 00:05:13431 std::unique_ptr<DownloadCreateInfo> info,
Min Qin0e4d82a2017-09-01 20:40:31432 std::unique_ptr<DownloadManager::InputStream> stream,
[email protected]530047e2013-07-12 17:02:25433 const DownloadUrlParameters::OnStartedCallback& on_started,
434 bool new_download,
avib7348942015-12-25 20:57:10435 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52436 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25437 DCHECK_NE(content::DownloadItem::kInvalidId, id);
avicebda7922016-10-21 17:37:54438 DownloadItemImpl* download = nullptr;
[email protected]530047e2013-07-12 17:02:25439 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02440 download = CreateActiveItem(id, *info);
441 } else {
avicebda7922016-10-21 17:37:54442 auto item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02443 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37444 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47445 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02446 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37447 // removed after it was resumed. Ignore. If the download is cancelled
448 // while resuming, then also ignore the request.
Min Qin5b912dc42017-06-07 23:10:18449 info->request_handle->CancelRequest(true);
[email protected]530047e2013-07-12 17:02:25450 if (!on_started.is_null())
avicebda7922016-10-21 17:37:54451 on_started.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
siggi2b05ad102017-07-21 18:57:56452 // The ByteStreamReader lives and dies on the download sequence.
asanka00b621f2016-02-19 18:09:23453 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
siggi2b05ad102017-07-21 18:57:56454 GetDownloadTaskRunner()->DeleteSoon(FROM_HERE, stream.release());
[email protected]530047e2013-07-12 17:02:25455 return;
[email protected]89ec81f2013-05-15 19:20:02456 }
avicebda7922016-10-21 17:37:54457 download = item_iterator->second.get();
[email protected]89ec81f2013-05-15 19:20:02458 }
459
[email protected]2dec8ec2013-02-07 19:20:34460 base::FilePath default_download_directory;
Antonio Gomes5986d9bca2017-08-30 22:15:56461#if defined(USE_X11)
thomasanderson1799a1222017-06-14 00:02:02462 // TODO(thomasanderson): Remove this when all Linux distros with
463 // versions of GTK lower than 3.14.7 are no longer supported. This
464 // should happen when support for Ubuntu Trusty and Debian Jessie
465 // are removed.
466 default_download_directory = GetTemporaryDownloadDirectory();
467#else
[email protected]96bfed052012-09-15 22:21:01468 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34469 base::FilePath website_save_directory; // Unused
470 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01471 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20472 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01473 }
thomasanderson1799a1222017-06-14 00:02:02474#endif
[email protected]96bfed052012-09-15 22:21:01475
dchengf6c40582016-04-09 00:05:13476 std::unique_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48477
asanka00b621f2016-02-19 18:09:23478 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
479 DCHECK(stream.get());
tfarina42834112016-09-22 13:38:20480 download_file.reset(file_factory_->CreateFile(
481 std::move(info->save_info), default_download_directory,
Min Qin218f1a532017-11-09 01:35:01482 std::move(stream), id, download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48483 }
asanka4350f6a2016-03-15 02:40:57484 // It is important to leave info->save_info intact in the case of an interrupt
485 // so that the DownloadItem can salvage what it can out of a failed resumption
486 // attempt.
[email protected]492c0092013-08-10 13:49:48487
asanka00b621f2016-02-19 18:09:23488 download->Start(std::move(download_file), std::move(info->request_handle),
489 *info);
[email protected]96c3bdbaa2012-08-31 16:39:54490
[email protected]381af492013-03-28 01:56:22491 // For interrupted downloads, Start() will transition the state to
492 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
493 // For new downloads, we notify here, rather than earlier, so that
494 // the download_file is bound to download and all the usual
495 // setters (e.g. Cancel) work.
ericwilligers254597b2016-10-17 10:32:31496 if (new_download) {
497 for (auto& observer : observers_)
498 observer.OnDownloadCreated(this, download);
499 }
[email protected]96c3bdbaa2012-08-31 16:39:54500
[email protected]530047e2013-07-12 17:02:25501 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56502 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35503}
504
[email protected]5656f8a2011-11-17 16:12:58505void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52506 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lukasza921fabfa2016-02-05 19:51:48507 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54508 DownloadItemImpl* item = it.second.get();
[email protected]3d95e542012-11-20 00:52:08509 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48510 }
511}
512
[email protected]db1d8f72012-07-13 19:23:16513void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52514 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47515 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16516 !download_item->GetFileExternallyRemoved() &&
517 delegate_) {
518 delegate_->CheckForFileExistence(
519 download_item,
tzik5b521ad02017-08-18 07:49:19520 base::BindOnce(&DownloadManagerImpl::OnFileExistenceChecked,
521 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48522 }
523}
524
avib7348942015-12-25 20:57:10525void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id,
[email protected]4c544cd2013-01-25 07:54:16526 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52527 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16528 if (!result) { // File does not exist.
skyostil66bd67912016-08-12 12:33:11529 if (base::ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51530 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16531 }
[email protected]9fc114672011-06-15 08:17:48532}
533
asankadb9e50ad2016-10-03 15:00:29534std::string DownloadManagerImpl::GetApplicationClientIdForFileScanning() const {
535 if (delegate_)
536 return delegate_->ApplicationClientIdForFileScanning();
537 return std::string();
538}
539
[email protected]35869622012-10-26 23:23:55540BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58541 return browser_context_;
542}
543
[email protected]530047e2013-07-12 17:02:25544void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34545 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12546 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27547 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13548 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25549 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52550 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25551 GetNextId(base::Bind(
552 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
dcheng36b6aec92015-12-26 06:16:36553 weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type,
554 base::Passed(std::move(request_handle)), item_created));
[email protected]530047e2013-07-12 17:02:25555}
556
557void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
558 const base::FilePath& main_file_path,
559 const GURL& page_url,
560 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13561 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25562 const DownloadItemImplCreated& item_created,
avib7348942015-12-25 20:57:10563 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52564 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25565 DCHECK_NE(content::DownloadItem::kInvalidId, id);
skyostil66bd67912016-08-12 12:33:11566 DCHECK(!base::ContainsKey(downloads_, id));
[email protected]3d95e542012-11-20 00:52:08567 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
Min Qin218f1a532017-11-09 01:35:01568 this, id, main_file_path, page_url, mime_type, std::move(request_handle));
avicebda7922016-10-21 17:37:54569 downloads_[download_item->GetId()] = base::WrapUnique(download_item);
skyostil66bd67912016-08-12 12:33:11570 DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid()));
asankaeef62b02016-03-14 21:23:11571 downloads_by_guid_[download_item->GetGuid()] = download_item;
ericwilligers254597b2016-10-17 10:32:31572 for (auto& observer : observers_)
573 observer.OnDownloadCreated(this, download_item);
[email protected]530047e2013-07-12 17:02:25574 if (!item_created.is_null())
575 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12576}
577
[email protected]8d68a3e02013-01-12 15:57:10578// Resume a download of a specific URL. We send the request to the
579// ResourceDispatcherHost, and let it send us responses like a regular
580// download.
581void DownloadManagerImpl::ResumeInterruptedDownload(
dchengf6c40582016-04-09 00:05:13582 std::unique_ptr<content::DownloadUrlParameters> params,
avib7348942015-12-25 20:57:10583 uint32_t id) {
Min Qinc60045c2017-10-17 03:02:22584 BeginDownloadInternal(std::move(params), id);
[email protected]8d68a3e02013-01-12 15:57:10585}
586
Min Qinc60045c2017-10-17 03:02:22587
[email protected]53ac00e82012-10-18 20:59:20588void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
dchengf6c40582016-04-09 00:05:13589 std::unique_ptr<DownloadItemFactory> item_factory) {
dcheng36b6aec92015-12-26 06:16:36590 item_factory_ = std::move(item_factory);
[email protected]53ac00e82012-10-18 20:59:20591}
592
593void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
dchengf6c40582016-04-09 00:05:13594 std::unique_ptr<DownloadFileFactory> file_factory) {
dcheng36b6aec92015-12-26 06:16:36595 file_factory_ = std::move(file_factory);
[email protected]53ac00e82012-10-18 20:59:20596}
597
[email protected]35869622012-10-26 23:23:55598DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20599 return file_factory_.get();
600}
601
[email protected]db1d8f72012-07-13 19:23:16602void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28603 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40604 return;
605
asankaeef62b02016-03-14 21:23:11606 downloads_by_guid_.erase(download->GetGuid());
avicebda7922016-10-21 17:37:54607 downloads_.erase(download->GetId());
initial.commit09911bf2008-07-26 23:55:29608}
609
Min Qin3cbdb8d2017-08-25 06:23:09610void DownloadManagerImpl::AddUrlDownloadHandler(
Min Qin40b72172017-09-27 00:19:17611 UniqueUrlDownloadHandlerPtr downloader) {
svaldez6901c902015-12-10 18:57:33612 if (downloader)
Min Qin3cbdb8d2017-08-25 06:23:09613 url_download_handlers_.push_back(std::move(downloader));
svaldez6901c902015-12-10 18:57:33614}
615
anantac82dd5be2016-08-26 01:21:53616// static
617DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest(
618 std::unique_ptr<net::URLRequest> url_request,
619 const Referrer& referrer,
620 ResourceContext* resource_context,
621 bool is_content_initiated,
622 int render_process_id,
623 int render_view_route_id,
624 int render_frame_route_id,
625 bool do_not_prompt_for_login) {
626 if (ResourceDispatcherHostImpl::Get()->is_shutdown())
627 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
628
629 // The URLRequest needs to be initialized with the referrer and other
630 // information prior to issuing it.
631 ResourceDispatcherHostImpl::Get()->InitializeURLRequest(
632 url_request.get(), referrer,
633 true, // download.
634 render_process_id, render_view_route_id, render_frame_route_id,
megjabloncaf312f2017-01-12 18:47:49635 PREVIEWS_OFF, resource_context);
anantac82dd5be2016-08-26 01:21:53636
637 // We treat a download as a main frame load, and thus update the policy URL on
638 // redirects.
639 //
640 // TODO(davidben): Is this correct? If this came from a
641 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
642 // appropriately?
643 url_request->set_first_party_url_policy(
644 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
645
646 const GURL& url = url_request->original_url();
647
648 // Check if the renderer is permitted to request the requested URL.
Min Qin4eb4ec432017-09-22 20:13:44649 if (!CanRequestURLFromRenderer(render_process_id, url))
anantac82dd5be2016-08-26 01:21:53650 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
anantac82dd5be2016-08-26 01:21:53651
652 const net::URLRequestContext* request_context = url_request->context();
mmenke1918ae782017-04-03 20:10:20653 if (!request_context->job_factory()->IsHandledProtocol(url.scheme())) {
anantac82dd5be2016-08-26 01:21:53654 DVLOG(1) << "Download request for unsupported protocol: "
655 << url.possibly_invalid_spec();
656 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
657 }
658
659 // From this point forward, the |DownloadResourceHandler| is responsible for
660 // |started_callback|.
661 // TODO(ananta)
662 // Find a better way to create the DownloadResourceHandler instance.
663 std::unique_ptr<ResourceHandler> handler(
664 DownloadResourceHandler::Create(url_request.get()));
665
666 ResourceDispatcherHostImpl::Get()->BeginURLRequest(
667 std::move(url_request), std::move(handler), true, // download
668 is_content_initiated, do_not_prompt_for_login, resource_context);
669 return DOWNLOAD_INTERRUPT_REASON_NONE;
670}
671
Min Qin40b72172017-09-27 00:19:17672NavigationURLLoader::NavigationInterceptionCB
673DownloadManagerImpl::GetNavigationInterceptionCB(
674 const scoped_refptr<ResourceResponse>& response,
675 mojo::ScopedDataPipeConsumerHandle consumer_handle,
676 const SSLStatus& ssl_status) {
677 return base::BindOnce(
678 &InterceptNavigationResponse,
679 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
680 weak_factory_.GetWeakPtr()),
681 weak_factory_.GetWeakPtr(), response, std::move(consumer_handle),
682 ssl_status);
683}
684
asanka6381bd4e2017-01-05 22:08:37685int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
msramekd9c162a2016-02-23 11:17:21686 const base::Callback<bool(const GURL&)>& url_filter,
asanka6381bd4e2017-01-05 22:08:37687 base::Time remove_begin,
688 base::Time remove_end) {
[email protected]237af6d2013-03-15 23:45:28689 int count = 0;
avicebda7922016-10-21 17:37:54690 auto it = downloads_.begin();
[email protected]237af6d2013-03-15 23:45:28691 while (it != downloads_.end()) {
avicebda7922016-10-21 17:37:54692 DownloadItemImpl* download = it->second.get();
[email protected]237af6d2013-03-15 23:45:28693
694 // Increment done here to protect against invalidation below.
695 ++it;
696
ttr31481dc54b2015-08-06 20:11:26697 if (download->GetState() != DownloadItem::IN_PROGRESS &&
asanka6381bd4e2017-01-05 22:08:37698 url_filter.Run(download->GetURL()) &&
699 download->GetStartTime() >= remove_begin &&
700 (remove_end.is_null() || download->GetStartTime() < remove_end)) {
[email protected]237af6d2013-03-15 23:45:28701 download->Remove();
702 count++;
initial.commit09911bf2008-07-26 23:55:29703 }
initial.commit09911bf2008-07-26 23:55:29704 }
[email protected]237af6d2013-03-15 23:45:28705 return count;
initial.commit09911bf2008-07-26 23:55:29706}
707
[email protected]0d4e30c2012-01-28 00:47:53708void DownloadManagerImpl::DownloadUrl(
Ramin Halavati03efa732017-06-12 09:28:20709 std::unique_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14710 if (params->post_id() >= 0) {
711 // Check this here so that the traceback is more useful.
712 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43713 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14714 }
Min Qin3cbdb8d2017-08-25 06:23:09715
Min Qinc60045c2017-10-17 03:02:22716 BeginDownloadInternal(std::move(params), content::DownloadItem::kInvalidId);
initial.commit09911bf2008-07-26 23:55:29717}
718
[email protected]5656f8a2011-11-17 16:12:58719void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29720 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29721}
722
[email protected]5656f8a2011-11-17 16:12:58723void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29724 observers_.RemoveObserver(observer);
725}
726
[email protected]3d95e542012-11-20 00:52:08727DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11728 const std::string& guid,
avib7348942015-12-25 20:57:10729 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34730 const base::FilePath& current_path,
731 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15732 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08733 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57734 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28735 const GURL& tab_url,
736 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45737 const std::string& mime_type,
738 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23739 base::Time start_time,
740 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21741 const std::string& etag,
742 const std::string& last_modified,
avib7348942015-12-25 20:57:10743 int64_t received_bytes,
744 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57745 const std::string& hash,
[email protected]3d95e542012-11-20 00:52:08746 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15747 DownloadDangerType danger_type,
748 DownloadInterruptReason interrupt_reason,
qinmin23b28572017-02-25 00:24:28749 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23750 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10751 bool transient,
qinmin23b28572017-02-25 00:24:28752 const std::vector<DownloadItem::ReceivedSlice>& received_slices) {
skyostil66bd67912016-08-12 12:33:11753 if (base::ContainsKey(downloads_, id)) {
[email protected]6065748f2013-10-29 01:07:43754 NOTREACHED();
avicebda7922016-10-21 17:37:54755 return nullptr;
[email protected]6065748f2013-10-29 01:07:43756 }
skyostil66bd67912016-08-12 12:33:11757 DCHECK(!base::ContainsKey(downloads_by_guid_, guid));
[email protected]3d95e542012-11-20 00:52:08758 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
asanka038d58d2016-04-14 00:29:28759 this, guid, id, current_path, target_path, url_chain, referrer_url,
asanka3b4be122016-05-11 04:27:57760 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type,
761 start_time, end_time, etag, last_modified, received_bytes, total_bytes,
shaktisahu60eb97f2017-03-03 08:53:23762 hash, state, danger_type, interrupt_reason, opened, last_access_time,
Min Qin218f1a532017-11-09 01:35:01763 transient, received_slices);
avicebda7922016-10-21 17:37:54764 downloads_[id] = base::WrapUnique(item);
asankaeef62b02016-03-14 21:23:11765 downloads_by_guid_[guid] = item;
ericwilligers254597b2016-10-17 10:32:31766 for (auto& observer : observers_)
767 observer.OnDownloadCreated(this, item);
pkastingf5279482016-07-27 02:18:20768 DVLOG(20) << __func__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08769 return item;
initial.commit09911bf2008-07-26 23:55:29770}
771
xingliud64fde652017-05-24 07:04:33772void DownloadManagerImpl::PostInitialization() {
773 DCHECK(!initialized_);
774 initialized_ = true;
775 for (auto& observer : observers_)
776 observer.OnManagerInitialized();
777}
778
779bool DownloadManagerImpl::IsManagerInitialized() const {
780 return initialized_;
781}
782
[email protected]5656f8a2011-11-17 16:12:58783int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56784 int count = 0;
lukasza921fabfa2016-02-05 19:51:48785 for (const auto& it : downloads_) {
786 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56787 ++count;
788 }
789 return count;
[email protected]5656f8a2011-11-17 16:12:58790}
791
[email protected]422a7d12013-10-21 12:10:42792int DownloadManagerImpl::NonMaliciousInProgressCount() const {
793 int count = 0;
lukasza921fabfa2016-02-05 19:51:48794 for (const auto& it : downloads_) {
795 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
796 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
797 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
798 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
799 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59800 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42801 ++count;
802 }
803 }
804 return count;
805}
806
avib7348942015-12-25 20:57:10807DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
avicebda7922016-10-21 17:37:54808 return base::ContainsKey(downloads_, download_id)
809 ? downloads_[download_id].get()
810 : nullptr;
asankaeef62b02016-03-14 21:23:11811}
812
813DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
skyostil66bd67912016-08-12 12:33:11814 return base::ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
815 : nullptr;
[email protected]ba7895d2012-06-22 19:02:52816}
817
Min Qin3cbdb8d2017-08-25 06:23:09818void DownloadManagerImpl::OnUrlDownloadStarted(
xingliu468824d2017-02-28 02:59:25819 std::unique_ptr<DownloadCreateInfo> download_create_info,
Min Qin0e4d82a2017-09-01 20:40:31820 std::unique_ptr<DownloadManager::InputStream> stream,
xingliu468824d2017-02-28 02:59:25821 const DownloadUrlParameters::OnStartedCallback& callback) {
Min Qin0e4d82a2017-09-01 20:40:31822 StartDownload(std::move(download_create_info), std::move(stream), callback);
xingliu468824d2017-02-28 02:59:25823}
824
Min Qin3cbdb8d2017-08-25 06:23:09825void DownloadManagerImpl::OnUrlDownloadStopped(UrlDownloadHandler* downloader) {
826 for (auto ptr = url_download_handlers_.begin();
827 ptr != url_download_handlers_.end(); ++ptr) {
xingliu468824d2017-02-28 02:59:25828 if (ptr->get() == downloader) {
Min Qin3cbdb8d2017-08-25 06:23:09829 url_download_handlers_.erase(ptr);
xingliu468824d2017-02-28 02:59:25830 return;
831 }
832 }
833}
834
[email protected]9f1f4092012-09-10 21:18:04835void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48836 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54837 downloads->push_back(it.second.get());
[email protected]9f1f4092012-09-10 21:18:04838 }
839}
840
[email protected]ffc0c2d2013-01-21 15:32:12841void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06842 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48843 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54844 DownloadItemImpl* item = it.second.get();
[email protected]803036e02013-06-15 17:08:47845 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40846 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06847 ++num_unopened;
848 }
[email protected]35869622012-10-26 23:23:55849 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12850
[email protected]0baf5922013-01-30 13:55:18851 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12852 delegate_->OpenDownload(download);
853}
854
855void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
856 if (delegate_)
857 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06858}
[email protected]5656f8a2011-11-17 16:12:58859
Min Qinc60045c2017-10-17 03:02:22860void DownloadManagerImpl::BeginDownloadInternal(
861 std::unique_ptr<content::DownloadUrlParameters> params,
862 uint32_t id) {
863 if (base::FeatureList::IsEnabled(features::kNetworkService)) {
864 std::unique_ptr<ResourceRequest> request = CreateResourceRequest(
865 params.get());
866 StoragePartitionImpl* storage_partition =
867 GetStoragePartition(browser_context_, params->render_process_host_id(),
868 params->render_frame_host_routing_id());
869 BrowserThread::PostTaskAndReplyWithResult(
870 BrowserThread::IO, FROM_HERE,
871 base::BindOnce(
872 &BeginResourceDownload, std::move(params), std::move(request),
873 storage_partition->url_loader_factory_getter(),
874 base::WrapRefCounted(storage_partition->GetFileSystemContext()),
875 id, weak_factory_.GetWeakPtr()),
876 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
877 weak_factory_.GetWeakPtr()));
878 } else {
879 BrowserThread::PostTaskAndReplyWithResult(
880 BrowserThread::IO, FROM_HERE,
881 base::BindOnce(&BeginDownload, std::move(params),
882 browser_context_->GetResourceContext(), id,
883 weak_factory_.GetWeakPtr()),
884 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
885 weak_factory_.GetWeakPtr()));
886 }
887}
888
[email protected]35869622012-10-26 23:23:55889} // namespace content