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