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