blob: 872464127f82afe8eae42a99d2600fd0e30ccbc8 [file] [log] [blame]
[email protected]f4f50ef2011-01-21 19:01:191// Copyright (c) 2011 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]2041cf342010-02-19 03:15:597#include "base/callback.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/file_util.h"
[email protected]503d03872011-05-06 08:36:269#include "base/i18n/case_conversion.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/logging.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/path_service.h"
[email protected]1b5044d2009-02-24 00:04:1412#include "base/rand_util.h"
[email protected]92926d92010-09-02 18:35:0613#include "base/stl_util-inl.h"
[email protected]2594c2b2010-11-08 23:04:2614#include "base/stringprintf.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"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/browser/browser_process.h"
[email protected]4cd82f72011-05-23 19:15:0120#include "chrome/browser/download/download_create_info.h"
[email protected]cd448092010-12-06 23:49:1321#include "chrome/browser/download/download_extensions.h"
[email protected]6c69796d2010-07-16 21:41:1622#include "chrome/browser/download/download_file_manager.h"
[email protected]82f37b02010-07-29 22:04:5723#include "chrome/browser/download/download_history.h"
[email protected]6c69796d2010-07-16 21:41:1624#include "chrome/browser/download/download_item.h"
[email protected]e5dc4222010-08-30 22:16:3225#include "chrome/browser/download/download_prefs.h"
[email protected]db6831a2011-06-09 21:08:2826#include "chrome/browser/download/download_request_handle.h"
[email protected]287b86b2011-02-26 00:11:3527#include "chrome/browser/download/download_safe_browsing_client.h"
[email protected]073ed7b2010-09-27 09:20:0228#include "chrome/browser/download/download_status_updater.h"
[email protected]e9ef0a62009-08-11 22:50:1329#include "chrome/browser/download/download_util.h"
[email protected]eaa7dd182010-12-14 11:09:0030#include "chrome/browser/extensions/extension_service.h"
[email protected]4cd82f72011-05-23 19:15:0131#include "chrome/browser/history/download_history_info.h"
[email protected]14a000d2010-04-29 21:44:2432#include "chrome/browser/platform_util.h"
[email protected]8ecad5e2010-12-02 21:18:3333#include "chrome/browser/profiles/profile.h"
[email protected]d2a8fb72010-01-21 05:31:4234#include "chrome/browser/tab_contents/tab_util.h"
[email protected]7b5dc002010-11-16 23:08:1035#include "chrome/browser/ui/browser.h"
[email protected]71b73f02011-04-06 15:57:2936#include "chrome/browser/ui/browser_list.h"
[email protected]ddb85052011-05-18 14:40:2737#include "chrome/browser/ui/download/download_tab_helper.h"
38#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
initial.commit09911bf2008-07-26 23:55:2939#include "chrome/common/chrome_paths.h"
[email protected]7324d1d2011-03-01 05:02:1640#include "content/browser/browser_thread.h"
41#include "content/browser/renderer_host/render_process_host.h"
42#include "content/browser/renderer_host/render_view_host.h"
43#include "content/browser/renderer_host/resource_dispatcher_host.h"
44#include "content/browser/tab_contents/tab_contents.h"
[email protected]f20d7332011-03-08 21:11:5345#include "content/common/notification_type.h"
[email protected]46072d42008-07-28 14:49:3546#include "googleurl/src/gurl.h"
[email protected]34ac8f32009-02-22 23:03:2747#include "grit/generated_resources.h"
[email protected]21ca982c2010-01-26 22:49:5548#include "grit/theme_resources.h"
initial.commit09911bf2008-07-26 23:55:2949#include "net/base/mime_util.h"
50#include "net/base/net_util.h"
[email protected]c051a1b2011-01-21 23:30:1751#include "ui/base/l10n/l10n_util.h"
[email protected]42ce29d2011-01-20 23:19:4652#include "ui/base/resource/resource_bundle.h"
initial.commit09911bf2008-07-26 23:55:2953
[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]da6e3922010-11-24 21:45:5070 VLOG(20) << __FUNCTION__ << "()"
71 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:1372 if (!shutdown_needed_)
73 return;
74 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:2975
[email protected]326a6a92010-09-10 20:21:1376 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
77
78 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:1879 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]326a6a92010-09-10 20:21:1380 NewRunnableMethod(file_manager_,
81 &DownloadFileManager::OnDownloadManagerShutdown,
[email protected]dc7cdcb92010-12-14 06:40:5482 make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:1383 }
initial.commit09911bf2008-07-26 23:55:2984
[email protected]f04182f32010-12-10 19:12:0785 AssertContainersConsistent();
86
87 // Go through all downloads in downloads_. Dangerous ones we need to
88 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:0989 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:0790 DownloadItem* download = *it;
91
92 // Save iterator from potential erases in this set done by called code.
93 // Iterators after an erasure point are still valid for lists and
94 // associative containers such as sets.
95 it++;
96
97 if (download->safety_state() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:2998 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:0799 // The user hasn't accepted it, so we need to remove it
100 // from the disk. This may or may not result in it being
101 // removed from the DownloadManager queues and deleted
102 // (specifically, DownloadManager::RemoveDownload only
103 // removes and deletes it if it's known to the history service)
104 // so the only thing we know after calling this function is that
105 // the download was deleted if-and-only-if it was removed
106 // from all queues.
[email protected]303077002011-04-19 23:21:01107 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26108 } else if (download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07109 download->Cancel(false);
110 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29111 }
112 }
113
[email protected]f04182f32010-12-10 19:12:07114 // At this point, all dangerous downloads have had their files removed
115 // and all in progress downloads have been cancelled. We can now delete
116 // anything left.
[email protected]9ccbb372008-10-10 18:50:32117
[email protected]5cd11b6e2011-06-10 20:30:59118 // Copy downloads_ to separate container so as not to set off checks
119 // in DownloadItem destruction.
120 DownloadSet downloads_to_delete;
121 downloads_to_delete.swap(downloads_);
122
initial.commit09911bf2008-07-26 23:55:29123 in_progress_.clear();
[email protected]70850c72011-01-11 17:31:27124 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59125 history_downloads_.clear();
[email protected]57fd1252010-12-23 17:24:09126#if !defined(NDEBUG)
127 save_page_as_downloads_.clear();
128#endif
[email protected]5cd11b6e2011-06-10 20:30:59129 STLDeleteElements(&downloads_to_delete);
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();
[email protected]68a49e52011-01-28 10:39:51139 download_prefs_.reset();
[email protected]82f37b02010-07-29 22:04:57140
initial.commit09911bf2008-07-26 23:55:29141 shutdown_needed_ = false;
142}
143
[email protected]82f37b02010-07-29 22:04:57144void DownloadManager::GetTemporaryDownloads(
145 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
146 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58147
[email protected]f04182f32010-12-10 19:12:07148 for (DownloadMap::iterator it = history_downloads_.begin();
149 it != history_downloads_.end(); ++it) {
[email protected]6aa4a1c02010-01-15 18:49:58150 if (it->second->is_temporary() &&
151 it->second->full_path().DirName() == dir_path)
[email protected]82f37b02010-07-29 22:04:57152 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58153 }
[email protected]6aa4a1c02010-01-15 18:49:58154}
155
[email protected]82f37b02010-07-29 22:04:57156void DownloadManager::GetAllDownloads(
157 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
158 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34159
[email protected]f04182f32010-12-10 19:12:07160 for (DownloadMap::iterator it = history_downloads_.begin();
161 it != history_downloads_.end(); ++it) {
[email protected]8ddbd66a2010-05-21 16:38:34162 if (!it->second->is_temporary() &&
163 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57164 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34165 }
[email protected]8ddbd66a2010-05-21 16:38:34166}
167
[email protected]82f37b02010-07-29 22:04:57168void DownloadManager::GetCurrentDownloads(
169 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
170 DCHECK(result);
[email protected]c4a530b2010-03-08 17:33:03171
[email protected]f04182f32010-12-10 19:12:07172 for (DownloadMap::iterator it = history_downloads_.begin();
173 it != history_downloads_.end(); ++it) {
[email protected]bf68a00b2011-04-07 17:28:26174 DownloadItem* item =it->second;
175 // Skip temporary items.
176 if (item->is_temporary())
177 continue;
178 // Skip items that have all their data, and are OK to save.
179 if (!item->IsPartialDownload() &&
180 (item->safety_state() != DownloadItem::DANGEROUS))
181 continue;
182 // Skip items that don't match |dir_path|.
183 // If |dir_path| is empty, all remaining items match.
184 if (!dir_path.empty() && (it->second->full_path().DirName() != dir_path))
185 continue;
186
187 result->push_back(item);
[email protected]c4a530b2010-03-08 17:33:03188 }
[email protected]f7e9fd62010-09-28 15:45:06189
190 // If we have a parent profile, let it add its downloads to the results.
191 Profile* original_profile = profile_->GetOriginalProfile();
192 if (original_profile != profile_)
193 original_profile->GetDownloadManager()->GetCurrentDownloads(dir_path,
194 result);
[email protected]c4a530b2010-03-08 17:33:03195}
196
[email protected]d3b12902010-08-16 23:39:42197void DownloadManager::SearchDownloads(const string16& query,
198 std::vector<DownloadItem*>* result) {
199 DCHECK(result);
200
[email protected]503d03872011-05-06 08:36:26201 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42202
[email protected]f04182f32010-12-10 19:12:07203 for (DownloadMap::iterator it = history_downloads_.begin();
204 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42205 DownloadItem* download_item = it->second;
206
207 if (download_item->is_temporary() || download_item->is_extension_install())
208 continue;
209
210 // Display Incognito downloads only in Incognito window, and vice versa.
211 // The Incognito Downloads page will get the list of non-Incognito downloads
212 // from its parent profile.
213 if (profile_->IsOffTheRecord() != download_item->is_otr())
214 continue;
215
216 if (download_item->MatchesQuery(query_lower))
217 result->push_back(download_item);
218 }
219
220 // If we have a parent profile, let it add its downloads to the results.
221 Profile* original_profile = profile_->GetOriginalProfile();
222 if (original_profile != profile_)
223 original_profile->GetDownloadManager()->SearchDownloads(query, result);
224}
225
initial.commit09911bf2008-07-26 23:55:29226// Query the history service for information about all persisted downloads.
227bool DownloadManager::Init(Profile* profile) {
228 DCHECK(profile);
229 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
230 shutdown_needed_ = true;
231
232 profile_ = profile;
[email protected]d3b12902010-08-16 23:39:42233 download_history_.reset(new DownloadHistory(profile));
[email protected]82f37b02010-07-29 22:04:57234 download_history_->Load(
235 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete));
[email protected]024f2f02010-04-30 22:51:46236
[email protected]e5dc4222010-08-30 22:16:32237 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs()));
238
[email protected]2941c2392010-07-15 22:54:30239 // In test mode, there may be no ResourceDispatcherHost. In this case it's
240 // safe to avoid setting |file_manager_| because we only call a small set of
241 // functions, none of which need it.
initial.commit09911bf2008-07-26 23:55:29242 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
[email protected]2941c2392010-07-15 22:54:30243 if (rdh) {
244 file_manager_ = rdh->download_file_manager();
245 DCHECK(file_manager_);
initial.commit09911bf2008-07-26 23:55:29246 }
247
[email protected]b0ab1d42010-02-24 19:29:28248 other_download_manager_observer_.reset(
249 new OtherDownloadManagerObserver(this));
250
initial.commit09911bf2008-07-26 23:55:29251 return true;
252}
253
initial.commit09911bf2008-07-26 23:55:29254// We have received a message from DownloadFileManager about a new download. We
255// create a download item and store it in our download map, and inform the
256// history system of a new download. Since this method can be called while the
257// history service thread is still reading the persistent state, we do not
[email protected]f04182f32010-12-10 19:12:07258// insert the new DownloadItem into 'history_downloads_' or inform our
[email protected]adb2f3d12011-01-23 16:24:54259// observers at this point. OnCreateDownloadEntryComplete() handles that
[email protected]f04182f32010-12-10 19:12:07260// finalization of the the download creation as a callback from the
261// history thread.
[email protected]4cd82f72011-05-23 19:15:01262void DownloadManager::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35264
[email protected]4cd82f72011-05-23 19:15:01265 DownloadItem* download = GetActiveDownloadItem(download_id);
266 if (!download)
267 return;
268
[email protected]287b86b2011-02-26 00:11:35269 // Create a client to verify download URL with safebrowsing.
270 // It deletes itself after the callback.
[email protected]26711732011-03-09 00:21:22271 scoped_refptr<DownloadSBClient> sb_client = new DownloadSBClient(
[email protected]4cd82f72011-05-23 19:15:01272 download_id, download->url_chain(), download->referrer_url());
[email protected]287b86b2011-02-26 00:11:35273 sb_client->CheckDownloadUrl(
[email protected]4cd82f72011-05-23 19:15:01274 NewCallback(this, &DownloadManager::CheckDownloadUrlDone));
[email protected]287b86b2011-02-26 00:11:35275}
276
[email protected]4cd82f72011-05-23 19:15:01277void DownloadManager::CheckDownloadUrlDone(int32 download_id,
[email protected]287b86b2011-02-26 00:11:35278 bool is_dangerous_url) {
279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29280
[email protected]4cd82f72011-05-23 19:15:01281 DownloadItem* download = GetActiveDownloadItem(download_id);
282 if (!download)
283 return;
284
285 if (is_dangerous_url)
286 download->MarkUrlDangerous();
287
[email protected]88008002011-05-24 23:14:15288 download_history_->CheckVisitedReferrerBefore(download_id,
289 download->referrer_url(),
290 NewCallback(this, &DownloadManager::CheckVisitedReferrerBeforeDone));
291}
292
293void DownloadManager::CheckVisitedReferrerBeforeDone(
294 int32 download_id,
295 bool visited_referrer_before) {
296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
297
298 DownloadItem* download = GetActiveDownloadItem(download_id);
299 if (!download)
300 return;
[email protected]287b86b2011-02-26 00:11:35301
[email protected]a60c8ae2009-12-25 06:50:57302 // Check whether this download is for an extension install or not.
[email protected]e6875c12010-07-18 11:14:13303 // Allow extensions to be explicitly saved.
[email protected]88008002011-05-24 23:14:15304 DownloadStateInfo state = download->state_info();
[email protected]4cd82f72011-05-23 19:15:01305 if (!state.prompt_user_for_save_location) {
306 if (UserScript::IsURLUserScript(download->GetURL(),
307 download->mime_type()) ||
308 (download->mime_type() == Extension::kMimeType)) {
309 state.is_extension_install = true;
[email protected]bac4f4b2011-03-05 02:01:40310 }
[email protected]a60c8ae2009-12-25 06:50:57311 }
312
[email protected]4cd82f72011-05-23 19:15:01313 if (state.force_file_name.empty()) {
[email protected]2941c2392010-07-15 22:54:30314 FilePath generated_name;
[email protected]4cd82f72011-05-23 19:15:01315 download_util::GenerateFileNameFromRequest(download->GetURL(),
316 download->content_disposition(),
317 download->referrer_charset(),
318 download->mime_type(),
319 &generated_name);
[email protected]2941c2392010-07-15 22:54:30320
321 // Freeze the user's preference for showing a Save As dialog. We're going
322 // to bounce around a bunch of threads and we don't want to worry about race
323 // conditions where the user changes this pref out from under us.
[email protected]0e9b6072011-02-17 12:42:05324 if (download_prefs_->PromptForDownload()) {
[email protected]2941c2392010-07-15 22:54:30325 // But ignore the user's preference for the following scenarios:
326 // 1) Extension installation. Note that we only care here about the case
327 // where an extension is installed, not when one is downloaded with
328 // "save as...".
329 // 2) Filetypes marked "always open." If the user just wants this file
330 // opened, don't bother asking where to keep it.
[email protected]4cd82f72011-05-23 19:15:01331 if (!state.is_extension_install &&
[email protected]2941c2392010-07-15 22:54:30332 !ShouldOpenFileBasedOnExtension(generated_name))
[email protected]4cd82f72011-05-23 19:15:01333 state.prompt_user_for_save_location = true;
[email protected]2941c2392010-07-15 22:54:30334 }
[email protected]14e0a102011-02-22 14:04:01335 if (download_prefs_->IsDownloadPathManaged()) {
[email protected]4cd82f72011-05-23 19:15:01336 state.prompt_user_for_save_location = false;
[email protected]14e0a102011-02-22 14:04:01337 }
[email protected]2941c2392010-07-15 22:54:30338
[email protected]8af9d032010-02-10 00:00:32339 // Determine the proper path for a download, by either one of the following:
340 // 1) using the default download directory.
341 // 2) prompting the user.
[email protected]4cd82f72011-05-23 19:15:01342 if (state.prompt_user_for_save_location && !last_download_path_.empty()) {
343 state.suggested_path = last_download_path_;
[email protected]80dc3612010-07-27 19:35:08344 } else {
[email protected]4cd82f72011-05-23 19:15:01345 state.suggested_path = download_prefs_->download_path();
[email protected]80dc3612010-07-27 19:35:08346 }
[email protected]4cd82f72011-05-23 19:15:01347 state.suggested_path = state.suggested_path.Append(generated_name);
[email protected]8af9d032010-02-10 00:00:32348 } else {
[email protected]4cd82f72011-05-23 19:15:01349 state.suggested_path = state.force_file_name;
[email protected]8af9d032010-02-10 00:00:32350 }
initial.commit09911bf2008-07-26 23:55:29351
[email protected]88008002011-05-24 23:14:15352 if (!state.prompt_user_for_save_location && state.force_file_name.empty()) {
353 state.is_dangerous_file =
354 IsDangerous(*download, state, visited_referrer_before);
355 }
[email protected]e9ebf3fc2008-10-17 22:06:58356
initial.commit09911bf2008-07-26 23:55:29357 // We need to move over to the download thread because we don't want to stat
358 // the suggested path on the UI thread.
[email protected]5a3b97e2010-10-05 09:49:11359 // We can only access preferences on the UI thread, so check the download path
360 // now and pass the value to the FILE thread.
[email protected]ca4b5fa32010-10-09 12:42:18361 BrowserThread::PostTask(
362 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37363 NewRunnableMethod(
[email protected]5a3b97e2010-10-05 09:49:11364 this,
365 &DownloadManager::CheckIfSuggestedPathExists,
[email protected]88008002011-05-24 23:14:15366 download->id(),
[email protected]4cd82f72011-05-23 19:15:01367 state,
[email protected]5a3b97e2010-10-05 09:49:11368 download_prefs()->download_path()));
initial.commit09911bf2008-07-26 23:55:29369}
370
[email protected]4cd82f72011-05-23 19:15:01371void DownloadManager::CheckIfSuggestedPathExists(int32 download_id,
372 DownloadStateInfo state,
[email protected]5a3b97e2010-10-05 09:49:11373 const FilePath& default_path) {
[email protected]ca4b5fa32010-10-09 12:42:18374 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
initial.commit09911bf2008-07-26 23:55:29375
[email protected]5a3b97e2010-10-05 09:49:11376 // Make sure the default download directory exists.
377 // TODO(phajdan.jr): only create the directory when we're sure the user
378 // is going to save there and not to another directory of his choice.
379 file_util::CreateDirectory(default_path);
380
initial.commit09911bf2008-07-26 23:55:29381 // Check writability of the suggested path. If we can't write to it, default
382 // to the user's "My Documents" directory. We'll prompt them in this case.
[email protected]4cd82f72011-05-23 19:15:01383 FilePath dir = state.suggested_path.DirName();
384 FilePath filename = state.suggested_path.BaseName();
[email protected]9ccbb372008-10-10 18:50:32385 if (!file_util::PathIsWritable(dir)) {
[email protected]da6e3922010-11-24 21:45:50386 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\"";
[email protected]4cd82f72011-05-23 19:15:01387 state.prompt_user_for_save_location = true;
388 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path);
389 state.suggested_path = state.suggested_path.Append(filename);
initial.commit09911bf2008-07-26 23:55:29390 }
391
[email protected]6cade212008-12-03 00:32:22392 // If the download is deemed dangerous, we'll use a temporary name for it.
[email protected]4cd82f72011-05-23 19:15:01393 if (state.IsDangerous()) {
394 state.target_name = FilePath(state.suggested_path).BaseName();
[email protected]9ccbb372008-10-10 18:50:32395 // Create a temporary file to hold the file until the user approves its
396 // download.
[email protected]7ae7c2cb2009-01-06 23:31:41397 FilePath::StringType file_name;
398 FilePath path;
[email protected]463e1b432011-01-21 22:05:51399#if defined(OS_WIN)
400 string16 unconfirmed_prefix =
401 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
402#else
403 std::string unconfirmed_prefix =
404 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
405#endif
406
[email protected]9ccbb372008-10-10 18:50:32407 while (path.empty()) {
[email protected]2594c2b2010-11-08 23:04:26408 base::SStringPrintf(
409 &file_name,
[email protected]463e1b432011-01-21 22:05:51410 unconfirmed_prefix.append(
411 FILE_PATH_LITERAL(" %d.crdownload")).c_str(),
[email protected]2594c2b2010-11-08 23:04:26412 base::RandInt(0, 100000));
[email protected]7ae7c2cb2009-01-06 23:31:41413 path = dir.Append(file_name);
[email protected]7d3851d82008-12-12 03:26:07414 if (file_util::PathExists(path))
[email protected]7ae7c2cb2009-01-06 23:31:41415 path = FilePath();
[email protected]9ccbb372008-10-10 18:50:32416 }
[email protected]4cd82f72011-05-23 19:15:01417 state.suggested_path = path;
[email protected]7a256ea2008-10-17 17:34:16418 } else {
[email protected]594cd7d2010-07-21 03:23:56419 // Do not add the path uniquifier if we are saving to a specific path as in
420 // the drag-out case.
[email protected]4cd82f72011-05-23 19:15:01421 if (state.force_file_name.empty()) {
422 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload(
423 state.suggested_path);
[email protected]594cd7d2010-07-21 03:23:56424 }
[email protected]7a256ea2008-10-17 17:34:16425 // We know the final path, build it if necessary.
[email protected]4cd82f72011-05-23 19:15:01426 if (state.path_uniquifier > 0) {
427 download_util::AppendNumberToPath(&(state.suggested_path),
428 state.path_uniquifier);
[email protected]7a256ea2008-10-17 17:34:16429 // Setting path_uniquifier to 0 to make sure we don't try to unique it
430 // later on.
[email protected]4cd82f72011-05-23 19:15:01431 state.path_uniquifier = 0;
432 } else if (state.path_uniquifier == -1) {
[email protected]7d3851d82008-12-12 03:26:07433 // We failed to find a unique path. We have to prompt the user.
[email protected]da6e3922010-11-24 21:45:50434 VLOG(1) << "Unable to find a unique path for suggested path \""
[email protected]4cd82f72011-05-23 19:15:01435 << state.suggested_path.value() << "\"";
436 state.prompt_user_for_save_location = true;
[email protected]7a256ea2008-10-17 17:34:16437 }
[email protected]9ccbb372008-10-10 18:50:32438 }
439
[email protected]594cd7d2010-07-21 03:23:56440 // Create an empty file at the suggested path so that we don't allocate the
441 // same "non-existant" path to multiple downloads.
442 // See: http://code.google.com/p/chromium/issues/detail?id=3662
[email protected]4cd82f72011-05-23 19:15:01443 if (!state.prompt_user_for_save_location &&
444 state.force_file_name.empty()) {
445 if (state.IsDangerous())
446 file_util::WriteFile(state.suggested_path, "", 0);
[email protected]594cd7d2010-07-21 03:23:56447 else
448 file_util::WriteFile(download_util::GetCrDownloadPath(
[email protected]4cd82f72011-05-23 19:15:01449 state.suggested_path), "", 0);
[email protected]7d3851d82008-12-12 03:26:07450 }
451
[email protected]ca4b5fa32010-10-09 12:42:18452 BrowserThread::PostTask(
453 BrowserThread::UI, FROM_HERE,
initial.commit09911bf2008-07-26 23:55:29454 NewRunnableMethod(this,
455 &DownloadManager::OnPathExistenceAvailable,
[email protected]4cd82f72011-05-23 19:15:01456 download_id,
457 state));
initial.commit09911bf2008-07-26 23:55:29458}
459
[email protected]88008002011-05-24 23:14:15460void DownloadManager::OnPathExistenceAvailable(int32 download_id,
461 DownloadStateInfo new_state) {
[email protected]ca4b5fa32010-10-09 12:42:18462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29463
[email protected]4cd82f72011-05-23 19:15:01464 DownloadItem* download = GetActiveDownloadItem(download_id);
465 if (!download)
466 return;
467
468 VLOG(20) << __FUNCTION__ << "()"
469 << " download = " << download->DebugString(true);
470
471 download->SetFileCheckResults(new_state);
472
473 FilePath suggested_path = download->suggested_path();
474
475 if (download->save_as()) {
initial.commit09911bf2008-07-26 23:55:29476 // We must ask the user for the place to put the download.
477 if (!select_file_dialog_.get())
478 select_file_dialog_ = SelectFileDialog::Create(this);
479
[email protected]db6831a2011-06-09 21:08:28480 DownloadRequestHandle request_handle = download->request_handle();
481 TabContents* contents = request_handle.GetTabContents();
[email protected]b949f1112009-04-12 20:03:08482 SelectFileDialog::FileTypeInfo file_type_info;
[email protected]4cd82f72011-05-23 19:15:01483 FilePath::StringType extension = suggested_path.Extension();
[email protected]07038782011-04-29 17:08:07484 if (!extension.empty()) {
485 extension.erase(extension.begin()); // drop the .
486 file_type_info.extensions.resize(1);
487 file_type_info.extensions[0].push_back(extension);
488 }
[email protected]b949f1112009-04-12 20:03:08489 file_type_info.include_all_files = true;
[email protected]076700e62009-04-01 18:41:23490 gfx::NativeWindow owning_window =
491 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL;
[email protected]4cd82f72011-05-23 19:15:01492 // |id_ptr| will be deleted in either FileSelected() or
493 // FileSelectionCancelled().
494 int32* id_ptr = new int32;
495 *id_ptr = download_id;
initial.commit09911bf2008-07-26 23:55:29496 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE,
[email protected]561abe62009-04-06 18:08:34497 string16(),
[email protected]4cd82f72011-05-23 19:15:01498 suggested_path,
[email protected]b949f1112009-04-12 20:03:08499 &file_type_info, 0, FILE_PATH_LITERAL(""),
[email protected]4cd82f72011-05-23 19:15:01500 contents, owning_window,
501 reinterpret_cast<void*>(id_ptr));
[email protected]f5920322011-03-24 20:34:16502 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]4cd82f72011-05-23 19:15:01503 SelectFileDialogDisplayed(download_id));
initial.commit09911bf2008-07-26 23:55:29504 } else {
505 // No prompting for download, just continue with the suggested name.
[email protected]4cd82f72011-05-23 19:15:01506 ContinueDownloadWithPath(download, suggested_path);
initial.commit09911bf2008-07-26 23:55:29507 }
508}
509
[email protected]c2e76012010-12-23 21:10:29510void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) {
511 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
512
513 DownloadItem* download = new DownloadItem(this, *info,
514 profile_->IsOffTheRecord());
[email protected]4cd82f72011-05-23 19:15:01515 int32 download_id = info->download_id;
516 DCHECK(!ContainsKey(in_progress_, download_id));
517 DCHECK(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29518 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01519 active_downloads_[download_id] = download;
[email protected]c2e76012010-12-23 21:10:29520}
521
[email protected]4cd82f72011-05-23 19:15:01522void DownloadManager::ContinueDownloadWithPath(DownloadItem* download,
523 const FilePath& chosen_file) {
[email protected]ca4b5fa32010-10-09 12:42:18524 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01525 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39526
[email protected]4cd82f72011-05-23 19:15:01527 int32 download_id = download->id();
initial.commit09911bf2008-07-26 23:55:29528
[email protected]70850c72011-01-11 17:31:27529 // NOTE(ahendrickson) Eventually |active_downloads_| will replace
530 // |in_progress_|, but we don't want to change the semantics yet.
[email protected]4cd82f72011-05-23 19:15:01531 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]70850c72011-01-11 17:31:27532 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01533 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27534
[email protected]4cd82f72011-05-23 19:15:01535 // Make sure the initial file name is set only once.
536 DCHECK(download->full_path().empty());
537 download->OnPathDetermined(chosen_file);
538 download->UpdateTarget();
539
540 VLOG(20) << __FUNCTION__ << "()"
541 << " download = " << download->DebugString(true);
542
543 in_progress_[download_id] = download;
[email protected]adb2f3d12011-01-23 16:24:54544 UpdateAppIcon(); // Reflect entry into in_progress_.
initial.commit09911bf2008-07-26 23:55:29545
[email protected]adb2f3d12011-01-23 16:24:54546 // Rename to intermediate name.
[email protected]f5920322011-03-24 20:34:16547 FilePath download_path;
[email protected]4cd82f72011-05-23 19:15:01548 if (download->IsDangerous()) {
[email protected]adb2f3d12011-01-23 16:24:54549 // The download is not safe. We can now rename the file to its
[email protected]f5920322011-03-24 20:34:16550 // tentative name using RenameInProgressDownloadFile.
551 // NOTE: The |Rename| below will be a no-op for dangerous files, as we're
552 // renaming it to the same name.
[email protected]4cd82f72011-05-23 19:15:01553 download_path = download->full_path();
[email protected]594cd7d2010-07-21 03:23:56554 } else {
[email protected]adb2f3d12011-01-23 16:24:54555 // The download is a safe download. We need to
556 // rename it to its intermediate '.crdownload' path. The final
557 // name after user confirmation will be set from
[email protected]48837962011-04-19 17:03:29558 // DownloadItem::OnDownloadCompleting.
[email protected]4cd82f72011-05-23 19:15:01559 download_path =
560 download_util::GetCrDownloadPath(download->full_path());
[email protected]594cd7d2010-07-21 03:23:56561 }
562
[email protected]f5920322011-03-24 20:34:16563 BrowserThread::PostTask(
564 BrowserThread::FILE, FROM_HERE,
565 NewRunnableMethod(
566 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile,
567 download->id(), download_path));
568
569 download->Rename(download_path);
570
[email protected]4cd82f72011-05-23 19:15:01571 download_history_->AddEntry(download,
[email protected]82f37b02010-07-29 22:04:57572 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete));
initial.commit09911bf2008-07-26 23:55:29573}
574
initial.commit09911bf2008-07-26 23:55:29575void DownloadManager::UpdateDownload(int32 download_id, int64 size) {
[email protected]70850c72011-01-11 17:31:27576 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
577 DownloadMap::iterator it = active_downloads_.find(download_id);
578 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29579 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26580 if (download->IsInProgress()) {
[email protected]70850c72011-01-11 17:31:27581 download->Update(size);
[email protected]adb2f3d12011-01-23 16:24:54582 UpdateAppIcon(); // Reflect size updates.
[email protected]70850c72011-01-11 17:31:27583 download_history_->UpdateEntry(download);
584 }
initial.commit09911bf2008-07-26 23:55:29585 }
586}
587
[email protected]bf68a00b2011-04-07 17:28:26588void DownloadManager::OnResponseCompleted(int32 download_id,
589 int64 size,
590 int os_error,
591 const std::string& hash) {
592 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
593 if (os_error == 0) {
594 OnAllDataSaved(download_id, size, hash);
595 } else {
596 OnDownloadError(download_id, size, os_error);
597 }
598}
599
[email protected]26711732011-03-09 00:21:22600void DownloadManager::OnAllDataSaved(int32 download_id,
601 int64 size,
602 const std::string& hash) {
[email protected]da6e3922010-11-24 21:45:50603 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
604 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35605 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32606
[email protected]c4f02c42011-01-24 21:55:06607 // If it's not in active_downloads_, that means it was cancelled; just
608 // ignore the notification.
609 if (active_downloads_.count(download_id) == 0)
610 return;
611
[email protected]adb2f3d12011-01-23 16:24:54612 DownloadItem* download = active_downloads_[download_id];
[email protected]a850ba42010-09-10 22:00:30613 download->OnAllDataSaved(size);
[email protected]9ccbb372008-10-10 18:50:32614
[email protected]26711732011-03-09 00:21:22615 // When hash is not available, it means either it is not calculated
616 // or there is error while it is calculated. We will skip the download hash
617 // check in that case.
618 if (!hash.empty()) {
619 scoped_refptr<DownloadSBClient> sb_client =
620 new DownloadSBClient(download_id,
[email protected]8799e542011-04-20 03:47:34621 download->url_chain(),
[email protected]26711732011-03-09 00:21:22622 download->referrer_url());
623 sb_client->CheckDownloadHash(
624 hash, NewCallback(this, &DownloadManager::CheckDownloadHashDone));
625 }
[email protected]adb2f3d12011-01-23 16:24:54626 MaybeCompleteDownload(download);
627}
[email protected]9ccbb372008-10-10 18:50:32628
[email protected]26711732011-03-09 00:21:22629// TODO(lzheng): This function currently works as a callback place holder.
630// Once we decide the hash check is reliable, we could move the
631// MaybeCompleteDownload in OnAllDataSaved to this function.
632void DownloadManager::CheckDownloadHashDone(int32 download_id,
633 bool is_dangerous_hash) {
634 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
635 DVLOG(1) << "CheckDownloadHashDone, download_id: " << download_id
636 << " is dangerous_hash: " << is_dangerous_hash;
637
638 // If it's not in active_downloads_, that means it was cancelled or
639 // the download already finished.
640 if (active_downloads_.count(download_id) == 0)
641 return;
642
643 DVLOG(1) << "CheckDownloadHashDone, url: "
[email protected]4cd82f72011-05-23 19:15:01644 << active_downloads_[download_id]->GetURL().spec();
[email protected]26711732011-03-09 00:21:22645}
646
[email protected]5cd11b6e2011-06-10 20:30:59647void DownloadManager::AssertNotInQueues(DownloadItem* download) {
648 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
649 CHECK(!ContainsKey(downloads_, download));
650 CHECK(!ContainsKey(active_downloads_, download->id()));
651 CHECK(!ContainsKey(in_progress_, download->id()));
652 CHECK(!ContainsKey(history_downloads_, download->db_handle()));
653}
654
[email protected]adb2f3d12011-01-23 16:24:54655bool DownloadManager::IsDownloadReadyForCompletion(DownloadItem* download) {
656 // If we don't have all the data, the download is not ready for
657 // completion.
658 if (!download->all_data_saved())
659 return false;
[email protected]6a7fb042010-02-01 16:30:47660
[email protected]9d7ef802011-02-25 19:03:35661 // If the download is dangerous, but not yet validated, it's not ready for
662 // completion.
663 if (download->safety_state() == DownloadItem::DANGEROUS)
664 return false;
665
[email protected]adb2f3d12011-01-23 16:24:54666 // If the download isn't active (e.g. has been cancelled) it's not
667 // ready for completion.
668 if (active_downloads_.count(download->id()) == 0)
669 return false;
670
671 // If the download hasn't been inserted into the history system
672 // (which occurs strictly after file name determination, intermediate
673 // file rename, and UI display) then it's not ready for completion.
674 return (download->db_handle() != DownloadHistory::kUninitializedHandle);
675}
676
677void DownloadManager::MaybeCompleteDownload(DownloadItem* download) {
678 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
679 VLOG(20) << __FUNCTION__ << "()" << " download = "
680 << download->DebugString(false);
681
682 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32683 return;
[email protected]9ccbb372008-10-10 18:50:32684
[email protected]adb2f3d12011-01-23 16:24:54685 // TODO(rdsmith): DCHECK that we only pass through this point
686 // once per download. The natural way to do this is by a state
687 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56688
[email protected]adb2f3d12011-01-23 16:24:54689 // Confirm we're in the proper set of states to be here;
[email protected]9d7ef802011-02-25 19:03:35690 // in in_progress_, have all data, have a history handle, (validated or safe).
691 DCHECK_NE(DownloadItem::DANGEROUS, download->safety_state());
[email protected]adb2f3d12011-01-23 16:24:54692 DCHECK_EQ(1u, in_progress_.count(download->id()));
693 DCHECK(download->all_data_saved());
694 DCHECK(download->db_handle() != DownloadHistory::kUninitializedHandle);
695 DCHECK_EQ(1u, history_downloads_.count(download->db_handle()));
696
697 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
698 << download->DebugString(false);
699
700 // Remove the id from in_progress
701 in_progress_.erase(download->id());
702 UpdateAppIcon(); // Reflect removal from in_progress_.
703
[email protected]adb2f3d12011-01-23 16:24:54704 download_history_->UpdateEntry(download);
705
[email protected]f5920322011-03-24 20:34:16706 // Finish the download.
[email protected]48837962011-04-19 17:03:29707 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32708}
709
[email protected]cc3c7c092011-05-09 18:40:21710void DownloadManager::DownloadCompleted(int32 download_id) {
[email protected]70850c72011-01-11 17:31:27711 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21712 DownloadItem* download = GetDownloadItem(download_id);
713 DCHECK(download);
714 download_history_->UpdateEntry(download);
[email protected]70850c72011-01-11 17:31:27715 active_downloads_.erase(download_id);
716}
717
[email protected]f5920322011-03-24 20:34:16718void DownloadManager::OnDownloadRenamedToFinalName(int download_id,
719 const FilePath& full_path,
720 int uniquifier) {
[email protected]da6e3922010-11-24 21:45:50721 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
[email protected]f5920322011-03-24 20:34:16722 << " full_path = \"" << full_path.value() << "\""
723 << " uniquifier = " << uniquifier;
[email protected]ca4b5fa32010-10-09 12:42:18724 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f5920322011-03-24 20:34:16725
[email protected]2e030682010-07-23 19:45:36726 DownloadItem* item = GetDownloadItem(download_id);
727 if (!item)
728 return;
[email protected]6cade212008-12-03 00:32:22729
[email protected]8fa1eeb52011-04-13 14:18:02730 if (item->safety_state() == DownloadItem::SAFE) {
731 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice";
732 }
733
[email protected]ca4b5fa32010-10-09 12:42:18734 BrowserThread::PostTask(
[email protected]f5920322011-03-24 20:34:16735 BrowserThread::FILE, FROM_HERE,
736 NewRunnableMethod(
737 file_manager_, &DownloadFileManager::CompleteDownload, download_id));
[email protected]9ccbb372008-10-10 18:50:32738
[email protected]f5920322011-03-24 20:34:16739 if (uniquifier)
740 item->set_path_uniquifier(uniquifier);
[email protected]9ccbb372008-10-10 18:50:32741
[email protected]f5920322011-03-24 20:34:16742 item->OnDownloadRenamedToFinalName(full_path);
743 download_history_->UpdateDownloadPath(item, full_path);
initial.commit09911bf2008-07-26 23:55:29744}
745
initial.commit09911bf2008-07-26 23:55:29746void DownloadManager::DownloadCancelled(int32 download_id) {
[email protected]70850c72011-01-11 17:31:27747 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29748 DownloadMap::iterator it = in_progress_.find(download_id);
749 if (it == in_progress_.end())
750 return;
751 DownloadItem* download = it->second;
752
[email protected]da6e3922010-11-24 21:45:50753 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
754 << " download = " << download->DebugString(true);
755
initial.commit09911bf2008-07-26 23:55:29756 // Clean up will happen when the history system create callback runs if we
757 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57758 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
initial.commit09911bf2008-07-26 23:55:29759 in_progress_.erase(it);
[email protected]70850c72011-01-11 17:31:27760 active_downloads_.erase(download_id);
[email protected]adb2f3d12011-01-23 16:24:54761 UpdateAppIcon(); // Reflect removal from in_progress_.
[email protected]82f37b02010-07-29 22:04:57762 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29763 }
764
[email protected]db6831a2011-06-09 21:08:28765 DownloadCancelledInternal(download_id, download->request_handle());
[email protected]d7d1c5c2009-08-05 23:52:50766}
767
[email protected]bc932ef2011-05-11 12:14:13768void DownloadManager::DownloadCancelledInternal(
[email protected]db6831a2011-06-09 21:08:28769 int download_id, DownloadRequestHandle request_handle) {
[email protected]f5920322011-03-24 20:34:16770 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]db6831a2011-06-09 21:08:28771 request_handle.CancelRequest();
[email protected]d7d1c5c2009-08-05 23:52:50772
[email protected]ca4b5fa32010-10-09 12:42:18773 BrowserThread::PostTask(
774 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37775 NewRunnableMethod(
776 file_manager_, &DownloadFileManager::CancelDownload, download_id));
initial.commit09911bf2008-07-26 23:55:29777}
778
[email protected]bf68a00b2011-04-07 17:28:26779void DownloadManager::OnDownloadError(int32 download_id,
780 int64 size,
781 int os_error) {
782 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
783 DownloadMap::iterator it = active_downloads_.find(download_id);
784 // A cancel at the right time could remove the download from the
785 // |active_downloads_| map before we get here.
786 if (it == active_downloads_.end())
787 return;
788
789 DownloadItem* download = it->second;
790
[email protected]4cd82f72011-05-23 19:15:01791 VLOG(20) << __FUNCTION__ << "()" << " Error " << os_error
792 << " at offset " << download->received_bytes()
793 << " for download = " << download->DebugString(true);
794
795 download->Interrupted(size, os_error);
[email protected]bf68a00b2011-04-07 17:28:26796
797 // TODO(ahendrickson) - Remove this when we add resuming of interrupted
798 // downloads, as we will keep the download item around in that case.
799 //
800 // Clean up will happen when the history system create callback runs if we
801 // don't have a valid db_handle yet.
802 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
803 in_progress_.erase(download_id);
804 active_downloads_.erase(download_id);
805 UpdateAppIcon(); // Reflect removal from in_progress_.
806 download_history_->UpdateEntry(download);
807 }
808
[email protected]bf68a00b2011-04-07 17:28:26809 BrowserThread::PostTask(
810 BrowserThread::FILE, FROM_HERE,
811 NewRunnableMethod(
812 file_manager_, &DownloadFileManager::CancelDownload, download_id));
813}
814
[email protected]6a7fb042010-02-01 16:30:47815void DownloadManager::UpdateAppIcon() {
[email protected]073ed7b2010-09-27 09:20:02816 if (status_updater_)
817 status_updater_->Update();
[email protected]6a7fb042010-02-01 16:30:47818}
819
initial.commit09911bf2008-07-26 23:55:29820void DownloadManager::RemoveDownload(int64 download_handle) {
[email protected]f04182f32010-12-10 19:12:07821 DownloadMap::iterator it = history_downloads_.find(download_handle);
822 if (it == history_downloads_.end())
initial.commit09911bf2008-07-26 23:55:29823 return;
824
825 // Make history update.
826 DownloadItem* download = it->second;
[email protected]82f37b02010-07-29 22:04:57827 download_history_->RemoveEntry(download);
initial.commit09911bf2008-07-26 23:55:29828
829 // Remove from our tables and delete.
[email protected]f04182f32010-12-10 19:12:07830 history_downloads_.erase(it);
831 int downloads_count = downloads_.erase(download);
832 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29833
834 // Tell observers to refresh their views.
[email protected]b0ab1d42010-02-24 19:29:28835 NotifyModelChanged();
[email protected]6f712872008-11-07 00:35:36836
837 delete download;
initial.commit09911bf2008-07-26 23:55:29838}
839
[email protected]e93d2822009-01-30 05:59:59840int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin,
841 const base::Time remove_end) {
[email protected]82f37b02010-07-29 22:04:57842 download_history_->RemoveEntriesBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29843
[email protected]a312a442010-12-15 23:40:33844 // All downloads visible to the user will be in the history,
845 // so scan that map.
[email protected]f04182f32010-12-10 19:12:07846 DownloadMap::iterator it = history_downloads_.begin();
[email protected]78b8fcc92009-03-31 17:36:28847 std::vector<DownloadItem*> pending_deletes;
[email protected]f04182f32010-12-10 19:12:07848 while (it != history_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29849 DownloadItem* download = it->second;
initial.commit09911bf2008-07-26 23:55:29850 if (download->start_time() >= remove_begin &&
851 (remove_end.is_null() || download->start_time() < remove_end) &&
[email protected]bf68a00b2011-04-07 17:28:26852 (download->IsComplete() ||
853 download->IsCancelled() ||
854 download->IsInterrupted())) {
initial.commit09911bf2008-07-26 23:55:29855 // Remove from the map and move to the next in the list.
[email protected]f04182f32010-12-10 19:12:07856 history_downloads_.erase(it++);
[email protected]a6604d92008-10-30 00:58:58857
858 // Also remove it from any completed dangerous downloads.
[email protected]78b8fcc92009-03-31 17:36:28859 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29860
initial.commit09911bf2008-07-26 23:55:29861 continue;
862 }
863
864 ++it;
865 }
866
[email protected]a312a442010-12-15 23:40:33867 // If we aren't deleting anything, we're done.
[email protected]57fd1252010-12-23 17:24:09868 if (pending_deletes.empty())
869 return 0;
initial.commit09911bf2008-07-26 23:55:29870
[email protected]a312a442010-12-15 23:40:33871 // Remove the chosen downloads from the main owning container.
872 for (std::vector<DownloadItem*>::iterator it = pending_deletes.begin();
873 it != pending_deletes.end(); it++) {
874 downloads_.erase(*it);
875 }
876
877 // Tell observers to refresh their views.
878 NotifyModelChanged();
879
880 // Delete the download items themselves.
[email protected]57fd1252010-12-23 17:24:09881 int num_deleted = static_cast<int>(pending_deletes.size());
882
[email protected]78b8fcc92009-03-31 17:36:28883 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
884 pending_deletes.clear();
885
initial.commit09911bf2008-07-26 23:55:29886 return num_deleted;
887}
888
[email protected]e93d2822009-01-30 05:59:59889int DownloadManager::RemoveDownloads(const base::Time remove_begin) {
890 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29891}
892
[email protected]d41355e6f2009-04-07 21:21:12893int DownloadManager::RemoveAllDownloads() {
[email protected]024f2f02010-04-30 22:51:46894 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) {
895 // This is an incognito downloader. Clear All should clear main download
896 // manager as well.
897 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads();
898 }
[email protected]d41355e6f2009-04-07 21:21:12899 // The null times make the date range unbounded.
900 return RemoveDownloadsBetween(base::Time(), base::Time());
901}
902
[email protected]ec4826a2010-09-21 09:15:59903void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download_item) {
[email protected]57fd1252010-12-23 17:24:09904#if !defined(NDEBUG)
905 save_page_as_downloads_.insert(download_item);
906#endif
[email protected]f04182f32010-12-10 19:12:07907 downloads_.insert(download_item);
[email protected]ec4826a2010-09-21 09:15:59908}
909
initial.commit09911bf2008-07-26 23:55:29910// Initiate a download of a specific URL. We send the request to the
911// ResourceDispatcherHost, and let it send us responses like a regular
912// download.
913void DownloadManager::DownloadUrl(const GURL& url,
914 const GURL& referrer,
[email protected]c9825a42009-05-01 22:51:50915 const std::string& referrer_charset,
[email protected]57c6a652009-05-04 07:58:34916 TabContents* tab_contents) {
[email protected]ae8945192010-07-20 16:56:26917 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
918 tab_contents);
[email protected]6aa4a1c02010-01-15 18:49:58919}
920
921void DownloadManager::DownloadUrlToFile(const GURL& url,
922 const GURL& referrer,
923 const std::string& referrer_charset,
[email protected]8af9d032010-02-10 00:00:32924 const DownloadSaveInfo& save_info,
[email protected]6aa4a1c02010-01-15 18:49:58925 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:34926 DCHECK(tab_contents);
[email protected]ed24fad2011-05-10 22:44:01927 // We send a pointer to content::ResourceContext, instead of the usual
928 // reference, so that a copy of the object isn't made.
[email protected]ca4b5fa32010-10-09 12:42:18929 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26930 NewRunnableFunction(&download_util::DownloadUrl,
931 url,
932 referrer,
933 referrer_charset,
934 save_info,
935 g_browser_process->resource_dispatcher_host(),
936 tab_contents->GetRenderProcessHost()->id(),
937 tab_contents->render_view_host()->routing_id(),
[email protected]ed24fad2011-05-10 22:44:01938 &tab_contents->profile()->GetResourceContext()));
initial.commit09911bf2008-07-26 23:55:29939}
940
initial.commit09911bf2008-07-26 23:55:29941void DownloadManager::AddObserver(Observer* observer) {
942 observers_.AddObserver(observer);
943 observer->ModelChanged();
944}
945
946void DownloadManager::RemoveObserver(Observer* observer) {
947 observers_.RemoveObserver(observer);
948}
949
[email protected]eccb9d12009-10-28 05:40:09950bool DownloadManager::ShouldOpenFileBasedOnExtension(
951 const FilePath& path) const {
[email protected]eccb9d12009-10-28 05:40:09952 FilePath::StringType extension = path.Extension();
953 if (extension.empty())
954 return false;
[email protected]92e11c82010-01-13 06:39:56955 if (Extension::IsExtension(path))
956 return false;
[email protected]eccb9d12009-10-28 05:40:09957 DCHECK(extension[0] == FilePath::kExtensionSeparator);
958 extension.erase(0, 1);
[email protected]e5dc4222010-08-30 22:16:32959 return download_prefs_->IsAutoOpenEnabledForExtension(extension);
initial.commit09911bf2008-07-26 23:55:29960}
961
[email protected]073ed7b2010-09-27 09:20:02962bool DownloadManager::IsDownloadProgressKnown() {
963 for (DownloadMap::iterator i = in_progress_.begin();
964 i != in_progress_.end(); ++i) {
965 if (i->second->total_bytes() <= 0)
966 return false;
967 }
968
969 return true;
970}
971
972int64 DownloadManager::GetInProgressDownloadCount() {
973 return in_progress_.size();
974}
975
976int64 DownloadManager::GetReceivedDownloadBytes() {
977 DCHECK(IsDownloadProgressKnown());
978 int64 received_bytes = 0;
979 for (DownloadMap::iterator i = in_progress_.begin();
980 i != in_progress_.end(); ++i) {
981 received_bytes += i->second->received_bytes();
982 }
983 return received_bytes;
984}
985
986int64 DownloadManager::GetTotalDownloadBytes() {
987 DCHECK(IsDownloadProgressKnown());
988 int64 total_bytes = 0;
989 for (DownloadMap::iterator i = in_progress_.begin();
990 i != in_progress_.end(); ++i) {
991 total_bytes += i->second->total_bytes();
992 }
993 return total_bytes;
994}
995
[email protected]561abe62009-04-06 18:08:34996void DownloadManager::FileSelected(const FilePath& path,
[email protected]23b357b2009-03-30 20:02:36997 int index, void* params) {
[email protected]4cd82f72011-05-23 19:15:01998 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
999
1000 int32* id_ptr = reinterpret_cast<int32*>(params);
1001 DCHECK(id_ptr != NULL);
1002 int32 download_id = *id_ptr;
1003 delete id_ptr;
1004
1005 DownloadItem* download = GetActiveDownloadItem(download_id);
1006 if (!download)
1007 return;
1008 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
1009 << " download = " << download->DebugString(true);
1010
1011 if (download->save_as())
[email protected]7ae7c2cb2009-01-06 23:31:411012 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:351013
[email protected]4cd82f72011-05-23 19:15:011014 // Make sure the initial file name is set only once.
1015 ContinueDownloadWithPath(download, path);
initial.commit09911bf2008-07-26 23:55:291016}
1017
1018void DownloadManager::FileSelectionCanceled(void* params) {
1019 // The user didn't pick a place to save the file, so need to cancel the
1020 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:011021 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1022 int32* id_ptr = reinterpret_cast<int32*>(params);
1023 DCHECK(id_ptr != NULL);
1024 int32 download_id = *id_ptr;
1025 delete id_ptr;
1026
1027 DownloadItem* download = GetActiveDownloadItem(download_id);
1028 if (!download)
1029 return;
1030
1031 VLOG(20) << __FUNCTION__ << "()"
1032 << " download = " << download->DebugString(true);
1033
[email protected]db6831a2011-06-09 21:08:281034 DownloadCancelledInternal(download_id, download->request_handle());
[email protected]4cd82f72011-05-23 19:15:011035}
1036
1037// TODO(phajdan.jr): This is apparently not being exercised in tests.
1038bool DownloadManager::IsDangerous(const DownloadItem& download,
[email protected]88008002011-05-24 23:14:151039 const DownloadStateInfo& state,
1040 bool visited_referrer_before) {
[email protected]4cd82f72011-05-23 19:15:011041 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1042
1043 bool auto_open = ShouldOpenFileBasedOnExtension(state.suggested_path);
1044 download_util::DownloadDangerLevel danger_level =
1045 download_util::GetFileDangerLevel(state.suggested_path.BaseName());
1046
1047 if (danger_level == download_util::Dangerous)
1048 return !(auto_open && state.has_user_gesture);
1049
1050 if (danger_level == download_util::AllowOnUserGesture &&
[email protected]88008002011-05-24 23:14:151051 (!state.has_user_gesture || !visited_referrer_before))
[email protected]4cd82f72011-05-23 19:15:011052 return true;
1053
1054 if (state.is_extension_install) {
1055 // Extensions that are not from the gallery are considered dangerous.
1056 ExtensionService* service = profile()->GetExtensionService();
1057 if (!service || !service->IsDownloadFromGallery(download.GetURL(),
1058 download.referrer_url()))
1059 return true;
1060 }
1061 return false;
initial.commit09911bf2008-07-26 23:55:291062}
1063
[email protected]9ccbb372008-10-10 18:50:321064void DownloadManager::DangerousDownloadValidated(DownloadItem* download) {
[email protected]9d7ef802011-02-25 19:03:351065 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:321066 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state());
1067 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED);
1068 download->UpdateObservers();
1069
[email protected]9d7ef802011-02-25 19:03:351070 MaybeCompleteDownload(download);
[email protected]9ccbb372008-10-10 18:50:321071}
1072
initial.commit09911bf2008-07-26 23:55:291073// Operations posted to us from the history service ----------------------------
1074
1075// The history service has retrieved all download entries. 'entries' contains
[email protected]4cd82f72011-05-23 19:15:011076// 'DownloadHistoryInfo's in sorted order (by ascending start_time).
initial.commit09911bf2008-07-26 23:55:291077void DownloadManager::OnQueryDownloadEntriesComplete(
[email protected]4cd82f72011-05-23 19:15:011078 std::vector<DownloadHistoryInfo>* entries) {
initial.commit09911bf2008-07-26 23:55:291079 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]aa033af2010-07-27 18:16:391080 DownloadItem* download = new DownloadItem(this, entries->at(i));
[email protected]f04182f32010-12-10 19:12:071081 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
1082 downloads_.insert(download);
1083 history_downloads_[download->db_handle()] = download;
[email protected]da6e3922010-11-24 21:45:501084 VLOG(20) << __FUNCTION__ << "()" << i << ">"
1085 << " download = " << download->DebugString(true);
initial.commit09911bf2008-07-26 23:55:291086 }
[email protected]b0ab1d42010-02-24 19:29:281087 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:291088}
1089
initial.commit09911bf2008-07-26 23:55:291090// Once the new DownloadItem's creation info has been committed to the history
1091// service, we associate the DownloadItem with the db handle, update our
[email protected]f04182f32010-12-10 19:12:071092// 'history_downloads_' map and inform observers.
[email protected]4cd82f72011-05-23 19:15:011093void DownloadManager::OnCreateDownloadEntryComplete(int32 download_id,
1094 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:271095 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:011096 DownloadItem* download = GetActiveDownloadItem(download_id);
1097 if (!download)
1098 return;
initial.commit09911bf2008-07-26 23:55:291099
[email protected]da6e3922010-11-24 21:45:501100 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
[email protected]4cd82f72011-05-23 19:15:011101 << " download_id = " << download_id
[email protected]da6e3922010-11-24 21:45:501102 << " download = " << download->DebugString(true);
[email protected]d2a8fb72010-01-21 05:31:421103
[email protected]5bcd73eb2011-03-23 21:14:021104 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
1105 // call this function with an invalid |db_handle|. For instance, this can
1106 // happen when the history database is offline. We cannot have multiple
1107 // DownloadItems with the same invalid db_handle, so we need to assign a
1108 // unique |db_handle| here.
1109 if (db_handle == DownloadHistory::kUninitializedHandle)
1110 db_handle = download_history_->GetNextFakeDbHandle();
1111
1112 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle);
1113 download->set_db_handle(db_handle);
1114
[email protected]5bcd73eb2011-03-23 21:14:021115 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
1116 history_downloads_[download->db_handle()] = download;
initial.commit09911bf2008-07-26 23:55:291117
[email protected]70850c72011-01-11 17:31:271118 // Show in the appropriate browser UI.
[email protected]9d7ef802011-02-25 19:03:351119 // This includes buttons to save or cancel, for a dangerous download.
[email protected]4cd82f72011-05-23 19:15:011120 ShowDownloadInBrowser(download);
initial.commit09911bf2008-07-26 23:55:291121
1122 // Inform interested objects about the new download.
[email protected]b0ab1d42010-02-24 19:29:281123 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:291124
[email protected]bf68a00b2011-04-07 17:28:261125 // If the download is still in progress, try to complete it.
[email protected]adb2f3d12011-01-23 16:24:541126 //
[email protected]bf68a00b2011-04-07 17:28:261127 // Otherwise, download has been cancelled or interrupted before we've
1128 // received the DB handle. We post one final message to the history
1129 // service so that it can be properly in sync with the DownloadItem's
1130 // completion status, and also inform any observers so that they get
1131 // more than just the start notification.
1132 if (download->IsInProgress()) {
1133 MaybeCompleteDownload(download);
1134 } else {
1135 DCHECK(download->IsCancelled())
1136 << " download = " << download->DebugString(true);
[email protected]4cd82f72011-05-23 19:15:011137 in_progress_.erase(download_id);
1138 active_downloads_.erase(download_id);
[email protected]82f37b02010-07-29 22:04:571139 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:291140 download->UpdateObservers();
1141 }
1142}
1143
[email protected]4cd82f72011-05-23 19:15:011144void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]bc932ef2011-05-11 12:14:131145
[email protected]8ddbd66a2010-05-21 16:38:341146 // The 'contents' may no longer exist if the user closed the tab before we
1147 // get this start completion event. If it does, tell the origin TabContents
1148 // to display its download shelf.
[email protected]db6831a2011-06-09 21:08:281149 DownloadRequestHandle request_handle = download->request_handle();
1150 TabContents* contents = request_handle.GetTabContents();
[email protected]ddb85052011-05-18 14:40:271151 TabContentsWrapper* wrapper = NULL;
1152 if (contents)
1153 wrapper = TabContentsWrapper::GetCurrentWrapperForContents(contents);
[email protected]5e595482009-05-06 20:16:531154
1155 // If the contents no longer exists, we start the download in the last active
1156 // browser. This is not ideal but better than fully hiding the download from
1157 // the user.
[email protected]ddb85052011-05-18 14:40:271158 if (!wrapper) {
[email protected]5e595482009-05-06 20:16:531159 Browser* last_active = BrowserList::GetLastActive();
1160 if (last_active)
[email protected]ddb85052011-05-18 14:40:271161 wrapper = last_active->GetSelectedTabContentsWrapper();
[email protected]5e595482009-05-06 20:16:531162 }
1163
[email protected]ddb85052011-05-18 14:40:271164 if (!wrapper)
[email protected]bc932ef2011-05-11 12:14:131165 return;
1166
[email protected]ddb85052011-05-18 14:40:271167 wrapper->download_tab_helper()->OnStartDownload(download);
[email protected]5e595482009-05-06 20:16:531168}
1169
[email protected]6cade212008-12-03 00:32:221170// Clears the last download path, used to initialize "save as" dialogs.
[email protected]905a08d2008-11-19 07:24:121171void DownloadManager::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:411172 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:381173}
[email protected]b0ab1d42010-02-24 19:29:281174
1175void DownloadManager::NotifyModelChanged() {
1176 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
1177}
1178
[email protected]4cd82f72011-05-23 19:15:011179DownloadItem* DownloadManager::GetDownloadItem(int download_id) {
1180 // The |history_downloads_| map is indexed by the download's db_handle,
1181 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:071182 for (DownloadMap::iterator it = history_downloads_.begin();
1183 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:361184 DownloadItem* item = it->second;
[email protected]4cd82f72011-05-23 19:15:011185 if (item->id() == download_id)
[email protected]2e030682010-07-23 19:45:361186 return item;
1187 }
1188 return NULL;
1189}
1190
[email protected]4cd82f72011-05-23 19:15:011191DownloadItem* DownloadManager::GetActiveDownloadItem(int download_id) {
1192 DCHECK(ContainsKey(active_downloads_, download_id));
1193 DownloadItem* download = active_downloads_[download_id];
1194 DCHECK(download != NULL);
1195 return download;
1196}
1197
[email protected]57fd1252010-12-23 17:24:091198// Confirm that everything in all maps is also in |downloads_|, and that
1199// everything in |downloads_| is also in some other map.
[email protected]f04182f32010-12-10 19:12:071200void DownloadManager::AssertContainersConsistent() const {
1201#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:091202 // Turn everything into sets.
[email protected]adb2f3d12011-01-23 16:24:541203 DownloadSet active_set, history_set;
[email protected]70850c72011-01-11 17:31:271204 const DownloadMap* input_maps[] = {&active_downloads_, &history_downloads_};
[email protected]adb2f3d12011-01-23 16:24:541205 DownloadSet* local_sets[] = {&active_set, &history_set};
[email protected]57fd1252010-12-23 17:24:091206 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(local_sets));
1207 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
1208 for (DownloadMap::const_iterator it = input_maps[i]->begin();
1209 it != input_maps[i]->end(); it++) {
1210 local_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:071211 }
1212 }
[email protected]57fd1252010-12-23 17:24:091213
1214 // Check if each set is fully present in downloads, and create a union.
[email protected]adb2f3d12011-01-23 16:24:541215 const DownloadSet* all_sets[] = {&active_set, &history_set,
[email protected]57fd1252010-12-23 17:24:091216 &save_page_as_downloads_};
1217 DownloadSet downloads_union;
1218 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1219 DownloadSet remainder;
1220 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1221 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
1222 downloads_.begin(), downloads_.end(),
1223 insert_it);
1224 DCHECK(remainder.empty());
1225 std::insert_iterator<DownloadSet>
1226 insert_union(downloads_union, downloads_union.end());
1227 std::set_union(downloads_union.begin(), downloads_union.end(),
1228 all_sets[i]->begin(), all_sets[i]->end(),
1229 insert_union);
1230 }
1231
1232 // Is everything in downloads_ present in one of the other sets?
1233 DownloadSet remainder;
1234 std::insert_iterator<DownloadSet>
1235 insert_remainder(remainder, remainder.begin());
1236 std::set_difference(downloads_.begin(), downloads_.end(),
1237 downloads_union.begin(), downloads_union.end(),
1238 insert_remainder);
1239 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:071240#endif
1241}
1242
[email protected]b0ab1d42010-02-24 19:29:281243// DownloadManager::OtherDownloadManagerObserver implementation ----------------
1244
1245DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver(
1246 DownloadManager* observing_download_manager)
1247 : observing_download_manager_(observing_download_manager),
1248 observed_download_manager_(NULL) {
1249 if (observing_download_manager->profile_->GetOriginalProfile() ==
1250 observing_download_manager->profile_) {
1251 return;
1252 }
1253
1254 observed_download_manager_ = observing_download_manager_->
1255 profile_->GetOriginalProfile()->GetDownloadManager();
1256 observed_download_manager_->AddObserver(this);
1257}
1258
1259DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() {
1260 if (observed_download_manager_)
1261 observed_download_manager_->RemoveObserver(this);
1262}
1263
1264void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1265 observing_download_manager_->NotifyModelChanged();
1266}
1267
[email protected]b0ab1d42010-02-24 19:29:281268void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1269 observed_download_manager_ = NULL;
1270}