blob: 362a74d091eeaf433c078cbf9c59dd2805716073 [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"
initial.commit09911bf2008-07-26 23:55:2912#include "base/path_service.h"
[email protected]1b5044d2009-02-24 00:04:1413#include "base/rand_util.h"
[email protected]92926d92010-09-02 18:35:0614#include "base/stl_util-inl.h"
[email protected]2594c2b2010-11-08 23:04:2615#include "base/stringprintf.h"
[email protected]1b5044d2009-02-24 00:04:1416#include "base/sys_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/task.h"
[email protected]ce7f62e32010-08-10 23:43:5918#include "base/utf_string_conversions.h"
[email protected]d2a8fb72010-01-21 05:31:4219#include "build/build_config.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/browser/browser_list.h"
21#include "chrome/browser/browser_process.h"
[email protected]facac1532010-10-11 22:59:5022#include "chrome/browser/browser_thread.h"
[email protected]cd448092010-12-06 23:49:1323#include "chrome/browser/download/download_extensions.h"
[email protected]6c69796d2010-07-16 21:41:1624#include "chrome/browser/download/download_file_manager.h"
[email protected]82f37b02010-07-29 22:04:5725#include "chrome/browser/download/download_history.h"
[email protected]6c69796d2010-07-16 21:41:1626#include "chrome/browser/download/download_item.h"
[email protected]e5dc4222010-08-30 22:16:3227#include "chrome/browser/download/download_prefs.h"
[email protected]073ed7b2010-09-27 09:20:0228#include "chrome/browser/download/download_status_updater.h"
[email protected]e9ef0a62009-08-11 22:50:1329#include "chrome/browser/download/download_util.h"
[email protected]8f783752009-04-01 23:33:4530#include "chrome/browser/extensions/extensions_service.h"
[email protected]a0835ac2010-09-13 19:40:0831#include "chrome/browser/history/download_create_info.h"
[email protected]be180c802009-10-23 06:33:3132#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]14a000d2010-04-29 21:44:2433#include "chrome/browser/platform_util.h"
[email protected]8ecad5e2010-12-02 21:18:3334#include "chrome/browser/profiles/profile.h"
[email protected]8c8657d62009-01-16 18:31:2635#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2536#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]21ca982c2010-01-26 22:49:5537#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3438#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]d2a8fb72010-01-21 05:31:4239#include "chrome/browser/tab_contents/tab_util.h"
[email protected]7b5dc002010-11-16 23:08:1040#include "chrome/browser/ui/browser.h"
initial.commit09911bf2008-07-26 23:55:2941#include "chrome/common/chrome_paths.h"
[email protected]91e1bd82009-09-03 22:04:4042#include "chrome/common/notification_service.h"
43#include "chrome/common/notification_type.h"
initial.commit09911bf2008-07-26 23:55:2944#include "chrome/common/pref_names.h"
[email protected]46072d42008-07-28 14:49:3545#include "googleurl/src/gurl.h"
[email protected]34ac8f32009-02-22 23:03:2746#include "grit/generated_resources.h"
[email protected]21ca982c2010-01-26 22:49:5547#include "grit/theme_resources.h"
initial.commit09911bf2008-07-26 23:55:2948#include "net/base/mime_util.h"
49#include "net/base/net_util.h"
initial.commit09911bf2008-07-26 23:55:2950
[email protected]b7f05882009-02-22 01:21:5651#if defined(OS_WIN)
[email protected]4a0765a2009-05-08 23:12:2552#include "app/win_util.h"
[email protected]a0a9577b2009-05-27 23:52:3253#endif
54
[email protected]073ed7b2010-09-27 09:20:0255DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater)
initial.commit09911bf2008-07-26 23:55:2956 : shutdown_needed_(false),
57 profile_(NULL),
[email protected]073ed7b2010-09-27 09:20:0258 file_manager_(NULL),
[email protected]a3d2bc42010-10-06 14:08:4959 status_updater_(status_updater->AsWeakPtr()) {
[email protected]073ed7b2010-09-27 09:20:0260 if (status_updater_)
61 status_updater_->AddDelegate(this);
initial.commit09911bf2008-07-26 23:55:2962}
63
64DownloadManager::~DownloadManager() {
[email protected]326a6a92010-09-10 20:21:1365 DCHECK(!shutdown_needed_);
[email protected]073ed7b2010-09-27 09:20:0266 if (status_updater_)
67 status_updater_->RemoveDelegate(this);
initial.commit09911bf2008-07-26 23:55:2968}
69
70void DownloadManager::Shutdown() {
[email protected]da6e3922010-11-24 21:45:5071 VLOG(20) << __FUNCTION__ << "()"
72 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:1373 if (!shutdown_needed_)
74 return;
75 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:2976
[email protected]326a6a92010-09-10 20:21:1377 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
78
79 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:1880 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]326a6a92010-09-10 20:21:1381 NewRunnableMethod(file_manager_,
82 &DownloadFileManager::OnDownloadManagerShutdown,
[email protected]dc7cdcb92010-12-14 06:40:5483 make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:1384 }
initial.commit09911bf2008-07-26 23:55:2985
[email protected]f04182f32010-12-10 19:12:0786 AssertContainersConsistent();
87
88 // Go through all downloads in downloads_. Dangerous ones we need to
89 // remove on disk, and in progress ones we need to cancel.
90 for (std::set<DownloadItem*>::iterator it = downloads_.begin();
91 it != downloads_.end();) {
92 DownloadItem* download = *it;
93
94 // Save iterator from potential erases in this set done by called code.
95 // Iterators after an erasure point are still valid for lists and
96 // associative containers such as sets.
97 it++;
98
99 if (download->safety_state() == DownloadItem::DANGEROUS &&
100 (download->state() == DownloadItem::IN_PROGRESS ||
101 download->state() == DownloadItem::COMPLETE)) {
102 // The user hasn't accepted it, so we need to remove it
103 // from the disk. This may or may not result in it being
104 // removed from the DownloadManager queues and deleted
105 // (specifically, DownloadManager::RemoveDownload only
106 // removes and deletes it if it's known to the history service)
107 // so the only thing we know after calling this function is that
108 // the download was deleted if-and-only-if it was removed
109 // from all queues.
110 download->Remove(true);
111 } else if (download->state() == DownloadItem::IN_PROGRESS) {
112 download->Cancel(false);
113 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29114 }
115 }
116
[email protected]f04182f32010-12-10 19:12:07117 // At this point, all dangerous downloads have had their files removed
118 // and all in progress downloads have been cancelled. We can now delete
119 // anything left.
120 STLDeleteElements(&downloads_);
[email protected]9ccbb372008-10-10 18:50:32121
[email protected]f04182f32010-12-10 19:12:07122 // And clear all non-owning containers.
initial.commit09911bf2008-07-26 23:55:29123 in_progress_.clear();
initial.commit09911bf2008-07-26 23:55:29124
125 file_manager_ = NULL;
126
initial.commit09911bf2008-07-26 23:55:29127 // Make sure the save as dialog doesn't notify us back if we're gone before
128 // it returns.
129 if (select_file_dialog_.get())
130 select_file_dialog_->ListenerDestroyed();
131
[email protected]82f37b02010-07-29 22:04:57132 download_history_.reset();
133
[email protected]2c93f6f2010-11-08 20:41:17134 request_context_getter_ = NULL;
135
initial.commit09911bf2008-07-26 23:55:29136 shutdown_needed_ = false;
137}
138
[email protected]82f37b02010-07-29 22:04:57139void DownloadManager::GetTemporaryDownloads(
140 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
141 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58142
[email protected]f04182f32010-12-10 19:12:07143 for (DownloadMap::iterator it = history_downloads_.begin();
144 it != history_downloads_.end(); ++it) {
[email protected]6aa4a1c02010-01-15 18:49:58145 if (it->second->is_temporary() &&
146 it->second->full_path().DirName() == dir_path)
[email protected]82f37b02010-07-29 22:04:57147 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58148 }
[email protected]6aa4a1c02010-01-15 18:49:58149}
150
[email protected]82f37b02010-07-29 22:04:57151void DownloadManager::GetAllDownloads(
152 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
153 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34154
[email protected]f04182f32010-12-10 19:12:07155 for (DownloadMap::iterator it = history_downloads_.begin();
156 it != history_downloads_.end(); ++it) {
[email protected]8ddbd66a2010-05-21 16:38:34157 if (!it->second->is_temporary() &&
158 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57159 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34160 }
[email protected]8ddbd66a2010-05-21 16:38:34161}
162
[email protected]82f37b02010-07-29 22:04:57163void DownloadManager::GetCurrentDownloads(
164 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
165 DCHECK(result);
[email protected]c4a530b2010-03-08 17:33:03166
[email protected]f04182f32010-12-10 19:12:07167 for (DownloadMap::iterator it = history_downloads_.begin();
168 it != history_downloads_.end(); ++it) {
[email protected]c4a530b2010-03-08 17:33:03169 if (!it->second->is_temporary() &&
170 (it->second->state() == DownloadItem::IN_PROGRESS ||
171 it->second->safety_state() == DownloadItem::DANGEROUS) &&
172 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57173 result->push_back(it->second);
[email protected]c4a530b2010-03-08 17:33:03174 }
[email protected]f7e9fd62010-09-28 15:45:06175
176 // If we have a parent profile, let it add its downloads to the results.
177 Profile* original_profile = profile_->GetOriginalProfile();
178 if (original_profile != profile_)
179 original_profile->GetDownloadManager()->GetCurrentDownloads(dir_path,
180 result);
181
[email protected]c4a530b2010-03-08 17:33:03182}
183
[email protected]d3b12902010-08-16 23:39:42184void DownloadManager::SearchDownloads(const string16& query,
185 std::vector<DownloadItem*>* result) {
186 DCHECK(result);
187
188 string16 query_lower(l10n_util::ToLower(query));
189
[email protected]f04182f32010-12-10 19:12:07190 for (DownloadMap::iterator it = history_downloads_.begin();
191 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42192 DownloadItem* download_item = it->second;
193
194 if (download_item->is_temporary() || download_item->is_extension_install())
195 continue;
196
197 // Display Incognito downloads only in Incognito window, and vice versa.
198 // The Incognito Downloads page will get the list of non-Incognito downloads
199 // from its parent profile.
200 if (profile_->IsOffTheRecord() != download_item->is_otr())
201 continue;
202
203 if (download_item->MatchesQuery(query_lower))
204 result->push_back(download_item);
205 }
206
207 // If we have a parent profile, let it add its downloads to the results.
208 Profile* original_profile = profile_->GetOriginalProfile();
209 if (original_profile != profile_)
210 original_profile->GetDownloadManager()->SearchDownloads(query, result);
211}
212
initial.commit09911bf2008-07-26 23:55:29213// Query the history service for information about all persisted downloads.
214bool DownloadManager::Init(Profile* profile) {
215 DCHECK(profile);
216 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
217 shutdown_needed_ = true;
218
219 profile_ = profile;
[email protected]be180c802009-10-23 06:33:31220 request_context_getter_ = profile_->GetRequestContext();
[email protected]d3b12902010-08-16 23:39:42221 download_history_.reset(new DownloadHistory(profile));
[email protected]82f37b02010-07-29 22:04:57222 download_history_->Load(
223 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete));
[email protected]024f2f02010-04-30 22:51:46224
[email protected]e5dc4222010-08-30 22:16:32225 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs()));
226
[email protected]2941c2392010-07-15 22:54:30227 // In test mode, there may be no ResourceDispatcherHost. In this case it's
228 // safe to avoid setting |file_manager_| because we only call a small set of
229 // functions, none of which need it.
initial.commit09911bf2008-07-26 23:55:29230 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
[email protected]2941c2392010-07-15 22:54:30231 if (rdh) {
232 file_manager_ = rdh->download_file_manager();
233 DCHECK(file_manager_);
initial.commit09911bf2008-07-26 23:55:29234 }
235
[email protected]b0ab1d42010-02-24 19:29:28236 other_download_manager_observer_.reset(
237 new OtherDownloadManagerObserver(this));
238
initial.commit09911bf2008-07-26 23:55:29239 return true;
240}
241
initial.commit09911bf2008-07-26 23:55:29242// We have received a message from DownloadFileManager about a new download. We
243// create a download item and store it in our download map, and inform the
244// history system of a new download. Since this method can be called while the
245// history service thread is still reading the persistent state, we do not
[email protected]f04182f32010-12-10 19:12:07246// insert the new DownloadItem into 'history_downloads_' or inform our
247// observers at this point. OnCreateDatabaseEntryComplete() handles that
248// finalization of the the download creation as a callback from the
249// history thread.
initial.commit09911bf2008-07-26 23:55:29250void DownloadManager::StartDownload(DownloadCreateInfo* info) {
[email protected]ca4b5fa32010-10-09 12:42:18251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29252 DCHECK(info);
253
[email protected]a60c8ae2009-12-25 06:50:57254 // Check whether this download is for an extension install or not.
[email protected]e6875c12010-07-18 11:14:13255 // Allow extensions to be explicitly saved.
256 if (!info->prompt_user_for_save_location) {
[email protected]a60c8ae2009-12-25 06:50:57257 if (UserScript::HasUserScriptFileExtension(info->url) ||
258 info->mime_type == Extension::kMimeType)
259 info->is_extension_install = true;
260 }
261
[email protected]8af9d032010-02-10 00:00:32262 if (info->save_info.file_path.empty()) {
[email protected]2941c2392010-07-15 22:54:30263 FilePath generated_name;
[email protected]7ba53e12010-08-05 17:14:00264 download_util::GenerateFileNameFromInfo(info, &generated_name);
[email protected]2941c2392010-07-15 22:54:30265
266 // Freeze the user's preference for showing a Save As dialog. We're going
267 // to bounce around a bunch of threads and we don't want to worry about race
268 // conditions where the user changes this pref out from under us.
[email protected]e5dc4222010-08-30 22:16:32269 if (download_prefs_->prompt_for_download()) {
[email protected]2941c2392010-07-15 22:54:30270 // But ignore the user's preference for the following scenarios:
271 // 1) Extension installation. Note that we only care here about the case
272 // where an extension is installed, not when one is downloaded with
273 // "save as...".
274 // 2) Filetypes marked "always open." If the user just wants this file
275 // opened, don't bother asking where to keep it.
276 if (!info->is_extension_install &&
277 !ShouldOpenFileBasedOnExtension(generated_name))
[email protected]e6875c12010-07-18 11:14:13278 info->prompt_user_for_save_location = true;
[email protected]2941c2392010-07-15 22:54:30279 }
280
[email protected]8af9d032010-02-10 00:00:32281 // Determine the proper path for a download, by either one of the following:
282 // 1) using the default download directory.
283 // 2) prompting the user.
[email protected]80dc3612010-07-27 19:35:08284 if (info->prompt_user_for_save_location && !last_download_path_.empty()){
[email protected]8af9d032010-02-10 00:00:32285 info->suggested_path = last_download_path_;
[email protected]80dc3612010-07-27 19:35:08286 } else {
[email protected]e5dc4222010-08-30 22:16:32287 info->suggested_path = download_prefs_->download_path();
[email protected]80dc3612010-07-27 19:35:08288 }
[email protected]8af9d032010-02-10 00:00:32289 info->suggested_path = info->suggested_path.Append(generated_name);
290 } else {
291 info->suggested_path = info->save_info.file_path;
292 }
initial.commit09911bf2008-07-26 23:55:29293
[email protected]e6875c12010-07-18 11:14:13294 if (!info->prompt_user_for_save_location &&
295 info->save_info.file_path.empty()) {
[email protected]d3071992010-10-08 15:24:07296 info->is_dangerous = download_util::IsDangerous(info, profile());
[email protected]e9ebf3fc2008-10-17 22:06:58297 }
298
initial.commit09911bf2008-07-26 23:55:29299 // We need to move over to the download thread because we don't want to stat
300 // the suggested path on the UI thread.
[email protected]5a3b97e2010-10-05 09:49:11301 // We can only access preferences on the UI thread, so check the download path
302 // now and pass the value to the FILE thread.
[email protected]ca4b5fa32010-10-09 12:42:18303 BrowserThread::PostTask(
304 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37305 NewRunnableMethod(
[email protected]5a3b97e2010-10-05 09:49:11306 this,
307 &DownloadManager::CheckIfSuggestedPathExists,
308 info,
309 download_prefs()->download_path()));
initial.commit09911bf2008-07-26 23:55:29310}
311
[email protected]5a3b97e2010-10-05 09:49:11312void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info,
313 const FilePath& default_path) {
[email protected]ca4b5fa32010-10-09 12:42:18314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
initial.commit09911bf2008-07-26 23:55:29315 DCHECK(info);
316
[email protected]5a3b97e2010-10-05 09:49:11317 // Make sure the default download directory exists.
318 // TODO(phajdan.jr): only create the directory when we're sure the user
319 // is going to save there and not to another directory of his choice.
320 file_util::CreateDirectory(default_path);
321
initial.commit09911bf2008-07-26 23:55:29322 // Check writability of the suggested path. If we can't write to it, default
323 // to the user's "My Documents" directory. We'll prompt them in this case.
[email protected]7ae7c2cb2009-01-06 23:31:41324 FilePath dir = info->suggested_path.DirName();
325 FilePath filename = info->suggested_path.BaseName();
[email protected]9ccbb372008-10-10 18:50:32326 if (!file_util::PathIsWritable(dir)) {
[email protected]da6e3922010-11-24 21:45:50327 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\"";
[email protected]e6875c12010-07-18 11:14:13328 info->prompt_user_for_save_location = true;
initial.commit09911bf2008-07-26 23:55:29329 PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path);
[email protected]7ae7c2cb2009-01-06 23:31:41330 info->suggested_path = info->suggested_path.Append(filename);
initial.commit09911bf2008-07-26 23:55:29331 }
332
[email protected]6cade212008-12-03 00:32:22333 // If the download is deemed dangerous, we'll use a temporary name for it.
[email protected]e9ebf3fc2008-10-17 22:06:58334 if (info->is_dangerous) {
[email protected]7ae7c2cb2009-01-06 23:31:41335 info->original_name = FilePath(info->suggested_path).BaseName();
[email protected]9ccbb372008-10-10 18:50:32336 // Create a temporary file to hold the file until the user approves its
337 // download.
[email protected]7ae7c2cb2009-01-06 23:31:41338 FilePath::StringType file_name;
339 FilePath path;
[email protected]9ccbb372008-10-10 18:50:32340 while (path.empty()) {
[email protected]2594c2b2010-11-08 23:04:26341 base::SStringPrintf(
342 &file_name,
343 FILE_PATH_LITERAL("unconfirmed %d.crdownload"),
344 base::RandInt(0, 100000));
[email protected]7ae7c2cb2009-01-06 23:31:41345 path = dir.Append(file_name);
[email protected]7d3851d82008-12-12 03:26:07346 if (file_util::PathExists(path))
[email protected]7ae7c2cb2009-01-06 23:31:41347 path = FilePath();
[email protected]9ccbb372008-10-10 18:50:32348 }
349 info->suggested_path = path;
[email protected]7a256ea2008-10-17 17:34:16350 } else {
[email protected]594cd7d2010-07-21 03:23:56351 // Do not add the path uniquifier if we are saving to a specific path as in
352 // the drag-out case.
353 if (info->save_info.file_path.empty()) {
354 info->path_uniquifier = download_util::GetUniquePathNumberWithCrDownload(
355 info->suggested_path);
356 }
[email protected]7a256ea2008-10-17 17:34:16357 // We know the final path, build it if necessary.
358 if (info->path_uniquifier > 0) {
[email protected]5a2388a2010-03-26 16:13:39359 download_util::AppendNumberToPath(&(info->suggested_path),
360 info->path_uniquifier);
[email protected]7a256ea2008-10-17 17:34:16361 // Setting path_uniquifier to 0 to make sure we don't try to unique it
362 // later on.
363 info->path_uniquifier = 0;
[email protected]7d3851d82008-12-12 03:26:07364 } else if (info->path_uniquifier == -1) {
365 // We failed to find a unique path. We have to prompt the user.
[email protected]da6e3922010-11-24 21:45:50366 VLOG(1) << "Unable to find a unique path for suggested path \""
367 << info->suggested_path.value() << "\"";
[email protected]e6875c12010-07-18 11:14:13368 info->prompt_user_for_save_location = true;
[email protected]7a256ea2008-10-17 17:34:16369 }
[email protected]9ccbb372008-10-10 18:50:32370 }
371
[email protected]594cd7d2010-07-21 03:23:56372 // Create an empty file at the suggested path so that we don't allocate the
373 // same "non-existant" path to multiple downloads.
374 // See: http://code.google.com/p/chromium/issues/detail?id=3662
[email protected]e6875c12010-07-18 11:14:13375 if (!info->prompt_user_for_save_location &&
376 info->save_info.file_path.empty()) {
[email protected]594cd7d2010-07-21 03:23:56377 if (info->is_dangerous)
378 file_util::WriteFile(info->suggested_path, "", 0);
379 else
380 file_util::WriteFile(download_util::GetCrDownloadPath(
381 info->suggested_path), "", 0);
[email protected]7d3851d82008-12-12 03:26:07382 }
383
[email protected]ca4b5fa32010-10-09 12:42:18384 BrowserThread::PostTask(
385 BrowserThread::UI, FROM_HERE,
initial.commit09911bf2008-07-26 23:55:29386 NewRunnableMethod(this,
387 &DownloadManager::OnPathExistenceAvailable,
388 info));
389}
390
391void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) {
[email protected]da6e3922010-11-24 21:45:50392 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString();
[email protected]ca4b5fa32010-10-09 12:42:18393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29394 DCHECK(info);
395
[email protected]e6875c12010-07-18 11:14:13396 if (info->prompt_user_for_save_location) {
initial.commit09911bf2008-07-26 23:55:29397 // We must ask the user for the place to put the download.
398 if (!select_file_dialog_.get())
399 select_file_dialog_ = SelectFileDialog::Create(this);
400
[email protected]76543b92009-08-31 17:27:45401 TabContents* contents = tab_util::GetTabContentsByID(info->child_id,
402 info->render_view_id);
[email protected]b949f1112009-04-12 20:03:08403 SelectFileDialog::FileTypeInfo file_type_info;
404 file_type_info.extensions.resize(1);
405 file_type_info.extensions[0].push_back(info->suggested_path.Extension());
[email protected]15bc8052009-04-17 19:57:24406 if (!file_type_info.extensions[0][0].empty())
407 file_type_info.extensions[0][0].erase(0, 1); // drop the .
[email protected]b949f1112009-04-12 20:03:08408 file_type_info.include_all_files = true;
[email protected]076700e62009-04-01 18:41:23409 gfx::NativeWindow owning_window =
410 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL;
initial.commit09911bf2008-07-26 23:55:29411 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE,
[email protected]561abe62009-04-06 18:08:34412 string16(),
413 info->suggested_path,
[email protected]b949f1112009-04-12 20:03:08414 &file_type_info, 0, FILE_PATH_LITERAL(""),
[email protected]0f44d3e2009-03-12 23:36:30415 owning_window, info);
[email protected]ac167ce2010-11-24 17:59:48416 FOR_EACH_OBSERVER(Observer, observers_, SelectFileDialogDisplayed());
initial.commit09911bf2008-07-26 23:55:29417 } else {
418 // No prompting for download, just continue with the suggested name.
[email protected]a850ba42010-09-10 22:00:30419 CreateDownloadItem(info, info->suggested_path);
initial.commit09911bf2008-07-26 23:55:29420 }
421}
422
[email protected]a850ba42010-09-10 22:00:30423void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info,
424 const FilePath& target_path) {
[email protected]ca4b5fa32010-10-09 12:42:18425 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]aa033af2010-07-27 18:16:39426
initial.commit09911bf2008-07-26 23:55:29427 scoped_ptr<DownloadCreateInfo> infop(info);
428 info->path = target_path;
429
[email protected]aa033af2010-07-27 18:16:39430 DownloadItem* download = new DownloadItem(this, *info,
431 profile_->IsOffTheRecord());
432 DCHECK(!ContainsKey(in_progress_, info->download_id));
433 in_progress_[info->download_id] = download;
[email protected]f04182f32010-12-10 19:12:07434 downloads_.insert(download);
initial.commit09911bf2008-07-26 23:55:29435
[email protected]aa033af2010-07-27 18:16:39436 bool download_finished = ContainsKey(pending_finished_downloads_,
437 info->download_id);
[email protected]594cd7d2010-07-21 03:23:56438
[email protected]da6e3922010-11-24 21:45:50439 VLOG(20) << __FUNCTION__ << "()"
440 << " download_finished = " << download_finished
441 << " info = " << info->DebugString();
442
[email protected]594cd7d2010-07-21 03:23:56443 if (download_finished || info->is_dangerous) {
444 // The download has already finished or the download is not safe.
445 // We can now rename the file to its final name (or its tentative name
446 // in dangerous download cases).
[email protected]ca4b5fa32010-10-09 12:42:18447 BrowserThread::PostTask(
448 BrowserThread::FILE, FROM_HERE,
[email protected]594cd7d2010-07-21 03:23:56449 NewRunnableMethod(
[email protected]0d7e79fa2010-10-08 23:35:47450 file_manager_, &DownloadFileManager::OnFinalDownloadName,
[email protected]dc7cdcb92010-12-14 06:40:54451 download->id(), target_path, !info->is_dangerous,
452 make_scoped_refptr(this)));
[email protected]594cd7d2010-07-21 03:23:56453 } else {
454 // The download hasn't finished and it is a safe download. We need to
455 // rename it to its intermediate '.crdownload' path.
456 FilePath download_path = download_util::GetCrDownloadPath(target_path);
[email protected]ca4b5fa32010-10-09 12:42:18457 BrowserThread::PostTask(
458 BrowserThread::FILE, FROM_HERE,
[email protected]594cd7d2010-07-21 03:23:56459 NewRunnableMethod(
[email protected]0d7e79fa2010-10-08 23:35:47460 file_manager_, &DownloadFileManager::OnIntermediateDownloadName,
[email protected]dc7cdcb92010-12-14 06:40:54461 download->id(), download_path, make_scoped_refptr(this)));
[email protected]44b94b82010-11-08 20:49:18462 download->Rename(download_path);
[email protected]594cd7d2010-07-21 03:23:56463 }
464
465 if (download_finished) {
466 // If the download already completed by the time we reached this point, then
467 // notify observers that it did.
[email protected]a850ba42010-09-10 22:00:30468 OnAllDataSaved(info->download_id,
469 pending_finished_downloads_[info->download_id]);
[email protected]594cd7d2010-07-21 03:23:56470 }
initial.commit09911bf2008-07-26 23:55:29471
[email protected]82f37b02010-07-29 22:04:57472 download_history_->AddEntry(*info, download,
473 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete));
[email protected]6a7fb042010-02-01 16:30:47474
475 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29476}
477
initial.commit09911bf2008-07-26 23:55:29478void DownloadManager::UpdateDownload(int32 download_id, int64 size) {
479 DownloadMap::iterator it = in_progress_.find(download_id);
480 if (it != in_progress_.end()) {
481 DownloadItem* download = it->second;
482 download->Update(size);
[email protected]82f37b02010-07-29 22:04:57483 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29484 }
[email protected]6a7fb042010-02-01 16:30:47485 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29486}
487
[email protected]a850ba42010-09-10 22:00:30488void DownloadManager::OnAllDataSaved(int32 download_id, int64 size) {
[email protected]da6e3922010-11-24 21:45:50489 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
490 << " size = " << size;
initial.commit09911bf2008-07-26 23:55:29491 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]9ccbb372008-10-10 18:50:32492 if (it == in_progress_.end()) {
initial.commit09911bf2008-07-26 23:55:29493 // The download is done, but the user hasn't selected a final location for
494 // it yet (the Save As dialog box is probably still showing), so just keep
495 // track of the fact that this download id is complete, when the
496 // DownloadItem is constructed later we'll notify its completion then.
497 PendingFinishedMap::iterator erase_it =
498 pending_finished_downloads_.find(download_id);
499 DCHECK(erase_it == pending_finished_downloads_.end());
500 pending_finished_downloads_[download_id] = size;
[email protected]da6e3922010-11-24 21:45:50501 VLOG(20) << __FUNCTION__ << "()" << " Added download_id = " << download_id
502 << " to pending_finished_downloads_";
[email protected]9ccbb372008-10-10 18:50:32503 return;
initial.commit09911bf2008-07-26 23:55:29504 }
[email protected]9ccbb372008-10-10 18:50:32505
506 // Remove the id from the list of pending ids.
507 PendingFinishedMap::iterator erase_it =
508 pending_finished_downloads_.find(download_id);
[email protected]da6e3922010-11-24 21:45:50509 if (erase_it != pending_finished_downloads_.end()) {
[email protected]9ccbb372008-10-10 18:50:32510 pending_finished_downloads_.erase(erase_it);
[email protected]da6e3922010-11-24 21:45:50511 VLOG(20) << __FUNCTION__ << "()" << " Removed download_id = " << download_id
512 << " from pending_finished_downloads_";
513 }
[email protected]9ccbb372008-10-10 18:50:32514
515 DownloadItem* download = it->second;
[email protected]da6e3922010-11-24 21:45:50516
517 VLOG(20) << __FUNCTION__ << "()"
518 << " download = " << download->DebugString(true);
519
[email protected]a850ba42010-09-10 22:00:30520 download->OnAllDataSaved(size);
[email protected]9ccbb372008-10-10 18:50:32521
522 // Clean up will happen when the history system create callback runs if we
523 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57524 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
[email protected]9ccbb372008-10-10 18:50:32525 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57526 download_history_->UpdateEntry(download);
[email protected]9ccbb372008-10-10 18:50:32527 }
528
[email protected]6a7fb042010-02-01 16:30:47529 UpdateAppIcon();
530
[email protected]9ccbb372008-10-10 18:50:32531 // If this a dangerous download not yet validated by the user, don't do
532 // anything. When the user notifies us, it will trigger a call to
533 // ProceedWithFinishedDangerousDownload.
534 if (download->safety_state() == DownloadItem::DANGEROUS) {
[email protected]9ccbb372008-10-10 18:50:32535 return;
536 }
537
538 if (download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) {
[email protected]6cade212008-12-03 00:32:22539 // We first need to rename the downloaded file from its temporary name to
[email protected]9ccbb372008-10-10 18:50:32540 // its final name before we can continue.
[email protected]ca4b5fa32010-10-09 12:42:18541 BrowserThread::PostTask(
542 BrowserThread::FILE, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:32543 NewRunnableMethod(
544 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
545 download->db_handle(),
[email protected]44b94b82010-11-08 20:49:18546 download->full_path(), download->target_name()));
[email protected]9ccbb372008-10-10 18:50:32547 return;
548 }
[email protected]594cd7d2010-07-21 03:23:56549
[email protected]9246de32010-11-10 22:58:47550 download->OnSafeDownloadFinished(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32551}
552
[email protected]8f783752009-04-01 23:33:45553void DownloadManager::DownloadRenamedToFinalName(int download_id,
554 const FilePath& full_path) {
[email protected]da6e3922010-11-24 21:45:50555 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
556 << " full_path = \"" << full_path.value() << "\"";
[email protected]ca4b5fa32010-10-09 12:42:18557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2e030682010-07-23 19:45:36558 DownloadItem* item = GetDownloadItem(download_id);
559 if (!item)
560 return;
[email protected]9246de32010-11-10 22:58:47561 item->OnDownloadRenamedToFinalName(full_path);
[email protected]8f783752009-04-01 23:33:45562}
563
[email protected]9ccbb372008-10-10 18:50:32564// Called on the file thread. Renames the downloaded file to its original name.
565void DownloadManager::ProceedWithFinishedDangerousDownload(
566 int64 download_handle,
[email protected]7ae7c2cb2009-01-06 23:31:41567 const FilePath& path,
568 const FilePath& original_name) {
[email protected]9ccbb372008-10-10 18:50:32569 bool success = false;
[email protected]7ae7c2cb2009-01-06 23:31:41570 FilePath new_path;
[email protected]7a256ea2008-10-17 17:34:16571 int uniquifier = 0;
[email protected]9ccbb372008-10-10 18:50:32572 if (file_util::PathExists(path)) {
[email protected]889ed35c2009-01-21 00:07:24573 new_path = path.DirName().Append(original_name);
[email protected]7a256ea2008-10-17 17:34:16574 // Make our name unique at this point, as if a dangerous file is downloading
575 // and a 2nd download is started for a file with the same name, they would
576 // have the same path. This is because we uniquify the name on download
577 // start, and at that time the first file does not exists yet, so the second
578 // file gets the same name.
[email protected]5a2388a2010-03-26 16:13:39579 uniquifier = download_util::GetUniquePathNumber(new_path);
[email protected]7a256ea2008-10-17 17:34:16580 if (uniquifier > 0)
[email protected]5a2388a2010-03-26 16:13:39581 download_util::AppendNumberToPath(&new_path, uniquifier);
[email protected]9ccbb372008-10-10 18:50:32582 success = file_util::Move(path, new_path);
583 } else {
584 NOTREACHED();
585 }
[email protected]6cade212008-12-03 00:32:22586
[email protected]ca4b5fa32010-10-09 12:42:18587 BrowserThread::PostTask(
588 BrowserThread::UI, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:32589 NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed,
[email protected]7a256ea2008-10-17 17:34:16590 download_handle, success, new_path, uniquifier));
[email protected]9ccbb372008-10-10 18:50:32591}
592
593// Call from the file thread when the finished dangerous download was renamed.
594void DownloadManager::DangerousDownloadRenamed(int64 download_handle,
595 bool success,
[email protected]7ae7c2cb2009-01-06 23:31:41596 const FilePath& new_path,
[email protected]7a256ea2008-10-17 17:34:16597 int new_path_uniquifier) {
[email protected]da6e3922010-11-24 21:45:50598 VLOG(20) << __FUNCTION__ << "()" << " download_handle = " << download_handle
599 << " success = " << success
600 << " new_path = \"" << new_path.value() << "\""
601 << " new_path_uniquifier = " << new_path_uniquifier;
[email protected]f04182f32010-12-10 19:12:07602 DownloadMap::iterator it = history_downloads_.find(download_handle);
603 if (it == history_downloads_.end()) {
[email protected]9ccbb372008-10-10 18:50:32604 NOTREACHED();
605 return;
606 }
607
608 DownloadItem* download = it->second;
609 // If we failed to rename the file, we'll just keep the name as is.
[email protected]7a256ea2008-10-17 17:34:16610 if (success) {
611 // We need to update the path uniquifier so that the UI shows the right
[email protected]44b94b82010-11-08 20:49:18612 // name when calling GetFileNameToReportUser().
[email protected]7a256ea2008-10-17 17:34:16613 download->set_path_uniquifier(new_path_uniquifier);
[email protected]9ccbb372008-10-10 18:50:32614 RenameDownload(download, new_path);
[email protected]7a256ea2008-10-17 17:34:16615 }
[email protected]9ccbb372008-10-10 18:50:32616
617 // Continue the download finished sequence.
[email protected]f04182f32010-12-10 19:12:07618 download->Finished();
initial.commit09911bf2008-07-26 23:55:29619}
620
initial.commit09911bf2008-07-26 23:55:29621void DownloadManager::DownloadCancelled(int32 download_id) {
622 DownloadMap::iterator it = in_progress_.find(download_id);
623 if (it == in_progress_.end())
624 return;
625 DownloadItem* download = it->second;
626
[email protected]da6e3922010-11-24 21:45:50627 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
628 << " download = " << download->DebugString(true);
629
initial.commit09911bf2008-07-26 23:55:29630 // Clean up will happen when the history system create callback runs if we
631 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57632 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
initial.commit09911bf2008-07-26 23:55:29633 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57634 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29635 }
636
[email protected]d7d1c5c2009-08-05 23:52:50637 DownloadCancelledInternal(download_id,
638 download->render_process_id(),
639 download->request_id());
[email protected]6a7fb042010-02-01 16:30:47640 UpdateAppIcon();
[email protected]d7d1c5c2009-08-05 23:52:50641}
642
643void DownloadManager::DownloadCancelledInternal(int download_id,
644 int render_process_id,
645 int request_id) {
[email protected]d85cf072009-10-27 03:59:31646 // Cancel the network request. RDH is guaranteed to outlive the IO thread.
[email protected]ca4b5fa32010-10-09 12:42:18647 BrowserThread::PostTask(
648 BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26649 NewRunnableFunction(&download_util::CancelDownloadRequest,
[email protected]d85cf072009-10-27 03:59:31650 g_browser_process->resource_dispatcher_host(),
651 render_process_id,
652 request_id));
[email protected]d7d1c5c2009-08-05 23:52:50653
[email protected]ca4b5fa32010-10-09 12:42:18654 BrowserThread::PostTask(
655 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37656 NewRunnableMethod(
657 file_manager_, &DownloadFileManager::CancelDownload, download_id));
initial.commit09911bf2008-07-26 23:55:29658}
659
660void DownloadManager::PauseDownload(int32 download_id, bool pause) {
661 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]d85cf072009-10-27 03:59:31662 if (it == in_progress_.end())
663 return;
initial.commit09911bf2008-07-26 23:55:29664
[email protected]d85cf072009-10-27 03:59:31665 DownloadItem* download = it->second;
666 if (pause == download->is_paused())
667 return;
initial.commit09911bf2008-07-26 23:55:29668
[email protected]ca4b5fa32010-10-09 12:42:18669 BrowserThread::PostTask(
670 BrowserThread::IO, FROM_HERE,
[email protected]e2abdeb22010-09-03 19:56:15671 NewRunnableMethod(this,
672 &DownloadManager::PauseDownloadRequest,
673 g_browser_process->resource_dispatcher_host(),
[email protected]7ba53e12010-08-05 17:14:00674 download->render_process_id(),
675 download->request_id(),
676 pause));
[email protected]9ccbb372008-10-10 18:50:32677}
678
[email protected]6a7fb042010-02-01 16:30:47679void DownloadManager::UpdateAppIcon() {
[email protected]073ed7b2010-09-27 09:20:02680 if (status_updater_)
681 status_updater_->Update();
[email protected]6a7fb042010-02-01 16:30:47682}
683
[email protected]9ccbb372008-10-10 18:50:32684void DownloadManager::RenameDownload(DownloadItem* download,
[email protected]7ae7c2cb2009-01-06 23:31:41685 const FilePath& new_path) {
[email protected]9ccbb372008-10-10 18:50:32686 download->Rename(new_path);
[email protected]82f37b02010-07-29 22:04:57687 download_history_->UpdateDownloadPath(download, new_path);
[email protected]9ccbb372008-10-10 18:50:32688}
689
[email protected]e2abdeb22010-09-03 19:56:15690void DownloadManager::PauseDownloadRequest(ResourceDispatcherHost* rdh,
691 int render_process_id,
692 int request_id,
693 bool pause) {
[email protected]ca4b5fa32010-10-09 12:42:18694 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]e2abdeb22010-09-03 19:56:15695 rdh->PauseRequest(render_process_id, request_id, pause);
696}
697
initial.commit09911bf2008-07-26 23:55:29698void DownloadManager::RemoveDownload(int64 download_handle) {
[email protected]f04182f32010-12-10 19:12:07699 DownloadMap::iterator it = history_downloads_.find(download_handle);
700 if (it == history_downloads_.end())
initial.commit09911bf2008-07-26 23:55:29701 return;
702
703 // Make history update.
704 DownloadItem* download = it->second;
[email protected]82f37b02010-07-29 22:04:57705 download_history_->RemoveEntry(download);
initial.commit09911bf2008-07-26 23:55:29706
707 // Remove from our tables and delete.
[email protected]f04182f32010-12-10 19:12:07708 history_downloads_.erase(it);
709 int downloads_count = downloads_.erase(download);
710 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29711
712 // Tell observers to refresh their views.
[email protected]b0ab1d42010-02-24 19:29:28713 NotifyModelChanged();
[email protected]6f712872008-11-07 00:35:36714
715 delete download;
initial.commit09911bf2008-07-26 23:55:29716}
717
[email protected]e93d2822009-01-30 05:59:59718int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin,
719 const base::Time remove_end) {
[email protected]82f37b02010-07-29 22:04:57720 download_history_->RemoveEntriesBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29721
[email protected]f04182f32010-12-10 19:12:07722 DownloadMap::iterator it = history_downloads_.begin();
[email protected]78b8fcc92009-03-31 17:36:28723 std::vector<DownloadItem*> pending_deletes;
[email protected]f04182f32010-12-10 19:12:07724 while (it != history_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29725 DownloadItem* download = it->second;
726 DownloadItem::DownloadState state = download->state();
727 if (download->start_time() >= remove_begin &&
728 (remove_end.is_null() || download->start_time() < remove_end) &&
729 (state == DownloadItem::COMPLETE ||
730 state == DownloadItem::CANCELLED)) {
731 // Remove from the map and move to the next in the list.
[email protected]f04182f32010-12-10 19:12:07732 history_downloads_.erase(it++);
[email protected]a6604d92008-10-30 00:58:58733
734 // Also remove it from any completed dangerous downloads.
[email protected]78b8fcc92009-03-31 17:36:28735 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29736
initial.commit09911bf2008-07-26 23:55:29737 continue;
738 }
739
740 ++it;
741 }
742
743 // Tell observers to refresh their views.
[email protected]78b8fcc92009-03-31 17:36:28744 int num_deleted = static_cast<int>(pending_deletes.size());
initial.commit09911bf2008-07-26 23:55:29745 if (num_deleted > 0)
[email protected]b0ab1d42010-02-24 19:29:28746 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29747
[email protected]78b8fcc92009-03-31 17:36:28748 // Delete the download items after updating the observers.
749 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
750 pending_deletes.clear();
751
initial.commit09911bf2008-07-26 23:55:29752 return num_deleted;
753}
754
[email protected]e93d2822009-01-30 05:59:59755int DownloadManager::RemoveDownloads(const base::Time remove_begin) {
756 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29757}
758
[email protected]d41355e6f2009-04-07 21:21:12759int DownloadManager::RemoveAllDownloads() {
[email protected]024f2f02010-04-30 22:51:46760 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) {
761 // This is an incognito downloader. Clear All should clear main download
762 // manager as well.
763 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads();
764 }
[email protected]d41355e6f2009-04-07 21:21:12765 // The null times make the date range unbounded.
766 return RemoveDownloadsBetween(base::Time(), base::Time());
767}
768
[email protected]ec4826a2010-09-21 09:15:59769void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download_item) {
[email protected]f04182f32010-12-10 19:12:07770 downloads_.insert(download_item);
[email protected]ec4826a2010-09-21 09:15:59771}
772
initial.commit09911bf2008-07-26 23:55:29773// Initiate a download of a specific URL. We send the request to the
774// ResourceDispatcherHost, and let it send us responses like a regular
775// download.
776void DownloadManager::DownloadUrl(const GURL& url,
777 const GURL& referrer,
[email protected]c9825a42009-05-01 22:51:50778 const std::string& referrer_charset,
[email protected]57c6a652009-05-04 07:58:34779 TabContents* tab_contents) {
[email protected]ae8945192010-07-20 16:56:26780 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
781 tab_contents);
[email protected]6aa4a1c02010-01-15 18:49:58782}
783
784void DownloadManager::DownloadUrlToFile(const GURL& url,
785 const GURL& referrer,
786 const std::string& referrer_charset,
[email protected]8af9d032010-02-10 00:00:32787 const DownloadSaveInfo& save_info,
[email protected]6aa4a1c02010-01-15 18:49:58788 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:34789 DCHECK(tab_contents);
[email protected]ca4b5fa32010-10-09 12:42:18790 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26791 NewRunnableFunction(&download_util::DownloadUrl,
792 url,
793 referrer,
794 referrer_charset,
795 save_info,
796 g_browser_process->resource_dispatcher_host(),
797 tab_contents->GetRenderProcessHost()->id(),
798 tab_contents->render_view_host()->routing_id(),
799 request_context_getter_));
initial.commit09911bf2008-07-26 23:55:29800}
801
initial.commit09911bf2008-07-26 23:55:29802void DownloadManager::AddObserver(Observer* observer) {
803 observers_.AddObserver(observer);
804 observer->ModelChanged();
805}
806
807void DownloadManager::RemoveObserver(Observer* observer) {
808 observers_.RemoveObserver(observer);
809}
810
[email protected]eccb9d12009-10-28 05:40:09811bool DownloadManager::ShouldOpenFileBasedOnExtension(
812 const FilePath& path) const {
[email protected]eccb9d12009-10-28 05:40:09813 FilePath::StringType extension = path.Extension();
814 if (extension.empty())
815 return false;
[email protected]cd448092010-12-06 23:49:13816 if (!download_util::IsFileExtensionSafe(extension))
[email protected]eccb9d12009-10-28 05:40:09817 return false;
[email protected]92e11c82010-01-13 06:39:56818 if (Extension::IsExtension(path))
819 return false;
[email protected]eccb9d12009-10-28 05:40:09820 DCHECK(extension[0] == FilePath::kExtensionSeparator);
821 extension.erase(0, 1);
[email protected]e5dc4222010-08-30 22:16:32822 return download_prefs_->IsAutoOpenEnabledForExtension(extension);
initial.commit09911bf2008-07-26 23:55:29823}
824
[email protected]073ed7b2010-09-27 09:20:02825bool DownloadManager::IsDownloadProgressKnown() {
826 for (DownloadMap::iterator i = in_progress_.begin();
827 i != in_progress_.end(); ++i) {
828 if (i->second->total_bytes() <= 0)
829 return false;
830 }
831
832 return true;
833}
834
835int64 DownloadManager::GetInProgressDownloadCount() {
836 return in_progress_.size();
837}
838
839int64 DownloadManager::GetReceivedDownloadBytes() {
840 DCHECK(IsDownloadProgressKnown());
841 int64 received_bytes = 0;
842 for (DownloadMap::iterator i = in_progress_.begin();
843 i != in_progress_.end(); ++i) {
844 received_bytes += i->second->received_bytes();
845 }
846 return received_bytes;
847}
848
849int64 DownloadManager::GetTotalDownloadBytes() {
850 DCHECK(IsDownloadProgressKnown());
851 int64 total_bytes = 0;
852 for (DownloadMap::iterator i = in_progress_.begin();
853 i != in_progress_.end(); ++i) {
854 total_bytes += i->second->total_bytes();
855 }
856 return total_bytes;
857}
858
[email protected]561abe62009-04-06 18:08:34859void DownloadManager::FileSelected(const FilePath& path,
[email protected]23b357b2009-03-30 20:02:36860 int index, void* params) {
initial.commit09911bf2008-07-26 23:55:29861 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]e6875c12010-07-18 11:14:13862 if (info->prompt_user_for_save_location)
[email protected]7ae7c2cb2009-01-06 23:31:41863 last_download_path_ = path.DirName();
[email protected]a850ba42010-09-10 22:00:30864 CreateDownloadItem(info, path);
initial.commit09911bf2008-07-26 23:55:29865}
866
867void DownloadManager::FileSelectionCanceled(void* params) {
868 // The user didn't pick a place to save the file, so need to cancel the
869 // download that's already in progress to the temporary location.
870 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]d7d1c5c2009-08-05 23:52:50871 DownloadCancelledInternal(info->download_id,
[email protected]76543b92009-08-31 17:27:45872 info->child_id,
[email protected]d7d1c5c2009-08-05 23:52:50873 info->request_id);
initial.commit09911bf2008-07-26 23:55:29874}
875
[email protected]9ccbb372008-10-10 18:50:32876void DownloadManager::DangerousDownloadValidated(DownloadItem* download) {
877 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state());
878 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED);
879 download->UpdateObservers();
880
881 // If the download is not complete, nothing to do. The required
882 // post-processing will be performed when it does complete.
883 if (download->state() != DownloadItem::COMPLETE)
884 return;
885
[email protected]ca4b5fa32010-10-09 12:42:18886 BrowserThread::PostTask(
887 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37888 NewRunnableMethod(
889 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
890 download->db_handle(), download->full_path(),
[email protected]44b94b82010-11-08 20:49:18891 download->target_name()));
[email protected]9ccbb372008-10-10 18:50:32892}
893
initial.commit09911bf2008-07-26 23:55:29894// Operations posted to us from the history service ----------------------------
895
896// The history service has retrieved all download entries. 'entries' contains
897// 'DownloadCreateInfo's in sorted order (by ascending start_time).
898void DownloadManager::OnQueryDownloadEntriesComplete(
899 std::vector<DownloadCreateInfo>* entries) {
900 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]aa033af2010-07-27 18:16:39901 DownloadItem* download = new DownloadItem(this, entries->at(i));
[email protected]f04182f32010-12-10 19:12:07902 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
903 downloads_.insert(download);
904 history_downloads_[download->db_handle()] = download;
[email protected]da6e3922010-11-24 21:45:50905 VLOG(20) << __FUNCTION__ << "()" << i << ">"
906 << " download = " << download->DebugString(true);
initial.commit09911bf2008-07-26 23:55:29907 }
[email protected]b0ab1d42010-02-24 19:29:28908 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29909}
910
initial.commit09911bf2008-07-26 23:55:29911// Once the new DownloadItem's creation info has been committed to the history
912// service, we associate the DownloadItem with the db handle, update our
[email protected]f04182f32010-12-10 19:12:07913// 'history_downloads_' map and inform observers.
[email protected]9f5cb562010-11-08 22:06:59914void DownloadManager::OnCreateDownloadEntryComplete(
[email protected]76d8b622010-11-11 19:50:05915 DownloadCreateInfo info,
[email protected]9f5cb562010-11-08 22:06:59916 int64 db_handle) {
initial.commit09911bf2008-07-26 23:55:29917 DownloadMap::iterator it = in_progress_.find(info.download_id);
918 DCHECK(it != in_progress_.end());
919
920 DownloadItem* download = it->second;
[email protected]da6e3922010-11-24 21:45:50921 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
922 << " download_id = " << info.download_id
923 << " download = " << download->DebugString(true);
[email protected]d2a8fb72010-01-21 05:31:42924
925 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
926 // call this function with an invalid |db_handle|. For instance, this can
927 // happen when the history database is offline. We cannot have multiple
928 // DownloadItems with the same invalid db_handle, so we need to assign a
929 // unique |db_handle| here.
[email protected]82f37b02010-07-29 22:04:57930 if (db_handle == DownloadHistory::kUninitializedHandle)
931 db_handle = download_history_->GetNextFakeDbHandle();
[email protected]d2a8fb72010-01-21 05:31:42932
[email protected]82f37b02010-07-29 22:04:57933 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle);
initial.commit09911bf2008-07-26 23:55:29934 download->set_db_handle(db_handle);
935
936 // Insert into our full map.
[email protected]f04182f32010-12-10 19:12:07937 DCHECK(history_downloads_.find(download->db_handle()) ==
938 history_downloads_.end());
939 history_downloads_[download->db_handle()] = download;
initial.commit09911bf2008-07-26 23:55:29940
[email protected]5e595482009-05-06 20:16:53941 // Show in the appropropriate browser UI.
942 ShowDownloadInBrowser(info, download);
initial.commit09911bf2008-07-26 23:55:29943
944 // Inform interested objects about the new download.
[email protected]b0ab1d42010-02-24 19:29:28945 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29946
947 // If this download has been completed before we've received the db handle,
948 // post one final message to the history service so that it can be properly
949 // in sync with the DownloadItem's completion status, and also inform any
950 // observers so that they get more than just the start notification.
951 if (download->state() != DownloadItem::IN_PROGRESS) {
952 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57953 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29954 download->UpdateObservers();
955 }
[email protected]6a7fb042010-02-01 16:30:47956
957 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29958}
959
[email protected]5e595482009-05-06 20:16:53960void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info,
961 DownloadItem* download) {
[email protected]8ddbd66a2010-05-21 16:38:34962 // The 'contents' may no longer exist if the user closed the tab before we
963 // get this start completion event. If it does, tell the origin TabContents
964 // to display its download shelf.
[email protected]76543b92009-08-31 17:27:45965 TabContents* contents = tab_util::GetTabContentsByID(info.child_id,
966 info.render_view_id);
[email protected]5e595482009-05-06 20:16:53967
968 // If the contents no longer exists, we start the download in the last active
969 // browser. This is not ideal but better than fully hiding the download from
970 // the user.
971 if (!contents) {
972 Browser* last_active = BrowserList::GetLastActive();
973 if (last_active)
974 contents = last_active->GetSelectedTabContents();
975 }
976
977 if (contents)
978 contents->OnStartDownload(download);
979}
980
[email protected]6cade212008-12-03 00:32:22981// Clears the last download path, used to initialize "save as" dialogs.
[email protected]905a08d2008-11-19 07:24:12982void DownloadManager::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:41983 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:38984}
[email protected]b0ab1d42010-02-24 19:29:28985
986void DownloadManager::NotifyModelChanged() {
987 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
988}
989
[email protected]2e030682010-07-23 19:45:36990DownloadItem* DownloadManager::GetDownloadItem(int id) {
[email protected]f04182f32010-12-10 19:12:07991 for (DownloadMap::iterator it = history_downloads_.begin();
992 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:36993 DownloadItem* item = it->second;
994 if (item->id() == id)
995 return item;
996 }
997 return NULL;
998}
999
[email protected]f04182f32010-12-10 19:12:071000void DownloadManager::AssertContainersConsistent() const {
1001#if !defined(NDEBUG)
1002 // Confirm that everything in all maps is also in downloads_.
1003 const DownloadMap* maps[] = {&in_progress_, &history_downloads_};
1004 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(maps)); i++) {
1005 for (DownloadMap::const_iterator it = maps[i]->begin();
1006 it != maps[i]->end(); it++) {
1007 DCHECK_EQ(1u, downloads_.count(it->second));
1008 }
1009 }
1010 // Note that downloads_ also includes save page as downloads; that's not
1011 // checked.
1012#endif
1013}
1014
[email protected]b0ab1d42010-02-24 19:29:281015// DownloadManager::OtherDownloadManagerObserver implementation ----------------
1016
1017DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver(
1018 DownloadManager* observing_download_manager)
1019 : observing_download_manager_(observing_download_manager),
1020 observed_download_manager_(NULL) {
1021 if (observing_download_manager->profile_->GetOriginalProfile() ==
1022 observing_download_manager->profile_) {
1023 return;
1024 }
1025
1026 observed_download_manager_ = observing_download_manager_->
1027 profile_->GetOriginalProfile()->GetDownloadManager();
1028 observed_download_manager_->AddObserver(this);
1029}
1030
1031DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() {
1032 if (observed_download_manager_)
1033 observed_download_manager_->RemoveObserver(this);
1034}
1035
1036void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1037 observing_download_manager_->NotifyModelChanged();
1038}
1039
[email protected]b0ab1d42010-02-24 19:29:281040void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1041 observed_download_manager_ = NULL;
1042}