blob: a8f79e2e0c468703c0bc34ae8e034895e5757f4e [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]ad50def52011-10-19 23:17:0733#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1634#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3035#include "content/public/browser/render_process_host.h"
[email protected]0bfbf882011-12-22 18:19:2736#include "content/public/browser/web_contents_delegate.h"
[email protected]27678b2a2012-02-04 22:09:1437#include "net/base/upload_data.h"
initial.commit09911bf2008-07-26 23:55:2938
[email protected]631bb742011-11-02 11:29:3939using content::BrowserThread;
[email protected]98e814062012-01-27 00:35:4240using content::DownloadId;
[email protected]e582fdd2011-12-20 16:48:1741using content::DownloadItem;
[email protected]8da82ea2012-03-11 22:16:5242using content::DownloadPersistentStoreInfo;
[email protected]ea114722012-03-12 01:11:2543using content::ResourceDispatcherHostImpl;
[email protected]2a6bc3e2011-12-28 23:51:3344using content::WebContents;
[email protected]631bb742011-11-02 11:29:3945
[email protected]a0ce3282011-08-19 20:49:5246namespace {
47
[email protected]fabf36d22011-10-28 21:50:1748// Param structs exist because base::Bind can only handle 6 args.
49struct URLParams {
[email protected]89e6aa72012-03-12 22:51:3350 URLParams(const GURL& url, const GURL& referrer, int64 post_id, bool cache)
51 : url_(url), referrer_(referrer), post_id_(post_id), prefer_cache_(cache) {}
[email protected]fabf36d22011-10-28 21:50:1752 GURL url_;
53 GURL referrer_;
[email protected]27678b2a2012-02-04 22:09:1454 int64 post_id_;
[email protected]89e6aa72012-03-12 22:51:3355 bool prefer_cache_;
[email protected]fabf36d22011-10-28 21:50:1756};
57
58struct RenderParams {
59 RenderParams(int rpi, int rvi)
60 : render_process_id_(rpi), render_view_id_(rvi) {}
61 int render_process_id_;
62 int render_view_id_;
63};
64
[email protected]89e6aa72012-03-12 22:51:3365void BeginDownload(
66 const URLParams& url_params,
[email protected]29a5ffc82012-03-13 19:35:5867 const content::DownloadSaveInfo& save_info,
[email protected]89e6aa72012-03-12 22:51:3368 ResourceDispatcherHostImpl* resource_dispatcher_host,
69 const RenderParams& render_params,
70 content::ResourceContext* context,
71 const content::DownloadManager::OnStartedCallback& callback) {
[email protected]c1ba99842012-01-19 20:56:0572 scoped_ptr<net::URLRequest> request(
73 new net::URLRequest(url_params.url_, resource_dispatcher_host));
[email protected]fabf36d22011-10-28 21:50:1774 request->set_referrer(url_params.referrer_.spec());
[email protected]27678b2a2012-02-04 22:09:1475 if (url_params.post_id_ >= 0) {
76 // The POST in this case does not have an actual body, and only works
77 // when retrieving data from cache. This is done because we don't want
78 // to do a re-POST without user consent, and currently don't have a good
79 // plan on how to display the UI for that.
[email protected]89e6aa72012-03-12 22:51:3380 DCHECK(url_params.prefer_cache_);
[email protected]27678b2a2012-02-04 22:09:1481 request->set_method("POST");
82 scoped_refptr<net::UploadData> upload_data = new net::UploadData();
83 upload_data->set_identifier(url_params.post_id_);
84 request->set_upload(upload_data);
85 }
[email protected]c79a0c02011-08-22 22:37:3786 resource_dispatcher_host->BeginDownload(
[email protected]ea114722012-03-12 01:11:2587 request.Pass(),
88 context,
[email protected]fabf36d22011-10-28 21:50:1789 render_params.render_process_id_,
90 render_params.render_view_id_,
[email protected]89e6aa72012-03-12 22:51:3391 url_params.prefer_cache_,
[email protected]ea114722012-03-12 01:11:2592 save_info,
[email protected]89e6aa72012-03-12 22:51:3393 callback);
[email protected]a0ce3282011-08-19 20:49:5294}
95
[email protected]33d22102012-01-25 17:46:5396class MapValueIteratorAdapter {
97 public:
98 explicit MapValueIteratorAdapter(
99 base::hash_map<int64, DownloadItem*>::const_iterator iter)
100 : iter_(iter) {
101 }
102 ~MapValueIteratorAdapter() {}
103
104 DownloadItem* operator*() { return iter_->second; }
105
106 MapValueIteratorAdapter& operator++() {
107 ++iter_;
108 return *this;
109 }
110
111 bool operator!=(const MapValueIteratorAdapter& that) const {
112 return iter_ != that.iter_;
113 }
114
115 private:
116 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
117 // Allow copy and assign.
118};
119
[email protected]5948e1a2012-03-10 00:19:18120void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm,
121 bool* result) {
122 if (dfm->NumberOfActiveDownloads())
123 *result = false;
124 BrowserThread::PostTask(
125 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
126}
127
[email protected]0a5c9112012-03-12 17:49:02128void EnsureNoPendingDownloadJobsOnIO(bool* result) {
[email protected]5948e1a2012-03-10 00:19:18129 scoped_refptr<DownloadFileManager> download_file_manager =
[email protected]ea114722012-03-12 01:11:25130 ResourceDispatcherHostImpl::Get()->download_file_manager();
[email protected]5948e1a2012-03-10 00:19:18131 BrowserThread::PostTask(
132 BrowserThread::FILE, FROM_HERE,
133 base::Bind(&EnsureNoPendingDownloadsOnFile,
134 download_file_manager, result));
135}
136
[email protected]a0ce3282011-08-19 20:49:52137} // namespace
138
[email protected]99907362012-01-11 05:41:40139namespace content {
140
141// static
142DownloadManager* DownloadManager::Create(
[email protected]ef17c9a2012-02-09 05:08:09143 content::DownloadManagerDelegate* delegate,
144 net::NetLog* net_log) {
145 return new DownloadManagerImpl(delegate, net_log);
[email protected]99907362012-01-11 05:41:40146}
147
[email protected]5948e1a2012-03-10 00:19:18148bool DownloadManager::EnsureNoPendingDownloadsForTesting() {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
150 bool result = true;
151 BrowserThread::PostTask(
152 BrowserThread::IO, FROM_HERE,
[email protected]0a5c9112012-03-12 17:49:02153 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result));
[email protected]5948e1a2012-03-10 00:19:18154 MessageLoop::current()->Run();
155 return result;
156}
157
[email protected]99907362012-01-11 05:41:40158} // namespace content
159
[email protected]5656f8a2011-11-17 16:12:58160DownloadManagerImpl::DownloadManagerImpl(
[email protected]ef17c9a2012-02-09 05:08:09161 content::DownloadManagerDelegate* delegate,
162 net::NetLog* net_log)
[email protected]5656f8a2011-11-17 16:12:58163 : shutdown_needed_(false),
164 browser_context_(NULL),
165 file_manager_(NULL),
[email protected]5656f8a2011-11-17 16:12:58166 delegate_(delegate),
[email protected]ef17c9a2012-02-09 05:08:09167 net_log_(net_log) {
initial.commit09911bf2008-07-26 23:55:29168}
169
[email protected]5656f8a2011-11-17 16:12:58170DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13171 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29172}
173
[email protected]5656f8a2011-11-17 16:12:58174DownloadId DownloadManagerImpl::GetNextId() {
[email protected]98e814062012-01-27 00:35:42175 return delegate_->GetNextId();
[email protected]2909e342011-10-29 00:46:53176}
177
[email protected]fc03de22011-12-06 23:28:12178bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) {
179 return delegate_->ShouldOpenDownload(item);
180}
181
182bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
183 return delegate_->ShouldOpenFileBasedOnExtension(path);
184}
185
[email protected]5656f8a2011-11-17 16:12:58186void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50187 VLOG(20) << __FUNCTION__ << "()"
188 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13189 if (!shutdown_needed_)
190 return;
191 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29192
[email protected]75e51b52012-02-04 16:57:54193 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08194 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13195
196 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:18197 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]fabf36d22011-10-28 21:50:17198 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
199 file_manager_, make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:13200 }
initial.commit09911bf2008-07-26 23:55:29201
[email protected]f04182f32010-12-10 19:12:07202 AssertContainersConsistent();
203
204 // Go through all downloads in downloads_. Dangerous ones we need to
205 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:09206 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:07207 DownloadItem* download = *it;
208
209 // Save iterator from potential erases in this set done by called code.
210 // Iterators after an erasure point are still valid for lists and
211 // associative containers such as sets.
212 it++;
213
[email protected]c09a8fd2011-11-21 19:54:50214 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29215 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07216 // The user hasn't accepted it, so we need to remove it
217 // from the disk. This may or may not result in it being
218 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12219 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07220 // removes and deletes it if it's known to the history service)
221 // so the only thing we know after calling this function is that
222 // the download was deleted if-and-only-if it was removed
223 // from all queues.
[email protected]303077002011-04-19 23:21:01224 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26225 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17226 download->Cancel(false);
227 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29228 }
229 }
230
[email protected]f04182f32010-12-10 19:12:07231 // At this point, all dangerous downloads have had their files removed
232 // and all in progress downloads have been cancelled. We can now delete
233 // anything left.
[email protected]9ccbb372008-10-10 18:50:32234
[email protected]5cd11b6e2011-06-10 20:30:59235 // Copy downloads_ to separate container so as not to set off checks
236 // in DownloadItem destruction.
237 DownloadSet downloads_to_delete;
238 downloads_to_delete.swap(downloads_);
239
initial.commit09911bf2008-07-26 23:55:29240 in_progress_.clear();
[email protected]70850c72011-01-11 17:31:27241 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59242 history_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59243 STLDeleteElements(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29244
[email protected]41f558fb2012-01-09 22:59:58245 // We'll have nothing more to report to the observers after this point.
246 observers_.Clear();
247
[email protected]6d0146c2011-08-04 19:13:04248 DCHECK(save_page_downloads_.empty());
249
initial.commit09911bf2008-07-26 23:55:29250 file_manager_ = NULL;
[email protected]2588ea9d2011-08-22 20:59:53251 delegate_->Shutdown();
initial.commit09911bf2008-07-26 23:55:29252}
253
[email protected]5656f8a2011-11-17 16:12:58254void DownloadManagerImpl::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04255 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57256 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58257
[email protected]f04182f32010-12-10 19:12:07258 for (DownloadMap::iterator it = history_downloads_.begin();
259 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50260 if (it->second->IsTemporary() &&
[email protected]fdd2715c2011-12-09 22:24:20261 (dir_path.empty() || it->second->GetFullPath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57262 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58263 }
[email protected]6aa4a1c02010-01-15 18:49:58264}
265
[email protected]5656f8a2011-11-17 16:12:58266void DownloadManagerImpl::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04267 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57268 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34269
[email protected]f04182f32010-12-10 19:12:07270 for (DownloadMap::iterator it = history_downloads_.begin();
271 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50272 if (!it->second->IsTemporary() &&
273 (dir_path.empty() || it->second->GetFullPath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57274 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34275 }
[email protected]8ddbd66a2010-05-21 16:38:34276}
277
[email protected]5656f8a2011-11-17 16:12:58278void DownloadManagerImpl::SearchDownloads(const string16& query,
279 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26280 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42281
[email protected]f04182f32010-12-10 19:12:07282 for (DownloadMap::iterator it = history_downloads_.begin();
283 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42284 DownloadItem* download_item = it->second;
285
[email protected]c09a8fd2011-11-21 19:54:50286 if (download_item->IsTemporary())
[email protected]d3b12902010-08-16 23:39:42287 continue;
288
289 // Display Incognito downloads only in Incognito window, and vice versa.
290 // The Incognito Downloads page will get the list of non-Incognito downloads
291 // from its parent profile.
[email protected]c09a8fd2011-11-21 19:54:50292 if (browser_context_->IsOffTheRecord() != download_item->IsOtr())
[email protected]d3b12902010-08-16 23:39:42293 continue;
294
295 if (download_item->MatchesQuery(query_lower))
296 result->push_back(download_item);
297 }
[email protected]d3b12902010-08-16 23:39:42298}
299
initial.commit09911bf2008-07-26 23:55:29300// Query the history service for information about all persisted downloads.
[email protected]5656f8a2011-11-17 16:12:58301bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03302 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29303 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
304 shutdown_needed_ = true;
305
[email protected]6d0c9fb2011-08-22 19:26:03306 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46307
[email protected]ea114722012-03-12 01:11:25308 // In test mode, there may be no ResourceDispatcherHostImpl. In this case
309 // it's safe to avoid setting |file_manager_| because we only call a small
310 // set of functions, none of which need it.
311 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
[email protected]b39e7a88b2012-01-10 21:43:17312 if (rdh) {
313 file_manager_ = rdh->download_file_manager();
314 DCHECK(file_manager_);
315 }
initial.commit09911bf2008-07-26 23:55:29316
initial.commit09911bf2008-07-26 23:55:29317 return true;
318}
319
[email protected]aa9881c2011-08-15 18:01:12320// We have received a message from DownloadFileManager about a new download.
[email protected]5656f8a2011-11-17 16:12:58321void DownloadManagerImpl::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35323
[email protected]aa9881c2011-08-15 18:01:12324 if (delegate_->ShouldStartDownload(download_id))
325 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35326}
327
[email protected]5656f8a2011-11-17 16:12:58328void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
330 for (DownloadMap::iterator it = history_downloads_.begin();
331 it != history_downloads_.end(); ++it) {
332 CheckForFileRemoval(it->second);
333 }
334}
335
[email protected]5656f8a2011-11-17 16:12:58336void DownloadManagerImpl::CheckForFileRemoval(DownloadItem* download_item) {
[email protected]9fc114672011-06-15 08:17:48337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
338 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50339 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48340 BrowserThread::PostTask(
341 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58342 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]c09a8fd2011-11-21 19:54:50343 this, download_item->GetDbHandle(),
[email protected]fabf36d22011-10-28 21:50:17344 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48345 }
346}
347
[email protected]5656f8a2011-11-17 16:12:58348void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]9fc114672011-06-15 08:17:48349 int64 db_handle, const FilePath& path) {
350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
351 if (!file_util::PathExists(path)) {
352 BrowserThread::PostTask(
353 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58354 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
355 this,
356 db_handle));
[email protected]9fc114672011-06-15 08:17:48357 }
358}
359
[email protected]5656f8a2011-11-17 16:12:58360void DownloadManagerImpl::OnFileRemovalDetected(int64 db_handle) {
[email protected]9fc114672011-06-15 08:17:48361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
362 DownloadMap::iterator it = history_downloads_.find(db_handle);
363 if (it != history_downloads_.end()) {
364 DownloadItem* download_item = it->second;
365 download_item->OnDownloadedFileRemoved();
366 }
367}
368
[email protected]443853c62011-12-22 19:22:41369void DownloadManagerImpl::RestartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18370 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29371
[email protected]4cd82f72011-05-23 19:15:01372 DownloadItem* download = GetActiveDownloadItem(download_id);
373 if (!download)
374 return;
375
376 VLOG(20) << __FUNCTION__ << "()"
377 << " download = " << download->DebugString(true);
378
[email protected]c09a8fd2011-11-21 19:54:50379 FilePath suggested_path = download->GetSuggestedPath();
[email protected]4cd82f72011-05-23 19:15:01380
[email protected]c09a8fd2011-11-21 19:54:50381 if (download->PromptUserForSaveLocation()) {
initial.commit09911bf2008-07-26 23:55:29382 // We must ask the user for the place to put the download.
[email protected]a62d42902012-01-24 17:24:38383 WebContents* contents = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:26384
[email protected]795b76a2011-12-14 16:52:53385 FilePath target_path;
386 // If |download| is a potentially dangerous file, then |suggested_path|
387 // contains the intermediate name instead of the final download
388 // filename. The latter is GetTargetName().
[email protected]a62d42902012-01-24 17:24:38389 if (download->GetDangerType() !=
390 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)
[email protected]795b76a2011-12-14 16:52:53391 target_path = suggested_path.DirName().Append(download->GetTargetName());
392 else
393 target_path = suggested_path;
[email protected]99cb7f82011-07-28 17:27:26394
[email protected]795b76a2011-12-14 16:52:53395 delegate_->ChooseDownloadPath(contents, target_path,
[email protected]84d57412012-03-03 08:59:55396 download_id);
[email protected]f5920322011-03-24 20:34:16397 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]75e51b52012-02-04 16:57:54398 SelectFileDialogDisplayed(this, download_id));
initial.commit09911bf2008-07-26 23:55:29399 } else {
400 // No prompting for download, just continue with the suggested name.
[email protected]4cd82f72011-05-23 19:15:01401 ContinueDownloadWithPath(download, suggested_path);
initial.commit09911bf2008-07-26 23:55:29402 }
403}
404
[email protected]37757c62011-12-20 20:07:12405content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58406 return browser_context_;
407}
408
409FilePath DownloadManagerImpl::LastDownloadPath() {
410 return last_download_path_;
411}
412
[email protected]ef17c9a2012-02-09 05:08:09413net::BoundNetLog DownloadManagerImpl::CreateDownloadItem(
[email protected]594e66fe2011-10-25 22:49:41414 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) {
[email protected]c2e76012010-12-23 21:10:29415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
416
[email protected]ef17c9a2012-02-09 05:08:09417 net::BoundNetLog bound_net_log =
418 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]c09a8fd2011-11-21 19:54:50419 DownloadItem* download = new DownloadItemImpl(
[email protected]ae77da82011-11-01 19:17:29420 this, *info, new DownloadRequestHandle(request_handle),
[email protected]ef17c9a2012-02-09 05:08:09421 browser_context_->IsOffTheRecord(), bound_net_log);
[email protected]2909e342011-10-29 00:46:53422 int32 download_id = info->download_id.local();
[email protected]4cd82f72011-05-23 19:15:01423 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]d8472d92011-08-26 20:15:20424
[email protected]0634626a2012-05-03 19:04:26425 DCHECK(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29426 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01427 active_downloads_[download_id] = download;
[email protected]ef17c9a2012-02-09 05:08:09428
429 return bound_net_log;
[email protected]c2e76012010-12-23 21:10:29430}
431
[email protected]fc03de22011-12-06 23:28:12432DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem(
433 const FilePath& main_file_path,
434 const GURL& page_url,
435 bool is_otr,
436 DownloadItem::Observer* observer) {
[email protected]ef17c9a2012-02-09 05:08:09437 net::BoundNetLog bound_net_log =
438 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]fc03de22011-12-06 23:28:12439 DownloadItem* download = new DownloadItemImpl(
[email protected]ef17c9a2012-02-09 05:08:09440 this, main_file_path, page_url, is_otr, GetNextId(), bound_net_log);
[email protected]fc03de22011-12-06 23:28:12441
442 download->AddObserver(observer);
443
444 DCHECK(!ContainsKey(save_page_downloads_, download->GetId()));
445 downloads_.insert(download);
446 save_page_downloads_[download->GetId()] = download;
447
448 // Will notify the observer in the callback.
449 delegate_->AddItemToPersistentStore(download);
450
451 return download;
452}
453
[email protected]795b76a2011-12-14 16:52:53454// For non-safe downloads with no prompting, |chosen_file| is the intermediate
455// path for saving the in-progress download. The final target filename for these
456// is |download->GetTargetName()|. For all other downloads (non-safe downloads
457// for which we have prompted for a save location, and all safe downloads),
458// |chosen_file| is the final target download path.
[email protected]5656f8a2011-11-17 16:12:58459void DownloadManagerImpl::ContinueDownloadWithPath(
460 DownloadItem* download, const FilePath& chosen_file) {
[email protected]ca4b5fa32010-10-09 12:42:18461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01462 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39463
[email protected]c09a8fd2011-11-21 19:54:50464 int32 download_id = download->GetId();
initial.commit09911bf2008-07-26 23:55:29465
[email protected]70850c72011-01-11 17:31:27466 // NOTE(ahendrickson) Eventually |active_downloads_| will replace
467 // |in_progress_|, but we don't want to change the semantics yet.
[email protected]4cd82f72011-05-23 19:15:01468 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]70850c72011-01-11 17:31:27469 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01470 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27471
[email protected]4cd82f72011-05-23 19:15:01472 // Make sure the initial file name is set only once.
[email protected]fdd2715c2011-12-09 22:24:20473 DCHECK(download->GetFullPath().empty());
[email protected]4cd82f72011-05-23 19:15:01474 download->OnPathDetermined(chosen_file);
[email protected]4cd82f72011-05-23 19:15:01475
476 VLOG(20) << __FUNCTION__ << "()"
477 << " download = " << download->DebugString(true);
478
479 in_progress_[download_id] = download;
initial.commit09911bf2008-07-26 23:55:29480
[email protected]adb2f3d12011-01-23 16:24:54481 // Rename to intermediate name.
[email protected]f5920322011-03-24 20:34:16482 FilePath download_path;
[email protected]385e93182012-01-30 17:11:03483 if (download->GetDangerType() !=
484 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) {
485 if (download->PromptUserForSaveLocation()) {
486 // When we prompt the user, we overwrite the FullPath with what the user
487 // wanted to use. Construct a file path using the previously determined
488 // intermediate filename and the new path.
489 // TODO(asanka): This can trample an in-progress download in the new
490 // target directory if it was using the same intermediate name.
491 FilePath file_name = download->GetSuggestedPath().BaseName();
492 download_path = download->GetFullPath().DirName().Append(file_name);
493 } else {
494 // The download's name is already set to an intermediate name, so no need
495 // to override.
496 download_path = download->GetFullPath();
497 }
498 } else {
499 // The download is a safe download. We need to rename it to its
500 // intermediate path. The final name after user confirmation will be set
501 // from DownloadItem::OnDownloadCompleting.
502 download_path = delegate_->GetIntermediatePath(download->GetFullPath());
503 }
[email protected]594cd7d2010-07-21 03:23:56504
[email protected]f5920322011-03-24 20:34:16505 BrowserThread::PostTask(
506 BrowserThread::FILE, FROM_HERE,
[email protected]fabf36d22011-10-28 21:50:17507 base::Bind(&DownloadFileManager::RenameInProgressDownloadFile,
[email protected]fc03de22011-12-06 23:28:12508 file_manager_, download->GetGlobalId(),
509 download_path));
[email protected]f5920322011-03-24 20:34:16510
511 download->Rename(download_path);
512
[email protected]2588ea9d2011-08-22 20:59:53513 delegate_->AddItemToPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29514}
515
[email protected]443853c62011-12-22 19:22:41516void DownloadManagerImpl::UpdateDownload(int32 download_id,
517 int64 bytes_so_far,
518 int64 bytes_per_sec,
[email protected]0afff032012-01-06 20:55:00519 const std::string& hash_state) {
[email protected]70850c72011-01-11 17:31:27520 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
521 DownloadMap::iterator it = active_downloads_.find(download_id);
522 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29523 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26524 if (download->IsInProgress()) {
[email protected]443853c62011-12-22 19:22:41525 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state);
[email protected]2588ea9d2011-08-22 20:59:53526 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27527 }
initial.commit09911bf2008-07-26 23:55:29528 }
529}
530
[email protected]5656f8a2011-11-17 16:12:58531void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
532 int64 size,
533 const std::string& hash) {
[email protected]33c6d3f12011-09-04 00:00:54534 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]da6e3922010-11-24 21:45:50535 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
536 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35537 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32538
[email protected]c4f02c42011-01-24 21:55:06539 // If it's not in active_downloads_, that means it was cancelled; just
540 // ignore the notification.
541 if (active_downloads_.count(download_id) == 0)
542 return;
543
[email protected]adb2f3d12011-01-23 16:24:54544 DownloadItem* download = active_downloads_[download_id];
[email protected]ac4af82f2011-11-10 19:09:37545 download->OnAllDataSaved(size, hash);
[email protected]b09f1282011-09-14 00:37:45546
[email protected]fc03de22011-12-06 23:28:12547 download->MaybeCompleteDownload();
[email protected]adb2f3d12011-01-23 16:24:54548}
[email protected]9ccbb372008-10-10 18:50:32549
[email protected]fc03de22011-12-06 23:28:12550void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const {
[email protected]c09a8fd2011-11-21 19:54:50551 if (download->GetState() == DownloadItem::REMOVING) {
[email protected]0634626a2012-05-03 19:04:26552 DCHECK(!ContainsKey(downloads_, download));
553 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
554 DCHECK(!ContainsKey(in_progress_, download->GetId()));
555 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17556 return;
557 }
558
559 // Should be in downloads_ if we're not REMOVING.
560 CHECK(ContainsKey(downloads_, download));
561
562 // Check history_downloads_ consistency.
[email protected]5009b7a2012-02-21 18:47:03563 if (download->IsPersisted()) {
[email protected]c09a8fd2011-11-21 19:54:50564 CHECK(ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17565 } else {
[email protected]fc03de22011-12-06 23:28:12566 for (DownloadMap::const_iterator it = history_downloads_.begin();
[email protected]7d413112011-06-16 18:50:17567 it != history_downloads_.end(); ++it) {
[email protected]0634626a2012-05-03 19:04:26568 DCHECK(it->second != download);
[email protected]7d413112011-06-16 18:50:17569 }
570 }
571
[email protected]c09a8fd2011-11-21 19:54:50572 int64 state = download->GetState();
[email protected]61b75a52011-08-29 16:34:34573 base::debug::Alias(&state);
[email protected]c09a8fd2011-11-21 19:54:50574 if (ContainsKey(active_downloads_, download->GetId())) {
[email protected]5009b7a2012-02-21 18:47:03575 if (download->IsPersisted())
[email protected]c09a8fd2011-11-21 19:54:50576 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
577 if (DownloadItem::IN_PROGRESS != download->GetState())
578 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle());
[email protected]f9a2997f2011-09-23 16:54:07579 }
[email protected]c09a8fd2011-11-21 19:54:50580 if (DownloadItem::IN_PROGRESS == download->GetState())
581 CHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]5cd11b6e2011-06-10 20:30:59582}
583
[email protected]5656f8a2011-11-17 16:12:58584bool DownloadManagerImpl::IsDownloadReadyForCompletion(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54585 // If we don't have all the data, the download is not ready for
586 // completion.
[email protected]c09a8fd2011-11-21 19:54:50587 if (!download->AllDataSaved())
[email protected]adb2f3d12011-01-23 16:24:54588 return false;
[email protected]6a7fb042010-02-01 16:30:47589
[email protected]9d7ef802011-02-25 19:03:35590 // If the download is dangerous, but not yet validated, it's not ready for
591 // completion.
[email protected]c09a8fd2011-11-21 19:54:50592 if (download->GetSafetyState() == DownloadItem::DANGEROUS)
[email protected]9d7ef802011-02-25 19:03:35593 return false;
594
[email protected]adb2f3d12011-01-23 16:24:54595 // If the download isn't active (e.g. has been cancelled) it's not
596 // ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50597 if (active_downloads_.count(download->GetId()) == 0)
[email protected]adb2f3d12011-01-23 16:24:54598 return false;
599
600 // If the download hasn't been inserted into the history system
601 // (which occurs strictly after file name determination, intermediate
602 // file rename, and UI display) then it's not ready for completion.
[email protected]5009b7a2012-02-21 18:47:03603 if (!download->IsPersisted())
[email protected]7054b592011-06-22 14:46:42604 return false;
605
606 return true;
[email protected]adb2f3d12011-01-23 16:24:54607}
608
[email protected]5656f8a2011-11-17 16:12:58609void DownloadManagerImpl::MaybeCompleteDownload(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54610 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
611 VLOG(20) << __FUNCTION__ << "()" << " download = "
612 << download->DebugString(false);
613
614 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32615 return;
[email protected]9ccbb372008-10-10 18:50:32616
[email protected]adb2f3d12011-01-23 16:24:54617 // TODO(rdsmith): DCHECK that we only pass through this point
618 // once per download. The natural way to do this is by a state
619 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56620
[email protected]adb2f3d12011-01-23 16:24:54621 // Confirm we're in the proper set of states to be here;
[email protected]9d7ef802011-02-25 19:03:35622 // in in_progress_, have all data, have a history handle, (validated or safe).
[email protected]c09a8fd2011-11-21 19:54:50623 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState());
624 DCHECK_EQ(1u, in_progress_.count(download->GetId()));
625 DCHECK(download->AllDataSaved());
[email protected]5009b7a2012-02-21 18:47:03626 DCHECK(download->IsPersisted());
[email protected]c09a8fd2011-11-21 19:54:50627 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle()));
[email protected]adb2f3d12011-01-23 16:24:54628
[email protected]c2918652011-11-01 18:50:23629 // Give the delegate a chance to override.
630 if (!delegate_->ShouldCompleteDownload(download))
631 return;
632
[email protected]adb2f3d12011-01-23 16:24:54633 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
634 << download->DebugString(false);
635
636 // Remove the id from in_progress
[email protected]c09a8fd2011-11-21 19:54:50637 in_progress_.erase(download->GetId());
[email protected]adb2f3d12011-01-23 16:24:54638
[email protected]2588ea9d2011-08-22 20:59:53639 delegate_->UpdateItemInPersistentStore(download);
[email protected]adb2f3d12011-01-23 16:24:54640
[email protected]f5920322011-03-24 20:34:16641 // Finish the download.
[email protected]48837962011-04-19 17:03:29642 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32643}
644
[email protected]fc03de22011-12-06 23:28:12645void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) {
[email protected]70850c72011-01-11 17:31:27646 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21647 DCHECK(download);
[email protected]2588ea9d2011-08-22 20:59:53648 delegate_->UpdateItemInPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12649 active_downloads_.erase(download->GetId());
650 AssertStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27651}
652
[email protected]5656f8a2011-11-17 16:12:58653void DownloadManagerImpl::OnDownloadRenamedToFinalName(
654 int download_id,
655 const FilePath& full_path,
656 int uniquifier) {
[email protected]da6e3922010-11-24 21:45:50657 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
[email protected]f5920322011-03-24 20:34:16658 << " full_path = \"" << full_path.value() << "\""
659 << " uniquifier = " << uniquifier;
[email protected]ca4b5fa32010-10-09 12:42:18660 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f5920322011-03-24 20:34:16661
[email protected]2e030682010-07-23 19:45:36662 DownloadItem* item = GetDownloadItem(download_id);
663 if (!item)
664 return;
[email protected]6cade212008-12-03 00:32:22665
[email protected]a62d42902012-01-24 17:24:38666 if (item->GetDangerType() == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS ||
[email protected]795b76a2011-12-14 16:52:53667 item->PromptUserForSaveLocation()) {
668 DCHECK_EQ(0, uniquifier)
669 << "We should not uniquify user supplied filenames or safe filenames "
670 "that have already been uniquified.";
[email protected]8fa1eeb52011-04-13 14:18:02671 }
672
[email protected]fabf36d22011-10-28 21:50:17673 BrowserThread::PostTask(
674 BrowserThread::FILE, FROM_HERE,
675 base::Bind(&DownloadFileManager::CompleteDownload,
[email protected]c09a8fd2011-11-21 19:54:50676 file_manager_, item->GetGlobalId()));
[email protected]9ccbb372008-10-10 18:50:32677
[email protected]f5920322011-03-24 20:34:16678 if (uniquifier)
[email protected]c09a8fd2011-11-21 19:54:50679 item->SetPathUniquifier(uniquifier);
[email protected]9ccbb372008-10-10 18:50:32680
[email protected]f5920322011-03-24 20:34:16681 item->OnDownloadRenamedToFinalName(full_path);
[email protected]2588ea9d2011-08-22 20:59:53682 delegate_->UpdatePathForItemInPersistentStore(item, full_path);
initial.commit09911bf2008-07-26 23:55:29683}
684
[email protected]5656f8a2011-11-17 16:12:58685void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]93af2272011-09-21 18:29:17686 DownloadItem* download = GetActiveDownload(download_id);
687 // A cancel at the right time could remove the download from the
688 // |active_downloads_| map before we get here.
689 if (!download)
690 return;
691
692 download->Cancel(true);
693}
694
[email protected]fc03de22011-12-06 23:28:12695void DownloadManagerImpl::DownloadCancelled(DownloadItem* download) {
[email protected]d8472d92011-08-26 20:15:20696 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20697
698 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50699 << " download = " << download->DebugString(true);
700
[email protected]93af2272011-09-21 18:29:17701 RemoveFromActiveList(download);
[email protected]47a881b2011-08-29 22:59:21702 // This function is called from the DownloadItem, so DI state
703 // should already have been updated.
[email protected]fc03de22011-12-06 23:28:12704 AssertStateConsistent(download);
initial.commit09911bf2008-07-26 23:55:29705
[email protected]15d90ba2011-11-03 03:41:55706 if (file_manager_)
707 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29708}
709
[email protected]bf3b08a2012-03-08 01:52:34710void DownloadManagerImpl::OnDownloadInterrupted(
711 int32 download_id,
712 int64 size,
713 const std::string& hash_state,
714 content::DownloadInterruptReason reason) {
[email protected]47a881b2011-08-29 22:59:21715 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
716
717 DownloadItem* download = GetActiveDownload(download_id);
718 if (!download)
719 return;
720
[email protected]be76b7e2011-10-13 12:57:57721 VLOG(20) << __FUNCTION__ << "()"
722 << " reason " << InterruptReasonDebugString(reason)
[email protected]c09a8fd2011-11-21 19:54:50723 << " at offset " << download->GetReceivedBytes()
[email protected]47a881b2011-08-29 22:59:21724 << " size = " << size
725 << " download = " << download->DebugString(true);
726
[email protected]93af2272011-09-21 18:29:17727 RemoveFromActiveList(download);
[email protected]443853c62011-12-22 19:22:41728 download->Interrupted(size, hash_state, reason);
[email protected]93af2272011-09-21 18:29:17729 download->OffThreadCancel(file_manager_);
[email protected]47a881b2011-08-29 22:59:21730}
731
[email protected]5656f8a2011-11-17 16:12:58732DownloadItem* DownloadManagerImpl::GetActiveDownload(int32 download_id) {
[email protected]bf68a00b2011-04-07 17:28:26733 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
734 DownloadMap::iterator it = active_downloads_.find(download_id);
[email protected]bf68a00b2011-04-07 17:28:26735 if (it == active_downloads_.end())
[email protected]47a881b2011-08-29 22:59:21736 return NULL;
[email protected]bf68a00b2011-04-07 17:28:26737
738 DownloadItem* download = it->second;
739
[email protected]47a881b2011-08-29 22:59:21740 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50741 DCHECK_EQ(download_id, download->GetId());
[email protected]4cd82f72011-05-23 19:15:01742
[email protected]47a881b2011-08-29 22:59:21743 return download;
744}
[email protected]54610672011-07-18 18:24:43745
[email protected]5656f8a2011-11-17 16:12:58746void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) {
[email protected]93af2272011-09-21 18:29:17747 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
748 DCHECK(download);
749
750 // Clean up will happen when the history system create callback runs if we
751 // don't have a valid db_handle yet.
[email protected]5009b7a2012-02-21 18:47:03752 if (download->IsPersisted()) {
[email protected]c09a8fd2011-11-21 19:54:50753 in_progress_.erase(download->GetId());
754 active_downloads_.erase(download->GetId());
[email protected]93af2272011-09-21 18:29:17755 delegate_->UpdateItemInPersistentStore(download);
756 }
757}
758
[email protected]fd3a82832012-01-19 20:35:12759bool DownloadManagerImpl::GenerateFileHash() {
760 return delegate_->GenerateFileHash();
761}
762
[email protected]5656f8a2011-11-17 16:12:58763content::DownloadManagerDelegate* DownloadManagerImpl::delegate() const {
764 return delegate_;
765}
766
767void DownloadManagerImpl::SetDownloadManagerDelegate(
[email protected]1bd0ef12011-10-20 05:24:17768 content::DownloadManagerDelegate* delegate) {
[email protected]9bb54ee2011-10-12 17:43:35769 delegate_ = delegate;
770}
771
[email protected]5656f8a2011-11-17 16:12:58772int DownloadManagerImpl::RemoveDownloadItems(
[email protected]6d0146c2011-08-04 19:13:04773 const DownloadVector& pending_deletes) {
774 if (pending_deletes.empty())
775 return 0;
776
777 // Delete from internal maps.
778 for (DownloadVector::const_iterator it = pending_deletes.begin();
779 it != pending_deletes.end();
780 ++it) {
781 DownloadItem* download = *it;
782 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50783 history_downloads_.erase(download->GetDbHandle());
784 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:04785 downloads_.erase(download);
786 }
787
788 // Tell observers to refresh their views.
789 NotifyModelChanged();
790
791 // Delete the download items themselves.
792 const int num_deleted = static_cast<int>(pending_deletes.size());
793 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
794 return num_deleted;
795}
796
[email protected]fc03de22011-12-06 23:28:12797void DownloadManagerImpl::DownloadRemoved(DownloadItem* download) {
798 if (history_downloads_.find(download->GetDbHandle()) ==
799 history_downloads_.end())
[email protected]93af2272011-09-21 18:29:17800 return;
801
802 // Make history update.
[email protected]93af2272011-09-21 18:29:17803 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29804
805 // Remove from our tables and delete.
[email protected]6d0146c2011-08-04 19:13:04806 int downloads_count = RemoveDownloadItems(DownloadVector(1, download));
[email protected]f04182f32010-12-10 19:12:07807 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29808}
809
[email protected]fd3a82832012-01-19 20:35:12810int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
811 base::Time remove_end) {
[email protected]2588ea9d2011-08-22 20:59:53812 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29813
[email protected]a312a442010-12-15 23:40:33814 // All downloads visible to the user will be in the history,
815 // so scan that map.
[email protected]6d0146c2011-08-04 19:13:04816 DownloadVector pending_deletes;
817 for (DownloadMap::const_iterator it = history_downloads_.begin();
818 it != history_downloads_.end();
819 ++it) {
initial.commit09911bf2008-07-26 23:55:29820 DownloadItem* download = it->second;
[email protected]c09a8fd2011-11-21 19:54:50821 if (download->GetStartTime() >= remove_begin &&
822 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04823 (download->IsComplete() || download->IsCancelled())) {
[email protected]fc03de22011-12-06 23:28:12824 AssertStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28825 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29826 }
initial.commit09911bf2008-07-26 23:55:29827 }
[email protected]6d0146c2011-08-04 19:13:04828 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29829}
830
[email protected]fd3a82832012-01-19 20:35:12831int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59832 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29833}
834
[email protected]5656f8a2011-11-17 16:12:58835int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]da4a5582011-10-17 19:08:06836 download_stats::RecordClearAllSize(history_downloads_.size());
[email protected]d41355e6f2009-04-07 21:21:12837 // The null times make the date range unbounded.
838 return RemoveDownloadsBetween(base::Time(), base::Time());
839}
840
initial.commit09911bf2008-07-26 23:55:29841// Initiate a download of a specific URL. We send the request to the
842// ResourceDispatcherHost, and let it send us responses like a regular
843// download.
[email protected]0d4e30c2012-01-28 00:47:53844void DownloadManagerImpl::DownloadUrl(
845 const GURL& url,
846 const GURL& referrer,
847 const std::string& referrer_charset,
848 bool prefer_cache,
[email protected]27678b2a2012-02-04 22:09:14849 int64 post_id,
[email protected]29a5ffc82012-03-13 19:35:58850 const content::DownloadSaveInfo& save_info,
[email protected]89e6aa72012-03-12 22:51:33851 WebContents* web_contents,
852 const OnStartedCallback& callback) {
[email protected]ea114722012-03-12 01:11:25853 ResourceDispatcherHostImpl* resource_dispatcher_host =
854 ResourceDispatcherHostImpl::Get();
855 DCHECK(resource_dispatcher_host);
[email protected]443853c62011-12-22 19:22:41856
[email protected]ed24fad2011-05-10 22:44:01857 // We send a pointer to content::ResourceContext, instead of the usual
858 // reference, so that a copy of the object isn't made.
[email protected]fabf36d22011-10-28 21:50:17859 // base::Bind can't handle 7 args, so we use URLParams and RenderParams.
860 BrowserThread::PostTask(
861 BrowserThread::IO, FROM_HERE,
[email protected]0d4e30c2012-01-28 00:47:53862 base::Bind(
863 &BeginDownload,
[email protected]89e6aa72012-03-12 22:51:33864 URLParams(url, referrer, post_id, prefer_cache),
[email protected]0d4e30c2012-01-28 00:47:53865 save_info,
866 resource_dispatcher_host,
[email protected]fbc5e5f92012-01-02 06:08:32867 RenderParams(web_contents->GetRenderProcessHost()->GetID(),
[email protected]9f76c1e2012-03-05 15:15:58868 web_contents->GetRenderViewHost()->GetRoutingID()),
[email protected]89e6aa72012-03-12 22:51:33869 web_contents->GetBrowserContext()->GetResourceContext(),
870 callback));
initial.commit09911bf2008-07-26 23:55:29871}
872
[email protected]5656f8a2011-11-17 16:12:58873void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29874 observers_.AddObserver(observer);
[email protected]a1e41e72012-02-22 17:41:25875 // TODO: It is the responsibility of the observers to query the
876 // DownloadManager. Remove the following call from here and update all
877 // observers.
[email protected]75e51b52012-02-04 16:57:54878 observer->ModelChanged(this);
initial.commit09911bf2008-07-26 23:55:29879}
880
[email protected]5656f8a2011-11-17 16:12:58881void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29882 observers_.RemoveObserver(observer);
883}
884
[email protected]84d57412012-03-03 08:59:55885void DownloadManagerImpl::FileSelected(const FilePath& path,
886 int32 download_id) {
[email protected]4cd82f72011-05-23 19:15:01887 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
888
[email protected]4cd82f72011-05-23 19:15:01889 DownloadItem* download = GetActiveDownloadItem(download_id);
890 if (!download)
891 return;
892 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
893 << " download = " << download->DebugString(true);
894
[email protected]71f55842012-03-24 04:09:02895 // Retain the last directory that was picked by the user. Exclude temporary
896 // downloads since the path likely points at the location of a temporary file.
897 if (download->PromptUserForSaveLocation() && !download->IsTemporary())
[email protected]7ae7c2cb2009-01-06 23:31:41898 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35899
[email protected]4cd82f72011-05-23 19:15:01900 // Make sure the initial file name is set only once.
901 ContinueDownloadWithPath(download, path);
initial.commit09911bf2008-07-26 23:55:29902}
903
[email protected]84d57412012-03-03 08:59:55904void DownloadManagerImpl::FileSelectionCanceled(int32 download_id) {
initial.commit09911bf2008-07-26 23:55:29905 // The user didn't pick a place to save the file, so need to cancel the
906 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01907 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01908
909 DownloadItem* download = GetActiveDownloadItem(download_id);
910 if (!download)
911 return;
912
913 VLOG(20) << __FUNCTION__ << "()"
914 << " download = " << download->DebugString(true);
915
[email protected]8f8bc112012-02-22 12:36:31916 download->Cancel(true);
[email protected]4cd82f72011-05-23 19:15:01917}
918
initial.commit09911bf2008-07-26 23:55:29919// Operations posted to us from the history service ----------------------------
920
921// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25922// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]5656f8a2011-11-17 16:12:58923void DownloadManagerImpl::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25924 std::vector<DownloadPersistentStoreInfo>* entries) {
initial.commit09911bf2008-07-26 23:55:29925 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]deb40832012-02-23 15:41:37926 int64 db_handle = entries->at(i).db_handle;
927 base::debug::Alias(&db_handle);
[email protected]0634626a2012-05-03 19:04:26928 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]deb40832012-02-23 15:41:37929
[email protected]ef17c9a2012-02-09 05:08:09930 net::BoundNetLog bound_net_log =
931 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]fc03de22011-12-06 23:28:12932 DownloadItem* download = new DownloadItemImpl(
[email protected]ef17c9a2012-02-09 05:08:09933 this, GetNextId(), entries->at(i), bound_net_log);
[email protected]f04182f32010-12-10 19:12:07934 downloads_.insert(download);
[email protected]c09a8fd2011-11-21 19:54:50935 history_downloads_[download->GetDbHandle()] = download;
[email protected]da6e3922010-11-24 21:45:50936 VLOG(20) << __FUNCTION__ << "()" << i << ">"
937 << " download = " << download->DebugString(true);
initial.commit09911bf2008-07-26 23:55:29938 }
[email protected]b0ab1d42010-02-24 19:29:28939 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48940 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29941}
942
[email protected]5656f8a2011-11-17 16:12:58943void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItem* download,
944 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27945 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0634626a2012-05-03 19:04:26946 DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]1e9fe7ff2011-06-24 17:37:33947
[email protected]da4a5582011-10-17 19:08:06948 download_stats::RecordHistorySize(history_downloads_.size());
949
[email protected]5009b7a2012-02-21 18:47:03950 DCHECK(!download->IsPersisted());
[email protected]c09a8fd2011-11-21 19:54:50951 download->SetDbHandle(db_handle);
[email protected]5009b7a2012-02-21 18:47:03952 download->SetIsPersisted();
[email protected]5bcd73eb2011-03-23 21:14:02953
[email protected]0634626a2012-05-03 19:04:26954 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]c09a8fd2011-11-21 19:54:50955 history_downloads_[download->GetDbHandle()] = download;
[email protected]6d0146c2011-08-04 19:13:04956
957 // Show in the appropriate browser UI.
958 // This includes buttons to save or cancel, for a dangerous download.
959 ShowDownloadInBrowser(download);
960
961 // Inform interested objects about the new download.
962 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19963}
964
[email protected]2588ea9d2011-08-22 20:59:53965
[email protected]5656f8a2011-11-17 16:12:58966void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id,
967 int64 db_handle) {
[email protected]2588ea9d2011-08-22 20:59:53968 if (save_page_downloads_.count(download_id)) {
969 OnSavePageItemAddedToPersistentStore(download_id, db_handle);
970 } else if (active_downloads_.count(download_id)) {
971 OnDownloadItemAddedToPersistentStore(download_id, db_handle);
972 }
973 // It's valid that we don't find a matching item, i.e. on shutdown.
974}
975
[email protected]f9a45b02011-06-30 23:49:19976// Once the new DownloadItem's creation info has been committed to the history
977// service, we associate the DownloadItem with the db handle, update our
978// 'history_downloads_' map and inform observers.
[email protected]5656f8a2011-11-17 16:12:58979void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore(
980 int32 download_id, int64 db_handle) {
[email protected]f9a45b02011-06-30 23:49:19981 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]19420cc2011-07-18 17:43:45982 DownloadItem* download = GetActiveDownloadItem(download_id);
[email protected]93af2272011-09-21 18:29:17983 if (!download)
[email protected]19420cc2011-07-18 17:43:45984 return;
[email protected]54610672011-07-18 18:24:43985
986 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
987 << " download_id = " << download_id
988 << " download = " << download->DebugString(true);
[email protected]f9a45b02011-06-30 23:49:19989
[email protected]e5107ce2011-09-19 20:36:13990 int32 matching_item_download_id
991 = (ContainsKey(history_downloads_, db_handle) ?
[email protected]c09a8fd2011-11-21 19:54:50992 history_downloads_[db_handle]->GetId() : 0);
[email protected]e5107ce2011-09-19 20:36:13993 base::debug::Alias(&matching_item_download_id);
994
[email protected]0634626a2012-05-03 19:04:26995 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:20996
[email protected]f9a45b02011-06-30 23:49:19997 AddDownloadItemToHistory(download, db_handle);
initial.commit09911bf2008-07-26 23:55:29998
[email protected]93af2272011-09-21 18:29:17999 // If the download is still in progress, try to complete it.
1000 //
1001 // Otherwise, download has been cancelled or interrupted before we've
1002 // received the DB handle. We post one final message to the history
1003 // service so that it can be properly in sync with the DownloadItem's
1004 // completion status, and also inform any observers so that they get
1005 // more than just the start notification.
1006 if (download->IsInProgress()) {
1007 MaybeCompleteDownload(download);
1008 } else {
[email protected]0634626a2012-05-03 19:04:261009 DCHECK(download->IsCancelled());
[email protected]93af2272011-09-21 18:29:171010 in_progress_.erase(download_id);
1011 active_downloads_.erase(download_id);
1012 delegate_->UpdateItemInPersistentStore(download);
1013 download->UpdateObservers();
1014 }
initial.commit09911bf2008-07-26 23:55:291015}
1016
[email protected]5656f8a2011-11-17 16:12:581017void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]a29e4f22012-04-12 21:22:031018 // The 'contents' may no longer exist if the user closed the contents before
1019 // we get this start completion event.
[email protected]a62d42902012-01-24 17:24:381020 WebContents* content = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:261021
1022 // If the contents no longer exists, we ask the embedder to suggest another
[email protected]a29e4f22012-04-12 21:22:031023 // contents.
[email protected]da1a27b2011-07-29 23:16:331024 if (!content)
[email protected]ef9572e2012-01-04 22:14:121025 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:531026
[email protected]0bfbf882011-12-22 18:19:271027 if (content && content->GetDelegate())
1028 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:531029}
1030
[email protected]5656f8a2011-11-17 16:12:581031int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:561032 // Don't use in_progress_.count() because Cancel() leaves items in
1033 // in_progress_ if they haven't made it into the persistent store yet.
1034 // Need to actually look at each item's state.
1035 int count = 0;
1036 for (DownloadMap::const_iterator it = in_progress_.begin();
1037 it != in_progress_.end(); ++it) {
1038 DownloadItem* item = it->second;
1039 if (item->IsInProgress())
1040 ++count;
1041 }
1042 return count;
[email protected]5656f8a2011-11-17 16:12:581043}
1044
[email protected]6cade212008-12-03 00:32:221045// Clears the last download path, used to initialize "save as" dialogs.
[email protected]5656f8a2011-11-17 16:12:581046void DownloadManagerImpl::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:411047 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:381048}
[email protected]b0ab1d42010-02-24 19:29:281049
[email protected]5656f8a2011-11-17 16:12:581050void DownloadManagerImpl::NotifyModelChanged() {
[email protected]75e51b52012-02-04 16:57:541051 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this));
[email protected]b0ab1d42010-02-24 19:29:281052}
1053
[email protected]5656f8a2011-11-17 16:12:581054DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) {
[email protected]4cd82f72011-05-23 19:15:011055 // The |history_downloads_| map is indexed by the download's db_handle,
1056 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:071057 for (DownloadMap::iterator it = history_downloads_.begin();
1058 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:361059 DownloadItem* item = it->second;
[email protected]c09a8fd2011-11-21 19:54:501060 if (item->GetId() == download_id)
[email protected]2e030682010-07-23 19:45:361061 return item;
1062 }
1063 return NULL;
1064}
1065
[email protected]5656f8a2011-11-17 16:12:581066DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) {
[email protected]5d3e83642011-12-16 01:14:361067 if (ContainsKey(active_downloads_, download_id))
1068 return active_downloads_[download_id];
1069 return NULL;
[email protected]4cd82f72011-05-23 19:15:011070}
1071
[email protected]57fd1252010-12-23 17:24:091072// Confirm that everything in all maps is also in |downloads_|, and that
1073// everything in |downloads_| is also in some other map.
[email protected]5656f8a2011-11-17 16:12:581074void DownloadManagerImpl::AssertContainersConsistent() const {
[email protected]f04182f32010-12-10 19:12:071075#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:091076 // Turn everything into sets.
[email protected]6d0146c2011-08-04 19:13:041077 const DownloadMap* input_maps[] = {&active_downloads_,
1078 &history_downloads_,
1079 &save_page_downloads_};
1080 DownloadSet active_set, history_set, save_page_set;
1081 DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set};
1082 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:091083 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
1084 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:041085 it != input_maps[i]->end(); ++it) {
1086 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:071087 }
1088 }
[email protected]57fd1252010-12-23 17:24:091089
1090 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:091091 DownloadSet downloads_union;
1092 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1093 DownloadSet remainder;
1094 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1095 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
1096 downloads_.begin(), downloads_.end(),
1097 insert_it);
1098 DCHECK(remainder.empty());
1099 std::insert_iterator<DownloadSet>
1100 insert_union(downloads_union, downloads_union.end());
1101 std::set_union(downloads_union.begin(), downloads_union.end(),
1102 all_sets[i]->begin(), all_sets[i]->end(),
1103 insert_union);
1104 }
1105
1106 // Is everything in downloads_ present in one of the other sets?
1107 DownloadSet remainder;
1108 std::insert_iterator<DownloadSet>
1109 insert_remainder(remainder, remainder.begin());
1110 std::set_difference(downloads_.begin(), downloads_.end(),
1111 downloads_union.begin(), downloads_union.end(),
1112 insert_remainder);
1113 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:071114#endif
1115}
1116
[email protected]6d0146c2011-08-04 19:13:041117// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:531118// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:041119// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:531120// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
1121// that the history event is update regardless of the order in which these two
1122// events complete.
[email protected]6d0146c2011-08-04 19:13:041123// If something removes the download item from the download manager (Remove,
1124// Shutdown) the result will be that the SavePage system will not be able to
1125// properly update the download item (which no longer exists) or the download
1126// history, but the action will complete properly anyway. This may lead to the
1127// history entry being wrong on a reload of chrome (specifically in the case of
1128// Initiation -> History Callback -> Removal -> Completion), but there's no way
1129// to solve that without canceling on Remove (which would then update the DB).
1130
[email protected]5656f8a2011-11-17 16:12:581131void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore(
1132 int32 download_id, int64 db_handle) {
[email protected]6d0146c2011-08-04 19:13:041133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1134
1135 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
1136 // This can happen if the download manager is shutting down and all maps
1137 // have been cleared.
1138 if (it == save_page_downloads_.end())
1139 return;
1140
1141 DownloadItem* download = it->second;
1142 if (!download) {
1143 NOTREACHED();
1144 return;
1145 }
1146
[email protected]0634626a2012-05-03 19:04:261147 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:201148
[email protected]6d0146c2011-08-04 19:13:041149 AddDownloadItemToHistory(download, db_handle);
1150
1151 // Finalize this download if it finished before the history callback.
1152 if (!download->IsInProgress())
1153 SavePageDownloadFinished(download);
1154}
1155
[email protected]5656f8a2011-11-17 16:12:581156void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) {
[email protected]5009b7a2012-02-21 18:47:031157 if (download->IsPersisted()) {
[email protected]2588ea9d2011-08-22 20:59:531158 delegate_->UpdateItemInPersistentStore(download);
[email protected]c09a8fd2011-11-21 19:54:501159 DCHECK(ContainsKey(save_page_downloads_, download->GetId()));
1160 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:041161
1162 if (download->IsComplete())
[email protected]ad50def52011-10-19 23:17:071163 content::NotificationService::current()->Notify(
[email protected]6d0146c2011-08-04 19:13:041164 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
[email protected]6c2381d2011-10-19 02:52:531165 content::Source<DownloadManager>(this),
1166 content::Details<DownloadItem>(download));
[email protected]6d0146c2011-08-04 19:13:041167 }
1168}
[email protected]da4a5582011-10-17 19:08:061169
[email protected]fc03de22011-12-06 23:28:121170void DownloadManagerImpl::DownloadOpened(DownloadItem* download) {
[email protected]da4a5582011-10-17 19:08:061171 delegate_->UpdateItemInPersistentStore(download);
1172 int num_unopened = 0;
1173 for (DownloadMap::iterator it = history_downloads_.begin();
1174 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:501175 if (it->second->IsComplete() && !it->second->GetOpened())
[email protected]da4a5582011-10-17 19:08:061176 ++num_unopened;
1177 }
1178 download_stats::RecordOpensOutstanding(num_unopened);
1179}
[email protected]5656f8a2011-11-17 16:12:581180
[email protected]5948e1a2012-03-10 00:19:181181void DownloadManagerImpl::SetFileManagerForTesting(
1182 DownloadFileManager* file_manager) {
[email protected]5656f8a2011-11-17 16:12:581183 file_manager_ = file_manager;
1184}