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