blob: 2bd747d5595620584fb09b60d1ed51b1212b1ebe [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"
[email protected]95f861e2013-07-18 02:07:1714#include "base/message_loop/message_loop.h"
[email protected]7286e3fc2011-07-19 22:13:2415#include "base/stl_util.h"
[email protected]10994d132013-06-11 07:16:1816#include "base/strings/stringprintf.h"
[email protected]40d11e02013-03-28 17:43:1417#include "base/strings/sys_string_conversions.h"
[email protected]d59d40c2012-08-08 18:18:3718#include "base/supports_user_data.h"
[email protected]eda58402011-09-21 19:32:0219#include "base/synchronization/lock.h"
[email protected]d2a8fb72010-01-21 05:31:4220#include "build/build_config.h"
[email protected]f8e92b5d2013-03-21 18:35:4621#include "content/browser/byte_stream.h"
[email protected]71bf3f5e2011-08-15 21:05:2222#include "content/browser/download/download_create_info.h"
[email protected]53ac00e82012-10-18 20:59:2023#include "content/browser/download/download_file_factory.h"
24#include "content/browser/download/download_item_factory.h"
[email protected]c09a8fd2011-11-21 19:54:5025#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0626#include "content/browser/download/download_stats.h"
[email protected]678c0362012-12-05 08:02:4427#include "content/browser/loader/resource_dispatcher_host_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3228#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2929#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1130#include "content/public/browser/browser_context.h"
[email protected]c38831a12011-10-28 12:44:4931#include "content/public/browser/browser_thread.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"
[email protected]c5a5c0842012-05-04 20:05:1442#include "net/base/load_flags.h"
[email protected]2ca01e52013-10-31 22:05:1943#include "net/base/request_priority.h"
[email protected]f288ef02012-12-15 20:28:2844#include "net/base/upload_bytes_element_reader.h"
45#include "net/base/upload_data_stream.h"
[email protected]eb795632012-09-01 00:19:3146#include "net/url_request/url_request_context.h"
initial.commit09911bf2008-07-26 23:55:2947
[email protected]35869622012-10-26 23:23:5548namespace content {
[email protected]a0ce3282011-08-19 20:49:5249namespace {
50
[email protected]8d68a3e02013-01-12 15:57:1051void BeginDownload(scoped_ptr<DownloadUrlParameters> params,
[email protected]530047e2013-07-12 17:02:2552 uint32 download_id) {
[email protected]c5a5c0842012-05-04 20:05:1453 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
54 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
55 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
56 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
[email protected]eb795632012-09-01 00:19:3157 scoped_ptr<net::URLRequest> request(
58 params->resource_context()->GetRequestContext()->CreateRequest(
[email protected]d670bbd2014-03-12 21:39:4259 params->url(), net::DEFAULT_PRIORITY, NULL, NULL));
[email protected]bb1c4662013-11-14 00:00:0760 request->SetLoadFlags(request->load_flags() | params->load_flags());
[email protected]c5a5c0842012-05-04 20:05:1461 request->set_method(params->method());
[email protected]fdae8bf2012-11-20 04:02:4562 if (!params->post_body().empty()) {
[email protected]f288ef02012-12-15 20:28:2863 const std::string& body = params->post_body();
64 scoped_ptr<net::UploadElementReader> reader(
65 net::UploadOwnedBytesElementReader::CreateWithString(body));
66 request->set_upload(make_scoped_ptr(
67 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)));
[email protected]fdae8bf2012-11-20 04:02:4568 }
[email protected]c5a5c0842012-05-04 20:05:1469 if (params->post_id() >= 0) {
[email protected]27678b2a2012-02-04 22:09:1470 // The POST in this case does not have an actual body, and only works
71 // when retrieving data from cache. This is done because we don't want
72 // to do a re-POST without user consent, and currently don't have a good
73 // plan on how to display the UI for that.
[email protected]c5a5c0842012-05-04 20:05:1474 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:4375 DCHECK_EQ("POST", params->method());
[email protected]f288ef02012-12-15 20:28:2876 ScopedVector<net::UploadElementReader> element_readers;
77 request->set_upload(make_scoped_ptr(
[email protected]96c77a72013-09-24 09:49:2078 new net::UploadDataStream(element_readers.Pass(), params->post_id())));
[email protected]27678b2a2012-02-04 22:09:1479 }
[email protected]8d68a3e02013-01-12 15:57:1080
81 // If we're not at the beginning of the file, retrieve only the remaining
82 // portion.
[email protected]fc179dd12013-03-16 09:35:1983 bool has_last_modified = !params->last_modified().empty();
84 bool has_etag = !params->etag().empty();
85
86 // If we've asked for a range, we want to make sure that we only
87 // get that range if our current copy of the information is good.
88 // We shouldn't be asked to continue if we don't have a verifier.
89 DCHECK(params->offset() == 0 || has_etag || has_last_modified);
90
[email protected]8d68a3e02013-01-12 15:57:1091 if (params->offset() > 0) {
92 request->SetExtraRequestHeaderByName(
93 "Range",
[email protected]7d3cbc92013-03-18 22:33:0494 base::StringPrintf("bytes=%" PRId64 "-", params->offset()),
[email protected]8d68a3e02013-01-12 15:57:1095 true);
96
[email protected]fc179dd12013-03-16 09:35:1997 if (has_last_modified) {
[email protected]8d68a3e02013-01-12 15:57:1098 request->SetExtraRequestHeaderByName("If-Unmodified-Since",
99 params->last_modified(),
100 true);
101 }
[email protected]fc179dd12013-03-16 09:35:19102 if (has_etag) {
[email protected]8d68a3e02013-01-12 15:57:10103 request->SetExtraRequestHeaderByName("If-Match", params->etag(), true);
104 }
105 }
106
[email protected]35869622012-10-26 23:23:55107 for (DownloadUrlParameters::RequestHeadersType::const_iterator iter
[email protected]c5a5c0842012-05-04 20:05:14108 = params->request_headers_begin();
109 iter != params->request_headers_end();
110 ++iter) {
111 request->SetExtraRequestHeaderByName(
[email protected]6065748f2013-10-29 01:07:43112 iter->first, iter->second, false /*overwrite*/);
[email protected]c5a5c0842012-05-04 20:05:14113 }
[email protected]49d812e2012-11-06 21:18:12114
115 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo());
116 save_info->file_path = params->file_path();
117 save_info->suggested_name = params->suggested_name();
118 save_info->offset = params->offset();
119 save_info->hash_state = params->hash_state();
120 save_info->prompt_for_save_location = params->prompt();
[email protected]b32f9a12014-04-03 11:51:21121 save_info->file = params->GetFile();
[email protected]49d812e2012-11-06 21:18:12122
[email protected]bdf5d6c2013-06-21 05:15:59123 ResourceDispatcherHost::Get()->BeginDownload(
[email protected]ea114722012-03-12 01:11:25124 request.Pass(),
[email protected]a60346f2013-06-25 16:43:23125 params->referrer(),
[email protected]a53e2f92012-05-15 15:27:06126 params->content_initiated(),
[email protected]c5a5c0842012-05-04 20:05:14127 params->resource_context(),
128 params->render_process_host_id(),
129 params->render_view_host_routing_id(),
130 params->prefer_cache(),
[email protected]49d812e2012-11-06 21:18:12131 save_info.Pass(),
[email protected]8d68a3e02013-01-12 15:57:10132 download_id,
[email protected]c5a5c0842012-05-04 20:05:14133 params->callback());
[email protected]a0ce3282011-08-19 20:49:52134}
135
[email protected]33d22102012-01-25 17:46:53136class MapValueIteratorAdapter {
137 public:
138 explicit MapValueIteratorAdapter(
139 base::hash_map<int64, DownloadItem*>::const_iterator iter)
140 : iter_(iter) {
141 }
142 ~MapValueIteratorAdapter() {}
143
144 DownloadItem* operator*() { return iter_->second; }
145
146 MapValueIteratorAdapter& operator++() {
147 ++iter_;
148 return *this;
149 }
150
151 bool operator!=(const MapValueIteratorAdapter& that) const {
152 return iter_ != that.iter_;
153 }
154
155 private:
156 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
157 // Allow copy and assign.
158};
159
[email protected]35869622012-10-26 23:23:55160class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03161 public:
[email protected]b3756012013-03-06 17:43:02162 DownloadItemFactoryImpl() {}
163 virtual ~DownloadItemFactoryImpl() {}
[email protected]d25fda12012-06-12 17:05:03164
[email protected]b3756012013-03-06 17:43:02165 virtual DownloadItemImpl* CreatePersistedItem(
166 DownloadItemImplDelegate* delegate,
[email protected]530047e2013-07-12 17:02:25167 uint32 download_id,
[email protected]b3756012013-03-06 17:43:02168 const base::FilePath& current_path,
169 const base::FilePath& target_path,
170 const std::vector<GURL>& url_chain,
171 const GURL& referrer_url,
[email protected]0e648562014-06-12 19:39:45172 const std::string& mime_type,
173 const std::string& original_mime_type,
[email protected]b3756012013-03-06 17:43:02174 const base::Time& start_time,
175 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21176 const std::string& etag,
177 const std::string& last_modified,
[email protected]b3756012013-03-06 17:43:02178 int64 received_bytes,
179 int64 total_bytes,
180 DownloadItem::DownloadState state,
181 DownloadDangerType danger_type,
182 DownloadInterruptReason interrupt_reason,
183 bool opened,
184 const net::BoundNetLog& bound_net_log) OVERRIDE {
185 return new DownloadItemImpl(
186 delegate,
187 download_id,
188 current_path,
189 target_path,
190 url_chain,
191 referrer_url,
[email protected]0e648562014-06-12 19:39:45192 mime_type,
193 original_mime_type,
[email protected]b3756012013-03-06 17:43:02194 start_time,
195 end_time,
[email protected]56cd5942013-08-08 23:53:21196 etag,
197 last_modified,
[email protected]b3756012013-03-06 17:43:02198 received_bytes,
199 total_bytes,
200 state,
201 danger_type,
202 interrupt_reason,
203 opened,
204 bound_net_log);
205 }
[email protected]d25fda12012-06-12 17:05:03206
[email protected]b3756012013-03-06 17:43:02207 virtual DownloadItemImpl* CreateActiveItem(
208 DownloadItemImplDelegate* delegate,
[email protected]530047e2013-07-12 17:02:25209 uint32 download_id,
[email protected]b3756012013-03-06 17:43:02210 const DownloadCreateInfo& info,
211 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]8f298352013-03-28 22:18:19212 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
[email protected]b3756012013-03-06 17:43:02213 }
[email protected]d25fda12012-06-12 17:05:03214
[email protected]b3756012013-03-06 17:43:02215 virtual DownloadItemImpl* CreateSavePageItem(
216 DownloadItemImplDelegate* delegate,
[email protected]530047e2013-07-12 17:02:25217 uint32 download_id,
[email protected]b3756012013-03-06 17:43:02218 const base::FilePath& path,
219 const GURL& url,
[email protected]b3756012013-03-06 17:43:02220 const std::string& mime_type,
221 scoped_ptr<DownloadRequestHandleInterface> request_handle,
222 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]8f298352013-03-28 22:18:19223 return new DownloadItemImpl(delegate, download_id, path, url,
[email protected]b3756012013-03-06 17:43:02224 mime_type, request_handle.Pass(),
225 bound_net_log);
226 }
[email protected]d25fda12012-06-12 17:05:03227};
228
[email protected]a0ce3282011-08-19 20:49:52229} // namespace
230
[email protected]d25fda12012-06-12 17:05:03231DownloadManagerImpl::DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38232 net::NetLog* net_log,
233 BrowserContext* browser_context)
[email protected]53ac00e82012-10-18 20:59:20234 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55235 file_factory_(new DownloadFileFactory()),
[email protected]09ea72c7422012-07-02 20:35:40236 history_size_(0),
[email protected]16798692013-04-23 18:08:38237 shutdown_needed_(true),
238 browser_context_(browser_context),
[email protected]33c20cd92012-06-14 22:02:50239 delegate_(NULL),
[email protected]eba4a4d2013-05-29 02:18:06240 net_log_(net_log),
241 weak_factory_(this) {
[email protected]16798692013-04-23 18:08:38242 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29243}
244
[email protected]5656f8a2011-11-17 16:12:58245DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13246 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29247}
248
[email protected]89ec81f2013-05-15 19:20:02249DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
[email protected]530047e2013-07-12 17:02:25250 uint32 id, const DownloadCreateInfo& info) {
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
252 DCHECK(!ContainsKey(downloads_, id));
[email protected]381af492013-03-28 01:56:22253 net::BoundNetLog bound_net_log =
254 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]89ec81f2013-05-15 19:20:02255 DownloadItemImpl* download =
[email protected]8f298352013-03-28 22:18:19256 item_factory_->CreateActiveItem(this, id, info, bound_net_log);
[email protected]530047e2013-07-12 17:02:25257 downloads_[id] = download;
[email protected]89ec81f2013-05-15 19:20:02258 return download;
[email protected]381af492013-03-28 01:56:22259}
260
[email protected]530047e2013-07-12 17:02:25261void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) {
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
263 if (delegate_) {
264 delegate_->GetNextId(callback);
265 return;
[email protected]491aaa62012-06-07 03:50:18266 }
[email protected]530047e2013-07-12 17:02:25267 static uint32 next_id = content::DownloadItem::kInvalidId + 1;
268 callback.Run(next_id++);
[email protected]2909e342011-10-29 00:46:53269}
270
[email protected]53ac00e82012-10-18 20:59:20271void DownloadManagerImpl::DetermineDownloadTarget(
272 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
273 // Note that this next call relies on
274 // DownloadItemImplDelegate::DownloadTargetCallback and
275 // DownloadManagerDelegate::DownloadTargetCallback having the same
276 // type. If the types ever diverge, gasket code will need to
277 // be written here.
278 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
[email protected]2dec8ec2013-02-07 19:20:34279 base::FilePath target_path = item->GetForcedFilePath();
[email protected]53ac00e82012-10-18 20:59:20280 // TODO(asanka): Determine a useful path if |target_path| is empty.
281 callback.Run(target_path,
282 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
[email protected]35869622012-10-26 23:23:55283 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
[email protected]53ac00e82012-10-18 20:59:20284 target_path);
285 }
[email protected]47665442012-07-27 02:31:22286}
287
[email protected]98299622013-01-03 22:26:50288bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43289 DownloadItemImpl* item, const base::Closure& complete_callback) {
290 if (!delegate_ ||
291 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50292 return true;
[email protected]18710a42a2012-10-17 19:50:43293 }
294 // Otherwise, the delegate has accepted responsibility to run the
295 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50296 return false;
[email protected]fc03de22011-12-06 23:28:12297}
298
[email protected]2dec8ec2013-02-07 19:20:34299bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(
300 const base::FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18301 if (!delegate_)
302 return false;
303
[email protected]fc03de22011-12-06 23:28:12304 return delegate_->ShouldOpenFileBasedOnExtension(path);
305}
306
[email protected]bde0e4f2012-11-08 22:49:59307bool DownloadManagerImpl::ShouldOpenDownload(
308 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43309 if (!delegate_)
310 return true;
311
[email protected]bde0e4f2012-11-08 22:49:59312 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
313 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
314 // to have the same type :-}.
315 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43316}
317
[email protected]35869622012-10-26 23:23:55318void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57319 delegate_ = delegate;
320}
321
[email protected]35869622012-10-26 23:23:55322DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28323 return delegate_;
324}
325
[email protected]5656f8a2011-11-17 16:12:58326void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50327 VLOG(20) << __FUNCTION__ << "()"
328 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13329 if (!shutdown_needed_)
330 return;
331 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29332
[email protected]75e51b52012-02-04 16:57:54333 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08334 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13335
[email protected]fe752272013-05-29 18:57:45336 // If there are in-progress downloads, cancel them. This also goes for
337 // dangerous downloads which will remain in history if they aren't explicitly
338 // accepted or discarded. Canceling will remove the intermediate download
339 // file.
340 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();
341 ++it) {
[email protected]db1d8f72012-07-13 19:23:16342 DownloadItemImpl* download = it->second;
[email protected]fe752272013-05-29 18:57:45343 if (download->GetState() == DownloadItem::IN_PROGRESS)
[email protected]93af2272011-09-21 18:29:17344 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29345 }
[email protected]7745ff02012-10-01 00:09:24346 STLDeleteValues(&downloads_);
[email protected]7e834f02012-08-09 20:38:56347 downloads_.clear();
initial.commit09911bf2008-07-26 23:55:29348
[email protected]41f558fb2012-01-09 22:59:58349 // We'll have nothing more to report to the observers after this point.
350 observers_.Clear();
351
[email protected]b441a8492012-06-06 14:55:57352 if (delegate_)
353 delegate_->Shutdown();
[email protected]47665442012-07-27 02:31:22354 delegate_ = NULL;
initial.commit09911bf2008-07-26 23:55:29355}
356
[email protected]530047e2013-07-12 17:02:25357void DownloadManagerImpl::StartDownload(
[email protected]2bddd2072012-06-18 16:16:51358 scoped_ptr<DownloadCreateInfo> info,
[email protected]530047e2013-07-12 17:02:25359 scoped_ptr<ByteStreamReader> stream,
360 const DownloadUrlParameters::OnStartedCallback& on_started) {
[email protected]ca4b5fa32010-10-09 12:42:18361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]530047e2013-07-12 17:02:25362 DCHECK(info);
363 uint32 download_id = info->download_id;
364 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
365 base::Callback<void(uint32)> got_id(base::Bind(
366 &DownloadManagerImpl::StartDownloadWithId,
367 weak_factory_.GetWeakPtr(),
368 base::Passed(info.Pass()),
369 base::Passed(stream.Pass()),
370 on_started,
371 new_download));
[email protected]89ec81f2013-05-15 19:20:02372 if (new_download) {
[email protected]530047e2013-07-12 17:02:25373 GetNextId(got_id);
374 } else {
375 got_id.Run(download_id);
376 }
377}
378
379void DownloadManagerImpl::StartDownloadWithId(
380 scoped_ptr<DownloadCreateInfo> info,
381 scoped_ptr<ByteStreamReader> stream,
382 const DownloadUrlParameters::OnStartedCallback& on_started,
383 bool new_download,
384 uint32 id) {
385 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
386 DCHECK_NE(content::DownloadItem::kInvalidId, id);
387 DownloadItemImpl* download = NULL;
388 if (new_download) {
[email protected]89ec81f2013-05-15 19:20:02389 download = CreateActiveItem(id, *info);
390 } else {
[email protected]530047e2013-07-12 17:02:25391 DownloadMap::iterator item_iterator = downloads_.find(id);
[email protected]89ec81f2013-05-15 19:20:02392 // Trying to resume an interrupted download.
[email protected]7b3eeca42013-05-23 20:56:37393 if (item_iterator == downloads_.end() ||
[email protected]803036e02013-06-15 17:08:47394 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
[email protected]89ec81f2013-05-15 19:20:02395 // If the download is no longer known to the DownloadManager, then it was
[email protected]7b3eeca42013-05-23 20:56:37396 // removed after it was resumed. Ignore. If the download is cancelled
397 // while resuming, then also ignore the request.
[email protected]89ec81f2013-05-15 19:20:02398 info->request_handle.CancelRequest();
[email protected]530047e2013-07-12 17:02:25399 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56400 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
[email protected]530047e2013-07-12 17:02:25401 return;
[email protected]89ec81f2013-05-15 19:20:02402 }
403 download = item_iterator->second;
[email protected]803036e02013-06-15 17:08:47404 DCHECK_EQ(DownloadItem::INTERRUPTED, download->GetState());
[email protected]e87e5832013-12-13 22:20:27405 download->MergeOriginInfoOnResume(*info);
[email protected]89ec81f2013-05-15 19:20:02406 }
407
[email protected]2dec8ec2013-02-07 19:20:34408 base::FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01409 if (delegate_) {
[email protected]2dec8ec2013-02-07 19:20:34410 base::FilePath website_save_directory; // Unused
411 bool skip_dir_check = false; // Unused
[email protected]96bfed052012-09-15 22:21:01412 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20413 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01414 }
415
[email protected]381af492013-03-28 01:56:22416 // Create the download file and start the download.
[email protected]35869622012-10-26 23:23:55417 scoped_ptr<DownloadFile> download_file(
[email protected]53ac00e82012-10-18 20:59:20418 file_factory_->CreateFile(
419 info->save_info.Pass(), default_download_directory,
420 info->url(), info->referrer_url,
[email protected]72b8419d2013-12-05 04:59:38421 delegate_ && delegate_->GenerateFileHash(),
[email protected]8d68a3e02013-01-12 15:57:10422 stream.Pass(), download->GetBoundNetLog(),
[email protected]53ac00e82012-10-18 20:59:20423 download->DestinationObserverAsWeakPtr()));
[email protected]492c0092013-08-10 13:49:48424
425 // Attach the client ID identifying the app to the AV system.
426 if (download_file.get() && delegate_) {
427 download_file->SetClientGuid(
428 delegate_->ApplicationClientIdForFileScanning());
429 }
430
[email protected]8d68a3e02013-01-12 15:57:10431 scoped_ptr<DownloadRequestHandleInterface> req_handle(
432 new DownloadRequestHandle(info->request_handle));
433 download->Start(download_file.Pass(), req_handle.Pass());
[email protected]96c3bdbaa2012-08-31 16:39:54434
[email protected]381af492013-03-28 01:56:22435 // For interrupted downloads, Start() will transition the state to
436 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
437 // For new downloads, we notify here, rather than earlier, so that
438 // the download_file is bound to download and all the usual
439 // setters (e.g. Cancel) work.
440 if (new_download)
441 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]96c3bdbaa2012-08-31 16:39:54442
[email protected]530047e2013-07-12 17:02:25443 if (!on_started.is_null())
[email protected]7f3918882014-01-14 06:14:56444 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]287b86b2011-02-26 00:11:35445}
446
[email protected]5656f8a2011-11-17 16:12:58447void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]09ea72c7422012-07-02 20:35:40449 for (DownloadMap::iterator it = downloads_.begin();
450 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16451 DownloadItemImpl* item = it->second;
[email protected]3d95e542012-11-20 00:52:08452 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48453 }
454}
455
[email protected]db1d8f72012-07-13 19:23:16456void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
[email protected]9fc114672011-06-15 08:17:48457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]803036e02013-06-15 17:08:47458 if ((download_item->GetState() == DownloadItem::COMPLETE) &&
[email protected]4c544cd2013-01-25 07:54:16459 !download_item->GetFileExternallyRemoved() &&
460 delegate_) {
461 delegate_->CheckForFileExistence(
462 download_item,
463 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
[email protected]eba4a4d2013-05-29 02:18:06464 weak_factory_.GetWeakPtr(), download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48465 }
466}
467
[email protected]530047e2013-07-12 17:02:25468void DownloadManagerImpl::OnFileExistenceChecked(uint32 download_id,
[email protected]4c544cd2013-01-25 07:54:16469 bool result) {
[email protected]9fc114672011-06-15 08:17:48470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4c544cd2013-01-25 07:54:16471 if (!result) { // File does not exist.
472 if (ContainsKey(downloads_, download_id))
[email protected]1c63da02014-01-17 17:09:51473 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]4c544cd2013-01-25 07:54:16474 }
[email protected]9fc114672011-06-15 08:17:48475}
476
[email protected]35869622012-10-26 23:23:55477BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58478 return browser_context_;
479}
480
[email protected]530047e2013-07-12 17:02:25481void DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]2dec8ec2013-02-07 19:20:34482 const base::FilePath& main_file_path,
[email protected]fc03de22011-12-06 23:28:12483 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27484 const std::string& mime_type,
[email protected]b3756012013-03-06 17:43:02485 scoped_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]530047e2013-07-12 17:02:25486 const DownloadItemImplCreated& item_created) {
487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
488 GetNextId(base::Bind(
489 &DownloadManagerImpl::CreateSavePackageDownloadItemWithId,
490 weak_factory_.GetWeakPtr(),
491 main_file_path,
492 page_url,
493 mime_type,
494 base::Passed(request_handle.Pass()),
495 item_created));
496}
497
498void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
499 const base::FilePath& main_file_path,
500 const GURL& page_url,
501 const std::string& mime_type,
502 scoped_ptr<DownloadRequestHandleInterface> request_handle,
503 const DownloadItemImplCreated& item_created,
504 uint32 id) {
505 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
506 DCHECK_NE(content::DownloadItem::kInvalidId, id);
507 DCHECK(!ContainsKey(downloads_, id));
[email protected]ef17c9a2012-02-09 05:08:09508 net::BoundNetLog bound_net_log =
509 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08510 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
[email protected]6474b112012-05-04 19:35:27511 this,
[email protected]530047e2013-07-12 17:02:25512 id,
[email protected]6474b112012-05-04 19:35:27513 main_file_path,
514 page_url,
[email protected]6474b112012-05-04 19:35:27515 mime_type,
[email protected]b3756012013-03-06 17:43:02516 request_handle.Pass(),
[email protected]6474b112012-05-04 19:35:27517 bound_net_log);
[email protected]3d95e542012-11-20 00:52:08518 downloads_[download_item->GetId()] = download_item;
519 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
520 this, download_item));
[email protected]530047e2013-07-12 17:02:25521 if (!item_created.is_null())
522 item_created.Run(download_item);
[email protected]fc03de22011-12-06 23:28:12523}
524
[email protected]df58aa82013-01-24 21:54:00525void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
526 DownloadItem* download_item) {
527 FOR_EACH_OBSERVER(Observer, observers_,
528 OnSavePackageSuccessfullyFinished(this, download_item));
529}
530
[email protected]8d68a3e02013-01-12 15:57:10531// Resume a download of a specific URL. We send the request to the
532// ResourceDispatcherHost, and let it send us responses like a regular
533// download.
534void DownloadManagerImpl::ResumeInterruptedDownload(
535 scoped_ptr<content::DownloadUrlParameters> params,
[email protected]530047e2013-07-12 17:02:25536 uint32 id) {
[email protected]11325a22013-07-30 23:02:26537 RecordDownloadSource(INITIATED_BY_RESUMPTION);
[email protected]8d68a3e02013-01-12 15:57:10538 BrowserThread::PostTask(
539 BrowserThread::IO,
540 FROM_HERE,
[email protected]c02087b512013-02-04 03:09:20541 base::Bind(&BeginDownload, base::Passed(&params), id));
[email protected]8d68a3e02013-01-12 15:57:10542}
543
[email protected]53ac00e82012-10-18 20:59:20544void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
[email protected]35869622012-10-26 23:23:55545 scoped_ptr<DownloadItemFactory> item_factory) {
[email protected]53ac00e82012-10-18 20:59:20546 item_factory_ = item_factory.Pass();
547}
548
549void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
[email protected]35869622012-10-26 23:23:55550 scoped_ptr<DownloadFileFactory> file_factory) {
[email protected]53ac00e82012-10-18 20:59:20551 file_factory_ = file_factory.Pass();
552}
553
[email protected]35869622012-10-26 23:23:55554DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20555 return file_factory_.get();
556}
557
[email protected]db1d8f72012-07-13 19:23:16558void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]237af6d2013-03-15 23:45:28559 if (!download)
[email protected]09ea72c7422012-07-02 20:35:40560 return;
561
[email protected]530047e2013-07-12 17:02:25562 uint32 download_id = download->GetId();
[email protected]6065748f2013-10-29 01:07:43563 if (downloads_.erase(download_id) == 0)
[email protected]237af6d2013-03-15 23:45:28564 return;
[email protected]237af6d2013-03-15 23:45:28565 delete download;
initial.commit09911bf2008-07-26 23:55:29566}
567
[email protected]fd3a82832012-01-19 20:35:12568int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
569 base::Time remove_end) {
[email protected]237af6d2013-03-15 23:45:28570 int count = 0;
571 DownloadMap::const_iterator it = downloads_.begin();
572 while (it != downloads_.end()) {
[email protected]db1d8f72012-07-13 19:23:16573 DownloadItemImpl* download = it->second;
[email protected]237af6d2013-03-15 23:45:28574
575 // Increment done here to protect against invalidation below.
576 ++it;
577
[email protected]3d95e542012-11-20 00:52:08578 if (download->GetStartTime() >= remove_begin &&
[email protected]c09a8fd2011-11-21 19:54:50579 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]803036e02013-06-15 17:08:47580 (download->GetState() != DownloadItem::IN_PROGRESS)) {
[email protected]237af6d2013-03-15 23:45:28581 // Erases the download from downloads_.
582 download->Remove();
583 count++;
initial.commit09911bf2008-07-26 23:55:29584 }
initial.commit09911bf2008-07-26 23:55:29585 }
[email protected]237af6d2013-03-15 23:45:28586 return count;
initial.commit09911bf2008-07-26 23:55:29587}
588
[email protected]fd3a82832012-01-19 20:35:12589int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59590 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29591}
592
[email protected]5656f8a2011-11-17 16:12:58593int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]d41355e6f2009-04-07 21:21:12594 // The null times make the date range unbounded.
[email protected]09ea72c7422012-07-02 20:35:40595 int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time());
[email protected]35869622012-10-26 23:23:55596 RecordClearAllSize(num_deleted);
[email protected]09ea72c7422012-07-02 20:35:40597 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12598}
599
[email protected]0d4e30c2012-01-28 00:47:53600void DownloadManagerImpl::DownloadUrl(
[email protected]35869622012-10-26 23:23:55601 scoped_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14602 if (params->post_id() >= 0) {
603 // Check this here so that the traceback is more useful.
604 DCHECK(params->prefer_cache());
[email protected]6065748f2013-10-29 01:07:43605 DCHECK_EQ("POST", params->method());
[email protected]c5a5c0842012-05-04 20:05:14606 }
607 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
[email protected]530047e2013-07-12 17:02:25608 &BeginDownload, base::Passed(&params),
609 content::DownloadItem::kInvalidId));
initial.commit09911bf2008-07-26 23:55:29610}
611
[email protected]5656f8a2011-11-17 16:12:58612void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29613 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29614}
615
[email protected]5656f8a2011-11-17 16:12:58616void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29617 observers_.RemoveObserver(observer);
618}
619
[email protected]3d95e542012-11-20 00:52:08620DownloadItem* DownloadManagerImpl::CreateDownloadItem(
[email protected]530047e2013-07-12 17:02:25621 uint32 id,
[email protected]2dec8ec2013-02-07 19:20:34622 const base::FilePath& current_path,
623 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15624 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08625 const GURL& referrer_url,
[email protected]0e648562014-06-12 19:39:45626 const std::string& mime_type,
627 const std::string& original_mime_type,
[email protected]3d95e542012-11-20 00:52:08628 const base::Time& start_time,
629 const base::Time& end_time,
[email protected]56cd5942013-08-08 23:53:21630 const std::string& etag,
631 const std::string& last_modified,
[email protected]3d95e542012-11-20 00:52:08632 int64 received_bytes,
633 int64 total_bytes,
634 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15635 DownloadDangerType danger_type,
636 DownloadInterruptReason interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08637 bool opened) {
[email protected]6065748f2013-10-29 01:07:43638 if (ContainsKey(downloads_, id)) {
639 NOTREACHED();
[email protected]530047e2013-07-12 17:02:25640 return NULL;
[email protected]6065748f2013-10-29 01:07:43641 }
[email protected]3d95e542012-11-20 00:52:08642 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
643 this,
[email protected]530047e2013-07-12 17:02:25644 id,
[email protected]876774692013-02-03 17:20:15645 current_path,
646 target_path,
647 url_chain,
[email protected]3d95e542012-11-20 00:52:08648 referrer_url,
[email protected]0e648562014-06-12 19:39:45649 mime_type,
650 original_mime_type,
[email protected]3d95e542012-11-20 00:52:08651 start_time,
652 end_time,
[email protected]56cd5942013-08-08 23:53:21653 etag,
654 last_modified,
[email protected]3d95e542012-11-20 00:52:08655 received_bytes,
656 total_bytes,
657 state,
[email protected]876774692013-02-03 17:20:15658 danger_type,
659 interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08660 opened,
661 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
[email protected]530047e2013-07-12 17:02:25662 downloads_[id] = item;
[email protected]3d95e542012-11-20 00:52:08663 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
664 VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
665 return item;
initial.commit09911bf2008-07-26 23:55:29666}
667
[email protected]5656f8a2011-11-17 16:12:58668int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56669 int count = 0;
[email protected]fd6ddb82012-11-27 00:38:43670 for (DownloadMap::const_iterator it = downloads_.begin();
671 it != downloads_.end(); ++it) {
[email protected]803036e02013-06-15 17:08:47672 if (it->second->GetState() == DownloadItem::IN_PROGRESS)
[email protected]007e7412012-03-13 20:10:56673 ++count;
674 }
675 return count;
[email protected]5656f8a2011-11-17 16:12:58676}
677
[email protected]422a7d12013-10-21 12:10:42678int DownloadManagerImpl::NonMaliciousInProgressCount() const {
679 int count = 0;
680 for (DownloadMap::const_iterator it = downloads_.begin();
681 it != downloads_.end(); ++it) {
682 if (it->second->GetState() == DownloadItem::IN_PROGRESS &&
683 it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL &&
684 it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT &&
[email protected]24eb5172013-10-27 02:03:59685 it->second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST &&
686 it->second->GetDangerType() !=
687 DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) {
[email protected]422a7d12013-10-21 12:10:42688 ++count;
689 }
690 }
691 return count;
692}
693
[email protected]530047e2013-07-12 17:02:25694DownloadItem* DownloadManagerImpl::GetDownload(uint32 download_id) {
[email protected]ba7895d2012-06-22 19:02:52695 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL;
696}
697
[email protected]9f1f4092012-09-10 21:18:04698void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
699 for (DownloadMap::iterator it = downloads_.begin();
700 it != downloads_.end(); ++it) {
701 downloads->push_back(it->second);
702 }
703}
704
[email protected]ffc0c2d2013-01-21 15:32:12705void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06706 int num_unopened = 0;
[email protected]09ea72c7422012-07-02 20:35:40707 for (DownloadMap::iterator it = downloads_.begin();
708 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16709 DownloadItemImpl* item = it->second;
[email protected]803036e02013-06-15 17:08:47710 if ((item->GetState() == DownloadItem::COMPLETE) &&
[email protected]09ea72c7422012-07-02 20:35:40711 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06712 ++num_unopened;
713 }
[email protected]35869622012-10-26 23:23:55714 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12715
[email protected]0baf5922013-01-30 13:55:18716 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12717 delegate_->OpenDownload(download);
718}
719
720void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
721 if (delegate_)
722 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06723}
[email protected]5656f8a2011-11-17 16:12:58724
[email protected]35869622012-10-26 23:23:55725} // namespace content