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