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