blob: bc12c5659c90d6e7053c1240cf28f5bcbdbc1e19 [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]71bf3f5e2011-08-15 21:05:225#include "content/browser/download/download_manager.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]e7557f172011-08-19 23:42:017#include <iterator>
8
[email protected]2041cf342010-02-19 03:15:599#include "base/callback.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/file_util.h"
[email protected]503d03872011-05-06 08:36:2611#include "base/i18n/case_conversion.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/logging.h"
[email protected]7286e3fc2011-07-19 22:13:2413#include "base/stl_util.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/task.h"
[email protected]d2a8fb72010-01-21 05:31:4215#include "build/build_config.h"
[email protected]6d0c9fb2011-08-22 19:26:0316#include "content/browser/browser_context.h"
[email protected]7324d1d2011-03-01 05:02:1617#include "content/browser/browser_thread.h"
[email protected]a0ce3282011-08-19 20:49:5218#include "content/browser/content_browser_client.h"
[email protected]71bf3f5e2011-08-15 21:05:2219#include "content/browser/download/download_create_info.h"
20#include "content/browser/download/download_file_manager.h"
21#include "content/browser/download/download_item.h"
[email protected]d6b7fd1e2011-08-16 22:42:0022#include "content/browser/download/download_manager_delegate.h"
[email protected]bb1a4212011-08-22 22:38:2523#include "content/browser/download/download_persistent_store_info.h"
[email protected]71bf3f5e2011-08-15 21:05:2224#include "content/browser/download/download_status_updater.h"
[email protected]7324d1d2011-03-01 05:02:1625#include "content/browser/renderer_host/render_process_host.h"
26#include "content/browser/renderer_host/render_view_host.h"
27#include "content/browser/renderer_host/resource_dispatcher_host.h"
28#include "content/browser/tab_contents/tab_contents.h"
[email protected]432115822011-07-10 15:52:2729#include "content/common/content_notification_types.h"
[email protected]6d0146c2011-08-04 19:13:0430#include "content/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2931
[email protected]a0ce3282011-08-19 20:49:5232namespace {
33
34void BeginDownload(
35 const GURL& url,
36 const GURL& referrer,
37 const DownloadSaveInfo& save_info,
[email protected]c79a0c02011-08-22 22:37:3738 ResourceDispatcherHost* resource_dispatcher_host,
39 int render_process_id,
[email protected]a0ce3282011-08-19 20:49:5240 int render_view_id,
41 const content::ResourceContext* context) {
[email protected]c79a0c02011-08-22 22:37:3742 resource_dispatcher_host->BeginDownload(
43 url, referrer, save_info, true, render_process_id, render_view_id,
44 *context);
[email protected]a0ce3282011-08-19 20:49:5245}
46
47} // namespace
48
[email protected]da1a27b2011-07-29 23:16:3349DownloadManager::DownloadManager(DownloadManagerDelegate* delegate,
50 DownloadStatusUpdater* status_updater)
initial.commit09911bf2008-07-26 23:55:2951 : shutdown_needed_(false),
[email protected]6d0c9fb2011-08-22 19:26:0352 browser_context_(NULL),
[email protected]073ed7b2010-09-27 09:20:0253 file_manager_(NULL),
[email protected]da1a27b2011-07-29 23:16:3354 status_updater_(status_updater->AsWeakPtr()),
[email protected]d8472d92011-08-26 20:15:2055 delegate_(delegate),
56 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) {
[email protected]073ed7b2010-09-27 09:20:0257 if (status_updater_)
58 status_updater_->AddDelegate(this);
initial.commit09911bf2008-07-26 23:55:2959}
60
61DownloadManager::~DownloadManager() {
[email protected]326a6a92010-09-10 20:21:1362 DCHECK(!shutdown_needed_);
[email protected]073ed7b2010-09-27 09:20:0263 if (status_updater_)
64 status_updater_->RemoveDelegate(this);
initial.commit09911bf2008-07-26 23:55:2965}
66
67void DownloadManager::Shutdown() {
[email protected]da6e3922010-11-24 21:45:5068 VLOG(20) << __FUNCTION__ << "()"
69 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:1370 if (!shutdown_needed_)
71 return;
72 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:2973
[email protected]326a6a92010-09-10 20:21:1374 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
75
76 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:1877 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]326a6a92010-09-10 20:21:1378 NewRunnableMethod(file_manager_,
79 &DownloadFileManager::OnDownloadManagerShutdown,
[email protected]dc7cdcb92010-12-14 06:40:5480 make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:1381 }
initial.commit09911bf2008-07-26 23:55:2982
[email protected]f04182f32010-12-10 19:12:0783 AssertContainersConsistent();
84
85 // Go through all downloads in downloads_. Dangerous ones we need to
86 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:0987 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:0788 DownloadItem* download = *it;
89
90 // Save iterator from potential erases in this set done by called code.
91 // Iterators after an erasure point are still valid for lists and
92 // associative containers such as sets.
93 it++;
94
95 if (download->safety_state() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:2996 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:0797 // The user hasn't accepted it, so we need to remove it
98 // from the disk. This may or may not result in it being
99 // removed from the DownloadManager queues and deleted
100 // (specifically, DownloadManager::RemoveDownload only
101 // removes and deletes it if it's known to the history service)
102 // so the only thing we know after calling this function is that
103 // the download was deleted if-and-only-if it was removed
104 // from all queues.
[email protected]303077002011-04-19 23:21:01105 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26106 } else if (download->IsPartialDownload()) {
[email protected]54610672011-07-18 18:24:43107 download->Cancel(false);
[email protected]2588ea9d2011-08-22 20:59:53108 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29109 }
110 }
111
[email protected]f04182f32010-12-10 19:12:07112 // At this point, all dangerous downloads have had their files removed
113 // and all in progress downloads have been cancelled. We can now delete
114 // anything left.
[email protected]9ccbb372008-10-10 18:50:32115
[email protected]5cd11b6e2011-06-10 20:30:59116 // Copy downloads_ to separate container so as not to set off checks
117 // in DownloadItem destruction.
118 DownloadSet downloads_to_delete;
119 downloads_to_delete.swap(downloads_);
120
initial.commit09911bf2008-07-26 23:55:29121 in_progress_.clear();
[email protected]70850c72011-01-11 17:31:27122 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59123 history_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59124 STLDeleteElements(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29125
[email protected]6d0146c2011-08-04 19:13:04126 DCHECK(save_page_downloads_.empty());
127
initial.commit09911bf2008-07-26 23:55:29128 file_manager_ = NULL;
[email protected]2588ea9d2011-08-22 20:59:53129 delegate_->Shutdown();
[email protected]82f37b02010-07-29 22:04:57130
initial.commit09911bf2008-07-26 23:55:29131 shutdown_needed_ = false;
132}
133
[email protected]82f37b02010-07-29 22:04:57134void DownloadManager::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04135 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57136 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58137
[email protected]f04182f32010-12-10 19:12:07138 for (DownloadMap::iterator it = history_downloads_.begin();
139 it != history_downloads_.end(); ++it) {
[email protected]6aa4a1c02010-01-15 18:49:58140 if (it->second->is_temporary() &&
141 it->second->full_path().DirName() == dir_path)
[email protected]82f37b02010-07-29 22:04:57142 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58143 }
[email protected]6aa4a1c02010-01-15 18:49:58144}
145
[email protected]82f37b02010-07-29 22:04:57146void DownloadManager::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04147 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57148 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34149
[email protected]f04182f32010-12-10 19:12:07150 for (DownloadMap::iterator it = history_downloads_.begin();
151 it != history_downloads_.end(); ++it) {
[email protected]8ddbd66a2010-05-21 16:38:34152 if (!it->second->is_temporary() &&
153 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57154 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34155 }
[email protected]8ddbd66a2010-05-21 16:38:34156}
157
[email protected]d3b12902010-08-16 23:39:42158void DownloadManager::SearchDownloads(const string16& query,
[email protected]6d0146c2011-08-04 19:13:04159 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26160 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42161
[email protected]f04182f32010-12-10 19:12:07162 for (DownloadMap::iterator it = history_downloads_.begin();
163 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42164 DownloadItem* download_item = it->second;
165
[email protected]8a282712011-08-23 19:28:00166 if (download_item->is_temporary())
[email protected]d3b12902010-08-16 23:39:42167 continue;
168
169 // Display Incognito downloads only in Incognito window, and vice versa.
170 // The Incognito Downloads page will get the list of non-Incognito downloads
171 // from its parent profile.
[email protected]6d0c9fb2011-08-22 19:26:03172 if (browser_context_->IsOffTheRecord() != download_item->is_otr())
[email protected]d3b12902010-08-16 23:39:42173 continue;
174
175 if (download_item->MatchesQuery(query_lower))
176 result->push_back(download_item);
177 }
[email protected]d3b12902010-08-16 23:39:42178}
179
initial.commit09911bf2008-07-26 23:55:29180// Query the history service for information about all persisted downloads.
[email protected]6d0c9fb2011-08-22 19:26:03181bool DownloadManager::Init(content::BrowserContext* browser_context) {
182 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29183 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
184 shutdown_needed_ = true;
185
[email protected]6d0c9fb2011-08-22 19:26:03186 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46187
[email protected]2941c2392010-07-15 22:54:30188 // In test mode, there may be no ResourceDispatcherHost. In this case it's
189 // safe to avoid setting |file_manager_| because we only call a small set of
190 // functions, none of which need it.
[email protected]a0ce3282011-08-19 20:49:52191 ResourceDispatcherHost* rdh =
192 content::GetContentClient()->browser()->GetResourceDispatcherHost();
[email protected]2941c2392010-07-15 22:54:30193 if (rdh) {
194 file_manager_ = rdh->download_file_manager();
195 DCHECK(file_manager_);
initial.commit09911bf2008-07-26 23:55:29196 }
197
initial.commit09911bf2008-07-26 23:55:29198 return true;
199}
200
[email protected]aa9881c2011-08-15 18:01:12201// We have received a message from DownloadFileManager about a new download.
[email protected]4cd82f72011-05-23 19:15:01202void DownloadManager::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35204
[email protected]aa9881c2011-08-15 18:01:12205 if (delegate_->ShouldStartDownload(download_id))
206 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35207}
208
[email protected]9fc114672011-06-15 08:17:48209void DownloadManager::CheckForHistoryFilesRemoval() {
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
211 for (DownloadMap::iterator it = history_downloads_.begin();
212 it != history_downloads_.end(); ++it) {
213 CheckForFileRemoval(it->second);
214 }
215}
216
217void DownloadManager::CheckForFileRemoval(DownloadItem* download_item) {
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
219 if (download_item->IsComplete() &&
220 !download_item->file_externally_removed()) {
221 BrowserThread::PostTask(
222 BrowserThread::FILE, FROM_HERE,
223 NewRunnableMethod(this,
224 &DownloadManager::CheckForFileRemovalOnFileThread,
225 download_item->db_handle(),
226 download_item->GetTargetFilePath()));
227 }
228}
229
230void DownloadManager::CheckForFileRemovalOnFileThread(
231 int64 db_handle, const FilePath& path) {
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
233 if (!file_util::PathExists(path)) {
234 BrowserThread::PostTask(
235 BrowserThread::UI, FROM_HERE,
236 NewRunnableMethod(this,
237 &DownloadManager::OnFileRemovalDetected,
238 db_handle));
239 }
240}
241
242void DownloadManager::OnFileRemovalDetected(int64 db_handle) {
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
244 DownloadMap::iterator it = history_downloads_.find(db_handle);
245 if (it != history_downloads_.end()) {
246 DownloadItem* download_item = it->second;
247 download_item->OnDownloadedFileRemoved();
248 }
249}
250
[email protected]aa9881c2011-08-15 18:01:12251void DownloadManager::RestartDownload(
252 int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29254
[email protected]4cd82f72011-05-23 19:15:01255 DownloadItem* download = GetActiveDownloadItem(download_id);
256 if (!download)
257 return;
258
259 VLOG(20) << __FUNCTION__ << "()"
260 << " download = " << download->DebugString(true);
261
[email protected]4cd82f72011-05-23 19:15:01262 FilePath suggested_path = download->suggested_path();
263
[email protected]da1a27b2011-07-29 23:16:33264 if (download->prompt_user_for_save_location()) {
initial.commit09911bf2008-07-26 23:55:29265 // We must ask the user for the place to put the download.
[email protected]db6831a2011-06-09 21:08:28266 DownloadRequestHandle request_handle = download->request_handle();
267 TabContents* contents = request_handle.GetTabContents();
[email protected]99cb7f82011-07-28 17:27:26268
[email protected]4cd82f72011-05-23 19:15:01269 // |id_ptr| will be deleted in either FileSelected() or
270 // FileSelectionCancelled().
271 int32* id_ptr = new int32;
272 *id_ptr = download_id;
[email protected]99cb7f82011-07-28 17:27:26273
[email protected]da1a27b2011-07-29 23:16:33274 delegate_->ChooseDownloadPath(
[email protected]aa9881c2011-08-15 18:01:12275 contents, suggested_path, reinterpret_cast<void*>(id_ptr));
[email protected]99cb7f82011-07-28 17:27:26276
[email protected]f5920322011-03-24 20:34:16277 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]fed38252011-07-08 17:26:50278 SelectFileDialogDisplayed(download_id));
initial.commit09911bf2008-07-26 23:55:29279 } else {
280 // No prompting for download, just continue with the suggested name.
[email protected]4cd82f72011-05-23 19:15:01281 ContinueDownloadWithPath(download, suggested_path);
initial.commit09911bf2008-07-26 23:55:29282 }
283}
284
[email protected]c2e76012010-12-23 21:10:29285void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) {
286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
287
288 DownloadItem* download = new DownloadItem(this, *info,
[email protected]6d0c9fb2011-08-22 19:26:03289 browser_context_->IsOffTheRecord());
[email protected]4cd82f72011-05-23 19:15:01290 int32 download_id = info->download_id;
291 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]d8472d92011-08-26 20:15:20292
293 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]821960a2011-08-23 20:40:03294 CHECK(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29295 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01296 active_downloads_[download_id] = download;
[email protected]c2e76012010-12-23 21:10:29297}
298
[email protected]4cd82f72011-05-23 19:15:01299void DownloadManager::ContinueDownloadWithPath(DownloadItem* download,
300 const FilePath& chosen_file) {
[email protected]ca4b5fa32010-10-09 12:42:18301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01302 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39303
[email protected]4cd82f72011-05-23 19:15:01304 int32 download_id = download->id();
initial.commit09911bf2008-07-26 23:55:29305
[email protected]70850c72011-01-11 17:31:27306 // NOTE(ahendrickson) Eventually |active_downloads_| will replace
307 // |in_progress_|, but we don't want to change the semantics yet.
[email protected]4cd82f72011-05-23 19:15:01308 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]70850c72011-01-11 17:31:27309 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01310 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27311
[email protected]4cd82f72011-05-23 19:15:01312 // Make sure the initial file name is set only once.
313 DCHECK(download->full_path().empty());
314 download->OnPathDetermined(chosen_file);
[email protected]4cd82f72011-05-23 19:15:01315
316 VLOG(20) << __FUNCTION__ << "()"
317 << " download = " << download->DebugString(true);
318
319 in_progress_[download_id] = download;
[email protected]5f8589fe2011-08-17 20:58:39320 UpdateDownloadProgress(); // Reflect entry into in_progress_.
initial.commit09911bf2008-07-26 23:55:29321
[email protected]adb2f3d12011-01-23 16:24:54322 // Rename to intermediate name.
[email protected]f5920322011-03-24 20:34:16323 FilePath download_path;
[email protected]ec865262011-08-23 20:01:48324 if (!delegate_->OverrideIntermediatePath(download, &download_path))
[email protected]4cd82f72011-05-23 19:15:01325 download_path = download->full_path();
[email protected]594cd7d2010-07-21 03:23:56326
[email protected]f5920322011-03-24 20:34:16327 BrowserThread::PostTask(
328 BrowserThread::FILE, FROM_HERE,
329 NewRunnableMethod(
330 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile,
331 download->id(), download_path));
332
333 download->Rename(download_path);
334
[email protected]2588ea9d2011-08-22 20:59:53335 delegate_->AddItemToPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29336}
337
initial.commit09911bf2008-07-26 23:55:29338void DownloadManager::UpdateDownload(int32 download_id, int64 size) {
[email protected]70850c72011-01-11 17:31:27339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
340 DownloadMap::iterator it = active_downloads_.find(download_id);
341 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29342 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26343 if (download->IsInProgress()) {
[email protected]70850c72011-01-11 17:31:27344 download->Update(size);
[email protected]5f8589fe2011-08-17 20:58:39345 UpdateDownloadProgress(); // Reflect size updates.
[email protected]2588ea9d2011-08-22 20:59:53346 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27347 }
initial.commit09911bf2008-07-26 23:55:29348 }
349}
350
[email protected]bf68a00b2011-04-07 17:28:26351void DownloadManager::OnResponseCompleted(int32 download_id,
352 int64 size,
353 int os_error,
354 const std::string& hash) {
355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]7c8e55ba2011-07-01 18:59:23356 // ERR_CONNECTION_CLOSED is allowed since a number of servers in the wild
357 // advertise a larger Content-Length than the amount of bytes in the message
358 // body, and then close the connection. Other browsers - IE8, Firefox 4.0.1,
359 // and Safari 5.0.4 - treat the download as complete in this case, so we
360 // follow their lead.
[email protected]1822a422011-07-15 15:49:19361 if (os_error == 0 || os_error == net::ERR_CONNECTION_CLOSED) {
[email protected]bf68a00b2011-04-07 17:28:26362 OnAllDataSaved(download_id, size, hash);
363 } else {
364 OnDownloadError(download_id, size, os_error);
365 }
366}
367
[email protected]26711732011-03-09 00:21:22368void DownloadManager::OnAllDataSaved(int32 download_id,
369 int64 size,
370 const std::string& hash) {
[email protected]da6e3922010-11-24 21:45:50371 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
372 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35373 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32374
[email protected]c4f02c42011-01-24 21:55:06375 // If it's not in active_downloads_, that means it was cancelled; just
376 // ignore the notification.
377 if (active_downloads_.count(download_id) == 0)
378 return;
379
[email protected]adb2f3d12011-01-23 16:24:54380 DownloadItem* download = active_downloads_[download_id];
[email protected]a850ba42010-09-10 22:00:30381 download->OnAllDataSaved(size);
[email protected]9ccbb372008-10-10 18:50:32382
[email protected]adb2f3d12011-01-23 16:24:54383 MaybeCompleteDownload(download);
384}
[email protected]9ccbb372008-10-10 18:50:32385
[email protected]7d413112011-06-16 18:50:17386void DownloadManager::AssertQueueStateConsistent(DownloadItem* download) {
[email protected]5cd11b6e2011-06-10 20:30:59387 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
[email protected]7d413112011-06-16 18:50:17388 if (download->state() == DownloadItem::REMOVING) {
389 CHECK(!ContainsKey(downloads_, download));
390 CHECK(!ContainsKey(active_downloads_, download->id()));
391 CHECK(!ContainsKey(in_progress_, download->id()));
392 CHECK(!ContainsKey(history_downloads_, download->db_handle()));
393 return;
394 }
395
396 // Should be in downloads_ if we're not REMOVING.
397 CHECK(ContainsKey(downloads_, download));
398
399 // Check history_downloads_ consistency.
[email protected]2588ea9d2011-08-22 20:59:53400 if (download->db_handle() != DownloadItem::kUninitializedHandle) {
[email protected]7d413112011-06-16 18:50:17401 CHECK(ContainsKey(history_downloads_, download->db_handle()));
402 } else {
403 // TODO(rdsmith): Somewhat painful; make sure to disable in
404 // release builds after resolution of http://crbug.com/85408.
405 for (DownloadMap::iterator it = history_downloads_.begin();
406 it != history_downloads_.end(); ++it) {
407 CHECK(it->second != download);
408 }
409 }
410
[email protected]61b75a52011-08-29 16:34:34411 int64 state = download->state();
412 base::debug::Alias(&state);
[email protected]821960a2011-08-23 20:40:03413 if (ContainsKey(active_downloads_, download->id()))
414 CHECK_EQ(DownloadItem::IN_PROGRESS, download->state());
415 if (DownloadItem::IN_PROGRESS == download->state())
416 CHECK(ContainsKey(active_downloads_, download->id()));
[email protected]5cd11b6e2011-06-10 20:30:59417}
418
[email protected]adb2f3d12011-01-23 16:24:54419bool DownloadManager::IsDownloadReadyForCompletion(DownloadItem* download) {
420 // If we don't have all the data, the download is not ready for
421 // completion.
422 if (!download->all_data_saved())
423 return false;
[email protected]6a7fb042010-02-01 16:30:47424
[email protected]9d7ef802011-02-25 19:03:35425 // If the download is dangerous, but not yet validated, it's not ready for
426 // completion.
427 if (download->safety_state() == DownloadItem::DANGEROUS)
428 return false;
429
[email protected]adb2f3d12011-01-23 16:24:54430 // If the download isn't active (e.g. has been cancelled) it's not
431 // ready for completion.
432 if (active_downloads_.count(download->id()) == 0)
433 return false;
434
435 // If the download hasn't been inserted into the history system
436 // (which occurs strictly after file name determination, intermediate
437 // file rename, and UI display) then it's not ready for completion.
[email protected]2588ea9d2011-08-22 20:59:53438 if (download->db_handle() == DownloadItem::kUninitializedHandle)
[email protected]7054b592011-06-22 14:46:42439 return false;
440
441 return true;
[email protected]adb2f3d12011-01-23 16:24:54442}
443
444void DownloadManager::MaybeCompleteDownload(DownloadItem* download) {
445 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
446 VLOG(20) << __FUNCTION__ << "()" << " download = "
447 << download->DebugString(false);
448
449 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32450 return;
[email protected]9ccbb372008-10-10 18:50:32451
[email protected]adb2f3d12011-01-23 16:24:54452 // TODO(rdsmith): DCHECK that we only pass through this point
453 // once per download. The natural way to do this is by a state
454 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56455
[email protected]adb2f3d12011-01-23 16:24:54456 // Confirm we're in the proper set of states to be here;
[email protected]9d7ef802011-02-25 19:03:35457 // in in_progress_, have all data, have a history handle, (validated or safe).
458 DCHECK_NE(DownloadItem::DANGEROUS, download->safety_state());
[email protected]adb2f3d12011-01-23 16:24:54459 DCHECK_EQ(1u, in_progress_.count(download->id()));
460 DCHECK(download->all_data_saved());
[email protected]2588ea9d2011-08-22 20:59:53461 DCHECK(download->db_handle() != DownloadItem::kUninitializedHandle);
[email protected]adb2f3d12011-01-23 16:24:54462 DCHECK_EQ(1u, history_downloads_.count(download->db_handle()));
463
464 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
465 << download->DebugString(false);
466
467 // Remove the id from in_progress
468 in_progress_.erase(download->id());
[email protected]5f8589fe2011-08-17 20:58:39469 UpdateDownloadProgress(); // Reflect removal from in_progress_.
[email protected]adb2f3d12011-01-23 16:24:54470
[email protected]2588ea9d2011-08-22 20:59:53471 delegate_->UpdateItemInPersistentStore(download);
[email protected]adb2f3d12011-01-23 16:24:54472
[email protected]f5920322011-03-24 20:34:16473 // Finish the download.
[email protected]48837962011-04-19 17:03:29474 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32475}
476
[email protected]cc3c7c092011-05-09 18:40:21477void DownloadManager::DownloadCompleted(int32 download_id) {
[email protected]70850c72011-01-11 17:31:27478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21479 DownloadItem* download = GetDownloadItem(download_id);
480 DCHECK(download);
[email protected]2588ea9d2011-08-22 20:59:53481 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27482 active_downloads_.erase(download_id);
[email protected]821960a2011-08-23 20:40:03483 AssertQueueStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27484}
485
[email protected]f5920322011-03-24 20:34:16486void DownloadManager::OnDownloadRenamedToFinalName(int download_id,
487 const FilePath& full_path,
488 int uniquifier) {
[email protected]da6e3922010-11-24 21:45:50489 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
[email protected]f5920322011-03-24 20:34:16490 << " full_path = \"" << full_path.value() << "\""
491 << " uniquifier = " << uniquifier;
[email protected]ca4b5fa32010-10-09 12:42:18492 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f5920322011-03-24 20:34:16493
[email protected]2e030682010-07-23 19:45:36494 DownloadItem* item = GetDownloadItem(download_id);
495 if (!item)
496 return;
[email protected]6cade212008-12-03 00:32:22497
[email protected]8fa1eeb52011-04-13 14:18:02498 if (item->safety_state() == DownloadItem::SAFE) {
499 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice";
500 }
501
[email protected]ca4b5fa32010-10-09 12:42:18502 BrowserThread::PostTask(
[email protected]f5920322011-03-24 20:34:16503 BrowserThread::FILE, FROM_HERE,
504 NewRunnableMethod(
505 file_manager_, &DownloadFileManager::CompleteDownload, download_id));
[email protected]9ccbb372008-10-10 18:50:32506
[email protected]f5920322011-03-24 20:34:16507 if (uniquifier)
508 item->set_path_uniquifier(uniquifier);
[email protected]9ccbb372008-10-10 18:50:32509
[email protected]f5920322011-03-24 20:34:16510 item->OnDownloadRenamedToFinalName(full_path);
[email protected]2588ea9d2011-08-22 20:59:53511 delegate_->UpdatePathForItemInPersistentStore(item, full_path);
initial.commit09911bf2008-07-26 23:55:29512}
513
[email protected]d8472d92011-08-26 20:15:20514void DownloadManager::CancelDownload(int32 download_id) {
[email protected]70850c72011-01-11 17:31:27515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20516 DownloadItem* download = GetDownloadItem(download_id);
517 if (!download)
initial.commit09911bf2008-07-26 23:55:29518 return;
initial.commit09911bf2008-07-26 23:55:29519
[email protected]d8472d92011-08-26 20:15:20520 download->Cancel(true);
521}
522
523void DownloadManager::DownloadCancelledInternal(DownloadItem* download) {
524 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
525 int download_id = download->id();
526
527 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50528 << " download = " << download->DebugString(true);
529
[email protected]54610672011-07-18 18:24:43530 // Clean up will happen when the history system create callback runs if we
531 // don't have a valid db_handle yet.
[email protected]2588ea9d2011-08-22 20:59:53532 if (download->db_handle() != DownloadItem::kUninitializedHandle) {
[email protected]d8472d92011-08-26 20:15:20533 in_progress_.erase(download_id);
[email protected]54610672011-07-18 18:24:43534 active_downloads_.erase(download_id);
[email protected]5f8589fe2011-08-17 20:58:39535 UpdateDownloadProgress(); // Reflect removal from in_progress_.
[email protected]2588ea9d2011-08-22 20:59:53536 delegate_->UpdateItemInPersistentStore(download);
[email protected]d8472d92011-08-26 20:15:20537
538 // This function is called from the DownloadItem, so DI state
539 // should already have been updated.
[email protected]821960a2011-08-23 20:40:03540 AssertQueueStateConsistent(download);
[email protected]54610672011-07-18 18:24:43541 }
initial.commit09911bf2008-07-26 23:55:29542
[email protected]d8472d92011-08-26 20:15:20543 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29544}
545
[email protected]bf68a00b2011-04-07 17:28:26546void DownloadManager::OnDownloadError(int32 download_id,
547 int64 size,
548 int os_error) {
549 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
550 DownloadMap::iterator it = active_downloads_.find(download_id);
551 // A cancel at the right time could remove the download from the
552 // |active_downloads_| map before we get here.
553 if (it == active_downloads_.end())
554 return;
555
556 DownloadItem* download = it->second;
557
[email protected]4cd82f72011-05-23 19:15:01558 VLOG(20) << __FUNCTION__ << "()" << " Error " << os_error
559 << " at offset " << download->received_bytes()
560 << " for download = " << download->DebugString(true);
561
[email protected]54610672011-07-18 18:24:43562 download->Interrupted(size, os_error);
563
564 // TODO(ahendrickson) - Remove this when we add resuming of interrupted
565 // downloads, as we will keep the download item around in that case.
566 //
567 // Clean up will happen when the history system create callback runs if we
568 // don't have a valid db_handle yet.
[email protected]2588ea9d2011-08-22 20:59:53569 if (download->db_handle() != DownloadItem::kUninitializedHandle) {
[email protected]54610672011-07-18 18:24:43570 in_progress_.erase(download_id);
571 active_downloads_.erase(download_id);
[email protected]5f8589fe2011-08-17 20:58:39572 UpdateDownloadProgress(); // Reflect removal from in_progress_.
[email protected]2588ea9d2011-08-22 20:59:53573 delegate_->UpdateItemInPersistentStore(download);
[email protected]54610672011-07-18 18:24:43574 }
575
576 BrowserThread::PostTask(
577 BrowserThread::FILE, FROM_HERE,
578 NewRunnableMethod(
579 file_manager_, &DownloadFileManager::CancelDownload, download_id));
[email protected]bf68a00b2011-04-07 17:28:26580}
581
[email protected]5f8589fe2011-08-17 20:58:39582void DownloadManager::UpdateDownloadProgress() {
583 delegate_->DownloadProgressUpdated();
[email protected]6a7fb042010-02-01 16:30:47584}
585
[email protected]6d0146c2011-08-04 19:13:04586int DownloadManager::RemoveDownloadItems(
587 const DownloadVector& pending_deletes) {
588 if (pending_deletes.empty())
589 return 0;
590
591 // Delete from internal maps.
592 for (DownloadVector::const_iterator it = pending_deletes.begin();
593 it != pending_deletes.end();
594 ++it) {
595 DownloadItem* download = *it;
596 DCHECK(download);
597 history_downloads_.erase(download->db_handle());
598 save_page_downloads_.erase(download->id());
599 downloads_.erase(download);
600 }
601
602 // Tell observers to refresh their views.
603 NotifyModelChanged();
604
605 // Delete the download items themselves.
606 const int num_deleted = static_cast<int>(pending_deletes.size());
607 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
608 return num_deleted;
609}
610
[email protected]54610672011-07-18 18:24:43611void DownloadManager::RemoveDownload(int64 download_handle) {
612 DownloadMap::iterator it = history_downloads_.find(download_handle);
613 if (it == history_downloads_.end())
614 return;
615
616 // Make history update.
617 DownloadItem* download = it->second;
[email protected]2588ea9d2011-08-22 20:59:53618 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29619
620 // Remove from our tables and delete.
[email protected]6d0146c2011-08-04 19:13:04621 int downloads_count = RemoveDownloadItems(DownloadVector(1, download));
[email protected]f04182f32010-12-10 19:12:07622 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29623}
624
[email protected]e93d2822009-01-30 05:59:59625int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin,
626 const base::Time remove_end) {
[email protected]2588ea9d2011-08-22 20:59:53627 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29628
[email protected]a312a442010-12-15 23:40:33629 // All downloads visible to the user will be in the history,
630 // so scan that map.
[email protected]6d0146c2011-08-04 19:13:04631 DownloadVector pending_deletes;
632 for (DownloadMap::const_iterator it = history_downloads_.begin();
633 it != history_downloads_.end();
634 ++it) {
initial.commit09911bf2008-07-26 23:55:29635 DownloadItem* download = it->second;
initial.commit09911bf2008-07-26 23:55:29636 if (download->start_time() >= remove_begin &&
637 (remove_end.is_null() || download->start_time() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04638 (download->IsComplete() || download->IsCancelled())) {
[email protected]7d413112011-06-16 18:50:17639 AssertQueueStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28640 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29641 }
initial.commit09911bf2008-07-26 23:55:29642 }
[email protected]6d0146c2011-08-04 19:13:04643 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29644}
645
[email protected]e93d2822009-01-30 05:59:59646int DownloadManager::RemoveDownloads(const base::Time remove_begin) {
647 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29648}
649
[email protected]d41355e6f2009-04-07 21:21:12650int DownloadManager::RemoveAllDownloads() {
651 // The null times make the date range unbounded.
652 return RemoveDownloadsBetween(base::Time(), base::Time());
653}
654
initial.commit09911bf2008-07-26 23:55:29655// Initiate a download of a specific URL. We send the request to the
656// ResourceDispatcherHost, and let it send us responses like a regular
657// download.
658void DownloadManager::DownloadUrl(const GURL& url,
659 const GURL& referrer,
[email protected]c9825a42009-05-01 22:51:50660 const std::string& referrer_charset,
[email protected]57c6a652009-05-04 07:58:34661 TabContents* tab_contents) {
[email protected]ae8945192010-07-20 16:56:26662 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
663 tab_contents);
[email protected]6aa4a1c02010-01-15 18:49:58664}
665
666void DownloadManager::DownloadUrlToFile(const GURL& url,
667 const GURL& referrer,
668 const std::string& referrer_charset,
[email protected]8af9d032010-02-10 00:00:32669 const DownloadSaveInfo& save_info,
[email protected]6aa4a1c02010-01-15 18:49:58670 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:34671 DCHECK(tab_contents);
[email protected]c79a0c02011-08-22 22:37:37672 ResourceDispatcherHost* resource_dispatcher_host =
673 content::GetContentClient()->browser()->GetResourceDispatcherHost();
[email protected]ed24fad2011-05-10 22:44:01674 // We send a pointer to content::ResourceContext, instead of the usual
675 // reference, so that a copy of the object isn't made.
[email protected]ca4b5fa32010-10-09 12:42:18676 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]a0ce3282011-08-19 20:49:52677 NewRunnableFunction(&BeginDownload,
[email protected]ae8945192010-07-20 16:56:26678 url,
679 referrer,
[email protected]ae8945192010-07-20 16:56:26680 save_info,
[email protected]c79a0c02011-08-22 22:37:37681 resource_dispatcher_host,
[email protected]ae8945192010-07-20 16:56:26682 tab_contents->GetRenderProcessHost()->id(),
683 tab_contents->render_view_host()->routing_id(),
[email protected]cafe4ad2011-07-28 18:34:56684 &tab_contents->browser_context()->
685 GetResourceContext()));
initial.commit09911bf2008-07-26 23:55:29686}
687
initial.commit09911bf2008-07-26 23:55:29688void DownloadManager::AddObserver(Observer* observer) {
689 observers_.AddObserver(observer);
690 observer->ModelChanged();
691}
692
693void DownloadManager::RemoveObserver(Observer* observer) {
694 observers_.RemoveObserver(observer);
695}
696
[email protected]073ed7b2010-09-27 09:20:02697bool DownloadManager::IsDownloadProgressKnown() {
698 for (DownloadMap::iterator i = in_progress_.begin();
699 i != in_progress_.end(); ++i) {
700 if (i->second->total_bytes() <= 0)
701 return false;
702 }
703
704 return true;
705}
706
707int64 DownloadManager::GetInProgressDownloadCount() {
708 return in_progress_.size();
709}
710
711int64 DownloadManager::GetReceivedDownloadBytes() {
712 DCHECK(IsDownloadProgressKnown());
713 int64 received_bytes = 0;
714 for (DownloadMap::iterator i = in_progress_.begin();
715 i != in_progress_.end(); ++i) {
716 received_bytes += i->second->received_bytes();
717 }
718 return received_bytes;
719}
720
721int64 DownloadManager::GetTotalDownloadBytes() {
722 DCHECK(IsDownloadProgressKnown());
723 int64 total_bytes = 0;
724 for (DownloadMap::iterator i = in_progress_.begin();
725 i != in_progress_.end(); ++i) {
726 total_bytes += i->second->total_bytes();
727 }
728 return total_bytes;
729}
730
[email protected]99cb7f82011-07-28 17:27:26731void DownloadManager::FileSelected(const FilePath& path, void* params) {
[email protected]4cd82f72011-05-23 19:15:01732 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
733
734 int32* id_ptr = reinterpret_cast<int32*>(params);
735 DCHECK(id_ptr != NULL);
736 int32 download_id = *id_ptr;
737 delete id_ptr;
738
739 DownloadItem* download = GetActiveDownloadItem(download_id);
740 if (!download)
741 return;
742 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
743 << " download = " << download->DebugString(true);
744
[email protected]da1a27b2011-07-29 23:16:33745 if (download->prompt_user_for_save_location())
[email protected]7ae7c2cb2009-01-06 23:31:41746 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35747
[email protected]4cd82f72011-05-23 19:15:01748 // Make sure the initial file name is set only once.
749 ContinueDownloadWithPath(download, path);
initial.commit09911bf2008-07-26 23:55:29750}
751
752void DownloadManager::FileSelectionCanceled(void* params) {
753 // The user didn't pick a place to save the file, so need to cancel the
754 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01755 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
756 int32* id_ptr = reinterpret_cast<int32*>(params);
757 DCHECK(id_ptr != NULL);
758 int32 download_id = *id_ptr;
759 delete id_ptr;
760
761 DownloadItem* download = GetActiveDownloadItem(download_id);
762 if (!download)
763 return;
764
765 VLOG(20) << __FUNCTION__ << "()"
766 << " download = " << download->DebugString(true);
767
[email protected]d8472d92011-08-26 20:15:20768 download->OffThreadCancel(file_manager_);
[email protected]4cd82f72011-05-23 19:15:01769}
770
initial.commit09911bf2008-07-26 23:55:29771// Operations posted to us from the history service ----------------------------
772
773// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25774// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]2588ea9d2011-08-22 20:59:53775void DownloadManager::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25776 std::vector<DownloadPersistentStoreInfo>* entries) {
[email protected]d8472d92011-08-26 20:15:20777 // TODO(rdsmith): Remove this and related logic when
778 // http://crbug.com/84508 is fixed.
779 largest_db_handle_in_history_ = 0;
780
initial.commit09911bf2008-07-26 23:55:29781 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]aa033af2010-07-27 18:16:39782 DownloadItem* download = new DownloadItem(this, entries->at(i));
[email protected]d8472d92011-08-26 20:15:20783 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]821960a2011-08-23 20:40:03784 CHECK(!ContainsKey(history_downloads_, download->db_handle()));
[email protected]f04182f32010-12-10 19:12:07785 downloads_.insert(download);
786 history_downloads_[download->db_handle()] = download;
[email protected]da6e3922010-11-24 21:45:50787 VLOG(20) << __FUNCTION__ << "()" << i << ">"
788 << " download = " << download->DebugString(true);
[email protected]d8472d92011-08-26 20:15:20789
790 if (download->db_handle() > largest_db_handle_in_history_)
791 largest_db_handle_in_history_ = download->db_handle();
initial.commit09911bf2008-07-26 23:55:29792 }
[email protected]b0ab1d42010-02-24 19:29:28793 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48794 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29795}
796
[email protected]f9a45b02011-06-30 23:49:19797void DownloadManager::AddDownloadItemToHistory(DownloadItem* download,
798 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27799 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d2a8fb72010-01-21 05:31:42800
[email protected]1e9fe7ff2011-06-24 17:37:33801 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508
802 // is fixed.
[email protected]2588ea9d2011-08-22 20:59:53803 CHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]1e9fe7ff2011-06-24 17:37:33804
[email protected]2588ea9d2011-08-22 20:59:53805 DCHECK(download->db_handle() == DownloadItem::kUninitializedHandle);
[email protected]5bcd73eb2011-03-23 21:14:02806 download->set_db_handle(db_handle);
807
[email protected]d8472d92011-08-26 20:15:20808 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508
809 // is fixed.
[email protected]821960a2011-08-23 20:40:03810 CHECK(!ContainsKey(history_downloads_, download->db_handle()));
[email protected]5bcd73eb2011-03-23 21:14:02811 history_downloads_[download->db_handle()] = download;
[email protected]6d0146c2011-08-04 19:13:04812
813 // Show in the appropriate browser UI.
814 // This includes buttons to save or cancel, for a dangerous download.
815 ShowDownloadInBrowser(download);
816
817 // Inform interested objects about the new download.
818 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19819}
820
[email protected]2588ea9d2011-08-22 20:59:53821
822void DownloadManager::OnItemAddedToPersistentStore(int32 download_id,
823 int64 db_handle) {
824 if (save_page_downloads_.count(download_id)) {
825 OnSavePageItemAddedToPersistentStore(download_id, db_handle);
826 } else if (active_downloads_.count(download_id)) {
827 OnDownloadItemAddedToPersistentStore(download_id, db_handle);
828 }
829 // It's valid that we don't find a matching item, i.e. on shutdown.
830}
831
[email protected]f9a45b02011-06-30 23:49:19832// Once the new DownloadItem's creation info has been committed to the history
833// service, we associate the DownloadItem with the db handle, update our
834// 'history_downloads_' map and inform observers.
[email protected]2588ea9d2011-08-22 20:59:53835void DownloadManager::OnDownloadItemAddedToPersistentStore(int32 download_id,
836 int64 db_handle) {
[email protected]f9a45b02011-06-30 23:49:19837 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]19420cc2011-07-18 17:43:45838 DownloadItem* download = GetActiveDownloadItem(download_id);
[email protected]54610672011-07-18 18:24:43839 if (!download)
[email protected]19420cc2011-07-18 17:43:45840 return;
[email protected]54610672011-07-18 18:24:43841
842 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
843 << " download_id = " << download_id
844 << " download = " << download->DebugString(true);
[email protected]f9a45b02011-06-30 23:49:19845
[email protected]d8472d92011-08-26 20:15:20846 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]d8472d92011-08-26 20:15:20847 int64 largest_handle = largest_db_handle_in_history_;
848 base::debug::Alias(&largest_handle);
[email protected]61b75a52011-08-29 16:34:34849 CHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:20850
[email protected]f9a45b02011-06-30 23:49:19851 AddDownloadItemToHistory(download, db_handle);
initial.commit09911bf2008-07-26 23:55:29852
[email protected]54610672011-07-18 18:24:43853 // If the download is still in progress, try to complete it.
854 //
855 // Otherwise, download has been cancelled or interrupted before we've
856 // received the DB handle. We post one final message to the history
857 // service so that it can be properly in sync with the DownloadItem's
858 // completion status, and also inform any observers so that they get
859 // more than just the start notification.
860 if (download->IsInProgress()) {
861 MaybeCompleteDownload(download);
862 } else {
[email protected]d8472d92011-08-26 20:15:20863 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508
864 // is fixed.
[email protected]821960a2011-08-23 20:40:03865 CHECK(download->IsCancelled())
[email protected]54610672011-07-18 18:24:43866 << " download = " << download->DebugString(true);
867 in_progress_.erase(download_id);
868 active_downloads_.erase(download_id);
[email protected]2588ea9d2011-08-22 20:59:53869 delegate_->UpdateItemInPersistentStore(download);
[email protected]54610672011-07-18 18:24:43870 download->UpdateObservers();
871 }
initial.commit09911bf2008-07-26 23:55:29872}
873
[email protected]4cd82f72011-05-23 19:15:01874void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]8ddbd66a2010-05-21 16:38:34875 // The 'contents' may no longer exist if the user closed the tab before we
[email protected]99cb7f82011-07-28 17:27:26876 // get this start completion event.
[email protected]db6831a2011-06-09 21:08:28877 DownloadRequestHandle request_handle = download->request_handle();
[email protected]686493142011-07-15 21:47:22878 TabContents* content = request_handle.GetTabContents();
[email protected]99cb7f82011-07-28 17:27:26879
880 // If the contents no longer exists, we ask the embedder to suggest another
881 // tab.
[email protected]da1a27b2011-07-29 23:16:33882 if (!content)
[email protected]aa9881c2011-08-15 18:01:12883 content = delegate_->GetAlternativeTabContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:53884
[email protected]99cb7f82011-07-28 17:27:26885 if (content)
886 content->OnStartDownload(download);
[email protected]5e595482009-05-06 20:16:53887}
888
[email protected]6cade212008-12-03 00:32:22889// Clears the last download path, used to initialize "save as" dialogs.
[email protected]905a08d2008-11-19 07:24:12890void DownloadManager::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:41891 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:38892}
[email protected]b0ab1d42010-02-24 19:29:28893
894void DownloadManager::NotifyModelChanged() {
895 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
896}
897
[email protected]4cd82f72011-05-23 19:15:01898DownloadItem* DownloadManager::GetDownloadItem(int download_id) {
899 // The |history_downloads_| map is indexed by the download's db_handle,
900 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:07901 for (DownloadMap::iterator it = history_downloads_.begin();
902 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:36903 DownloadItem* item = it->second;
[email protected]4cd82f72011-05-23 19:15:01904 if (item->id() == download_id)
[email protected]2e030682010-07-23 19:45:36905 return item;
906 }
907 return NULL;
908}
909
[email protected]4cd82f72011-05-23 19:15:01910DownloadItem* DownloadManager::GetActiveDownloadItem(int download_id) {
911 DCHECK(ContainsKey(active_downloads_, download_id));
912 DownloadItem* download = active_downloads_[download_id];
913 DCHECK(download != NULL);
914 return download;
915}
916
[email protected]57fd1252010-12-23 17:24:09917// Confirm that everything in all maps is also in |downloads_|, and that
918// everything in |downloads_| is also in some other map.
[email protected]f04182f32010-12-10 19:12:07919void DownloadManager::AssertContainersConsistent() const {
920#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:09921 // Turn everything into sets.
[email protected]6d0146c2011-08-04 19:13:04922 const DownloadMap* input_maps[] = {&active_downloads_,
923 &history_downloads_,
924 &save_page_downloads_};
925 DownloadSet active_set, history_set, save_page_set;
926 DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set};
927 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:09928 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
929 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:04930 it != input_maps[i]->end(); ++it) {
931 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:07932 }
933 }
[email protected]57fd1252010-12-23 17:24:09934
935 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:09936 DownloadSet downloads_union;
937 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
938 DownloadSet remainder;
939 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
940 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
941 downloads_.begin(), downloads_.end(),
942 insert_it);
943 DCHECK(remainder.empty());
944 std::insert_iterator<DownloadSet>
945 insert_union(downloads_union, downloads_union.end());
946 std::set_union(downloads_union.begin(), downloads_union.end(),
947 all_sets[i]->begin(), all_sets[i]->end(),
948 insert_union);
949 }
950
951 // Is everything in downloads_ present in one of the other sets?
952 DownloadSet remainder;
953 std::insert_iterator<DownloadSet>
954 insert_remainder(remainder, remainder.begin());
955 std::set_difference(downloads_.begin(), downloads_.end(),
956 downloads_union.begin(), downloads_union.end(),
957 insert_remainder);
958 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:07959#endif
960}
961
[email protected]6d0146c2011-08-04 19:13:04962void DownloadManager::SavePageDownloadStarted(DownloadItem* download) {
963 DCHECK(!ContainsKey(save_page_downloads_, download->id()));
964 downloads_.insert(download);
965 save_page_downloads_[download->id()] = download;
966
967 // Add this entry to the history service.
968 // Additionally, the UI is notified in the callback.
[email protected]2588ea9d2011-08-22 20:59:53969 delegate_->AddItemToPersistentStore(download);
[email protected]6d0146c2011-08-04 19:13:04970}
971
972// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:53973// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:04974// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:53975// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
976// that the history event is update regardless of the order in which these two
977// events complete.
[email protected]6d0146c2011-08-04 19:13:04978// If something removes the download item from the download manager (Remove,
979// Shutdown) the result will be that the SavePage system will not be able to
980// properly update the download item (which no longer exists) or the download
981// history, but the action will complete properly anyway. This may lead to the
982// history entry being wrong on a reload of chrome (specifically in the case of
983// Initiation -> History Callback -> Removal -> Completion), but there's no way
984// to solve that without canceling on Remove (which would then update the DB).
985
[email protected]2588ea9d2011-08-22 20:59:53986void DownloadManager::OnSavePageItemAddedToPersistentStore(int32 download_id,
987 int64 db_handle) {
[email protected]6d0146c2011-08-04 19:13:04988 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
989
990 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
991 // This can happen if the download manager is shutting down and all maps
992 // have been cleared.
993 if (it == save_page_downloads_.end())
994 return;
995
996 DownloadItem* download = it->second;
997 if (!download) {
998 NOTREACHED();
999 return;
1000 }
1001
[email protected]d8472d92011-08-26 20:15:201002 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]d8472d92011-08-26 20:15:201003 int64 largest_handle = largest_db_handle_in_history_;
1004 base::debug::Alias(&largest_handle);
[email protected]61b75a52011-08-29 16:34:341005 CHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:201006
[email protected]6d0146c2011-08-04 19:13:041007 AddDownloadItemToHistory(download, db_handle);
1008
1009 // Finalize this download if it finished before the history callback.
1010 if (!download->IsInProgress())
1011 SavePageDownloadFinished(download);
1012}
1013
1014void DownloadManager::SavePageDownloadFinished(DownloadItem* download) {
[email protected]2588ea9d2011-08-22 20:59:531015 if (download->db_handle() != DownloadItem::kUninitializedHandle) {
1016 delegate_->UpdateItemInPersistentStore(download);
[email protected]6d0146c2011-08-04 19:13:041017 DCHECK(ContainsKey(save_page_downloads_, download->id()));
1018 save_page_downloads_.erase(download->id());
1019
1020 if (download->IsComplete())
1021 NotificationService::current()->Notify(
1022 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
1023 Source<DownloadManager>(this),
1024 Details<DownloadItem>(download));
1025 }
1026}