blob: 32328b42f9a5734824d0bb3e72b3d88a7d8db4f0 [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"
initial.commit09911bf2008-07-26 23:55:2912#include "base/file_util.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"
[email protected]f1675202012-07-09 15:18:0015#include "base/message_loop.h"
[email protected]7286e3fc2011-07-19 22:13:2416#include "base/stl_util.h"
[email protected]eda58402011-09-21 19:32:0217#include "base/stringprintf.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"
20#include "base/sys_string_conversions.h"
[email protected]d2a8fb72010-01-21 05:31:4221#include "build/build_config.h"
[email protected]2bddd2072012-06-18 16:16:5122#include "content/browser/download/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]c38831a12011-10-28 12:44:4932#include "content/public/browser/browser_thread.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"
[email protected]c5a5c0842012-05-04 20:05:1443#include "net/base/load_flags.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"
[email protected]f859eba2012-05-30 17:22:4947#include "webkit/glue/webkit_glue.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
[email protected]8d68a3e02013-01-12 15:57:1052void BeginDownload(scoped_ptr<DownloadUrlParameters> params,
53 DownloadId download_id) {
[email protected]c5a5c0842012-05-04 20:05:1454 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
55 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
56 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
57 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
[email protected]eb795632012-09-01 00:19:3158 scoped_ptr<net::URLRequest> request(
59 params->resource_context()->GetRequestContext()->CreateRequest(
60 params->url(), NULL));
[email protected]f859eba2012-05-30 17:22:4961 request->set_referrer(params->referrer().url.spec());
62 webkit_glue::ConfigureURLRequestForReferrerPolicy(
63 request.get(), params->referrer().policy);
[email protected]c5a5c0842012-05-04 20:05:1464 request->set_load_flags(request->load_flags() | params->load_flags());
65 request->set_method(params->method());
[email protected]fdae8bf2012-11-20 04:02:4566 if (!params->post_body().empty()) {
[email protected]f288ef02012-12-15 20:28:2867 const std::string& body = params->post_body();
68 scoped_ptr<net::UploadElementReader> reader(
69 net::UploadOwnedBytesElementReader::CreateWithString(body));
70 request->set_upload(make_scoped_ptr(
71 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)));
[email protected]fdae8bf2012-11-20 04:02:4572 }
[email protected]c5a5c0842012-05-04 20:05:1473 if (params->post_id() >= 0) {
[email protected]27678b2a2012-02-04 22:09:1474 // The POST in this case does not have an actual body, and only works
75 // when retrieving data from cache. This is done because we don't want
76 // to do a re-POST without user consent, and currently don't have a good
77 // plan on how to display the UI for that.
[email protected]c5a5c0842012-05-04 20:05:1478 DCHECK(params->prefer_cache());
79 DCHECK(params->method() == "POST");
[email protected]f288ef02012-12-15 20:28:2880 ScopedVector<net::UploadElementReader> element_readers;
81 request->set_upload(make_scoped_ptr(
82 new net::UploadDataStream(&element_readers, params->post_id())));
[email protected]27678b2a2012-02-04 22:09:1483 }
[email protected]8d68a3e02013-01-12 15:57:1084
85 // If we're not at the beginning of the file, retrieve only the remaining
86 // portion.
87 if (params->offset() > 0) {
88 request->SetExtraRequestHeaderByName(
89 "Range",
90 StringPrintf("bytes=%" PRId64 "-", params->offset()),
91 true);
92
93 // If we've asked for a range, we want to make sure that we only
94 // get that range if our current copy of the information is good.
95 if (!params->last_modified().empty()) {
96 request->SetExtraRequestHeaderByName("If-Unmodified-Since",
97 params->last_modified(),
98 true);
99 }
100 if (!params->etag().empty()) {
101 request->SetExtraRequestHeaderByName("If-Match", params->etag(), true);
102 }
103 }
104
[email protected]35869622012-10-26 23:23:55105 for (DownloadUrlParameters::RequestHeadersType::const_iterator iter
[email protected]c5a5c0842012-05-04 20:05:14106 = params->request_headers_begin();
107 iter != params->request_headers_end();
108 ++iter) {
109 request->SetExtraRequestHeaderByName(
110 iter->first, iter->second, false/*overwrite*/);
111 }
[email protected]49d812e2012-11-06 21:18:12112
113 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo());
114 save_info->file_path = params->file_path();
115 save_info->suggested_name = params->suggested_name();
116 save_info->offset = params->offset();
117 save_info->hash_state = params->hash_state();
118 save_info->prompt_for_save_location = params->prompt();
119 save_info->file_stream = params->GetFileStream();
120
[email protected]56f0b082012-06-14 07:12:32121 params->resource_dispatcher_host()->BeginDownload(
[email protected]ea114722012-03-12 01:11:25122 request.Pass(),
[email protected]a53e2f92012-05-15 15:27:06123 params->content_initiated(),
[email protected]c5a5c0842012-05-04 20:05:14124 params->resource_context(),
125 params->render_process_host_id(),
126 params->render_view_host_routing_id(),
127 params->prefer_cache(),
[email protected]49d812e2012-11-06 21:18:12128 save_info.Pass(),
[email protected]8d68a3e02013-01-12 15:57:10129 download_id,
[email protected]c5a5c0842012-05-04 20:05:14130 params->callback());
[email protected]a0ce3282011-08-19 20:49:52131}
132
[email protected]33d22102012-01-25 17:46:53133class MapValueIteratorAdapter {
134 public:
135 explicit MapValueIteratorAdapter(
136 base::hash_map<int64, DownloadItem*>::const_iterator iter)
137 : iter_(iter) {
138 }
139 ~MapValueIteratorAdapter() {}
140
141 DownloadItem* operator*() { return iter_->second; }
142
143 MapValueIteratorAdapter& operator++() {
144 ++iter_;
145 return *this;
146 }
147
148 bool operator!=(const MapValueIteratorAdapter& that) const {
149 return iter_ != that.iter_;
150 }
151
152 private:
153 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
154 // Allow copy and assign.
155};
156
[email protected]53ac00e82012-10-18 20:59:20157void EnsureNoPendingDownloadJobsOnFile(bool* result) {
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]35869622012-10-26 23:23:55159 *result = (DownloadFile::GetNumberOfDownloadFiles() == 0);
[email protected]5948e1a2012-03-10 00:19:18160 BrowserThread::PostTask(
161 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
162}
163
[email protected]35869622012-10-26 23:23:55164class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03165 public:
166 DownloadItemFactoryImpl() {}
167 virtual ~DownloadItemFactoryImpl() {}
168
[email protected]db1d8f72012-07-13 19:23:16169 virtual DownloadItemImpl* CreatePersistedItem(
170 DownloadItemImplDelegate* delegate,
[email protected]35869622012-10-26 23:23:55171 DownloadId download_id,
[email protected]3d95e542012-11-20 00:52:08172 const FilePath& path,
173 const GURL& url,
174 const GURL& referrer_url,
175 const base::Time& start_time,
176 const base::Time& end_time,
177 int64 received_bytes,
178 int64 total_bytes,
179 DownloadItem::DownloadState state,
180 bool opened,
[email protected]d25fda12012-06-12 17:05:03181 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]3d95e542012-11-20 00:52:08182 return new DownloadItemImpl(
183 delegate,
184 download_id,
185 path,
186 url,
187 referrer_url,
188 start_time,
189 end_time,
190 received_bytes,
191 total_bytes,
192 state,
193 opened,
194 bound_net_log);
[email protected]d25fda12012-06-12 17:05:03195 }
196
[email protected]db1d8f72012-07-13 19:23:16197 virtual DownloadItemImpl* CreateActiveItem(
198 DownloadItemImplDelegate* delegate,
[email protected]d25fda12012-06-12 17:05:03199 const DownloadCreateInfo& info,
[email protected]d25fda12012-06-12 17:05:03200 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]8d68a3e02013-01-12 15:57:10201 return new DownloadItemImpl(delegate, info, bound_net_log);
[email protected]d25fda12012-06-12 17:05:03202 }
203
[email protected]db1d8f72012-07-13 19:23:16204 virtual DownloadItemImpl* CreateSavePageItem(
205 DownloadItemImplDelegate* delegate,
[email protected]d25fda12012-06-12 17:05:03206 const FilePath& path,
207 const GURL& url,
[email protected]35869622012-10-26 23:23:55208 DownloadId download_id,
[email protected]d25fda12012-06-12 17:05:03209 const std::string& mime_type,
210 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]66521d02012-08-14 17:51:54211 return new DownloadItemImpl(delegate, path, url, download_id,
[email protected]d25fda12012-06-12 17:05:03212 mime_type, bound_net_log);
213 }
214};
215
[email protected]a0ce3282011-08-19 20:49:52216} // namespace
217
[email protected]d25fda12012-06-12 17:05:03218DownloadManagerImpl::DownloadManagerImpl(
[email protected]d25fda12012-06-12 17:05:03219 net::NetLog* net_log)
[email protected]53ac00e82012-10-18 20:59:20220 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55221 file_factory_(new DownloadFileFactory()),
[email protected]09ea72c7422012-07-02 20:35:40222 history_size_(0),
[email protected]d25fda12012-06-12 17:05:03223 shutdown_needed_(false),
[email protected]b441a8492012-06-06 14:55:57224 browser_context_(NULL),
[email protected]33c20cd92012-06-14 22:02:50225 delegate_(NULL),
[email protected]b441a8492012-06-06 14:55:57226 net_log_(net_log) {
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]5656f8a2011-11-17 16:12:58233DownloadId DownloadManagerImpl::GetNextId() {
[email protected]491aaa62012-06-07 03:50:18234 DownloadId id;
235 if (delegate_)
236 id = delegate_->GetNextId();
237 if (!id.IsValid()) {
238 static int next_id;
239 id = DownloadId(browser_context_, ++next_id);
240 }
241
242 return id;
[email protected]2909e342011-10-29 00:46:53243}
244
[email protected]53ac00e82012-10-18 20:59:20245void DownloadManagerImpl::DetermineDownloadTarget(
246 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
247 // Note that this next call relies on
248 // DownloadItemImplDelegate::DownloadTargetCallback and
249 // DownloadManagerDelegate::DownloadTargetCallback having the same
250 // type. If the types ever diverge, gasket code will need to
251 // be written here.
252 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
253 FilePath target_path = item->GetForcedFilePath();
254 // TODO(asanka): Determine a useful path if |target_path| is empty.
255 callback.Run(target_path,
256 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
[email protected]35869622012-10-26 23:23:55257 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
[email protected]53ac00e82012-10-18 20:59:20258 target_path);
259 }
[email protected]47665442012-07-27 02:31:22260}
261
[email protected]98299622013-01-03 22:26:50262bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43263 DownloadItemImpl* item, const base::Closure& complete_callback) {
264 if (!delegate_ ||
265 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50266 return true;
[email protected]18710a42a2012-10-17 19:50:43267 }
268 // Otherwise, the delegate has accepted responsibility to run the
269 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50270 return false;
[email protected]fc03de22011-12-06 23:28:12271}
272
273bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18274 if (!delegate_)
275 return false;
276
[email protected]fc03de22011-12-06 23:28:12277 return delegate_->ShouldOpenFileBasedOnExtension(path);
278}
279
[email protected]bde0e4f2012-11-08 22:49:59280bool DownloadManagerImpl::ShouldOpenDownload(
281 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43282 if (!delegate_)
283 return true;
284
[email protected]bde0e4f2012-11-08 22:49:59285 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
286 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
287 // to have the same type :-}.
288 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43289}
290
[email protected]35869622012-10-26 23:23:55291void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57292 delegate_ = delegate;
293}
294
[email protected]35869622012-10-26 23:23:55295DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28296 return delegate_;
297}
298
[email protected]5656f8a2011-11-17 16:12:58299void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50300 VLOG(20) << __FUNCTION__ << "()"
301 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13302 if (!shutdown_needed_)
303 return;
304 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29305
[email protected]75e51b52012-02-04 16:57:54306 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08307 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13308
[email protected]f04182f32010-12-10 19:12:07309 // Go through all downloads in downloads_. Dangerous ones we need to
310 // remove on disk, and in progress ones we need to cancel.
[email protected]ba7895d2012-06-22 19:02:52311 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]db1d8f72012-07-13 19:23:16312 DownloadItemImpl* download = it->second;
[email protected]f04182f32010-12-10 19:12:07313
314 // Save iterator from potential erases in this set done by called code.
315 // Iterators after an erasure point are still valid for lists and
316 // associative containers such as sets.
317 it++;
318
[email protected]c09a8fd2011-11-21 19:54:50319 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29320 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07321 // The user hasn't accepted it, so we need to remove it
322 // from the disk. This may or may not result in it being
323 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12324 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07325 // removes and deletes it if it's known to the history service)
326 // so the only thing we know after calling this function is that
327 // the download was deleted if-and-only-if it was removed
328 // from all queues.
[email protected]303077002011-04-19 23:21:01329 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26330 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17331 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29332 }
333 }
334
[email protected]f04182f32010-12-10 19:12:07335 // At this point, all dangerous downloads have had their files removed
336 // and all in progress downloads have been cancelled. We can now delete
337 // anything left.
[email protected]9ccbb372008-10-10 18:50:32338
[email protected]7745ff02012-10-01 00:09:24339 STLDeleteValues(&downloads_);
[email protected]7e834f02012-08-09 20:38:56340 downloads_.clear();
initial.commit09911bf2008-07-26 23:55:29341
[email protected]41f558fb2012-01-09 22:59:58342 // We'll have nothing more to report to the observers after this point.
343 observers_.Clear();
344
[email protected]b441a8492012-06-06 14:55:57345 if (delegate_)
346 delegate_->Shutdown();
[email protected]47665442012-07-27 02:31:22347 delegate_ = NULL;
initial.commit09911bf2008-07-26 23:55:29348}
349
[email protected]35869622012-10-26 23:23:55350bool DownloadManagerImpl::Init(BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03351 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29352 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
353 shutdown_needed_ = true;
354
[email protected]6d0c9fb2011-08-22 19:26:03355 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46356
initial.commit09911bf2008-07-26 23:55:29357 return true;
358}
359
[email protected]3d43eef2012-10-09 23:17:56360DownloadItem* DownloadManagerImpl::StartDownload(
[email protected]2bddd2072012-06-18 16:16:51361 scoped_ptr<DownloadCreateInfo> info,
[email protected]35869622012-10-26 23:23:55362 scoped_ptr<ByteStreamReader> stream) {
[email protected]ca4b5fa32010-10-09 12:42:18363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35364
[email protected]53ac00e82012-10-18 20:59:20365 FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01366 if (delegate_) {
367 FilePath website_save_directory; // Unused
368 bool skip_dir_check = false; // Unused
369 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20370 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01371 }
372
[email protected]53ac00e82012-10-18 20:59:20373 // We create the DownloadItem before the DownloadFile because the
374 // DownloadItem already needs to handle a state in which there is
375 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads)
[email protected]8d68a3e02013-01-12 15:57:10376 DownloadItemImpl* download = GetOrCreateDownloadItem(info.get());
[email protected]35869622012-10-26 23:23:55377 scoped_ptr<DownloadFile> download_file(
[email protected]53ac00e82012-10-18 20:59:20378 file_factory_->CreateFile(
379 info->save_info.Pass(), default_download_directory,
380 info->url(), info->referrer_url,
[email protected]3a2b2b5c2012-11-01 23:55:30381 delegate_->GenerateFileHash(),
[email protected]8d68a3e02013-01-12 15:57:10382 stream.Pass(), download->GetBoundNetLog(),
[email protected]53ac00e82012-10-18 20:59:20383 download->DestinationObserverAsWeakPtr()));
[email protected]8d68a3e02013-01-12 15:57:10384 scoped_ptr<DownloadRequestHandleInterface> req_handle(
385 new DownloadRequestHandle(info->request_handle));
386 download->Start(download_file.Pass(), req_handle.Pass());
[email protected]96c3bdbaa2012-08-31 16:39:54387
[email protected]53ac00e82012-10-18 20:59:20388 // Delay notification until after Start() so that download_file is bound
389 // to download and all the usual setters (e.g. Cancel) work.
390 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]96c3bdbaa2012-08-31 16:39:54391
[email protected]53ac00e82012-10-18 20:59:20392 return download;
[email protected]287b86b2011-02-26 00:11:35393}
394
[email protected]5656f8a2011-11-17 16:12:58395void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48396 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]09ea72c7422012-07-02 20:35:40397 for (DownloadMap::iterator it = downloads_.begin();
398 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16399 DownloadItemImpl* item = it->second;
[email protected]3d95e542012-11-20 00:52:08400 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48401 }
402}
403
[email protected]db1d8f72012-07-13 19:23:16404void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
[email protected]9fc114672011-06-15 08:17:48405 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
406 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50407 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48408 BrowserThread::PostTask(
409 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58410 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]09ea72c7422012-07-02 20:35:40411 this, download_item->GetId(),
[email protected]fabf36d22011-10-28 21:50:17412 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48413 }
414}
415
[email protected]5656f8a2011-11-17 16:12:58416void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]09ea72c7422012-07-02 20:35:40417 int32 download_id, const FilePath& path) {
[email protected]9fc114672011-06-15 08:17:48418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
419 if (!file_util::PathExists(path)) {
420 BrowserThread::PostTask(
421 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58422 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
423 this,
[email protected]09ea72c7422012-07-02 20:35:40424 download_id));
[email protected]9fc114672011-06-15 08:17:48425 }
426}
427
[email protected]09ea72c7422012-07-02 20:35:40428void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) {
[email protected]9fc114672011-06-15 08:17:48429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]db1d8f72012-07-13 19:23:16430 if (ContainsKey(downloads_, download_id))
431 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]9fc114672011-06-15 08:17:48432}
433
[email protected]35869622012-10-26 23:23:55434BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58435 return browser_context_;
436}
437
[email protected]8d68a3e02013-01-12 15:57:10438DownloadItemImpl* DownloadManagerImpl::GetOrCreateDownloadItem(
439 DownloadCreateInfo* info) {
[email protected]c2e76012010-12-23 21:10:29440 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
441
[email protected]d25fda12012-06-12 17:05:03442 if (!info->download_id.IsValid())
443 info->download_id = GetNextId();
[email protected]d8472d92011-08-26 20:15:20444
[email protected]8d68a3e02013-01-12 15:57:10445 DownloadItemImpl* download = NULL;
446 if (ContainsKey(downloads_, info->download_id.local())) {
447 // Resuming an existing download.
448 download = downloads_[info->download_id.local()];
449 DCHECK(download->IsInterrupted());
450 } else {
451 // New download
452 net::BoundNetLog bound_net_log =
453 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
454 download = item_factory_->CreateActiveItem(this, *info, bound_net_log);
455 downloads_[download->GetId()] = download;
456 }
457
[email protected]53ac00e82012-10-18 20:59:20458 return download;
[email protected]c2e76012010-12-23 21:10:29459}
460
[email protected]db1d8f72012-07-13 19:23:16461DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]fc03de22011-12-06 23:28:12462 const FilePath& main_file_path,
463 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27464 const std::string& mime_type,
[email protected]fc03de22011-12-06 23:28:12465 DownloadItem::Observer* observer) {
[email protected]ef17c9a2012-02-09 05:08:09466 net::BoundNetLog bound_net_log =
467 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08468 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
[email protected]6474b112012-05-04 19:35:27469 this,
470 main_file_path,
471 page_url,
[email protected]6474b112012-05-04 19:35:27472 GetNextId(),
473 mime_type,
474 bound_net_log);
[email protected]fc03de22011-12-06 23:28:12475
[email protected]3d95e542012-11-20 00:52:08476 download_item->AddObserver(observer);
477 DCHECK(!ContainsKey(downloads_, download_item->GetId()));
478 downloads_[download_item->GetId()] = download_item;
479 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
480 this, download_item));
[email protected]fc03de22011-12-06 23:28:12481
[email protected]3d95e542012-11-20 00:52:08482 // TODO(asanka): Make the ui an observer.
483 ShowDownloadInBrowser(download_item);
[email protected]fc03de22011-12-06 23:28:12484
[email protected]3d95e542012-11-20 00:52:08485 return download_item;
[email protected]fc03de22011-12-06 23:28:12486}
487
[email protected]5656f8a2011-11-17 16:12:58488void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]fd6ddb82012-11-27 00:38:43489 DownloadItem* download = GetDownload(download_id);
490 if (!download || !download->IsInProgress())
491 return;
492 download->Cancel(true);
[email protected]93af2272011-09-21 18:29:17493}
494
[email protected]8d68a3e02013-01-12 15:57:10495// Resume a download of a specific URL. We send the request to the
496// ResourceDispatcherHost, and let it send us responses like a regular
497// download.
498void DownloadManagerImpl::ResumeInterruptedDownload(
499 scoped_ptr<content::DownloadUrlParameters> params,
500 content::DownloadId id) {
501 BrowserThread::PostTask(
502 BrowserThread::IO,
503 FROM_HERE,
504 base::Bind(&BeginDownload, base::Passed(params.Pass()), id));
505}
506
[email protected]53ac00e82012-10-18 20:59:20507void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
[email protected]35869622012-10-26 23:23:55508 scoped_ptr<DownloadItemFactory> item_factory) {
[email protected]53ac00e82012-10-18 20:59:20509 item_factory_ = item_factory.Pass();
510}
511
512void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
[email protected]35869622012-10-26 23:23:55513 scoped_ptr<DownloadFileFactory> file_factory) {
[email protected]53ac00e82012-10-18 20:59:20514 file_factory_ = file_factory.Pass();
515}
516
[email protected]35869622012-10-26 23:23:55517DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20518 return file_factory_.get();
519}
520
[email protected]5656f8a2011-11-17 16:12:58521int DownloadManagerImpl::RemoveDownloadItems(
[email protected]db1d8f72012-07-13 19:23:16522 const DownloadItemImplVector& pending_deletes) {
[email protected]6d0146c2011-08-04 19:13:04523 if (pending_deletes.empty())
524 return 0;
525
526 // Delete from internal maps.
[email protected]db1d8f72012-07-13 19:23:16527 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin();
[email protected]7e834f02012-08-09 20:38:56528 it != pending_deletes.end();
529 ++it) {
[email protected]db1d8f72012-07-13 19:23:16530 DownloadItemImpl* download = *it;
[email protected]6d0146c2011-08-04 19:13:04531 DCHECK(download);
[email protected]7e834f02012-08-09 20:38:56532 int32 download_id = download->GetId();
533 delete download;
534 downloads_.erase(download_id);
[email protected]6d0146c2011-08-04 19:13:04535 }
[email protected]7e834f02012-08-09 20:38:56536 return static_cast<int>(pending_deletes.size());
[email protected]6d0146c2011-08-04 19:13:04537}
538
[email protected]db1d8f72012-07-13 19:23:16539void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]09ea72c7422012-07-02 20:35:40540 if (!download ||
541 downloads_.find(download->GetId()) == downloads_.end())
542 return;
543
initial.commit09911bf2008-07-26 23:55:29544 // Remove from our tables and delete.
[email protected]db1d8f72012-07-13 19:23:16545 int downloads_count =
546 RemoveDownloadItems(DownloadItemImplVector(1, download));
[email protected]f04182f32010-12-10 19:12:07547 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29548}
549
[email protected]fd3a82832012-01-19 20:35:12550int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
551 base::Time remove_end) {
[email protected]db1d8f72012-07-13 19:23:16552 DownloadItemImplVector pending_deletes;
[email protected]09ea72c7422012-07-02 20:35:40553 for (DownloadMap::const_iterator it = downloads_.begin();
554 it != downloads_.end();
[email protected]6d0146c2011-08-04 19:13:04555 ++it) {
[email protected]db1d8f72012-07-13 19:23:16556 DownloadItemImpl* download = it->second;
[email protected]3d95e542012-11-20 00:52:08557 if (download->GetStartTime() >= remove_begin &&
[email protected]c09a8fd2011-11-21 19:54:50558 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04559 (download->IsComplete() || download->IsCancelled())) {
[email protected]7e834f02012-08-09 20:38:56560 download->NotifyRemoved();
[email protected]78b8fcc92009-03-31 17:36:28561 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29562 }
initial.commit09911bf2008-07-26 23:55:29563 }
[email protected]6d0146c2011-08-04 19:13:04564 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29565}
566
[email protected]fd3a82832012-01-19 20:35:12567int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59568 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29569}
570
[email protected]5656f8a2011-11-17 16:12:58571int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]d41355e6f2009-04-07 21:21:12572 // The null times make the date range unbounded.
[email protected]09ea72c7422012-07-02 20:35:40573 int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time());
[email protected]35869622012-10-26 23:23:55574 RecordClearAllSize(num_deleted);
[email protected]09ea72c7422012-07-02 20:35:40575 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12576}
577
[email protected]0d4e30c2012-01-28 00:47:53578void DownloadManagerImpl::DownloadUrl(
[email protected]35869622012-10-26 23:23:55579 scoped_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14580 if (params->post_id() >= 0) {
581 // Check this here so that the traceback is more useful.
582 DCHECK(params->prefer_cache());
583 DCHECK(params->method() == "POST");
584 }
585 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
[email protected]8d68a3e02013-01-12 15:57:10586 &BeginDownload, base::Passed(&params), DownloadId()));
initial.commit09911bf2008-07-26 23:55:29587}
588
[email protected]5656f8a2011-11-17 16:12:58589void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29590 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29591}
592
[email protected]5656f8a2011-11-17 16:12:58593void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29594 observers_.RemoveObserver(observer);
595}
596
[email protected]3d95e542012-11-20 00:52:08597DownloadItem* DownloadManagerImpl::CreateDownloadItem(
598 const FilePath& path,
599 const GURL& url,
600 const GURL& referrer_url,
601 const base::Time& start_time,
602 const base::Time& end_time,
603 int64 received_bytes,
604 int64 total_bytes,
605 DownloadItem::DownloadState state,
606 bool opened) {
607 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
608 this,
609 GetNextId(),
610 path,
611 url,
612 referrer_url,
613 start_time,
614 end_time,
615 received_bytes,
616 total_bytes,
617 state,
618 opened,
619 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
620 DCHECK(!ContainsKey(downloads_, item->GetId()));
621 downloads_[item->GetId()] = item;
622 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
623 VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
624 return item;
initial.commit09911bf2008-07-26 23:55:29625}
626
[email protected]3d95e542012-11-20 00:52:08627// TODO(asanka) Move into an observer.
[email protected]db1d8f72012-07-13 19:23:16628void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) {
[email protected]a29e4f22012-04-12 21:22:03629 // The 'contents' may no longer exist if the user closed the contents before
630 // we get this start completion event.
[email protected]a62d42902012-01-24 17:24:38631 WebContents* content = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:26632
633 // If the contents no longer exists, we ask the embedder to suggest another
[email protected]a29e4f22012-04-12 21:22:03634 // contents.
[email protected]491aaa62012-06-07 03:50:18635 if (!content && delegate_)
[email protected]ef9572e2012-01-04 22:14:12636 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:53637
[email protected]0bfbf882011-12-22 18:19:27638 if (content && content->GetDelegate())
639 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:53640}
641
[email protected]5656f8a2011-11-17 16:12:58642int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56643 int count = 0;
[email protected]fd6ddb82012-11-27 00:38:43644 for (DownloadMap::const_iterator it = downloads_.begin();
645 it != downloads_.end(); ++it) {
646 if (it->second->IsInProgress())
[email protected]007e7412012-03-13 20:10:56647 ++count;
648 }
649 return count;
[email protected]5656f8a2011-11-17 16:12:58650}
651
[email protected]ba7895d2012-06-22 19:02:52652DownloadItem* DownloadManagerImpl::GetDownload(int download_id) {
653 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL;
654}
655
[email protected]9f1f4092012-09-10 21:18:04656void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
657 for (DownloadMap::iterator it = downloads_.begin();
658 it != downloads_.end(); ++it) {
659 downloads->push_back(it->second);
660 }
661}
662
[email protected]db1d8f72012-07-13 19:23:16663void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06664 int num_unopened = 0;
[email protected]09ea72c7422012-07-02 20:35:40665 for (DownloadMap::iterator it = downloads_.begin();
666 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16667 DownloadItemImpl* item = it->second;
[email protected]09ea72c7422012-07-02 20:35:40668 if (item->IsComplete() &&
669 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06670 ++num_unopened;
671 }
[email protected]35869622012-10-26 23:23:55672 RecordOpensOutstanding(num_unopened);
[email protected]da4a5582011-10-17 19:08:06673}
[email protected]5656f8a2011-11-17 16:12:58674
[email protected]35869622012-10-26 23:23:55675} // namespace content