blob: e9ead6ca4295a92e7035d3d22d10246331adf73b [file] [log] [blame]
[email protected]21ca982c2010-01-26 22:49:551// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]cdaa8652008-09-13 02:48:595#include "chrome/browser/download/download_manager.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]21ca982c2010-01-26 22:49:558#include "app/resource_bundle.h"
[email protected]2041cf342010-02-19 03:15:599#include "base/callback.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/file_util.h"
11#include "base/logging.h"
12#include "base/message_loop.h"
13#include "base/path_service.h"
[email protected]1b5044d2009-02-24 00:04:1414#include "base/rand_util.h"
[email protected]807204142009-05-05 03:31:4415#include "base/stl_util-inl.h"
initial.commit09911bf2008-07-26 23:55:2916#include "base/string_util.h"
[email protected]1b5044d2009-02-24 00:04:1417#include "base/sys_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/task.h"
19#include "base/thread.h"
20#include "base/timer.h"
[email protected]d2a8fb72010-01-21 05:31:4221#include "build/build_config.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/browser/browser_list.h"
23#include "chrome/browser/browser_process.h"
[email protected]d83d03aa2009-11-02 21:44:3724#include "chrome/browser/chrome_thread.h"
[email protected]cdaa8652008-09-13 02:48:5925#include "chrome/browser/download/download_file.h"
[email protected]e9ef0a62009-08-11 22:50:1326#include "chrome/browser/download/download_util.h"
[email protected]866930682009-08-18 22:53:4727#include "chrome/browser/extensions/crx_installer.h"
[email protected]2a464a92009-08-01 17:58:3528#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]8f783752009-04-01 23:33:4529#include "chrome/browser/extensions/extensions_service.h"
[email protected]be180c802009-10-23 06:33:3130#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]052313b2010-02-19 09:43:0831#include "chrome/browser/pref_service.h"
initial.commit09911bf2008-07-26 23:55:2932#include "chrome/browser/profile.h"
[email protected]8c8657d62009-01-16 18:31:2633#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2534#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]e3c404b2008-12-23 01:07:3235#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
[email protected]21ca982c2010-01-26 22:49:5536#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3437#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]d2a8fb72010-01-21 05:31:4238#include "chrome/browser/tab_contents/tab_util.h"
[email protected]4a0380c2009-07-26 07:25:3239#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2940#include "chrome/common/chrome_paths.h"
[email protected]5b1a0e22009-05-26 19:00:5841#include "chrome/common/extensions/extension.h"
[email protected]6657afa62009-11-04 02:15:2042#include "chrome/common/extensions/user_script.h"
[email protected]91e1bd82009-09-03 22:04:4043#include "chrome/common/notification_service.h"
44#include "chrome/common/notification_type.h"
[email protected]076700e62009-04-01 18:41:2345#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2946#include "chrome/common/pref_names.h"
[email protected]46072d42008-07-28 14:49:3547#include "googleurl/src/gurl.h"
[email protected]d81706b82009-04-03 20:28:4448#include "grit/chromium_strings.h"
[email protected]34ac8f32009-02-22 23:03:2749#include "grit/generated_resources.h"
[email protected]21ca982c2010-01-26 22:49:5550#include "grit/theme_resources.h"
initial.commit09911bf2008-07-26 23:55:2951#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]b7f05882009-02-22 01:21:5655#if defined(OS_WIN)
[email protected]4a0765a2009-05-08 23:12:2556#include "app/win_util.h"
[email protected]b7f05882009-02-22 01:21:5657#include "base/registry.h"
58#include "base/win_util.h"
[email protected]a0a9577b2009-05-27 23:52:3259#endif
60
[email protected]21ca982c2010-01-26 22:49:5561namespace {
62
initial.commit09911bf2008-07-26 23:55:2963// Periodically update our observers.
64class 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]21ca982c2010-01-26 22:49:5574const int kUpdateTimeMs = 1000;
initial.commit09911bf2008-07-26 23:55:2975
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]6cade212008-12-03 00:32:2278// database handles in incognito mode starting at -1 and progressively getting
79// more negative.
[email protected]21ca982c2010-01-26 22:49:5580const int kUninitializedHandle = 0;
initial.commit09911bf2008-07-26 23:55:2981
[email protected]7a256ea2008-10-17 17:34:1682// Appends the passed the number between parenthesis the path before the
83// extension.
[email protected]21ca982c2010-01-26 22:49:5584void AppendNumberToPath(FilePath* path, int number) {
[email protected]7ae7c2cb2009-01-06 23:31:4185 file_util::InsertBeforeExtension(path,
86 StringPrintf(FILE_PATH_LITERAL(" (%d)"), number));
[email protected]7a256ea2008-10-17 17:34:1687}
88
89// Attempts to find a number that can be appended to that path to make it
90// unique. If |path| does not exist, 0 is returned. If it fails to find such
91// a number, -1 is returned.
[email protected]21ca982c2010-01-26 22:49:5592int GetUniquePathNumber(const FilePath& path) {
initial.commit09911bf2008-07-26 23:55:2993 const int kMaxAttempts = 100;
94
[email protected]7a256ea2008-10-17 17:34:1695 if (!file_util::PathExists(path))
96 return 0;
initial.commit09911bf2008-07-26 23:55:2997
[email protected]7ae7c2cb2009-01-06 23:31:4198 FilePath new_path;
initial.commit09911bf2008-07-26 23:55:2999 for (int count = 1; count <= kMaxAttempts; ++count) {
[email protected]7ae7c2cb2009-01-06 23:31:41100 new_path = FilePath(path);
[email protected]7a256ea2008-10-17 17:34:16101 AppendNumberToPath(&new_path, count);
initial.commit09911bf2008-07-26 23:55:29102
[email protected]7a256ea2008-10-17 17:34:16103 if (!file_util::PathExists(new_path))
104 return count;
initial.commit09911bf2008-07-26 23:55:29105 }
106
[email protected]7a256ea2008-10-17 17:34:16107 return -1;
initial.commit09911bf2008-07-26 23:55:29108}
109
[email protected]b0ab1d42010-02-24 19:29:28110// Used to sort download items based on descending start time.
111bool CompareStartTime(DownloadItem* first, DownloadItem* second) {
112 return first->start_time() > second->start_time();
113}
114
[email protected]21ca982c2010-01-26 22:49:55115} // namespace
116
initial.commit09911bf2008-07-26 23:55:29117// DownloadItem implementation -------------------------------------------------
118
119// Constructor for reading from the history service.
120DownloadItem::DownloadItem(const DownloadCreateInfo& info)
121 : id_(-1),
122 full_path_(info.path),
123 url_(info.url),
[email protected]e435d6b72009-07-25 03:15:58124 referrer_url_(info.referrer_url),
125 mime_type_(info.mime_type),
initial.commit09911bf2008-07-26 23:55:29126 total_bytes_(info.total_bytes),
127 received_bytes_(info.received_bytes),
[email protected]b7f05882009-02-22 01:21:56128 start_tick_(base::TimeTicks()),
initial.commit09911bf2008-07-26 23:55:29129 state_(static_cast<DownloadState>(info.state)),
130 start_time_(info.start_time),
131 db_handle_(info.db_handle),
initial.commit09911bf2008-07-26 23:55:29132 manager_(NULL),
133 is_paused_(false),
134 open_when_complete_(false),
[email protected]b7f05882009-02-22 01:21:56135 safety_state_(SAFE),
[email protected]0aad67b2009-07-15 20:34:28136 auto_opened_(false),
[email protected]b7f05882009-02-22 01:21:56137 original_name_(info.original_name),
initial.commit09911bf2008-07-26 23:55:29138 render_process_id_(-1),
[email protected]6aa4a1c02010-01-15 18:49:58139 request_id_(-1),
140 save_as_(false),
[email protected]b0ab1d42010-02-24 19:29:28141 is_otr_(false),
[email protected]5748eb82010-01-20 15:12:37142 is_extension_install_(info.is_extension_install),
[email protected]6aa4a1c02010-01-15 18:49:58143 name_finalized_(false),
144 is_temporary_(false) {
initial.commit09911bf2008-07-26 23:55:29145 if (state_ == IN_PROGRESS)
146 state_ = CANCELLED;
147 Init(false /* don't start progress timer */);
148}
149
150// Constructor for DownloadItem created via user action in the main thread.
151DownloadItem::DownloadItem(int32 download_id,
[email protected]7ae7c2cb2009-01-06 23:31:41152 const FilePath& path,
[email protected]7a256ea2008-10-17 17:34:16153 int path_uniquifier,
[email protected]f6b48532009-02-12 01:56:32154 const GURL& url,
[email protected]494c06e2009-07-25 01:06:42155 const GURL& referrer_url,
[email protected]e435d6b72009-07-25 03:15:58156 const std::string& mime_type,
[email protected]7ae7c2cb2009-01-06 23:31:41157 const FilePath& original_name,
[email protected]e93d2822009-01-30 05:59:59158 const base::Time start_time,
initial.commit09911bf2008-07-26 23:55:29159 int64 download_size,
160 int render_process_id,
[email protected]9ccbb372008-10-10 18:50:32161 int request_id,
[email protected]67f373a2009-09-22 02:44:51162 bool is_dangerous,
[email protected]a60c8ae2009-12-25 06:50:57163 bool save_as,
[email protected]b0ab1d42010-02-24 19:29:28164 bool is_otr,
[email protected]6aa4a1c02010-01-15 18:49:58165 bool is_extension_install,
166 bool is_temporary)
initial.commit09911bf2008-07-26 23:55:29167 : id_(download_id),
168 full_path_(path),
[email protected]7a256ea2008-10-17 17:34:16169 path_uniquifier_(path_uniquifier),
initial.commit09911bf2008-07-26 23:55:29170 url_(url),
[email protected]494c06e2009-07-25 01:06:42171 referrer_url_(referrer_url),
[email protected]e435d6b72009-07-25 03:15:58172 mime_type_(mime_type),
initial.commit09911bf2008-07-26 23:55:29173 total_bytes_(download_size),
174 received_bytes_(0),
[email protected]b7f05882009-02-22 01:21:56175 start_tick_(base::TimeTicks::Now()),
initial.commit09911bf2008-07-26 23:55:29176 state_(IN_PROGRESS),
177 start_time_(start_time),
178 db_handle_(kUninitializedHandle),
initial.commit09911bf2008-07-26 23:55:29179 manager_(NULL),
180 is_paused_(false),
181 open_when_complete_(false),
[email protected]b7f05882009-02-22 01:21:56182 safety_state_(is_dangerous ? DANGEROUS : SAFE),
[email protected]0aad67b2009-07-15 20:34:28183 auto_opened_(false),
[email protected]b7f05882009-02-22 01:21:56184 original_name_(original_name),
initial.commit09911bf2008-07-26 23:55:29185 render_process_id_(render_process_id),
[email protected]67f373a2009-09-22 02:44:51186 request_id_(request_id),
[email protected]a60c8ae2009-12-25 06:50:57187 save_as_(save_as),
[email protected]b0ab1d42010-02-24 19:29:28188 is_otr_(is_otr),
[email protected]6aa4a1c02010-01-15 18:49:58189 is_extension_install_(is_extension_install),
190 name_finalized_(false),
191 is_temporary_(is_temporary) {
initial.commit09911bf2008-07-26 23:55:29192 Init(true /* start progress timer */);
193}
194
195void DownloadItem::Init(bool start_timer) {
[email protected]7ae7c2cb2009-01-06 23:31:41196 file_name_ = full_path_.BaseName();
initial.commit09911bf2008-07-26 23:55:29197 if (start_timer)
198 StartProgressTimer();
199}
200
201DownloadItem::~DownloadItem() {
initial.commit09911bf2008-07-26 23:55:29202 state_ = REMOVING;
203 UpdateObservers();
204}
205
206void DownloadItem::AddObserver(Observer* observer) {
207 observers_.AddObserver(observer);
208}
209
210void DownloadItem::RemoveObserver(Observer* observer) {
211 observers_.RemoveObserver(observer);
212}
213
214void DownloadItem::UpdateObservers() {
215 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this));
216}
217
[email protected]6aa4a1c02010-01-15 18:49:58218void DownloadItem::NotifyObserversDownloadFileCompleted() {
219 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadFileCompleted(this));
220}
221
[email protected]45e3c122009-04-07 19:58:03222void DownloadItem::NotifyObserversDownloadOpened() {
223 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this));
224}
225
initial.commit09911bf2008-07-26 23:55:29226// If we've received more data than we were expecting (bad server info?), revert
227// to 'unknown size mode'.
228void DownloadItem::UpdateSize(int64 bytes_so_far) {
229 received_bytes_ = bytes_so_far;
230 if (received_bytes_ > total_bytes_)
231 total_bytes_ = 0;
232}
233
234// Updates from the download thread may have been posted while this download
235// was being cancelled in the UI thread, so we'll accept them unless we're
236// complete.
237void DownloadItem::Update(int64 bytes_so_far) {
238 if (state_ == COMPLETE) {
239 NOTREACHED();
240 return;
241 }
242 UpdateSize(bytes_so_far);
243 UpdateObservers();
244}
245
[email protected]6cade212008-12-03 00:32:22246// Triggered by a user action.
initial.commit09911bf2008-07-26 23:55:29247void DownloadItem::Cancel(bool update_history) {
248 if (state_ != IN_PROGRESS) {
249 // Small downloads might be complete before this method has a chance to run.
250 return;
251 }
252 state_ = CANCELLED;
253 UpdateObservers();
254 StopProgressTimer();
255 if (update_history)
256 manager_->DownloadCancelled(id_);
257}
258
259void DownloadItem::Finished(int64 size) {
260 state_ = COMPLETE;
261 UpdateSize(size);
initial.commit09911bf2008-07-26 23:55:29262 StopProgressTimer();
263}
264
[email protected]9ccbb372008-10-10 18:50:32265void DownloadItem::Remove(bool delete_on_disk) {
initial.commit09911bf2008-07-26 23:55:29266 Cancel(true);
267 state_ = REMOVING;
[email protected]9ccbb372008-10-10 18:50:32268 if (delete_on_disk)
269 manager_->DeleteDownload(full_path_);
initial.commit09911bf2008-07-26 23:55:29270 manager_->RemoveDownload(db_handle_);
[email protected]6cade212008-12-03 00:32:22271 // We have now been deleted.
initial.commit09911bf2008-07-26 23:55:29272}
273
274void DownloadItem::StartProgressTimer() {
[email protected]e93d2822009-01-30 05:59:59275 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this,
[email protected]2d316662008-09-03 18:18:14276 &DownloadItem::UpdateObservers);
initial.commit09911bf2008-07-26 23:55:29277}
278
279void DownloadItem::StopProgressTimer() {
[email protected]2d316662008-09-03 18:18:14280 update_timer_.Stop();
initial.commit09911bf2008-07-26 23:55:29281}
282
[email protected]e93d2822009-01-30 05:59:59283bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const {
initial.commit09911bf2008-07-26 23:55:29284 if (total_bytes_ <= 0)
285 return false; // We never received the content_length for this download.
286
287 int64 speed = CurrentSpeed();
288 if (speed == 0)
289 return false;
290
291 *remaining =
[email protected]e93d2822009-01-30 05:59:59292 base::TimeDelta::FromSeconds((total_bytes_ - received_bytes_) / speed);
initial.commit09911bf2008-07-26 23:55:29293 return true;
294}
295
296int64 DownloadItem::CurrentSpeed() const {
[email protected]b7f05882009-02-22 01:21:56297 base::TimeDelta diff = base::TimeTicks::Now() - start_tick_;
298 int64 diff_ms = diff.InMilliseconds();
299 return diff_ms == 0 ? 0 : received_bytes_ * 1000 / diff_ms;
initial.commit09911bf2008-07-26 23:55:29300}
301
302int DownloadItem::PercentComplete() const {
303 int percent = -1;
304 if (total_bytes_ > 0)
305 percent = static_cast<int>(received_bytes_ * 100.0 / total_bytes_);
306 return percent;
307}
308
[email protected]7ae7c2cb2009-01-06 23:31:41309void DownloadItem::Rename(const FilePath& full_path) {
initial.commit09911bf2008-07-26 23:55:29310 DCHECK(!full_path.empty());
311 full_path_ = full_path;
[email protected]7ae7c2cb2009-01-06 23:31:41312 file_name_ = full_path_.BaseName();
initial.commit09911bf2008-07-26 23:55:29313}
314
315void DownloadItem::TogglePause() {
316 DCHECK(state_ == IN_PROGRESS);
317 manager_->PauseDownload(id_, !is_paused_);
318 is_paused_ = !is_paused_;
319 UpdateObservers();
320}
321
[email protected]7ae7c2cb2009-01-06 23:31:41322FilePath DownloadItem::GetFileName() const {
[email protected]9ccbb372008-10-10 18:50:32323 if (safety_state_ == DownloadItem::SAFE)
324 return file_name_;
[email protected]7a256ea2008-10-17 17:34:16325 if (path_uniquifier_ > 0) {
[email protected]7ae7c2cb2009-01-06 23:31:41326 FilePath name(original_name_);
[email protected]7a256ea2008-10-17 17:34:16327 AppendNumberToPath(&name, path_uniquifier_);
328 return name;
329 }
[email protected]9ccbb372008-10-10 18:50:32330 return original_name_;
331}
332
initial.commit09911bf2008-07-26 23:55:29333// DownloadManager implementation ----------------------------------------------
334
335// static
336void DownloadManager::RegisterUserPrefs(PrefService* prefs) {
337 prefs->RegisterBooleanPref(prefs::kPromptForDownload, false);
338 prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen, L"");
[email protected]f052118e2008-09-05 02:25:32339 prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false);
340
341 // The default download path is userprofile\download.
[email protected]290c9702010-03-09 04:01:36342 const FilePath& default_download_path =
343 download_util::GetDefaultDownloadDirectory();
[email protected]b9636002009-03-04 00:05:25344 prefs->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
345 default_download_path);
[email protected]f052118e2008-09-05 02:25:32346
347 // If the download path is dangerous we forcefully reset it. But if we do
348 // so we set a flag to make sure we only do it once, to avoid fighting
349 // the user if he really wants it on an unsafe place such as the desktop.
350
351 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) {
[email protected]7ae7c2cb2009-01-06 23:31:41352 FilePath current_download_dir = FilePath::FromWStringHack(
353 prefs->GetString(prefs::kDownloadDefaultDirectory));
[email protected]290c9702010-03-09 04:01:36354 if (download_util::DownloadPathIsDangerous(current_download_dir)) {
[email protected]f052118e2008-09-05 02:25:32355 prefs->SetString(prefs::kDownloadDefaultDirectory,
[email protected]7ae7c2cb2009-01-06 23:31:41356 default_download_path.ToWStringHack());
[email protected]f052118e2008-09-05 02:25:32357 }
358 prefs->SetBoolean(prefs::kDownloadDirUpgraded, true);
359 }
initial.commit09911bf2008-07-26 23:55:29360}
361
362DownloadManager::DownloadManager()
363 : shutdown_needed_(false),
364 profile_(NULL),
[email protected]d2a8fb72010-01-21 05:31:42365 file_manager_(NULL),
366 fake_db_handle_(kUninitializedHandle - 1) {
initial.commit09911bf2008-07-26 23:55:29367}
368
369DownloadManager::~DownloadManager() {
[email protected]b0ab1d42010-02-24 19:29:28370 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
371
initial.commit09911bf2008-07-26 23:55:29372 if (shutdown_needed_)
373 Shutdown();
374}
375
376void DownloadManager::Shutdown() {
377 DCHECK(shutdown_needed_) << "Shutdown called when not needed.";
378
379 // Stop receiving download updates
380 file_manager_->RemoveDownloadManager(this);
381
382 // Stop making history service requests
383 cancelable_consumer_.CancelAllRequests();
384
385 // 'in_progress_' may contain DownloadItems that have not finished the start
386 // complete (from the history service) and thus aren't in downloads_.
387 DownloadMap::iterator it = in_progress_.begin();
[email protected]9ccbb372008-10-10 18:50:32388 std::set<DownloadItem*> to_remove;
initial.commit09911bf2008-07-26 23:55:29389 for (; it != in_progress_.end(); ++it) {
390 DownloadItem* download = it->second;
[email protected]9ccbb372008-10-10 18:50:32391 if (download->safety_state() == DownloadItem::DANGEROUS) {
392 // Forget about any download that the user did not approve.
393 // Note that we cannot call download->Remove() this would invalidate our
394 // iterator.
395 to_remove.insert(download);
396 continue;
initial.commit09911bf2008-07-26 23:55:29397 }
[email protected]9ccbb372008-10-10 18:50:32398 DCHECK_EQ(DownloadItem::IN_PROGRESS, download->state());
399 download->Cancel(false);
400 UpdateHistoryForDownload(download);
initial.commit09911bf2008-07-26 23:55:29401 if (download->db_handle() == kUninitializedHandle) {
402 // An invalid handle means that 'download' does not yet exist in
403 // 'downloads_', so we have to delete it here.
404 delete download;
405 }
406 }
407
[email protected]9ccbb372008-10-10 18:50:32408 // 'dangerous_finished_' contains all complete downloads that have not been
409 // approved. They should be removed.
410 it = dangerous_finished_.begin();
411 for (; it != dangerous_finished_.end(); ++it)
412 to_remove.insert(it->second);
413
414 // Remove the dangerous download that are not approved.
415 for (std::set<DownloadItem*>::const_iterator rm_it = to_remove.begin();
416 rm_it != to_remove.end(); ++rm_it) {
417 DownloadItem* download = *rm_it;
[email protected]e10e17c72008-10-15 17:48:32418 int64 handle = download->db_handle();
[email protected]9ccbb372008-10-10 18:50:32419 download->Remove(true);
[email protected]e10e17c72008-10-15 17:48:32420 // Same as above, delete the download if it is not in 'downloads_' (as the
421 // Remove() call above won't have deleted it).
422 if (handle == kUninitializedHandle)
[email protected]9ccbb372008-10-10 18:50:32423 delete download;
424 }
425 to_remove.clear();
426
initial.commit09911bf2008-07-26 23:55:29427 in_progress_.clear();
[email protected]9ccbb372008-10-10 18:50:32428 dangerous_finished_.clear();
initial.commit09911bf2008-07-26 23:55:29429 STLDeleteValues(&downloads_);
430
431 file_manager_ = NULL;
432
433 // Save our file extensions to auto open.
434 SaveAutoOpens();
435
436 // Make sure the save as dialog doesn't notify us back if we're gone before
437 // it returns.
438 if (select_file_dialog_.get())
439 select_file_dialog_->ListenerDestroyed();
440
441 shutdown_needed_ = false;
442}
443
444// Issue a history query for downloads matching 'search_text'. If 'search_text'
445// is empty, return all downloads that we know about.
446void DownloadManager::GetDownloads(Observer* observer,
447 const std::wstring& search_text) {
[email protected]b0ab1d42010-02-24 19:29:28448 std::vector<DownloadItem*> otr_downloads;
449
450 if (profile_->IsOffTheRecord() && search_text.empty()) {
451 // List all incognito downloads and add that to the downloads the parent
452 // profile lists.
453 otr_downloads.reserve(downloads_.size());
454 for (DownloadMap::iterator it = downloads_.begin();
455 it != downloads_.end(); ++it) {
456 DownloadItem* download = it->second;
457 if (download->is_otr() && !download->is_extension_install() &&
458 !download->is_temporary()) {
459 otr_downloads.push_back(download);
460 }
461 }
462 }
463
464 profile_->GetOriginalProfile()->GetDownloadManager()->
465 DoGetDownloads(observer, search_text, otr_downloads);
466}
467
468void DownloadManager::DoGetDownloads(
469 Observer* observer,
470 const std::wstring& search_text,
471 std::vector<DownloadItem*>& otr_downloads) {
initial.commit09911bf2008-07-26 23:55:29472 DCHECK(observer);
473
474 // Return a empty list if we've not yet received the set of downloads from the
475 // history system (we'll update all observers once we get that list in
476 // OnQueryDownloadEntriesComplete), or if there are no downloads at all.
initial.commit09911bf2008-07-26 23:55:29477 if (downloads_.empty()) {
[email protected]b0ab1d42010-02-24 19:29:28478 observer->SetDownloads(otr_downloads);
initial.commit09911bf2008-07-26 23:55:29479 return;
480 }
481
[email protected]b0ab1d42010-02-24 19:29:28482 std::vector<DownloadItem*> download_copy;
initial.commit09911bf2008-07-26 23:55:29483 // We already know all the downloads and there is no filter, so just return a
484 // copy to the observer.
485 if (search_text.empty()) {
486 download_copy.reserve(downloads_.size());
487 for (DownloadMap::iterator it = downloads_.begin();
488 it != downloads_.end(); ++it) {
[email protected]67f373a2009-09-22 02:44:51489 if (it->second->db_handle() > kUninitializedHandle)
490 download_copy.push_back(it->second);
initial.commit09911bf2008-07-26 23:55:29491 }
492
[email protected]b0ab1d42010-02-24 19:29:28493 // Merge sort based on start time.
494 std::vector<DownloadItem*> merged_downloads;
495 std::merge(otr_downloads.begin(), otr_downloads.end(),
496 download_copy.begin(), download_copy.end(),
497 std::back_inserter(merged_downloads),
498 CompareStartTime);
499
initial.commit09911bf2008-07-26 23:55:29500 // We retain ownership of the DownloadItems.
[email protected]b0ab1d42010-02-24 19:29:28501 observer->SetDownloads(merged_downloads);
initial.commit09911bf2008-07-26 23:55:29502 return;
503 }
504
[email protected]b0ab1d42010-02-24 19:29:28505 DCHECK(otr_downloads.empty());
506
initial.commit09911bf2008-07-26 23:55:29507 // Issue a request to the history service for a list of downloads matching
508 // our search text.
509 HistoryService* hs =
510 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
511 if (hs) {
512 HistoryService::Handle h =
513 hs->SearchDownloads(search_text,
514 &cancelable_consumer_,
515 NewCallback(this,
516 &DownloadManager::OnSearchComplete));
517 cancelable_consumer_.SetClientData(hs, h, observer);
518 }
519}
520
[email protected]6aa4a1c02010-01-15 18:49:58521void DownloadManager::GetTemporaryDownloads(Observer* observer,
522 const FilePath& dir_path) {
523 DCHECK(observer);
524
525 std::vector<DownloadItem*> download_copy;
526
527 for (DownloadMap::iterator it = downloads_.begin();
528 it != downloads_.end(); ++it) {
529 if (it->second->is_temporary() &&
530 it->second->full_path().DirName() == dir_path)
531 download_copy.push_back(it->second);
532 }
533
534 observer->SetDownloads(download_copy);
535}
536
[email protected]c4a530b2010-03-08 17:33:03537void DownloadManager::GetCurrentDownloads(Observer* observer,
538 const FilePath& dir_path) {
539 DCHECK(observer);
540
541 std::vector<DownloadItem*> download_copy;
542
543 for (DownloadMap::iterator it = downloads_.begin();
544 it != downloads_.end(); ++it) {
545 if (!it->second->is_temporary() &&
546 (it->second->state() == DownloadItem::IN_PROGRESS ||
547 it->second->safety_state() == DownloadItem::DANGEROUS) &&
548 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
549 download_copy.push_back(it->second);
550 }
551
552 observer->SetDownloads(download_copy);
553}
554
initial.commit09911bf2008-07-26 23:55:29555// Query the history service for information about all persisted downloads.
556bool DownloadManager::Init(Profile* profile) {
557 DCHECK(profile);
558 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
559 shutdown_needed_ = true;
560
561 profile_ = profile;
[email protected]be180c802009-10-23 06:33:31562 request_context_getter_ = profile_->GetRequestContext();
initial.commit09911bf2008-07-26 23:55:29563
564 // 'incognito mode' will have access to past downloads, but we won't store
565 // information about new downloads while in that mode.
566 QueryHistoryForDownloads();
567
568 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
569 if (!rdh) {
570 NOTREACHED();
571 return false;
572 }
573
574 file_manager_ = rdh->download_file_manager();
575 if (!file_manager_) {
576 NOTREACHED();
577 return false;
578 }
579
initial.commit09911bf2008-07-26 23:55:29580 // Get our user preference state.
581 PrefService* prefs = profile_->GetPrefs();
582 DCHECK(prefs);
583 prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL);
584
initial.commit09911bf2008-07-26 23:55:29585 download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL);
586
[email protected]bb69e9b32008-08-14 23:08:14587 // Ensure that the download directory specified in the preferences exists.
[email protected]d83d03aa2009-11-02 21:44:37588 ChromeThread::PostTask(
589 ChromeThread::FILE, FROM_HERE,
[email protected]309b7642009-12-09 03:08:50590 NewRunnableFunction(&file_util::CreateDirectory, download_path()));
initial.commit09911bf2008-07-26 23:55:29591
[email protected]2b2f8f72009-02-24 22:42:05592 // We store any file extension that should be opened automatically at
593 // download completion in this pref.
initial.commit09911bf2008-07-26 23:55:29594 std::wstring extensions_to_open =
595 prefs->GetString(prefs::kDownloadExtensionsToOpen);
596 std::vector<std::wstring> extensions;
597 SplitString(extensions_to_open, L':', &extensions);
598 for (size_t i = 0; i < extensions.size(); ++i) {
[email protected]eccb9d12009-10-28 05:40:09599 if (!extensions[i].empty() && !IsExecutableFile(
600 FilePath::FromWStringHack(extensions[i])))
[email protected]b7f05882009-02-22 01:21:56601 auto_open_.insert(FilePath::FromWStringHack(extensions[i]).value());
initial.commit09911bf2008-07-26 23:55:29602 }
603
[email protected]b0ab1d42010-02-24 19:29:28604 other_download_manager_observer_.reset(
605 new OtherDownloadManagerObserver(this));
606
initial.commit09911bf2008-07-26 23:55:29607 return true;
608}
609
610void DownloadManager::QueryHistoryForDownloads() {
611 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
612 if (hs) {
613 hs->QueryDownloads(
614 &cancelable_consumer_,
615 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete));
616 }
617}
618
619// We have received a message from DownloadFileManager about a new download. We
620// create a download item and store it in our download map, and inform the
621// history system of a new download. Since this method can be called while the
622// history service thread is still reading the persistent state, we do not
623// insert the new DownloadItem into 'downloads_' or inform our observers at this
624// point. OnCreateDatabaseEntryComplete() handles that finalization of the the
625// download creation as a callback from the history thread.
626void DownloadManager::StartDownload(DownloadCreateInfo* info) {
[email protected]d83d03aa2009-11-02 21:44:37627 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
initial.commit09911bf2008-07-26 23:55:29628 DCHECK(info);
629
[email protected]a60c8ae2009-12-25 06:50:57630 // Check whether this download is for an extension install or not.
631 if (!info->save_as) { // Allow extensions to be explicitly saved.
632 if (UserScript::HasUserScriptFileExtension(info->url) ||
633 info->mime_type == Extension::kMimeType)
634 info->is_extension_install = true;
635 }
636
[email protected]7d3851d82008-12-12 03:26:07637 // Freeze the user's preference for showing a Save As dialog. We're going to
638 // bounce around a bunch of threads and we don't want to worry about race
639 // conditions where the user changes this pref out from under us.
[email protected]7bc4f702009-09-25 22:12:38640 if (*prompt_for_download_) {
641 // But never obey the preference for extension installation. Note that we
642 // only care here about the case where an extension is installed, not when
643 // one is downloaded with "save as...".
[email protected]a60c8ae2009-12-25 06:50:57644 if (!info->is_extension_install)
[email protected]7bc4f702009-09-25 22:12:38645 info->save_as = true;
646 }
[email protected]7d3851d82008-12-12 03:26:07647
[email protected]8af9d032010-02-10 00:00:32648 if (info->save_info.file_path.empty()) {
649 // Determine the proper path for a download, by either one of the following:
650 // 1) using the default download directory.
651 // 2) prompting the user.
652 FilePath generated_name;
653 GenerateFileNameFromInfo(info, &generated_name);
654 if (info->save_as && !last_download_path_.empty())
655 info->suggested_path = last_download_path_;
656 else
657 info->suggested_path = download_path();
658 info->suggested_path = info->suggested_path.Append(generated_name);
659 } else {
660 info->suggested_path = info->save_info.file_path;
661 }
initial.commit09911bf2008-07-26 23:55:29662
[email protected]8af9d032010-02-10 00:00:32663 if (!info->save_as && info->save_info.file_path.empty()) {
[email protected]4289d9b2009-07-25 21:17:34664 // Downloads can be marked as dangerous for two reasons:
665 // a) They have a dangerous-looking filename
666 // b) They are an extension that is not from the gallery
667 if (IsDangerous(info->suggested_path.BaseName()))
668 info->is_dangerous = true;
[email protected]a60c8ae2009-12-25 06:50:57669 else if (info->is_extension_install &&
[email protected]b7c2f252009-12-08 00:47:23670 !ExtensionsService::IsDownloadFromGallery(info->url,
671 info->referrer_url)) {
[email protected]4289d9b2009-07-25 21:17:34672 info->is_dangerous = true;
673 }
[email protected]e9ebf3fc2008-10-17 22:06:58674 }
675
initial.commit09911bf2008-07-26 23:55:29676 // We need to move over to the download thread because we don't want to stat
677 // the suggested path on the UI thread.
[email protected]d83d03aa2009-11-02 21:44:37678 ChromeThread::PostTask(
679 ChromeThread::FILE, FROM_HERE,
680 NewRunnableMethod(
681 this, &DownloadManager::CheckIfSuggestedPathExists, info));
initial.commit09911bf2008-07-26 23:55:29682}
683
684void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info) {
685 DCHECK(info);
686
687 // Check writability of the suggested path. If we can't write to it, default
688 // to the user's "My Documents" directory. We'll prompt them in this case.
[email protected]7ae7c2cb2009-01-06 23:31:41689 FilePath dir = info->suggested_path.DirName();
690 FilePath filename = info->suggested_path.BaseName();
[email protected]9ccbb372008-10-10 18:50:32691 if (!file_util::PathIsWritable(dir)) {
initial.commit09911bf2008-07-26 23:55:29692 info->save_as = true;
initial.commit09911bf2008-07-26 23:55:29693 PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path);
[email protected]7ae7c2cb2009-01-06 23:31:41694 info->suggested_path = info->suggested_path.Append(filename);
initial.commit09911bf2008-07-26 23:55:29695 }
696
[email protected]8af9d032010-02-10 00:00:32697 // Do not add the path uniquifier if we are saving to a specific path as in
698 // the drag-out case.
699 if (info->save_info.file_path.empty())
700 info->path_uniquifier = GetUniquePathNumber(info->suggested_path);
initial.commit09911bf2008-07-26 23:55:29701
[email protected]6cade212008-12-03 00:32:22702 // If the download is deemed dangerous, we'll use a temporary name for it.
[email protected]e9ebf3fc2008-10-17 22:06:58703 if (info->is_dangerous) {
[email protected]7ae7c2cb2009-01-06 23:31:41704 info->original_name = FilePath(info->suggested_path).BaseName();
[email protected]9ccbb372008-10-10 18:50:32705 // Create a temporary file to hold the file until the user approves its
706 // download.
[email protected]7ae7c2cb2009-01-06 23:31:41707 FilePath::StringType file_name;
708 FilePath path;
[email protected]9ccbb372008-10-10 18:50:32709 while (path.empty()) {
[email protected]7ae7c2cb2009-01-06 23:31:41710 SStringPrintf(&file_name, FILE_PATH_LITERAL("unconfirmed %d.download"),
[email protected]9ccbb372008-10-10 18:50:32711 base::RandInt(0, 100000));
[email protected]7ae7c2cb2009-01-06 23:31:41712 path = dir.Append(file_name);
[email protected]7d3851d82008-12-12 03:26:07713 if (file_util::PathExists(path))
[email protected]7ae7c2cb2009-01-06 23:31:41714 path = FilePath();
[email protected]9ccbb372008-10-10 18:50:32715 }
716 info->suggested_path = path;
[email protected]7a256ea2008-10-17 17:34:16717 } else {
718 // We know the final path, build it if necessary.
719 if (info->path_uniquifier > 0) {
720 AppendNumberToPath(&(info->suggested_path), info->path_uniquifier);
721 // Setting path_uniquifier to 0 to make sure we don't try to unique it
722 // later on.
723 info->path_uniquifier = 0;
[email protected]7d3851d82008-12-12 03:26:07724 } else if (info->path_uniquifier == -1) {
725 // We failed to find a unique path. We have to prompt the user.
726 info->save_as = true;
[email protected]7a256ea2008-10-17 17:34:16727 }
[email protected]9ccbb372008-10-10 18:50:32728 }
729
[email protected]8af9d032010-02-10 00:00:32730 if (!info->save_as && info->save_info.file_path.empty()) {
[email protected]7d3851d82008-12-12 03:26:07731 // Create an empty file at the suggested path so that we don't allocate the
732 // same "non-existant" path to multiple downloads.
733 // See: http://code.google.com/p/chromium/issues/detail?id=3662
[email protected]7ff3f632009-10-13 18:43:35734 file_util::WriteFile(info->suggested_path, "", 0);
[email protected]7d3851d82008-12-12 03:26:07735 }
736
initial.commit09911bf2008-07-26 23:55:29737 // Now we return to the UI thread.
[email protected]d83d03aa2009-11-02 21:44:37738 ChromeThread::PostTask(
739 ChromeThread::UI, FROM_HERE,
initial.commit09911bf2008-07-26 23:55:29740 NewRunnableMethod(this,
741 &DownloadManager::OnPathExistenceAvailable,
742 info));
743}
744
745void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) {
[email protected]d83d03aa2009-11-02 21:44:37746 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
initial.commit09911bf2008-07-26 23:55:29747 DCHECK(info);
748
[email protected]7d3851d82008-12-12 03:26:07749 if (info->save_as) {
initial.commit09911bf2008-07-26 23:55:29750 // We must ask the user for the place to put the download.
751 if (!select_file_dialog_.get())
752 select_file_dialog_ = SelectFileDialog::Create(this);
753
[email protected]76543b92009-08-31 17:27:45754 TabContents* contents = tab_util::GetTabContentsByID(info->child_id,
755 info->render_view_id);
[email protected]b949f1112009-04-12 20:03:08756 SelectFileDialog::FileTypeInfo file_type_info;
757 file_type_info.extensions.resize(1);
758 file_type_info.extensions[0].push_back(info->suggested_path.Extension());
[email protected]15bc8052009-04-17 19:57:24759 if (!file_type_info.extensions[0][0].empty())
760 file_type_info.extensions[0][0].erase(0, 1); // drop the .
[email protected]b949f1112009-04-12 20:03:08761 file_type_info.include_all_files = true;
[email protected]076700e62009-04-01 18:41:23762 gfx::NativeWindow owning_window =
763 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL;
initial.commit09911bf2008-07-26 23:55:29764 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE,
[email protected]561abe62009-04-06 18:08:34765 string16(),
766 info->suggested_path,
[email protected]b949f1112009-04-12 20:03:08767 &file_type_info, 0, FILE_PATH_LITERAL(""),
[email protected]0f44d3e2009-03-12 23:36:30768 owning_window, info);
initial.commit09911bf2008-07-26 23:55:29769 } else {
770 // No prompting for download, just continue with the suggested name.
771 ContinueStartDownload(info, info->suggested_path);
772 }
773}
774
775void DownloadManager::ContinueStartDownload(DownloadCreateInfo* info,
[email protected]7ae7c2cb2009-01-06 23:31:41776 const FilePath& target_path) {
initial.commit09911bf2008-07-26 23:55:29777 scoped_ptr<DownloadCreateInfo> infop(info);
778 info->path = target_path;
779
780 DownloadItem* download = NULL;
781 DownloadMap::iterator it = in_progress_.find(info->download_id);
782 if (it == in_progress_.end()) {
783 download = new DownloadItem(info->download_id,
784 info->path,
[email protected]7a256ea2008-10-17 17:34:16785 info->path_uniquifier,
initial.commit09911bf2008-07-26 23:55:29786 info->url,
[email protected]494c06e2009-07-25 01:06:42787 info->referrer_url,
[email protected]e435d6b72009-07-25 03:15:58788 info->mime_type,
[email protected]9ccbb372008-10-10 18:50:32789 info->original_name,
initial.commit09911bf2008-07-26 23:55:29790 info->start_time,
791 info->total_bytes,
[email protected]76543b92009-08-31 17:27:45792 info->child_id,
[email protected]9ccbb372008-10-10 18:50:32793 info->request_id,
[email protected]67f373a2009-09-22 02:44:51794 info->is_dangerous,
[email protected]a60c8ae2009-12-25 06:50:57795 info->save_as,
[email protected]b0ab1d42010-02-24 19:29:28796 profile_->IsOffTheRecord(),
[email protected]6aa4a1c02010-01-15 18:49:58797 info->is_extension_install,
[email protected]8af9d032010-02-10 00:00:32798 !info->save_info.file_path.empty());
initial.commit09911bf2008-07-26 23:55:29799 download->set_manager(this);
800 in_progress_[info->download_id] = download;
801 } else {
802 NOTREACHED(); // Should not exist!
803 return;
804 }
805
[email protected]6b323782009-03-27 18:43:08806 // Called before DownloadFinished in order to avoid a race condition where we
807 // attempt to open a completed download before it has been renamed.
[email protected]d83d03aa2009-11-02 21:44:37808 ChromeThread::PostTask(
809 ChromeThread::FILE, FROM_HERE,
810 NewRunnableMethod(
811 file_manager_, &DownloadFileManager::OnFinalDownloadName,
812 download->id(), target_path, this));
[email protected]6b323782009-03-27 18:43:08813
initial.commit09911bf2008-07-26 23:55:29814 // If the download already completed by the time we reached this point, then
815 // notify observers that it did.
816 PendingFinishedMap::iterator pending_it =
817 pending_finished_downloads_.find(info->download_id);
818 if (pending_it != pending_finished_downloads_.end())
819 DownloadFinished(pending_it->first, pending_it->second);
820
821 download->Rename(target_path);
822
[email protected]67f373a2009-09-22 02:44:51823 // Do not store the download in the history database for a few special cases:
824 // - incognito mode (that is the point of this mode)
825 // - extensions (users don't think of extension installation as 'downloading')
[email protected]6aa4a1c02010-01-15 18:49:58826 // - temporary download, like in drag-and-drop
[email protected]67f373a2009-09-22 02:44:51827 // We have to make sure that these handles don't collide with normal db
828 // handles, so we use a negative value. Eventually, they could overlap, but
829 // you'd have to do enough downloading that your ISP would likely stab you in
830 // the neck first. YMMV.
[email protected]b0ab1d42010-02-24 19:29:28831 if (download->is_otr() || download->is_extension_install() ||
[email protected]6aa4a1c02010-01-15 18:49:58832 download->is_temporary()) {
[email protected]d2a8fb72010-01-21 05:31:42833 OnCreateDownloadEntryComplete(*info, fake_db_handle_.GetNext());
initial.commit09911bf2008-07-26 23:55:29834 } else {
835 // Update the history system with the new download.
[email protected]6cade212008-12-03 00:32:22836 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong.
initial.commit09911bf2008-07-26 23:55:29837 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
838 if (hs) {
839 hs->CreateDownload(
840 *info, &cancelable_consumer_,
841 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete));
842 }
843 }
[email protected]6a7fb042010-02-01 16:30:47844
845 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29846}
847
848// Convenience function for updating the history service for a download.
849void DownloadManager::UpdateHistoryForDownload(DownloadItem* download) {
850 DCHECK(download);
851
852 // Don't store info in the database if the download was initiated while in
853 // incognito mode or if it hasn't been initialized in our database table.
854 if (download->db_handle() <= kUninitializedHandle)
855 return;
856
[email protected]6cade212008-12-03 00:32:22857 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong.
initial.commit09911bf2008-07-26 23:55:29858 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
859 if (hs) {
860 hs->UpdateDownload(download->received_bytes(),
861 download->state(),
862 download->db_handle());
863 }
864}
865
866void DownloadManager::RemoveDownloadFromHistory(DownloadItem* download) {
867 DCHECK(download);
[email protected]6cade212008-12-03 00:32:22868 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong.
initial.commit09911bf2008-07-26 23:55:29869 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
870 if (download->db_handle() > kUninitializedHandle && hs)
871 hs->RemoveDownload(download->db_handle());
872}
873
[email protected]e93d2822009-01-30 05:59:59874void DownloadManager::RemoveDownloadsFromHistoryBetween(
875 const base::Time remove_begin,
876 const base::Time remove_end) {
[email protected]6cade212008-12-03 00:32:22877 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong.
initial.commit09911bf2008-07-26 23:55:29878 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
879 if (hs)
880 hs->RemoveDownloadsBetween(remove_begin, remove_end);
881}
882
883void DownloadManager::UpdateDownload(int32 download_id, int64 size) {
884 DownloadMap::iterator it = in_progress_.find(download_id);
885 if (it != in_progress_.end()) {
886 DownloadItem* download = it->second;
887 download->Update(size);
888 UpdateHistoryForDownload(download);
889 }
[email protected]6a7fb042010-02-01 16:30:47890 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29891}
892
893void DownloadManager::DownloadFinished(int32 download_id, int64 size) {
894 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]9ccbb372008-10-10 18:50:32895 if (it == in_progress_.end()) {
initial.commit09911bf2008-07-26 23:55:29896 // The download is done, but the user hasn't selected a final location for
897 // it yet (the Save As dialog box is probably still showing), so just keep
898 // track of the fact that this download id is complete, when the
899 // DownloadItem is constructed later we'll notify its completion then.
900 PendingFinishedMap::iterator erase_it =
901 pending_finished_downloads_.find(download_id);
902 DCHECK(erase_it == pending_finished_downloads_.end());
903 pending_finished_downloads_[download_id] = size;
[email protected]9ccbb372008-10-10 18:50:32904 return;
initial.commit09911bf2008-07-26 23:55:29905 }
[email protected]9ccbb372008-10-10 18:50:32906
907 // Remove the id from the list of pending ids.
908 PendingFinishedMap::iterator erase_it =
909 pending_finished_downloads_.find(download_id);
910 if (erase_it != pending_finished_downloads_.end())
911 pending_finished_downloads_.erase(erase_it);
912
913 DownloadItem* download = it->second;
914 download->Finished(size);
915
916 // Clean up will happen when the history system create callback runs if we
917 // don't have a valid db_handle yet.
918 if (download->db_handle() != kUninitializedHandle) {
919 in_progress_.erase(it);
[email protected]9ccbb372008-10-10 18:50:32920 UpdateHistoryForDownload(download);
921 }
922
[email protected]6a7fb042010-02-01 16:30:47923 UpdateAppIcon();
924
[email protected]9ccbb372008-10-10 18:50:32925 // If this a dangerous download not yet validated by the user, don't do
926 // anything. When the user notifies us, it will trigger a call to
927 // ProceedWithFinishedDangerousDownload.
928 if (download->safety_state() == DownloadItem::DANGEROUS) {
929 dangerous_finished_[download_id] = download;
930 return;
931 }
932
933 if (download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) {
[email protected]6cade212008-12-03 00:32:22934 // We first need to rename the downloaded file from its temporary name to
[email protected]9ccbb372008-10-10 18:50:32935 // its final name before we can continue.
[email protected]d83d03aa2009-11-02 21:44:37936 ChromeThread::PostTask(
937 ChromeThread::FILE, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:32938 NewRunnableMethod(
939 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
940 download->db_handle(),
941 download->full_path(), download->original_name()));
942 return;
943 }
944 ContinueDownloadFinished(download);
945}
946
[email protected]8f783752009-04-01 23:33:45947void DownloadManager::DownloadRenamedToFinalName(int download_id,
948 const FilePath& full_path) {
[email protected]6aa4a1c02010-01-15 18:49:58949 DownloadMap::iterator it = downloads_.begin();
950 while (it != downloads_.end()) {
951 DownloadItem* download = it->second;
952 if (download->id() == download_id) {
953 // The download file is meant to be completed if both the filename is
954 // finalized and the file data is downloaded. The ordering of these two
955 // actions is indeterministic. Thus, if we are still in downloading the
956 // file, delay the notification.
957 download->set_name_finalized(true);
958 if (download->state() == DownloadItem::COMPLETE)
959 download->NotifyObserversDownloadFileCompleted();
960 return;
961 }
962 it++;
963 }
[email protected]8f783752009-04-01 23:33:45964}
965
[email protected]9ccbb372008-10-10 18:50:32966void DownloadManager::ContinueDownloadFinished(DownloadItem* download) {
967 // If this was a dangerous download, it has now been approved and must be
968 // removed from dangerous_finished_ so it does not get deleted on shutdown.
969 DownloadMap::iterator it = dangerous_finished_.find(download->id());
970 if (it != dangerous_finished_.end())
971 dangerous_finished_.erase(it);
972
[email protected]0aad67b2009-07-15 20:34:28973 // Handle chrome extensions explicitly and skip the shell execute.
[email protected]a60c8ae2009-12-25 06:50:57974 if (download->is_extension_install()) {
[email protected]494c06e2009-07-25 01:06:42975 OpenChromeExtension(download->full_path(), download->url(),
976 download->referrer_url());
[email protected]0aad67b2009-07-15 20:34:28977 download->set_auto_opened(true);
978 } else if (download->open_when_complete() ||
[email protected]6aa4a1c02010-01-15 18:49:58979 ShouldOpenFileBasedOnExtension(download->full_path()) ||
980 download->is_temporary()) {
981 // If the download is temporary, like in drag-and-drop, do not open it but
982 // we still need to set it auto-opened so that it can be removed from the
983 // download shelf.
984 if (!download->is_temporary())
985 OpenDownloadInShell(download, NULL);
[email protected]0aad67b2009-07-15 20:34:28986 download->set_auto_opened(true);
987 }
[email protected]9ccbb372008-10-10 18:50:32988
[email protected]0aad67b2009-07-15 20:34:28989 // Notify our observers that we are complete (the call to Finished() set the
990 // state to complete but did not notify).
991 download->UpdateObservers();
[email protected]6aa4a1c02010-01-15 18:49:58992
993 // The download file is meant to be completed if both the filename is
994 // finalized and the file data is downloaded. The ordering of these two
995 // actions is indeterministic. Thus, if the filename is not finalized yet,
996 // delay the notification.
997 if (download->name_finalized())
998 download->NotifyObserversDownloadFileCompleted();
[email protected]0aad67b2009-07-15 20:34:28999}
[email protected]eccb9d12009-10-28 05:40:091000
[email protected]9ccbb372008-10-10 18:50:321001// Called on the file thread. Renames the downloaded file to its original name.
1002void DownloadManager::ProceedWithFinishedDangerousDownload(
1003 int64 download_handle,
[email protected]7ae7c2cb2009-01-06 23:31:411004 const FilePath& path,
1005 const FilePath& original_name) {
[email protected]9ccbb372008-10-10 18:50:321006 bool success = false;
[email protected]7ae7c2cb2009-01-06 23:31:411007 FilePath new_path;
[email protected]7a256ea2008-10-17 17:34:161008 int uniquifier = 0;
[email protected]9ccbb372008-10-10 18:50:321009 if (file_util::PathExists(path)) {
[email protected]889ed35c2009-01-21 00:07:241010 new_path = path.DirName().Append(original_name);
[email protected]7a256ea2008-10-17 17:34:161011 // Make our name unique at this point, as if a dangerous file is downloading
1012 // and a 2nd download is started for a file with the same name, they would
1013 // have the same path. This is because we uniquify the name on download
1014 // start, and at that time the first file does not exists yet, so the second
1015 // file gets the same name.
1016 uniquifier = GetUniquePathNumber(new_path);
1017 if (uniquifier > 0)
1018 AppendNumberToPath(&new_path, uniquifier);
[email protected]9ccbb372008-10-10 18:50:321019 success = file_util::Move(path, new_path);
1020 } else {
1021 NOTREACHED();
1022 }
[email protected]6cade212008-12-03 00:32:221023
[email protected]d83d03aa2009-11-02 21:44:371024 ChromeThread::PostTask(
1025 ChromeThread::UI, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:321026 NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed,
[email protected]7a256ea2008-10-17 17:34:161027 download_handle, success, new_path, uniquifier));
[email protected]9ccbb372008-10-10 18:50:321028}
1029
1030// Call from the file thread when the finished dangerous download was renamed.
1031void DownloadManager::DangerousDownloadRenamed(int64 download_handle,
1032 bool success,
[email protected]7ae7c2cb2009-01-06 23:31:411033 const FilePath& new_path,
[email protected]7a256ea2008-10-17 17:34:161034 int new_path_uniquifier) {
[email protected]9ccbb372008-10-10 18:50:321035 DownloadMap::iterator it = downloads_.find(download_handle);
1036 if (it == downloads_.end()) {
1037 NOTREACHED();
1038 return;
1039 }
1040
1041 DownloadItem* download = it->second;
1042 // If we failed to rename the file, we'll just keep the name as is.
[email protected]7a256ea2008-10-17 17:34:161043 if (success) {
1044 // We need to update the path uniquifier so that the UI shows the right
1045 // name when calling GetFileName().
1046 download->set_path_uniquifier(new_path_uniquifier);
[email protected]9ccbb372008-10-10 18:50:321047 RenameDownload(download, new_path);
[email protected]7a256ea2008-10-17 17:34:161048 }
[email protected]9ccbb372008-10-10 18:50:321049
1050 // Continue the download finished sequence.
1051 ContinueDownloadFinished(download);
initial.commit09911bf2008-07-26 23:55:291052}
1053
1054// static
initial.commit09911bf2008-07-26 23:55:291055void DownloadManager::OnCancelDownloadRequest(ResourceDispatcherHost* rdh,
1056 int render_process_id,
1057 int request_id) {
[email protected]d85cf072009-10-27 03:59:311058 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
initial.commit09911bf2008-07-26 23:55:291059 rdh->CancelRequest(render_process_id, request_id, false);
1060}
1061
1062void DownloadManager::DownloadCancelled(int32 download_id) {
1063 DownloadMap::iterator it = in_progress_.find(download_id);
1064 if (it == in_progress_.end())
1065 return;
1066 DownloadItem* download = it->second;
1067
initial.commit09911bf2008-07-26 23:55:291068 // Clean up will happen when the history system create callback runs if we
1069 // don't have a valid db_handle yet.
1070 if (download->db_handle() != kUninitializedHandle) {
1071 in_progress_.erase(it);
initial.commit09911bf2008-07-26 23:55:291072 UpdateHistoryForDownload(download);
1073 }
1074
[email protected]d7d1c5c2009-08-05 23:52:501075 DownloadCancelledInternal(download_id,
1076 download->render_process_id(),
1077 download->request_id());
[email protected]6a7fb042010-02-01 16:30:471078 UpdateAppIcon();
[email protected]d7d1c5c2009-08-05 23:52:501079}
1080
1081void DownloadManager::DownloadCancelledInternal(int download_id,
1082 int render_process_id,
1083 int request_id) {
[email protected]d85cf072009-10-27 03:59:311084 // Cancel the network request. RDH is guaranteed to outlive the IO thread.
1085 ChromeThread::PostTask(
1086 ChromeThread::IO, FROM_HERE,
1087 NewRunnableFunction(&DownloadManager::OnCancelDownloadRequest,
1088 g_browser_process->resource_dispatcher_host(),
1089 render_process_id,
1090 request_id));
[email protected]d7d1c5c2009-08-05 23:52:501091
initial.commit09911bf2008-07-26 23:55:291092 // Tell the file manager to cancel the download.
[email protected]d7d1c5c2009-08-05 23:52:501093 file_manager_->RemoveDownload(download_id, this); // On the UI thread
[email protected]d83d03aa2009-11-02 21:44:371094 ChromeThread::PostTask(
1095 ChromeThread::FILE, FROM_HERE,
1096 NewRunnableMethod(
1097 file_manager_, &DownloadFileManager::CancelDownload, download_id));
initial.commit09911bf2008-07-26 23:55:291098}
1099
1100void DownloadManager::PauseDownload(int32 download_id, bool pause) {
1101 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]d85cf072009-10-27 03:59:311102 if (it == in_progress_.end())
1103 return;
initial.commit09911bf2008-07-26 23:55:291104
[email protected]d85cf072009-10-27 03:59:311105 DownloadItem* download = it->second;
1106 if (pause == download->is_paused())
1107 return;
initial.commit09911bf2008-07-26 23:55:291108
[email protected]d85cf072009-10-27 03:59:311109 // Inform the ResourceDispatcherHost of the new pause state.
1110 ChromeThread::PostTask(
1111 ChromeThread::IO, FROM_HERE,
1112 NewRunnableFunction(&DownloadManager::OnPauseDownloadRequest,
1113 g_browser_process->resource_dispatcher_host(),
1114 download->render_process_id(),
1115 download->request_id(),
1116 pause));
initial.commit09911bf2008-07-26 23:55:291117}
1118
1119// static
1120void DownloadManager::OnPauseDownloadRequest(ResourceDispatcherHost* rdh,
1121 int render_process_id,
1122 int request_id,
1123 bool pause) {
1124 rdh->PauseRequest(render_process_id, request_id, pause);
1125}
1126
[email protected]7ae7c2cb2009-01-06 23:31:411127bool DownloadManager::IsDangerous(const FilePath& file_name) {
[email protected]9ccbb372008-10-10 18:50:321128 // TODO(jcampan): Improve me.
[email protected]eccb9d12009-10-28 05:40:091129 return IsExecutableFile(file_name);
[email protected]9ccbb372008-10-10 18:50:321130}
1131
[email protected]6a7fb042010-02-01 16:30:471132void DownloadManager::UpdateAppIcon() {
1133 int64 total_bytes = 0;
1134 int64 received_bytes = 0;
1135 int download_count = 0;
1136 bool progress_known = true;
1137
1138 for (DownloadMap::iterator i = in_progress_.begin();
1139 i != in_progress_.end();
1140 ++i) {
1141 ++download_count;
1142 const DownloadItem* item = i->second;
1143 if (item->total_bytes() > 0) {
1144 total_bytes += item->total_bytes();
1145 received_bytes += item->received_bytes();
1146 } else {
1147 // This download didn't specify a Content-Length, so the combined progress
1148 // bar neeeds to be indeterminate.
1149 progress_known = false;
1150 }
1151 }
1152
1153 float progress = 0;
1154 if (progress_known && download_count)
1155 progress = (float)received_bytes / total_bytes;
1156
1157 download_util::UpdateAppIconDownloadProgress(download_count,
1158 progress_known,
1159 progress);
1160}
1161
[email protected]9ccbb372008-10-10 18:50:321162void DownloadManager::RenameDownload(DownloadItem* download,
[email protected]7ae7c2cb2009-01-06 23:31:411163 const FilePath& new_path) {
[email protected]9ccbb372008-10-10 18:50:321164 download->Rename(new_path);
1165
1166 // Update the history.
1167
1168 // No update necessary if the download was initiated while in incognito mode.
1169 if (download->db_handle() <= kUninitializedHandle)
1170 return;
1171
[email protected]6cade212008-12-03 00:32:221172 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong.
[email protected]9ccbb372008-10-10 18:50:321173 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1174 if (hs)
[email protected]7ae7c2cb2009-01-06 23:31:411175 hs->UpdateDownloadPath(new_path.ToWStringHack(), download->db_handle());
[email protected]9ccbb372008-10-10 18:50:321176}
1177
initial.commit09911bf2008-07-26 23:55:291178void DownloadManager::RemoveDownload(int64 download_handle) {
1179 DownloadMap::iterator it = downloads_.find(download_handle);
1180 if (it == downloads_.end())
1181 return;
1182
1183 // Make history update.
1184 DownloadItem* download = it->second;
1185 RemoveDownloadFromHistory(download);
1186
1187 // Remove from our tables and delete.
1188 downloads_.erase(it);
[email protected]9ccbb372008-10-10 18:50:321189 it = dangerous_finished_.find(download->id());
1190 if (it != dangerous_finished_.end())
1191 dangerous_finished_.erase(it);
initial.commit09911bf2008-07-26 23:55:291192
1193 // Tell observers to refresh their views.
[email protected]b0ab1d42010-02-24 19:29:281194 NotifyModelChanged();
[email protected]6f712872008-11-07 00:35:361195
1196 delete download;
initial.commit09911bf2008-07-26 23:55:291197}
1198
[email protected]e93d2822009-01-30 05:59:591199int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin,
1200 const base::Time remove_end) {
initial.commit09911bf2008-07-26 23:55:291201 RemoveDownloadsFromHistoryBetween(remove_begin, remove_end);
1202
initial.commit09911bf2008-07-26 23:55:291203 DownloadMap::iterator it = downloads_.begin();
[email protected]78b8fcc92009-03-31 17:36:281204 std::vector<DownloadItem*> pending_deletes;
initial.commit09911bf2008-07-26 23:55:291205 while (it != downloads_.end()) {
1206 DownloadItem* download = it->second;
1207 DownloadItem::DownloadState state = download->state();
1208 if (download->start_time() >= remove_begin &&
1209 (remove_end.is_null() || download->start_time() < remove_end) &&
1210 (state == DownloadItem::COMPLETE ||
1211 state == DownloadItem::CANCELLED)) {
1212 // Remove from the map and move to the next in the list.
[email protected]b7f05882009-02-22 01:21:561213 downloads_.erase(it++);
[email protected]a6604d92008-10-30 00:58:581214
1215 // Also remove it from any completed dangerous downloads.
1216 DownloadMap::iterator dit = dangerous_finished_.find(download->id());
1217 if (dit != dangerous_finished_.end())
1218 dangerous_finished_.erase(dit);
1219
[email protected]78b8fcc92009-03-31 17:36:281220 pending_deletes.push_back(download);
[email protected]b0ab1d42010-02-24 19:29:281221 // Observer interface.
initial.commit09911bf2008-07-26 23:55:291222
initial.commit09911bf2008-07-26 23:55:291223 continue;
1224 }
1225
1226 ++it;
1227 }
1228
1229 // Tell observers to refresh their views.
[email protected]78b8fcc92009-03-31 17:36:281230 int num_deleted = static_cast<int>(pending_deletes.size());
initial.commit09911bf2008-07-26 23:55:291231 if (num_deleted > 0)
[email protected]b0ab1d42010-02-24 19:29:281232 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:291233
[email protected]78b8fcc92009-03-31 17:36:281234 // Delete the download items after updating the observers.
1235 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
1236 pending_deletes.clear();
1237
initial.commit09911bf2008-07-26 23:55:291238 return num_deleted;
1239}
1240
[email protected]e93d2822009-01-30 05:59:591241int DownloadManager::RemoveDownloads(const base::Time remove_begin) {
1242 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:291243}
1244
[email protected]d41355e6f2009-04-07 21:21:121245int DownloadManager::RemoveAllDownloads() {
1246 // The null times make the date range unbounded.
1247 return RemoveDownloadsBetween(base::Time(), base::Time());
1248}
1249
initial.commit09911bf2008-07-26 23:55:291250// Initiate a download of a specific URL. We send the request to the
1251// ResourceDispatcherHost, and let it send us responses like a regular
1252// download.
1253void DownloadManager::DownloadUrl(const GURL& url,
1254 const GURL& referrer,
[email protected]c9825a42009-05-01 22:51:501255 const std::string& referrer_charset,
[email protected]57c6a652009-05-04 07:58:341256 TabContents* tab_contents) {
[email protected]6aa4a1c02010-01-15 18:49:581257 file_manager_->DownloadUrl(url,
1258 referrer,
1259 referrer_charset,
[email protected]8af9d032010-02-10 00:00:321260 DownloadSaveInfo(),
[email protected]8cb5d5b2010-02-09 11:36:161261 tab_contents->GetRenderProcessHost()->id(),
[email protected]6aa4a1c02010-01-15 18:49:581262 tab_contents->render_view_host()->routing_id(),
1263 request_context_getter_);
1264}
1265
1266void DownloadManager::DownloadUrlToFile(const GURL& url,
1267 const GURL& referrer,
1268 const std::string& referrer_charset,
[email protected]8af9d032010-02-10 00:00:321269 const DownloadSaveInfo& save_info,
[email protected]6aa4a1c02010-01-15 18:49:581270 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:341271 DCHECK(tab_contents);
initial.commit09911bf2008-07-26 23:55:291272 file_manager_->DownloadUrl(url,
1273 referrer,
[email protected]be180c802009-10-23 06:33:311274 referrer_charset,
[email protected]8af9d032010-02-10 00:00:321275 save_info,
[email protected]8cb5d5b2010-02-09 11:36:161276 tab_contents->GetRenderProcessHost()->id(),
[email protected]57c6a652009-05-04 07:58:341277 tab_contents->render_view_host()->routing_id(),
[email protected]be180c802009-10-23 06:33:311278 request_context_getter_);
initial.commit09911bf2008-07-26 23:55:291279}
1280
[email protected]7ae7c2cb2009-01-06 23:31:411281void DownloadManager::GenerateExtension(
1282 const FilePath& file_name,
1283 const std::string& mime_type,
1284 FilePath::StringType* generated_extension) {
initial.commit09911bf2008-07-26 23:55:291285 // We're worried about three things here:
1286 //
1287 // 1) Security. Many sites let users upload content, such as buddy icons, to
1288 // their web sites. We want to mitigate the case where an attacker
1289 // supplies a malicious executable with an executable file extension but an
1290 // honest site serves the content with a benign content type, such as
1291 // image/jpeg.
1292 //
1293 // 2) Usability. If the site fails to provide a file extension, we want to
1294 // guess a reasonable file extension based on the content type.
1295 //
1296 // 3) Shell integration. Some file extensions automatically integrate with
1297 // the shell. We block these extensions to prevent a malicious web site
1298 // from integrating with the user's shell.
1299
[email protected]7ae7c2cb2009-01-06 23:31:411300 static const FilePath::CharType default_extension[] =
1301 FILE_PATH_LITERAL("download");
initial.commit09911bf2008-07-26 23:55:291302
1303 // See if our file name already contains an extension.
[email protected]7ae7c2cb2009-01-06 23:31:411304 FilePath::StringType extension(
1305 file_util::GetFileExtensionFromPath(file_name));
initial.commit09911bf2008-07-26 23:55:291306
[email protected]b7f05882009-02-22 01:21:561307#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291308 // Rename shell-integrated extensions.
1309 if (win_util::IsShellIntegratedExtension(extension))
1310 extension.assign(default_extension);
[email protected]b7f05882009-02-22 01:21:561311#endif
initial.commit09911bf2008-07-26 23:55:291312
1313 std::string mime_type_from_extension;
[email protected]bae0ea12009-02-14 01:20:411314 net::GetMimeTypeFromFile(file_name,
[email protected]7ae7c2cb2009-01-06 23:31:411315 &mime_type_from_extension);
initial.commit09911bf2008-07-26 23:55:291316 if (mime_type == mime_type_from_extension) {
1317 // The hinted extension matches the mime type. It looks like a winner.
1318 generated_extension->swap(extension);
1319 return;
1320 }
1321
[email protected]eccb9d12009-10-28 05:40:091322 if (IsExecutableExtension(extension) && !IsExecutableMimeType(mime_type)) {
initial.commit09911bf2008-07-26 23:55:291323 // We want to be careful about executable extensions. The worry here is
1324 // that a trusted web site could be tricked into dropping an executable file
1325 // on the user's filesystem.
[email protected]a9bb6f692008-07-30 16:40:101326 if (!net::GetPreferredExtensionForMimeType(mime_type, &extension)) {
initial.commit09911bf2008-07-26 23:55:291327 // We couldn't find a good extension for this content type. Use a dummy
1328 // extension instead.
1329 extension.assign(default_extension);
1330 }
1331 }
1332
1333 if (extension.empty()) {
[email protected]a9bb6f692008-07-30 16:40:101334 net::GetPreferredExtensionForMimeType(mime_type, &extension);
initial.commit09911bf2008-07-26 23:55:291335 } else {
[email protected]6cade212008-12-03 00:32:221336 // Append extension generated from the mime type if:
initial.commit09911bf2008-07-26 23:55:291337 // 1. New extension is not ".txt"
1338 // 2. New extension is not the same as the already existing extension.
1339 // 3. New extension is not executable. This action mitigates the case when
[email protected]7ae7c2cb2009-01-06 23:31:411340 // an executable is hidden in a benign file extension;
initial.commit09911bf2008-07-26 23:55:291341 // E.g. my-cat.jpg becomes my-cat.jpg.js if content type is
1342 // application/x-javascript.
[email protected]e106457b2009-03-25 22:43:371343 // 4. New extension is not ".tar" for .gz files. For misconfigured web
1344 // servers, i.e. bug 5772.
[email protected]e32642f62009-09-11 21:58:291345 // 5. The original extension is not ".tgz" & the new extension is not "gz".
[email protected]7ae7c2cb2009-01-06 23:31:411346 FilePath::StringType append_extension;
[email protected]a9bb6f692008-07-30 16:40:101347 if (net::GetPreferredExtensionForMimeType(mime_type, &append_extension)) {
[email protected]3f156552009-02-09 19:44:171348 if (append_extension != FILE_PATH_LITERAL("txt") &&
[email protected]7ae7c2cb2009-01-06 23:31:411349 append_extension != extension &&
[email protected]eccb9d12009-10-28 05:40:091350 !IsExecutableExtension(append_extension) &&
[email protected]e32642f62009-09-11 21:58:291351 !(append_extension == FILE_PATH_LITERAL("gz") &&
1352 extension == FILE_PATH_LITERAL("tgz")) &&
[email protected]e106457b2009-03-25 22:43:371353 (append_extension != FILE_PATH_LITERAL("tar") ||
1354 extension != FILE_PATH_LITERAL("gz"))) {
[email protected]3f156552009-02-09 19:44:171355 extension += FILE_PATH_LITERAL(".");
initial.commit09911bf2008-07-26 23:55:291356 extension += append_extension;
[email protected]3f156552009-02-09 19:44:171357 }
initial.commit09911bf2008-07-26 23:55:291358 }
1359 }
1360
1361 generated_extension->swap(extension);
1362}
1363
[email protected]6aa4a1c02010-01-15 18:49:581364void DownloadManager::GenerateFileNameFromInfo(DownloadCreateInfo* info,
1365 FilePath* generated_name) {
1366 GenerateFileName(GURL(info->url),
1367 info->content_disposition,
1368 info->referrer_charset,
1369 info->mime_type,
1370 generated_name);
1371}
1372
1373void DownloadManager::GenerateFileName(const GURL& url,
1374 const std::string& content_disposition,
1375 const std::string& referrer_charset,
1376 const std::string& mime_type,
[email protected]7ae7c2cb2009-01-06 23:31:411377 FilePath* generated_name) {
[email protected]630947c2009-11-04 18:37:311378 std::wstring default_name =
1379 l10n_util::GetString(IDS_DEFAULT_DOWNLOAD_FILENAME);
1380#if defined(OS_WIN)
1381 FilePath default_file_path(default_name);
1382#elif defined(OS_POSIX)
1383 FilePath default_file_path(base::SysWideToNativeMB(default_name));
1384#endif
1385
[email protected]6aa4a1c02010-01-15 18:49:581386 *generated_name = net::GetSuggestedFilename(GURL(url),
1387 content_disposition,
1388 referrer_charset,
[email protected]630947c2009-11-04 18:37:311389 default_file_path);
1390
[email protected]7ae7c2cb2009-01-06 23:31:411391 DCHECK(!generated_name->empty());
initial.commit09911bf2008-07-26 23:55:291392
[email protected]6aa4a1c02010-01-15 18:49:581393 GenerateSafeFileName(mime_type, generated_name);
initial.commit09911bf2008-07-26 23:55:291394}
1395
1396void DownloadManager::AddObserver(Observer* observer) {
1397 observers_.AddObserver(observer);
1398 observer->ModelChanged();
1399}
1400
1401void DownloadManager::RemoveObserver(Observer* observer) {
1402 observers_.RemoveObserver(observer);
1403}
1404
1405// Post Windows Shell operations to the Download thread, to avoid blocking the
1406// user interface.
1407void DownloadManager::ShowDownloadInShell(const DownloadItem* download) {
1408 DCHECK(file_manager_);
[email protected]d83d03aa2009-11-02 21:44:371409 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
[email protected]8b6ff012009-08-18 22:29:581410#if defined(OS_MACOSX)
1411 // Mac needs to run this operation on the UI thread.
1412 platform_util::ShowItemInFolder(download->full_path());
1413#else
[email protected]d83d03aa2009-11-02 21:44:371414 ChromeThread::PostTask(
1415 ChromeThread::FILE, FROM_HERE,
1416 NewRunnableMethod(
1417 file_manager_, &DownloadFileManager::OnShowDownloadInShell,
1418 FilePath(download->full_path())));
[email protected]8b6ff012009-08-18 22:29:581419#endif
initial.commit09911bf2008-07-26 23:55:291420}
1421
[email protected]8f783752009-04-01 23:33:451422void DownloadManager::OpenDownload(const DownloadItem* download,
1423 gfx::NativeView parent_window) {
[email protected]0e34d7892009-06-05 19:17:401424 // Open Chrome extensions with ExtensionsService. For everything else do shell
[email protected]8f783752009-04-01 23:33:451425 // execute.
[email protected]a60c8ae2009-12-25 06:50:571426 if (download->is_extension_install()) {
[email protected]494c06e2009-07-25 01:06:421427 OpenChromeExtension(download->full_path(), download->url(),
1428 download->referrer_url());
[email protected]8f783752009-04-01 23:33:451429 } else {
1430 OpenDownloadInShell(download, parent_window);
1431 }
1432}
1433
[email protected]c1e432a2009-07-22 21:21:481434void DownloadManager::OpenChromeExtension(const FilePath& full_path,
[email protected]494c06e2009-07-25 01:06:421435 const GURL& download_url,
1436 const GURL& referrer_url) {
[email protected]6ef635e42009-07-26 06:16:121437 // We don't support extensions in OTR mode.
[email protected]2a464a92009-08-01 17:58:351438 ExtensionsService* service = profile_->GetExtensionsService();
1439 if (service) {
[email protected]91e1bd82009-09-03 22:04:401440 NotificationService* nservice = NotificationService::current();
[email protected]6dfbbf82010-03-12 23:09:161441 GURL nonconst_download_url = download_url;
1442 nservice->Notify(NotificationType::EXTENSION_READY_FOR_INSTALL,
1443 Source<DownloadManager>(this),
1444 Details<GURL>(&nonconst_download_url));
1445
1446 scoped_refptr<CrxInstaller> installer(
1447 new CrxInstaller(service->install_directory(),
1448 service,
1449 new ExtensionInstallUI(profile_)));
1450 installer->set_delete_source(true);
1451
[email protected]a050d712009-11-06 00:40:101452 if (UserScript::HasUserScriptFileExtension(download_url)) {
[email protected]6dfbbf82010-03-12 23:09:161453 installer->InstallUserScript(full_path, download_url);
[email protected]6657afa62009-11-04 02:15:201454 } else {
[email protected]6dfbbf82010-03-12 23:09:161455 installer->set_allow_privilege_increase(true);
1456 installer->set_original_url(download_url);
1457 installer->InstallCrx(full_path);
[email protected]6657afa62009-11-04 02:15:201458 }
[email protected]21ca982c2010-01-26 22:49:551459 } 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(
1470 IDR_INFOBAR_PLUGIN_INSTALL)));
1471 }
[email protected]2a464a92009-08-01 17:58:351472 }
[email protected]8f783752009-04-01 23:33:451473}
1474
initial.commit09911bf2008-07-26 23:55:291475void DownloadManager::OpenDownloadInShell(const DownloadItem* download,
[email protected]e93d2822009-01-30 05:59:591476 gfx::NativeView parent_window) {
initial.commit09911bf2008-07-26 23:55:291477 DCHECK(file_manager_);
[email protected]d83d03aa2009-11-02 21:44:371478 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
[email protected]8b6ff012009-08-18 22:29:581479#if defined(OS_MACOSX)
1480 // Mac OS X requires opening downloads on the UI thread.
1481 platform_util::OpenItem(download->full_path());
1482#else
[email protected]d83d03aa2009-11-02 21:44:371483 ChromeThread::PostTask(
1484 ChromeThread::FILE, FROM_HERE,
1485 NewRunnableMethod(
1486 file_manager_, &DownloadFileManager::OnOpenDownloadInShell,
1487 download->full_path(), download->url(), parent_window));
[email protected]8b6ff012009-08-18 22:29:581488#endif
initial.commit09911bf2008-07-26 23:55:291489}
1490
[email protected]eccb9d12009-10-28 05:40:091491void DownloadManager::OpenFilesBasedOnExtension(
1492 const FilePath& path, bool open) {
1493 FilePath::StringType extension = path.Extension();
1494 if (extension.empty())
1495 return;
1496 DCHECK(extension[0] == FilePath::kExtensionSeparator);
1497 extension.erase(0, 1);
1498 if (open && !IsExecutableExtension(extension))
initial.commit09911bf2008-07-26 23:55:291499 auto_open_.insert(extension);
1500 else
1501 auto_open_.erase(extension);
1502 SaveAutoOpens();
1503}
1504
[email protected]eccb9d12009-10-28 05:40:091505bool DownloadManager::ShouldOpenFileBasedOnExtension(
1506 const FilePath& path) const {
[email protected]eccb9d12009-10-28 05:40:091507 FilePath::StringType extension = path.Extension();
1508 if (extension.empty())
1509 return false;
1510 if (IsExecutableExtension(extension))
1511 return false;
[email protected]92e11c82010-01-13 06:39:561512 if (Extension::IsExtension(path))
1513 return false;
[email protected]eccb9d12009-10-28 05:40:091514 DCHECK(extension[0] == FilePath::kExtensionSeparator);
1515 extension.erase(0, 1);
[email protected]92e11c82010-01-13 06:39:561516 if (auto_open_.find(extension) != auto_open_.end())
[email protected]8c756ac2009-01-30 23:36:411517 return true;
initial.commit09911bf2008-07-26 23:55:291518 return false;
1519}
1520
[email protected]7b73d992008-12-15 20:56:461521static const char* kExecutableWhiteList[] = {
initial.commit09911bf2008-07-26 23:55:291522 // JavaScript is just as powerful as EXE.
[email protected]7b73d992008-12-15 20:56:461523 "text/javascript",
1524 "text/javascript;version=*",
[email protected]54d8d452009-04-08 17:29:241525 // Registry files can cause critical changes to the MS OS behavior.
1526 // Addition of this mimetype also addresses bug 7337.
1527 "text/x-registry",
[email protected]60ff8f912008-12-05 07:58:391528 // Some sites use binary/octet-stream to mean application/octet-stream.
1529 // See http://code.google.com/p/chromium/issues/detail?id=1573
[email protected]7b73d992008-12-15 20:56:461530 "binary/octet-stream"
1531};
initial.commit09911bf2008-07-26 23:55:291532
[email protected]7b73d992008-12-15 20:56:461533static const char* kExecutableBlackList[] = {
initial.commit09911bf2008-07-26 23:55:291534 // These application types are not executable.
[email protected]7b73d992008-12-15 20:56:461535 "application/*+xml",
1536 "application/xml"
1537};
initial.commit09911bf2008-07-26 23:55:291538
[email protected]7b73d992008-12-15 20:56:461539// static
1540bool DownloadManager::IsExecutableMimeType(const std::string& mime_type) {
[email protected]bae0ea12009-02-14 01:20:411541 for (size_t i = 0; i < arraysize(kExecutableWhiteList); ++i) {
[email protected]7b73d992008-12-15 20:56:461542 if (net::MatchesMimeType(kExecutableWhiteList[i], mime_type))
1543 return true;
1544 }
[email protected]bae0ea12009-02-14 01:20:411545 for (size_t i = 0; i < arraysize(kExecutableBlackList); ++i) {
[email protected]7b73d992008-12-15 20:56:461546 if (net::MatchesMimeType(kExecutableBlackList[i], mime_type))
1547 return false;
1548 }
1549 // We consider only other application types to be executable.
1550 return net::MatchesMimeType("application/*", mime_type);
initial.commit09911bf2008-07-26 23:55:291551}
1552
[email protected]eccb9d12009-10-28 05:40:091553bool DownloadManager::IsExecutableFile(const FilePath& path) const {
1554 return IsExecutableExtension(path.Extension());
1555}
1556
1557bool DownloadManager::IsExecutableExtension(
[email protected]6aa4a1c02010-01-15 18:49:581558 const FilePath::StringType& extension) {
[email protected]eccb9d12009-10-28 05:40:091559 if (extension.empty())
1560 return false;
[email protected]64da0b932009-02-24 02:30:041561 if (!IsStringASCII(extension))
1562 return false;
[email protected]a0a9577b2009-05-27 23:52:321563#if defined(OS_WIN)
[email protected]64da0b932009-02-24 02:30:041564 std::string ascii_extension = WideToASCII(extension);
[email protected]e9ef0a62009-08-11 22:50:131565#elif defined(OS_POSIX)
[email protected]a0a9577b2009-05-27 23:52:321566 std::string ascii_extension = extension;
1567#endif
[email protected]64da0b932009-02-24 02:30:041568
[email protected]eccb9d12009-10-28 05:40:091569 // Strip out leading dot if it's still there
1570 if (ascii_extension[0] == FilePath::kExtensionSeparator)
1571 ascii_extension.erase(0, 1);
1572
[email protected]6aa4a1c02010-01-15 18:49:581573 return download_util::IsExecutableExtension(ascii_extension);
initial.commit09911bf2008-07-26 23:55:291574}
1575
1576void DownloadManager::ResetAutoOpenFiles() {
1577 auto_open_.clear();
1578 SaveAutoOpens();
1579}
1580
1581bool DownloadManager::HasAutoOpenFileTypesRegistered() const {
1582 return !auto_open_.empty();
1583}
1584
1585void DownloadManager::SaveAutoOpens() {
1586 PrefService* prefs = profile_->GetPrefs();
1587 if (prefs) {
[email protected]7ae7c2cb2009-01-06 23:31:411588 FilePath::StringType extensions;
[email protected]eccb9d12009-10-28 05:40:091589 for (AutoOpenSet::iterator it = auto_open_.begin();
initial.commit09911bf2008-07-26 23:55:291590 it != auto_open_.end(); ++it) {
[email protected]7ae7c2cb2009-01-06 23:31:411591 extensions += *it + FILE_PATH_LITERAL(":");
initial.commit09911bf2008-07-26 23:55:291592 }
1593 if (!extensions.empty())
1594 extensions.erase(extensions.size() - 1);
[email protected]b7f05882009-02-22 01:21:561595
1596 std::wstring extensions_w;
1597#if defined(OS_WIN)
1598 extensions_w = extensions;
1599#elif defined(OS_POSIX)
[email protected]1b5044d2009-02-24 00:04:141600 extensions_w = base::SysNativeMBToWide(extensions);
[email protected]b7f05882009-02-22 01:21:561601#endif
1602
1603 prefs->SetString(prefs::kDownloadExtensionsToOpen, extensions_w);
initial.commit09911bf2008-07-26 23:55:291604 }
1605}
1606
[email protected]561abe62009-04-06 18:08:341607void DownloadManager::FileSelected(const FilePath& path,
[email protected]23b357b2009-03-30 20:02:361608 int index, void* params) {
initial.commit09911bf2008-07-26 23:55:291609 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]7d3851d82008-12-12 03:26:071610 if (info->save_as)
[email protected]7ae7c2cb2009-01-06 23:31:411611 last_download_path_ = path.DirName();
initial.commit09911bf2008-07-26 23:55:291612 ContinueStartDownload(info, path);
1613}
1614
1615void DownloadManager::FileSelectionCanceled(void* params) {
1616 // The user didn't pick a place to save the file, so need to cancel the
1617 // download that's already in progress to the temporary location.
1618 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]d7d1c5c2009-08-05 23:52:501619 DownloadCancelledInternal(info->download_id,
[email protected]76543b92009-08-31 17:27:451620 info->child_id,
[email protected]d7d1c5c2009-08-05 23:52:501621 info->request_id);
initial.commit09911bf2008-07-26 23:55:291622}
1623
[email protected]7ae7c2cb2009-01-06 23:31:411624void DownloadManager::DeleteDownload(const FilePath& path) {
[email protected]d83d03aa2009-11-02 21:44:371625 ChromeThread::PostTask(
1626 ChromeThread::FILE, FROM_HERE,
1627 NewRunnableFunction(&DownloadFileManager::DeleteFile, FilePath(path)));
[email protected]9ccbb372008-10-10 18:50:321628}
1629
1630
1631void DownloadManager::DangerousDownloadValidated(DownloadItem* download) {
1632 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state());
1633 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED);
1634 download->UpdateObservers();
1635
1636 // If the download is not complete, nothing to do. The required
1637 // post-processing will be performed when it does complete.
1638 if (download->state() != DownloadItem::COMPLETE)
1639 return;
1640
[email protected]d83d03aa2009-11-02 21:44:371641 ChromeThread::PostTask(
1642 ChromeThread::FILE, FROM_HERE,
1643 NewRunnableMethod(
1644 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
1645 download->db_handle(), download->full_path(),
1646 download->original_name()));
[email protected]9ccbb372008-10-10 18:50:321647}
1648
[email protected]6aa4a1c02010-01-15 18:49:581649void DownloadManager::GenerateSafeFileName(const std::string& mime_type,
[email protected]7ae7c2cb2009-01-06 23:31:411650 FilePath* file_name) {
1651 // Make sure we get the right file extension
1652 FilePath::StringType extension;
[email protected]763f946a2009-01-06 19:04:391653 GenerateExtension(*file_name, mime_type, &extension);
1654 file_util::ReplaceExtension(file_name, extension);
1655
[email protected]2b2f8f72009-02-24 22:42:051656#if defined(OS_WIN)
[email protected]763f946a2009-01-06 19:04:391657 // Prepend "_" to the file name if it's a reserved name
[email protected]7ae7c2cb2009-01-06 23:31:411658 FilePath::StringType leaf_name = file_name->BaseName().value();
[email protected]763f946a2009-01-06 19:04:391659 DCHECK(!leaf_name.empty());
1660 if (win_util::IsReservedName(leaf_name)) {
[email protected]7ae7c2cb2009-01-06 23:31:411661 leaf_name = FilePath::StringType(FILE_PATH_LITERAL("_")) + leaf_name;
1662 *file_name = file_name->DirName();
1663 if (file_name->value() == FilePath::kCurrentDirectory) {
1664 *file_name = FilePath(leaf_name);
[email protected]763f946a2009-01-06 19:04:391665 } else {
[email protected]7ae7c2cb2009-01-06 23:31:411666 *file_name = file_name->Append(leaf_name);
[email protected]763f946a2009-01-06 19:04:391667 }
1668 }
[email protected]b7f05882009-02-22 01:21:561669#endif
[email protected]763f946a2009-01-06 19:04:391670}
1671
initial.commit09911bf2008-07-26 23:55:291672// Operations posted to us from the history service ----------------------------
1673
1674// The history service has retrieved all download entries. 'entries' contains
1675// 'DownloadCreateInfo's in sorted order (by ascending start_time).
1676void DownloadManager::OnQueryDownloadEntriesComplete(
1677 std::vector<DownloadCreateInfo>* entries) {
1678 for (size_t i = 0; i < entries->size(); ++i) {
1679 DownloadItem* download = new DownloadItem(entries->at(i));
1680 DCHECK(downloads_.find(download->db_handle()) == downloads_.end());
1681 downloads_[download->db_handle()] = download;
1682 download->set_manager(this);
1683 }
[email protected]b0ab1d42010-02-24 19:29:281684 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:291685}
1686
initial.commit09911bf2008-07-26 23:55:291687// Once the new DownloadItem's creation info has been committed to the history
1688// service, we associate the DownloadItem with the db handle, update our
1689// 'downloads_' map and inform observers.
1690void DownloadManager::OnCreateDownloadEntryComplete(DownloadCreateInfo info,
1691 int64 db_handle) {
1692 DownloadMap::iterator it = in_progress_.find(info.download_id);
1693 DCHECK(it != in_progress_.end());
1694
1695 DownloadItem* download = it->second;
[email protected]d2a8fb72010-01-21 05:31:421696
1697 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
1698 // call this function with an invalid |db_handle|. For instance, this can
1699 // happen when the history database is offline. We cannot have multiple
1700 // DownloadItems with the same invalid db_handle, so we need to assign a
1701 // unique |db_handle| here.
1702 if (db_handle == kUninitializedHandle)
1703 db_handle = fake_db_handle_.GetNext();
1704
initial.commit09911bf2008-07-26 23:55:291705 DCHECK(download->db_handle() == kUninitializedHandle);
1706 download->set_db_handle(db_handle);
1707
1708 // Insert into our full map.
1709 DCHECK(downloads_.find(download->db_handle()) == downloads_.end());
1710 downloads_[download->db_handle()] = download;
1711
[email protected]5e595482009-05-06 20:16:531712 // Show in the appropropriate browser UI.
1713 ShowDownloadInBrowser(info, download);
initial.commit09911bf2008-07-26 23:55:291714
1715 // Inform interested objects about the new download.
[email protected]b0ab1d42010-02-24 19:29:281716 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:291717
1718 // If this download has been completed before we've received the db handle,
1719 // post one final message to the history service so that it can be properly
1720 // in sync with the DownloadItem's completion status, and also inform any
1721 // observers so that they get more than just the start notification.
1722 if (download->state() != DownloadItem::IN_PROGRESS) {
1723 in_progress_.erase(it);
initial.commit09911bf2008-07-26 23:55:291724 UpdateHistoryForDownload(download);
1725 download->UpdateObservers();
1726 }
[email protected]6a7fb042010-02-01 16:30:471727
1728 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:291729}
1730
1731// Called when the history service has retrieved the list of downloads that
1732// match the search text.
1733void DownloadManager::OnSearchComplete(HistoryService::Handle handle,
1734 std::vector<int64>* results) {
1735 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1736 Observer* requestor = cancelable_consumer_.GetClientData(hs, handle);
1737 if (!requestor)
1738 return;
1739
1740 std::vector<DownloadItem*> searched_downloads;
1741 for (std::vector<int64>::iterator it = results->begin();
1742 it != results->end(); ++it) {
1743 DownloadMap::iterator dit = downloads_.find(*it);
1744 if (dit != downloads_.end())
1745 searched_downloads.push_back(dit->second);
1746 }
1747
1748 requestor->SetDownloads(searched_downloads);
1749}
[email protected]905a08d2008-11-19 07:24:121750
[email protected]5e595482009-05-06 20:16:531751void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info,
1752 DownloadItem* download) {
[email protected]5e595482009-05-06 20:16:531753 // The 'contents' may no longer exist if the user closed the tab before we get
1754 // this start completion event. If it does, tell the origin TabContents to
1755 // display its download shelf.
[email protected]76543b92009-08-31 17:27:451756 TabContents* contents = tab_util::GetTabContentsByID(info.child_id,
1757 info.render_view_id);
[email protected]5e595482009-05-06 20:16:531758
1759 // If the contents no longer exists, we start the download in the last active
1760 // browser. This is not ideal but better than fully hiding the download from
1761 // the user.
1762 if (!contents) {
1763 Browser* last_active = BrowserList::GetLastActive();
1764 if (last_active)
1765 contents = last_active->GetSelectedTabContents();
1766 }
1767
1768 if (contents)
1769 contents->OnStartDownload(download);
1770}
1771
[email protected]6cade212008-12-03 00:32:221772// Clears the last download path, used to initialize "save as" dialogs.
[email protected]905a08d2008-11-19 07:24:121773void DownloadManager::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:411774 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:381775}
[email protected]b0ab1d42010-02-24 19:29:281776
1777void DownloadManager::NotifyModelChanged() {
1778 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
1779}
1780
1781// DownloadManager::OtherDownloadManagerObserver implementation ----------------
1782
1783DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver(
1784 DownloadManager* observing_download_manager)
1785 : observing_download_manager_(observing_download_manager),
1786 observed_download_manager_(NULL) {
1787 if (observing_download_manager->profile_->GetOriginalProfile() ==
1788 observing_download_manager->profile_) {
1789 return;
1790 }
1791
1792 observed_download_manager_ = observing_download_manager_->
1793 profile_->GetOriginalProfile()->GetDownloadManager();
1794 observed_download_manager_->AddObserver(this);
1795}
1796
1797DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() {
1798 if (observed_download_manager_)
1799 observed_download_manager_->RemoveObserver(this);
1800}
1801
1802void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1803 observing_download_manager_->NotifyModelChanged();
1804}
1805
1806void DownloadManager::OtherDownloadManagerObserver::SetDownloads(
1807 std::vector<DownloadItem*>& downloads) {
1808}
1809
1810void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1811 observed_download_manager_ = NULL;
1812}