blob: 6099eca4cf8befcdd88d2f9e95ca982b51be9192 [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]b3c41c0b2012-03-06 15:48:3228#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]ea114722012-03-12 01:11:2529#include "content/browser/renderer_host/resource_dispatcher_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]c5a5c0842012-05-04 20:05:1442#include "net/base/load_flags.h"
[email protected]27678b2a2012-02-04 22:09:1443#include "net/base/upload_data.h"
[email protected]eb795632012-09-01 00:19:3144#include "net/url_request/url_request_context.h"
[email protected]f859eba2012-05-30 17:22:4945#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2946
[email protected]35869622012-10-26 23:23:5547namespace content {
[email protected]a0ce3282011-08-19 20:49:5248namespace {
49
[email protected]35869622012-10-26 23:23:5550void BeginDownload(scoped_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:1451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
52 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
53 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
54 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
[email protected]eb795632012-09-01 00:19:3155 scoped_ptr<net::URLRequest> request(
56 params->resource_context()->GetRequestContext()->CreateRequest(
57 params->url(), NULL));
[email protected]f859eba2012-05-30 17:22:4958 request->set_referrer(params->referrer().url.spec());
59 webkit_glue::ConfigureURLRequestForReferrerPolicy(
60 request.get(), params->referrer().policy);
[email protected]c5a5c0842012-05-04 20:05:1461 request->set_load_flags(request->load_flags() | params->load_flags());
62 request->set_method(params->method());
[email protected]fdae8bf2012-11-20 04:02:4563 if (!params->post_body().empty()) {
64 scoped_refptr<net::UploadData> upload_data(new net::UploadData());
65 upload_data->AppendBytes(params->post_body().data(),
66 params->post_body().size());
67 request->set_upload(upload_data);
68 }
[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());
75 DCHECK(params->method() == "POST");
[email protected]27678b2a2012-02-04 22:09:1476 scoped_refptr<net::UploadData> upload_data = new net::UploadData();
[email protected]c5a5c0842012-05-04 20:05:1477 upload_data->set_identifier(params->post_id());
[email protected]27678b2a2012-02-04 22:09:1478 request->set_upload(upload_data);
79 }
[email protected]35869622012-10-26 23:23:5580 for (DownloadUrlParameters::RequestHeadersType::const_iterator iter
[email protected]c5a5c0842012-05-04 20:05:1481 = params->request_headers_begin();
82 iter != params->request_headers_end();
83 ++iter) {
84 request->SetExtraRequestHeaderByName(
85 iter->first, iter->second, false/*overwrite*/);
86 }
[email protected]49d812e2012-11-06 21:18:1287
88 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo());
89 save_info->file_path = params->file_path();
90 save_info->suggested_name = params->suggested_name();
91 save_info->offset = params->offset();
92 save_info->hash_state = params->hash_state();
93 save_info->prompt_for_save_location = params->prompt();
94 save_info->file_stream = params->GetFileStream();
95
[email protected]56f0b082012-06-14 07:12:3296 params->resource_dispatcher_host()->BeginDownload(
[email protected]ea114722012-03-12 01:11:2597 request.Pass(),
[email protected]a53e2f92012-05-15 15:27:0698 params->content_initiated(),
[email protected]c5a5c0842012-05-04 20:05:1499 params->resource_context(),
100 params->render_process_host_id(),
101 params->render_view_host_routing_id(),
102 params->prefer_cache(),
[email protected]49d812e2012-11-06 21:18:12103 save_info.Pass(),
[email protected]c5a5c0842012-05-04 20:05:14104 params->callback());
[email protected]a0ce3282011-08-19 20:49:52105}
106
[email protected]33d22102012-01-25 17:46:53107class MapValueIteratorAdapter {
108 public:
109 explicit MapValueIteratorAdapter(
110 base::hash_map<int64, DownloadItem*>::const_iterator iter)
111 : iter_(iter) {
112 }
113 ~MapValueIteratorAdapter() {}
114
115 DownloadItem* operator*() { return iter_->second; }
116
117 MapValueIteratorAdapter& operator++() {
118 ++iter_;
119 return *this;
120 }
121
122 bool operator!=(const MapValueIteratorAdapter& that) const {
123 return iter_ != that.iter_;
124 }
125
126 private:
127 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
128 // Allow copy and assign.
129};
130
[email protected]53ac00e82012-10-18 20:59:20131void EnsureNoPendingDownloadJobsOnFile(bool* result) {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]35869622012-10-26 23:23:55133 *result = (DownloadFile::GetNumberOfDownloadFiles() == 0);
[email protected]5948e1a2012-03-10 00:19:18134 BrowserThread::PostTask(
135 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
136}
137
[email protected]35869622012-10-26 23:23:55138class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03139 public:
140 DownloadItemFactoryImpl() {}
141 virtual ~DownloadItemFactoryImpl() {}
142
[email protected]db1d8f72012-07-13 19:23:16143 virtual DownloadItemImpl* CreatePersistedItem(
144 DownloadItemImplDelegate* delegate,
[email protected]35869622012-10-26 23:23:55145 DownloadId download_id,
[email protected]3d95e542012-11-20 00:52:08146 const FilePath& path,
147 const GURL& url,
148 const GURL& referrer_url,
149 const base::Time& start_time,
150 const base::Time& end_time,
151 int64 received_bytes,
152 int64 total_bytes,
153 DownloadItem::DownloadState state,
154 bool opened,
[email protected]d25fda12012-06-12 17:05:03155 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]3d95e542012-11-20 00:52:08156 return new DownloadItemImpl(
157 delegate,
158 download_id,
159 path,
160 url,
161 referrer_url,
162 start_time,
163 end_time,
164 received_bytes,
165 total_bytes,
166 state,
167 opened,
168 bound_net_log);
[email protected]d25fda12012-06-12 17:05:03169 }
170
[email protected]db1d8f72012-07-13 19:23:16171 virtual DownloadItemImpl* CreateActiveItem(
172 DownloadItemImplDelegate* delegate,
[email protected]d25fda12012-06-12 17:05:03173 const DownloadCreateInfo& info,
[email protected]94d841cf2012-06-12 20:58:20174 scoped_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]d25fda12012-06-12 17:05:03175 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]94d841cf2012-06-12 20:58:20176 return new DownloadItemImpl(delegate, info, request_handle.Pass(),
[email protected]66521d02012-08-14 17:51:54177 bound_net_log);
[email protected]d25fda12012-06-12 17:05:03178 }
179
[email protected]db1d8f72012-07-13 19:23:16180 virtual DownloadItemImpl* CreateSavePageItem(
181 DownloadItemImplDelegate* delegate,
[email protected]d25fda12012-06-12 17:05:03182 const FilePath& path,
183 const GURL& url,
[email protected]35869622012-10-26 23:23:55184 DownloadId download_id,
[email protected]d25fda12012-06-12 17:05:03185 const std::string& mime_type,
186 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]66521d02012-08-14 17:51:54187 return new DownloadItemImpl(delegate, path, url, download_id,
[email protected]d25fda12012-06-12 17:05:03188 mime_type, bound_net_log);
189 }
190};
191
[email protected]a0ce3282011-08-19 20:49:52192} // namespace
193
[email protected]d25fda12012-06-12 17:05:03194DownloadManagerImpl::DownloadManagerImpl(
[email protected]d25fda12012-06-12 17:05:03195 net::NetLog* net_log)
[email protected]53ac00e82012-10-18 20:59:20196 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55197 file_factory_(new DownloadFileFactory()),
[email protected]09ea72c7422012-07-02 20:35:40198 history_size_(0),
[email protected]d25fda12012-06-12 17:05:03199 shutdown_needed_(false),
[email protected]b441a8492012-06-06 14:55:57200 browser_context_(NULL),
[email protected]33c20cd92012-06-14 22:02:50201 delegate_(NULL),
[email protected]b441a8492012-06-06 14:55:57202 net_log_(net_log) {
initial.commit09911bf2008-07-26 23:55:29203}
204
[email protected]5656f8a2011-11-17 16:12:58205DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13206 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29207}
208
[email protected]5656f8a2011-11-17 16:12:58209DownloadId DownloadManagerImpl::GetNextId() {
[email protected]491aaa62012-06-07 03:50:18210 DownloadId id;
211 if (delegate_)
212 id = delegate_->GetNextId();
213 if (!id.IsValid()) {
214 static int next_id;
215 id = DownloadId(browser_context_, ++next_id);
216 }
217
218 return id;
[email protected]2909e342011-10-29 00:46:53219}
220
[email protected]53ac00e82012-10-18 20:59:20221void DownloadManagerImpl::DetermineDownloadTarget(
222 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
223 // Note that this next call relies on
224 // DownloadItemImplDelegate::DownloadTargetCallback and
225 // DownloadManagerDelegate::DownloadTargetCallback having the same
226 // type. If the types ever diverge, gasket code will need to
227 // be written here.
228 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
229 FilePath target_path = item->GetForcedFilePath();
230 // TODO(asanka): Determine a useful path if |target_path| is empty.
231 callback.Run(target_path,
232 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
[email protected]35869622012-10-26 23:23:55233 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
[email protected]53ac00e82012-10-18 20:59:20234 target_path);
235 }
[email protected]47665442012-07-27 02:31:22236}
237
[email protected]18710a42a2012-10-17 19:50:43238void DownloadManagerImpl::ReadyForDownloadCompletion(
239 DownloadItemImpl* item, const base::Closure& complete_callback) {
240 if (!delegate_ ||
241 delegate_->ShouldCompleteDownload(item, complete_callback)) {
242 complete_callback.Run();
243 }
244 // Otherwise, the delegate has accepted responsibility to run the
245 // callback when the download is ready for completion.
[email protected]fc03de22011-12-06 23:28:12246}
247
248bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18249 if (!delegate_)
250 return false;
251
[email protected]fc03de22011-12-06 23:28:12252 return delegate_->ShouldOpenFileBasedOnExtension(path);
253}
254
[email protected]bde0e4f2012-11-08 22:49:59255bool DownloadManagerImpl::ShouldOpenDownload(
256 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43257 if (!delegate_)
258 return true;
259
[email protected]bde0e4f2012-11-08 22:49:59260 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
261 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
262 // to have the same type :-}.
263 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43264}
265
[email protected]35869622012-10-26 23:23:55266void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57267 delegate_ = delegate;
268}
269
[email protected]35869622012-10-26 23:23:55270DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28271 return delegate_;
272}
273
[email protected]5656f8a2011-11-17 16:12:58274void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50275 VLOG(20) << __FUNCTION__ << "()"
276 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13277 if (!shutdown_needed_)
278 return;
279 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29280
[email protected]75e51b52012-02-04 16:57:54281 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08282 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13283
[email protected]f04182f32010-12-10 19:12:07284 AssertContainersConsistent();
285
286 // Go through all downloads in downloads_. Dangerous ones we need to
287 // remove on disk, and in progress ones we need to cancel.
[email protected]ba7895d2012-06-22 19:02:52288 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]db1d8f72012-07-13 19:23:16289 DownloadItemImpl* download = it->second;
[email protected]f04182f32010-12-10 19:12:07290
291 // Save iterator from potential erases in this set done by called code.
292 // Iterators after an erasure point are still valid for lists and
293 // associative containers such as sets.
294 it++;
295
[email protected]c09a8fd2011-11-21 19:54:50296 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29297 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07298 // The user hasn't accepted it, so we need to remove it
299 // from the disk. This may or may not result in it being
300 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12301 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07302 // removes and deletes it if it's known to the history service)
303 // so the only thing we know after calling this function is that
304 // the download was deleted if-and-only-if it was removed
305 // from all queues.
[email protected]303077002011-04-19 23:21:01306 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26307 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17308 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29309 }
310 }
311
[email protected]f04182f32010-12-10 19:12:07312 // At this point, all dangerous downloads have had their files removed
313 // and all in progress downloads have been cancelled. We can now delete
314 // anything left.
[email protected]9ccbb372008-10-10 18:50:32315
[email protected]bd466f302012-10-08 17:11:19316 // We delete the downloads before clearing the active_downloads_ map
317 // so that downloads in the COMPLETING_INTERNAL state (which will have
318 // ignored the Cancel() above) will still show up in active_downloads_
319 // in order to satisfy the invariants enforced in AssertStateConsistent().
[email protected]7745ff02012-10-01 00:09:24320 STLDeleteValues(&downloads_);
[email protected]bd466f302012-10-08 17:11:19321 active_downloads_.clear();
[email protected]7e834f02012-08-09 20:38:56322 downloads_.clear();
initial.commit09911bf2008-07-26 23:55:29323
[email protected]41f558fb2012-01-09 22:59:58324 // We'll have nothing more to report to the observers after this point.
325 observers_.Clear();
326
[email protected]b441a8492012-06-06 14:55:57327 if (delegate_)
328 delegate_->Shutdown();
[email protected]47665442012-07-27 02:31:22329 delegate_ = NULL;
initial.commit09911bf2008-07-26 23:55:29330}
331
[email protected]35869622012-10-26 23:23:55332bool DownloadManagerImpl::Init(BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03333 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29334 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
335 shutdown_needed_ = true;
336
[email protected]6d0c9fb2011-08-22 19:26:03337 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46338
initial.commit09911bf2008-07-26 23:55:29339 return true;
340}
341
[email protected]3d43eef2012-10-09 23:17:56342DownloadItem* DownloadManagerImpl::StartDownload(
[email protected]2bddd2072012-06-18 16:16:51343 scoped_ptr<DownloadCreateInfo> info,
[email protected]35869622012-10-26 23:23:55344 scoped_ptr<ByteStreamReader> stream) {
[email protected]ca4b5fa32010-10-09 12:42:18345 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35346
[email protected]53ac00e82012-10-18 20:59:20347 net::BoundNetLog bound_net_log =
348 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]2bddd2072012-06-18 16:16:51349
[email protected]53ac00e82012-10-18 20:59:20350 FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01351 if (delegate_) {
352 FilePath website_save_directory; // Unused
353 bool skip_dir_check = false; // Unused
354 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20355 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01356 }
357
[email protected]53ac00e82012-10-18 20:59:20358 // We create the DownloadItem before the DownloadFile because the
359 // DownloadItem already needs to handle a state in which there is
360 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads)
361 DownloadItemImpl* download =
362 CreateDownloadItem(info.get(), bound_net_log);
[email protected]35869622012-10-26 23:23:55363 scoped_ptr<DownloadFile> download_file(
[email protected]53ac00e82012-10-18 20:59:20364 file_factory_->CreateFile(
365 info->save_info.Pass(), default_download_directory,
366 info->url(), info->referrer_url,
[email protected]3a2b2b5c2012-11-01 23:55:30367 delegate_->GenerateFileHash(),
[email protected]53ac00e82012-10-18 20:59:20368 stream.Pass(), bound_net_log,
369 download->DestinationObserverAsWeakPtr()));
370 download->Start(download_file.Pass());
[email protected]96c3bdbaa2012-08-31 16:39:54371
[email protected]53ac00e82012-10-18 20:59:20372 // Delay notification until after Start() so that download_file is bound
373 // to download and all the usual setters (e.g. Cancel) work.
374 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]96c3bdbaa2012-08-31 16:39:54375
[email protected]53ac00e82012-10-18 20:59:20376 return download;
[email protected]287b86b2011-02-26 00:11:35377}
378
[email protected]5656f8a2011-11-17 16:12:58379void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48380 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]09ea72c7422012-07-02 20:35:40381 for (DownloadMap::iterator it = downloads_.begin();
382 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16383 DownloadItemImpl* item = it->second;
[email protected]3d95e542012-11-20 00:52:08384 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48385 }
386}
387
[email protected]db1d8f72012-07-13 19:23:16388void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
[email protected]9fc114672011-06-15 08:17:48389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
390 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50391 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48392 BrowserThread::PostTask(
393 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58394 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]09ea72c7422012-07-02 20:35:40395 this, download_item->GetId(),
[email protected]fabf36d22011-10-28 21:50:17396 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48397 }
398}
399
[email protected]5656f8a2011-11-17 16:12:58400void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]09ea72c7422012-07-02 20:35:40401 int32 download_id, const FilePath& path) {
[email protected]9fc114672011-06-15 08:17:48402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
403 if (!file_util::PathExists(path)) {
404 BrowserThread::PostTask(
405 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58406 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
407 this,
[email protected]09ea72c7422012-07-02 20:35:40408 download_id));
[email protected]9fc114672011-06-15 08:17:48409 }
410}
411
[email protected]09ea72c7422012-07-02 20:35:40412void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) {
[email protected]9fc114672011-06-15 08:17:48413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]db1d8f72012-07-13 19:23:16414 if (ContainsKey(downloads_, download_id))
415 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]9fc114672011-06-15 08:17:48416}
417
[email protected]35869622012-10-26 23:23:55418BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58419 return browser_context_;
420}
421
[email protected]53ac00e82012-10-18 20:59:20422DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem(
423 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) {
[email protected]c2e76012010-12-23 21:10:29424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
425
[email protected]d25fda12012-06-12 17:05:03426 if (!info->download_id.IsValid())
427 info->download_id = GetNextId();
[email protected]53ac00e82012-10-18 20:59:20428 DownloadItemImpl* download = item_factory_->CreateActiveItem(
[email protected]2bddd2072012-06-18 16:16:51429 this, *info,
430 scoped_ptr<DownloadRequestHandleInterface>(
431 new DownloadRequestHandle(info->request_handle)).Pass(),
[email protected]66521d02012-08-14 17:51:54432 bound_net_log);
[email protected]d8472d92011-08-26 20:15:20433
[email protected]ba7895d2012-06-22 19:02:52434 DCHECK(!ContainsKey(downloads_, download->GetId()));
435 downloads_[download->GetId()] = download;
436 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
437 active_downloads_[download->GetId()] = download;
[email protected]ef17c9a2012-02-09 05:08:09438
[email protected]53ac00e82012-10-18 20:59:20439 return download;
[email protected]c2e76012010-12-23 21:10:29440}
441
[email protected]db1d8f72012-07-13 19:23:16442DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]fc03de22011-12-06 23:28:12443 const FilePath& main_file_path,
444 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27445 const std::string& mime_type,
[email protected]fc03de22011-12-06 23:28:12446 DownloadItem::Observer* observer) {
[email protected]ef17c9a2012-02-09 05:08:09447 net::BoundNetLog bound_net_log =
448 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08449 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
[email protected]6474b112012-05-04 19:35:27450 this,
451 main_file_path,
452 page_url,
[email protected]6474b112012-05-04 19:35:27453 GetNextId(),
454 mime_type,
455 bound_net_log);
[email protected]fc03de22011-12-06 23:28:12456
[email protected]3d95e542012-11-20 00:52:08457 download_item->AddObserver(observer);
458 DCHECK(!ContainsKey(downloads_, download_item->GetId()));
459 downloads_[download_item->GetId()] = download_item;
460 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
461 this, download_item));
[email protected]fc03de22011-12-06 23:28:12462
[email protected]3d95e542012-11-20 00:52:08463 // TODO(asanka): Make the ui an observer.
464 ShowDownloadInBrowser(download_item);
[email protected]fc03de22011-12-06 23:28:12465
[email protected]3d95e542012-11-20 00:52:08466 return download_item;
[email protected]fc03de22011-12-06 23:28:12467}
468
[email protected]db1d8f72012-07-13 19:23:16469void DownloadManagerImpl::AssertStateConsistent(
470 DownloadItemImpl* download) const {
[email protected]ba7895d2012-06-22 19:02:52471 CHECK(ContainsKey(downloads_, download->GetId()));
[email protected]7d413112011-06-16 18:50:17472
[email protected]c09a8fd2011-11-21 19:54:50473 int64 state = download->GetState();
[email protected]61b75a52011-08-29 16:34:34474 base::debug::Alias(&state);
[email protected]c09a8fd2011-11-21 19:54:50475 if (DownloadItem::IN_PROGRESS == download->GetState())
476 CHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]5cd11b6e2011-06-10 20:30:59477}
478
[email protected]db1d8f72012-07-13 19:23:16479void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) {
[email protected]70850c72011-01-11 17:31:27480 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21481 DCHECK(download);
[email protected]fc03de22011-12-06 23:28:12482 active_downloads_.erase(download->GetId());
483 AssertStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27484}
485
[email protected]5656f8a2011-11-17 16:12:58486void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]93af2272011-09-21 18:29:17487 // A cancel at the right time could remove the download from the
488 // |active_downloads_| map before we get here.
[email protected]db1d8f72012-07-13 19:23:16489 if (ContainsKey(active_downloads_, download_id))
490 active_downloads_[download_id]->Cancel(true);
[email protected]93af2272011-09-21 18:29:17491}
492
[email protected]db1d8f72012-07-13 19:23:16493void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) {
[email protected]d8472d92011-08-26 20:15:20494 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20495
496 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50497 << " download = " << download->DebugString(true);
498
[email protected]93af2272011-09-21 18:29:17499 RemoveFromActiveList(download);
[email protected]47a881b2011-08-29 22:59:21500 // This function is called from the DownloadItem, so DI state
501 // should already have been updated.
[email protected]fc03de22011-12-06 23:28:12502 AssertStateConsistent(download);
[email protected]47a881b2011-08-29 22:59:21503}
504
[email protected]db1d8f72012-07-13 19:23:16505void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) {
[email protected]93af2272011-09-21 18:29:17506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
507 DCHECK(download);
[email protected]3d95e542012-11-20 00:52:08508 active_downloads_.erase(download->GetId());
[email protected]93af2272011-09-21 18:29:17509}
510
[email protected]53ac00e82012-10-18 20:59:20511void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
[email protected]35869622012-10-26 23:23:55512 scoped_ptr<DownloadItemFactory> item_factory) {
[email protected]53ac00e82012-10-18 20:59:20513 item_factory_ = item_factory.Pass();
514}
515
516void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
[email protected]35869622012-10-26 23:23:55517 scoped_ptr<DownloadFileFactory> file_factory) {
[email protected]53ac00e82012-10-18 20:59:20518 file_factory_ = file_factory.Pass();
519}
520
[email protected]35869622012-10-26 23:23:55521DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20522 return file_factory_.get();
523}
524
[email protected]5656f8a2011-11-17 16:12:58525int DownloadManagerImpl::RemoveDownloadItems(
[email protected]db1d8f72012-07-13 19:23:16526 const DownloadItemImplVector& pending_deletes) {
[email protected]6d0146c2011-08-04 19:13:04527 if (pending_deletes.empty())
528 return 0;
529
530 // Delete from internal maps.
[email protected]db1d8f72012-07-13 19:23:16531 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin();
[email protected]7e834f02012-08-09 20:38:56532 it != pending_deletes.end();
533 ++it) {
[email protected]db1d8f72012-07-13 19:23:16534 DownloadItemImpl* download = *it;
[email protected]6d0146c2011-08-04 19:13:04535 DCHECK(download);
[email protected]7e834f02012-08-09 20:38:56536 int32 download_id = download->GetId();
537 delete download;
538 downloads_.erase(download_id);
[email protected]6d0146c2011-08-04 19:13:04539 }
[email protected]7e834f02012-08-09 20:38:56540 return static_cast<int>(pending_deletes.size());
[email protected]6d0146c2011-08-04 19:13:04541}
542
[email protected]db1d8f72012-07-13 19:23:16543void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]09ea72c7422012-07-02 20:35:40544 if (!download ||
545 downloads_.find(download->GetId()) == downloads_.end())
546 return;
547
initial.commit09911bf2008-07-26 23:55:29548 // Remove from our tables and delete.
[email protected]db1d8f72012-07-13 19:23:16549 int downloads_count =
550 RemoveDownloadItems(DownloadItemImplVector(1, download));
[email protected]f04182f32010-12-10 19:12:07551 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29552}
553
[email protected]fd3a82832012-01-19 20:35:12554int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
555 base::Time remove_end) {
[email protected]db1d8f72012-07-13 19:23:16556 DownloadItemImplVector pending_deletes;
[email protected]09ea72c7422012-07-02 20:35:40557 for (DownloadMap::const_iterator it = downloads_.begin();
558 it != downloads_.end();
[email protected]6d0146c2011-08-04 19:13:04559 ++it) {
[email protected]db1d8f72012-07-13 19:23:16560 DownloadItemImpl* download = it->second;
[email protected]3d95e542012-11-20 00:52:08561 if (download->GetStartTime() >= remove_begin &&
[email protected]c09a8fd2011-11-21 19:54:50562 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04563 (download->IsComplete() || download->IsCancelled())) {
[email protected]fc03de22011-12-06 23:28:12564 AssertStateConsistent(download);
[email protected]7e834f02012-08-09 20:38:56565 download->NotifyRemoved();
[email protected]78b8fcc92009-03-31 17:36:28566 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29567 }
initial.commit09911bf2008-07-26 23:55:29568 }
[email protected]6d0146c2011-08-04 19:13:04569 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29570}
571
[email protected]fd3a82832012-01-19 20:35:12572int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59573 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29574}
575
[email protected]5656f8a2011-11-17 16:12:58576int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]d41355e6f2009-04-07 21:21:12577 // The null times make the date range unbounded.
[email protected]09ea72c7422012-07-02 20:35:40578 int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time());
[email protected]35869622012-10-26 23:23:55579 RecordClearAllSize(num_deleted);
[email protected]09ea72c7422012-07-02 20:35:40580 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12581}
582
[email protected]0d4e30c2012-01-28 00:47:53583void DownloadManagerImpl::DownloadUrl(
[email protected]35869622012-10-26 23:23:55584 scoped_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14585 if (params->post_id() >= 0) {
586 // Check this here so that the traceback is more useful.
587 DCHECK(params->prefer_cache());
588 DCHECK(params->method() == "POST");
589 }
590 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
[email protected]c873c862012-10-17 15:32:12591 &BeginDownload, base::Passed(params.Pass())));
initial.commit09911bf2008-07-26 23:55:29592}
593
[email protected]5656f8a2011-11-17 16:12:58594void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29595 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29596}
597
[email protected]5656f8a2011-11-17 16:12:58598void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29599 observers_.RemoveObserver(observer);
600}
601
[email protected]3d95e542012-11-20 00:52:08602DownloadItem* DownloadManagerImpl::CreateDownloadItem(
603 const FilePath& path,
604 const GURL& url,
605 const GURL& referrer_url,
606 const base::Time& start_time,
607 const base::Time& end_time,
608 int64 received_bytes,
609 int64 total_bytes,
610 DownloadItem::DownloadState state,
611 bool opened) {
612 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
613 this,
614 GetNextId(),
615 path,
616 url,
617 referrer_url,
618 start_time,
619 end_time,
620 received_bytes,
621 total_bytes,
622 state,
623 opened,
624 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
625 DCHECK(!ContainsKey(downloads_, item->GetId()));
626 downloads_[item->GetId()] = item;
627 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
628 VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
629 return item;
initial.commit09911bf2008-07-26 23:55:29630}
631
[email protected]3d95e542012-11-20 00:52:08632// TODO(asanka) Move into an observer.
[email protected]db1d8f72012-07-13 19:23:16633void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) {
[email protected]a29e4f22012-04-12 21:22:03634 // The 'contents' may no longer exist if the user closed the contents before
635 // we get this start completion event.
[email protected]a62d42902012-01-24 17:24:38636 WebContents* content = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:26637
638 // If the contents no longer exists, we ask the embedder to suggest another
[email protected]a29e4f22012-04-12 21:22:03639 // contents.
[email protected]491aaa62012-06-07 03:50:18640 if (!content && delegate_)
[email protected]ef9572e2012-01-04 22:14:12641 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:53642
[email protected]0bfbf882011-12-22 18:19:27643 if (content && content->GetDelegate())
644 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:53645}
646
[email protected]5656f8a2011-11-17 16:12:58647int DownloadManagerImpl::InProgressCount() const {
[email protected]550520f2012-05-14 20:58:48648 // Don't use active_downloads_.count() because Cancel() leaves items in
649 // active_downloads_ if they haven't made it into the persistent store yet.
[email protected]007e7412012-03-13 20:10:56650 // Need to actually look at each item's state.
651 int count = 0;
[email protected]550520f2012-05-14 20:58:48652 for (DownloadMap::const_iterator it = active_downloads_.begin();
653 it != active_downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16654 DownloadItemImpl* item = it->second;
[email protected]007e7412012-03-13 20:10:56655 if (item->IsInProgress())
656 ++count;
657 }
658 return count;
[email protected]5656f8a2011-11-17 16:12:58659}
660
[email protected]ba7895d2012-06-22 19:02:52661DownloadItem* DownloadManagerImpl::GetDownload(int download_id) {
662 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL;
663}
664
[email protected]9f1f4092012-09-10 21:18:04665void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
666 for (DownloadMap::iterator it = downloads_.begin();
667 it != downloads_.end(); ++it) {
668 downloads->push_back(it->second);
669 }
670}
671
[email protected]57fd1252010-12-23 17:24:09672// Confirm that everything in all maps is also in |downloads_|, and that
673// everything in |downloads_| is also in some other map.
[email protected]5656f8a2011-11-17 16:12:58674void DownloadManagerImpl::AssertContainersConsistent() const {
[email protected]f04182f32010-12-10 19:12:07675#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:09676 // Turn everything into sets.
[email protected]99229b22012-07-11 17:38:50677 const DownloadMap* input_maps[] = {&active_downloads_};
678 DownloadSet active_set;
679 DownloadSet* all_sets[] = {&active_set};
[email protected]6d0146c2011-08-04 19:13:04680 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:09681 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
682 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:04683 it != input_maps[i]->end(); ++it) {
684 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:07685 }
686 }
[email protected]57fd1252010-12-23 17:24:09687
[email protected]ba7895d2012-06-22 19:02:52688 DownloadSet all_downloads;
689 for (DownloadMap::const_iterator it = downloads_.begin();
690 it != downloads_.end(); ++it) {
691 all_downloads.insert(it->second);
692 }
693
[email protected]57fd1252010-12-23 17:24:09694 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:09695 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
696 DownloadSet remainder;
697 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
698 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
[email protected]ba7895d2012-06-22 19:02:52699 all_downloads.begin(), all_downloads.end(),
[email protected]57fd1252010-12-23 17:24:09700 insert_it);
701 DCHECK(remainder.empty());
[email protected]57fd1252010-12-23 17:24:09702 }
[email protected]f04182f32010-12-10 19:12:07703#endif
704}
705
[email protected]db1d8f72012-07-13 19:23:16706void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06707 int num_unopened = 0;
[email protected]09ea72c7422012-07-02 20:35:40708 for (DownloadMap::iterator it = downloads_.begin();
709 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16710 DownloadItemImpl* item = it->second;
[email protected]09ea72c7422012-07-02 20:35:40711 if (item->IsComplete() &&
712 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06713 ++num_unopened;
714 }
[email protected]35869622012-10-26 23:23:55715 RecordOpensOutstanding(num_unopened);
[email protected]da4a5582011-10-17 19:08:06716}
[email protected]5656f8a2011-11-17 16:12:58717
[email protected]35869622012-10-26 23:23:55718} // namespace content