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