blob: c2c89e2fcfc495d069d5f117547dff296a975f8e [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"
[email protected]678c0362012-12-05 08:02:4433#include "content/browser/loader/resource_dispatcher_host_impl.h"
anantac82dd5be2016-08-26 01:21:5334#include "content/browser/loader/resource_request_info_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3235#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2936#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1137#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4438#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3439#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1740#include "content/public/browser/download_manager_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1441#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0742#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1643#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3044#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1345#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2746#include "content/public/browser/web_contents_delegate.h"
megjabloncaf312f2017-01-12 18:47:4947#include "content/public/common/previews_state.h"
[email protected]8d68a3e02013-01-12 15:57:1048#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0749#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1450#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1951#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2852#include "net/base/upload_bytes_element_reader.h"
mikecirone8b85c432016-09-08 19:11:0053#include "net/log/net_log_source_type.h"
mikecironef22f9812016-10-04 03:40:1954#include "net/log/net_log_with_source.h"
[email protected]eb795632012-09-01 00:19:3155#include "net/url_request/url_request_context.h"
asanka6150c522016-03-25 21:40:5756#include "storage/browser/blob/blob_url_request_job_factory.h"
ttr31481dc54b2015-08-06 20:11:2657#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2958
Tim Brown07e062e82017-06-26 19:38:0359#if defined(USE_X11) && !defined(OS_CHROMEOS)
thomasanderson1799a1222017-06-14 00:02:0260#include "base/nix/xdg_util.h"
61#endif
62
[email protected]35869622012-10-26 23:23:5563namespace content {
[email protected]a0ce3282011-08-19 20:49:5264namespace {
65
dchengf6c40582016-04-09 00:05:1366std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload(
67 std::unique_ptr<DownloadUrlParameters> params,
asanka3b4be122016-05-11 04:27:5768 content::ResourceContext* resource_context,
avib7348942015-12-25 20:57:1069 uint32_t download_id,
svaldez6901c902015-12-10 18:57:3370 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:5271 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:1072
dchengf6c40582016-04-09 00:05:1373 std::unique_ptr<net::URLRequest> url_request =
Ramin Halavati03efa732017-06-12 09:28:2074 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
dchengf6c40582016-04-09 00:05:1375 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
asanka6150c522016-03-25 21:40:5776 params->GetBlobDataHandle();
77 if (blob_data_handle) {
78 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
79 url_request.get(), std::move(blob_data_handle));
80 }
[email protected]fc179dd12013-03-16 09:35:1981
asanka00b621f2016-02-19 18:09:2382 // If there's a valid renderer process associated with the request, then the
83 // request should be driven by the ResourceLoader. Pass it over to the
84 // ResourceDispatcherHostImpl which will in turn pass it along to the
85 // ResourceLoader.
asanka3b4be122016-05-11 04:27:5786 if (params->render_process_host_id() >= 0) {
anantac82dd5be2016-08-26 01:21:5387 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest(
88 std::move(url_request), params->referrer(), resource_context,
89 params->content_initiated(), params->render_process_host_id(),
90 params->render_view_host_routing_id(),
91 params->render_frame_host_routing_id(),
92 params->do_not_prompt_for_login());
asanka00b621f2016-02-19 18:09:2393
94 // If the download was accepted, the DownloadResourceHandler is now
95 // responsible for driving the request to completion.
96 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
97 return nullptr;
98
99 // Otherwise, create an interrupted download.
dchengf6c40582016-04-09 00:05:13100 std::unique_ptr<DownloadCreateInfo> failed_created_info(
tfarina42834112016-09-22 13:38:20101 new DownloadCreateInfo(base::Time::Now(), net::NetLogWithSource(),
dchengf6c40582016-04-09 00:05:13102 base::WrapUnique(new DownloadSaveInfo)));
asanka00b621f2016-02-19 18:09:23103 failed_created_info->url_chain.push_back(params->url());
104 failed_created_info->result = reason;
dchengf6c40582016-04-09 00:05:13105 std::unique_ptr<ByteStreamReader> empty_byte_stream;
asanka00b621f2016-02-19 18:09:23106 BrowserThread::PostTask(
107 BrowserThread::UI, FROM_HERE,
108 base::Bind(&DownloadManager::StartDownload, download_manager,
109 base::Passed(&failed_created_info),
110 base::Passed(&empty_byte_stream), params->callback()));
svaldez6901c902015-12-10 18:57:33111 return nullptr;
112 }
asanka00b621f2016-02-19 18:09:23113
dchengf6c40582016-04-09 00:05:13114 return std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>(
asanka00b621f2016-02-19 18:09:23115 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
qinmin462abbb2017-05-03 21:55:42116 params->referrer(), false)
svaldez6901c902015-12-10 18:57:33117 .release());
[email protected]a0ce3282011-08-19 20:49:52118}
119
[email protected]35869622012-10-26 23:23:55120class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03121 public:
[email protected]b3756012013-03-06 17:43:02122 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58123 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03124
dchengc2282aa2014-10-21 12:07:58125 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02126 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11127 const std::string& guid,
avib7348942015-12-25 20:57:10128 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02129 const base::FilePath& current_path,
130 const base::FilePath& target_path,
131 const std::vector<GURL>& url_chain,
132 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57133 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28134 const GURL& tab_url,
135 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45136 const std::string& mime_type,
137 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23138 base::Time start_time,
139 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21140 const std::string& etag,
141 const std::string& last_modified,
avib7348942015-12-25 20:57:10142 int64_t received_bytes,
143 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57144 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02145 DownloadItem::DownloadState state,
146 DownloadDangerType danger_type,
147 DownloadInterruptReason interrupt_reason,
148 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23149 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10150 bool transient,
qinmin23b28572017-02-25 00:24:28151 const std::vector<DownloadItem::ReceivedSlice>& received_slices,
tfarina42834112016-09-22 13:38:20152 const net::NetLogWithSource& net_log) override {
asanka038d58d2016-04-14 00:29:28153 return new DownloadItemImpl(
154 delegate, guid, download_id, current_path, target_path, url_chain,
asanka3b4be122016-05-11 04:27:57155 referrer_url, site_url, tab_url, tab_refererr_url, mime_type,
156 original_mime_type, start_time, end_time, etag, last_modified,
157 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason,
shaktisahufd49a3e2017-03-30 18:35:10158 opened, last_access_time, transient, received_slices, net_log);
[email protected]b3756012013-03-06 17:43:02159 }
[email protected]d25fda12012-06-12 17:05:03160
dchengc2282aa2014-10-21 12:07:58161 DownloadItemImpl* CreateActiveItem(
[email protected]b3756012013-03-06 17:43:02162 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10163 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02164 const DownloadCreateInfo& info,
tfarina42834112016-09-22 13:38:20165 const net::NetLogWithSource& net_log) override {
166 return new DownloadItemImpl(delegate, download_id, info, net_log);
[email protected]b3756012013-03-06 17:43:02167 }
[email protected]d25fda12012-06-12 17:05:03168
dchengc2282aa2014-10-21 12:07:58169 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02170 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10171 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02172 const base::FilePath& path,
173 const GURL& url,
[email protected]b3756012013-03-06 17:43:02174 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13175 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
tfarina42834112016-09-22 13:38:20176 const net::NetLogWithSource& net_log) override {
dcheng36b6aec92015-12-26 06:16:36177 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
tfarina42834112016-09-22 13:38:20178 std::move(request_handle), net_log);
[email protected]b3756012013-03-06 17:43:02179 }
[email protected]d25fda12012-06-12 17:05:03180};
181
Tim Brown07e062e82017-06-26 19:38:03182#if defined(USE_X11) && !defined(OS_CHROMEOS)
thomasanderson1799a1222017-06-14 00:02:02183base::FilePath GetTemporaryDownloadDirectory() {
184 std::unique_ptr<base::Environment> env(base::Environment::Create());
185 return base::nix::GetXDGDirectory(env.get(), "XDG_DATA_HOME", ".local/share");
186}
187#endif
188
[email protected]a0ce3282011-08-19 20:49:52189} // namespace
190
avicebda7922016-10-21 17:37:54191DownloadManagerImpl::DownloadManagerImpl(net::NetLog* net_log,
192 BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20193 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55194 file_factory_(new DownloadFileFactory()),
[email protected]16798692013-04-23 18:08:38195 shutdown_needed_(true),
xingliud64fde652017-05-24 07:04:33196 initialized_(false),
[email protected]16798692013-04-23 18:08:38197 browser_context_(browser_context),
avicebda7922016-10-21 17:37:54198 delegate_(nullptr),
[email protected]eba4a4d2013-05-29 02:18:06199 net_log_(net_log),
200 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38201 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29202}
203
[email protected]5656f8a2011-11-17 16:12:58204DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13205 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29206}
207
[email protected]89ec81f2013-05-15 19:20:02208DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10209 uint32_t id,
210 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52211 DCHECK_CURRENTLY_ON(BrowserThread::UI);
skyostil66bd67912016-08-12 12:33:11212 DCHECK(!base::ContainsKey(downloads_, id));
tfarina42834112016-09-22 13:38:20213 net::NetLogWithSource net_log =
214 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD);
[email protected]89ec81f2013-05-15 19:20:02215 DownloadItemImpl* download =
tfarina42834112016-09-22 13:38:20216 item_factory_->CreateActiveItem(this, id, info, net_log);
avicebda7922016-10-21 17:37:54217 downloads_[id] = base::WrapUnique(download);
asankaeef62b02016-03-14 21:23:11218 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02219 return download;
[email protected]381af492013-03-28 01:56:22220}
221
[email protected]530047e2013-07-12 17:02:25222void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52223 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25224 if (delegate_) {
225 delegate_->GetNextId(callback);
226 return;
[email protected]491aaa62012-06-07 03:50:18227 }
avib7348942015-12-25 20:57:10228 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25229 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53230}
231
[email protected]53ac00e82012-10-18 20:59:20232void DownloadManagerImpl::DetermineDownloadTarget(
233 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
234 // Note that this next call relies on
235 // DownloadItemImplDelegate::DownloadTargetCallback and
236 // DownloadManagerDelegate::DownloadTargetCallback having the same
237 // type. If the types ever diverge, gasket code will need to
238 // be written here.
239 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34240 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20241 // TODO(asanka): Determine a useful path if |target_path| is empty.
Asanka Herath1ba0e9f2017-04-03 18:48:53242 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
243 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path,
244 DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]53ac00e82012-10-18 20:59:20245 }
[email protected]47665442012-07-27 02:31:22246}
247
[email protected]98299622013-01-03 22:26:50248bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43249 DownloadItemImpl* item, const base::Closure& complete_callback) {
250 if (!delegate_ ||
251 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50252 return true;
[email protected]18710a42a2012-10-17 19:50:43253 }
254 // Otherwise, the delegate has accepted responsibility to run the
255 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50256 return false;
[email protected]fc03de22011-12-06 23:28:12257}
258
[email protected]2dec8ec2013-02-07 19:20:34259bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
260 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18261 if (!delegate_)
262 return false;
263
[email protected]fc03de22011-12-06 23:28:12264 return delegate_->ShouldOpenFileBasedOnExtension(path);
265}
266
[email protected]bde0e4f2012-11-08 22:49:59267bool DownloadManagerImpl::ShouldOpenDownload(
268 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43269 if (!delegate_)
270 return true;
271
[email protected]bde0e4f2012-11-08 22:49:59272 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
273 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
274 // to have the same type :-}.
275 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43276}
277
[email protected]35869622012-10-26 23:23:55278void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57279 delegate_ = delegate;
280}
281
[email protected]35869622012-10-26 23:23:55282DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28283 return delegate_;
284}
285
[email protected]5656f8a2011-11-17 16:12:58286void DownloadManagerImpl::Shutdown() {
pkastingf5279482016-07-27 02:18:20287 DVLOG(20) << __func__ << "() shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13288 if (!shutdown_needed_)
289 return;
290 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29291
ericwilligers254597b2016-10-17 10:32:31292 for (auto& observer : observers_)
293 observer.ManagerGoingDown(this);
[email protected]fb4f8d902011-09-16 06:07:08294 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13295
[email protected]fe752272013-05-29 18:57:45296 // If there are in-progress downloads, cancel them. This also goes for
297 // dangerous downloads which will remain in history if they aren't explicitly
298 // accepted or discarded. Canceling will remove the intermediate download
299 // file.
lukasza921fabfa2016-02-05 19:51:48300 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54301 DownloadItemImpl* download = it.second.get();
[email protected]fe752272013-05-29 18:57:45302 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17303 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29304 }
avicebda7922016-10-21 17:37:54305 downloads_.clear();
asankaeef62b02016-03-14 21:23:11306 downloads_by_guid_.clear();
svaldez9a07ab332016-01-12 18:29:01307 url_downloaders_.clear();
initial.commit09911bf2008-07-26 23:55:29308
[email protected]41f558fb2012-01-09 22:59:58309 // We'll have nothing more to report to the observers after this point.
310 observers_.Clear();
311
[email protected]b441a8492012-06-06 14:55:57312 if (delegate_)
313 delegate_->Shutdown();
avicebda7922016-10-21 17:37:54314 delegate_ = nullptr;
initial.commit09911bf2008-07-26 23:55:29315}
316
[email protected]530047e2013-07-12 17:02:25317void DownloadManagerImpl::StartDownload(
dchengf6c40582016-04-09 00:05:13318 std::unique_ptr<DownloadCreateInfo> info,
319 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25320 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52321 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25322 DCHECK(info);
asanka00b621f2016-02-19 18:09:23323 // |stream| is only non-nil if the download request was successful.
324 DCHECK((info->result == DOWNLOAD_INTERRUPT_REASON_NONE && stream.get()) ||
325 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && !stream.get()));
pkastingf5279482016-07-27 02:18:20326 DVLOG(20) << __func__
327 << "() result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10328 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25329 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
jialiul82b8e502016-09-30 17:42:42330 if (new_download)
331 RecordDownloadConnectionSecurity(info->url(), info->url_chain);
avib7348942015-12-25 20:57:10332 base::Callback<void(uint32_t)> got_id(base::Bind(
333 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
334 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02335 if (new_download) {
[email protected]530047e2013-07-12 17:02:25336 GetNextId(got_id);
337 } else {
338 got_id.Run(download_id);
339 }
340}
341
342void DownloadManagerImpl::StartDownloadWithId(
dchengf6c40582016-04-09 00:05:13343 std::unique_ptr<DownloadCreateInfo> info,
344 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25345 const DownloadUrlParameters::OnStartedCallback& on_started,
346 bool new_download,
avib7348942015-12-25 20:57:10347 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52348 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25349 DCHECK_NE(content::DownloadItem::kInvalidId, id);
avicebda7922016-10-21 17:37:54350 DownloadItemImpl* download = nullptr;
[email protected]530047e2013-07-12 17:02:25351 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02352 download = CreateActiveItem(id, *info);
353 } else {
avicebda7922016-10-21 17:37:54354 auto item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02355 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37356 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47357 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02358 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37359 // removed after it was resumed. Ignore. If the download is cancelled
360 // while resuming, then also ignore the request.
Min Qin5b912dc42017-06-07 23:10:18361 info->request_handle->CancelRequest(true);
[email protected]530047e2013-07-12 17:02:25362 if (!on_started.is_null())
avicebda7922016-10-21 17:37:54363 on_started.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
siggi2b05ad102017-07-21 18:57:56364 // The ByteStreamReader lives and dies on the download sequence.
asanka00b621f2016-02-19 18:09:23365 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
siggi2b05ad102017-07-21 18:57:56366 GetDownloadTaskRunner()->DeleteSoon(FROM_HERE, stream.release());
[email protected]530047e2013-07-12 17:02:25367 return;
[email protected]89ec81f2013-05-15 19:20:02368 }
avicebda7922016-10-21 17:37:54369 download = item_iterator->second.get();
[email protected]89ec81f2013-05-15 19:20:02370 }
371
[email protected]2dec8ec2013-02-07 19:20:34372 base::FilePath default_download_directory;
Tim Brown07e062e82017-06-26 19:38:03373#if defined(USE_X11) && !defined(OS_CHROMEOS)
thomasanderson1799a1222017-06-14 00:02:02374 // TODO(thomasanderson): Remove this when all Linux distros with
375 // versions of GTK lower than 3.14.7 are no longer supported. This
376 // should happen when support for Ubuntu Trusty and Debian Jessie
377 // are removed.
378 default_download_directory = GetTemporaryDownloadDirectory();
379#else
[email protected]96bfed052012-09-15 22:21:01380 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34381 base::FilePath website_save_directory; // Unused
382 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01383 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20384 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01385 }
thomasanderson1799a1222017-06-14 00:02:02386#endif
[email protected]96bfed052012-09-15 22:21:01387
dchengf6c40582016-04-09 00:05:13388 std::unique_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48389
asanka00b621f2016-02-19 18:09:23390 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
391 DCHECK(stream.get());
tfarina42834112016-09-22 13:38:20392 download_file.reset(file_factory_->CreateFile(
393 std::move(info->save_info), default_download_directory,
xingliu583a61a2017-04-10 22:12:37394 std::move(stream), download->GetNetLogWithSource(),
tfarina42834112016-09-22 13:38:20395 download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48396 }
asanka4350f6a2016-03-15 02:40:57397 // It is important to leave info->save_info intact in the case of an interrupt
398 // so that the DownloadItem can salvage what it can out of a failed resumption
399 // attempt.
[email protected]492c0092013-08-10 13:49:48400
asanka00b621f2016-02-19 18:09:23401 download->Start(std::move(download_file), std::move(info->request_handle),
402 *info);
[email protected]96c3bdbaa2012-08-31 16:39:54403
[email protected]381af492013-03-28 01:56:22404 // For interrupted downloads, Start() will transition the state to
405 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
406 // For new downloads, we notify here, rather than earlier, so that
407 // the download_file is bound to download and all the usual
408 // setters (e.g. Cancel) work.
ericwilligers254597b2016-10-17 10:32:31409 if (new_download) {
410 for (auto& observer : observers_)
411 observer.OnDownloadCreated(this, download);
412 }
[email protected]96c3bdbaa2012-08-31 16:39:54413
[email protected]530047e2013-07-12 17:02:25414 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56415 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35416}
417
[email protected]5656f8a2011-11-17 16:12:58418void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52419 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lukasza921fabfa2016-02-05 19:51:48420 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54421 DownloadItemImpl* item = it.second.get();
[email protected]3d95e542012-11-20 00:52:08422 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48423 }
424}
425
[email protected]db1d8f72012-07-13 19:23:16426void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52427 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47428 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16429 !download_item->GetFileExternallyRemoved() &&
430 delegate_) {
431 delegate_->CheckForFileExistence(
432 download_item,
433 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
[email protected]eba4a4d2013-05-29 02:18:06434 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48435 }
436}
437
avib7348942015-12-25 20:57:10438void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id,
[email protected]4c544cd2013-01-25 07:54:16439 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52440 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16441 if (!result) { // File does not exist.
skyostil66bd67912016-08-12 12:33:11442 if (base::ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51443 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16444 }
[email protected]9fc114672011-06-15 08:17:48445}
446
asankadb9e50ad2016-10-03 15:00:29447std::string DownloadManagerImpl::GetApplicationClientIdForFileScanning() const {
448 if (delegate_)
449 return delegate_->ApplicationClientIdForFileScanning();
450 return std::string();
451}
452
[email protected]35869622012-10-26 23:23:55453BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58454 return browser_context_;
455}
456
[email protected]530047e2013-07-12 17:02:25457void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34458 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12459 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27460 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13461 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25462 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52463 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25464 GetNextId(base::Bind(
465 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
dcheng36b6aec92015-12-26 06:16:36466 weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type,
467 base::Passed(std::move(request_handle)), item_created));
[email protected]530047e2013-07-12 17:02:25468}
469
470void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
471 const base::FilePath& main_file_path,
472 const GURL& page_url,
473 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13474 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25475 const DownloadItemImplCreated& item_created,
avib7348942015-12-25 20:57:10476 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52477 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25478 DCHECK_NE(content::DownloadItem::kInvalidId, id);
skyostil66bd67912016-08-12 12:33:11479 DCHECK(!base::ContainsKey(downloads_, id));
tfarina42834112016-09-22 13:38:20480 net::NetLogWithSource net_log =
481 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08482 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
dcheng36b6aec92015-12-26 06:16:36483 this, id, main_file_path, page_url, mime_type, std::move(request_handle),
tfarina42834112016-09-22 13:38:20484 net_log);
avicebda7922016-10-21 17:37:54485 downloads_[download_item->GetId()] = base::WrapUnique(download_item);
skyostil66bd67912016-08-12 12:33:11486 DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid()));
asankaeef62b02016-03-14 21:23:11487 downloads_by_guid_[download_item->GetGuid()] = download_item;
ericwilligers254597b2016-10-17 10:32:31488 for (auto& observer : observers_)
489 observer.OnDownloadCreated(this, download_item);
[email protected]530047e2013-07-12 17:02:25490 if (!item_created.is_null())
491 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12492}
493
[email protected]8d68a3e02013-01-12 15:57:10494// Resume a download of a specific URL. We send the request to the
495// ResourceDispatcherHost, and let it send us responses like a regular
496// download.
497void DownloadManagerImpl::ResumeInterruptedDownload(
dchengf6c40582016-04-09 00:05:13498 std::unique_ptr<content::DownloadUrlParameters> params,
avib7348942015-12-25 20:57:10499 uint32_t id) {
svaldez6901c902015-12-10 18:57:33500 BrowserThread::PostTaskAndReplyWithResult(
501 BrowserThread::IO, FROM_HERE,
asanka3b4be122016-05-11 04:27:57502 base::Bind(&BeginDownload, base::Passed(&params),
503 browser_context_->GetResourceContext(), id,
svaldez6901c902015-12-10 18:57:33504 weak_factory_.GetWeakPtr()),
505 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
506 weak_factory_.GetWeakPtr()));
[email protected]8d68a3e02013-01-12 15:57:10507}
508
[email protected]53ac00e82012-10-18 20:59:20509void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
dchengf6c40582016-04-09 00:05:13510 std::unique_ptr<DownloadItemFactory> item_factory) {
dcheng36b6aec92015-12-26 06:16:36511 item_factory_ = std::move(item_factory);
[email protected]53ac00e82012-10-18 20:59:20512}
513
514void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
dchengf6c40582016-04-09 00:05:13515 std::unique_ptr<DownloadFileFactory> file_factory) {
dcheng36b6aec92015-12-26 06:16:36516 file_factory_ = std::move(file_factory);
[email protected]53ac00e82012-10-18 20:59:20517}
518
[email protected]35869622012-10-26 23:23:55519DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20520 return file_factory_.get();
521}
522
[email protected]db1d8f72012-07-13 19:23:16523void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28524 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40525 return;
526
asankaeef62b02016-03-14 21:23:11527 downloads_by_guid_.erase(download->GetGuid());
avicebda7922016-10-21 17:37:54528 downloads_.erase(download->GetId());
initial.commit09911bf2008-07-26 23:55:29529}
530
svaldez6901c902015-12-10 18:57:33531void DownloadManagerImpl::AddUrlDownloader(
dchengf6c40582016-04-09 00:05:13532 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>
533 downloader) {
svaldez6901c902015-12-10 18:57:33534 if (downloader)
dcheng36b6aec92015-12-26 06:16:36535 url_downloaders_.push_back(std::move(downloader));
svaldez6901c902015-12-10 18:57:33536}
537
anantac82dd5be2016-08-26 01:21:53538// static
539DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest(
540 std::unique_ptr<net::URLRequest> url_request,
541 const Referrer& referrer,
542 ResourceContext* resource_context,
543 bool is_content_initiated,
544 int render_process_id,
545 int render_view_route_id,
546 int render_frame_route_id,
547 bool do_not_prompt_for_login) {
548 if (ResourceDispatcherHostImpl::Get()->is_shutdown())
549 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
550
551 // The URLRequest needs to be initialized with the referrer and other
552 // information prior to issuing it.
553 ResourceDispatcherHostImpl::Get()->InitializeURLRequest(
554 url_request.get(), referrer,
555 true, // download.
556 render_process_id, render_view_route_id, render_frame_route_id,
megjabloncaf312f2017-01-12 18:47:49557 PREVIEWS_OFF, resource_context);
anantac82dd5be2016-08-26 01:21:53558
559 // We treat a download as a main frame load, and thus update the policy URL on
560 // redirects.
561 //
562 // TODO(davidben): Is this correct? If this came from a
563 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
564 // appropriately?
565 url_request->set_first_party_url_policy(
566 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
567
568 const GURL& url = url_request->original_url();
569
570 // Check if the renderer is permitted to request the requested URL.
571 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
572 render_process_id, url)) {
573 DVLOG(1) << "Denied unauthorized download request for "
574 << url.possibly_invalid_spec();
575 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
576 }
577
578 const net::URLRequestContext* request_context = url_request->context();
mmenke1918ae782017-04-03 20:10:20579 if (!request_context->job_factory()->IsHandledProtocol(url.scheme())) {
anantac82dd5be2016-08-26 01:21:53580 DVLOG(1) << "Download request for unsupported protocol: "
581 << url.possibly_invalid_spec();
582 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
583 }
584
585 // From this point forward, the |DownloadResourceHandler| is responsible for
586 // |started_callback|.
587 // TODO(ananta)
588 // Find a better way to create the DownloadResourceHandler instance.
589 std::unique_ptr<ResourceHandler> handler(
590 DownloadResourceHandler::Create(url_request.get()));
591
592 ResourceDispatcherHostImpl::Get()->BeginURLRequest(
593 std::move(url_request), std::move(handler), true, // download
594 is_content_initiated, do_not_prompt_for_login, resource_context);
595 return DOWNLOAD_INTERRUPT_REASON_NONE;
596}
597
asanka6381bd4e2017-01-05 22:08:37598int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
msramekd9c162a2016-02-23 11:17:21599 const base::Callback<bool(const GURL&)>& url_filter,
asanka6381bd4e2017-01-05 22:08:37600 base::Time remove_begin,
601 base::Time remove_end) {
[email protected]237af6d2013-03-15 23:45:28602 int count = 0;
avicebda7922016-10-21 17:37:54603 auto it = downloads_.begin();
[email protected]237af6d2013-03-15 23:45:28604 while (it != downloads_.end()) {
avicebda7922016-10-21 17:37:54605 DownloadItemImpl* download = it->second.get();
[email protected]237af6d2013-03-15 23:45:28606
607 // Increment done here to protect against invalidation below.
608 ++it;
609
ttr31481dc54b2015-08-06 20:11:26610 if (download->GetState() != DownloadItem::IN_PROGRESS &&
asanka6381bd4e2017-01-05 22:08:37611 url_filter.Run(download->GetURL()) &&
612 download->GetStartTime() >= remove_begin &&
613 (remove_end.is_null() || download->GetStartTime() < remove_end)) {
[email protected]237af6d2013-03-15 23:45:28614 download->Remove();
615 count++;
initial.commit09911bf2008-07-26 23:55:29616 }
initial.commit09911bf2008-07-26 23:55:29617 }
[email protected]237af6d2013-03-15 23:45:28618 return count;
initial.commit09911bf2008-07-26 23:55:29619}
620
[email protected]0d4e30c2012-01-28 00:47:53621void DownloadManagerImpl::DownloadUrl(
Ramin Halavati03efa732017-06-12 09:28:20622 std::unique_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14623 if (params->post_id() >= 0) {
624 // Check this here so that the traceback is more useful.
625 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43626 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14627 }
svaldez6901c902015-12-10 18:57:33628 BrowserThread::PostTaskAndReplyWithResult(
629 BrowserThread::IO, FROM_HERE,
630 base::Bind(&BeginDownload, base::Passed(&params),
asanka3b4be122016-05-11 04:27:57631 browser_context_->GetResourceContext(),
svaldez6901c902015-12-10 18:57:33632 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()),
633 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
634 weak_factory_.GetWeakPtr()));
initial.commit09911bf2008-07-26 23:55:29635}
636
[email protected]5656f8a2011-11-17 16:12:58637void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29638 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29639}
640
[email protected]5656f8a2011-11-17 16:12:58641void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29642 observers_.RemoveObserver(observer);
643}
644
[email protected]3d95e542012-11-20 00:52:08645DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11646 const std::string& guid,
avib7348942015-12-25 20:57:10647 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34648 const base::FilePath& current_path,
649 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15650 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08651 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57652 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28653 const GURL& tab_url,
654 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45655 const std::string& mime_type,
656 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23657 base::Time start_time,
658 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21659 const std::string& etag,
660 const std::string& last_modified,
avib7348942015-12-25 20:57:10661 int64_t received_bytes,
662 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57663 const std::string& hash,
[email protected]3d95e542012-11-20 00:52:08664 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15665 DownloadDangerType danger_type,
666 DownloadInterruptReason interrupt_reason,
qinmin23b28572017-02-25 00:24:28667 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23668 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10669 bool transient,
qinmin23b28572017-02-25 00:24:28670 const std::vector<DownloadItem::ReceivedSlice>& received_slices) {
skyostil66bd67912016-08-12 12:33:11671 if (base::ContainsKey(downloads_, id)) {
[email protected]6065748f2013-10-29 01:07:43672 NOTREACHED();
avicebda7922016-10-21 17:37:54673 return nullptr;
[email protected]6065748f2013-10-29 01:07:43674 }
skyostil66bd67912016-08-12 12:33:11675 DCHECK(!base::ContainsKey(downloads_by_guid_, guid));
[email protected]3d95e542012-11-20 00:52:08676 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
asanka038d58d2016-04-14 00:29:28677 this, guid, id, current_path, target_path, url_chain, referrer_url,
asanka3b4be122016-05-11 04:27:57678 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type,
679 start_time, end_time, etag, last_modified, received_bytes, total_bytes,
shaktisahu60eb97f2017-03-03 08:53:23680 hash, state, danger_type, interrupt_reason, opened, last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10681 transient, received_slices,
tfarina42834112016-09-22 13:38:20682 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD));
avicebda7922016-10-21 17:37:54683 downloads_[id] = base::WrapUnique(item);
asankaeef62b02016-03-14 21:23:11684 downloads_by_guid_[guid] = item;
ericwilligers254597b2016-10-17 10:32:31685 for (auto& observer : observers_)
686 observer.OnDownloadCreated(this, item);
pkastingf5279482016-07-27 02:18:20687 DVLOG(20) << __func__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08688 return item;
initial.commit09911bf2008-07-26 23:55:29689}
690
xingliud64fde652017-05-24 07:04:33691void DownloadManagerImpl::PostInitialization() {
692 DCHECK(!initialized_);
693 initialized_ = true;
694 for (auto& observer : observers_)
695 observer.OnManagerInitialized();
696}
697
698bool DownloadManagerImpl::IsManagerInitialized() const {
699 return initialized_;
700}
701
[email protected]5656f8a2011-11-17 16:12:58702int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56703 int count = 0;
lukasza921fabfa2016-02-05 19:51:48704 for (const auto& it : downloads_) {
705 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56706 ++count;
707 }
708 return count;
[email protected]5656f8a2011-11-17 16:12:58709}
710
[email protected]422a7d12013-10-21 12:10:42711int DownloadManagerImpl::NonMaliciousInProgressCount() const {
712 int count = 0;
lukasza921fabfa2016-02-05 19:51:48713 for (const auto& it : downloads_) {
714 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
715 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
716 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
717 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
718 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59719 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42720 ++count;
721 }
722 }
723 return count;
724}
725
avib7348942015-12-25 20:57:10726DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
avicebda7922016-10-21 17:37:54727 return base::ContainsKey(downloads_, download_id)
728 ? downloads_[download_id].get()
729 : nullptr;
asankaeef62b02016-03-14 21:23:11730}
731
732DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
733 DCHECK(guid == base::ToUpperASCII(guid));
skyostil66bd67912016-08-12 12:33:11734 return base::ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
735 : nullptr;
[email protected]ba7895d2012-06-22 19:02:52736}
737
xingliu468824d2017-02-28 02:59:25738void DownloadManagerImpl::OnUrlDownloaderStarted(
739 std::unique_ptr<DownloadCreateInfo> download_create_info,
740 std::unique_ptr<ByteStreamReader> stream_reader,
741 const DownloadUrlParameters::OnStartedCallback& callback) {
742 StartDownload(std::move(download_create_info), std::move(stream_reader),
743 callback);
744}
745
746void DownloadManagerImpl::OnUrlDownloaderStopped(UrlDownloader* downloader) {
747 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end();
748 ++ptr) {
749 if (ptr->get() == downloader) {
750 url_downloaders_.erase(ptr);
751 return;
752 }
753 }
754}
755
[email protected]9f1f4092012-09-10 21:18:04756void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48757 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54758 downloads->push_back(it.second.get());
[email protected]9f1f4092012-09-10 21:18:04759 }
760}
761
[email protected]ffc0c2d2013-01-21 15:32:12762void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06763 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48764 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54765 DownloadItemImpl* item = it.second.get();
[email protected]803036e02013-06-15 17:08:47766 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40767 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06768 ++num_unopened;
769 }
[email protected]35869622012-10-26 23:23:55770 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12771
[email protected]0baf5922013-01-30 13:55:18772 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12773 delegate_->OpenDownload(download);
774}
775
776void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
777 if (delegate_)
778 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06779}
[email protected]5656f8a2011-11-17 16:12:58780
[email protected]35869622012-10-26 23:23:55781} // namespace content