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