blob: dd0827e14c9654f086e1c500e6155b4911efda5e [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"
[email protected]71bf3f5e2011-08-15 21:05:2225#include "content/browser/download/download_create_info.h"
[email protected]53ac00e82012-10-18 20:59:2026#include "content/browser/download/download_file_factory.h"
27#include "content/browser/download/download_item_factory.h"
[email protected]c09a8fd2011-11-21 19:54:5028#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0629#include "content/browser/download/download_stats.h"
[email protected]678c0362012-12-05 08:02:4430#include "content/browser/loader/resource_dispatcher_host_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3231#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2932#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1133#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4434#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3435#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1736#include "content/public/browser/download_manager_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1437#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0738#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1639#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3040#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1341#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2742#include "content/public/browser/web_contents_delegate.h"
[email protected]8d68a3e02013-01-12 15:57:1043#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0744#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1445#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1946#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2847#include "net/base/upload_bytes_element_reader.h"
[email protected]eb795632012-09-01 00:19:3148#include "net/url_request/url_request_context.h"
asanka6150c522016-03-25 21:40:5749#include "storage/browser/blob/blob_url_request_job_factory.h"
ttr31481dc54b2015-08-06 20:11:2650#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2951
[email protected]35869622012-10-26 23:23:5552namespace content {
[email protected]a0ce3282011-08-19 20:49:5253namespace {
54
dchengf6c40582016-04-09 00:05:1355std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload(
56 std::unique_ptr<DownloadUrlParameters> params,
asanka3b4be122016-05-11 04:27:5757 content::ResourceContext* resource_context,
avib7348942015-12-25 20:57:1058 uint32_t download_id,
svaldez6901c902015-12-10 18:57:3359 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:5260 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:1061
dchengf6c40582016-04-09 00:05:1362 std::unique_ptr<net::URLRequest> url_request =
asanka00b621f2016-02-19 18:09:2363 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
dchengf6c40582016-04-09 00:05:1364 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
asanka6150c522016-03-25 21:40:5765 params->GetBlobDataHandle();
66 if (blob_data_handle) {
67 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
68 url_request.get(), std::move(blob_data_handle));
69 }
[email protected]fc179dd12013-03-16 09:35:1970
asanka00b621f2016-02-19 18:09:2371 // If there's a valid renderer process associated with the request, then the
72 // request should be driven by the ResourceLoader. Pass it over to the
73 // ResourceDispatcherHostImpl which will in turn pass it along to the
74 // ResourceLoader.
asanka3b4be122016-05-11 04:27:5775 if (params->render_process_host_id() >= 0) {
asanka00b621f2016-02-19 18:09:2376 DownloadInterruptReason reason =
77 ResourceDispatcherHostImpl::Get()->BeginDownload(
78 std::move(url_request), params->referrer(),
asanka3b4be122016-05-11 04:27:5779 params->content_initiated(), resource_context,
asanka00b621f2016-02-19 18:09:2380 params->render_process_host_id(),
81 params->render_view_host_routing_id(),
82 params->render_frame_host_routing_id(),
83 params->do_not_prompt_for_login());
84
85 // If the download was accepted, the DownloadResourceHandler is now
86 // responsible for driving the request to completion.
87 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
88 return nullptr;
89
90 // Otherwise, create an interrupted download.
dchengf6c40582016-04-09 00:05:1391 std::unique_ptr<DownloadCreateInfo> failed_created_info(
asanka00b621f2016-02-19 18:09:2392 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(),
dchengf6c40582016-04-09 00:05:1393 base::WrapUnique(new DownloadSaveInfo)));
asanka00b621f2016-02-19 18:09:2394 failed_created_info->url_chain.push_back(params->url());
95 failed_created_info->result = reason;
dchengf6c40582016-04-09 00:05:1396 std::unique_ptr<ByteStreamReader> empty_byte_stream;
asanka00b621f2016-02-19 18:09:2397 BrowserThread::PostTask(
98 BrowserThread::UI, FROM_HERE,
99 base::Bind(&DownloadManager::StartDownload, download_manager,
100 base::Passed(&failed_created_info),
101 base::Passed(&empty_byte_stream), params->callback()));
svaldez6901c902015-12-10 18:57:33102 return nullptr;
103 }
asanka00b621f2016-02-19 18:09:23104
dchengf6c40582016-04-09 00:05:13105 return std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>(
asanka00b621f2016-02-19 18:09:23106 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
107 params->referrer())
svaldez6901c902015-12-10 18:57:33108 .release());
[email protected]a0ce3282011-08-19 20:49:52109}
110
[email protected]35869622012-10-26 23:23:55111class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03112 public:
[email protected]b3756012013-03-06 17:43:02113 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58114 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03115
dchengc2282aa2014-10-21 12:07:58116 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02117 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11118 const std::string& guid,
avib7348942015-12-25 20:57:10119 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02120 const base::FilePath& current_path,
121 const base::FilePath& target_path,
122 const std::vector<GURL>& url_chain,
123 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57124 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28125 const GURL& tab_url,
126 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45127 const std::string& mime_type,
128 const std::string& original_mime_type,
[email protected]b3756012013-03-06 17:43:02129 const base::Time& start_time,
130 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21131 const std::string& etag,
132 const std::string& last_modified,
avib7348942015-12-25 20:57:10133 int64_t received_bytes,
134 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57135 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02136 DownloadItem::DownloadState state,
137 DownloadDangerType danger_type,
138 DownloadInterruptReason interrupt_reason,
139 bool opened,
mohan.reddy0673e932014-10-07 16:17:38140 const net::BoundNetLog& bound_net_log) override {
asanka038d58d2016-04-14 00:29:28141 return new DownloadItemImpl(
142 delegate, guid, download_id, current_path, target_path, url_chain,
asanka3b4be122016-05-11 04:27:57143 referrer_url, site_url, tab_url, tab_refererr_url, mime_type,
144 original_mime_type, start_time, end_time, etag, last_modified,
145 received_bytes, total_bytes, hash, state, danger_type, interrupt_reason,
146 opened, bound_net_log);
[email protected]b3756012013-03-06 17:43:02147 }
[email protected]d25fda12012-06-12 17:05:03148
dchengc2282aa2014-10-21 12:07:58149 DownloadItemImpl* CreateActiveItem(
[email protected]b3756012013-03-06 17:43:02150 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10151 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02152 const DownloadCreateInfo& info,
mohan.reddy0673e932014-10-07 16:17:38153 const net::BoundNetLog& bound_net_log) override {
[email protected]8f298352013-03-28 22:18:19154 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
[email protected]b3756012013-03-06 17:43:02155 }
[email protected]d25fda12012-06-12 17:05:03156
dchengc2282aa2014-10-21 12:07:58157 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02158 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10159 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02160 const base::FilePath& path,
161 const GURL& url,
[email protected]b3756012013-03-06 17:43:02162 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13163 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
mohan.reddy0673e932014-10-07 16:17:38164 const net::BoundNetLog& bound_net_log) override {
dcheng36b6aec92015-12-26 06:16:36165 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
166 std::move(request_handle), bound_net_log);
[email protected]b3756012013-03-06 17:43:02167 }
[email protected]d25fda12012-06-12 17:05:03168};
169
[email protected]a0ce3282011-08-19 20:49:52170} // namespace
171
[email protected]d25fda12012-06-12 17:05:03172DownloadManagerImpl::DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38173 net::NetLog* net_log,
174 BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20175 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55176 file_factory_(new DownloadFileFactory()),
[email protected]16798692013-04-23 18:08:38177 shutdown_needed_(true),
178 browser_context_(browser_context),
[email protected]33c20cd92012-06-14 22:02:50179 delegate_(NULL),
[email protected]eba4a4d2013-05-29 02:18:06180 net_log_(net_log),
181 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38182 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29183}
184
[email protected]5656f8a2011-11-17 16:12:58185DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13186 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29187}
188
[email protected]89ec81f2013-05-15 19:20:02189DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10190 uint32_t id,
191 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52192 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25193 DCHECK(!ContainsKey(downloads_, id));
[email protected]381af492013-03-28 01:56:22194 net::BoundNetLog bound_net_log =
195 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]89ec81f2013-05-15 19:20:02196 DownloadItemImpl* download =
[email protected]8f298352013-03-28 22:18:19197 item_factory_->CreateActiveItem(this, id, info, bound_net_log);
[email protected]530047e2013-07-12 17:02:25198 downloads_[id] = download;
asankaeef62b02016-03-14 21:23:11199 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02200 return download;
[email protected]381af492013-03-28 01:56:22201}
202
[email protected]530047e2013-07-12 17:02:25203void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52204 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25205 if (delegate_) {
206 delegate_->GetNextId(callback);
207 return;
[email protected]491aaa62012-06-07 03:50:18208 }
avib7348942015-12-25 20:57:10209 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25210 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53211}
212
[email protected]53ac00e82012-10-18 20:59:20213void DownloadManagerImpl::DetermineDownloadTarget(
214 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
215 // Note that this next call relies on
216 // DownloadItemImplDelegate::DownloadTargetCallback and
217 // DownloadManagerDelegate::DownloadTargetCallback having the same
218 // type. If the types ever diverge, gasket code will need to
219 // be written here.
220 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34221 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20222 // TODO(asanka): Determine a useful path if |target_path| is empty.
223 callback.Run(target_path,
224 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
[email protected]35869622012-10-26 23:23:55225 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
[email protected]53ac00e82012-10-18 20:59:20226 target_path);
227 }
[email protected]47665442012-07-27 02:31:22228}
229
[email protected]98299622013-01-03 22:26:50230bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43231 DownloadItemImpl* item, const base::Closure& complete_callback) {
232 if (!delegate_ ||
233 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50234 return true;
[email protected]18710a42a2012-10-17 19:50:43235 }
236 // Otherwise, the delegate has accepted responsibility to run the
237 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50238 return false;
[email protected]fc03de22011-12-06 23:28:12239}
240
[email protected]2dec8ec2013-02-07 19:20:34241bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
242 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18243 if (!delegate_)
244 return false;
245
[email protected]fc03de22011-12-06 23:28:12246 return delegate_->ShouldOpenFileBasedOnExtension(path);
247}
248
[email protected]bde0e4f2012-11-08 22:49:59249bool DownloadManagerImpl::ShouldOpenDownload(
250 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43251 if (!delegate_)
252 return true;
253
[email protected]bde0e4f2012-11-08 22:49:59254 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
255 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
256 // to have the same type :-}.
257 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43258}
259
[email protected]35869622012-10-26 23:23:55260void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57261 delegate_ = delegate;
262}
263
[email protected]35869622012-10-26 23:23:55264DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28265 return delegate_;
266}
267
[email protected]5656f8a2011-11-17 16:12:58268void DownloadManagerImpl::Shutdown() {
pkastingf5279482016-07-27 02:18:20269 DVLOG(20) << __func__ << "() shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13270 if (!shutdown_needed_)
271 return;
272 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29273
[email protected]75e51b52012-02-04 16:57:54274 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08275 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13276
[email protected]fe752272013-05-29 18:57:45277 // If there are in-progress downloads, cancel them. This also goes for
278 // dangerous downloads which will remain in history if they aren't explicitly
279 // accepted or discarded. Canceling will remove the intermediate download
280 // file.
lukasza921fabfa2016-02-05 19:51:48281 for (const auto& it : downloads_) {
282 DownloadItemImpl* download = it.second;
[email protected]fe752272013-05-29 18:57:45283 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17284 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29285 }
[email protected]7745ff02012-10-01 00:09:24286 STLDeleteValues(&downloads_);
asankaeef62b02016-03-14 21:23:11287 downloads_by_guid_.clear();
svaldez9a07ab332016-01-12 18:29:01288 url_downloaders_.clear();
initial.commit09911bf2008-07-26 23:55:29289
[email protected]41f558fb2012-01-09 22:59:58290 // We'll have nothing more to report to the observers after this point.
291 observers_.Clear();
292
[email protected]b441a8492012-06-06 14:55:57293 if (delegate_)
294 delegate_->Shutdown();
[email protected]47665442012-07-27 02:31:22295 delegate_ = NULL;
initial.commit09911bf2008-07-26 23:55:29296}
297
[email protected]530047e2013-07-12 17:02:25298void DownloadManagerImpl::StartDownload(
dchengf6c40582016-04-09 00:05:13299 std::unique_ptr<DownloadCreateInfo> info,
300 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25301 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52302 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25303 DCHECK(info);
asanka00b621f2016-02-19 18:09:23304 // |stream| is only non-nil if the download request was successful.
305 DCHECK((info->result == DOWNLOAD_INTERRUPT_REASON_NONE && stream.get()) ||
306 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && !stream.get()));
pkastingf5279482016-07-27 02:18:20307 DVLOG(20) << __func__
308 << "() result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10309 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25310 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
avib7348942015-12-25 20:57:10311 base::Callback<void(uint32_t)> got_id(base::Bind(
312 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
313 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02314 if (new_download) {
[email protected]530047e2013-07-12 17:02:25315 GetNextId(got_id);
316 } else {
317 got_id.Run(download_id);
318 }
319}
320
321void DownloadManagerImpl::StartDownloadWithId(
dchengf6c40582016-04-09 00:05:13322 std::unique_ptr<DownloadCreateInfo> info,
323 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25324 const DownloadUrlParameters::OnStartedCallback& on_started,
325 bool new_download,
avib7348942015-12-25 20:57:10326 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52327 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25328 DCHECK_NE(content::DownloadItem::kInvalidId, id);
329 DownloadItemImpl* download = NULL;
330 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02331 download = CreateActiveItem(id, *info);
332 } else {
[email protected]530047e2013-07-12 17:02:25333 DownloadMap::iterator item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02334 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37335 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47336 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02337 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37338 // removed after it was resumed. Ignore. If the download is cancelled
339 // while resuming, then also ignore the request.
asanka64114152015-12-21 21:28:49340 info->request_handle->CancelRequest();
[email protected]530047e2013-07-12 17:02:25341 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56342 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
asanka81c2214f2016-01-13 20:05:54343 // The ByteStreamReader lives and dies on the FILE thread.
asanka00b621f2016-02-19 18:09:23344 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
345 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
346 stream.release());
[email protected]530047e2013-07-12 17:02:25347 return;
[email protected]89ec81f2013-05-15 19:20:02348 }
349 download = item_iterator->second;
[email protected]89ec81f2013-05-15 19:20:02350 }
351
[email protected]2dec8ec2013-02-07 19:20:34352 base::FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01353 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34354 base::FilePath website_save_directory; // Unused
355 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01356 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20357 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01358 }
359
dchengf6c40582016-04-09 00:05:13360 std::unique_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48361
asanka00b621f2016-02-19 18:09:23362 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
363 DCHECK(stream.get());
asanka4350f6a2016-03-15 02:40:57364 download_file.reset(
365 file_factory_->CreateFile(std::move(info->save_info),
366 default_download_directory,
367 std::move(stream),
368 download->GetBoundNetLog(),
369 download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48370 }
asanka4350f6a2016-03-15 02:40:57371 // It is important to leave info->save_info intact in the case of an interrupt
372 // so that the DownloadItem can salvage what it can out of a failed resumption
373 // attempt.
[email protected]492c0092013-08-10 13:49:48374
asanka00b621f2016-02-19 18:09:23375 download->Start(std::move(download_file), std::move(info->request_handle),
376 *info);
[email protected]96c3bdbaa2012-08-31 16:39:54377
[email protected]381af492013-03-28 01:56:22378 // For interrupted downloads, Start() will transition the state to
379 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
380 // For new downloads, we notify here, rather than earlier, so that
381 // the download_file is bound to download and all the usual
382 // setters (e.g. Cancel) work.
383 if (new_download)
384 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]96c3bdbaa2012-08-31 16:39:54385
[email protected]530047e2013-07-12 17:02:25386 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56387 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35388}
389
[email protected]5656f8a2011-11-17 16:12:58390void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52391 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lukasza921fabfa2016-02-05 19:51:48392 for (const auto& it : downloads_) {
393 DownloadItemImpl* item = it.second;
[email protected]3d95e542012-11-20 00:52:08394 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48395 }
396}
397
[email protected]db1d8f72012-07-13 19:23:16398void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52399 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47400 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16401 !download_item->GetFileExternallyRemoved() &&
402 delegate_) {
403 delegate_->CheckForFileExistence(
404 download_item,
405 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
[email protected]eba4a4d2013-05-29 02:18:06406 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48407 }
408}
409
avib7348942015-12-25 20:57:10410void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id,
[email protected]4c544cd2013-01-25 07:54:16411 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52412 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16413 if (!result) { // File does not exist.
414 if (ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51415 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16416 }
[email protected]9fc114672011-06-15 08:17:48417}
418
[email protected]35869622012-10-26 23:23:55419BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58420 return browser_context_;
421}
422
[email protected]530047e2013-07-12 17:02:25423void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34424 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12425 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27426 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13427 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25428 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52429 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25430 GetNextId(base::Bind(
431 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
dcheng36b6aec92015-12-26 06:16:36432 weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type,
433 base::Passed(std::move(request_handle)), item_created));
[email protected]530047e2013-07-12 17:02:25434}
435
436void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
437 const base::FilePath& main_file_path,
438 const GURL& page_url,
439 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13440 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25441 const DownloadItemImplCreated& item_created,
avib7348942015-12-25 20:57:10442 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52443 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25444 DCHECK_NE(content::DownloadItem::kInvalidId, id);
445 DCHECK(!ContainsKey(downloads_, id));
[email protected]ef17c9a2012-02-09 05:08:09446 net::BoundNetLog bound_net_log =
447 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08448 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
dcheng36b6aec92015-12-26 06:16:36449 this, id, main_file_path, page_url, mime_type, std::move(request_handle),
[email protected]6474b112012-05-04 19:35:27450 bound_net_log);
[email protected]3d95e542012-11-20 00:52:08451 downloads_[download_item->GetId()] = download_item;
asankaeef62b02016-03-14 21:23:11452 DCHECK(!ContainsKey(downloads_by_guid_, download_item->GetGuid()));
453 downloads_by_guid_[download_item->GetGuid()] = download_item;
[email protected]3d95e542012-11-20 00:52:08454 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
455 this, download_item));
[email protected]530047e2013-07-12 17:02:25456 if (!item_created.is_null())
457 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12458}
459
[email protected]df58aa82013-01-24 21:54:00460void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
461 DownloadItem* download_item) {
462 FOR_EACH_OBSERVER(Observer, observers_,
463 OnSavePackageSuccessfullyFinished(this, download_item));
464}
465
[email protected]8d68a3e02013-01-12 15:57:10466// Resume a download of a specific URL. We send the request to the
467// ResourceDispatcherHost, and let it send us responses like a regular
468// download.
469void DownloadManagerImpl::ResumeInterruptedDownload(
dchengf6c40582016-04-09 00:05:13470 std::unique_ptr<content::DownloadUrlParameters> params,
avib7348942015-12-25 20:57:10471 uint32_t id) {
svaldez6901c902015-12-10 18:57:33472 BrowserThread::PostTaskAndReplyWithResult(
473 BrowserThread::IO, FROM_HERE,
asanka3b4be122016-05-11 04:27:57474 base::Bind(&BeginDownload, base::Passed(&params),
475 browser_context_->GetResourceContext(), id,
svaldez6901c902015-12-10 18:57:33476 weak_factory_.GetWeakPtr()),
477 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
478 weak_factory_.GetWeakPtr()));
[email protected]8d68a3e02013-01-12 15:57:10479}
480
[email protected]53ac00e82012-10-18 20:59:20481void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
dchengf6c40582016-04-09 00:05:13482 std::unique_ptr<DownloadItemFactory> item_factory) {
dcheng36b6aec92015-12-26 06:16:36483 item_factory_ = std::move(item_factory);
[email protected]53ac00e82012-10-18 20:59:20484}
485
486void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
dchengf6c40582016-04-09 00:05:13487 std::unique_ptr<DownloadFileFactory> file_factory) {
dcheng36b6aec92015-12-26 06:16:36488 file_factory_ = std::move(file_factory);
[email protected]53ac00e82012-10-18 20:59:20489}
490
[email protected]35869622012-10-26 23:23:55491DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20492 return file_factory_.get();
493}
494
[email protected]db1d8f72012-07-13 19:23:16495void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28496 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40497 return;
498
asankaeef62b02016-03-14 21:23:11499 downloads_by_guid_.erase(download->GetGuid());
500
avib7348942015-12-25 20:57:10501 uint32_t download_id = download->GetId();
[email protected]6065748f2013-10-29 01:07:43502 if (downloads_.erase(download_id) == 0)
[email protected]237af6d2013-03-15 23:45:28503 return;
[email protected]237af6d2013-03-15 23:45:28504 delete download;
initial.commit09911bf2008-07-26 23:55:29505}
506
svaldez6901c902015-12-10 18:57:33507void DownloadManagerImpl::AddUrlDownloader(
dchengf6c40582016-04-09 00:05:13508 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>
509 downloader) {
svaldez6901c902015-12-10 18:57:33510 if (downloader)
dcheng36b6aec92015-12-26 06:16:36511 url_downloaders_.push_back(std::move(downloader));
svaldez6901c902015-12-10 18:57:33512}
513
514void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) {
515 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end();
516 ++ptr) {
517 if (ptr->get() == downloader) {
518 url_downloaders_.erase(ptr);
519 return;
520 }
521 }
522}
523
ttr31481dc54b2015-08-06 20:11:26524namespace {
525
msramekd9c162a2016-02-23 11:17:21526bool EmptyFilter(const GURL& url) {
527 return true;
ttr31481dc54b2015-08-06 20:11:26528}
529
msramekd9c162a2016-02-23 11:17:21530bool RemoveDownloadByURLAndTime(
531 const base::Callback<bool(const GURL&)>& url_filter,
532 base::Time remove_begin,
533 base::Time remove_end,
534 const DownloadItemImpl* download_item) {
535 return url_filter.Run(download_item->GetURL()) &&
536 download_item->GetStartTime() >= remove_begin &&
537 (remove_end.is_null() || download_item->GetStartTime() < remove_end);
ttr31481dc54b2015-08-06 20:11:26538}
539
540} // namespace
541
542int DownloadManagerImpl::RemoveDownloads(const DownloadRemover& remover) {
[email protected]237af6d2013-03-15 23:45:28543 int count = 0;
544 DownloadMap::const_iterator it = downloads_.begin();
545 while (it != downloads_.end()) {
[email protected]db1d8f72012-07-13 19:23:16546 DownloadItemImpl* download = it->second;
[email protected]237af6d2013-03-15 23:45:28547
548 // Increment done here to protect against invalidation below.
549 ++it;
550
ttr31481dc54b2015-08-06 20:11:26551 if (download->GetState() != DownloadItem::IN_PROGRESS &&
552 remover.Run(download)) {
[email protected]237af6d2013-03-15 23:45:28553 download->Remove();
554 count++;
initial.commit09911bf2008-07-26 23:55:29555 }
initial.commit09911bf2008-07-26 23:55:29556 }
[email protected]237af6d2013-03-15 23:45:28557 return count;
initial.commit09911bf2008-07-26 23:55:29558}
559
msramekd9c162a2016-02-23 11:17:21560int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
561 const base::Callback<bool(const GURL&)>& url_filter,
ttr31481dc54b2015-08-06 20:11:26562 base::Time remove_begin,
563 base::Time remove_end) {
msramekd9c162a2016-02-23 11:17:21564 return RemoveDownloads(base::Bind(&RemoveDownloadByURLAndTime,
565 url_filter,
566 remove_begin, remove_end));
ttr31481dc54b2015-08-06 20:11:26567}
568
[email protected]5656f8a2011-11-17 16:12:58569int DownloadManagerImpl::RemoveAllDownloads() {
msramekd9c162a2016-02-23 11:17:21570 const base::Callback<bool(const GURL&)> empty_filter =
571 base::Bind(&EmptyFilter);
[email protected]d41355e6f2009-04-07 21:21:12572 // The null times make the date range unbounded.
msramekd9c162a2016-02-23 11:17:21573 int num_deleted = RemoveDownloadsByURLAndTime(
574 empty_filter, base::Time(), base::Time());
[email protected]35869622012-10-26 23:23:55575 RecordClearAllSize(num_deleted);
[email protected]09ea72c7422012-07-02 20:35:40576 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12577}
578
[email protected]0d4e30c2012-01-28 00:47:53579void DownloadManagerImpl::DownloadUrl(
dchengf6c40582016-04-09 00:05:13580 std::unique_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14581 if (params->post_id() >= 0) {
582 // Check this here so that the traceback is more useful.
583 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43584 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14585 }
svaldez6901c902015-12-10 18:57:33586 BrowserThread::PostTaskAndReplyWithResult(
587 BrowserThread::IO, FROM_HERE,
588 base::Bind(&BeginDownload, base::Passed(&params),
asanka3b4be122016-05-11 04:27:57589 browser_context_->GetResourceContext(),
svaldez6901c902015-12-10 18:57:33590 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()),
591 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
592 weak_factory_.GetWeakPtr()));
initial.commit09911bf2008-07-26 23:55:29593}
594
[email protected]5656f8a2011-11-17 16:12:58595void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29596 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29597}
598
[email protected]5656f8a2011-11-17 16:12:58599void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29600 observers_.RemoveObserver(observer);
601}
602
[email protected]3d95e542012-11-20 00:52:08603DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11604 const std::string& guid,
avib7348942015-12-25 20:57:10605 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34606 const base::FilePath& current_path,
607 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15608 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08609 const GURL& referrer_url,
asanka3b4be122016-05-11 04:27:57610 const GURL& site_url,
asanka038d58d2016-04-14 00:29:28611 const GURL& tab_url,
612 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45613 const std::string& mime_type,
614 const std::string& original_mime_type,
[email protected]3d95e542012-11-20 00:52:08615 const base::Time& start_time,
616 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21617 const std::string& etag,
618 const std::string& last_modified,
avib7348942015-12-25 20:57:10619 int64_t received_bytes,
620 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57621 const std::string& hash,
[email protected]3d95e542012-11-20 00:52:08622 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15623 DownloadDangerType danger_type,
624 DownloadInterruptReason interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08625 bool opened) {
[email protected]6065748f2013-10-29 01:07:43626 if (ContainsKey(downloads_, id)) {
627 NOTREACHED();
[email protected]530047e2013-07-12 17:02:25628 return NULL;
[email protected]6065748f2013-10-29 01:07:43629 }
asankaeef62b02016-03-14 21:23:11630 DCHECK(!ContainsKey(downloads_by_guid_, guid));
[email protected]3d95e542012-11-20 00:52:08631 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
asanka038d58d2016-04-14 00:29:28632 this, guid, id, current_path, target_path, url_chain, referrer_url,
asanka3b4be122016-05-11 04:27:57633 site_url, tab_url, tab_refererr_url, mime_type, original_mime_type,
634 start_time, end_time, etag, last_modified, received_bytes, total_bytes,
635 hash, state, danger_type, interrupt_reason, opened,
[email protected]3d95e542012-11-20 00:52:08636 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
[email protected]530047e2013-07-12 17:02:25637 downloads_[id] = item;
asankaeef62b02016-03-14 21:23:11638 downloads_by_guid_[guid] = item;
[email protected]3d95e542012-11-20 00:52:08639 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
pkastingf5279482016-07-27 02:18:20640 DVLOG(20) << __func__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08641 return item;
initial.commit09911bf2008-07-26 23:55:29642}
643
[email protected]5656f8a2011-11-17 16:12:58644int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56645 int count = 0;
lukasza921fabfa2016-02-05 19:51:48646 for (const auto& it : downloads_) {
647 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56648 ++count;
649 }
650 return count;
[email protected]5656f8a2011-11-17 16:12:58651}
652
[email protected]422a7d12013-10-21 12:10:42653int DownloadManagerImpl::NonMaliciousInProgressCount() const {
654 int count = 0;
lukasza921fabfa2016-02-05 19:51:48655 for (const auto& it : downloads_) {
656 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
657 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
658 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
659 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
660 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59661 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42662 ++count;
663 }
664 }
665 return count;
666}
667
avib7348942015-12-25 20:57:10668DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
asankaeef62b02016-03-14 21:23:11669 return ContainsKey(downloads_, download_id) ? downloads_[download_id]
670 : nullptr;
671}
672
673DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
674 DCHECK(guid == base::ToUpperASCII(guid));
675 return ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
676 : nullptr;
[email protected]ba7895d2012-06-22 19:02:52677}
678
[email protected]9f1f4092012-09-10 21:18:04679void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48680 for (const auto& it : downloads_) {
681 downloads->push_back(it.second);
[email protected]9f1f4092012-09-10 21:18:04682 }
683}
684
[email protected]ffc0c2d2013-01-21 15:32:12685void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06686 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48687 for (const auto& it : downloads_) {
688 DownloadItemImpl* item = it.second;
[email protected]803036e02013-06-15 17:08:47689 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40690 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06691 ++num_unopened;
692 }
[email protected]35869622012-10-26 23:23:55693 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12694
[email protected]0baf5922013-01-30 13:55:18695 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12696 delegate_->OpenDownload(download);
697}
698
699void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
700 if (delegate_)
701 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06702}
[email protected]5656f8a2011-11-17 16:12:58703
[email protected]35869622012-10-26 23:23:55704} // namespace content