blob: 3296daae4373e441bcea7127d96816d3ece80e58 [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]7286e3fc2011-07-19 22:13:2415#include "base/stl_util.h"
[email protected]eda58402011-09-21 19:32:0216#include "base/stringprintf.h"
17#include "base/synchronization/lock.h"
18#include "base/sys_string_conversions.h"
[email protected]d2a8fb72010-01-21 05:31:4219#include "build/build_config.h"
[email protected]71bf3f5e2011-08-15 21:05:2220#include "content/browser/download/download_create_info.h"
21#include "content/browser/download/download_file_manager.h"
[email protected]c09a8fd2011-11-21 19:54:5022#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0623#include "content/browser/download/download_stats.h"
[email protected]b3c41c0b2012-03-06 15:48:3224#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]ea114722012-03-12 01:11:2525#include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2926#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1127#include "content/public/browser/browser_context.h"
[email protected]c38831a12011-10-28 12:44:4928#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:4429#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3430#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1731#include "content/public/browser/download_manager_delegate.h"
[email protected]8da82ea2012-03-11 22:16:5232#include "content/public/browser/download_persistent_store_info.h"
[email protected]c5a5c0842012-05-04 20:05:1433#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0734#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1635#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3036#include "content/public/browser/render_process_host.h"
[email protected]0bfbf882011-12-22 18:19:2737#include "content/public/browser/web_contents_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1438#include "net/base/load_flags.h"
[email protected]27678b2a2012-02-04 22:09:1439#include "net/base/upload_data.h"
[email protected]f859eba2012-05-30 17:22:4940#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2941
[email protected]631bb742011-11-02 11:29:3942using content::BrowserThread;
[email protected]98e814062012-01-27 00:35:4243using content::DownloadId;
[email protected]e582fdd2011-12-20 16:48:1744using content::DownloadItem;
[email protected]8da82ea2012-03-11 22:16:5245using content::DownloadPersistentStoreInfo;
[email protected]ea114722012-03-12 01:11:2546using content::ResourceDispatcherHostImpl;
[email protected]2a6bc3e2011-12-28 23:51:3347using content::WebContents;
[email protected]631bb742011-11-02 11:29:3948
[email protected]a0ce3282011-08-19 20:49:5249namespace {
50
[email protected]c5a5c0842012-05-04 20:05:1451void BeginDownload(content::DownloadUrlParameters* params) {
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
53 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
54 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
55 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
56 content::ResourceDispatcherHostImpl* resource_dispatcher_host =
57 static_cast<content::ResourceDispatcherHostImpl*>(
58 params->resource_dispatcher_host());
59 scoped_ptr<net::URLRequest> request(new net::URLRequest(
60 params->url(), resource_dispatcher_host));
[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());
66 if (!params->post_body().empty())
67 request->AppendBytesToUpload(params->post_body().data(),
68 params->post_body().size());
69 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]c5a5c0842012-05-04 20:05:1480 for (content::DownloadUrlParameters::RequestHeadersType::const_iterator iter
81 = 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]c79a0c02011-08-22 22:37:3787 resource_dispatcher_host->BeginDownload(
[email protected]ea114722012-03-12 01:11:2588 request.Pass(),
[email protected]a53e2f92012-05-15 15:27:0689 params->content_initiated(),
[email protected]c5a5c0842012-05-04 20:05:1490 params->resource_context(),
91 params->render_process_host_id(),
92 params->render_view_host_routing_id(),
93 params->prefer_cache(),
94 params->save_info(),
95 params->callback());
[email protected]a0ce3282011-08-19 20:49:5296}
97
[email protected]33d22102012-01-25 17:46:5398class MapValueIteratorAdapter {
99 public:
100 explicit MapValueIteratorAdapter(
101 base::hash_map<int64, DownloadItem*>::const_iterator iter)
102 : iter_(iter) {
103 }
104 ~MapValueIteratorAdapter() {}
105
106 DownloadItem* operator*() { return iter_->second; }
107
108 MapValueIteratorAdapter& operator++() {
109 ++iter_;
110 return *this;
111 }
112
113 bool operator!=(const MapValueIteratorAdapter& that) const {
114 return iter_ != that.iter_;
115 }
116
117 private:
118 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
119 // Allow copy and assign.
120};
121
[email protected]5948e1a2012-03-10 00:19:18122void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm,
123 bool* result) {
124 if (dfm->NumberOfActiveDownloads())
125 *result = false;
126 BrowserThread::PostTask(
127 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
128}
129
[email protected]0a5c9112012-03-12 17:49:02130void EnsureNoPendingDownloadJobsOnIO(bool* result) {
[email protected]5948e1a2012-03-10 00:19:18131 scoped_refptr<DownloadFileManager> download_file_manager =
[email protected]ea114722012-03-12 01:11:25132 ResourceDispatcherHostImpl::Get()->download_file_manager();
[email protected]5948e1a2012-03-10 00:19:18133 BrowserThread::PostTask(
134 BrowserThread::FILE, FROM_HERE,
135 base::Bind(&EnsureNoPendingDownloadsOnFile,
136 download_file_manager, result));
137}
138
[email protected]a0ce3282011-08-19 20:49:52139} // namespace
140
[email protected]99907362012-01-11 05:41:40141namespace content {
142
143// static
144DownloadManager* DownloadManager::Create(
[email protected]ef17c9a2012-02-09 05:08:09145 content::DownloadManagerDelegate* delegate,
146 net::NetLog* net_log) {
147 return new DownloadManagerImpl(delegate, net_log);
[email protected]99907362012-01-11 05:41:40148}
149
[email protected]5948e1a2012-03-10 00:19:18150bool DownloadManager::EnsureNoPendingDownloadsForTesting() {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
152 bool result = true;
153 BrowserThread::PostTask(
154 BrowserThread::IO, FROM_HERE,
[email protected]0a5c9112012-03-12 17:49:02155 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result));
[email protected]5948e1a2012-03-10 00:19:18156 MessageLoop::current()->Run();
157 return result;
158}
159
[email protected]99907362012-01-11 05:41:40160} // namespace content
161
[email protected]5656f8a2011-11-17 16:12:58162DownloadManagerImpl::DownloadManagerImpl(
[email protected]ef17c9a2012-02-09 05:08:09163 content::DownloadManagerDelegate* delegate,
164 net::NetLog* net_log)
[email protected]5656f8a2011-11-17 16:12:58165 : shutdown_needed_(false),
166 browser_context_(NULL),
167 file_manager_(NULL),
[email protected]5656f8a2011-11-17 16:12:58168 delegate_(delegate),
[email protected]ef17c9a2012-02-09 05:08:09169 net_log_(net_log) {
initial.commit09911bf2008-07-26 23:55:29170}
171
[email protected]5656f8a2011-11-17 16:12:58172DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13173 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29174}
175
[email protected]5656f8a2011-11-17 16:12:58176DownloadId DownloadManagerImpl::GetNextId() {
[email protected]98e814062012-01-27 00:35:42177 return delegate_->GetNextId();
[email protected]2909e342011-10-29 00:46:53178}
179
[email protected]fc03de22011-12-06 23:28:12180bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) {
181 return delegate_->ShouldOpenDownload(item);
182}
183
184bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
185 return delegate_->ShouldOpenFileBasedOnExtension(path);
186}
187
[email protected]5656f8a2011-11-17 16:12:58188void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50189 VLOG(20) << __FUNCTION__ << "()"
190 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13191 if (!shutdown_needed_)
192 return;
193 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29194
[email protected]75e51b52012-02-04 16:57:54195 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08196 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13197
198 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:18199 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]fabf36d22011-10-28 21:50:17200 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
201 file_manager_, make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:13202 }
initial.commit09911bf2008-07-26 23:55:29203
[email protected]f04182f32010-12-10 19:12:07204 AssertContainersConsistent();
205
206 // Go through all downloads in downloads_. Dangerous ones we need to
207 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:09208 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:07209 DownloadItem* download = *it;
210
211 // Save iterator from potential erases in this set done by called code.
212 // Iterators after an erasure point are still valid for lists and
213 // associative containers such as sets.
214 it++;
215
[email protected]c09a8fd2011-11-21 19:54:50216 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29217 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07218 // The user hasn't accepted it, so we need to remove it
219 // from the disk. This may or may not result in it being
220 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12221 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07222 // removes and deletes it if it's known to the history service)
223 // so the only thing we know after calling this function is that
224 // the download was deleted if-and-only-if it was removed
225 // from all queues.
[email protected]303077002011-04-19 23:21:01226 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26227 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17228 download->Cancel(false);
229 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29230 }
231 }
232
[email protected]f04182f32010-12-10 19:12:07233 // At this point, all dangerous downloads have had their files removed
234 // and all in progress downloads have been cancelled. We can now delete
235 // anything left.
[email protected]9ccbb372008-10-10 18:50:32236
[email protected]5cd11b6e2011-06-10 20:30:59237 // Copy downloads_ to separate container so as not to set off checks
238 // in DownloadItem destruction.
239 DownloadSet downloads_to_delete;
240 downloads_to_delete.swap(downloads_);
241
[email protected]70850c72011-01-11 17:31:27242 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59243 history_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59244 STLDeleteElements(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29245
[email protected]41f558fb2012-01-09 22:59:58246 // We'll have nothing more to report to the observers after this point.
247 observers_.Clear();
248
[email protected]6d0146c2011-08-04 19:13:04249 DCHECK(save_page_downloads_.empty());
250
initial.commit09911bf2008-07-26 23:55:29251 file_manager_ = NULL;
[email protected]2588ea9d2011-08-22 20:59:53252 delegate_->Shutdown();
initial.commit09911bf2008-07-26 23:55:29253}
254
[email protected]5656f8a2011-11-17 16:12:58255void DownloadManagerImpl::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04256 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57257 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58258
[email protected]f04182f32010-12-10 19:12:07259 for (DownloadMap::iterator it = history_downloads_.begin();
260 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50261 if (it->second->IsTemporary() &&
[email protected]3d833de2012-05-30 23:32:06262 (dir_path.empty() ||
263 it->second->GetTargetFilePath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57264 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58265 }
[email protected]6aa4a1c02010-01-15 18:49:58266}
267
[email protected]5656f8a2011-11-17 16:12:58268void DownloadManagerImpl::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04269 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57270 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34271
[email protected]f04182f32010-12-10 19:12:07272 for (DownloadMap::iterator it = history_downloads_.begin();
273 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50274 if (!it->second->IsTemporary() &&
[email protected]3d833de2012-05-30 23:32:06275 (dir_path.empty() ||
276 it->second->GetTargetFilePath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57277 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34278 }
[email protected]8ddbd66a2010-05-21 16:38:34279}
280
[email protected]5656f8a2011-11-17 16:12:58281void DownloadManagerImpl::SearchDownloads(const string16& query,
282 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26283 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42284
[email protected]f04182f32010-12-10 19:12:07285 for (DownloadMap::iterator it = history_downloads_.begin();
286 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42287 DownloadItem* download_item = it->second;
288
[email protected]c09a8fd2011-11-21 19:54:50289 if (download_item->IsTemporary())
[email protected]d3b12902010-08-16 23:39:42290 continue;
291
292 // Display Incognito downloads only in Incognito window, and vice versa.
293 // The Incognito Downloads page will get the list of non-Incognito downloads
294 // from its parent profile.
[email protected]c09a8fd2011-11-21 19:54:50295 if (browser_context_->IsOffTheRecord() != download_item->IsOtr())
[email protected]d3b12902010-08-16 23:39:42296 continue;
297
298 if (download_item->MatchesQuery(query_lower))
299 result->push_back(download_item);
300 }
[email protected]d3b12902010-08-16 23:39:42301}
302
initial.commit09911bf2008-07-26 23:55:29303// Query the history service for information about all persisted downloads.
[email protected]5656f8a2011-11-17 16:12:58304bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03305 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29306 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
307 shutdown_needed_ = true;
308
[email protected]6d0c9fb2011-08-22 19:26:03309 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46310
[email protected]ea114722012-03-12 01:11:25311 // In test mode, there may be no ResourceDispatcherHostImpl. In this case
312 // it's safe to avoid setting |file_manager_| because we only call a small
313 // set of functions, none of which need it.
314 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
[email protected]b39e7a88b2012-01-10 21:43:17315 if (rdh) {
316 file_manager_ = rdh->download_file_manager();
317 DCHECK(file_manager_);
318 }
initial.commit09911bf2008-07-26 23:55:29319
initial.commit09911bf2008-07-26 23:55:29320 return true;
321}
322
[email protected]aa9881c2011-08-15 18:01:12323// We have received a message from DownloadFileManager about a new download.
[email protected]5656f8a2011-11-17 16:12:58324void DownloadManagerImpl::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35326
[email protected]aa9881c2011-08-15 18:01:12327 if (delegate_->ShouldStartDownload(download_id))
328 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35329}
330
[email protected]5656f8a2011-11-17 16:12:58331void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
333 for (DownloadMap::iterator it = history_downloads_.begin();
334 it != history_downloads_.end(); ++it) {
335 CheckForFileRemoval(it->second);
336 }
337}
338
[email protected]5656f8a2011-11-17 16:12:58339void DownloadManagerImpl::CheckForFileRemoval(DownloadItem* download_item) {
[email protected]9fc114672011-06-15 08:17:48340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
341 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50342 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48343 BrowserThread::PostTask(
344 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58345 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]c09a8fd2011-11-21 19:54:50346 this, download_item->GetDbHandle(),
[email protected]fabf36d22011-10-28 21:50:17347 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48348 }
349}
350
[email protected]5656f8a2011-11-17 16:12:58351void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]9fc114672011-06-15 08:17:48352 int64 db_handle, const FilePath& path) {
353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
354 if (!file_util::PathExists(path)) {
355 BrowserThread::PostTask(
356 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58357 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
358 this,
359 db_handle));
[email protected]9fc114672011-06-15 08:17:48360 }
361}
362
[email protected]5656f8a2011-11-17 16:12:58363void DownloadManagerImpl::OnFileRemovalDetected(int64 db_handle) {
[email protected]9fc114672011-06-15 08:17:48364 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
365 DownloadMap::iterator it = history_downloads_.find(db_handle);
366 if (it != history_downloads_.end()) {
367 DownloadItem* download_item = it->second;
368 download_item->OnDownloadedFileRemoved();
369 }
370}
371
[email protected]443853c62011-12-22 19:22:41372void DownloadManagerImpl::RestartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18373 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29374
[email protected]4cd82f72011-05-23 19:15:01375 DownloadItem* download = GetActiveDownloadItem(download_id);
376 if (!download)
377 return;
378
379 VLOG(20) << __FUNCTION__ << "()"
380 << " download = " << download->DebugString(true);
381
[email protected]3d833de2012-05-30 23:32:06382 if (download->GetTargetDisposition() ==
383 DownloadItem::TARGET_DISPOSITION_PROMPT) {
initial.commit09911bf2008-07-26 23:55:29384 // We must ask the user for the place to put the download.
[email protected]a62d42902012-01-24 17:24:38385 WebContents* contents = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:26386
[email protected]3d833de2012-05-30 23:32:06387 delegate_->ChooseDownloadPath(contents, download->GetTargetFilePath(),
[email protected]84d57412012-03-03 08:59:55388 download_id);
[email protected]f5920322011-03-24 20:34:16389 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]75e51b52012-02-04 16:57:54390 SelectFileDialogDisplayed(this, download_id));
initial.commit09911bf2008-07-26 23:55:29391 } else {
[email protected]3d833de2012-05-30 23:32:06392 // No prompting for download, just continue with the current target path.
393 OnTargetPathAvailable(download);
initial.commit09911bf2008-07-26 23:55:29394 }
395}
396
[email protected]37757c62011-12-20 20:07:12397content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58398 return browser_context_;
399}
400
401FilePath DownloadManagerImpl::LastDownloadPath() {
402 return last_download_path_;
403}
404
[email protected]ef17c9a2012-02-09 05:08:09405net::BoundNetLog DownloadManagerImpl::CreateDownloadItem(
[email protected]594e66fe2011-10-25 22:49:41406 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) {
[email protected]c2e76012010-12-23 21:10:29407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
408
[email protected]ef17c9a2012-02-09 05:08:09409 net::BoundNetLog bound_net_log =
410 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]c09a8fd2011-11-21 19:54:50411 DownloadItem* download = new DownloadItemImpl(
[email protected]ae77da82011-11-01 19:17:29412 this, *info, new DownloadRequestHandle(request_handle),
[email protected]ef17c9a2012-02-09 05:08:09413 browser_context_->IsOffTheRecord(), bound_net_log);
[email protected]2909e342011-10-29 00:46:53414 int32 download_id = info->download_id.local();
[email protected]d8472d92011-08-26 20:15:20415
[email protected]0634626a2012-05-03 19:04:26416 DCHECK(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29417 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01418 active_downloads_[download_id] = download;
[email protected]ef17c9a2012-02-09 05:08:09419
420 return bound_net_log;
[email protected]c2e76012010-12-23 21:10:29421}
422
[email protected]fc03de22011-12-06 23:28:12423DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem(
424 const FilePath& main_file_path,
425 const GURL& page_url,
426 bool is_otr,
[email protected]6474b112012-05-04 19:35:27427 const std::string& mime_type,
[email protected]fc03de22011-12-06 23:28:12428 DownloadItem::Observer* observer) {
[email protected]ef17c9a2012-02-09 05:08:09429 net::BoundNetLog bound_net_log =
430 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]fc03de22011-12-06 23:28:12431 DownloadItem* download = new DownloadItemImpl(
[email protected]6474b112012-05-04 19:35:27432 this,
433 main_file_path,
434 page_url,
435 is_otr,
436 GetNextId(),
437 mime_type,
438 bound_net_log);
[email protected]fc03de22011-12-06 23:28:12439
440 download->AddObserver(observer);
441
442 DCHECK(!ContainsKey(save_page_downloads_, download->GetId()));
443 downloads_.insert(download);
444 save_page_downloads_[download->GetId()] = download;
445
446 // Will notify the observer in the callback.
447 delegate_->AddItemToPersistentStore(download);
448
449 return download;
450}
451
[email protected]3d833de2012-05-30 23:32:06452// The target path for the download item is now valid. We proceed with the
453// determination of an intermediate path.
454void DownloadManagerImpl::OnTargetPathAvailable(DownloadItem* download) {
[email protected]ca4b5fa32010-10-09 12:42:18455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01456 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39457
[email protected]c09a8fd2011-11-21 19:54:50458 int32 download_id = download->GetId();
initial.commit09911bf2008-07-26 23:55:29459
[email protected]70850c72011-01-11 17:31:27460 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01461 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27462
[email protected]4cd82f72011-05-23 19:15:01463 VLOG(20) << __FUNCTION__ << "()"
464 << " download = " << download->DebugString(true);
465
[email protected]adb2f3d12011-01-23 16:24:54466 // Rename to intermediate name.
[email protected]3d833de2012-05-30 23:32:06467 // TODO(asanka): Skip this rename if download->AllDataSaved() is true. This
468 // avoids a spurious rename when we can just rename to the final
469 // filename. Unnecessary renames may cause bugs like
470 // http://crbug.com/74187.
471 bool ok_to_overwrite = true;
472 FilePath intermediate_path =
473 delegate_->GetIntermediatePath(*download, &ok_to_overwrite);
474 // We want the intermediate and target paths to refer to the same directory so
475 // that they are both on the same device and subject to same
476 // space/permission/availability constraints.
477 DCHECK(intermediate_path.DirName() ==
478 download->GetTargetFilePath().DirName());
479 download->OnIntermediatePathDetermined(file_manager_, intermediate_path,
480 ok_to_overwrite);
initial.commit09911bf2008-07-26 23:55:29481}
482
[email protected]443853c62011-12-22 19:22:41483void DownloadManagerImpl::UpdateDownload(int32 download_id,
484 int64 bytes_so_far,
485 int64 bytes_per_sec,
[email protected]0afff032012-01-06 20:55:00486 const std::string& hash_state) {
[email protected]70850c72011-01-11 17:31:27487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
488 DownloadMap::iterator it = active_downloads_.find(download_id);
489 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29490 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26491 if (download->IsInProgress()) {
[email protected]443853c62011-12-22 19:22:41492 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state);
[email protected]2588ea9d2011-08-22 20:59:53493 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27494 }
initial.commit09911bf2008-07-26 23:55:29495 }
496}
497
[email protected]5656f8a2011-11-17 16:12:58498void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
499 int64 size,
500 const std::string& hash) {
[email protected]33c6d3f12011-09-04 00:00:54501 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]da6e3922010-11-24 21:45:50502 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
503 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32505
[email protected]c4f02c42011-01-24 21:55:06506 // If it's not in active_downloads_, that means it was cancelled; just
507 // ignore the notification.
508 if (active_downloads_.count(download_id) == 0)
509 return;
510
[email protected]adb2f3d12011-01-23 16:24:54511 DownloadItem* download = active_downloads_[download_id];
[email protected]ac4af82f2011-11-10 19:09:37512 download->OnAllDataSaved(size, hash);
[email protected]da4cd4262012-05-18 20:42:35513 MaybeCompleteDownload(download);
[email protected]adb2f3d12011-01-23 16:24:54514}
[email protected]9ccbb372008-10-10 18:50:32515
[email protected]fc03de22011-12-06 23:28:12516void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const {
[email protected]c09a8fd2011-11-21 19:54:50517 if (download->GetState() == DownloadItem::REMOVING) {
[email protected]0634626a2012-05-03 19:04:26518 DCHECK(!ContainsKey(downloads_, download));
519 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
[email protected]0634626a2012-05-03 19:04:26520 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17521 return;
522 }
523
524 // Should be in downloads_ if we're not REMOVING.
525 CHECK(ContainsKey(downloads_, download));
526
527 // Check history_downloads_ consistency.
[email protected]5009b7a2012-02-21 18:47:03528 if (download->IsPersisted()) {
[email protected]c09a8fd2011-11-21 19:54:50529 CHECK(ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17530 } else {
[email protected]fc03de22011-12-06 23:28:12531 for (DownloadMap::const_iterator it = history_downloads_.begin();
[email protected]7d413112011-06-16 18:50:17532 it != history_downloads_.end(); ++it) {
[email protected]0634626a2012-05-03 19:04:26533 DCHECK(it->second != download);
[email protected]7d413112011-06-16 18:50:17534 }
535 }
536
[email protected]c09a8fd2011-11-21 19:54:50537 int64 state = download->GetState();
[email protected]61b75a52011-08-29 16:34:34538 base::debug::Alias(&state);
[email protected]c09a8fd2011-11-21 19:54:50539 if (ContainsKey(active_downloads_, download->GetId())) {
[email protected]5009b7a2012-02-21 18:47:03540 if (download->IsPersisted())
[email protected]c09a8fd2011-11-21 19:54:50541 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
542 if (DownloadItem::IN_PROGRESS != download->GetState())
543 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle());
[email protected]f9a2997f2011-09-23 16:54:07544 }
[email protected]c09a8fd2011-11-21 19:54:50545 if (DownloadItem::IN_PROGRESS == download->GetState())
546 CHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]5cd11b6e2011-06-10 20:30:59547}
548
[email protected]5656f8a2011-11-17 16:12:58549bool DownloadManagerImpl::IsDownloadReadyForCompletion(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54550 // If we don't have all the data, the download is not ready for
551 // completion.
[email protected]c09a8fd2011-11-21 19:54:50552 if (!download->AllDataSaved())
[email protected]adb2f3d12011-01-23 16:24:54553 return false;
[email protected]6a7fb042010-02-01 16:30:47554
[email protected]9d7ef802011-02-25 19:03:35555 // If the download is dangerous, but not yet validated, it's not ready for
556 // completion.
[email protected]c09a8fd2011-11-21 19:54:50557 if (download->GetSafetyState() == DownloadItem::DANGEROUS)
[email protected]9d7ef802011-02-25 19:03:35558 return false;
559
[email protected]adb2f3d12011-01-23 16:24:54560 // If the download isn't active (e.g. has been cancelled) it's not
561 // ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50562 if (active_downloads_.count(download->GetId()) == 0)
[email protected]adb2f3d12011-01-23 16:24:54563 return false;
564
565 // If the download hasn't been inserted into the history system
566 // (which occurs strictly after file name determination, intermediate
567 // file rename, and UI display) then it's not ready for completion.
[email protected]5009b7a2012-02-21 18:47:03568 if (!download->IsPersisted())
[email protected]7054b592011-06-22 14:46:42569 return false;
570
571 return true;
[email protected]adb2f3d12011-01-23 16:24:54572}
573
[email protected]6474b112012-05-04 19:35:27574// When SavePackage downloads MHTML to GData (see
575// SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it
576// does for non-SavePackage downloads, but SavePackage downloads never satisfy
577// IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls
578// DownloadItem::UpdateObservers() when the upload completes so that SavePackage
579// notices that the upload has completed and runs its normal Finish() pathway.
580// MaybeCompleteDownload() is never the mechanism by which SavePackage completes
581// downloads. SavePackage always uses its own Finish() to mark downloads
582// complete.
583
[email protected]5656f8a2011-11-17 16:12:58584void DownloadManagerImpl::MaybeCompleteDownload(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54585 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
586 VLOG(20) << __FUNCTION__ << "()" << " download = "
587 << download->DebugString(false);
588
589 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32590 return;
[email protected]9ccbb372008-10-10 18:50:32591
[email protected]adb2f3d12011-01-23 16:24:54592 // TODO(rdsmith): DCHECK that we only pass through this point
593 // once per download. The natural way to do this is by a state
594 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56595
[email protected]adb2f3d12011-01-23 16:24:54596 // Confirm we're in the proper set of states to be here;
[email protected]550520f2012-05-14 20:58:48597 // have all data, have a history handle, (validated or safe).
598 DCHECK(download->IsInProgress());
[email protected]c09a8fd2011-11-21 19:54:50599 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState());
[email protected]c09a8fd2011-11-21 19:54:50600 DCHECK(download->AllDataSaved());
[email protected]5009b7a2012-02-21 18:47:03601 DCHECK(download->IsPersisted());
[email protected]c09a8fd2011-11-21 19:54:50602 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle()));
[email protected]adb2f3d12011-01-23 16:24:54603
[email protected]da4cd4262012-05-18 20:42:35604 // Give the delegate a chance to override. It's ok to keep re-setting the
605 // delegate's |complete_callback| cb as long as there isn't another call-point
606 // trying to set it to a different cb. TODO(benjhayden): Change the callback
607 // to point directly to the item instead of |this| when DownloadItem supports
608 // weak-ptrs.
609 if (!delegate_->ShouldCompleteDownload(download, base::Bind(
610 &DownloadManagerImpl::MaybeCompleteDownloadById,
611 this, download->GetId())))
[email protected]c2918652011-11-01 18:50:23612 return;
613
[email protected]adb2f3d12011-01-23 16:24:54614 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
615 << download->DebugString(false);
616
[email protected]2588ea9d2011-08-22 20:59:53617 delegate_->UpdateItemInPersistentStore(download);
[email protected]48837962011-04-19 17:03:29618 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32619}
620
[email protected]da4cd4262012-05-18 20:42:35621void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) {
622 DownloadItem* download_item = GetActiveDownload(download_id);
623 if (download_item != NULL)
624 MaybeCompleteDownload(download_item);
625}
626
[email protected]fc03de22011-12-06 23:28:12627void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) {
[email protected]70850c72011-01-11 17:31:27628 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21629 DCHECK(download);
[email protected]2588ea9d2011-08-22 20:59:53630 delegate_->UpdateItemInPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12631 active_downloads_.erase(download->GetId());
632 AssertStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27633}
634
[email protected]5656f8a2011-11-17 16:12:58635void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]93af2272011-09-21 18:29:17636 DownloadItem* download = GetActiveDownload(download_id);
637 // A cancel at the right time could remove the download from the
638 // |active_downloads_| map before we get here.
639 if (!download)
640 return;
641
642 download->Cancel(true);
643}
644
[email protected]fc03de22011-12-06 23:28:12645void DownloadManagerImpl::DownloadCancelled(DownloadItem* download) {
[email protected]d8472d92011-08-26 20:15:20646 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20647
648 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50649 << " download = " << download->DebugString(true);
650
[email protected]93af2272011-09-21 18:29:17651 RemoveFromActiveList(download);
[email protected]47a881b2011-08-29 22:59:21652 // This function is called from the DownloadItem, so DI state
653 // should already have been updated.
[email protected]fc03de22011-12-06 23:28:12654 AssertStateConsistent(download);
initial.commit09911bf2008-07-26 23:55:29655
[email protected]15d90ba2011-11-03 03:41:55656 if (file_manager_)
657 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29658}
659
[email protected]bf3b08a2012-03-08 01:52:34660void DownloadManagerImpl::OnDownloadInterrupted(
661 int32 download_id,
662 int64 size,
663 const std::string& hash_state,
664 content::DownloadInterruptReason reason) {
[email protected]47a881b2011-08-29 22:59:21665 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
666
667 DownloadItem* download = GetActiveDownload(download_id);
668 if (!download)
669 return;
670
[email protected]be76b7e2011-10-13 12:57:57671 VLOG(20) << __FUNCTION__ << "()"
672 << " reason " << InterruptReasonDebugString(reason)
[email protected]c09a8fd2011-11-21 19:54:50673 << " at offset " << download->GetReceivedBytes()
[email protected]47a881b2011-08-29 22:59:21674 << " size = " << size
675 << " download = " << download->DebugString(true);
676
[email protected]93af2272011-09-21 18:29:17677 RemoveFromActiveList(download);
[email protected]443853c62011-12-22 19:22:41678 download->Interrupted(size, hash_state, reason);
[email protected]93af2272011-09-21 18:29:17679 download->OffThreadCancel(file_manager_);
[email protected]47a881b2011-08-29 22:59:21680}
681
[email protected]5656f8a2011-11-17 16:12:58682DownloadItem* DownloadManagerImpl::GetActiveDownload(int32 download_id) {
[email protected]bf68a00b2011-04-07 17:28:26683 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
684 DownloadMap::iterator it = active_downloads_.find(download_id);
[email protected]bf68a00b2011-04-07 17:28:26685 if (it == active_downloads_.end())
[email protected]47a881b2011-08-29 22:59:21686 return NULL;
[email protected]bf68a00b2011-04-07 17:28:26687
688 DownloadItem* download = it->second;
689
[email protected]47a881b2011-08-29 22:59:21690 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50691 DCHECK_EQ(download_id, download->GetId());
[email protected]4cd82f72011-05-23 19:15:01692
[email protected]47a881b2011-08-29 22:59:21693 return download;
694}
[email protected]54610672011-07-18 18:24:43695
[email protected]5656f8a2011-11-17 16:12:58696void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) {
[email protected]93af2272011-09-21 18:29:17697 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
698 DCHECK(download);
699
700 // Clean up will happen when the history system create callback runs if we
701 // don't have a valid db_handle yet.
[email protected]5009b7a2012-02-21 18:47:03702 if (download->IsPersisted()) {
[email protected]c09a8fd2011-11-21 19:54:50703 active_downloads_.erase(download->GetId());
[email protected]93af2272011-09-21 18:29:17704 delegate_->UpdateItemInPersistentStore(download);
705 }
706}
707
[email protected]fd3a82832012-01-19 20:35:12708bool DownloadManagerImpl::GenerateFileHash() {
709 return delegate_->GenerateFileHash();
710}
711
[email protected]5656f8a2011-11-17 16:12:58712content::DownloadManagerDelegate* DownloadManagerImpl::delegate() const {
713 return delegate_;
714}
715
716void DownloadManagerImpl::SetDownloadManagerDelegate(
[email protected]1bd0ef12011-10-20 05:24:17717 content::DownloadManagerDelegate* delegate) {
[email protected]9bb54ee2011-10-12 17:43:35718 delegate_ = delegate;
719}
720
[email protected]5656f8a2011-11-17 16:12:58721int DownloadManagerImpl::RemoveDownloadItems(
[email protected]6d0146c2011-08-04 19:13:04722 const DownloadVector& pending_deletes) {
723 if (pending_deletes.empty())
724 return 0;
725
726 // Delete from internal maps.
727 for (DownloadVector::const_iterator it = pending_deletes.begin();
728 it != pending_deletes.end();
729 ++it) {
730 DownloadItem* download = *it;
731 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50732 history_downloads_.erase(download->GetDbHandle());
733 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:04734 downloads_.erase(download);
735 }
736
737 // Tell observers to refresh their views.
738 NotifyModelChanged();
739
740 // Delete the download items themselves.
741 const int num_deleted = static_cast<int>(pending_deletes.size());
742 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
743 return num_deleted;
744}
745
[email protected]fc03de22011-12-06 23:28:12746void DownloadManagerImpl::DownloadRemoved(DownloadItem* download) {
747 if (history_downloads_.find(download->GetDbHandle()) ==
748 history_downloads_.end())
[email protected]93af2272011-09-21 18:29:17749 return;
750
751 // Make history update.
[email protected]93af2272011-09-21 18:29:17752 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29753
754 // Remove from our tables and delete.
[email protected]6d0146c2011-08-04 19:13:04755 int downloads_count = RemoveDownloadItems(DownloadVector(1, download));
[email protected]f04182f32010-12-10 19:12:07756 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29757}
758
[email protected]fd3a82832012-01-19 20:35:12759int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
760 base::Time remove_end) {
[email protected]2588ea9d2011-08-22 20:59:53761 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29762
[email protected]a312a442010-12-15 23:40:33763 // All downloads visible to the user will be in the history,
764 // so scan that map.
[email protected]6d0146c2011-08-04 19:13:04765 DownloadVector pending_deletes;
766 for (DownloadMap::const_iterator it = history_downloads_.begin();
767 it != history_downloads_.end();
768 ++it) {
initial.commit09911bf2008-07-26 23:55:29769 DownloadItem* download = it->second;
[email protected]c09a8fd2011-11-21 19:54:50770 if (download->GetStartTime() >= remove_begin &&
771 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04772 (download->IsComplete() || download->IsCancelled())) {
[email protected]fc03de22011-12-06 23:28:12773 AssertStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28774 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29775 }
initial.commit09911bf2008-07-26 23:55:29776 }
[email protected]6d0146c2011-08-04 19:13:04777 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29778}
779
[email protected]fd3a82832012-01-19 20:35:12780int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59781 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29782}
783
[email protected]5656f8a2011-11-17 16:12:58784int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]da4a5582011-10-17 19:08:06785 download_stats::RecordClearAllSize(history_downloads_.size());
[email protected]d41355e6f2009-04-07 21:21:12786 // The null times make the date range unbounded.
787 return RemoveDownloadsBetween(base::Time(), base::Time());
788}
789
[email protected]0d4e30c2012-01-28 00:47:53790void DownloadManagerImpl::DownloadUrl(
[email protected]c5a5c0842012-05-04 20:05:14791 scoped_ptr<content::DownloadUrlParameters> params) {
792 if (params->post_id() >= 0) {
793 // Check this here so that the traceback is more useful.
794 DCHECK(params->prefer_cache());
795 DCHECK(params->method() == "POST");
796 }
797 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
798 &BeginDownload, base::Owned(params.release())));
initial.commit09911bf2008-07-26 23:55:29799}
800
[email protected]5656f8a2011-11-17 16:12:58801void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29802 observers_.AddObserver(observer);
[email protected]a1e41e72012-02-22 17:41:25803 // TODO: It is the responsibility of the observers to query the
804 // DownloadManager. Remove the following call from here and update all
805 // observers.
[email protected]75e51b52012-02-04 16:57:54806 observer->ModelChanged(this);
initial.commit09911bf2008-07-26 23:55:29807}
808
[email protected]5656f8a2011-11-17 16:12:58809void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29810 observers_.RemoveObserver(observer);
811}
812
[email protected]84d57412012-03-03 08:59:55813void DownloadManagerImpl::FileSelected(const FilePath& path,
814 int32 download_id) {
[email protected]4cd82f72011-05-23 19:15:01815 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3d833de2012-05-30 23:32:06816 DCHECK(!path.empty());
[email protected]4cd82f72011-05-23 19:15:01817
[email protected]4cd82f72011-05-23 19:15:01818 DownloadItem* download = GetActiveDownloadItem(download_id);
819 if (!download)
820 return;
821 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
[email protected]3d833de2012-05-30 23:32:06822 << " download = " << download->DebugString(true);
[email protected]4cd82f72011-05-23 19:15:01823
[email protected]3d833de2012-05-30 23:32:06824 // Retain the last directory. Exclude temporary downloads since the path
825 // likely points at the location of a temporary file.
826 if (!download->IsTemporary())
[email protected]7ae7c2cb2009-01-06 23:31:41827 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35828
[email protected]4cd82f72011-05-23 19:15:01829 // Make sure the initial file name is set only once.
[email protected]3d833de2012-05-30 23:32:06830 download->OnTargetPathSelected(path);
831 OnTargetPathAvailable(download);
initial.commit09911bf2008-07-26 23:55:29832}
833
[email protected]84d57412012-03-03 08:59:55834void DownloadManagerImpl::FileSelectionCanceled(int32 download_id) {
initial.commit09911bf2008-07-26 23:55:29835 // The user didn't pick a place to save the file, so need to cancel the
836 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01837 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01838
839 DownloadItem* download = GetActiveDownloadItem(download_id);
840 if (!download)
841 return;
842
843 VLOG(20) << __FUNCTION__ << "()"
844 << " download = " << download->DebugString(true);
845
[email protected]8f8bc112012-02-22 12:36:31846 download->Cancel(true);
[email protected]4cd82f72011-05-23 19:15:01847}
848
initial.commit09911bf2008-07-26 23:55:29849// Operations posted to us from the history service ----------------------------
850
851// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25852// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]5656f8a2011-11-17 16:12:58853void DownloadManagerImpl::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25854 std::vector<DownloadPersistentStoreInfo>* entries) {
initial.commit09911bf2008-07-26 23:55:29855 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]deb40832012-02-23 15:41:37856 int64 db_handle = entries->at(i).db_handle;
857 base::debug::Alias(&db_handle);
[email protected]0634626a2012-05-03 19:04:26858 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]deb40832012-02-23 15:41:37859
[email protected]ef17c9a2012-02-09 05:08:09860 net::BoundNetLog bound_net_log =
861 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]fc03de22011-12-06 23:28:12862 DownloadItem* download = new DownloadItemImpl(
[email protected]ef17c9a2012-02-09 05:08:09863 this, GetNextId(), entries->at(i), bound_net_log);
[email protected]f04182f32010-12-10 19:12:07864 downloads_.insert(download);
[email protected]c09a8fd2011-11-21 19:54:50865 history_downloads_[download->GetDbHandle()] = download;
[email protected]da6e3922010-11-24 21:45:50866 VLOG(20) << __FUNCTION__ << "()" << i << ">"
867 << " download = " << download->DebugString(true);
initial.commit09911bf2008-07-26 23:55:29868 }
[email protected]b0ab1d42010-02-24 19:29:28869 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48870 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29871}
872
[email protected]5656f8a2011-11-17 16:12:58873void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItem* download,
874 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27875 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0634626a2012-05-03 19:04:26876 DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]1e9fe7ff2011-06-24 17:37:33877
[email protected]da4a5582011-10-17 19:08:06878 download_stats::RecordHistorySize(history_downloads_.size());
879
[email protected]5009b7a2012-02-21 18:47:03880 DCHECK(!download->IsPersisted());
[email protected]c09a8fd2011-11-21 19:54:50881 download->SetDbHandle(db_handle);
[email protected]5009b7a2012-02-21 18:47:03882 download->SetIsPersisted();
[email protected]5bcd73eb2011-03-23 21:14:02883
[email protected]0634626a2012-05-03 19:04:26884 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]c09a8fd2011-11-21 19:54:50885 history_downloads_[download->GetDbHandle()] = download;
[email protected]6d0146c2011-08-04 19:13:04886
887 // Show in the appropriate browser UI.
888 // This includes buttons to save or cancel, for a dangerous download.
889 ShowDownloadInBrowser(download);
890
891 // Inform interested objects about the new download.
892 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19893}
894
[email protected]2588ea9d2011-08-22 20:59:53895
[email protected]5656f8a2011-11-17 16:12:58896void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id,
897 int64 db_handle) {
[email protected]2588ea9d2011-08-22 20:59:53898 if (save_page_downloads_.count(download_id)) {
899 OnSavePageItemAddedToPersistentStore(download_id, db_handle);
900 } else if (active_downloads_.count(download_id)) {
901 OnDownloadItemAddedToPersistentStore(download_id, db_handle);
902 }
903 // It's valid that we don't find a matching item, i.e. on shutdown.
904}
905
[email protected]f9a45b02011-06-30 23:49:19906// Once the new DownloadItem's creation info has been committed to the history
907// service, we associate the DownloadItem with the db handle, update our
908// 'history_downloads_' map and inform observers.
[email protected]5656f8a2011-11-17 16:12:58909void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore(
910 int32 download_id, int64 db_handle) {
[email protected]f9a45b02011-06-30 23:49:19911 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]19420cc2011-07-18 17:43:45912 DownloadItem* download = GetActiveDownloadItem(download_id);
[email protected]93af2272011-09-21 18:29:17913 if (!download)
[email protected]19420cc2011-07-18 17:43:45914 return;
[email protected]54610672011-07-18 18:24:43915
916 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
917 << " download_id = " << download_id
918 << " download = " << download->DebugString(true);
[email protected]f9a45b02011-06-30 23:49:19919
[email protected]e5107ce2011-09-19 20:36:13920 int32 matching_item_download_id
921 = (ContainsKey(history_downloads_, db_handle) ?
[email protected]c09a8fd2011-11-21 19:54:50922 history_downloads_[db_handle]->GetId() : 0);
[email protected]e5107ce2011-09-19 20:36:13923 base::debug::Alias(&matching_item_download_id);
924
[email protected]0634626a2012-05-03 19:04:26925 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:20926
[email protected]f9a45b02011-06-30 23:49:19927 AddDownloadItemToHistory(download, db_handle);
initial.commit09911bf2008-07-26 23:55:29928
[email protected]93af2272011-09-21 18:29:17929 // If the download is still in progress, try to complete it.
930 //
931 // Otherwise, download has been cancelled or interrupted before we've
932 // received the DB handle. We post one final message to the history
933 // service so that it can be properly in sync with the DownloadItem's
934 // completion status, and also inform any observers so that they get
935 // more than just the start notification.
936 if (download->IsInProgress()) {
937 MaybeCompleteDownload(download);
938 } else {
[email protected]0634626a2012-05-03 19:04:26939 DCHECK(download->IsCancelled());
[email protected]93af2272011-09-21 18:29:17940 active_downloads_.erase(download_id);
941 delegate_->UpdateItemInPersistentStore(download);
942 download->UpdateObservers();
943 }
initial.commit09911bf2008-07-26 23:55:29944}
945
[email protected]5656f8a2011-11-17 16:12:58946void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]a29e4f22012-04-12 21:22:03947 // The 'contents' may no longer exist if the user closed the contents before
948 // we get this start completion event.
[email protected]a62d42902012-01-24 17:24:38949 WebContents* content = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:26950
951 // If the contents no longer exists, we ask the embedder to suggest another
[email protected]a29e4f22012-04-12 21:22:03952 // contents.
[email protected]da1a27b2011-07-29 23:16:33953 if (!content)
[email protected]ef9572e2012-01-04 22:14:12954 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:53955
[email protected]0bfbf882011-12-22 18:19:27956 if (content && content->GetDelegate())
957 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:53958}
959
[email protected]5656f8a2011-11-17 16:12:58960int DownloadManagerImpl::InProgressCount() const {
[email protected]550520f2012-05-14 20:58:48961 // Don't use active_downloads_.count() because Cancel() leaves items in
962 // active_downloads_ if they haven't made it into the persistent store yet.
[email protected]007e7412012-03-13 20:10:56963 // Need to actually look at each item's state.
964 int count = 0;
[email protected]550520f2012-05-14 20:58:48965 for (DownloadMap::const_iterator it = active_downloads_.begin();
966 it != active_downloads_.end(); ++it) {
[email protected]007e7412012-03-13 20:10:56967 DownloadItem* item = it->second;
968 if (item->IsInProgress())
969 ++count;
970 }
971 return count;
[email protected]5656f8a2011-11-17 16:12:58972}
973
[email protected]6cade212008-12-03 00:32:22974// Clears the last download path, used to initialize "save as" dialogs.
[email protected]5656f8a2011-11-17 16:12:58975void DownloadManagerImpl::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:41976 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:38977}
[email protected]b0ab1d42010-02-24 19:29:28978
[email protected]5656f8a2011-11-17 16:12:58979void DownloadManagerImpl::NotifyModelChanged() {
[email protected]75e51b52012-02-04 16:57:54980 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this));
[email protected]b0ab1d42010-02-24 19:29:28981}
982
[email protected]5656f8a2011-11-17 16:12:58983DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) {
[email protected]4cd82f72011-05-23 19:15:01984 // The |history_downloads_| map is indexed by the download's db_handle,
985 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:07986 for (DownloadMap::iterator it = history_downloads_.begin();
987 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:36988 DownloadItem* item = it->second;
[email protected]c09a8fd2011-11-21 19:54:50989 if (item->GetId() == download_id)
[email protected]2e030682010-07-23 19:45:36990 return item;
991 }
992 return NULL;
993}
994
[email protected]5656f8a2011-11-17 16:12:58995DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) {
[email protected]5d3e83642011-12-16 01:14:36996 if (ContainsKey(active_downloads_, download_id))
997 return active_downloads_[download_id];
998 return NULL;
[email protected]4cd82f72011-05-23 19:15:01999}
1000
[email protected]57fd1252010-12-23 17:24:091001// Confirm that everything in all maps is also in |downloads_|, and that
1002// everything in |downloads_| is also in some other map.
[email protected]5656f8a2011-11-17 16:12:581003void DownloadManagerImpl::AssertContainersConsistent() const {
[email protected]f04182f32010-12-10 19:12:071004#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:091005 // Turn everything into sets.
[email protected]6d0146c2011-08-04 19:13:041006 const DownloadMap* input_maps[] = {&active_downloads_,
1007 &history_downloads_,
1008 &save_page_downloads_};
1009 DownloadSet active_set, history_set, save_page_set;
1010 DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set};
1011 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:091012 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
1013 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:041014 it != input_maps[i]->end(); ++it) {
1015 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:071016 }
1017 }
[email protected]57fd1252010-12-23 17:24:091018
1019 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:091020 DownloadSet downloads_union;
1021 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1022 DownloadSet remainder;
1023 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1024 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
1025 downloads_.begin(), downloads_.end(),
1026 insert_it);
1027 DCHECK(remainder.empty());
1028 std::insert_iterator<DownloadSet>
1029 insert_union(downloads_union, downloads_union.end());
1030 std::set_union(downloads_union.begin(), downloads_union.end(),
1031 all_sets[i]->begin(), all_sets[i]->end(),
1032 insert_union);
1033 }
1034
1035 // Is everything in downloads_ present in one of the other sets?
1036 DownloadSet remainder;
1037 std::insert_iterator<DownloadSet>
1038 insert_remainder(remainder, remainder.begin());
1039 std::set_difference(downloads_.begin(), downloads_.end(),
1040 downloads_union.begin(), downloads_union.end(),
1041 insert_remainder);
1042 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:071043#endif
1044}
1045
[email protected]6d0146c2011-08-04 19:13:041046// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:531047// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:041048// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:531049// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
1050// that the history event is update regardless of the order in which these two
1051// events complete.
[email protected]6d0146c2011-08-04 19:13:041052// If something removes the download item from the download manager (Remove,
1053// Shutdown) the result will be that the SavePage system will not be able to
1054// properly update the download item (which no longer exists) or the download
1055// history, but the action will complete properly anyway. This may lead to the
1056// history entry being wrong on a reload of chrome (specifically in the case of
1057// Initiation -> History Callback -> Removal -> Completion), but there's no way
1058// to solve that without canceling on Remove (which would then update the DB).
1059
[email protected]5656f8a2011-11-17 16:12:581060void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore(
1061 int32 download_id, int64 db_handle) {
[email protected]6d0146c2011-08-04 19:13:041062 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1063
1064 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
1065 // This can happen if the download manager is shutting down and all maps
1066 // have been cleared.
1067 if (it == save_page_downloads_.end())
1068 return;
1069
1070 DownloadItem* download = it->second;
1071 if (!download) {
1072 NOTREACHED();
1073 return;
1074 }
1075
[email protected]0634626a2012-05-03 19:04:261076 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:201077
[email protected]6d0146c2011-08-04 19:13:041078 AddDownloadItemToHistory(download, db_handle);
1079
1080 // Finalize this download if it finished before the history callback.
1081 if (!download->IsInProgress())
1082 SavePageDownloadFinished(download);
1083}
1084
[email protected]5656f8a2011-11-17 16:12:581085void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) {
[email protected]5009b7a2012-02-21 18:47:031086 if (download->IsPersisted()) {
[email protected]2588ea9d2011-08-22 20:59:531087 delegate_->UpdateItemInPersistentStore(download);
[email protected]c09a8fd2011-11-21 19:54:501088 DCHECK(ContainsKey(save_page_downloads_, download->GetId()));
1089 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:041090
1091 if (download->IsComplete())
[email protected]ad50def52011-10-19 23:17:071092 content::NotificationService::current()->Notify(
[email protected]6d0146c2011-08-04 19:13:041093 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
[email protected]6c2381d2011-10-19 02:52:531094 content::Source<DownloadManager>(this),
1095 content::Details<DownloadItem>(download));
[email protected]6d0146c2011-08-04 19:13:041096 }
1097}
[email protected]da4a5582011-10-17 19:08:061098
[email protected]fc03de22011-12-06 23:28:121099void DownloadManagerImpl::DownloadOpened(DownloadItem* download) {
[email protected]da4a5582011-10-17 19:08:061100 delegate_->UpdateItemInPersistentStore(download);
1101 int num_unopened = 0;
1102 for (DownloadMap::iterator it = history_downloads_.begin();
1103 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:501104 if (it->second->IsComplete() && !it->second->GetOpened())
[email protected]da4a5582011-10-17 19:08:061105 ++num_unopened;
1106 }
1107 download_stats::RecordOpensOutstanding(num_unopened);
1108}
[email protected]5656f8a2011-11-17 16:12:581109
[email protected]3d833de2012-05-30 23:32:061110void DownloadManagerImpl::DownloadRenamedToIntermediateName(
1111 DownloadItem* download) {
1112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1113 // If the rename failed, we receive an OnDownloadInterrupted() call before we
1114 // receive the DownloadRenamedToIntermediateName() call.
1115 delegate_->AddItemToPersistentStore(download);
1116}
1117
1118void DownloadManagerImpl::DownloadRenamedToFinalName(
1119 DownloadItem* download) {
1120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1121 // If the rename failed, we receive an OnDownloadInterrupted() call before we
1122 // receive the DownloadRenamedToFinalName() call.
1123 delegate_->UpdatePathForItemInPersistentStore(download,
1124 download->GetFullPath());
1125}
1126
[email protected]5948e1a2012-03-10 00:19:181127void DownloadManagerImpl::SetFileManagerForTesting(
1128 DownloadFileManager* file_manager) {
[email protected]5656f8a2011-11-17 16:12:581129 file_manager_ = file_manager;
1130}