blob: ca423c4aaf480ce34319446c129482e59c1bb70c [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"
svaldez6901c902015-12-10 18:57:3315#include "base/memory/weak_ptr.h"
[email protected]95f861e2013-07-18 02:07:1716#include "base/message_loop/message_loop.h"
[email protected]7286e3fc2011-07-19 22:13:2417#include "base/stl_util.h"
[email protected]10994d132013-06-11 07:16:1818#include "base/strings/stringprintf.h"
[email protected]40d11e02013-03-28 17:43:1419#include "base/strings/sys_string_conversions.h"
[email protected]d59d40c2012-08-08 18:18:3720#include "base/supports_user_data.h"
[email protected]eda58402011-09-21 19:32:0221#include "base/synchronization/lock.h"
[email protected]d2a8fb72010-01-21 05:31:4222#include "build/build_config.h"
[email protected]f8e92b5d2013-03-21 18:35:4623#include "content/browser/byte_stream.h"
[email protected]71bf3f5e2011-08-15 21:05:2224#include "content/browser/download/download_create_info.h"
[email protected]53ac00e82012-10-18 20:59:2025#include "content/browser/download/download_file_factory.h"
26#include "content/browser/download/download_item_factory.h"
[email protected]c09a8fd2011-11-21 19:54:5027#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0628#include "content/browser/download/download_stats.h"
[email protected]678c0362012-12-05 08:02:4429#include "content/browser/loader/resource_dispatcher_host_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3230#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2931#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1132#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4433#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3434#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1735#include "content/public/browser/download_manager_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1436#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0737#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1638#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3039#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1340#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2741#include "content/public/browser/web_contents_delegate.h"
[email protected]8d68a3e02013-01-12 15:57:1042#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0743#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1444#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1945#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2846#include "net/base/upload_bytes_element_reader.h"
[email protected]eb795632012-09-01 00:19:3147#include "net/url_request/url_request_context.h"
ttr31481dc54b2015-08-06 20:11:2648#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2949
[email protected]35869622012-10-26 23:23:5550namespace content {
[email protected]a0ce3282011-08-19 20:49:5251namespace {
52
svaldez6901c902015-12-10 18:57:3353scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload(
54 scoped_ptr<DownloadUrlParameters> params,
avib7348942015-12-25 20:57:1055 uint32_t download_id,
svaldez6901c902015-12-10 18:57:3356 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:5257 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]8d68a3e02013-01-12 15:57:1058
asanka00b621f2016-02-19 18:09:2359 scoped_ptr<net::URLRequest> url_request =
60 DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get());
[email protected]fc179dd12013-03-16 09:35:1961
asanka00b621f2016-02-19 18:09:2362 // If there's a valid renderer process associated with the request, then the
63 // request should be driven by the ResourceLoader. Pass it over to the
64 // ResourceDispatcherHostImpl which will in turn pass it along to the
65 // ResourceLoader.
svaldez6901c902015-12-10 18:57:3366 if (params->render_process_host_id() != -1) {
asanka00b621f2016-02-19 18:09:2367 DownloadInterruptReason reason =
68 ResourceDispatcherHostImpl::Get()->BeginDownload(
69 std::move(url_request), params->referrer(),
70 params->content_initiated(), params->resource_context(),
71 params->render_process_host_id(),
72 params->render_view_host_routing_id(),
73 params->render_frame_host_routing_id(),
74 params->do_not_prompt_for_login());
75
76 // If the download was accepted, the DownloadResourceHandler is now
77 // responsible for driving the request to completion.
78 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
79 return nullptr;
80
81 // Otherwise, create an interrupted download.
82 scoped_ptr<DownloadCreateInfo> failed_created_info(
83 new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(),
84 make_scoped_ptr(new DownloadSaveInfo)));
85 failed_created_info->url_chain.push_back(params->url());
86 failed_created_info->result = reason;
87 scoped_ptr<ByteStreamReader> empty_byte_stream;
88 BrowserThread::PostTask(
89 BrowserThread::UI, FROM_HERE,
90 base::Bind(&DownloadManager::StartDownload, download_manager,
91 base::Passed(&failed_created_info),
92 base::Passed(&empty_byte_stream), params->callback()));
svaldez6901c902015-12-10 18:57:3393 return nullptr;
94 }
asanka00b621f2016-02-19 18:09:2395
svaldez6901c902015-12-10 18:57:3396 return scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>(
asanka00b621f2016-02-19 18:09:2397 UrlDownloader::BeginDownload(download_manager, std::move(url_request),
98 params->referrer())
svaldez6901c902015-12-10 18:57:3399 .release());
[email protected]a0ce3282011-08-19 20:49:52100}
101
[email protected]35869622012-10-26 23:23:55102class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03103 public:
[email protected]b3756012013-03-06 17:43:02104 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58105 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03106
dchengc2282aa2014-10-21 12:07:58107 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02108 DownloadItemImplDelegate* delegate,
asankaeef62b02016-03-14 21:23:11109 const std::string& guid,
avib7348942015-12-25 20:57:10110 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02111 const base::FilePath& current_path,
112 const base::FilePath& target_path,
113 const std::vector<GURL>& url_chain,
114 const GURL& referrer_url,
[email protected]0e648562014-06-12 19:39:45115 const std::string& mime_type,
116 const std::string& original_mime_type,
[email protected]b3756012013-03-06 17:43:02117 const base::Time& start_time,
118 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21119 const std::string& etag,
120 const std::string& last_modified,
avib7348942015-12-25 20:57:10121 int64_t received_bytes,
122 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57123 const std::string& hash,
[email protected]b3756012013-03-06 17:43:02124 DownloadItem::DownloadState state,
125 DownloadDangerType danger_type,
126 DownloadInterruptReason interrupt_reason,
127 bool opened,
mohan.reddy0673e932014-10-07 16:17:38128 const net::BoundNetLog& bound_net_log) override {
asankaeef62b02016-03-14 21:23:11129 return new DownloadItemImpl(delegate,
130 guid,
131 download_id,
132 current_path,
133 target_path,
134 url_chain,
135 referrer_url,
136 mime_type,
137 original_mime_type,
138 start_time,
139 end_time,
140 etag,
141 last_modified,
142 received_bytes,
143 total_bytes,
asanka4350f6a2016-03-15 02:40:57144 hash,
asankaeef62b02016-03-14 21:23:11145 state,
146 danger_type,
147 interrupt_reason,
148 opened,
149 bound_net_log);
[email protected]b3756012013-03-06 17:43:02150 }
[email protected]d25fda12012-06-12 17:05:03151
dchengc2282aa2014-10-21 12:07:58152 DownloadItemImpl* CreateActiveItem(
[email protected]b3756012013-03-06 17:43:02153 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10154 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02155 const DownloadCreateInfo& info,
mohan.reddy0673e932014-10-07 16:17:38156 const net::BoundNetLog& bound_net_log) override {
[email protected]8f298352013-03-28 22:18:19157 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
[email protected]b3756012013-03-06 17:43:02158 }
[email protected]d25fda12012-06-12 17:05:03159
dchengc2282aa2014-10-21 12:07:58160 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02161 DownloadItemImplDelegate* delegate,
avib7348942015-12-25 20:57:10162 uint32_t download_id,
[email protected]b3756012013-03-06 17:43:02163 const base::FilePath& path,
164 const GURL& url,
[email protected]b3756012013-03-06 17:43:02165 const std::string& mime_type,
166 scoped_ptr<DownloadRequestHandleInterface> request_handle,
mohan.reddy0673e932014-10-07 16:17:38167 const net::BoundNetLog& bound_net_log) override {
dcheng36b6aec92015-12-26 06:16:36168 return new DownloadItemImpl(delegate, download_id, path, url, mime_type,
169 std::move(request_handle), bound_net_log);
[email protected]b3756012013-03-06 17:43:02170 }
[email protected]d25fda12012-06-12 17:05:03171};
172
[email protected]a0ce3282011-08-19 20:49:52173} // namespace
174
[email protected]d25fda12012-06-12 17:05:03175DownloadManagerImpl::DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38176 net::NetLog* net_log,
177 BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20178 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55179 file_factory_(new DownloadFileFactory()),
[email protected]09ea72c7422012-07-02 20:35:40180 history_size_(0),
[email protected]16798692013-04-23 18:08:38181 shutdown_needed_(true),
182 browser_context_(browser_context),
[email protected]33c20cd92012-06-14 22:02:50183 delegate_(NULL),
[email protected]eba4a4d2013-05-29 02:18:06184 net_log_(net_log),
185 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38186 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29187}
188
[email protected]5656f8a2011-11-17 16:12:58189DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13190 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29191}
192
[email protected]89ec81f2013-05-15 19:20:02193DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
avib7348942015-12-25 20:57:10194 uint32_t id,
195 const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52196 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25197 DCHECK(!ContainsKey(downloads_, id));
[email protected]381af492013-03-28 01:56:22198 net::BoundNetLog bound_net_log =
199 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]89ec81f2013-05-15 19:20:02200 DownloadItemImpl* download =
[email protected]8f298352013-03-28 22:18:19201 item_factory_->CreateActiveItem(this, id, info, bound_net_log);
[email protected]530047e2013-07-12 17:02:25202 downloads_[id] = download;
asankaeef62b02016-03-14 21:23:11203 downloads_by_guid_[download->GetGuid()] = download;
[email protected]89ec81f2013-05-15 19:20:02204 return download;
[email protected]381af492013-03-28 01:56:22205}
206
[email protected]530047e2013-07-12 17:02:25207void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52208 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25209 if (delegate_) {
210 delegate_->GetNextId(callback);
211 return;
[email protected]491aaa62012-06-07 03:50:18212 }
avib7348942015-12-25 20:57:10213 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25214 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53215}
216
[email protected]53ac00e82012-10-18 20:59:20217void DownloadManagerImpl::DetermineDownloadTarget(
218 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
219 // Note that this next call relies on
220 // DownloadItemImplDelegate::DownloadTargetCallback and
221 // DownloadManagerDelegate::DownloadTargetCallback having the same
222 // type. If the types ever diverge, gasket code will need to
223 // be written here.
224 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34225 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20226 // TODO(asanka): Determine a useful path if |target_path| is empty.
227 callback.Run(target_path,
228 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
[email protected]35869622012-10-26 23:23:55229 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
[email protected]53ac00e82012-10-18 20:59:20230 target_path);
231 }
[email protected]47665442012-07-27 02:31:22232}
233
[email protected]98299622013-01-03 22:26:50234bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43235 DownloadItemImpl* item, const base::Closure& complete_callback) {
236 if (!delegate_ ||
237 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50238 return true;
[email protected]18710a42a2012-10-17 19:50:43239 }
240 // Otherwise, the delegate has accepted responsibility to run the
241 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50242 return false;
[email protected]fc03de22011-12-06 23:28:12243}
244
[email protected]2dec8ec2013-02-07 19:20:34245bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
246 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18247 if (!delegate_)
248 return false;
249
[email protected]fc03de22011-12-06 23:28:12250 return delegate_->ShouldOpenFileBasedOnExtension(path);
251}
252
[email protected]bde0e4f2012-11-08 22:49:59253bool DownloadManagerImpl::ShouldOpenDownload(
254 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43255 if (!delegate_)
256 return true;
257
[email protected]bde0e4f2012-11-08 22:49:59258 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
259 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
260 // to have the same type :-}.
261 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43262}
263
[email protected]35869622012-10-26 23:23:55264void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57265 delegate_ = delegate;
266}
267
[email protected]35869622012-10-26 23:23:55268DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28269 return delegate_;
270}
271
[email protected]5656f8a2011-11-17 16:12:58272void DownloadManagerImpl::Shutdown() {
anujk.sharmad9a28742014-11-12 18:27:14273 DVLOG(20) << __FUNCTION__ << "()"
274 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13275 if (!shutdown_needed_)
276 return;
277 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29278
[email protected]75e51b52012-02-04 16:57:54279 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08280 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13281
[email protected]fe752272013-05-29 18:57:45282 // If there are in-progress downloads, cancel them. This also goes for
283 // dangerous downloads which will remain in history if they aren't explicitly
284 // accepted or discarded. Canceling will remove the intermediate download
285 // file.
lukasza921fabfa2016-02-05 19:51:48286 for (const auto& it : downloads_) {
287 DownloadItemImpl* download = it.second;
[email protected]fe752272013-05-29 18:57:45288 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17289 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29290 }
[email protected]7745ff02012-10-01 00:09:24291 STLDeleteValues(&downloads_);
asankaeef62b02016-03-14 21:23:11292 downloads_by_guid_.clear();
svaldez9a07ab332016-01-12 18:29:01293 url_downloaders_.clear();
initial.commit09911bf2008-07-26 23:55:29294
[email protected]41f558fb2012-01-09 22:59:58295 // We'll have nothing more to report to the observers after this point.
296 observers_.Clear();
297
[email protected]b441a8492012-06-06 14:55:57298 if (delegate_)
299 delegate_->Shutdown();
[email protected]47665442012-07-27 02:31:22300 delegate_ = NULL;
initial.commit09911bf2008-07-26 23:55:29301}
302
[email protected]530047e2013-07-12 17:02:25303void DownloadManagerImpl::StartDownload(
[email protected]2bddd2072012-06-18 16:16:51304 scoped_ptr<DownloadCreateInfo> info,
[email protected]530047e2013-07-12 17:02:25305 scoped_ptr<ByteStreamReader> stream,
306 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52307 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25308 DCHECK(info);
asanka00b621f2016-02-19 18:09:23309 // |stream| is only non-nil if the download request was successful.
310 DCHECK((info->result == DOWNLOAD_INTERRUPT_REASON_NONE && stream.get()) ||
311 (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && !stream.get()));
312 DVLOG(20) << __FUNCTION__ << "()"
313 << " result=" << DownloadInterruptReasonToString(info->result);
avib7348942015-12-25 20:57:10314 uint32_t download_id = info->download_id;
[email protected]530047e2013-07-12 17:02:25315 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
avib7348942015-12-25 20:57:10316 base::Callback<void(uint32_t)> got_id(base::Bind(
317 &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(),
318 base::Passed(&info), base::Passed(&stream), on_started, new_download));
[email protected]89ec81f2013-05-15 19:20:02319 if (new_download) {
[email protected]530047e2013-07-12 17:02:25320 GetNextId(got_id);
321 } else {
322 got_id.Run(download_id);
323 }
324}
325
326void DownloadManagerImpl::StartDownloadWithId(
327 scoped_ptr<DownloadCreateInfo> info,
328 scoped_ptr<ByteStreamReader> stream,
329 const DownloadUrlParameters::OnStartedCallback& on_started,
330 bool new_download,
avib7348942015-12-25 20:57:10331 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52332 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25333 DCHECK_NE(content::DownloadItem::kInvalidId, id);
334 DownloadItemImpl* download = NULL;
335 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02336 download = CreateActiveItem(id, *info);
337 } else {
[email protected]530047e2013-07-12 17:02:25338 DownloadMap::iterator item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02339 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37340 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47341 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02342 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37343 // removed after it was resumed. Ignore. If the download is cancelled
344 // while resuming, then also ignore the request.
asanka64114152015-12-21 21:28:49345 info->request_handle->CancelRequest();
[email protected]530047e2013-07-12 17:02:25346 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56347 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
asanka81c2214f2016-01-13 20:05:54348 // The ByteStreamReader lives and dies on the FILE thread.
asanka00b621f2016-02-19 18:09:23349 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
350 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
351 stream.release());
[email protected]530047e2013-07-12 17:02:25352 return;
[email protected]89ec81f2013-05-15 19:20:02353 }
354 download = item_iterator->second;
[email protected]89ec81f2013-05-15 19:20:02355 }
356
[email protected]2dec8ec2013-02-07 19:20:34357 base::FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01358 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34359 base::FilePath website_save_directory; // Unused
360 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01361 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20362 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01363 }
364
asanka00b621f2016-02-19 18:09:23365 scoped_ptr<DownloadFile> download_file;
[email protected]492c0092013-08-10 13:49:48366
asanka00b621f2016-02-19 18:09:23367 if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
368 DCHECK(stream.get());
asanka4350f6a2016-03-15 02:40:57369 download_file.reset(
370 file_factory_->CreateFile(std::move(info->save_info),
371 default_download_directory,
372 std::move(stream),
373 download->GetBoundNetLog(),
374 download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48375 }
asanka4350f6a2016-03-15 02:40:57376 // It is important to leave info->save_info intact in the case of an interrupt
377 // so that the DownloadItem can salvage what it can out of a failed resumption
378 // attempt.
[email protected]492c0092013-08-10 13:49:48379
asanka00b621f2016-02-19 18:09:23380 download->Start(std::move(download_file), std::move(info->request_handle),
381 *info);
[email protected]96c3bdbaa2012-08-31 16:39:54382
[email protected]381af492013-03-28 01:56:22383 // For interrupted downloads, Start() will transition the state to
384 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
385 // For new downloads, we notify here, rather than earlier, so that
386 // the download_file is bound to download and all the usual
387 // setters (e.g. Cancel) work.
388 if (new_download)
389 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]96c3bdbaa2012-08-31 16:39:54390
[email protected]530047e2013-07-12 17:02:25391 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56392 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35393}
394
[email protected]5656f8a2011-11-17 16:12:58395void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52396 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lukasza921fabfa2016-02-05 19:51:48397 for (const auto& it : downloads_) {
398 DownloadItemImpl* item = it.second;
[email protected]3d95e542012-11-20 00:52:08399 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48400 }
401}
402
[email protected]db1d8f72012-07-13 19:23:16403void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52404 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47405 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16406 !download_item->GetFileExternallyRemoved() &&
407 delegate_) {
408 delegate_->CheckForFileExistence(
409 download_item,
410 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
[email protected]eba4a4d2013-05-29 02:18:06411 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48412 }
413}
414
avib7348942015-12-25 20:57:10415void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id,
[email protected]4c544cd2013-01-25 07:54:16416 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52417 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16418 if (!result) { // File does not exist.
419 if (ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51420 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16421 }
[email protected]9fc114672011-06-15 08:17:48422}
423
[email protected]35869622012-10-26 23:23:55424BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58425 return browser_context_;
426}
427
[email protected]530047e2013-07-12 17:02:25428void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34429 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12430 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27431 const std::string& mime_type,
[email protected]b3756012013-03-06 17:43:02432 scoped_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25433 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52434 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25435 GetNextId(base::Bind(
436 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
dcheng36b6aec92015-12-26 06:16:36437 weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type,
438 base::Passed(std::move(request_handle)), item_created));
[email protected]530047e2013-07-12 17:02:25439}
440
441void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
442 const base::FilePath& main_file_path,
443 const GURL& page_url,
444 const std::string& mime_type,
445 scoped_ptr<DownloadRequestHandleInterface> request_handle,
446 const DownloadItemImplCreated& item_created,
avib7348942015-12-25 20:57:10447 uint32_t id) {
mostynbfbcdc27a2015-03-13 17:58:52448 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25449 DCHECK_NE(content::DownloadItem::kInvalidId, id);
450 DCHECK(!ContainsKey(downloads_, id));
[email protected]ef17c9a2012-02-09 05:08:09451 net::BoundNetLog bound_net_log =
452 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08453 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
dcheng36b6aec92015-12-26 06:16:36454 this, id, main_file_path, page_url, mime_type, std::move(request_handle),
[email protected]6474b112012-05-04 19:35:27455 bound_net_log);
[email protected]3d95e542012-11-20 00:52:08456 downloads_[download_item->GetId()] = download_item;
asankaeef62b02016-03-14 21:23:11457 DCHECK(!ContainsKey(downloads_by_guid_, download_item->GetGuid()));
458 downloads_by_guid_[download_item->GetGuid()] = download_item;
[email protected]3d95e542012-11-20 00:52:08459 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
460 this, download_item));
[email protected]530047e2013-07-12 17:02:25461 if (!item_created.is_null())
462 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12463}
464
[email protected]df58aa82013-01-24 21:54:00465void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
466 DownloadItem* download_item) {
467 FOR_EACH_OBSERVER(Observer, observers_,
468 OnSavePackageSuccessfullyFinished(this, download_item));
469}
470
[email protected]8d68a3e02013-01-12 15:57:10471// Resume a download of a specific URL. We send the request to the
472// ResourceDispatcherHost, and let it send us responses like a regular
473// download.
474void DownloadManagerImpl::ResumeInterruptedDownload(
475 scoped_ptr<content::DownloadUrlParameters> params,
avib7348942015-12-25 20:57:10476 uint32_t id) {
[email protected]11325a22013-07-30 23:02:26477 RecordDownloadSource(INITIATED_BY_RESUMPTION);
svaldez6901c902015-12-10 18:57:33478 BrowserThread::PostTaskAndReplyWithResult(
479 BrowserThread::IO, FROM_HERE,
480 base::Bind(&BeginDownload, base::Passed(&params), id,
481 weak_factory_.GetWeakPtr()),
482 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
483 weak_factory_.GetWeakPtr()));
[email protected]8d68a3e02013-01-12 15:57:10484}
485
[email protected]53ac00e82012-10-18 20:59:20486void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
[email protected]35869622012-10-26 23:23:55487 scoped_ptr<DownloadItemFactory> item_factory) {
dcheng36b6aec92015-12-26 06:16:36488 item_factory_ = std::move(item_factory);
[email protected]53ac00e82012-10-18 20:59:20489}
490
491void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
[email protected]35869622012-10-26 23:23:55492 scoped_ptr<DownloadFileFactory> file_factory) {
dcheng36b6aec92015-12-26 06:16:36493 file_factory_ = std::move(file_factory);
[email protected]53ac00e82012-10-18 20:59:20494}
495
[email protected]35869622012-10-26 23:23:55496DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20497 return file_factory_.get();
498}
499
[email protected]db1d8f72012-07-13 19:23:16500void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28501 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40502 return;
503
asankaeef62b02016-03-14 21:23:11504 downloads_by_guid_.erase(download->GetGuid());
505
avib7348942015-12-25 20:57:10506 uint32_t download_id = download->GetId();
[email protected]6065748f2013-10-29 01:07:43507 if (downloads_.erase(download_id) == 0)
[email protected]237af6d2013-03-15 23:45:28508 return;
[email protected]237af6d2013-03-15 23:45:28509 delete download;
initial.commit09911bf2008-07-26 23:55:29510}
511
svaldez6901c902015-12-10 18:57:33512void DownloadManagerImpl::AddUrlDownloader(
513 scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> downloader) {
514 if (downloader)
dcheng36b6aec92015-12-26 06:16:36515 url_downloaders_.push_back(std::move(downloader));
svaldez6901c902015-12-10 18:57:33516}
517
518void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) {
519 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end();
520 ++ptr) {
521 if (ptr->get() == downloader) {
522 url_downloaders_.erase(ptr);
523 return;
524 }
525 }
526}
527
ttr31481dc54b2015-08-06 20:11:26528namespace {
529
msramekd9c162a2016-02-23 11:17:21530bool EmptyFilter(const GURL& url) {
531 return true;
ttr31481dc54b2015-08-06 20:11:26532}
533
msramekd9c162a2016-02-23 11:17:21534bool RemoveDownloadByURLAndTime(
535 const base::Callback<bool(const GURL&)>& url_filter,
536 base::Time remove_begin,
537 base::Time remove_end,
538 const DownloadItemImpl* download_item) {
539 return url_filter.Run(download_item->GetURL()) &&
540 download_item->GetStartTime() >= remove_begin &&
541 (remove_end.is_null() || download_item->GetStartTime() < remove_end);
ttr31481dc54b2015-08-06 20:11:26542}
543
544} // namespace
545
546int DownloadManagerImpl::RemoveDownloads(const DownloadRemover& remover) {
[email protected]237af6d2013-03-15 23:45:28547 int count = 0;
548 DownloadMap::const_iterator it = downloads_.begin();
549 while (it != downloads_.end()) {
[email protected]db1d8f72012-07-13 19:23:16550 DownloadItemImpl* download = it->second;
[email protected]237af6d2013-03-15 23:45:28551
552 // Increment done here to protect against invalidation below.
553 ++it;
554
ttr31481dc54b2015-08-06 20:11:26555 if (download->GetState() != DownloadItem::IN_PROGRESS &&
556 remover.Run(download)) {
[email protected]237af6d2013-03-15 23:45:28557 download->Remove();
558 count++;
initial.commit09911bf2008-07-26 23:55:29559 }
initial.commit09911bf2008-07-26 23:55:29560 }
[email protected]237af6d2013-03-15 23:45:28561 return count;
initial.commit09911bf2008-07-26 23:55:29562}
563
msramekd9c162a2016-02-23 11:17:21564int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
565 const base::Callback<bool(const GURL&)>& url_filter,
ttr31481dc54b2015-08-06 20:11:26566 base::Time remove_begin,
567 base::Time remove_end) {
msramekd9c162a2016-02-23 11:17:21568 return RemoveDownloads(base::Bind(&RemoveDownloadByURLAndTime,
569 url_filter,
570 remove_begin, remove_end));
ttr31481dc54b2015-08-06 20:11:26571}
572
[email protected]5656f8a2011-11-17 16:12:58573int DownloadManagerImpl::RemoveAllDownloads() {
msramekd9c162a2016-02-23 11:17:21574 const base::Callback<bool(const GURL&)> empty_filter =
575 base::Bind(&EmptyFilter);
[email protected]d41355e6f2009-04-07 21:21:12576 // The null times make the date range unbounded.
msramekd9c162a2016-02-23 11:17:21577 int num_deleted = RemoveDownloadsByURLAndTime(
578 empty_filter, base::Time(), base::Time());
[email protected]35869622012-10-26 23:23:55579 RecordClearAllSize(num_deleted);
[email protected]09ea72c7422012-07-02 20:35:40580 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12581}
582
[email protected]0d4e30c2012-01-28 00:47:53583void DownloadManagerImpl::DownloadUrl(
[email protected]35869622012-10-26 23:23:55584 scoped_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14585 if (params->post_id() >= 0) {
586 // Check this here so that the traceback is more useful.
587 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43588 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14589 }
svaldez6901c902015-12-10 18:57:33590 BrowserThread::PostTaskAndReplyWithResult(
591 BrowserThread::IO, FROM_HERE,
592 base::Bind(&BeginDownload, base::Passed(&params),
593 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()),
594 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
595 weak_factory_.GetWeakPtr()));
initial.commit09911bf2008-07-26 23:55:29596}
597
[email protected]5656f8a2011-11-17 16:12:58598void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29599 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29600}
601
[email protected]5656f8a2011-11-17 16:12:58602void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29603 observers_.RemoveObserver(observer);
604}
605
[email protected]3d95e542012-11-20 00:52:08606DownloadItem* DownloadManagerImpl::CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11607 const std::string& guid,
avib7348942015-12-25 20:57:10608 uint32_t id,
[email protected]2dec8ec2013-02-07 19:20:34609 const base::FilePath& current_path,
610 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15611 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08612 const GURL& referrer_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(
632 this,
asankaeef62b02016-03-14 21:23:11633 guid,
[email protected]530047e2013-07-12 17:02:25634 id,
[email protected]876774692013-02-03 17:20:15635 current_path,
636 target_path,
637 url_chain,
[email protected]3d95e542012-11-20 00:52:08638 referrer_url,
[email protected]0e648562014-06-12 19:39:45639 mime_type,
640 original_mime_type,
[email protected]3d95e542012-11-20 00:52:08641 start_time,
642 end_time,
[email protected]56cd5942013-08-08 23:53:21643 etag,
644 last_modified,
[email protected]3d95e542012-11-20 00:52:08645 received_bytes,
646 total_bytes,
asanka4350f6a2016-03-15 02:40:57647 hash,
[email protected]3d95e542012-11-20 00:52:08648 state,
[email protected]876774692013-02-03 17:20:15649 danger_type,
650 interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08651 opened,
652 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
[email protected]530047e2013-07-12 17:02:25653 downloads_[id] = item;
asankaeef62b02016-03-14 21:23:11654 downloads_by_guid_[guid] = item;
[email protected]3d95e542012-11-20 00:52:08655 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
anujk.sharmad9a28742014-11-12 18:27:14656 DVLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08657 return item;
initial.commit09911bf2008-07-26 23:55:29658}
659
[email protected]5656f8a2011-11-17 16:12:58660int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56661 int count = 0;
lukasza921fabfa2016-02-05 19:51:48662 for (const auto& it : downloads_) {
663 if (it.second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56664 ++count;
665 }
666 return count;
[email protected]5656f8a2011-11-17 16:12:58667}
668
[email protected]422a7d12013-10-21 12:10:42669int DownloadManagerImpl::NonMaliciousInProgressCount() const {
670 int count = 0;
lukasza921fabfa2016-02-05 19:51:48671 for (const auto& it : downloads_) {
672 if (it.second->GetState() == DownloadItem::IN_PROGRESS &&
673 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
674 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
675 it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
676 it.second->GetDangerType() !=
[email protected]24eb5172013-10-27 02:03:59677 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42678 ++count;
679 }
680 }
681 return count;
682}
683
avib7348942015-12-25 20:57:10684DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) {
asankaeef62b02016-03-14 21:23:11685 return ContainsKey(downloads_, download_id) ? downloads_[download_id]
686 : nullptr;
687}
688
689DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) {
690 DCHECK(guid == base::ToUpperASCII(guid));
691 return ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid]
692 : nullptr;
[email protected]ba7895d2012-06-22 19:02:52693}
694
[email protected]9f1f4092012-09-10 21:18:04695void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
lukasza921fabfa2016-02-05 19:51:48696 for (const auto& it : downloads_) {
697 downloads->push_back(it.second);
[email protected]9f1f4092012-09-10 21:18:04698 }
699}
700
[email protected]ffc0c2d2013-01-21 15:32:12701void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06702 int num_unopened = 0;
lukasza921fabfa2016-02-05 19:51:48703 for (const auto& it : downloads_) {
704 DownloadItemImpl* item = it.second;
[email protected]803036e02013-06-15 17:08:47705 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40706 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06707 ++num_unopened;
708 }
[email protected]35869622012-10-26 23:23:55709 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12710
[email protected]0baf5922013-01-30 13:55:18711 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12712 delegate_->OpenDownload(download);
713}
714
715void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
716 if (delegate_)
717 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06718}
[email protected]5656f8a2011-11-17 16:12:58719
[email protected]35869622012-10-26 23:23:55720} // namespace content