blob: db6bc811fc318d68b4964bbabb864d9fcbc6ba2e [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,
avib7348942015-12-25 20:57:1057 uint32_t download_id,
svaldez6901c902015-12-10 18:57:3358 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:5259 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:1060
dchengf6c40582016-04-09 00:05:1361 std::unique_ptr<net::URLRequest> url_request =
asanka00b621f2016-02-19 18:09:2362 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
dchengf6c40582016-04-09 00:05:1363 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
asanka6150c522016-03-25 21:40:5764 params->GetBlobDataHandle();
65 if (blob_data_handle) {
66 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
67 url_request.get(), std::move(blob_data_handle));
68 }
[email protected]fc179dd12013-03-16 09:35:1969
asanka00b621f2016-02-19 18:09:2370 // If there's a valid renderer process associated with the request, then the
71 // request should be driven by the ResourceLoader. Pass it over to the
72 // ResourceDispatcherHostImpl which will in turn pass it along to the
73 // ResourceLoader.
svaldez6901c902015-12-10 18:57:3374 if (params->render_process_host_id() != -1) {
asanka00b621f2016-02-19 18:09:2375 DownloadInterruptReason reason =
76 ResourceDispatcherHostImpl::Get()->BeginDownload(
77 std::move(url_request), params->referrer(),
78 params->content_initiated(), params->resource_context(),
79 params->render_process_host_id(),
80 params->render_view_host_routing_id(),
81 params->render_frame_host_routing_id(),
82 params->do_not_prompt_for_login());
83
84 // If the download was accepted, the DownloadResourceHandler is now
85 // responsible for driving the request to completion.
86 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
87 return nullptr;
88
89 // Otherwise, create an interrupted download.
dchengf6c40582016-04-09 00:05:1390 std::unique_ptr<DownloadCreateInfo> failed_created_info(
asanka00b621f2016-02-19 18:09:2391 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(),
dchengf6c40582016-04-09 00:05:1392 base::WrapUnique(new DownloadSaveInfo)));
asanka00b621f2016-02-19 18:09:2393 failed_created_info->url_chain.push_back(params->url());
94 failed_created_info->result = reason;
dchengf6c40582016-04-09 00:05:1395 std::unique_ptr<ByteStreamReader> empty_byte_stream;
asanka00b621f2016-02-19 18:09:2396 BrowserThread::PostTask(
97 BrowserThread::UI, FROM_HERE,
98 base::Bind(&DownloadManager::StartDownload, download_manager,
99 base::Passed(&failed_created_info),
100 base::Passed(&empty_byte_stream), params->callback()));
svaldez6901c902015-12-10 18:57:33101 return nullptr;
102 }
asanka00b621f2016-02-19 18:09:23103
dchengf6c40582016-04-09 00:05:13104 return std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>(
asanka00b621f2016-02-19 18:09:23105 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
106 params->referrer())
svaldez6901c902015-12-10 18:57:33107 .release());
[email protected]a0ce3282011-08-19 20:49:52108}
109
[email protected]35869622012-10-26 23:23:55110class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03111 public:
[email protected]b3756012013-03-06 17:43:02112 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58113 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03114
dchengc2282aa2014-10-21 12:07:58115 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02116 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11117 const std::string& guid,
avib7348942015-12-25 20:57:10118 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02119 const base::FilePath& current_path,
120 const base::FilePath& target_path,
121 const std::vector<GURL>& url_chain,
122 const GURL& referrer_url,
asanka038d58d2016-04-14 00:29:28123 const GURL& tab_url,
124 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45125 const std::string& mime_type,
126 const std::string& original_mime_type,
[email protected]b3756012013-03-06 17:43:02127 const base::Time& start_time,
128 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21129 const std::string& etag,
130 const std::string& last_modified,
avib7348942015-12-25 20:57:10131 int64_t received_bytes,
132 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57133 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02134 DownloadItem::DownloadState state,
135 DownloadDangerType danger_type,
136 DownloadInterruptReason interrupt_reason,
137 bool opened,
mohan.reddy0673e932014-10-07 16:17:38138 const net::BoundNetLog& bound_net_log) override {
asanka038d58d2016-04-14 00:29:28139 return new DownloadItemImpl(
140 delegate, guid, download_id, current_path, target_path, url_chain,
141 referrer_url, tab_url, tab_refererr_url, mime_type, original_mime_type,
142 start_time, end_time, etag, last_modified, received_bytes, total_bytes,
143 hash, state, danger_type, interrupt_reason, opened, bound_net_log);
[email protected]b3756012013-03-06 17:43:02144 }
[email protected]d25fda12012-06-12 17:05:03145
dchengc2282aa2014-10-21 12:07:58146 DownloadItemImpl* CreateActiveItem(
[email protected]b3756012013-03-06 17:43:02147 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10148 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02149 const DownloadCreateInfo& info,
mohan.reddy0673e932014-10-07 16:17:38150 const net::BoundNetLog& bound_net_log) override {
[email protected]8f298352013-03-28 22:18:19151 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
[email protected]b3756012013-03-06 17:43:02152 }
[email protected]d25fda12012-06-12 17:05:03153
dchengc2282aa2014-10-21 12:07:58154 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02155 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10156 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02157 const base::FilePath& path,
158 const GURL& url,
[email protected]b3756012013-03-06 17:43:02159 const std::string& mime_type,
dchengf6c40582016-04-09 00:05:13160 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
mohan.reddy0673e932014-10-07 16:17:38161 const net::BoundNetLog& bound_net_log) override {
dcheng36b6aec92015-12-26 06:16:36162 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
163 std::move(request_handle), bound_net_log);
[email protected]b3756012013-03-06 17:43:02164 }
[email protected]d25fda12012-06-12 17:05:03165};
166
[email protected]a0ce3282011-08-19 20:49:52167} // namespace
168
[email protected]d25fda12012-06-12 17:05:03169DownloadManagerImpl::DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38170 net::NetLog* net_log,
171 BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20172 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55173 file_factory_(new DownloadFileFactory()),
[email protected]09ea72c7422012-07-02 20:35:40174 history_size_(0),
[email protected]16798692013-04-23 18:08:38175 shutdown_needed_(true),
176 browser_context_(browser_context),
[email protected]33c20cd92012-06-14 22:02:50177 delegate_(NULL),
[email protected]eba4a4d2013-05-29 02:18:06178 net_log_(net_log),
179 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38180 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29181}
182
[email protected]5656f8a2011-11-17 16:12:58183DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13184 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29185}
186
[email protected]89ec81f2013-05-15 19:20:02187DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10188 uint32_t id,
189 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52190 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25191 DCHECK(!ContainsKey(downloads_, id));
[email protected]381af492013-03-28 01:56:22192 net::BoundNetLog bound_net_log =
193 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]89ec81f2013-05-15 19:20:02194 DownloadItemImpl* download =
[email protected]8f298352013-03-28 22:18:19195 item_factory_->CreateActiveItem(this, id, info, bound_net_log);
[email protected]530047e2013-07-12 17:02:25196 downloads_[id] = download;
asankaeef62b02016-03-14 21:23:11197 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02198 return download;
[email protected]381af492013-03-28 01:56:22199}
200
[email protected]530047e2013-07-12 17:02:25201void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52202 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25203 if (delegate_) {
204 delegate_->GetNextId(callback);
205 return;
[email protected]491aaa62012-06-07 03:50:18206 }
avib7348942015-12-25 20:57:10207 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25208 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53209}
210
[email protected]53ac00e82012-10-18 20:59:20211void DownloadManagerImpl::DetermineDownloadTarget(
212 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
213 // Note that this next call relies on
214 // DownloadItemImplDelegate::DownloadTargetCallback and
215 // DownloadManagerDelegate::DownloadTargetCallback having the same
216 // type. If the types ever diverge, gasket code will need to
217 // be written here.
218 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34219 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20220 // TODO(asanka): Determine a useful path if |target_path| is empty.
221 callback.Run(target_path,
222 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
[email protected]35869622012-10-26 23:23:55223 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
[email protected]53ac00e82012-10-18 20:59:20224 target_path);
225 }
[email protected]47665442012-07-27 02:31:22226}
227
[email protected]98299622013-01-03 22:26:50228bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43229 DownloadItemImpl* item, const base::Closure& complete_callback) {
230 if (!delegate_ ||
231 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50232 return true;
[email protected]18710a42a2012-10-17 19:50:43233 }
234 // Otherwise, the delegate has accepted responsibility to run the
235 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50236 return false;
[email protected]fc03de22011-12-06 23:28:12237}
238
[email protected]2dec8ec2013-02-07 19:20:34239bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
240 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18241 if (!delegate_)
242 return false;
243
[email protected]fc03de22011-12-06 23:28:12244 return delegate_->ShouldOpenFileBasedOnExtension(path);
245}
246
[email protected]bde0e4f2012-11-08 22:49:59247bool DownloadManagerImpl::ShouldOpenDownload(
248 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43249 if (!delegate_)
250 return true;
251
[email protected]bde0e4f2012-11-08 22:49:59252 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
253 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
254 // to have the same type :-}.
255 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43256}
257
[email protected]35869622012-10-26 23:23:55258void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57259 delegate_ = delegate;
260}
261
[email protected]35869622012-10-26 23:23:55262DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28263 return delegate_;
264}
265
[email protected]5656f8a2011-11-17 16:12:58266void DownloadManagerImpl::Shutdown() {
anujk.sharmad9a28742014-11-12 18:27:14267 DVLOG(20) << __FUNCTION__ << "()"
268 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13269 if (!shutdown_needed_)
270 return;
271 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29272
[email protected]75e51b52012-02-04 16:57:54273 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08274 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13275
[email protected]fe752272013-05-29 18:57:45276 // If there are in-progress downloads, cancel them. This also goes for
277 // dangerous downloads which will remain in history if they aren't explicitly
278 // accepted or discarded. Canceling will remove the intermediate download
279 // file.
lukasza921fabfa2016-02-05 19:51:48280 for (const auto& it : downloads_) {
281 DownloadItemImpl* download = it.second;
[email protected]fe752272013-05-29 18:57:45282 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17283 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29284 }
[email protected]7745ff02012-10-01 00:09:24285 STLDeleteValues(&downloads_);
asankaeef62b02016-03-14 21:23:11286 downloads_by_guid_.clear();
svaldez9a07ab332016-01-12 18:29:01287 url_downloaders_.clear();
initial.commit09911bf2008-07-26 23:55:29288
[email protected]41f558fb2012-01-09 22:59:58289 // We'll have nothing more to report to the observers after this point.
290 observers_.Clear();
291
[email protected]b441a8492012-06-06 14:55:57292 if (delegate_)
293 delegate_->Shutdown();
[email protected]47665442012-07-27 02:31:22294 delegate_ = NULL;
initial.commit09911bf2008-07-26 23:55:29295}
296
[email protected]530047e2013-07-12 17:02:25297void DownloadManagerImpl::StartDownload(
dchengf6c40582016-04-09 00:05:13298 std::unique_ptr<DownloadCreateInfo> info,
299 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25300 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52301 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25302 DCHECK(info);
asanka00b621f2016-02-19 18:09:23303 // |stream| is only non-nil if the download request was successful.
304 DCHECK((info->result == DOWNLOAD_INTERRUPT_REASON_NONE && stream.get()) ||
305 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && !stream.get()));
306 DVLOG(20) << __FUNCTION__ << "()"
307 << " result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10308 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25309 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
avib7348942015-12-25 20:57:10310 base::Callback<void(uint32_t)> got_id(base::Bind(
311 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
312 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02313 if (new_download) {
[email protected]530047e2013-07-12 17:02:25314 GetNextId(got_id);
315 } else {
316 got_id.Run(download_id);
317 }
318}
319
320void DownloadManagerImpl::StartDownloadWithId(
dchengf6c40582016-04-09 00:05:13321 std::unique_ptr<DownloadCreateInfo> info,
322 std::unique_ptr<ByteStreamReader> stream,
[email protected]530047e2013-07-12 17:02:25323 const DownloadUrlParameters::OnStartedCallback& on_started,
324 bool new_download,
avib7348942015-12-25 20:57:10325 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52326 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25327 DCHECK_NE(content::DownloadItem::kInvalidId, id);
328 DownloadItemImpl* download = NULL;
329 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02330 download = CreateActiveItem(id, *info);
331 } else {
[email protected]530047e2013-07-12 17:02:25332 DownloadMap::iterator item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02333 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37334 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47335 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02336 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37337 // removed after it was resumed. Ignore. If the download is cancelled
338 // while resuming, then also ignore the request.
asanka64114152015-12-21 21:28:49339 info->request_handle->CancelRequest();
[email protected]530047e2013-07-12 17:02:25340 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56341 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
asanka81c2214f2016-01-13 20:05:54342 // The ByteStreamReader lives and dies on the FILE thread.
asanka00b621f2016-02-19 18:09:23343 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
344 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
345 stream.release());
[email protected]530047e2013-07-12 17:02:25346 return;
[email protected]89ec81f2013-05-15 19:20:02347 }
348 download = item_iterator->second;
[email protected]89ec81f2013-05-15 19:20:02349 }
350
[email protected]2dec8ec2013-02-07 19:20:34351 base::FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01352 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34353 base::FilePath website_save_directory; // Unused
354 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01355 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20356 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01357 }
358
dchengf6c40582016-04-09 00:05:13359 std::unique_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48360
asanka00b621f2016-02-19 18:09:23361 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
362 DCHECK(stream.get());
asanka4350f6a2016-03-15 02:40:57363 download_file.reset(
364 file_factory_->CreateFile(std::move(info->save_info),
365 default_download_directory,
366 std::move(stream),
367 download->GetBoundNetLog(),
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.
413 if (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);
444 DCHECK(!ContainsKey(downloads_, id));
[email protected]ef17c9a2012-02-09 05:08:09445 net::BoundNetLog bound_net_log =
446 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_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),
[email protected]6474b112012-05-04 19:35:27449 bound_net_log);
[email protected]3d95e542012-11-20 00:52:08450 downloads_[download_item->GetId()] = download_item;
asankaeef62b02016-03-14 21:23:11451 DCHECK(!ContainsKey(downloads_by_guid_, download_item->GetGuid()));
452 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) {
[email protected]11325a22013-07-30 23:02:26471 RecordDownloadSource(INITIATED_BY_RESUMPTION);
svaldez6901c902015-12-10 18:57:33472 BrowserThread::PostTaskAndReplyWithResult(
473 BrowserThread::IO, FROM_HERE,
474 base::Bind(&BeginDownload, base::Passed(&params), id,
475 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
ttr31481dc54b2015-08-06 20:11:26523namespace {
524
msramekd9c162a2016-02-23 11:17:21525bool EmptyFilter(const GURL& url) {
526 return true;
ttr31481dc54b2015-08-06 20:11:26527}
528
msramekd9c162a2016-02-23 11:17:21529bool RemoveDownloadByURLAndTime(
530 const base::Callback<bool(const GURL&)>& url_filter,
531 base::Time remove_begin,
532 base::Time remove_end,
533 const DownloadItemImpl* download_item) {
534 return url_filter.Run(download_item->GetURL()) &&
535 download_item->GetStartTime() >= remove_begin &&
536 (remove_end.is_null() || download_item->GetStartTime() < remove_end);
ttr31481dc54b2015-08-06 20:11:26537}
538
539} // namespace
540
541int DownloadManagerImpl::RemoveDownloads(const DownloadRemover& remover) {
[email protected]237af6d2013-03-15 23:45:28542 int count = 0;
543 DownloadMap::const_iterator it = downloads_.begin();
544 while (it != downloads_.end()) {
[email protected]db1d8f72012-07-13 19:23:16545 DownloadItemImpl* download = it->second;
[email protected]237af6d2013-03-15 23:45:28546
547 // Increment done here to protect against invalidation below.
548 ++it;
549
ttr31481dc54b2015-08-06 20:11:26550 if (download->GetState() != DownloadItem::IN_PROGRESS &&
551 remover.Run(download)) {
[email protected]237af6d2013-03-15 23:45:28552 download->Remove();
553 count++;
initial.commit09911bf2008-07-26 23:55:29554 }
initial.commit09911bf2008-07-26 23:55:29555 }
[email protected]237af6d2013-03-15 23:45:28556 return count;
initial.commit09911bf2008-07-26 23:55:29557}
558
msramekd9c162a2016-02-23 11:17:21559int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
560 const base::Callback<bool(const GURL&)>& url_filter,
ttr31481dc54b2015-08-06 20:11:26561 base::Time remove_begin,
562 base::Time remove_end) {
msramekd9c162a2016-02-23 11:17:21563 return RemoveDownloads(base::Bind(&RemoveDownloadByURLAndTime,
564 url_filter,
565 remove_begin, remove_end));
ttr31481dc54b2015-08-06 20:11:26566}
567
[email protected]5656f8a2011-11-17 16:12:58568int DownloadManagerImpl::RemoveAllDownloads() {
msramekd9c162a2016-02-23 11:17:21569 const base::Callback<bool(const GURL&)> empty_filter =
570 base::Bind(&EmptyFilter);
[email protected]d41355e6f2009-04-07 21:21:12571 // The null times make the date range unbounded.
msramekd9c162a2016-02-23 11:17:21572 int num_deleted = RemoveDownloadsByURLAndTime(
573 empty_filter, base::Time(), base::Time());
[email protected]35869622012-10-26 23:23:55574 RecordClearAllSize(num_deleted);
[email protected]09ea72c7422012-07-02 20:35:40575 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12576}
577
[email protected]0d4e30c2012-01-28 00:47:53578void DownloadManagerImpl::DownloadUrl(
dchengf6c40582016-04-09 00:05:13579 std::unique_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14580 if (params->post_id() >= 0) {
581 // Check this here so that the traceback is more useful.
582 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43583 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14584 }
svaldez6901c902015-12-10 18:57:33585 BrowserThread::PostTaskAndReplyWithResult(
586 BrowserThread::IO, FROM_HERE,
587 base::Bind(&BeginDownload, base::Passed(&params),
588 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()),
589 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
590 weak_factory_.GetWeakPtr()));
initial.commit09911bf2008-07-26 23:55:29591}
592
[email protected]5656f8a2011-11-17 16:12:58593void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29594 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29595}
596
[email protected]5656f8a2011-11-17 16:12:58597void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29598 observers_.RemoveObserver(observer);
599}
600
[email protected]3d95e542012-11-20 00:52:08601DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11602 const std::string& guid,
avib7348942015-12-25 20:57:10603 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34604 const base::FilePath& current_path,
605 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15606 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08607 const GURL& referrer_url,
asanka038d58d2016-04-14 00:29:28608 const GURL& tab_url,
609 const GURL& tab_refererr_url,
[email protected]0e648562014-06-12 19:39:45610 const std::string& mime_type,
611 const std::string& original_mime_type,
[email protected]3d95e542012-11-20 00:52:08612 const base::Time& start_time,
613 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21614 const std::string& etag,
615 const std::string& last_modified,
avib7348942015-12-25 20:57:10616 int64_t received_bytes,
617 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57618 const std::string& hash,
[email protected]3d95e542012-11-20 00:52:08619 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15620 DownloadDangerType danger_type,
621 DownloadInterruptReason interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08622 bool opened) {
[email protected]6065748f2013-10-29 01:07:43623 if (ContainsKey(downloads_, id)) {
624 NOTREACHED();
[email protected]530047e2013-07-12 17:02:25625 return NULL;
[email protected]6065748f2013-10-29 01:07:43626 }
asankaeef62b02016-03-14 21:23:11627 DCHECK(!ContainsKey(downloads_by_guid_, guid));
[email protected]3d95e542012-11-20 00:52:08628 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
asanka038d58d2016-04-14 00:29:28629 this, guid, id, current_path, target_path, url_chain, referrer_url,
630 tab_url, tab_refererr_url, mime_type, original_mime_type, start_time,
631 end_time, etag, last_modified, received_bytes, total_bytes, hash, state,
632 danger_type, interrupt_reason, opened,
[email protected]3d95e542012-11-20 00:52:08633 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
[email protected]530047e2013-07-12 17:02:25634 downloads_[id] = item;
asankaeef62b02016-03-14 21:23:11635 downloads_by_guid_[guid] = item;
[email protected]3d95e542012-11-20 00:52:08636 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
anujk.sharmad9a28742014-11-12 18:27:14637 DVLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08638 return item;
initial.commit09911bf2008-07-26 23:55:29639}
640
[email protected]5656f8a2011-11-17 16:12:58641int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56642 int count = 0;
lukasza921fabfa2016-02-05 19:51:48643 for (const auto& it : downloads_) {
644 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56645 ++count;
646 }
647 return count;
[email protected]5656f8a2011-11-17 16:12:58648}
649
[email protected]422a7d12013-10-21 12:10:42650int DownloadManagerImpl::NonMaliciousInProgressCount() const {
651 int count = 0;
lukasza921fabfa2016-02-05 19:51:48652 for (const auto& it : downloads_) {
653 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
654 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
655 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
656 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
657 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59658 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42659 ++count;
660 }
661 }
662 return count;
663}
664
avib7348942015-12-25 20:57:10665DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
asankaeef62b02016-03-14 21:23:11666 return ContainsKey(downloads_, download_id) ? downloads_[download_id]
667 : nullptr;
668}
669
670DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
671 DCHECK(guid == base::ToUpperASCII(guid));
672 return ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
673 : nullptr;
[email protected]ba7895d2012-06-22 19:02:52674}
675
[email protected]9f1f4092012-09-10 21:18:04676void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48677 for (const auto& it : downloads_) {
678 downloads->push_back(it.second);
[email protected]9f1f4092012-09-10 21:18:04679 }
680}
681
[email protected]ffc0c2d2013-01-21 15:32:12682void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06683 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48684 for (const auto& it : downloads_) {
685 DownloadItemImpl* item = it.second;
[email protected]803036e02013-06-15 17:08:47686 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40687 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06688 ++num_unopened;
689 }
[email protected]35869622012-10-26 23:23:55690 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12691
[email protected]0baf5922013-01-30 13:55:18692 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12693 delegate_->OpenDownload(download);
694}
695
696void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
697 if (delegate_)
698 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06699}
[email protected]5656f8a2011-11-17 16:12:58700
[email protected]35869622012-10-26 23:23:55701} // namespace content