blob: 7475915ffe9742c6ad31f3678f4973417a787350 [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.
117 STLDeleteElements(&downloads_);
[email protected]9ccbb372008-10-10 18:50:32118
[email protected]f04182f32010-12-10 19:12:07119 // And clear all non-owning containers.
initial.commit09911bf2008-07-26 23:55:29120 in_progress_.clear();
[email protected]70850c72011-01-11 17:31:27121 active_downloads_.clear();
[email protected]57fd1252010-12-23 17:24:09122#if !defined(NDEBUG)
123 save_page_as_downloads_.clear();
124#endif
initial.commit09911bf2008-07-26 23:55:29125
126 file_manager_ = NULL;
127
initial.commit09911bf2008-07-26 23:55:29128 // Make sure the save as dialog doesn't notify us back if we're gone before
129 // it returns.
130 if (select_file_dialog_.get())
131 select_file_dialog_->ListenerDestroyed();
132
[email protected]82f37b02010-07-29 22:04:57133 download_history_.reset();
[email protected]68a49e52011-01-28 10:39:51134 download_prefs_.reset();
[email protected]82f37b02010-07-29 22:04:57135
initial.commit09911bf2008-07-26 23:55:29136 shutdown_needed_ = false;
137}
138
[email protected]82f37b02010-07-29 22:04:57139void DownloadManager::GetTemporaryDownloads(
140 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
141 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58142
[email protected]f04182f32010-12-10 19:12:07143 for (DownloadMap::iterator it = history_downloads_.begin();
144 it != history_downloads_.end(); ++it) {
[email protected]6aa4a1c02010-01-15 18:49:58145 if (it->second->is_temporary() &&
146 it->second->full_path().DirName() == dir_path)
[email protected]82f37b02010-07-29 22:04:57147 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58148 }
[email protected]6aa4a1c02010-01-15 18:49:58149}
150
[email protected]82f37b02010-07-29 22:04:57151void DownloadManager::GetAllDownloads(
152 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
153 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34154
[email protected]f04182f32010-12-10 19:12:07155 for (DownloadMap::iterator it = history_downloads_.begin();
156 it != history_downloads_.end(); ++it) {
[email protected]8ddbd66a2010-05-21 16:38:34157 if (!it->second->is_temporary() &&
158 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57159 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34160 }
[email protected]8ddbd66a2010-05-21 16:38:34161}
162
[email protected]82f37b02010-07-29 22:04:57163void DownloadManager::GetCurrentDownloads(
164 const FilePath& dir_path, std::vector<DownloadItem*>* result) {
165 DCHECK(result);
[email protected]c4a530b2010-03-08 17:33:03166
[email protected]f04182f32010-12-10 19:12:07167 for (DownloadMap::iterator it = history_downloads_.begin();
168 it != history_downloads_.end(); ++it) {
[email protected]bf68a00b2011-04-07 17:28:26169 DownloadItem* item =it->second;
170 // Skip temporary items.
171 if (item->is_temporary())
172 continue;
173 // Skip items that have all their data, and are OK to save.
174 if (!item->IsPartialDownload() &&
175 (item->safety_state() != DownloadItem::DANGEROUS))
176 continue;
177 // Skip items that don't match |dir_path|.
178 // If |dir_path| is empty, all remaining items match.
179 if (!dir_path.empty() && (it->second->full_path().DirName() != dir_path))
180 continue;
181
182 result->push_back(item);
[email protected]c4a530b2010-03-08 17:33:03183 }
[email protected]f7e9fd62010-09-28 15:45:06184
185 // If we have a parent profile, let it add its downloads to the results.
186 Profile* original_profile = profile_->GetOriginalProfile();
187 if (original_profile != profile_)
188 original_profile->GetDownloadManager()->GetCurrentDownloads(dir_path,
189 result);
[email protected]c4a530b2010-03-08 17:33:03190}
191
[email protected]d3b12902010-08-16 23:39:42192void DownloadManager::SearchDownloads(const string16& query,
193 std::vector<DownloadItem*>* result) {
194 DCHECK(result);
195
[email protected]503d03872011-05-06 08:36:26196 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42197
[email protected]f04182f32010-12-10 19:12:07198 for (DownloadMap::iterator it = history_downloads_.begin();
199 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42200 DownloadItem* download_item = it->second;
201
202 if (download_item->is_temporary() || download_item->is_extension_install())
203 continue;
204
205 // Display Incognito downloads only in Incognito window, and vice versa.
206 // The Incognito Downloads page will get the list of non-Incognito downloads
207 // from its parent profile.
208 if (profile_->IsOffTheRecord() != download_item->is_otr())
209 continue;
210
211 if (download_item->MatchesQuery(query_lower))
212 result->push_back(download_item);
213 }
214
215 // If we have a parent profile, let it add its downloads to the results.
216 Profile* original_profile = profile_->GetOriginalProfile();
217 if (original_profile != profile_)
218 original_profile->GetDownloadManager()->SearchDownloads(query, result);
219}
220
initial.commit09911bf2008-07-26 23:55:29221// Query the history service for information about all persisted downloads.
222bool DownloadManager::Init(Profile* profile) {
223 DCHECK(profile);
224 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
225 shutdown_needed_ = true;
226
227 profile_ = profile;
[email protected]d3b12902010-08-16 23:39:42228 download_history_.reset(new DownloadHistory(profile));
[email protected]82f37b02010-07-29 22:04:57229 download_history_->Load(
230 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete));
[email protected]024f2f02010-04-30 22:51:46231
[email protected]e5dc4222010-08-30 22:16:32232 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs()));
233
[email protected]2941c2392010-07-15 22:54:30234 // In test mode, there may be no ResourceDispatcherHost. In this case it's
235 // safe to avoid setting |file_manager_| because we only call a small set of
236 // functions, none of which need it.
initial.commit09911bf2008-07-26 23:55:29237 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
[email protected]2941c2392010-07-15 22:54:30238 if (rdh) {
239 file_manager_ = rdh->download_file_manager();
240 DCHECK(file_manager_);
initial.commit09911bf2008-07-26 23:55:29241 }
242
[email protected]b0ab1d42010-02-24 19:29:28243 other_download_manager_observer_.reset(
244 new OtherDownloadManagerObserver(this));
245
initial.commit09911bf2008-07-26 23:55:29246 return true;
247}
248
initial.commit09911bf2008-07-26 23:55:29249// We have received a message from DownloadFileManager about a new download. We
250// create a download item and store it in our download map, and inform the
251// history system of a new download. Since this method can be called while the
252// history service thread is still reading the persistent state, we do not
[email protected]f04182f32010-12-10 19:12:07253// insert the new DownloadItem into 'history_downloads_' or inform our
[email protected]adb2f3d12011-01-23 16:24:54254// observers at this point. OnCreateDownloadEntryComplete() handles that
[email protected]f04182f32010-12-10 19:12:07255// finalization of the the download creation as a callback from the
256// history thread.
[email protected]4cd82f72011-05-23 19:15:01257void DownloadManager::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35259
[email protected]4cd82f72011-05-23 19:15:01260 DownloadItem* download = GetActiveDownloadItem(download_id);
261 if (!download)
262 return;
263
[email protected]287b86b2011-02-26 00:11:35264 // Create a client to verify download URL with safebrowsing.
265 // It deletes itself after the callback.
[email protected]26711732011-03-09 00:21:22266 scoped_refptr<DownloadSBClient> sb_client = new DownloadSBClient(
[email protected]4cd82f72011-05-23 19:15:01267 download_id, download->url_chain(), download->referrer_url());
[email protected]287b86b2011-02-26 00:11:35268 sb_client->CheckDownloadUrl(
[email protected]4cd82f72011-05-23 19:15:01269 NewCallback(this, &DownloadManager::CheckDownloadUrlDone));
[email protected]287b86b2011-02-26 00:11:35270}
271
[email protected]4cd82f72011-05-23 19:15:01272void DownloadManager::CheckDownloadUrlDone(int32 download_id,
[email protected]287b86b2011-02-26 00:11:35273 bool is_dangerous_url) {
274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29275
[email protected]4cd82f72011-05-23 19:15:01276 DownloadItem* download = GetActiveDownloadItem(download_id);
277 if (!download)
278 return;
279
280 if (is_dangerous_url)
281 download->MarkUrlDangerous();
282
[email protected]88008002011-05-24 23:14:15283 download_history_->CheckVisitedReferrerBefore(download_id,
284 download->referrer_url(),
285 NewCallback(this, &DownloadManager::CheckVisitedReferrerBeforeDone));
286}
287
288void DownloadManager::CheckVisitedReferrerBeforeDone(
289 int32 download_id,
290 bool visited_referrer_before) {
291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
292
293 DownloadItem* download = GetActiveDownloadItem(download_id);
294 if (!download)
295 return;
[email protected]287b86b2011-02-26 00:11:35296
[email protected]a60c8ae2009-12-25 06:50:57297 // Check whether this download is for an extension install or not.
[email protected]e6875c12010-07-18 11:14:13298 // Allow extensions to be explicitly saved.
[email protected]88008002011-05-24 23:14:15299 DownloadStateInfo state = download->state_info();
[email protected]4cd82f72011-05-23 19:15:01300 if (!state.prompt_user_for_save_location) {
301 if (UserScript::IsURLUserScript(download->GetURL(),
302 download->mime_type()) ||
303 (download->mime_type() == Extension::kMimeType)) {
304 state.is_extension_install = true;
[email protected]bac4f4b2011-03-05 02:01:40305 }
[email protected]a60c8ae2009-12-25 06:50:57306 }
307
[email protected]4cd82f72011-05-23 19:15:01308 if (state.force_file_name.empty()) {
[email protected]2941c2392010-07-15 22:54:30309 FilePath generated_name;
[email protected]4cd82f72011-05-23 19:15:01310 download_util::GenerateFileNameFromRequest(download->GetURL(),
311 download->content_disposition(),
312 download->referrer_charset(),
313 download->mime_type(),
314 &generated_name);
[email protected]2941c2392010-07-15 22:54:30315
316 // Freeze the user's preference for showing a Save As dialog. We're going
317 // to bounce around a bunch of threads and we don't want to worry about race
318 // conditions where the user changes this pref out from under us.
[email protected]0e9b6072011-02-17 12:42:05319 if (download_prefs_->PromptForDownload()) {
[email protected]2941c2392010-07-15 22:54:30320 // But ignore the user's preference for the following scenarios:
321 // 1) Extension installation. Note that we only care here about the case
322 // where an extension is installed, not when one is downloaded with
323 // "save as...".
324 // 2) Filetypes marked "always open." If the user just wants this file
325 // opened, don't bother asking where to keep it.
[email protected]4cd82f72011-05-23 19:15:01326 if (!state.is_extension_install &&
[email protected]2941c2392010-07-15 22:54:30327 !ShouldOpenFileBasedOnExtension(generated_name))
[email protected]4cd82f72011-05-23 19:15:01328 state.prompt_user_for_save_location = true;
[email protected]2941c2392010-07-15 22:54:30329 }
[email protected]14e0a102011-02-22 14:04:01330 if (download_prefs_->IsDownloadPathManaged()) {
[email protected]4cd82f72011-05-23 19:15:01331 state.prompt_user_for_save_location = false;
[email protected]14e0a102011-02-22 14:04:01332 }
[email protected]2941c2392010-07-15 22:54:30333
[email protected]8af9d032010-02-10 00:00:32334 // Determine the proper path for a download, by either one of the following:
335 // 1) using the default download directory.
336 // 2) prompting the user.
[email protected]4cd82f72011-05-23 19:15:01337 if (state.prompt_user_for_save_location && !last_download_path_.empty()) {
338 state.suggested_path = last_download_path_;
[email protected]80dc3612010-07-27 19:35:08339 } else {
[email protected]4cd82f72011-05-23 19:15:01340 state.suggested_path = download_prefs_->download_path();
[email protected]80dc3612010-07-27 19:35:08341 }
[email protected]4cd82f72011-05-23 19:15:01342 state.suggested_path = state.suggested_path.Append(generated_name);
[email protected]8af9d032010-02-10 00:00:32343 } else {
[email protected]4cd82f72011-05-23 19:15:01344 state.suggested_path = state.force_file_name;
[email protected]8af9d032010-02-10 00:00:32345 }
initial.commit09911bf2008-07-26 23:55:29346
[email protected]88008002011-05-24 23:14:15347 if (!state.prompt_user_for_save_location && state.force_file_name.empty()) {
348 state.is_dangerous_file =
349 IsDangerous(*download, state, visited_referrer_before);
350 }
[email protected]e9ebf3fc2008-10-17 22:06:58351
initial.commit09911bf2008-07-26 23:55:29352 // We need to move over to the download thread because we don't want to stat
353 // the suggested path on the UI thread.
[email protected]5a3b97e2010-10-05 09:49:11354 // We can only access preferences on the UI thread, so check the download path
355 // now and pass the value to the FILE thread.
[email protected]ca4b5fa32010-10-09 12:42:18356 BrowserThread::PostTask(
357 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37358 NewRunnableMethod(
[email protected]5a3b97e2010-10-05 09:49:11359 this,
360 &DownloadManager::CheckIfSuggestedPathExists,
[email protected]88008002011-05-24 23:14:15361 download->id(),
[email protected]4cd82f72011-05-23 19:15:01362 state,
[email protected]5a3b97e2010-10-05 09:49:11363 download_prefs()->download_path()));
initial.commit09911bf2008-07-26 23:55:29364}
365
[email protected]4cd82f72011-05-23 19:15:01366void DownloadManager::CheckIfSuggestedPathExists(int32 download_id,
367 DownloadStateInfo state,
[email protected]5a3b97e2010-10-05 09:49:11368 const FilePath& default_path) {
[email protected]ca4b5fa32010-10-09 12:42:18369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
initial.commit09911bf2008-07-26 23:55:29370
[email protected]5a3b97e2010-10-05 09:49:11371 // Make sure the default download directory exists.
372 // TODO(phajdan.jr): only create the directory when we're sure the user
373 // is going to save there and not to another directory of his choice.
374 file_util::CreateDirectory(default_path);
375
initial.commit09911bf2008-07-26 23:55:29376 // Check writability of the suggested path. If we can't write to it, default
377 // to the user's "My Documents" directory. We'll prompt them in this case.
[email protected]4cd82f72011-05-23 19:15:01378 FilePath dir = state.suggested_path.DirName();
379 FilePath filename = state.suggested_path.BaseName();
[email protected]9ccbb372008-10-10 18:50:32380 if (!file_util::PathIsWritable(dir)) {
[email protected]da6e3922010-11-24 21:45:50381 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\"";
[email protected]4cd82f72011-05-23 19:15:01382 state.prompt_user_for_save_location = true;
383 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path);
384 state.suggested_path = state.suggested_path.Append(filename);
initial.commit09911bf2008-07-26 23:55:29385 }
386
[email protected]6cade212008-12-03 00:32:22387 // If the download is deemed dangerous, we'll use a temporary name for it.
[email protected]4cd82f72011-05-23 19:15:01388 if (state.IsDangerous()) {
389 state.target_name = FilePath(state.suggested_path).BaseName();
[email protected]9ccbb372008-10-10 18:50:32390 // Create a temporary file to hold the file until the user approves its
391 // download.
[email protected]7ae7c2cb2009-01-06 23:31:41392 FilePath::StringType file_name;
393 FilePath path;
[email protected]463e1b432011-01-21 22:05:51394#if defined(OS_WIN)
395 string16 unconfirmed_prefix =
396 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
397#else
398 std::string unconfirmed_prefix =
399 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
400#endif
401
[email protected]9ccbb372008-10-10 18:50:32402 while (path.empty()) {
[email protected]2594c2b2010-11-08 23:04:26403 base::SStringPrintf(
404 &file_name,
[email protected]463e1b432011-01-21 22:05:51405 unconfirmed_prefix.append(
406 FILE_PATH_LITERAL(" %d.crdownload")).c_str(),
[email protected]2594c2b2010-11-08 23:04:26407 base::RandInt(0, 100000));
[email protected]7ae7c2cb2009-01-06 23:31:41408 path = dir.Append(file_name);
[email protected]7d3851d82008-12-12 03:26:07409 if (file_util::PathExists(path))
[email protected]7ae7c2cb2009-01-06 23:31:41410 path = FilePath();
[email protected]9ccbb372008-10-10 18:50:32411 }
[email protected]4cd82f72011-05-23 19:15:01412 state.suggested_path = path;
[email protected]7a256ea2008-10-17 17:34:16413 } else {
[email protected]594cd7d2010-07-21 03:23:56414 // Do not add the path uniquifier if we are saving to a specific path as in
415 // the drag-out case.
[email protected]4cd82f72011-05-23 19:15:01416 if (state.force_file_name.empty()) {
417 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload(
418 state.suggested_path);
[email protected]594cd7d2010-07-21 03:23:56419 }
[email protected]7a256ea2008-10-17 17:34:16420 // We know the final path, build it if necessary.
[email protected]4cd82f72011-05-23 19:15:01421 if (state.path_uniquifier > 0) {
422 download_util::AppendNumberToPath(&(state.suggested_path),
423 state.path_uniquifier);
[email protected]7a256ea2008-10-17 17:34:16424 // Setting path_uniquifier to 0 to make sure we don't try to unique it
425 // later on.
[email protected]4cd82f72011-05-23 19:15:01426 state.path_uniquifier = 0;
427 } else if (state.path_uniquifier == -1) {
[email protected]7d3851d82008-12-12 03:26:07428 // We failed to find a unique path. We have to prompt the user.
[email protected]da6e3922010-11-24 21:45:50429 VLOG(1) << "Unable to find a unique path for suggested path \""
[email protected]4cd82f72011-05-23 19:15:01430 << state.suggested_path.value() << "\"";
431 state.prompt_user_for_save_location = true;
[email protected]7a256ea2008-10-17 17:34:16432 }
[email protected]9ccbb372008-10-10 18:50:32433 }
434
[email protected]594cd7d2010-07-21 03:23:56435 // Create an empty file at the suggested path so that we don't allocate the
436 // same "non-existant" path to multiple downloads.
437 // See: http://code.google.com/p/chromium/issues/detail?id=3662
[email protected]4cd82f72011-05-23 19:15:01438 if (!state.prompt_user_for_save_location &&
439 state.force_file_name.empty()) {
440 if (state.IsDangerous())
441 file_util::WriteFile(state.suggested_path, "", 0);
[email protected]594cd7d2010-07-21 03:23:56442 else
443 file_util::WriteFile(download_util::GetCrDownloadPath(
[email protected]4cd82f72011-05-23 19:15:01444 state.suggested_path), "", 0);
[email protected]7d3851d82008-12-12 03:26:07445 }
446
[email protected]ca4b5fa32010-10-09 12:42:18447 BrowserThread::PostTask(
448 BrowserThread::UI, FROM_HERE,
initial.commit09911bf2008-07-26 23:55:29449 NewRunnableMethod(this,
450 &DownloadManager::OnPathExistenceAvailable,
[email protected]4cd82f72011-05-23 19:15:01451 download_id,
452 state));
initial.commit09911bf2008-07-26 23:55:29453}
454
[email protected]88008002011-05-24 23:14:15455void DownloadManager::OnPathExistenceAvailable(int32 download_id,
456 DownloadStateInfo new_state) {
[email protected]ca4b5fa32010-10-09 12:42:18457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29458
[email protected]4cd82f72011-05-23 19:15:01459 DownloadItem* download = GetActiveDownloadItem(download_id);
460 if (!download)
461 return;
462
463 VLOG(20) << __FUNCTION__ << "()"
464 << " download = " << download->DebugString(true);
465
466 download->SetFileCheckResults(new_state);
467
468 FilePath suggested_path = download->suggested_path();
469
470 if (download->save_as()) {
initial.commit09911bf2008-07-26 23:55:29471 // We must ask the user for the place to put the download.
472 if (!select_file_dialog_.get())
473 select_file_dialog_ = SelectFileDialog::Create(this);
474
[email protected]db6831a2011-06-09 21:08:28475 DownloadRequestHandle request_handle = download->request_handle();
476 TabContents* contents = request_handle.GetTabContents();
[email protected]b949f1112009-04-12 20:03:08477 SelectFileDialog::FileTypeInfo file_type_info;
[email protected]4cd82f72011-05-23 19:15:01478 FilePath::StringType extension = suggested_path.Extension();
[email protected]07038782011-04-29 17:08:07479 if (!extension.empty()) {
480 extension.erase(extension.begin()); // drop the .
481 file_type_info.extensions.resize(1);
482 file_type_info.extensions[0].push_back(extension);
483 }
[email protected]b949f1112009-04-12 20:03:08484 file_type_info.include_all_files = true;
[email protected]076700e62009-04-01 18:41:23485 gfx::NativeWindow owning_window =
486 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL;
[email protected]4cd82f72011-05-23 19:15:01487 // |id_ptr| will be deleted in either FileSelected() or
488 // FileSelectionCancelled().
489 int32* id_ptr = new int32;
490 *id_ptr = download_id;
initial.commit09911bf2008-07-26 23:55:29491 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE,
[email protected]561abe62009-04-06 18:08:34492 string16(),
[email protected]4cd82f72011-05-23 19:15:01493 suggested_path,
[email protected]b949f1112009-04-12 20:03:08494 &file_type_info, 0, FILE_PATH_LITERAL(""),
[email protected]4cd82f72011-05-23 19:15:01495 contents, owning_window,
496 reinterpret_cast<void*>(id_ptr));
[email protected]f5920322011-03-24 20:34:16497 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]4cd82f72011-05-23 19:15:01498 SelectFileDialogDisplayed(download_id));
initial.commit09911bf2008-07-26 23:55:29499 } else {
500 // No prompting for download, just continue with the suggested name.
[email protected]4cd82f72011-05-23 19:15:01501 ContinueDownloadWithPath(download, suggested_path);
initial.commit09911bf2008-07-26 23:55:29502 }
503}
504
[email protected]c2e76012010-12-23 21:10:29505void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) {
506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
507
508 DownloadItem* download = new DownloadItem(this, *info,
509 profile_->IsOffTheRecord());
[email protected]4cd82f72011-05-23 19:15:01510 int32 download_id = info->download_id;
511 DCHECK(!ContainsKey(in_progress_, download_id));
512 DCHECK(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29513 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01514 active_downloads_[download_id] = download;
[email protected]c2e76012010-12-23 21:10:29515}
516
[email protected]4cd82f72011-05-23 19:15:01517void DownloadManager::ContinueDownloadWithPath(DownloadItem* download,
518 const FilePath& chosen_file) {
[email protected]ca4b5fa32010-10-09 12:42:18519 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01520 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39521
[email protected]4cd82f72011-05-23 19:15:01522 int32 download_id = download->id();
initial.commit09911bf2008-07-26 23:55:29523
[email protected]70850c72011-01-11 17:31:27524 // NOTE(ahendrickson) Eventually |active_downloads_| will replace
525 // |in_progress_|, but we don't want to change the semantics yet.
[email protected]4cd82f72011-05-23 19:15:01526 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]70850c72011-01-11 17:31:27527 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01528 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27529
[email protected]4cd82f72011-05-23 19:15:01530 // Make sure the initial file name is set only once.
531 DCHECK(download->full_path().empty());
532 download->OnPathDetermined(chosen_file);
533 download->UpdateTarget();
534
535 VLOG(20) << __FUNCTION__ << "()"
536 << " download = " << download->DebugString(true);
537
538 in_progress_[download_id] = download;
[email protected]adb2f3d12011-01-23 16:24:54539 UpdateAppIcon(); // Reflect entry into in_progress_.
initial.commit09911bf2008-07-26 23:55:29540
[email protected]adb2f3d12011-01-23 16:24:54541 // Rename to intermediate name.
[email protected]f5920322011-03-24 20:34:16542 FilePath download_path;
[email protected]4cd82f72011-05-23 19:15:01543 if (download->IsDangerous()) {
[email protected]adb2f3d12011-01-23 16:24:54544 // The download is not safe. We can now rename the file to its
[email protected]f5920322011-03-24 20:34:16545 // tentative name using RenameInProgressDownloadFile.
546 // NOTE: The |Rename| below will be a no-op for dangerous files, as we're
547 // renaming it to the same name.
[email protected]4cd82f72011-05-23 19:15:01548 download_path = download->full_path();
[email protected]594cd7d2010-07-21 03:23:56549 } else {
[email protected]adb2f3d12011-01-23 16:24:54550 // The download is a safe download. We need to
551 // rename it to its intermediate '.crdownload' path. The final
552 // name after user confirmation will be set from
[email protected]48837962011-04-19 17:03:29553 // DownloadItem::OnDownloadCompleting.
[email protected]4cd82f72011-05-23 19:15:01554 download_path =
555 download_util::GetCrDownloadPath(download->full_path());
[email protected]594cd7d2010-07-21 03:23:56556 }
557
[email protected]f5920322011-03-24 20:34:16558 BrowserThread::PostTask(
559 BrowserThread::FILE, FROM_HERE,
560 NewRunnableMethod(
561 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile,
562 download->id(), download_path));
563
564 download->Rename(download_path);
565
[email protected]4cd82f72011-05-23 19:15:01566 download_history_->AddEntry(download,
[email protected]82f37b02010-07-29 22:04:57567 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete));
initial.commit09911bf2008-07-26 23:55:29568}
569
initial.commit09911bf2008-07-26 23:55:29570void DownloadManager::UpdateDownload(int32 download_id, int64 size) {
[email protected]70850c72011-01-11 17:31:27571 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
572 DownloadMap::iterator it = active_downloads_.find(download_id);
573 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29574 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26575 if (download->IsInProgress()) {
[email protected]70850c72011-01-11 17:31:27576 download->Update(size);
[email protected]adb2f3d12011-01-23 16:24:54577 UpdateAppIcon(); // Reflect size updates.
[email protected]70850c72011-01-11 17:31:27578 download_history_->UpdateEntry(download);
579 }
initial.commit09911bf2008-07-26 23:55:29580 }
581}
582
[email protected]bf68a00b2011-04-07 17:28:26583void DownloadManager::OnResponseCompleted(int32 download_id,
584 int64 size,
585 int os_error,
586 const std::string& hash) {
587 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
588 if (os_error == 0) {
589 OnAllDataSaved(download_id, size, hash);
590 } else {
591 OnDownloadError(download_id, size, os_error);
592 }
593}
594
[email protected]26711732011-03-09 00:21:22595void DownloadManager::OnAllDataSaved(int32 download_id,
596 int64 size,
597 const std::string& hash) {
[email protected]da6e3922010-11-24 21:45:50598 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
599 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35600 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32601
[email protected]c4f02c42011-01-24 21:55:06602 // If it's not in active_downloads_, that means it was cancelled; just
603 // ignore the notification.
604 if (active_downloads_.count(download_id) == 0)
605 return;
606
[email protected]adb2f3d12011-01-23 16:24:54607 DownloadItem* download = active_downloads_[download_id];
[email protected]a850ba42010-09-10 22:00:30608 download->OnAllDataSaved(size);
[email protected]9ccbb372008-10-10 18:50:32609
[email protected]26711732011-03-09 00:21:22610 // When hash is not available, it means either it is not calculated
611 // or there is error while it is calculated. We will skip the download hash
612 // check in that case.
613 if (!hash.empty()) {
614 scoped_refptr<DownloadSBClient> sb_client =
615 new DownloadSBClient(download_id,
[email protected]8799e542011-04-20 03:47:34616 download->url_chain(),
[email protected]26711732011-03-09 00:21:22617 download->referrer_url());
618 sb_client->CheckDownloadHash(
619 hash, NewCallback(this, &DownloadManager::CheckDownloadHashDone));
620 }
[email protected]adb2f3d12011-01-23 16:24:54621 MaybeCompleteDownload(download);
622}
[email protected]9ccbb372008-10-10 18:50:32623
[email protected]26711732011-03-09 00:21:22624// TODO(lzheng): This function currently works as a callback place holder.
625// Once we decide the hash check is reliable, we could move the
626// MaybeCompleteDownload in OnAllDataSaved to this function.
627void DownloadManager::CheckDownloadHashDone(int32 download_id,
628 bool is_dangerous_hash) {
629 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
630 DVLOG(1) << "CheckDownloadHashDone, download_id: " << download_id
631 << " is dangerous_hash: " << is_dangerous_hash;
632
633 // If it's not in active_downloads_, that means it was cancelled or
634 // the download already finished.
635 if (active_downloads_.count(download_id) == 0)
636 return;
637
638 DVLOG(1) << "CheckDownloadHashDone, url: "
[email protected]4cd82f72011-05-23 19:15:01639 << active_downloads_[download_id]->GetURL().spec();
[email protected]26711732011-03-09 00:21:22640}
641
[email protected]adb2f3d12011-01-23 16:24:54642bool DownloadManager::IsDownloadReadyForCompletion(DownloadItem* download) {
643 // If we don't have all the data, the download is not ready for
644 // completion.
645 if (!download->all_data_saved())
646 return false;
[email protected]6a7fb042010-02-01 16:30:47647
[email protected]9d7ef802011-02-25 19:03:35648 // If the download is dangerous, but not yet validated, it's not ready for
649 // completion.
650 if (download->safety_state() == DownloadItem::DANGEROUS)
651 return false;
652
[email protected]adb2f3d12011-01-23 16:24:54653 // If the download isn't active (e.g. has been cancelled) it's not
654 // ready for completion.
655 if (active_downloads_.count(download->id()) == 0)
656 return false;
657
658 // If the download hasn't been inserted into the history system
659 // (which occurs strictly after file name determination, intermediate
660 // file rename, and UI display) then it's not ready for completion.
661 return (download->db_handle() != DownloadHistory::kUninitializedHandle);
662}
663
664void DownloadManager::MaybeCompleteDownload(DownloadItem* download) {
665 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
666 VLOG(20) << __FUNCTION__ << "()" << " download = "
667 << download->DebugString(false);
668
669 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32670 return;
[email protected]9ccbb372008-10-10 18:50:32671
[email protected]adb2f3d12011-01-23 16:24:54672 // TODO(rdsmith): DCHECK that we only pass through this point
673 // once per download. The natural way to do this is by a state
674 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56675
[email protected]adb2f3d12011-01-23 16:24:54676 // Confirm we're in the proper set of states to be here;
[email protected]9d7ef802011-02-25 19:03:35677 // in in_progress_, have all data, have a history handle, (validated or safe).
678 DCHECK_NE(DownloadItem::DANGEROUS, download->safety_state());
[email protected]adb2f3d12011-01-23 16:24:54679 DCHECK_EQ(1u, in_progress_.count(download->id()));
680 DCHECK(download->all_data_saved());
681 DCHECK(download->db_handle() != DownloadHistory::kUninitializedHandle);
682 DCHECK_EQ(1u, history_downloads_.count(download->db_handle()));
683
684 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
685 << download->DebugString(false);
686
687 // Remove the id from in_progress
688 in_progress_.erase(download->id());
689 UpdateAppIcon(); // Reflect removal from in_progress_.
690
[email protected]adb2f3d12011-01-23 16:24:54691 download_history_->UpdateEntry(download);
692
[email protected]f5920322011-03-24 20:34:16693 // Finish the download.
[email protected]48837962011-04-19 17:03:29694 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32695}
696
[email protected]cc3c7c092011-05-09 18:40:21697void DownloadManager::DownloadCompleted(int32 download_id) {
[email protected]70850c72011-01-11 17:31:27698 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21699 DownloadItem* download = GetDownloadItem(download_id);
700 DCHECK(download);
701 download_history_->UpdateEntry(download);
[email protected]70850c72011-01-11 17:31:27702 active_downloads_.erase(download_id);
703}
704
[email protected]f5920322011-03-24 20:34:16705void DownloadManager::OnDownloadRenamedToFinalName(int download_id,
706 const FilePath& full_path,
707 int uniquifier) {
[email protected]da6e3922010-11-24 21:45:50708 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
[email protected]f5920322011-03-24 20:34:16709 << " full_path = \"" << full_path.value() << "\""
710 << " uniquifier = " << uniquifier;
[email protected]ca4b5fa32010-10-09 12:42:18711 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f5920322011-03-24 20:34:16712
[email protected]2e030682010-07-23 19:45:36713 DownloadItem* item = GetDownloadItem(download_id);
714 if (!item)
715 return;
[email protected]6cade212008-12-03 00:32:22716
[email protected]8fa1eeb52011-04-13 14:18:02717 if (item->safety_state() == DownloadItem::SAFE) {
718 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice";
719 }
720
[email protected]ca4b5fa32010-10-09 12:42:18721 BrowserThread::PostTask(
[email protected]f5920322011-03-24 20:34:16722 BrowserThread::FILE, FROM_HERE,
723 NewRunnableMethod(
724 file_manager_, &DownloadFileManager::CompleteDownload, download_id));
[email protected]9ccbb372008-10-10 18:50:32725
[email protected]f5920322011-03-24 20:34:16726 if (uniquifier)
727 item->set_path_uniquifier(uniquifier);
[email protected]9ccbb372008-10-10 18:50:32728
[email protected]f5920322011-03-24 20:34:16729 item->OnDownloadRenamedToFinalName(full_path);
730 download_history_->UpdateDownloadPath(item, full_path);
initial.commit09911bf2008-07-26 23:55:29731}
732
initial.commit09911bf2008-07-26 23:55:29733void DownloadManager::DownloadCancelled(int32 download_id) {
[email protected]70850c72011-01-11 17:31:27734 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29735 DownloadMap::iterator it = in_progress_.find(download_id);
736 if (it == in_progress_.end())
737 return;
738 DownloadItem* download = it->second;
739
[email protected]da6e3922010-11-24 21:45:50740 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
741 << " download = " << download->DebugString(true);
742
initial.commit09911bf2008-07-26 23:55:29743 // Clean up will happen when the history system create callback runs if we
744 // don't have a valid db_handle yet.
[email protected]82f37b02010-07-29 22:04:57745 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
initial.commit09911bf2008-07-26 23:55:29746 in_progress_.erase(it);
[email protected]70850c72011-01-11 17:31:27747 active_downloads_.erase(download_id);
[email protected]adb2f3d12011-01-23 16:24:54748 UpdateAppIcon(); // Reflect removal from in_progress_.
[email protected]82f37b02010-07-29 22:04:57749 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:29750 }
751
[email protected]db6831a2011-06-09 21:08:28752 DownloadCancelledInternal(download_id, download->request_handle());
[email protected]d7d1c5c2009-08-05 23:52:50753}
754
[email protected]bc932ef2011-05-11 12:14:13755void DownloadManager::DownloadCancelledInternal(
[email protected]db6831a2011-06-09 21:08:28756 int download_id, DownloadRequestHandle request_handle) {
[email protected]f5920322011-03-24 20:34:16757 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]db6831a2011-06-09 21:08:28758 request_handle.CancelRequest();
[email protected]d7d1c5c2009-08-05 23:52:50759
[email protected]ca4b5fa32010-10-09 12:42:18760 BrowserThread::PostTask(
761 BrowserThread::FILE, FROM_HERE,
[email protected]d83d03aa2009-11-02 21:44:37762 NewRunnableMethod(
763 file_manager_, &DownloadFileManager::CancelDownload, download_id));
initial.commit09911bf2008-07-26 23:55:29764}
765
[email protected]bf68a00b2011-04-07 17:28:26766void DownloadManager::OnDownloadError(int32 download_id,
767 int64 size,
768 int os_error) {
769 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
770 DownloadMap::iterator it = active_downloads_.find(download_id);
771 // A cancel at the right time could remove the download from the
772 // |active_downloads_| map before we get here.
773 if (it == active_downloads_.end())
774 return;
775
776 DownloadItem* download = it->second;
777
[email protected]4cd82f72011-05-23 19:15:01778 VLOG(20) << __FUNCTION__ << "()" << " Error " << os_error
779 << " at offset " << download->received_bytes()
780 << " for download = " << download->DebugString(true);
781
782 download->Interrupted(size, os_error);
[email protected]bf68a00b2011-04-07 17:28:26783
784 // TODO(ahendrickson) - Remove this when we add resuming of interrupted
785 // downloads, as we will keep the download item around in that case.
786 //
787 // Clean up will happen when the history system create callback runs if we
788 // don't have a valid db_handle yet.
789 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
790 in_progress_.erase(download_id);
791 active_downloads_.erase(download_id);
792 UpdateAppIcon(); // Reflect removal from in_progress_.
793 download_history_->UpdateEntry(download);
794 }
795
[email protected]bf68a00b2011-04-07 17:28:26796 BrowserThread::PostTask(
797 BrowserThread::FILE, FROM_HERE,
798 NewRunnableMethod(
799 file_manager_, &DownloadFileManager::CancelDownload, download_id));
800}
801
[email protected]6a7fb042010-02-01 16:30:47802void DownloadManager::UpdateAppIcon() {
[email protected]073ed7b2010-09-27 09:20:02803 if (status_updater_)
804 status_updater_->Update();
[email protected]6a7fb042010-02-01 16:30:47805}
806
initial.commit09911bf2008-07-26 23:55:29807void DownloadManager::RemoveDownload(int64 download_handle) {
[email protected]f04182f32010-12-10 19:12:07808 DownloadMap::iterator it = history_downloads_.find(download_handle);
809 if (it == history_downloads_.end())
initial.commit09911bf2008-07-26 23:55:29810 return;
811
812 // Make history update.
813 DownloadItem* download = it->second;
[email protected]82f37b02010-07-29 22:04:57814 download_history_->RemoveEntry(download);
initial.commit09911bf2008-07-26 23:55:29815
816 // Remove from our tables and delete.
[email protected]f04182f32010-12-10 19:12:07817 history_downloads_.erase(it);
818 int downloads_count = downloads_.erase(download);
819 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29820
821 // Tell observers to refresh their views.
[email protected]b0ab1d42010-02-24 19:29:28822 NotifyModelChanged();
[email protected]6f712872008-11-07 00:35:36823
824 delete download;
initial.commit09911bf2008-07-26 23:55:29825}
826
[email protected]e93d2822009-01-30 05:59:59827int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin,
828 const base::Time remove_end) {
[email protected]82f37b02010-07-29 22:04:57829 download_history_->RemoveEntriesBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29830
[email protected]a312a442010-12-15 23:40:33831 // All downloads visible to the user will be in the history,
832 // so scan that map.
[email protected]f04182f32010-12-10 19:12:07833 DownloadMap::iterator it = history_downloads_.begin();
[email protected]78b8fcc92009-03-31 17:36:28834 std::vector<DownloadItem*> pending_deletes;
[email protected]f04182f32010-12-10 19:12:07835 while (it != history_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29836 DownloadItem* download = it->second;
initial.commit09911bf2008-07-26 23:55:29837 if (download->start_time() >= remove_begin &&
838 (remove_end.is_null() || download->start_time() < remove_end) &&
[email protected]bf68a00b2011-04-07 17:28:26839 (download->IsComplete() ||
840 download->IsCancelled() ||
841 download->IsInterrupted())) {
initial.commit09911bf2008-07-26 23:55:29842 // Remove from the map and move to the next in the list.
[email protected]f04182f32010-12-10 19:12:07843 history_downloads_.erase(it++);
[email protected]a6604d92008-10-30 00:58:58844
845 // Also remove it from any completed dangerous downloads.
[email protected]78b8fcc92009-03-31 17:36:28846 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29847
initial.commit09911bf2008-07-26 23:55:29848 continue;
849 }
850
851 ++it;
852 }
853
[email protected]a312a442010-12-15 23:40:33854 // If we aren't deleting anything, we're done.
[email protected]57fd1252010-12-23 17:24:09855 if (pending_deletes.empty())
856 return 0;
initial.commit09911bf2008-07-26 23:55:29857
[email protected]a312a442010-12-15 23:40:33858 // Remove the chosen downloads from the main owning container.
859 for (std::vector<DownloadItem*>::iterator it = pending_deletes.begin();
860 it != pending_deletes.end(); it++) {
861 downloads_.erase(*it);
862 }
863
864 // Tell observers to refresh their views.
865 NotifyModelChanged();
866
867 // Delete the download items themselves.
[email protected]57fd1252010-12-23 17:24:09868 int num_deleted = static_cast<int>(pending_deletes.size());
869
[email protected]78b8fcc92009-03-31 17:36:28870 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
871 pending_deletes.clear();
872
initial.commit09911bf2008-07-26 23:55:29873 return num_deleted;
874}
875
[email protected]e93d2822009-01-30 05:59:59876int DownloadManager::RemoveDownloads(const base::Time remove_begin) {
877 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29878}
879
[email protected]d41355e6f2009-04-07 21:21:12880int DownloadManager::RemoveAllDownloads() {
[email protected]024f2f02010-04-30 22:51:46881 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) {
882 // This is an incognito downloader. Clear All should clear main download
883 // manager as well.
884 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads();
885 }
[email protected]d41355e6f2009-04-07 21:21:12886 // The null times make the date range unbounded.
887 return RemoveDownloadsBetween(base::Time(), base::Time());
888}
889
[email protected]ec4826a2010-09-21 09:15:59890void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download_item) {
[email protected]57fd1252010-12-23 17:24:09891#if !defined(NDEBUG)
892 save_page_as_downloads_.insert(download_item);
893#endif
[email protected]f04182f32010-12-10 19:12:07894 downloads_.insert(download_item);
[email protected]ec4826a2010-09-21 09:15:59895}
896
initial.commit09911bf2008-07-26 23:55:29897// Initiate a download of a specific URL. We send the request to the
898// ResourceDispatcherHost, and let it send us responses like a regular
899// download.
900void DownloadManager::DownloadUrl(const GURL& url,
901 const GURL& referrer,
[email protected]c9825a42009-05-01 22:51:50902 const std::string& referrer_charset,
[email protected]57c6a652009-05-04 07:58:34903 TabContents* tab_contents) {
[email protected]ae8945192010-07-20 16:56:26904 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
905 tab_contents);
[email protected]6aa4a1c02010-01-15 18:49:58906}
907
908void DownloadManager::DownloadUrlToFile(const GURL& url,
909 const GURL& referrer,
910 const std::string& referrer_charset,
[email protected]8af9d032010-02-10 00:00:32911 const DownloadSaveInfo& save_info,
[email protected]6aa4a1c02010-01-15 18:49:58912 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:34913 DCHECK(tab_contents);
[email protected]ed24fad2011-05-10 22:44:01914 // We send a pointer to content::ResourceContext, instead of the usual
915 // reference, so that a copy of the object isn't made.
[email protected]ca4b5fa32010-10-09 12:42:18916 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]ae8945192010-07-20 16:56:26917 NewRunnableFunction(&download_util::DownloadUrl,
918 url,
919 referrer,
920 referrer_charset,
921 save_info,
922 g_browser_process->resource_dispatcher_host(),
923 tab_contents->GetRenderProcessHost()->id(),
924 tab_contents->render_view_host()->routing_id(),
[email protected]ed24fad2011-05-10 22:44:01925 &tab_contents->profile()->GetResourceContext()));
initial.commit09911bf2008-07-26 23:55:29926}
927
initial.commit09911bf2008-07-26 23:55:29928void DownloadManager::AddObserver(Observer* observer) {
929 observers_.AddObserver(observer);
930 observer->ModelChanged();
931}
932
933void DownloadManager::RemoveObserver(Observer* observer) {
934 observers_.RemoveObserver(observer);
935}
936
[email protected]eccb9d12009-10-28 05:40:09937bool DownloadManager::ShouldOpenFileBasedOnExtension(
938 const FilePath& path) const {
[email protected]eccb9d12009-10-28 05:40:09939 FilePath::StringType extension = path.Extension();
940 if (extension.empty())
941 return false;
[email protected]92e11c82010-01-13 06:39:56942 if (Extension::IsExtension(path))
943 return false;
[email protected]eccb9d12009-10-28 05:40:09944 DCHECK(extension[0] == FilePath::kExtensionSeparator);
945 extension.erase(0, 1);
[email protected]e5dc4222010-08-30 22:16:32946 return download_prefs_->IsAutoOpenEnabledForExtension(extension);
initial.commit09911bf2008-07-26 23:55:29947}
948
[email protected]073ed7b2010-09-27 09:20:02949bool DownloadManager::IsDownloadProgressKnown() {
950 for (DownloadMap::iterator i = in_progress_.begin();
951 i != in_progress_.end(); ++i) {
952 if (i->second->total_bytes() <= 0)
953 return false;
954 }
955
956 return true;
957}
958
959int64 DownloadManager::GetInProgressDownloadCount() {
960 return in_progress_.size();
961}
962
963int64 DownloadManager::GetReceivedDownloadBytes() {
964 DCHECK(IsDownloadProgressKnown());
965 int64 received_bytes = 0;
966 for (DownloadMap::iterator i = in_progress_.begin();
967 i != in_progress_.end(); ++i) {
968 received_bytes += i->second->received_bytes();
969 }
970 return received_bytes;
971}
972
973int64 DownloadManager::GetTotalDownloadBytes() {
974 DCHECK(IsDownloadProgressKnown());
975 int64 total_bytes = 0;
976 for (DownloadMap::iterator i = in_progress_.begin();
977 i != in_progress_.end(); ++i) {
978 total_bytes += i->second->total_bytes();
979 }
980 return total_bytes;
981}
982
[email protected]561abe62009-04-06 18:08:34983void DownloadManager::FileSelected(const FilePath& path,
[email protected]23b357b2009-03-30 20:02:36984 int index, void* params) {
[email protected]4cd82f72011-05-23 19:15:01985 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
986
987 int32* id_ptr = reinterpret_cast<int32*>(params);
988 DCHECK(id_ptr != NULL);
989 int32 download_id = *id_ptr;
990 delete id_ptr;
991
992 DownloadItem* download = GetActiveDownloadItem(download_id);
993 if (!download)
994 return;
995 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
996 << " download = " << download->DebugString(true);
997
998 if (download->save_as())
[email protected]7ae7c2cb2009-01-06 23:31:41999 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:351000
[email protected]4cd82f72011-05-23 19:15:011001 // Make sure the initial file name is set only once.
1002 ContinueDownloadWithPath(download, path);
initial.commit09911bf2008-07-26 23:55:291003}
1004
1005void DownloadManager::FileSelectionCanceled(void* params) {
1006 // The user didn't pick a place to save the file, so need to cancel the
1007 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:011008 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1009 int32* id_ptr = reinterpret_cast<int32*>(params);
1010 DCHECK(id_ptr != NULL);
1011 int32 download_id = *id_ptr;
1012 delete id_ptr;
1013
1014 DownloadItem* download = GetActiveDownloadItem(download_id);
1015 if (!download)
1016 return;
1017
1018 VLOG(20) << __FUNCTION__ << "()"
1019 << " download = " << download->DebugString(true);
1020
[email protected]db6831a2011-06-09 21:08:281021 DownloadCancelledInternal(download_id, download->request_handle());
[email protected]4cd82f72011-05-23 19:15:011022}
1023
1024// TODO(phajdan.jr): This is apparently not being exercised in tests.
1025bool DownloadManager::IsDangerous(const DownloadItem& download,
[email protected]88008002011-05-24 23:14:151026 const DownloadStateInfo& state,
1027 bool visited_referrer_before) {
[email protected]4cd82f72011-05-23 19:15:011028 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1029
1030 bool auto_open = ShouldOpenFileBasedOnExtension(state.suggested_path);
1031 download_util::DownloadDangerLevel danger_level =
1032 download_util::GetFileDangerLevel(state.suggested_path.BaseName());
1033
1034 if (danger_level == download_util::Dangerous)
1035 return !(auto_open && state.has_user_gesture);
1036
1037 if (danger_level == download_util::AllowOnUserGesture &&
[email protected]88008002011-05-24 23:14:151038 (!state.has_user_gesture || !visited_referrer_before))
[email protected]4cd82f72011-05-23 19:15:011039 return true;
1040
1041 if (state.is_extension_install) {
1042 // Extensions that are not from the gallery are considered dangerous.
1043 ExtensionService* service = profile()->GetExtensionService();
1044 if (!service || !service->IsDownloadFromGallery(download.GetURL(),
1045 download.referrer_url()))
1046 return true;
1047 }
1048 return false;
initial.commit09911bf2008-07-26 23:55:291049}
1050
[email protected]9ccbb372008-10-10 18:50:321051void DownloadManager::DangerousDownloadValidated(DownloadItem* download) {
[email protected]9d7ef802011-02-25 19:03:351052 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:321053 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state());
1054 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED);
1055 download->UpdateObservers();
1056
[email protected]9d7ef802011-02-25 19:03:351057 MaybeCompleteDownload(download);
[email protected]9ccbb372008-10-10 18:50:321058}
1059
initial.commit09911bf2008-07-26 23:55:291060// Operations posted to us from the history service ----------------------------
1061
1062// The history service has retrieved all download entries. 'entries' contains
[email protected]4cd82f72011-05-23 19:15:011063// 'DownloadHistoryInfo's in sorted order (by ascending start_time).
initial.commit09911bf2008-07-26 23:55:291064void DownloadManager::OnQueryDownloadEntriesComplete(
[email protected]4cd82f72011-05-23 19:15:011065 std::vector<DownloadHistoryInfo>* entries) {
initial.commit09911bf2008-07-26 23:55:291066 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]aa033af2010-07-27 18:16:391067 DownloadItem* download = new DownloadItem(this, entries->at(i));
[email protected]f04182f32010-12-10 19:12:071068 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
1069 downloads_.insert(download);
1070 history_downloads_[download->db_handle()] = download;
[email protected]da6e3922010-11-24 21:45:501071 VLOG(20) << __FUNCTION__ << "()" << i << ">"
1072 << " download = " << download->DebugString(true);
initial.commit09911bf2008-07-26 23:55:291073 }
[email protected]b0ab1d42010-02-24 19:29:281074 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:291075}
1076
initial.commit09911bf2008-07-26 23:55:291077// Once the new DownloadItem's creation info has been committed to the history
1078// service, we associate the DownloadItem with the db handle, update our
[email protected]f04182f32010-12-10 19:12:071079// 'history_downloads_' map and inform observers.
[email protected]4cd82f72011-05-23 19:15:011080void DownloadManager::OnCreateDownloadEntryComplete(int32 download_id,
1081 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:271082 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:011083 DownloadItem* download = GetActiveDownloadItem(download_id);
1084 if (!download)
1085 return;
initial.commit09911bf2008-07-26 23:55:291086
[email protected]da6e3922010-11-24 21:45:501087 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
[email protected]4cd82f72011-05-23 19:15:011088 << " download_id = " << download_id
[email protected]da6e3922010-11-24 21:45:501089 << " download = " << download->DebugString(true);
[email protected]d2a8fb72010-01-21 05:31:421090
[email protected]5bcd73eb2011-03-23 21:14:021091 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
1092 // call this function with an invalid |db_handle|. For instance, this can
1093 // happen when the history database is offline. We cannot have multiple
1094 // DownloadItems with the same invalid db_handle, so we need to assign a
1095 // unique |db_handle| here.
1096 if (db_handle == DownloadHistory::kUninitializedHandle)
1097 db_handle = download_history_->GetNextFakeDbHandle();
1098
1099 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle);
1100 download->set_db_handle(db_handle);
1101
[email protected]5bcd73eb2011-03-23 21:14:021102 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
1103 history_downloads_[download->db_handle()] = download;
initial.commit09911bf2008-07-26 23:55:291104
[email protected]70850c72011-01-11 17:31:271105 // Show in the appropriate browser UI.
[email protected]9d7ef802011-02-25 19:03:351106 // This includes buttons to save or cancel, for a dangerous download.
[email protected]4cd82f72011-05-23 19:15:011107 ShowDownloadInBrowser(download);
initial.commit09911bf2008-07-26 23:55:291108
1109 // Inform interested objects about the new download.
[email protected]b0ab1d42010-02-24 19:29:281110 NotifyModelChanged();
initial.commit09911bf2008-07-26 23:55:291111
[email protected]bf68a00b2011-04-07 17:28:261112 // If the download is still in progress, try to complete it.
[email protected]adb2f3d12011-01-23 16:24:541113 //
[email protected]bf68a00b2011-04-07 17:28:261114 // Otherwise, download has been cancelled or interrupted before we've
1115 // received the DB handle. We post one final message to the history
1116 // service so that it can be properly in sync with the DownloadItem's
1117 // completion status, and also inform any observers so that they get
1118 // more than just the start notification.
1119 if (download->IsInProgress()) {
1120 MaybeCompleteDownload(download);
1121 } else {
1122 DCHECK(download->IsCancelled())
1123 << " download = " << download->DebugString(true);
[email protected]4cd82f72011-05-23 19:15:011124 in_progress_.erase(download_id);
1125 active_downloads_.erase(download_id);
[email protected]82f37b02010-07-29 22:04:571126 download_history_->UpdateEntry(download);
initial.commit09911bf2008-07-26 23:55:291127 download->UpdateObservers();
1128 }
1129}
1130
[email protected]4cd82f72011-05-23 19:15:011131void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]bc932ef2011-05-11 12:14:131132
[email protected]8ddbd66a2010-05-21 16:38:341133 // The 'contents' may no longer exist if the user closed the tab before we
1134 // get this start completion event. If it does, tell the origin TabContents
1135 // to display its download shelf.
[email protected]db6831a2011-06-09 21:08:281136 DownloadRequestHandle request_handle = download->request_handle();
1137 TabContents* contents = request_handle.GetTabContents();
[email protected]ddb85052011-05-18 14:40:271138 TabContentsWrapper* wrapper = NULL;
1139 if (contents)
1140 wrapper = TabContentsWrapper::GetCurrentWrapperForContents(contents);
[email protected]5e595482009-05-06 20:16:531141
1142 // If the contents no longer exists, we start the download in the last active
1143 // browser. This is not ideal but better than fully hiding the download from
1144 // the user.
[email protected]ddb85052011-05-18 14:40:271145 if (!wrapper) {
[email protected]5e595482009-05-06 20:16:531146 Browser* last_active = BrowserList::GetLastActive();
1147 if (last_active)
[email protected]ddb85052011-05-18 14:40:271148 wrapper = last_active->GetSelectedTabContentsWrapper();
[email protected]5e595482009-05-06 20:16:531149 }
1150
[email protected]ddb85052011-05-18 14:40:271151 if (!wrapper)
[email protected]bc932ef2011-05-11 12:14:131152 return;
1153
[email protected]ddb85052011-05-18 14:40:271154 wrapper->download_tab_helper()->OnStartDownload(download);
[email protected]5e595482009-05-06 20:16:531155}
1156
[email protected]6cade212008-12-03 00:32:221157// Clears the last download path, used to initialize "save as" dialogs.
[email protected]905a08d2008-11-19 07:24:121158void DownloadManager::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:411159 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:381160}
[email protected]b0ab1d42010-02-24 19:29:281161
1162void DownloadManager::NotifyModelChanged() {
1163 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
1164}
1165
[email protected]4cd82f72011-05-23 19:15:011166DownloadItem* DownloadManager::GetDownloadItem(int download_id) {
1167 // The |history_downloads_| map is indexed by the download's db_handle,
1168 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:071169 for (DownloadMap::iterator it = history_downloads_.begin();
1170 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:361171 DownloadItem* item = it->second;
[email protected]4cd82f72011-05-23 19:15:011172 if (item->id() == download_id)
[email protected]2e030682010-07-23 19:45:361173 return item;
1174 }
1175 return NULL;
1176}
1177
[email protected]4cd82f72011-05-23 19:15:011178DownloadItem* DownloadManager::GetActiveDownloadItem(int download_id) {
1179 DCHECK(ContainsKey(active_downloads_, download_id));
1180 DownloadItem* download = active_downloads_[download_id];
1181 DCHECK(download != NULL);
1182 return download;
1183}
1184
[email protected]57fd1252010-12-23 17:24:091185// Confirm that everything in all maps is also in |downloads_|, and that
1186// everything in |downloads_| is also in some other map.
[email protected]f04182f32010-12-10 19:12:071187void DownloadManager::AssertContainersConsistent() const {
1188#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:091189 // Turn everything into sets.
[email protected]adb2f3d12011-01-23 16:24:541190 DownloadSet active_set, history_set;
[email protected]70850c72011-01-11 17:31:271191 const DownloadMap* input_maps[] = {&active_downloads_, &history_downloads_};
[email protected]adb2f3d12011-01-23 16:24:541192 DownloadSet* local_sets[] = {&active_set, &history_set};
[email protected]57fd1252010-12-23 17:24:091193 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(local_sets));
1194 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
1195 for (DownloadMap::const_iterator it = input_maps[i]->begin();
1196 it != input_maps[i]->end(); it++) {
1197 local_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:071198 }
1199 }
[email protected]57fd1252010-12-23 17:24:091200
1201 // Check if each set is fully present in downloads, and create a union.
[email protected]adb2f3d12011-01-23 16:24:541202 const DownloadSet* all_sets[] = {&active_set, &history_set,
[email protected]57fd1252010-12-23 17:24:091203 &save_page_as_downloads_};
1204 DownloadSet downloads_union;
1205 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1206 DownloadSet remainder;
1207 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1208 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
1209 downloads_.begin(), downloads_.end(),
1210 insert_it);
1211 DCHECK(remainder.empty());
1212 std::insert_iterator<DownloadSet>
1213 insert_union(downloads_union, downloads_union.end());
1214 std::set_union(downloads_union.begin(), downloads_union.end(),
1215 all_sets[i]->begin(), all_sets[i]->end(),
1216 insert_union);
1217 }
1218
1219 // Is everything in downloads_ present in one of the other sets?
1220 DownloadSet remainder;
1221 std::insert_iterator<DownloadSet>
1222 insert_remainder(remainder, remainder.begin());
1223 std::set_difference(downloads_.begin(), downloads_.end(),
1224 downloads_union.begin(), downloads_union.end(),
1225 insert_remainder);
1226 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:071227#endif
1228}
1229
[email protected]b0ab1d42010-02-24 19:29:281230// DownloadManager::OtherDownloadManagerObserver implementation ----------------
1231
1232DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver(
1233 DownloadManager* observing_download_manager)
1234 : observing_download_manager_(observing_download_manager),
1235 observed_download_manager_(NULL) {
1236 if (observing_download_manager->profile_->GetOriginalProfile() ==
1237 observing_download_manager->profile_) {
1238 return;
1239 }
1240
1241 observed_download_manager_ = observing_download_manager_->
1242 profile_->GetOriginalProfile()->GetDownloadManager();
1243 observed_download_manager_->AddObserver(this);
1244}
1245
1246DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() {
1247 if (observed_download_manager_)
1248 observed_download_manager_->RemoveObserver(this);
1249}
1250
1251void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1252 observing_download_manager_->NotifyModelChanged();
1253}
1254
[email protected]b0ab1d42010-02-24 19:29:281255void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1256 observed_download_manager_ = NULL;
1257}