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