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