blob: 7faa64a72a8cb27e14c861c10eb2ef5dda6e2648 [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]1b5044d2009-02-24 00:04:1415#include "base/sys_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2916#include "base/task.h"
[email protected]ce7f62e32010-08-10 23:43:5917#include "base/utf_string_conversions.h"
[email protected]d2a8fb72010-01-21 05:31:4218#include "build/build_config.h"
[email protected]6c69796d2010-07-16 21:41:1619#include "chrome/browser/browser.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/browser/browser_list.h"
21#include "chrome/browser/browser_process.h"
[email protected]d83d03aa2009-11-02 21:44:3722#include "chrome/browser/chrome_thread.h"
[email protected]6c69796d2010-07-16 21:41:1623#include "chrome/browser/download/download_file_manager.h"
[email protected]82f37b02010-07-29 22:04:5724#include "chrome/browser/download/download_history.h"
[email protected]6c69796d2010-07-16 21:41:1625#include "chrome/browser/download/download_item.h"
[email protected]e5dc4222010-08-30 22:16:3226#include "chrome/browser/download/download_prefs.h"
[email protected]073ed7b2010-09-27 09:20:0227#include "chrome/browser/download/download_status_updater.h"
[email protected]e9ef0a62009-08-11 22:50:1328#include "chrome/browser/download/download_util.h"
[email protected]8f783752009-04-01 23:33:4529#include "chrome/browser/extensions/extensions_service.h"
[email protected]a0835ac2010-09-13 19:40:0830#include "chrome/browser/history/download_create_info.h"
[email protected]be180c802009-10-23 06:33:3131#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]14a000d2010-04-29 21:44:2432#include "chrome/browser/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2933#include "chrome/browser/profile.h"
[email protected]8c8657d62009-01-16 18:31:2634#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2535#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]21ca982c2010-01-26 22:49:5536#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3437#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]d2a8fb72010-01-21 05:31:4238#include "chrome/browser/tab_contents/tab_util.h"
initial.commit09911bf2008-07-26 23:55:2939#include "chrome/common/chrome_paths.h"
[email protected]91e1bd82009-09-03 22:04:4040#include "chrome/common/notification_service.h"
41#include "chrome/common/notification_type.h"
initial.commit09911bf2008-07-26 23:55:2942#include "chrome/common/pref_names.h"
[email protected]46072d42008-07-28 14:49:3543#include "googleurl/src/gurl.h"
[email protected]34ac8f32009-02-22 23:03:2744#include "grit/generated_resources.h"
[email protected]21ca982c2010-01-26 22:49:5545#include "grit/theme_resources.h"
initial.commit09911bf2008-07-26 23:55:2946#include "net/base/mime_util.h"
47#include "net/base/net_util.h"
initial.commit09911bf2008-07-26 23:55:2948
[email protected]b7f05882009-02-22 01:21:5649#if defined(OS_WIN)
[email protected]4a0765a2009-05-08 23:12:2550#include "app/win_util.h"
[email protected]a0a9577b2009-05-27 23:52:3251#endif
52
[email protected]073ed7b2010-09-27 09:20:0253DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater)
initial.commit09911bf2008-07-26 23:55:2954 : shutdown_needed_(false),
55 profile_(NULL),
[email protected]073ed7b2010-09-27 09:20:0256 file_manager_(NULL),
[email protected]a3d2bc42010-10-06 14:08:4957 status_updater_(status_updater->AsWeakPtr()) {
[email protected]073ed7b2010-09-27 09:20:0258 if (status_updater_)
59 status_updater_->AddDelegate(this);
initial.commit09911bf2008-07-26 23:55:2960}
61
62DownloadManager::~DownloadManager() {
[email protected]326a6a92010-09-10 20:21:1363 DCHECK(!shutdown_needed_);
[email protected]073ed7b2010-09-27 09:20:0264 if (status_updater_)
65 status_updater_->RemoveDelegate(this);
initial.commit09911bf2008-07-26 23:55:2966}
67
68void DownloadManager::Shutdown() {
[email protected]326a6a92010-09-10 20:21:1369 if (!shutdown_needed_)
70 return;
71 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:2972
[email protected]326a6a92010-09-10 20:21:1373 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
74
75 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:1876 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]326a6a92010-09-10 20:21:1377 NewRunnableMethod(file_manager_,
78 &DownloadFileManager::OnDownloadManagerShutdown,
[email protected]0d7e79fa2010-10-08 23:35:4779 this));
[email protected]326a6a92010-09-10 20:21:1380 }
initial.commit09911bf2008-07-26 23:55:2981
initial.commit09911bf2008-07-26 23:55:2982 // 'in_progress_' may contain DownloadItems that have not finished the start
83 // complete (from the history service) and thus aren't in downloads_.
84 DownloadMap::iterator it = in_progress_.begin();
[email protected]9ccbb372008-10-10 18:50:3285 std::set<DownloadItem*> to_remove;
initial.commit09911bf2008-07-26 23:55:2986 for (; it != in_progress_.end(); ++it) {
87 DownloadItem* download = it->second;
[email protected]9ccbb372008-10-10 18:50:3288 if (download->safety_state() == DownloadItem::DANGEROUS) {
89 // Forget about any download that the user did not approve.
90 // Note that we cannot call download->Remove() this would invalidate our
91 // iterator.
92 to_remove.insert(download);
93 continue;
initial.commit09911bf2008-07-26 23:55:2994 }
[email protected]9ccbb372008-10-10 18:50:3295 DCHECK_EQ(DownloadItem::IN_PROGRESS, download->state());
96 download->Cancel(false);
[email protected]82f37b02010-07-29 22:04:5797 download_history_->UpdateEntry(download);
98 if (download->db_handle() == DownloadHistory::kUninitializedHandle) {
initial.commit09911bf2008-07-26 23:55:2999 // An invalid handle means that 'download' does not yet exist in
100 // 'downloads_', so we have to delete it here.
101 delete download;
102 }
103 }
104
[email protected]9ccbb372008-10-10 18:50:32105 // 'dangerous_finished_' contains all complete downloads that have not been
106 // approved. They should be removed.
107 it = dangerous_finished_.begin();
108 for (; it != dangerous_finished_.end(); ++it)
109 to_remove.insert(it->second);
110
111 // Remove the dangerous download that are not approved.
112 for (std::set<DownloadItem*>::const_iterator rm_it = to_remove.begin();
113 rm_it != to_remove.end(); ++rm_it) {
114 DownloadItem* download = *rm_it;
[email protected]e10e17c72008-10-15 17:48:32115 int64 handle = download->db_handle();
[email protected]9ccbb372008-10-10 18:50:32116 download->Remove(true);
[email protected]e10e17c72008-10-15 17:48:32117 // Same as above, delete the download if it is not in 'downloads_' (as the
118 // Remove() call above won't have deleted it).
[email protected]82f37b02010-07-29 22:04:57119 if (handle == DownloadHistory::kUninitializedHandle)
[email protected]9ccbb372008-10-10 18:50:32120 delete download;
121 }
122 to_remove.clear();
123
initial.commit09911bf2008-07-26 23:55:29124 in_progress_.clear();
[email protected]9ccbb372008-10-10 18:50:32125 dangerous_finished_.clear();
initial.commit09911bf2008-07-26 23:55:29126 STLDeleteValues(&downloads_);
[email protected]ec4826a2010-09-21 09:15:59127 STLDeleteContainerPointers(save_page_downloads_.begin(),
128 save_page_downloads_.end());
initial.commit09911bf2008-07-26 23:55:29129
130 file_manager_ = NULL;
131
initial.commit09911bf2008-07-26 23:55:29132 // Make sure the save as dialog doesn't notify us back if we're gone before
133 // it returns.
134 if (select_file_dialog_.get())
135 select_file_dialog_->ListenerDestroyed();
136
[email protected]82f37b02010-07-29 22:04:57137 download_history_.reset();
138
initial.commit09911bf2008-07-26 23:55:29139 shutdown_needed_ = false;
140}
141
[email protected]82f37b02010-07-29 22:04:57142void DownloadManager::GetTemporaryDownloads(
143 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
144 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58145
146 for (DownloadMap::iterator it = downloads_.begin();
147 it != downloads_.end(); ++it) {
148 if (it->second->is_temporary() &&
149 it->second->full_path().DirName() == dir_path)
[email protected]82f37b02010-07-29 22:04:57150 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58151 }
[email protected]6aa4a1c02010-01-15 18:49:58152}
153
[email protected]82f37b02010-07-29 22:04:57154void DownloadManager::GetAllDownloads(
155 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
156 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34157
158 for (DownloadMap::iterator it = downloads_.begin();
159 it != downloads_.end(); ++it) {
160 if (!it->second->is_temporary() &&
161 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57162 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34163 }
[email protected]8ddbd66a2010-05-21 16:38:34164}
165
[email protected]82f37b02010-07-29 22:04:57166void DownloadManager::GetCurrentDownloads(
167 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
168 DCHECK(result);
[email protected]c4a530b2010-03-08 17:33:03169
170 for (DownloadMap::iterator it = downloads_.begin();
171 it != downloads_.end(); ++it) {
172 if (!it->second->is_temporary() &&
173 (it->second->state() == DownloadItem::IN_PROGRESS ||
174 it->second->safety_state() == DownloadItem::DANGEROUS) &&
175 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57176 result->push_back(it->second);
[email protected]c4a530b2010-03-08 17:33:03177 }
[email protected]f7e9fd62010-09-28 15:45:06178
179 // If we have a parent profile, let it add its downloads to the results.
180 Profile* original_profile = profile_->GetOriginalProfile();
181 if (original_profile != profile_)
182 original_profile->GetDownloadManager()->GetCurrentDownloads(dir_path,
183 result);
184
[email protected]c4a530b2010-03-08 17:33:03185}
186
[email protected]d3b12902010-08-16 23:39:42187void DownloadManager::SearchDownloads(const string16& query,
188 std::vector<DownloadItem*>* result) {
189 DCHECK(result);
190
191 string16 query_lower(l10n_util::ToLower(query));
192
193 for (DownloadMap::iterator it = downloads_.begin();
194 it != downloads_.end(); ++it) {
195 DownloadItem* download_item = it->second;
196
197 if (download_item->is_temporary() || download_item->is_extension_install())
198 continue;
199
200 // Display Incognito downloads only in Incognito window, and vice versa.
201 // The Incognito Downloads page will get the list of non-Incognito downloads
202 // from its parent profile.
203 if (profile_->IsOffTheRecord() != download_item->is_otr())
204 continue;
205
206 if (download_item->MatchesQuery(query_lower))
207 result->push_back(download_item);
208 }
209
210 // If we have a parent profile, let it add its downloads to the results.
211 Profile* original_profile = profile_->GetOriginalProfile();
212 if (original_profile != profile_)
213 original_profile->GetDownloadManager()->SearchDownloads(query, result);
214}
215
initial.commit09911bf2008-07-26 23:55:29216// Query the history service for information about all persisted downloads.
217bool DownloadManager::Init(Profile* profile) {
218 DCHECK(profile);
219 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
220 shutdown_needed_ = true;
221
222 profile_ = profile;
[email protected]be180c802009-10-23 06:33:31223 request_context_getter_ = profile_->GetRequestContext();
[email protected]d3b12902010-08-16 23:39:42224 download_history_.reset(new DownloadHistory(profile));
[email protected]82f37b02010-07-29 22:04:57225 download_history_->Load(
226 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete));
[email protected]024f2f02010-04-30 22:51:46227
[email protected]e5dc4222010-08-30 22:16:32228 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs()));
229
[email protected]2941c2392010-07-15 22:54:30230 // In test mode, there may be no ResourceDispatcherHost. In this case it's
231 // safe to avoid setting |file_manager_| because we only call a small set of
232 // functions, none of which need it.
initial.commit09911bf2008-07-26 23:55:29233 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
[email protected]2941c2392010-07-15 22:54:30234 if (rdh) {
235 file_manager_ = rdh->download_file_manager();
236 DCHECK(file_manager_);
initial.commit09911bf2008-07-26 23:55:29237 }
238
[email protected]b0ab1d42010-02-24 19:29:28239 other_download_manager_observer_.reset(
240 new OtherDownloadManagerObserver(this));
241
initial.commit09911bf2008-07-26 23:55:29242 return true;
243}
244
initial.commit09911bf2008-07-26 23:55:29245// We have received a message from DownloadFileManager about a new download. We
246// create a download item and store it in our download map, and inform the
247// history system of a new download. Since this method can be called while the
248// history service thread is still reading the persistent state, we do not
249// insert the new DownloadItem into 'downloads_' or inform our observers at this
250// point. OnCreateDatabaseEntryComplete() handles that finalization of the the
251// download creation as a callback from the history thread.
252void DownloadManager::StartDownload(DownloadCreateInfo* info) {
[email protected]ca4b5fa32010-10-09 12:42:18253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29254 DCHECK(info);
255
[email protected]a60c8ae2009-12-25 06:50:57256 // Check whether this download is for an extension install or not.
[email protected]e6875c12010-07-18 11:14:13257 // Allow extensions to be explicitly saved.
258 if (!info->prompt_user_for_save_location) {
[email protected]a60c8ae2009-12-25 06:50:57259 if (UserScript::HasUserScriptFileExtension(info->url) ||
260 info->mime_type == Extension::kMimeType)
261 info->is_extension_install = true;
262 }
263
[email protected]8af9d032010-02-10 00:00:32264 if (info->save_info.file_path.empty()) {
[email protected]2941c2392010-07-15 22:54:30265 FilePath generated_name;
[email protected]7ba53e12010-08-05 17:14:00266 download_util::GenerateFileNameFromInfo(info, &generated_name);
[email protected]2941c2392010-07-15 22:54:30267
268 // Freeze the user's preference for showing a Save As dialog. We're going
269 // to bounce around a bunch of threads and we don't want to worry about race
270 // conditions where the user changes this pref out from under us.
[email protected]e5dc4222010-08-30 22:16:32271 if (download_prefs_->prompt_for_download()) {
[email protected]2941c2392010-07-15 22:54:30272 // But ignore the user's preference for the following scenarios:
273 // 1) Extension installation. Note that we only care here about the case
274 // where an extension is installed, not when one is downloaded with
275 // "save as...".
276 // 2) Filetypes marked "always open." If the user just wants this file
277 // opened, don't bother asking where to keep it.
278 if (!info->is_extension_install &&
279 !ShouldOpenFileBasedOnExtension(generated_name))
[email protected]e6875c12010-07-18 11:14:13280 info->prompt_user_for_save_location = true;
[email protected]2941c2392010-07-15 22:54:30281 }
282
[email protected]8af9d032010-02-10 00:00:32283 // Determine the proper path for a download, by either one of the following:
284 // 1) using the default download directory.
285 // 2) prompting the user.
[email protected]80dc3612010-07-27 19:35:08286 if (info->prompt_user_for_save_location && !last_download_path_.empty()){
[email protected]8af9d032010-02-10 00:00:32287 info->suggested_path = last_download_path_;
[email protected]80dc3612010-07-27 19:35:08288 } else {
[email protected]e5dc4222010-08-30 22:16:32289 info->suggested_path = download_prefs_->download_path();
[email protected]80dc3612010-07-27 19:35:08290 }
[email protected]8af9d032010-02-10 00:00:32291 info->suggested_path = info->suggested_path.Append(generated_name);
292 } else {
293 info->suggested_path = info->save_info.file_path;
294 }
initial.commit09911bf2008-07-26 23:55:29295
[email protected]e6875c12010-07-18 11:14:13296 if (!info->prompt_user_for_save_location &&
297 info->save_info.file_path.empty()) {
[email protected]d3071992010-10-08 15:24:07298 info->is_dangerous = download_util::IsDangerous(info, profile());
[email protected]e9ebf3fc2008-10-17 22:06:58299 }
300
initial.commit09911bf2008-07-26 23:55:29301 // We need to move over to the download thread because we don't want to stat
302 // the suggested path on the UI thread.
[email protected]5a3b97e2010-10-05 09:49:11303 // We can only access preferences on the UI thread, so check the download path
304 // now and pass the value to the FILE thread.
[email protected]ca4b5fa32010-10-09 12:42:18305 BrowserThread::PostTask(
306 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37307 NewRunnableMethod(
[email protected]5a3b97e2010-10-05 09:49:11308 this,
309 &DownloadManager::CheckIfSuggestedPathExists,
310 info,
311 download_prefs()->download_path()));
initial.commit09911bf2008-07-26 23:55:29312}
313
[email protected]5a3b97e2010-10-05 09:49:11314void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info,
315 const FilePath& default_path) {
[email protected]ca4b5fa32010-10-09 12:42:18316 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
initial.commit09911bf2008-07-26 23:55:29317 DCHECK(info);
318
[email protected]5a3b97e2010-10-05 09:49:11319 // Make sure the default download directory exists.
320 // TODO(phajdan.jr): only create the directory when we're sure the user
321 // is going to save there and not to another directory of his choice.
322 file_util::CreateDirectory(default_path);
323
initial.commit09911bf2008-07-26 23:55:29324 // Check writability of the suggested path. If we can't write to it, default
325 // to the user's "My Documents" directory. We'll prompt them in this case.
[email protected]7ae7c2cb2009-01-06 23:31:41326 FilePath dir = info->suggested_path.DirName();
327 FilePath filename = info->suggested_path.BaseName();
[email protected]9ccbb372008-10-10 18:50:32328 if (!file_util::PathIsWritable(dir)) {
[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]594cd7d2010-07-21 03:23:56342 SStringPrintf(&file_name, FILE_PATH_LITERAL("unconfirmed %d.crdownload"),
[email protected]9ccbb372008-10-10 18:50:32343 base::RandInt(0, 100000));
[email protected]7ae7c2cb2009-01-06 23:31:41344 path = dir.Append(file_name);
[email protected]7d3851d82008-12-12 03:26:07345 if (file_util::PathExists(path))
[email protected]7ae7c2cb2009-01-06 23:31:41346 path = FilePath();
[email protected]9ccbb372008-10-10 18:50:32347 }
348 info->suggested_path = path;
[email protected]7a256ea2008-10-17 17:34:16349 } else {
[email protected]594cd7d2010-07-21 03:23:56350 // Do not add the path uniquifier if we are saving to a specific path as in
351 // the drag-out case.
352 if (info->save_info.file_path.empty()) {
353 info->path_uniquifier = download_util::GetUniquePathNumberWithCrDownload(
354 info->suggested_path);
355 }
[email protected]7a256ea2008-10-17 17:34:16356 // We know the final path, build it if necessary.
357 if (info->path_uniquifier > 0) {
[email protected]5a2388a2010-03-26 16:13:39358 download_util::AppendNumberToPath(&(info->suggested_path),
359 info->path_uniquifier);
[email protected]7a256ea2008-10-17 17:34:16360 // Setting path_uniquifier to 0 to make sure we don't try to unique it
361 // later on.
362 info->path_uniquifier = 0;
[email protected]7d3851d82008-12-12 03:26:07363 } else if (info->path_uniquifier == -1) {
364 // We failed to find a unique path. We have to prompt the user.
[email protected]e6875c12010-07-18 11:14:13365 info->prompt_user_for_save_location = true;
[email protected]7a256ea2008-10-17 17:34:16366 }
[email protected]9ccbb372008-10-10 18:50:32367 }
368
[email protected]594cd7d2010-07-21 03:23:56369 // Create an empty file at the suggested path so that we don't allocate the
370 // same "non-existant" path to multiple downloads.
371 // See: http://code.google.com/p/chromium/issues/detail?id=3662
[email protected]e6875c12010-07-18 11:14:13372 if (!info->prompt_user_for_save_location &&
373 info->save_info.file_path.empty()) {
[email protected]594cd7d2010-07-21 03:23:56374 if (info->is_dangerous)
375 file_util::WriteFile(info->suggested_path, "", 0);
376 else
377 file_util::WriteFile(download_util::GetCrDownloadPath(
378 info->suggested_path), "", 0);
[email protected]7d3851d82008-12-12 03:26:07379 }
380
[email protected]ca4b5fa32010-10-09 12:42:18381 BrowserThread::PostTask(
382 BrowserThread::UI, FROM_HERE,
initial.commit09911bf2008-07-26 23:55:29383 NewRunnableMethod(this,
384 &DownloadManager::OnPathExistenceAvailable,
385 info));
386}
387
388void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) {
[email protected]ca4b5fa32010-10-09 12:42:18389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29390 DCHECK(info);
391
[email protected]e6875c12010-07-18 11:14:13392 if (info->prompt_user_for_save_location) {
initial.commit09911bf2008-07-26 23:55:29393 // We must ask the user for the place to put the download.
394 if (!select_file_dialog_.get())
395 select_file_dialog_ = SelectFileDialog::Create(this);
396
[email protected]76543b92009-08-31 17:27:45397 TabContents* contents = tab_util::GetTabContentsByID(info->child_id,
398 info->render_view_id);
[email protected]b949f1112009-04-12 20:03:08399 SelectFileDialog::FileTypeInfo file_type_info;
400 file_type_info.extensions.resize(1);
401 file_type_info.extensions[0].push_back(info->suggested_path.Extension());
[email protected]15bc8052009-04-17 19:57:24402 if (!file_type_info.extensions[0][0].empty())
403 file_type_info.extensions[0][0].erase(0, 1); // drop the .
[email protected]b949f1112009-04-12 20:03:08404 file_type_info.include_all_files = true;
[email protected]076700e62009-04-01 18:41:23405 gfx::NativeWindow owning_window =
406 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL;
initial.commit09911bf2008-07-26 23:55:29407 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE,
[email protected]561abe62009-04-06 18:08:34408 string16(),
409 info->suggested_path,
[email protected]b949f1112009-04-12 20:03:08410 &file_type_info, 0, FILE_PATH_LITERAL(""),
[email protected]0f44d3e2009-03-12 23:36:30411 owning_window, info);
initial.commit09911bf2008-07-26 23:55:29412 } else {
413 // No prompting for download, just continue with the suggested name.
[email protected]a850ba42010-09-10 22:00:30414 CreateDownloadItem(info, info->suggested_path);
initial.commit09911bf2008-07-26 23:55:29415 }
416}
417
[email protected]a850ba42010-09-10 22:00:30418void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info,
419 const FilePath& target_path) {
[email protected]ca4b5fa32010-10-09 12:42:18420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]aa033af2010-07-27 18:16:39421
initial.commit09911bf2008-07-26 23:55:29422 scoped_ptr<DownloadCreateInfo> infop(info);
423 info->path = target_path;
424
[email protected]aa033af2010-07-27 18:16:39425 DownloadItem* download = new DownloadItem(this, *info,
426 profile_->IsOffTheRecord());
427 DCHECK(!ContainsKey(in_progress_, info->download_id));
428 in_progress_[info->download_id] = download;
initial.commit09911bf2008-07-26 23:55:29429
[email protected]aa033af2010-07-27 18:16:39430 bool download_finished = ContainsKey(pending_finished_downloads_,
431 info->download_id);
[email protected]594cd7d2010-07-21 03:23:56432
433 if (download_finished || info->is_dangerous) {
434 // The download has already finished or the download is not safe.
435 // We can now rename the file to its final name (or its tentative name
436 // in dangerous download cases).
[email protected]ca4b5fa32010-10-09 12:42:18437 BrowserThread::PostTask(
438 BrowserThread::FILE, FROM_HERE,
[email protected]594cd7d2010-07-21 03:23:56439 NewRunnableMethod(
[email protected]0d7e79fa2010-10-08 23:35:47440 file_manager_, &DownloadFileManager::OnFinalDownloadName,
441 download->id(), target_path, !info->is_dangerous, this));
[email protected]594cd7d2010-07-21 03:23:56442 } else {
443 // The download hasn't finished and it is a safe download. We need to
444 // rename it to its intermediate '.crdownload' path.
445 FilePath download_path = download_util::GetCrDownloadPath(target_path);
[email protected]ca4b5fa32010-10-09 12:42:18446 BrowserThread::PostTask(
447 BrowserThread::FILE, FROM_HERE,
[email protected]594cd7d2010-07-21 03:23:56448 NewRunnableMethod(
[email protected]0d7e79fa2010-10-08 23:35:47449 file_manager_, &DownloadFileManager::OnIntermediateDownloadName,
450 download->id(), download_path, this));
[email protected]594cd7d2010-07-21 03:23:56451 download->set_need_final_rename(true);
452 }
453
454 if (download_finished) {
455 // If the download already completed by the time we reached this point, then
456 // notify observers that it did.
[email protected]a850ba42010-09-10 22:00:30457 OnAllDataSaved(info->download_id,
458 pending_finished_downloads_[info->download_id]);
[email protected]594cd7d2010-07-21 03:23:56459 }
initial.commit09911bf2008-07-26 23:55:29460
461 download->Rename(target_path);
462
[email protected]82f37b02010-07-29 22:04:57463 download_history_->AddEntry(*info, download,
464 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete));
[email protected]6a7fb042010-02-01 16:30:47465
466 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29467}
468
initial.commit09911bf2008-07-26 23:55:29469void DownloadManager::UpdateDownload(int32 download_id, int64 size) {
470 DownloadMap::iterator it = in_progress_.find(download_id);
471 if (it != in_progress_.end()) {
472 DownloadItem* download = it->second;
473 download->Update(size);
[email protected]82f37b02010-07-29 22:04:57474 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29475 }
[email protected]6a7fb042010-02-01 16:30:47476 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29477}
478
[email protected]a850ba42010-09-10 22:00:30479void DownloadManager::OnAllDataSaved(int32 download_id, int64 size) {
initial.commit09911bf2008-07-26 23:55:29480 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]9ccbb372008-10-10 18:50:32481 if (it == in_progress_.end()) {
initial.commit09911bf2008-07-26 23:55:29482 // The download is done, but the user hasn't selected a final location for
483 // it yet (the Save As dialog box is probably still showing), so just keep
484 // track of the fact that this download id is complete, when the
485 // DownloadItem is constructed later we'll notify its completion then.
486 PendingFinishedMap::iterator erase_it =
487 pending_finished_downloads_.find(download_id);
488 DCHECK(erase_it == pending_finished_downloads_.end());
489 pending_finished_downloads_[download_id] = size;
[email protected]9ccbb372008-10-10 18:50:32490 return;
initial.commit09911bf2008-07-26 23:55:29491 }
[email protected]9ccbb372008-10-10 18:50:32492
493 // Remove the id from the list of pending ids.
494 PendingFinishedMap::iterator erase_it =
495 pending_finished_downloads_.find(download_id);
496 if (erase_it != pending_finished_downloads_.end())
497 pending_finished_downloads_.erase(erase_it);
498
499 DownloadItem* download = it->second;
[email protected]a850ba42010-09-10 22:00:30500 download->OnAllDataSaved(size);
[email protected]9ccbb372008-10-10 18:50:32501
502 // Clean up will happen when the history system create callback runs if we
503 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57504 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
[email protected]9ccbb372008-10-10 18:50:32505 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57506 download_history_->UpdateEntry(download);
[email protected]9ccbb372008-10-10 18:50:32507 }
508
[email protected]6a7fb042010-02-01 16:30:47509 UpdateAppIcon();
510
[email protected]9ccbb372008-10-10 18:50:32511 // If this a dangerous download not yet validated by the user, don't do
512 // anything. When the user notifies us, it will trigger a call to
513 // ProceedWithFinishedDangerousDownload.
514 if (download->safety_state() == DownloadItem::DANGEROUS) {
515 dangerous_finished_[download_id] = download;
516 return;
517 }
518
519 if (download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) {
[email protected]6cade212008-12-03 00:32:22520 // We first need to rename the downloaded file from its temporary name to
[email protected]9ccbb372008-10-10 18:50:32521 // its final name before we can continue.
[email protected]ca4b5fa32010-10-09 12:42:18522 BrowserThread::PostTask(
523 BrowserThread::FILE, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:32524 NewRunnableMethod(
525 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
526 download->db_handle(),
527 download->full_path(), download->original_name()));
528 return;
529 }
[email protected]594cd7d2010-07-21 03:23:56530
531 if (download->need_final_rename()) {
[email protected]ca4b5fa32010-10-09 12:42:18532 BrowserThread::PostTask(
533 BrowserThread::FILE, FROM_HERE,
[email protected]594cd7d2010-07-21 03:23:56534 NewRunnableMethod(
[email protected]0d7e79fa2010-10-08 23:35:47535 file_manager_, &DownloadFileManager::OnFinalDownloadName,
536 download->id(), download->full_path(), false, this));
[email protected]594cd7d2010-07-21 03:23:56537 return;
538 }
539
[email protected]9ccbb372008-10-10 18:50:32540 ContinueDownloadFinished(download);
541}
542
[email protected]8f783752009-04-01 23:33:45543void DownloadManager::DownloadRenamedToFinalName(int download_id,
544 const FilePath& full_path) {
[email protected]ca4b5fa32010-10-09 12:42:18545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]aa033af2010-07-27 18:16:39546
[email protected]2e030682010-07-23 19:45:36547 DownloadItem* item = GetDownloadItem(download_id);
548 if (!item)
549 return;
550 item->OnNameFinalized();
[email protected]594cd7d2010-07-21 03:23:56551
[email protected]2e030682010-07-23 19:45:36552 // This was called from DownloadFinished; continue to call
553 // ContinueDownloadFinished.
554 if (item->need_final_rename()) {
555 item->set_need_final_rename(false);
556 ContinueDownloadFinished(item);
[email protected]6aa4a1c02010-01-15 18:49:58557 }
[email protected]8f783752009-04-01 23:33:45558}
559
[email protected]9ccbb372008-10-10 18:50:32560void DownloadManager::ContinueDownloadFinished(DownloadItem* download) {
[email protected]ca4b5fa32010-10-09 12:42:18561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]aa033af2010-07-27 18:16:39562
[email protected]9ccbb372008-10-10 18:50:32563 // If this was a dangerous download, it has now been approved and must be
564 // removed from dangerous_finished_ so it does not get deleted on shutdown.
[email protected]aa033af2010-07-27 18:16:39565 dangerous_finished_.erase(download->id());
[email protected]9ccbb372008-10-10 18:50:32566
[email protected]a850ba42010-09-10 22:00:30567 download->Finished();
[email protected]0aad67b2009-07-15 20:34:28568}
[email protected]eccb9d12009-10-28 05:40:09569
[email protected]9ccbb372008-10-10 18:50:32570// Called on the file thread. Renames the downloaded file to its original name.
571void DownloadManager::ProceedWithFinishedDangerousDownload(
572 int64 download_handle,
[email protected]7ae7c2cb2009-01-06 23:31:41573 const FilePath& path,
574 const FilePath& original_name) {
[email protected]9ccbb372008-10-10 18:50:32575 bool success = false;
[email protected]7ae7c2cb2009-01-06 23:31:41576 FilePath new_path;
[email protected]7a256ea2008-10-17 17:34:16577 int uniquifier = 0;
[email protected]9ccbb372008-10-10 18:50:32578 if (file_util::PathExists(path)) {
[email protected]889ed35c2009-01-21 00:07:24579 new_path = path.DirName().Append(original_name);
[email protected]7a256ea2008-10-17 17:34:16580 // Make our name unique at this point, as if a dangerous file is downloading
581 // and a 2nd download is started for a file with the same name, they would
582 // have the same path. This is because we uniquify the name on download
583 // start, and at that time the first file does not exists yet, so the second
584 // file gets the same name.
[email protected]5a2388a2010-03-26 16:13:39585 uniquifier = download_util::GetUniquePathNumber(new_path);
[email protected]7a256ea2008-10-17 17:34:16586 if (uniquifier > 0)
[email protected]5a2388a2010-03-26 16:13:39587 download_util::AppendNumberToPath(&new_path, uniquifier);
[email protected]9ccbb372008-10-10 18:50:32588 success = file_util::Move(path, new_path);
589 } else {
590 NOTREACHED();
591 }
[email protected]6cade212008-12-03 00:32:22592
[email protected]ca4b5fa32010-10-09 12:42:18593 BrowserThread::PostTask(
594 BrowserThread::UI, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:32595 NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed,
[email protected]7a256ea2008-10-17 17:34:16596 download_handle, success, new_path, uniquifier));
[email protected]9ccbb372008-10-10 18:50:32597}
598
599// Call from the file thread when the finished dangerous download was renamed.
600void DownloadManager::DangerousDownloadRenamed(int64 download_handle,
601 bool success,
[email protected]7ae7c2cb2009-01-06 23:31:41602 const FilePath& new_path,
[email protected]7a256ea2008-10-17 17:34:16603 int new_path_uniquifier) {
[email protected]9ccbb372008-10-10 18:50:32604 DownloadMap::iterator it = downloads_.find(download_handle);
605 if (it == downloads_.end()) {
606 NOTREACHED();
607 return;
608 }
609
610 DownloadItem* download = it->second;
611 // If we failed to rename the file, we'll just keep the name as is.
[email protected]7a256ea2008-10-17 17:34:16612 if (success) {
613 // We need to update the path uniquifier so that the UI shows the right
614 // name when calling GetFileName().
615 download->set_path_uniquifier(new_path_uniquifier);
[email protected]9ccbb372008-10-10 18:50:32616 RenameDownload(download, new_path);
[email protected]7a256ea2008-10-17 17:34:16617 }
[email protected]9ccbb372008-10-10 18:50:32618
619 // Continue the download finished sequence.
620 ContinueDownloadFinished(download);
initial.commit09911bf2008-07-26 23:55:29621}
622
initial.commit09911bf2008-07-26 23:55:29623void DownloadManager::DownloadCancelled(int32 download_id) {
624 DownloadMap::iterator it = in_progress_.find(download_id);
625 if (it == in_progress_.end())
626 return;
627 DownloadItem* download = it->second;
628
initial.commit09911bf2008-07-26 23:55:29629 // Clean up will happen when the history system create callback runs if we
630 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57631 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
initial.commit09911bf2008-07-26 23:55:29632 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57633 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29634 }
635
[email protected]d7d1c5c2009-08-05 23:52:50636 DownloadCancelledInternal(download_id,
637 download->render_process_id(),
638 download->request_id());
[email protected]6a7fb042010-02-01 16:30:47639 UpdateAppIcon();
[email protected]d7d1c5c2009-08-05 23:52:50640}
641
642void DownloadManager::DownloadCancelledInternal(int download_id,
643 int render_process_id,
644 int request_id) {
[email protected]d85cf072009-10-27 03:59:31645 // Cancel the network request. RDH is guaranteed to outlive the IO thread.
[email protected]ca4b5fa32010-10-09 12:42:18646 BrowserThread::PostTask(
647 BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26648 NewRunnableFunction(&download_util::CancelDownloadRequest,
[email protected]d85cf072009-10-27 03:59:31649 g_browser_process->resource_dispatcher_host(),
650 render_process_id,
651 request_id));
[email protected]d7d1c5c2009-08-05 23:52:50652
[email protected]ca4b5fa32010-10-09 12:42:18653 BrowserThread::PostTask(
654 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37655 NewRunnableMethod(
656 file_manager_, &DownloadFileManager::CancelDownload, download_id));
initial.commit09911bf2008-07-26 23:55:29657}
658
659void DownloadManager::PauseDownload(int32 download_id, bool pause) {
660 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]d85cf072009-10-27 03:59:31661 if (it == in_progress_.end())
662 return;
initial.commit09911bf2008-07-26 23:55:29663
[email protected]d85cf072009-10-27 03:59:31664 DownloadItem* download = it->second;
665 if (pause == download->is_paused())
666 return;
initial.commit09911bf2008-07-26 23:55:29667
[email protected]ca4b5fa32010-10-09 12:42:18668 BrowserThread::PostTask(
669 BrowserThread::IO, FROM_HERE,
[email protected]e2abdeb22010-09-03 19:56:15670 NewRunnableMethod(this,
671 &DownloadManager::PauseDownloadRequest,
672 g_browser_process->resource_dispatcher_host(),
[email protected]7ba53e12010-08-05 17:14:00673 download->render_process_id(),
674 download->request_id(),
675 pause));
[email protected]9ccbb372008-10-10 18:50:32676}
677
[email protected]6a7fb042010-02-01 16:30:47678void DownloadManager::UpdateAppIcon() {
[email protected]073ed7b2010-09-27 09:20:02679 if (status_updater_)
680 status_updater_->Update();
[email protected]6a7fb042010-02-01 16:30:47681}
682
[email protected]9ccbb372008-10-10 18:50:32683void DownloadManager::RenameDownload(DownloadItem* download,
[email protected]7ae7c2cb2009-01-06 23:31:41684 const FilePath& new_path) {
[email protected]9ccbb372008-10-10 18:50:32685 download->Rename(new_path);
[email protected]82f37b02010-07-29 22:04:57686 download_history_->UpdateDownloadPath(download, new_path);
[email protected]9ccbb372008-10-10 18:50:32687}
688
[email protected]e2abdeb22010-09-03 19:56:15689void DownloadManager::PauseDownloadRequest(ResourceDispatcherHost* rdh,
690 int render_process_id,
691 int request_id,
692 bool pause) {
[email protected]ca4b5fa32010-10-09 12:42:18693 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]e2abdeb22010-09-03 19:56:15694 rdh->PauseRequest(render_process_id, request_id, pause);
695}
696
initial.commit09911bf2008-07-26 23:55:29697void DownloadManager::RemoveDownload(int64 download_handle) {
698 DownloadMap::iterator it = downloads_.find(download_handle);
699 if (it == downloads_.end())
700 return;
701
702 // Make history update.
703 DownloadItem* download = it->second;
[email protected]82f37b02010-07-29 22:04:57704 download_history_->RemoveEntry(download);
initial.commit09911bf2008-07-26 23:55:29705
706 // Remove from our tables and delete.
707 downloads_.erase(it);
[email protected]9ccbb372008-10-10 18:50:32708 it = dangerous_finished_.find(download->id());
709 if (it != dangerous_finished_.end())
710 dangerous_finished_.erase(it);
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
initial.commit09911bf2008-07-26 23:55:29722 DownloadMap::iterator it = downloads_.begin();
[email protected]78b8fcc92009-03-31 17:36:28723 std::vector<DownloadItem*> pending_deletes;
initial.commit09911bf2008-07-26 23:55:29724 while (it != downloads_.end()) {
725 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]b7f05882009-02-22 01:21:56732 downloads_.erase(it++);
[email protected]a6604d92008-10-30 00:58:58733
734 // Also remove it from any completed dangerous downloads.
735 DownloadMap::iterator dit = dangerous_finished_.find(download->id());
736 if (dit != dangerous_finished_.end())
737 dangerous_finished_.erase(dit);
738
[email protected]78b8fcc92009-03-31 17:36:28739 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29740
initial.commit09911bf2008-07-26 23:55:29741 continue;
742 }
743
744 ++it;
745 }
746
747 // Tell observers to refresh their views.
[email protected]78b8fcc92009-03-31 17:36:28748 int num_deleted = static_cast<int>(pending_deletes.size());
initial.commit09911bf2008-07-26 23:55:29749 if (num_deleted > 0)
[email protected]b0ab1d42010-02-24 19:29:28750 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29751
[email protected]78b8fcc92009-03-31 17:36:28752 // Delete the download items after updating the observers.
753 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
754 pending_deletes.clear();
755
initial.commit09911bf2008-07-26 23:55:29756 return num_deleted;
757}
758
[email protected]e93d2822009-01-30 05:59:59759int DownloadManager::RemoveDownloads(const base::Time remove_begin) {
760 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29761}
762
[email protected]d41355e6f2009-04-07 21:21:12763int DownloadManager::RemoveAllDownloads() {
[email protected]024f2f02010-04-30 22:51:46764 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) {
765 // This is an incognito downloader. Clear All should clear main download
766 // manager as well.
767 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads();
768 }
[email protected]d41355e6f2009-04-07 21:21:12769 // The null times make the date range unbounded.
770 return RemoveDownloadsBetween(base::Time(), base::Time());
771}
772
[email protected]ec4826a2010-09-21 09:15:59773void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download_item) {
774 save_page_downloads_.push_back(download_item);
775}
776
initial.commit09911bf2008-07-26 23:55:29777// Initiate a download of a specific URL. We send the request to the
778// ResourceDispatcherHost, and let it send us responses like a regular
779// download.
780void DownloadManager::DownloadUrl(const GURL& url,
781 const GURL& referrer,
[email protected]c9825a42009-05-01 22:51:50782 const std::string& referrer_charset,
[email protected]57c6a652009-05-04 07:58:34783 TabContents* tab_contents) {
[email protected]ae8945192010-07-20 16:56:26784 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
785 tab_contents);
[email protected]6aa4a1c02010-01-15 18:49:58786}
787
788void DownloadManager::DownloadUrlToFile(const GURL& url,
789 const GURL& referrer,
790 const std::string& referrer_charset,
[email protected]8af9d032010-02-10 00:00:32791 const DownloadSaveInfo& save_info,
[email protected]6aa4a1c02010-01-15 18:49:58792 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:34793 DCHECK(tab_contents);
[email protected]ca4b5fa32010-10-09 12:42:18794 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26795 NewRunnableFunction(&download_util::DownloadUrl,
796 url,
797 referrer,
798 referrer_charset,
799 save_info,
800 g_browser_process->resource_dispatcher_host(),
801 tab_contents->GetRenderProcessHost()->id(),
802 tab_contents->render_view_host()->routing_id(),
803 request_context_getter_));
initial.commit09911bf2008-07-26 23:55:29804}
805
initial.commit09911bf2008-07-26 23:55:29806void DownloadManager::AddObserver(Observer* observer) {
807 observers_.AddObserver(observer);
808 observer->ModelChanged();
809}
810
811void DownloadManager::RemoveObserver(Observer* observer) {
812 observers_.RemoveObserver(observer);
813}
814
[email protected]eccb9d12009-10-28 05:40:09815bool DownloadManager::ShouldOpenFileBasedOnExtension(
816 const FilePath& path) const {
[email protected]eccb9d12009-10-28 05:40:09817 FilePath::StringType extension = path.Extension();
818 if (extension.empty())
819 return false;
[email protected]7ba53e12010-08-05 17:14:00820 if (download_util::IsExecutableExtension(extension))
[email protected]eccb9d12009-10-28 05:40:09821 return false;
[email protected]92e11c82010-01-13 06:39:56822 if (Extension::IsExtension(path))
823 return false;
[email protected]eccb9d12009-10-28 05:40:09824 DCHECK(extension[0] == FilePath::kExtensionSeparator);
825 extension.erase(0, 1);
[email protected]e5dc4222010-08-30 22:16:32826 return download_prefs_->IsAutoOpenEnabledForExtension(extension);
initial.commit09911bf2008-07-26 23:55:29827}
828
[email protected]073ed7b2010-09-27 09:20:02829bool DownloadManager::IsDownloadProgressKnown() {
830 for (DownloadMap::iterator i = in_progress_.begin();
831 i != in_progress_.end(); ++i) {
832 if (i->second->total_bytes() <= 0)
833 return false;
834 }
835
836 return true;
837}
838
839int64 DownloadManager::GetInProgressDownloadCount() {
840 return in_progress_.size();
841}
842
843int64 DownloadManager::GetReceivedDownloadBytes() {
844 DCHECK(IsDownloadProgressKnown());
845 int64 received_bytes = 0;
846 for (DownloadMap::iterator i = in_progress_.begin();
847 i != in_progress_.end(); ++i) {
848 received_bytes += i->second->received_bytes();
849 }
850 return received_bytes;
851}
852
853int64 DownloadManager::GetTotalDownloadBytes() {
854 DCHECK(IsDownloadProgressKnown());
855 int64 total_bytes = 0;
856 for (DownloadMap::iterator i = in_progress_.begin();
857 i != in_progress_.end(); ++i) {
858 total_bytes += i->second->total_bytes();
859 }
860 return total_bytes;
861}
862
[email protected]561abe62009-04-06 18:08:34863void DownloadManager::FileSelected(const FilePath& path,
[email protected]23b357b2009-03-30 20:02:36864 int index, void* params) {
initial.commit09911bf2008-07-26 23:55:29865 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]e6875c12010-07-18 11:14:13866 if (info->prompt_user_for_save_location)
[email protected]7ae7c2cb2009-01-06 23:31:41867 last_download_path_ = path.DirName();
[email protected]a850ba42010-09-10 22:00:30868 CreateDownloadItem(info, path);
initial.commit09911bf2008-07-26 23:55:29869}
870
871void DownloadManager::FileSelectionCanceled(void* params) {
872 // The user didn't pick a place to save the file, so need to cancel the
873 // download that's already in progress to the temporary location.
874 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]d7d1c5c2009-08-05 23:52:50875 DownloadCancelledInternal(info->download_id,
[email protected]76543b92009-08-31 17:27:45876 info->child_id,
[email protected]d7d1c5c2009-08-05 23:52:50877 info->request_id);
initial.commit09911bf2008-07-26 23:55:29878}
879
[email protected]9ccbb372008-10-10 18:50:32880void DownloadManager::DangerousDownloadValidated(DownloadItem* download) {
881 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state());
882 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED);
883 download->UpdateObservers();
884
885 // If the download is not complete, nothing to do. The required
886 // post-processing will be performed when it does complete.
887 if (download->state() != DownloadItem::COMPLETE)
888 return;
889
[email protected]ca4b5fa32010-10-09 12:42:18890 BrowserThread::PostTask(
891 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37892 NewRunnableMethod(
893 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
894 download->db_handle(), download->full_path(),
895 download->original_name()));
[email protected]9ccbb372008-10-10 18:50:32896}
897
initial.commit09911bf2008-07-26 23:55:29898// Operations posted to us from the history service ----------------------------
899
900// The history service has retrieved all download entries. 'entries' contains
901// 'DownloadCreateInfo's in sorted order (by ascending start_time).
902void DownloadManager::OnQueryDownloadEntriesComplete(
903 std::vector<DownloadCreateInfo>* entries) {
904 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]aa033af2010-07-27 18:16:39905 DownloadItem* download = new DownloadItem(this, entries->at(i));
906 DCHECK(!ContainsKey(downloads_, download->db_handle()));
initial.commit09911bf2008-07-26 23:55:29907 downloads_[download->db_handle()] = download;
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
914// 'downloads_' map and inform observers.
915void DownloadManager::OnCreateDownloadEntryComplete(DownloadCreateInfo info,
916 int64 db_handle) {
917 DownloadMap::iterator it = in_progress_.find(info.download_id);
918 DCHECK(it != in_progress_.end());
919
920 DownloadItem* download = it->second;
[email protected]d2a8fb72010-01-21 05:31:42921
922 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
923 // call this function with an invalid |db_handle|. For instance, this can
924 // happen when the history database is offline. We cannot have multiple
925 // DownloadItems with the same invalid db_handle, so we need to assign a
926 // unique |db_handle| here.
[email protected]82f37b02010-07-29 22:04:57927 if (db_handle == DownloadHistory::kUninitializedHandle)
928 db_handle = download_history_->GetNextFakeDbHandle();
[email protected]d2a8fb72010-01-21 05:31:42929
[email protected]82f37b02010-07-29 22:04:57930 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle);
initial.commit09911bf2008-07-26 23:55:29931 download->set_db_handle(db_handle);
932
933 // Insert into our full map.
934 DCHECK(downloads_.find(download->db_handle()) == downloads_.end());
935 downloads_[download->db_handle()] = download;
936
[email protected]5e595482009-05-06 20:16:53937 // Show in the appropropriate browser UI.
938 ShowDownloadInBrowser(info, download);
initial.commit09911bf2008-07-26 23:55:29939
940 // Inform interested objects about the new download.
[email protected]b0ab1d42010-02-24 19:29:28941 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29942
943 // If this download has been completed before we've received the db handle,
944 // post one final message to the history service so that it can be properly
945 // in sync with the DownloadItem's completion status, and also inform any
946 // observers so that they get more than just the start notification.
947 if (download->state() != DownloadItem::IN_PROGRESS) {
948 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57949 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29950 download->UpdateObservers();
951 }
[email protected]6a7fb042010-02-01 16:30:47952
953 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29954}
955
[email protected]5e595482009-05-06 20:16:53956void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info,
957 DownloadItem* download) {
[email protected]8ddbd66a2010-05-21 16:38:34958 // The 'contents' may no longer exist if the user closed the tab before we
959 // get this start completion event. If it does, tell the origin TabContents
960 // to display its download shelf.
[email protected]76543b92009-08-31 17:27:45961 TabContents* contents = tab_util::GetTabContentsByID(info.child_id,
962 info.render_view_id);
[email protected]5e595482009-05-06 20:16:53963
964 // If the contents no longer exists, we start the download in the last active
965 // browser. This is not ideal but better than fully hiding the download from
966 // the user.
967 if (!contents) {
968 Browser* last_active = BrowserList::GetLastActive();
969 if (last_active)
970 contents = last_active->GetSelectedTabContents();
971 }
972
973 if (contents)
974 contents->OnStartDownload(download);
975}
976
[email protected]6cade212008-12-03 00:32:22977// Clears the last download path, used to initialize "save as" dialogs.
[email protected]905a08d2008-11-19 07:24:12978void DownloadManager::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:41979 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:38980}
[email protected]b0ab1d42010-02-24 19:29:28981
982void DownloadManager::NotifyModelChanged() {
983 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
984}
985
[email protected]2e030682010-07-23 19:45:36986DownloadItem* DownloadManager::GetDownloadItem(int id) {
987 for (DownloadMap::iterator it = downloads_.begin();
988 it != downloads_.end(); ++it) {
989 DownloadItem* item = it->second;
990 if (item->id() == id)
991 return item;
992 }
993 return NULL;
994}
995
[email protected]b0ab1d42010-02-24 19:29:28996// DownloadManager::OtherDownloadManagerObserver implementation ----------------
997
998DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver(
999 DownloadManager* observing_download_manager)
1000 : observing_download_manager_(observing_download_manager),
1001 observed_download_manager_(NULL) {
1002 if (observing_download_manager->profile_->GetOriginalProfile() ==
1003 observing_download_manager->profile_) {
1004 return;
1005 }
1006
1007 observed_download_manager_ = observing_download_manager_->
1008 profile_->GetOriginalProfile()->GetDownloadManager();
1009 observed_download_manager_->AddObserver(this);
1010}
1011
1012DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() {
1013 if (observed_download_manager_)
1014 observed_download_manager_->RemoveObserver(this);
1015}
1016
1017void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1018 observing_download_manager_->NotifyModelChanged();
1019}
1020
[email protected]b0ab1d42010-02-24 19:29:281021void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1022 observed_download_manager_ = NULL;
1023}