blob: 32115071eb425cfad93ff343014de656db278f59 [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]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"
[email protected]7b5dc002010-11-16 23:08:1039#include "chrome/browser/ui/browser.h"
initial.commit09911bf2008-07-26 23:55:2940#include "chrome/common/chrome_paths.h"
[email protected]91e1bd82009-09-03 22:04:4041#include "chrome/common/notification_service.h"
42#include "chrome/common/notification_type.h"
initial.commit09911bf2008-07-26 23:55:2943#include "chrome/common/pref_names.h"
[email protected]46072d42008-07-28 14:49:3544#include "googleurl/src/gurl.h"
[email protected]34ac8f32009-02-22 23:03:2745#include "grit/generated_resources.h"
[email protected]21ca982c2010-01-26 22:49:5546#include "grit/theme_resources.h"
initial.commit09911bf2008-07-26 23:55:2947#include "net/base/mime_util.h"
48#include "net/base/net_util.h"
initial.commit09911bf2008-07-26 23:55:2949
[email protected]b7f05882009-02-22 01:21:5650#if defined(OS_WIN)
[email protected]4a0765a2009-05-08 23:12:2551#include "app/win_util.h"
[email protected]a0a9577b2009-05-27 23:52:3252#endif
53
[email protected]073ed7b2010-09-27 09:20:0254DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater)
initial.commit09911bf2008-07-26 23:55:2955 : shutdown_needed_(false),
56 profile_(NULL),
[email protected]073ed7b2010-09-27 09:20:0257 file_manager_(NULL),
[email protected]a3d2bc42010-10-06 14:08:4958 status_updater_(status_updater->AsWeakPtr()) {
[email protected]073ed7b2010-09-27 09:20:0259 if (status_updater_)
60 status_updater_->AddDelegate(this);
initial.commit09911bf2008-07-26 23:55:2961}
62
63DownloadManager::~DownloadManager() {
[email protected]326a6a92010-09-10 20:21:1364 DCHECK(!shutdown_needed_);
[email protected]073ed7b2010-09-27 09:20:0265 if (status_updater_)
66 status_updater_->RemoveDelegate(this);
initial.commit09911bf2008-07-26 23:55:2967}
68
69void DownloadManager::Shutdown() {
[email protected]326a6a92010-09-10 20:21:1370 if (!shutdown_needed_)
71 return;
72 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:2973
[email protected]326a6a92010-09-10 20:21:1374 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
75
76 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:1877 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]326a6a92010-09-10 20:21:1378 NewRunnableMethod(file_manager_,
79 &DownloadFileManager::OnDownloadManagerShutdown,
[email protected]0d7e79fa2010-10-08 23:35:4780 this));
[email protected]326a6a92010-09-10 20:21:1381 }
initial.commit09911bf2008-07-26 23:55:2982
initial.commit09911bf2008-07-26 23:55:2983 // 'in_progress_' may contain DownloadItems that have not finished the start
84 // complete (from the history service) and thus aren't in downloads_.
85 DownloadMap::iterator it = in_progress_.begin();
[email protected]9ccbb372008-10-10 18:50:3286 std::set<DownloadItem*> to_remove;
initial.commit09911bf2008-07-26 23:55:2987 for (; it != in_progress_.end(); ++it) {
88 DownloadItem* download = it->second;
[email protected]9ccbb372008-10-10 18:50:3289 if (download->safety_state() == DownloadItem::DANGEROUS) {
90 // Forget about any download that the user did not approve.
91 // Note that we cannot call download->Remove() this would invalidate our
92 // iterator.
93 to_remove.insert(download);
94 continue;
initial.commit09911bf2008-07-26 23:55:2995 }
[email protected]9ccbb372008-10-10 18:50:3296 DCHECK_EQ(DownloadItem::IN_PROGRESS, download->state());
97 download->Cancel(false);
[email protected]82f37b02010-07-29 22:04:5798 download_history_->UpdateEntry(download);
99 if (download->db_handle() == DownloadHistory::kUninitializedHandle) {
initial.commit09911bf2008-07-26 23:55:29100 // An invalid handle means that 'download' does not yet exist in
101 // 'downloads_', so we have to delete it here.
102 delete download;
103 }
104 }
105
[email protected]9ccbb372008-10-10 18:50:32106 // 'dangerous_finished_' contains all complete downloads that have not been
107 // approved. They should be removed.
108 it = dangerous_finished_.begin();
109 for (; it != dangerous_finished_.end(); ++it)
110 to_remove.insert(it->second);
111
112 // Remove the dangerous download that are not approved.
113 for (std::set<DownloadItem*>::const_iterator rm_it = to_remove.begin();
114 rm_it != to_remove.end(); ++rm_it) {
115 DownloadItem* download = *rm_it;
[email protected]e10e17c72008-10-15 17:48:32116 int64 handle = download->db_handle();
[email protected]9ccbb372008-10-10 18:50:32117 download->Remove(true);
[email protected]e10e17c72008-10-15 17:48:32118 // Same as above, delete the download if it is not in 'downloads_' (as the
119 // Remove() call above won't have deleted it).
[email protected]82f37b02010-07-29 22:04:57120 if (handle == DownloadHistory::kUninitializedHandle)
[email protected]9ccbb372008-10-10 18:50:32121 delete download;
122 }
123 to_remove.clear();
124
initial.commit09911bf2008-07-26 23:55:29125 in_progress_.clear();
[email protected]9ccbb372008-10-10 18:50:32126 dangerous_finished_.clear();
initial.commit09911bf2008-07-26 23:55:29127 STLDeleteValues(&downloads_);
[email protected]ec4826a2010-09-21 09:15:59128 STLDeleteContainerPointers(save_page_downloads_.begin(),
129 save_page_downloads_.end());
initial.commit09911bf2008-07-26 23:55:29130
131 file_manager_ = NULL;
132
initial.commit09911bf2008-07-26 23:55:29133 // Make sure the save as dialog doesn't notify us back if we're gone before
134 // it returns.
135 if (select_file_dialog_.get())
136 select_file_dialog_->ListenerDestroyed();
137
[email protected]82f37b02010-07-29 22:04:57138 download_history_.reset();
139
[email protected]2c93f6f2010-11-08 20:41:17140 request_context_getter_ = NULL;
141
initial.commit09911bf2008-07-26 23:55:29142 shutdown_needed_ = false;
143}
144
[email protected]82f37b02010-07-29 22:04:57145void DownloadManager::GetTemporaryDownloads(
146 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
147 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58148
149 for (DownloadMap::iterator it = downloads_.begin();
150 it != downloads_.end(); ++it) {
151 if (it->second->is_temporary() &&
152 it->second->full_path().DirName() == dir_path)
[email protected]82f37b02010-07-29 22:04:57153 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58154 }
[email protected]6aa4a1c02010-01-15 18:49:58155}
156
[email protected]82f37b02010-07-29 22:04:57157void DownloadManager::GetAllDownloads(
158 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
159 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34160
161 for (DownloadMap::iterator it = downloads_.begin();
162 it != downloads_.end(); ++it) {
163 if (!it->second->is_temporary() &&
164 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57165 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34166 }
[email protected]8ddbd66a2010-05-21 16:38:34167}
168
[email protected]82f37b02010-07-29 22:04:57169void DownloadManager::GetCurrentDownloads(
170 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
171 DCHECK(result);
[email protected]c4a530b2010-03-08 17:33:03172
173 for (DownloadMap::iterator it = downloads_.begin();
174 it != downloads_.end(); ++it) {
175 if (!it->second->is_temporary() &&
176 (it->second->state() == DownloadItem::IN_PROGRESS ||
177 it->second->safety_state() == DownloadItem::DANGEROUS) &&
178 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57179 result->push_back(it->second);
[email protected]c4a530b2010-03-08 17:33:03180 }
[email protected]f7e9fd62010-09-28 15:45:06181
182 // If we have a parent profile, let it add its downloads to the results.
183 Profile* original_profile = profile_->GetOriginalProfile();
184 if (original_profile != profile_)
185 original_profile->GetDownloadManager()->GetCurrentDownloads(dir_path,
186 result);
187
[email protected]c4a530b2010-03-08 17:33:03188}
189
[email protected]d3b12902010-08-16 23:39:42190void DownloadManager::SearchDownloads(const string16& query,
191 std::vector<DownloadItem*>* result) {
192 DCHECK(result);
193
194 string16 query_lower(l10n_util::ToLower(query));
195
196 for (DownloadMap::iterator it = downloads_.begin();
197 it != downloads_.end(); ++it) {
198 DownloadItem* download_item = it->second;
199
200 if (download_item->is_temporary() || download_item->is_extension_install())
201 continue;
202
203 // Display Incognito downloads only in Incognito window, and vice versa.
204 // The Incognito Downloads page will get the list of non-Incognito downloads
205 // from its parent profile.
206 if (profile_->IsOffTheRecord() != download_item->is_otr())
207 continue;
208
209 if (download_item->MatchesQuery(query_lower))
210 result->push_back(download_item);
211 }
212
213 // If we have a parent profile, let it add its downloads to the results.
214 Profile* original_profile = profile_->GetOriginalProfile();
215 if (original_profile != profile_)
216 original_profile->GetDownloadManager()->SearchDownloads(query, result);
217}
218
initial.commit09911bf2008-07-26 23:55:29219// Query the history service for information about all persisted downloads.
220bool DownloadManager::Init(Profile* profile) {
221 DCHECK(profile);
222 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
223 shutdown_needed_ = true;
224
225 profile_ = profile;
[email protected]be180c802009-10-23 06:33:31226 request_context_getter_ = profile_->GetRequestContext();
[email protected]d3b12902010-08-16 23:39:42227 download_history_.reset(new DownloadHistory(profile));
[email protected]82f37b02010-07-29 22:04:57228 download_history_->Load(
229 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete));
[email protected]024f2f02010-04-30 22:51:46230
[email protected]e5dc4222010-08-30 22:16:32231 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs()));
232
[email protected]2941c2392010-07-15 22:54:30233 // In test mode, there may be no ResourceDispatcherHost. In this case it's
234 // safe to avoid setting |file_manager_| because we only call a small set of
235 // functions, none of which need it.
initial.commit09911bf2008-07-26 23:55:29236 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
[email protected]2941c2392010-07-15 22:54:30237 if (rdh) {
238 file_manager_ = rdh->download_file_manager();
239 DCHECK(file_manager_);
initial.commit09911bf2008-07-26 23:55:29240 }
241
[email protected]b0ab1d42010-02-24 19:29:28242 other_download_manager_observer_.reset(
243 new OtherDownloadManagerObserver(this));
244
initial.commit09911bf2008-07-26 23:55:29245 return true;
246}
247
initial.commit09911bf2008-07-26 23:55:29248// We have received a message from DownloadFileManager about a new download. We
249// create a download item and store it in our download map, and inform the
250// history system of a new download. Since this method can be called while the
251// history service thread is still reading the persistent state, we do not
252// insert the new DownloadItem into 'downloads_' or inform our observers at this
253// point. OnCreateDatabaseEntryComplete() handles that finalization of the the
254// download creation as a callback from the history thread.
255void DownloadManager::StartDownload(DownloadCreateInfo* info) {
[email protected]ca4b5fa32010-10-09 12:42:18256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29257 DCHECK(info);
258
[email protected]a60c8ae2009-12-25 06:50:57259 // Check whether this download is for an extension install or not.
[email protected]e6875c12010-07-18 11:14:13260 // Allow extensions to be explicitly saved.
261 if (!info->prompt_user_for_save_location) {
[email protected]a60c8ae2009-12-25 06:50:57262 if (UserScript::HasUserScriptFileExtension(info->url) ||
263 info->mime_type == Extension::kMimeType)
264 info->is_extension_install = true;
265 }
266
[email protected]8af9d032010-02-10 00:00:32267 if (info->save_info.file_path.empty()) {
[email protected]2941c2392010-07-15 22:54:30268 FilePath generated_name;
[email protected]7ba53e12010-08-05 17:14:00269 download_util::GenerateFileNameFromInfo(info, &generated_name);
[email protected]2941c2392010-07-15 22:54:30270
271 // Freeze the user's preference for showing a Save As dialog. We're going
272 // to bounce around a bunch of threads and we don't want to worry about race
273 // conditions where the user changes this pref out from under us.
[email protected]e5dc4222010-08-30 22:16:32274 if (download_prefs_->prompt_for_download()) {
[email protected]2941c2392010-07-15 22:54:30275 // But ignore the user's preference for the following scenarios:
276 // 1) Extension installation. Note that we only care here about the case
277 // where an extension is installed, not when one is downloaded with
278 // "save as...".
279 // 2) Filetypes marked "always open." If the user just wants this file
280 // opened, don't bother asking where to keep it.
281 if (!info->is_extension_install &&
282 !ShouldOpenFileBasedOnExtension(generated_name))
[email protected]e6875c12010-07-18 11:14:13283 info->prompt_user_for_save_location = true;
[email protected]2941c2392010-07-15 22:54:30284 }
285
[email protected]8af9d032010-02-10 00:00:32286 // Determine the proper path for a download, by either one of the following:
287 // 1) using the default download directory.
288 // 2) prompting the user.
[email protected]80dc3612010-07-27 19:35:08289 if (info->prompt_user_for_save_location && !last_download_path_.empty()){
[email protected]8af9d032010-02-10 00:00:32290 info->suggested_path = last_download_path_;
[email protected]80dc3612010-07-27 19:35:08291 } else {
[email protected]e5dc4222010-08-30 22:16:32292 info->suggested_path = download_prefs_->download_path();
[email protected]80dc3612010-07-27 19:35:08293 }
[email protected]8af9d032010-02-10 00:00:32294 info->suggested_path = info->suggested_path.Append(generated_name);
295 } else {
296 info->suggested_path = info->save_info.file_path;
297 }
initial.commit09911bf2008-07-26 23:55:29298
[email protected]e6875c12010-07-18 11:14:13299 if (!info->prompt_user_for_save_location &&
300 info->save_info.file_path.empty()) {
[email protected]d3071992010-10-08 15:24:07301 info->is_dangerous = download_util::IsDangerous(info, profile());
[email protected]e9ebf3fc2008-10-17 22:06:58302 }
303
initial.commit09911bf2008-07-26 23:55:29304 // We need to move over to the download thread because we don't want to stat
305 // the suggested path on the UI thread.
[email protected]5a3b97e2010-10-05 09:49:11306 // We can only access preferences on the UI thread, so check the download path
307 // now and pass the value to the FILE thread.
[email protected]ca4b5fa32010-10-09 12:42:18308 BrowserThread::PostTask(
309 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37310 NewRunnableMethod(
[email protected]5a3b97e2010-10-05 09:49:11311 this,
312 &DownloadManager::CheckIfSuggestedPathExists,
313 info,
314 download_prefs()->download_path()));
initial.commit09911bf2008-07-26 23:55:29315}
316
[email protected]5a3b97e2010-10-05 09:49:11317void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info,
318 const FilePath& default_path) {
[email protected]ca4b5fa32010-10-09 12:42:18319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
initial.commit09911bf2008-07-26 23:55:29320 DCHECK(info);
321
[email protected]5a3b97e2010-10-05 09:49:11322 // Make sure the default download directory exists.
323 // TODO(phajdan.jr): only create the directory when we're sure the user
324 // is going to save there and not to another directory of his choice.
325 file_util::CreateDirectory(default_path);
326
initial.commit09911bf2008-07-26 23:55:29327 // Check writability of the suggested path. If we can't write to it, default
328 // to the user's "My Documents" directory. We'll prompt them in this case.
[email protected]7ae7c2cb2009-01-06 23:31:41329 FilePath dir = info->suggested_path.DirName();
330 FilePath filename = info->suggested_path.BaseName();
[email protected]9ccbb372008-10-10 18:50:32331 if (!file_util::PathIsWritable(dir)) {
[email protected]e6875c12010-07-18 11:14:13332 info->prompt_user_for_save_location = true;
initial.commit09911bf2008-07-26 23:55:29333 PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path);
[email protected]7ae7c2cb2009-01-06 23:31:41334 info->suggested_path = info->suggested_path.Append(filename);
initial.commit09911bf2008-07-26 23:55:29335 }
336
[email protected]6cade212008-12-03 00:32:22337 // If the download is deemed dangerous, we'll use a temporary name for it.
[email protected]e9ebf3fc2008-10-17 22:06:58338 if (info->is_dangerous) {
[email protected]7ae7c2cb2009-01-06 23:31:41339 info->original_name = FilePath(info->suggested_path).BaseName();
[email protected]9ccbb372008-10-10 18:50:32340 // Create a temporary file to hold the file until the user approves its
341 // download.
[email protected]7ae7c2cb2009-01-06 23:31:41342 FilePath::StringType file_name;
343 FilePath path;
[email protected]9ccbb372008-10-10 18:50:32344 while (path.empty()) {
[email protected]2594c2b2010-11-08 23:04:26345 base::SStringPrintf(
346 &file_name,
347 FILE_PATH_LITERAL("unconfirmed %d.crdownload"),
348 base::RandInt(0, 100000));
[email protected]7ae7c2cb2009-01-06 23:31:41349 path = dir.Append(file_name);
[email protected]7d3851d82008-12-12 03:26:07350 if (file_util::PathExists(path))
[email protected]7ae7c2cb2009-01-06 23:31:41351 path = FilePath();
[email protected]9ccbb372008-10-10 18:50:32352 }
353 info->suggested_path = path;
[email protected]7a256ea2008-10-17 17:34:16354 } else {
[email protected]594cd7d2010-07-21 03:23:56355 // Do not add the path uniquifier if we are saving to a specific path as in
356 // the drag-out case.
357 if (info->save_info.file_path.empty()) {
358 info->path_uniquifier = download_util::GetUniquePathNumberWithCrDownload(
359 info->suggested_path);
360 }
[email protected]7a256ea2008-10-17 17:34:16361 // We know the final path, build it if necessary.
362 if (info->path_uniquifier > 0) {
[email protected]5a2388a2010-03-26 16:13:39363 download_util::AppendNumberToPath(&(info->suggested_path),
364 info->path_uniquifier);
[email protected]7a256ea2008-10-17 17:34:16365 // Setting path_uniquifier to 0 to make sure we don't try to unique it
366 // later on.
367 info->path_uniquifier = 0;
[email protected]7d3851d82008-12-12 03:26:07368 } else if (info->path_uniquifier == -1) {
369 // We failed to find a unique path. We have to prompt the user.
[email protected]e6875c12010-07-18 11:14:13370 info->prompt_user_for_save_location = true;
[email protected]7a256ea2008-10-17 17:34:16371 }
[email protected]9ccbb372008-10-10 18:50:32372 }
373
[email protected]594cd7d2010-07-21 03:23:56374 // Create an empty file at the suggested path so that we don't allocate the
375 // same "non-existant" path to multiple downloads.
376 // See: http://code.google.com/p/chromium/issues/detail?id=3662
[email protected]e6875c12010-07-18 11:14:13377 if (!info->prompt_user_for_save_location &&
378 info->save_info.file_path.empty()) {
[email protected]594cd7d2010-07-21 03:23:56379 if (info->is_dangerous)
380 file_util::WriteFile(info->suggested_path, "", 0);
381 else
382 file_util::WriteFile(download_util::GetCrDownloadPath(
383 info->suggested_path), "", 0);
[email protected]7d3851d82008-12-12 03:26:07384 }
385
[email protected]ca4b5fa32010-10-09 12:42:18386 BrowserThread::PostTask(
387 BrowserThread::UI, FROM_HERE,
initial.commit09911bf2008-07-26 23:55:29388 NewRunnableMethod(this,
389 &DownloadManager::OnPathExistenceAvailable,
390 info));
391}
392
393void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) {
[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;
initial.commit09911bf2008-07-26 23:55:29435
[email protected]aa033af2010-07-27 18:16:39436 bool download_finished = ContainsKey(pending_finished_downloads_,
437 info->download_id);
[email protected]594cd7d2010-07-21 03:23:56438
439 if (download_finished || info->is_dangerous) {
440 // The download has already finished or the download is not safe.
441 // We can now rename the file to its final name (or its tentative name
442 // in dangerous download cases).
[email protected]ca4b5fa32010-10-09 12:42:18443 BrowserThread::PostTask(
444 BrowserThread::FILE, FROM_HERE,
[email protected]594cd7d2010-07-21 03:23:56445 NewRunnableMethod(
[email protected]0d7e79fa2010-10-08 23:35:47446 file_manager_, &DownloadFileManager::OnFinalDownloadName,
447 download->id(), target_path, !info->is_dangerous, this));
[email protected]594cd7d2010-07-21 03:23:56448 } else {
449 // The download hasn't finished and it is a safe download. We need to
450 // rename it to its intermediate '.crdownload' path.
451 FilePath download_path = download_util::GetCrDownloadPath(target_path);
[email protected]ca4b5fa32010-10-09 12:42:18452 BrowserThread::PostTask(
453 BrowserThread::FILE, FROM_HERE,
[email protected]594cd7d2010-07-21 03:23:56454 NewRunnableMethod(
[email protected]0d7e79fa2010-10-08 23:35:47455 file_manager_, &DownloadFileManager::OnIntermediateDownloadName,
456 download->id(), download_path, this));
[email protected]44b94b82010-11-08 20:49:18457 download->Rename(download_path);
[email protected]594cd7d2010-07-21 03:23:56458 }
459
460 if (download_finished) {
461 // If the download already completed by the time we reached this point, then
462 // notify observers that it did.
[email protected]a850ba42010-09-10 22:00:30463 OnAllDataSaved(info->download_id,
464 pending_finished_downloads_[info->download_id]);
[email protected]594cd7d2010-07-21 03:23:56465 }
initial.commit09911bf2008-07-26 23:55:29466
[email protected]82f37b02010-07-29 22:04:57467 download_history_->AddEntry(*info, download,
468 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete));
[email protected]6a7fb042010-02-01 16:30:47469
470 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29471}
472
initial.commit09911bf2008-07-26 23:55:29473void DownloadManager::UpdateDownload(int32 download_id, int64 size) {
474 DownloadMap::iterator it = in_progress_.find(download_id);
475 if (it != in_progress_.end()) {
476 DownloadItem* download = it->second;
477 download->Update(size);
[email protected]82f37b02010-07-29 22:04:57478 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29479 }
[email protected]6a7fb042010-02-01 16:30:47480 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29481}
482
[email protected]a850ba42010-09-10 22:00:30483void DownloadManager::OnAllDataSaved(int32 download_id, int64 size) {
initial.commit09911bf2008-07-26 23:55:29484 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]9ccbb372008-10-10 18:50:32485 if (it == in_progress_.end()) {
initial.commit09911bf2008-07-26 23:55:29486 // The download is done, but the user hasn't selected a final location for
487 // it yet (the Save As dialog box is probably still showing), so just keep
488 // track of the fact that this download id is complete, when the
489 // DownloadItem is constructed later we'll notify its completion then.
490 PendingFinishedMap::iterator erase_it =
491 pending_finished_downloads_.find(download_id);
492 DCHECK(erase_it == pending_finished_downloads_.end());
493 pending_finished_downloads_[download_id] = size;
[email protected]9ccbb372008-10-10 18:50:32494 return;
initial.commit09911bf2008-07-26 23:55:29495 }
[email protected]9ccbb372008-10-10 18:50:32496
497 // Remove the id from the list of pending ids.
498 PendingFinishedMap::iterator erase_it =
499 pending_finished_downloads_.find(download_id);
500 if (erase_it != pending_finished_downloads_.end())
501 pending_finished_downloads_.erase(erase_it);
502
503 DownloadItem* download = it->second;
[email protected]a850ba42010-09-10 22:00:30504 download->OnAllDataSaved(size);
[email protected]9ccbb372008-10-10 18:50:32505
506 // Clean up will happen when the history system create callback runs if we
507 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57508 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
[email protected]9ccbb372008-10-10 18:50:32509 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57510 download_history_->UpdateEntry(download);
[email protected]9ccbb372008-10-10 18:50:32511 }
512
[email protected]6a7fb042010-02-01 16:30:47513 UpdateAppIcon();
514
[email protected]9ccbb372008-10-10 18:50:32515 // If this a dangerous download not yet validated by the user, don't do
516 // anything. When the user notifies us, it will trigger a call to
517 // ProceedWithFinishedDangerousDownload.
518 if (download->safety_state() == DownloadItem::DANGEROUS) {
519 dangerous_finished_[download_id] = download;
520 return;
521 }
522
523 if (download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) {
[email protected]6cade212008-12-03 00:32:22524 // We first need to rename the downloaded file from its temporary name to
[email protected]9ccbb372008-10-10 18:50:32525 // its final name before we can continue.
[email protected]ca4b5fa32010-10-09 12:42:18526 BrowserThread::PostTask(
527 BrowserThread::FILE, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:32528 NewRunnableMethod(
529 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
530 download->db_handle(),
[email protected]44b94b82010-11-08 20:49:18531 download->full_path(), download->target_name()));
[email protected]9ccbb372008-10-10 18:50:32532 return;
533 }
[email protected]594cd7d2010-07-21 03:23:56534
[email protected]9246de32010-11-10 22:58:47535 download->OnSafeDownloadFinished(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32536}
537
[email protected]8f783752009-04-01 23:33:45538void DownloadManager::DownloadRenamedToFinalName(int download_id,
539 const FilePath& full_path) {
[email protected]ca4b5fa32010-10-09 12:42:18540 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2e030682010-07-23 19:45:36541 DownloadItem* item = GetDownloadItem(download_id);
542 if (!item)
543 return;
[email protected]9246de32010-11-10 22:58:47544 item->OnDownloadRenamedToFinalName(full_path);
[email protected]8f783752009-04-01 23:33:45545}
546
[email protected]9246de32010-11-10 22:58:47547void DownloadManager::DownloadFinished(DownloadItem* download) {
[email protected]ca4b5fa32010-10-09 12:42:18548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]aa033af2010-07-27 18:16:39549
[email protected]9ccbb372008-10-10 18:50:32550 // If this was a dangerous download, it has now been approved and must be
551 // removed from dangerous_finished_ so it does not get deleted on shutdown.
[email protected]aa033af2010-07-27 18:16:39552 dangerous_finished_.erase(download->id());
[email protected]9ccbb372008-10-10 18:50:32553
[email protected]a850ba42010-09-10 22:00:30554 download->Finished();
[email protected]0aad67b2009-07-15 20:34:28555}
[email protected]eccb9d12009-10-28 05:40:09556
[email protected]9ccbb372008-10-10 18:50:32557// Called on the file thread. Renames the downloaded file to its original name.
558void DownloadManager::ProceedWithFinishedDangerousDownload(
559 int64 download_handle,
[email protected]7ae7c2cb2009-01-06 23:31:41560 const FilePath& path,
561 const FilePath& original_name) {
[email protected]9ccbb372008-10-10 18:50:32562 bool success = false;
[email protected]7ae7c2cb2009-01-06 23:31:41563 FilePath new_path;
[email protected]7a256ea2008-10-17 17:34:16564 int uniquifier = 0;
[email protected]9ccbb372008-10-10 18:50:32565 if (file_util::PathExists(path)) {
[email protected]889ed35c2009-01-21 00:07:24566 new_path = path.DirName().Append(original_name);
[email protected]7a256ea2008-10-17 17:34:16567 // Make our name unique at this point, as if a dangerous file is downloading
568 // and a 2nd download is started for a file with the same name, they would
569 // have the same path. This is because we uniquify the name on download
570 // start, and at that time the first file does not exists yet, so the second
571 // file gets the same name.
[email protected]5a2388a2010-03-26 16:13:39572 uniquifier = download_util::GetUniquePathNumber(new_path);
[email protected]7a256ea2008-10-17 17:34:16573 if (uniquifier > 0)
[email protected]5a2388a2010-03-26 16:13:39574 download_util::AppendNumberToPath(&new_path, uniquifier);
[email protected]9ccbb372008-10-10 18:50:32575 success = file_util::Move(path, new_path);
576 } else {
577 NOTREACHED();
578 }
[email protected]6cade212008-12-03 00:32:22579
[email protected]ca4b5fa32010-10-09 12:42:18580 BrowserThread::PostTask(
581 BrowserThread::UI, FROM_HERE,
[email protected]9ccbb372008-10-10 18:50:32582 NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed,
[email protected]7a256ea2008-10-17 17:34:16583 download_handle, success, new_path, uniquifier));
[email protected]9ccbb372008-10-10 18:50:32584}
585
586// Call from the file thread when the finished dangerous download was renamed.
587void DownloadManager::DangerousDownloadRenamed(int64 download_handle,
588 bool success,
[email protected]7ae7c2cb2009-01-06 23:31:41589 const FilePath& new_path,
[email protected]7a256ea2008-10-17 17:34:16590 int new_path_uniquifier) {
[email protected]9ccbb372008-10-10 18:50:32591 DownloadMap::iterator it = downloads_.find(download_handle);
592 if (it == downloads_.end()) {
593 NOTREACHED();
594 return;
595 }
596
597 DownloadItem* download = it->second;
598 // If we failed to rename the file, we'll just keep the name as is.
[email protected]7a256ea2008-10-17 17:34:16599 if (success) {
600 // We need to update the path uniquifier so that the UI shows the right
[email protected]44b94b82010-11-08 20:49:18601 // name when calling GetFileNameToReportUser().
[email protected]7a256ea2008-10-17 17:34:16602 download->set_path_uniquifier(new_path_uniquifier);
[email protected]9ccbb372008-10-10 18:50:32603 RenameDownload(download, new_path);
[email protected]7a256ea2008-10-17 17:34:16604 }
[email protected]9ccbb372008-10-10 18:50:32605
606 // Continue the download finished sequence.
[email protected]9246de32010-11-10 22:58:47607 DownloadFinished(download);
initial.commit09911bf2008-07-26 23:55:29608}
609
initial.commit09911bf2008-07-26 23:55:29610void DownloadManager::DownloadCancelled(int32 download_id) {
611 DownloadMap::iterator it = in_progress_.find(download_id);
612 if (it == in_progress_.end())
613 return;
614 DownloadItem* download = it->second;
615
initial.commit09911bf2008-07-26 23:55:29616 // Clean up will happen when the history system create callback runs if we
617 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57618 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
initial.commit09911bf2008-07-26 23:55:29619 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57620 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29621 }
622
[email protected]d7d1c5c2009-08-05 23:52:50623 DownloadCancelledInternal(download_id,
624 download->render_process_id(),
625 download->request_id());
[email protected]6a7fb042010-02-01 16:30:47626 UpdateAppIcon();
[email protected]d7d1c5c2009-08-05 23:52:50627}
628
629void DownloadManager::DownloadCancelledInternal(int download_id,
630 int render_process_id,
631 int request_id) {
[email protected]d85cf072009-10-27 03:59:31632 // Cancel the network request. RDH is guaranteed to outlive the IO thread.
[email protected]ca4b5fa32010-10-09 12:42:18633 BrowserThread::PostTask(
634 BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26635 NewRunnableFunction(&download_util::CancelDownloadRequest,
[email protected]d85cf072009-10-27 03:59:31636 g_browser_process->resource_dispatcher_host(),
637 render_process_id,
638 request_id));
[email protected]d7d1c5c2009-08-05 23:52:50639
[email protected]ca4b5fa32010-10-09 12:42:18640 BrowserThread::PostTask(
641 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37642 NewRunnableMethod(
643 file_manager_, &DownloadFileManager::CancelDownload, download_id));
initial.commit09911bf2008-07-26 23:55:29644}
645
646void DownloadManager::PauseDownload(int32 download_id, bool pause) {
647 DownloadMap::iterator it = in_progress_.find(download_id);
[email protected]d85cf072009-10-27 03:59:31648 if (it == in_progress_.end())
649 return;
initial.commit09911bf2008-07-26 23:55:29650
[email protected]d85cf072009-10-27 03:59:31651 DownloadItem* download = it->second;
652 if (pause == download->is_paused())
653 return;
initial.commit09911bf2008-07-26 23:55:29654
[email protected]ca4b5fa32010-10-09 12:42:18655 BrowserThread::PostTask(
656 BrowserThread::IO, FROM_HERE,
[email protected]e2abdeb22010-09-03 19:56:15657 NewRunnableMethod(this,
658 &DownloadManager::PauseDownloadRequest,
659 g_browser_process->resource_dispatcher_host(),
[email protected]7ba53e12010-08-05 17:14:00660 download->render_process_id(),
661 download->request_id(),
662 pause));
[email protected]9ccbb372008-10-10 18:50:32663}
664
[email protected]6a7fb042010-02-01 16:30:47665void DownloadManager::UpdateAppIcon() {
[email protected]073ed7b2010-09-27 09:20:02666 if (status_updater_)
667 status_updater_->Update();
[email protected]6a7fb042010-02-01 16:30:47668}
669
[email protected]9ccbb372008-10-10 18:50:32670void DownloadManager::RenameDownload(DownloadItem* download,
[email protected]7ae7c2cb2009-01-06 23:31:41671 const FilePath& new_path) {
[email protected]9ccbb372008-10-10 18:50:32672 download->Rename(new_path);
[email protected]82f37b02010-07-29 22:04:57673 download_history_->UpdateDownloadPath(download, new_path);
[email protected]9ccbb372008-10-10 18:50:32674}
675
[email protected]e2abdeb22010-09-03 19:56:15676void DownloadManager::PauseDownloadRequest(ResourceDispatcherHost* rdh,
677 int render_process_id,
678 int request_id,
679 bool pause) {
[email protected]ca4b5fa32010-10-09 12:42:18680 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]e2abdeb22010-09-03 19:56:15681 rdh->PauseRequest(render_process_id, request_id, pause);
682}
683
initial.commit09911bf2008-07-26 23:55:29684void DownloadManager::RemoveDownload(int64 download_handle) {
685 DownloadMap::iterator it = downloads_.find(download_handle);
686 if (it == downloads_.end())
687 return;
688
689 // Make history update.
690 DownloadItem* download = it->second;
[email protected]82f37b02010-07-29 22:04:57691 download_history_->RemoveEntry(download);
initial.commit09911bf2008-07-26 23:55:29692
693 // Remove from our tables and delete.
694 downloads_.erase(it);
[email protected]9ccbb372008-10-10 18:50:32695 it = dangerous_finished_.find(download->id());
696 if (it != dangerous_finished_.end())
697 dangerous_finished_.erase(it);
initial.commit09911bf2008-07-26 23:55:29698
699 // Tell observers to refresh their views.
[email protected]b0ab1d42010-02-24 19:29:28700 NotifyModelChanged();
[email protected]6f712872008-11-07 00:35:36701
702 delete download;
initial.commit09911bf2008-07-26 23:55:29703}
704
[email protected]e93d2822009-01-30 05:59:59705int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin,
706 const base::Time remove_end) {
[email protected]82f37b02010-07-29 22:04:57707 download_history_->RemoveEntriesBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29708
initial.commit09911bf2008-07-26 23:55:29709 DownloadMap::iterator it = downloads_.begin();
[email protected]78b8fcc92009-03-31 17:36:28710 std::vector<DownloadItem*> pending_deletes;
initial.commit09911bf2008-07-26 23:55:29711 while (it != downloads_.end()) {
712 DownloadItem* download = it->second;
713 DownloadItem::DownloadState state = download->state();
714 if (download->start_time() >= remove_begin &&
715 (remove_end.is_null() || download->start_time() < remove_end) &&
716 (state == DownloadItem::COMPLETE ||
717 state == DownloadItem::CANCELLED)) {
718 // Remove from the map and move to the next in the list.
[email protected]b7f05882009-02-22 01:21:56719 downloads_.erase(it++);
[email protected]a6604d92008-10-30 00:58:58720
721 // Also remove it from any completed dangerous downloads.
722 DownloadMap::iterator dit = dangerous_finished_.find(download->id());
723 if (dit != dangerous_finished_.end())
724 dangerous_finished_.erase(dit);
725
[email protected]78b8fcc92009-03-31 17:36:28726 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29727
initial.commit09911bf2008-07-26 23:55:29728 continue;
729 }
730
731 ++it;
732 }
733
734 // Tell observers to refresh their views.
[email protected]78b8fcc92009-03-31 17:36:28735 int num_deleted = static_cast<int>(pending_deletes.size());
initial.commit09911bf2008-07-26 23:55:29736 if (num_deleted > 0)
[email protected]b0ab1d42010-02-24 19:29:28737 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29738
[email protected]78b8fcc92009-03-31 17:36:28739 // Delete the download items after updating the observers.
740 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
741 pending_deletes.clear();
742
initial.commit09911bf2008-07-26 23:55:29743 return num_deleted;
744}
745
[email protected]e93d2822009-01-30 05:59:59746int DownloadManager::RemoveDownloads(const base::Time remove_begin) {
747 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29748}
749
[email protected]d41355e6f2009-04-07 21:21:12750int DownloadManager::RemoveAllDownloads() {
[email protected]024f2f02010-04-30 22:51:46751 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) {
752 // This is an incognito downloader. Clear All should clear main download
753 // manager as well.
754 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads();
755 }
[email protected]d41355e6f2009-04-07 21:21:12756 // The null times make the date range unbounded.
757 return RemoveDownloadsBetween(base::Time(), base::Time());
758}
759
[email protected]ec4826a2010-09-21 09:15:59760void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download_item) {
761 save_page_downloads_.push_back(download_item);
762}
763
initial.commit09911bf2008-07-26 23:55:29764// Initiate a download of a specific URL. We send the request to the
765// ResourceDispatcherHost, and let it send us responses like a regular
766// download.
767void DownloadManager::DownloadUrl(const GURL& url,
768 const GURL& referrer,
[email protected]c9825a42009-05-01 22:51:50769 const std::string& referrer_charset,
[email protected]57c6a652009-05-04 07:58:34770 TabContents* tab_contents) {
[email protected]ae8945192010-07-20 16:56:26771 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
772 tab_contents);
[email protected]6aa4a1c02010-01-15 18:49:58773}
774
775void DownloadManager::DownloadUrlToFile(const GURL& url,
776 const GURL& referrer,
777 const std::string& referrer_charset,
[email protected]8af9d032010-02-10 00:00:32778 const DownloadSaveInfo& save_info,
[email protected]6aa4a1c02010-01-15 18:49:58779 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:34780 DCHECK(tab_contents);
[email protected]ca4b5fa32010-10-09 12:42:18781 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26782 NewRunnableFunction(&download_util::DownloadUrl,
783 url,
784 referrer,
785 referrer_charset,
786 save_info,
787 g_browser_process->resource_dispatcher_host(),
788 tab_contents->GetRenderProcessHost()->id(),
789 tab_contents->render_view_host()->routing_id(),
790 request_context_getter_));
initial.commit09911bf2008-07-26 23:55:29791}
792
initial.commit09911bf2008-07-26 23:55:29793void DownloadManager::AddObserver(Observer* observer) {
794 observers_.AddObserver(observer);
795 observer->ModelChanged();
796}
797
798void DownloadManager::RemoveObserver(Observer* observer) {
799 observers_.RemoveObserver(observer);
800}
801
[email protected]eccb9d12009-10-28 05:40:09802bool DownloadManager::ShouldOpenFileBasedOnExtension(
803 const FilePath& path) const {
[email protected]eccb9d12009-10-28 05:40:09804 FilePath::StringType extension = path.Extension();
805 if (extension.empty())
806 return false;
[email protected]7ba53e12010-08-05 17:14:00807 if (download_util::IsExecutableExtension(extension))
[email protected]eccb9d12009-10-28 05:40:09808 return false;
[email protected]92e11c82010-01-13 06:39:56809 if (Extension::IsExtension(path))
810 return false;
[email protected]eccb9d12009-10-28 05:40:09811 DCHECK(extension[0] == FilePath::kExtensionSeparator);
812 extension.erase(0, 1);
[email protected]e5dc4222010-08-30 22:16:32813 return download_prefs_->IsAutoOpenEnabledForExtension(extension);
initial.commit09911bf2008-07-26 23:55:29814}
815
[email protected]073ed7b2010-09-27 09:20:02816bool DownloadManager::IsDownloadProgressKnown() {
817 for (DownloadMap::iterator i = in_progress_.begin();
818 i != in_progress_.end(); ++i) {
819 if (i->second->total_bytes() <= 0)
820 return false;
821 }
822
823 return true;
824}
825
826int64 DownloadManager::GetInProgressDownloadCount() {
827 return in_progress_.size();
828}
829
830int64 DownloadManager::GetReceivedDownloadBytes() {
831 DCHECK(IsDownloadProgressKnown());
832 int64 received_bytes = 0;
833 for (DownloadMap::iterator i = in_progress_.begin();
834 i != in_progress_.end(); ++i) {
835 received_bytes += i->second->received_bytes();
836 }
837 return received_bytes;
838}
839
840int64 DownloadManager::GetTotalDownloadBytes() {
841 DCHECK(IsDownloadProgressKnown());
842 int64 total_bytes = 0;
843 for (DownloadMap::iterator i = in_progress_.begin();
844 i != in_progress_.end(); ++i) {
845 total_bytes += i->second->total_bytes();
846 }
847 return total_bytes;
848}
849
[email protected]561abe62009-04-06 18:08:34850void DownloadManager::FileSelected(const FilePath& path,
[email protected]23b357b2009-03-30 20:02:36851 int index, void* params) {
initial.commit09911bf2008-07-26 23:55:29852 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]e6875c12010-07-18 11:14:13853 if (info->prompt_user_for_save_location)
[email protected]7ae7c2cb2009-01-06 23:31:41854 last_download_path_ = path.DirName();
[email protected]a850ba42010-09-10 22:00:30855 CreateDownloadItem(info, path);
initial.commit09911bf2008-07-26 23:55:29856}
857
858void DownloadManager::FileSelectionCanceled(void* params) {
859 // The user didn't pick a place to save the file, so need to cancel the
860 // download that's already in progress to the temporary location.
861 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
[email protected]d7d1c5c2009-08-05 23:52:50862 DownloadCancelledInternal(info->download_id,
[email protected]76543b92009-08-31 17:27:45863 info->child_id,
[email protected]d7d1c5c2009-08-05 23:52:50864 info->request_id);
initial.commit09911bf2008-07-26 23:55:29865}
866
[email protected]9ccbb372008-10-10 18:50:32867void DownloadManager::DangerousDownloadValidated(DownloadItem* download) {
868 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state());
869 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED);
870 download->UpdateObservers();
871
872 // If the download is not complete, nothing to do. The required
873 // post-processing will be performed when it does complete.
874 if (download->state() != DownloadItem::COMPLETE)
875 return;
876
[email protected]ca4b5fa32010-10-09 12:42:18877 BrowserThread::PostTask(
878 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37879 NewRunnableMethod(
880 this, &DownloadManager::ProceedWithFinishedDangerousDownload,
881 download->db_handle(), download->full_path(),
[email protected]44b94b82010-11-08 20:49:18882 download->target_name()));
[email protected]9ccbb372008-10-10 18:50:32883}
884
initial.commit09911bf2008-07-26 23:55:29885// Operations posted to us from the history service ----------------------------
886
887// The history service has retrieved all download entries. 'entries' contains
888// 'DownloadCreateInfo's in sorted order (by ascending start_time).
889void DownloadManager::OnQueryDownloadEntriesComplete(
890 std::vector<DownloadCreateInfo>* entries) {
891 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]aa033af2010-07-27 18:16:39892 DownloadItem* download = new DownloadItem(this, entries->at(i));
893 DCHECK(!ContainsKey(downloads_, download->db_handle()));
initial.commit09911bf2008-07-26 23:55:29894 downloads_[download->db_handle()] = download;
initial.commit09911bf2008-07-26 23:55:29895 }
[email protected]b0ab1d42010-02-24 19:29:28896 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29897}
898
initial.commit09911bf2008-07-26 23:55:29899// Once the new DownloadItem's creation info has been committed to the history
900// service, we associate the DownloadItem with the db handle, update our
901// 'downloads_' map and inform observers.
[email protected]9f5cb562010-11-08 22:06:59902void DownloadManager::OnCreateDownloadEntryComplete(
[email protected]76d8b622010-11-11 19:50:05903 DownloadCreateInfo info,
[email protected]9f5cb562010-11-08 22:06:59904 int64 db_handle) {
initial.commit09911bf2008-07-26 23:55:29905 DownloadMap::iterator it = in_progress_.find(info.download_id);
906 DCHECK(it != in_progress_.end());
907
908 DownloadItem* download = it->second;
[email protected]d2a8fb72010-01-21 05:31:42909
910 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
911 // call this function with an invalid |db_handle|. For instance, this can
912 // happen when the history database is offline. We cannot have multiple
913 // DownloadItems with the same invalid db_handle, so we need to assign a
914 // unique |db_handle| here.
[email protected]82f37b02010-07-29 22:04:57915 if (db_handle == DownloadHistory::kUninitializedHandle)
916 db_handle = download_history_->GetNextFakeDbHandle();
[email protected]d2a8fb72010-01-21 05:31:42917
[email protected]82f37b02010-07-29 22:04:57918 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle);
initial.commit09911bf2008-07-26 23:55:29919 download->set_db_handle(db_handle);
920
921 // Insert into our full map.
922 DCHECK(downloads_.find(download->db_handle()) == downloads_.end());
923 downloads_[download->db_handle()] = download;
924
[email protected]5e595482009-05-06 20:16:53925 // Show in the appropropriate browser UI.
926 ShowDownloadInBrowser(info, download);
initial.commit09911bf2008-07-26 23:55:29927
928 // Inform interested objects about the new download.
[email protected]b0ab1d42010-02-24 19:29:28929 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:29930
931 // If this download has been completed before we've received the db handle,
932 // post one final message to the history service so that it can be properly
933 // in sync with the DownloadItem's completion status, and also inform any
934 // observers so that they get more than just the start notification.
935 if (download->state() != DownloadItem::IN_PROGRESS) {
936 in_progress_.erase(it);
[email protected]82f37b02010-07-29 22:04:57937 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29938 download->UpdateObservers();
939 }
[email protected]6a7fb042010-02-01 16:30:47940
941 UpdateAppIcon();
initial.commit09911bf2008-07-26 23:55:29942}
943
[email protected]5e595482009-05-06 20:16:53944void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info,
945 DownloadItem* download) {
[email protected]8ddbd66a2010-05-21 16:38:34946 // The 'contents' may no longer exist if the user closed the tab before we
947 // get this start completion event. If it does, tell the origin TabContents
948 // to display its download shelf.
[email protected]76543b92009-08-31 17:27:45949 TabContents* contents = tab_util::GetTabContentsByID(info.child_id,
950 info.render_view_id);
[email protected]5e595482009-05-06 20:16:53951
952 // If the contents no longer exists, we start the download in the last active
953 // browser. This is not ideal but better than fully hiding the download from
954 // the user.
955 if (!contents) {
956 Browser* last_active = BrowserList::GetLastActive();
957 if (last_active)
958 contents = last_active->GetSelectedTabContents();
959 }
960
961 if (contents)
962 contents->OnStartDownload(download);
963}
964
[email protected]6cade212008-12-03 00:32:22965// Clears the last download path, used to initialize "save as" dialogs.
[email protected]905a08d2008-11-19 07:24:12966void DownloadManager::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:41967 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:38968}
[email protected]b0ab1d42010-02-24 19:29:28969
970void DownloadManager::NotifyModelChanged() {
971 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
972}
973
[email protected]2e030682010-07-23 19:45:36974DownloadItem* DownloadManager::GetDownloadItem(int id) {
975 for (DownloadMap::iterator it = downloads_.begin();
976 it != downloads_.end(); ++it) {
977 DownloadItem* item = it->second;
978 if (item->id() == id)
979 return item;
980 }
981 return NULL;
982}
983
[email protected]b0ab1d42010-02-24 19:29:28984// DownloadManager::OtherDownloadManagerObserver implementation ----------------
985
986DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver(
987 DownloadManager* observing_download_manager)
988 : observing_download_manager_(observing_download_manager),
989 observed_download_manager_(NULL) {
990 if (observing_download_manager->profile_->GetOriginalProfile() ==
991 observing_download_manager->profile_) {
992 return;
993 }
994
995 observed_download_manager_ = observing_download_manager_->
996 profile_->GetOriginalProfile()->GetDownloadManager();
997 observed_download_manager_->AddObserver(this);
998}
999
1000DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() {
1001 if (observed_download_manager_)
1002 observed_download_manager_->RemoveObserver(this);
1003}
1004
1005void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1006 observing_download_manager_->NotifyModelChanged();
1007}
1008
[email protected]b0ab1d42010-02-24 19:29:281009void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1010 observed_download_manager_ = NULL;
1011}