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