blob: ec6eabc73ea89d88bc9d8efacc12a172847ccd88 [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"
[email protected]7286e3fc2011-07-19 22:13:2418#include "base/stl_util.h"
[email protected]10994d132013-06-11 07:16:1819#include "base/strings/stringprintf.h"
[email protected]40d11e02013-03-28 17:43:1420#include "base/strings/sys_string_conversions.h"
[email protected]d59d40c2012-08-08 18:18:3721#include "base/supports_user_data.h"
[email protected]eda58402011-09-21 19:32:0222#include "base/synchronization/lock.h"
[email protected]d2a8fb72010-01-21 05:31:4223#include "build/build_config.h"
[email protected]f8e92b5d2013-03-21 18:35:4624#include "content/browser/byte_stream.h"
anantac82dd5be2016-08-26 01:21:5325#include "content/browser/child_process_security_policy_impl.h"
[email protected]71bf3f5e2011-08-15 21:05:2226#include "content/browser/download/download_create_info.h"
[email protected]53ac00e82012-10-18 20:59:2027#include "content/browser/download/download_file_factory.h"
28#include "content/browser/download/download_item_factory.h"
[email protected]c09a8fd2011-11-21 19:54:5029#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0630#include "content/browser/download/download_stats.h"
[email protected]678c0362012-12-05 08:02:4431#include "content/browser/loader/resource_dispatcher_host_impl.h"
anantac82dd5be2016-08-26 01:21:5332#include "content/browser/loader/resource_request_info_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3233#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2934#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1135#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4436#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3437#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1738#include "content/public/browser/download_manager_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1439#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0740#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1641#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3042#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1343#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2744#include "content/public/browser/web_contents_delegate.h"
[email protected]8d68a3e02013-01-12 15:57:1045#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0746#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1447#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1948#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2849#include "net/base/upload_bytes_element_reader.h"
mikecirone8b85c432016-09-08 19:11:0050#include "net/log/net_log_source_type.h"
[email protected]eb795632012-09-01 00:19:3151#include "net/url_request/url_request_context.h"
asanka6150c522016-03-25 21:40:5752#include "storage/browser/blob/blob_url_request_job_factory.h"
ttr31481dc54b2015-08-06 20:11:2653#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2954
[email protected]35869622012-10-26 23:23:5555namespace content {
[email protected]a0ce3282011-08-19 20:49:5256namespace {
57
dchengf6c40582016-04-09 00:05:1358std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload(
59 std::unique_ptr<DownloadUrlParameters> params,
asanka3b4be122016-05-11 04:27:5760 content::ResourceContext* resource_context,
avib7348942015-12-25 20:57:1061 uint32_t download_id,
svaldez6901c902015-12-10 18:57:3362 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:5263 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:1064
dchengf6c40582016-04-09 00:05:1365 std::unique_ptr<net::URLRequest> url_request =
asanka00b621f2016-02-19 18:09:2366 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
dchengf6c40582016-04-09 00:05:1367 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
asanka6150c522016-03-25 21:40:5768 params->GetBlobDataHandle();
69 if (blob_data_handle) {
70 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
71 url_request.get(), std::move(blob_data_handle));
72 }
[email protected]fc179dd12013-03-16 09:35:1973
asanka00b621f2016-02-19 18:09:2374 // If there's a valid renderer process associated with the request, then the
75 // request should be driven by the ResourceLoader. Pass it over to the
76 // ResourceDispatcherHostImpl which will in turn pass it along to the
77 // ResourceLoader.
asanka3b4be122016-05-11 04:27:5778 if (params->render_process_host_id() >= 0) {
anantac82dd5be2016-08-26 01:21:5379 DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest(
80 std::move(url_request), params->referrer(), resource_context,
81 params->content_initiated(), params->render_process_host_id(),
82 params->render_view_host_routing_id(),
83 params->render_frame_host_routing_id(),
84 params->do_not_prompt_for_login());
asanka00b621f2016-02-19 18:09:2385
86 // If the download was accepted, the DownloadResourceHandler is now
87 // responsible for driving the request to completion.
88 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
89 return nullptr;
90
91 // Otherwise, create an interrupted download.
dchengf6c40582016-04-09 00:05:1392 std::unique_ptr<DownloadCreateInfo> failed_created_info(
tfarina42834112016-09-22 13:38:2093 new DownloadCreateInfo(base::Time::Now(), net::NetLogWithSource(),
dchengf6c40582016-04-09 00:05:1394 base::WrapUnique(new DownloadSaveInfo)));
asanka00b621f2016-02-19 18:09:2395 failed_created_info->url_chain.push_back(params->url());
96 failed_created_info->result = reason;
dchengf6c40582016-04-09 00:05:1397 std::unique_ptr<ByteStreamReader> empty_byte_stream;
asanka00b621f2016-02-19 18:09:2398 BrowserThread::PostTask(
99 BrowserThread::UI, FROM_HERE,
100 base::Bind(&DownloadManager::StartDownload, download_manager,
101 base::Passed(&failed_created_info),
102 base::Passed(&empty_byte_stream), params->callback()));
svaldez6901c902015-12-10 18:57:33103 return nullptr;
104 }
asanka00b621f2016-02-19 18:09:23105
dchengf6c40582016-04-09 00:05:13106 return std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>(
asanka00b621f2016-02-19 18:09:23107 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
108 params->referrer())
svaldez6901c902015-12-10 18:57:33109 .release());
[email protected]a0ce3282011-08-19 20:49:52110}
111
[email protected]35869622012-10-26 23:23:55112class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03113 public:
[email protected]b3756012013-03-06 17:43:02114 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58115 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03116
dchengc2282aa2014-10-21 12:07:58117 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02118 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11119 const std::string& guid,
avib7348942015-12-25 20:57:10120 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02121 const base::FilePath& current_path,
122 const base::FilePath& target_path,
123 const std::vector<GURL>& url_chain,
124 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57125 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28126 const GURL& tab_url,
127 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45128 const std::string& mime_type,
129 const std::string& original_mime_type,
[email protected]b3756012013-03-06 17:43:02130 const base::Time& start_time,
131 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21132 const std::string& etag,
133 const std::string& last_modified,
avib7348942015-12-25 20:57:10134 int64_t received_bytes,
135 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57136 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02137 DownloadItem::DownloadState state,
138 DownloadDangerType danger_type,
139 DownloadInterruptReason interrupt_reason,
140 bool opened,
tfarina42834112016-09-22 13:38:20141 const net::NetLogWithSource& net_log) override {
asanka038d58d2016-04-14 00:29:28142 return new DownloadItemImpl(
143 delegate, guid, download_id, current_path, target_path, url_chain,
asanka3b4be122016-05-11 04:27:57144 referrer_url, site_url, tab_url, tab_refererr_url, mime_type,
145 original_mime_type, start_time, end_time, etag, last_modified,
146 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason,
tfarina42834112016-09-22 13:38:20147 opened, net_log);
[email protected]b3756012013-03-06 17:43:02148 }
[email protected]d25fda12012-06-12 17:05:03149
dchengc2282aa2014-10-21 12:07:58150 DownloadItemImpl* CreateActiveItem(
[email protected]b3756012013-03-06 17:43:02151 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10152 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02153 const DownloadCreateInfo& info,
tfarina42834112016-09-22 13:38:20154 const net::NetLogWithSource& net_log) override {
155 return new DownloadItemImpl(delegate, download_id, info, net_log);
[email protected]b3756012013-03-06 17:43:02156 }
[email protected]d25fda12012-06-12 17:05:03157
dchengc2282aa2014-10-21 12:07:58158 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02159 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10160 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02161 const base::FilePath& path,
162 const GURL& url,
[email protected]b3756012013-03-06 17:43:02163 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13164 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
tfarina42834112016-09-22 13:38:20165 const net::NetLogWithSource& net_log) override {
dcheng36b6aec92015-12-26 06:16:36166 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
tfarina42834112016-09-22 13:38:20167 std::move(request_handle), net_log);
[email protected]b3756012013-03-06 17:43:02168 }
[email protected]d25fda12012-06-12 17:05:03169};
170
[email protected]a0ce3282011-08-19 20:49:52171} // namespace
172
[email protected]d25fda12012-06-12 17:05:03173DownloadManagerImpl::DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38174 net::NetLog* net_log,
175 BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20176 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55177 file_factory_(new DownloadFileFactory()),
[email protected]16798692013-04-23 18:08:38178 shutdown_needed_(true),
179 browser_context_(browser_context),
[email protected]33c20cd92012-06-14 22:02:50180 delegate_(NULL),
[email protected]eba4a4d2013-05-29 02:18:06181 net_log_(net_log),
182 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38183 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29184}
185
[email protected]5656f8a2011-11-17 16:12:58186DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13187 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29188}
189
[email protected]89ec81f2013-05-15 19:20:02190DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10191 uint32_t id,
192 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52193 DCHECK_CURRENTLY_ON(BrowserThread::UI);
skyostil66bd67912016-08-12 12:33:11194 DCHECK(!base::ContainsKey(downloads_, id));
tfarina42834112016-09-22 13:38:20195 net::NetLogWithSource net_log =
196 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD);
[email protected]89ec81f2013-05-15 19:20:02197 DownloadItemImpl* download =
tfarina42834112016-09-22 13:38:20198 item_factory_->CreateActiveItem(this, id, info, net_log);
[email protected]530047e2013-07-12 17:02:25199 downloads_[id] = download;
asankaeef62b02016-03-14 21:23:11200 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02201 return download;
[email protected]381af492013-03-28 01:56:22202}
203
[email protected]530047e2013-07-12 17:02:25204void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52205 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25206 if (delegate_) {
207 delegate_->GetNextId(callback);
208 return;
[email protected]491aaa62012-06-07 03:50:18209 }
avib7348942015-12-25 20:57:10210 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25211 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53212}
213
[email protected]53ac00e82012-10-18 20:59:20214void DownloadManagerImpl::DetermineDownloadTarget(
215 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
216 // Note that this next call relies on
217 // DownloadItemImplDelegate::DownloadTargetCallback and
218 // DownloadManagerDelegate::DownloadTargetCallback having the same
219 // type. If the types ever diverge, gasket code will need to
220 // be written here.
221 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34222 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20223 // TODO(asanka): Determine a useful path if |target_path| is empty.
224 callback.Run(target_path,
225 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
[email protected]35869622012-10-26 23:23:55226 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
[email protected]53ac00e82012-10-18 20:59:20227 target_path);
228 }
[email protected]47665442012-07-27 02:31:22229}
230
[email protected]98299622013-01-03 22:26:50231bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43232 DownloadItemImpl* item, const base::Closure& complete_callback) {
233 if (!delegate_ ||
234 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50235 return true;
[email protected]18710a42a2012-10-17 19:50:43236 }
237 // Otherwise, the delegate has accepted responsibility to run the
238 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50239 return false;
[email protected]fc03de22011-12-06 23:28:12240}
241
[email protected]2dec8ec2013-02-07 19:20:34242bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
243 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18244 if (!delegate_)
245 return false;
246
[email protected]fc03de22011-12-06 23:28:12247 return delegate_->ShouldOpenFileBasedOnExtension(path);
248}
249
[email protected]bde0e4f2012-11-08 22:49:59250bool DownloadManagerImpl::ShouldOpenDownload(
251 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43252 if (!delegate_)
253 return true;
254
[email protected]bde0e4f2012-11-08 22:49:59255 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
256 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
257 // to have the same type :-}.
258 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43259}
260
[email protected]35869622012-10-26 23:23:55261void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57262 delegate_ = delegate;
263}
264
[email protected]35869622012-10-26 23:23:55265DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28266 return delegate_;
267}
268
[email protected]5656f8a2011-11-17 16:12:58269void DownloadManagerImpl::Shutdown() {
pkastingf5279482016-07-27 02:18:20270 DVLOG(20) << __func__ << "() shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13271 if (!shutdown_needed_)
272 return;
273 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29274
[email protected]75e51b52012-02-04 16:57:54275 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08276 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13277
[email protected]fe752272013-05-29 18:57:45278 // If there are in-progress downloads, cancel them. This also goes for
279 // dangerous downloads which will remain in history if they aren't explicitly
280 // accepted or discarded. Canceling will remove the intermediate download
281 // file.
lukasza921fabfa2016-02-05 19:51:48282 for (const auto& it : downloads_) {
283 DownloadItemImpl* download = it.second;
[email protected]fe752272013-05-29 18:57:45284 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17285 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29286 }
skyostil66bd67912016-08-12 12:33:11287 base::STLDeleteValues(&downloads_);
asankaeef62b02016-03-14 21:23:11288 downloads_by_guid_.clear();
svaldez9a07ab332016-01-12 18:29:01289 url_downloaders_.clear();
initial.commit09911bf2008-07-26 23:55:29290
[email protected]41f558fb2012-01-09 22:59:58291 // We'll have nothing more to report to the observers after this point.
292 observers_.Clear();
293
[email protected]b441a8492012-06-06 14:55:57294 if (delegate_)
295 delegate_->Shutdown();
[email protected]47665442012-07-27 02:31:22296 delegate_ = NULL;
initial.commit09911bf2008-07-26 23:55:29297}
298
[email protected]530047e2013-07-12 17:02:25299void DownloadManagerImpl::StartDownload(
dchengf6c40582016-04-09 00:05:13300 std::unique_ptr<DownloadCreateInfo> info,
301 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25302 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52303 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25304 DCHECK(info);
asanka00b621f2016-02-19 18:09:23305 // |stream| is only non-nil if the download request was successful.
306 DCHECK((info->result == DOWNLOAD_INTERRUPT_REASON_NONE && stream.get()) ||
307 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && !stream.get()));
pkastingf5279482016-07-27 02:18:20308 DVLOG(20) << __func__
309 << "() result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10310 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25311 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
avib7348942015-12-25 20:57:10312 base::Callback<void(uint32_t)> got_id(base::Bind(
313 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
314 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02315 if (new_download) {
[email protected]530047e2013-07-12 17:02:25316 GetNextId(got_id);
317 } else {
318 got_id.Run(download_id);
319 }
320}
321
322void DownloadManagerImpl::StartDownloadWithId(
dchengf6c40582016-04-09 00:05:13323 std::unique_ptr<DownloadCreateInfo> info,
324 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25325 const DownloadUrlParameters::OnStartedCallback& on_started,
326 bool new_download,
avib7348942015-12-25 20:57:10327 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52328 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25329 DCHECK_NE(content::DownloadItem::kInvalidId, id);
330 DownloadItemImpl* download = NULL;
331 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02332 download = CreateActiveItem(id, *info);
333 } else {
[email protected]530047e2013-07-12 17:02:25334 DownloadMap::iterator item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02335 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37336 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47337 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02338 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37339 // removed after it was resumed. Ignore. If the download is cancelled
340 // while resuming, then also ignore the request.
asanka64114152015-12-21 21:28:49341 info->request_handle->CancelRequest();
[email protected]530047e2013-07-12 17:02:25342 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56343 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
asanka81c2214f2016-01-13 20:05:54344 // The ByteStreamReader lives and dies on the FILE thread.
asanka00b621f2016-02-19 18:09:23345 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
346 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
347 stream.release());
[email protected]530047e2013-07-12 17:02:25348 return;
[email protected]89ec81f2013-05-15 19:20:02349 }
350 download = item_iterator->second;
[email protected]89ec81f2013-05-15 19:20:02351 }
352
[email protected]2dec8ec2013-02-07 19:20:34353 base::FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01354 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34355 base::FilePath website_save_directory; // Unused
356 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01357 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20358 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01359 }
360
dchengf6c40582016-04-09 00:05:13361 std::unique_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48362
asanka00b621f2016-02-19 18:09:23363 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
364 DCHECK(stream.get());
tfarina42834112016-09-22 13:38:20365 download_file.reset(file_factory_->CreateFile(
366 std::move(info->save_info), default_download_directory,
367 std::move(stream), download->GetNetLogWithSource(),
368 download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48369 }
asanka4350f6a2016-03-15 02:40:57370 // It is important to leave info->save_info intact in the case of an interrupt
371 // so that the DownloadItem can salvage what it can out of a failed resumption
372 // attempt.
[email protected]492c0092013-08-10 13:49:48373
asanka00b621f2016-02-19 18:09:23374 download->Start(std::move(download_file), std::move(info->request_handle),
375 *info);
[email protected]96c3bdbaa2012-08-31 16:39:54376
[email protected]381af492013-03-28 01:56:22377 // For interrupted downloads, Start() will transition the state to
378 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
379 // For new downloads, we notify here, rather than earlier, so that
380 // the download_file is bound to download and all the usual
381 // setters (e.g. Cancel) work.
382 if (new_download)
383 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]96c3bdbaa2012-08-31 16:39:54384
[email protected]530047e2013-07-12 17:02:25385 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56386 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35387}
388
[email protected]5656f8a2011-11-17 16:12:58389void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52390 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lukasza921fabfa2016-02-05 19:51:48391 for (const auto& it : downloads_) {
392 DownloadItemImpl* item = it.second;
[email protected]3d95e542012-11-20 00:52:08393 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48394 }
395}
396
[email protected]db1d8f72012-07-13 19:23:16397void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52398 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47399 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16400 !download_item->GetFileExternallyRemoved() &&
401 delegate_) {
402 delegate_->CheckForFileExistence(
403 download_item,
404 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
[email protected]eba4a4d2013-05-29 02:18:06405 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48406 }
407}
408
avib7348942015-12-25 20:57:10409void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id,
[email protected]4c544cd2013-01-25 07:54:16410 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52411 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16412 if (!result) { // File does not exist.
skyostil66bd67912016-08-12 12:33:11413 if (base::ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51414 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16415 }
[email protected]9fc114672011-06-15 08:17:48416}
417
[email protected]35869622012-10-26 23:23:55418BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58419 return browser_context_;
420}
421
[email protected]530047e2013-07-12 17:02:25422void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34423 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12424 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27425 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13426 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25427 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52428 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25429 GetNextId(base::Bind(
430 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
dcheng36b6aec92015-12-26 06:16:36431 weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type,
432 base::Passed(std::move(request_handle)), item_created));
[email protected]530047e2013-07-12 17:02:25433}
434
435void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
436 const base::FilePath& main_file_path,
437 const GURL& page_url,
438 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13439 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25440 const DownloadItemImplCreated& item_created,
avib7348942015-12-25 20:57:10441 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52442 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25443 DCHECK_NE(content::DownloadItem::kInvalidId, id);
skyostil66bd67912016-08-12 12:33:11444 DCHECK(!base::ContainsKey(downloads_, id));
tfarina42834112016-09-22 13:38:20445 net::NetLogWithSource net_log =
446 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08447 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
dcheng36b6aec92015-12-26 06:16:36448 this, id, main_file_path, page_url, mime_type, std::move(request_handle),
tfarina42834112016-09-22 13:38:20449 net_log);
[email protected]3d95e542012-11-20 00:52:08450 downloads_[download_item->GetId()] = download_item;
skyostil66bd67912016-08-12 12:33:11451 DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid()));
asankaeef62b02016-03-14 21:23:11452 downloads_by_guid_[download_item->GetGuid()] = download_item;
[email protected]3d95e542012-11-20 00:52:08453 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
454 this, download_item));
[email protected]530047e2013-07-12 17:02:25455 if (!item_created.is_null())
456 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12457}
458
[email protected]df58aa82013-01-24 21:54:00459void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
460 DownloadItem* download_item) {
461 FOR_EACH_OBSERVER(Observer, observers_,
462 OnSavePackageSuccessfullyFinished(this, download_item));
463}
464
[email protected]8d68a3e02013-01-12 15:57:10465// Resume a download of a specific URL. We send the request to the
466// ResourceDispatcherHost, and let it send us responses like a regular
467// download.
468void DownloadManagerImpl::ResumeInterruptedDownload(
dchengf6c40582016-04-09 00:05:13469 std::unique_ptr<content::DownloadUrlParameters> params,
avib7348942015-12-25 20:57:10470 uint32_t id) {
svaldez6901c902015-12-10 18:57:33471 BrowserThread::PostTaskAndReplyWithResult(
472 BrowserThread::IO, FROM_HERE,
asanka3b4be122016-05-11 04:27:57473 base::Bind(&BeginDownload, base::Passed(&params),
474 browser_context_->GetResourceContext(), id,
svaldez6901c902015-12-10 18:57:33475 weak_factory_.GetWeakPtr()),
476 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
477 weak_factory_.GetWeakPtr()));
[email protected]8d68a3e02013-01-12 15:57:10478}
479
[email protected]53ac00e82012-10-18 20:59:20480void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
dchengf6c40582016-04-09 00:05:13481 std::unique_ptr<DownloadItemFactory> item_factory) {
dcheng36b6aec92015-12-26 06:16:36482 item_factory_ = std::move(item_factory);
[email protected]53ac00e82012-10-18 20:59:20483}
484
485void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
dchengf6c40582016-04-09 00:05:13486 std::unique_ptr<DownloadFileFactory> file_factory) {
dcheng36b6aec92015-12-26 06:16:36487 file_factory_ = std::move(file_factory);
[email protected]53ac00e82012-10-18 20:59:20488}
489
[email protected]35869622012-10-26 23:23:55490DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20491 return file_factory_.get();
492}
493
[email protected]db1d8f72012-07-13 19:23:16494void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28495 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40496 return;
497
asankaeef62b02016-03-14 21:23:11498 downloads_by_guid_.erase(download->GetGuid());
499
avib7348942015-12-25 20:57:10500 uint32_t download_id = download->GetId();
[email protected]6065748f2013-10-29 01:07:43501 if (downloads_.erase(download_id) == 0)
[email protected]237af6d2013-03-15 23:45:28502 return;
[email protected]237af6d2013-03-15 23:45:28503 delete download;
initial.commit09911bf2008-07-26 23:55:29504}
505
svaldez6901c902015-12-10 18:57:33506void DownloadManagerImpl::AddUrlDownloader(
dchengf6c40582016-04-09 00:05:13507 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>
508 downloader) {
svaldez6901c902015-12-10 18:57:33509 if (downloader)
dcheng36b6aec92015-12-26 06:16:36510 url_downloaders_.push_back(std::move(downloader));
svaldez6901c902015-12-10 18:57:33511}
512
513void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) {
514 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end();
515 ++ptr) {
516 if (ptr->get() == downloader) {
517 url_downloaders_.erase(ptr);
518 return;
519 }
520 }
521}
522
anantac82dd5be2016-08-26 01:21:53523// static
524DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest(
525 std::unique_ptr<net::URLRequest> url_request,
526 const Referrer& referrer,
527 ResourceContext* resource_context,
528 bool is_content_initiated,
529 int render_process_id,
530 int render_view_route_id,
531 int render_frame_route_id,
532 bool do_not_prompt_for_login) {
533 if (ResourceDispatcherHostImpl::Get()->is_shutdown())
534 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
535
536 // The URLRequest needs to be initialized with the referrer and other
537 // information prior to issuing it.
538 ResourceDispatcherHostImpl::Get()->InitializeURLRequest(
539 url_request.get(), referrer,
540 true, // download.
541 render_process_id, render_view_route_id, render_frame_route_id,
542 resource_context);
543
544 // We treat a download as a main frame load, and thus update the policy URL on
545 // redirects.
546 //
547 // TODO(davidben): Is this correct? If this came from a
548 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
549 // appropriately?
550 url_request->set_first_party_url_policy(
551 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
552
553 const GURL& url = url_request->original_url();
554
555 // Check if the renderer is permitted to request the requested URL.
556 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
557 render_process_id, url)) {
558 DVLOG(1) << "Denied unauthorized download request for "
559 << url.possibly_invalid_spec();
560 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
561 }
562
563 const net::URLRequestContext* request_context = url_request->context();
564 if (!request_context->job_factory()->IsHandledURL(url)) {
565 DVLOG(1) << "Download request for unsupported protocol: "
566 << url.possibly_invalid_spec();
567 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
568 }
569
570 // From this point forward, the |DownloadResourceHandler| is responsible for
571 // |started_callback|.
572 // TODO(ananta)
573 // Find a better way to create the DownloadResourceHandler instance.
574 std::unique_ptr<ResourceHandler> handler(
575 DownloadResourceHandler::Create(url_request.get()));
576
577 ResourceDispatcherHostImpl::Get()->BeginURLRequest(
578 std::move(url_request), std::move(handler), true, // download
579 is_content_initiated, do_not_prompt_for_login, resource_context);
580 return DOWNLOAD_INTERRUPT_REASON_NONE;
581}
582
ttr31481dc54b2015-08-06 20:11:26583namespace {
584
msramekd9c162a2016-02-23 11:17:21585bool EmptyFilter(const GURL& url) {
586 return true;
ttr31481dc54b2015-08-06 20:11:26587}
588
msramekd9c162a2016-02-23 11:17:21589bool RemoveDownloadByURLAndTime(
590 const base::Callback<bool(const GURL&)>& url_filter,
591 base::Time remove_begin,
592 base::Time remove_end,
593 const DownloadItemImpl* download_item) {
594 return url_filter.Run(download_item->GetURL()) &&
595 download_item->GetStartTime() >= remove_begin &&
596 (remove_end.is_null() || download_item->GetStartTime() < remove_end);
ttr31481dc54b2015-08-06 20:11:26597}
598
599} // namespace
600
601int DownloadManagerImpl::RemoveDownloads(const DownloadRemover& remover) {
[email protected]237af6d2013-03-15 23:45:28602 int count = 0;
603 DownloadMap::const_iterator it = downloads_.begin();
604 while (it != downloads_.end()) {
[email protected]db1d8f72012-07-13 19:23:16605 DownloadItemImpl* download = it->second;
[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 &&
611 remover.Run(download)) {
[email protected]237af6d2013-03-15 23:45:28612 download->Remove();
613 count++;
initial.commit09911bf2008-07-26 23:55:29614 }
initial.commit09911bf2008-07-26 23:55:29615 }
[email protected]237af6d2013-03-15 23:45:28616 return count;
initial.commit09911bf2008-07-26 23:55:29617}
618
msramekd9c162a2016-02-23 11:17:21619int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
620 const base::Callback<bool(const GURL&)>& url_filter,
ttr31481dc54b2015-08-06 20:11:26621 base::Time remove_begin,
622 base::Time remove_end) {
msramekd9c162a2016-02-23 11:17:21623 return RemoveDownloads(base::Bind(&RemoveDownloadByURLAndTime,
624 url_filter,
625 remove_begin, remove_end));
ttr31481dc54b2015-08-06 20:11:26626}
627
[email protected]5656f8a2011-11-17 16:12:58628int DownloadManagerImpl::RemoveAllDownloads() {
msramekd9c162a2016-02-23 11:17:21629 const base::Callback<bool(const GURL&)> empty_filter =
630 base::Bind(&EmptyFilter);
[email protected]d41355e6f2009-04-07 21:21:12631 // The null times make the date range unbounded.
msramekd9c162a2016-02-23 11:17:21632 int num_deleted = RemoveDownloadsByURLAndTime(
633 empty_filter, base::Time(), base::Time());
[email protected]35869622012-10-26 23:23:55634 RecordClearAllSize(num_deleted);
[email protected]09ea72c7422012-07-02 20:35:40635 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12636}
637
[email protected]0d4e30c2012-01-28 00:47:53638void DownloadManagerImpl::DownloadUrl(
dchengf6c40582016-04-09 00:05:13639 std::unique_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14640 if (params->post_id() >= 0) {
641 // Check this here so that the traceback is more useful.
642 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43643 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14644 }
svaldez6901c902015-12-10 18:57:33645 BrowserThread::PostTaskAndReplyWithResult(
646 BrowserThread::IO, FROM_HERE,
647 base::Bind(&BeginDownload, base::Passed(&params),
asanka3b4be122016-05-11 04:27:57648 browser_context_->GetResourceContext(),
svaldez6901c902015-12-10 18:57:33649 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()),
650 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
651 weak_factory_.GetWeakPtr()));
initial.commit09911bf2008-07-26 23:55:29652}
653
[email protected]5656f8a2011-11-17 16:12:58654void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29655 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29656}
657
[email protected]5656f8a2011-11-17 16:12:58658void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29659 observers_.RemoveObserver(observer);
660}
661
[email protected]3d95e542012-11-20 00:52:08662DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11663 const std::string& guid,
avib7348942015-12-25 20:57:10664 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34665 const base::FilePath& current_path,
666 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15667 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08668 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57669 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28670 const GURL& tab_url,
671 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45672 const std::string& mime_type,
673 const std::string& original_mime_type,
[email protected]3d95e542012-11-20 00:52:08674 const base::Time& start_time,
675 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21676 const std::string& etag,
677 const std::string& last_modified,
avib7348942015-12-25 20:57:10678 int64_t received_bytes,
679 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57680 const std::string& hash,
[email protected]3d95e542012-11-20 00:52:08681 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15682 DownloadDangerType danger_type,
683 DownloadInterruptReason interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08684 bool opened) {
skyostil66bd67912016-08-12 12:33:11685 if (base::ContainsKey(downloads_, id)) {
[email protected]6065748f2013-10-29 01:07:43686 NOTREACHED();
[email protected]530047e2013-07-12 17:02:25687 return NULL;
[email protected]6065748f2013-10-29 01:07:43688 }
skyostil66bd67912016-08-12 12:33:11689 DCHECK(!base::ContainsKey(downloads_by_guid_, guid));
[email protected]3d95e542012-11-20 00:52:08690 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
asanka038d58d2016-04-14 00:29:28691 this, guid, id, current_path, target_path, url_chain, referrer_url,
asanka3b4be122016-05-11 04:27:57692 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type,
693 start_time, end_time, etag, last_modified, received_bytes, total_bytes,
694 hash, state, danger_type, interrupt_reason, opened,
tfarina42834112016-09-22 13:38:20695 net::NetLogWithSource::Make(net_log_, net::NetLogSourceType::DOWNLOAD));
[email protected]530047e2013-07-12 17:02:25696 downloads_[id] = item;
asankaeef62b02016-03-14 21:23:11697 downloads_by_guid_[guid] = item;
[email protected]3d95e542012-11-20 00:52:08698 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
pkastingf5279482016-07-27 02:18:20699 DVLOG(20) << __func__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08700 return item;
initial.commit09911bf2008-07-26 23:55:29701}
702
[email protected]5656f8a2011-11-17 16:12:58703int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56704 int count = 0;
lukasza921fabfa2016-02-05 19:51:48705 for (const auto& it : downloads_) {
706 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56707 ++count;
708 }
709 return count;
[email protected]5656f8a2011-11-17 16:12:58710}
711
[email protected]422a7d12013-10-21 12:10:42712int DownloadManagerImpl::NonMaliciousInProgressCount() const {
713 int count = 0;
lukasza921fabfa2016-02-05 19:51:48714 for (const auto& it : downloads_) {
715 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
716 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
717 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
718 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
719 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59720 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42721 ++count;
722 }
723 }
724 return count;
725}
726
avib7348942015-12-25 20:57:10727DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
skyostil66bd67912016-08-12 12:33:11728 return base::ContainsKey(downloads_, download_id) ? downloads_[download_id]
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
[email protected]9f1f4092012-09-10 21:18:04738void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48739 for (const auto& it : downloads_) {
740 downloads->push_back(it.second);
[email protected]9f1f4092012-09-10 21:18:04741 }
742}
743
[email protected]ffc0c2d2013-01-21 15:32:12744void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06745 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48746 for (const auto& it : downloads_) {
747 DownloadItemImpl* item = it.second;
[email protected]803036e02013-06-15 17:08:47748 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40749 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06750 ++num_unopened;
751 }
[email protected]35869622012-10-26 23:23:55752 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12753
[email protected]0baf5922013-01-30 13:55:18754 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12755 delegate_->OpenDownload(download);
756}
757
758void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
759 if (delegate_)
760 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06761}
[email protected]5656f8a2011-11-17 16:12:58762
[email protected]35869622012-10-26 23:23:55763} // namespace content