blob: 77c30c030e83fb260cc3a3e46bd523080f96e013 [file] [log] [blame]
[email protected]f4f50ef2011-01-21 19:01:191// Copyright (c) 2011 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"
initial.commit09911bf2008-07-26 23:55:2911#include "base/file_util.h"
[email protected]503d03872011-05-06 08:36:2612#include "base/i18n/case_conversion.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/logging.h"
[email protected]7286e3fc2011-07-19 22:13:2414#include "base/stl_util.h"
[email protected]eda58402011-09-21 19:32:0215#include "base/stringprintf.h"
16#include "base/synchronization/lock.h"
17#include "base/sys_string_conversions.h"
[email protected]d2a8fb72010-01-21 05:31:4218#include "build/build_config.h"
[email protected]6d0c9fb2011-08-22 19:26:0319#include "content/browser/browser_context.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]c38831a12011-10-28 12:44:4931#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:4432#include "content/public/browser/content_browser_client.h"
[email protected]1bd0ef12011-10-20 05:24:1733#include "content/public/browser/download_manager_delegate.h"
[email protected]ad50def52011-10-19 23:17:0734#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1635#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3036#include "content/public/browser/render_process_host.h"
initial.commit09911bf2008-07-26 23:55:2937
[email protected]631bb742011-11-02 11:29:3938using content::BrowserThread;
39
[email protected]a0ce3282011-08-19 20:49:5240namespace {
41
[email protected]fabf36d22011-10-28 21:50:1742// Param structs exist because base::Bind can only handle 6 args.
43struct URLParams {
44 URLParams(const GURL& url, const GURL& referrer)
45 : url_(url), referrer_(referrer) {}
46 GURL url_;
47 GURL referrer_;
48};
49
50struct RenderParams {
51 RenderParams(int rpi, int rvi)
52 : render_process_id_(rpi), render_view_id_(rvi) {}
53 int render_process_id_;
54 int render_view_id_;
55};
56
57void BeginDownload(const URLParams& url_params,
58 const DownloadSaveInfo& save_info,
59 ResourceDispatcherHost* resource_dispatcher_host,
60 const RenderParams& render_params,
61 const content::ResourceContext* context) {
62 net::URLRequest* request = new net::URLRequest(url_params.url_,
63 resource_dispatcher_host);
64 request->set_referrer(url_params.referrer_.spec());
[email protected]c79a0c02011-08-22 22:37:3765 resource_dispatcher_host->BeginDownload(
[email protected]fabf36d22011-10-28 21:50:1766 request, save_info, true,
[email protected]8e3ae68c2011-09-16 22:15:4767 DownloadResourceHandler::OnStartedCallback(),
[email protected]fabf36d22011-10-28 21:50:1768 render_params.render_process_id_,
69 render_params.render_view_id_,
[email protected]c79a0c02011-08-22 22:37:3770 *context);
[email protected]a0ce3282011-08-19 20:49:5271}
72
73} // namespace
74
[email protected]5656f8a2011-11-17 16:12:5875DownloadManagerImpl::DownloadManagerImpl(
76 content::DownloadManagerDelegate* delegate,
77 DownloadIdFactory* id_factory,
78 DownloadStatusUpdater* status_updater)
79 : shutdown_needed_(false),
80 browser_context_(NULL),
81 file_manager_(NULL),
82 status_updater_((status_updater != NULL)
83 ? status_updater->AsWeakPtr()
84 : base::WeakPtr<DownloadStatusUpdater>()),
85 delegate_(delegate),
86 id_factory_(id_factory),
87 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) {
[email protected]eda58402011-09-21 19:32:0288 // NOTE(benjhayden): status_updater may be NULL when using
89 // TestingBrowserProcess.
90 if (status_updater_.get() != NULL)
[email protected]073ed7b2010-09-27 09:20:0291 status_updater_->AddDelegate(this);
initial.commit09911bf2008-07-26 23:55:2992}
93
[email protected]5656f8a2011-11-17 16:12:5894DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:1395 DCHECK(!shutdown_needed_);
[email protected]eda58402011-09-21 19:32:0296 if (status_updater_.get() != NULL)
[email protected]073ed7b2010-09-27 09:20:0297 status_updater_->RemoveDelegate(this);
initial.commit09911bf2008-07-26 23:55:2998}
99
[email protected]5656f8a2011-11-17 16:12:58100DownloadId DownloadManagerImpl::GetNextId() {
[email protected]2909e342011-10-29 00:46:53101 return id_factory_->GetNextId();
102}
103
[email protected]5656f8a2011-11-17 16:12:58104void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50105 VLOG(20) << __FUNCTION__ << "()"
106 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13107 if (!shutdown_needed_)
108 return;
109 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29110
[email protected]326a6a92010-09-10 20:21:13111 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
[email protected]fb4f8d902011-09-16 06:07:08112 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13113
114 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:18115 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]fabf36d22011-10-28 21:50:17116 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
117 file_manager_, make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:13118 }
initial.commit09911bf2008-07-26 23:55:29119
[email protected]f04182f32010-12-10 19:12:07120 AssertContainersConsistent();
121
122 // Go through all downloads in downloads_. Dangerous ones we need to
123 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:09124 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:07125 DownloadItem* download = *it;
126
127 // Save iterator from potential erases in this set done by called code.
128 // Iterators after an erasure point are still valid for lists and
129 // associative containers such as sets.
130 it++;
131
[email protected]c09a8fd2011-11-21 19:54:50132 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29133 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07134 // The user hasn't accepted it, so we need to remove it
135 // from the disk. This may or may not result in it being
136 // removed from the DownloadManager queues and deleted
137 // (specifically, DownloadManager::RemoveDownload only
138 // removes and deletes it if it's known to the history service)
139 // so the only thing we know after calling this function is that
140 // the download was deleted if-and-only-if it was removed
141 // from all queues.
[email protected]303077002011-04-19 23:21:01142 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26143 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17144 download->Cancel(false);
145 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29146 }
147 }
148
[email protected]f04182f32010-12-10 19:12:07149 // At this point, all dangerous downloads have had their files removed
150 // and all in progress downloads have been cancelled. We can now delete
151 // anything left.
[email protected]9ccbb372008-10-10 18:50:32152
[email protected]5cd11b6e2011-06-10 20:30:59153 // Copy downloads_ to separate container so as not to set off checks
154 // in DownloadItem destruction.
155 DownloadSet downloads_to_delete;
156 downloads_to_delete.swap(downloads_);
157
initial.commit09911bf2008-07-26 23:55:29158 in_progress_.clear();
[email protected]70850c72011-01-11 17:31:27159 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59160 history_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59161 STLDeleteElements(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29162
[email protected]6d0146c2011-08-04 19:13:04163 DCHECK(save_page_downloads_.empty());
164
initial.commit09911bf2008-07-26 23:55:29165 file_manager_ = NULL;
[email protected]2588ea9d2011-08-22 20:59:53166 delegate_->Shutdown();
[email protected]82f37b02010-07-29 22:04:57167
initial.commit09911bf2008-07-26 23:55:29168 shutdown_needed_ = false;
169}
170
[email protected]5656f8a2011-11-17 16:12:58171void DownloadManagerImpl::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04172 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57173 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58174
[email protected]f04182f32010-12-10 19:12:07175 for (DownloadMap::iterator it = history_downloads_.begin();
176 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50177 if (it->second->IsTemporary() &&
178 it->second->GetFullPath().DirName() == dir_path)
[email protected]82f37b02010-07-29 22:04:57179 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58180 }
[email protected]6aa4a1c02010-01-15 18:49:58181}
182
[email protected]5656f8a2011-11-17 16:12:58183void DownloadManagerImpl::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04184 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57185 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34186
[email protected]f04182f32010-12-10 19:12:07187 for (DownloadMap::iterator it = history_downloads_.begin();
188 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50189 if (!it->second->IsTemporary() &&
190 (dir_path.empty() || it->second->GetFullPath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57191 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34192 }
[email protected]8ddbd66a2010-05-21 16:38:34193}
194
[email protected]5656f8a2011-11-17 16:12:58195void DownloadManagerImpl::SearchDownloads(const string16& query,
196 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26197 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42198
[email protected]f04182f32010-12-10 19:12:07199 for (DownloadMap::iterator it = history_downloads_.begin();
200 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42201 DownloadItem* download_item = it->second;
202
[email protected]c09a8fd2011-11-21 19:54:50203 if (download_item->IsTemporary())
[email protected]d3b12902010-08-16 23:39:42204 continue;
205
206 // Display Incognito downloads only in Incognito window, and vice versa.
207 // The Incognito Downloads page will get the list of non-Incognito downloads
208 // from its parent profile.
[email protected]c09a8fd2011-11-21 19:54:50209 if (browser_context_->IsOffTheRecord() != download_item->IsOtr())
[email protected]d3b12902010-08-16 23:39:42210 continue;
211
212 if (download_item->MatchesQuery(query_lower))
213 result->push_back(download_item);
214 }
[email protected]d3b12902010-08-16 23:39:42215}
216
initial.commit09911bf2008-07-26 23:55:29217// Query the history service for information about all persisted downloads.
[email protected]5656f8a2011-11-17 16:12:58218bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03219 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29220 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
221 shutdown_needed_ = true;
222
[email protected]6d0c9fb2011-08-22 19:26:03223 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46224
[email protected]2941c2392010-07-15 22:54:30225 // In test mode, there may be no ResourceDispatcherHost. In this case it's
226 // safe to avoid setting |file_manager_| because we only call a small set of
227 // functions, none of which need it.
[email protected]a0ce3282011-08-19 20:49:52228 ResourceDispatcherHost* rdh =
229 content::GetContentClient()->browser()->GetResourceDispatcherHost();
[email protected]2941c2392010-07-15 22:54:30230 if (rdh) {
231 file_manager_ = rdh->download_file_manager();
232 DCHECK(file_manager_);
initial.commit09911bf2008-07-26 23:55:29233 }
234
initial.commit09911bf2008-07-26 23:55:29235 return true;
236}
237
[email protected]aa9881c2011-08-15 18:01:12238// We have received a message from DownloadFileManager about a new download.
[email protected]5656f8a2011-11-17 16:12:58239void DownloadManagerImpl::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35241
[email protected]aa9881c2011-08-15 18:01:12242 if (delegate_->ShouldStartDownload(download_id))
243 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35244}
245
[email protected]5656f8a2011-11-17 16:12:58246void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
248 for (DownloadMap::iterator it = history_downloads_.begin();
249 it != history_downloads_.end(); ++it) {
250 CheckForFileRemoval(it->second);
251 }
252}
253
[email protected]5656f8a2011-11-17 16:12:58254void DownloadManagerImpl::CheckForFileRemoval(DownloadItem* download_item) {
[email protected]9fc114672011-06-15 08:17:48255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
256 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50257 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48258 BrowserThread::PostTask(
259 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58260 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]c09a8fd2011-11-21 19:54:50261 this, download_item->GetDbHandle(),
[email protected]fabf36d22011-10-28 21:50:17262 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48263 }
264}
265
[email protected]5656f8a2011-11-17 16:12:58266void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]9fc114672011-06-15 08:17:48267 int64 db_handle, const FilePath& path) {
268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
269 if (!file_util::PathExists(path)) {
270 BrowserThread::PostTask(
271 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58272 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
273 this,
274 db_handle));
[email protected]9fc114672011-06-15 08:17:48275 }
276}
277
[email protected]5656f8a2011-11-17 16:12:58278void DownloadManagerImpl::OnFileRemovalDetected(int64 db_handle) {
[email protected]9fc114672011-06-15 08:17:48279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
280 DownloadMap::iterator it = history_downloads_.find(db_handle);
281 if (it != history_downloads_.end()) {
282 DownloadItem* download_item = it->second;
283 download_item->OnDownloadedFileRemoved();
284 }
285}
286
[email protected]5656f8a2011-11-17 16:12:58287void DownloadManagerImpl::RestartDownload(
[email protected]aa9881c2011-08-15 18:01:12288 int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29290
[email protected]4cd82f72011-05-23 19:15:01291 DownloadItem* download = GetActiveDownloadItem(download_id);
292 if (!download)
293 return;
294
295 VLOG(20) << __FUNCTION__ << "()"
296 << " download = " << download->DebugString(true);
297
[email protected]c09a8fd2011-11-21 19:54:50298 FilePath suggested_path = download->GetSuggestedPath();
[email protected]4cd82f72011-05-23 19:15:01299
[email protected]c09a8fd2011-11-21 19:54:50300 if (download->PromptUserForSaveLocation()) {
initial.commit09911bf2008-07-26 23:55:29301 // We must ask the user for the place to put the download.
[email protected]ae77da82011-11-01 19:17:29302 TabContents* contents = download->GetTabContents();
[email protected]99cb7f82011-07-28 17:27:26303
[email protected]4cd82f72011-05-23 19:15:01304 // |id_ptr| will be deleted in either FileSelected() or
[email protected]93af2272011-09-21 18:29:17305 // FileSelectionCancelled().
[email protected]4cd82f72011-05-23 19:15:01306 int32* id_ptr = new int32;
307 *id_ptr = download_id;
[email protected]99cb7f82011-07-28 17:27:26308
[email protected]da1a27b2011-07-29 23:16:33309 delegate_->ChooseDownloadPath(
[email protected]aa9881c2011-08-15 18:01:12310 contents, suggested_path, reinterpret_cast<void*>(id_ptr));
[email protected]99cb7f82011-07-28 17:27:26311
[email protected]f5920322011-03-24 20:34:16312 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]fed38252011-07-08 17:26:50313 SelectFileDialogDisplayed(download_id));
initial.commit09911bf2008-07-26 23:55:29314 } else {
315 // No prompting for download, just continue with the suggested name.
[email protected]4cd82f72011-05-23 19:15:01316 ContinueDownloadWithPath(download, suggested_path);
initial.commit09911bf2008-07-26 23:55:29317 }
318}
319
[email protected]5656f8a2011-11-17 16:12:58320content::BrowserContext* DownloadManagerImpl::BrowserContext() {
321 return browser_context_;
322}
323
324FilePath DownloadManagerImpl::LastDownloadPath() {
325 return last_download_path_;
326}
327
328void DownloadManagerImpl::CreateDownloadItem(
[email protected]594e66fe2011-10-25 22:49:41329 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) {
[email protected]c2e76012010-12-23 21:10:29330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
331
[email protected]c09a8fd2011-11-21 19:54:50332 DownloadItem* download = new DownloadItemImpl(
[email protected]ae77da82011-11-01 19:17:29333 this, *info, new DownloadRequestHandle(request_handle),
334 browser_context_->IsOffTheRecord());
[email protected]2909e342011-10-29 00:46:53335 int32 download_id = info->download_id.local();
[email protected]4cd82f72011-05-23 19:15:01336 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]d8472d92011-08-26 20:15:20337
338 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]821960a2011-08-23 20:40:03339 CHECK(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29340 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01341 active_downloads_[download_id] = download;
[email protected]c2e76012010-12-23 21:10:29342}
343
[email protected]5656f8a2011-11-17 16:12:58344void DownloadManagerImpl::ContinueDownloadWithPath(
345 DownloadItem* download, const FilePath& chosen_file) {
[email protected]ca4b5fa32010-10-09 12:42:18346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01347 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39348
[email protected]c09a8fd2011-11-21 19:54:50349 int32 download_id = download->GetId();
initial.commit09911bf2008-07-26 23:55:29350
[email protected]70850c72011-01-11 17:31:27351 // NOTE(ahendrickson) Eventually |active_downloads_| will replace
352 // |in_progress_|, but we don't want to change the semantics yet.
[email protected]4cd82f72011-05-23 19:15:01353 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]70850c72011-01-11 17:31:27354 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01355 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27356
[email protected]4cd82f72011-05-23 19:15:01357 // Make sure the initial file name is set only once.
[email protected]4cd82f72011-05-23 19:15:01358 download->OnPathDetermined(chosen_file);
[email protected]4cd82f72011-05-23 19:15:01359
360 VLOG(20) << __FUNCTION__ << "()"
361 << " download = " << download->DebugString(true);
362
363 in_progress_[download_id] = download;
[email protected]5f8589fe2011-08-17 20:58:39364 UpdateDownloadProgress(); // Reflect entry into in_progress_.
initial.commit09911bf2008-07-26 23:55:29365
[email protected]adb2f3d12011-01-23 16:24:54366 // Rename to intermediate name.
[email protected]f5920322011-03-24 20:34:16367 FilePath download_path;
[email protected]ec865262011-08-23 20:01:48368 if (!delegate_->OverrideIntermediatePath(download, &download_path))
[email protected]c09a8fd2011-11-21 19:54:50369 download_path = download->GetFullPath();
[email protected]594cd7d2010-07-21 03:23:56370
[email protected]f5920322011-03-24 20:34:16371 BrowserThread::PostTask(
372 BrowserThread::FILE, FROM_HERE,
[email protected]fabf36d22011-10-28 21:50:17373 base::Bind(&DownloadFileManager::RenameInProgressDownloadFile,
[email protected]c09a8fd2011-11-21 19:54:50374 file_manager_, download->GetGlobalId(), download_path));
[email protected]f5920322011-03-24 20:34:16375
376 download->Rename(download_path);
377
[email protected]2588ea9d2011-08-22 20:59:53378 delegate_->AddItemToPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29379}
380
[email protected]e384fd82011-11-30 06:37:20381void DownloadManagerImpl::UpdateDownload(int32 download_id, int64 bytes_so_far,
382 int64 bytes_per_sec) {
[email protected]70850c72011-01-11 17:31:27383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
384 DownloadMap::iterator it = active_downloads_.find(download_id);
385 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29386 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26387 if (download->IsInProgress()) {
[email protected]e384fd82011-11-30 06:37:20388 download->UpdateProgress(bytes_so_far, bytes_per_sec);
[email protected]5f8589fe2011-08-17 20:58:39389 UpdateDownloadProgress(); // Reflect size updates.
[email protected]2588ea9d2011-08-22 20:59:53390 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27391 }
initial.commit09911bf2008-07-26 23:55:29392 }
393}
394
[email protected]5656f8a2011-11-17 16:12:58395void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
396 int64 size,
397 const std::string& hash) {
[email protected]33c6d3f12011-09-04 00:00:54398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]da6e3922010-11-24 21:45:50399 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
400 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32402
[email protected]c4f02c42011-01-24 21:55:06403 // If it's not in active_downloads_, that means it was cancelled; just
404 // ignore the notification.
405 if (active_downloads_.count(download_id) == 0)
406 return;
407
[email protected]adb2f3d12011-01-23 16:24:54408 DownloadItem* download = active_downloads_[download_id];
[email protected]ac4af82f2011-11-10 19:09:37409 download->OnAllDataSaved(size, hash);
410 delegate_->OnResponseCompleted(download);
[email protected]b09f1282011-09-14 00:37:45411
[email protected]adb2f3d12011-01-23 16:24:54412 MaybeCompleteDownload(download);
413}
[email protected]9ccbb372008-10-10 18:50:32414
[email protected]5656f8a2011-11-17 16:12:58415void DownloadManagerImpl::AssertQueueStateConsistent(DownloadItem* download) {
[email protected]5cd11b6e2011-06-10 20:30:59416 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
[email protected]c09a8fd2011-11-21 19:54:50417 if (download->GetState() == DownloadItem::REMOVING) {
[email protected]7d413112011-06-16 18:50:17418 CHECK(!ContainsKey(downloads_, download));
[email protected]c09a8fd2011-11-21 19:54:50419 CHECK(!ContainsKey(active_downloads_, download->GetId()));
420 CHECK(!ContainsKey(in_progress_, download->GetId()));
421 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17422 return;
423 }
424
425 // Should be in downloads_ if we're not REMOVING.
426 CHECK(ContainsKey(downloads_, download));
427
428 // Check history_downloads_ consistency.
[email protected]c09a8fd2011-11-21 19:54:50429 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) {
430 CHECK(ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17431 } else {
432 // TODO(rdsmith): Somewhat painful; make sure to disable in
433 // release builds after resolution of http://crbug.com/85408.
434 for (DownloadMap::iterator it = history_downloads_.begin();
435 it != history_downloads_.end(); ++it) {
436 CHECK(it->second != download);
437 }
438 }
439
[email protected]c09a8fd2011-11-21 19:54:50440 int64 state = download->GetState();
[email protected]61b75a52011-08-29 16:34:34441 base::debug::Alias(&state);
[email protected]c09a8fd2011-11-21 19:54:50442 if (ContainsKey(active_downloads_, download->GetId())) {
443 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle)
444 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
445 if (DownloadItem::IN_PROGRESS != download->GetState())
446 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle());
[email protected]f9a2997f2011-09-23 16:54:07447 }
[email protected]c09a8fd2011-11-21 19:54:50448 if (DownloadItem::IN_PROGRESS == download->GetState())
449 CHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]5cd11b6e2011-06-10 20:30:59450}
451
[email protected]5656f8a2011-11-17 16:12:58452bool DownloadManagerImpl::IsDownloadReadyForCompletion(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54453 // If we don't have all the data, the download is not ready for
454 // completion.
[email protected]c09a8fd2011-11-21 19:54:50455 if (!download->AllDataSaved())
[email protected]adb2f3d12011-01-23 16:24:54456 return false;
[email protected]6a7fb042010-02-01 16:30:47457
[email protected]9d7ef802011-02-25 19:03:35458 // If the download is dangerous, but not yet validated, it's not ready for
459 // completion.
[email protected]c09a8fd2011-11-21 19:54:50460 if (download->GetSafetyState() == DownloadItem::DANGEROUS)
[email protected]9d7ef802011-02-25 19:03:35461 return false;
462
[email protected]adb2f3d12011-01-23 16:24:54463 // If the download isn't active (e.g. has been cancelled) it's not
464 // ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50465 if (active_downloads_.count(download->GetId()) == 0)
[email protected]adb2f3d12011-01-23 16:24:54466 return false;
467
468 // If the download hasn't been inserted into the history system
469 // (which occurs strictly after file name determination, intermediate
470 // file rename, and UI display) then it's not ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50471 if (download->GetDbHandle() == DownloadItem::kUninitializedHandle)
[email protected]7054b592011-06-22 14:46:42472 return false;
473
474 return true;
[email protected]adb2f3d12011-01-23 16:24:54475}
476
[email protected]5656f8a2011-11-17 16:12:58477void DownloadManagerImpl::MaybeCompleteDownload(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
479 VLOG(20) << __FUNCTION__ << "()" << " download = "
480 << download->DebugString(false);
481
482 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32483 return;
[email protected]9ccbb372008-10-10 18:50:32484
[email protected]adb2f3d12011-01-23 16:24:54485 // TODO(rdsmith): DCHECK that we only pass through this point
486 // once per download. The natural way to do this is by a state
487 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56488
[email protected]adb2f3d12011-01-23 16:24:54489 // Confirm we're in the proper set of states to be here;
[email protected]9d7ef802011-02-25 19:03:35490 // in in_progress_, have all data, have a history handle, (validated or safe).
[email protected]c09a8fd2011-11-21 19:54:50491 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState());
492 DCHECK_EQ(1u, in_progress_.count(download->GetId()));
493 DCHECK(download->AllDataSaved());
494 DCHECK(download->GetDbHandle() != DownloadItem::kUninitializedHandle);
495 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle()));
[email protected]adb2f3d12011-01-23 16:24:54496
[email protected]c2918652011-11-01 18:50:23497 // Give the delegate a chance to override.
498 if (!delegate_->ShouldCompleteDownload(download))
499 return;
500
[email protected]adb2f3d12011-01-23 16:24:54501 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
502 << download->DebugString(false);
503
504 // Remove the id from in_progress
[email protected]c09a8fd2011-11-21 19:54:50505 in_progress_.erase(download->GetId());
[email protected]5f8589fe2011-08-17 20:58:39506 UpdateDownloadProgress(); // Reflect removal from in_progress_.
[email protected]adb2f3d12011-01-23 16:24:54507
[email protected]2588ea9d2011-08-22 20:59:53508 delegate_->UpdateItemInPersistentStore(download);
[email protected]adb2f3d12011-01-23 16:24:54509
[email protected]f5920322011-03-24 20:34:16510 // Finish the download.
[email protected]48837962011-04-19 17:03:29511 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32512}
513
[email protected]5656f8a2011-11-17 16:12:58514void DownloadManagerImpl::DownloadCompleted(int32 download_id) {
[email protected]70850c72011-01-11 17:31:27515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21516 DownloadItem* download = GetDownloadItem(download_id);
517 DCHECK(download);
[email protected]2588ea9d2011-08-22 20:59:53518 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27519 active_downloads_.erase(download_id);
[email protected]821960a2011-08-23 20:40:03520 AssertQueueStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27521}
522
[email protected]5656f8a2011-11-17 16:12:58523void DownloadManagerImpl::OnDownloadRenamedToFinalName(
524 int download_id,
525 const FilePath& full_path,
526 int uniquifier) {
[email protected]da6e3922010-11-24 21:45:50527 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
[email protected]f5920322011-03-24 20:34:16528 << " full_path = \"" << full_path.value() << "\""
529 << " uniquifier = " << uniquifier;
[email protected]ca4b5fa32010-10-09 12:42:18530 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f5920322011-03-24 20:34:16531
[email protected]2e030682010-07-23 19:45:36532 DownloadItem* item = GetDownloadItem(download_id);
533 if (!item)
534 return;
[email protected]6cade212008-12-03 00:32:22535
[email protected]c09a8fd2011-11-21 19:54:50536 if (item->GetSafetyState() == DownloadItem::SAFE) {
[email protected]8fa1eeb52011-04-13 14:18:02537 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice";
538 }
539
[email protected]fabf36d22011-10-28 21:50:17540 BrowserThread::PostTask(
541 BrowserThread::FILE, FROM_HERE,
542 base::Bind(&DownloadFileManager::CompleteDownload,
[email protected]c09a8fd2011-11-21 19:54:50543 file_manager_, item->GetGlobalId()));
[email protected]9ccbb372008-10-10 18:50:32544
[email protected]f5920322011-03-24 20:34:16545 if (uniquifier)
[email protected]c09a8fd2011-11-21 19:54:50546 item->SetPathUniquifier(uniquifier);
[email protected]9ccbb372008-10-10 18:50:32547
[email protected]f5920322011-03-24 20:34:16548 item->OnDownloadRenamedToFinalName(full_path);
[email protected]2588ea9d2011-08-22 20:59:53549 delegate_->UpdatePathForItemInPersistentStore(item, full_path);
initial.commit09911bf2008-07-26 23:55:29550}
551
[email protected]5656f8a2011-11-17 16:12:58552void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]93af2272011-09-21 18:29:17553 DownloadItem* download = GetActiveDownload(download_id);
554 // A cancel at the right time could remove the download from the
555 // |active_downloads_| map before we get here.
556 if (!download)
557 return;
558
559 download->Cancel(true);
560}
561
[email protected]5656f8a2011-11-17 16:12:58562void DownloadManagerImpl::DownloadCancelledInternal(DownloadItem* download) {
[email protected]d8472d92011-08-26 20:15:20563 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20564
565 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50566 << " download = " << download->DebugString(true);
567
[email protected]93af2272011-09-21 18:29:17568 RemoveFromActiveList(download);
[email protected]47a881b2011-08-29 22:59:21569 // This function is called from the DownloadItem, so DI state
570 // should already have been updated.
571 AssertQueueStateConsistent(download);
initial.commit09911bf2008-07-26 23:55:29572
[email protected]15d90ba2011-11-03 03:41:55573 if (file_manager_)
574 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29575}
576
[email protected]5656f8a2011-11-17 16:12:58577void DownloadManagerImpl::OnDownloadInterrupted(int32 download_id,
578 int64 size,
579 InterruptReason reason) {
[email protected]47a881b2011-08-29 22:59:21580 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
581
582 DownloadItem* download = GetActiveDownload(download_id);
583 if (!download)
584 return;
585
[email protected]be76b7e2011-10-13 12:57:57586 VLOG(20) << __FUNCTION__ << "()"
587 << " reason " << InterruptReasonDebugString(reason)
[email protected]c09a8fd2011-11-21 19:54:50588 << " at offset " << download->GetReceivedBytes()
[email protected]47a881b2011-08-29 22:59:21589 << " size = " << size
590 << " download = " << download->DebugString(true);
591
[email protected]93af2272011-09-21 18:29:17592 RemoveFromActiveList(download);
[email protected]be76b7e2011-10-13 12:57:57593 download->Interrupted(size, reason);
[email protected]93af2272011-09-21 18:29:17594 download->OffThreadCancel(file_manager_);
[email protected]47a881b2011-08-29 22:59:21595}
596
[email protected]5656f8a2011-11-17 16:12:58597DownloadItem* DownloadManagerImpl::GetActiveDownload(int32 download_id) {
[email protected]bf68a00b2011-04-07 17:28:26598 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
599 DownloadMap::iterator it = active_downloads_.find(download_id);
[email protected]bf68a00b2011-04-07 17:28:26600 if (it == active_downloads_.end())
[email protected]47a881b2011-08-29 22:59:21601 return NULL;
[email protected]bf68a00b2011-04-07 17:28:26602
603 DownloadItem* download = it->second;
604
[email protected]47a881b2011-08-29 22:59:21605 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50606 DCHECK_EQ(download_id, download->GetId());
[email protected]4cd82f72011-05-23 19:15:01607
[email protected]47a881b2011-08-29 22:59:21608 return download;
609}
[email protected]54610672011-07-18 18:24:43610
[email protected]5656f8a2011-11-17 16:12:58611void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) {
[email protected]93af2272011-09-21 18:29:17612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
613 DCHECK(download);
614
615 // Clean up will happen when the history system create callback runs if we
616 // don't have a valid db_handle yet.
[email protected]c09a8fd2011-11-21 19:54:50617 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) {
618 in_progress_.erase(download->GetId());
619 active_downloads_.erase(download->GetId());
[email protected]93af2272011-09-21 18:29:17620 UpdateDownloadProgress(); // Reflect removal from in_progress_.
621 delegate_->UpdateItemInPersistentStore(download);
622 }
623}
624
[email protected]5656f8a2011-11-17 16:12:58625content::DownloadManagerDelegate* DownloadManagerImpl::delegate() const {
626 return delegate_;
627}
628
629void DownloadManagerImpl::SetDownloadManagerDelegate(
[email protected]1bd0ef12011-10-20 05:24:17630 content::DownloadManagerDelegate* delegate) {
[email protected]9bb54ee2011-10-12 17:43:35631 delegate_ = delegate;
632}
633
[email protected]5656f8a2011-11-17 16:12:58634void DownloadManagerImpl::UpdateDownloadProgress() {
[email protected]5f8589fe2011-08-17 20:58:39635 delegate_->DownloadProgressUpdated();
[email protected]6a7fb042010-02-01 16:30:47636}
637
[email protected]5656f8a2011-11-17 16:12:58638int DownloadManagerImpl::RemoveDownloadItems(
[email protected]6d0146c2011-08-04 19:13:04639 const DownloadVector& pending_deletes) {
640 if (pending_deletes.empty())
641 return 0;
642
643 // Delete from internal maps.
644 for (DownloadVector::const_iterator it = pending_deletes.begin();
645 it != pending_deletes.end();
646 ++it) {
647 DownloadItem* download = *it;
648 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50649 history_downloads_.erase(download->GetDbHandle());
650 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:04651 downloads_.erase(download);
652 }
653
654 // Tell observers to refresh their views.
655 NotifyModelChanged();
656
657 // Delete the download items themselves.
658 const int num_deleted = static_cast<int>(pending_deletes.size());
659 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
660 return num_deleted;
661}
662
[email protected]5656f8a2011-11-17 16:12:58663void DownloadManagerImpl::RemoveDownload(int64 download_handle) {
[email protected]93af2272011-09-21 18:29:17664 DownloadMap::iterator it = history_downloads_.find(download_handle);
665 if (it == history_downloads_.end())
666 return;
667
668 // Make history update.
669 DownloadItem* download = it->second;
670 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29671
672 // Remove from our tables and delete.
[email protected]6d0146c2011-08-04 19:13:04673 int downloads_count = RemoveDownloadItems(DownloadVector(1, download));
[email protected]f04182f32010-12-10 19:12:07674 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29675}
676
[email protected]5656f8a2011-11-17 16:12:58677int DownloadManagerImpl::RemoveDownloadsBetween(const base::Time remove_begin,
678 const base::Time remove_end) {
[email protected]2588ea9d2011-08-22 20:59:53679 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29680
[email protected]a312a442010-12-15 23:40:33681 // All downloads visible to the user will be in the history,
682 // so scan that map.
[email protected]6d0146c2011-08-04 19:13:04683 DownloadVector pending_deletes;
684 for (DownloadMap::const_iterator it = history_downloads_.begin();
685 it != history_downloads_.end();
686 ++it) {
initial.commit09911bf2008-07-26 23:55:29687 DownloadItem* download = it->second;
[email protected]c09a8fd2011-11-21 19:54:50688 if (download->GetStartTime() >= remove_begin &&
689 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04690 (download->IsComplete() || download->IsCancelled())) {
[email protected]7d413112011-06-16 18:50:17691 AssertQueueStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28692 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29693 }
initial.commit09911bf2008-07-26 23:55:29694 }
[email protected]6d0146c2011-08-04 19:13:04695 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29696}
697
[email protected]5656f8a2011-11-17 16:12:58698int DownloadManagerImpl::RemoveDownloads(const base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59699 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29700}
701
[email protected]5656f8a2011-11-17 16:12:58702int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]da4a5582011-10-17 19:08:06703 download_stats::RecordClearAllSize(history_downloads_.size());
[email protected]d41355e6f2009-04-07 21:21:12704 // The null times make the date range unbounded.
705 return RemoveDownloadsBetween(base::Time(), base::Time());
706}
707
initial.commit09911bf2008-07-26 23:55:29708// Initiate a download of a specific URL. We send the request to the
709// ResourceDispatcherHost, and let it send us responses like a regular
710// download.
[email protected]5656f8a2011-11-17 16:12:58711void DownloadManagerImpl::DownloadUrl(const GURL& url,
712 const GURL& referrer,
713 const std::string& referrer_charset,
714 TabContents* tab_contents) {
[email protected]ae8945192010-07-20 16:56:26715 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
716 tab_contents);
[email protected]6aa4a1c02010-01-15 18:49:58717}
718
[email protected]5656f8a2011-11-17 16:12:58719void DownloadManagerImpl::DownloadUrlToFile(const GURL& url,
720 const GURL& referrer,
721 const std::string& referrer_charset,
722 const DownloadSaveInfo& save_info,
723 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:34724 DCHECK(tab_contents);
[email protected]c79a0c02011-08-22 22:37:37725 ResourceDispatcherHost* resource_dispatcher_host =
726 content::GetContentClient()->browser()->GetResourceDispatcherHost();
[email protected]ed24fad2011-05-10 22:44:01727 // We send a pointer to content::ResourceContext, instead of the usual
728 // reference, so that a copy of the object isn't made.
[email protected]fabf36d22011-10-28 21:50:17729 // base::Bind can't handle 7 args, so we use URLParams and RenderParams.
730 BrowserThread::PostTask(
731 BrowserThread::IO, FROM_HERE,
732 base::Bind(&BeginDownload,
733 URLParams(url, referrer), save_info, resource_dispatcher_host,
[email protected]f3b1a082011-11-18 00:34:30734 RenderParams(tab_contents->GetRenderProcessHost()->GetID(),
[email protected]fabf36d22011-10-28 21:50:17735 tab_contents->render_view_host()->routing_id()),
736 &tab_contents->browser_context()->GetResourceContext()));
initial.commit09911bf2008-07-26 23:55:29737}
738
[email protected]5656f8a2011-11-17 16:12:58739void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29740 observers_.AddObserver(observer);
741 observer->ModelChanged();
742}
743
[email protected]5656f8a2011-11-17 16:12:58744void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29745 observers_.RemoveObserver(observer);
746}
747
[email protected]5656f8a2011-11-17 16:12:58748bool DownloadManagerImpl::IsDownloadProgressKnown() const {
[email protected]45f432e942011-10-25 18:17:22749 for (DownloadMap::const_iterator i = in_progress_.begin();
[email protected]073ed7b2010-09-27 09:20:02750 i != in_progress_.end(); ++i) {
[email protected]c09a8fd2011-11-21 19:54:50751 if (i->second->GetTotalBytes() <= 0)
[email protected]073ed7b2010-09-27 09:20:02752 return false;
753 }
754
755 return true;
756}
757
[email protected]5656f8a2011-11-17 16:12:58758int64 DownloadManagerImpl::GetInProgressDownloadCount() const {
[email protected]073ed7b2010-09-27 09:20:02759 return in_progress_.size();
760}
761
[email protected]5656f8a2011-11-17 16:12:58762int64 DownloadManagerImpl::GetReceivedDownloadBytes() const {
[email protected]073ed7b2010-09-27 09:20:02763 DCHECK(IsDownloadProgressKnown());
764 int64 received_bytes = 0;
[email protected]45f432e942011-10-25 18:17:22765 for (DownloadMap::const_iterator i = in_progress_.begin();
[email protected]073ed7b2010-09-27 09:20:02766 i != in_progress_.end(); ++i) {
[email protected]c09a8fd2011-11-21 19:54:50767 received_bytes += i->second->GetReceivedBytes();
[email protected]073ed7b2010-09-27 09:20:02768 }
769 return received_bytes;
770}
771
[email protected]5656f8a2011-11-17 16:12:58772int64 DownloadManagerImpl::GetTotalDownloadBytes() const {
[email protected]073ed7b2010-09-27 09:20:02773 DCHECK(IsDownloadProgressKnown());
774 int64 total_bytes = 0;
[email protected]45f432e942011-10-25 18:17:22775 for (DownloadMap::const_iterator i = in_progress_.begin();
[email protected]073ed7b2010-09-27 09:20:02776 i != in_progress_.end(); ++i) {
[email protected]c09a8fd2011-11-21 19:54:50777 total_bytes += i->second->GetTotalBytes();
[email protected]073ed7b2010-09-27 09:20:02778 }
779 return total_bytes;
780}
781
[email protected]5656f8a2011-11-17 16:12:58782void DownloadManagerImpl::FileSelected(const FilePath& path, void* params) {
[email protected]4cd82f72011-05-23 19:15:01783 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
784
785 int32* id_ptr = reinterpret_cast<int32*>(params);
786 DCHECK(id_ptr != NULL);
787 int32 download_id = *id_ptr;
788 delete id_ptr;
789
790 DownloadItem* download = GetActiveDownloadItem(download_id);
791 if (!download)
792 return;
793 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
794 << " download = " << download->DebugString(true);
795
[email protected]c09a8fd2011-11-21 19:54:50796 if (download->PromptUserForSaveLocation())
[email protected]7ae7c2cb2009-01-06 23:31:41797 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35798
[email protected]4cd82f72011-05-23 19:15:01799 // Make sure the initial file name is set only once.
800 ContinueDownloadWithPath(download, path);
initial.commit09911bf2008-07-26 23:55:29801}
802
[email protected]5656f8a2011-11-17 16:12:58803void DownloadManagerImpl::FileSelectionCanceled(void* params) {
initial.commit09911bf2008-07-26 23:55:29804 // The user didn't pick a place to save the file, so need to cancel the
805 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01806 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
807 int32* id_ptr = reinterpret_cast<int32*>(params);
808 DCHECK(id_ptr != NULL);
809 int32 download_id = *id_ptr;
810 delete id_ptr;
811
812 DownloadItem* download = GetActiveDownloadItem(download_id);
813 if (!download)
814 return;
815
816 VLOG(20) << __FUNCTION__ << "()"
817 << " download = " << download->DebugString(true);
818
[email protected]93af2272011-09-21 18:29:17819 // TODO(ahendrickson) -- This currently has no effect, as the download is
820 // not put on the active list until the file selection is complete. Need
821 // to put it on the active list earlier in the process.
822 RemoveFromActiveList(download);
823
824 download->OffThreadCancel(file_manager_);
[email protected]4cd82f72011-05-23 19:15:01825}
826
initial.commit09911bf2008-07-26 23:55:29827// Operations posted to us from the history service ----------------------------
828
829// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25830// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]5656f8a2011-11-17 16:12:58831void DownloadManagerImpl::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25832 std::vector<DownloadPersistentStoreInfo>* entries) {
[email protected]d8472d92011-08-26 20:15:20833 // TODO(rdsmith): Remove this and related logic when
[email protected]639ddad2011-10-05 02:53:47834 // http://crbug.com/85408 is fixed.
[email protected]d8472d92011-08-26 20:15:20835 largest_db_handle_in_history_ = 0;
836
initial.commit09911bf2008-07-26 23:55:29837 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]c09a8fd2011-11-21 19:54:50838 DownloadItem* download = new DownloadItemImpl(this, entries->at(i));
[email protected]d8472d92011-08-26 20:15:20839 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]c09a8fd2011-11-21 19:54:50840 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]f04182f32010-12-10 19:12:07841 downloads_.insert(download);
[email protected]c09a8fd2011-11-21 19:54:50842 history_downloads_[download->GetDbHandle()] = download;
[email protected]da6e3922010-11-24 21:45:50843 VLOG(20) << __FUNCTION__ << "()" << i << ">"
844 << " download = " << download->DebugString(true);
[email protected]d8472d92011-08-26 20:15:20845
[email protected]c09a8fd2011-11-21 19:54:50846 if (download->GetDbHandle() > largest_db_handle_in_history_)
847 largest_db_handle_in_history_ = download->GetDbHandle();
initial.commit09911bf2008-07-26 23:55:29848 }
[email protected]b0ab1d42010-02-24 19:29:28849 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48850 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29851}
852
[email protected]5656f8a2011-11-17 16:12:58853void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItem* download,
854 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27855 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d2a8fb72010-01-21 05:31:42856
[email protected]639ddad2011-10-05 02:53:47857 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/85408
[email protected]1e9fe7ff2011-06-24 17:37:33858 // is fixed.
[email protected]2588ea9d2011-08-22 20:59:53859 CHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]1e9fe7ff2011-06-24 17:37:33860
[email protected]da4a5582011-10-17 19:08:06861 download_stats::RecordHistorySize(history_downloads_.size());
862
[email protected]c09a8fd2011-11-21 19:54:50863 DCHECK(download->GetDbHandle() == DownloadItem::kUninitializedHandle);
864 download->SetDbHandle(db_handle);
[email protected]5bcd73eb2011-03-23 21:14:02865
[email protected]639ddad2011-10-05 02:53:47866 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/85408
[email protected]d8472d92011-08-26 20:15:20867 // is fixed.
[email protected]c09a8fd2011-11-21 19:54:50868 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
869 history_downloads_[download->GetDbHandle()] = download;
[email protected]6d0146c2011-08-04 19:13:04870
871 // Show in the appropriate browser UI.
872 // This includes buttons to save or cancel, for a dangerous download.
873 ShowDownloadInBrowser(download);
874
875 // Inform interested objects about the new download.
876 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19877}
878
[email protected]2588ea9d2011-08-22 20:59:53879
[email protected]5656f8a2011-11-17 16:12:58880void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id,
881 int64 db_handle) {
[email protected]2588ea9d2011-08-22 20:59:53882 if (save_page_downloads_.count(download_id)) {
883 OnSavePageItemAddedToPersistentStore(download_id, db_handle);
884 } else if (active_downloads_.count(download_id)) {
885 OnDownloadItemAddedToPersistentStore(download_id, db_handle);
886 }
887 // It's valid that we don't find a matching item, i.e. on shutdown.
888}
889
[email protected]f9a45b02011-06-30 23:49:19890// Once the new DownloadItem's creation info has been committed to the history
891// service, we associate the DownloadItem with the db handle, update our
892// 'history_downloads_' map and inform observers.
[email protected]5656f8a2011-11-17 16:12:58893void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore(
894 int32 download_id, int64 db_handle) {
[email protected]f9a45b02011-06-30 23:49:19895 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]19420cc2011-07-18 17:43:45896 DownloadItem* download = GetActiveDownloadItem(download_id);
[email protected]93af2272011-09-21 18:29:17897 if (!download)
[email protected]19420cc2011-07-18 17:43:45898 return;
[email protected]54610672011-07-18 18:24:43899
900 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
901 << " download_id = " << download_id
902 << " download = " << download->DebugString(true);
[email protected]f9a45b02011-06-30 23:49:19903
[email protected]d8472d92011-08-26 20:15:20904 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]d8472d92011-08-26 20:15:20905 int64 largest_handle = largest_db_handle_in_history_;
906 base::debug::Alias(&largest_handle);
[email protected]e5107ce2011-09-19 20:36:13907 int32 matching_item_download_id
908 = (ContainsKey(history_downloads_, db_handle) ?
[email protected]c09a8fd2011-11-21 19:54:50909 history_downloads_[db_handle]->GetId() : 0);
[email protected]e5107ce2011-09-19 20:36:13910 base::debug::Alias(&matching_item_download_id);
911
[email protected]61b75a52011-08-29 16:34:34912 CHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:20913
[email protected]f9a45b02011-06-30 23:49:19914 AddDownloadItemToHistory(download, db_handle);
initial.commit09911bf2008-07-26 23:55:29915
[email protected]93af2272011-09-21 18:29:17916 // If the download is still in progress, try to complete it.
917 //
918 // Otherwise, download has been cancelled or interrupted before we've
919 // received the DB handle. We post one final message to the history
920 // service so that it can be properly in sync with the DownloadItem's
921 // completion status, and also inform any observers so that they get
922 // more than just the start notification.
923 if (download->IsInProgress()) {
924 MaybeCompleteDownload(download);
925 } else {
[email protected]639ddad2011-10-05 02:53:47926 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/85408
[email protected]93af2272011-09-21 18:29:17927 // is fixed.
928 CHECK(download->IsCancelled())
929 << " download = " << download->DebugString(true);
930 in_progress_.erase(download_id);
931 active_downloads_.erase(download_id);
932 delegate_->UpdateItemInPersistentStore(download);
933 download->UpdateObservers();
934 }
initial.commit09911bf2008-07-26 23:55:29935}
936
[email protected]5656f8a2011-11-17 16:12:58937void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]8ddbd66a2010-05-21 16:38:34938 // The 'contents' may no longer exist if the user closed the tab before we
[email protected]99cb7f82011-07-28 17:27:26939 // get this start completion event.
[email protected]ae77da82011-11-01 19:17:29940 TabContents* content = download->GetTabContents();
[email protected]99cb7f82011-07-28 17:27:26941
942 // If the contents no longer exists, we ask the embedder to suggest another
943 // tab.
[email protected]da1a27b2011-07-29 23:16:33944 if (!content)
[email protected]aa9881c2011-08-15 18:01:12945 content = delegate_->GetAlternativeTabContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:53946
[email protected]99cb7f82011-07-28 17:27:26947 if (content)
948 content->OnStartDownload(download);
[email protected]5e595482009-05-06 20:16:53949}
950
[email protected]5656f8a2011-11-17 16:12:58951int DownloadManagerImpl::InProgressCount() const {
952 return static_cast<int>(in_progress_.size());
953}
954
[email protected]6cade212008-12-03 00:32:22955// Clears the last download path, used to initialize "save as" dialogs.
[email protected]5656f8a2011-11-17 16:12:58956void DownloadManagerImpl::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:41957 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:38958}
[email protected]b0ab1d42010-02-24 19:29:28959
[email protected]5656f8a2011-11-17 16:12:58960void DownloadManagerImpl::NotifyModelChanged() {
[email protected]b0ab1d42010-02-24 19:29:28961 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
962}
963
[email protected]5656f8a2011-11-17 16:12:58964DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) {
[email protected]4cd82f72011-05-23 19:15:01965 // The |history_downloads_| map is indexed by the download's db_handle,
966 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:07967 for (DownloadMap::iterator it = history_downloads_.begin();
968 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:36969 DownloadItem* item = it->second;
[email protected]c09a8fd2011-11-21 19:54:50970 if (item->GetId() == download_id)
[email protected]2e030682010-07-23 19:45:36971 return item;
972 }
973 return NULL;
974}
975
[email protected]5656f8a2011-11-17 16:12:58976DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) {
[email protected]93af2272011-09-21 18:29:17977 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]4cd82f72011-05-23 19:15:01978 DownloadItem* download = active_downloads_[download_id];
979 DCHECK(download != NULL);
980 return download;
981}
982
[email protected]57fd1252010-12-23 17:24:09983// Confirm that everything in all maps is also in |downloads_|, and that
984// everything in |downloads_| is also in some other map.
[email protected]5656f8a2011-11-17 16:12:58985void DownloadManagerImpl::AssertContainersConsistent() const {
[email protected]f04182f32010-12-10 19:12:07986#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:09987 // Turn everything into sets.
[email protected]6d0146c2011-08-04 19:13:04988 const DownloadMap* input_maps[] = {&active_downloads_,
989 &history_downloads_,
990 &save_page_downloads_};
991 DownloadSet active_set, history_set, save_page_set;
992 DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set};
993 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:09994 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
995 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:04996 it != input_maps[i]->end(); ++it) {
997 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:07998 }
999 }
[email protected]57fd1252010-12-23 17:24:091000
1001 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:091002 DownloadSet downloads_union;
1003 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1004 DownloadSet remainder;
1005 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1006 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
1007 downloads_.begin(), downloads_.end(),
1008 insert_it);
1009 DCHECK(remainder.empty());
1010 std::insert_iterator<DownloadSet>
1011 insert_union(downloads_union, downloads_union.end());
1012 std::set_union(downloads_union.begin(), downloads_union.end(),
1013 all_sets[i]->begin(), all_sets[i]->end(),
1014 insert_union);
1015 }
1016
1017 // Is everything in downloads_ present in one of the other sets?
1018 DownloadSet remainder;
1019 std::insert_iterator<DownloadSet>
1020 insert_remainder(remainder, remainder.begin());
1021 std::set_difference(downloads_.begin(), downloads_.end(),
1022 downloads_union.begin(), downloads_union.end(),
1023 insert_remainder);
1024 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:071025#endif
1026}
1027
[email protected]5656f8a2011-11-17 16:12:581028void DownloadManagerImpl::SavePageDownloadStarted(DownloadItem* download) {
[email protected]c09a8fd2011-11-21 19:54:501029 DCHECK(!ContainsKey(save_page_downloads_, download->GetId()));
[email protected]6d0146c2011-08-04 19:13:041030 downloads_.insert(download);
[email protected]c09a8fd2011-11-21 19:54:501031 save_page_downloads_[download->GetId()] = download;
[email protected]6d0146c2011-08-04 19:13:041032
1033 // Add this entry to the history service.
1034 // Additionally, the UI is notified in the callback.
[email protected]2588ea9d2011-08-22 20:59:531035 delegate_->AddItemToPersistentStore(download);
[email protected]6d0146c2011-08-04 19:13:041036}
1037
1038// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:531039// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:041040// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:531041// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
1042// that the history event is update regardless of the order in which these two
1043// events complete.
[email protected]6d0146c2011-08-04 19:13:041044// If something removes the download item from the download manager (Remove,
1045// Shutdown) the result will be that the SavePage system will not be able to
1046// properly update the download item (which no longer exists) or the download
1047// history, but the action will complete properly anyway. This may lead to the
1048// history entry being wrong on a reload of chrome (specifically in the case of
1049// Initiation -> History Callback -> Removal -> Completion), but there's no way
1050// to solve that without canceling on Remove (which would then update the DB).
1051
[email protected]5656f8a2011-11-17 16:12:581052void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore(
1053 int32 download_id, int64 db_handle) {
[email protected]6d0146c2011-08-04 19:13:041054 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1055
1056 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
1057 // This can happen if the download manager is shutting down and all maps
1058 // have been cleared.
1059 if (it == save_page_downloads_.end())
1060 return;
1061
1062 DownloadItem* download = it->second;
1063 if (!download) {
1064 NOTREACHED();
1065 return;
1066 }
1067
[email protected]d8472d92011-08-26 20:15:201068 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]d8472d92011-08-26 20:15:201069 int64 largest_handle = largest_db_handle_in_history_;
1070 base::debug::Alias(&largest_handle);
[email protected]61b75a52011-08-29 16:34:341071 CHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:201072
[email protected]6d0146c2011-08-04 19:13:041073 AddDownloadItemToHistory(download, db_handle);
1074
1075 // Finalize this download if it finished before the history callback.
1076 if (!download->IsInProgress())
1077 SavePageDownloadFinished(download);
1078}
1079
[email protected]5656f8a2011-11-17 16:12:581080void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) {
[email protected]c09a8fd2011-11-21 19:54:501081 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) {
[email protected]2588ea9d2011-08-22 20:59:531082 delegate_->UpdateItemInPersistentStore(download);
[email protected]c09a8fd2011-11-21 19:54:501083 DCHECK(ContainsKey(save_page_downloads_, download->GetId()));
1084 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:041085
1086 if (download->IsComplete())
[email protected]ad50def52011-10-19 23:17:071087 content::NotificationService::current()->Notify(
[email protected]6d0146c2011-08-04 19:13:041088 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
[email protected]6c2381d2011-10-19 02:52:531089 content::Source<DownloadManager>(this),
1090 content::Details<DownloadItem>(download));
[email protected]6d0146c2011-08-04 19:13:041091 }
1092}
[email protected]da4a5582011-10-17 19:08:061093
[email protected]5656f8a2011-11-17 16:12:581094void DownloadManagerImpl::MarkDownloadOpened(DownloadItem* download) {
[email protected]da4a5582011-10-17 19:08:061095 delegate_->UpdateItemInPersistentStore(download);
1096 int num_unopened = 0;
1097 for (DownloadMap::iterator it = history_downloads_.begin();
1098 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:501099 if (it->second->IsComplete() && !it->second->GetOpened())
[email protected]da4a5582011-10-17 19:08:061100 ++num_unopened;
1101 }
1102 download_stats::RecordOpensOutstanding(num_unopened);
1103}
[email protected]5656f8a2011-11-17 16:12:581104
1105void DownloadManagerImpl::SetFileManager(DownloadFileManager* file_manager) {
1106 file_manager_ = file_manager;
1107}