[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] | 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] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 17 | #include "chrome/browser/browser.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] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 20 | #include "chrome/browser/chrome_thread.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] | e9ef0a6 | 2009-08-11 22:50:13 | [diff] [blame] | 24 | #include "chrome/browser/download/download_util.h" |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 25 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 26 | #include "chrome/browser/extensions/extension_install_ui.h" |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 27 | #include "chrome/browser/extensions/extensions_service.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 28 | #include "chrome/browser/history/download_types.h" |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 29 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 30 | #include "chrome/browser/platform_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | #include "chrome/browser/profile.h" |
[email protected] | 8c8657d6 | 2009-01-16 18:31:26 | [diff] [blame] | 32 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 6524b5f9 | 2009-01-22 17:48:25 | [diff] [blame] | 33 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 34 | #include "chrome/browser/tab_contents/infobar_delegate.h" |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 35 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 36 | #include "chrome/browser/tab_contents/tab_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | #include "chrome/common/chrome_paths.h" |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 38 | #include "chrome/common/notification_service.h" |
| 39 | #include "chrome/common/notification_type.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | #include "chrome/common/pref_names.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 41 | #include "googleurl/src/gurl.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 42 | #include "grit/generated_resources.h" |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 43 | #include "grit/theme_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | #include "net/base/mime_util.h" |
| 45 | #include "net/base/net_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 47 | #if defined(OS_WIN) |
[email protected] | 4a0765a | 2009-05-08 23:12:25 | [diff] [blame] | 48 | #include "app/win_util.h" |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 49 | #include "base/registry.h" |
| 50 | #include "base/win_util.h" |
[email protected] | a0a9577b | 2009-05-27 23:52:32 | [diff] [blame] | 51 | #endif |
| 52 | |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 53 | namespace { |
| 54 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 55 | // Used to sort download items based on descending start time. |
| 56 | bool CompareStartTime(DownloadItem* first, DownloadItem* second) { |
| 57 | return first->start_time() > second->start_time(); |
| 58 | } |
| 59 | |
[email protected] | da74e5ae2 | 2010-07-14 16:12:37 | [diff] [blame] | 60 | void DeleteDownloadedFile(const FilePath& path) { |
| 61 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 62 | |
| 63 | // Make sure we only delete files. |
| 64 | if (!file_util::DirectoryExists(path)) |
| 65 | file_util::Delete(path, false); |
| 66 | } |
| 67 | |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 68 | } // namespace |
| 69 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | // static |
| 71 | void DownloadManager::RegisterUserPrefs(PrefService* prefs) { |
| 72 | prefs->RegisterBooleanPref(prefs::kPromptForDownload, false); |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 73 | prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen, ""); |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 74 | prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false); |
| 75 | |
| 76 | // The default download path is userprofile\download. |
[email protected] | 290c970 | 2010-03-09 04:01:36 | [diff] [blame] | 77 | const FilePath& default_download_path = |
| 78 | download_util::GetDefaultDownloadDirectory(); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 79 | prefs->RegisterFilePathPref(prefs::kDownloadDefaultDirectory, |
| 80 | default_download_path); |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 81 | #if defined(OS_CHROMEOS) |
| 82 | // Ensure that the download directory specified in the preferences exists. |
| 83 | ChromeThread::PostTask( |
| 84 | ChromeThread::FILE, FROM_HERE, |
| 85 | NewRunnableFunction(&file_util::CreateDirectory, default_download_path)); |
| 86 | #endif |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 87 | |
| 88 | // If the download path is dangerous we forcefully reset it. But if we do |
| 89 | // so we set a flag to make sure we only do it once, to avoid fighting |
| 90 | // the user if he really wants it on an unsafe place such as the desktop. |
| 91 | |
| 92 | if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { |
[email protected] | 38233e6 | 2010-04-26 04:43:36 | [diff] [blame] | 93 | FilePath current_download_dir = prefs->GetFilePath( |
| 94 | prefs::kDownloadDefaultDirectory); |
[email protected] | 290c970 | 2010-03-09 04:01:36 | [diff] [blame] | 95 | if (download_util::DownloadPathIsDangerous(current_download_dir)) { |
[email protected] | 38233e6 | 2010-04-26 04:43:36 | [diff] [blame] | 96 | prefs->SetFilePath(prefs::kDownloadDefaultDirectory, |
| 97 | default_download_path); |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 98 | } |
| 99 | prefs->SetBoolean(prefs::kDownloadDirUpgraded, true); |
| 100 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | DownloadManager::DownloadManager() |
| 104 | : shutdown_needed_(false), |
| 105 | profile_(NULL), |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 106 | file_manager_(NULL) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | DownloadManager::~DownloadManager() { |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 110 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); |
| 111 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | if (shutdown_needed_) |
| 113 | Shutdown(); |
| 114 | } |
| 115 | |
| 116 | void DownloadManager::Shutdown() { |
| 117 | DCHECK(shutdown_needed_) << "Shutdown called when not needed."; |
| 118 | |
| 119 | // Stop receiving download updates |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 120 | if (file_manager_) |
| 121 | file_manager_->RemoveDownloadManager(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | // 'in_progress_' may contain DownloadItems that have not finished the start |
| 124 | // complete (from the history service) and thus aren't in downloads_. |
| 125 | DownloadMap::iterator it = in_progress_.begin(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 126 | std::set<DownloadItem*> to_remove; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | for (; it != in_progress_.end(); ++it) { |
| 128 | DownloadItem* download = it->second; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 129 | if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 130 | // Forget about any download that the user did not approve. |
| 131 | // Note that we cannot call download->Remove() this would invalidate our |
| 132 | // iterator. |
| 133 | to_remove.insert(download); |
| 134 | continue; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 136 | DCHECK_EQ(DownloadItem::IN_PROGRESS, download->state()); |
| 137 | download->Cancel(false); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 138 | download_history_->UpdateEntry(download); |
| 139 | if (download->db_handle() == DownloadHistory::kUninitializedHandle) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | // An invalid handle means that 'download' does not yet exist in |
| 141 | // 'downloads_', so we have to delete it here. |
| 142 | delete download; |
| 143 | } |
| 144 | } |
| 145 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 146 | // 'dangerous_finished_' contains all complete downloads that have not been |
| 147 | // approved. They should be removed. |
| 148 | it = dangerous_finished_.begin(); |
| 149 | for (; it != dangerous_finished_.end(); ++it) |
| 150 | to_remove.insert(it->second); |
| 151 | |
| 152 | // Remove the dangerous download that are not approved. |
| 153 | for (std::set<DownloadItem*>::const_iterator rm_it = to_remove.begin(); |
| 154 | rm_it != to_remove.end(); ++rm_it) { |
| 155 | DownloadItem* download = *rm_it; |
[email protected] | e10e17c7 | 2008-10-15 17:48:32 | [diff] [blame] | 156 | int64 handle = download->db_handle(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 157 | download->Remove(true); |
[email protected] | e10e17c7 | 2008-10-15 17:48:32 | [diff] [blame] | 158 | // Same as above, delete the download if it is not in 'downloads_' (as the |
| 159 | // Remove() call above won't have deleted it). |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 160 | if (handle == DownloadHistory::kUninitializedHandle) |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 161 | delete download; |
| 162 | } |
| 163 | to_remove.clear(); |
| 164 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | in_progress_.clear(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 166 | dangerous_finished_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | STLDeleteValues(&downloads_); |
| 168 | |
| 169 | file_manager_ = NULL; |
| 170 | |
| 171 | // Save our file extensions to auto open. |
| 172 | SaveAutoOpens(); |
| 173 | |
| 174 | // Make sure the save as dialog doesn't notify us back if we're gone before |
| 175 | // it returns. |
| 176 | if (select_file_dialog_.get()) |
| 177 | select_file_dialog_->ListenerDestroyed(); |
| 178 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 179 | download_history_.reset(); |
| 180 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | shutdown_needed_ = false; |
| 182 | } |
| 183 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 184 | void DownloadManager::GetTemporaryDownloads( |
| 185 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 186 | DCHECK(result); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 187 | |
| 188 | for (DownloadMap::iterator it = downloads_.begin(); |
| 189 | it != downloads_.end(); ++it) { |
| 190 | if (it->second->is_temporary() && |
| 191 | it->second->full_path().DirName() == dir_path) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 192 | result->push_back(it->second); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 193 | } |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 196 | void DownloadManager::GetAllDownloads( |
| 197 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 198 | DCHECK(result); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 199 | |
| 200 | for (DownloadMap::iterator it = downloads_.begin(); |
| 201 | it != downloads_.end(); ++it) { |
| 202 | if (!it->second->is_temporary() && |
| 203 | (dir_path.empty() || it->second->full_path().DirName() == dir_path)) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 204 | result->push_back(it->second); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 205 | } |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 208 | void DownloadManager::GetCurrentDownloads( |
| 209 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 210 | DCHECK(result); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 211 | |
| 212 | for (DownloadMap::iterator it = downloads_.begin(); |
| 213 | it != downloads_.end(); ++it) { |
| 214 | if (!it->second->is_temporary() && |
| 215 | (it->second->state() == DownloadItem::IN_PROGRESS || |
| 216 | it->second->safety_state() == DownloadItem::DANGEROUS) && |
| 217 | (dir_path.empty() || it->second->full_path().DirName() == dir_path)) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 218 | result->push_back(it->second); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 219 | } |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 220 | } |
| 221 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | // Query the history service for information about all persisted downloads. |
| 223 | bool DownloadManager::Init(Profile* profile) { |
| 224 | DCHECK(profile); |
| 225 | DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 226 | shutdown_needed_ = true; |
| 227 | |
| 228 | profile_ = profile; |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 229 | request_context_getter_ = profile_->GetRequestContext(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 230 | download_history_.reset(new DownloadHistory(profile, this)); |
| 231 | download_history_->Load( |
| 232 | NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 233 | |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 234 | // In test mode, there may be no ResourceDispatcherHost. In this case it's |
| 235 | // safe to avoid setting |file_manager_| because we only call a small set of |
| 236 | // functions, none of which need it. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 238 | if (rdh) { |
| 239 | file_manager_ = rdh->download_file_manager(); |
| 240 | DCHECK(file_manager_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | } |
| 242 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | // Get our user preference state. |
| 244 | PrefService* prefs = profile_->GetPrefs(); |
| 245 | DCHECK(prefs); |
| 246 | prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL); |
| 247 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL); |
[email protected] | bb69e9b3 | 2008-08-14 23:08:14 | [diff] [blame] | 249 | // Ensure that the download directory specified in the preferences exists. |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 250 | ChromeThread::PostTask( |
| 251 | ChromeThread::FILE, FROM_HERE, |
[email protected] | 309b764 | 2009-12-09 03:08:50 | [diff] [blame] | 252 | NewRunnableFunction(&file_util::CreateDirectory, download_path())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | |
[email protected] | 2b2f8f7 | 2009-02-24 22:42:05 | [diff] [blame] | 254 | // We store any file extension that should be opened automatically at |
| 255 | // download completion in this pref. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 256 | std::string extensions_to_open = |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | prefs->GetString(prefs::kDownloadExtensionsToOpen); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 258 | std::vector<std::string> extensions; |
| 259 | SplitString(extensions_to_open, ':', &extensions); |
| 260 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 261 | for (size_t i = 0; i < extensions.size(); ++i) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 262 | #if defined(OS_POSIX) |
| 263 | FilePath path(extensions[i]); |
| 264 | #elif defined(OS_WIN) |
| 265 | FilePath path(UTF8ToWide(extensions[i])); |
| 266 | #endif |
| 267 | if (!extensions[i].empty() && !IsExecutableFile(path)) |
| 268 | auto_open_.insert(path.value()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 271 | other_download_manager_observer_.reset( |
| 272 | new OtherDownloadManagerObserver(this)); |
| 273 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 274 | return true; |
| 275 | } |
| 276 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 277 | // We have received a message from DownloadFileManager about a new download. We |
| 278 | // create a download item and store it in our download map, and inform the |
| 279 | // history system of a new download. Since this method can be called while the |
| 280 | // history service thread is still reading the persistent state, we do not |
| 281 | // insert the new DownloadItem into 'downloads_' or inform our observers at this |
| 282 | // point. OnCreateDatabaseEntryComplete() handles that finalization of the the |
| 283 | // download creation as a callback from the history thread. |
| 284 | void DownloadManager::StartDownload(DownloadCreateInfo* info) { |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 285 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | DCHECK(info); |
| 287 | |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 288 | // Check whether this download is for an extension install or not. |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 289 | // Allow extensions to be explicitly saved. |
| 290 | if (!info->prompt_user_for_save_location) { |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 291 | if (UserScript::HasUserScriptFileExtension(info->url) || |
| 292 | info->mime_type == Extension::kMimeType) |
| 293 | info->is_extension_install = true; |
| 294 | } |
| 295 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 296 | if (info->save_info.file_path.empty()) { |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 297 | FilePath generated_name; |
| 298 | GenerateFileNameFromInfo(info, &generated_name); |
| 299 | |
| 300 | // Freeze the user's preference for showing a Save As dialog. We're going |
| 301 | // to bounce around a bunch of threads and we don't want to worry about race |
| 302 | // conditions where the user changes this pref out from under us. |
| 303 | if (*prompt_for_download_) { |
| 304 | // But ignore the user's preference for the following scenarios: |
| 305 | // 1) Extension installation. Note that we only care here about the case |
| 306 | // where an extension is installed, not when one is downloaded with |
| 307 | // "save as...". |
| 308 | // 2) Filetypes marked "always open." If the user just wants this file |
| 309 | // opened, don't bother asking where to keep it. |
| 310 | if (!info->is_extension_install && |
| 311 | !ShouldOpenFileBasedOnExtension(generated_name)) |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 312 | info->prompt_user_for_save_location = true; |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 315 | // Determine the proper path for a download, by either one of the following: |
| 316 | // 1) using the default download directory. |
| 317 | // 2) prompting the user. |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 318 | if (info->prompt_user_for_save_location && !last_download_path_.empty()){ |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 319 | info->suggested_path = last_download_path_; |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 320 | } else { |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 321 | info->suggested_path = download_path(); |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 322 | } |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 323 | info->suggested_path = info->suggested_path.Append(generated_name); |
| 324 | } else { |
| 325 | info->suggested_path = info->save_info.file_path; |
| 326 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 328 | if (!info->prompt_user_for_save_location && |
| 329 | info->save_info.file_path.empty()) { |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 330 | // Downloads can be marked as dangerous for two reasons: |
| 331 | // a) They have a dangerous-looking filename |
| 332 | // b) They are an extension that is not from the gallery |
| 333 | if (IsDangerous(info->suggested_path.BaseName())) |
| 334 | info->is_dangerous = true; |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 335 | else if (info->is_extension_install && |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 336 | !ExtensionsService::IsDownloadFromGallery(info->url, |
| 337 | info->referrer_url)) { |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 338 | info->is_dangerous = true; |
| 339 | } |
[email protected] | e9ebf3fc | 2008-10-17 22:06:58 | [diff] [blame] | 340 | } |
| 341 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 342 | // We need to move over to the download thread because we don't want to stat |
| 343 | // the suggested path on the UI thread. |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 344 | ChromeThread::PostTask( |
| 345 | ChromeThread::FILE, FROM_HERE, |
| 346 | NewRunnableMethod( |
| 347 | this, &DownloadManager::CheckIfSuggestedPathExists, info)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 351 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 352 | DCHECK(info); |
| 353 | |
| 354 | // Check writability of the suggested path. If we can't write to it, default |
| 355 | // 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] | 356 | FilePath dir = info->suggested_path.DirName(); |
| 357 | FilePath filename = info->suggested_path.BaseName(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 358 | if (!file_util::PathIsWritable(dir)) { |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 359 | info->prompt_user_for_save_location = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 360 | PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path); |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 361 | info->suggested_path = info->suggested_path.Append(filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | } |
| 363 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 364 | // 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] | 365 | if (info->is_dangerous) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 366 | info->original_name = FilePath(info->suggested_path).BaseName(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 367 | // Create a temporary file to hold the file until the user approves its |
| 368 | // download. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 369 | FilePath::StringType file_name; |
| 370 | FilePath path; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 371 | while (path.empty()) { |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 372 | SStringPrintf(&file_name, FILE_PATH_LITERAL("unconfirmed %d.crdownload"), |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 373 | base::RandInt(0, 100000)); |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 374 | path = dir.Append(file_name); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 375 | if (file_util::PathExists(path)) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 376 | path = FilePath(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 377 | } |
| 378 | info->suggested_path = path; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 379 | } else { |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 380 | // Do not add the path uniquifier if we are saving to a specific path as in |
| 381 | // the drag-out case. |
| 382 | if (info->save_info.file_path.empty()) { |
| 383 | info->path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( |
| 384 | info->suggested_path); |
| 385 | } |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 386 | // We know the final path, build it if necessary. |
| 387 | if (info->path_uniquifier > 0) { |
[email protected] | 5a2388a | 2010-03-26 16:13:39 | [diff] [blame] | 388 | download_util::AppendNumberToPath(&(info->suggested_path), |
| 389 | info->path_uniquifier); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 390 | // Setting path_uniquifier to 0 to make sure we don't try to unique it |
| 391 | // later on. |
| 392 | info->path_uniquifier = 0; |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 393 | } else if (info->path_uniquifier == -1) { |
| 394 | // We failed to find a unique path. We have to prompt the user. |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 395 | info->prompt_user_for_save_location = true; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 396 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 397 | } |
| 398 | |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 399 | // Create an empty file at the suggested path so that we don't allocate the |
| 400 | // same "non-existant" path to multiple downloads. |
| 401 | // See: http://code.google.com/p/chromium/issues/detail?id=3662 |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 402 | if (!info->prompt_user_for_save_location && |
| 403 | info->save_info.file_path.empty()) { |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 404 | if (info->is_dangerous) |
| 405 | file_util::WriteFile(info->suggested_path, "", 0); |
| 406 | else |
| 407 | file_util::WriteFile(download_util::GetCrDownloadPath( |
| 408 | info->suggested_path), "", 0); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 409 | } |
| 410 | |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 411 | ChromeThread::PostTask( |
| 412 | ChromeThread::UI, FROM_HERE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | NewRunnableMethod(this, |
| 414 | &DownloadManager::OnPathExistenceAvailable, |
| 415 | info)); |
| 416 | } |
| 417 | |
| 418 | void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) { |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 419 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::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); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 442 | } else { |
| 443 | // No prompting for download, just continue with the suggested name. |
| 444 | ContinueStartDownload(info, info->suggested_path); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void DownloadManager::ContinueStartDownload(DownloadCreateInfo* info, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 449 | const FilePath& target_path) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 450 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 451 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | scoped_ptr<DownloadCreateInfo> infop(info); |
| 453 | info->path = target_path; |
| 454 | |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 455 | DownloadItem* download = new DownloadItem(this, *info, |
| 456 | profile_->IsOffTheRecord()); |
| 457 | DCHECK(!ContainsKey(in_progress_, info->download_id)); |
| 458 | in_progress_[info->download_id] = download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 459 | |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 460 | bool download_finished = ContainsKey(pending_finished_downloads_, |
| 461 | info->download_id); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 462 | |
| 463 | if (download_finished || info->is_dangerous) { |
| 464 | // The download has already finished or the download is not safe. |
| 465 | // We can now rename the file to its final name (or its tentative name |
| 466 | // in dangerous download cases). |
| 467 | ChromeThread::PostTask( |
| 468 | ChromeThread::FILE, FROM_HERE, |
| 469 | NewRunnableMethod( |
| 470 | file_manager_, &DownloadFileManager::OnFinalDownloadName, |
| 471 | download->id(), target_path, !info->is_dangerous, this)); |
| 472 | } else { |
| 473 | // The download hasn't finished and it is a safe download. We need to |
| 474 | // rename it to its intermediate '.crdownload' path. |
| 475 | FilePath download_path = download_util::GetCrDownloadPath(target_path); |
| 476 | ChromeThread::PostTask( |
| 477 | ChromeThread::FILE, FROM_HERE, |
| 478 | NewRunnableMethod( |
| 479 | file_manager_, &DownloadFileManager::OnIntermediateDownloadName, |
| 480 | download->id(), download_path, this)); |
| 481 | download->set_need_final_rename(true); |
| 482 | } |
| 483 | |
| 484 | if (download_finished) { |
| 485 | // If the download already completed by the time we reached this point, then |
| 486 | // notify observers that it did. |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 487 | DownloadFinished(info->download_id, |
| 488 | pending_finished_downloads_[info->download_id]); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 489 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 490 | |
| 491 | download->Rename(target_path); |
| 492 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 493 | download_history_->AddEntry(*info, download, |
| 494 | NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 495 | |
| 496 | UpdateAppIcon(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 497 | } |
| 498 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 499 | void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
| 500 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 501 | if (it != in_progress_.end()) { |
| 502 | DownloadItem* download = it->second; |
| 503 | download->Update(size); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 504 | download_history_->UpdateEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 505 | } |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 506 | UpdateAppIcon(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void DownloadManager::DownloadFinished(int32 download_id, int64 size) { |
| 510 | DownloadMap::iterator it = in_progress_.find(download_id); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 511 | if (it == in_progress_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 512 | // The download is done, but the user hasn't selected a final location for |
| 513 | // it yet (the Save As dialog box is probably still showing), so just keep |
| 514 | // track of the fact that this download id is complete, when the |
| 515 | // DownloadItem is constructed later we'll notify its completion then. |
| 516 | PendingFinishedMap::iterator erase_it = |
| 517 | pending_finished_downloads_.find(download_id); |
| 518 | DCHECK(erase_it == pending_finished_downloads_.end()); |
| 519 | pending_finished_downloads_[download_id] = size; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 520 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 521 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 522 | |
| 523 | // Remove the id from the list of pending ids. |
| 524 | PendingFinishedMap::iterator erase_it = |
| 525 | pending_finished_downloads_.find(download_id); |
| 526 | if (erase_it != pending_finished_downloads_.end()) |
| 527 | pending_finished_downloads_.erase(erase_it); |
| 528 | |
| 529 | DownloadItem* download = it->second; |
| 530 | download->Finished(size); |
| 531 | |
| 532 | // Clean up will happen when the history system create callback runs if we |
| 533 | // don't have a valid db_handle yet. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 534 | if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 535 | in_progress_.erase(it); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 536 | download_history_->UpdateEntry(download); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 537 | } |
| 538 | |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 539 | UpdateAppIcon(); |
| 540 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 541 | // If this a dangerous download not yet validated by the user, don't do |
| 542 | // anything. When the user notifies us, it will trigger a call to |
| 543 | // ProceedWithFinishedDangerousDownload. |
| 544 | if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 545 | dangerous_finished_[download_id] = download; |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | if (download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) { |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 550 | // We first need to rename the downloaded file from its temporary name to |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 551 | // its final name before we can continue. |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 552 | ChromeThread::PostTask( |
| 553 | ChromeThread::FILE, FROM_HERE, |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 554 | NewRunnableMethod( |
| 555 | this, &DownloadManager::ProceedWithFinishedDangerousDownload, |
| 556 | download->db_handle(), |
| 557 | download->full_path(), download->original_name())); |
| 558 | return; |
| 559 | } |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 560 | |
| 561 | if (download->need_final_rename()) { |
| 562 | ChromeThread::PostTask( |
| 563 | ChromeThread::FILE, FROM_HERE, |
| 564 | NewRunnableMethod( |
| 565 | file_manager_, &DownloadFileManager::OnFinalDownloadName, |
| 566 | download->id(), download->full_path(), false, this)); |
| 567 | return; |
| 568 | } |
| 569 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 570 | ContinueDownloadFinished(download); |
| 571 | } |
| 572 | |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 573 | void DownloadManager::DownloadRenamedToFinalName(int download_id, |
| 574 | const FilePath& full_path) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 575 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 576 | |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 577 | DownloadItem* item = GetDownloadItem(download_id); |
| 578 | if (!item) |
| 579 | return; |
| 580 | item->OnNameFinalized(); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 581 | |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 582 | // This was called from DownloadFinished; continue to call |
| 583 | // ContinueDownloadFinished. |
| 584 | if (item->need_final_rename()) { |
| 585 | item->set_need_final_rename(false); |
| 586 | ContinueDownloadFinished(item); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 587 | } |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 588 | } |
| 589 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 590 | void DownloadManager::ContinueDownloadFinished(DownloadItem* download) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 591 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 592 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 593 | // If this was a dangerous download, it has now been approved and must be |
| 594 | // removed from dangerous_finished_ so it does not get deleted on shutdown. |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 595 | dangerous_finished_.erase(download->id()); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 596 | |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 597 | // Handle chrome extensions explicitly and skip the shell execute. |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 598 | if (download->is_extension_install()) { |
[email protected] | d4a7188 | 2010-06-25 16:36:41 | [diff] [blame] | 599 | OpenChromeExtension(download->full_path(), |
| 600 | download->url(), |
| 601 | download->referrer_url(), |
| 602 | download->original_mime_type()); |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 603 | download->set_auto_opened(true); |
| 604 | } else if (download->open_when_complete() || |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 605 | ShouldOpenFileBasedOnExtension(download->full_path()) || |
| 606 | download->is_temporary()) { |
| 607 | // If the download is temporary, like in drag-and-drop, do not open it but |
| 608 | // we still need to set it auto-opened so that it can be removed from the |
| 609 | // download shelf. |
| 610 | if (!download->is_temporary()) |
| 611 | OpenDownloadInShell(download, NULL); |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 612 | download->set_auto_opened(true); |
| 613 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 614 | |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 615 | // Notify our observers that we are complete (the call to Finished() set the |
| 616 | // state to complete but did not notify). |
| 617 | download->UpdateObservers(); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 618 | |
| 619 | // The download file is meant to be completed if both the filename is |
| 620 | // finalized and the file data is downloaded. The ordering of these two |
| 621 | // actions is indeterministic. Thus, if the filename is not finalized yet, |
| 622 | // delay the notification. |
| 623 | if (download->name_finalized()) |
| 624 | download->NotifyObserversDownloadFileCompleted(); |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 625 | } |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 626 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 627 | // Called on the file thread. Renames the downloaded file to its original name. |
| 628 | void DownloadManager::ProceedWithFinishedDangerousDownload( |
| 629 | int64 download_handle, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 630 | const FilePath& path, |
| 631 | const FilePath& original_name) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 632 | bool success = false; |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 633 | FilePath new_path; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 634 | int uniquifier = 0; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 635 | if (file_util::PathExists(path)) { |
[email protected] | 889ed35c | 2009-01-21 00:07:24 | [diff] [blame] | 636 | new_path = path.DirName().Append(original_name); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 637 | // Make our name unique at this point, as if a dangerous file is downloading |
| 638 | // and a 2nd download is started for a file with the same name, they would |
| 639 | // have the same path. This is because we uniquify the name on download |
| 640 | // start, and at that time the first file does not exists yet, so the second |
| 641 | // file gets the same name. |
[email protected] | 5a2388a | 2010-03-26 16:13:39 | [diff] [blame] | 642 | uniquifier = download_util::GetUniquePathNumber(new_path); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 643 | if (uniquifier > 0) |
[email protected] | 5a2388a | 2010-03-26 16:13:39 | [diff] [blame] | 644 | download_util::AppendNumberToPath(&new_path, uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 645 | success = file_util::Move(path, new_path); |
| 646 | } else { |
| 647 | NOTREACHED(); |
| 648 | } |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 649 | |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 650 | ChromeThread::PostTask( |
| 651 | ChromeThread::UI, FROM_HERE, |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 652 | NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 653 | download_handle, success, new_path, uniquifier)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | // Call from the file thread when the finished dangerous download was renamed. |
| 657 | void DownloadManager::DangerousDownloadRenamed(int64 download_handle, |
| 658 | bool success, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 659 | const FilePath& new_path, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 660 | int new_path_uniquifier) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 661 | DownloadMap::iterator it = downloads_.find(download_handle); |
| 662 | if (it == downloads_.end()) { |
| 663 | NOTREACHED(); |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | DownloadItem* download = it->second; |
| 668 | // 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] | 669 | if (success) { |
| 670 | // We need to update the path uniquifier so that the UI shows the right |
| 671 | // name when calling GetFileName(). |
| 672 | download->set_path_uniquifier(new_path_uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 673 | RenameDownload(download, new_path); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 674 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 675 | |
| 676 | // Continue the download finished sequence. |
| 677 | ContinueDownloadFinished(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 678 | } |
| 679 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 680 | void DownloadManager::DownloadCancelled(int32 download_id) { |
| 681 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 682 | if (it == in_progress_.end()) |
| 683 | return; |
| 684 | DownloadItem* download = it->second; |
| 685 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 686 | // Clean up will happen when the history system create callback runs if we |
| 687 | // don't have a valid db_handle yet. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 688 | if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 689 | in_progress_.erase(it); |
[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()); |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 696 | UpdateAppIcon(); |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void DownloadManager::DownloadCancelledInternal(int download_id, |
| 700 | int render_process_id, |
| 701 | int request_id) { |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 702 | // Cancel the network request. RDH is guaranteed to outlive the IO thread. |
| 703 | ChromeThread::PostTask( |
| 704 | ChromeThread::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 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 710 | // Tell the file manager to cancel the download. |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 711 | file_manager_->RemoveDownload(download_id, this); // On the UI thread |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 712 | ChromeThread::PostTask( |
| 713 | ChromeThread::FILE, FROM_HERE, |
| 714 | NewRunnableMethod( |
| 715 | file_manager_, &DownloadFileManager::CancelDownload, download_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | void DownloadManager::PauseDownload(int32 download_id, bool pause) { |
| 719 | DownloadMap::iterator it = in_progress_.find(download_id); |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 720 | if (it == in_progress_.end()) |
| 721 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 722 | |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 723 | DownloadItem* download = it->second; |
| 724 | if (pause == download->is_paused()) |
| 725 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 726 | |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 727 | // Inform the ResourceDispatcherHost of the new pause state. |
| 728 | ChromeThread::PostTask( |
| 729 | ChromeThread::IO, FROM_HERE, |
| 730 | NewRunnableFunction(&DownloadManager::OnPauseDownloadRequest, |
| 731 | g_browser_process->resource_dispatcher_host(), |
| 732 | download->render_process_id(), |
| 733 | download->request_id(), |
| 734 | pause)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | // static |
| 738 | void DownloadManager::OnPauseDownloadRequest(ResourceDispatcherHost* rdh, |
| 739 | int render_process_id, |
| 740 | int request_id, |
| 741 | bool pause) { |
| 742 | rdh->PauseRequest(render_process_id, request_id, pause); |
| 743 | } |
| 744 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 745 | bool DownloadManager::IsDangerous(const FilePath& file_name) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 746 | // TODO(jcampan): Improve me. |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 747 | return IsExecutableFile(file_name); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 748 | } |
| 749 | |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 750 | void DownloadManager::UpdateAppIcon() { |
| 751 | int64 total_bytes = 0; |
| 752 | int64 received_bytes = 0; |
| 753 | int download_count = 0; |
| 754 | bool progress_known = true; |
| 755 | |
| 756 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 757 | i != in_progress_.end(); |
| 758 | ++i) { |
| 759 | ++download_count; |
| 760 | const DownloadItem* item = i->second; |
| 761 | if (item->total_bytes() > 0) { |
| 762 | total_bytes += item->total_bytes(); |
| 763 | received_bytes += item->received_bytes(); |
| 764 | } else { |
| 765 | // This download didn't specify a Content-Length, so the combined progress |
| 766 | // bar neeeds to be indeterminate. |
| 767 | progress_known = false; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | float progress = 0; |
| 772 | if (progress_known && download_count) |
| 773 | progress = (float)received_bytes / total_bytes; |
| 774 | |
| 775 | download_util::UpdateAppIconDownloadProgress(download_count, |
| 776 | progress_known, |
| 777 | progress); |
| 778 | } |
| 779 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 780 | void DownloadManager::RenameDownload(DownloadItem* download, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 781 | const FilePath& new_path) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 782 | download->Rename(new_path); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 783 | download_history_->UpdateDownloadPath(download, new_path); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 784 | } |
| 785 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 786 | void DownloadManager::RemoveDownload(int64 download_handle) { |
| 787 | DownloadMap::iterator it = downloads_.find(download_handle); |
| 788 | if (it == downloads_.end()) |
| 789 | return; |
| 790 | |
| 791 | // Make history update. |
| 792 | DownloadItem* download = it->second; |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 793 | download_history_->RemoveEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 794 | |
| 795 | // Remove from our tables and delete. |
| 796 | downloads_.erase(it); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 797 | it = dangerous_finished_.find(download->id()); |
| 798 | if (it != dangerous_finished_.end()) |
| 799 | dangerous_finished_.erase(it); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 800 | |
| 801 | // Tell observers to refresh their views. |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 802 | NotifyModelChanged(); |
[email protected] | 6f71287 | 2008-11-07 00:35:36 | [diff] [blame] | 803 | |
| 804 | delete download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 805 | } |
| 806 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 807 | int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin, |
| 808 | const base::Time remove_end) { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 809 | download_history_->RemoveEntriesBetween(remove_begin, remove_end); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 810 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 811 | DownloadMap::iterator it = downloads_.begin(); |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 812 | std::vector<DownloadItem*> pending_deletes; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 813 | while (it != downloads_.end()) { |
| 814 | DownloadItem* download = it->second; |
| 815 | DownloadItem::DownloadState state = download->state(); |
| 816 | if (download->start_time() >= remove_begin && |
| 817 | (remove_end.is_null() || download->start_time() < remove_end) && |
| 818 | (state == DownloadItem::COMPLETE || |
| 819 | state == DownloadItem::CANCELLED)) { |
| 820 | // Remove from the map and move to the next in the list. |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 821 | downloads_.erase(it++); |
[email protected] | a6604d9 | 2008-10-30 00:58:58 | [diff] [blame] | 822 | |
| 823 | // Also remove it from any completed dangerous downloads. |
| 824 | DownloadMap::iterator dit = dangerous_finished_.find(download->id()); |
| 825 | if (dit != dangerous_finished_.end()) |
| 826 | dangerous_finished_.erase(dit); |
| 827 | |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 828 | pending_deletes.push_back(download); |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 829 | // Observer interface. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 830 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 831 | continue; |
| 832 | } |
| 833 | |
| 834 | ++it; |
| 835 | } |
| 836 | |
| 837 | // Tell observers to refresh their views. |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 838 | int num_deleted = static_cast<int>(pending_deletes.size()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 839 | if (num_deleted > 0) |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 840 | NotifyModelChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 841 | |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 842 | // Delete the download items after updating the observers. |
| 843 | STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end()); |
| 844 | pending_deletes.clear(); |
| 845 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 846 | return num_deleted; |
| 847 | } |
| 848 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 849 | int DownloadManager::RemoveDownloads(const base::Time remove_begin) { |
| 850 | return RemoveDownloadsBetween(remove_begin, base::Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 851 | } |
| 852 | |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 853 | int DownloadManager::RemoveAllDownloads() { |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 854 | if (this != profile_->GetOriginalProfile()->GetDownloadManager()) { |
| 855 | // This is an incognito downloader. Clear All should clear main download |
| 856 | // manager as well. |
| 857 | profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads(); |
| 858 | } |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 859 | // The null times make the date range unbounded. |
| 860 | return RemoveDownloadsBetween(base::Time(), base::Time()); |
| 861 | } |
| 862 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 863 | // Initiate a download of a specific URL. We send the request to the |
| 864 | // ResourceDispatcherHost, and let it send us responses like a regular |
| 865 | // download. |
| 866 | void DownloadManager::DownloadUrl(const GURL& url, |
| 867 | const GURL& referrer, |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 868 | const std::string& referrer_charset, |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 869 | TabContents* tab_contents) { |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 870 | DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(), |
| 871 | tab_contents); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | void DownloadManager::DownloadUrlToFile(const GURL& url, |
| 875 | const GURL& referrer, |
| 876 | const std::string& referrer_charset, |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 877 | const DownloadSaveInfo& save_info, |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 878 | TabContents* tab_contents) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 879 | DCHECK(tab_contents); |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 880 | ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, |
| 881 | NewRunnableFunction(&download_util::DownloadUrl, |
| 882 | url, |
| 883 | referrer, |
| 884 | referrer_charset, |
| 885 | save_info, |
| 886 | g_browser_process->resource_dispatcher_host(), |
| 887 | tab_contents->GetRenderProcessHost()->id(), |
| 888 | tab_contents->render_view_host()->routing_id(), |
| 889 | request_context_getter_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 890 | } |
| 891 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 892 | void DownloadManager::GenerateExtension( |
| 893 | const FilePath& file_name, |
| 894 | const std::string& mime_type, |
| 895 | FilePath::StringType* generated_extension) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 896 | // We're worried about three things here: |
| 897 | // |
| 898 | // 1) Security. Many sites let users upload content, such as buddy icons, to |
| 899 | // their web sites. We want to mitigate the case where an attacker |
| 900 | // supplies a malicious executable with an executable file extension but an |
| 901 | // honest site serves the content with a benign content type, such as |
| 902 | // image/jpeg. |
| 903 | // |
| 904 | // 2) Usability. If the site fails to provide a file extension, we want to |
| 905 | // guess a reasonable file extension based on the content type. |
| 906 | // |
| 907 | // 3) Shell integration. Some file extensions automatically integrate with |
| 908 | // the shell. We block these extensions to prevent a malicious web site |
| 909 | // from integrating with the user's shell. |
| 910 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 911 | static const FilePath::CharType default_extension[] = |
| 912 | FILE_PATH_LITERAL("download"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 913 | |
| 914 | // See if our file name already contains an extension. |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 915 | FilePath::StringType extension = file_name.Extension(); |
| 916 | if (!extension.empty()) |
| 917 | extension.erase(extension.begin()); // Erase preceding '.'. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 918 | |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 919 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 920 | // Rename shell-integrated extensions. |
| 921 | if (win_util::IsShellIntegratedExtension(extension)) |
| 922 | extension.assign(default_extension); |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 923 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 924 | |
| 925 | std::string mime_type_from_extension; |
[email protected] | bae0ea1 | 2009-02-14 01:20:41 | [diff] [blame] | 926 | net::GetMimeTypeFromFile(file_name, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 927 | &mime_type_from_extension); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 928 | if (mime_type == mime_type_from_extension) { |
| 929 | // The hinted extension matches the mime type. It looks like a winner. |
| 930 | generated_extension->swap(extension); |
| 931 | return; |
| 932 | } |
| 933 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 934 | if (IsExecutableExtension(extension) && !IsExecutableMimeType(mime_type)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 935 | // We want to be careful about executable extensions. The worry here is |
| 936 | // that a trusted web site could be tricked into dropping an executable file |
| 937 | // on the user's filesystem. |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 938 | if (!net::GetPreferredExtensionForMimeType(mime_type, &extension)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 939 | // We couldn't find a good extension for this content type. Use a dummy |
| 940 | // extension instead. |
| 941 | extension.assign(default_extension); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (extension.empty()) { |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 946 | net::GetPreferredExtensionForMimeType(mime_type, &extension); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 947 | } else { |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 948 | // Append extension generated from the mime type if: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 949 | // 1. New extension is not ".txt" |
| 950 | // 2. New extension is not the same as the already existing extension. |
| 951 | // 3. New extension is not executable. This action mitigates the case when |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 952 | // an executable is hidden in a benign file extension; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 953 | // E.g. my-cat.jpg becomes my-cat.jpg.js if content type is |
| 954 | // application/x-javascript. |
[email protected] | 3432291 | 2010-07-27 21:24:39 | [diff] [blame] | 955 | // 4. New extension is not ".tar" for .tar.gz files. For misconfigured web |
[email protected] | e106457b | 2009-03-25 22:43:37 | [diff] [blame] | 956 | // servers, i.e. bug 5772. |
[email protected] | e32642f6 | 2009-09-11 21:58:29 | [diff] [blame] | 957 | // 5. The original extension is not ".tgz" & the new extension is not "gz". |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 958 | FilePath::StringType append_extension; |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 959 | if (net::GetPreferredExtensionForMimeType(mime_type, &append_extension)) { |
[email protected] | 3f15655 | 2009-02-09 19:44:17 | [diff] [blame] | 960 | if (append_extension != FILE_PATH_LITERAL("txt") && |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 961 | append_extension != extension && |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 962 | !IsExecutableExtension(append_extension) && |
[email protected] | e32642f6 | 2009-09-11 21:58:29 | [diff] [blame] | 963 | !(append_extension == FILE_PATH_LITERAL("gz") && |
| 964 | extension == FILE_PATH_LITERAL("tgz")) && |
[email protected] | e106457b | 2009-03-25 22:43:37 | [diff] [blame] | 965 | (append_extension != FILE_PATH_LITERAL("tar") || |
[email protected] | 3432291 | 2010-07-27 21:24:39 | [diff] [blame] | 966 | extension != FILE_PATH_LITERAL("tar.gz"))) { |
[email protected] | 3f15655 | 2009-02-09 19:44:17 | [diff] [blame] | 967 | extension += FILE_PATH_LITERAL("."); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 968 | extension += append_extension; |
[email protected] | 3f15655 | 2009-02-09 19:44:17 | [diff] [blame] | 969 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
| 973 | generated_extension->swap(extension); |
| 974 | } |
| 975 | |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 976 | void DownloadManager::GenerateFileNameFromInfo(DownloadCreateInfo* info, |
| 977 | FilePath* generated_name) { |
| 978 | GenerateFileName(GURL(info->url), |
| 979 | info->content_disposition, |
| 980 | info->referrer_charset, |
| 981 | info->mime_type, |
| 982 | generated_name); |
| 983 | } |
| 984 | |
| 985 | void DownloadManager::GenerateFileName(const GURL& url, |
| 986 | const std::string& content_disposition, |
| 987 | const std::string& referrer_charset, |
| 988 | const std::string& mime_type, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 989 | FilePath* generated_name) { |
[email protected] | 630947c | 2009-11-04 18:37:31 | [diff] [blame] | 990 | std::wstring default_name = |
| 991 | l10n_util::GetString(IDS_DEFAULT_DOWNLOAD_FILENAME); |
| 992 | #if defined(OS_WIN) |
| 993 | FilePath default_file_path(default_name); |
| 994 | #elif defined(OS_POSIX) |
| 995 | FilePath default_file_path(base::SysWideToNativeMB(default_name)); |
| 996 | #endif |
| 997 | |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 998 | *generated_name = net::GetSuggestedFilename(GURL(url), |
| 999 | content_disposition, |
| 1000 | referrer_charset, |
[email protected] | 630947c | 2009-11-04 18:37:31 | [diff] [blame] | 1001 | default_file_path); |
| 1002 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1003 | DCHECK(!generated_name->empty()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1004 | |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1005 | GenerateSafeFileName(mime_type, generated_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | void DownloadManager::AddObserver(Observer* observer) { |
| 1009 | observers_.AddObserver(observer); |
| 1010 | observer->ModelChanged(); |
| 1011 | } |
| 1012 | |
| 1013 | void DownloadManager::RemoveObserver(Observer* observer) { |
| 1014 | observers_.RemoveObserver(observer); |
| 1015 | } |
| 1016 | |
| 1017 | // Post Windows Shell operations to the Download thread, to avoid blocking the |
| 1018 | // user interface. |
| 1019 | void DownloadManager::ShowDownloadInShell(const DownloadItem* download) { |
| 1020 | DCHECK(file_manager_); |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 1021 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
[email protected] | 8b6ff01 | 2009-08-18 22:29:58 | [diff] [blame] | 1022 | #if defined(OS_MACOSX) |
| 1023 | // Mac needs to run this operation on the UI thread. |
| 1024 | platform_util::ShowItemInFolder(download->full_path()); |
| 1025 | #else |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 1026 | ChromeThread::PostTask( |
| 1027 | ChromeThread::FILE, FROM_HERE, |
| 1028 | NewRunnableMethod( |
| 1029 | file_manager_, &DownloadFileManager::OnShowDownloadInShell, |
| 1030 | FilePath(download->full_path()))); |
[email protected] | 8b6ff01 | 2009-08-18 22:29:58 | [diff] [blame] | 1031 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1032 | } |
| 1033 | |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 1034 | void DownloadManager::OpenDownload(const DownloadItem* download, |
| 1035 | gfx::NativeView parent_window) { |
[email protected] | 0e34d789 | 2009-06-05 19:17:40 | [diff] [blame] | 1036 | // Open Chrome extensions with ExtensionsService. For everything else do shell |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 1037 | // execute. |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 1038 | if (download->is_extension_install()) { |
[email protected] | d4a7188 | 2010-06-25 16:36:41 | [diff] [blame] | 1039 | OpenChromeExtension(download->full_path(), |
| 1040 | download->url(), |
| 1041 | download->referrer_url(), |
| 1042 | download->original_mime_type()); |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 1043 | } else { |
| 1044 | OpenDownloadInShell(download, parent_window); |
| 1045 | } |
| 1046 | } |
| 1047 | |
[email protected] | d4a7188 | 2010-06-25 16:36:41 | [diff] [blame] | 1048 | void DownloadManager::OpenChromeExtension( |
| 1049 | const FilePath& full_path, |
| 1050 | const GURL& download_url, |
| 1051 | const GURL& referrer_url, |
| 1052 | const std::string& original_mime_type) { |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 1053 | // We don't support extensions in OTR mode. |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 1054 | ExtensionsService* service = profile_->GetExtensionsService(); |
| 1055 | if (service) { |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 1056 | NotificationService* nservice = NotificationService::current(); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 1057 | GURL nonconst_download_url = download_url; |
| 1058 | nservice->Notify(NotificationType::EXTENSION_READY_FOR_INSTALL, |
| 1059 | Source<DownloadManager>(this), |
| 1060 | Details<GURL>(&nonconst_download_url)); |
| 1061 | |
| 1062 | scoped_refptr<CrxInstaller> installer( |
| 1063 | new CrxInstaller(service->install_directory(), |
| 1064 | service, |
| 1065 | new ExtensionInstallUI(profile_))); |
| 1066 | installer->set_delete_source(true); |
| 1067 | |
[email protected] | a050d71 | 2009-11-06 00:40:10 | [diff] [blame] | 1068 | if (UserScript::HasUserScriptFileExtension(download_url)) { |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 1069 | installer->InstallUserScript(full_path, download_url); |
[email protected] | 6657afa6 | 2009-11-04 02:15:20 | [diff] [blame] | 1070 | } else { |
[email protected] | be18e158 | 2010-06-03 21:50:42 | [diff] [blame] | 1071 | bool is_gallery_download = |
| 1072 | ExtensionsService::IsDownloadFromGallery(download_url, referrer_url); |
[email protected] | d4a7188 | 2010-06-25 16:36:41 | [diff] [blame] | 1073 | installer->set_original_mime_type(original_mime_type); |
| 1074 | installer->set_apps_require_extension_mime_type(true); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 1075 | installer->set_allow_privilege_increase(true); |
| 1076 | installer->set_original_url(download_url); |
[email protected] | 9f72aa0 | 2010-06-25 10:01:05 | [diff] [blame] | 1077 | installer->set_limit_web_extent_to_download_host(!is_gallery_download); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 1078 | installer->InstallCrx(full_path); |
[email protected] | 6657afa6 | 2009-11-04 02:15:20 | [diff] [blame] | 1079 | } |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 1080 | } else { |
| 1081 | TabContents* contents = NULL; |
[email protected] | a65bce4e | 2010-06-09 16:00:15 | [diff] [blame] | 1082 | // Get last active normal browser of profile. |
| 1083 | Browser* last_active = BrowserList::FindBrowserWithType(profile_, |
[email protected] | 074e4f4 | 2010-06-23 16:12:06 | [diff] [blame] | 1084 | Browser::TYPE_NORMAL, true); |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 1085 | if (last_active) |
| 1086 | contents = last_active->GetSelectedTabContents(); |
| 1087 | if (contents) { |
| 1088 | contents->AddInfoBar( |
| 1089 | new SimpleAlertInfoBarDelegate(contents, |
| 1090 | l10n_util::GetString( |
| 1091 | IDS_EXTENSION_INCOGNITO_INSTALL_INFOBAR_LABEL), |
| 1092 | ResourceBundle::GetSharedInstance().GetBitmapNamed( |
[email protected] | 938e1f9 | 2010-04-01 18:09:42 | [diff] [blame] | 1093 | IDR_INFOBAR_PLUGIN_INSTALL), |
| 1094 | true)); |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 1095 | } |
[email protected] | 2a464a9 | 2009-08-01 17:58:35 | [diff] [blame] | 1096 | } |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 1097 | } |
| 1098 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1099 | void DownloadManager::OpenDownloadInShell(const DownloadItem* download, |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 1100 | gfx::NativeView parent_window) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1101 | DCHECK(file_manager_); |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 1102 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
[email protected] | 8b6ff01 | 2009-08-18 22:29:58 | [diff] [blame] | 1103 | #if defined(OS_MACOSX) |
| 1104 | // Mac OS X requires opening downloads on the UI thread. |
| 1105 | platform_util::OpenItem(download->full_path()); |
| 1106 | #else |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 1107 | ChromeThread::PostTask( |
| 1108 | ChromeThread::FILE, FROM_HERE, |
| 1109 | NewRunnableMethod( |
| 1110 | file_manager_, &DownloadFileManager::OnOpenDownloadInShell, |
| 1111 | download->full_path(), download->url(), parent_window)); |
[email protected] | 8b6ff01 | 2009-08-18 22:29:58 | [diff] [blame] | 1112 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1113 | } |
| 1114 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1115 | void DownloadManager::OpenFilesBasedOnExtension( |
| 1116 | const FilePath& path, bool open) { |
| 1117 | FilePath::StringType extension = path.Extension(); |
| 1118 | if (extension.empty()) |
| 1119 | return; |
| 1120 | DCHECK(extension[0] == FilePath::kExtensionSeparator); |
| 1121 | extension.erase(0, 1); |
| 1122 | if (open && !IsExecutableExtension(extension)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1123 | auto_open_.insert(extension); |
| 1124 | else |
| 1125 | auto_open_.erase(extension); |
| 1126 | SaveAutoOpens(); |
| 1127 | } |
| 1128 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1129 | bool DownloadManager::ShouldOpenFileBasedOnExtension( |
| 1130 | const FilePath& path) const { |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1131 | FilePath::StringType extension = path.Extension(); |
| 1132 | if (extension.empty()) |
| 1133 | return false; |
| 1134 | if (IsExecutableExtension(extension)) |
| 1135 | return false; |
[email protected] | 92e11c8 | 2010-01-13 06:39:56 | [diff] [blame] | 1136 | if (Extension::IsExtension(path)) |
| 1137 | return false; |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1138 | DCHECK(extension[0] == FilePath::kExtensionSeparator); |
| 1139 | extension.erase(0, 1); |
[email protected] | 92e11c8 | 2010-01-13 06:39:56 | [diff] [blame] | 1140 | if (auto_open_.find(extension) != auto_open_.end()) |
[email protected] | 8c756ac | 2009-01-30 23:36:41 | [diff] [blame] | 1141 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1142 | return false; |
| 1143 | } |
| 1144 | |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1145 | static const char* kExecutableWhiteList[] = { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1146 | // JavaScript is just as powerful as EXE. |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1147 | "text/javascript", |
| 1148 | "text/javascript;version=*", |
[email protected] | 54d8d45 | 2009-04-08 17:29:24 | [diff] [blame] | 1149 | // Registry files can cause critical changes to the MS OS behavior. |
| 1150 | // Addition of this mimetype also addresses bug 7337. |
| 1151 | "text/x-registry", |
[email protected] | 60ff8f91 | 2008-12-05 07:58:39 | [diff] [blame] | 1152 | // Some sites use binary/octet-stream to mean application/octet-stream. |
| 1153 | // See http://code.google.com/p/chromium/issues/detail?id=1573 |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1154 | "binary/octet-stream" |
| 1155 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1156 | |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1157 | static const char* kExecutableBlackList[] = { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1158 | // These application types are not executable. |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1159 | "application/*+xml", |
| 1160 | "application/xml" |
| 1161 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1162 | |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1163 | // static |
| 1164 | bool DownloadManager::IsExecutableMimeType(const std::string& mime_type) { |
[email protected] | bae0ea1 | 2009-02-14 01:20:41 | [diff] [blame] | 1165 | for (size_t i = 0; i < arraysize(kExecutableWhiteList); ++i) { |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1166 | if (net::MatchesMimeType(kExecutableWhiteList[i], mime_type)) |
| 1167 | return true; |
| 1168 | } |
[email protected] | bae0ea1 | 2009-02-14 01:20:41 | [diff] [blame] | 1169 | for (size_t i = 0; i < arraysize(kExecutableBlackList); ++i) { |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1170 | if (net::MatchesMimeType(kExecutableBlackList[i], mime_type)) |
| 1171 | return false; |
| 1172 | } |
| 1173 | // We consider only other application types to be executable. |
| 1174 | return net::MatchesMimeType("application/*", mime_type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1175 | } |
| 1176 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1177 | bool DownloadManager::IsExecutableFile(const FilePath& path) const { |
| 1178 | return IsExecutableExtension(path.Extension()); |
| 1179 | } |
| 1180 | |
| 1181 | bool DownloadManager::IsExecutableExtension( |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1182 | const FilePath::StringType& extension) { |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1183 | if (extension.empty()) |
| 1184 | return false; |
[email protected] | 64da0b93 | 2009-02-24 02:30:04 | [diff] [blame] | 1185 | if (!IsStringASCII(extension)) |
| 1186 | return false; |
[email protected] | a0a9577b | 2009-05-27 23:52:32 | [diff] [blame] | 1187 | #if defined(OS_WIN) |
[email protected] | 64da0b93 | 2009-02-24 02:30:04 | [diff] [blame] | 1188 | std::string ascii_extension = WideToASCII(extension); |
[email protected] | e9ef0a6 | 2009-08-11 22:50:13 | [diff] [blame] | 1189 | #elif defined(OS_POSIX) |
[email protected] | a0a9577b | 2009-05-27 23:52:32 | [diff] [blame] | 1190 | std::string ascii_extension = extension; |
| 1191 | #endif |
[email protected] | 64da0b93 | 2009-02-24 02:30:04 | [diff] [blame] | 1192 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1193 | // Strip out leading dot if it's still there |
| 1194 | if (ascii_extension[0] == FilePath::kExtensionSeparator) |
| 1195 | ascii_extension.erase(0, 1); |
| 1196 | |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1197 | return download_util::IsExecutableExtension(ascii_extension); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | void DownloadManager::ResetAutoOpenFiles() { |
| 1201 | auto_open_.clear(); |
| 1202 | SaveAutoOpens(); |
| 1203 | } |
| 1204 | |
| 1205 | bool DownloadManager::HasAutoOpenFileTypesRegistered() const { |
| 1206 | return !auto_open_.empty(); |
| 1207 | } |
| 1208 | |
| 1209 | void DownloadManager::SaveAutoOpens() { |
| 1210 | PrefService* prefs = profile_->GetPrefs(); |
| 1211 | if (prefs) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 1212 | std::string extensions; |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 1213 | for (AutoOpenSet::iterator it = auto_open_.begin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1214 | it != auto_open_.end(); ++it) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 1215 | #if defined(OS_POSIX) |
| 1216 | std::string this_extension = *it; |
| 1217 | #elif defined(OS_WIN) |
| 1218 | std::string this_extension = base::SysWideToUTF8(*it); |
| 1219 | #endif |
| 1220 | extensions += this_extension + ":"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1221 | } |
| 1222 | if (!extensions.empty()) |
| 1223 | extensions.erase(extensions.size() - 1); |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 1224 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 1225 | prefs->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 1229 | DownloadItem* DownloadManager::GetDownloadItemFromDbHandle(int64 db_handle) { |
| 1230 | for (DownloadMap::iterator it = downloads_.begin(); |
| 1231 | it != downloads_.end(); ++it) { |
| 1232 | DownloadItem* item = it->second; |
| 1233 | if (item->db_handle() == db_handle) |
| 1234 | return item; |
| 1235 | } |
| 1236 | return NULL; |
| 1237 | } |
| 1238 | |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 1239 | void DownloadManager::FileSelected(const FilePath& path, |
[email protected] | 23b357b | 2009-03-30 20:02:36 | [diff] [blame] | 1240 | int index, void* params) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1241 | DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 1242 | if (info->prompt_user_for_save_location) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1243 | last_download_path_ = path.DirName(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1244 | ContinueStartDownload(info, path); |
| 1245 | } |
| 1246 | |
| 1247 | void DownloadManager::FileSelectionCanceled(void* params) { |
| 1248 | // The user didn't pick a place to save the file, so need to cancel the |
| 1249 | // download that's already in progress to the temporary location. |
| 1250 | DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 1251 | DownloadCancelledInternal(info->download_id, |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 1252 | info->child_id, |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 1253 | info->request_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1254 | } |
| 1255 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1256 | void DownloadManager::DeleteDownload(const FilePath& path) { |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 1257 | ChromeThread::PostTask( |
| 1258 | ChromeThread::FILE, FROM_HERE, |
[email protected] | da74e5ae2 | 2010-07-14 16:12:37 | [diff] [blame] | 1259 | NewRunnableFunction(&DeleteDownloadedFile, path)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 1260 | } |
| 1261 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 1262 | void DownloadManager::DangerousDownloadValidated(DownloadItem* download) { |
| 1263 | DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state()); |
| 1264 | download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED); |
| 1265 | download->UpdateObservers(); |
| 1266 | |
| 1267 | // If the download is not complete, nothing to do. The required |
| 1268 | // post-processing will be performed when it does complete. |
| 1269 | if (download->state() != DownloadItem::COMPLETE) |
| 1270 | return; |
| 1271 | |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 1272 | ChromeThread::PostTask( |
| 1273 | ChromeThread::FILE, FROM_HERE, |
| 1274 | NewRunnableMethod( |
| 1275 | this, &DownloadManager::ProceedWithFinishedDangerousDownload, |
| 1276 | download->db_handle(), download->full_path(), |
| 1277 | download->original_name())); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 1278 | } |
| 1279 | |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 1280 | void DownloadManager::GenerateSafeFileName(const std::string& mime_type, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1281 | FilePath* file_name) { |
| 1282 | // Make sure we get the right file extension |
| 1283 | FilePath::StringType extension; |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1284 | GenerateExtension(*file_name, mime_type, &extension); |
[email protected] | 1bdaa60c | 2010-06-01 01:57:43 | [diff] [blame] | 1285 | *file_name = file_name->ReplaceExtension(extension); |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1286 | |
[email protected] | 2b2f8f7 | 2009-02-24 22:42:05 | [diff] [blame] | 1287 | #if defined(OS_WIN) |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1288 | // Prepend "_" to the file name if it's a reserved name |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1289 | FilePath::StringType leaf_name = file_name->BaseName().value(); |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1290 | DCHECK(!leaf_name.empty()); |
| 1291 | if (win_util::IsReservedName(leaf_name)) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1292 | leaf_name = FilePath::StringType(FILE_PATH_LITERAL("_")) + leaf_name; |
| 1293 | *file_name = file_name->DirName(); |
| 1294 | if (file_name->value() == FilePath::kCurrentDirectory) { |
| 1295 | *file_name = FilePath(leaf_name); |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1296 | } else { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1297 | *file_name = file_name->Append(leaf_name); |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1298 | } |
| 1299 | } |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 1300 | #endif |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1301 | } |
| 1302 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1303 | // Operations posted to us from the history service ---------------------------- |
| 1304 | |
| 1305 | // The history service has retrieved all download entries. 'entries' contains |
| 1306 | // 'DownloadCreateInfo's in sorted order (by ascending start_time). |
| 1307 | void DownloadManager::OnQueryDownloadEntriesComplete( |
| 1308 | std::vector<DownloadCreateInfo>* entries) { |
| 1309 | for (size_t i = 0; i < entries->size(); ++i) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 1310 | DownloadItem* download = new DownloadItem(this, entries->at(i)); |
| 1311 | DCHECK(!ContainsKey(downloads_, download->db_handle())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1312 | downloads_[download->db_handle()] = download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1313 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1314 | NotifyModelChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1315 | } |
| 1316 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1317 | // Once the new DownloadItem's creation info has been committed to the history |
| 1318 | // service, we associate the DownloadItem with the db handle, update our |
| 1319 | // 'downloads_' map and inform observers. |
| 1320 | void DownloadManager::OnCreateDownloadEntryComplete(DownloadCreateInfo info, |
| 1321 | int64 db_handle) { |
| 1322 | DownloadMap::iterator it = in_progress_.find(info.download_id); |
| 1323 | DCHECK(it != in_progress_.end()); |
| 1324 | |
| 1325 | DownloadItem* download = it->second; |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 1326 | |
| 1327 | // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
| 1328 | // call this function with an invalid |db_handle|. For instance, this can |
| 1329 | // happen when the history database is offline. We cannot have multiple |
| 1330 | // DownloadItems with the same invalid db_handle, so we need to assign a |
| 1331 | // unique |db_handle| here. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 1332 | if (db_handle == DownloadHistory::kUninitializedHandle) |
| 1333 | db_handle = download_history_->GetNextFakeDbHandle(); |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 1334 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 1335 | DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1336 | download->set_db_handle(db_handle); |
| 1337 | |
| 1338 | // Insert into our full map. |
| 1339 | DCHECK(downloads_.find(download->db_handle()) == downloads_.end()); |
| 1340 | downloads_[download->db_handle()] = download; |
| 1341 | |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1342 | // Show in the appropropriate browser UI. |
| 1343 | ShowDownloadInBrowser(info, download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1344 | |
| 1345 | // Inform interested objects about the new download. |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1346 | NotifyModelChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1347 | |
| 1348 | // If this download has been completed before we've received the db handle, |
| 1349 | // post one final message to the history service so that it can be properly |
| 1350 | // in sync with the DownloadItem's completion status, and also inform any |
| 1351 | // observers so that they get more than just the start notification. |
| 1352 | if (download->state() != DownloadItem::IN_PROGRESS) { |
| 1353 | in_progress_.erase(it); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame^] | 1354 | download_history_->UpdateEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1355 | download->UpdateObservers(); |
| 1356 | } |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 1357 | |
| 1358 | UpdateAppIcon(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1359 | } |
| 1360 | |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1361 | void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info, |
| 1362 | DownloadItem* download) { |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 1363 | // The 'contents' may no longer exist if the user closed the tab before we |
| 1364 | // get this start completion event. If it does, tell the origin TabContents |
| 1365 | // to display its download shelf. |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 1366 | TabContents* contents = tab_util::GetTabContentsByID(info.child_id, |
| 1367 | info.render_view_id); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1368 | |
| 1369 | // If the contents no longer exists, we start the download in the last active |
| 1370 | // browser. This is not ideal but better than fully hiding the download from |
| 1371 | // the user. |
| 1372 | if (!contents) { |
| 1373 | Browser* last_active = BrowserList::GetLastActive(); |
| 1374 | if (last_active) |
| 1375 | contents = last_active->GetSelectedTabContents(); |
| 1376 | } |
| 1377 | |
| 1378 | if (contents) |
| 1379 | contents->OnStartDownload(download); |
| 1380 | } |
| 1381 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 1382 | // Clears the last download path, used to initialize "save as" dialogs. |
[email protected] | 905a08d | 2008-11-19 07:24:12 | [diff] [blame] | 1383 | void DownloadManager::ClearLastDownloadPath() { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1384 | last_download_path_ = FilePath(); |
[email protected] | eea4662 | 2009-07-15 20:49:38 | [diff] [blame] | 1385 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1386 | |
| 1387 | void DownloadManager::NotifyModelChanged() { |
| 1388 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
| 1389 | } |
| 1390 | |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 1391 | DownloadItem* DownloadManager::GetDownloadItem(int id) { |
| 1392 | for (DownloadMap::iterator it = downloads_.begin(); |
| 1393 | it != downloads_.end(); ++it) { |
| 1394 | DownloadItem* item = it->second; |
| 1395 | if (item->id() == id) |
| 1396 | return item; |
| 1397 | } |
| 1398 | return NULL; |
| 1399 | } |
| 1400 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1401 | // DownloadManager::OtherDownloadManagerObserver implementation ---------------- |
| 1402 | |
| 1403 | DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver( |
| 1404 | DownloadManager* observing_download_manager) |
| 1405 | : observing_download_manager_(observing_download_manager), |
| 1406 | observed_download_manager_(NULL) { |
| 1407 | if (observing_download_manager->profile_->GetOriginalProfile() == |
| 1408 | observing_download_manager->profile_) { |
| 1409 | return; |
| 1410 | } |
| 1411 | |
| 1412 | observed_download_manager_ = observing_download_manager_-> |
| 1413 | profile_->GetOriginalProfile()->GetDownloadManager(); |
| 1414 | observed_download_manager_->AddObserver(this); |
| 1415 | } |
| 1416 | |
| 1417 | DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() { |
| 1418 | if (observed_download_manager_) |
| 1419 | observed_download_manager_->RemoveObserver(this); |
| 1420 | } |
| 1421 | |
| 1422 | void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
| 1423 | observing_download_manager_->NotifyModelChanged(); |
| 1424 | } |
| 1425 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1426 | void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
| 1427 | observed_download_manager_ = NULL; |
| 1428 | } |