blob: 75763de1452020605e958570280d49cd79a2e063 [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>
8
[email protected]eda58402011-09-21 19:32:029#include "base/bind.h"
[email protected]2041cf342010-02-19 03:15:5910#include "base/callback.h"
[email protected]c6944272012-01-06 22:12:2811#include "base/debug/alias.h"
[email protected]503d03872011-05-06 08:36:2612#include "base/i18n/case_conversion.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/logging.h"
svaldez6901c902015-12-10 18:57:3314#include "base/memory/weak_ptr.h"
[email protected]95f861e2013-07-18 02:07:1715#include "base/message_loop/message_loop.h"
[email protected]7286e3fc2011-07-19 22:13:2416#include "base/stl_util.h"
[email protected]10994d132013-06-11 07:16:1817#include "base/strings/stringprintf.h"
[email protected]40d11e02013-03-28 17:43:1418#include "base/strings/sys_string_conversions.h"
[email protected]d59d40c2012-08-08 18:18:3719#include "base/supports_user_data.h"
[email protected]eda58402011-09-21 19:32:0220#include "base/synchronization/lock.h"
[email protected]d2a8fb72010-01-21 05:31:4221#include "build/build_config.h"
[email protected]f8e92b5d2013-03-21 18:35:4622#include "content/browser/byte_stream.h"
[email protected]71bf3f5e2011-08-15 21:05:2223#include "content/browser/download/download_create_info.h"
[email protected]53ac00e82012-10-18 20:59:2024#include "content/browser/download/download_file_factory.h"
25#include "content/browser/download/download_item_factory.h"
[email protected]c09a8fd2011-11-21 19:54:5026#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0627#include "content/browser/download/download_stats.h"
[email protected]678c0362012-12-05 08:02:4428#include "content/browser/loader/resource_dispatcher_host_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3229#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2930#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1131#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4432#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3433#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1734#include "content/public/browser/download_manager_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1435#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0736#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1637#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3038#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1339#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2740#include "content/public/browser/web_contents_delegate.h"
[email protected]8d68a3e02013-01-12 15:57:1041#include "content/public/common/referrer.h"
mmenkecbc2b712014-10-09 20:29:0742#include "net/base/elements_upload_data_stream.h"
[email protected]c5a5c0842012-05-04 20:05:1443#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1944#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2845#include "net/base/upload_bytes_element_reader.h"
[email protected]eb795632012-09-01 00:19:3146#include "net/url_request/url_request_context.h"
ttr31481dc54b2015-08-06 20:11:2647#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2948
[email protected]35869622012-10-26 23:23:5549namespace content {
[email protected]a0ce3282011-08-19 20:49:5250namespace {
51
svaldez6901c902015-12-10 18:57:3352scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload(
53 scoped_ptr<DownloadUrlParameters> params,
54 uint32 download_id,
55 base::WeakPtr<DownloadManagerImpl> download_manager) {
mostynbfbcdc27a2015-03-13 17:58:5256 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]c5a5c0842012-05-04 20:05:1457 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
58 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
59 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
[email protected]eb795632012-09-01 00:19:3160 scoped_ptr<net::URLRequest> request(
61 params->resource_context()->GetRequestContext()->CreateRequest(
davidben151423e2015-03-23 18:48:3662 params->url(), net::DEFAULT_PRIORITY, NULL));
[email protected]c5a5c0842012-05-04 20:05:1463 request->set_method(params->method());
[email protected]fdae8bf2012-11-20 04:02:4564 if (!params->post_body().empty()) {
[email protected]f288ef02012-12-15 20:28:2865 const std::string& body = params->post_body();
66 scoped_ptr<net::UploadElementReader> reader(
67 net::UploadOwnedBytesElementReader::CreateWithString(body));
mmenkecbc2b712014-10-09 20:29:0768 request->set_upload(
69 net::ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0));
[email protected]fdae8bf2012-11-20 04:02:4570 }
[email protected]c5a5c0842012-05-04 20:05:1471 if (params->post_id() >= 0) {
[email protected]27678b2a2012-02-04 22:09:1472 // The POST in this case does not have an actual body, and only works
73 // when retrieving data from cache. This is done because we don't want
74 // to do a re-POST without user consent, and currently don't have a good
75 // plan on how to display the UI for that.
[email protected]c5a5c0842012-05-04 20:05:1476 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:4377 DCHECK_EQ("POST", params->method());
olli.raula6df48b2a2015-11-26 07:40:2278 std::vector<scoped_ptr<net::UploadElementReader>> element_readers;
79 request->set_upload(make_scoped_ptr(new net::ElementsUploadDataStream(
80 std::move(element_readers), params->post_id())));
[email protected]27678b2a2012-02-04 22:09:1481 }
[email protected]8d68a3e02013-01-12 15:57:1082
83 // If we're not at the beginning of the file, retrieve only the remaining
84 // portion.
[email protected]fc179dd12013-03-16 09:35:1985 bool has_last_modified = !params->last_modified().empty();
86 bool has_etag = !params->etag().empty();
87
88 // If we've asked for a range, we want to make sure that we only
89 // get that range if our current copy of the information is good.
90 // We shouldn't be asked to continue if we don't have a verifier.
91 DCHECK(params->offset() == 0 || has_etag || has_last_modified);
92
asankaeaa4e862015-12-02 19:32:0593 if (params->offset() > 0 && (has_etag || has_last_modified)) {
[email protected]8d68a3e02013-01-12 15:57:1094 request->SetExtraRequestHeaderByName(
95 "Range",
[email protected]7d3cbc92013-03-18 22:33:0496 base::StringPrintf("bytes=%" PRId64 "-", params->offset()),
[email protected]8d68a3e02013-01-12 15:57:1097 true);
98
asankaeaa4e862015-12-02 19:32:0599 // In accordance with RFC 2616 Section 14.27, use If-Range to specify that
100 // the server return the entire entity if the validator doesn't match.
101 // Last-Modified can be used in the absence of ETag as a validator if the
102 // response headers satisfied the HttpUtil::HasStrongValidators() predicate.
103 //
104 // This function assumes that HasStrongValidators() was true and that the
105 // ETag and Last-Modified header values supplied are valid.
106 request->SetExtraRequestHeaderByName(
107 "If-Range", has_etag ? params->etag() : params->last_modified(), true);
[email protected]8d68a3e02013-01-12 15:57:10108 }
109
[email protected]35869622012-10-26 23:23:55110 for (DownloadUrlParameters::RequestHeadersType::const_iterator iter
[email protected]c5a5c0842012-05-04 20:05:14111 = params->request_headers_begin();
112 iter != params->request_headers_end();
113 ++iter) {
114 request->SetExtraRequestHeaderByName(
[email protected]6065748f2013-10-29 01:07:43115 iter->first, iter->second, false /*overwrite*/);
[email protected]c5a5c0842012-05-04 20:05:14116 }
[email protected]49d812e2012-11-06 21:18:12117
118 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo());
119 save_info->file_path = params->file_path();
120 save_info->suggested_name = params->suggested_name();
121 save_info->offset = params->offset();
122 save_info->hash_state = params->hash_state();
123 save_info->prompt_for_save_location = params->prompt();
[email protected]b32f9a12014-04-03 11:51:21124 save_info->file = params->GetFile();
[email protected]49d812e2012-11-06 21:18:12125
svaldez6901c902015-12-10 18:57:33126 if (params->render_process_host_id() != -1) {
127 ResourceDispatcherHost::Get()->BeginDownload(
asanka64114152015-12-21 21:28:49128 std::move(request), params->referrer(), params->content_initiated(),
svaldez6901c902015-12-10 18:57:33129 params->resource_context(), params->render_process_host_id(),
130 params->render_view_host_routing_id(),
131 params->render_frame_host_routing_id(), params->prefer_cache(),
asanka64114152015-12-21 21:28:49132 params->do_not_prompt_for_login(), std::move(save_info), download_id,
svaldez6901c902015-12-10 18:57:33133 params->callback());
134 return nullptr;
135 }
136 return scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>(
asanka64114152015-12-21 21:28:49137 UrlDownloader::BeginDownload(download_manager, std::move(request),
138 params->referrer(), params->prefer_cache(),
139 std::move(save_info), download_id,
140 params->callback())
svaldez6901c902015-12-10 18:57:33141 .release());
[email protected]a0ce3282011-08-19 20:49:52142}
143
[email protected]35869622012-10-26 23:23:55144class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03145 public:
[email protected]b3756012013-03-06 17:43:02146 DownloadItemFactoryImpl() {}
dchengc2282aa2014-10-21 12:07:58147 ~DownloadItemFactoryImpl() override {}
[email protected]d25fda12012-06-12 17:05:03148
dchengc2282aa2014-10-21 12:07:58149 DownloadItemImpl* CreatePersistedItem(
[email protected]b3756012013-03-06 17:43:02150 DownloadItemImplDelegate* delegate,
[email protected]530047e2013-07-12 17:02:25151 uint32 download_id,
[email protected]b3756012013-03-06 17:43:02152 const base::FilePath& current_path,
153 const base::FilePath& target_path,
154 const std::vector<GURL>& url_chain,
155 const GURL& referrer_url,
[email protected]0e648562014-06-12 19:39:45156 const std::string& mime_type,
157 const std::string& original_mime_type,
[email protected]b3756012013-03-06 17:43:02158 const base::Time& start_time,
159 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21160 const std::string& etag,
161 const std::string& last_modified,
[email protected]b3756012013-03-06 17:43:02162 int64 received_bytes,
163 int64 total_bytes,
164 DownloadItem::DownloadState state,
165 DownloadDangerType danger_type,
166 DownloadInterruptReason interrupt_reason,
167 bool opened,
mohan.reddy0673e932014-10-07 16:17:38168 const net::BoundNetLog& bound_net_log) override {
[email protected]b3756012013-03-06 17:43:02169 return new DownloadItemImpl(
170 delegate,
171 download_id,
172 current_path,
173 target_path,
174 url_chain,
175 referrer_url,
[email protected]0e648562014-06-12 19:39:45176 mime_type,
177 original_mime_type,
[email protected]b3756012013-03-06 17:43:02178 start_time,
179 end_time,
[email protected]56cd5942013-08-08 23:53:21180 etag,
181 last_modified,
[email protected]b3756012013-03-06 17:43:02182 received_bytes,
183 total_bytes,
184 state,
185 danger_type,
186 interrupt_reason,
187 opened,
188 bound_net_log);
189 }
[email protected]d25fda12012-06-12 17:05:03190
dchengc2282aa2014-10-21 12:07:58191 DownloadItemImpl* CreateActiveItem(
[email protected]b3756012013-03-06 17:43:02192 DownloadItemImplDelegate* delegate,
[email protected]530047e2013-07-12 17:02:25193 uint32 download_id,
[email protected]b3756012013-03-06 17:43:02194 const DownloadCreateInfo& info,
mohan.reddy0673e932014-10-07 16:17:38195 const net::BoundNetLog& bound_net_log) override {
[email protected]8f298352013-03-28 22:18:19196 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
[email protected]b3756012013-03-06 17:43:02197 }
[email protected]d25fda12012-06-12 17:05:03198
dchengc2282aa2014-10-21 12:07:58199 DownloadItemImpl* CreateSavePageItem(
[email protected]b3756012013-03-06 17:43:02200 DownloadItemImplDelegate* delegate,
[email protected]530047e2013-07-12 17:02:25201 uint32 download_id,
[email protected]b3756012013-03-06 17:43:02202 const base::FilePath& path,
203 const GURL& url,
[email protected]b3756012013-03-06 17:43:02204 const std::string& mime_type,
205 scoped_ptr<DownloadRequestHandleInterface> request_handle,
mohan.reddy0673e932014-10-07 16:17:38206 const net::BoundNetLog& bound_net_log) override {
[email protected]8f298352013-03-28 22:18:19207 return new DownloadItemImpl(delegate, download_id, path, url,
[email protected]b3756012013-03-06 17:43:02208 mime_type, request_handle.Pass(),
209 bound_net_log);
210 }
[email protected]d25fda12012-06-12 17:05:03211};
212
[email protected]a0ce3282011-08-19 20:49:52213} // namespace
214
[email protected]d25fda12012-06-12 17:05:03215DownloadManagerImpl::DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38216 net::NetLog* net_log,
217 BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20218 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55219 file_factory_(new DownloadFileFactory()),
[email protected]09ea72c7422012-07-02 20:35:40220 history_size_(0),
[email protected]16798692013-04-23 18:08:38221 shutdown_needed_(true),
222 browser_context_(browser_context),
[email protected]33c20cd92012-06-14 22:02:50223 delegate_(NULL),
[email protected]eba4a4d2013-05-29 02:18:06224 net_log_(net_log),
225 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38226 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29227}
228
[email protected]5656f8a2011-11-17 16:12:58229DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13230 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29231}
232
[email protected]89ec81f2013-05-15 19:20:02233DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
[email protected]530047e2013-07-12 17:02:25234 uint32 id, const DownloadCreateInfo& info) {
mostynbfbcdc27a2015-03-13 17:58:52235 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25236 DCHECK(!ContainsKey(downloads_, id));
[email protected]381af492013-03-28 01:56:22237 net::BoundNetLog bound_net_log =
238 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]89ec81f2013-05-15 19:20:02239 DownloadItemImpl* download =
[email protected]8f298352013-03-28 22:18:19240 item_factory_->CreateActiveItem(this, id, info, bound_net_log);
[email protected]530047e2013-07-12 17:02:25241 downloads_[id] = download;
[email protected]89ec81f2013-05-15 19:20:02242 return download;
[email protected]381af492013-03-28 01:56:22243}
244
[email protected]530047e2013-07-12 17:02:25245void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52246 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25247 if (delegate_) {
248 delegate_->GetNextId(callback);
249 return;
[email protected]491aaa62012-06-07 03:50:18250 }
[email protected]530047e2013-07-12 17:02:25251 static uint32 next_id = content::DownloadItem::kInvalidId + 1;
252 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53253}
254
[email protected]53ac00e82012-10-18 20:59:20255void DownloadManagerImpl::DetermineDownloadTarget(
256 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
257 // Note that this next call relies on
258 // DownloadItemImplDelegate::DownloadTargetCallback and
259 // DownloadManagerDelegate::DownloadTargetCallback having the same
260 // type. If the types ever diverge, gasket code will need to
261 // be written here.
262 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34263 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20264 // TODO(asanka): Determine a useful path if |target_path| is empty.
265 callback.Run(target_path,
266 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
[email protected]35869622012-10-26 23:23:55267 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
[email protected]53ac00e82012-10-18 20:59:20268 target_path);
269 }
[email protected]47665442012-07-27 02:31:22270}
271
[email protected]98299622013-01-03 22:26:50272bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43273 DownloadItemImpl* item, const base::Closure& complete_callback) {
274 if (!delegate_ ||
275 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50276 return true;
[email protected]18710a42a2012-10-17 19:50:43277 }
278 // Otherwise, the delegate has accepted responsibility to run the
279 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50280 return false;
[email protected]fc03de22011-12-06 23:28:12281}
282
[email protected]2dec8ec2013-02-07 19:20:34283bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
284 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18285 if (!delegate_)
286 return false;
287
[email protected]fc03de22011-12-06 23:28:12288 return delegate_->ShouldOpenFileBasedOnExtension(path);
289}
290
[email protected]bde0e4f2012-11-08 22:49:59291bool DownloadManagerImpl::ShouldOpenDownload(
292 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43293 if (!delegate_)
294 return true;
295
[email protected]bde0e4f2012-11-08 22:49:59296 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
297 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
298 // to have the same type :-}.
299 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43300}
301
[email protected]35869622012-10-26 23:23:55302void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57303 delegate_ = delegate;
304}
305
[email protected]35869622012-10-26 23:23:55306DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28307 return delegate_;
308}
309
[email protected]5656f8a2011-11-17 16:12:58310void DownloadManagerImpl::Shutdown() {
anujk.sharmad9a28742014-11-12 18:27:14311 DVLOG(20) << __FUNCTION__ << "()"
312 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13313 if (!shutdown_needed_)
314 return;
315 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29316
[email protected]75e51b52012-02-04 16:57:54317 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08318 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13319
[email protected]fe752272013-05-29 18:57:45320 // If there are in-progress downloads, cancel them. This also goes for
321 // dangerous downloads which will remain in history if they aren't explicitly
322 // accepted or discarded. Canceling will remove the intermediate download
323 // file.
324 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();
325 ++it) {
[email protected]db1d8f72012-07-13 19:23:16326 DownloadItemImpl* download = it->second;
[email protected]fe752272013-05-29 18:57:45327 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17328 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29329 }
[email protected]7745ff02012-10-01 00:09:24330 STLDeleteValues(&downloads_);
initial.commit09911bf2008-07-26 23:55:29331
[email protected]41f558fb2012-01-09 22:59:58332 // We'll have nothing more to report to the observers after this point.
333 observers_.Clear();
334
[email protected]b441a8492012-06-06 14:55:57335 if (delegate_)
336 delegate_->Shutdown();
[email protected]47665442012-07-27 02:31:22337 delegate_ = NULL;
initial.commit09911bf2008-07-26 23:55:29338}
339
[email protected]530047e2013-07-12 17:02:25340void DownloadManagerImpl::StartDownload(
[email protected]2bddd2072012-06-18 16:16:51341 scoped_ptr<DownloadCreateInfo> info,
[email protected]530047e2013-07-12 17:02:25342 scoped_ptr<ByteStreamReader> stream,
343 const DownloadUrlParameters::OnStartedCallback& on_started) {
mostynbfbcdc27a2015-03-13 17:58:52344 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25345 DCHECK(info);
346 uint32 download_id = info->download_id;
347 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
348 base::Callback<void(uint32)> got_id(base::Bind(
349 &DownloadManagerImpl::StartDownloadWithId,
350 weak_factory_.GetWeakPtr(),
asanka64114152015-12-21 21:28:49351 base::Passed(&info),
352 base::Passed(&stream),
[email protected]530047e2013-07-12 17:02:25353 on_started,
354 new_download));
[email protected]89ec81f2013-05-15 19:20:02355 if (new_download) {
[email protected]530047e2013-07-12 17:02:25356 GetNextId(got_id);
357 } else {
358 got_id.Run(download_id);
359 }
360}
361
362void DownloadManagerImpl::StartDownloadWithId(
363 scoped_ptr<DownloadCreateInfo> info,
364 scoped_ptr<ByteStreamReader> stream,
365 const DownloadUrlParameters::OnStartedCallback& on_started,
366 bool new_download,
367 uint32 id) {
mostynbfbcdc27a2015-03-13 17:58:52368 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25369 DCHECK_NE(content::DownloadItem::kInvalidId, id);
370 DownloadItemImpl* download = NULL;
371 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02372 download = CreateActiveItem(id, *info);
373 } else {
[email protected]530047e2013-07-12 17:02:25374 DownloadMap::iterator item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02375 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37376 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47377 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02378 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37379 // removed after it was resumed. Ignore. If the download is cancelled
380 // while resuming, then also ignore the request.
asanka64114152015-12-21 21:28:49381 info->request_handle->CancelRequest();
[email protected]530047e2013-07-12 17:02:25382 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56383 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
[email protected]530047e2013-07-12 17:02:25384 return;
[email protected]89ec81f2013-05-15 19:20:02385 }
386 download = item_iterator->second;
[email protected]803036e02013-06-15 17:08:47387 DCHECK_EQ(DownloadItem::INTERRUPTED, download->GetState());
[email protected]e87e5832013-12-13 22:20:27388 download->MergeOriginInfoOnResume(*info);
[email protected]89ec81f2013-05-15 19:20:02389 }
390
[email protected]2dec8ec2013-02-07 19:20:34391 base::FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01392 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34393 base::FilePath website_save_directory; // Unused
394 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01395 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20396 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01397 }
398
[email protected]381af492013-03-28 01:56:22399 // Create the download file and start the download.
asanka64114152015-12-21 21:28:49400 scoped_ptr<DownloadFile> download_file(file_factory_->CreateFile(
401 std::move(info->save_info), default_download_directory, info->url(),
402 info->referrer_url, delegate_ && delegate_->GenerateFileHash(),
403 std::move(stream), download->GetBoundNetLog(),
404 download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48405
406 // Attach the client ID identifying the app to the AV system.
407 if (download_file.get() && delegate_) {
408 download_file->SetClientGuid(
409 delegate_->ApplicationClientIdForFileScanning());
410 }
411
asanka64114152015-12-21 21:28:49412 download->Start(std::move(download_file), std::move(info->request_handle));
[email protected]96c3bdbaa2012-08-31 16:39:54413
[email protected]381af492013-03-28 01:56:22414 // For interrupted downloads, Start() will transition the state to
415 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
416 // For new downloads, we notify here, rather than earlier, so that
417 // the download_file is bound to download and all the usual
418 // setters (e.g. Cancel) work.
419 if (new_download)
420 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]96c3bdbaa2012-08-31 16:39:54421
[email protected]530047e2013-07-12 17:02:25422 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56423 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35424}
425
[email protected]5656f8a2011-11-17 16:12:58426void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
mostynbfbcdc27a2015-03-13 17:58:52427 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]09ea72c7422012-07-02 20:35:40428 for (DownloadMap::iterator it = downloads_.begin();
429 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16430 DownloadItemImpl* item = it->second;
[email protected]3d95e542012-11-20 00:52:08431 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48432 }
433}
434
[email protected]db1d8f72012-07-13 19:23:16435void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
mostynbfbcdc27a2015-03-13 17:58:52436 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]803036e02013-06-15 17:08:47437 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16438 !download_item->GetFileExternallyRemoved() &&
439 delegate_) {
440 delegate_->CheckForFileExistence(
441 download_item,
442 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
[email protected]eba4a4d2013-05-29 02:18:06443 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48444 }
445}
446
[email protected]530047e2013-07-12 17:02:25447void DownloadManagerImpl::OnFileExistenceChecked(uint32 download_id,
[email protected]4c544cd2013-01-25 07:54:16448 bool result) {
mostynbfbcdc27a2015-03-13 17:58:52449 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]4c544cd2013-01-25 07:54:16450 if (!result) { // File does not exist.
451 if (ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51452 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16453 }
[email protected]9fc114672011-06-15 08:17:48454}
455
[email protected]35869622012-10-26 23:23:55456BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58457 return browser_context_;
458}
459
[email protected]530047e2013-07-12 17:02:25460void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34461 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12462 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27463 const std::string& mime_type,
[email protected]b3756012013-03-06 17:43:02464 scoped_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25465 const DownloadItemImplCreated& item_created) {
mostynbfbcdc27a2015-03-13 17:58:52466 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25467 GetNextId(base::Bind(
468 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
469 weak_factory_.GetWeakPtr(),
470 main_file_path,
471 page_url,
472 mime_type,
473 base::Passed(request_handle.Pass()),
474 item_created));
475}
476
477void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
478 const base::FilePath& main_file_path,
479 const GURL& page_url,
480 const std::string& mime_type,
481 scoped_ptr<DownloadRequestHandleInterface> request_handle,
482 const DownloadItemImplCreated& item_created,
483 uint32 id) {
mostynbfbcdc27a2015-03-13 17:58:52484 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]530047e2013-07-12 17:02:25485 DCHECK_NE(content::DownloadItem::kInvalidId, id);
486 DCHECK(!ContainsKey(downloads_, id));
[email protected]ef17c9a2012-02-09 05:08:09487 net::BoundNetLog bound_net_log =
488 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08489 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
[email protected]6474b112012-05-04 19:35:27490 this,
[email protected]530047e2013-07-12 17:02:25491 id,
[email protected]6474b112012-05-04 19:35:27492 main_file_path,
493 page_url,
[email protected]6474b112012-05-04 19:35:27494 mime_type,
[email protected]b3756012013-03-06 17:43:02495 request_handle.Pass(),
[email protected]6474b112012-05-04 19:35:27496 bound_net_log);
[email protected]3d95e542012-11-20 00:52:08497 downloads_[download_item->GetId()] = download_item;
498 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
499 this, download_item));
[email protected]530047e2013-07-12 17:02:25500 if (!item_created.is_null())
501 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12502}
503
[email protected]df58aa82013-01-24 21:54:00504void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
505 DownloadItem* download_item) {
506 FOR_EACH_OBSERVER(Observer, observers_,
507 OnSavePackageSuccessfullyFinished(this, download_item));
508}
509
[email protected]8d68a3e02013-01-12 15:57:10510// Resume a download of a specific URL. We send the request to the
511// ResourceDispatcherHost, and let it send us responses like a regular
512// download.
513void DownloadManagerImpl::ResumeInterruptedDownload(
514 scoped_ptr<content::DownloadUrlParameters> params,
[email protected]530047e2013-07-12 17:02:25515 uint32 id) {
[email protected]11325a22013-07-30 23:02:26516 RecordDownloadSource(INITIATED_BY_RESUMPTION);
svaldez6901c902015-12-10 18:57:33517 BrowserThread::PostTaskAndReplyWithResult(
518 BrowserThread::IO, FROM_HERE,
519 base::Bind(&BeginDownload, base::Passed(&params), id,
520 weak_factory_.GetWeakPtr()),
521 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
522 weak_factory_.GetWeakPtr()));
[email protected]8d68a3e02013-01-12 15:57:10523}
524
[email protected]53ac00e82012-10-18 20:59:20525void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
[email protected]35869622012-10-26 23:23:55526 scoped_ptr<DownloadItemFactory> item_factory) {
[email protected]53ac00e82012-10-18 20:59:20527 item_factory_ = item_factory.Pass();
528}
529
530void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
[email protected]35869622012-10-26 23:23:55531 scoped_ptr<DownloadFileFactory> file_factory) {
[email protected]53ac00e82012-10-18 20:59:20532 file_factory_ = file_factory.Pass();
533}
534
[email protected]35869622012-10-26 23:23:55535DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20536 return file_factory_.get();
537}
538
[email protected]db1d8f72012-07-13 19:23:16539void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28540 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40541 return;
542
[email protected]530047e2013-07-12 17:02:25543 uint32 download_id = download->GetId();
[email protected]6065748f2013-10-29 01:07:43544 if (downloads_.erase(download_id) == 0)
[email protected]237af6d2013-03-15 23:45:28545 return;
[email protected]237af6d2013-03-15 23:45:28546 delete download;
initial.commit09911bf2008-07-26 23:55:29547}
548
svaldez6901c902015-12-10 18:57:33549void DownloadManagerImpl::AddUrlDownloader(
550 scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> downloader) {
551 if (downloader)
552 url_downloaders_.push_back(downloader.Pass());
553}
554
555void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) {
556 for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end();
557 ++ptr) {
558 if (ptr->get() == downloader) {
559 url_downloaders_.erase(ptr);
560 return;
561 }
562 }
563}
564
ttr31481dc54b2015-08-06 20:11:26565namespace {
566
567bool RemoveDownloadBetween(base::Time remove_begin,
568 base::Time remove_end,
569 const DownloadItemImpl* download_item) {
570 return download_item->GetStartTime() >= remove_begin &&
571 (remove_end.is_null() || download_item->GetStartTime() < remove_end);
572}
573
574bool RemoveDownloadByOriginAndTime(const url::Origin& origin,
575 base::Time remove_begin,
576 base::Time remove_end,
577 const DownloadItemImpl* download_item) {
578 return origin.IsSameOriginWith(url::Origin(download_item->GetURL())) &&
579 RemoveDownloadBetween(remove_begin, remove_end, download_item);
580}
581
582} // namespace
583
584int DownloadManagerImpl::RemoveDownloads(const DownloadRemover& remover) {
[email protected]237af6d2013-03-15 23:45:28585 int count = 0;
586 DownloadMap::const_iterator it = downloads_.begin();
587 while (it != downloads_.end()) {
[email protected]db1d8f72012-07-13 19:23:16588 DownloadItemImpl* download = it->second;
[email protected]237af6d2013-03-15 23:45:28589
590 // Increment done here to protect against invalidation below.
591 ++it;
592
ttr31481dc54b2015-08-06 20:11:26593 if (download->GetState() != DownloadItem::IN_PROGRESS &&
594 remover.Run(download)) {
[email protected]237af6d2013-03-15 23:45:28595 download->Remove();
596 count++;
initial.commit09911bf2008-07-26 23:55:29597 }
initial.commit09911bf2008-07-26 23:55:29598 }
[email protected]237af6d2013-03-15 23:45:28599 return count;
initial.commit09911bf2008-07-26 23:55:29600}
601
ttr31481dc54b2015-08-06 20:11:26602int DownloadManagerImpl::RemoveDownloadsByOriginAndTime(
603 const url::Origin& origin,
604 base::Time remove_begin,
605 base::Time remove_end) {
606 return RemoveDownloads(base::Bind(&RemoveDownloadByOriginAndTime,
607 base::ConstRef(origin), remove_begin,
608 remove_end));
609}
610
611int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
612 base::Time remove_end) {
613 return RemoveDownloads(
614 base::Bind(&RemoveDownloadBetween, remove_begin, remove_end));
615}
616
[email protected]fd3a82832012-01-19 20:35:12617int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59618 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29619}
620
[email protected]5656f8a2011-11-17 16:12:58621int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]d41355e6f2009-04-07 21:21:12622 // The null times make the date range unbounded.
[email protected]09ea72c7422012-07-02 20:35:40623 int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time());
[email protected]35869622012-10-26 23:23:55624 RecordClearAllSize(num_deleted);
[email protected]09ea72c7422012-07-02 20:35:40625 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12626}
627
[email protected]0d4e30c2012-01-28 00:47:53628void DownloadManagerImpl::DownloadUrl(
[email protected]35869622012-10-26 23:23:55629 scoped_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14630 if (params->post_id() >= 0) {
631 // Check this here so that the traceback is more useful.
632 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43633 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14634 }
svaldez6901c902015-12-10 18:57:33635 BrowserThread::PostTaskAndReplyWithResult(
636 BrowserThread::IO, FROM_HERE,
637 base::Bind(&BeginDownload, base::Passed(&params),
638 content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()),
639 base::Bind(&DownloadManagerImpl::AddUrlDownloader,
640 weak_factory_.GetWeakPtr()));
initial.commit09911bf2008-07-26 23:55:29641}
642
[email protected]5656f8a2011-11-17 16:12:58643void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29644 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29645}
646
[email protected]5656f8a2011-11-17 16:12:58647void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29648 observers_.RemoveObserver(observer);
649}
650
[email protected]3d95e542012-11-20 00:52:08651DownloadItem* DownloadManagerImpl::CreateDownloadItem(
[email protected]530047e2013-07-12 17:02:25652 uint32 id,
[email protected]2dec8ec2013-02-07 19:20:34653 const base::FilePath& current_path,
654 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15655 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08656 const GURL& referrer_url,
[email protected]0e648562014-06-12 19:39:45657 const std::string& mime_type,
658 const std::string& original_mime_type,
[email protected]3d95e542012-11-20 00:52:08659 const base::Time& start_time,
660 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21661 const std::string& etag,
662 const std::string& last_modified,
[email protected]3d95e542012-11-20 00:52:08663 int64 received_bytes,
664 int64 total_bytes,
665 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15666 DownloadDangerType danger_type,
667 DownloadInterruptReason interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08668 bool opened) {
[email protected]6065748f2013-10-29 01:07:43669 if (ContainsKey(downloads_, id)) {
670 NOTREACHED();
[email protected]530047e2013-07-12 17:02:25671 return NULL;
[email protected]6065748f2013-10-29 01:07:43672 }
[email protected]3d95e542012-11-20 00:52:08673 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
674 this,
[email protected]530047e2013-07-12 17:02:25675 id,
[email protected]876774692013-02-03 17:20:15676 current_path,
677 target_path,
678 url_chain,
[email protected]3d95e542012-11-20 00:52:08679 referrer_url,
[email protected]0e648562014-06-12 19:39:45680 mime_type,
681 original_mime_type,
[email protected]3d95e542012-11-20 00:52:08682 start_time,
683 end_time,
[email protected]56cd5942013-08-08 23:53:21684 etag,
685 last_modified,
[email protected]3d95e542012-11-20 00:52:08686 received_bytes,
687 total_bytes,
688 state,
[email protected]876774692013-02-03 17:20:15689 danger_type,
690 interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08691 opened,
692 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
[email protected]530047e2013-07-12 17:02:25693 downloads_[id] = item;
[email protected]3d95e542012-11-20 00:52:08694 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
anujk.sharmad9a28742014-11-12 18:27:14695 DVLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
[email protected]3d95e542012-11-20 00:52:08696 return item;
initial.commit09911bf2008-07-26 23:55:29697}
698
[email protected]5656f8a2011-11-17 16:12:58699int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56700 int count = 0;
[email protected]fd6ddb82012-11-27 00:38:43701 for (DownloadMap::const_iterator it = downloads_.begin();
702 it != downloads_.end(); ++it) {
[email protected]803036e02013-06-15 17:08:47703 if (it->second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56704 ++count;
705 }
706 return count;
[email protected]5656f8a2011-11-17 16:12:58707}
708
[email protected]422a7d12013-10-21 12:10:42709int DownloadManagerImpl::NonMaliciousInProgressCount() const {
710 int count = 0;
711 for (DownloadMap::const_iterator it = downloads_.begin();
712 it != downloads_.end(); ++it) {
713 if (it->second->GetState() == DownloadItem::IN_PROGRESS &&
714 it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
715 it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
[email protected]24eb5172013-10-27 02:03:59716 it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
717 it->second->GetDangerType() !=
718 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42719 ++count;
720 }
721 }
722 return count;
723}
724
[email protected]530047e2013-07-12 17:02:25725DownloadItem* DownloadManagerImpl::GetDownload(uint32 download_id) {
[email protected]ba7895d2012-06-22 19:02:52726 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL;
727}
728
[email protected]9f1f4092012-09-10 21:18:04729void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
730 for (DownloadMap::iterator it = downloads_.begin();
731 it != downloads_.end(); ++it) {
732 downloads->push_back(it->second);
733 }
734}
735
[email protected]ffc0c2d2013-01-21 15:32:12736void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06737 int num_unopened = 0;
[email protected]09ea72c7422012-07-02 20:35:40738 for (DownloadMap::iterator it = downloads_.begin();
739 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16740 DownloadItemImpl* item = it->second;
[email protected]803036e02013-06-15 17:08:47741 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40742 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06743 ++num_unopened;
744 }
[email protected]35869622012-10-26 23:23:55745 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12746
[email protected]0baf5922013-01-30 13:55:18747 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12748 delegate_->OpenDownload(download);
749}
750
751void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
752 if (delegate_)
753 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06754}
[email protected]5656f8a2011-11-17 16:12:58755
[email protected]35869622012-10-26 23:23:55756} // namespace content