blob: ff6459014be447783ec6f60edf40a6f0ffa09385 [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"
[email protected]678c0362012-12-05 08:02:4432#include "content/browser/loader/resource_dispatcher_host_impl.h"
anantac82dd5be2016-08-26 01:21:5333#include "content/browser/loader/resource_request_info_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3234#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2935#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1136#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4437#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3438#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1739#include "content/public/browser/download_manager_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1440#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0741#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1642#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3043#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1344#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2745#include "content/public/browser/web_contents_delegate.h"
megjabloncaf312f2017-01-12 18:47:4946#include "content/public/common/previews_state.h"
[email protected]8d68a3e02013-01-12 15:57:1047#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0748#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1449#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1950#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2851#include "net/base/upload_bytes_element_reader.h"
mikecirone8b85c432016-09-08 19:11:0052#include "net/log/net_log_source_type.h"
mikecironef22f9812016-10-04 03:40:1953#include "net/log/net_log_with_source.h"
[email protected]eb795632012-09-01 00:19:3154#include "net/url_request/url_request_context.h"
asanka6150c522016-03-25 21:40:5755#include "storage/browser/blob/blob_url_request_job_factory.h"
ttr31481dc54b2015-08-06 20:11:2656#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2957
[email protected]35869622012-10-26 23:23:5558namespace content {
[email protected]a0ce3282011-08-19 20:49:5259namespace {
60
dchengf6c40582016-04-09 00:05:1361std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload(
62 std::unique_ptr<DownloadUrlParameters> params,
asanka3b4be122016-05-11 04:27:5763 content::ResourceContext* resource_context,
avib7348942015-12-25 20:57:1064 uint32_t download_id,
svaldez6901c902015-12-10 18:57:3365 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:5266 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:1067
dchengf6c40582016-04-09 00:05:1368 std::unique_ptr<net::URLRequest> url_request =
asanka00b621f2016-02-19 18:09:2369 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
dchengf6c40582016-04-09 00:05:1370 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
asanka6150c522016-03-25 21:40:5771 params->GetBlobDataHandle();
72 if (blob_data_handle) {
73 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
74 url_request.get(), std::move(blob_data_handle));
75 }
[email protected]fc179dd12013-03-16 09:35:1976
asanka00b621f2016-02-19 18:09:2377 // If there's a valid renderer process associated with the request, then the
78 // request should be driven by the ResourceLoader. Pass it over to the
79 // ResourceDispatcherHostImpl which will in turn pass it along to the
80 // ResourceLoader.
asanka3b4be122016-05-11 04:27:5781 if (params->render_process_host_id() >= 0) {
anantac82dd5be2016-08-26 01:21:5382 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest(
83 std::move(url_request), params->referrer(), resource_context,
84 params->content_initiated(), params->render_process_host_id(),
85 params->render_view_host_routing_id(),
86 params->render_frame_host_routing_id(),
87 params->do_not_prompt_for_login());
asanka00b621f2016-02-19 18:09:2388
89 // If the download was accepted, the DownloadResourceHandler is now
90 // responsible for driving the request to completion.
91 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
92 return nullptr;
93
94 // Otherwise, create an interrupted download.
dchengf6c40582016-04-09 00:05:1395 std::unique_ptr<DownloadCreateInfo> failed_created_info(
tfarina42834112016-09-22 13:38:2096 new DownloadCreateInfo(base::Time::Now(), net::NetLogWithSource(),
dchengf6c40582016-04-09 00:05:1397 base::WrapUnique(new DownloadSaveInfo)));
asanka00b621f2016-02-19 18:09:2398 failed_created_info->url_chain.push_back(params->url());
99 failed_created_info->result = reason;
dchengf6c40582016-04-09 00:05:13100 std::unique_ptr<ByteStreamReader> empty_byte_stream;
asanka00b621f2016-02-19 18:09:23101 BrowserThread::PostTask(
102 BrowserThread::UI, FROM_HERE,
103 base::Bind(&DownloadManager::StartDownload, download_manager,
104 base::Passed(&failed_created_info),
105 base::Passed(&empty_byte_stream), params->callback()));
svaldez6901c902015-12-10 18:57:33106 return nullptr;
107 }
asanka00b621f2016-02-19 18:09:23108
dchengf6c40582016-04-09 00:05:13109 return std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>(
asanka00b621f2016-02-19 18:09:23110 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
111 params->referrer())
svaldez6901c902015-12-10 18:57:33112 .release());
[email protected]a0ce3282011-08-19 20:49:52113}
114
[email protected]35869622012-10-26 23:23:55115class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03116 public:
[email protected]b3756012013-03-06 17:43:02117 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58118 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03119
dchengc2282aa2014-10-21 12:07:58120 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02121 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11122 const std::string& guid,
avib7348942015-12-25 20:57:10123 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02124 const base::FilePath& current_path,
125 const base::FilePath& target_path,
126 const std::vector<GURL>& url_chain,
127 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57128 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28129 const GURL& tab_url,
130 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45131 const std::string& mime_type,
132 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23133 base::Time start_time,
134 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21135 const std::string& etag,
136 const std::string& last_modified,
avib7348942015-12-25 20:57:10137 int64_t received_bytes,
138 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57139 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02140 DownloadItem::DownloadState state,
141 DownloadDangerType danger_type,
142 DownloadInterruptReason interrupt_reason,
143 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23144 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10145 bool transient,
qinmin23b28572017-02-25 00:24:28146 const std::vector<DownloadItem::ReceivedSlice>& received_slices,
tfarina42834112016-09-22 13:38:20147 const net::NetLogWithSource& net_log) override {
asanka038d58d2016-04-14 00:29:28148 return new DownloadItemImpl(
149 delegate, guid, download_id, current_path, target_path, url_chain,
asanka3b4be122016-05-11 04:27:57150 referrer_url, site_url, tab_url, tab_refererr_url, mime_type,
151 original_mime_type, start_time, end_time, etag, last_modified,
152 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason,
shaktisahufd49a3e2017-03-30 18:35:10153 opened, last_access_time, transient, received_slices, net_log);
[email protected]b3756012013-03-06 17:43:02154 }
[email protected]d25fda12012-06-12 17:05:03155
dchengc2282aa2014-10-21 12:07:58156 DownloadItemImpl* CreateActiveItem(
[email protected]b3756012013-03-06 17:43:02157 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10158 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02159 const DownloadCreateInfo& info,
tfarina42834112016-09-22 13:38:20160 const net::NetLogWithSource& net_log) override {
161 return new DownloadItemImpl(delegate, download_id, info, net_log);
[email protected]b3756012013-03-06 17:43:02162 }
[email protected]d25fda12012-06-12 17:05:03163
dchengc2282aa2014-10-21 12:07:58164 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02165 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10166 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02167 const base::FilePath& path,
168 const GURL& url,
[email protected]b3756012013-03-06 17:43:02169 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13170 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
tfarina42834112016-09-22 13:38:20171 const net::NetLogWithSource& net_log) override {
dcheng36b6aec92015-12-26 06:16:36172 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
tfarina42834112016-09-22 13:38:20173 std::move(request_handle), net_log);
[email protected]b3756012013-03-06 17:43:02174 }
[email protected]d25fda12012-06-12 17:05:03175};
176
[email protected]a0ce3282011-08-19 20:49:52177} // namespace
178
avicebda7922016-10-21 17:37:54179DownloadManagerImpl::DownloadManagerImpl(net::NetLog* net_log,
180 BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20181 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55182 file_factory_(new DownloadFileFactory()),
[email protected]16798692013-04-23 18:08:38183 shutdown_needed_(true),
184 browser_context_(browser_context),
avicebda7922016-10-21 17:37:54185 delegate_(nullptr),
[email protected]eba4a4d2013-05-29 02:18:06186 net_log_(net_log),
187 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38188 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29189}
190
[email protected]5656f8a2011-11-17 16:12:58191DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13192 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29193}
194
[email protected]89ec81f2013-05-15 19:20:02195DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10196 uint32_t id,
197 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52198 DCHECK_CURRENTLY_ON(BrowserThread::UI);
skyostil66bd67912016-08-12 12:33:11199 DCHECK(!base::ContainsKey(downloads_, id));
tfarina42834112016-09-22 13:38:20200 net::NetLogWithSource net_log =
201 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD);
[email protected]89ec81f2013-05-15 19:20:02202 DownloadItemImpl* download =
tfarina42834112016-09-22 13:38:20203 item_factory_->CreateActiveItem(this, id, info, net_log);
avicebda7922016-10-21 17:37:54204 downloads_[id] = base::WrapUnique(download);
asankaeef62b02016-03-14 21:23:11205 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02206 return download;
[email protected]381af492013-03-28 01:56:22207}
208
[email protected]530047e2013-07-12 17:02:25209void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52210 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25211 if (delegate_) {
212 delegate_->GetNextId(callback);
213 return;
[email protected]491aaa62012-06-07 03:50:18214 }
avib7348942015-12-25 20:57:10215 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25216 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53217}
218
[email protected]53ac00e82012-10-18 20:59:20219void DownloadManagerImpl::DetermineDownloadTarget(
220 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
221 // Note that this next call relies on
222 // DownloadItemImplDelegate::DownloadTargetCallback and
223 // DownloadManagerDelegate::DownloadTargetCallback having the same
224 // type. If the types ever diverge, gasket code will need to
225 // be written here.
226 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34227 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20228 // TODO(asanka): Determine a useful path if |target_path| is empty.
Asanka Herath1ba0e9f2017-04-03 18:48:53229 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
230 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path,
231 DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]53ac00e82012-10-18 20:59:20232 }
[email protected]47665442012-07-27 02:31:22233}
234
[email protected]98299622013-01-03 22:26:50235bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43236 DownloadItemImpl* item, const base::Closure& complete_callback) {
237 if (!delegate_ ||
238 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50239 return true;
[email protected]18710a42a2012-10-17 19:50:43240 }
241 // Otherwise, the delegate has accepted responsibility to run the
242 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50243 return false;
[email protected]fc03de22011-12-06 23:28:12244}
245
[email protected]2dec8ec2013-02-07 19:20:34246bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
247 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18248 if (!delegate_)
249 return false;
250
[email protected]fc03de22011-12-06 23:28:12251 return delegate_->ShouldOpenFileBasedOnExtension(path);
252}
253
[email protected]bde0e4f2012-11-08 22:49:59254bool DownloadManagerImpl::ShouldOpenDownload(
255 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43256 if (!delegate_)
257 return true;
258
[email protected]bde0e4f2012-11-08 22:49:59259 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
260 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
261 // to have the same type :-}.
262 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43263}
264
[email protected]35869622012-10-26 23:23:55265void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57266 delegate_ = delegate;
267}
268
[email protected]35869622012-10-26 23:23:55269DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28270 return delegate_;
271}
272
[email protected]5656f8a2011-11-17 16:12:58273void DownloadManagerImpl::Shutdown() {
pkastingf5279482016-07-27 02:18:20274 DVLOG(20) << __func__ << "() shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13275 if (!shutdown_needed_)
276 return;
277 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29278
ericwilligers254597b2016-10-17 10:32:31279 for (auto& observer : observers_)
280 observer.ManagerGoingDown(this);
[email protected]fb4f8d902011-09-16 06:07:08281 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13282
[email protected]fe752272013-05-29 18:57:45283 // If there are in-progress downloads, cancel them. This also goes for
284 // dangerous downloads which will remain in history if they aren't explicitly
285 // accepted or discarded. Canceling will remove the intermediate download
286 // file.
lukasza921fabfa2016-02-05 19:51:48287 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54288 DownloadItemImpl* download = it.second.get();
[email protected]fe752272013-05-29 18:57:45289 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17290 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29291 }
avicebda7922016-10-21 17:37:54292 downloads_.clear();
asankaeef62b02016-03-14 21:23:11293 downloads_by_guid_.clear();
svaldez9a07ab332016-01-12 18:29:01294 url_downloaders_.clear();
initial.commit09911bf2008-07-26 23:55:29295
[email protected]41f558fb2012-01-09 22:59:58296 // We'll have nothing more to report to the observers after this point.
297 observers_.Clear();
298
[email protected]b441a8492012-06-06 14:55:57299 if (delegate_)
300 delegate_->Shutdown();
avicebda7922016-10-21 17:37:54301 delegate_ = nullptr;
initial.commit09911bf2008-07-26 23:55:29302}
303
[email protected]530047e2013-07-12 17:02:25304void DownloadManagerImpl::StartDownload(
dchengf6c40582016-04-09 00:05:13305 std::unique_ptr<DownloadCreateInfo> info,
306 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25307 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52308 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25309 DCHECK(info);
asanka00b621f2016-02-19 18:09:23310 // |stream| is only non-nil if the download request was successful.
311 DCHECK((info->result == DOWNLOAD_INTERRUPT_REASON_NONE && stream.get()) ||
312 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && !stream.get()));
pkastingf5279482016-07-27 02:18:20313 DVLOG(20) << __func__
314 << "() result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10315 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25316 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
jialiul82b8e502016-09-30 17:42:42317 if (new_download)
318 RecordDownloadConnectionSecurity(info->url(), info->url_chain);
avib7348942015-12-25 20:57:10319 base::Callback<void(uint32_t)> got_id(base::Bind(
320 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
321 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02322 if (new_download) {
[email protected]530047e2013-07-12 17:02:25323 GetNextId(got_id);
324 } else {
325 got_id.Run(download_id);
326 }
327}
328
329void DownloadManagerImpl::StartDownloadWithId(
dchengf6c40582016-04-09 00:05:13330 std::unique_ptr<DownloadCreateInfo> info,
331 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25332 const DownloadUrlParameters::OnStartedCallback& on_started,
333 bool new_download,
avib7348942015-12-25 20:57:10334 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52335 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25336 DCHECK_NE(content::DownloadItem::kInvalidId, id);
avicebda7922016-10-21 17:37:54337 DownloadItemImpl* download = nullptr;
[email protected]530047e2013-07-12 17:02:25338 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02339 download = CreateActiveItem(id, *info);
340 } else {
avicebda7922016-10-21 17:37:54341 auto item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02342 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37343 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47344 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02345 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37346 // removed after it was resumed. Ignore. If the download is cancelled
347 // while resuming, then also ignore the request.
asanka64114152015-12-21 21:28:49348 info->request_handle->CancelRequest();
[email protected]530047e2013-07-12 17:02:25349 if (!on_started.is_null())
avicebda7922016-10-21 17:37:54350 on_started.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
asanka81c2214f2016-01-13 20:05:54351 // The ByteStreamReader lives and dies on the FILE thread.
asanka00b621f2016-02-19 18:09:23352 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
353 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
354 stream.release());
[email protected]530047e2013-07-12 17:02:25355 return;
[email protected]89ec81f2013-05-15 19:20:02356 }
avicebda7922016-10-21 17:37:54357 download = item_iterator->second.get();
[email protected]89ec81f2013-05-15 19:20:02358 }
359
[email protected]2dec8ec2013-02-07 19:20:34360 base::FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01361 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34362 base::FilePath website_save_directory; // Unused
363 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01364 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20365 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01366 }
367
dchengf6c40582016-04-09 00:05:13368 std::unique_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48369
asanka00b621f2016-02-19 18:09:23370 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
371 DCHECK(stream.get());
tfarina42834112016-09-22 13:38:20372 download_file.reset(file_factory_->CreateFile(
373 std::move(info->save_info), default_download_directory,
qinmin4373e642017-03-06 22:47:09374 std::move(stream), download->GetReceivedSlices(),
375 download->GetNetLogWithSource(),
tfarina42834112016-09-22 13:38:20376 download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48377 }
asanka4350f6a2016-03-15 02:40:57378 // It is important to leave info->save_info intact in the case of an interrupt
379 // so that the DownloadItem can salvage what it can out of a failed resumption
380 // attempt.
[email protected]492c0092013-08-10 13:49:48381
asanka00b621f2016-02-19 18:09:23382 download->Start(std::move(download_file), std::move(info->request_handle),
383 *info);
[email protected]96c3bdbaa2012-08-31 16:39:54384
[email protected]381af492013-03-28 01:56:22385 // For interrupted downloads, Start() will transition the state to
386 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
387 // For new downloads, we notify here, rather than earlier, so that
388 // the download_file is bound to download and all the usual
389 // setters (e.g. Cancel) work.
ericwilligers254597b2016-10-17 10:32:31390 if (new_download) {
391 for (auto& observer : observers_)
392 observer.OnDownloadCreated(this, download);
393 }
[email protected]96c3bdbaa2012-08-31 16:39:54394
[email protected]530047e2013-07-12 17:02:25395 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56396 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35397}
398
[email protected]5656f8a2011-11-17 16:12:58399void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52400 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lukasza921fabfa2016-02-05 19:51:48401 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54402 DownloadItemImpl* item = it.second.get();
[email protected]3d95e542012-11-20 00:52:08403 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48404 }
405}
406
[email protected]db1d8f72012-07-13 19:23:16407void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52408 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47409 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16410 !download_item->GetFileExternallyRemoved() &&
411 delegate_) {
412 delegate_->CheckForFileExistence(
413 download_item,
414 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
[email protected]eba4a4d2013-05-29 02:18:06415 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48416 }
417}
418
avib7348942015-12-25 20:57:10419void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id,
[email protected]4c544cd2013-01-25 07:54:16420 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52421 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16422 if (!result) { // File does not exist.
skyostil66bd67912016-08-12 12:33:11423 if (base::ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51424 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16425 }
[email protected]9fc114672011-06-15 08:17:48426}
427
asankadb9e50ad2016-10-03 15:00:29428std::string DownloadManagerImpl::GetApplicationClientIdForFileScanning() const {
429 if (delegate_)
430 return delegate_->ApplicationClientIdForFileScanning();
431 return std::string();
432}
433
[email protected]35869622012-10-26 23:23:55434BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58435 return browser_context_;
436}
437
[email protected]530047e2013-07-12 17:02:25438void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34439 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12440 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27441 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13442 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25443 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52444 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25445 GetNextId(base::Bind(
446 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
dcheng36b6aec92015-12-26 06:16:36447 weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type,
448 base::Passed(std::move(request_handle)), item_created));
[email protected]530047e2013-07-12 17:02:25449}
450
451void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
452 const base::FilePath& main_file_path,
453 const GURL& page_url,
454 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13455 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25456 const DownloadItemImplCreated& item_created,
avib7348942015-12-25 20:57:10457 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52458 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25459 DCHECK_NE(content::DownloadItem::kInvalidId, id);
skyostil66bd67912016-08-12 12:33:11460 DCHECK(!base::ContainsKey(downloads_, id));
tfarina42834112016-09-22 13:38:20461 net::NetLogWithSource net_log =
462 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08463 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
dcheng36b6aec92015-12-26 06:16:36464 this, id, main_file_path, page_url, mime_type, std::move(request_handle),
tfarina42834112016-09-22 13:38:20465 net_log);
avicebda7922016-10-21 17:37:54466 downloads_[download_item->GetId()] = base::WrapUnique(download_item);
skyostil66bd67912016-08-12 12:33:11467 DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid()));
asankaeef62b02016-03-14 21:23:11468 downloads_by_guid_[download_item->GetGuid()] = download_item;
ericwilligers254597b2016-10-17 10:32:31469 for (auto& observer : observers_)
470 observer.OnDownloadCreated(this, download_item);
[email protected]530047e2013-07-12 17:02:25471 if (!item_created.is_null())
472 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12473}
474
[email protected]df58aa82013-01-24 21:54:00475void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
476 DownloadItem* download_item) {
ericwilligers254597b2016-10-17 10:32:31477 for (auto& observer : observers_)
478 observer.OnSavePackageSuccessfullyFinished(this, download_item);
[email protected]df58aa82013-01-24 21:54:00479}
480
[email protected]8d68a3e02013-01-12 15:57:10481// Resume a download of a specific URL. We send the request to the
482// ResourceDispatcherHost, and let it send us responses like a regular
483// download.
484void DownloadManagerImpl::ResumeInterruptedDownload(
dchengf6c40582016-04-09 00:05:13485 std::unique_ptr<content::DownloadUrlParameters> params,
avib7348942015-12-25 20:57:10486 uint32_t id) {
svaldez6901c902015-12-10 18:57:33487 BrowserThread::PostTaskAndReplyWithResult(
488 BrowserThread::IO, FROM_HERE,
asanka3b4be122016-05-11 04:27:57489 base::Bind(&BeginDownload, base::Passed(&params),
490 browser_context_->GetResourceContext(), id,
svaldez6901c902015-12-10 18:57:33491 weak_factory_.GetWeakPtr()),
492 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
493 weak_factory_.GetWeakPtr()));
[email protected]8d68a3e02013-01-12 15:57:10494}
495
[email protected]53ac00e82012-10-18 20:59:20496void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
dchengf6c40582016-04-09 00:05:13497 std::unique_ptr<DownloadItemFactory> item_factory) {
dcheng36b6aec92015-12-26 06:16:36498 item_factory_ = std::move(item_factory);
[email protected]53ac00e82012-10-18 20:59:20499}
500
501void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
dchengf6c40582016-04-09 00:05:13502 std::unique_ptr<DownloadFileFactory> file_factory) {
dcheng36b6aec92015-12-26 06:16:36503 file_factory_ = std::move(file_factory);
[email protected]53ac00e82012-10-18 20:59:20504}
505
[email protected]35869622012-10-26 23:23:55506DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20507 return file_factory_.get();
508}
509
[email protected]db1d8f72012-07-13 19:23:16510void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28511 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40512 return;
513
asankaeef62b02016-03-14 21:23:11514 downloads_by_guid_.erase(download->GetGuid());
avicebda7922016-10-21 17:37:54515 downloads_.erase(download->GetId());
initial.commit09911bf2008-07-26 23:55:29516}
517
svaldez6901c902015-12-10 18:57:33518void DownloadManagerImpl::AddUrlDownloader(
dchengf6c40582016-04-09 00:05:13519 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>
520 downloader) {
svaldez6901c902015-12-10 18:57:33521 if (downloader)
dcheng36b6aec92015-12-26 06:16:36522 url_downloaders_.push_back(std::move(downloader));
svaldez6901c902015-12-10 18:57:33523}
524
anantac82dd5be2016-08-26 01:21:53525// static
526DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest(
527 std::unique_ptr<net::URLRequest> url_request,
528 const Referrer& referrer,
529 ResourceContext* resource_context,
530 bool is_content_initiated,
531 int render_process_id,
532 int render_view_route_id,
533 int render_frame_route_id,
534 bool do_not_prompt_for_login) {
535 if (ResourceDispatcherHostImpl::Get()->is_shutdown())
536 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
537
538 // The URLRequest needs to be initialized with the referrer and other
539 // information prior to issuing it.
540 ResourceDispatcherHostImpl::Get()->InitializeURLRequest(
541 url_request.get(), referrer,
542 true, // download.
543 render_process_id, render_view_route_id, render_frame_route_id,
megjabloncaf312f2017-01-12 18:47:49544 PREVIEWS_OFF, resource_context);
anantac82dd5be2016-08-26 01:21:53545
546 // We treat a download as a main frame load, and thus update the policy URL on
547 // redirects.
548 //
549 // TODO(davidben): Is this correct? If this came from a
550 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
551 // appropriately?
552 url_request->set_first_party_url_policy(
553 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
554
555 const GURL& url = url_request->original_url();
556
557 // Check if the renderer is permitted to request the requested URL.
558 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
559 render_process_id, url)) {
560 DVLOG(1) << "Denied unauthorized download request for "
561 << url.possibly_invalid_spec();
562 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
563 }
564
565 const net::URLRequestContext* request_context = url_request->context();
mmenke1918ae782017-04-03 20:10:20566 if (!request_context->job_factory()->IsHandledProtocol(url.scheme())) {
anantac82dd5be2016-08-26 01:21:53567 DVLOG(1) << "Download request for unsupported protocol: "
568 << url.possibly_invalid_spec();
569 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
570 }
571
572 // From this point forward, the |DownloadResourceHandler| is responsible for
573 // |started_callback|.
574 // TODO(ananta)
575 // Find a better way to create the DownloadResourceHandler instance.
576 std::unique_ptr<ResourceHandler> handler(
577 DownloadResourceHandler::Create(url_request.get()));
578
579 ResourceDispatcherHostImpl::Get()->BeginURLRequest(
580 std::move(url_request), std::move(handler), true, // download
581 is_content_initiated, do_not_prompt_for_login, resource_context);
582 return DOWNLOAD_INTERRUPT_REASON_NONE;
583}
584
asanka6381bd4e2017-01-05 22:08:37585int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
msramekd9c162a2016-02-23 11:17:21586 const base::Callback<bool(const GURL&)>& url_filter,
asanka6381bd4e2017-01-05 22:08:37587 base::Time remove_begin,
588 base::Time remove_end) {
[email protected]237af6d2013-03-15 23:45:28589 int count = 0;
avicebda7922016-10-21 17:37:54590 auto it = downloads_.begin();
[email protected]237af6d2013-03-15 23:45:28591 while (it != downloads_.end()) {
avicebda7922016-10-21 17:37:54592 DownloadItemImpl* download = it->second.get();
[email protected]237af6d2013-03-15 23:45:28593
594 // Increment done here to protect against invalidation below.
595 ++it;
596
ttr31481dc54b2015-08-06 20:11:26597 if (download->GetState() != DownloadItem::IN_PROGRESS &&
asanka6381bd4e2017-01-05 22:08:37598 url_filter.Run(download->GetURL()) &&
599 download->GetStartTime() >= remove_begin &&
600 (remove_end.is_null() || download->GetStartTime() < remove_end)) {
[email protected]237af6d2013-03-15 23:45:28601 download->Remove();
602 count++;
initial.commit09911bf2008-07-26 23:55:29603 }
initial.commit09911bf2008-07-26 23:55:29604 }
[email protected]237af6d2013-03-15 23:45:28605 return count;
initial.commit09911bf2008-07-26 23:55:29606}
607
[email protected]0d4e30c2012-01-28 00:47:53608void DownloadManagerImpl::DownloadUrl(
dchengf6c40582016-04-09 00:05:13609 std::unique_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14610 if (params->post_id() >= 0) {
611 // Check this here so that the traceback is more useful.
612 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43613 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14614 }
svaldez6901c902015-12-10 18:57:33615 BrowserThread::PostTaskAndReplyWithResult(
616 BrowserThread::IO, FROM_HERE,
617 base::Bind(&BeginDownload, base::Passed(&params),
asanka3b4be122016-05-11 04:27:57618 browser_context_->GetResourceContext(),
svaldez6901c902015-12-10 18:57:33619 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()),
620 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
621 weak_factory_.GetWeakPtr()));
initial.commit09911bf2008-07-26 23:55:29622}
623
[email protected]5656f8a2011-11-17 16:12:58624void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29625 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29626}
627
[email protected]5656f8a2011-11-17 16:12:58628void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29629 observers_.RemoveObserver(observer);
630}
631
[email protected]3d95e542012-11-20 00:52:08632DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11633 const std::string& guid,
avib7348942015-12-25 20:57:10634 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34635 const base::FilePath& current_path,
636 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15637 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08638 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57639 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28640 const GURL& tab_url,
641 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45642 const std::string& mime_type,
643 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23644 base::Time start_time,
645 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21646 const std::string& etag,
647 const std::string& last_modified,
avib7348942015-12-25 20:57:10648 int64_t received_bytes,
649 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57650 const std::string& hash,
[email protected]3d95e542012-11-20 00:52:08651 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15652 DownloadDangerType danger_type,
653 DownloadInterruptReason interrupt_reason,
qinmin23b28572017-02-25 00:24:28654 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23655 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10656 bool transient,
qinmin23b28572017-02-25 00:24:28657 const std::vector<DownloadItem::ReceivedSlice>& received_slices) {
skyostil66bd67912016-08-12 12:33:11658 if (base::ContainsKey(downloads_, id)) {
[email protected]6065748f2013-10-29 01:07:43659 NOTREACHED();
avicebda7922016-10-21 17:37:54660 return nullptr;
[email protected]6065748f2013-10-29 01:07:43661 }
skyostil66bd67912016-08-12 12:33:11662 DCHECK(!base::ContainsKey(downloads_by_guid_, guid));
[email protected]3d95e542012-11-20 00:52:08663 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
asanka038d58d2016-04-14 00:29:28664 this, guid, id, current_path, target_path, url_chain, referrer_url,
asanka3b4be122016-05-11 04:27:57665 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type,
666 start_time, end_time, etag, last_modified, received_bytes, total_bytes,
shaktisahu60eb97f2017-03-03 08:53:23667 hash, state, danger_type, interrupt_reason, opened, last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10668 transient, received_slices,
tfarina42834112016-09-22 13:38:20669 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD));
avicebda7922016-10-21 17:37:54670 downloads_[id] = base::WrapUnique(item);
asankaeef62b02016-03-14 21:23:11671 downloads_by_guid_[guid] = item;
ericwilligers254597b2016-10-17 10:32:31672 for (auto& observer : observers_)
673 observer.OnDownloadCreated(this, item);
pkastingf5279482016-07-27 02:18:20674 DVLOG(20) << __func__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08675 return item;
initial.commit09911bf2008-07-26 23:55:29676}
677
[email protected]5656f8a2011-11-17 16:12:58678int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56679 int count = 0;
lukasza921fabfa2016-02-05 19:51:48680 for (const auto& it : downloads_) {
681 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56682 ++count;
683 }
684 return count;
[email protected]5656f8a2011-11-17 16:12:58685}
686
[email protected]422a7d12013-10-21 12:10:42687int DownloadManagerImpl::NonMaliciousInProgressCount() const {
688 int count = 0;
lukasza921fabfa2016-02-05 19:51:48689 for (const auto& it : downloads_) {
690 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
691 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
692 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
693 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
694 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59695 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42696 ++count;
697 }
698 }
699 return count;
700}
701
avib7348942015-12-25 20:57:10702DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
avicebda7922016-10-21 17:37:54703 return base::ContainsKey(downloads_, download_id)
704 ? downloads_[download_id].get()
705 : nullptr;
asankaeef62b02016-03-14 21:23:11706}
707
708DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
709 DCHECK(guid == base::ToUpperASCII(guid));
skyostil66bd67912016-08-12 12:33:11710 return base::ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
711 : nullptr;
[email protected]ba7895d2012-06-22 19:02:52712}
713
xingliu468824d2017-02-28 02:59:25714void DownloadManagerImpl::OnUrlDownloaderStarted(
715 std::unique_ptr<DownloadCreateInfo> download_create_info,
716 std::unique_ptr<ByteStreamReader> stream_reader,
717 const DownloadUrlParameters::OnStartedCallback& callback) {
718 StartDownload(std::move(download_create_info), std::move(stream_reader),
719 callback);
720}
721
722void DownloadManagerImpl::OnUrlDownloaderStopped(UrlDownloader* downloader) {
723 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end();
724 ++ptr) {
725 if (ptr->get() == downloader) {
726 url_downloaders_.erase(ptr);
727 return;
728 }
729 }
730}
731
[email protected]9f1f4092012-09-10 21:18:04732void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48733 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54734 downloads->push_back(it.second.get());
[email protected]9f1f4092012-09-10 21:18:04735 }
736}
737
[email protected]ffc0c2d2013-01-21 15:32:12738void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06739 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48740 for (const auto& it : downloads_) {
avicebda7922016-10-21 17:37:54741 DownloadItemImpl* item = it.second.get();
[email protected]803036e02013-06-15 17:08:47742 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40743 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06744 ++num_unopened;
745 }
[email protected]35869622012-10-26 23:23:55746 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12747
[email protected]0baf5922013-01-30 13:55:18748 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12749 delegate_->OpenDownload(download);
750}
751
752void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
753 if (delegate_)
754 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06755}
[email protected]5656f8a2011-11-17 16:12:58756
[email protected]35869622012-10-26 23:23:55757} // namespace content