blob: ae2928991c0aa1c2af3999086b48938889948223 [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]bb1a4212011-08-22 22:38:2523#include "content/browser/download/download_persistent_store_info.h"
[email protected]da4a5582011-10-17 19:08:0624#include "content/browser/download/download_stats.h"
[email protected]be76b7e2011-10-13 12:57:5725#include "content/browser/download/interrupt_reasons.h"
[email protected]7324d1d2011-03-01 05:02:1626#include "content/browser/renderer_host/render_view_host.h"
27#include "content/browser/renderer_host/resource_dispatcher_host.h"
28#include "content/browser/tab_contents/tab_contents.h"
[email protected]ccb797302011-12-15 16:55:1129#include "content/public/browser/browser_context.h"
[email protected]c38831a12011-10-28 12:44:4930#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:4431#include "content/public/browser/content_browser_client.h"
[email protected]1bd0ef12011-10-20 05:24:1732#include "content/public/browser/download_manager_delegate.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]a896ce32012-01-09 22:04:0739// TODO(benjhayden): Change this to DCHECK when we have more debugging
40// information from the next dev cycle, before the next stable/beta branch is
41// cut, in order to prevent unnecessary crashes on those channels. If we still
42// don't have root cause before the dev cycle after the next stable/beta
43// releases, uncomment it out to re-enable debugging checks. Whenever this macro
44// is toggled, the corresponding macro in download_database.cc should also
45// be toggled. When 96627 is fixed, this macro and all its usages can be
46// deleted or permanently changed to DCHECK as appropriate.
47#define CHECK_96627 CHECK
48
[email protected]631bb742011-11-02 11:29:3949using content::BrowserThread;
[email protected]98e814062012-01-27 00:35:4250using content::DownloadId;
[email protected]e582fdd2011-12-20 16:48:1751using content::DownloadItem;
[email protected]2a6bc3e2011-12-28 23:51:3352using content::WebContents;
[email protected]631bb742011-11-02 11:29:3953
[email protected]a0ce3282011-08-19 20:49:5254namespace {
55
[email protected]fabf36d22011-10-28 21:50:1756// Param structs exist because base::Bind can only handle 6 args.
57struct URLParams {
[email protected]27678b2a2012-02-04 22:09:1458 URLParams(const GURL& url, const GURL& referrer, int64 post_id)
59 : url_(url), referrer_(referrer), post_id_(post_id) {}
[email protected]fabf36d22011-10-28 21:50:1760 GURL url_;
61 GURL referrer_;
[email protected]27678b2a2012-02-04 22:09:1462 int64 post_id_;
[email protected]fabf36d22011-10-28 21:50:1763};
64
65struct RenderParams {
66 RenderParams(int rpi, int rvi)
67 : render_process_id_(rpi), render_view_id_(rvi) {}
68 int render_process_id_;
69 int render_view_id_;
70};
71
72void BeginDownload(const URLParams& url_params,
[email protected]0d4e30c2012-01-28 00:47:5373 bool prefer_cache,
[email protected]fabf36d22011-10-28 21:50:1774 const DownloadSaveInfo& save_info,
75 ResourceDispatcherHost* resource_dispatcher_host,
76 const RenderParams& render_params,
77 const content::ResourceContext* context) {
[email protected]c1ba99842012-01-19 20:56:0578 scoped_ptr<net::URLRequest> request(
79 new net::URLRequest(url_params.url_, resource_dispatcher_host));
[email protected]fabf36d22011-10-28 21:50:1780 request->set_referrer(url_params.referrer_.spec());
[email protected]27678b2a2012-02-04 22:09:1481 if (url_params.post_id_ >= 0) {
82 // The POST in this case does not have an actual body, and only works
83 // when retrieving data from cache. This is done because we don't want
84 // to do a re-POST without user consent, and currently don't have a good
85 // plan on how to display the UI for that.
86 DCHECK(prefer_cache);
87 request->set_method("POST");
88 scoped_refptr<net::UploadData> upload_data = new net::UploadData();
89 upload_data->set_identifier(url_params.post_id_);
90 request->set_upload(upload_data);
91 }
[email protected]c79a0c02011-08-22 22:37:3792 resource_dispatcher_host->BeginDownload(
[email protected]0d4e30c2012-01-28 00:47:5393 request.Pass(), prefer_cache, save_info,
[email protected]8e3ae68c2011-09-16 22:15:4794 DownloadResourceHandler::OnStartedCallback(),
[email protected]fabf36d22011-10-28 21:50:1795 render_params.render_process_id_,
96 render_params.render_view_id_,
[email protected]c79a0c02011-08-22 22:37:3797 *context);
[email protected]a0ce3282011-08-19 20:49:5298}
99
[email protected]33d22102012-01-25 17:46:53100class MapValueIteratorAdapter {
101 public:
102 explicit MapValueIteratorAdapter(
103 base::hash_map<int64, DownloadItem*>::const_iterator iter)
104 : iter_(iter) {
105 }
106 ~MapValueIteratorAdapter() {}
107
108 DownloadItem* operator*() { return iter_->second; }
109
110 MapValueIteratorAdapter& operator++() {
111 ++iter_;
112 return *this;
113 }
114
115 bool operator!=(const MapValueIteratorAdapter& that) const {
116 return iter_ != that.iter_;
117 }
118
119 private:
120 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
121 // Allow copy and assign.
122};
123
[email protected]a0ce3282011-08-19 20:49:52124} // namespace
125
[email protected]99907362012-01-11 05:41:40126namespace content {
127
128// static
129DownloadManager* DownloadManager::Create(
[email protected]75e51b52012-02-04 16:57:54130 content::DownloadManagerDelegate* delegate) {
131 return new DownloadManagerImpl(delegate);
[email protected]99907362012-01-11 05:41:40132}
133
134} // namespace content
135
[email protected]5656f8a2011-11-17 16:12:58136DownloadManagerImpl::DownloadManagerImpl(
[email protected]75e51b52012-02-04 16:57:54137 content::DownloadManagerDelegate* delegate)
[email protected]5656f8a2011-11-17 16:12:58138 : shutdown_needed_(false),
139 browser_context_(NULL),
140 file_manager_(NULL),
[email protected]5656f8a2011-11-17 16:12:58141 delegate_(delegate),
[email protected]5656f8a2011-11-17 16:12:58142 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) {
initial.commit09911bf2008-07-26 23:55:29143}
144
[email protected]5656f8a2011-11-17 16:12:58145DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13146 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29147}
148
[email protected]5656f8a2011-11-17 16:12:58149DownloadId DownloadManagerImpl::GetNextId() {
[email protected]98e814062012-01-27 00:35:42150 return delegate_->GetNextId();
[email protected]2909e342011-10-29 00:46:53151}
152
[email protected]fc03de22011-12-06 23:28:12153bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) {
154 return delegate_->ShouldOpenDownload(item);
155}
156
157bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
158 return delegate_->ShouldOpenFileBasedOnExtension(path);
159}
160
[email protected]5656f8a2011-11-17 16:12:58161void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50162 VLOG(20) << __FUNCTION__ << "()"
163 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13164 if (!shutdown_needed_)
165 return;
166 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29167
[email protected]75e51b52012-02-04 16:57:54168 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08169 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13170
171 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:18172 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]fabf36d22011-10-28 21:50:17173 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
174 file_manager_, make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:13175 }
initial.commit09911bf2008-07-26 23:55:29176
[email protected]f04182f32010-12-10 19:12:07177 AssertContainersConsistent();
178
179 // Go through all downloads in downloads_. Dangerous ones we need to
180 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:09181 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:07182 DownloadItem* download = *it;
183
184 // Save iterator from potential erases in this set done by called code.
185 // Iterators after an erasure point are still valid for lists and
186 // associative containers such as sets.
187 it++;
188
[email protected]c09a8fd2011-11-21 19:54:50189 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29190 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07191 // The user hasn't accepted it, so we need to remove it
192 // from the disk. This may or may not result in it being
193 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12194 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07195 // removes and deletes it if it's known to the history service)
196 // so the only thing we know after calling this function is that
197 // the download was deleted if-and-only-if it was removed
198 // from all queues.
[email protected]303077002011-04-19 23:21:01199 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26200 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17201 download->Cancel(false);
202 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29203 }
204 }
205
[email protected]f04182f32010-12-10 19:12:07206 // At this point, all dangerous downloads have had their files removed
207 // and all in progress downloads have been cancelled. We can now delete
208 // anything left.
[email protected]9ccbb372008-10-10 18:50:32209
[email protected]5cd11b6e2011-06-10 20:30:59210 // Copy downloads_ to separate container so as not to set off checks
211 // in DownloadItem destruction.
212 DownloadSet downloads_to_delete;
213 downloads_to_delete.swap(downloads_);
214
initial.commit09911bf2008-07-26 23:55:29215 in_progress_.clear();
[email protected]70850c72011-01-11 17:31:27216 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59217 history_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59218 STLDeleteElements(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29219
[email protected]41f558fb2012-01-09 22:59:58220 // We'll have nothing more to report to the observers after this point.
221 observers_.Clear();
222
[email protected]6d0146c2011-08-04 19:13:04223 DCHECK(save_page_downloads_.empty());
224
initial.commit09911bf2008-07-26 23:55:29225 file_manager_ = NULL;
[email protected]2588ea9d2011-08-22 20:59:53226 delegate_->Shutdown();
initial.commit09911bf2008-07-26 23:55:29227}
228
[email protected]5656f8a2011-11-17 16:12:58229void DownloadManagerImpl::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04230 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57231 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58232
[email protected]f04182f32010-12-10 19:12:07233 for (DownloadMap::iterator it = history_downloads_.begin();
234 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50235 if (it->second->IsTemporary() &&
[email protected]fdd2715c2011-12-09 22:24:20236 (dir_path.empty() || it->second->GetFullPath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57237 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58238 }
[email protected]6aa4a1c02010-01-15 18:49:58239}
240
[email protected]5656f8a2011-11-17 16:12:58241void DownloadManagerImpl::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04242 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57243 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34244
[email protected]f04182f32010-12-10 19:12:07245 for (DownloadMap::iterator it = history_downloads_.begin();
246 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50247 if (!it->second->IsTemporary() &&
248 (dir_path.empty() || it->second->GetFullPath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57249 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34250 }
[email protected]8ddbd66a2010-05-21 16:38:34251}
252
[email protected]5656f8a2011-11-17 16:12:58253void DownloadManagerImpl::SearchDownloads(const string16& query,
254 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26255 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42256
[email protected]f04182f32010-12-10 19:12:07257 for (DownloadMap::iterator it = history_downloads_.begin();
258 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42259 DownloadItem* download_item = it->second;
260
[email protected]c09a8fd2011-11-21 19:54:50261 if (download_item->IsTemporary())
[email protected]d3b12902010-08-16 23:39:42262 continue;
263
264 // Display Incognito downloads only in Incognito window, and vice versa.
265 // The Incognito Downloads page will get the list of non-Incognito downloads
266 // from its parent profile.
[email protected]c09a8fd2011-11-21 19:54:50267 if (browser_context_->IsOffTheRecord() != download_item->IsOtr())
[email protected]d3b12902010-08-16 23:39:42268 continue;
269
270 if (download_item->MatchesQuery(query_lower))
271 result->push_back(download_item);
272 }
[email protected]d3b12902010-08-16 23:39:42273}
274
initial.commit09911bf2008-07-26 23:55:29275// Query the history service for information about all persisted downloads.
[email protected]5656f8a2011-11-17 16:12:58276bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03277 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29278 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
279 shutdown_needed_ = true;
280
[email protected]6d0c9fb2011-08-22 19:26:03281 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46282
[email protected]b39e7a88b2012-01-10 21:43:17283 // In test mode, there may be no ResourceDispatcherHost. In this case it's
284 // safe to avoid setting |file_manager_| because we only call a small set of
285 // functions, none of which need it.
[email protected]99907362012-01-11 05:41:40286 ResourceDispatcherHost* rdh = ResourceDispatcherHost::Get();
[email protected]b39e7a88b2012-01-10 21:43:17287 if (rdh) {
288 file_manager_ = rdh->download_file_manager();
289 DCHECK(file_manager_);
290 }
initial.commit09911bf2008-07-26 23:55:29291
initial.commit09911bf2008-07-26 23:55:29292 return true;
293}
294
[email protected]aa9881c2011-08-15 18:01:12295// We have received a message from DownloadFileManager about a new download.
[email protected]5656f8a2011-11-17 16:12:58296void DownloadManagerImpl::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35298
[email protected]aa9881c2011-08-15 18:01:12299 if (delegate_->ShouldStartDownload(download_id))
300 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35301}
302
[email protected]5656f8a2011-11-17 16:12:58303void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
305 for (DownloadMap::iterator it = history_downloads_.begin();
306 it != history_downloads_.end(); ++it) {
307 CheckForFileRemoval(it->second);
308 }
309}
310
[email protected]5656f8a2011-11-17 16:12:58311void DownloadManagerImpl::CheckForFileRemoval(DownloadItem* download_item) {
[email protected]9fc114672011-06-15 08:17:48312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
313 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50314 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48315 BrowserThread::PostTask(
316 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58317 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]c09a8fd2011-11-21 19:54:50318 this, download_item->GetDbHandle(),
[email protected]fabf36d22011-10-28 21:50:17319 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48320 }
321}
322
[email protected]5656f8a2011-11-17 16:12:58323void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]9fc114672011-06-15 08:17:48324 int64 db_handle, const FilePath& path) {
325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
326 if (!file_util::PathExists(path)) {
327 BrowserThread::PostTask(
328 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58329 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
330 this,
331 db_handle));
[email protected]9fc114672011-06-15 08:17:48332 }
333}
334
[email protected]5656f8a2011-11-17 16:12:58335void DownloadManagerImpl::OnFileRemovalDetected(int64 db_handle) {
[email protected]9fc114672011-06-15 08:17:48336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
337 DownloadMap::iterator it = history_downloads_.find(db_handle);
338 if (it != history_downloads_.end()) {
339 DownloadItem* download_item = it->second;
340 download_item->OnDownloadedFileRemoved();
341 }
342}
343
[email protected]443853c62011-12-22 19:22:41344void DownloadManagerImpl::RestartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18345 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29346
[email protected]4cd82f72011-05-23 19:15:01347 DownloadItem* download = GetActiveDownloadItem(download_id);
348 if (!download)
349 return;
350
351 VLOG(20) << __FUNCTION__ << "()"
352 << " download = " << download->DebugString(true);
353
[email protected]c09a8fd2011-11-21 19:54:50354 FilePath suggested_path = download->GetSuggestedPath();
[email protected]4cd82f72011-05-23 19:15:01355
[email protected]c09a8fd2011-11-21 19:54:50356 if (download->PromptUserForSaveLocation()) {
initial.commit09911bf2008-07-26 23:55:29357 // We must ask the user for the place to put the download.
[email protected]a62d42902012-01-24 17:24:38358 WebContents* contents = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:26359
[email protected]4cd82f72011-05-23 19:15:01360 // |id_ptr| will be deleted in either FileSelected() or
[email protected]93af2272011-09-21 18:29:17361 // FileSelectionCancelled().
[email protected]4cd82f72011-05-23 19:15:01362 int32* id_ptr = new int32;
363 *id_ptr = download_id;
[email protected]795b76a2011-12-14 16:52:53364 FilePath target_path;
365 // If |download| is a potentially dangerous file, then |suggested_path|
366 // contains the intermediate name instead of the final download
367 // filename. The latter is GetTargetName().
[email protected]a62d42902012-01-24 17:24:38368 if (download->GetDangerType() !=
369 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)
[email protected]795b76a2011-12-14 16:52:53370 target_path = suggested_path.DirName().Append(download->GetTargetName());
371 else
372 target_path = suggested_path;
[email protected]99cb7f82011-07-28 17:27:26373
[email protected]795b76a2011-12-14 16:52:53374 delegate_->ChooseDownloadPath(contents, target_path,
375 reinterpret_cast<void*>(id_ptr));
[email protected]f5920322011-03-24 20:34:16376 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]75e51b52012-02-04 16:57:54377 SelectFileDialogDisplayed(this, download_id));
initial.commit09911bf2008-07-26 23:55:29378 } else {
379 // No prompting for download, just continue with the suggested name.
[email protected]4cd82f72011-05-23 19:15:01380 ContinueDownloadWithPath(download, suggested_path);
initial.commit09911bf2008-07-26 23:55:29381 }
382}
383
[email protected]37757c62011-12-20 20:07:12384content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58385 return browser_context_;
386}
387
388FilePath DownloadManagerImpl::LastDownloadPath() {
389 return last_download_path_;
390}
391
392void DownloadManagerImpl::CreateDownloadItem(
[email protected]594e66fe2011-10-25 22:49:41393 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) {
[email protected]c2e76012010-12-23 21:10:29394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
395
[email protected]c09a8fd2011-11-21 19:54:50396 DownloadItem* download = new DownloadItemImpl(
[email protected]ae77da82011-11-01 19:17:29397 this, *info, new DownloadRequestHandle(request_handle),
398 browser_context_->IsOffTheRecord());
[email protected]2909e342011-10-29 00:46:53399 int32 download_id = info->download_id.local();
[email protected]4cd82f72011-05-23 19:15:01400 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]d8472d92011-08-26 20:15:20401
[email protected]a896ce32012-01-09 22:04:07402 CHECK_96627(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29403 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01404 active_downloads_[download_id] = download;
[email protected]c2e76012010-12-23 21:10:29405}
406
[email protected]fc03de22011-12-06 23:28:12407DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem(
408 const FilePath& main_file_path,
409 const GURL& page_url,
410 bool is_otr,
411 DownloadItem::Observer* observer) {
412 DownloadItem* download = new DownloadItemImpl(
413 this, main_file_path, page_url, is_otr, GetNextId());
414
415 download->AddObserver(observer);
416
417 DCHECK(!ContainsKey(save_page_downloads_, download->GetId()));
418 downloads_.insert(download);
419 save_page_downloads_[download->GetId()] = download;
420
421 // Will notify the observer in the callback.
422 delegate_->AddItemToPersistentStore(download);
423
424 return download;
425}
426
[email protected]795b76a2011-12-14 16:52:53427// For non-safe downloads with no prompting, |chosen_file| is the intermediate
428// path for saving the in-progress download. The final target filename for these
429// is |download->GetTargetName()|. For all other downloads (non-safe downloads
430// for which we have prompted for a save location, and all safe downloads),
431// |chosen_file| is the final target download path.
[email protected]5656f8a2011-11-17 16:12:58432void DownloadManagerImpl::ContinueDownloadWithPath(
433 DownloadItem* download, const FilePath& chosen_file) {
[email protected]ca4b5fa32010-10-09 12:42:18434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01435 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39436
[email protected]c09a8fd2011-11-21 19:54:50437 int32 download_id = download->GetId();
initial.commit09911bf2008-07-26 23:55:29438
[email protected]70850c72011-01-11 17:31:27439 // NOTE(ahendrickson) Eventually |active_downloads_| will replace
440 // |in_progress_|, but we don't want to change the semantics yet.
[email protected]4cd82f72011-05-23 19:15:01441 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]70850c72011-01-11 17:31:27442 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01443 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27444
[email protected]4cd82f72011-05-23 19:15:01445 // Make sure the initial file name is set only once.
[email protected]fdd2715c2011-12-09 22:24:20446 DCHECK(download->GetFullPath().empty());
[email protected]4cd82f72011-05-23 19:15:01447 download->OnPathDetermined(chosen_file);
[email protected]4cd82f72011-05-23 19:15:01448
449 VLOG(20) << __FUNCTION__ << "()"
450 << " download = " << download->DebugString(true);
451
452 in_progress_[download_id] = download;
[email protected]5f8589fe2011-08-17 20:58:39453 UpdateDownloadProgress(); // Reflect entry into in_progress_.
initial.commit09911bf2008-07-26 23:55:29454
[email protected]adb2f3d12011-01-23 16:24:54455 // Rename to intermediate name.
[email protected]f5920322011-03-24 20:34:16456 FilePath download_path;
[email protected]385e93182012-01-30 17:11:03457 if (download->GetDangerType() !=
458 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) {
459 if (download->PromptUserForSaveLocation()) {
460 // When we prompt the user, we overwrite the FullPath with what the user
461 // wanted to use. Construct a file path using the previously determined
462 // intermediate filename and the new path.
463 // TODO(asanka): This can trample an in-progress download in the new
464 // target directory if it was using the same intermediate name.
465 FilePath file_name = download->GetSuggestedPath().BaseName();
466 download_path = download->GetFullPath().DirName().Append(file_name);
467 } else {
468 // The download's name is already set to an intermediate name, so no need
469 // to override.
470 download_path = download->GetFullPath();
471 }
472 } else {
473 // The download is a safe download. We need to rename it to its
474 // intermediate path. The final name after user confirmation will be set
475 // from DownloadItem::OnDownloadCompleting.
476 download_path = delegate_->GetIntermediatePath(download->GetFullPath());
477 }
[email protected]594cd7d2010-07-21 03:23:56478
[email protected]f5920322011-03-24 20:34:16479 BrowserThread::PostTask(
480 BrowserThread::FILE, FROM_HERE,
[email protected]fabf36d22011-10-28 21:50:17481 base::Bind(&DownloadFileManager::RenameInProgressDownloadFile,
[email protected]fc03de22011-12-06 23:28:12482 file_manager_, download->GetGlobalId(),
483 download_path));
[email protected]f5920322011-03-24 20:34:16484
485 download->Rename(download_path);
486
[email protected]2588ea9d2011-08-22 20:59:53487 delegate_->AddItemToPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29488}
489
[email protected]443853c62011-12-22 19:22:41490void DownloadManagerImpl::UpdateDownload(int32 download_id,
491 int64 bytes_so_far,
492 int64 bytes_per_sec,
[email protected]0afff032012-01-06 20:55:00493 const std::string& hash_state) {
[email protected]70850c72011-01-11 17:31:27494 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
495 DownloadMap::iterator it = active_downloads_.find(download_id);
496 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29497 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26498 if (download->IsInProgress()) {
[email protected]443853c62011-12-22 19:22:41499 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state);
[email protected]5f8589fe2011-08-17 20:58:39500 UpdateDownloadProgress(); // Reflect size updates.
[email protected]2588ea9d2011-08-22 20:59:53501 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27502 }
initial.commit09911bf2008-07-26 23:55:29503 }
504}
505
[email protected]5656f8a2011-11-17 16:12:58506void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
507 int64 size,
508 const std::string& hash) {
[email protected]33c6d3f12011-09-04 00:00:54509 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]da6e3922010-11-24 21:45:50510 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
511 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35512 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32513
[email protected]c4f02c42011-01-24 21:55:06514 // If it's not in active_downloads_, that means it was cancelled; just
515 // ignore the notification.
516 if (active_downloads_.count(download_id) == 0)
517 return;
518
[email protected]adb2f3d12011-01-23 16:24:54519 DownloadItem* download = active_downloads_[download_id];
[email protected]ac4af82f2011-11-10 19:09:37520 download->OnAllDataSaved(size, hash);
[email protected]b09f1282011-09-14 00:37:45521
[email protected]fc03de22011-12-06 23:28:12522 download->MaybeCompleteDownload();
[email protected]adb2f3d12011-01-23 16:24:54523}
[email protected]9ccbb372008-10-10 18:50:32524
[email protected]fc03de22011-12-06 23:28:12525void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const {
[email protected]a896ce32012-01-09 22:04:07526 // TODO(rdsmith): Change to DCHECK after http://crbug.com/96627 resolved.
[email protected]c09a8fd2011-11-21 19:54:50527 if (download->GetState() == DownloadItem::REMOVING) {
[email protected]7d413112011-06-16 18:50:17528 CHECK(!ContainsKey(downloads_, download));
[email protected]c09a8fd2011-11-21 19:54:50529 CHECK(!ContainsKey(active_downloads_, download->GetId()));
530 CHECK(!ContainsKey(in_progress_, download->GetId()));
531 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17532 return;
533 }
534
535 // Should be in downloads_ if we're not REMOVING.
536 CHECK(ContainsKey(downloads_, download));
537
538 // Check history_downloads_ consistency.
[email protected]c09a8fd2011-11-21 19:54:50539 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) {
540 CHECK(ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17541 } else {
[email protected]fc03de22011-12-06 23:28:12542 for (DownloadMap::const_iterator it = history_downloads_.begin();
[email protected]7d413112011-06-16 18:50:17543 it != history_downloads_.end(); ++it) {
[email protected]a896ce32012-01-09 22:04:07544 CHECK_96627(it->second != download);
[email protected]7d413112011-06-16 18:50:17545 }
546 }
547
[email protected]c09a8fd2011-11-21 19:54:50548 int64 state = download->GetState();
[email protected]61b75a52011-08-29 16:34:34549 base::debug::Alias(&state);
[email protected]c09a8fd2011-11-21 19:54:50550 if (ContainsKey(active_downloads_, download->GetId())) {
551 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle)
552 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
553 if (DownloadItem::IN_PROGRESS != download->GetState())
554 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle());
[email protected]f9a2997f2011-09-23 16:54:07555 }
[email protected]c09a8fd2011-11-21 19:54:50556 if (DownloadItem::IN_PROGRESS == download->GetState())
557 CHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]5cd11b6e2011-06-10 20:30:59558}
559
[email protected]5656f8a2011-11-17 16:12:58560bool DownloadManagerImpl::IsDownloadReadyForCompletion(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54561 // If we don't have all the data, the download is not ready for
562 // completion.
[email protected]c09a8fd2011-11-21 19:54:50563 if (!download->AllDataSaved())
[email protected]adb2f3d12011-01-23 16:24:54564 return false;
[email protected]6a7fb042010-02-01 16:30:47565
[email protected]9d7ef802011-02-25 19:03:35566 // If the download is dangerous, but not yet validated, it's not ready for
567 // completion.
[email protected]c09a8fd2011-11-21 19:54:50568 if (download->GetSafetyState() == DownloadItem::DANGEROUS)
[email protected]9d7ef802011-02-25 19:03:35569 return false;
570
[email protected]adb2f3d12011-01-23 16:24:54571 // If the download isn't active (e.g. has been cancelled) it's not
572 // ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50573 if (active_downloads_.count(download->GetId()) == 0)
[email protected]adb2f3d12011-01-23 16:24:54574 return false;
575
576 // If the download hasn't been inserted into the history system
577 // (which occurs strictly after file name determination, intermediate
578 // file rename, and UI display) then it's not ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50579 if (download->GetDbHandle() == DownloadItem::kUninitializedHandle)
[email protected]7054b592011-06-22 14:46:42580 return false;
581
582 return true;
[email protected]adb2f3d12011-01-23 16:24:54583}
584
[email protected]5656f8a2011-11-17 16:12:58585void DownloadManagerImpl::MaybeCompleteDownload(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54586 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
587 VLOG(20) << __FUNCTION__ << "()" << " download = "
588 << download->DebugString(false);
589
590 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32591 return;
[email protected]9ccbb372008-10-10 18:50:32592
[email protected]adb2f3d12011-01-23 16:24:54593 // TODO(rdsmith): DCHECK that we only pass through this point
594 // once per download. The natural way to do this is by a state
595 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56596
[email protected]adb2f3d12011-01-23 16:24:54597 // Confirm we're in the proper set of states to be here;
[email protected]9d7ef802011-02-25 19:03:35598 // in in_progress_, have all data, have a history handle, (validated or safe).
[email protected]c09a8fd2011-11-21 19:54:50599 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState());
600 DCHECK_EQ(1u, in_progress_.count(download->GetId()));
601 DCHECK(download->AllDataSaved());
602 DCHECK(download->GetDbHandle() != DownloadItem::kUninitializedHandle);
603 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle()));
[email protected]adb2f3d12011-01-23 16:24:54604
[email protected]c2918652011-11-01 18:50:23605 // Give the delegate a chance to override.
606 if (!delegate_->ShouldCompleteDownload(download))
607 return;
608
[email protected]adb2f3d12011-01-23 16:24:54609 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
610 << download->DebugString(false);
611
612 // Remove the id from in_progress
[email protected]c09a8fd2011-11-21 19:54:50613 in_progress_.erase(download->GetId());
[email protected]5f8589fe2011-08-17 20:58:39614 UpdateDownloadProgress(); // Reflect removal from in_progress_.
[email protected]adb2f3d12011-01-23 16:24:54615
[email protected]2588ea9d2011-08-22 20:59:53616 delegate_->UpdateItemInPersistentStore(download);
[email protected]adb2f3d12011-01-23 16:24:54617
[email protected]f5920322011-03-24 20:34:16618 // Finish the download.
[email protected]48837962011-04-19 17:03:29619 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32620}
621
[email protected]fc03de22011-12-06 23:28:12622void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) {
[email protected]70850c72011-01-11 17:31:27623 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21624 DCHECK(download);
[email protected]2588ea9d2011-08-22 20:59:53625 delegate_->UpdateItemInPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12626 active_downloads_.erase(download->GetId());
627 AssertStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27628}
629
[email protected]5656f8a2011-11-17 16:12:58630void DownloadManagerImpl::OnDownloadRenamedToFinalName(
631 int download_id,
632 const FilePath& full_path,
633 int uniquifier) {
[email protected]da6e3922010-11-24 21:45:50634 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
[email protected]f5920322011-03-24 20:34:16635 << " full_path = \"" << full_path.value() << "\""
636 << " uniquifier = " << uniquifier;
[email protected]ca4b5fa32010-10-09 12:42:18637 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f5920322011-03-24 20:34:16638
[email protected]2e030682010-07-23 19:45:36639 DownloadItem* item = GetDownloadItem(download_id);
640 if (!item)
641 return;
[email protected]6cade212008-12-03 00:32:22642
[email protected]a62d42902012-01-24 17:24:38643 if (item->GetDangerType() == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS ||
[email protected]795b76a2011-12-14 16:52:53644 item->PromptUserForSaveLocation()) {
645 DCHECK_EQ(0, uniquifier)
646 << "We should not uniquify user supplied filenames or safe filenames "
647 "that have already been uniquified.";
[email protected]8fa1eeb52011-04-13 14:18:02648 }
649
[email protected]fabf36d22011-10-28 21:50:17650 BrowserThread::PostTask(
651 BrowserThread::FILE, FROM_HERE,
652 base::Bind(&DownloadFileManager::CompleteDownload,
[email protected]c09a8fd2011-11-21 19:54:50653 file_manager_, item->GetGlobalId()));
[email protected]9ccbb372008-10-10 18:50:32654
[email protected]f5920322011-03-24 20:34:16655 if (uniquifier)
[email protected]c09a8fd2011-11-21 19:54:50656 item->SetPathUniquifier(uniquifier);
[email protected]9ccbb372008-10-10 18:50:32657
[email protected]f5920322011-03-24 20:34:16658 item->OnDownloadRenamedToFinalName(full_path);
[email protected]2588ea9d2011-08-22 20:59:53659 delegate_->UpdatePathForItemInPersistentStore(item, full_path);
initial.commit09911bf2008-07-26 23:55:29660}
661
[email protected]5656f8a2011-11-17 16:12:58662void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]93af2272011-09-21 18:29:17663 DownloadItem* download = GetActiveDownload(download_id);
664 // A cancel at the right time could remove the download from the
665 // |active_downloads_| map before we get here.
666 if (!download)
667 return;
668
669 download->Cancel(true);
670}
671
[email protected]fc03de22011-12-06 23:28:12672void DownloadManagerImpl::DownloadCancelled(DownloadItem* download) {
[email protected]d8472d92011-08-26 20:15:20673 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20674
675 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50676 << " download = " << download->DebugString(true);
677
[email protected]93af2272011-09-21 18:29:17678 RemoveFromActiveList(download);
[email protected]47a881b2011-08-29 22:59:21679 // This function is called from the DownloadItem, so DI state
680 // should already have been updated.
[email protected]fc03de22011-12-06 23:28:12681 AssertStateConsistent(download);
initial.commit09911bf2008-07-26 23:55:29682
[email protected]15d90ba2011-11-03 03:41:55683 if (file_manager_)
684 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29685}
686
[email protected]5656f8a2011-11-17 16:12:58687void DownloadManagerImpl::OnDownloadInterrupted(int32 download_id,
688 int64 size,
[email protected]0afff032012-01-06 20:55:00689 const std::string& hash_state,
[email protected]5656f8a2011-11-17 16:12:58690 InterruptReason reason) {
[email protected]47a881b2011-08-29 22:59:21691 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
692
693 DownloadItem* download = GetActiveDownload(download_id);
694 if (!download)
695 return;
696
[email protected]be76b7e2011-10-13 12:57:57697 VLOG(20) << __FUNCTION__ << "()"
698 << " reason " << InterruptReasonDebugString(reason)
[email protected]c09a8fd2011-11-21 19:54:50699 << " at offset " << download->GetReceivedBytes()
[email protected]47a881b2011-08-29 22:59:21700 << " size = " << size
701 << " download = " << download->DebugString(true);
702
[email protected]93af2272011-09-21 18:29:17703 RemoveFromActiveList(download);
[email protected]443853c62011-12-22 19:22:41704 download->Interrupted(size, hash_state, reason);
[email protected]93af2272011-09-21 18:29:17705 download->OffThreadCancel(file_manager_);
[email protected]47a881b2011-08-29 22:59:21706}
707
[email protected]5656f8a2011-11-17 16:12:58708DownloadItem* DownloadManagerImpl::GetActiveDownload(int32 download_id) {
[email protected]bf68a00b2011-04-07 17:28:26709 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
710 DownloadMap::iterator it = active_downloads_.find(download_id);
[email protected]bf68a00b2011-04-07 17:28:26711 if (it == active_downloads_.end())
[email protected]47a881b2011-08-29 22:59:21712 return NULL;
[email protected]bf68a00b2011-04-07 17:28:26713
714 DownloadItem* download = it->second;
715
[email protected]47a881b2011-08-29 22:59:21716 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50717 DCHECK_EQ(download_id, download->GetId());
[email protected]4cd82f72011-05-23 19:15:01718
[email protected]47a881b2011-08-29 22:59:21719 return download;
720}
[email protected]54610672011-07-18 18:24:43721
[email protected]5656f8a2011-11-17 16:12:58722void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) {
[email protected]93af2272011-09-21 18:29:17723 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
724 DCHECK(download);
725
726 // Clean up will happen when the history system create callback runs if we
727 // don't have a valid db_handle yet.
[email protected]c09a8fd2011-11-21 19:54:50728 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) {
729 in_progress_.erase(download->GetId());
730 active_downloads_.erase(download->GetId());
[email protected]93af2272011-09-21 18:29:17731 UpdateDownloadProgress(); // Reflect removal from in_progress_.
732 delegate_->UpdateItemInPersistentStore(download);
733 }
734}
735
[email protected]fd3a82832012-01-19 20:35:12736bool DownloadManagerImpl::GenerateFileHash() {
737 return delegate_->GenerateFileHash();
738}
739
[email protected]5656f8a2011-11-17 16:12:58740content::DownloadManagerDelegate* DownloadManagerImpl::delegate() const {
741 return delegate_;
742}
743
744void DownloadManagerImpl::SetDownloadManagerDelegate(
[email protected]1bd0ef12011-10-20 05:24:17745 content::DownloadManagerDelegate* delegate) {
[email protected]9bb54ee2011-10-12 17:43:35746 delegate_ = delegate;
747}
748
[email protected]5656f8a2011-11-17 16:12:58749void DownloadManagerImpl::UpdateDownloadProgress() {
[email protected]5f8589fe2011-08-17 20:58:39750 delegate_->DownloadProgressUpdated();
[email protected]6a7fb042010-02-01 16:30:47751}
752
[email protected]5656f8a2011-11-17 16:12:58753int DownloadManagerImpl::RemoveDownloadItems(
[email protected]6d0146c2011-08-04 19:13:04754 const DownloadVector& pending_deletes) {
755 if (pending_deletes.empty())
756 return 0;
757
758 // Delete from internal maps.
759 for (DownloadVector::const_iterator it = pending_deletes.begin();
760 it != pending_deletes.end();
761 ++it) {
762 DownloadItem* download = *it;
763 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50764 history_downloads_.erase(download->GetDbHandle());
765 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:04766 downloads_.erase(download);
767 }
768
769 // Tell observers to refresh their views.
770 NotifyModelChanged();
771
772 // Delete the download items themselves.
773 const int num_deleted = static_cast<int>(pending_deletes.size());
774 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
775 return num_deleted;
776}
777
[email protected]fc03de22011-12-06 23:28:12778void DownloadManagerImpl::DownloadRemoved(DownloadItem* download) {
779 if (history_downloads_.find(download->GetDbHandle()) ==
780 history_downloads_.end())
[email protected]93af2272011-09-21 18:29:17781 return;
782
783 // Make history update.
[email protected]93af2272011-09-21 18:29:17784 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29785
786 // Remove from our tables and delete.
[email protected]6d0146c2011-08-04 19:13:04787 int downloads_count = RemoveDownloadItems(DownloadVector(1, download));
[email protected]f04182f32010-12-10 19:12:07788 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29789}
790
[email protected]fd3a82832012-01-19 20:35:12791int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
792 base::Time remove_end) {
[email protected]2588ea9d2011-08-22 20:59:53793 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29794
[email protected]a312a442010-12-15 23:40:33795 // All downloads visible to the user will be in the history,
796 // so scan that map.
[email protected]6d0146c2011-08-04 19:13:04797 DownloadVector pending_deletes;
798 for (DownloadMap::const_iterator it = history_downloads_.begin();
799 it != history_downloads_.end();
800 ++it) {
initial.commit09911bf2008-07-26 23:55:29801 DownloadItem* download = it->second;
[email protected]c09a8fd2011-11-21 19:54:50802 if (download->GetStartTime() >= remove_begin &&
803 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04804 (download->IsComplete() || download->IsCancelled())) {
[email protected]fc03de22011-12-06 23:28:12805 AssertStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28806 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29807 }
initial.commit09911bf2008-07-26 23:55:29808 }
[email protected]6d0146c2011-08-04 19:13:04809 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29810}
811
[email protected]fd3a82832012-01-19 20:35:12812int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59813 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29814}
815
[email protected]5656f8a2011-11-17 16:12:58816int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]da4a5582011-10-17 19:08:06817 download_stats::RecordClearAllSize(history_downloads_.size());
[email protected]d41355e6f2009-04-07 21:21:12818 // The null times make the date range unbounded.
819 return RemoveDownloadsBetween(base::Time(), base::Time());
820}
821
initial.commit09911bf2008-07-26 23:55:29822// Initiate a download of a specific URL. We send the request to the
823// ResourceDispatcherHost, and let it send us responses like a regular
824// download.
[email protected]0d4e30c2012-01-28 00:47:53825void DownloadManagerImpl::DownloadUrl(
826 const GURL& url,
827 const GURL& referrer,
828 const std::string& referrer_charset,
829 bool prefer_cache,
[email protected]27678b2a2012-02-04 22:09:14830 int64 post_id,
[email protected]0d4e30c2012-01-28 00:47:53831 const DownloadSaveInfo& save_info,
832 WebContents* web_contents) {
[email protected]c79a0c02011-08-22 22:37:37833 ResourceDispatcherHost* resource_dispatcher_host =
[email protected]99907362012-01-11 05:41:40834 ResourceDispatcherHost::Get();
[email protected]443853c62011-12-22 19:22:41835
[email protected]ed24fad2011-05-10 22:44:01836 // We send a pointer to content::ResourceContext, instead of the usual
837 // reference, so that a copy of the object isn't made.
[email protected]fabf36d22011-10-28 21:50:17838 // base::Bind can't handle 7 args, so we use URLParams and RenderParams.
839 BrowserThread::PostTask(
840 BrowserThread::IO, FROM_HERE,
[email protected]0d4e30c2012-01-28 00:47:53841 base::Bind(
842 &BeginDownload,
[email protected]27678b2a2012-02-04 22:09:14843 URLParams(url, referrer, post_id),
[email protected]0d4e30c2012-01-28 00:47:53844 prefer_cache,
845 save_info,
846 resource_dispatcher_host,
[email protected]fbc5e5f92012-01-02 06:08:32847 RenderParams(web_contents->GetRenderProcessHost()->GetID(),
848 web_contents->GetRenderViewHost()->routing_id()),
849 &web_contents->GetBrowserContext()->GetResourceContext()));
initial.commit09911bf2008-07-26 23:55:29850}
851
[email protected]5656f8a2011-11-17 16:12:58852void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29853 observers_.AddObserver(observer);
[email protected]75e51b52012-02-04 16:57:54854 observer->ModelChanged(this);
initial.commit09911bf2008-07-26 23:55:29855}
856
[email protected]5656f8a2011-11-17 16:12:58857void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29858 observers_.RemoveObserver(observer);
859}
860
[email protected]5656f8a2011-11-17 16:12:58861void DownloadManagerImpl::FileSelected(const FilePath& path, void* params) {
[email protected]4cd82f72011-05-23 19:15:01862 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
863
864 int32* id_ptr = reinterpret_cast<int32*>(params);
865 DCHECK(id_ptr != NULL);
866 int32 download_id = *id_ptr;
867 delete id_ptr;
868
869 DownloadItem* download = GetActiveDownloadItem(download_id);
870 if (!download)
871 return;
872 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
873 << " download = " << download->DebugString(true);
874
[email protected]c09a8fd2011-11-21 19:54:50875 if (download->PromptUserForSaveLocation())
[email protected]7ae7c2cb2009-01-06 23:31:41876 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35877
[email protected]4cd82f72011-05-23 19:15:01878 // Make sure the initial file name is set only once.
879 ContinueDownloadWithPath(download, path);
initial.commit09911bf2008-07-26 23:55:29880}
881
[email protected]5656f8a2011-11-17 16:12:58882void DownloadManagerImpl::FileSelectionCanceled(void* params) {
initial.commit09911bf2008-07-26 23:55:29883 // The user didn't pick a place to save the file, so need to cancel the
884 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01885 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
886 int32* id_ptr = reinterpret_cast<int32*>(params);
887 DCHECK(id_ptr != NULL);
888 int32 download_id = *id_ptr;
889 delete id_ptr;
890
891 DownloadItem* download = GetActiveDownloadItem(download_id);
892 if (!download)
893 return;
894
895 VLOG(20) << __FUNCTION__ << "()"
896 << " download = " << download->DebugString(true);
897
[email protected]93af2272011-09-21 18:29:17898 // TODO(ahendrickson) -- This currently has no effect, as the download is
899 // not put on the active list until the file selection is complete. Need
900 // to put it on the active list earlier in the process.
901 RemoveFromActiveList(download);
902
903 download->OffThreadCancel(file_manager_);
[email protected]4cd82f72011-05-23 19:15:01904}
905
initial.commit09911bf2008-07-26 23:55:29906// Operations posted to us from the history service ----------------------------
907
908// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25909// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]5656f8a2011-11-17 16:12:58910void DownloadManagerImpl::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25911 std::vector<DownloadPersistentStoreInfo>* entries) {
[email protected]d8472d92011-08-26 20:15:20912 // TODO(rdsmith): Remove this and related logic when
[email protected]a896ce32012-01-09 22:04:07913 // http://crbug.com/96627 is fixed.
[email protected]d8472d92011-08-26 20:15:20914 largest_db_handle_in_history_ = 0;
915
initial.commit09911bf2008-07-26 23:55:29916 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]fc03de22011-12-06 23:28:12917 DownloadItem* download = new DownloadItemImpl(
918 this, GetNextId(), entries->at(i));
[email protected]a896ce32012-01-09 22:04:07919 CHECK_96627(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]f04182f32010-12-10 19:12:07920 downloads_.insert(download);
[email protected]c09a8fd2011-11-21 19:54:50921 history_downloads_[download->GetDbHandle()] = download;
[email protected]da6e3922010-11-24 21:45:50922 VLOG(20) << __FUNCTION__ << "()" << i << ">"
923 << " download = " << download->DebugString(true);
[email protected]d8472d92011-08-26 20:15:20924
[email protected]c09a8fd2011-11-21 19:54:50925 if (download->GetDbHandle() > largest_db_handle_in_history_)
926 largest_db_handle_in_history_ = download->GetDbHandle();
initial.commit09911bf2008-07-26 23:55:29927 }
[email protected]b0ab1d42010-02-24 19:29:28928 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48929 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29930}
931
[email protected]5656f8a2011-11-17 16:12:58932void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItem* download,
933 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27934 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d2a8fb72010-01-21 05:31:42935
[email protected]a896ce32012-01-09 22:04:07936 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/96627
[email protected]1e9fe7ff2011-06-24 17:37:33937 // is fixed.
[email protected]2588ea9d2011-08-22 20:59:53938 CHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]1e9fe7ff2011-06-24 17:37:33939
[email protected]da4a5582011-10-17 19:08:06940 download_stats::RecordHistorySize(history_downloads_.size());
941
[email protected]c09a8fd2011-11-21 19:54:50942 DCHECK(download->GetDbHandle() == DownloadItem::kUninitializedHandle);
943 download->SetDbHandle(db_handle);
[email protected]5bcd73eb2011-03-23 21:14:02944
[email protected]a896ce32012-01-09 22:04:07945 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/96627
[email protected]d8472d92011-08-26 20:15:20946 // is fixed.
[email protected]a896ce32012-01-09 22:04:07947 CHECK_96627(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]c09a8fd2011-11-21 19:54:50948 history_downloads_[download->GetDbHandle()] = download;
[email protected]6d0146c2011-08-04 19:13:04949
950 // Show in the appropriate browser UI.
951 // This includes buttons to save or cancel, for a dangerous download.
952 ShowDownloadInBrowser(download);
953
954 // Inform interested objects about the new download.
955 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19956}
957
[email protected]2588ea9d2011-08-22 20:59:53958
[email protected]5656f8a2011-11-17 16:12:58959void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id,
960 int64 db_handle) {
[email protected]2588ea9d2011-08-22 20:59:53961 if (save_page_downloads_.count(download_id)) {
962 OnSavePageItemAddedToPersistentStore(download_id, db_handle);
963 } else if (active_downloads_.count(download_id)) {
964 OnDownloadItemAddedToPersistentStore(download_id, db_handle);
965 }
966 // It's valid that we don't find a matching item, i.e. on shutdown.
967}
968
[email protected]f9a45b02011-06-30 23:49:19969// Once the new DownloadItem's creation info has been committed to the history
970// service, we associate the DownloadItem with the db handle, update our
971// 'history_downloads_' map and inform observers.
[email protected]5656f8a2011-11-17 16:12:58972void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore(
973 int32 download_id, int64 db_handle) {
[email protected]f9a45b02011-06-30 23:49:19974 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]19420cc2011-07-18 17:43:45975 DownloadItem* download = GetActiveDownloadItem(download_id);
[email protected]93af2272011-09-21 18:29:17976 if (!download)
[email protected]19420cc2011-07-18 17:43:45977 return;
[email protected]54610672011-07-18 18:24:43978
979 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
980 << " download_id = " << download_id
981 << " download = " << download->DebugString(true);
[email protected]f9a45b02011-06-30 23:49:19982
[email protected]a896ce32012-01-09 22:04:07983 // TODO(rdsmith): Remove after http://crbug.com/96627 resolved.
[email protected]d8472d92011-08-26 20:15:20984 int64 largest_handle = largest_db_handle_in_history_;
985 base::debug::Alias(&largest_handle);
[email protected]e5107ce2011-09-19 20:36:13986 int32 matching_item_download_id
987 = (ContainsKey(history_downloads_, db_handle) ?
[email protected]c09a8fd2011-11-21 19:54:50988 history_downloads_[db_handle]->GetId() : 0);
[email protected]e5107ce2011-09-19 20:36:13989 base::debug::Alias(&matching_item_download_id);
990
[email protected]a896ce32012-01-09 22:04:07991 CHECK_96627(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:20992
[email protected]f9a45b02011-06-30 23:49:19993 AddDownloadItemToHistory(download, db_handle);
initial.commit09911bf2008-07-26 23:55:29994
[email protected]93af2272011-09-21 18:29:17995 // If the download is still in progress, try to complete it.
996 //
997 // Otherwise, download has been cancelled or interrupted before we've
998 // received the DB handle. We post one final message to the history
999 // service so that it can be properly in sync with the DownloadItem's
1000 // completion status, and also inform any observers so that they get
1001 // more than just the start notification.
1002 if (download->IsInProgress()) {
1003 MaybeCompleteDownload(download);
1004 } else {
[email protected]a896ce32012-01-09 22:04:071005 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/96627
[email protected]93af2272011-09-21 18:29:171006 // is fixed.
1007 CHECK(download->IsCancelled())
1008 << " download = " << download->DebugString(true);
1009 in_progress_.erase(download_id);
1010 active_downloads_.erase(download_id);
1011 delegate_->UpdateItemInPersistentStore(download);
1012 download->UpdateObservers();
1013 }
initial.commit09911bf2008-07-26 23:55:291014}
1015
[email protected]5656f8a2011-11-17 16:12:581016void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]8ddbd66a2010-05-21 16:38:341017 // The 'contents' may no longer exist if the user closed the tab before we
[email protected]99cb7f82011-07-28 17:27:261018 // get this start completion event.
[email protected]a62d42902012-01-24 17:24:381019 WebContents* content = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:261020
1021 // If the contents no longer exists, we ask the embedder to suggest another
1022 // tab.
[email protected]da1a27b2011-07-29 23:16:331023 if (!content)
[email protected]ef9572e2012-01-04 22:14:121024 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:531025
[email protected]0bfbf882011-12-22 18:19:271026 if (content && content->GetDelegate())
1027 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:531028}
1029
[email protected]5656f8a2011-11-17 16:12:581030int DownloadManagerImpl::InProgressCount() const {
1031 return static_cast<int>(in_progress_.size());
1032}
1033
[email protected]6cade212008-12-03 00:32:221034// Clears the last download path, used to initialize "save as" dialogs.
[email protected]5656f8a2011-11-17 16:12:581035void DownloadManagerImpl::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:411036 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:381037}
[email protected]b0ab1d42010-02-24 19:29:281038
[email protected]5656f8a2011-11-17 16:12:581039void DownloadManagerImpl::NotifyModelChanged() {
[email protected]75e51b52012-02-04 16:57:541040 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this));
[email protected]b0ab1d42010-02-24 19:29:281041}
1042
[email protected]5656f8a2011-11-17 16:12:581043DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) {
[email protected]4cd82f72011-05-23 19:15:011044 // The |history_downloads_| map is indexed by the download's db_handle,
1045 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:071046 for (DownloadMap::iterator it = history_downloads_.begin();
1047 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:361048 DownloadItem* item = it->second;
[email protected]c09a8fd2011-11-21 19:54:501049 if (item->GetId() == download_id)
[email protected]2e030682010-07-23 19:45:361050 return item;
1051 }
1052 return NULL;
1053}
1054
[email protected]5656f8a2011-11-17 16:12:581055DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) {
[email protected]5d3e83642011-12-16 01:14:361056 if (ContainsKey(active_downloads_, download_id))
1057 return active_downloads_[download_id];
1058 return NULL;
[email protected]4cd82f72011-05-23 19:15:011059}
1060
[email protected]57fd1252010-12-23 17:24:091061// Confirm that everything in all maps is also in |downloads_|, and that
1062// everything in |downloads_| is also in some other map.
[email protected]5656f8a2011-11-17 16:12:581063void DownloadManagerImpl::AssertContainersConsistent() const {
[email protected]f04182f32010-12-10 19:12:071064#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:091065 // Turn everything into sets.
[email protected]6d0146c2011-08-04 19:13:041066 const DownloadMap* input_maps[] = {&active_downloads_,
1067 &history_downloads_,
1068 &save_page_downloads_};
1069 DownloadSet active_set, history_set, save_page_set;
1070 DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set};
1071 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:091072 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
1073 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:041074 it != input_maps[i]->end(); ++it) {
1075 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:071076 }
1077 }
[email protected]57fd1252010-12-23 17:24:091078
1079 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:091080 DownloadSet downloads_union;
1081 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1082 DownloadSet remainder;
1083 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1084 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
1085 downloads_.begin(), downloads_.end(),
1086 insert_it);
1087 DCHECK(remainder.empty());
1088 std::insert_iterator<DownloadSet>
1089 insert_union(downloads_union, downloads_union.end());
1090 std::set_union(downloads_union.begin(), downloads_union.end(),
1091 all_sets[i]->begin(), all_sets[i]->end(),
1092 insert_union);
1093 }
1094
1095 // Is everything in downloads_ present in one of the other sets?
1096 DownloadSet remainder;
1097 std::insert_iterator<DownloadSet>
1098 insert_remainder(remainder, remainder.begin());
1099 std::set_difference(downloads_.begin(), downloads_.end(),
1100 downloads_union.begin(), downloads_union.end(),
1101 insert_remainder);
1102 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:071103#endif
1104}
1105
[email protected]6d0146c2011-08-04 19:13:041106// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:531107// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:041108// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:531109// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
1110// that the history event is update regardless of the order in which these two
1111// events complete.
[email protected]6d0146c2011-08-04 19:13:041112// If something removes the download item from the download manager (Remove,
1113// Shutdown) the result will be that the SavePage system will not be able to
1114// properly update the download item (which no longer exists) or the download
1115// history, but the action will complete properly anyway. This may lead to the
1116// history entry being wrong on a reload of chrome (specifically in the case of
1117// Initiation -> History Callback -> Removal -> Completion), but there's no way
1118// to solve that without canceling on Remove (which would then update the DB).
1119
[email protected]5656f8a2011-11-17 16:12:581120void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore(
1121 int32 download_id, int64 db_handle) {
[email protected]6d0146c2011-08-04 19:13:041122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1123
1124 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
1125 // This can happen if the download manager is shutting down and all maps
1126 // have been cleared.
1127 if (it == save_page_downloads_.end())
1128 return;
1129
1130 DownloadItem* download = it->second;
1131 if (!download) {
1132 NOTREACHED();
1133 return;
1134 }
1135
[email protected]a896ce32012-01-09 22:04:071136 // TODO(rdsmith): Remove after http://crbug.com/96627 resolved.
[email protected]d8472d92011-08-26 20:15:201137 int64 largest_handle = largest_db_handle_in_history_;
1138 base::debug::Alias(&largest_handle);
[email protected]a896ce32012-01-09 22:04:071139 CHECK_96627(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:201140
[email protected]6d0146c2011-08-04 19:13:041141 AddDownloadItemToHistory(download, db_handle);
1142
1143 // Finalize this download if it finished before the history callback.
1144 if (!download->IsInProgress())
1145 SavePageDownloadFinished(download);
1146}
1147
[email protected]5656f8a2011-11-17 16:12:581148void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) {
[email protected]c09a8fd2011-11-21 19:54:501149 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) {
[email protected]2588ea9d2011-08-22 20:59:531150 delegate_->UpdateItemInPersistentStore(download);
[email protected]c09a8fd2011-11-21 19:54:501151 DCHECK(ContainsKey(save_page_downloads_, download->GetId()));
1152 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:041153
1154 if (download->IsComplete())
[email protected]ad50def52011-10-19 23:17:071155 content::NotificationService::current()->Notify(
[email protected]6d0146c2011-08-04 19:13:041156 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
[email protected]6c2381d2011-10-19 02:52:531157 content::Source<DownloadManager>(this),
1158 content::Details<DownloadItem>(download));
[email protected]6d0146c2011-08-04 19:13:041159 }
1160}
[email protected]da4a5582011-10-17 19:08:061161
[email protected]fc03de22011-12-06 23:28:121162void DownloadManagerImpl::DownloadOpened(DownloadItem* download) {
[email protected]da4a5582011-10-17 19:08:061163 delegate_->UpdateItemInPersistentStore(download);
1164 int num_unopened = 0;
1165 for (DownloadMap::iterator it = history_downloads_.begin();
1166 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:501167 if (it->second->IsComplete() && !it->second->GetOpened())
[email protected]da4a5582011-10-17 19:08:061168 ++num_unopened;
1169 }
1170 download_stats::RecordOpensOutstanding(num_unopened);
1171}
[email protected]5656f8a2011-11-17 16:12:581172
1173void DownloadManagerImpl::SetFileManager(DownloadFileManager* file_manager) {
1174 file_manager_ = file_manager;
1175}