[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] | ce7f62e3 | 2010-08-10 23:43:59 | [diff] [blame^] | 16 | #include "base/utf_string_conversions.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 17 | #include "build/build_config.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 18 | #include "chrome/browser/browser.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "chrome/browser/browser_list.h" |
| 20 | #include "chrome/browser/browser_process.h" |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 21 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 22 | #include "chrome/browser/download/download_file_manager.h" |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 23 | #include "chrome/browser/download/download_history.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 24 | #include "chrome/browser/download/download_item.h" |
[email protected] | e9ef0a6 | 2009-08-11 22:50:13 | [diff] [blame] | 25 | #include "chrome/browser/download/download_util.h" |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 26 | #include "chrome/browser/extensions/extensions_service.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 27 | #include "chrome/browser/history/download_types.h" |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 28 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 29 | #include "chrome/browser/platform_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | #include "chrome/browser/profile.h" |
[email protected] | 8c8657d6 | 2009-01-16 18:31:26 | [diff] [blame] | 31 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 6524b5f9 | 2009-01-22 17:48:25 | [diff] [blame] | 32 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 33 | #include "chrome/browser/tab_contents/infobar_delegate.h" |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 34 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 35 | #include "chrome/browser/tab_contents/tab_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | #include "chrome/common/chrome_paths.h" |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 37 | #include "chrome/common/notification_service.h" |
| 38 | #include "chrome/common/notification_type.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | #include "chrome/common/pref_names.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 40 | #include "googleurl/src/gurl.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 41 | #include "grit/generated_resources.h" |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 42 | #include "grit/theme_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | #include "net/base/mime_util.h" |
| 44 | #include "net/base/net_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 46 | #if defined(OS_WIN) |
[email protected] | 4a0765a | 2009-05-08 23:12:25 | [diff] [blame] | 47 | #include "app/win_util.h" |
[email protected] | a0a9577b | 2009-05-27 23:52:32 | [diff] [blame] | 48 | #endif |
| 49 | |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 50 | namespace { |
| 51 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 52 | // Used to sort download items based on descending start time. |
| 53 | bool CompareStartTime(DownloadItem* first, DownloadItem* second) { |
| 54 | return first->start_time() > second->start_time(); |
| 55 | } |
| 56 | |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 57 | } // namespace |
| 58 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | // static |
| 60 | void DownloadManager::RegisterUserPrefs(PrefService* prefs) { |
| 61 | prefs->RegisterBooleanPref(prefs::kPromptForDownload, false); |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 62 | prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen, ""); |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 63 | prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false); |
| 64 | |
| 65 | // The default download path is userprofile\download. |
[email protected] | 290c970 | 2010-03-09 04:01:36 | [diff] [blame] | 66 | const FilePath& default_download_path = |
| 67 | download_util::GetDefaultDownloadDirectory(); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 68 | prefs->RegisterFilePathPref(prefs::kDownloadDefaultDirectory, |
| 69 | default_download_path); |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 70 | #if defined(OS_CHROMEOS) |
| 71 | // Ensure that the download directory specified in the preferences exists. |
| 72 | ChromeThread::PostTask( |
| 73 | ChromeThread::FILE, FROM_HERE, |
| 74 | NewRunnableFunction(&file_util::CreateDirectory, default_download_path)); |
| 75 | #endif |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 76 | |
| 77 | // If the download path is dangerous we forcefully reset it. But if we do |
| 78 | // so we set a flag to make sure we only do it once, to avoid fighting |
| 79 | // the user if he really wants it on an unsafe place such as the desktop. |
| 80 | |
| 81 | if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { |
[email protected] | 38233e6 | 2010-04-26 04:43:36 | [diff] [blame] | 82 | FilePath current_download_dir = prefs->GetFilePath( |
| 83 | prefs::kDownloadDefaultDirectory); |
[email protected] | 290c970 | 2010-03-09 04:01:36 | [diff] [blame] | 84 | if (download_util::DownloadPathIsDangerous(current_download_dir)) { |
[email protected] | 38233e6 | 2010-04-26 04:43:36 | [diff] [blame] | 85 | prefs->SetFilePath(prefs::kDownloadDefaultDirectory, |
| 86 | default_download_path); |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 87 | } |
| 88 | prefs->SetBoolean(prefs::kDownloadDirUpgraded, true); |
| 89 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | DownloadManager::DownloadManager() |
| 93 | : shutdown_needed_(false), |
| 94 | profile_(NULL), |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 95 | file_manager_(NULL) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | DownloadManager::~DownloadManager() { |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 99 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); |
| 100 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | if (shutdown_needed_) |
| 102 | Shutdown(); |
| 103 | } |
| 104 | |
| 105 | void DownloadManager::Shutdown() { |
| 106 | DCHECK(shutdown_needed_) << "Shutdown called when not needed."; |
| 107 | |
| 108 | // Stop receiving download updates |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 109 | if (file_manager_) |
| 110 | file_manager_->RemoveDownloadManager(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | // 'in_progress_' may contain DownloadItems that have not finished the start |
| 113 | // complete (from the history service) and thus aren't in downloads_. |
| 114 | DownloadMap::iterator it = in_progress_.begin(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 115 | std::set<DownloadItem*> to_remove; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | for (; it != in_progress_.end(); ++it) { |
| 117 | DownloadItem* download = it->second; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 118 | if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 119 | // Forget about any download that the user did not approve. |
| 120 | // Note that we cannot call download->Remove() this would invalidate our |
| 121 | // iterator. |
| 122 | to_remove.insert(download); |
| 123 | continue; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 125 | DCHECK_EQ(DownloadItem::IN_PROGRESS, download->state()); |
| 126 | download->Cancel(false); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 127 | download_history_->UpdateEntry(download); |
| 128 | if (download->db_handle() == DownloadHistory::kUninitializedHandle) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | // An invalid handle means that 'download' does not yet exist in |
| 130 | // 'downloads_', so we have to delete it here. |
| 131 | delete download; |
| 132 | } |
| 133 | } |
| 134 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 135 | // 'dangerous_finished_' contains all complete downloads that have not been |
| 136 | // approved. They should be removed. |
| 137 | it = dangerous_finished_.begin(); |
| 138 | for (; it != dangerous_finished_.end(); ++it) |
| 139 | to_remove.insert(it->second); |
| 140 | |
| 141 | // Remove the dangerous download that are not approved. |
| 142 | for (std::set<DownloadItem*>::const_iterator rm_it = to_remove.begin(); |
| 143 | rm_it != to_remove.end(); ++rm_it) { |
| 144 | DownloadItem* download = *rm_it; |
[email protected] | e10e17c7 | 2008-10-15 17:48:32 | [diff] [blame] | 145 | int64 handle = download->db_handle(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 146 | download->Remove(true); |
[email protected] | e10e17c7 | 2008-10-15 17:48:32 | [diff] [blame] | 147 | // Same as above, delete the download if it is not in 'downloads_' (as the |
| 148 | // Remove() call above won't have deleted it). |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 149 | if (handle == DownloadHistory::kUninitializedHandle) |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 150 | delete download; |
| 151 | } |
| 152 | to_remove.clear(); |
| 153 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | in_progress_.clear(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 155 | dangerous_finished_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | STLDeleteValues(&downloads_); |
| 157 | |
| 158 | file_manager_ = NULL; |
| 159 | |
| 160 | // Save our file extensions to auto open. |
| 161 | SaveAutoOpens(); |
| 162 | |
| 163 | // Make sure the save as dialog doesn't notify us back if we're gone before |
| 164 | // it returns. |
| 165 | if (select_file_dialog_.get()) |
| 166 | select_file_dialog_->ListenerDestroyed(); |
| 167 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 168 | download_history_.reset(); |
| 169 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | shutdown_needed_ = false; |
| 171 | } |
| 172 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 173 | void DownloadManager::GetTemporaryDownloads( |
| 174 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 175 | DCHECK(result); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 176 | |
| 177 | for (DownloadMap::iterator it = downloads_.begin(); |
| 178 | it != downloads_.end(); ++it) { |
| 179 | if (it->second->is_temporary() && |
| 180 | it->second->full_path().DirName() == dir_path) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 181 | result->push_back(it->second); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 182 | } |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 185 | void DownloadManager::GetAllDownloads( |
| 186 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 187 | DCHECK(result); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 188 | |
| 189 | for (DownloadMap::iterator it = downloads_.begin(); |
| 190 | it != downloads_.end(); ++it) { |
| 191 | if (!it->second->is_temporary() && |
| 192 | (dir_path.empty() || it->second->full_path().DirName() == dir_path)) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 193 | result->push_back(it->second); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 194 | } |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 197 | void DownloadManager::GetCurrentDownloads( |
| 198 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 199 | DCHECK(result); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 200 | |
| 201 | for (DownloadMap::iterator it = downloads_.begin(); |
| 202 | it != downloads_.end(); ++it) { |
| 203 | if (!it->second->is_temporary() && |
| 204 | (it->second->state() == DownloadItem::IN_PROGRESS || |
| 205 | it->second->safety_state() == DownloadItem::DANGEROUS) && |
| 206 | (dir_path.empty() || it->second->full_path().DirName() == dir_path)) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 207 | result->push_back(it->second); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 208 | } |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 209 | } |
| 210 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | // Query the history service for information about all persisted downloads. |
| 212 | bool DownloadManager::Init(Profile* profile) { |
| 213 | DCHECK(profile); |
| 214 | DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 215 | shutdown_needed_ = true; |
| 216 | |
| 217 | profile_ = profile; |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 218 | request_context_getter_ = profile_->GetRequestContext(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 219 | download_history_.reset(new DownloadHistory(profile, this)); |
| 220 | download_history_->Load( |
| 221 | NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 222 | |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 223 | // In test mode, there may be no ResourceDispatcherHost. In this case it's |
| 224 | // safe to avoid setting |file_manager_| because we only call a small set of |
| 225 | // functions, none of which need it. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 226 | ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 227 | if (rdh) { |
| 228 | file_manager_ = rdh->download_file_manager(); |
| 229 | DCHECK(file_manager_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | } |
| 231 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | // Get our user preference state. |
| 233 | PrefService* prefs = profile_->GetPrefs(); |
| 234 | DCHECK(prefs); |
| 235 | prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL); |
| 236 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL); |
[email protected] | bb69e9b3 | 2008-08-14 23:08:14 | [diff] [blame] | 238 | // Ensure that the download directory specified in the preferences exists. |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 239 | ChromeThread::PostTask( |
| 240 | ChromeThread::FILE, FROM_HERE, |
[email protected] | 309b764 | 2009-12-09 03:08:50 | [diff] [blame] | 241 | NewRunnableFunction(&file_util::CreateDirectory, download_path())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | |
[email protected] | 2b2f8f7 | 2009-02-24 22:42:05 | [diff] [blame] | 243 | // We store any file extension that should be opened automatically at |
| 244 | // download completion in this pref. |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 245 | std::string extensions_to_open = |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 246 | prefs->GetString(prefs::kDownloadExtensionsToOpen); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 247 | std::vector<std::string> extensions; |
| 248 | SplitString(extensions_to_open, ':', &extensions); |
| 249 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | for (size_t i = 0; i < extensions.size(); ++i) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 251 | #if defined(OS_POSIX) |
| 252 | FilePath path(extensions[i]); |
| 253 | #elif defined(OS_WIN) |
| 254 | FilePath path(UTF8ToWide(extensions[i])); |
| 255 | #endif |
| 256 | if (!extensions[i].empty() && !IsExecutableFile(path)) |
| 257 | auto_open_.insert(path.value()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 260 | other_download_manager_observer_.reset( |
| 261 | new OtherDownloadManagerObserver(this)); |
| 262 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 266 | // We have received a message from DownloadFileManager about a new download. We |
| 267 | // create a download item and store it in our download map, and inform the |
| 268 | // history system of a new download. Since this method can be called while the |
| 269 | // history service thread is still reading the persistent state, we do not |
| 270 | // insert the new DownloadItem into 'downloads_' or inform our observers at this |
| 271 | // point. OnCreateDatabaseEntryComplete() handles that finalization of the the |
| 272 | // download creation as a callback from the history thread. |
| 273 | void DownloadManager::StartDownload(DownloadCreateInfo* info) { |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 274 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | DCHECK(info); |
| 276 | |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 277 | // Check whether this download is for an extension install or not. |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 278 | // Allow extensions to be explicitly saved. |
| 279 | if (!info->prompt_user_for_save_location) { |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 280 | if (UserScript::HasUserScriptFileExtension(info->url) || |
| 281 | info->mime_type == Extension::kMimeType) |
| 282 | info->is_extension_install = true; |
| 283 | } |
| 284 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 285 | if (info->save_info.file_path.empty()) { |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 286 | FilePath generated_name; |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 287 | download_util::GenerateFileNameFromInfo(info, &generated_name); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 288 | |
| 289 | // Freeze the user's preference for showing a Save As dialog. We're going |
| 290 | // to bounce around a bunch of threads and we don't want to worry about race |
| 291 | // conditions where the user changes this pref out from under us. |
| 292 | if (*prompt_for_download_) { |
| 293 | // But ignore the user's preference for the following scenarios: |
| 294 | // 1) Extension installation. Note that we only care here about the case |
| 295 | // where an extension is installed, not when one is downloaded with |
| 296 | // "save as...". |
| 297 | // 2) Filetypes marked "always open." If the user just wants this file |
| 298 | // opened, don't bother asking where to keep it. |
| 299 | if (!info->is_extension_install && |
| 300 | !ShouldOpenFileBasedOnExtension(generated_name)) |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 301 | info->prompt_user_for_save_location = true; |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 304 | // Determine the proper path for a download, by either one of the following: |
| 305 | // 1) using the default download directory. |
| 306 | // 2) prompting the user. |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 307 | if (info->prompt_user_for_save_location && !last_download_path_.empty()){ |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 308 | info->suggested_path = last_download_path_; |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 309 | } else { |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 310 | info->suggested_path = download_path(); |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 311 | } |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 312 | info->suggested_path = info->suggested_path.Append(generated_name); |
| 313 | } else { |
| 314 | info->suggested_path = info->save_info.file_path; |
| 315 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 316 | |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 317 | if (!info->prompt_user_for_save_location && |
| 318 | info->save_info.file_path.empty()) { |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 319 | // Downloads can be marked as dangerous for two reasons: |
| 320 | // a) They have a dangerous-looking filename |
| 321 | // b) They are an extension that is not from the gallery |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 322 | if (IsExecutableFile(info->suggested_path.BaseName())) |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 323 | info->is_dangerous = true; |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 324 | else if (info->is_extension_install && |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 325 | !ExtensionsService::IsDownloadFromGallery(info->url, |
| 326 | info->referrer_url)) { |
[email protected] | 4289d9b | 2009-07-25 21:17:34 | [diff] [blame] | 327 | info->is_dangerous = true; |
| 328 | } |
[email protected] | e9ebf3fc | 2008-10-17 22:06:58 | [diff] [blame] | 329 | } |
| 330 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 331 | // We need to move over to the download thread because we don't want to stat |
| 332 | // the suggested path on the UI thread. |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 333 | ChromeThread::PostTask( |
| 334 | ChromeThread::FILE, FROM_HERE, |
| 335 | NewRunnableMethod( |
| 336 | this, &DownloadManager::CheckIfSuggestedPathExists, info)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 340 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | DCHECK(info); |
| 342 | |
| 343 | // Check writability of the suggested path. If we can't write to it, default |
| 344 | // 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] | 345 | FilePath dir = info->suggested_path.DirName(); |
| 346 | FilePath filename = info->suggested_path.BaseName(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 347 | if (!file_util::PathIsWritable(dir)) { |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 348 | info->prompt_user_for_save_location = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 349 | PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path); |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 350 | info->suggested_path = info->suggested_path.Append(filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 351 | } |
| 352 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 353 | // 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] | 354 | if (info->is_dangerous) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 355 | info->original_name = FilePath(info->suggested_path).BaseName(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 356 | // Create a temporary file to hold the file until the user approves its |
| 357 | // download. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 358 | FilePath::StringType file_name; |
| 359 | FilePath path; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 360 | while (path.empty()) { |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 361 | SStringPrintf(&file_name, FILE_PATH_LITERAL("unconfirmed %d.crdownload"), |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 362 | base::RandInt(0, 100000)); |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 363 | path = dir.Append(file_name); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 364 | if (file_util::PathExists(path)) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 365 | path = FilePath(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 366 | } |
| 367 | info->suggested_path = path; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 368 | } else { |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 369 | // Do not add the path uniquifier if we are saving to a specific path as in |
| 370 | // the drag-out case. |
| 371 | if (info->save_info.file_path.empty()) { |
| 372 | info->path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( |
| 373 | info->suggested_path); |
| 374 | } |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 375 | // We know the final path, build it if necessary. |
| 376 | if (info->path_uniquifier > 0) { |
[email protected] | 5a2388a | 2010-03-26 16:13:39 | [diff] [blame] | 377 | download_util::AppendNumberToPath(&(info->suggested_path), |
| 378 | info->path_uniquifier); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 379 | // Setting path_uniquifier to 0 to make sure we don't try to unique it |
| 380 | // later on. |
| 381 | info->path_uniquifier = 0; |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 382 | } else if (info->path_uniquifier == -1) { |
| 383 | // We failed to find a unique path. We have to prompt the user. |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 384 | info->prompt_user_for_save_location = true; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 385 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 388 | // Create an empty file at the suggested path so that we don't allocate the |
| 389 | // same "non-existant" path to multiple downloads. |
| 390 | // See: http://code.google.com/p/chromium/issues/detail?id=3662 |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 391 | if (!info->prompt_user_for_save_location && |
| 392 | info->save_info.file_path.empty()) { |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 393 | if (info->is_dangerous) |
| 394 | file_util::WriteFile(info->suggested_path, "", 0); |
| 395 | else |
| 396 | file_util::WriteFile(download_util::GetCrDownloadPath( |
| 397 | info->suggested_path), "", 0); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 398 | } |
| 399 | |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 400 | ChromeThread::PostTask( |
| 401 | ChromeThread::UI, FROM_HERE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | NewRunnableMethod(this, |
| 403 | &DownloadManager::OnPathExistenceAvailable, |
| 404 | info)); |
| 405 | } |
| 406 | |
| 407 | void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) { |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 408 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | DCHECK(info); |
| 410 | |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 411 | if (info->prompt_user_for_save_location) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 412 | // We must ask the user for the place to put the download. |
| 413 | if (!select_file_dialog_.get()) |
| 414 | select_file_dialog_ = SelectFileDialog::Create(this); |
| 415 | |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 416 | TabContents* contents = tab_util::GetTabContentsByID(info->child_id, |
| 417 | info->render_view_id); |
[email protected] | b949f111 | 2009-04-12 20:03:08 | [diff] [blame] | 418 | SelectFileDialog::FileTypeInfo file_type_info; |
| 419 | file_type_info.extensions.resize(1); |
| 420 | file_type_info.extensions[0].push_back(info->suggested_path.Extension()); |
[email protected] | 15bc805 | 2009-04-17 19:57:24 | [diff] [blame] | 421 | if (!file_type_info.extensions[0][0].empty()) |
| 422 | file_type_info.extensions[0][0].erase(0, 1); // drop the . |
[email protected] | b949f111 | 2009-04-12 20:03:08 | [diff] [blame] | 423 | file_type_info.include_all_files = true; |
[email protected] | 076700e6 | 2009-04-01 18:41:23 | [diff] [blame] | 424 | gfx::NativeWindow owning_window = |
| 425 | contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 427 | string16(), |
| 428 | info->suggested_path, |
[email protected] | b949f111 | 2009-04-12 20:03:08 | [diff] [blame] | 429 | &file_type_info, 0, FILE_PATH_LITERAL(""), |
[email protected] | 0f44d3e | 2009-03-12 23:36:30 | [diff] [blame] | 430 | owning_window, info); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 431 | } else { |
| 432 | // No prompting for download, just continue with the suggested name. |
| 433 | ContinueStartDownload(info, info->suggested_path); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void DownloadManager::ContinueStartDownload(DownloadCreateInfo* info, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 438 | const FilePath& target_path) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 439 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 440 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | scoped_ptr<DownloadCreateInfo> infop(info); |
| 442 | info->path = target_path; |
| 443 | |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 444 | DownloadItem* download = new DownloadItem(this, *info, |
| 445 | profile_->IsOffTheRecord()); |
| 446 | DCHECK(!ContainsKey(in_progress_, info->download_id)); |
| 447 | in_progress_[info->download_id] = download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 448 | |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 449 | bool download_finished = ContainsKey(pending_finished_downloads_, |
| 450 | info->download_id); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 451 | |
| 452 | if (download_finished || info->is_dangerous) { |
| 453 | // The download has already finished or the download is not safe. |
| 454 | // We can now rename the file to its final name (or its tentative name |
| 455 | // in dangerous download cases). |
| 456 | ChromeThread::PostTask( |
| 457 | ChromeThread::FILE, FROM_HERE, |
| 458 | NewRunnableMethod( |
| 459 | file_manager_, &DownloadFileManager::OnFinalDownloadName, |
| 460 | download->id(), target_path, !info->is_dangerous, this)); |
| 461 | } else { |
| 462 | // The download hasn't finished and it is a safe download. We need to |
| 463 | // rename it to its intermediate '.crdownload' path. |
| 464 | FilePath download_path = download_util::GetCrDownloadPath(target_path); |
| 465 | ChromeThread::PostTask( |
| 466 | ChromeThread::FILE, FROM_HERE, |
| 467 | NewRunnableMethod( |
| 468 | file_manager_, &DownloadFileManager::OnIntermediateDownloadName, |
| 469 | download->id(), download_path, this)); |
| 470 | download->set_need_final_rename(true); |
| 471 | } |
| 472 | |
| 473 | if (download_finished) { |
| 474 | // If the download already completed by the time we reached this point, then |
| 475 | // notify observers that it did. |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 476 | DownloadFinished(info->download_id, |
| 477 | pending_finished_downloads_[info->download_id]); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 478 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | |
| 480 | download->Rename(target_path); |
| 481 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 482 | download_history_->AddEntry(*info, download, |
| 483 | NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 484 | |
| 485 | UpdateAppIcon(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 486 | } |
| 487 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 488 | void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
| 489 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 490 | if (it != in_progress_.end()) { |
| 491 | DownloadItem* download = it->second; |
| 492 | download->Update(size); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 493 | download_history_->UpdateEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | } |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 495 | UpdateAppIcon(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void DownloadManager::DownloadFinished(int32 download_id, int64 size) { |
| 499 | DownloadMap::iterator it = in_progress_.find(download_id); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 500 | if (it == in_progress_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 501 | // The download is done, but the user hasn't selected a final location for |
| 502 | // it yet (the Save As dialog box is probably still showing), so just keep |
| 503 | // track of the fact that this download id is complete, when the |
| 504 | // DownloadItem is constructed later we'll notify its completion then. |
| 505 | PendingFinishedMap::iterator erase_it = |
| 506 | pending_finished_downloads_.find(download_id); |
| 507 | DCHECK(erase_it == pending_finished_downloads_.end()); |
| 508 | pending_finished_downloads_[download_id] = size; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 509 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 511 | |
| 512 | // Remove the id from the list of pending ids. |
| 513 | PendingFinishedMap::iterator erase_it = |
| 514 | pending_finished_downloads_.find(download_id); |
| 515 | if (erase_it != pending_finished_downloads_.end()) |
| 516 | pending_finished_downloads_.erase(erase_it); |
| 517 | |
| 518 | DownloadItem* download = it->second; |
| 519 | download->Finished(size); |
| 520 | |
| 521 | // Clean up will happen when the history system create callback runs if we |
| 522 | // don't have a valid db_handle yet. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 523 | if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 524 | in_progress_.erase(it); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 525 | download_history_->UpdateEntry(download); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 526 | } |
| 527 | |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 528 | UpdateAppIcon(); |
| 529 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 530 | // If this a dangerous download not yet validated by the user, don't do |
| 531 | // anything. When the user notifies us, it will trigger a call to |
| 532 | // ProceedWithFinishedDangerousDownload. |
| 533 | if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 534 | dangerous_finished_[download_id] = download; |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | if (download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) { |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 539 | // We first need to rename the downloaded file from its temporary name to |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 540 | // its final name before we can continue. |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 541 | ChromeThread::PostTask( |
| 542 | ChromeThread::FILE, FROM_HERE, |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 543 | NewRunnableMethod( |
| 544 | this, &DownloadManager::ProceedWithFinishedDangerousDownload, |
| 545 | download->db_handle(), |
| 546 | download->full_path(), download->original_name())); |
| 547 | return; |
| 548 | } |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 549 | |
| 550 | if (download->need_final_rename()) { |
| 551 | ChromeThread::PostTask( |
| 552 | ChromeThread::FILE, FROM_HERE, |
| 553 | NewRunnableMethod( |
| 554 | file_manager_, &DownloadFileManager::OnFinalDownloadName, |
| 555 | download->id(), download->full_path(), false, this)); |
| 556 | return; |
| 557 | } |
| 558 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 559 | ContinueDownloadFinished(download); |
| 560 | } |
| 561 | |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 562 | void DownloadManager::DownloadRenamedToFinalName(int download_id, |
| 563 | const FilePath& full_path) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 564 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 565 | |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 566 | DownloadItem* item = GetDownloadItem(download_id); |
| 567 | if (!item) |
| 568 | return; |
| 569 | item->OnNameFinalized(); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 570 | |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 571 | // This was called from DownloadFinished; continue to call |
| 572 | // ContinueDownloadFinished. |
| 573 | if (item->need_final_rename()) { |
| 574 | item->set_need_final_rename(false); |
| 575 | ContinueDownloadFinished(item); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 576 | } |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 577 | } |
| 578 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 579 | void DownloadManager::ContinueDownloadFinished(DownloadItem* download) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 580 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 581 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 582 | // If this was a dangerous download, it has now been approved and must be |
| 583 | // removed from dangerous_finished_ so it does not get deleted on shutdown. |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 584 | dangerous_finished_.erase(download->id()); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 585 | |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 586 | // Handle chrome extensions explicitly and skip the shell execute. |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 587 | if (download->is_extension_install()) { |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 588 | download_util::OpenChromeExtension(profile_, this, *download); |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 589 | download->set_auto_opened(true); |
| 590 | } else if (download->open_when_complete() || |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 591 | ShouldOpenFileBasedOnExtension(download->full_path()) || |
| 592 | download->is_temporary()) { |
| 593 | // If the download is temporary, like in drag-and-drop, do not open it but |
| 594 | // we still need to set it auto-opened so that it can be removed from the |
| 595 | // download shelf. |
| 596 | if (!download->is_temporary()) |
| 597 | OpenDownloadInShell(download, NULL); |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 598 | download->set_auto_opened(true); |
| 599 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 600 | |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 601 | // Notify our observers that we are complete (the call to Finished() set the |
| 602 | // state to complete but did not notify). |
| 603 | download->UpdateObservers(); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 604 | |
| 605 | // The download file is meant to be completed if both the filename is |
| 606 | // finalized and the file data is downloaded. The ordering of these two |
| 607 | // actions is indeterministic. Thus, if the filename is not finalized yet, |
| 608 | // delay the notification. |
| 609 | if (download->name_finalized()) |
| 610 | download->NotifyObserversDownloadFileCompleted(); |
[email protected] | 0aad67b | 2009-07-15 20:34:28 | [diff] [blame] | 611 | } |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 612 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 613 | // Called on the file thread. Renames the downloaded file to its original name. |
| 614 | void DownloadManager::ProceedWithFinishedDangerousDownload( |
| 615 | int64 download_handle, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 616 | const FilePath& path, |
| 617 | const FilePath& original_name) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 618 | bool success = false; |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 619 | FilePath new_path; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 620 | int uniquifier = 0; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 621 | if (file_util::PathExists(path)) { |
[email protected] | 889ed35c | 2009-01-21 00:07:24 | [diff] [blame] | 622 | new_path = path.DirName().Append(original_name); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 623 | // Make our name unique at this point, as if a dangerous file is downloading |
| 624 | // and a 2nd download is started for a file with the same name, they would |
| 625 | // have the same path. This is because we uniquify the name on download |
| 626 | // start, and at that time the first file does not exists yet, so the second |
| 627 | // file gets the same name. |
[email protected] | 5a2388a | 2010-03-26 16:13:39 | [diff] [blame] | 628 | uniquifier = download_util::GetUniquePathNumber(new_path); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 629 | if (uniquifier > 0) |
[email protected] | 5a2388a | 2010-03-26 16:13:39 | [diff] [blame] | 630 | download_util::AppendNumberToPath(&new_path, uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 631 | success = file_util::Move(path, new_path); |
| 632 | } else { |
| 633 | NOTREACHED(); |
| 634 | } |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 635 | |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 636 | ChromeThread::PostTask( |
| 637 | ChromeThread::UI, FROM_HERE, |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 638 | NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 639 | download_handle, success, new_path, uniquifier)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | // Call from the file thread when the finished dangerous download was renamed. |
| 643 | void DownloadManager::DangerousDownloadRenamed(int64 download_handle, |
| 644 | bool success, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 645 | const FilePath& new_path, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 646 | int new_path_uniquifier) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 647 | DownloadMap::iterator it = downloads_.find(download_handle); |
| 648 | if (it == downloads_.end()) { |
| 649 | NOTREACHED(); |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | DownloadItem* download = it->second; |
| 654 | // 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] | 655 | if (success) { |
| 656 | // We need to update the path uniquifier so that the UI shows the right |
| 657 | // name when calling GetFileName(). |
| 658 | download->set_path_uniquifier(new_path_uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 659 | RenameDownload(download, new_path); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 660 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 661 | |
| 662 | // Continue the download finished sequence. |
| 663 | ContinueDownloadFinished(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 664 | } |
| 665 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 666 | void DownloadManager::DownloadCancelled(int32 download_id) { |
| 667 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 668 | if (it == in_progress_.end()) |
| 669 | return; |
| 670 | DownloadItem* download = it->second; |
| 671 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 672 | // Clean up will happen when the history system create callback runs if we |
| 673 | // don't have a valid db_handle yet. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 674 | if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 675 | in_progress_.erase(it); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 676 | download_history_->UpdateEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 677 | } |
| 678 | |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 679 | DownloadCancelledInternal(download_id, |
| 680 | download->render_process_id(), |
| 681 | download->request_id()); |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 682 | UpdateAppIcon(); |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | void DownloadManager::DownloadCancelledInternal(int download_id, |
| 686 | int render_process_id, |
| 687 | int request_id) { |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 688 | // Cancel the network request. RDH is guaranteed to outlive the IO thread. |
| 689 | ChromeThread::PostTask( |
| 690 | ChromeThread::IO, FROM_HERE, |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 691 | NewRunnableFunction(&download_util::CancelDownloadRequest, |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 692 | g_browser_process->resource_dispatcher_host(), |
| 693 | render_process_id, |
| 694 | request_id)); |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 695 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 696 | // Tell the file manager to cancel the download. |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 697 | file_manager_->RemoveDownload(download_id, this); // On the UI thread |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 698 | ChromeThread::PostTask( |
| 699 | ChromeThread::FILE, FROM_HERE, |
| 700 | NewRunnableMethod( |
| 701 | file_manager_, &DownloadFileManager::CancelDownload, download_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 702 | } |
| 703 | |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 704 | // DownloadManager is owned by RDH, no need for reference counting. |
| 705 | DISABLE_RUNNABLE_METHOD_REFCOUNT(ResourceDispatcherHost); |
| 706 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 707 | void DownloadManager::PauseDownload(int32 download_id, bool pause) { |
| 708 | DownloadMap::iterator it = in_progress_.find(download_id); |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 709 | if (it == in_progress_.end()) |
| 710 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 711 | |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 712 | DownloadItem* download = it->second; |
| 713 | if (pause == download->is_paused()) |
| 714 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 715 | |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 716 | // Inform the ResourceDispatcherHost of the new pause state. |
| 717 | ChromeThread::PostTask( |
| 718 | ChromeThread::IO, FROM_HERE, |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 719 | NewRunnableMethod(g_browser_process->resource_dispatcher_host(), |
| 720 | &ResourceDispatcherHost::PauseRequest, |
| 721 | download->render_process_id(), |
| 722 | download->request_id(), |
| 723 | pause)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 724 | } |
| 725 | |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 726 | void DownloadManager::UpdateAppIcon() { |
| 727 | int64 total_bytes = 0; |
| 728 | int64 received_bytes = 0; |
| 729 | int download_count = 0; |
| 730 | bool progress_known = true; |
| 731 | |
| 732 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 733 | i != in_progress_.end(); |
| 734 | ++i) { |
| 735 | ++download_count; |
| 736 | const DownloadItem* item = i->second; |
| 737 | if (item->total_bytes() > 0) { |
| 738 | total_bytes += item->total_bytes(); |
| 739 | received_bytes += item->received_bytes(); |
| 740 | } else { |
| 741 | // This download didn't specify a Content-Length, so the combined progress |
| 742 | // bar neeeds to be indeterminate. |
| 743 | progress_known = false; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | float progress = 0; |
| 748 | if (progress_known && download_count) |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 749 | progress = static_cast<float>(received_bytes) / total_bytes; |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 750 | |
| 751 | download_util::UpdateAppIconDownloadProgress(download_count, |
| 752 | progress_known, |
| 753 | progress); |
| 754 | } |
| 755 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 756 | void DownloadManager::RenameDownload(DownloadItem* download, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 757 | const FilePath& new_path) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 758 | download->Rename(new_path); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 759 | download_history_->UpdateDownloadPath(download, new_path); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 760 | } |
| 761 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 762 | void DownloadManager::RemoveDownload(int64 download_handle) { |
| 763 | DownloadMap::iterator it = downloads_.find(download_handle); |
| 764 | if (it == downloads_.end()) |
| 765 | return; |
| 766 | |
| 767 | // Make history update. |
| 768 | DownloadItem* download = it->second; |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 769 | download_history_->RemoveEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 770 | |
| 771 | // Remove from our tables and delete. |
| 772 | downloads_.erase(it); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 773 | it = dangerous_finished_.find(download->id()); |
| 774 | if (it != dangerous_finished_.end()) |
| 775 | dangerous_finished_.erase(it); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 776 | |
| 777 | // Tell observers to refresh their views. |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 778 | NotifyModelChanged(); |
[email protected] | 6f71287 | 2008-11-07 00:35:36 | [diff] [blame] | 779 | |
| 780 | delete download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 781 | } |
| 782 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 783 | int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin, |
| 784 | const base::Time remove_end) { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 785 | download_history_->RemoveEntriesBetween(remove_begin, remove_end); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 786 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 787 | DownloadMap::iterator it = downloads_.begin(); |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 788 | std::vector<DownloadItem*> pending_deletes; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 789 | while (it != downloads_.end()) { |
| 790 | DownloadItem* download = it->second; |
| 791 | DownloadItem::DownloadState state = download->state(); |
| 792 | if (download->start_time() >= remove_begin && |
| 793 | (remove_end.is_null() || download->start_time() < remove_end) && |
| 794 | (state == DownloadItem::COMPLETE || |
| 795 | state == DownloadItem::CANCELLED)) { |
| 796 | // Remove from the map and move to the next in the list. |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 797 | downloads_.erase(it++); |
[email protected] | a6604d9 | 2008-10-30 00:58:58 | [diff] [blame] | 798 | |
| 799 | // Also remove it from any completed dangerous downloads. |
| 800 | DownloadMap::iterator dit = dangerous_finished_.find(download->id()); |
| 801 | if (dit != dangerous_finished_.end()) |
| 802 | dangerous_finished_.erase(dit); |
| 803 | |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 804 | pending_deletes.push_back(download); |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 805 | // Observer interface. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 806 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 807 | continue; |
| 808 | } |
| 809 | |
| 810 | ++it; |
| 811 | } |
| 812 | |
| 813 | // Tell observers to refresh their views. |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 814 | int num_deleted = static_cast<int>(pending_deletes.size()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 815 | if (num_deleted > 0) |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 816 | NotifyModelChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 817 | |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 818 | // Delete the download items after updating the observers. |
| 819 | STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end()); |
| 820 | pending_deletes.clear(); |
| 821 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 822 | return num_deleted; |
| 823 | } |
| 824 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 825 | int DownloadManager::RemoveDownloads(const base::Time remove_begin) { |
| 826 | return RemoveDownloadsBetween(remove_begin, base::Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 827 | } |
| 828 | |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 829 | int DownloadManager::RemoveAllDownloads() { |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 830 | if (this != profile_->GetOriginalProfile()->GetDownloadManager()) { |
| 831 | // This is an incognito downloader. Clear All should clear main download |
| 832 | // manager as well. |
| 833 | profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads(); |
| 834 | } |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 835 | // The null times make the date range unbounded. |
| 836 | return RemoveDownloadsBetween(base::Time(), base::Time()); |
| 837 | } |
| 838 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 839 | // Initiate a download of a specific URL. We send the request to the |
| 840 | // ResourceDispatcherHost, and let it send us responses like a regular |
| 841 | // download. |
| 842 | void DownloadManager::DownloadUrl(const GURL& url, |
| 843 | const GURL& referrer, |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 844 | const std::string& referrer_charset, |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 845 | TabContents* tab_contents) { |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 846 | DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(), |
| 847 | tab_contents); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | void DownloadManager::DownloadUrlToFile(const GURL& url, |
| 851 | const GURL& referrer, |
| 852 | const std::string& referrer_charset, |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 853 | const DownloadSaveInfo& save_info, |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 854 | TabContents* tab_contents) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 855 | DCHECK(tab_contents); |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 856 | ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, |
| 857 | NewRunnableFunction(&download_util::DownloadUrl, |
| 858 | url, |
| 859 | referrer, |
| 860 | referrer_charset, |
| 861 | save_info, |
| 862 | g_browser_process->resource_dispatcher_host(), |
| 863 | tab_contents->GetRenderProcessHost()->id(), |
| 864 | tab_contents->render_view_host()->routing_id(), |
| 865 | request_context_getter_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 866 | } |
| 867 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 868 | void DownloadManager::AddObserver(Observer* observer) { |
| 869 | observers_.AddObserver(observer); |
| 870 | observer->ModelChanged(); |
| 871 | } |
| 872 | |
| 873 | void DownloadManager::RemoveObserver(Observer* observer) { |
| 874 | observers_.RemoveObserver(observer); |
| 875 | } |
| 876 | |
| 877 | // Post Windows Shell operations to the Download thread, to avoid blocking the |
| 878 | // user interface. |
| 879 | void DownloadManager::ShowDownloadInShell(const DownloadItem* download) { |
| 880 | DCHECK(file_manager_); |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 881 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
[email protected] | 8b6ff01 | 2009-08-18 22:29:58 | [diff] [blame] | 882 | #if defined(OS_MACOSX) |
| 883 | // Mac needs to run this operation on the UI thread. |
| 884 | platform_util::ShowItemInFolder(download->full_path()); |
| 885 | #else |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 886 | ChromeThread::PostTask( |
| 887 | ChromeThread::FILE, FROM_HERE, |
| 888 | NewRunnableMethod( |
| 889 | file_manager_, &DownloadFileManager::OnShowDownloadInShell, |
| 890 | FilePath(download->full_path()))); |
[email protected] | 8b6ff01 | 2009-08-18 22:29:58 | [diff] [blame] | 891 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 892 | } |
| 893 | |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 894 | void DownloadManager::OpenDownload(const DownloadItem* download, |
| 895 | gfx::NativeView parent_window) { |
[email protected] | 0e34d789 | 2009-06-05 19:17:40 | [diff] [blame] | 896 | // Open Chrome extensions with ExtensionsService. For everything else do shell |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 897 | // execute. |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 898 | if (download->is_extension_install()) { |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 899 | download_util::OpenChromeExtension(profile_, this, *download); |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 900 | } else { |
| 901 | OpenDownloadInShell(download, parent_window); |
| 902 | } |
| 903 | } |
| 904 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 905 | void DownloadManager::OpenDownloadInShell(const DownloadItem* download, |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 906 | gfx::NativeView parent_window) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 907 | DCHECK(file_manager_); |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 908 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
[email protected] | 8b6ff01 | 2009-08-18 22:29:58 | [diff] [blame] | 909 | #if defined(OS_MACOSX) |
| 910 | // Mac OS X requires opening downloads on the UI thread. |
| 911 | platform_util::OpenItem(download->full_path()); |
| 912 | #else |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 913 | ChromeThread::PostTask( |
| 914 | ChromeThread::FILE, FROM_HERE, |
| 915 | NewRunnableMethod( |
| 916 | file_manager_, &DownloadFileManager::OnOpenDownloadInShell, |
| 917 | download->full_path(), download->url(), parent_window)); |
[email protected] | 8b6ff01 | 2009-08-18 22:29:58 | [diff] [blame] | 918 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 919 | } |
| 920 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 921 | void DownloadManager::OpenFilesBasedOnExtension( |
| 922 | const FilePath& path, bool open) { |
| 923 | FilePath::StringType extension = path.Extension(); |
| 924 | if (extension.empty()) |
| 925 | return; |
| 926 | DCHECK(extension[0] == FilePath::kExtensionSeparator); |
| 927 | extension.erase(0, 1); |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 928 | if (open && !download_util::IsExecutableExtension(extension)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 929 | auto_open_.insert(extension); |
| 930 | else |
| 931 | auto_open_.erase(extension); |
| 932 | SaveAutoOpens(); |
| 933 | } |
| 934 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 935 | bool DownloadManager::ShouldOpenFileBasedOnExtension( |
| 936 | const FilePath& path) const { |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 937 | FilePath::StringType extension = path.Extension(); |
| 938 | if (extension.empty()) |
| 939 | return false; |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 940 | if (download_util::IsExecutableExtension(extension)) |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 941 | return false; |
[email protected] | 92e11c8 | 2010-01-13 06:39:56 | [diff] [blame] | 942 | if (Extension::IsExtension(path)) |
| 943 | return false; |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 944 | DCHECK(extension[0] == FilePath::kExtensionSeparator); |
| 945 | extension.erase(0, 1); |
[email protected] | 92e11c8 | 2010-01-13 06:39:56 | [diff] [blame] | 946 | if (auto_open_.find(extension) != auto_open_.end()) |
[email protected] | 8c756ac | 2009-01-30 23:36:41 | [diff] [blame] | 947 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 948 | return false; |
| 949 | } |
| 950 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 951 | bool DownloadManager::IsExecutableFile(const FilePath& path) const { |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 952 | return download_util::IsExecutableExtension(path.Extension()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | void DownloadManager::ResetAutoOpenFiles() { |
| 956 | auto_open_.clear(); |
| 957 | SaveAutoOpens(); |
| 958 | } |
| 959 | |
| 960 | bool DownloadManager::HasAutoOpenFileTypesRegistered() const { |
| 961 | return !auto_open_.empty(); |
| 962 | } |
| 963 | |
| 964 | void DownloadManager::SaveAutoOpens() { |
| 965 | PrefService* prefs = profile_->GetPrefs(); |
| 966 | if (prefs) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 967 | std::string extensions; |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 968 | for (AutoOpenSet::iterator it = auto_open_.begin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 969 | it != auto_open_.end(); ++it) { |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 970 | #if defined(OS_POSIX) |
| 971 | std::string this_extension = *it; |
| 972 | #elif defined(OS_WIN) |
| 973 | std::string this_extension = base::SysWideToUTF8(*it); |
| 974 | #endif |
| 975 | extensions += this_extension + ":"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 976 | } |
| 977 | if (!extensions.empty()) |
| 978 | extensions.erase(extensions.size() - 1); |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 979 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 980 | prefs->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 984 | DownloadItem* DownloadManager::GetDownloadItemFromDbHandle(int64 db_handle) { |
| 985 | for (DownloadMap::iterator it = downloads_.begin(); |
| 986 | it != downloads_.end(); ++it) { |
| 987 | DownloadItem* item = it->second; |
| 988 | if (item->db_handle() == db_handle) |
| 989 | return item; |
| 990 | } |
| 991 | return NULL; |
| 992 | } |
| 993 | |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 994 | void DownloadManager::FileSelected(const FilePath& path, |
[email protected] | 23b357b | 2009-03-30 20:02:36 | [diff] [blame] | 995 | int index, void* params) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 996 | DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 997 | if (info->prompt_user_for_save_location) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 998 | last_download_path_ = path.DirName(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 999 | ContinueStartDownload(info, path); |
| 1000 | } |
| 1001 | |
| 1002 | void DownloadManager::FileSelectionCanceled(void* params) { |
| 1003 | // The user didn't pick a place to save the file, so need to cancel the |
| 1004 | // download that's already in progress to the temporary location. |
| 1005 | DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 1006 | DownloadCancelledInternal(info->download_id, |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 1007 | info->child_id, |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 1008 | info->request_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1009 | } |
| 1010 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 1011 | void DownloadManager::DangerousDownloadValidated(DownloadItem* download) { |
| 1012 | DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state()); |
| 1013 | download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED); |
| 1014 | download->UpdateObservers(); |
| 1015 | |
| 1016 | // If the download is not complete, nothing to do. The required |
| 1017 | // post-processing will be performed when it does complete. |
| 1018 | if (download->state() != DownloadItem::COMPLETE) |
| 1019 | return; |
| 1020 | |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 1021 | ChromeThread::PostTask( |
| 1022 | ChromeThread::FILE, FROM_HERE, |
| 1023 | NewRunnableMethod( |
| 1024 | this, &DownloadManager::ProceedWithFinishedDangerousDownload, |
| 1025 | download->db_handle(), download->full_path(), |
| 1026 | download->original_name())); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 1027 | } |
| 1028 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1029 | // Operations posted to us from the history service ---------------------------- |
| 1030 | |
| 1031 | // The history service has retrieved all download entries. 'entries' contains |
| 1032 | // 'DownloadCreateInfo's in sorted order (by ascending start_time). |
| 1033 | void DownloadManager::OnQueryDownloadEntriesComplete( |
| 1034 | std::vector<DownloadCreateInfo>* entries) { |
| 1035 | for (size_t i = 0; i < entries->size(); ++i) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 1036 | DownloadItem* download = new DownloadItem(this, entries->at(i)); |
| 1037 | DCHECK(!ContainsKey(downloads_, download->db_handle())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1038 | downloads_[download->db_handle()] = download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1039 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1040 | NotifyModelChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1041 | } |
| 1042 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1043 | // Once the new DownloadItem's creation info has been committed to the history |
| 1044 | // service, we associate the DownloadItem with the db handle, update our |
| 1045 | // 'downloads_' map and inform observers. |
| 1046 | void DownloadManager::OnCreateDownloadEntryComplete(DownloadCreateInfo info, |
| 1047 | int64 db_handle) { |
| 1048 | DownloadMap::iterator it = in_progress_.find(info.download_id); |
| 1049 | DCHECK(it != in_progress_.end()); |
| 1050 | |
| 1051 | DownloadItem* download = it->second; |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 1052 | |
| 1053 | // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
| 1054 | // call this function with an invalid |db_handle|. For instance, this can |
| 1055 | // happen when the history database is offline. We cannot have multiple |
| 1056 | // DownloadItems with the same invalid db_handle, so we need to assign a |
| 1057 | // unique |db_handle| here. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 1058 | if (db_handle == DownloadHistory::kUninitializedHandle) |
| 1059 | db_handle = download_history_->GetNextFakeDbHandle(); |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 1060 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 1061 | DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1062 | download->set_db_handle(db_handle); |
| 1063 | |
| 1064 | // Insert into our full map. |
| 1065 | DCHECK(downloads_.find(download->db_handle()) == downloads_.end()); |
| 1066 | downloads_[download->db_handle()] = download; |
| 1067 | |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1068 | // Show in the appropropriate browser UI. |
| 1069 | ShowDownloadInBrowser(info, download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1070 | |
| 1071 | // Inform interested objects about the new download. |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1072 | NotifyModelChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1073 | |
| 1074 | // If this download has been completed before we've received the db handle, |
| 1075 | // post one final message to the history service so that it can be properly |
| 1076 | // in sync with the DownloadItem's completion status, and also inform any |
| 1077 | // observers so that they get more than just the start notification. |
| 1078 | if (download->state() != DownloadItem::IN_PROGRESS) { |
| 1079 | in_progress_.erase(it); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 1080 | download_history_->UpdateEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1081 | download->UpdateObservers(); |
| 1082 | } |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 1083 | |
| 1084 | UpdateAppIcon(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1085 | } |
| 1086 | |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1087 | void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info, |
| 1088 | DownloadItem* download) { |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 1089 | // The 'contents' may no longer exist if the user closed the tab before we |
| 1090 | // get this start completion event. If it does, tell the origin TabContents |
| 1091 | // to display its download shelf. |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 1092 | TabContents* contents = tab_util::GetTabContentsByID(info.child_id, |
| 1093 | info.render_view_id); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1094 | |
| 1095 | // If the contents no longer exists, we start the download in the last active |
| 1096 | // browser. This is not ideal but better than fully hiding the download from |
| 1097 | // the user. |
| 1098 | if (!contents) { |
| 1099 | Browser* last_active = BrowserList::GetLastActive(); |
| 1100 | if (last_active) |
| 1101 | contents = last_active->GetSelectedTabContents(); |
| 1102 | } |
| 1103 | |
| 1104 | if (contents) |
| 1105 | contents->OnStartDownload(download); |
| 1106 | } |
| 1107 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 1108 | // Clears the last download path, used to initialize "save as" dialogs. |
[email protected] | 905a08d | 2008-11-19 07:24:12 | [diff] [blame] | 1109 | void DownloadManager::ClearLastDownloadPath() { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1110 | last_download_path_ = FilePath(); |
[email protected] | eea4662 | 2009-07-15 20:49:38 | [diff] [blame] | 1111 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1112 | |
| 1113 | void DownloadManager::NotifyModelChanged() { |
| 1114 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
| 1115 | } |
| 1116 | |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 1117 | DownloadItem* DownloadManager::GetDownloadItem(int id) { |
| 1118 | for (DownloadMap::iterator it = downloads_.begin(); |
| 1119 | it != downloads_.end(); ++it) { |
| 1120 | DownloadItem* item = it->second; |
| 1121 | if (item->id() == id) |
| 1122 | return item; |
| 1123 | } |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1127 | // DownloadManager::OtherDownloadManagerObserver implementation ---------------- |
| 1128 | |
| 1129 | DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver( |
| 1130 | DownloadManager* observing_download_manager) |
| 1131 | : observing_download_manager_(observing_download_manager), |
| 1132 | observed_download_manager_(NULL) { |
| 1133 | if (observing_download_manager->profile_->GetOriginalProfile() == |
| 1134 | observing_download_manager->profile_) { |
| 1135 | return; |
| 1136 | } |
| 1137 | |
| 1138 | observed_download_manager_ = observing_download_manager_-> |
| 1139 | profile_->GetOriginalProfile()->GetDownloadManager(); |
| 1140 | observed_download_manager_->AddObserver(this); |
| 1141 | } |
| 1142 | |
| 1143 | DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() { |
| 1144 | if (observed_download_manager_) |
| 1145 | observed_download_manager_->RemoveObserver(this); |
| 1146 | } |
| 1147 | |
| 1148 | void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
| 1149 | observing_download_manager_->NotifyModelChanged(); |
| 1150 | } |
| 1151 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1152 | void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
| 1153 | observed_download_manager_ = NULL; |
| 1154 | } |