blob: 0abd4a0f418fa3be54c0ec86c85db72b95c72805 [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"
[email protected]ccb797302011-12-15 16:55:1141#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4442#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3443#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1744#include "content/public/browser/download_manager_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1445#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0746#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1647#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3048#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1349#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2750#include "content/public/browser/web_contents_delegate.h"
Min Qin3cbdb8d2017-08-25 06:23:0951#include "content/public/common/content_features.h"
megjabloncaf312f2017-01-12 18:47:4952#include "content/public/common/previews_state.h"
[email protected]8d68a3e02013-01-12 15:57:1053#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0754#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1455#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1956#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2857#include "net/base/upload_bytes_element_reader.h"
mikecirone8b85c432016-09-08 19:11:0058#include "net/log/net_log_source_type.h"
mikecironef22f9812016-10-04 03:40:1959#include "net/log/net_log_with_source.h"
[email protected]eb795632012-09-01 00:19:3160#include "net/url_request/url_request_context.h"
asanka6150c522016-03-25 21:40:5761#include "storage/browser/blob/blob_url_request_job_factory.h"
ttr31481dc54b2015-08-06 20:11:2662#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2963
Tim Brown07e062e82017-06-26 19:38:0364#if defined(USE_X11) && !defined(OS_CHROMEOS)
thomasanderson1799a1222017-06-14 00:02:0265#include "base/nix/xdg_util.h"
66#endif
67
[email protected]35869622012-10-26 23:23:5568namespace content {
[email protected]a0ce3282011-08-19 20:49:5269namespace {
70
Min Qin3cbdb8d2017-08-25 06:23:0971scoped_refptr<URLLoaderFactoryGetter> GetURLLoaderFactoryGetter(
72 BrowserContext* context,
73 int render_process_id,
74 int render_frame_id) {
75 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 }
84 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
85 BrowserContext::GetStoragePartition(context, site_instance));
86 return partition->url_loader_factory_getter();
87}
88
dchengf6c40582016-04-09 00:05:1389std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload(
90 std::unique_ptr<DownloadUrlParameters> params,
asanka3b4be122016-05-11 04:27:5791 content::ResourceContext* resource_context,
avib7348942015-12-25 20:57:1092 uint32_t download_id,
svaldez6901c902015-12-10 18:57:3393 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:5294 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:1095
dchengf6c40582016-04-09 00:05:1396 std::unique_ptr<net::URLRequest> url_request =
Ramin Halavati03efa732017-06-12 09:28:2097 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
dchengf6c40582016-04-09 00:05:1398 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
asanka6150c522016-03-25 21:40:5799 params->GetBlobDataHandle();
100 if (blob_data_handle) {
101 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
102 url_request.get(), std::move(blob_data_handle));
103 }
[email protected]fc179dd12013-03-16 09:35:19104
asanka00b621f2016-02-19 18:09:23105 // If there's a valid renderer process associated with the request, then the
106 // request should be driven by the ResourceLoader. Pass it over to the
107 // ResourceDispatcherHostImpl which will in turn pass it along to the
108 // ResourceLoader.
asanka3b4be122016-05-11 04:27:57109 if (params->render_process_host_id() >= 0) {
anantac82dd5be2016-08-26 01:21:53110 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest(
111 std::move(url_request), params->referrer(), resource_context,
112 params->content_initiated(), params->render_process_host_id(),
113 params->render_view_host_routing_id(),
114 params->render_frame_host_routing_id(),
115 params->do_not_prompt_for_login());
asanka00b621f2016-02-19 18:09:23116
117 // If the download was accepted, the DownloadResourceHandler is now
118 // responsible for driving the request to completion.
119 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
120 return nullptr;
121
122 // Otherwise, create an interrupted download.
dchengf6c40582016-04-09 00:05:13123 std::unique_ptr<DownloadCreateInfo> failed_created_info(
tfarina42834112016-09-22 13:38:20124 new DownloadCreateInfo(base::Time::Now(), net::NetLogWithSource(),
dchengf6c40582016-04-09 00:05:13125 base::WrapUnique(new DownloadSaveInfo)));
asanka00b621f2016-02-19 18:09:23126 failed_created_info->url_chain.push_back(params->url());
127 failed_created_info->result = reason;
dchengf6c40582016-04-09 00:05:13128 std::unique_ptr<ByteStreamReader> empty_byte_stream;
asanka00b621f2016-02-19 18:09:23129 BrowserThread::PostTask(
130 BrowserThread::UI, FROM_HERE,
tzik5b521ad02017-08-18 07:49:19131 base::BindOnce(&DownloadManager::StartDownload, download_manager,
132 base::Passed(&failed_created_info),
133 base::Passed(&empty_byte_stream), params->callback()));
svaldez6901c902015-12-10 18:57:33134 return nullptr;
135 }
asanka00b621f2016-02-19 18:09:23136
dchengf6c40582016-04-09 00:05:13137 return std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>(
asanka00b621f2016-02-19 18:09:23138 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
qinmin462abbb2017-05-03 21:55:42139 params->referrer(), false)
svaldez6901c902015-12-10 18:57:33140 .release());
[email protected]a0ce3282011-08-19 20:49:52141}
142
Min Qin3cbdb8d2017-08-25 06:23:09143std::unique_ptr<ResourceDownloader, BrowserThread::DeleteOnIOThread>
144BeginResourceDownload(
145 std::unique_ptr<DownloadUrlParameters> params,
146 std::unique_ptr<ResourceRequest> request,
147 scoped_refptr<URLLoaderFactoryGetter> url_loader_factory_getter,
148 uint32_t download_id,
149 base::WeakPtr<DownloadManagerImpl> download_manager) {
150 DCHECK_CURRENTLY_ON(BrowserThread::IO);
151
152 return std::unique_ptr<ResourceDownloader, BrowserThread::DeleteOnIOThread>(
153 ResourceDownloader::BeginDownload(download_manager, std::move(params),
154 std::move(request),
155 url_loader_factory_getter, download_id,
156 false)
157 .release());
158}
159
[email protected]35869622012-10-26 23:23:55160class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03161 public:
[email protected]b3756012013-03-06 17:43:02162 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58163 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03164
dchengc2282aa2014-10-21 12:07:58165 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02166 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11167 const std::string& guid,
avib7348942015-12-25 20:57:10168 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02169 const base::FilePath& current_path,
170 const base::FilePath& target_path,
171 const std::vector<GURL>& url_chain,
172 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57173 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28174 const GURL& tab_url,
175 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45176 const std::string& mime_type,
177 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23178 base::Time start_time,
179 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21180 const std::string& etag,
181 const std::string& last_modified,
avib7348942015-12-25 20:57:10182 int64_t received_bytes,
183 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57184 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02185 DownloadItem::DownloadState state,
186 DownloadDangerType danger_type,
187 DownloadInterruptReason interrupt_reason,
188 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23189 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10190 bool transient,
qinmin23b28572017-02-25 00:24:28191 const std::vector<DownloadItem::ReceivedSlice>& received_slices,
tfarina42834112016-09-22 13:38:20192 const net::NetLogWithSource& net_log) override {
asanka038d58d2016-04-14 00:29:28193 return new DownloadItemImpl(
194 delegate, guid, download_id, current_path, target_path, url_chain,
asanka3b4be122016-05-11 04:27:57195 referrer_url, site_url, tab_url, tab_refererr_url, mime_type,
196 original_mime_type, start_time, end_time, etag, last_modified,
197 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason,
shaktisahufd49a3e2017-03-30 18:35:10198 opened, last_access_time, transient, received_slices, net_log);
[email protected]b3756012013-03-06 17:43:02199 }
[email protected]d25fda12012-06-12 17:05:03200
dchengc2282aa2014-10-21 12:07:58201 DownloadItemImpl* CreateActiveItem(
[email protected]b3756012013-03-06 17:43:02202 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10203 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02204 const DownloadCreateInfo& info,
tfarina42834112016-09-22 13:38:20205 const net::NetLogWithSource& net_log) override {
206 return new DownloadItemImpl(delegate, download_id, info, net_log);
[email protected]b3756012013-03-06 17:43:02207 }
[email protected]d25fda12012-06-12 17:05:03208
dchengc2282aa2014-10-21 12:07:58209 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02210 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10211 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02212 const base::FilePath& path,
213 const GURL& url,
[email protected]b3756012013-03-06 17:43:02214 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13215 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
tfarina42834112016-09-22 13:38:20216 const net::NetLogWithSource& net_log) override {
dcheng36b6aec92015-12-26 06:16:36217 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
tfarina42834112016-09-22 13:38:20218 std::move(request_handle), net_log);
[email protected]b3756012013-03-06 17:43:02219 }
[email protected]d25fda12012-06-12 17:05:03220};
221
Tim Brown07e062e82017-06-26 19:38:03222#if defined(USE_X11) && !defined(OS_CHROMEOS)
thomasanderson1799a1222017-06-14 00:02:02223base::FilePath GetTemporaryDownloadDirectory() {
224 std::unique_ptr<base::Environment> env(base::Environment::Create());
225 return base::nix::GetXDGDirectory(env.get(), "XDG_DATA_HOME", ".local/share");
226}
227#endif
228
[email protected]a0ce3282011-08-19 20:49:52229} // namespace
230
avicebda7922016-10-21 17:37:54231DownloadManagerImpl::DownloadManagerImpl(net::NetLog* net_log,
232 BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20233 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55234 file_factory_(new DownloadFileFactory()),
[email protected]16798692013-04-23 18:08:38235 shutdown_needed_(true),
xingliud64fde652017-05-24 07:04:33236 initialized_(false),
[email protected]16798692013-04-23 18:08:38237 browser_context_(browser_context),
avicebda7922016-10-21 17:37:54238 delegate_(nullptr),
[email protected]eba4a4d2013-05-29 02:18:06239 net_log_(net_log),
240 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38241 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29242}
243
[email protected]5656f8a2011-11-17 16:12:58244DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13245 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29246}
247
[email protected]89ec81f2013-05-15 19:20:02248DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10249 uint32_t id,
250 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52251 DCHECK_CURRENTLY_ON(BrowserThread::UI);
skyostil66bd67912016-08-12 12:33:11252 DCHECK(!base::ContainsKey(downloads_, id));
tfarina42834112016-09-22 13:38:20253 net::NetLogWithSource net_log =
254 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD);
[email protected]89ec81f2013-05-15 19:20:02255 DownloadItemImpl* download =
tfarina42834112016-09-22 13:38:20256 item_factory_->CreateActiveItem(this, id, info, net_log);
avicebda7922016-10-21 17:37:54257 downloads_[id] = base::WrapUnique(download);
asankaeef62b02016-03-14 21:23:11258 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02259 return download;
[email protected]381af492013-03-28 01:56:22260}
261
[email protected]530047e2013-07-12 17:02:25262void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52263 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25264 if (delegate_) {
265 delegate_->GetNextId(callback);
266 return;
[email protected]491aaa62012-06-07 03:50:18267 }
avib7348942015-12-25 20:57:10268 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25269 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53270}
271
[email protected]53ac00e82012-10-18 20:59:20272void DownloadManagerImpl::DetermineDownloadTarget(
273 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
274 // Note that this next call relies on
275 // DownloadItemImplDelegate::DownloadTargetCallback and
276 // DownloadManagerDelegate::DownloadTargetCallback having the same
277 // type. If the types ever diverge, gasket code will need to
278 // be written here.
279 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34280 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20281 // TODO(asanka): Determine a useful path if |target_path| is empty.
Asanka Herath1ba0e9f2017-04-03 18:48:53282 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
283 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path,
284 DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]53ac00e82012-10-18 20:59:20285 }
[email protected]47665442012-07-27 02:31:22286}
287
[email protected]98299622013-01-03 22:26:50288bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43289 DownloadItemImpl* item, const base::Closure& complete_callback) {
290 if (!delegate_ ||
291 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50292 return true;
[email protected]18710a42a2012-10-17 19:50:43293 }
294 // Otherwise, the delegate has accepted responsibility to run the
295 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50296 return false;
[email protected]fc03de22011-12-06 23:28:12297}
298
[email protected]2dec8ec2013-02-07 19:20:34299bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
300 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18301 if (!delegate_)
302 return false;
303
[email protected]fc03de22011-12-06 23:28:12304 return delegate_->ShouldOpenFileBasedOnExtension(path);
305}
306
[email protected]bde0e4f2012-11-08 22:49:59307bool DownloadManagerImpl::ShouldOpenDownload(
308 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43309 if (!delegate_)
310 return true;
311
[email protected]bde0e4f2012-11-08 22:49:59312 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
313 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
314 // to have the same type :-}.
315 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43316}
317
[email protected]35869622012-10-26 23:23:55318void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57319 delegate_ = delegate;
320}
321
[email protected]35869622012-10-26 23:23:55322DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28323 return delegate_;
324}
325
[email protected]5656f8a2011-11-17 16:12:58326void DownloadManagerImpl::Shutdown() {
pkastingf5279482016-07-27 02:18:20327 DVLOG(20) << __func__ << "() shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13328 if (!shutdown_needed_)
329 return;
330 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29331
ericwilligers254597b2016-10-17 10:32:31332 for (auto& observer : observers_)
333 observer.ManagerGoingDown(this);
[email protected]fb4f8d902011-09-16 06:07:08334 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13335
[email protected]fe752272013-05-29 18:57:45336 // If there are in-progress downloads, cancel them. This also goes for
337 // dangerous downloads which will remain in history if they aren't explicitly
338 // accepted or discarded. Canceling will remove the intermediate download
339 // file.
lukasza921fabfa2016-02-05 19:51:48340 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54341 DownloadItemImpl* download = it.second.get();
[email protected]fe752272013-05-29 18:57:45342 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17343 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29344 }
avicebda7922016-10-21 17:37:54345 downloads_.clear();
asankaeef62b02016-03-14 21:23:11346 downloads_by_guid_.clear();
Min Qin3cbdb8d2017-08-25 06:23:09347 url_download_handlers_.clear();
initial.commit09911bf2008-07-26 23:55:29348
[email protected]41f558fb2012-01-09 22:59:58349 // We'll have nothing more to report to the observers after this point.
350 observers_.Clear();
351
[email protected]b441a8492012-06-06 14:55:57352 if (delegate_)
353 delegate_->Shutdown();
avicebda7922016-10-21 17:37:54354 delegate_ = nullptr;
initial.commit09911bf2008-07-26 23:55:29355}
356
[email protected]530047e2013-07-12 17:02:25357void DownloadManagerImpl::StartDownload(
dchengf6c40582016-04-09 00:05:13358 std::unique_ptr<DownloadCreateInfo> info,
359 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25360 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52361 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25362 DCHECK(info);
asanka00b621f2016-02-19 18:09:23363 // |stream| is only non-nil if the download request was successful.
364 DCHECK((info->result == DOWNLOAD_INTERRUPT_REASON_NONE && stream.get()) ||
365 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && !stream.get()));
pkastingf5279482016-07-27 02:18:20366 DVLOG(20) << __func__
367 << "() result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10368 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25369 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
jialiul82b8e502016-09-30 17:42:42370 if (new_download)
371 RecordDownloadConnectionSecurity(info->url(), info->url_chain);
avib7348942015-12-25 20:57:10372 base::Callback<void(uint32_t)> got_id(base::Bind(
373 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
374 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02375 if (new_download) {
[email protected]530047e2013-07-12 17:02:25376 GetNextId(got_id);
377 } else {
378 got_id.Run(download_id);
379 }
380}
381
382void DownloadManagerImpl::StartDownloadWithId(
dchengf6c40582016-04-09 00:05:13383 std::unique_ptr<DownloadCreateInfo> info,
384 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25385 const DownloadUrlParameters::OnStartedCallback& on_started,
386 bool new_download,
avib7348942015-12-25 20:57:10387 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52388 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25389 DCHECK_NE(content::DownloadItem::kInvalidId, id);
avicebda7922016-10-21 17:37:54390 DownloadItemImpl* download = nullptr;
[email protected]530047e2013-07-12 17:02:25391 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02392 download = CreateActiveItem(id, *info);
393 } else {
avicebda7922016-10-21 17:37:54394 auto item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02395 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37396 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47397 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02398 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37399 // removed after it was resumed. Ignore. If the download is cancelled
400 // while resuming, then also ignore the request.
Min Qin5b912dc42017-06-07 23:10:18401 info->request_handle->CancelRequest(true);
[email protected]530047e2013-07-12 17:02:25402 if (!on_started.is_null())
avicebda7922016-10-21 17:37:54403 on_started.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
siggi2b05ad102017-07-21 18:57:56404 // The ByteStreamReader lives and dies on the download sequence.
asanka00b621f2016-02-19 18:09:23405 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
siggi2b05ad102017-07-21 18:57:56406 GetDownloadTaskRunner()->DeleteSoon(FROM_HERE, stream.release());
[email protected]530047e2013-07-12 17:02:25407 return;
[email protected]89ec81f2013-05-15 19:20:02408 }
avicebda7922016-10-21 17:37:54409 download = item_iterator->second.get();
[email protected]89ec81f2013-05-15 19:20:02410 }
411
[email protected]2dec8ec2013-02-07 19:20:34412 base::FilePath default_download_directory;
Tim Brown07e062e82017-06-26 19:38:03413#if defined(USE_X11) && !defined(OS_CHROMEOS)
thomasanderson1799a1222017-06-14 00:02:02414 // TODO(thomasanderson): Remove this when all Linux distros with
415 // versions of GTK lower than 3.14.7 are no longer supported. This
416 // should happen when support for Ubuntu Trusty and Debian Jessie
417 // are removed.
418 default_download_directory = GetTemporaryDownloadDirectory();
419#else
[email protected]96bfed052012-09-15 22:21:01420 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34421 base::FilePath website_save_directory; // Unused
422 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01423 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20424 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01425 }
thomasanderson1799a1222017-06-14 00:02:02426#endif
[email protected]96bfed052012-09-15 22:21:01427
dchengf6c40582016-04-09 00:05:13428 std::unique_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48429
asanka00b621f2016-02-19 18:09:23430 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
431 DCHECK(stream.get());
tfarina42834112016-09-22 13:38:20432 download_file.reset(file_factory_->CreateFile(
433 std::move(info->save_info), default_download_directory,
xingliu583a61a2017-04-10 22:12:37434 std::move(stream), download->GetNetLogWithSource(),
tfarina42834112016-09-22 13:38:20435 download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48436 }
asanka4350f6a2016-03-15 02:40:57437 // It is important to leave info->save_info intact in the case of an interrupt
438 // so that the DownloadItem can salvage what it can out of a failed resumption
439 // attempt.
[email protected]492c0092013-08-10 13:49:48440
asanka00b621f2016-02-19 18:09:23441 download->Start(std::move(download_file), std::move(info->request_handle),
442 *info);
[email protected]96c3bdbaa2012-08-31 16:39:54443
[email protected]381af492013-03-28 01:56:22444 // For interrupted downloads, Start() will transition the state to
445 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
446 // For new downloads, we notify here, rather than earlier, so that
447 // the download_file is bound to download and all the usual
448 // setters (e.g. Cancel) work.
ericwilligers254597b2016-10-17 10:32:31449 if (new_download) {
450 for (auto& observer : observers_)
451 observer.OnDownloadCreated(this, download);
452 }
[email protected]96c3bdbaa2012-08-31 16:39:54453
[email protected]530047e2013-07-12 17:02:25454 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56455 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35456}
457
[email protected]5656f8a2011-11-17 16:12:58458void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52459 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lukasza921fabfa2016-02-05 19:51:48460 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54461 DownloadItemImpl* item = it.second.get();
[email protected]3d95e542012-11-20 00:52:08462 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48463 }
464}
465
[email protected]db1d8f72012-07-13 19:23:16466void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52467 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47468 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16469 !download_item->GetFileExternallyRemoved() &&
470 delegate_) {
471 delegate_->CheckForFileExistence(
472 download_item,
tzik5b521ad02017-08-18 07:49:19473 base::BindOnce(&DownloadManagerImpl::OnFileExistenceChecked,
474 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48475 }
476}
477
avib7348942015-12-25 20:57:10478void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id,
[email protected]4c544cd2013-01-25 07:54:16479 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52480 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16481 if (!result) { // File does not exist.
skyostil66bd67912016-08-12 12:33:11482 if (base::ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51483 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16484 }
[email protected]9fc114672011-06-15 08:17:48485}
486
asankadb9e50ad2016-10-03 15:00:29487std::string DownloadManagerImpl::GetApplicationClientIdForFileScanning() const {
488 if (delegate_)
489 return delegate_->ApplicationClientIdForFileScanning();
490 return std::string();
491}
492
[email protected]35869622012-10-26 23:23:55493BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58494 return browser_context_;
495}
496
[email protected]530047e2013-07-12 17:02:25497void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34498 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12499 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27500 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13501 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25502 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52503 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25504 GetNextId(base::Bind(
505 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
dcheng36b6aec92015-12-26 06:16:36506 weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type,
507 base::Passed(std::move(request_handle)), item_created));
[email protected]530047e2013-07-12 17:02:25508}
509
510void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
511 const base::FilePath& main_file_path,
512 const GURL& page_url,
513 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13514 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25515 const DownloadItemImplCreated& item_created,
avib7348942015-12-25 20:57:10516 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52517 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25518 DCHECK_NE(content::DownloadItem::kInvalidId, id);
skyostil66bd67912016-08-12 12:33:11519 DCHECK(!base::ContainsKey(downloads_, id));
tfarina42834112016-09-22 13:38:20520 net::NetLogWithSource net_log =
521 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08522 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
dcheng36b6aec92015-12-26 06:16:36523 this, id, main_file_path, page_url, mime_type, std::move(request_handle),
tfarina42834112016-09-22 13:38:20524 net_log);
avicebda7922016-10-21 17:37:54525 downloads_[download_item->GetId()] = base::WrapUnique(download_item);
skyostil66bd67912016-08-12 12:33:11526 DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid()));
asankaeef62b02016-03-14 21:23:11527 downloads_by_guid_[download_item->GetGuid()] = download_item;
ericwilligers254597b2016-10-17 10:32:31528 for (auto& observer : observers_)
529 observer.OnDownloadCreated(this, download_item);
[email protected]530047e2013-07-12 17:02:25530 if (!item_created.is_null())
531 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12532}
533
[email protected]8d68a3e02013-01-12 15:57:10534// Resume a download of a specific URL. We send the request to the
535// ResourceDispatcherHost, and let it send us responses like a regular
536// download.
537void DownloadManagerImpl::ResumeInterruptedDownload(
dchengf6c40582016-04-09 00:05:13538 std::unique_ptr<content::DownloadUrlParameters> params,
avib7348942015-12-25 20:57:10539 uint32_t id) {
svaldez6901c902015-12-10 18:57:33540 BrowserThread::PostTaskAndReplyWithResult(
541 BrowserThread::IO, FROM_HERE,
asanka3b4be122016-05-11 04:27:57542 base::Bind(&BeginDownload, base::Passed(&params),
543 browser_context_->GetResourceContext(), id,
svaldez6901c902015-12-10 18:57:33544 weak_factory_.GetWeakPtr()),
Min Qin3cbdb8d2017-08-25 06:23:09545 base::Bind(&DownloadManagerImpl::AddUrlDownloadHandler,
svaldez6901c902015-12-10 18:57:33546 weak_factory_.GetWeakPtr()));
[email protected]8d68a3e02013-01-12 15:57:10547}
548
[email protected]53ac00e82012-10-18 20:59:20549void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
dchengf6c40582016-04-09 00:05:13550 std::unique_ptr<DownloadItemFactory> item_factory) {
dcheng36b6aec92015-12-26 06:16:36551 item_factory_ = std::move(item_factory);
[email protected]53ac00e82012-10-18 20:59:20552}
553
554void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
dchengf6c40582016-04-09 00:05:13555 std::unique_ptr<DownloadFileFactory> file_factory) {
dcheng36b6aec92015-12-26 06:16:36556 file_factory_ = std::move(file_factory);
[email protected]53ac00e82012-10-18 20:59:20557}
558
[email protected]35869622012-10-26 23:23:55559DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20560 return file_factory_.get();
561}
562
[email protected]db1d8f72012-07-13 19:23:16563void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28564 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40565 return;
566
asankaeef62b02016-03-14 21:23:11567 downloads_by_guid_.erase(download->GetGuid());
avicebda7922016-10-21 17:37:54568 downloads_.erase(download->GetId());
initial.commit09911bf2008-07-26 23:55:29569}
570
Min Qin3cbdb8d2017-08-25 06:23:09571void DownloadManagerImpl::AddUrlDownloadHandler(
572 std::unique_ptr<UrlDownloadHandler, BrowserThread::DeleteOnIOThread>
dchengf6c40582016-04-09 00:05:13573 downloader) {
svaldez6901c902015-12-10 18:57:33574 if (downloader)
Min Qin3cbdb8d2017-08-25 06:23:09575 url_download_handlers_.push_back(std::move(downloader));
svaldez6901c902015-12-10 18:57:33576}
577
anantac82dd5be2016-08-26 01:21:53578// static
579DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest(
580 std::unique_ptr<net::URLRequest> url_request,
581 const Referrer& referrer,
582 ResourceContext* resource_context,
583 bool is_content_initiated,
584 int render_process_id,
585 int render_view_route_id,
586 int render_frame_route_id,
587 bool do_not_prompt_for_login) {
588 if (ResourceDispatcherHostImpl::Get()->is_shutdown())
589 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
590
591 // The URLRequest needs to be initialized with the referrer and other
592 // information prior to issuing it.
593 ResourceDispatcherHostImpl::Get()->InitializeURLRequest(
594 url_request.get(), referrer,
595 true, // download.
596 render_process_id, render_view_route_id, render_frame_route_id,
megjabloncaf312f2017-01-12 18:47:49597 PREVIEWS_OFF, resource_context);
anantac82dd5be2016-08-26 01:21:53598
599 // We treat a download as a main frame load, and thus update the policy URL on
600 // redirects.
601 //
602 // TODO(davidben): Is this correct? If this came from a
603 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
604 // appropriately?
605 url_request->set_first_party_url_policy(
606 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
607
608 const GURL& url = url_request->original_url();
609
610 // Check if the renderer is permitted to request the requested URL.
611 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
612 render_process_id, url)) {
613 DVLOG(1) << "Denied unauthorized download request for "
614 << url.possibly_invalid_spec();
615 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
616 }
617
618 const net::URLRequestContext* request_context = url_request->context();
mmenke1918ae782017-04-03 20:10:20619 if (!request_context->job_factory()->IsHandledProtocol(url.scheme())) {
anantac82dd5be2016-08-26 01:21:53620 DVLOG(1) << "Download request for unsupported protocol: "
621 << url.possibly_invalid_spec();
622 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
623 }
624
625 // From this point forward, the |DownloadResourceHandler| is responsible for
626 // |started_callback|.
627 // TODO(ananta)
628 // Find a better way to create the DownloadResourceHandler instance.
629 std::unique_ptr<ResourceHandler> handler(
630 DownloadResourceHandler::Create(url_request.get()));
631
632 ResourceDispatcherHostImpl::Get()->BeginURLRequest(
633 std::move(url_request), std::move(handler), true, // download
634 is_content_initiated, do_not_prompt_for_login, resource_context);
635 return DOWNLOAD_INTERRUPT_REASON_NONE;
636}
637
asanka6381bd4e2017-01-05 22:08:37638int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
msramekd9c162a2016-02-23 11:17:21639 const base::Callback<bool(const GURL&)>& url_filter,
asanka6381bd4e2017-01-05 22:08:37640 base::Time remove_begin,
641 base::Time remove_end) {
[email protected]237af6d2013-03-15 23:45:28642 int count = 0;
avicebda7922016-10-21 17:37:54643 auto it = downloads_.begin();
[email protected]237af6d2013-03-15 23:45:28644 while (it != downloads_.end()) {
avicebda7922016-10-21 17:37:54645 DownloadItemImpl* download = it->second.get();
[email protected]237af6d2013-03-15 23:45:28646
647 // Increment done here to protect against invalidation below.
648 ++it;
649
ttr31481dc54b2015-08-06 20:11:26650 if (download->GetState() != DownloadItem::IN_PROGRESS &&
asanka6381bd4e2017-01-05 22:08:37651 url_filter.Run(download->GetURL()) &&
652 download->GetStartTime() >= remove_begin &&
653 (remove_end.is_null() || download->GetStartTime() < remove_end)) {
[email protected]237af6d2013-03-15 23:45:28654 download->Remove();
655 count++;
initial.commit09911bf2008-07-26 23:55:29656 }
initial.commit09911bf2008-07-26 23:55:29657 }
[email protected]237af6d2013-03-15 23:45:28658 return count;
initial.commit09911bf2008-07-26 23:55:29659}
660
[email protected]0d4e30c2012-01-28 00:47:53661void DownloadManagerImpl::DownloadUrl(
Ramin Halavati03efa732017-06-12 09:28:20662 std::unique_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14663 if (params->post_id() >= 0) {
664 // Check this here so that the traceback is more useful.
665 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43666 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14667 }
Min Qin3cbdb8d2017-08-25 06:23:09668
669 // TODO(qinmin): remove false from the if statement once download works when
670 // network service is enabled, or once we disable the tests that are currently
671 // passing with the URLRequest code path.
672 if (base::FeatureList::IsEnabled(features::kNetworkService) && false) {
673 std::unique_ptr<ResourceRequest> request = CreateResourceRequest(
674 params.get());
675 BrowserThread::PostTaskAndReplyWithResult(
676 BrowserThread::IO, FROM_HERE,
677 base::BindOnce(&BeginResourceDownload, std::move(params),
678 std::move(request),
679 GetURLLoaderFactoryGetter(
680 browser_context_, params->render_process_host_id(),
681 params->render_frame_host_routing_id()),
682 content::DownloadItem::kInvalidId,
683 weak_factory_.GetWeakPtr()),
684 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
685 weak_factory_.GetWeakPtr()));
686 } else {
687 BrowserThread::PostTaskAndReplyWithResult(
688 BrowserThread::IO, FROM_HERE,
689 base::BindOnce(&BeginDownload, std::move(params),
690 browser_context_->GetResourceContext(),
691 content::DownloadItem::kInvalidId,
692 weak_factory_.GetWeakPtr()),
693 base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler,
694 weak_factory_.GetWeakPtr()));
695 }
initial.commit09911bf2008-07-26 23:55:29696}
697
[email protected]5656f8a2011-11-17 16:12:58698void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29699 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29700}
701
[email protected]5656f8a2011-11-17 16:12:58702void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29703 observers_.RemoveObserver(observer);
704}
705
[email protected]3d95e542012-11-20 00:52:08706DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11707 const std::string& guid,
avib7348942015-12-25 20:57:10708 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34709 const base::FilePath& current_path,
710 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15711 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08712 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57713 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28714 const GURL& tab_url,
715 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45716 const std::string& mime_type,
717 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23718 base::Time start_time,
719 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21720 const std::string& etag,
721 const std::string& last_modified,
avib7348942015-12-25 20:57:10722 int64_t received_bytes,
723 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57724 const std::string& hash,
[email protected]3d95e542012-11-20 00:52:08725 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15726 DownloadDangerType danger_type,
727 DownloadInterruptReason interrupt_reason,
qinmin23b28572017-02-25 00:24:28728 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23729 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10730 bool transient,
qinmin23b28572017-02-25 00:24:28731 const std::vector<DownloadItem::ReceivedSlice>& received_slices) {
skyostil66bd67912016-08-12 12:33:11732 if (base::ContainsKey(downloads_, id)) {
[email protected]6065748f2013-10-29 01:07:43733 NOTREACHED();
avicebda7922016-10-21 17:37:54734 return nullptr;
[email protected]6065748f2013-10-29 01:07:43735 }
skyostil66bd67912016-08-12 12:33:11736 DCHECK(!base::ContainsKey(downloads_by_guid_, guid));
[email protected]3d95e542012-11-20 00:52:08737 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
asanka038d58d2016-04-14 00:29:28738 this, guid, id, current_path, target_path, url_chain, referrer_url,
asanka3b4be122016-05-11 04:27:57739 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type,
740 start_time, end_time, etag, last_modified, received_bytes, total_bytes,
shaktisahu60eb97f2017-03-03 08:53:23741 hash, state, danger_type, interrupt_reason, opened, last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10742 transient, received_slices,
tfarina42834112016-09-22 13:38:20743 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD));
avicebda7922016-10-21 17:37:54744 downloads_[id] = base::WrapUnique(item);
asankaeef62b02016-03-14 21:23:11745 downloads_by_guid_[guid] = item;
ericwilligers254597b2016-10-17 10:32:31746 for (auto& observer : observers_)
747 observer.OnDownloadCreated(this, item);
pkastingf5279482016-07-27 02:18:20748 DVLOG(20) << __func__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08749 return item;
initial.commit09911bf2008-07-26 23:55:29750}
751
xingliud64fde652017-05-24 07:04:33752void DownloadManagerImpl::PostInitialization() {
753 DCHECK(!initialized_);
754 initialized_ = true;
755 for (auto& observer : observers_)
756 observer.OnManagerInitialized();
757}
758
759bool DownloadManagerImpl::IsManagerInitialized() const {
760 return initialized_;
761}
762
[email protected]5656f8a2011-11-17 16:12:58763int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56764 int count = 0;
lukasza921fabfa2016-02-05 19:51:48765 for (const auto& it : downloads_) {
766 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56767 ++count;
768 }
769 return count;
[email protected]5656f8a2011-11-17 16:12:58770}
771
[email protected]422a7d12013-10-21 12:10:42772int DownloadManagerImpl::NonMaliciousInProgressCount() const {
773 int count = 0;
lukasza921fabfa2016-02-05 19:51:48774 for (const auto& it : downloads_) {
775 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
776 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
777 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
778 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
779 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59780 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42781 ++count;
782 }
783 }
784 return count;
785}
786
avib7348942015-12-25 20:57:10787DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
avicebda7922016-10-21 17:37:54788 return base::ContainsKey(downloads_, download_id)
789 ? downloads_[download_id].get()
790 : nullptr;
asankaeef62b02016-03-14 21:23:11791}
792
793DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
skyostil66bd67912016-08-12 12:33:11794 return base::ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
795 : nullptr;
[email protected]ba7895d2012-06-22 19:02:52796}
797
Min Qin3cbdb8d2017-08-25 06:23:09798void DownloadManagerImpl::OnUrlDownloadStarted(
xingliu468824d2017-02-28 02:59:25799 std::unique_ptr<DownloadCreateInfo> download_create_info,
Min Qin3cbdb8d2017-08-25 06:23:09800 std::unique_ptr<UrlDownloadHandler::InputStream> input_stream,
xingliu468824d2017-02-28 02:59:25801 const DownloadUrlParameters::OnStartedCallback& callback) {
Min Qin3cbdb8d2017-08-25 06:23:09802 if (!base::FeatureList::IsEnabled(features::kNetworkService)) {
803 StartDownload(std::move(download_create_info),
804 std::move(input_stream->stream_reader_), callback);
805 }
xingliu468824d2017-02-28 02:59:25806}
807
Min Qin3cbdb8d2017-08-25 06:23:09808void DownloadManagerImpl::OnUrlDownloadStopped(UrlDownloadHandler* downloader) {
809 for (auto ptr = url_download_handlers_.begin();
810 ptr != url_download_handlers_.end(); ++ptr) {
xingliu468824d2017-02-28 02:59:25811 if (ptr->get() == downloader) {
Min Qin3cbdb8d2017-08-25 06:23:09812 url_download_handlers_.erase(ptr);
xingliu468824d2017-02-28 02:59:25813 return;
814 }
815 }
816}
817
[email protected]9f1f4092012-09-10 21:18:04818void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48819 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54820 downloads->push_back(it.second.get());
[email protected]9f1f4092012-09-10 21:18:04821 }
822}
823
[email protected]ffc0c2d2013-01-21 15:32:12824void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06825 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48826 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54827 DownloadItemImpl* item = it.second.get();
[email protected]803036e02013-06-15 17:08:47828 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40829 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06830 ++num_unopened;
831 }
[email protected]35869622012-10-26 23:23:55832 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12833
[email protected]0baf5922013-01-30 13:55:18834 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12835 delegate_->OpenDownload(download);
836}
837
838void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
839 if (delegate_)
840 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06841}
[email protected]5656f8a2011-11-17 16:12:58842
[email protected]35869622012-10-26 23:23:55843} // namespace content