license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | |
| 5 | #include <time.h> |
| 6 | |
[email protected] | cdaa865 | 2008-09-13 02:48:59 | [diff] [blame] | 7 | #include "chrome/browser/download/download_manager.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
| 9 | #include "base/file_util.h" |
| 10 | #include "base/logging.h" |
| 11 | #include "base/message_loop.h" |
| 12 | #include "base/path_service.h" |
| 13 | #include "base/registry.h" |
| 14 | #include "base/string_util.h" |
| 15 | #include "base/task.h" |
| 16 | #include "base/thread.h" |
| 17 | #include "base/timer.h" |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 18 | #include "base/rand_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "base/win_util.h" |
| 20 | #include "chrome/browser/browser_list.h" |
| 21 | #include "chrome/browser/browser_process.h" |
[email protected] | cdaa865 | 2008-09-13 02:48:59 | [diff] [blame] | 22 | #include "chrome/browser/download/download_file.h" |
| 23 | #include "chrome/browser/download/download_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | #include "chrome/browser/profile.h" |
| 25 | #include "chrome/browser/render_process_host.h" |
| 26 | #include "chrome/browser/render_view_host.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 27 | #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | #include "chrome/browser/tab_util.h" |
| 29 | #include "chrome/browser/web_contents.h" |
| 30 | #include "chrome/common/chrome_paths.h" |
| 31 | #include "chrome/common/l10n_util.h" |
| 32 | #include "chrome/common/notification_service.h" |
| 33 | #include "chrome/common/pref_names.h" |
| 34 | #include "chrome/common/pref_service.h" |
| 35 | #include "chrome/common/stl_util-inl.h" |
| 36 | #include "chrome/common/win_util.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 37 | #include "googleurl/src/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | #include "net/base/mime_util.h" |
| 39 | #include "net/base/net_util.h" |
| 40 | #include "net/url_request/url_request_context.h" |
| 41 | |
| 42 | #include "generated_resources.h" |
| 43 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 44 | using base::Time; |
| 45 | using base::TimeDelta; |
| 46 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | // Periodically update our observers. |
| 48 | class DownloadItemUpdateTask : public Task { |
| 49 | public: |
| 50 | explicit DownloadItemUpdateTask(DownloadItem* item) : item_(item) {} |
| 51 | void Run() { if (item_) item_->UpdateObservers(); } |
| 52 | |
| 53 | private: |
| 54 | DownloadItem* item_; |
| 55 | }; |
| 56 | |
| 57 | // Update frequency (milliseconds). |
| 58 | static const int kUpdateTimeMs = 1000; |
| 59 | |
| 60 | // Our download table ID starts at 1, so we use 0 to represent a download that |
| 61 | // has started, but has not yet had its data persisted in the table. We use fake |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 62 | // database handles in incognito mode starting at -1 and progressively getting |
| 63 | // more negative. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | static const int kUninitializedHandle = 0; |
| 65 | |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 66 | // Appends the passed the number between parenthesis the path before the |
| 67 | // extension. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 68 | static void AppendNumberToPath(FilePath* path, int number) { |
| 69 | file_util::InsertBeforeExtension(path, |
| 70 | StringPrintf(FILE_PATH_LITERAL(" (%d)"), number)); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Attempts to find a number that can be appended to that path to make it |
| 74 | // unique. If |path| does not exist, 0 is returned. If it fails to find such |
| 75 | // a number, -1 is returned. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 76 | static int GetUniquePathNumber(const FilePath& path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | const int kMaxAttempts = 100; |
| 78 | |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 79 | if (!file_util::PathExists(path)) |
| 80 | return 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 82 | FilePath new_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | for (int count = 1; count <= kMaxAttempts; ++count) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 84 | new_path = FilePath(path); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 85 | AppendNumberToPath(&new_path, count); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 87 | if (!file_util::PathExists(new_path)) |
| 88 | return count; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 91 | return -1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 94 | static bool DownloadPathIsDangerous(const FilePath& download_path) { |
| 95 | FilePath desktop_dir; |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 96 | if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { |
| 97 | NOTREACHED(); |
| 98 | return false; |
| 99 | } |
| 100 | return (download_path == desktop_dir); |
| 101 | } |
| 102 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | // DownloadItem implementation ------------------------------------------------- |
| 104 | |
| 105 | // Constructor for reading from the history service. |
| 106 | DownloadItem::DownloadItem(const DownloadCreateInfo& info) |
| 107 | : id_(-1), |
| 108 | full_path_(info.path), |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 109 | original_name_(info.original_name), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | url_(info.url), |
| 111 | total_bytes_(info.total_bytes), |
| 112 | received_bytes_(info.received_bytes), |
| 113 | start_tick_(0), |
| 114 | state_(static_cast<DownloadState>(info.state)), |
| 115 | start_time_(info.start_time), |
| 116 | db_handle_(info.db_handle), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | manager_(NULL), |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 118 | safety_state_(SAFE), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | is_paused_(false), |
| 120 | open_when_complete_(false), |
| 121 | render_process_id_(-1), |
| 122 | request_id_(-1) { |
| 123 | if (state_ == IN_PROGRESS) |
| 124 | state_ = CANCELLED; |
| 125 | Init(false /* don't start progress timer */); |
| 126 | } |
| 127 | |
| 128 | // Constructor for DownloadItem created via user action in the main thread. |
| 129 | DownloadItem::DownloadItem(int32 download_id, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 130 | const FilePath& path, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 131 | int path_uniquifier, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | const std::wstring& url, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 133 | const FilePath& original_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | const Time start_time, |
| 135 | int64 download_size, |
| 136 | int render_process_id, |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 137 | int request_id, |
| 138 | bool is_dangerous) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | : id_(download_id), |
| 140 | full_path_(path), |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 141 | path_uniquifier_(path_uniquifier), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | url_(url), |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 143 | original_name_(original_name), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | total_bytes_(download_size), |
| 145 | received_bytes_(0), |
| 146 | start_tick_(GetTickCount()), |
| 147 | state_(IN_PROGRESS), |
| 148 | start_time_(start_time), |
| 149 | db_handle_(kUninitializedHandle), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | manager_(NULL), |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 151 | safety_state_(is_dangerous ? DANGEROUS : SAFE), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | is_paused_(false), |
| 153 | open_when_complete_(false), |
| 154 | render_process_id_(render_process_id), |
| 155 | request_id_(request_id) { |
| 156 | Init(true /* start progress timer */); |
| 157 | } |
| 158 | |
| 159 | void DownloadItem::Init(bool start_timer) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 160 | file_name_ = full_path_.BaseName(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | if (start_timer) |
| 162 | StartProgressTimer(); |
| 163 | } |
| 164 | |
| 165 | DownloadItem::~DownloadItem() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | state_ = REMOVING; |
| 167 | UpdateObservers(); |
| 168 | } |
| 169 | |
| 170 | void DownloadItem::AddObserver(Observer* observer) { |
| 171 | observers_.AddObserver(observer); |
| 172 | } |
| 173 | |
| 174 | void DownloadItem::RemoveObserver(Observer* observer) { |
| 175 | observers_.RemoveObserver(observer); |
| 176 | } |
| 177 | |
| 178 | void DownloadItem::UpdateObservers() { |
| 179 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); |
| 180 | } |
| 181 | |
| 182 | // If we've received more data than we were expecting (bad server info?), revert |
| 183 | // to 'unknown size mode'. |
| 184 | void DownloadItem::UpdateSize(int64 bytes_so_far) { |
| 185 | received_bytes_ = bytes_so_far; |
| 186 | if (received_bytes_ > total_bytes_) |
| 187 | total_bytes_ = 0; |
| 188 | } |
| 189 | |
| 190 | // Updates from the download thread may have been posted while this download |
| 191 | // was being cancelled in the UI thread, so we'll accept them unless we're |
| 192 | // complete. |
| 193 | void DownloadItem::Update(int64 bytes_so_far) { |
| 194 | if (state_ == COMPLETE) { |
| 195 | NOTREACHED(); |
| 196 | return; |
| 197 | } |
| 198 | UpdateSize(bytes_so_far); |
| 199 | UpdateObservers(); |
| 200 | } |
| 201 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 202 | // Triggered by a user action. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | void DownloadItem::Cancel(bool update_history) { |
| 204 | if (state_ != IN_PROGRESS) { |
| 205 | // Small downloads might be complete before this method has a chance to run. |
| 206 | return; |
| 207 | } |
| 208 | state_ = CANCELLED; |
| 209 | UpdateObservers(); |
| 210 | StopProgressTimer(); |
| 211 | if (update_history) |
| 212 | manager_->DownloadCancelled(id_); |
| 213 | } |
| 214 | |
| 215 | void DownloadItem::Finished(int64 size) { |
| 216 | state_ = COMPLETE; |
| 217 | UpdateSize(size); |
[email protected] | 22fbe5a | 2008-10-29 22:20:40 | [diff] [blame] | 218 | UpdateObservers(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 219 | StopProgressTimer(); |
| 220 | } |
| 221 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 222 | void DownloadItem::Remove(bool delete_on_disk) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | Cancel(true); |
| 224 | state_ = REMOVING; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 225 | if (delete_on_disk) |
| 226 | manager_->DeleteDownload(full_path_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | manager_->RemoveDownload(db_handle_); |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 228 | // We have now been deleted. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void DownloadItem::StartProgressTimer() { |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 232 | update_timer_.Start(TimeDelta::FromMilliseconds(kUpdateTimeMs), this, |
| 233 | &DownloadItem::UpdateObservers); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void DownloadItem::StopProgressTimer() { |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 237 | update_timer_.Stop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool DownloadItem::TimeRemaining(TimeDelta* remaining) const { |
| 241 | if (total_bytes_ <= 0) |
| 242 | return false; // We never received the content_length for this download. |
| 243 | |
| 244 | int64 speed = CurrentSpeed(); |
| 245 | if (speed == 0) |
| 246 | return false; |
| 247 | |
| 248 | *remaining = |
| 249 | TimeDelta::FromSeconds((total_bytes_ - received_bytes_) / speed); |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | int64 DownloadItem::CurrentSpeed() const { |
| 254 | uintptr_t diff = GetTickCount() - start_tick_; |
| 255 | return diff == 0 ? 0 : received_bytes_ * 1000 / diff; |
| 256 | } |
| 257 | |
| 258 | int DownloadItem::PercentComplete() const { |
| 259 | int percent = -1; |
| 260 | if (total_bytes_ > 0) |
| 261 | percent = static_cast<int>(received_bytes_ * 100.0 / total_bytes_); |
| 262 | return percent; |
| 263 | } |
| 264 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 265 | void DownloadItem::Rename(const FilePath& full_path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 266 | DCHECK(!full_path.empty()); |
| 267 | full_path_ = full_path; |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 268 | file_name_ = full_path_.BaseName(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void DownloadItem::TogglePause() { |
| 272 | DCHECK(state_ == IN_PROGRESS); |
| 273 | manager_->PauseDownload(id_, !is_paused_); |
| 274 | is_paused_ = !is_paused_; |
| 275 | UpdateObservers(); |
| 276 | } |
| 277 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 278 | FilePath DownloadItem::GetFileName() const { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 279 | if (safety_state_ == DownloadItem::SAFE) |
| 280 | return file_name_; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 281 | if (path_uniquifier_ > 0) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 282 | FilePath name(original_name_); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 283 | AppendNumberToPath(&name, path_uniquifier_); |
| 284 | return name; |
| 285 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 286 | return original_name_; |
| 287 | } |
| 288 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | // DownloadManager implementation ---------------------------------------------- |
| 290 | |
| 291 | // static |
| 292 | void DownloadManager::RegisterUserPrefs(PrefService* prefs) { |
| 293 | prefs->RegisterBooleanPref(prefs::kPromptForDownload, false); |
| 294 | prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen, L""); |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 295 | prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false); |
| 296 | |
| 297 | // The default download path is userprofile\download. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 298 | FilePath default_download_path; |
[email protected] | cbc43fc | 2008-10-28 00:44:12 | [diff] [blame] | 299 | if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, |
| 300 | &default_download_path)) { |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 301 | NOTREACHED(); |
| 302 | } |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 303 | prefs->RegisterStringPref(prefs::kDownloadDefaultDirectory, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 304 | default_download_path.ToWStringHack()); |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 305 | |
| 306 | // If the download path is dangerous we forcefully reset it. But if we do |
| 307 | // so we set a flag to make sure we only do it once, to avoid fighting |
| 308 | // the user if he really wants it on an unsafe place such as the desktop. |
| 309 | |
| 310 | if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 311 | FilePath current_download_dir = FilePath::FromWStringHack( |
| 312 | prefs->GetString(prefs::kDownloadDefaultDirectory)); |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 313 | if (DownloadPathIsDangerous(current_download_dir)) { |
| 314 | prefs->SetString(prefs::kDownloadDefaultDirectory, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 315 | default_download_path.ToWStringHack()); |
[email protected] | f052118e | 2008-09-05 02:25:32 | [diff] [blame] | 316 | } |
| 317 | prefs->SetBoolean(prefs::kDownloadDirUpgraded, true); |
| 318 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | DownloadManager::DownloadManager() |
| 322 | : shutdown_needed_(false), |
| 323 | profile_(NULL), |
| 324 | file_manager_(NULL), |
| 325 | ui_loop_(MessageLoop::current()), |
| 326 | file_loop_(NULL) { |
| 327 | } |
| 328 | |
| 329 | DownloadManager::~DownloadManager() { |
| 330 | if (shutdown_needed_) |
| 331 | Shutdown(); |
| 332 | } |
| 333 | |
| 334 | void DownloadManager::Shutdown() { |
| 335 | DCHECK(shutdown_needed_) << "Shutdown called when not needed."; |
| 336 | |
| 337 | // Stop receiving download updates |
| 338 | file_manager_->RemoveDownloadManager(this); |
| 339 | |
| 340 | // Stop making history service requests |
| 341 | cancelable_consumer_.CancelAllRequests(); |
| 342 | |
| 343 | // 'in_progress_' may contain DownloadItems that have not finished the start |
| 344 | // complete (from the history service) and thus aren't in downloads_. |
| 345 | DownloadMap::iterator it = in_progress_.begin(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 346 | std::set<DownloadItem*> to_remove; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 347 | for (; it != in_progress_.end(); ++it) { |
| 348 | DownloadItem* download = it->second; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 349 | if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 350 | // Forget about any download that the user did not approve. |
| 351 | // Note that we cannot call download->Remove() this would invalidate our |
| 352 | // iterator. |
| 353 | to_remove.insert(download); |
| 354 | continue; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 355 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 356 | DCHECK_EQ(DownloadItem::IN_PROGRESS, download->state()); |
| 357 | download->Cancel(false); |
| 358 | UpdateHistoryForDownload(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | if (download->db_handle() == kUninitializedHandle) { |
| 360 | // An invalid handle means that 'download' does not yet exist in |
| 361 | // 'downloads_', so we have to delete it here. |
| 362 | delete download; |
| 363 | } |
| 364 | } |
| 365 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 366 | // 'dangerous_finished_' contains all complete downloads that have not been |
| 367 | // approved. They should be removed. |
| 368 | it = dangerous_finished_.begin(); |
| 369 | for (; it != dangerous_finished_.end(); ++it) |
| 370 | to_remove.insert(it->second); |
| 371 | |
| 372 | // Remove the dangerous download that are not approved. |
| 373 | for (std::set<DownloadItem*>::const_iterator rm_it = to_remove.begin(); |
| 374 | rm_it != to_remove.end(); ++rm_it) { |
| 375 | DownloadItem* download = *rm_it; |
[email protected] | e10e17c7 | 2008-10-15 17:48:32 | [diff] [blame] | 376 | int64 handle = download->db_handle(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 377 | download->Remove(true); |
[email protected] | e10e17c7 | 2008-10-15 17:48:32 | [diff] [blame] | 378 | // Same as above, delete the download if it is not in 'downloads_' (as the |
| 379 | // Remove() call above won't have deleted it). |
| 380 | if (handle == kUninitializedHandle) |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 381 | delete download; |
| 382 | } |
| 383 | to_remove.clear(); |
| 384 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | in_progress_.clear(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 386 | dangerous_finished_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 387 | STLDeleteValues(&downloads_); |
| 388 | |
| 389 | file_manager_ = NULL; |
| 390 | |
| 391 | // Save our file extensions to auto open. |
| 392 | SaveAutoOpens(); |
| 393 | |
| 394 | // Make sure the save as dialog doesn't notify us back if we're gone before |
| 395 | // it returns. |
| 396 | if (select_file_dialog_.get()) |
| 397 | select_file_dialog_->ListenerDestroyed(); |
| 398 | |
| 399 | shutdown_needed_ = false; |
| 400 | } |
| 401 | |
| 402 | // Issue a history query for downloads matching 'search_text'. If 'search_text' |
| 403 | // is empty, return all downloads that we know about. |
| 404 | void DownloadManager::GetDownloads(Observer* observer, |
| 405 | const std::wstring& search_text) { |
| 406 | DCHECK(observer); |
| 407 | |
| 408 | // Return a empty list if we've not yet received the set of downloads from the |
| 409 | // history system (we'll update all observers once we get that list in |
| 410 | // OnQueryDownloadEntriesComplete), or if there are no downloads at all. |
| 411 | std::vector<DownloadItem*> download_copy; |
| 412 | if (downloads_.empty()) { |
| 413 | observer->SetDownloads(download_copy); |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | // We already know all the downloads and there is no filter, so just return a |
| 418 | // copy to the observer. |
| 419 | if (search_text.empty()) { |
| 420 | download_copy.reserve(downloads_.size()); |
| 421 | for (DownloadMap::iterator it = downloads_.begin(); |
| 422 | it != downloads_.end(); ++it) { |
| 423 | download_copy.push_back(it->second); |
| 424 | } |
| 425 | |
| 426 | // We retain ownership of the DownloadItems. |
| 427 | observer->SetDownloads(download_copy); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | // Issue a request to the history service for a list of downloads matching |
| 432 | // our search text. |
| 433 | HistoryService* hs = |
| 434 | profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 435 | if (hs) { |
| 436 | HistoryService::Handle h = |
| 437 | hs->SearchDownloads(search_text, |
| 438 | &cancelable_consumer_, |
| 439 | NewCallback(this, |
| 440 | &DownloadManager::OnSearchComplete)); |
| 441 | cancelable_consumer_.SetClientData(hs, h, observer); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // Query the history service for information about all persisted downloads. |
| 446 | bool DownloadManager::Init(Profile* profile) { |
| 447 | DCHECK(profile); |
| 448 | DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 449 | shutdown_needed_ = true; |
| 450 | |
| 451 | profile_ = profile; |
| 452 | request_context_ = profile_->GetRequestContext(); |
| 453 | |
| 454 | // 'incognito mode' will have access to past downloads, but we won't store |
| 455 | // information about new downloads while in that mode. |
| 456 | QueryHistoryForDownloads(); |
| 457 | |
| 458 | ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
| 459 | if (!rdh) { |
| 460 | NOTREACHED(); |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | file_manager_ = rdh->download_file_manager(); |
| 465 | if (!file_manager_) { |
| 466 | NOTREACHED(); |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | file_loop_ = g_browser_process->file_thread()->message_loop(); |
| 471 | if (!file_loop_) { |
| 472 | NOTREACHED(); |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | // Get our user preference state. |
| 477 | PrefService* prefs = profile_->GetPrefs(); |
| 478 | DCHECK(prefs); |
| 479 | prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL); |
| 480 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 481 | download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL); |
| 482 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 483 | // This variable is needed to resolve which CreateDirectory we want to point |
| 484 | // to. Without it, the NewRunnableFunction cannot resolve the ambiguity. |
| 485 | // TODO(estade): when file_util::CreateDirectory(wstring) is removed, |
| 486 | // get rid of |CreateDirectoryPtr|. |
| 487 | bool (*CreateDirectoryPtr)(const FilePath&) = &file_util::CreateDirectory; |
[email protected] | bb69e9b3 | 2008-08-14 23:08:14 | [diff] [blame] | 488 | // Ensure that the download directory specified in the preferences exists. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 489 | file_loop_->PostTask(FROM_HERE, NewRunnableFunction( |
| 490 | CreateDirectoryPtr, download_path())); |
[email protected] | bb69e9b3 | 2008-08-14 23:08:14 | [diff] [blame] | 491 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 492 | // We store any file extension that should be opened automatically at |
| 493 | // download completion in this pref. |
| 494 | download_util::InitializeExeTypes(&exe_types_); |
| 495 | |
| 496 | std::wstring extensions_to_open = |
| 497 | prefs->GetString(prefs::kDownloadExtensionsToOpen); |
| 498 | std::vector<std::wstring> extensions; |
| 499 | SplitString(extensions_to_open, L':', &extensions); |
| 500 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 501 | if (!extensions[i].empty() && !IsExecutable(extensions[i])) |
| 502 | auto_open_.insert(extensions[i]); |
| 503 | } |
| 504 | |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | void DownloadManager::QueryHistoryForDownloads() { |
| 509 | HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 510 | if (hs) { |
| 511 | hs->QueryDownloads( |
| 512 | &cancelable_consumer_, |
| 513 | NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // We have received a message from DownloadFileManager about a new download. We |
| 518 | // create a download item and store it in our download map, and inform the |
| 519 | // history system of a new download. Since this method can be called while the |
| 520 | // history service thread is still reading the persistent state, we do not |
| 521 | // insert the new DownloadItem into 'downloads_' or inform our observers at this |
| 522 | // point. OnCreateDatabaseEntryComplete() handles that finalization of the the |
| 523 | // download creation as a callback from the history thread. |
| 524 | void DownloadManager::StartDownload(DownloadCreateInfo* info) { |
| 525 | DCHECK(MessageLoop::current() == ui_loop_); |
| 526 | DCHECK(info); |
| 527 | |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 528 | // Freeze the user's preference for showing a Save As dialog. We're going to |
| 529 | // bounce around a bunch of threads and we don't want to worry about race |
| 530 | // conditions where the user changes this pref out from under us. |
| 531 | if (*prompt_for_download_) |
| 532 | info->save_as = true; |
| 533 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | // Determine the proper path for a download, by choosing either the default |
| 535 | // download directory, or prompting the user. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 536 | FilePath generated_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 537 | GenerateFilename(info, &generated_name); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 538 | if (info->save_as && !last_download_path_.empty()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 539 | info->suggested_path = last_download_path_; |
| 540 | else |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 541 | info->suggested_path = download_path(); |
| 542 | info->suggested_path = info->suggested_path.Append(generated_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 543 | |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 544 | if (!info->save_as) { |
| 545 | // Let's check if this download is dangerous, based on its name. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 546 | info->is_dangerous = IsDangerous(info->suggested_path.BaseName()); |
[email protected] | e9ebf3fc | 2008-10-17 22:06:58 | [diff] [blame] | 547 | } |
| 548 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 549 | // We need to move over to the download thread because we don't want to stat |
| 550 | // the suggested path on the UI thread. |
| 551 | file_loop_->PostTask(FROM_HERE, |
| 552 | NewRunnableMethod(this, |
| 553 | &DownloadManager::CheckIfSuggestedPathExists, |
| 554 | info)); |
| 555 | } |
| 556 | |
| 557 | void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info) { |
| 558 | DCHECK(info); |
| 559 | |
| 560 | // Check writability of the suggested path. If we can't write to it, default |
| 561 | // 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^] | 562 | FilePath dir = info->suggested_path.DirName(); |
| 563 | FilePath filename = info->suggested_path.BaseName(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 564 | if (!file_util::PathIsWritable(dir)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 565 | info->save_as = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 566 | PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path); |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 567 | info->suggested_path = info->suggested_path.Append(filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 568 | } |
| 569 | |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 570 | info->path_uniquifier = GetUniquePathNumber(info->suggested_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 572 | // 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] | 573 | if (info->is_dangerous) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 574 | info->original_name = FilePath(info->suggested_path).BaseName(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 575 | // Create a temporary file to hold the file until the user approves its |
| 576 | // download. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 577 | FilePath::StringType file_name; |
| 578 | FilePath path; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 579 | while (path.empty()) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 580 | SStringPrintf(&file_name, FILE_PATH_LITERAL("unconfirmed %d.download"), |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 581 | base::RandInt(0, 100000)); |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 582 | path = dir.Append(file_name); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 583 | if (file_util::PathExists(path)) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 584 | path = FilePath(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 585 | } |
| 586 | info->suggested_path = path; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 587 | } else { |
| 588 | // We know the final path, build it if necessary. |
| 589 | if (info->path_uniquifier > 0) { |
| 590 | AppendNumberToPath(&(info->suggested_path), info->path_uniquifier); |
| 591 | // Setting path_uniquifier to 0 to make sure we don't try to unique it |
| 592 | // later on. |
| 593 | info->path_uniquifier = 0; |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 594 | } else if (info->path_uniquifier == -1) { |
| 595 | // We failed to find a unique path. We have to prompt the user. |
| 596 | info->save_as = true; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 597 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 598 | } |
| 599 | |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 600 | if (!info->save_as) { |
| 601 | // Create an empty file at the suggested path so that we don't allocate the |
| 602 | // same "non-existant" path to multiple downloads. |
| 603 | // See: http://code.google.com/p/chromium/issues/detail?id=3662 |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 604 | file_util::WriteFile(info->suggested_path.ToWStringHack(), "", 0); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 605 | } |
| 606 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 607 | // Now we return to the UI thread. |
| 608 | ui_loop_->PostTask(FROM_HERE, |
| 609 | NewRunnableMethod(this, |
| 610 | &DownloadManager::OnPathExistenceAvailable, |
| 611 | info)); |
| 612 | } |
| 613 | |
| 614 | void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) { |
| 615 | DCHECK(MessageLoop::current() == ui_loop_); |
| 616 | DCHECK(info); |
| 617 | |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 618 | if (info->save_as) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 619 | // We must ask the user for the place to put the download. |
| 620 | if (!select_file_dialog_.get()) |
| 621 | select_file_dialog_ = SelectFileDialog::Create(this); |
| 622 | |
[email protected] | a3a1d14 | 2008-12-19 00:42:30 | [diff] [blame] | 623 | WebContents* contents = tab_util::GetWebContentsByID( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 624 | info->render_process_id, info->render_view_id); |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 625 | std::wstring filter = |
| 626 | win_util::GetFileFilterFromPath(info->suggested_path.value()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 627 | HWND owning_hwnd = |
| 628 | contents ? GetAncestor(contents->GetContainerHWND(), GA_ROOT) : NULL; |
| 629 | select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 630 | std::wstring(), |
| 631 | info->suggested_path.ToWStringHack(), |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 632 | filter, std::wstring(), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 633 | owning_hwnd, info); |
| 634 | } else { |
| 635 | // No prompting for download, just continue with the suggested name. |
| 636 | ContinueStartDownload(info, info->suggested_path); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | void DownloadManager::ContinueStartDownload(DownloadCreateInfo* info, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 641 | const FilePath& target_path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 642 | scoped_ptr<DownloadCreateInfo> infop(info); |
| 643 | info->path = target_path; |
| 644 | |
| 645 | DownloadItem* download = NULL; |
| 646 | DownloadMap::iterator it = in_progress_.find(info->download_id); |
| 647 | if (it == in_progress_.end()) { |
| 648 | download = new DownloadItem(info->download_id, |
| 649 | info->path, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 650 | info->path_uniquifier, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 651 | info->url, |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 652 | info->original_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 653 | info->start_time, |
| 654 | info->total_bytes, |
| 655 | info->render_process_id, |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 656 | info->request_id, |
| 657 | info->is_dangerous); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 658 | download->set_manager(this); |
| 659 | in_progress_[info->download_id] = download; |
| 660 | } else { |
| 661 | NOTREACHED(); // Should not exist! |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | // If the download already completed by the time we reached this point, then |
| 666 | // notify observers that it did. |
| 667 | PendingFinishedMap::iterator pending_it = |
| 668 | pending_finished_downloads_.find(info->download_id); |
| 669 | if (pending_it != pending_finished_downloads_.end()) |
| 670 | DownloadFinished(pending_it->first, pending_it->second); |
| 671 | |
| 672 | download->Rename(target_path); |
| 673 | |
| 674 | file_loop_->PostTask(FROM_HERE, |
| 675 | NewRunnableMethod(file_manager_, |
| 676 | &DownloadFileManager::OnFinalDownloadName, |
| 677 | download->id(), |
| 678 | target_path)); |
| 679 | |
| 680 | if (profile_->IsOffTheRecord()) { |
| 681 | // Fake a db handle for incognito mode, since nothing is actually stored in |
| 682 | // the database in this mode. We have to make sure that these handles don't |
| 683 | // collide with normal db handles, so we use a negative value. Eventually, |
| 684 | // they could overlap, but you'd have to do enough downloading that your ISP |
| 685 | // would likely stab you in the neck first. YMMV. |
| 686 | static int64 fake_db_handle = kUninitializedHandle - 1; |
| 687 | OnCreateDownloadEntryComplete(*info, fake_db_handle--); |
| 688 | } else { |
| 689 | // Update the history system with the new download. |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 690 | // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 691 | HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 692 | if (hs) { |
| 693 | hs->CreateDownload( |
| 694 | *info, &cancelable_consumer_, |
| 695 | NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | // Convenience function for updating the history service for a download. |
| 701 | void DownloadManager::UpdateHistoryForDownload(DownloadItem* download) { |
| 702 | DCHECK(download); |
| 703 | |
| 704 | // Don't store info in the database if the download was initiated while in |
| 705 | // incognito mode or if it hasn't been initialized in our database table. |
| 706 | if (download->db_handle() <= kUninitializedHandle) |
| 707 | return; |
| 708 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 709 | // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 710 | HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 711 | if (hs) { |
| 712 | hs->UpdateDownload(download->received_bytes(), |
| 713 | download->state(), |
| 714 | download->db_handle()); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | void DownloadManager::RemoveDownloadFromHistory(DownloadItem* download) { |
| 719 | DCHECK(download); |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 720 | // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 721 | HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 722 | if (download->db_handle() > kUninitializedHandle && hs) |
| 723 | hs->RemoveDownload(download->db_handle()); |
| 724 | } |
| 725 | |
| 726 | void DownloadManager::RemoveDownloadsFromHistoryBetween(const Time remove_begin, |
| 727 | const Time remove_end) { |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 728 | // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 729 | HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 730 | if (hs) |
| 731 | hs->RemoveDownloadsBetween(remove_begin, remove_end); |
| 732 | } |
| 733 | |
| 734 | void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
| 735 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 736 | if (it != in_progress_.end()) { |
| 737 | DownloadItem* download = it->second; |
| 738 | download->Update(size); |
| 739 | UpdateHistoryForDownload(download); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | void DownloadManager::DownloadFinished(int32 download_id, int64 size) { |
| 744 | DownloadMap::iterator it = in_progress_.find(download_id); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 745 | if (it == in_progress_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 746 | // The download is done, but the user hasn't selected a final location for |
| 747 | // it yet (the Save As dialog box is probably still showing), so just keep |
| 748 | // track of the fact that this download id is complete, when the |
| 749 | // DownloadItem is constructed later we'll notify its completion then. |
| 750 | PendingFinishedMap::iterator erase_it = |
| 751 | pending_finished_downloads_.find(download_id); |
| 752 | DCHECK(erase_it == pending_finished_downloads_.end()); |
| 753 | pending_finished_downloads_[download_id] = size; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 754 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 755 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 756 | |
| 757 | // Remove the id from the list of pending ids. |
| 758 | PendingFinishedMap::iterator erase_it = |
| 759 | pending_finished_downloads_.find(download_id); |
| 760 | if (erase_it != pending_finished_downloads_.end()) |
| 761 | pending_finished_downloads_.erase(erase_it); |
| 762 | |
| 763 | DownloadItem* download = it->second; |
| 764 | download->Finished(size); |
| 765 | |
| 766 | // Clean up will happen when the history system create callback runs if we |
| 767 | // don't have a valid db_handle yet. |
| 768 | if (download->db_handle() != kUninitializedHandle) { |
| 769 | in_progress_.erase(it); |
| 770 | NotifyAboutDownloadStop(); |
| 771 | UpdateHistoryForDownload(download); |
| 772 | } |
| 773 | |
| 774 | // If this a dangerous download not yet validated by the user, don't do |
| 775 | // anything. When the user notifies us, it will trigger a call to |
| 776 | // ProceedWithFinishedDangerousDownload. |
| 777 | if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 778 | dangerous_finished_[download_id] = download; |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | if (download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) { |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 783 | // We first need to rename the downloaded file from its temporary name to |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 784 | // its final name before we can continue. |
| 785 | file_loop_->PostTask(FROM_HERE, |
| 786 | NewRunnableMethod( |
| 787 | this, &DownloadManager::ProceedWithFinishedDangerousDownload, |
| 788 | download->db_handle(), |
| 789 | download->full_path(), download->original_name())); |
| 790 | return; |
| 791 | } |
| 792 | ContinueDownloadFinished(download); |
| 793 | } |
| 794 | |
| 795 | void DownloadManager::ContinueDownloadFinished(DownloadItem* download) { |
| 796 | // If this was a dangerous download, it has now been approved and must be |
| 797 | // removed from dangerous_finished_ so it does not get deleted on shutdown. |
| 798 | DownloadMap::iterator it = dangerous_finished_.find(download->id()); |
| 799 | if (it != dangerous_finished_.end()) |
| 800 | dangerous_finished_.erase(it); |
| 801 | |
| 802 | // Notify our observers that we are complete (the call to Finished() set the |
| 803 | // state to complete but did not notify). |
| 804 | download->UpdateObservers(); |
| 805 | |
| 806 | // Open the download if the user or user prefs indicate it should be. |
| 807 | const std::wstring extension = |
| 808 | file_util::GetFileExtensionFromPath(download->full_path()); |
| 809 | if (download->open_when_complete() || ShouldOpenFileExtension(extension)) |
| 810 | OpenDownloadInShell(download, NULL); |
| 811 | } |
| 812 | |
| 813 | // Called on the file thread. Renames the downloaded file to its original name. |
| 814 | void DownloadManager::ProceedWithFinishedDangerousDownload( |
| 815 | int64 download_handle, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 816 | const FilePath& path, |
| 817 | const FilePath& original_name) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 818 | bool success = false; |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 819 | FilePath new_path; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 820 | int uniquifier = 0; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 821 | if (file_util::PathExists(path)) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 822 | new_path = new_path.DirName().Append(original_name); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 823 | // Make our name unique at this point, as if a dangerous file is downloading |
| 824 | // and a 2nd download is started for a file with the same name, they would |
| 825 | // have the same path. This is because we uniquify the name on download |
| 826 | // start, and at that time the first file does not exists yet, so the second |
| 827 | // file gets the same name. |
| 828 | uniquifier = GetUniquePathNumber(new_path); |
| 829 | if (uniquifier > 0) |
| 830 | AppendNumberToPath(&new_path, uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 831 | success = file_util::Move(path, new_path); |
| 832 | } else { |
| 833 | NOTREACHED(); |
| 834 | } |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 835 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 836 | ui_loop_->PostTask(FROM_HERE, |
| 837 | NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 838 | download_handle, success, new_path, uniquifier)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // Call from the file thread when the finished dangerous download was renamed. |
| 842 | void DownloadManager::DangerousDownloadRenamed(int64 download_handle, |
| 843 | bool success, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 844 | const FilePath& new_path, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 845 | int new_path_uniquifier) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 846 | DownloadMap::iterator it = downloads_.find(download_handle); |
| 847 | if (it == downloads_.end()) { |
| 848 | NOTREACHED(); |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | DownloadItem* download = it->second; |
| 853 | // 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] | 854 | if (success) { |
| 855 | // We need to update the path uniquifier so that the UI shows the right |
| 856 | // name when calling GetFileName(). |
| 857 | download->set_path_uniquifier(new_path_uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 858 | RenameDownload(download, new_path); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 859 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 860 | |
| 861 | // Continue the download finished sequence. |
| 862 | ContinueDownloadFinished(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | // static |
| 866 | // We have to tell the ResourceDispatcherHost to cancel the download from this |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 867 | // thread, since we can't forward tasks from the file thread to the IO thread |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 868 | // reliably (crash on shutdown race condition). |
| 869 | void DownloadManager::CancelDownloadRequest(int render_process_id, |
| 870 | int request_id) { |
| 871 | ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 872 | base::Thread* io_thread = g_browser_process->io_thread(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 873 | if (!io_thread || !rdh) |
| 874 | return; |
| 875 | io_thread->message_loop()->PostTask(FROM_HERE, |
| 876 | NewRunnableFunction(&DownloadManager::OnCancelDownloadRequest, |
| 877 | rdh, |
| 878 | render_process_id, |
| 879 | request_id)); |
| 880 | } |
| 881 | |
| 882 | // static |
| 883 | void DownloadManager::OnCancelDownloadRequest(ResourceDispatcherHost* rdh, |
| 884 | int render_process_id, |
| 885 | int request_id) { |
| 886 | rdh->CancelRequest(render_process_id, request_id, false); |
| 887 | } |
| 888 | |
| 889 | void DownloadManager::DownloadCancelled(int32 download_id) { |
| 890 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 891 | if (it == in_progress_.end()) |
| 892 | return; |
| 893 | DownloadItem* download = it->second; |
| 894 | |
| 895 | CancelDownloadRequest(download->render_process_id(), download->request_id()); |
| 896 | |
| 897 | // Clean up will happen when the history system create callback runs if we |
| 898 | // don't have a valid db_handle yet. |
| 899 | if (download->db_handle() != kUninitializedHandle) { |
| 900 | in_progress_.erase(it); |
| 901 | NotifyAboutDownloadStop(); |
| 902 | UpdateHistoryForDownload(download); |
| 903 | } |
| 904 | |
| 905 | // Tell the file manager to cancel the download. |
| 906 | file_manager_->RemoveDownload(download->id(), this); // On the UI thread |
| 907 | file_loop_->PostTask(FROM_HERE, |
| 908 | NewRunnableMethod(file_manager_, |
| 909 | &DownloadFileManager::CancelDownload, |
| 910 | download->id())); |
| 911 | } |
| 912 | |
| 913 | void DownloadManager::PauseDownload(int32 download_id, bool pause) { |
| 914 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 915 | if (it != in_progress_.end()) { |
| 916 | DownloadItem* download = it->second; |
| 917 | if (pause == download->is_paused()) |
| 918 | return; |
| 919 | |
| 920 | // Inform the ResourceDispatcherHost of the new pause state. |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 921 | base::Thread* io_thread = g_browser_process->io_thread(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 922 | ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
| 923 | if (!io_thread || !rdh) |
| 924 | return; |
| 925 | |
| 926 | io_thread->message_loop()->PostTask(FROM_HERE, |
| 927 | NewRunnableFunction(&DownloadManager::OnPauseDownloadRequest, |
| 928 | rdh, |
| 929 | download->render_process_id(), |
| 930 | download->request_id(), |
| 931 | pause)); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | // static |
| 936 | void DownloadManager::OnPauseDownloadRequest(ResourceDispatcherHost* rdh, |
| 937 | int render_process_id, |
| 938 | int request_id, |
| 939 | bool pause) { |
| 940 | rdh->PauseRequest(render_process_id, request_id, pause); |
| 941 | } |
| 942 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 943 | bool DownloadManager::IsDangerous(const FilePath& file_name) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 944 | // TODO(jcampan): Improve me. |
| 945 | return IsExecutable(file_util::GetFileExtensionFromPath(file_name)); |
| 946 | } |
| 947 | |
| 948 | void DownloadManager::RenameDownload(DownloadItem* download, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 949 | const FilePath& new_path) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 950 | download->Rename(new_path); |
| 951 | |
| 952 | // Update the history. |
| 953 | |
| 954 | // No update necessary if the download was initiated while in incognito mode. |
| 955 | if (download->db_handle() <= kUninitializedHandle) |
| 956 | return; |
| 957 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 958 | // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 959 | HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 960 | if (hs) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 961 | hs->UpdateDownloadPath(new_path.ToWStringHack(), download->db_handle()); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 962 | } |
| 963 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 964 | void DownloadManager::RemoveDownload(int64 download_handle) { |
| 965 | DownloadMap::iterator it = downloads_.find(download_handle); |
| 966 | if (it == downloads_.end()) |
| 967 | return; |
| 968 | |
| 969 | // Make history update. |
| 970 | DownloadItem* download = it->second; |
| 971 | RemoveDownloadFromHistory(download); |
| 972 | |
| 973 | // Remove from our tables and delete. |
| 974 | downloads_.erase(it); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 975 | it = dangerous_finished_.find(download->id()); |
| 976 | if (it != dangerous_finished_.end()) |
| 977 | dangerous_finished_.erase(it); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 978 | |
| 979 | // Tell observers to refresh their views. |
| 980 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
[email protected] | 6f71287 | 2008-11-07 00:35:36 | [diff] [blame] | 981 | |
| 982 | delete download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | int DownloadManager::RemoveDownloadsBetween(const Time remove_begin, |
| 986 | const Time remove_end) { |
| 987 | RemoveDownloadsFromHistoryBetween(remove_begin, remove_end); |
| 988 | |
| 989 | int num_deleted = 0; |
| 990 | DownloadMap::iterator it = downloads_.begin(); |
| 991 | while (it != downloads_.end()) { |
| 992 | DownloadItem* download = it->second; |
| 993 | DownloadItem::DownloadState state = download->state(); |
| 994 | if (download->start_time() >= remove_begin && |
| 995 | (remove_end.is_null() || download->start_time() < remove_end) && |
| 996 | (state == DownloadItem::COMPLETE || |
| 997 | state == DownloadItem::CANCELLED)) { |
| 998 | // Remove from the map and move to the next in the list. |
| 999 | it = downloads_.erase(it); |
[email protected] | a6604d9 | 2008-10-30 00:58:58 | [diff] [blame] | 1000 | |
| 1001 | // Also remove it from any completed dangerous downloads. |
| 1002 | DownloadMap::iterator dit = dangerous_finished_.find(download->id()); |
| 1003 | if (dit != dangerous_finished_.end()) |
| 1004 | dangerous_finished_.erase(dit); |
| 1005 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1006 | delete download; |
| 1007 | |
| 1008 | ++num_deleted; |
| 1009 | continue; |
| 1010 | } |
| 1011 | |
| 1012 | ++it; |
| 1013 | } |
| 1014 | |
| 1015 | // Tell observers to refresh their views. |
| 1016 | if (num_deleted > 0) |
| 1017 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
| 1018 | |
| 1019 | return num_deleted; |
| 1020 | } |
| 1021 | |
| 1022 | int DownloadManager::RemoveDownloads(const Time remove_begin) { |
| 1023 | return RemoveDownloadsBetween(remove_begin, Time()); |
| 1024 | } |
| 1025 | |
| 1026 | // Initiate a download of a specific URL. We send the request to the |
| 1027 | // ResourceDispatcherHost, and let it send us responses like a regular |
| 1028 | // download. |
| 1029 | void DownloadManager::DownloadUrl(const GURL& url, |
| 1030 | const GURL& referrer, |
| 1031 | WebContents* web_contents) { |
| 1032 | DCHECK(web_contents); |
| 1033 | file_manager_->DownloadUrl(url, |
| 1034 | referrer, |
| 1035 | web_contents->process()->host_id(), |
| 1036 | web_contents->render_view_host()->routing_id(), |
| 1037 | request_context_.get()); |
| 1038 | } |
| 1039 | |
| 1040 | void DownloadManager::NotifyAboutDownloadStart() { |
| 1041 | NotificationService::current()-> |
| 1042 | Notify(NOTIFY_DOWNLOAD_START, NotificationService::AllSources(), |
| 1043 | NotificationService::NoDetails()); |
| 1044 | } |
| 1045 | |
| 1046 | void DownloadManager::NotifyAboutDownloadStop() { |
| 1047 | NotificationService::current()-> |
| 1048 | Notify(NOTIFY_DOWNLOAD_STOP, NotificationService::AllSources(), |
| 1049 | NotificationService::NoDetails()); |
| 1050 | } |
| 1051 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1052 | void DownloadManager::GenerateExtension( |
| 1053 | const FilePath& file_name, |
| 1054 | const std::string& mime_type, |
| 1055 | FilePath::StringType* generated_extension) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1056 | // We're worried about three things here: |
| 1057 | // |
| 1058 | // 1) Security. Many sites let users upload content, such as buddy icons, to |
| 1059 | // their web sites. We want to mitigate the case where an attacker |
| 1060 | // supplies a malicious executable with an executable file extension but an |
| 1061 | // honest site serves the content with a benign content type, such as |
| 1062 | // image/jpeg. |
| 1063 | // |
| 1064 | // 2) Usability. If the site fails to provide a file extension, we want to |
| 1065 | // guess a reasonable file extension based on the content type. |
| 1066 | // |
| 1067 | // 3) Shell integration. Some file extensions automatically integrate with |
| 1068 | // the shell. We block these extensions to prevent a malicious web site |
| 1069 | // from integrating with the user's shell. |
| 1070 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1071 | static const FilePath::CharType default_extension[] = |
| 1072 | FILE_PATH_LITERAL("download"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1073 | |
| 1074 | // See if our file name already contains an extension. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1075 | FilePath::StringType extension( |
| 1076 | file_util::GetFileExtensionFromPath(file_name)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1077 | |
| 1078 | // Rename shell-integrated extensions. |
| 1079 | if (win_util::IsShellIntegratedExtension(extension)) |
| 1080 | extension.assign(default_extension); |
| 1081 | |
| 1082 | std::string mime_type_from_extension; |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1083 | net::GetMimeTypeFromFile(file_name.ToWStringHack(), |
| 1084 | &mime_type_from_extension); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1085 | if (mime_type == mime_type_from_extension) { |
| 1086 | // The hinted extension matches the mime type. It looks like a winner. |
| 1087 | generated_extension->swap(extension); |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | if (IsExecutable(extension) && !IsExecutableMimeType(mime_type)) { |
| 1092 | // We want to be careful about executable extensions. The worry here is |
| 1093 | // that a trusted web site could be tricked into dropping an executable file |
| 1094 | // on the user's filesystem. |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 1095 | if (!net::GetPreferredExtensionForMimeType(mime_type, &extension)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1096 | // We couldn't find a good extension for this content type. Use a dummy |
| 1097 | // extension instead. |
| 1098 | extension.assign(default_extension); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | if (extension.empty()) { |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 1103 | net::GetPreferredExtensionForMimeType(mime_type, &extension); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1104 | } else { |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 1105 | // Append extension generated from the mime type if: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1106 | // 1. New extension is not ".txt" |
| 1107 | // 2. New extension is not the same as the already existing extension. |
| 1108 | // 3. New extension is not executable. This action mitigates the case when |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1109 | // an executable is hidden in a benign file extension; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1110 | // E.g. my-cat.jpg becomes my-cat.jpg.js if content type is |
| 1111 | // application/x-javascript. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1112 | FilePath::StringType append_extension; |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 1113 | if (net::GetPreferredExtensionForMimeType(mime_type, &append_extension)) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1114 | if (append_extension != FILE_PATH_LITERAL(".txt") && |
| 1115 | append_extension != extension && |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1116 | !IsExecutable(append_extension)) |
| 1117 | extension += append_extension; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | generated_extension->swap(extension); |
| 1122 | } |
| 1123 | |
| 1124 | void DownloadManager::GenerateFilename(DownloadCreateInfo* info, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1125 | FilePath* generated_name) { |
| 1126 | *generated_name = FilePath::FromWStringHack( |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 1127 | net::GetSuggestedFilename(GURL(info->url), |
| 1128 | info->content_disposition, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1129 | L"download")); |
| 1130 | DCHECK(!generated_name->empty()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1131 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1132 | GenerateSafeFilename(info->mime_type, generated_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | void DownloadManager::AddObserver(Observer* observer) { |
| 1136 | observers_.AddObserver(observer); |
| 1137 | observer->ModelChanged(); |
| 1138 | } |
| 1139 | |
| 1140 | void DownloadManager::RemoveObserver(Observer* observer) { |
| 1141 | observers_.RemoveObserver(observer); |
| 1142 | } |
| 1143 | |
| 1144 | // Post Windows Shell operations to the Download thread, to avoid blocking the |
| 1145 | // user interface. |
| 1146 | void DownloadManager::ShowDownloadInShell(const DownloadItem* download) { |
| 1147 | DCHECK(file_manager_); |
| 1148 | file_loop_->PostTask(FROM_HERE, |
| 1149 | NewRunnableMethod(file_manager_, |
| 1150 | &DownloadFileManager::OnShowDownloadInShell, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1151 | FilePath(download->full_path()))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | void DownloadManager::OpenDownloadInShell(const DownloadItem* download, |
| 1155 | HWND parent_window) { |
| 1156 | DCHECK(file_manager_); |
| 1157 | file_loop_->PostTask(FROM_HERE, |
| 1158 | NewRunnableMethod(file_manager_, |
| 1159 | &DownloadFileManager::OnOpenDownloadInShell, |
| 1160 | download->full_path(), download->url(), parent_window)); |
| 1161 | } |
| 1162 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1163 | void DownloadManager::OpenFilesOfExtension( |
| 1164 | const FilePath::StringType& extension, bool open) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1165 | if (open && !IsExecutable(extension)) |
| 1166 | auto_open_.insert(extension); |
| 1167 | else |
| 1168 | auto_open_.erase(extension); |
| 1169 | SaveAutoOpens(); |
| 1170 | } |
| 1171 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1172 | bool DownloadManager::ShouldOpenFileExtension( |
| 1173 | const FilePath::StringType& extension) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1174 | if (!IsExecutable(extension) && |
| 1175 | auto_open_.find(extension) != auto_open_.end()) |
| 1176 | return true; |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1180 | static const char* kExecutableWhiteList[] = { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1181 | // JavaScript is just as powerful as EXE. |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1182 | "text/javascript", |
| 1183 | "text/javascript;version=*", |
[email protected] | 60ff8f91 | 2008-12-05 07:58:39 | [diff] [blame] | 1184 | // Some sites use binary/octet-stream to mean application/octet-stream. |
| 1185 | // See http://code.google.com/p/chromium/issues/detail?id=1573 |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1186 | "binary/octet-stream" |
| 1187 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1188 | |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1189 | static const char* kExecutableBlackList[] = { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1190 | // These application types are not executable. |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1191 | "application/*+xml", |
| 1192 | "application/xml" |
| 1193 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1194 | |
[email protected] | 7b73d99 | 2008-12-15 20:56:46 | [diff] [blame] | 1195 | // static |
| 1196 | bool DownloadManager::IsExecutableMimeType(const std::string& mime_type) { |
| 1197 | for (int i=0; i < arraysize(kExecutableWhiteList); ++i) { |
| 1198 | if (net::MatchesMimeType(kExecutableWhiteList[i], mime_type)) |
| 1199 | return true; |
| 1200 | } |
| 1201 | for (int i=0; i < arraysize(kExecutableBlackList); ++i) { |
| 1202 | if (net::MatchesMimeType(kExecutableBlackList[i], mime_type)) |
| 1203 | return false; |
| 1204 | } |
| 1205 | // We consider only other application types to be executable. |
| 1206 | return net::MatchesMimeType("application/*", mime_type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1207 | } |
| 1208 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1209 | bool DownloadManager::IsExecutable(const FilePath::StringType& extension) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1210 | return exe_types_.find(extension) != exe_types_.end(); |
| 1211 | } |
| 1212 | |
| 1213 | void DownloadManager::ResetAutoOpenFiles() { |
| 1214 | auto_open_.clear(); |
| 1215 | SaveAutoOpens(); |
| 1216 | } |
| 1217 | |
| 1218 | bool DownloadManager::HasAutoOpenFileTypesRegistered() const { |
| 1219 | return !auto_open_.empty(); |
| 1220 | } |
| 1221 | |
| 1222 | void DownloadManager::SaveAutoOpens() { |
| 1223 | PrefService* prefs = profile_->GetPrefs(); |
| 1224 | if (prefs) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1225 | FilePath::StringType extensions; |
| 1226 | for (std::set<FilePath::StringType>::iterator it = auto_open_.begin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1227 | it != auto_open_.end(); ++it) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1228 | extensions += *it + FILE_PATH_LITERAL(":"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1229 | } |
| 1230 | if (!extensions.empty()) |
| 1231 | extensions.erase(extensions.size() - 1); |
| 1232 | prefs->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
| 1233 | } |
| 1234 | } |
| 1235 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1236 | void DownloadManager::FileSelected(const std::wstring& path_string, |
| 1237 | void* params) { |
| 1238 | FilePath path = FilePath::FromWStringHack(path_string); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1239 | DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 1240 | if (info->save_as) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1241 | last_download_path_ = path.DirName(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1242 | ContinueStartDownload(info, path); |
| 1243 | } |
| 1244 | |
| 1245 | void DownloadManager::FileSelectionCanceled(void* params) { |
| 1246 | // The user didn't pick a place to save the file, so need to cancel the |
| 1247 | // download that's already in progress to the temporary location. |
| 1248 | DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); |
| 1249 | file_loop_->PostTask(FROM_HERE, |
| 1250 | NewRunnableMethod(file_manager_, &DownloadFileManager::CancelDownload, |
| 1251 | info->download_id)); |
| 1252 | } |
| 1253 | |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1254 | void DownloadManager::DeleteDownload(const FilePath& path) { |
| 1255 | file_loop_->PostTask(FROM_HERE, NewRunnableFunction( |
| 1256 | &DownloadFileManager::DeleteFile, FilePath(path))); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | |
| 1260 | void DownloadManager::DangerousDownloadValidated(DownloadItem* download) { |
| 1261 | DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state()); |
| 1262 | download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED); |
| 1263 | download->UpdateObservers(); |
| 1264 | |
| 1265 | // If the download is not complete, nothing to do. The required |
| 1266 | // post-processing will be performed when it does complete. |
| 1267 | if (download->state() != DownloadItem::COMPLETE) |
| 1268 | return; |
| 1269 | |
| 1270 | file_loop_->PostTask(FROM_HERE, |
| 1271 | NewRunnableMethod(this, |
| 1272 | &DownloadManager::ProceedWithFinishedDangerousDownload, |
| 1273 | download->db_handle(), download->full_path(), |
| 1274 | download->original_name())); |
| 1275 | } |
| 1276 | |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1277 | void DownloadManager::GenerateSafeFilename(const std::string& mime_type, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1278 | FilePath* file_name) { |
| 1279 | // Make sure we get the right file extension |
| 1280 | FilePath::StringType extension; |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1281 | GenerateExtension(*file_name, mime_type, &extension); |
| 1282 | file_util::ReplaceExtension(file_name, extension); |
| 1283 | |
| 1284 | // Prepend "_" to the file name if it's a reserved name |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1285 | FilePath::StringType leaf_name = file_name->BaseName().value(); |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1286 | DCHECK(!leaf_name.empty()); |
| 1287 | if (win_util::IsReservedName(leaf_name)) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1288 | leaf_name = FilePath::StringType(FILE_PATH_LITERAL("_")) + leaf_name; |
| 1289 | *file_name = file_name->DirName(); |
| 1290 | if (file_name->value() == FilePath::kCurrentDirectory) { |
| 1291 | *file_name = FilePath(leaf_name); |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1292 | } else { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1293 | *file_name = file_name->Append(leaf_name); |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1298 | // Operations posted to us from the history service ---------------------------- |
| 1299 | |
| 1300 | // The history service has retrieved all download entries. 'entries' contains |
| 1301 | // 'DownloadCreateInfo's in sorted order (by ascending start_time). |
| 1302 | void DownloadManager::OnQueryDownloadEntriesComplete( |
| 1303 | std::vector<DownloadCreateInfo>* entries) { |
| 1304 | for (size_t i = 0; i < entries->size(); ++i) { |
| 1305 | DownloadItem* download = new DownloadItem(entries->at(i)); |
| 1306 | DCHECK(downloads_.find(download->db_handle()) == downloads_.end()); |
| 1307 | downloads_[download->db_handle()] = download; |
| 1308 | download->set_manager(this); |
| 1309 | } |
| 1310 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | // Once the new DownloadItem's creation info has been committed to the history |
| 1315 | // service, we associate the DownloadItem with the db handle, update our |
| 1316 | // 'downloads_' map and inform observers. |
| 1317 | void DownloadManager::OnCreateDownloadEntryComplete(DownloadCreateInfo info, |
| 1318 | int64 db_handle) { |
| 1319 | DownloadMap::iterator it = in_progress_.find(info.download_id); |
| 1320 | DCHECK(it != in_progress_.end()); |
| 1321 | |
| 1322 | DownloadItem* download = it->second; |
| 1323 | DCHECK(download->db_handle() == kUninitializedHandle); |
| 1324 | download->set_db_handle(db_handle); |
| 1325 | |
| 1326 | // Insert into our full map. |
| 1327 | DCHECK(downloads_.find(download->db_handle()) == downloads_.end()); |
| 1328 | downloads_[download->db_handle()] = download; |
| 1329 | |
| 1330 | // The 'contents' may no longer exist if the user closed the tab before we get |
| 1331 | // this start completion event. If it does, tell the origin WebContents to |
| 1332 | // display its download shelf. |
| 1333 | TabContents* contents = |
[email protected] | a3a1d14 | 2008-12-19 00:42:30 | [diff] [blame] | 1334 | tab_util::GetWebContentsByID(info.render_process_id, info.render_view_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1335 | |
| 1336 | // If the contents no longer exists or is no longer active, we start the |
| 1337 | // download in the last active browser. This is not ideal but better than |
| 1338 | // fully hiding the download from the user. Note: non active means that the |
| 1339 | // user navigated away from the tab contents. This has nothing to do with |
| 1340 | // tab selection. |
| 1341 | if (!contents || !contents->is_active()) { |
| 1342 | Browser* last_active = BrowserList::GetLastActive(); |
| 1343 | if (last_active) |
| 1344 | contents = last_active->GetSelectedTabContents(); |
| 1345 | } |
| 1346 | |
| 1347 | if (contents) |
| 1348 | contents->OnStartDownload(download); |
| 1349 | |
| 1350 | // Inform interested objects about the new download. |
| 1351 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
| 1352 | NotifyAboutDownloadStart(); |
| 1353 | |
| 1354 | // If this download has been completed before we've received the db handle, |
| 1355 | // post one final message to the history service so that it can be properly |
| 1356 | // in sync with the DownloadItem's completion status, and also inform any |
| 1357 | // observers so that they get more than just the start notification. |
| 1358 | if (download->state() != DownloadItem::IN_PROGRESS) { |
| 1359 | in_progress_.erase(it); |
| 1360 | NotifyAboutDownloadStop(); |
| 1361 | UpdateHistoryForDownload(download); |
| 1362 | download->UpdateObservers(); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | // Called when the history service has retrieved the list of downloads that |
| 1367 | // match the search text. |
| 1368 | void DownloadManager::OnSearchComplete(HistoryService::Handle handle, |
| 1369 | std::vector<int64>* results) { |
| 1370 | HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 1371 | Observer* requestor = cancelable_consumer_.GetClientData(hs, handle); |
| 1372 | if (!requestor) |
| 1373 | return; |
| 1374 | |
| 1375 | std::vector<DownloadItem*> searched_downloads; |
| 1376 | for (std::vector<int64>::iterator it = results->begin(); |
| 1377 | it != results->end(); ++it) { |
| 1378 | DownloadMap::iterator dit = downloads_.find(*it); |
| 1379 | if (dit != downloads_.end()) |
| 1380 | searched_downloads.push_back(dit->second); |
| 1381 | } |
| 1382 | |
| 1383 | requestor->SetDownloads(searched_downloads); |
| 1384 | } |
[email protected] | 905a08d | 2008-11-19 07:24:12 | [diff] [blame] | 1385 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 1386 | // Clears the last download path, used to initialize "save as" dialogs. |
[email protected] | 905a08d | 2008-11-19 07:24:12 | [diff] [blame] | 1387 | void DownloadManager::ClearLastDownloadPath() { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame^] | 1388 | last_download_path_ = FilePath(); |
[email protected] | 905a08d | 2008-11-19 07:24:12 | [diff] [blame] | 1389 | } |