blob: 1487b19809c909771a7057a633597a45ee7bfa8f [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]2909e342011-10-29 00:46:5322#include "content/browser/download/download_id_factory.h"
[email protected]c09a8fd2011-11-21 19:54:5023#include "content/browser/download/download_item_impl.h"
[email protected]bb1a4212011-08-22 22:38:2524#include "content/browser/download/download_persistent_store_info.h"
[email protected]da4a5582011-10-17 19:08:0625#include "content/browser/download/download_stats.h"
[email protected]71bf3f5e2011-08-15 21:05:2226#include "content/browser/download/download_status_updater.h"
[email protected]be76b7e2011-10-13 12:57:5727#include "content/browser/download/interrupt_reasons.h"
[email protected]7324d1d2011-03-01 05:02:1628#include "content/browser/renderer_host/render_view_host.h"
29#include "content/browser/renderer_host/resource_dispatcher_host.h"
30#include "content/browser/tab_contents/tab_contents.h"
[email protected]ccb797302011-12-15 16:55:1131#include "content/public/browser/browser_context.h"
[email protected]c38831a12011-10-28 12:44:4932#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:4433#include "content/public/browser/content_browser_client.h"
[email protected]1bd0ef12011-10-20 05:24:1734#include "content/public/browser/download_manager_delegate.h"
[email protected]ad50def52011-10-19 23:17:0735#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1636#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3037#include "content/public/browser/render_process_host.h"
[email protected]0bfbf882011-12-22 18:19:2738#include "content/public/browser/web_contents_delegate.h"
initial.commit09911bf2008-07-26 23:55:2939
[email protected]a896ce32012-01-09 22:04:0740// TODO(benjhayden): Change this to DCHECK when we have more debugging
41// information from the next dev cycle, before the next stable/beta branch is
42// cut, in order to prevent unnecessary crashes on those channels. If we still
43// don't have root cause before the dev cycle after the next stable/beta
44// releases, uncomment it out to re-enable debugging checks. Whenever this macro
45// is toggled, the corresponding macro in download_database.cc should also
46// be toggled. When 96627 is fixed, this macro and all its usages can be
47// deleted or permanently changed to DCHECK as appropriate.
48#define CHECK_96627 CHECK
49
[email protected]631bb742011-11-02 11:29:3950using content::BrowserThread;
[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 {
58 URLParams(const GURL& url, const GURL& referrer)
59 : url_(url), referrer_(referrer) {}
60 GURL url_;
61 GURL referrer_;
62};
63
64struct RenderParams {
65 RenderParams(int rpi, int rvi)
66 : render_process_id_(rpi), render_view_id_(rvi) {}
67 int render_process_id_;
68 int render_view_id_;
69};
70
71void BeginDownload(const URLParams& url_params,
72 const DownloadSaveInfo& save_info,
73 ResourceDispatcherHost* resource_dispatcher_host,
74 const RenderParams& render_params,
75 const content::ResourceContext* context) {
76 net::URLRequest* request = new net::URLRequest(url_params.url_,
77 resource_dispatcher_host);
78 request->set_referrer(url_params.referrer_.spec());
[email protected]c79a0c02011-08-22 22:37:3779 resource_dispatcher_host->BeginDownload(
[email protected]fabf36d22011-10-28 21:50:1780 request, save_info, true,
[email protected]8e3ae68c2011-09-16 22:15:4781 DownloadResourceHandler::OnStartedCallback(),
[email protected]fabf36d22011-10-28 21:50:1782 render_params.render_process_id_,
83 render_params.render_view_id_,
[email protected]c79a0c02011-08-22 22:37:3784 *context);
[email protected]a0ce3282011-08-19 20:49:5285}
86
87} // namespace
88
[email protected]5656f8a2011-11-17 16:12:5889DownloadManagerImpl::DownloadManagerImpl(
90 content::DownloadManagerDelegate* delegate,
91 DownloadIdFactory* id_factory,
92 DownloadStatusUpdater* status_updater)
93 : shutdown_needed_(false),
94 browser_context_(NULL),
95 file_manager_(NULL),
96 status_updater_((status_updater != NULL)
97 ? status_updater->AsWeakPtr()
98 : base::WeakPtr<DownloadStatusUpdater>()),
99 delegate_(delegate),
100 id_factory_(id_factory),
101 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) {
[email protected]eda58402011-09-21 19:32:02102 // NOTE(benjhayden): status_updater may be NULL when using
103 // TestingBrowserProcess.
104 if (status_updater_.get() != NULL)
[email protected]073ed7b2010-09-27 09:20:02105 status_updater_->AddDelegate(this);
initial.commit09911bf2008-07-26 23:55:29106}
107
[email protected]5656f8a2011-11-17 16:12:58108DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13109 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29110}
111
[email protected]5656f8a2011-11-17 16:12:58112DownloadId DownloadManagerImpl::GetNextId() {
[email protected]2909e342011-10-29 00:46:53113 return id_factory_->GetNextId();
114}
115
[email protected]fc03de22011-12-06 23:28:12116bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) {
117 return delegate_->ShouldOpenDownload(item);
118}
119
120bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
121 return delegate_->ShouldOpenFileBasedOnExtension(path);
122}
123
[email protected]5656f8a2011-11-17 16:12:58124void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50125 VLOG(20) << __FUNCTION__ << "()"
126 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13127 if (!shutdown_needed_)
128 return;
129 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29130
[email protected]326a6a92010-09-10 20:21:13131 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
[email protected]fb4f8d902011-09-16 06:07:08132 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13133
134 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:18135 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]fabf36d22011-10-28 21:50:17136 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
137 file_manager_, make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:13138 }
initial.commit09911bf2008-07-26 23:55:29139
[email protected]f04182f32010-12-10 19:12:07140 AssertContainersConsistent();
141
142 // Go through all downloads in downloads_. Dangerous ones we need to
143 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:09144 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:07145 DownloadItem* download = *it;
146
147 // Save iterator from potential erases in this set done by called code.
148 // Iterators after an erasure point are still valid for lists and
149 // associative containers such as sets.
150 it++;
151
[email protected]c09a8fd2011-11-21 19:54:50152 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29153 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07154 // The user hasn't accepted it, so we need to remove it
155 // from the disk. This may or may not result in it being
156 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12157 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07158 // removes and deletes it if it's known to the history service)
159 // so the only thing we know after calling this function is that
160 // the download was deleted if-and-only-if it was removed
161 // from all queues.
[email protected]303077002011-04-19 23:21:01162 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26163 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17164 download->Cancel(false);
165 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29166 }
167 }
168
[email protected]f04182f32010-12-10 19:12:07169 // At this point, all dangerous downloads have had their files removed
170 // and all in progress downloads have been cancelled. We can now delete
171 // anything left.
[email protected]9ccbb372008-10-10 18:50:32172
[email protected]5cd11b6e2011-06-10 20:30:59173 // Copy downloads_ to separate container so as not to set off checks
174 // in DownloadItem destruction.
175 DownloadSet downloads_to_delete;
176 downloads_to_delete.swap(downloads_);
177
initial.commit09911bf2008-07-26 23:55:29178 in_progress_.clear();
[email protected]70850c72011-01-11 17:31:27179 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59180 history_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59181 STLDeleteElements(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29182
[email protected]41f558fb2012-01-09 22:59:58183 // We'll have nothing more to report to the observers after this point.
184 observers_.Clear();
185
[email protected]6d0146c2011-08-04 19:13:04186 DCHECK(save_page_downloads_.empty());
187
initial.commit09911bf2008-07-26 23:55:29188 file_manager_ = NULL;
[email protected]2588ea9d2011-08-22 20:59:53189 delegate_->Shutdown();
[email protected]82f37b02010-07-29 22:04:57190
[email protected]41f558fb2012-01-09 22:59:58191 if (status_updater_)
192 status_updater_->RemoveDelegate(this);
193 status_updater_.reset();
initial.commit09911bf2008-07-26 23:55:29194}
195
[email protected]5656f8a2011-11-17 16:12:58196void DownloadManagerImpl::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04197 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57198 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58199
[email protected]f04182f32010-12-10 19:12:07200 for (DownloadMap::iterator it = history_downloads_.begin();
201 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50202 if (it->second->IsTemporary() &&
[email protected]fdd2715c2011-12-09 22:24:20203 (dir_path.empty() || it->second->GetFullPath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57204 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58205 }
[email protected]6aa4a1c02010-01-15 18:49:58206}
207
[email protected]5656f8a2011-11-17 16:12:58208void DownloadManagerImpl::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04209 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57210 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34211
[email protected]f04182f32010-12-10 19:12:07212 for (DownloadMap::iterator it = history_downloads_.begin();
213 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50214 if (!it->second->IsTemporary() &&
215 (dir_path.empty() || it->second->GetFullPath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57216 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34217 }
[email protected]8ddbd66a2010-05-21 16:38:34218}
219
[email protected]5656f8a2011-11-17 16:12:58220void DownloadManagerImpl::SearchDownloads(const string16& query,
221 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26222 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42223
[email protected]f04182f32010-12-10 19:12:07224 for (DownloadMap::iterator it = history_downloads_.begin();
225 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42226 DownloadItem* download_item = it->second;
227
[email protected]c09a8fd2011-11-21 19:54:50228 if (download_item->IsTemporary())
[email protected]d3b12902010-08-16 23:39:42229 continue;
230
231 // Display Incognito downloads only in Incognito window, and vice versa.
232 // The Incognito Downloads page will get the list of non-Incognito downloads
233 // from its parent profile.
[email protected]c09a8fd2011-11-21 19:54:50234 if (browser_context_->IsOffTheRecord() != download_item->IsOtr())
[email protected]d3b12902010-08-16 23:39:42235 continue;
236
237 if (download_item->MatchesQuery(query_lower))
238 result->push_back(download_item);
239 }
[email protected]d3b12902010-08-16 23:39:42240}
241
initial.commit09911bf2008-07-26 23:55:29242// Query the history service for information about all persisted downloads.
[email protected]5656f8a2011-11-17 16:12:58243bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03244 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29245 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
246 shutdown_needed_ = true;
247
[email protected]6d0c9fb2011-08-22 19:26:03248 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46249
[email protected]b39e7a88b2012-01-10 21:43:17250 // In test mode, there may be no ResourceDispatcherHost. In this case it's
251 // safe to avoid setting |file_manager_| because we only call a small set of
252 // functions, none of which need it.
253 ResourceDispatcherHost* rdh =
254 content::GetContentClient()->browser()->GetResourceDispatcherHost();
255 if (rdh) {
256 file_manager_ = rdh->download_file_manager();
257 DCHECK(file_manager_);
258 }
initial.commit09911bf2008-07-26 23:55:29259
initial.commit09911bf2008-07-26 23:55:29260 return true;
261}
262
[email protected]aa9881c2011-08-15 18:01:12263// We have received a message from DownloadFileManager about a new download.
[email protected]5656f8a2011-11-17 16:12:58264void DownloadManagerImpl::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35266
[email protected]aa9881c2011-08-15 18:01:12267 if (delegate_->ShouldStartDownload(download_id))
268 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35269}
270
[email protected]5656f8a2011-11-17 16:12:58271void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
273 for (DownloadMap::iterator it = history_downloads_.begin();
274 it != history_downloads_.end(); ++it) {
275 CheckForFileRemoval(it->second);
276 }
277}
278
[email protected]5656f8a2011-11-17 16:12:58279void DownloadManagerImpl::CheckForFileRemoval(DownloadItem* download_item) {
[email protected]9fc114672011-06-15 08:17:48280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
281 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50282 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48283 BrowserThread::PostTask(
284 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58285 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]c09a8fd2011-11-21 19:54:50286 this, download_item->GetDbHandle(),
[email protected]fabf36d22011-10-28 21:50:17287 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48288 }
289}
290
[email protected]5656f8a2011-11-17 16:12:58291void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]9fc114672011-06-15 08:17:48292 int64 db_handle, const FilePath& path) {
293 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
294 if (!file_util::PathExists(path)) {
295 BrowserThread::PostTask(
296 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58297 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
298 this,
299 db_handle));
[email protected]9fc114672011-06-15 08:17:48300 }
301}
302
[email protected]5656f8a2011-11-17 16:12:58303void DownloadManagerImpl::OnFileRemovalDetected(int64 db_handle) {
[email protected]9fc114672011-06-15 08:17:48304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
305 DownloadMap::iterator it = history_downloads_.find(db_handle);
306 if (it != history_downloads_.end()) {
307 DownloadItem* download_item = it->second;
308 download_item->OnDownloadedFileRemoved();
309 }
310}
311
[email protected]443853c62011-12-22 19:22:41312void DownloadManagerImpl::RestartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29314
[email protected]4cd82f72011-05-23 19:15:01315 DownloadItem* download = GetActiveDownloadItem(download_id);
316 if (!download)
317 return;
318
319 VLOG(20) << __FUNCTION__ << "()"
320 << " download = " << download->DebugString(true);
321
[email protected]c09a8fd2011-11-21 19:54:50322 FilePath suggested_path = download->GetSuggestedPath();
[email protected]4cd82f72011-05-23 19:15:01323
[email protected]c09a8fd2011-11-21 19:54:50324 if (download->PromptUserForSaveLocation()) {
initial.commit09911bf2008-07-26 23:55:29325 // We must ask the user for the place to put the download.
[email protected]ef9572e2012-01-04 22:14:12326 WebContents* contents = download->GetTabContents();
[email protected]99cb7f82011-07-28 17:27:26327
[email protected]4cd82f72011-05-23 19:15:01328 // |id_ptr| will be deleted in either FileSelected() or
[email protected]93af2272011-09-21 18:29:17329 // FileSelectionCancelled().
[email protected]4cd82f72011-05-23 19:15:01330 int32* id_ptr = new int32;
331 *id_ptr = download_id;
[email protected]795b76a2011-12-14 16:52:53332 FilePath target_path;
333 // If |download| is a potentially dangerous file, then |suggested_path|
334 // contains the intermediate name instead of the final download
335 // filename. The latter is GetTargetName().
336 if (download->GetDangerType() != DownloadStateInfo::NOT_DANGEROUS)
337 target_path = suggested_path.DirName().Append(download->GetTargetName());
338 else
339 target_path = suggested_path;
[email protected]99cb7f82011-07-28 17:27:26340
[email protected]795b76a2011-12-14 16:52:53341 delegate_->ChooseDownloadPath(contents, target_path,
342 reinterpret_cast<void*>(id_ptr));
[email protected]f5920322011-03-24 20:34:16343 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]fed38252011-07-08 17:26:50344 SelectFileDialogDisplayed(download_id));
initial.commit09911bf2008-07-26 23:55:29345 } else {
346 // No prompting for download, just continue with the suggested name.
[email protected]4cd82f72011-05-23 19:15:01347 ContinueDownloadWithPath(download, suggested_path);
initial.commit09911bf2008-07-26 23:55:29348 }
349}
350
[email protected]37757c62011-12-20 20:07:12351content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58352 return browser_context_;
353}
354
355FilePath DownloadManagerImpl::LastDownloadPath() {
356 return last_download_path_;
357}
358
359void DownloadManagerImpl::CreateDownloadItem(
[email protected]594e66fe2011-10-25 22:49:41360 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) {
[email protected]c2e76012010-12-23 21:10:29361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
362
[email protected]c09a8fd2011-11-21 19:54:50363 DownloadItem* download = new DownloadItemImpl(
[email protected]ae77da82011-11-01 19:17:29364 this, *info, new DownloadRequestHandle(request_handle),
365 browser_context_->IsOffTheRecord());
[email protected]2909e342011-10-29 00:46:53366 int32 download_id = info->download_id.local();
[email protected]4cd82f72011-05-23 19:15:01367 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]d8472d92011-08-26 20:15:20368
[email protected]a896ce32012-01-09 22:04:07369 CHECK_96627(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29370 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01371 active_downloads_[download_id] = download;
[email protected]c2e76012010-12-23 21:10:29372}
373
[email protected]fc03de22011-12-06 23:28:12374DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem(
375 const FilePath& main_file_path,
376 const GURL& page_url,
377 bool is_otr,
378 DownloadItem::Observer* observer) {
379 DownloadItem* download = new DownloadItemImpl(
380 this, main_file_path, page_url, is_otr, GetNextId());
381
382 download->AddObserver(observer);
383
384 DCHECK(!ContainsKey(save_page_downloads_, download->GetId()));
385 downloads_.insert(download);
386 save_page_downloads_[download->GetId()] = download;
387
388 // Will notify the observer in the callback.
389 delegate_->AddItemToPersistentStore(download);
390
391 return download;
392}
393
[email protected]795b76a2011-12-14 16:52:53394// For non-safe downloads with no prompting, |chosen_file| is the intermediate
395// path for saving the in-progress download. The final target filename for these
396// is |download->GetTargetName()|. For all other downloads (non-safe downloads
397// for which we have prompted for a save location, and all safe downloads),
398// |chosen_file| is the final target download path.
[email protected]5656f8a2011-11-17 16:12:58399void DownloadManagerImpl::ContinueDownloadWithPath(
400 DownloadItem* download, const FilePath& chosen_file) {
[email protected]ca4b5fa32010-10-09 12:42:18401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01402 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39403
[email protected]c09a8fd2011-11-21 19:54:50404 int32 download_id = download->GetId();
initial.commit09911bf2008-07-26 23:55:29405
[email protected]70850c72011-01-11 17:31:27406 // NOTE(ahendrickson) Eventually |active_downloads_| will replace
407 // |in_progress_|, but we don't want to change the semantics yet.
[email protected]4cd82f72011-05-23 19:15:01408 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]70850c72011-01-11 17:31:27409 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01410 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27411
[email protected]4cd82f72011-05-23 19:15:01412 // Make sure the initial file name is set only once.
[email protected]fdd2715c2011-12-09 22:24:20413 DCHECK(download->GetFullPath().empty());
[email protected]4cd82f72011-05-23 19:15:01414 download->OnPathDetermined(chosen_file);
[email protected]4cd82f72011-05-23 19:15:01415
416 VLOG(20) << __FUNCTION__ << "()"
417 << " download = " << download->DebugString(true);
418
419 in_progress_[download_id] = download;
[email protected]5f8589fe2011-08-17 20:58:39420 UpdateDownloadProgress(); // Reflect entry into in_progress_.
initial.commit09911bf2008-07-26 23:55:29421
[email protected]adb2f3d12011-01-23 16:24:54422 // Rename to intermediate name.
[email protected]f5920322011-03-24 20:34:16423 FilePath download_path;
[email protected]ec865262011-08-23 20:01:48424 if (!delegate_->OverrideIntermediatePath(download, &download_path))
[email protected]c09a8fd2011-11-21 19:54:50425 download_path = download->GetFullPath();
[email protected]594cd7d2010-07-21 03:23:56426
[email protected]f5920322011-03-24 20:34:16427 BrowserThread::PostTask(
428 BrowserThread::FILE, FROM_HERE,
[email protected]fabf36d22011-10-28 21:50:17429 base::Bind(&DownloadFileManager::RenameInProgressDownloadFile,
[email protected]fc03de22011-12-06 23:28:12430 file_manager_, download->GetGlobalId(),
431 download_path));
[email protected]f5920322011-03-24 20:34:16432
433 download->Rename(download_path);
434
[email protected]2588ea9d2011-08-22 20:59:53435 delegate_->AddItemToPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29436}
437
[email protected]443853c62011-12-22 19:22:41438void DownloadManagerImpl::UpdateDownload(int32 download_id,
439 int64 bytes_so_far,
440 int64 bytes_per_sec,
[email protected]0afff032012-01-06 20:55:00441 const std::string& hash_state) {
[email protected]70850c72011-01-11 17:31:27442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
443 DownloadMap::iterator it = active_downloads_.find(download_id);
444 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29445 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26446 if (download->IsInProgress()) {
[email protected]443853c62011-12-22 19:22:41447 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state);
[email protected]5f8589fe2011-08-17 20:58:39448 UpdateDownloadProgress(); // Reflect size updates.
[email protected]2588ea9d2011-08-22 20:59:53449 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27450 }
initial.commit09911bf2008-07-26 23:55:29451 }
452}
453
[email protected]5656f8a2011-11-17 16:12:58454void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
455 int64 size,
456 const std::string& hash) {
[email protected]33c6d3f12011-09-04 00:00:54457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]da6e3922010-11-24 21:45:50458 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
459 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32461
[email protected]c4f02c42011-01-24 21:55:06462 // If it's not in active_downloads_, that means it was cancelled; just
463 // ignore the notification.
464 if (active_downloads_.count(download_id) == 0)
465 return;
466
[email protected]adb2f3d12011-01-23 16:24:54467 DownloadItem* download = active_downloads_[download_id];
[email protected]ac4af82f2011-11-10 19:09:37468 download->OnAllDataSaved(size, hash);
469 delegate_->OnResponseCompleted(download);
[email protected]b09f1282011-09-14 00:37:45470
[email protected]fc03de22011-12-06 23:28:12471 download->MaybeCompleteDownload();
[email protected]adb2f3d12011-01-23 16:24:54472}
[email protected]9ccbb372008-10-10 18:50:32473
[email protected]fc03de22011-12-06 23:28:12474void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const {
[email protected]a896ce32012-01-09 22:04:07475 // TODO(rdsmith): Change to DCHECK after http://crbug.com/96627 resolved.
[email protected]c09a8fd2011-11-21 19:54:50476 if (download->GetState() == DownloadItem::REMOVING) {
[email protected]7d413112011-06-16 18:50:17477 CHECK(!ContainsKey(downloads_, download));
[email protected]c09a8fd2011-11-21 19:54:50478 CHECK(!ContainsKey(active_downloads_, download->GetId()));
479 CHECK(!ContainsKey(in_progress_, download->GetId()));
480 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17481 return;
482 }
483
484 // Should be in downloads_ if we're not REMOVING.
485 CHECK(ContainsKey(downloads_, download));
486
487 // Check history_downloads_ consistency.
[email protected]c09a8fd2011-11-21 19:54:50488 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) {
489 CHECK(ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17490 } else {
[email protected]fc03de22011-12-06 23:28:12491 for (DownloadMap::const_iterator it = history_downloads_.begin();
[email protected]7d413112011-06-16 18:50:17492 it != history_downloads_.end(); ++it) {
[email protected]a896ce32012-01-09 22:04:07493 CHECK_96627(it->second != download);
[email protected]7d413112011-06-16 18:50:17494 }
495 }
496
[email protected]c09a8fd2011-11-21 19:54:50497 int64 state = download->GetState();
[email protected]61b75a52011-08-29 16:34:34498 base::debug::Alias(&state);
[email protected]c09a8fd2011-11-21 19:54:50499 if (ContainsKey(active_downloads_, download->GetId())) {
500 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle)
501 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
502 if (DownloadItem::IN_PROGRESS != download->GetState())
503 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle());
[email protected]f9a2997f2011-09-23 16:54:07504 }
[email protected]c09a8fd2011-11-21 19:54:50505 if (DownloadItem::IN_PROGRESS == download->GetState())
506 CHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]5cd11b6e2011-06-10 20:30:59507}
508
[email protected]5656f8a2011-11-17 16:12:58509bool DownloadManagerImpl::IsDownloadReadyForCompletion(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54510 // If we don't have all the data, the download is not ready for
511 // completion.
[email protected]c09a8fd2011-11-21 19:54:50512 if (!download->AllDataSaved())
[email protected]adb2f3d12011-01-23 16:24:54513 return false;
[email protected]6a7fb042010-02-01 16:30:47514
[email protected]9d7ef802011-02-25 19:03:35515 // If the download is dangerous, but not yet validated, it's not ready for
516 // completion.
[email protected]c09a8fd2011-11-21 19:54:50517 if (download->GetSafetyState() == DownloadItem::DANGEROUS)
[email protected]9d7ef802011-02-25 19:03:35518 return false;
519
[email protected]adb2f3d12011-01-23 16:24:54520 // If the download isn't active (e.g. has been cancelled) it's not
521 // ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50522 if (active_downloads_.count(download->GetId()) == 0)
[email protected]adb2f3d12011-01-23 16:24:54523 return false;
524
525 // If the download hasn't been inserted into the history system
526 // (which occurs strictly after file name determination, intermediate
527 // file rename, and UI display) then it's not ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50528 if (download->GetDbHandle() == DownloadItem::kUninitializedHandle)
[email protected]7054b592011-06-22 14:46:42529 return false;
530
531 return true;
[email protected]adb2f3d12011-01-23 16:24:54532}
533
[email protected]5656f8a2011-11-17 16:12:58534void DownloadManagerImpl::MaybeCompleteDownload(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54535 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
536 VLOG(20) << __FUNCTION__ << "()" << " download = "
537 << download->DebugString(false);
538
539 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32540 return;
[email protected]9ccbb372008-10-10 18:50:32541
[email protected]adb2f3d12011-01-23 16:24:54542 // TODO(rdsmith): DCHECK that we only pass through this point
543 // once per download. The natural way to do this is by a state
544 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56545
[email protected]adb2f3d12011-01-23 16:24:54546 // Confirm we're in the proper set of states to be here;
[email protected]9d7ef802011-02-25 19:03:35547 // in in_progress_, have all data, have a history handle, (validated or safe).
[email protected]c09a8fd2011-11-21 19:54:50548 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState());
549 DCHECK_EQ(1u, in_progress_.count(download->GetId()));
550 DCHECK(download->AllDataSaved());
551 DCHECK(download->GetDbHandle() != DownloadItem::kUninitializedHandle);
552 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle()));
[email protected]adb2f3d12011-01-23 16:24:54553
[email protected]c2918652011-11-01 18:50:23554 // Give the delegate a chance to override.
555 if (!delegate_->ShouldCompleteDownload(download))
556 return;
557
[email protected]adb2f3d12011-01-23 16:24:54558 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
559 << download->DebugString(false);
560
561 // Remove the id from in_progress
[email protected]c09a8fd2011-11-21 19:54:50562 in_progress_.erase(download->GetId());
[email protected]5f8589fe2011-08-17 20:58:39563 UpdateDownloadProgress(); // Reflect removal from in_progress_.
[email protected]adb2f3d12011-01-23 16:24:54564
[email protected]2588ea9d2011-08-22 20:59:53565 delegate_->UpdateItemInPersistentStore(download);
[email protected]adb2f3d12011-01-23 16:24:54566
[email protected]f5920322011-03-24 20:34:16567 // Finish the download.
[email protected]48837962011-04-19 17:03:29568 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32569}
570
[email protected]fc03de22011-12-06 23:28:12571void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) {
[email protected]70850c72011-01-11 17:31:27572 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21573 DCHECK(download);
[email protected]2588ea9d2011-08-22 20:59:53574 delegate_->UpdateItemInPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12575 active_downloads_.erase(download->GetId());
576 AssertStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27577}
578
[email protected]5656f8a2011-11-17 16:12:58579void DownloadManagerImpl::OnDownloadRenamedToFinalName(
580 int download_id,
581 const FilePath& full_path,
582 int uniquifier) {
[email protected]da6e3922010-11-24 21:45:50583 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
[email protected]f5920322011-03-24 20:34:16584 << " full_path = \"" << full_path.value() << "\""
585 << " uniquifier = " << uniquifier;
[email protected]ca4b5fa32010-10-09 12:42:18586 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f5920322011-03-24 20:34:16587
[email protected]2e030682010-07-23 19:45:36588 DownloadItem* item = GetDownloadItem(download_id);
589 if (!item)
590 return;
[email protected]6cade212008-12-03 00:32:22591
[email protected]795b76a2011-12-14 16:52:53592 if (item->GetDangerType() == DownloadStateInfo::NOT_DANGEROUS ||
593 item->PromptUserForSaveLocation()) {
594 DCHECK_EQ(0, uniquifier)
595 << "We should not uniquify user supplied filenames or safe filenames "
596 "that have already been uniquified.";
[email protected]8fa1eeb52011-04-13 14:18:02597 }
598
[email protected]fabf36d22011-10-28 21:50:17599 BrowserThread::PostTask(
600 BrowserThread::FILE, FROM_HERE,
601 base::Bind(&DownloadFileManager::CompleteDownload,
[email protected]c09a8fd2011-11-21 19:54:50602 file_manager_, item->GetGlobalId()));
[email protected]9ccbb372008-10-10 18:50:32603
[email protected]f5920322011-03-24 20:34:16604 if (uniquifier)
[email protected]c09a8fd2011-11-21 19:54:50605 item->SetPathUniquifier(uniquifier);
[email protected]9ccbb372008-10-10 18:50:32606
[email protected]f5920322011-03-24 20:34:16607 item->OnDownloadRenamedToFinalName(full_path);
[email protected]2588ea9d2011-08-22 20:59:53608 delegate_->UpdatePathForItemInPersistentStore(item, full_path);
initial.commit09911bf2008-07-26 23:55:29609}
610
[email protected]5656f8a2011-11-17 16:12:58611void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]93af2272011-09-21 18:29:17612 DownloadItem* download = GetActiveDownload(download_id);
613 // A cancel at the right time could remove the download from the
614 // |active_downloads_| map before we get here.
615 if (!download)
616 return;
617
618 download->Cancel(true);
619}
620
[email protected]fc03de22011-12-06 23:28:12621void DownloadManagerImpl::DownloadCancelled(DownloadItem* download) {
[email protected]d8472d92011-08-26 20:15:20622 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20623
624 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50625 << " download = " << download->DebugString(true);
626
[email protected]93af2272011-09-21 18:29:17627 RemoveFromActiveList(download);
[email protected]47a881b2011-08-29 22:59:21628 // This function is called from the DownloadItem, so DI state
629 // should already have been updated.
[email protected]fc03de22011-12-06 23:28:12630 AssertStateConsistent(download);
initial.commit09911bf2008-07-26 23:55:29631
[email protected]15d90ba2011-11-03 03:41:55632 if (file_manager_)
633 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29634}
635
[email protected]5656f8a2011-11-17 16:12:58636void DownloadManagerImpl::OnDownloadInterrupted(int32 download_id,
637 int64 size,
[email protected]0afff032012-01-06 20:55:00638 const std::string& hash_state,
[email protected]5656f8a2011-11-17 16:12:58639 InterruptReason reason) {
[email protected]47a881b2011-08-29 22:59:21640 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
641
642 DownloadItem* download = GetActiveDownload(download_id);
643 if (!download)
644 return;
645
[email protected]be76b7e2011-10-13 12:57:57646 VLOG(20) << __FUNCTION__ << "()"
647 << " reason " << InterruptReasonDebugString(reason)
[email protected]c09a8fd2011-11-21 19:54:50648 << " at offset " << download->GetReceivedBytes()
[email protected]47a881b2011-08-29 22:59:21649 << " size = " << size
650 << " download = " << download->DebugString(true);
651
[email protected]93af2272011-09-21 18:29:17652 RemoveFromActiveList(download);
[email protected]443853c62011-12-22 19:22:41653 download->Interrupted(size, hash_state, reason);
[email protected]93af2272011-09-21 18:29:17654 download->OffThreadCancel(file_manager_);
[email protected]47a881b2011-08-29 22:59:21655}
656
[email protected]5656f8a2011-11-17 16:12:58657DownloadItem* DownloadManagerImpl::GetActiveDownload(int32 download_id) {
[email protected]bf68a00b2011-04-07 17:28:26658 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
659 DownloadMap::iterator it = active_downloads_.find(download_id);
[email protected]bf68a00b2011-04-07 17:28:26660 if (it == active_downloads_.end())
[email protected]47a881b2011-08-29 22:59:21661 return NULL;
[email protected]bf68a00b2011-04-07 17:28:26662
663 DownloadItem* download = it->second;
664
[email protected]47a881b2011-08-29 22:59:21665 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50666 DCHECK_EQ(download_id, download->GetId());
[email protected]4cd82f72011-05-23 19:15:01667
[email protected]47a881b2011-08-29 22:59:21668 return download;
669}
[email protected]54610672011-07-18 18:24:43670
[email protected]5656f8a2011-11-17 16:12:58671void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) {
[email protected]93af2272011-09-21 18:29:17672 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
673 DCHECK(download);
674
675 // Clean up will happen when the history system create callback runs if we
676 // don't have a valid db_handle yet.
[email protected]c09a8fd2011-11-21 19:54:50677 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) {
678 in_progress_.erase(download->GetId());
679 active_downloads_.erase(download->GetId());
[email protected]93af2272011-09-21 18:29:17680 UpdateDownloadProgress(); // Reflect removal from in_progress_.
681 delegate_->UpdateItemInPersistentStore(download);
682 }
683}
684
[email protected]5656f8a2011-11-17 16:12:58685content::DownloadManagerDelegate* DownloadManagerImpl::delegate() const {
686 return delegate_;
687}
688
689void DownloadManagerImpl::SetDownloadManagerDelegate(
[email protected]1bd0ef12011-10-20 05:24:17690 content::DownloadManagerDelegate* delegate) {
[email protected]9bb54ee2011-10-12 17:43:35691 delegate_ = delegate;
692}
693
[email protected]5656f8a2011-11-17 16:12:58694void DownloadManagerImpl::UpdateDownloadProgress() {
[email protected]5f8589fe2011-08-17 20:58:39695 delegate_->DownloadProgressUpdated();
[email protected]6a7fb042010-02-01 16:30:47696}
697
[email protected]5656f8a2011-11-17 16:12:58698int DownloadManagerImpl::RemoveDownloadItems(
[email protected]6d0146c2011-08-04 19:13:04699 const DownloadVector& pending_deletes) {
700 if (pending_deletes.empty())
701 return 0;
702
703 // Delete from internal maps.
704 for (DownloadVector::const_iterator it = pending_deletes.begin();
705 it != pending_deletes.end();
706 ++it) {
707 DownloadItem* download = *it;
708 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50709 history_downloads_.erase(download->GetDbHandle());
710 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:04711 downloads_.erase(download);
712 }
713
714 // Tell observers to refresh their views.
715 NotifyModelChanged();
716
717 // Delete the download items themselves.
718 const int num_deleted = static_cast<int>(pending_deletes.size());
719 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
720 return num_deleted;
721}
722
[email protected]fc03de22011-12-06 23:28:12723void DownloadManagerImpl::DownloadRemoved(DownloadItem* download) {
724 if (history_downloads_.find(download->GetDbHandle()) ==
725 history_downloads_.end())
[email protected]93af2272011-09-21 18:29:17726 return;
727
728 // Make history update.
[email protected]93af2272011-09-21 18:29:17729 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29730
731 // Remove from our tables and delete.
[email protected]6d0146c2011-08-04 19:13:04732 int downloads_count = RemoveDownloadItems(DownloadVector(1, download));
[email protected]f04182f32010-12-10 19:12:07733 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29734}
735
[email protected]5656f8a2011-11-17 16:12:58736int DownloadManagerImpl::RemoveDownloadsBetween(const base::Time remove_begin,
737 const base::Time remove_end) {
[email protected]2588ea9d2011-08-22 20:59:53738 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29739
[email protected]a312a442010-12-15 23:40:33740 // All downloads visible to the user will be in the history,
741 // so scan that map.
[email protected]6d0146c2011-08-04 19:13:04742 DownloadVector pending_deletes;
743 for (DownloadMap::const_iterator it = history_downloads_.begin();
744 it != history_downloads_.end();
745 ++it) {
initial.commit09911bf2008-07-26 23:55:29746 DownloadItem* download = it->second;
[email protected]c09a8fd2011-11-21 19:54:50747 if (download->GetStartTime() >= remove_begin &&
748 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04749 (download->IsComplete() || download->IsCancelled())) {
[email protected]fc03de22011-12-06 23:28:12750 AssertStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28751 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29752 }
initial.commit09911bf2008-07-26 23:55:29753 }
[email protected]6d0146c2011-08-04 19:13:04754 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29755}
756
[email protected]5656f8a2011-11-17 16:12:58757int DownloadManagerImpl::RemoveDownloads(const base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59758 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29759}
760
[email protected]5656f8a2011-11-17 16:12:58761int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]da4a5582011-10-17 19:08:06762 download_stats::RecordClearAllSize(history_downloads_.size());
[email protected]d41355e6f2009-04-07 21:21:12763 // The null times make the date range unbounded.
764 return RemoveDownloadsBetween(base::Time(), base::Time());
765}
766
initial.commit09911bf2008-07-26 23:55:29767// Initiate a download of a specific URL. We send the request to the
768// ResourceDispatcherHost, and let it send us responses like a regular
769// download.
[email protected]5656f8a2011-11-17 16:12:58770void DownloadManagerImpl::DownloadUrl(const GURL& url,
771 const GURL& referrer,
772 const std::string& referrer_charset,
[email protected]bb81f382012-01-03 22:45:44773 WebContents* web_contents) {
[email protected]ae8945192010-07-20 16:56:26774 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
[email protected]bb81f382012-01-03 22:45:44775 web_contents);
[email protected]6aa4a1c02010-01-15 18:49:58776}
777
[email protected]5656f8a2011-11-17 16:12:58778void DownloadManagerImpl::DownloadUrlToFile(const GURL& url,
779 const GURL& referrer,
780 const std::string& referrer_charset,
781 const DownloadSaveInfo& save_info,
[email protected]fbc5e5f92012-01-02 06:08:32782 WebContents* web_contents) {
[email protected]c79a0c02011-08-22 22:37:37783 ResourceDispatcherHost* resource_dispatcher_host =
[email protected]b39e7a88b2012-01-10 21:43:17784 content::GetContentClient()->browser()->GetResourceDispatcherHost();
[email protected]443853c62011-12-22 19:22:41785
[email protected]ed24fad2011-05-10 22:44:01786 // We send a pointer to content::ResourceContext, instead of the usual
787 // reference, so that a copy of the object isn't made.
[email protected]fabf36d22011-10-28 21:50:17788 // base::Bind can't handle 7 args, so we use URLParams and RenderParams.
789 BrowserThread::PostTask(
790 BrowserThread::IO, FROM_HERE,
791 base::Bind(&BeginDownload,
792 URLParams(url, referrer), save_info, resource_dispatcher_host,
[email protected]fbc5e5f92012-01-02 06:08:32793 RenderParams(web_contents->GetRenderProcessHost()->GetID(),
794 web_contents->GetRenderViewHost()->routing_id()),
795 &web_contents->GetBrowserContext()->GetResourceContext()));
initial.commit09911bf2008-07-26 23:55:29796}
797
[email protected]5656f8a2011-11-17 16:12:58798void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29799 observers_.AddObserver(observer);
800 observer->ModelChanged();
801}
802
[email protected]5656f8a2011-11-17 16:12:58803void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29804 observers_.RemoveObserver(observer);
805}
806
[email protected]5656f8a2011-11-17 16:12:58807bool DownloadManagerImpl::IsDownloadProgressKnown() const {
[email protected]45f432e942011-10-25 18:17:22808 for (DownloadMap::const_iterator i = in_progress_.begin();
[email protected]073ed7b2010-09-27 09:20:02809 i != in_progress_.end(); ++i) {
[email protected]c09a8fd2011-11-21 19:54:50810 if (i->second->GetTotalBytes() <= 0)
[email protected]073ed7b2010-09-27 09:20:02811 return false;
812 }
813
814 return true;
815}
816
[email protected]5656f8a2011-11-17 16:12:58817int64 DownloadManagerImpl::GetInProgressDownloadCount() const {
[email protected]073ed7b2010-09-27 09:20:02818 return in_progress_.size();
819}
820
[email protected]5656f8a2011-11-17 16:12:58821int64 DownloadManagerImpl::GetReceivedDownloadBytes() const {
[email protected]073ed7b2010-09-27 09:20:02822 DCHECK(IsDownloadProgressKnown());
823 int64 received_bytes = 0;
[email protected]45f432e942011-10-25 18:17:22824 for (DownloadMap::const_iterator i = in_progress_.begin();
[email protected]073ed7b2010-09-27 09:20:02825 i != in_progress_.end(); ++i) {
[email protected]c09a8fd2011-11-21 19:54:50826 received_bytes += i->second->GetReceivedBytes();
[email protected]073ed7b2010-09-27 09:20:02827 }
828 return received_bytes;
829}
830
[email protected]5656f8a2011-11-17 16:12:58831int64 DownloadManagerImpl::GetTotalDownloadBytes() const {
[email protected]073ed7b2010-09-27 09:20:02832 DCHECK(IsDownloadProgressKnown());
833 int64 total_bytes = 0;
[email protected]45f432e942011-10-25 18:17:22834 for (DownloadMap::const_iterator i = in_progress_.begin();
[email protected]073ed7b2010-09-27 09:20:02835 i != in_progress_.end(); ++i) {
[email protected]c09a8fd2011-11-21 19:54:50836 total_bytes += i->second->GetTotalBytes();
[email protected]073ed7b2010-09-27 09:20:02837 }
838 return total_bytes;
839}
840
[email protected]5656f8a2011-11-17 16:12:58841void DownloadManagerImpl::FileSelected(const FilePath& path, void* params) {
[email protected]4cd82f72011-05-23 19:15:01842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
843
844 int32* id_ptr = reinterpret_cast<int32*>(params);
845 DCHECK(id_ptr != NULL);
846 int32 download_id = *id_ptr;
847 delete id_ptr;
848
849 DownloadItem* download = GetActiveDownloadItem(download_id);
850 if (!download)
851 return;
852 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
853 << " download = " << download->DebugString(true);
854
[email protected]c09a8fd2011-11-21 19:54:50855 if (download->PromptUserForSaveLocation())
[email protected]7ae7c2cb2009-01-06 23:31:41856 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35857
[email protected]4cd82f72011-05-23 19:15:01858 // Make sure the initial file name is set only once.
859 ContinueDownloadWithPath(download, path);
initial.commit09911bf2008-07-26 23:55:29860}
861
[email protected]5656f8a2011-11-17 16:12:58862void DownloadManagerImpl::FileSelectionCanceled(void* params) {
initial.commit09911bf2008-07-26 23:55:29863 // The user didn't pick a place to save the file, so need to cancel the
864 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01865 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
866 int32* id_ptr = reinterpret_cast<int32*>(params);
867 DCHECK(id_ptr != NULL);
868 int32 download_id = *id_ptr;
869 delete id_ptr;
870
871 DownloadItem* download = GetActiveDownloadItem(download_id);
872 if (!download)
873 return;
874
875 VLOG(20) << __FUNCTION__ << "()"
876 << " download = " << download->DebugString(true);
877
[email protected]93af2272011-09-21 18:29:17878 // TODO(ahendrickson) -- This currently has no effect, as the download is
879 // not put on the active list until the file selection is complete. Need
880 // to put it on the active list earlier in the process.
881 RemoveFromActiveList(download);
882
883 download->OffThreadCancel(file_manager_);
[email protected]4cd82f72011-05-23 19:15:01884}
885
initial.commit09911bf2008-07-26 23:55:29886// Operations posted to us from the history service ----------------------------
887
888// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25889// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]5656f8a2011-11-17 16:12:58890void DownloadManagerImpl::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25891 std::vector<DownloadPersistentStoreInfo>* entries) {
[email protected]d8472d92011-08-26 20:15:20892 // TODO(rdsmith): Remove this and related logic when
[email protected]a896ce32012-01-09 22:04:07893 // http://crbug.com/96627 is fixed.
[email protected]d8472d92011-08-26 20:15:20894 largest_db_handle_in_history_ = 0;
895
initial.commit09911bf2008-07-26 23:55:29896 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]fc03de22011-12-06 23:28:12897 DownloadItem* download = new DownloadItemImpl(
898 this, GetNextId(), entries->at(i));
[email protected]a896ce32012-01-09 22:04:07899 CHECK_96627(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]f04182f32010-12-10 19:12:07900 downloads_.insert(download);
[email protected]c09a8fd2011-11-21 19:54:50901 history_downloads_[download->GetDbHandle()] = download;
[email protected]da6e3922010-11-24 21:45:50902 VLOG(20) << __FUNCTION__ << "()" << i << ">"
903 << " download = " << download->DebugString(true);
[email protected]d8472d92011-08-26 20:15:20904
[email protected]c09a8fd2011-11-21 19:54:50905 if (download->GetDbHandle() > largest_db_handle_in_history_)
906 largest_db_handle_in_history_ = download->GetDbHandle();
initial.commit09911bf2008-07-26 23:55:29907 }
[email protected]b0ab1d42010-02-24 19:29:28908 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48909 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29910}
911
[email protected]5656f8a2011-11-17 16:12:58912void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItem* download,
913 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27914 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d2a8fb72010-01-21 05:31:42915
[email protected]a896ce32012-01-09 22:04:07916 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/96627
[email protected]1e9fe7ff2011-06-24 17:37:33917 // is fixed.
[email protected]2588ea9d2011-08-22 20:59:53918 CHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]1e9fe7ff2011-06-24 17:37:33919
[email protected]da4a5582011-10-17 19:08:06920 download_stats::RecordHistorySize(history_downloads_.size());
921
[email protected]c09a8fd2011-11-21 19:54:50922 DCHECK(download->GetDbHandle() == DownloadItem::kUninitializedHandle);
923 download->SetDbHandle(db_handle);
[email protected]5bcd73eb2011-03-23 21:14:02924
[email protected]a896ce32012-01-09 22:04:07925 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/96627
[email protected]d8472d92011-08-26 20:15:20926 // is fixed.
[email protected]a896ce32012-01-09 22:04:07927 CHECK_96627(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]c09a8fd2011-11-21 19:54:50928 history_downloads_[download->GetDbHandle()] = download;
[email protected]6d0146c2011-08-04 19:13:04929
930 // Show in the appropriate browser UI.
931 // This includes buttons to save or cancel, for a dangerous download.
932 ShowDownloadInBrowser(download);
933
934 // Inform interested objects about the new download.
935 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19936}
937
[email protected]2588ea9d2011-08-22 20:59:53938
[email protected]5656f8a2011-11-17 16:12:58939void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id,
940 int64 db_handle) {
[email protected]2588ea9d2011-08-22 20:59:53941 if (save_page_downloads_.count(download_id)) {
942 OnSavePageItemAddedToPersistentStore(download_id, db_handle);
943 } else if (active_downloads_.count(download_id)) {
944 OnDownloadItemAddedToPersistentStore(download_id, db_handle);
945 }
946 // It's valid that we don't find a matching item, i.e. on shutdown.
947}
948
[email protected]f9a45b02011-06-30 23:49:19949// Once the new DownloadItem's creation info has been committed to the history
950// service, we associate the DownloadItem with the db handle, update our
951// 'history_downloads_' map and inform observers.
[email protected]5656f8a2011-11-17 16:12:58952void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore(
953 int32 download_id, int64 db_handle) {
[email protected]f9a45b02011-06-30 23:49:19954 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]19420cc2011-07-18 17:43:45955 DownloadItem* download = GetActiveDownloadItem(download_id);
[email protected]93af2272011-09-21 18:29:17956 if (!download)
[email protected]19420cc2011-07-18 17:43:45957 return;
[email protected]54610672011-07-18 18:24:43958
959 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
960 << " download_id = " << download_id
961 << " download = " << download->DebugString(true);
[email protected]f9a45b02011-06-30 23:49:19962
[email protected]a896ce32012-01-09 22:04:07963 // TODO(rdsmith): Remove after http://crbug.com/96627 resolved.
[email protected]d8472d92011-08-26 20:15:20964 int64 largest_handle = largest_db_handle_in_history_;
965 base::debug::Alias(&largest_handle);
[email protected]e5107ce2011-09-19 20:36:13966 int32 matching_item_download_id
967 = (ContainsKey(history_downloads_, db_handle) ?
[email protected]c09a8fd2011-11-21 19:54:50968 history_downloads_[db_handle]->GetId() : 0);
[email protected]e5107ce2011-09-19 20:36:13969 base::debug::Alias(&matching_item_download_id);
970
[email protected]a896ce32012-01-09 22:04:07971 CHECK_96627(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:20972
[email protected]f9a45b02011-06-30 23:49:19973 AddDownloadItemToHistory(download, db_handle);
initial.commit09911bf2008-07-26 23:55:29974
[email protected]93af2272011-09-21 18:29:17975 // If the download is still in progress, try to complete it.
976 //
977 // Otherwise, download has been cancelled or interrupted before we've
978 // received the DB handle. We post one final message to the history
979 // service so that it can be properly in sync with the DownloadItem's
980 // completion status, and also inform any observers so that they get
981 // more than just the start notification.
982 if (download->IsInProgress()) {
983 MaybeCompleteDownload(download);
984 } else {
[email protected]a896ce32012-01-09 22:04:07985 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/96627
[email protected]93af2272011-09-21 18:29:17986 // is fixed.
987 CHECK(download->IsCancelled())
988 << " download = " << download->DebugString(true);
989 in_progress_.erase(download_id);
990 active_downloads_.erase(download_id);
991 delegate_->UpdateItemInPersistentStore(download);
992 download->UpdateObservers();
993 }
initial.commit09911bf2008-07-26 23:55:29994}
995
[email protected]5656f8a2011-11-17 16:12:58996void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]8ddbd66a2010-05-21 16:38:34997 // The 'contents' may no longer exist if the user closed the tab before we
[email protected]99cb7f82011-07-28 17:27:26998 // get this start completion event.
[email protected]2a6bc3e2011-12-28 23:51:33999 WebContents* content = download->GetTabContents();
[email protected]99cb7f82011-07-28 17:27:261000
1001 // If the contents no longer exists, we ask the embedder to suggest another
1002 // tab.
[email protected]da1a27b2011-07-29 23:16:331003 if (!content)
[email protected]ef9572e2012-01-04 22:14:121004 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:531005
[email protected]0bfbf882011-12-22 18:19:271006 if (content && content->GetDelegate())
1007 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:531008}
1009
[email protected]5656f8a2011-11-17 16:12:581010int DownloadManagerImpl::InProgressCount() const {
1011 return static_cast<int>(in_progress_.size());
1012}
1013
[email protected]6cade212008-12-03 00:32:221014// Clears the last download path, used to initialize "save as" dialogs.
[email protected]5656f8a2011-11-17 16:12:581015void DownloadManagerImpl::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:411016 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:381017}
[email protected]b0ab1d42010-02-24 19:29:281018
[email protected]5656f8a2011-11-17 16:12:581019void DownloadManagerImpl::NotifyModelChanged() {
[email protected]b0ab1d42010-02-24 19:29:281020 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
1021}
1022
[email protected]5656f8a2011-11-17 16:12:581023DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) {
[email protected]4cd82f72011-05-23 19:15:011024 // The |history_downloads_| map is indexed by the download's db_handle,
1025 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:071026 for (DownloadMap::iterator it = history_downloads_.begin();
1027 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:361028 DownloadItem* item = it->second;
[email protected]c09a8fd2011-11-21 19:54:501029 if (item->GetId() == download_id)
[email protected]2e030682010-07-23 19:45:361030 return item;
1031 }
1032 return NULL;
1033}
1034
[email protected]5656f8a2011-11-17 16:12:581035DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) {
[email protected]5d3e83642011-12-16 01:14:361036 if (ContainsKey(active_downloads_, download_id))
1037 return active_downloads_[download_id];
1038 return NULL;
[email protected]4cd82f72011-05-23 19:15:011039}
1040
[email protected]57fd1252010-12-23 17:24:091041// Confirm that everything in all maps is also in |downloads_|, and that
1042// everything in |downloads_| is also in some other map.
[email protected]5656f8a2011-11-17 16:12:581043void DownloadManagerImpl::AssertContainersConsistent() const {
[email protected]f04182f32010-12-10 19:12:071044#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:091045 // Turn everything into sets.
[email protected]6d0146c2011-08-04 19:13:041046 const DownloadMap* input_maps[] = {&active_downloads_,
1047 &history_downloads_,
1048 &save_page_downloads_};
1049 DownloadSet active_set, history_set, save_page_set;
1050 DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set};
1051 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:091052 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
1053 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:041054 it != input_maps[i]->end(); ++it) {
1055 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:071056 }
1057 }
[email protected]57fd1252010-12-23 17:24:091058
1059 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:091060 DownloadSet downloads_union;
1061 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1062 DownloadSet remainder;
1063 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1064 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
1065 downloads_.begin(), downloads_.end(),
1066 insert_it);
1067 DCHECK(remainder.empty());
1068 std::insert_iterator<DownloadSet>
1069 insert_union(downloads_union, downloads_union.end());
1070 std::set_union(downloads_union.begin(), downloads_union.end(),
1071 all_sets[i]->begin(), all_sets[i]->end(),
1072 insert_union);
1073 }
1074
1075 // Is everything in downloads_ present in one of the other sets?
1076 DownloadSet remainder;
1077 std::insert_iterator<DownloadSet>
1078 insert_remainder(remainder, remainder.begin());
1079 std::set_difference(downloads_.begin(), downloads_.end(),
1080 downloads_union.begin(), downloads_union.end(),
1081 insert_remainder);
1082 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:071083#endif
1084}
1085
[email protected]6d0146c2011-08-04 19:13:041086// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:531087// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:041088// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:531089// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
1090// that the history event is update regardless of the order in which these two
1091// events complete.
[email protected]6d0146c2011-08-04 19:13:041092// If something removes the download item from the download manager (Remove,
1093// Shutdown) the result will be that the SavePage system will not be able to
1094// properly update the download item (which no longer exists) or the download
1095// history, but the action will complete properly anyway. This may lead to the
1096// history entry being wrong on a reload of chrome (specifically in the case of
1097// Initiation -> History Callback -> Removal -> Completion), but there's no way
1098// to solve that without canceling on Remove (which would then update the DB).
1099
[email protected]5656f8a2011-11-17 16:12:581100void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore(
1101 int32 download_id, int64 db_handle) {
[email protected]6d0146c2011-08-04 19:13:041102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1103
1104 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
1105 // This can happen if the download manager is shutting down and all maps
1106 // have been cleared.
1107 if (it == save_page_downloads_.end())
1108 return;
1109
1110 DownloadItem* download = it->second;
1111 if (!download) {
1112 NOTREACHED();
1113 return;
1114 }
1115
[email protected]a896ce32012-01-09 22:04:071116 // TODO(rdsmith): Remove after http://crbug.com/96627 resolved.
[email protected]d8472d92011-08-26 20:15:201117 int64 largest_handle = largest_db_handle_in_history_;
1118 base::debug::Alias(&largest_handle);
[email protected]a896ce32012-01-09 22:04:071119 CHECK_96627(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:201120
[email protected]6d0146c2011-08-04 19:13:041121 AddDownloadItemToHistory(download, db_handle);
1122
1123 // Finalize this download if it finished before the history callback.
1124 if (!download->IsInProgress())
1125 SavePageDownloadFinished(download);
1126}
1127
[email protected]5656f8a2011-11-17 16:12:581128void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) {
[email protected]c09a8fd2011-11-21 19:54:501129 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) {
[email protected]2588ea9d2011-08-22 20:59:531130 delegate_->UpdateItemInPersistentStore(download);
[email protected]c09a8fd2011-11-21 19:54:501131 DCHECK(ContainsKey(save_page_downloads_, download->GetId()));
1132 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:041133
1134 if (download->IsComplete())
[email protected]ad50def52011-10-19 23:17:071135 content::NotificationService::current()->Notify(
[email protected]6d0146c2011-08-04 19:13:041136 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
[email protected]6c2381d2011-10-19 02:52:531137 content::Source<DownloadManager>(this),
1138 content::Details<DownloadItem>(download));
[email protected]6d0146c2011-08-04 19:13:041139 }
1140}
[email protected]da4a5582011-10-17 19:08:061141
[email protected]fc03de22011-12-06 23:28:121142void DownloadManagerImpl::DownloadOpened(DownloadItem* download) {
[email protected]da4a5582011-10-17 19:08:061143 delegate_->UpdateItemInPersistentStore(download);
1144 int num_unopened = 0;
1145 for (DownloadMap::iterator it = history_downloads_.begin();
1146 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:501147 if (it->second->IsComplete() && !it->second->GetOpened())
[email protected]da4a5582011-10-17 19:08:061148 ++num_unopened;
1149 }
1150 download_stats::RecordOpensOutstanding(num_unopened);
1151}
[email protected]5656f8a2011-11-17 16:12:581152
1153void DownloadManagerImpl::SetFileManager(DownloadFileManager* file_manager) {
1154 file_manager_ = file_manager;
1155}