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