[email protected] | f4f50ef | 2011-01-21 19:01:19 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 5 | #include "content/browser/download/download_manager.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | e7557f17 | 2011-08-19 23:42:01 | [diff] [blame] | 7 | #include <iterator> |
| 8 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 9 | #include "base/callback.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/file_util.h" |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 11 | #include "base/i18n/case_conversion.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 13 | #include "base/stl_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/task.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | e9ef0a6 | 2009-08-11 22:50:13 | [diff] [blame] | 16 | #include "chrome/browser/download/download_util.h" |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 17 | #include "content/browser/browser_context.h" |
[email protected] | 7324d1d | 2011-03-01 05:02:16 | [diff] [blame] | 18 | #include "content/browser/browser_thread.h" |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 19 | #include "content/browser/content_browser_client.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 20 | #include "content/browser/download/download_create_info.h" |
| 21 | #include "content/browser/download/download_file_manager.h" |
| 22 | #include "content/browser/download/download_item.h" |
[email protected] | d6b7fd1e | 2011-08-16 22:42:00 | [diff] [blame] | 23 | #include "content/browser/download/download_manager_delegate.h" |
[email protected] | bb1a421 | 2011-08-22 22:38:25 | [diff] [blame^] | 24 | #include "content/browser/download/download_persistent_store_info.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 25 | #include "content/browser/download/download_status_updater.h" |
[email protected] | 7324d1d | 2011-03-01 05:02:16 | [diff] [blame] | 26 | #include "content/browser/renderer_host/render_process_host.h" |
| 27 | #include "content/browser/renderer_host/render_view_host.h" |
| 28 | #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 29 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 30 | #include "content/common/content_notification_types.h" |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 31 | #include "content/common/notification_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | void BeginDownload( |
| 36 | const GURL& url, |
| 37 | const GURL& referrer, |
| 38 | const DownloadSaveInfo& save_info, |
[email protected] | c79a0c0 | 2011-08-22 22:37:37 | [diff] [blame] | 39 | ResourceDispatcherHost* resource_dispatcher_host, |
| 40 | int render_process_id, |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 41 | int render_view_id, |
| 42 | const content::ResourceContext* context) { |
[email protected] | c79a0c0 | 2011-08-22 22:37:37 | [diff] [blame] | 43 | resource_dispatcher_host->BeginDownload( |
| 44 | url, referrer, save_info, true, render_process_id, render_view_id, |
| 45 | *context); |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | } // namespace |
| 49 | |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame] | 50 | DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, |
| 51 | DownloadStatusUpdater* status_updater) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | : shutdown_needed_(false), |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 53 | browser_context_(NULL), |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 54 | file_manager_(NULL), |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame] | 55 | status_updater_(status_updater->AsWeakPtr()), |
| 56 | delegate_(delegate) { |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 57 | if (status_updater_) |
| 58 | status_updater_->AddDelegate(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | DownloadManager::~DownloadManager() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 62 | DCHECK(!shutdown_needed_); |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 63 | if (status_updater_) |
| 64 | status_updater_->RemoveDelegate(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void DownloadManager::Shutdown() { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 68 | VLOG(20) << __FUNCTION__ << "()" |
| 69 | << " shutdown_needed_ = " << shutdown_needed_; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 70 | if (!shutdown_needed_) |
| 71 | return; |
| 72 | shutdown_needed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 74 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); |
| 75 | |
| 76 | if (file_manager_) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 77 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 78 | NewRunnableMethod(file_manager_, |
| 79 | &DownloadFileManager::OnDownloadManagerShutdown, |
[email protected] | dc7cdcb9 | 2010-12-14 06:40:54 | [diff] [blame] | 80 | make_scoped_refptr(this))); |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 81 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 83 | 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] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 87 | for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 88 | 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] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 96 | download->IsPartialDownload()) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 97 | // 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] | 30307700 | 2011-04-19 23:21:01 | [diff] [blame] | 105 | download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 106 | } else if (download->IsPartialDownload()) { |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 107 | download->Cancel(false); |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 108 | delegate_->UpdateItemInPersistentStore(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 112 | // 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] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 115 | |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 116 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | in_progress_.clear(); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 122 | active_downloads_.clear(); |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 123 | history_downloads_.clear(); |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 124 | STLDeleteElements(&downloads_to_delete); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 126 | DCHECK(save_page_downloads_.empty()); |
| 127 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | file_manager_ = NULL; |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 129 | delegate_->Shutdown(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 130 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | shutdown_needed_ = false; |
| 132 | } |
| 133 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 134 | void DownloadManager::GetTemporaryDownloads( |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 135 | const FilePath& dir_path, DownloadVector* result) { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 136 | DCHECK(result); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 137 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 138 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 139 | it != history_downloads_.end(); ++it) { |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 140 | if (it->second->is_temporary() && |
| 141 | it->second->full_path().DirName() == dir_path) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 142 | result->push_back(it->second); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 143 | } |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 146 | void DownloadManager::GetAllDownloads( |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 147 | const FilePath& dir_path, DownloadVector* result) { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 148 | DCHECK(result); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 149 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 150 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 151 | it != history_downloads_.end(); ++it) { |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 152 | if (!it->second->is_temporary() && |
| 153 | (dir_path.empty() || it->second->full_path().DirName() == dir_path)) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 154 | result->push_back(it->second); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 155 | } |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 158 | void DownloadManager::SearchDownloads(const string16& query, |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 159 | DownloadVector* result) { |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 160 | string16 query_lower(base::i18n::ToLower(query)); |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 161 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 162 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 163 | it != history_downloads_.end(); ++it) { |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 164 | DownloadItem* download_item = it->second; |
| 165 | |
| 166 | if (download_item->is_temporary() || download_item->is_extension_install()) |
| 167 | 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] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 172 | if (browser_context_->IsOffTheRecord() != download_item->is_otr()) |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 173 | continue; |
| 174 | |
| 175 | if (download_item->MatchesQuery(query_lower)) |
| 176 | result->push_back(download_item); |
| 177 | } |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 178 | } |
| 179 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 180 | // Query the history service for information about all persisted downloads. |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 181 | bool DownloadManager::Init(content::BrowserContext* browser_context) { |
| 182 | DCHECK(browser_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 183 | DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 184 | shutdown_needed_ = true; |
| 185 | |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 186 | browser_context_ = browser_context; |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 187 | |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 188 | // 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] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 191 | ResourceDispatcherHost* rdh = |
| 192 | content::GetContentClient()->browser()->GetResourceDispatcherHost(); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 193 | if (rdh) { |
| 194 | file_manager_ = rdh->download_file_manager(); |
| 195 | DCHECK(file_manager_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | } |
| 197 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
[email protected] | aa9881c | 2011-08-15 18:01:12 | [diff] [blame] | 201 | // We have received a message from DownloadFileManager about a new download. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 202 | void DownloadManager::StartDownload(int32 download_id) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 203 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 204 | |
[email protected] | aa9881c | 2011-08-15 18:01:12 | [diff] [blame] | 205 | if (delegate_->ShouldStartDownload(download_id)) |
| 206 | RestartDownload(download_id); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 209 | void 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 | |
| 217 | void 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 | |
| 230 | void 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 | |
| 242 | void 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] | aa9881c | 2011-08-15 18:01:12 | [diff] [blame] | 251 | void DownloadManager::RestartDownload( |
| 252 | int32 download_id) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 253 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 254 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 255 | DownloadItem* download = GetActiveDownloadItem(download_id); |
| 256 | if (!download) |
| 257 | return; |
| 258 | |
| 259 | VLOG(20) << __FUNCTION__ << "()" |
| 260 | << " download = " << download->DebugString(true); |
| 261 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 262 | FilePath suggested_path = download->suggested_path(); |
| 263 | |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame] | 264 | if (download->prompt_user_for_save_location()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 265 | // We must ask the user for the place to put the download. |
[email protected] | db6831a | 2011-06-09 21:08:28 | [diff] [blame] | 266 | DownloadRequestHandle request_handle = download->request_handle(); |
| 267 | TabContents* contents = request_handle.GetTabContents(); |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 268 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 269 | // |id_ptr| will be deleted in either FileSelected() or |
| 270 | // FileSelectionCancelled(). |
| 271 | int32* id_ptr = new int32; |
| 272 | *id_ptr = download_id; |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 273 | |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame] | 274 | delegate_->ChooseDownloadPath( |
[email protected] | aa9881c | 2011-08-15 18:01:12 | [diff] [blame] | 275 | contents, suggested_path, reinterpret_cast<void*>(id_ptr)); |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 276 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 277 | FOR_EACH_OBSERVER(Observer, observers_, |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 278 | SelectFileDialogDisplayed(download_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | } else { |
| 280 | // No prompting for download, just continue with the suggested name. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 281 | ContinueDownloadWithPath(download, suggested_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 285 | void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
| 286 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 287 | |
| 288 | DownloadItem* download = new DownloadItem(this, *info, |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 289 | browser_context_->IsOffTheRecord()); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 290 | int32 download_id = info->download_id; |
| 291 | DCHECK(!ContainsKey(in_progress_, download_id)); |
| 292 | DCHECK(!ContainsKey(active_downloads_, download_id)); |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 293 | downloads_.insert(download); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 294 | active_downloads_[download_id] = download; |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 297 | void DownloadManager::ContinueDownloadWithPath(DownloadItem* download, |
| 298 | const FilePath& chosen_file) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 299 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 300 | DCHECK(download); |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 301 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 302 | int32 download_id = download->id(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 304 | // NOTE(ahendrickson) Eventually |active_downloads_| will replace |
| 305 | // |in_progress_|, but we don't want to change the semantics yet. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 306 | DCHECK(!ContainsKey(in_progress_, download_id)); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 307 | DCHECK(ContainsKey(downloads_, download)); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 308 | DCHECK(ContainsKey(active_downloads_, download_id)); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 309 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 310 | // Make sure the initial file name is set only once. |
| 311 | DCHECK(download->full_path().empty()); |
| 312 | download->OnPathDetermined(chosen_file); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 313 | |
| 314 | VLOG(20) << __FUNCTION__ << "()" |
| 315 | << " download = " << download->DebugString(true); |
| 316 | |
| 317 | in_progress_[download_id] = download; |
[email protected] | 5f8589fe | 2011-08-17 20:58:39 | [diff] [blame] | 318 | UpdateDownloadProgress(); // Reflect entry into in_progress_. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 320 | // Rename to intermediate name. |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 321 | FilePath download_path; |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 322 | if (download->IsDangerous()) { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 323 | // The download is not safe. We can now rename the file to its |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 324 | // tentative name using RenameInProgressDownloadFile. |
| 325 | // NOTE: The |Rename| below will be a no-op for dangerous files, as we're |
| 326 | // renaming it to the same name. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 327 | download_path = download->full_path(); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 328 | } else { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 329 | // The download is a safe download. We need to |
| 330 | // rename it to its intermediate '.crdownload' path. The final |
| 331 | // name after user confirmation will be set from |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 332 | // DownloadItem::OnDownloadCompleting. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 333 | download_path = |
| 334 | download_util::GetCrDownloadPath(download->full_path()); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 335 | } |
| 336 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 337 | BrowserThread::PostTask( |
| 338 | BrowserThread::FILE, FROM_HERE, |
| 339 | NewRunnableMethod( |
| 340 | file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, |
| 341 | download->id(), download_path)); |
| 342 | |
| 343 | download->Rename(download_path); |
| 344 | |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 345 | delegate_->AddItemToPersistentStore(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | } |
| 347 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 349 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 350 | DownloadMap::iterator it = active_downloads_.find(download_id); |
| 351 | if (it != active_downloads_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 352 | DownloadItem* download = it->second; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 353 | if (download->IsInProgress()) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 354 | download->Update(size); |
[email protected] | 5f8589fe | 2011-08-17 20:58:39 | [diff] [blame] | 355 | UpdateDownloadProgress(); // Reflect size updates. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 356 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 357 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 361 | void DownloadManager::OnResponseCompleted(int32 download_id, |
| 362 | int64 size, |
| 363 | int os_error, |
| 364 | const std::string& hash) { |
| 365 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 7c8e55ba | 2011-07-01 18:59:23 | [diff] [blame] | 366 | // ERR_CONNECTION_CLOSED is allowed since a number of servers in the wild |
| 367 | // advertise a larger Content-Length than the amount of bytes in the message |
| 368 | // body, and then close the connection. Other browsers - IE8, Firefox 4.0.1, |
| 369 | // and Safari 5.0.4 - treat the download as complete in this case, so we |
| 370 | // follow their lead. |
[email protected] | 1822a42 | 2011-07-15 15:49:19 | [diff] [blame] | 371 | if (os_error == 0 || os_error == net::ERR_CONNECTION_CLOSED) { |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 372 | OnAllDataSaved(download_id, size, hash); |
| 373 | } else { |
| 374 | OnDownloadError(download_id, size, os_error); |
| 375 | } |
| 376 | } |
| 377 | |
[email protected] | 2671173 | 2011-03-09 00:21:22 | [diff] [blame] | 378 | void DownloadManager::OnAllDataSaved(int32 download_id, |
| 379 | int64 size, |
| 380 | const std::string& hash) { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 381 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 382 | << " size = " << size; |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 383 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 384 | |
[email protected] | c4f02c4 | 2011-01-24 21:55:06 | [diff] [blame] | 385 | // If it's not in active_downloads_, that means it was cancelled; just |
| 386 | // ignore the notification. |
| 387 | if (active_downloads_.count(download_id) == 0) |
| 388 | return; |
| 389 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 390 | DownloadItem* download = active_downloads_[download_id]; |
[email protected] | a850ba4 | 2010-09-10 22:00:30 | [diff] [blame] | 391 | download->OnAllDataSaved(size); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 392 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 393 | MaybeCompleteDownload(download); |
| 394 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 395 | |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 396 | void DownloadManager::AssertQueueStateConsistent(DownloadItem* download) { |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 397 | // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 398 | if (download->state() == DownloadItem::REMOVING) { |
| 399 | CHECK(!ContainsKey(downloads_, download)); |
| 400 | CHECK(!ContainsKey(active_downloads_, download->id())); |
| 401 | CHECK(!ContainsKey(in_progress_, download->id())); |
| 402 | CHECK(!ContainsKey(history_downloads_, download->db_handle())); |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | // Should be in downloads_ if we're not REMOVING. |
| 407 | CHECK(ContainsKey(downloads_, download)); |
| 408 | |
| 409 | // Check history_downloads_ consistency. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 410 | if (download->db_handle() != DownloadItem::kUninitializedHandle) { |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 411 | CHECK(ContainsKey(history_downloads_, download->db_handle())); |
| 412 | } else { |
| 413 | // TODO(rdsmith): Somewhat painful; make sure to disable in |
| 414 | // release builds after resolution of http://crbug.com/85408. |
| 415 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 416 | it != history_downloads_.end(); ++it) { |
| 417 | CHECK(it->second != download); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | CHECK(ContainsKey(active_downloads_, download->id()) == |
| 422 | (download->state() == DownloadItem::IN_PROGRESS)); |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 423 | CHECK(ContainsKey(in_progress_, download->id()) == |
| 424 | (download->state() == DownloadItem::IN_PROGRESS)); |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 425 | } |
| 426 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 427 | bool DownloadManager::IsDownloadReadyForCompletion(DownloadItem* download) { |
| 428 | // If we don't have all the data, the download is not ready for |
| 429 | // completion. |
| 430 | if (!download->all_data_saved()) |
| 431 | return false; |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 432 | |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 433 | // If the download is dangerous, but not yet validated, it's not ready for |
| 434 | // completion. |
| 435 | if (download->safety_state() == DownloadItem::DANGEROUS) |
| 436 | return false; |
| 437 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 438 | // If the download isn't active (e.g. has been cancelled) it's not |
| 439 | // ready for completion. |
| 440 | if (active_downloads_.count(download->id()) == 0) |
| 441 | return false; |
| 442 | |
| 443 | // If the download hasn't been inserted into the history system |
| 444 | // (which occurs strictly after file name determination, intermediate |
| 445 | // file rename, and UI display) then it's not ready for completion. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 446 | if (download->db_handle() == DownloadItem::kUninitializedHandle) |
[email protected] | 7054b59 | 2011-06-22 14:46:42 | [diff] [blame] | 447 | return false; |
| 448 | |
| 449 | return true; |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void DownloadManager::MaybeCompleteDownload(DownloadItem* download) { |
| 453 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 454 | VLOG(20) << __FUNCTION__ << "()" << " download = " |
| 455 | << download->DebugString(false); |
| 456 | |
| 457 | if (!IsDownloadReadyForCompletion(download)) |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 458 | return; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 459 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 460 | // TODO(rdsmith): DCHECK that we only pass through this point |
| 461 | // once per download. The natural way to do this is by a state |
| 462 | // transition on the DownloadItem. |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 463 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 464 | // Confirm we're in the proper set of states to be here; |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 465 | // in in_progress_, have all data, have a history handle, (validated or safe). |
| 466 | DCHECK_NE(DownloadItem::DANGEROUS, download->safety_state()); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 467 | DCHECK_EQ(1u, in_progress_.count(download->id())); |
| 468 | DCHECK(download->all_data_saved()); |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 469 | DCHECK(download->db_handle() != DownloadItem::kUninitializedHandle); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 470 | DCHECK_EQ(1u, history_downloads_.count(download->db_handle())); |
| 471 | |
| 472 | VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
| 473 | << download->DebugString(false); |
| 474 | |
| 475 | // Remove the id from in_progress |
| 476 | in_progress_.erase(download->id()); |
[email protected] | 5f8589fe | 2011-08-17 20:58:39 | [diff] [blame] | 477 | UpdateDownloadProgress(); // Reflect removal from in_progress_. |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 478 | |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 479 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 480 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 481 | // Finish the download. |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 482 | download->OnDownloadCompleting(file_manager_); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 483 | } |
| 484 | |
[email protected] | cc3c7c09 | 2011-05-09 18:40:21 | [diff] [blame] | 485 | void DownloadManager::DownloadCompleted(int32 download_id) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 486 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | cc3c7c09 | 2011-05-09 18:40:21 | [diff] [blame] | 487 | DownloadItem* download = GetDownloadItem(download_id); |
| 488 | DCHECK(download); |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 489 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 490 | active_downloads_.erase(download_id); |
| 491 | } |
| 492 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 493 | void DownloadManager::OnDownloadRenamedToFinalName(int download_id, |
| 494 | const FilePath& full_path, |
| 495 | int uniquifier) { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 496 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 497 | << " full_path = \"" << full_path.value() << "\"" |
| 498 | << " uniquifier = " << uniquifier; |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 499 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 500 | |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 501 | DownloadItem* item = GetDownloadItem(download_id); |
| 502 | if (!item) |
| 503 | return; |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 504 | |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 505 | if (item->safety_state() == DownloadItem::SAFE) { |
| 506 | DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
| 507 | } |
| 508 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 509 | BrowserThread::PostTask( |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 510 | BrowserThread::FILE, FROM_HERE, |
| 511 | NewRunnableMethod( |
| 512 | file_manager_, &DownloadFileManager::CompleteDownload, download_id)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 513 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 514 | if (uniquifier) |
| 515 | item->set_path_uniquifier(uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 516 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 517 | item->OnDownloadRenamedToFinalName(full_path); |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 518 | delegate_->UpdatePathForItemInPersistentStore(item, full_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 519 | } |
| 520 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 521 | void DownloadManager::DownloadCancelled(int32 download_id) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 522 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 523 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 524 | if (it == in_progress_.end()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 525 | return; |
| 526 | DownloadItem* download = it->second; |
| 527 | |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 528 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 529 | << " download = " << download->DebugString(true); |
| 530 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 531 | // Clean up will happen when the history system create callback runs if we |
| 532 | // don't have a valid db_handle yet. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 533 | if (download->db_handle() != DownloadItem::kUninitializedHandle) { |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 534 | in_progress_.erase(it); |
| 535 | active_downloads_.erase(download_id); |
[email protected] | 5f8589fe | 2011-08-17 20:58:39 | [diff] [blame] | 536 | UpdateDownloadProgress(); // Reflect removal from in_progress_. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 537 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 538 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 539 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 540 | DownloadCancelledInternal(download_id, download->request_handle()); |
| 541 | } |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 542 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 543 | void DownloadManager::DownloadCancelledInternal( |
| 544 | int download_id, const DownloadRequestHandle& request_handle) { |
| 545 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 546 | request_handle.CancelRequest(); |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 547 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 548 | BrowserThread::PostTask( |
| 549 | BrowserThread::FILE, FROM_HERE, |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 550 | NewRunnableMethod( |
| 551 | file_manager_, &DownloadFileManager::CancelDownload, download_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | } |
| 553 | |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 554 | void DownloadManager::OnDownloadError(int32 download_id, |
| 555 | int64 size, |
| 556 | int os_error) { |
| 557 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 558 | DownloadMap::iterator it = active_downloads_.find(download_id); |
| 559 | // A cancel at the right time could remove the download from the |
| 560 | // |active_downloads_| map before we get here. |
| 561 | if (it == active_downloads_.end()) |
| 562 | return; |
| 563 | |
| 564 | DownloadItem* download = it->second; |
| 565 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 566 | VLOG(20) << __FUNCTION__ << "()" << " Error " << os_error |
| 567 | << " at offset " << download->received_bytes() |
| 568 | << " for download = " << download->DebugString(true); |
| 569 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 570 | download->Interrupted(size, os_error); |
| 571 | |
| 572 | // TODO(ahendrickson) - Remove this when we add resuming of interrupted |
| 573 | // downloads, as we will keep the download item around in that case. |
| 574 | // |
| 575 | // Clean up will happen when the history system create callback runs if we |
| 576 | // don't have a valid db_handle yet. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 577 | if (download->db_handle() != DownloadItem::kUninitializedHandle) { |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 578 | in_progress_.erase(download_id); |
| 579 | active_downloads_.erase(download_id); |
[email protected] | 5f8589fe | 2011-08-17 20:58:39 | [diff] [blame] | 580 | UpdateDownloadProgress(); // Reflect removal from in_progress_. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 581 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | BrowserThread::PostTask( |
| 585 | BrowserThread::FILE, FROM_HERE, |
| 586 | NewRunnableMethod( |
| 587 | file_manager_, &DownloadFileManager::CancelDownload, download_id)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 588 | } |
| 589 | |
[email protected] | 5f8589fe | 2011-08-17 20:58:39 | [diff] [blame] | 590 | void DownloadManager::UpdateDownloadProgress() { |
| 591 | delegate_->DownloadProgressUpdated(); |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 592 | } |
| 593 | |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 594 | int DownloadManager::RemoveDownloadItems( |
| 595 | const DownloadVector& pending_deletes) { |
| 596 | if (pending_deletes.empty()) |
| 597 | return 0; |
| 598 | |
| 599 | // Delete from internal maps. |
| 600 | for (DownloadVector::const_iterator it = pending_deletes.begin(); |
| 601 | it != pending_deletes.end(); |
| 602 | ++it) { |
| 603 | DownloadItem* download = *it; |
| 604 | DCHECK(download); |
| 605 | history_downloads_.erase(download->db_handle()); |
| 606 | save_page_downloads_.erase(download->id()); |
| 607 | downloads_.erase(download); |
| 608 | } |
| 609 | |
| 610 | // Tell observers to refresh their views. |
| 611 | NotifyModelChanged(); |
| 612 | |
| 613 | // Delete the download items themselves. |
| 614 | const int num_deleted = static_cast<int>(pending_deletes.size()); |
| 615 | STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end()); |
| 616 | return num_deleted; |
| 617 | } |
| 618 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 619 | void DownloadManager::RemoveDownload(int64 download_handle) { |
| 620 | DownloadMap::iterator it = history_downloads_.find(download_handle); |
| 621 | if (it == history_downloads_.end()) |
| 622 | return; |
| 623 | |
| 624 | // Make history update. |
| 625 | DownloadItem* download = it->second; |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 626 | delegate_->RemoveItemFromPersistentStore(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 627 | |
| 628 | // Remove from our tables and delete. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 629 | int downloads_count = RemoveDownloadItems(DownloadVector(1, download)); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 630 | DCHECK_EQ(1, downloads_count); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 631 | } |
| 632 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 633 | int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin, |
| 634 | const base::Time remove_end) { |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 635 | delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 636 | |
[email protected] | a312a44 | 2010-12-15 23:40:33 | [diff] [blame] | 637 | // All downloads visible to the user will be in the history, |
| 638 | // so scan that map. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 639 | DownloadVector pending_deletes; |
| 640 | for (DownloadMap::const_iterator it = history_downloads_.begin(); |
| 641 | it != history_downloads_.end(); |
| 642 | ++it) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 643 | DownloadItem* download = it->second; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 644 | if (download->start_time() >= remove_begin && |
| 645 | (remove_end.is_null() || download->start_time() < remove_end) && |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 646 | (download->IsComplete() || download->IsCancelled())) { |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 647 | AssertQueueStateConsistent(download); |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 648 | pending_deletes.push_back(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 649 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 650 | } |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 651 | return RemoveDownloadItems(pending_deletes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 652 | } |
| 653 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 654 | int DownloadManager::RemoveDownloads(const base::Time remove_begin) { |
| 655 | return RemoveDownloadsBetween(remove_begin, base::Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 656 | } |
| 657 | |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 658 | int DownloadManager::RemoveAllDownloads() { |
| 659 | // The null times make the date range unbounded. |
| 660 | return RemoveDownloadsBetween(base::Time(), base::Time()); |
| 661 | } |
| 662 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 663 | // Initiate a download of a specific URL. We send the request to the |
| 664 | // ResourceDispatcherHost, and let it send us responses like a regular |
| 665 | // download. |
| 666 | void DownloadManager::DownloadUrl(const GURL& url, |
| 667 | const GURL& referrer, |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 668 | const std::string& referrer_charset, |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 669 | TabContents* tab_contents) { |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 670 | DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(), |
| 671 | tab_contents); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | void DownloadManager::DownloadUrlToFile(const GURL& url, |
| 675 | const GURL& referrer, |
| 676 | const std::string& referrer_charset, |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 677 | const DownloadSaveInfo& save_info, |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 678 | TabContents* tab_contents) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 679 | DCHECK(tab_contents); |
[email protected] | c79a0c0 | 2011-08-22 22:37:37 | [diff] [blame] | 680 | ResourceDispatcherHost* resource_dispatcher_host = |
| 681 | content::GetContentClient()->browser()->GetResourceDispatcherHost(); |
[email protected] | ed24fad | 2011-05-10 22:44:01 | [diff] [blame] | 682 | // We send a pointer to content::ResourceContext, instead of the usual |
| 683 | // reference, so that a copy of the object isn't made. |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 684 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 685 | NewRunnableFunction(&BeginDownload, |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 686 | url, |
| 687 | referrer, |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 688 | save_info, |
[email protected] | c79a0c0 | 2011-08-22 22:37:37 | [diff] [blame] | 689 | resource_dispatcher_host, |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 690 | tab_contents->GetRenderProcessHost()->id(), |
| 691 | tab_contents->render_view_host()->routing_id(), |
[email protected] | cafe4ad | 2011-07-28 18:34:56 | [diff] [blame] | 692 | &tab_contents->browser_context()-> |
| 693 | GetResourceContext())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 694 | } |
| 695 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 696 | void DownloadManager::AddObserver(Observer* observer) { |
| 697 | observers_.AddObserver(observer); |
| 698 | observer->ModelChanged(); |
| 699 | } |
| 700 | |
| 701 | void DownloadManager::RemoveObserver(Observer* observer) { |
| 702 | observers_.RemoveObserver(observer); |
| 703 | } |
| 704 | |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 705 | bool DownloadManager::IsDownloadProgressKnown() { |
| 706 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 707 | i != in_progress_.end(); ++i) { |
| 708 | if (i->second->total_bytes() <= 0) |
| 709 | return false; |
| 710 | } |
| 711 | |
| 712 | return true; |
| 713 | } |
| 714 | |
| 715 | int64 DownloadManager::GetInProgressDownloadCount() { |
| 716 | return in_progress_.size(); |
| 717 | } |
| 718 | |
| 719 | int64 DownloadManager::GetReceivedDownloadBytes() { |
| 720 | DCHECK(IsDownloadProgressKnown()); |
| 721 | int64 received_bytes = 0; |
| 722 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 723 | i != in_progress_.end(); ++i) { |
| 724 | received_bytes += i->second->received_bytes(); |
| 725 | } |
| 726 | return received_bytes; |
| 727 | } |
| 728 | |
| 729 | int64 DownloadManager::GetTotalDownloadBytes() { |
| 730 | DCHECK(IsDownloadProgressKnown()); |
| 731 | int64 total_bytes = 0; |
| 732 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 733 | i != in_progress_.end(); ++i) { |
| 734 | total_bytes += i->second->total_bytes(); |
| 735 | } |
| 736 | return total_bytes; |
| 737 | } |
| 738 | |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 739 | void DownloadManager::FileSelected(const FilePath& path, void* params) { |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 740 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 741 | |
| 742 | int32* id_ptr = reinterpret_cast<int32*>(params); |
| 743 | DCHECK(id_ptr != NULL); |
| 744 | int32 download_id = *id_ptr; |
| 745 | delete id_ptr; |
| 746 | |
| 747 | DownloadItem* download = GetActiveDownloadItem(download_id); |
| 748 | if (!download) |
| 749 | return; |
| 750 | VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\"" |
| 751 | << " download = " << download->DebugString(true); |
| 752 | |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame] | 753 | if (download->prompt_user_for_save_location()) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 754 | last_download_path_ = path.DirName(); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 755 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 756 | // Make sure the initial file name is set only once. |
| 757 | ContinueDownloadWithPath(download, path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | void DownloadManager::FileSelectionCanceled(void* params) { |
| 761 | // The user didn't pick a place to save the file, so need to cancel the |
| 762 | // download that's already in progress to the temporary location. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 763 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 764 | int32* id_ptr = reinterpret_cast<int32*>(params); |
| 765 | DCHECK(id_ptr != NULL); |
| 766 | int32 download_id = *id_ptr; |
| 767 | delete id_ptr; |
| 768 | |
| 769 | DownloadItem* download = GetActiveDownloadItem(download_id); |
| 770 | if (!download) |
| 771 | return; |
| 772 | |
| 773 | VLOG(20) << __FUNCTION__ << "()" |
| 774 | << " download = " << download->DebugString(true); |
| 775 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 776 | DownloadCancelledInternal(download_id, download->request_handle()); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 777 | } |
| 778 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 779 | // Operations posted to us from the history service ---------------------------- |
| 780 | |
| 781 | // The history service has retrieved all download entries. 'entries' contains |
[email protected] | bb1a421 | 2011-08-22 22:38:25 | [diff] [blame^] | 782 | // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 783 | void DownloadManager::OnPersistentStoreQueryComplete( |
[email protected] | bb1a421 | 2011-08-22 22:38:25 | [diff] [blame^] | 784 | std::vector<DownloadPersistentStoreInfo>* entries) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 785 | for (size_t i = 0; i < entries->size(); ++i) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 786 | DownloadItem* download = new DownloadItem(this, entries->at(i)); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 787 | DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
| 788 | downloads_.insert(download); |
| 789 | history_downloads_[download->db_handle()] = download; |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 790 | VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 791 | << " download = " << download->DebugString(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 792 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 793 | NotifyModelChanged(); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 794 | CheckForHistoryFilesRemoval(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 795 | } |
| 796 | |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 797 | void DownloadManager::AddDownloadItemToHistory(DownloadItem* download, |
| 798 | int64 db_handle) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 799 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 800 | |
[email protected] | 1e9fe7ff | 2011-06-24 17:37:33 | [diff] [blame] | 801 | // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508 |
| 802 | // is fixed. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 803 | CHECK_NE(DownloadItem::kUninitializedHandle, db_handle); |
[email protected] | 1e9fe7ff | 2011-06-24 17:37:33 | [diff] [blame] | 804 | |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 805 | DCHECK(download->db_handle() == DownloadItem::kUninitializedHandle); |
[email protected] | 5bcd73eb | 2011-03-23 21:14:02 | [diff] [blame] | 806 | download->set_db_handle(db_handle); |
| 807 | |
[email protected] | 5bcd73eb | 2011-03-23 21:14:02 | [diff] [blame] | 808 | DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
| 809 | history_downloads_[download->db_handle()] = download; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 810 | |
| 811 | // Show in the appropriate browser UI. |
| 812 | // This includes buttons to save or cancel, for a dangerous download. |
| 813 | ShowDownloadInBrowser(download); |
| 814 | |
| 815 | // Inform interested objects about the new download. |
| 816 | NotifyModelChanged(); |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 817 | } |
| 818 | |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 819 | |
| 820 | void DownloadManager::OnItemAddedToPersistentStore(int32 download_id, |
| 821 | int64 db_handle) { |
| 822 | if (save_page_downloads_.count(download_id)) { |
| 823 | OnSavePageItemAddedToPersistentStore(download_id, db_handle); |
| 824 | } else if (active_downloads_.count(download_id)) { |
| 825 | OnDownloadItemAddedToPersistentStore(download_id, db_handle); |
| 826 | } |
| 827 | // It's valid that we don't find a matching item, i.e. on shutdown. |
| 828 | } |
| 829 | |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 830 | // Once the new DownloadItem's creation info has been committed to the history |
| 831 | // service, we associate the DownloadItem with the db handle, update our |
| 832 | // 'history_downloads_' map and inform observers. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 833 | void DownloadManager::OnDownloadItemAddedToPersistentStore(int32 download_id, |
| 834 | int64 db_handle) { |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 835 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 19420cc | 2011-07-18 17:43:45 | [diff] [blame] | 836 | DownloadItem* download = GetActiveDownloadItem(download_id); |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 837 | if (!download) |
[email protected] | 19420cc | 2011-07-18 17:43:45 | [diff] [blame] | 838 | return; |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 839 | |
| 840 | VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle |
| 841 | << " download_id = " << download_id |
| 842 | << " download = " << download->DebugString(true); |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 843 | |
| 844 | AddDownloadItemToHistory(download, db_handle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 845 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 846 | // If the download is still in progress, try to complete it. |
| 847 | // |
| 848 | // Otherwise, download has been cancelled or interrupted before we've |
| 849 | // received the DB handle. We post one final message to the history |
| 850 | // service so that it can be properly in sync with the DownloadItem's |
| 851 | // completion status, and also inform any observers so that they get |
| 852 | // more than just the start notification. |
| 853 | if (download->IsInProgress()) { |
| 854 | MaybeCompleteDownload(download); |
| 855 | } else { |
| 856 | DCHECK(download->IsCancelled()) |
| 857 | << " download = " << download->DebugString(true); |
| 858 | in_progress_.erase(download_id); |
| 859 | active_downloads_.erase(download_id); |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 860 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 861 | download->UpdateObservers(); |
| 862 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 863 | } |
| 864 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 865 | void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) { |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 866 | // The 'contents' may no longer exist if the user closed the tab before we |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 867 | // get this start completion event. |
[email protected] | db6831a | 2011-06-09 21:08:28 | [diff] [blame] | 868 | DownloadRequestHandle request_handle = download->request_handle(); |
[email protected] | 68649314 | 2011-07-15 21:47:22 | [diff] [blame] | 869 | TabContents* content = request_handle.GetTabContents(); |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 870 | |
| 871 | // If the contents no longer exists, we ask the embedder to suggest another |
| 872 | // tab. |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame] | 873 | if (!content) |
[email protected] | aa9881c | 2011-08-15 18:01:12 | [diff] [blame] | 874 | content = delegate_->GetAlternativeTabContentsToNotifyForDownload(); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 875 | |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 876 | if (content) |
| 877 | content->OnStartDownload(download); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 878 | } |
| 879 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 880 | // Clears the last download path, used to initialize "save as" dialogs. |
[email protected] | 905a08d | 2008-11-19 07:24:12 | [diff] [blame] | 881 | void DownloadManager::ClearLastDownloadPath() { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 882 | last_download_path_ = FilePath(); |
[email protected] | eea4662 | 2009-07-15 20:49:38 | [diff] [blame] | 883 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 884 | |
| 885 | void DownloadManager::NotifyModelChanged() { |
| 886 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
| 887 | } |
| 888 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 889 | DownloadItem* DownloadManager::GetDownloadItem(int download_id) { |
| 890 | // The |history_downloads_| map is indexed by the download's db_handle, |
| 891 | // not its id, so we have to iterate. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 892 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 893 | it != history_downloads_.end(); ++it) { |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 894 | DownloadItem* item = it->second; |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 895 | if (item->id() == download_id) |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 896 | return item; |
| 897 | } |
| 898 | return NULL; |
| 899 | } |
| 900 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 901 | DownloadItem* DownloadManager::GetActiveDownloadItem(int download_id) { |
| 902 | DCHECK(ContainsKey(active_downloads_, download_id)); |
| 903 | DownloadItem* download = active_downloads_[download_id]; |
| 904 | DCHECK(download != NULL); |
| 905 | return download; |
| 906 | } |
| 907 | |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 908 | // Confirm that everything in all maps is also in |downloads_|, and that |
| 909 | // everything in |downloads_| is also in some other map. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 910 | void DownloadManager::AssertContainersConsistent() const { |
| 911 | #if !defined(NDEBUG) |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 912 | // Turn everything into sets. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 913 | const DownloadMap* input_maps[] = {&active_downloads_, |
| 914 | &history_downloads_, |
| 915 | &save_page_downloads_}; |
| 916 | DownloadSet active_set, history_set, save_page_set; |
| 917 | DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set}; |
| 918 | DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets)); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 919 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) { |
| 920 | for (DownloadMap::const_iterator it = input_maps[i]->begin(); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 921 | it != input_maps[i]->end(); ++it) { |
| 922 | all_sets[i]->insert(&*it->second); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 923 | } |
| 924 | } |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 925 | |
| 926 | // Check if each set is fully present in downloads, and create a union. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 927 | DownloadSet downloads_union; |
| 928 | for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) { |
| 929 | DownloadSet remainder; |
| 930 | std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
| 931 | std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
| 932 | downloads_.begin(), downloads_.end(), |
| 933 | insert_it); |
| 934 | DCHECK(remainder.empty()); |
| 935 | std::insert_iterator<DownloadSet> |
| 936 | insert_union(downloads_union, downloads_union.end()); |
| 937 | std::set_union(downloads_union.begin(), downloads_union.end(), |
| 938 | all_sets[i]->begin(), all_sets[i]->end(), |
| 939 | insert_union); |
| 940 | } |
| 941 | |
| 942 | // Is everything in downloads_ present in one of the other sets? |
| 943 | DownloadSet remainder; |
| 944 | std::insert_iterator<DownloadSet> |
| 945 | insert_remainder(remainder, remainder.begin()); |
| 946 | std::set_difference(downloads_.begin(), downloads_.end(), |
| 947 | downloads_union.begin(), downloads_union.end(), |
| 948 | insert_remainder); |
| 949 | DCHECK(remainder.empty()); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 950 | #endif |
| 951 | } |
| 952 | |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 953 | void DownloadManager::SavePageDownloadStarted(DownloadItem* download) { |
| 954 | DCHECK(!ContainsKey(save_page_downloads_, download->id())); |
| 955 | downloads_.insert(download); |
| 956 | save_page_downloads_[download->id()] = download; |
| 957 | |
| 958 | // Add this entry to the history service. |
| 959 | // Additionally, the UI is notified in the callback. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 960 | delegate_->AddItemToPersistentStore(download); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | // SavePackage will call SavePageDownloadFinished upon completion/cancellation. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 964 | // The history callback will call OnSavePageItemAddedToPersistentStore. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 965 | // If the download finishes before the history callback, |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 966 | // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring |
| 967 | // that the history event is update regardless of the order in which these two |
| 968 | // events complete. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 969 | // If something removes the download item from the download manager (Remove, |
| 970 | // Shutdown) the result will be that the SavePage system will not be able to |
| 971 | // properly update the download item (which no longer exists) or the download |
| 972 | // history, but the action will complete properly anyway. This may lead to the |
| 973 | // history entry being wrong on a reload of chrome (specifically in the case of |
| 974 | // Initiation -> History Callback -> Removal -> Completion), but there's no way |
| 975 | // to solve that without canceling on Remove (which would then update the DB). |
| 976 | |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 977 | void DownloadManager::OnSavePageItemAddedToPersistentStore(int32 download_id, |
| 978 | int64 db_handle) { |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 979 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 980 | |
| 981 | DownloadMap::const_iterator it = save_page_downloads_.find(download_id); |
| 982 | // This can happen if the download manager is shutting down and all maps |
| 983 | // have been cleared. |
| 984 | if (it == save_page_downloads_.end()) |
| 985 | return; |
| 986 | |
| 987 | DownloadItem* download = it->second; |
| 988 | if (!download) { |
| 989 | NOTREACHED(); |
| 990 | return; |
| 991 | } |
| 992 | |
| 993 | AddDownloadItemToHistory(download, db_handle); |
| 994 | |
| 995 | // Finalize this download if it finished before the history callback. |
| 996 | if (!download->IsInProgress()) |
| 997 | SavePageDownloadFinished(download); |
| 998 | } |
| 999 | |
| 1000 | void DownloadManager::SavePageDownloadFinished(DownloadItem* download) { |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 1001 | if (download->db_handle() != DownloadItem::kUninitializedHandle) { |
| 1002 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1003 | DCHECK(ContainsKey(save_page_downloads_, download->id())); |
| 1004 | save_page_downloads_.erase(download->id()); |
| 1005 | |
| 1006 | if (download->IsComplete()) |
| 1007 | NotificationService::current()->Notify( |
| 1008 | content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 1009 | Source<DownloadManager>(this), |
| 1010 | Details<DownloadItem>(download)); |
| 1011 | } |
| 1012 | } |