[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] | cdaa865 | 2008-09-13 02:48:59 | [diff] [blame] | 5 | #include "chrome/browser/download/download_manager.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 7 | #include "base/callback.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 9 | #include "base/i18n/case_conversion.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 11 | #include "base/path_service.h" |
[email protected] | 1b5044d | 2009-02-24 00:04:14 | [diff] [blame] | 12 | #include "base/rand_util.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 13 | #include "base/stl_util.h" |
[email protected] | 2594c2b | 2010-11-08 23:04:26 | [diff] [blame] | 14 | #include "base/stringprintf.h" |
[email protected] | 1b5044d | 2009-02-24 00:04:14 | [diff] [blame] | 15 | #include "base/sys_string_conversions.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | #include "base/task.h" |
[email protected] | ce7f62e3 | 2010-08-10 23:43:59 | [diff] [blame] | 17 | #include "base/utf_string_conversions.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 18 | #include "build/build_config.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "chrome/browser/browser_process.h" |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 20 | #include "chrome/browser/download/download_create_info.h" |
[email protected] | cd44809 | 2010-12-06 23:49:13 | [diff] [blame] | 21 | #include "chrome/browser/download/download_extensions.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 22 | #include "chrome/browser/download/download_file_manager.h" |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 23 | #include "chrome/browser/download/download_history.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 24 | #include "chrome/browser/download/download_item.h" |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame^] | 25 | #include "chrome/browser/download/download_manager_delegate.h" |
[email protected] | e5dc422 | 2010-08-30 22:16:32 | [diff] [blame] | 26 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | db6831a | 2011-06-09 21:08:28 | [diff] [blame] | 27 | #include "chrome/browser/download/download_request_handle.h" |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 28 | #include "chrome/browser/download/download_safe_browsing_client.h" |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 29 | #include "chrome/browser/download/download_status_updater.h" |
[email protected] | e9ef0a6 | 2009-08-11 22:50:13 | [diff] [blame] | 30 | #include "chrome/browser/download/download_util.h" |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 31 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 32 | #include "chrome/browser/history/download_history_info.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 33 | #include "chrome/browser/profiles/profile.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 34 | #include "chrome/browser/tab_contents/tab_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | #include "chrome/common/chrome_paths.h" |
[email protected] | 8c40da6 | 2011-07-13 22:58:46 | [diff] [blame] | 36 | #include "chrome/common/pref_names.h" |
[email protected] | 7324d1d | 2011-03-01 05:02:16 | [diff] [blame] | 37 | #include "content/browser/browser_thread.h" |
[email protected] | 7324d1d | 2011-03-01 05:02:16 | [diff] [blame] | 38 | #include "content/browser/renderer_host/render_process_host.h" |
| 39 | #include "content/browser/renderer_host/render_view_host.h" |
| 40 | #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 41 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 42 | #include "content/common/content_notification_types.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 43 | #include "googleurl/src/gurl.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 44 | #include "grit/generated_resources.h" |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 45 | #include "grit/theme_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | #include "net/base/mime_util.h" |
| 47 | #include "net/base/net_util.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 48 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 49 | #include "ui/base/resource/resource_bundle.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame^] | 51 | DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, |
| 52 | DownloadStatusUpdater* status_updater) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | : shutdown_needed_(false), |
| 54 | profile_(NULL), |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 55 | file_manager_(NULL), |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame^] | 56 | status_updater_(status_updater->AsWeakPtr()), |
| 57 | delegate_(delegate) { |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 58 | if (status_updater_) |
| 59 | status_updater_->AddDelegate(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | DownloadManager::~DownloadManager() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 63 | DCHECK(!shutdown_needed_); |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 64 | if (status_updater_) |
| 65 | status_updater_->RemoveDelegate(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void DownloadManager::Shutdown() { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 69 | VLOG(20) << __FUNCTION__ << "()" |
| 70 | << " shutdown_needed_ = " << shutdown_needed_; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 71 | if (!shutdown_needed_) |
| 72 | return; |
| 73 | shutdown_needed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 75 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); |
| 76 | |
| 77 | if (file_manager_) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 78 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 79 | NewRunnableMethod(file_manager_, |
| 80 | &DownloadFileManager::OnDownloadManagerShutdown, |
[email protected] | dc7cdcb9 | 2010-12-14 06:40:54 | [diff] [blame] | 81 | make_scoped_refptr(this))); |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 82 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 84 | AssertContainersConsistent(); |
| 85 | |
| 86 | // Go through all downloads in downloads_. Dangerous ones we need to |
| 87 | // remove on disk, and in progress ones we need to cancel. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 88 | for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 89 | DownloadItem* download = *it; |
| 90 | |
| 91 | // Save iterator from potential erases in this set done by called code. |
| 92 | // Iterators after an erasure point are still valid for lists and |
| 93 | // associative containers such as sets. |
| 94 | it++; |
| 95 | |
| 96 | if (download->safety_state() == DownloadItem::DANGEROUS && |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 97 | download->IsPartialDownload()) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 98 | // The user hasn't accepted it, so we need to remove it |
| 99 | // from the disk. This may or may not result in it being |
| 100 | // removed from the DownloadManager queues and deleted |
| 101 | // (specifically, DownloadManager::RemoveDownload only |
| 102 | // removes and deletes it if it's known to the history service) |
| 103 | // so the only thing we know after calling this function is that |
| 104 | // the download was deleted if-and-only-if it was removed |
| 105 | // from all queues. |
[email protected] | 30307700 | 2011-04-19 23:21:01 | [diff] [blame] | 106 | download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 107 | } else if (download->IsPartialDownload()) { |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 108 | download->Cancel(false); |
| 109 | download_history_->UpdateEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 113 | // At this point, all dangerous downloads have had their files removed |
| 114 | // and all in progress downloads have been cancelled. We can now delete |
| 115 | // anything left. |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 116 | |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 117 | // Copy downloads_ to separate container so as not to set off checks |
| 118 | // in DownloadItem destruction. |
| 119 | DownloadSet downloads_to_delete; |
| 120 | downloads_to_delete.swap(downloads_); |
| 121 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | in_progress_.clear(); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 123 | active_downloads_.clear(); |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 124 | history_downloads_.clear(); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 125 | #if !defined(NDEBUG) |
| 126 | save_page_as_downloads_.clear(); |
| 127 | #endif |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 128 | STLDeleteElements(&downloads_to_delete); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | |
| 130 | file_manager_ = NULL; |
| 131 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 132 | download_history_.reset(); |
[email protected] | 68a49e5 | 2011-01-28 10:39:51 | [diff] [blame] | 133 | download_prefs_.reset(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 134 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | shutdown_needed_ = false; |
| 136 | } |
| 137 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 138 | void DownloadManager::GetTemporaryDownloads( |
| 139 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 140 | DCHECK(result); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 141 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 142 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 143 | it != history_downloads_.end(); ++it) { |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 144 | if (it->second->is_temporary() && |
| 145 | it->second->full_path().DirName() == dir_path) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 146 | result->push_back(it->second); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 147 | } |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 150 | void DownloadManager::GetAllDownloads( |
| 151 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 152 | DCHECK(result); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 153 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 154 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 155 | it != history_downloads_.end(); ++it) { |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 156 | if (!it->second->is_temporary() && |
| 157 | (dir_path.empty() || it->second->full_path().DirName() == dir_path)) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 158 | result->push_back(it->second); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 159 | } |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 162 | void DownloadManager::GetCurrentDownloads( |
| 163 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 164 | DCHECK(result); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 165 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 166 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 167 | it != history_downloads_.end(); ++it) { |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 168 | DownloadItem* item =it->second; |
| 169 | // Skip temporary items. |
| 170 | if (item->is_temporary()) |
| 171 | continue; |
| 172 | // Skip items that have all their data, and are OK to save. |
| 173 | if (!item->IsPartialDownload() && |
| 174 | (item->safety_state() != DownloadItem::DANGEROUS)) |
| 175 | continue; |
| 176 | // Skip items that don't match |dir_path|. |
| 177 | // If |dir_path| is empty, all remaining items match. |
| 178 | if (!dir_path.empty() && (it->second->full_path().DirName() != dir_path)) |
| 179 | continue; |
| 180 | |
| 181 | result->push_back(item); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 182 | } |
[email protected] | f7e9fd6 | 2010-09-28 15:45:06 | [diff] [blame] | 183 | |
| 184 | // If we have a parent profile, let it add its downloads to the results. |
| 185 | Profile* original_profile = profile_->GetOriginalProfile(); |
| 186 | if (original_profile != profile_) |
| 187 | original_profile->GetDownloadManager()->GetCurrentDownloads(dir_path, |
| 188 | result); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 191 | void DownloadManager::SearchDownloads(const string16& query, |
| 192 | std::vector<DownloadItem*>* result) { |
| 193 | DCHECK(result); |
| 194 | |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 195 | string16 query_lower(base::i18n::ToLower(query)); |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 196 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 197 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 198 | it != history_downloads_.end(); ++it) { |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 199 | DownloadItem* download_item = it->second; |
| 200 | |
| 201 | if (download_item->is_temporary() || download_item->is_extension_install()) |
| 202 | continue; |
| 203 | |
| 204 | // Display Incognito downloads only in Incognito window, and vice versa. |
| 205 | // The Incognito Downloads page will get the list of non-Incognito downloads |
| 206 | // from its parent profile. |
| 207 | if (profile_->IsOffTheRecord() != download_item->is_otr()) |
| 208 | continue; |
| 209 | |
| 210 | if (download_item->MatchesQuery(query_lower)) |
| 211 | result->push_back(download_item); |
| 212 | } |
| 213 | |
| 214 | // If we have a parent profile, let it add its downloads to the results. |
| 215 | Profile* original_profile = profile_->GetOriginalProfile(); |
| 216 | if (original_profile != profile_) |
| 217 | original_profile->GetDownloadManager()->SearchDownloads(query, result); |
| 218 | } |
| 219 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | // Query the history service for information about all persisted downloads. |
| 221 | bool DownloadManager::Init(Profile* profile) { |
| 222 | DCHECK(profile); |
| 223 | DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 224 | shutdown_needed_ = true; |
| 225 | |
| 226 | profile_ = profile; |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 227 | download_history_.reset(new DownloadHistory(profile)); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 228 | download_history_->Load( |
| 229 | NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 230 | |
[email protected] | e5dc422 | 2010-08-30 22:16:32 | [diff] [blame] | 231 | download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs())); |
| 232 | |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 233 | // In test mode, there may be no ResourceDispatcherHost. In this case it's |
| 234 | // safe to avoid setting |file_manager_| because we only call a small set of |
| 235 | // functions, none of which need it. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 237 | if (rdh) { |
| 238 | file_manager_ = rdh->download_file_manager(); |
| 239 | DCHECK(file_manager_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 242 | other_download_manager_observer_.reset( |
| 243 | new OtherDownloadManagerObserver(this)); |
| 244 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | // We have received a message from DownloadFileManager about a new download. We |
| 249 | // create a download item and store it in our download map, and inform the |
| 250 | // history system of a new download. Since this method can be called while the |
| 251 | // history service thread is still reading the persistent state, we do not |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 252 | // insert the new DownloadItem into 'history_downloads_' or inform our |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 253 | // observers at this point. OnCreateDownloadEntryComplete() handles that |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 254 | // finalization of the the download creation as a callback from the |
| 255 | // history thread. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 256 | void DownloadManager::StartDownload(int32 download_id) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 257 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 258 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 259 | DownloadItem* download = GetActiveDownloadItem(download_id); |
| 260 | if (!download) |
| 261 | return; |
| 262 | |
[email protected] | 4b58e7d | 2011-07-11 10:22:56 | [diff] [blame] | 263 | #if defined(ENABLE_SAFE_BROWSING) |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 264 | // Create a client to verify download URL with safebrowsing. |
| 265 | // It deletes itself after the callback. |
[email protected] | 2671173 | 2011-03-09 00:21:22 | [diff] [blame] | 266 | scoped_refptr<DownloadSBClient> sb_client = new DownloadSBClient( |
[email protected] | 8c40da6 | 2011-07-13 22:58:46 | [diff] [blame] | 267 | download_id, download->url_chain(), download->referrer_url(), |
| 268 | profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 269 | sb_client->CheckDownloadUrl( |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 270 | NewCallback(this, &DownloadManager::CheckDownloadUrlDone)); |
[email protected] | 4b58e7d | 2011-07-11 10:22:56 | [diff] [blame] | 271 | #else |
| 272 | CheckDownloadUrlDone(download_id, false); |
| 273 | #endif |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 274 | } |
| 275 | |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 276 | void DownloadManager::CheckForHistoryFilesRemoval() { |
| 277 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 278 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 279 | it != history_downloads_.end(); ++it) { |
| 280 | CheckForFileRemoval(it->second); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void DownloadManager::CheckForFileRemoval(DownloadItem* download_item) { |
| 285 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 286 | if (download_item->IsComplete() && |
| 287 | !download_item->file_externally_removed()) { |
| 288 | BrowserThread::PostTask( |
| 289 | BrowserThread::FILE, FROM_HERE, |
| 290 | NewRunnableMethod(this, |
| 291 | &DownloadManager::CheckForFileRemovalOnFileThread, |
| 292 | download_item->db_handle(), |
| 293 | download_item->GetTargetFilePath())); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void DownloadManager::CheckForFileRemovalOnFileThread( |
| 298 | int64 db_handle, const FilePath& path) { |
| 299 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 300 | if (!file_util::PathExists(path)) { |
| 301 | BrowserThread::PostTask( |
| 302 | BrowserThread::UI, FROM_HERE, |
| 303 | NewRunnableMethod(this, |
| 304 | &DownloadManager::OnFileRemovalDetected, |
| 305 | db_handle)); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void DownloadManager::OnFileRemovalDetected(int64 db_handle) { |
| 310 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 311 | DownloadMap::iterator it = history_downloads_.find(db_handle); |
| 312 | if (it != history_downloads_.end()) { |
| 313 | DownloadItem* download_item = it->second; |
| 314 | download_item->OnDownloadedFileRemoved(); |
| 315 | } |
| 316 | } |
| 317 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 318 | void DownloadManager::CheckDownloadUrlDone(int32 download_id, |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 319 | bool is_dangerous_url) { |
| 320 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 321 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 322 | DownloadItem* download = GetActiveDownloadItem(download_id); |
| 323 | if (!download) |
| 324 | return; |
| 325 | |
| 326 | if (is_dangerous_url) |
| 327 | download->MarkUrlDangerous(); |
| 328 | |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 329 | download_history_->CheckVisitedReferrerBefore(download_id, |
| 330 | download->referrer_url(), |
| 331 | NewCallback(this, &DownloadManager::CheckVisitedReferrerBeforeDone)); |
| 332 | } |
| 333 | |
| 334 | void DownloadManager::CheckVisitedReferrerBeforeDone( |
| 335 | int32 download_id, |
| 336 | bool visited_referrer_before) { |
| 337 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 338 | |
| 339 | DownloadItem* download = GetActiveDownloadItem(download_id); |
| 340 | if (!download) |
| 341 | return; |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 342 | |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 343 | // Check whether this download is for an extension install or not. |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 344 | // Allow extensions to be explicitly saved. |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 345 | DownloadStateInfo state = download->state_info(); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 346 | if (!state.prompt_user_for_save_location) { |
| 347 | if (UserScript::IsURLUserScript(download->GetURL(), |
| 348 | download->mime_type()) || |
| 349 | (download->mime_type() == Extension::kMimeType)) { |
| 350 | state.is_extension_install = true; |
[email protected] | bac4f4b | 2011-03-05 02:01:40 | [diff] [blame] | 351 | } |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 352 | } |
| 353 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 354 | if (state.force_file_name.empty()) { |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 355 | FilePath generated_name; |
[email protected] | 0622875ab | 2011-07-27 12:10:34 | [diff] [blame] | 356 | download_util::GenerateFileNameFromRequest(*download, |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 357 | &generated_name); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 358 | |
| 359 | // Freeze the user's preference for showing a Save As dialog. We're going |
| 360 | // to bounce around a bunch of threads and we don't want to worry about race |
| 361 | // conditions where the user changes this pref out from under us. |
[email protected] | 0e9b607 | 2011-02-17 12:42:05 | [diff] [blame] | 362 | if (download_prefs_->PromptForDownload()) { |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 363 | // But ignore the user's preference for the following scenarios: |
| 364 | // 1) Extension installation. Note that we only care here about the case |
| 365 | // where an extension is installed, not when one is downloaded with |
| 366 | // "save as...". |
| 367 | // 2) Filetypes marked "always open." If the user just wants this file |
| 368 | // opened, don't bother asking where to keep it. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 369 | if (!state.is_extension_install && |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 370 | !ShouldOpenFileBasedOnExtension(generated_name)) |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 371 | state.prompt_user_for_save_location = true; |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 372 | } |
[email protected] | 14e0a10 | 2011-02-22 14:04:01 | [diff] [blame] | 373 | if (download_prefs_->IsDownloadPathManaged()) { |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 374 | state.prompt_user_for_save_location = false; |
[email protected] | 14e0a10 | 2011-02-22 14:04:01 | [diff] [blame] | 375 | } |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 376 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 377 | // Determine the proper path for a download, by either one of the following: |
| 378 | // 1) using the default download directory. |
| 379 | // 2) prompting the user. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 380 | if (state.prompt_user_for_save_location && !last_download_path_.empty()) { |
| 381 | state.suggested_path = last_download_path_; |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 382 | } else { |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 383 | state.suggested_path = download_prefs_->download_path(); |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 384 | } |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 385 | state.suggested_path = state.suggested_path.Append(generated_name); |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 386 | } else { |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 387 | state.suggested_path = state.force_file_name; |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 388 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 390 | if (!state.prompt_user_for_save_location && state.force_file_name.empty()) { |
| 391 | state.is_dangerous_file = |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame^] | 392 | IsDangerousFile(*download, state, visited_referrer_before); |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 393 | } |
[email protected] | e9ebf3fc | 2008-10-17 22:06:58 | [diff] [blame] | 394 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | // We need to move over to the download thread because we don't want to stat |
| 396 | // the suggested path on the UI thread. |
[email protected] | 5a3b97e | 2010-10-05 09:49:11 | [diff] [blame] | 397 | // We can only access preferences on the UI thread, so check the download path |
| 398 | // now and pass the value to the FILE thread. |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 399 | BrowserThread::PostTask( |
| 400 | BrowserThread::FILE, FROM_HERE, |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 401 | NewRunnableMethod( |
[email protected] | 5a3b97e | 2010-10-05 09:49:11 | [diff] [blame] | 402 | this, |
| 403 | &DownloadManager::CheckIfSuggestedPathExists, |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 404 | download->id(), |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 405 | state, |
[email protected] | 5a3b97e | 2010-10-05 09:49:11 | [diff] [blame] | 406 | download_prefs()->download_path())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | } |
| 408 | |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 409 | void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, |
| 410 | DownloadStateInfo state, |
| 411 | const FilePath& default_path) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 412 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 414 | // Make sure the default download directory exists. |
| 415 | // TODO(phajdan.jr): only create the directory when we're sure the user |
| 416 | // is going to save there and not to another directory of his choice. |
| 417 | file_util::CreateDirectory(default_path); |
| 418 | |
| 419 | // Check writability of the suggested path. If we can't write to it, default |
| 420 | // to the user's "My Documents" directory. We'll prompt them in this case. |
| 421 | FilePath dir = state.suggested_path.DirName(); |
| 422 | FilePath filename = state.suggested_path.BaseName(); |
| 423 | if (!file_util::PathIsWritable(dir)) { |
| 424 | VLOG(1) << "Unable to write to directory \"" << dir.value() << "\""; |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 425 | state.prompt_user_for_save_location = true; |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 426 | PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path); |
| 427 | state.suggested_path = state.suggested_path.Append(filename); |
| 428 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 429 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 430 | // If the download is deemed dangerous, we'll use a temporary name for it. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 431 | if (state.IsDangerous()) { |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 432 | state.target_name = FilePath(state.suggested_path).BaseName(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 433 | // Create a temporary file to hold the file until the user approves its |
| 434 | // download. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 435 | FilePath::StringType file_name; |
| 436 | FilePath path; |
[email protected] | 463e1b43 | 2011-01-21 22:05:51 | [diff] [blame] | 437 | #if defined(OS_WIN) |
| 438 | string16 unconfirmed_prefix = |
| 439 | l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
| 440 | #else |
| 441 | std::string unconfirmed_prefix = |
| 442 | l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
| 443 | #endif |
| 444 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 445 | while (path.empty()) { |
[email protected] | 2594c2b | 2010-11-08 23:04:26 | [diff] [blame] | 446 | base::SStringPrintf( |
| 447 | &file_name, |
[email protected] | 463e1b43 | 2011-01-21 22:05:51 | [diff] [blame] | 448 | unconfirmed_prefix.append( |
| 449 | FILE_PATH_LITERAL(" %d.crdownload")).c_str(), |
[email protected] | 2594c2b | 2010-11-08 23:04:26 | [diff] [blame] | 450 | base::RandInt(0, 100000)); |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 451 | path = dir.Append(file_name); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 452 | if (file_util::PathExists(path)) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 453 | path = FilePath(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 454 | } |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 455 | state.suggested_path = path; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 456 | } else { |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 457 | // Do not add the path uniquifier if we are saving to a specific path as in |
| 458 | // the drag-out case. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 459 | if (state.force_file_name.empty()) { |
| 460 | state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( |
| 461 | state.suggested_path); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 462 | } |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 463 | // We know the final path, build it if necessary. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 464 | if (state.path_uniquifier > 0) { |
| 465 | download_util::AppendNumberToPath(&(state.suggested_path), |
| 466 | state.path_uniquifier); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 467 | // Setting path_uniquifier to 0 to make sure we don't try to unique it |
| 468 | // later on. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 469 | state.path_uniquifier = 0; |
| 470 | } else if (state.path_uniquifier == -1) { |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 471 | // We failed to find a unique path. We have to prompt the user. |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 472 | VLOG(1) << "Unable to find a unique path for suggested path \"" |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 473 | << state.suggested_path.value() << "\""; |
| 474 | state.prompt_user_for_save_location = true; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 475 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 476 | } |
| 477 | |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 478 | // Create an empty file at the suggested path so that we don't allocate the |
| 479 | // same "non-existant" path to multiple downloads. |
| 480 | // See: http://code.google.com/p/chromium/issues/detail?id=3662 |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 481 | if (!state.prompt_user_for_save_location && |
| 482 | state.force_file_name.empty()) { |
| 483 | if (state.IsDangerous()) |
| 484 | file_util::WriteFile(state.suggested_path, "", 0); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 485 | else |
| 486 | file_util::WriteFile(download_util::GetCrDownloadPath( |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 487 | state.suggested_path), "", 0); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 490 | BrowserThread::PostTask( |
| 491 | BrowserThread::UI, FROM_HERE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 492 | NewRunnableMethod(this, |
| 493 | &DownloadManager::OnPathExistenceAvailable, |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 494 | download_id, |
| 495 | state)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 496 | } |
| 497 | |
[email protected] | 2200146 | 2011-06-22 17:42:51 | [diff] [blame] | 498 | void DownloadManager::OnPathExistenceAvailable( |
| 499 | int32 download_id, const DownloadStateInfo& new_state) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 500 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 501 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 502 | DownloadItem* download = GetActiveDownloadItem(download_id); |
| 503 | if (!download) |
| 504 | return; |
| 505 | |
| 506 | VLOG(20) << __FUNCTION__ << "()" |
| 507 | << " download = " << download->DebugString(true); |
| 508 | |
| 509 | download->SetFileCheckResults(new_state); |
| 510 | |
| 511 | FilePath suggested_path = download->suggested_path(); |
| 512 | |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame^] | 513 | if (download->prompt_user_for_save_location()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 514 | // We must ask the user for the place to put the download. |
[email protected] | db6831a | 2011-06-09 21:08:28 | [diff] [blame] | 515 | DownloadRequestHandle request_handle = download->request_handle(); |
| 516 | TabContents* contents = request_handle.GetTabContents(); |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 517 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 518 | // |id_ptr| will be deleted in either FileSelected() or |
| 519 | // FileSelectionCancelled(). |
| 520 | int32* id_ptr = new int32; |
| 521 | *id_ptr = download_id; |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 522 | |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame^] | 523 | delegate_->ChooseDownloadPath( |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 524 | this, contents, suggested_path, reinterpret_cast<void*>(id_ptr)); |
| 525 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 526 | FOR_EACH_OBSERVER(Observer, observers_, |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 527 | SelectFileDialogDisplayed(download_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 528 | } else { |
| 529 | // No prompting for download, just continue with the suggested name. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 530 | ContinueDownloadWithPath(download, suggested_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 534 | void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
| 535 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 536 | |
| 537 | DownloadItem* download = new DownloadItem(this, *info, |
| 538 | profile_->IsOffTheRecord()); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 539 | int32 download_id = info->download_id; |
| 540 | DCHECK(!ContainsKey(in_progress_, download_id)); |
| 541 | DCHECK(!ContainsKey(active_downloads_, download_id)); |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 542 | downloads_.insert(download); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 543 | active_downloads_[download_id] = download; |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 544 | } |
| 545 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 546 | void DownloadManager::ContinueDownloadWithPath(DownloadItem* download, |
| 547 | const FilePath& chosen_file) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 548 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 549 | DCHECK(download); |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 550 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 551 | int32 download_id = download->id(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 553 | // NOTE(ahendrickson) Eventually |active_downloads_| will replace |
| 554 | // |in_progress_|, but we don't want to change the semantics yet. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 555 | DCHECK(!ContainsKey(in_progress_, download_id)); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 556 | DCHECK(ContainsKey(downloads_, download)); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 557 | DCHECK(ContainsKey(active_downloads_, download_id)); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 558 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 559 | // Make sure the initial file name is set only once. |
| 560 | DCHECK(download->full_path().empty()); |
| 561 | download->OnPathDetermined(chosen_file); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 562 | |
| 563 | VLOG(20) << __FUNCTION__ << "()" |
| 564 | << " download = " << download->DebugString(true); |
| 565 | |
| 566 | in_progress_[download_id] = download; |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 567 | UpdateAppIcon(); // Reflect entry into in_progress_. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 568 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 569 | // Rename to intermediate name. |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 570 | FilePath download_path; |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 571 | if (download->IsDangerous()) { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 572 | // The download is not safe. We can now rename the file to its |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 573 | // tentative name using RenameInProgressDownloadFile. |
| 574 | // NOTE: The |Rename| below will be a no-op for dangerous files, as we're |
| 575 | // renaming it to the same name. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 576 | download_path = download->full_path(); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 577 | } else { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 578 | // The download is a safe download. We need to |
| 579 | // rename it to its intermediate '.crdownload' path. The final |
| 580 | // name after user confirmation will be set from |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 581 | // DownloadItem::OnDownloadCompleting. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 582 | download_path = |
| 583 | download_util::GetCrDownloadPath(download->full_path()); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 584 | } |
| 585 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 586 | BrowserThread::PostTask( |
| 587 | BrowserThread::FILE, FROM_HERE, |
| 588 | NewRunnableMethod( |
| 589 | file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, |
| 590 | download->id(), download_path)); |
| 591 | |
| 592 | download->Rename(download_path); |
| 593 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 594 | download_history_->AddEntry(download, |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 595 | NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 596 | } |
| 597 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 598 | void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 599 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 600 | DownloadMap::iterator it = active_downloads_.find(download_id); |
| 601 | if (it != active_downloads_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 602 | DownloadItem* download = it->second; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 603 | if (download->IsInProgress()) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 604 | download->Update(size); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 605 | UpdateAppIcon(); // Reflect size updates. |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 606 | download_history_->UpdateEntry(download); |
| 607 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 611 | void DownloadManager::OnResponseCompleted(int32 download_id, |
| 612 | int64 size, |
| 613 | int os_error, |
| 614 | const std::string& hash) { |
| 615 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 7c8e55ba | 2011-07-01 18:59:23 | [diff] [blame] | 616 | // ERR_CONNECTION_CLOSED is allowed since a number of servers in the wild |
| 617 | // advertise a larger Content-Length than the amount of bytes in the message |
| 618 | // body, and then close the connection. Other browsers - IE8, Firefox 4.0.1, |
| 619 | // and Safari 5.0.4 - treat the download as complete in this case, so we |
| 620 | // follow their lead. |
[email protected] | 1822a42 | 2011-07-15 15:49:19 | [diff] [blame] | 621 | if (os_error == 0 || os_error == net::ERR_CONNECTION_CLOSED) { |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 622 | OnAllDataSaved(download_id, size, hash); |
| 623 | } else { |
| 624 | OnDownloadError(download_id, size, os_error); |
| 625 | } |
| 626 | } |
| 627 | |
[email protected] | 2671173 | 2011-03-09 00:21:22 | [diff] [blame] | 628 | void DownloadManager::OnAllDataSaved(int32 download_id, |
| 629 | int64 size, |
| 630 | const std::string& hash) { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 631 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 632 | << " size = " << size; |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 633 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 634 | |
[email protected] | c4f02c4 | 2011-01-24 21:55:06 | [diff] [blame] | 635 | // If it's not in active_downloads_, that means it was cancelled; just |
| 636 | // ignore the notification. |
| 637 | if (active_downloads_.count(download_id) == 0) |
| 638 | return; |
| 639 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 640 | DownloadItem* download = active_downloads_[download_id]; |
[email protected] | a850ba4 | 2010-09-10 22:00:30 | [diff] [blame] | 641 | download->OnAllDataSaved(size); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 642 | |
[email protected] | 2671173 | 2011-03-09 00:21:22 | [diff] [blame] | 643 | // When hash is not available, it means either it is not calculated |
| 644 | // or there is error while it is calculated. We will skip the download hash |
| 645 | // check in that case. |
| 646 | if (!hash.empty()) { |
[email protected] | 4b58e7d | 2011-07-11 10:22:56 | [diff] [blame] | 647 | #if defined(ENABLE_SAFE_BROWSING) |
[email protected] | 2671173 | 2011-03-09 00:21:22 | [diff] [blame] | 648 | scoped_refptr<DownloadSBClient> sb_client = |
| 649 | new DownloadSBClient(download_id, |
[email protected] | 8799e54 | 2011-04-20 03:47:34 | [diff] [blame] | 650 | download->url_chain(), |
[email protected] | 8c40da6 | 2011-07-13 22:58:46 | [diff] [blame] | 651 | download->referrer_url(), |
| 652 | profile_->GetPrefs()->GetBoolean( |
| 653 | prefs::kSafeBrowsingEnabled)); |
[email protected] | 2671173 | 2011-03-09 00:21:22 | [diff] [blame] | 654 | sb_client->CheckDownloadHash( |
| 655 | hash, NewCallback(this, &DownloadManager::CheckDownloadHashDone)); |
[email protected] | 4b58e7d | 2011-07-11 10:22:56 | [diff] [blame] | 656 | #else |
| 657 | CheckDownloadHashDone(download_id, false); |
| 658 | #endif |
[email protected] | 2671173 | 2011-03-09 00:21:22 | [diff] [blame] | 659 | } |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 660 | MaybeCompleteDownload(download); |
| 661 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 662 | |
[email protected] | 2671173 | 2011-03-09 00:21:22 | [diff] [blame] | 663 | // TODO(lzheng): This function currently works as a callback place holder. |
| 664 | // Once we decide the hash check is reliable, we could move the |
| 665 | // MaybeCompleteDownload in OnAllDataSaved to this function. |
| 666 | void DownloadManager::CheckDownloadHashDone(int32 download_id, |
| 667 | bool is_dangerous_hash) { |
| 668 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 669 | DVLOG(1) << "CheckDownloadHashDone, download_id: " << download_id |
| 670 | << " is dangerous_hash: " << is_dangerous_hash; |
| 671 | |
| 672 | // If it's not in active_downloads_, that means it was cancelled or |
| 673 | // the download already finished. |
| 674 | if (active_downloads_.count(download_id) == 0) |
| 675 | return; |
| 676 | |
| 677 | DVLOG(1) << "CheckDownloadHashDone, url: " |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 678 | << active_downloads_[download_id]->GetURL().spec(); |
[email protected] | 2671173 | 2011-03-09 00:21:22 | [diff] [blame] | 679 | } |
| 680 | |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 681 | void DownloadManager::AssertQueueStateConsistent(DownloadItem* download) { |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 682 | // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 683 | if (download->state() == DownloadItem::REMOVING) { |
| 684 | CHECK(!ContainsKey(downloads_, download)); |
| 685 | CHECK(!ContainsKey(active_downloads_, download->id())); |
| 686 | CHECK(!ContainsKey(in_progress_, download->id())); |
| 687 | CHECK(!ContainsKey(history_downloads_, download->db_handle())); |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | // Should be in downloads_ if we're not REMOVING. |
| 692 | CHECK(ContainsKey(downloads_, download)); |
| 693 | |
| 694 | // Check history_downloads_ consistency. |
| 695 | if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
| 696 | CHECK(ContainsKey(history_downloads_, download->db_handle())); |
| 697 | } else { |
| 698 | // TODO(rdsmith): Somewhat painful; make sure to disable in |
| 699 | // release builds after resolution of http://crbug.com/85408. |
| 700 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 701 | it != history_downloads_.end(); ++it) { |
| 702 | CHECK(it->second != download); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | CHECK(ContainsKey(active_downloads_, download->id()) == |
| 707 | (download->state() == DownloadItem::IN_PROGRESS)); |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 708 | CHECK(ContainsKey(in_progress_, download->id()) == |
| 709 | (download->state() == DownloadItem::IN_PROGRESS)); |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 710 | } |
| 711 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 712 | bool DownloadManager::IsDownloadReadyForCompletion(DownloadItem* download) { |
| 713 | // If we don't have all the data, the download is not ready for |
| 714 | // completion. |
| 715 | if (!download->all_data_saved()) |
| 716 | return false; |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 717 | |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 718 | // If the download is dangerous, but not yet validated, it's not ready for |
| 719 | // completion. |
| 720 | if (download->safety_state() == DownloadItem::DANGEROUS) |
| 721 | return false; |
| 722 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 723 | // If the download isn't active (e.g. has been cancelled) it's not |
| 724 | // ready for completion. |
| 725 | if (active_downloads_.count(download->id()) == 0) |
| 726 | return false; |
| 727 | |
| 728 | // If the download hasn't been inserted into the history system |
| 729 | // (which occurs strictly after file name determination, intermediate |
| 730 | // file rename, and UI display) then it's not ready for completion. |
[email protected] | 7054b59 | 2011-06-22 14:46:42 | [diff] [blame] | 731 | if (download->db_handle() == DownloadHistory::kUninitializedHandle) |
| 732 | return false; |
| 733 | |
| 734 | return true; |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | void DownloadManager::MaybeCompleteDownload(DownloadItem* download) { |
| 738 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 739 | VLOG(20) << __FUNCTION__ << "()" << " download = " |
| 740 | << download->DebugString(false); |
| 741 | |
| 742 | if (!IsDownloadReadyForCompletion(download)) |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 743 | return; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 744 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 745 | // TODO(rdsmith): DCHECK that we only pass through this point |
| 746 | // once per download. The natural way to do this is by a state |
| 747 | // transition on the DownloadItem. |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 748 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 749 | // Confirm we're in the proper set of states to be here; |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 750 | // in in_progress_, have all data, have a history handle, (validated or safe). |
| 751 | DCHECK_NE(DownloadItem::DANGEROUS, download->safety_state()); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 752 | DCHECK_EQ(1u, in_progress_.count(download->id())); |
| 753 | DCHECK(download->all_data_saved()); |
| 754 | DCHECK(download->db_handle() != DownloadHistory::kUninitializedHandle); |
| 755 | DCHECK_EQ(1u, history_downloads_.count(download->db_handle())); |
| 756 | |
| 757 | VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
| 758 | << download->DebugString(false); |
| 759 | |
| 760 | // Remove the id from in_progress |
| 761 | in_progress_.erase(download->id()); |
| 762 | UpdateAppIcon(); // Reflect removal from in_progress_. |
| 763 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 764 | download_history_->UpdateEntry(download); |
| 765 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 766 | // Finish the download. |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 767 | download->OnDownloadCompleting(file_manager_); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 768 | } |
| 769 | |
[email protected] | cc3c7c09 | 2011-05-09 18:40:21 | [diff] [blame] | 770 | void DownloadManager::DownloadCompleted(int32 download_id) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 771 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | cc3c7c09 | 2011-05-09 18:40:21 | [diff] [blame] | 772 | DownloadItem* download = GetDownloadItem(download_id); |
| 773 | DCHECK(download); |
| 774 | download_history_->UpdateEntry(download); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 775 | active_downloads_.erase(download_id); |
| 776 | } |
| 777 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 778 | void DownloadManager::OnDownloadRenamedToFinalName(int download_id, |
| 779 | const FilePath& full_path, |
| 780 | int uniquifier) { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 781 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 782 | << " full_path = \"" << full_path.value() << "\"" |
| 783 | << " uniquifier = " << uniquifier; |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 784 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 785 | |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 786 | DownloadItem* item = GetDownloadItem(download_id); |
| 787 | if (!item) |
| 788 | return; |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 789 | |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 790 | if (item->safety_state() == DownloadItem::SAFE) { |
| 791 | DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
| 792 | } |
| 793 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 794 | BrowserThread::PostTask( |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 795 | BrowserThread::FILE, FROM_HERE, |
| 796 | NewRunnableMethod( |
| 797 | file_manager_, &DownloadFileManager::CompleteDownload, download_id)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 798 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 799 | if (uniquifier) |
| 800 | item->set_path_uniquifier(uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 801 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 802 | item->OnDownloadRenamedToFinalName(full_path); |
| 803 | download_history_->UpdateDownloadPath(item, full_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 804 | } |
| 805 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 806 | void DownloadManager::DownloadCancelled(int32 download_id) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 807 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 808 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 809 | if (it == in_progress_.end()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 810 | return; |
| 811 | DownloadItem* download = it->second; |
| 812 | |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 813 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 814 | << " download = " << download->DebugString(true); |
| 815 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 816 | // Clean up will happen when the history system create callback runs if we |
| 817 | // don't have a valid db_handle yet. |
| 818 | if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
| 819 | in_progress_.erase(it); |
| 820 | active_downloads_.erase(download_id); |
| 821 | UpdateAppIcon(); // Reflect removal from in_progress_. |
| 822 | download_history_->UpdateEntry(download); |
| 823 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 824 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 825 | DownloadCancelledInternal(download_id, download->request_handle()); |
| 826 | } |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 827 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 828 | void DownloadManager::DownloadCancelledInternal( |
| 829 | int download_id, const DownloadRequestHandle& request_handle) { |
| 830 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 831 | request_handle.CancelRequest(); |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 832 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 833 | BrowserThread::PostTask( |
| 834 | BrowserThread::FILE, FROM_HERE, |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 835 | NewRunnableMethod( |
| 836 | file_manager_, &DownloadFileManager::CancelDownload, download_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 837 | } |
| 838 | |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 839 | void DownloadManager::OnDownloadError(int32 download_id, |
| 840 | int64 size, |
| 841 | int os_error) { |
| 842 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 843 | DownloadMap::iterator it = active_downloads_.find(download_id); |
| 844 | // A cancel at the right time could remove the download from the |
| 845 | // |active_downloads_| map before we get here. |
| 846 | if (it == active_downloads_.end()) |
| 847 | return; |
| 848 | |
| 849 | DownloadItem* download = it->second; |
| 850 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 851 | VLOG(20) << __FUNCTION__ << "()" << " Error " << os_error |
| 852 | << " at offset " << download->received_bytes() |
| 853 | << " for download = " << download->DebugString(true); |
| 854 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 855 | download->Interrupted(size, os_error); |
| 856 | |
| 857 | // TODO(ahendrickson) - Remove this when we add resuming of interrupted |
| 858 | // downloads, as we will keep the download item around in that case. |
| 859 | // |
| 860 | // Clean up will happen when the history system create callback runs if we |
| 861 | // don't have a valid db_handle yet. |
| 862 | if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
| 863 | in_progress_.erase(download_id); |
| 864 | active_downloads_.erase(download_id); |
| 865 | UpdateAppIcon(); // Reflect removal from in_progress_. |
| 866 | download_history_->UpdateEntry(download); |
| 867 | } |
| 868 | |
| 869 | BrowserThread::PostTask( |
| 870 | BrowserThread::FILE, FROM_HERE, |
| 871 | NewRunnableMethod( |
| 872 | file_manager_, &DownloadFileManager::CancelDownload, download_id)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 873 | } |
| 874 | |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 875 | void DownloadManager::UpdateAppIcon() { |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 876 | if (status_updater_) |
| 877 | status_updater_->Update(); |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 878 | } |
| 879 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 880 | void DownloadManager::RemoveDownload(int64 download_handle) { |
| 881 | DownloadMap::iterator it = history_downloads_.find(download_handle); |
| 882 | if (it == history_downloads_.end()) |
| 883 | return; |
| 884 | |
| 885 | // Make history update. |
| 886 | DownloadItem* download = it->second; |
| 887 | download_history_->RemoveEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 888 | |
| 889 | // Remove from our tables and delete. |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 890 | history_downloads_.erase(it); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 891 | int downloads_count = downloads_.erase(download); |
| 892 | DCHECK_EQ(1, downloads_count); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 893 | |
| 894 | // Tell observers to refresh their views. |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 895 | NotifyModelChanged(); |
[email protected] | 6f71287 | 2008-11-07 00:35:36 | [diff] [blame] | 896 | |
| 897 | delete download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 898 | } |
| 899 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 900 | int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin, |
| 901 | const base::Time remove_end) { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 902 | download_history_->RemoveEntriesBetween(remove_begin, remove_end); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 903 | |
[email protected] | a312a44 | 2010-12-15 23:40:33 | [diff] [blame] | 904 | // All downloads visible to the user will be in the history, |
| 905 | // so scan that map. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 906 | DownloadMap::iterator it = history_downloads_.begin(); |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 907 | std::vector<DownloadItem*> pending_deletes; |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 908 | while (it != history_downloads_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 909 | DownloadItem* download = it->second; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 910 | if (download->start_time() >= remove_begin && |
| 911 | (remove_end.is_null() || download->start_time() < remove_end) && |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 912 | (download->IsComplete() || |
| 913 | download->IsCancelled() || |
| 914 | download->IsInterrupted())) { |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 915 | AssertQueueStateConsistent(download); |
| 916 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 917 | // Remove from the map and move to the next in the list. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 918 | history_downloads_.erase(it++); |
[email protected] | a6604d9 | 2008-10-30 00:58:58 | [diff] [blame] | 919 | |
| 920 | // Also remove it from any completed dangerous downloads. |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 921 | pending_deletes.push_back(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 922 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 923 | continue; |
| 924 | } |
| 925 | |
| 926 | ++it; |
| 927 | } |
| 928 | |
[email protected] | a312a44 | 2010-12-15 23:40:33 | [diff] [blame] | 929 | // If we aren't deleting anything, we're done. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 930 | if (pending_deletes.empty()) |
| 931 | return 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 932 | |
[email protected] | a312a44 | 2010-12-15 23:40:33 | [diff] [blame] | 933 | // Remove the chosen downloads from the main owning container. |
| 934 | for (std::vector<DownloadItem*>::iterator it = pending_deletes.begin(); |
| 935 | it != pending_deletes.end(); it++) { |
| 936 | downloads_.erase(*it); |
| 937 | } |
| 938 | |
| 939 | // Tell observers to refresh their views. |
| 940 | NotifyModelChanged(); |
| 941 | |
| 942 | // Delete the download items themselves. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 943 | int num_deleted = static_cast<int>(pending_deletes.size()); |
| 944 | |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 945 | STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end()); |
| 946 | pending_deletes.clear(); |
| 947 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 948 | return num_deleted; |
| 949 | } |
| 950 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 951 | int DownloadManager::RemoveDownloads(const base::Time remove_begin) { |
| 952 | return RemoveDownloadsBetween(remove_begin, base::Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 953 | } |
| 954 | |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 955 | int DownloadManager::RemoveAllDownloads() { |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 956 | if (this != profile_->GetOriginalProfile()->GetDownloadManager()) { |
| 957 | // This is an incognito downloader. Clear All should clear main download |
| 958 | // manager as well. |
| 959 | profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads(); |
| 960 | } |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 961 | // The null times make the date range unbounded. |
| 962 | return RemoveDownloadsBetween(base::Time(), base::Time()); |
| 963 | } |
| 964 | |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 965 | void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download) { |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 966 | #if !defined(NDEBUG) |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 967 | save_page_as_downloads_.insert(download); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 968 | #endif |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 969 | downloads_.insert(download); |
| 970 | // Add to history and notify observers. |
| 971 | AddDownloadItemToHistory(download, DownloadHistory::kUninitializedHandle); |
| 972 | NotifyModelChanged(); |
[email protected] | ec4826a | 2010-09-21 09:15:59 | [diff] [blame] | 973 | } |
| 974 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 975 | // Initiate a download of a specific URL. We send the request to the |
| 976 | // ResourceDispatcherHost, and let it send us responses like a regular |
| 977 | // download. |
| 978 | void DownloadManager::DownloadUrl(const GURL& url, |
| 979 | const GURL& referrer, |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 980 | const std::string& referrer_charset, |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 981 | TabContents* tab_contents) { |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 982 | DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(), |
| 983 | tab_contents); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | void DownloadManager::DownloadUrlToFile(const GURL& url, |
| 987 | const GURL& referrer, |
| 988 | const std::string& referrer_charset, |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 989 | const DownloadSaveInfo& save_info, |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 990 | TabContents* tab_contents) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 991 | DCHECK(tab_contents); |
[email protected] | ed24fad | 2011-05-10 22:44:01 | [diff] [blame] | 992 | // We send a pointer to content::ResourceContext, instead of the usual |
| 993 | // reference, so that a copy of the object isn't made. |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 994 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 995 | NewRunnableFunction(&download_util::DownloadUrl, |
| 996 | url, |
| 997 | referrer, |
| 998 | referrer_charset, |
| 999 | save_info, |
| 1000 | g_browser_process->resource_dispatcher_host(), |
| 1001 | tab_contents->GetRenderProcessHost()->id(), |
| 1002 | tab_contents->render_view_host()->routing_id(), |
[email protected] | cafe4ad | 2011-07-28 18:34:56 | [diff] [blame] | 1003 | &tab_contents->browser_context()-> |
| 1004 | GetResourceContext())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1005 | } |
| 1006 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1007 | void DownloadManager::AddObserver(Observer* observer) { |
| 1008 | observers_.AddObserver(observer); |
| 1009 | observer->ModelChanged(); |
| 1010 | } |
| 1011 | |
| 1012 | void DownloadManager::RemoveObserver(Observer* observer) { |
| 1013 | observers_.RemoveObserver(observer); |
| 1014 | } |
| 1015 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1016 | bool DownloadManager::ShouldOpenFileBasedOnExtension( |
| 1017 | const FilePath& path) const { |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1018 | FilePath::StringType extension = path.Extension(); |
| 1019 | if (extension.empty()) |
| 1020 | return false; |
[email protected] | 92e11c8 | 2010-01-13 06:39:56 | [diff] [blame] | 1021 | if (Extension::IsExtension(path)) |
| 1022 | return false; |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1023 | DCHECK(extension[0] == FilePath::kExtensionSeparator); |
| 1024 | extension.erase(0, 1); |
[email protected] | e5dc422 | 2010-08-30 22:16:32 | [diff] [blame] | 1025 | return download_prefs_->IsAutoOpenEnabledForExtension(extension); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1026 | } |
| 1027 | |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 1028 | bool DownloadManager::IsDownloadProgressKnown() { |
| 1029 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 1030 | i != in_progress_.end(); ++i) { |
| 1031 | if (i->second->total_bytes() <= 0) |
| 1032 | return false; |
| 1033 | } |
| 1034 | |
| 1035 | return true; |
| 1036 | } |
| 1037 | |
| 1038 | int64 DownloadManager::GetInProgressDownloadCount() { |
| 1039 | return in_progress_.size(); |
| 1040 | } |
| 1041 | |
| 1042 | int64 DownloadManager::GetReceivedDownloadBytes() { |
| 1043 | DCHECK(IsDownloadProgressKnown()); |
| 1044 | int64 received_bytes = 0; |
| 1045 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 1046 | i != in_progress_.end(); ++i) { |
| 1047 | received_bytes += i->second->received_bytes(); |
| 1048 | } |
| 1049 | return received_bytes; |
| 1050 | } |
| 1051 | |
| 1052 | int64 DownloadManager::GetTotalDownloadBytes() { |
| 1053 | DCHECK(IsDownloadProgressKnown()); |
| 1054 | int64 total_bytes = 0; |
| 1055 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 1056 | i != in_progress_.end(); ++i) { |
| 1057 | total_bytes += i->second->total_bytes(); |
| 1058 | } |
| 1059 | return total_bytes; |
| 1060 | } |
| 1061 | |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 1062 | void DownloadManager::FileSelected(const FilePath& path, void* params) { |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1063 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1064 | |
| 1065 | int32* id_ptr = reinterpret_cast<int32*>(params); |
| 1066 | DCHECK(id_ptr != NULL); |
| 1067 | int32 download_id = *id_ptr; |
| 1068 | delete id_ptr; |
| 1069 | |
| 1070 | DownloadItem* download = GetActiveDownloadItem(download_id); |
| 1071 | if (!download) |
| 1072 | return; |
| 1073 | VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\"" |
| 1074 | << " download = " << download->DebugString(true); |
| 1075 | |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame^] | 1076 | if (download->prompt_user_for_save_location()) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1077 | last_download_path_ = path.DirName(); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 1078 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1079 | // Make sure the initial file name is set only once. |
| 1080 | ContinueDownloadWithPath(download, path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | void DownloadManager::FileSelectionCanceled(void* params) { |
| 1084 | // The user didn't pick a place to save the file, so need to cancel the |
| 1085 | // download that's already in progress to the temporary location. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1086 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1087 | int32* id_ptr = reinterpret_cast<int32*>(params); |
| 1088 | DCHECK(id_ptr != NULL); |
| 1089 | int32 download_id = *id_ptr; |
| 1090 | delete id_ptr; |
| 1091 | |
| 1092 | DownloadItem* download = GetActiveDownloadItem(download_id); |
| 1093 | if (!download) |
| 1094 | return; |
| 1095 | |
| 1096 | VLOG(20) << __FUNCTION__ << "()" |
| 1097 | << " download = " << download->DebugString(true); |
| 1098 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 1099 | DownloadCancelledInternal(download_id, download->request_handle()); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | // TODO(phajdan.jr): This is apparently not being exercised in tests. |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame^] | 1103 | bool DownloadManager::IsDangerousFile(const DownloadItem& download, |
| 1104 | const DownloadStateInfo& state, |
| 1105 | bool visited_referrer_before) { |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1106 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1107 | |
| 1108 | bool auto_open = ShouldOpenFileBasedOnExtension(state.suggested_path); |
| 1109 | download_util::DownloadDangerLevel danger_level = |
| 1110 | download_util::GetFileDangerLevel(state.suggested_path.BaseName()); |
| 1111 | |
| 1112 | if (danger_level == download_util::Dangerous) |
| 1113 | return !(auto_open && state.has_user_gesture); |
| 1114 | |
| 1115 | if (danger_level == download_util::AllowOnUserGesture && |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 1116 | (!state.has_user_gesture || !visited_referrer_before)) |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1117 | return true; |
| 1118 | |
| 1119 | if (state.is_extension_install) { |
| 1120 | // Extensions that are not from the gallery are considered dangerous. |
| 1121 | ExtensionService* service = profile()->GetExtensionService(); |
| 1122 | if (!service || !service->IsDownloadFromGallery(download.GetURL(), |
| 1123 | download.referrer_url())) |
| 1124 | return true; |
| 1125 | } |
| 1126 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | // Operations posted to us from the history service ---------------------------- |
| 1130 | |
| 1131 | // The history service has retrieved all download entries. 'entries' contains |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1132 | // 'DownloadHistoryInfo's in sorted order (by ascending start_time). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1133 | void DownloadManager::OnQueryDownloadEntriesComplete( |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1134 | std::vector<DownloadHistoryInfo>* entries) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1135 | for (size_t i = 0; i < entries->size(); ++i) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 1136 | DownloadItem* download = new DownloadItem(this, entries->at(i)); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1137 | DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
| 1138 | downloads_.insert(download); |
| 1139 | history_downloads_[download->db_handle()] = download; |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 1140 | VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 1141 | << " download = " << download->DebugString(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1142 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1143 | NotifyModelChanged(); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 1144 | CheckForHistoryFilesRemoval(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1145 | } |
| 1146 | |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 1147 | void DownloadManager::AddDownloadItemToHistory(DownloadItem* download, |
| 1148 | int64 db_handle) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 1149 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 1150 | |
[email protected] | 5bcd73eb | 2011-03-23 21:14:02 | [diff] [blame] | 1151 | // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
| 1152 | // call this function with an invalid |db_handle|. For instance, this can |
| 1153 | // happen when the history database is offline. We cannot have multiple |
| 1154 | // DownloadItems with the same invalid db_handle, so we need to assign a |
| 1155 | // unique |db_handle| here. |
| 1156 | if (db_handle == DownloadHistory::kUninitializedHandle) |
| 1157 | db_handle = download_history_->GetNextFakeDbHandle(); |
| 1158 | |
[email protected] | 1e9fe7ff | 2011-06-24 17:37:33 | [diff] [blame] | 1159 | // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508 |
| 1160 | // is fixed. |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 1161 | CHECK_NE(DownloadHistory::kUninitializedHandle, db_handle); |
[email protected] | 1e9fe7ff | 2011-06-24 17:37:33 | [diff] [blame] | 1162 | |
[email protected] | 5bcd73eb | 2011-03-23 21:14:02 | [diff] [blame] | 1163 | DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); |
| 1164 | download->set_db_handle(db_handle); |
| 1165 | |
[email protected] | 5bcd73eb | 2011-03-23 21:14:02 | [diff] [blame] | 1166 | DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
| 1167 | history_downloads_[download->db_handle()] = download; |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | // Once the new DownloadItem's creation info has been committed to the history |
| 1171 | // service, we associate the DownloadItem with the db handle, update our |
| 1172 | // 'history_downloads_' map and inform observers. |
| 1173 | void DownloadManager::OnCreateDownloadEntryComplete(int32 download_id, |
| 1174 | int64 db_handle) { |
| 1175 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 19420cc | 2011-07-18 17:43:45 | [diff] [blame] | 1176 | DownloadItem* download = GetActiveDownloadItem(download_id); |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 1177 | if (!download) |
[email protected] | 19420cc | 2011-07-18 17:43:45 | [diff] [blame] | 1178 | return; |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 1179 | |
| 1180 | VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle |
| 1181 | << " download_id = " << download_id |
| 1182 | << " download = " << download->DebugString(true); |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 1183 | |
| 1184 | AddDownloadItemToHistory(download, db_handle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1185 | |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 1186 | // Show in the appropriate browser UI. |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 1187 | // This includes buttons to save or cancel, for a dangerous download. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1188 | ShowDownloadInBrowser(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1189 | |
| 1190 | // Inform interested objects about the new download. |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1191 | NotifyModelChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1192 | |
[email protected] | 5461067 | 2011-07-18 18:24:43 | [diff] [blame] | 1193 | // If the download is still in progress, try to complete it. |
| 1194 | // |
| 1195 | // Otherwise, download has been cancelled or interrupted before we've |
| 1196 | // received the DB handle. We post one final message to the history |
| 1197 | // service so that it can be properly in sync with the DownloadItem's |
| 1198 | // completion status, and also inform any observers so that they get |
| 1199 | // more than just the start notification. |
| 1200 | if (download->IsInProgress()) { |
| 1201 | MaybeCompleteDownload(download); |
| 1202 | } else { |
| 1203 | DCHECK(download->IsCancelled()) |
| 1204 | << " download = " << download->DebugString(true); |
| 1205 | in_progress_.erase(download_id); |
| 1206 | active_downloads_.erase(download_id); |
| 1207 | download_history_->UpdateEntry(download); |
| 1208 | download->UpdateObservers(); |
| 1209 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1210 | } |
| 1211 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1212 | void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) { |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 1213 | // 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] | 1214 | // get this start completion event. |
[email protected] | db6831a | 2011-06-09 21:08:28 | [diff] [blame] | 1215 | DownloadRequestHandle request_handle = download->request_handle(); |
[email protected] | 68649314 | 2011-07-15 21:47:22 | [diff] [blame] | 1216 | TabContents* content = request_handle.GetTabContents(); |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 1217 | |
| 1218 | // If the contents no longer exists, we ask the embedder to suggest another |
| 1219 | // tab. |
[email protected] | da1a27b | 2011-07-29 23:16:33 | [diff] [blame^] | 1220 | if (!content) |
| 1221 | content = delegate_->GetAlternativeTabContentsToNotifyForDownload(this); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1222 | |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 1223 | if (content) |
| 1224 | content->OnStartDownload(download); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1225 | } |
| 1226 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 1227 | // Clears the last download path, used to initialize "save as" dialogs. |
[email protected] | 905a08d | 2008-11-19 07:24:12 | [diff] [blame] | 1228 | void DownloadManager::ClearLastDownloadPath() { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1229 | last_download_path_ = FilePath(); |
[email protected] | eea4662 | 2009-07-15 20:49:38 | [diff] [blame] | 1230 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1231 | |
| 1232 | void DownloadManager::NotifyModelChanged() { |
| 1233 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
| 1234 | } |
| 1235 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1236 | DownloadItem* DownloadManager::GetDownloadItem(int download_id) { |
| 1237 | // The |history_downloads_| map is indexed by the download's db_handle, |
| 1238 | // not its id, so we have to iterate. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1239 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 1240 | it != history_downloads_.end(); ++it) { |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 1241 | DownloadItem* item = it->second; |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1242 | if (item->id() == download_id) |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 1243 | return item; |
| 1244 | } |
| 1245 | return NULL; |
| 1246 | } |
| 1247 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1248 | DownloadItem* DownloadManager::GetActiveDownloadItem(int download_id) { |
| 1249 | DCHECK(ContainsKey(active_downloads_, download_id)); |
| 1250 | DownloadItem* download = active_downloads_[download_id]; |
| 1251 | DCHECK(download != NULL); |
| 1252 | return download; |
| 1253 | } |
| 1254 | |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1255 | // Confirm that everything in all maps is also in |downloads_|, and that |
| 1256 | // everything in |downloads_| is also in some other map. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1257 | void DownloadManager::AssertContainersConsistent() const { |
| 1258 | #if !defined(NDEBUG) |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1259 | // Turn everything into sets. |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1260 | DownloadSet active_set, history_set; |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 1261 | const DownloadMap* input_maps[] = {&active_downloads_, &history_downloads_}; |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1262 | DownloadSet* local_sets[] = {&active_set, &history_set}; |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1263 | DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(local_sets)); |
| 1264 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) { |
| 1265 | for (DownloadMap::const_iterator it = input_maps[i]->begin(); |
| 1266 | it != input_maps[i]->end(); it++) { |
| 1267 | local_sets[i]->insert(&*it->second); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1268 | } |
| 1269 | } |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1270 | |
| 1271 | // Check if each set is fully present in downloads, and create a union. |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1272 | const DownloadSet* all_sets[] = {&active_set, &history_set, |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1273 | &save_page_as_downloads_}; |
| 1274 | DownloadSet downloads_union; |
| 1275 | for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) { |
| 1276 | DownloadSet remainder; |
| 1277 | std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
| 1278 | std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
| 1279 | downloads_.begin(), downloads_.end(), |
| 1280 | insert_it); |
| 1281 | DCHECK(remainder.empty()); |
| 1282 | std::insert_iterator<DownloadSet> |
| 1283 | insert_union(downloads_union, downloads_union.end()); |
| 1284 | std::set_union(downloads_union.begin(), downloads_union.end(), |
| 1285 | all_sets[i]->begin(), all_sets[i]->end(), |
| 1286 | insert_union); |
| 1287 | } |
| 1288 | |
| 1289 | // Is everything in downloads_ present in one of the other sets? |
| 1290 | DownloadSet remainder; |
| 1291 | std::insert_iterator<DownloadSet> |
| 1292 | insert_remainder(remainder, remainder.begin()); |
| 1293 | std::set_difference(downloads_.begin(), downloads_.end(), |
| 1294 | downloads_union.begin(), downloads_union.end(), |
| 1295 | insert_remainder); |
| 1296 | DCHECK(remainder.empty()); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1297 | #endif |
| 1298 | } |
| 1299 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1300 | // DownloadManager::OtherDownloadManagerObserver implementation ---------------- |
| 1301 | |
| 1302 | DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver( |
| 1303 | DownloadManager* observing_download_manager) |
| 1304 | : observing_download_manager_(observing_download_manager), |
| 1305 | observed_download_manager_(NULL) { |
| 1306 | if (observing_download_manager->profile_->GetOriginalProfile() == |
| 1307 | observing_download_manager->profile_) { |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | observed_download_manager_ = observing_download_manager_-> |
| 1312 | profile_->GetOriginalProfile()->GetDownloadManager(); |
| 1313 | observed_download_manager_->AddObserver(this); |
| 1314 | } |
| 1315 | |
| 1316 | DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() { |
| 1317 | if (observed_download_manager_) |
| 1318 | observed_download_manager_->RemoveObserver(this); |
| 1319 | } |
| 1320 | |
| 1321 | void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
| 1322 | observing_download_manager_->NotifyModelChanged(); |
| 1323 | } |
| 1324 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1325 | void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
| 1326 | observed_download_manager_ = NULL; |
| 1327 | } |