blob: 5c0577f61ddfc4e5dbfae8987fe6afd6273fc80b [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]eaa7dd182010-12-14 11:09:0030#include "chrome/browser/extensions/extension_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]8e2b6472010-12-15 22:19:4837#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
[email protected]21ca982c2010-01-26 22:49:5538#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3439#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]d2a8fb72010-01-21 05:31:4240#include "chrome/browser/tab_contents/tab_util.h"
[email protected]7b5dc002010-11-16 23:08:1041#include "chrome/browser/ui/browser.h"
initial.commit09911bf2008-07-26 23:55:2942#include "chrome/common/chrome_paths.h"
[email protected]91e1bd82009-09-03 22:04:4043#include "chrome/common/notification_service.h"
44#include "chrome/common/notification_type.h"
initial.commit09911bf2008-07-26 23:55:2945#include "chrome/common/pref_names.h"
[email protected]46072d42008-07-28 14:49:3546#include "googleurl/src/gurl.h"
[email protected]34ac8f32009-02-22 23:03:2747#include "grit/generated_resources.h"
[email protected]21ca982c2010-01-26 22:49:5548#include "grit/theme_resources.h"
initial.commit09911bf2008-07-26 23:55:2949#include "net/base/mime_util.h"
50#include "net/base/net_util.h"
initial.commit09911bf2008-07-26 23:55:2951
[email protected]b7f05882009-02-22 01:21:5652#if defined(OS_WIN)
[email protected]4a0765a2009-05-08 23:12:2553#include "app/win_util.h"
[email protected]a0a9577b2009-05-27 23:52:3254#endif
55
[email protected]073ed7b2010-09-27 09:20:0256DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater)
initial.commit09911bf2008-07-26 23:55:2957 : shutdown_needed_(false),
58 profile_(NULL),
[email protected]073ed7b2010-09-27 09:20:0259 file_manager_(NULL),
[email protected]a3d2bc42010-10-06 14:08:4960 status_updater_(status_updater->AsWeakPtr()) {
[email protected]073ed7b2010-09-27 09:20:0261 if (status_updater_)
62 status_updater_->AddDelegate(this);
initial.commit09911bf2008-07-26 23:55:2963}
64
65DownloadManager::~DownloadManager() {
[email protected]326a6a92010-09-10 20:21:1366 DCHECK(!shutdown_needed_);
[email protected]073ed7b2010-09-27 09:20:0267 if (status_updater_)
68 status_updater_->RemoveDelegate(this);
initial.commit09911bf2008-07-26 23:55:2969}
70
71void DownloadManager::Shutdown() {
[email protected]da6e3922010-11-24 21:45:5072 VLOG(20) << __FUNCTION__ << "()"
73 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:1374 if (!shutdown_needed_)
75 return;
76 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:2977
[email protected]326a6a92010-09-10 20:21:1378 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
79
80 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:1881 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]326a6a92010-09-10 20:21:1382 NewRunnableMethod(file_manager_,
83 &DownloadFileManager::OnDownloadManagerShutdown,
[email protected]dc7cdcb92010-12-14 06:40:5484 make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:1385 }
initial.commit09911bf2008-07-26 23:55:2986
[email protected]f04182f32010-12-10 19:12:0787 AssertContainersConsistent();
88
89 // Go through all downloads in downloads_. Dangerous ones we need to
90 // remove on disk, and in progress ones we need to cancel.
91 for (std::set<DownloadItem*>::iterator it = downloads_.begin();
92 it != downloads_.end();) {
93 DownloadItem* download = *it;
94
95 // Save iterator from potential erases in this set done by called code.
96 // Iterators after an erasure point are still valid for lists and
97 // associative containers such as sets.
98 it++;
99
100 if (download->safety_state() == DownloadItem::DANGEROUS &&
101 (download->state() == DownloadItem::IN_PROGRESS ||
102 download->state() == DownloadItem::COMPLETE)) {
103 // The user hasn't accepted it, so we need to remove it
104 // from the disk. This may or may not result in it being
105 // removed from the DownloadManager queues and deleted
106 // (specifically, DownloadManager::RemoveDownload only
107 // removes and deletes it if it's known to the history service)
108 // so the only thing we know after calling this function is that
109 // the download was deleted if-and-only-if it was removed
110 // from all queues.
111 download->Remove(true);
112 } else if (download->state() == DownloadItem::IN_PROGRESS) {
113 download->Cancel(false);
114 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29115 }
116 }
117
[email protected]f04182f32010-12-10 19:12:07118 // At this point, all dangerous downloads have had their files removed
119 // and all in progress downloads have been cancelled. We can now delete
120 // anything left.
121 STLDeleteElements(&downloads_);
[email protected]9ccbb372008-10-10 18:50:32122
[email protected]f04182f32010-12-10 19:12:07123 // And clear all non-owning containers.
initial.commit09911bf2008-07-26 23:55:29124 in_progress_.clear();
initial.commit09911bf2008-07-26 23:55:29125
126 file_manager_ = NULL;
127
initial.commit09911bf2008-07-26 23:55:29128 // Make sure the save as dialog doesn't notify us back if we're gone before
129 // it returns.
130 if (select_file_dialog_.get())
131 select_file_dialog_->ListenerDestroyed();
132
[email protected]82f37b02010-07-29 22:04:57133 download_history_.reset();
134
[email protected]2c93f6f2010-11-08 20:41:17135 request_context_getter_ = NULL;
136
initial.commit09911bf2008-07-26 23:55:29137 shutdown_needed_ = false;
138}
139
[email protected]82f37b02010-07-29 22:04:57140void DownloadManager::GetTemporaryDownloads(
141 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
142 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58143
[email protected]f04182f32010-12-10 19:12:07144 for (DownloadMap::iterator it = history_downloads_.begin();
145 it != history_downloads_.end(); ++it) {
[email protected]6aa4a1c02010-01-15 18:49:58146 if (it->second->is_temporary() &&
147 it->second->full_path().DirName() == dir_path)
[email protected]82f37b02010-07-29 22:04:57148 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58149 }
[email protected]6aa4a1c02010-01-15 18:49:58150}
151
[email protected]82f37b02010-07-29 22:04:57152void DownloadManager::GetAllDownloads(
153 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
154 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34155
[email protected]f04182f32010-12-10 19:12:07156 for (DownloadMap::iterator it = history_downloads_.begin();
157 it != history_downloads_.end(); ++it) {
[email protected]8ddbd66a2010-05-21 16:38:34158 if (!it->second->is_temporary() &&
159 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57160 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34161 }
[email protected]8ddbd66a2010-05-21 16:38:34162}
163
[email protected]82f37b02010-07-29 22:04:57164void DownloadManager::GetCurrentDownloads(
165 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
166 DCHECK(result);
[email protected]c4a530b2010-03-08 17:33:03167
[email protected]f04182f32010-12-10 19:12:07168 for (DownloadMap::iterator it = history_downloads_.begin();
169 it != history_downloads_.end(); ++it) {
[email protected]c4a530b2010-03-08 17:33:03170 if (!it->second->is_temporary() &&
171 (it->second->state() == DownloadItem::IN_PROGRESS ||
172 it->second->safety_state() == DownloadItem::DANGEROUS) &&
173 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57174 result->push_back(it->second);
[email protected]c4a530b2010-03-08 17:33:03175 }
[email protected]f7e9fd62010-09-28 15:45:06176
177 // If we have a parent profile, let it add its downloads to the results.
178 Profile* original_profile = profile_->GetOriginalProfile();
179 if (original_profile != profile_)
180 original_profile->GetDownloadManager()->GetCurrentDownloads(dir_path,
181 result);
182
[email protected]c4a530b2010-03-08 17:33:03183}
184
[email protected]d3b12902010-08-16 23:39:42185void DownloadManager::SearchDownloads(const string16& query,
186 std::vector<DownloadItem*>* result) {
187 DCHECK(result);
188
189 string16 query_lower(l10n_util::ToLower(query));
190
[email protected]f04182f32010-12-10 19:12:07191 for (DownloadMap::iterator it = history_downloads_.begin();
192 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42193 DownloadItem* download_item = it->second;
194
195 if (download_item->is_temporary() || download_item->is_extension_install())
196 continue;
197
198 // Display Incognito downloads only in Incognito window, and vice versa.
199 // The Incognito Downloads page will get the list of non-Incognito downloads
200 // from its parent profile.
201 if (profile_->IsOffTheRecord() != download_item->is_otr())
202 continue;
203
204 if (download_item->MatchesQuery(query_lower))
205 result->push_back(download_item);
206 }
207
208 // If we have a parent profile, let it add its downloads to the results.
209 Profile* original_profile = profile_->GetOriginalProfile();
210 if (original_profile != profile_)
211 original_profile->GetDownloadManager()->SearchDownloads(query, result);
212}
213
initial.commit09911bf2008-07-26 23:55:29214// Query the history service for information about all persisted downloads.
215bool DownloadManager::Init(Profile* profile) {
216 DCHECK(profile);
217 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
218 shutdown_needed_ = true;
219
220 profile_ = profile;
[email protected]be180c802009-10-23 06:33:31221 request_context_getter_ = profile_->GetRequestContext();
[email protected]d3b12902010-08-16 23:39:42222 download_history_.reset(new DownloadHistory(profile));
[email protected]82f37b02010-07-29 22:04:57223 download_history_->Load(
224 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete));
[email protected]024f2f02010-04-30 22:51:46225
[email protected]e5dc4222010-08-30 22:16:32226 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs()));
227
[email protected]2941c2392010-07-15 22:54:30228 // In test mode, there may be no ResourceDispatcherHost. In this case it's
229 // safe to avoid setting |file_manager_| because we only call a small set of
230 // functions, none of which need it.
initial.commit09911bf2008-07-26 23:55:29231 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
[email protected]2941c2392010-07-15 22:54:30232 if (rdh) {
233 file_manager_ = rdh->download_file_manager();
234 DCHECK(file_manager_);
initial.commit09911bf2008-07-26 23:55:29235 }
236
[email protected]b0ab1d42010-02-24 19:29:28237 other_download_manager_observer_.reset(
238 new OtherDownloadManagerObserver(this));
239
initial.commit09911bf2008-07-26 23:55:29240 return true;
241}
242
initial.commit09911bf2008-07-26 23:55:29243// We have received a message from DownloadFileManager about a new download. We
244// create a download item and store it in our download map, and inform the
245// history system of a new download. Since this method can be called while the
246// history service thread is still reading the persistent state, we do not
[email protected]f04182f32010-12-10 19:12:07247// insert the new DownloadItem into 'history_downloads_' or inform our
248// observers at this point. OnCreateDatabaseEntryComplete() handles that
249// finalization of the the download creation as a callback from the
250// history thread.
initial.commit09911bf2008-07-26 23:55:29251void DownloadManager::StartDownload(DownloadCreateInfo* info) {
[email protected]ca4b5fa32010-10-09 12:42:18252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29253 DCHECK(info);
254
[email protected]a60c8ae2009-12-25 06:50:57255 // Check whether this download is for an extension install or not.
[email protected]e6875c12010-07-18 11:14:13256 // Allow extensions to be explicitly saved.
257 if (!info->prompt_user_for_save_location) {
[email protected]a60c8ae2009-12-25 06:50:57258 if (UserScript::HasUserScriptFileExtension(info->url) ||
259 info->mime_type == Extension::kMimeType)
260 info->is_extension_install = true;
261 }
262
[email protected]8af9d032010-02-10 00:00:32263 if (info->save_info.file_path.empty()) {
[email protected]2941c2392010-07-15 22:54:30264 FilePath generated_name;
[email protected]7ba53e12010-08-05 17:14:00265 download_util::GenerateFileNameFromInfo(info, &generated_name);
[email protected]2941c2392010-07-15 22:54:30266
267 // Freeze the user's preference for showing a Save As dialog. We're going
268 // to bounce around a bunch of threads and we don't want to worry about race
269 // conditions where the user changes this pref out from under us.
[email protected]e5dc4222010-08-30 22:16:32270 if (download_prefs_->prompt_for_download()) {
[email protected]2941c2392010-07-15 22:54:30271 // But ignore the user's preference for the following scenarios:
272 // 1) Extension installation. Note that we only care here about the case
273 // where an extension is installed, not when one is downloaded with
274 // "save as...".
275 // 2) Filetypes marked "always open." If the user just wants this file
276 // opened, don't bother asking where to keep it.
277 if (!info->is_extension_install &&
278 !ShouldOpenFileBasedOnExtension(generated_name))
[email protected]e6875c12010-07-18 11:14:13279 info->prompt_user_for_save_location = true;
[email protected]2941c2392010-07-15 22:54:30280 }
281
[email protected]8af9d032010-02-10 00:00:32282 // Determine the proper path for a download, by either one of the following:
283 // 1) using the default download directory.
284 // 2) prompting the user.
[email protected]80dc3612010-07-27 19:35:08285 if (info->prompt_user_for_save_location && !last_download_path_.empty()){
[email protected]8af9d032010-02-10 00:00:32286 info->suggested_path = last_download_path_;
[email protected]80dc3612010-07-27 19:35:08287 } else {
[email protected]e5dc4222010-08-30 22:16:32288 info->suggested_path = download_prefs_->download_path();
[email protected]80dc3612010-07-27 19:35:08289 }
[email protected]8af9d032010-02-10 00:00:32290 info->suggested_path = info->suggested_path.Append(generated_name);
291 } else {
292 info->suggested_path = info->save_info.file_path;
293 }
initial.commit09911bf2008-07-26 23:55:29294
[email protected]e6875c12010-07-18 11:14:13295 if (!info->prompt_user_for_save_location &&
296 info->save_info.file_path.empty()) {
[email protected]d3071992010-10-08 15:24:07297 info->is_dangerous = download_util::IsDangerous(info, profile());
[email protected]e9ebf3fc2008-10-17 22:06:58298 }
299
initial.commit09911bf2008-07-26 23:55:29300 // We need to move over to the download thread because we don't want to stat
301 // the suggested path on the UI thread.
[email protected]5a3b97e2010-10-05 09:49:11302 // We can only access preferences on the UI thread, so check the download path
303 // now and pass the value to the FILE thread.
[email protected]ca4b5fa32010-10-09 12:42:18304 BrowserThread::PostTask(
305 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37306 NewRunnableMethod(
[email protected]5a3b97e2010-10-05 09:49:11307 this,
308 &DownloadManager::CheckIfSuggestedPathExists,
309 info,
310 download_prefs()->download_path()));
initial.commit09911bf2008-07-26 23:55:29311}
312
[email protected]5a3b97e2010-10-05 09:49:11313void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info,
314 const FilePath& default_path) {
[email protected]ca4b5fa32010-10-09 12:42:18315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
initial.commit09911bf2008-07-26 23:55:29316 DCHECK(info);
317
[email protected]5a3b97e2010-10-05 09:49:11318 // Make sure the default download directory exists.
319 // TODO(phajdan.jr): only create the directory when we're sure the user
320 // is going to save there and not to another directory of his choice.
321 file_util::CreateDirectory(default_path);
322
initial.commit09911bf2008-07-26 23:55:29323 // Check writability of the suggested path. If we can't write to it, default
324 // to the user's "My Documents" directory. We'll prompt them in this case.
[email protected]7ae7c2cb2009-01-06 23:31:41325 FilePath dir = info->suggested_path.DirName();
326 FilePath filename = info->suggested_path.BaseName();
[email protected]9ccbb372008-10-10 18:50:32327 if (!file_util::PathIsWritable(dir)) {
[email protected]da6e3922010-11-24 21:45:50328 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\"";
[email protected]e6875c12010-07-18 11:14:13329 info->prompt_user_for_save_location = true;
initial.commit09911bf2008-07-26 23:55:29330 PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path);
[email protected]7ae7c2cb2009-01-06 23:31:41331 info->suggested_path = info->suggested_path.Append(filename);
initial.commit09911bf2008-07-26 23:55:29332 }
333
[email protected]6cade212008-12-03 00:32:22334 // If the download is deemed dangerous, we'll use a temporary name for it.
[email protected]e9ebf3fc2008-10-17 22:06:58335 if (info->is_dangerous) {
[email protected]7ae7c2cb2009-01-06 23:31:41336 info->original_name = FilePath(info->suggested_path).BaseName();
[email protected]9ccbb372008-10-10 18:50:32337 // Create a temporary file to hold the file until the user approves its
338 // download.
[email protected]7ae7c2cb2009-01-06 23:31:41339 FilePath::StringType file_name;
340 FilePath path;
[email protected]9ccbb372008-10-10 18:50:32341 while (path.empty()) {
[email protected]2594c2b2010-11-08 23:04:26342 base::SStringPrintf(
343 &file_name,
344 FILE_PATH_LITERAL("unconfirmed %d.crdownload"),
345 base::RandInt(0, 100000));
[email protected]7ae7c2cb2009-01-06 23:31:41346 path = dir.Append(file_name);
[email protected]7d3851d82008-12-12 03:26:07347 if (file_util::PathExists(path))
[email protected]7ae7c2cb2009-01-06 23:31:41348 path = FilePath();
[email protected]9ccbb372008-10-10 18:50:32349 }
350 info->suggested_path = path;
[email protected]7a256ea2008-10-17 17:34:16351 } else {
[email protected]594cd7d2010-07-21 03:23:56352 // Do not add the path uniquifier if we are saving to a specific path as in
353 // the drag-out case.
354 if (info->save_info.file_path.empty()) {
355 info->path_uniquifier = download_util::GetUniquePathNumberWithCrDownload(
356 info->suggested_path);
357 }
[email protected]7a256ea2008-10-17 17:34:16358 // We know the final path, build it if necessary.
359 if (info->path_uniquifier > 0) {
[email protected]5a2388a2010-03-26 16:13:39360 download_util::AppendNumberToPath(&(info->suggested_path),
361 info->path_uniquifier);
[email protected]7a256ea2008-10-17 17:34:16362 // Setting path_uniquifier to 0 to make sure we don't try to unique it
363 // later on.
364 info->path_uniquifier = 0;
[email protected]7d3851d82008-12-12 03:26:07365 } else if (info->path_uniquifier == -1) {
366 // We failed to find a unique path. We have to prompt the user.
[email protected]da6e3922010-11-24 21:45:50367 VLOG(1) << "Unable to find a unique path for suggested path \""
368 << info->suggested_path.value() << "\"";
[email protected]e6875c12010-07-18 11:14:13369 info->prompt_user_for_save_location = true;
[email protected]7a256ea2008-10-17 17:34:16370 }
[email protected]9ccbb372008-10-10 18:50:32371 }
372
[email protected]594cd7d2010-07-21 03:23:56373 // Create an empty file at the suggested path so that we don't allocate the
374 // same "non-existant" path to multiple downloads.
375 // See: http://code.google.com/p/chromium/issues/detail?id=3662
[email protected]e6875c12010-07-18 11:14:13376 if (!info->prompt_user_for_save_location &&
377 info->save_info.file_path.empty()) {
[email protected]594cd7d2010-07-21 03:23:56378 if (info->is_dangerous)
379 file_util::WriteFile(info->suggested_path, "", 0);
380 else
381 file_util::WriteFile(download_util::GetCrDownloadPath(
382 info->suggested_path), "", 0);
[email protected]7d3851d82008-12-12 03:26:07383 }
384
[email protected]ca4b5fa32010-10-09 12:42:18385 BrowserThread::PostTask(
386 BrowserThread::UI, FROM_HERE,
initial.commit09911bf2008-07-26 23:55:29387 NewRunnableMethod(this,
388 &DownloadManager::OnPathExistenceAvailable,
389 info));
390}
391
392void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) {
[email protected]da6e3922010-11-24 21:45:50393 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString();
[email protected]ca4b5fa32010-10-09 12:42:18394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29395 DCHECK(info);
396
[email protected]e6875c12010-07-18 11:14:13397 if (info->prompt_user_for_save_location) {
initial.commit09911bf2008-07-26 23:55:29398 // We must ask the user for the place to put the download.
399 if (!select_file_dialog_.get())
400 select_file_dialog_ = SelectFileDialog::Create(this);
401
[email protected]76543b92009-08-31 17:27:45402 TabContents* contents = tab_util::GetTabContentsByID(info->child_id,
403 info->render_view_id);
[email protected]b949f1112009-04-12 20:03:08404 SelectFileDialog::FileTypeInfo file_type_info;
405 file_type_info.extensions.resize(1);
406 file_type_info.extensions[0].push_back(info->suggested_path.Extension());
[email protected]15bc8052009-04-17 19:57:24407 if (!file_type_info.extensions[0][0].empty())
408 file_type_info.extensions[0][0].erase(0, 1); // drop the .
[email protected]b949f1112009-04-12 20:03:08409 file_type_info.include_all_files = true;
[email protected]076700e62009-04-01 18:41:23410 gfx::NativeWindow owning_window =
411 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL;
initial.commit09911bf2008-07-26 23:55:29412 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE,
[email protected]561abe62009-04-06 18:08:34413 string16(),
414 info->suggested_path,
[email protected]b949f1112009-04-12 20:03:08415 &file_type_info, 0, FILE_PATH_LITERAL(""),
[email protected]0f44d3e2009-03-12 23:36:30416 owning_window, info);
[email protected]ac167ce2010-11-24 17:59:48417 FOR_EACH_OBSERVER(Observer, observers_, SelectFileDialogDisplayed());
initial.commit09911bf2008-07-26 23:55:29418 } else {
419 // No prompting for download, just continue with the suggested name.
[email protected]a850ba42010-09-10 22:00:30420 CreateDownloadItem(info, info->suggested_path);
initial.commit09911bf2008-07-26 23:55:29421 }
422}
423
[email protected]a850ba42010-09-10 22:00:30424void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info,
425 const FilePath& target_path) {
[email protected]ca4b5fa32010-10-09 12:42:18426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]aa033af2010-07-27 18:16:39427
initial.commit09911bf2008-07-26 23:55:29428 scoped_ptr<DownloadCreateInfo> infop(info);
429 info->path = target_path;
430
[email protected]aa033af2010-07-27 18:16:39431 DownloadItem* download = new DownloadItem(this, *info,
432 profile_->IsOffTheRecord());
433 DCHECK(!ContainsKey(in_progress_, info->download_id));
434 in_progress_[info->download_id] = download;
[email protected]f04182f32010-12-10 19:12:07435 downloads_.insert(download);
initial.commit09911bf2008-07-26 23:55:29436
[email protected]aa033af2010-07-27 18:16:39437 bool download_finished = ContainsKey(pending_finished_downloads_,
438 info->download_id);
[email protected]594cd7d2010-07-21 03:23:56439
[email protected]da6e3922010-11-24 21:45:50440 VLOG(20) << __FUNCTION__ << "()"
441 << " download_finished = " << download_finished
442 << " info = " << info->DebugString();
443
[email protected]594cd7d2010-07-21 03:23:56444 if (download_finished || info->is_dangerous) {
445 // The download has already finished or the download is not safe.
446 // We can now rename the file to its final name (or its tentative name
447 // in dangerous download cases).
[email protected]ca4b5fa32010-10-09 12:42:18448 BrowserThread::PostTask(
449 BrowserThread::FILE, FROM_HERE,
[email protected]594cd7d2010-07-21 03:23:56450 NewRunnableMethod(
[email protected]0d7e79fa2010-10-08 23:35:47451 file_manager_, &DownloadFileManager::OnFinalDownloadName,
[email protected]dc7cdcb92010-12-14 06:40:54452 download->id(), target_path, !info->is_dangerous,
453 make_scoped_refptr(this)));
[email protected]594cd7d2010-07-21 03:23:56454 } else {
455 // The download hasn't finished and it is a safe download. We need to
456 // rename it to its intermediate '.crdownload' path.
457 FilePath download_path = download_util::GetCrDownloadPath(target_path);
[email protected]ca4b5fa32010-10-09 12:42:18458 BrowserThread::PostTask(
459 BrowserThread::FILE, FROM_HERE,
[email protected]594cd7d2010-07-21 03:23:56460 NewRunnableMethod(
[email protected]0d7e79fa2010-10-08 23:35:47461 file_manager_, &DownloadFileManager::OnIntermediateDownloadName,
[email protected]dc7cdcb92010-12-14 06:40:54462 download->id(), download_path, make_scoped_refptr(this)));
[email protected]44b94b82010-11-08 20:49:18463 download->Rename(download_path);
[email protected]594cd7d2010-07-21 03:23:56464 }
465
466 if (download_finished) {
467 // If the download already completed by the time we reached this point, then
468 // notify observers that it did.
[email protected]a850ba42010-09-10 22:00:30469 OnAllDataSaved(info->download_id,
470 pending_finished_downloads_[info->download_id]);
[email protected]594cd7d2010-07-21 03:23:56471 }
initial.commit09911bf2008-07-26 23:55:29472
[email protected]82f37b02010-07-29 22:04:57473 download_history_->AddEntry(*info, download,
474 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete));
[email protected]6a7fb042010-02-01 16:30:47475
476 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29477}
478
initial.commit09911bf2008-07-26 23:55:29479void DownloadManager::UpdateDownload(int32 download_id, int64 size) {
480 DownloadMap::iterator it = in_progress_.find(download_id);
481 if (it != in_progress_.end()) {
482 DownloadItem* download = it->second;
483 download->Update(size);
[email protected]82f37b02010-07-29 22:04:57484 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29485 }
[email protected]6a7fb042010-02-01 16:30:47486 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29487}
488
[email protected]a850ba42010-09-10 22:00:30489void DownloadManager::OnAllDataSaved(int32 download_id, int64 size) {
[email protected]da6e3922010-11-24 21:45:50490 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
491 << " size = " << size;
initial.commit09911bf2008-07-26 23:55:29492 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]9ccbb372008-10-10 18:50:32493 if (it == in_progress_.end()) {
initial.commit09911bf2008-07-26 23:55:29494 // The download is done, but the user hasn't selected a final location for
495 // it yet (the Save As dialog box is probably still showing), so just keep
496 // track of the fact that this download id is complete, when the
497 // DownloadItem is constructed later we'll notify its completion then.
498 PendingFinishedMap::iterator erase_it =
499 pending_finished_downloads_.find(download_id);
500 DCHECK(erase_it == pending_finished_downloads_.end());
501 pending_finished_downloads_[download_id] = size;
[email protected]da6e3922010-11-24 21:45:50502 VLOG(20) << __FUNCTION__ << "()" << " Added download_id = " << download_id
503 << " to pending_finished_downloads_";
[email protected]9ccbb372008-10-10 18:50:32504 return;
initial.commit09911bf2008-07-26 23:55:29505 }
[email protected]9ccbb372008-10-10 18:50:32506
507 // Remove the id from the list of pending ids.
508 PendingFinishedMap::iterator erase_it =
509 pending_finished_downloads_.find(download_id);
[email protected]da6e3922010-11-24 21:45:50510 if (erase_it != pending_finished_downloads_.end()) {
[email protected]9ccbb372008-10-10 18:50:32511 pending_finished_downloads_.erase(erase_it);
[email protected]da6e3922010-11-24 21:45:50512 VLOG(20) << __FUNCTION__ << "()" << " Removed download_id = " << download_id
513 << " from pending_finished_downloads_";
514 }
[email protected]9ccbb372008-10-10 18:50:32515
516 DownloadItem* download = it->second;
[email protected]da6e3922010-11-24 21:45:50517
518 VLOG(20) << __FUNCTION__ << "()"
519 << " download = " << download->DebugString(true);
520
[email protected]a850ba42010-09-10 22:00:30521 download->OnAllDataSaved(size);
[email protected]9ccbb372008-10-10 18:50:32522
523 // Clean up will happen when the history system create callback runs if we
524 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57525 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
[email protected]9ccbb372008-10-10 18:50:32526 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57527 download_history_->UpdateEntry(download);
[email protected]9ccbb372008-10-10 18:50:32528 }
529
[email protected]6a7fb042010-02-01 16:30:47530 UpdateAppIcon();
531
[email protected]9ccbb372008-10-10 18:50:32532 // If this a dangerous download not yet validated by the user, don't do
533 // anything. When the user notifies us, it will trigger a call to
534 // ProceedWithFinishedDangerousDownload.
535 if (download->safety_state() == DownloadItem::DANGEROUS) {
[email protected]9ccbb372008-10-10 18:50:32536 return;
537 }
538
539 if (download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) {
[email protected]6cade212008-12-03 00:32:22540 // We first need to rename the downloaded file from its temporary name to
[email protected]9ccbb372008-10-10 18:50:32541 // its final name before we can continue.
[email protected]ca4b5fa32010-10-09 12:42:18542 BrowserThread::PostTask(
543 BrowserThread::FILE, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:32544 NewRunnableMethod(
545 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
546 download->db_handle(),
[email protected]44b94b82010-11-08 20:49:18547 download->full_path(), download->target_name()));
[email protected]9ccbb372008-10-10 18:50:32548 return;
549 }
[email protected]594cd7d2010-07-21 03:23:56550
[email protected]9246de32010-11-10 22:58:47551 download->OnSafeDownloadFinished(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32552}
553
[email protected]8f783752009-04-01 23:33:45554void DownloadManager::DownloadRenamedToFinalName(int download_id,
555 const FilePath& full_path) {
[email protected]da6e3922010-11-24 21:45:50556 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
557 << " full_path = \"" << full_path.value() << "\"";
[email protected]ca4b5fa32010-10-09 12:42:18558 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2e030682010-07-23 19:45:36559 DownloadItem* item = GetDownloadItem(download_id);
560 if (!item)
561 return;
[email protected]9246de32010-11-10 22:58:47562 item->OnDownloadRenamedToFinalName(full_path);
[email protected]8f783752009-04-01 23:33:45563}
564
[email protected]9ccbb372008-10-10 18:50:32565// Called on the file thread. Renames the downloaded file to its original name.
566void DownloadManager::ProceedWithFinishedDangerousDownload(
567 int64 download_handle,
[email protected]7ae7c2cb2009-01-06 23:31:41568 const FilePath& path,
569 const FilePath& original_name) {
[email protected]9ccbb372008-10-10 18:50:32570 bool success = false;
[email protected]7ae7c2cb2009-01-06 23:31:41571 FilePath new_path;
[email protected]7a256ea2008-10-17 17:34:16572 int uniquifier = 0;
[email protected]9ccbb372008-10-10 18:50:32573 if (file_util::PathExists(path)) {
[email protected]889ed35c2009-01-21 00:07:24574 new_path = path.DirName().Append(original_name);
[email protected]7a256ea2008-10-17 17:34:16575 // Make our name unique at this point, as if a dangerous file is downloading
576 // and a 2nd download is started for a file with the same name, they would
577 // have the same path. This is because we uniquify the name on download
578 // start, and at that time the first file does not exists yet, so the second
579 // file gets the same name.
[email protected]5a2388a2010-03-26 16:13:39580 uniquifier = download_util::GetUniquePathNumber(new_path);
[email protected]7a256ea2008-10-17 17:34:16581 if (uniquifier > 0)
[email protected]5a2388a2010-03-26 16:13:39582 download_util::AppendNumberToPath(&new_path, uniquifier);
[email protected]9ccbb372008-10-10 18:50:32583 success = file_util::Move(path, new_path);
584 } else {
585 NOTREACHED();
586 }
[email protected]6cade212008-12-03 00:32:22587
[email protected]ca4b5fa32010-10-09 12:42:18588 BrowserThread::PostTask(
589 BrowserThread::UI, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:32590 NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed,
[email protected]7a256ea2008-10-17 17:34:16591 download_handle, success, new_path, uniquifier));
[email protected]9ccbb372008-10-10 18:50:32592}
593
594// Call from the file thread when the finished dangerous download was renamed.
595void DownloadManager::DangerousDownloadRenamed(int64 download_handle,
596 bool success,
[email protected]7ae7c2cb2009-01-06 23:31:41597 const FilePath& new_path,
[email protected]7a256ea2008-10-17 17:34:16598 int new_path_uniquifier) {
[email protected]da6e3922010-11-24 21:45:50599 VLOG(20) << __FUNCTION__ << "()" << " download_handle = " << download_handle
600 << " success = " << success
601 << " new_path = \"" << new_path.value() << "\""
602 << " new_path_uniquifier = " << new_path_uniquifier;
[email protected]f04182f32010-12-10 19:12:07603 DownloadMap::iterator it = history_downloads_.find(download_handle);
604 if (it == history_downloads_.end()) {
[email protected]9ccbb372008-10-10 18:50:32605 NOTREACHED();
606 return;
607 }
608
609 DownloadItem* download = it->second;
610 // If we failed to rename the file, we'll just keep the name as is.
[email protected]7a256ea2008-10-17 17:34:16611 if (success) {
612 // We need to update the path uniquifier so that the UI shows the right
[email protected]44b94b82010-11-08 20:49:18613 // name when calling GetFileNameToReportUser().
[email protected]7a256ea2008-10-17 17:34:16614 download->set_path_uniquifier(new_path_uniquifier);
[email protected]9ccbb372008-10-10 18:50:32615 RenameDownload(download, new_path);
[email protected]7a256ea2008-10-17 17:34:16616 }
[email protected]9ccbb372008-10-10 18:50:32617
618 // Continue the download finished sequence.
[email protected]f04182f32010-12-10 19:12:07619 download->Finished();
initial.commit09911bf2008-07-26 23:55:29620}
621
initial.commit09911bf2008-07-26 23:55:29622void DownloadManager::DownloadCancelled(int32 download_id) {
623 DownloadMap::iterator it = in_progress_.find(download_id);
624 if (it == in_progress_.end())
625 return;
626 DownloadItem* download = it->second;
627
[email protected]da6e3922010-11-24 21:45:50628 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
629 << " download = " << download->DebugString(true);
630
initial.commit09911bf2008-07-26 23:55:29631 // Clean up will happen when the history system create callback runs if we
632 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57633 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
initial.commit09911bf2008-07-26 23:55:29634 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57635 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29636 }
637
[email protected]d7d1c5c2009-08-05 23:52:50638 DownloadCancelledInternal(download_id,
639 download->render_process_id(),
640 download->request_id());
[email protected]6a7fb042010-02-01 16:30:47641 UpdateAppIcon();
[email protected]d7d1c5c2009-08-05 23:52:50642}
643
644void DownloadManager::DownloadCancelledInternal(int download_id,
645 int render_process_id,
646 int request_id) {
[email protected]d85cf072009-10-27 03:59:31647 // Cancel the network request. RDH is guaranteed to outlive the IO thread.
[email protected]ca4b5fa32010-10-09 12:42:18648 BrowserThread::PostTask(
649 BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26650 NewRunnableFunction(&download_util::CancelDownloadRequest,
[email protected]d85cf072009-10-27 03:59:31651 g_browser_process->resource_dispatcher_host(),
652 render_process_id,
653 request_id));
[email protected]d7d1c5c2009-08-05 23:52:50654
[email protected]ca4b5fa32010-10-09 12:42:18655 BrowserThread::PostTask(
656 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37657 NewRunnableMethod(
658 file_manager_, &DownloadFileManager::CancelDownload, download_id));
initial.commit09911bf2008-07-26 23:55:29659}
660
661void DownloadManager::PauseDownload(int32 download_id, bool pause) {
662 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]d85cf072009-10-27 03:59:31663 if (it == in_progress_.end())
664 return;
initial.commit09911bf2008-07-26 23:55:29665
[email protected]d85cf072009-10-27 03:59:31666 DownloadItem* download = it->second;
667 if (pause == download->is_paused())
668 return;
initial.commit09911bf2008-07-26 23:55:29669
[email protected]ca4b5fa32010-10-09 12:42:18670 BrowserThread::PostTask(
671 BrowserThread::IO, FROM_HERE,
[email protected]e2abdeb22010-09-03 19:56:15672 NewRunnableMethod(this,
673 &DownloadManager::PauseDownloadRequest,
674 g_browser_process->resource_dispatcher_host(),
[email protected]7ba53e12010-08-05 17:14:00675 download->render_process_id(),
676 download->request_id(),
677 pause));
[email protected]9ccbb372008-10-10 18:50:32678}
679
[email protected]6a7fb042010-02-01 16:30:47680void DownloadManager::UpdateAppIcon() {
[email protected]073ed7b2010-09-27 09:20:02681 if (status_updater_)
682 status_updater_->Update();
[email protected]6a7fb042010-02-01 16:30:47683}
684
[email protected]9ccbb372008-10-10 18:50:32685void DownloadManager::RenameDownload(DownloadItem* download,
[email protected]7ae7c2cb2009-01-06 23:31:41686 const FilePath& new_path) {
[email protected]9ccbb372008-10-10 18:50:32687 download->Rename(new_path);
[email protected]82f37b02010-07-29 22:04:57688 download_history_->UpdateDownloadPath(download, new_path);
[email protected]9ccbb372008-10-10 18:50:32689}
690
[email protected]e2abdeb22010-09-03 19:56:15691void DownloadManager::PauseDownloadRequest(ResourceDispatcherHost* rdh,
692 int render_process_id,
693 int request_id,
694 bool pause) {
[email protected]ca4b5fa32010-10-09 12:42:18695 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]e2abdeb22010-09-03 19:56:15696 rdh->PauseRequest(render_process_id, request_id, pause);
697}
698
initial.commit09911bf2008-07-26 23:55:29699void DownloadManager::RemoveDownload(int64 download_handle) {
[email protected]f04182f32010-12-10 19:12:07700 DownloadMap::iterator it = history_downloads_.find(download_handle);
701 if (it == history_downloads_.end())
initial.commit09911bf2008-07-26 23:55:29702 return;
703
704 // Make history update.
705 DownloadItem* download = it->second;
[email protected]82f37b02010-07-29 22:04:57706 download_history_->RemoveEntry(download);
initial.commit09911bf2008-07-26 23:55:29707
708 // Remove from our tables and delete.
[email protected]f04182f32010-12-10 19:12:07709 history_downloads_.erase(it);
710 int downloads_count = downloads_.erase(download);
711 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29712
713 // Tell observers to refresh their views.
[email protected]b0ab1d42010-02-24 19:29:28714 NotifyModelChanged();
[email protected]6f712872008-11-07 00:35:36715
716 delete download;
initial.commit09911bf2008-07-26 23:55:29717}
718
[email protected]e93d2822009-01-30 05:59:59719int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin,
720 const base::Time remove_end) {
[email protected]82f37b02010-07-29 22:04:57721 download_history_->RemoveEntriesBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29722
[email protected]f04182f32010-12-10 19:12:07723 DownloadMap::iterator it = history_downloads_.begin();
[email protected]78b8fcc92009-03-31 17:36:28724 std::vector<DownloadItem*> pending_deletes;
[email protected]f04182f32010-12-10 19:12:07725 while (it != history_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29726 DownloadItem* download = it->second;
727 DownloadItem::DownloadState state = download->state();
728 if (download->start_time() >= remove_begin &&
729 (remove_end.is_null() || download->start_time() < remove_end) &&
730 (state == DownloadItem::COMPLETE ||
731 state == DownloadItem::CANCELLED)) {
732 // Remove from the map and move to the next in the list.
[email protected]f04182f32010-12-10 19:12:07733 history_downloads_.erase(it++);
[email protected]a6604d92008-10-30 00:58:58734
735 // Also remove it from any completed dangerous downloads.
[email protected]78b8fcc92009-03-31 17:36:28736 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29737
initial.commit09911bf2008-07-26 23:55:29738 continue;
739 }
740
741 ++it;
742 }
743
744 // Tell observers to refresh their views.
[email protected]78b8fcc92009-03-31 17:36:28745 int num_deleted = static_cast<int>(pending_deletes.size());
initial.commit09911bf2008-07-26 23:55:29746 if (num_deleted > 0)
[email protected]b0ab1d42010-02-24 19:29:28747 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29748
[email protected]78b8fcc92009-03-31 17:36:28749 // Delete the download items after updating the observers.
750 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
751 pending_deletes.clear();
752
initial.commit09911bf2008-07-26 23:55:29753 return num_deleted;
754}
755
[email protected]e93d2822009-01-30 05:59:59756int DownloadManager::RemoveDownloads(const base::Time remove_begin) {
757 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29758}
759
[email protected]d41355e6f2009-04-07 21:21:12760int DownloadManager::RemoveAllDownloads() {
[email protected]024f2f02010-04-30 22:51:46761 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) {
762 // This is an incognito downloader. Clear All should clear main download
763 // manager as well.
764 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads();
765 }
[email protected]d41355e6f2009-04-07 21:21:12766 // The null times make the date range unbounded.
767 return RemoveDownloadsBetween(base::Time(), base::Time());
768}
769
[email protected]ec4826a2010-09-21 09:15:59770void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download_item) {
[email protected]f04182f32010-12-10 19:12:07771 downloads_.insert(download_item);
[email protected]ec4826a2010-09-21 09:15:59772}
773
initial.commit09911bf2008-07-26 23:55:29774// Initiate a download of a specific URL. We send the request to the
775// ResourceDispatcherHost, and let it send us responses like a regular
776// download.
777void DownloadManager::DownloadUrl(const GURL& url,
778 const GURL& referrer,
[email protected]c9825a42009-05-01 22:51:50779 const std::string& referrer_charset,
[email protected]57c6a652009-05-04 07:58:34780 TabContents* tab_contents) {
[email protected]ae8945192010-07-20 16:56:26781 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
782 tab_contents);
[email protected]6aa4a1c02010-01-15 18:49:58783}
784
785void DownloadManager::DownloadUrlToFile(const GURL& url,
786 const GURL& referrer,
787 const std::string& referrer_charset,
[email protected]8af9d032010-02-10 00:00:32788 const DownloadSaveInfo& save_info,
[email protected]6aa4a1c02010-01-15 18:49:58789 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:34790 DCHECK(tab_contents);
[email protected]ca4b5fa32010-10-09 12:42:18791 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26792 NewRunnableFunction(&download_util::DownloadUrl,
793 url,
794 referrer,
795 referrer_charset,
796 save_info,
797 g_browser_process->resource_dispatcher_host(),
798 tab_contents->GetRenderProcessHost()->id(),
799 tab_contents->render_view_host()->routing_id(),
800 request_context_getter_));
initial.commit09911bf2008-07-26 23:55:29801}
802
initial.commit09911bf2008-07-26 23:55:29803void DownloadManager::AddObserver(Observer* observer) {
804 observers_.AddObserver(observer);
805 observer->ModelChanged();
806}
807
808void DownloadManager::RemoveObserver(Observer* observer) {
809 observers_.RemoveObserver(observer);
810}
811
[email protected]eccb9d12009-10-28 05:40:09812bool DownloadManager::ShouldOpenFileBasedOnExtension(
813 const FilePath& path) const {
[email protected]eccb9d12009-10-28 05:40:09814 FilePath::StringType extension = path.Extension();
815 if (extension.empty())
816 return false;
[email protected]cd448092010-12-06 23:49:13817 if (!download_util::IsFileExtensionSafe(extension))
[email protected]eccb9d12009-10-28 05:40:09818 return false;
[email protected]92e11c82010-01-13 06:39:56819 if (Extension::IsExtension(path))
820 return false;
[email protected]eccb9d12009-10-28 05:40:09821 DCHECK(extension[0] == FilePath::kExtensionSeparator);
822 extension.erase(0, 1);
[email protected]e5dc4222010-08-30 22:16:32823 return download_prefs_->IsAutoOpenEnabledForExtension(extension);
initial.commit09911bf2008-07-26 23:55:29824}
825
[email protected]073ed7b2010-09-27 09:20:02826bool DownloadManager::IsDownloadProgressKnown() {
827 for (DownloadMap::iterator i = in_progress_.begin();
828 i != in_progress_.end(); ++i) {
829 if (i->second->total_bytes() <= 0)
830 return false;
831 }
832
833 return true;
834}
835
836int64 DownloadManager::GetInProgressDownloadCount() {
837 return in_progress_.size();
838}
839
840int64 DownloadManager::GetReceivedDownloadBytes() {
841 DCHECK(IsDownloadProgressKnown());
842 int64 received_bytes = 0;
843 for (DownloadMap::iterator i = in_progress_.begin();
844 i != in_progress_.end(); ++i) {
845 received_bytes += i->second->received_bytes();
846 }
847 return received_bytes;
848}
849
850int64 DownloadManager::GetTotalDownloadBytes() {
851 DCHECK(IsDownloadProgressKnown());
852 int64 total_bytes = 0;
853 for (DownloadMap::iterator i = in_progress_.begin();
854 i != in_progress_.end(); ++i) {
855 total_bytes += i->second->total_bytes();
856 }
857 return total_bytes;
858}
859
[email protected]561abe62009-04-06 18:08:34860void DownloadManager::FileSelected(const FilePath& path,
[email protected]23b357b2009-03-30 20:02:36861 int index, void* params) {
initial.commit09911bf2008-07-26 23:55:29862 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]e6875c12010-07-18 11:14:13863 if (info->prompt_user_for_save_location)
[email protected]7ae7c2cb2009-01-06 23:31:41864 last_download_path_ = path.DirName();
[email protected]a850ba42010-09-10 22:00:30865 CreateDownloadItem(info, path);
initial.commit09911bf2008-07-26 23:55:29866}
867
868void DownloadManager::FileSelectionCanceled(void* params) {
869 // The user didn't pick a place to save the file, so need to cancel the
870 // download that's already in progress to the temporary location.
871 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]d7d1c5c2009-08-05 23:52:50872 DownloadCancelledInternal(info->download_id,
[email protected]76543b92009-08-31 17:27:45873 info->child_id,
[email protected]d7d1c5c2009-08-05 23:52:50874 info->request_id);
initial.commit09911bf2008-07-26 23:55:29875}
876
[email protected]9ccbb372008-10-10 18:50:32877void DownloadManager::DangerousDownloadValidated(DownloadItem* download) {
878 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state());
879 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED);
880 download->UpdateObservers();
881
882 // If the download is not complete, nothing to do. The required
883 // post-processing will be performed when it does complete.
884 if (download->state() != DownloadItem::COMPLETE)
885 return;
886
[email protected]ca4b5fa32010-10-09 12:42:18887 BrowserThread::PostTask(
888 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37889 NewRunnableMethod(
890 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
891 download->db_handle(), download->full_path(),
[email protected]44b94b82010-11-08 20:49:18892 download->target_name()));
[email protected]9ccbb372008-10-10 18:50:32893}
894
initial.commit09911bf2008-07-26 23:55:29895// Operations posted to us from the history service ----------------------------
896
897// The history service has retrieved all download entries. 'entries' contains
898// 'DownloadCreateInfo's in sorted order (by ascending start_time).
899void DownloadManager::OnQueryDownloadEntriesComplete(
900 std::vector<DownloadCreateInfo>* entries) {
901 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]aa033af2010-07-27 18:16:39902 DownloadItem* download = new DownloadItem(this, entries->at(i));
[email protected]f04182f32010-12-10 19:12:07903 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
904 downloads_.insert(download);
905 history_downloads_[download->db_handle()] = download;
[email protected]da6e3922010-11-24 21:45:50906 VLOG(20) << __FUNCTION__ << "()" << i << ">"
907 << " download = " << download->DebugString(true);
initial.commit09911bf2008-07-26 23:55:29908 }
[email protected]b0ab1d42010-02-24 19:29:28909 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29910}
911
initial.commit09911bf2008-07-26 23:55:29912// Once the new DownloadItem's creation info has been committed to the history
913// service, we associate the DownloadItem with the db handle, update our
[email protected]f04182f32010-12-10 19:12:07914// 'history_downloads_' map and inform observers.
[email protected]9f5cb562010-11-08 22:06:59915void DownloadManager::OnCreateDownloadEntryComplete(
[email protected]76d8b622010-11-11 19:50:05916 DownloadCreateInfo info,
[email protected]9f5cb562010-11-08 22:06:59917 int64 db_handle) {
initial.commit09911bf2008-07-26 23:55:29918 DownloadMap::iterator it = in_progress_.find(info.download_id);
919 DCHECK(it != in_progress_.end());
920
921 DownloadItem* download = it->second;
[email protected]da6e3922010-11-24 21:45:50922 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
923 << " download_id = " << info.download_id
924 << " download = " << download->DebugString(true);
[email protected]d2a8fb72010-01-21 05:31:42925
926 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
927 // call this function with an invalid |db_handle|. For instance, this can
928 // happen when the history database is offline. We cannot have multiple
929 // DownloadItems with the same invalid db_handle, so we need to assign a
930 // unique |db_handle| here.
[email protected]82f37b02010-07-29 22:04:57931 if (db_handle == DownloadHistory::kUninitializedHandle)
932 db_handle = download_history_->GetNextFakeDbHandle();
[email protected]d2a8fb72010-01-21 05:31:42933
[email protected]82f37b02010-07-29 22:04:57934 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle);
initial.commit09911bf2008-07-26 23:55:29935 download->set_db_handle(db_handle);
936
937 // Insert into our full map.
[email protected]f04182f32010-12-10 19:12:07938 DCHECK(history_downloads_.find(download->db_handle()) ==
939 history_downloads_.end());
940 history_downloads_[download->db_handle()] = download;
initial.commit09911bf2008-07-26 23:55:29941
[email protected]5e595482009-05-06 20:16:53942 // Show in the appropropriate browser UI.
943 ShowDownloadInBrowser(info, download);
initial.commit09911bf2008-07-26 23:55:29944
945 // Inform interested objects about the new download.
[email protected]b0ab1d42010-02-24 19:29:28946 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29947
948 // If this download has been completed before we've received the db handle,
949 // post one final message to the history service so that it can be properly
950 // in sync with the DownloadItem's completion status, and also inform any
951 // observers so that they get more than just the start notification.
952 if (download->state() != DownloadItem::IN_PROGRESS) {
953 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57954 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29955 download->UpdateObservers();
956 }
[email protected]6a7fb042010-02-01 16:30:47957
958 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29959}
960
[email protected]5e595482009-05-06 20:16:53961void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info,
962 DownloadItem* download) {
[email protected]8ddbd66a2010-05-21 16:38:34963 // The 'contents' may no longer exist if the user closed the tab before we
964 // get this start completion event. If it does, tell the origin TabContents
965 // to display its download shelf.
[email protected]76543b92009-08-31 17:27:45966 TabContents* contents = tab_util::GetTabContentsByID(info.child_id,
967 info.render_view_id);
[email protected]5e595482009-05-06 20:16:53968
969 // If the contents no longer exists, we start the download in the last active
970 // browser. This is not ideal but better than fully hiding the download from
971 // the user.
972 if (!contents) {
973 Browser* last_active = BrowserList::GetLastActive();
974 if (last_active)
975 contents = last_active->GetSelectedTabContents();
976 }
977
978 if (contents)
979 contents->OnStartDownload(download);
980}
981
[email protected]6cade212008-12-03 00:32:22982// Clears the last download path, used to initialize "save as" dialogs.
[email protected]905a08d2008-11-19 07:24:12983void DownloadManager::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:41984 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:38985}
[email protected]b0ab1d42010-02-24 19:29:28986
987void DownloadManager::NotifyModelChanged() {
988 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
989}
990
[email protected]2e030682010-07-23 19:45:36991DownloadItem* DownloadManager::GetDownloadItem(int id) {
[email protected]f04182f32010-12-10 19:12:07992 for (DownloadMap::iterator it = history_downloads_.begin();
993 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:36994 DownloadItem* item = it->second;
995 if (item->id() == id)
996 return item;
997 }
998 return NULL;
999}
1000
[email protected]f04182f32010-12-10 19:12:071001void DownloadManager::AssertContainersConsistent() const {
1002#if !defined(NDEBUG)
1003 // Confirm that everything in all maps is also in downloads_.
1004 const DownloadMap* maps[] = {&in_progress_, &history_downloads_};
1005 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(maps)); i++) {
1006 for (DownloadMap::const_iterator it = maps[i]->begin();
1007 it != maps[i]->end(); it++) {
1008 DCHECK_EQ(1u, downloads_.count(it->second));
1009 }
1010 }
1011 // Note that downloads_ also includes save page as downloads; that's not
1012 // checked.
1013#endif
1014}
1015
[email protected]b0ab1d42010-02-24 19:29:281016// DownloadManager::OtherDownloadManagerObserver implementation ----------------
1017
1018DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver(
1019 DownloadManager* observing_download_manager)
1020 : observing_download_manager_(observing_download_manager),
1021 observed_download_manager_(NULL) {
1022 if (observing_download_manager->profile_->GetOriginalProfile() ==
1023 observing_download_manager->profile_) {
1024 return;
1025 }
1026
1027 observed_download_manager_ = observing_download_manager_->
1028 profile_->GetOriginalProfile()->GetDownloadManager();
1029 observed_download_manager_->AddObserver(this);
1030}
1031
1032DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() {
1033 if (observed_download_manager_)
1034 observed_download_manager_->RemoveObserver(this);
1035}
1036
1037void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1038 observing_download_manager_->NotifyModelChanged();
1039}
1040
[email protected]b0ab1d42010-02-24 19:29:281041void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1042 observed_download_manager_ = NULL;
1043}