blob: 394de5eda20b16423b4e262893f73b4b61b1fd3c [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]71bf3f5e2011-08-15 21:05:225#include "content/browser/download/download_manager.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]e7557f172011-08-19 23:42:017#include <iterator>
8
[email protected]2041cf342010-02-19 03:15:599#include "base/callback.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/file_util.h"
[email protected]503d03872011-05-06 08:36:2611#include "base/i18n/case_conversion.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/logging.h"
[email protected]7286e3fc2011-07-19 22:13:2413#include "base/stl_util.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/task.h"
[email protected]d2a8fb72010-01-21 05:31:4215#include "build/build_config.h"
[email protected]6d0c9fb2011-08-22 19:26:0316#include "content/browser/browser_context.h"
[email protected]7324d1d2011-03-01 05:02:1617#include "content/browser/browser_thread.h"
[email protected]a0ce3282011-08-19 20:49:5218#include "content/browser/content_browser_client.h"
[email protected]71bf3f5e2011-08-15 21:05:2219#include "content/browser/download/download_create_info.h"
20#include "content/browser/download/download_file_manager.h"
21#include "content/browser/download/download_item.h"
[email protected]d6b7fd1e2011-08-16 22:42:0022#include "content/browser/download/download_manager_delegate.h"
[email protected]bb1a4212011-08-22 22:38:2523#include "content/browser/download/download_persistent_store_info.h"
[email protected]71bf3f5e2011-08-15 21:05:2224#include "content/browser/download/download_status_updater.h"
[email protected]7324d1d2011-03-01 05:02:1625#include "content/browser/renderer_host/render_process_host.h"
26#include "content/browser/renderer_host/render_view_host.h"
27#include "content/browser/renderer_host/resource_dispatcher_host.h"
28#include "content/browser/tab_contents/tab_contents.h"
[email protected]432115822011-07-10 15:52:2729#include "content/common/content_notification_types.h"
[email protected]6d0146c2011-08-04 19:13:0430#include "content/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2931
[email protected]a0ce3282011-08-19 20:49:5232namespace {
33
34void BeginDownload(
35 const GURL& url,
36 const GURL& referrer,
37 const DownloadSaveInfo& save_info,
[email protected]c79a0c02011-08-22 22:37:3738 ResourceDispatcherHost* resource_dispatcher_host,
39 int render_process_id,
[email protected]a0ce3282011-08-19 20:49:5240 int render_view_id,
41 const content::ResourceContext* context) {
[email protected]c79a0c02011-08-22 22:37:3742 resource_dispatcher_host->BeginDownload(
43 url, referrer, save_info, true, render_process_id, render_view_id,
44 *context);
[email protected]a0ce3282011-08-19 20:49:5245}
46
47} // namespace
48
[email protected]da1a27b2011-07-29 23:16:3349DownloadManager::DownloadManager(DownloadManagerDelegate* delegate,
50 DownloadStatusUpdater* status_updater)
initial.commit09911bf2008-07-26 23:55:2951 : shutdown_needed_(false),
[email protected]6d0c9fb2011-08-22 19:26:0352 browser_context_(NULL),
[email protected]073ed7b2010-09-27 09:20:0253 file_manager_(NULL),
[email protected]c2475df2011-09-07 23:52:2254 status_updater_(status_updater->AsWeakPtr()),
[email protected]d8472d92011-08-26 20:15:2055 delegate_(delegate),
56 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) {
[email protected]c2475df2011-09-07 23:52:2257 if (status_updater_)
[email protected]073ed7b2010-09-27 09:20:0258 status_updater_->AddDelegate(this);
initial.commit09911bf2008-07-26 23:55:2959}
60
61DownloadManager::~DownloadManager() {
[email protected]326a6a92010-09-10 20:21:1362 DCHECK(!shutdown_needed_);
[email protected]c2475df2011-09-07 23:52:2263 if (status_updater_)
[email protected]073ed7b2010-09-27 09:20:0264 status_updater_->RemoveDelegate(this);
initial.commit09911bf2008-07-26 23:55:2965}
66
67void DownloadManager::Shutdown() {
[email protected]da6e3922010-11-24 21:45:5068 VLOG(20) << __FUNCTION__ << "()"
69 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:1370 if (!shutdown_needed_)
71 return;
72 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:2973
[email protected]326a6a92010-09-10 20:21:1374 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
75
76 if (file_manager_) {
[email protected]ca4b5fa32010-10-09 12:42:1877 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]326a6a92010-09-10 20:21:1378 NewRunnableMethod(file_manager_,
79 &DownloadFileManager::OnDownloadManagerShutdown,
[email protected]dc7cdcb92010-12-14 06:40:5480 make_scoped_refptr(this)));
[email protected]326a6a92010-09-10 20:21:1381 }
initial.commit09911bf2008-07-26 23:55:2982
[email protected]f04182f32010-12-10 19:12:0783 AssertContainersConsistent();
84
85 // Go through all downloads in downloads_. Dangerous ones we need to
86 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:0987 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:0788 DownloadItem* download = *it;
89
90 // Save iterator from potential erases in this set done by called code.
91 // Iterators after an erasure point are still valid for lists and
92 // associative containers such as sets.
93 it++;
94
95 if (download->safety_state() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:2996 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:0797 // The user hasn't accepted it, so we need to remove it
98 // from the disk. This may or may not result in it being
99 // removed from the DownloadManager queues and deleted
100 // (specifically, DownloadManager::RemoveDownload only
101 // removes and deletes it if it's known to the history service)
102 // so the only thing we know after calling this function is that
103 // the download was deleted if-and-only-if it was removed
104 // from all queues.
[email protected]303077002011-04-19 23:21:01105 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26106 } else if (download->IsPartialDownload()) {
[email protected]54610672011-07-18 18:24:43107 download->Cancel(false);
[email protected]2588ea9d2011-08-22 20:59:53108 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29109 }
110 }
111
[email protected]f04182f32010-12-10 19:12:07112 // At this point, all dangerous downloads have had their files removed
113 // and all in progress downloads have been cancelled. We can now delete
114 // anything left.
[email protected]9ccbb372008-10-10 18:50:32115
[email protected]5cd11b6e2011-06-10 20:30:59116 // Copy downloads_ to separate container so as not to set off checks
117 // in DownloadItem destruction.
118 DownloadSet downloads_to_delete;
119 downloads_to_delete.swap(downloads_);
120
initial.commit09911bf2008-07-26 23:55:29121 in_progress_.clear();
[email protected]70850c72011-01-11 17:31:27122 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59123 history_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59124 STLDeleteElements(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29125
[email protected]6d0146c2011-08-04 19:13:04126 DCHECK(save_page_downloads_.empty());
127
initial.commit09911bf2008-07-26 23:55:29128 file_manager_ = NULL;
[email protected]2588ea9d2011-08-22 20:59:53129 delegate_->Shutdown();
[email protected]82f37b02010-07-29 22:04:57130
initial.commit09911bf2008-07-26 23:55:29131 shutdown_needed_ = false;
132}
133
[email protected]82f37b02010-07-29 22:04:57134void DownloadManager::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04135 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57136 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58137
[email protected]f04182f32010-12-10 19:12:07138 for (DownloadMap::iterator it = history_downloads_.begin();
139 it != history_downloads_.end(); ++it) {
[email protected]6aa4a1c02010-01-15 18:49:58140 if (it->second->is_temporary() &&
141 it->second->full_path().DirName() == dir_path)
[email protected]82f37b02010-07-29 22:04:57142 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58143 }
[email protected]6aa4a1c02010-01-15 18:49:58144}
145
[email protected]82f37b02010-07-29 22:04:57146void DownloadManager::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04147 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57148 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34149
[email protected]f04182f32010-12-10 19:12:07150 for (DownloadMap::iterator it = history_downloads_.begin();
151 it != history_downloads_.end(); ++it) {
[email protected]8ddbd66a2010-05-21 16:38:34152 if (!it->second->is_temporary() &&
153 (dir_path.empty() || it->second->full_path().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57154 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34155 }
[email protected]8ddbd66a2010-05-21 16:38:34156}
157
[email protected]d3b12902010-08-16 23:39:42158void DownloadManager::SearchDownloads(const string16& query,
[email protected]6d0146c2011-08-04 19:13:04159 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26160 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42161
[email protected]f04182f32010-12-10 19:12:07162 for (DownloadMap::iterator it = history_downloads_.begin();
163 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42164 DownloadItem* download_item = it->second;
165
[email protected]8a282712011-08-23 19:28:00166 if (download_item->is_temporary())
[email protected]d3b12902010-08-16 23:39:42167 continue;
168
169 // Display Incognito downloads only in Incognito window, and vice versa.
170 // The Incognito Downloads page will get the list of non-Incognito downloads
171 // from its parent profile.
[email protected]6d0c9fb2011-08-22 19:26:03172 if (browser_context_->IsOffTheRecord() != download_item->is_otr())
[email protected]d3b12902010-08-16 23:39:42173 continue;
174
175 if (download_item->MatchesQuery(query_lower))
176 result->push_back(download_item);
177 }
[email protected]d3b12902010-08-16 23:39:42178}
179
initial.commit09911bf2008-07-26 23:55:29180// Query the history service for information about all persisted downloads.
[email protected]6d0c9fb2011-08-22 19:26:03181bool DownloadManager::Init(content::BrowserContext* browser_context) {
182 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29183 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
184 shutdown_needed_ = true;
185
[email protected]6d0c9fb2011-08-22 19:26:03186 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46187
[email protected]2941c2392010-07-15 22:54:30188 // In test mode, there may be no ResourceDispatcherHost. In this case it's
189 // safe to avoid setting |file_manager_| because we only call a small set of
190 // functions, none of which need it.
[email protected]a0ce3282011-08-19 20:49:52191 ResourceDispatcherHost* rdh =
192 content::GetContentClient()->browser()->GetResourceDispatcherHost();
[email protected]2941c2392010-07-15 22:54:30193 if (rdh) {
194 file_manager_ = rdh->download_file_manager();
195 DCHECK(file_manager_);
initial.commit09911bf2008-07-26 23:55:29196 }
197
initial.commit09911bf2008-07-26 23:55:29198 return true;
199}
200
[email protected]aa9881c2011-08-15 18:01:12201// We have received a message from DownloadFileManager about a new download.
[email protected]4cd82f72011-05-23 19:15:01202void DownloadManager::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35204
[email protected]aa9881c2011-08-15 18:01:12205 if (delegate_->ShouldStartDownload(download_id))
206 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35207}
208
[email protected]9fc114672011-06-15 08:17:48209void DownloadManager::CheckForHistoryFilesRemoval() {
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
211 for (DownloadMap::iterator it = history_downloads_.begin();
212 it != history_downloads_.end(); ++it) {
213 CheckForFileRemoval(it->second);
214 }
215}
216
217void DownloadManager::CheckForFileRemoval(DownloadItem* download_item) {
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
219 if (download_item->IsComplete() &&
220 !download_item->file_externally_removed()) {
221 BrowserThread::PostTask(
222 BrowserThread::FILE, FROM_HERE,
223 NewRunnableMethod(this,
224 &DownloadManager::CheckForFileRemovalOnFileThread,
225 download_item->db_handle(),
226 download_item->GetTargetFilePath()));
227 }
228}
229
230void DownloadManager::CheckForFileRemovalOnFileThread(
231 int64 db_handle, const FilePath& path) {
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
233 if (!file_util::PathExists(path)) {
234 BrowserThread::PostTask(
235 BrowserThread::UI, FROM_HERE,
236 NewRunnableMethod(this,
237 &DownloadManager::OnFileRemovalDetected,
238 db_handle));
239 }
240}
241
242void DownloadManager::OnFileRemovalDetected(int64 db_handle) {
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
244 DownloadMap::iterator it = history_downloads_.find(db_handle);
245 if (it != history_downloads_.end()) {
246 DownloadItem* download_item = it->second;
247 download_item->OnDownloadedFileRemoved();
248 }
249}
250
[email protected]aa9881c2011-08-15 18:01:12251void DownloadManager::RestartDownload(
252 int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29254
[email protected]4cd82f72011-05-23 19:15:01255 DownloadItem* download = GetActiveDownloadItem(download_id);
256 if (!download)
257 return;
258
259 VLOG(20) << __FUNCTION__ << "()"
260 << " download = " << download->DebugString(true);
261
[email protected]4cd82f72011-05-23 19:15:01262 FilePath suggested_path = download->suggested_path();
263
[email protected]da1a27b2011-07-29 23:16:33264 if (download->prompt_user_for_save_location()) {
initial.commit09911bf2008-07-26 23:55:29265 // We must ask the user for the place to put the download.
[email protected]db6831a2011-06-09 21:08:28266 DownloadRequestHandle request_handle = download->request_handle();
267 TabContents* contents = request_handle.GetTabContents();
[email protected]99cb7f82011-07-28 17:27:26268
[email protected]4cd82f72011-05-23 19:15:01269 // |id_ptr| will be deleted in either FileSelected() or
270 // FileSelectionCancelled().
271 int32* id_ptr = new int32;
272 *id_ptr = download_id;
[email protected]99cb7f82011-07-28 17:27:26273
[email protected]da1a27b2011-07-29 23:16:33274 delegate_->ChooseDownloadPath(
[email protected]aa9881c2011-08-15 18:01:12275 contents, suggested_path, reinterpret_cast<void*>(id_ptr));
[email protected]99cb7f82011-07-28 17:27:26276
[email protected]f5920322011-03-24 20:34:16277 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]fed38252011-07-08 17:26:50278 SelectFileDialogDisplayed(download_id));
initial.commit09911bf2008-07-26 23:55:29279 } else {
280 // No prompting for download, just continue with the suggested name.
[email protected]4cd82f72011-05-23 19:15:01281 ContinueDownloadWithPath(download, suggested_path);
initial.commit09911bf2008-07-26 23:55:29282 }
283}
284
[email protected]c2e76012010-12-23 21:10:29285void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) {
286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
287
288 DownloadItem* download = new DownloadItem(this, *info,
[email protected]6d0c9fb2011-08-22 19:26:03289 browser_context_->IsOffTheRecord());
[email protected]4cd82f72011-05-23 19:15:01290 int32 download_id = info->download_id;
291 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]d8472d92011-08-26 20:15:20292
293 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]821960a2011-08-23 20:40:03294 CHECK(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29295 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01296 active_downloads_[download_id] = download;
[email protected]c2e76012010-12-23 21:10:29297}
298
[email protected]4cd82f72011-05-23 19:15:01299void DownloadManager::ContinueDownloadWithPath(DownloadItem* download,
300 const FilePath& chosen_file) {
[email protected]ca4b5fa32010-10-09 12:42:18301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01302 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39303
[email protected]4cd82f72011-05-23 19:15:01304 int32 download_id = download->id();
initial.commit09911bf2008-07-26 23:55:29305
[email protected]70850c72011-01-11 17:31:27306 // NOTE(ahendrickson) Eventually |active_downloads_| will replace
307 // |in_progress_|, but we don't want to change the semantics yet.
[email protected]4cd82f72011-05-23 19:15:01308 DCHECK(!ContainsKey(in_progress_, download_id));
[email protected]70850c72011-01-11 17:31:27309 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01310 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27311
[email protected]4cd82f72011-05-23 19:15:01312 // Make sure the initial file name is set only once.
313 DCHECK(download->full_path().empty());
314 download->OnPathDetermined(chosen_file);
[email protected]4cd82f72011-05-23 19:15:01315
316 VLOG(20) << __FUNCTION__ << "()"
317 << " download = " << download->DebugString(true);
318
319 in_progress_[download_id] = download;
[email protected]5f8589fe2011-08-17 20:58:39320 UpdateDownloadProgress(); // Reflect entry into in_progress_.
initial.commit09911bf2008-07-26 23:55:29321
[email protected]adb2f3d12011-01-23 16:24:54322 // Rename to intermediate name.
[email protected]f5920322011-03-24 20:34:16323 FilePath download_path;
[email protected]ec865262011-08-23 20:01:48324 if (!delegate_->OverrideIntermediatePath(download, &download_path))
[email protected]4cd82f72011-05-23 19:15:01325 download_path = download->full_path();
[email protected]594cd7d2010-07-21 03:23:56326
[email protected]f5920322011-03-24 20:34:16327 BrowserThread::PostTask(
328 BrowserThread::FILE, FROM_HERE,
329 NewRunnableMethod(
330 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile,
[email protected]c2475df2011-09-07 23:52:22331 download->id(), download_path));
[email protected]f5920322011-03-24 20:34:16332
333 download->Rename(download_path);
334
[email protected]2588ea9d2011-08-22 20:59:53335 delegate_->AddItemToPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29336}
337
initial.commit09911bf2008-07-26 23:55:29338void DownloadManager::UpdateDownload(int32 download_id, int64 size) {
[email protected]70850c72011-01-11 17:31:27339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
340 DownloadMap::iterator it = active_downloads_.find(download_id);
341 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29342 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26343 if (download->IsInProgress()) {
[email protected]70850c72011-01-11 17:31:27344 download->Update(size);
[email protected]5f8589fe2011-08-17 20:58:39345 UpdateDownloadProgress(); // Reflect size updates.
[email protected]2588ea9d2011-08-22 20:59:53346 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27347 }
initial.commit09911bf2008-07-26 23:55:29348 }
349}
350
[email protected]bf68a00b2011-04-07 17:28:26351void DownloadManager::OnResponseCompleted(int32 download_id,
352 int64 size,
[email protected]bf68a00b2011-04-07 17:28:26353 const std::string& hash) {
[email protected]33c6d3f12011-09-04 00:00:54354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]da6e3922010-11-24 21:45:50355 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
356 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35357 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32358
[email protected]c4f02c42011-01-24 21:55:06359 // If it's not in active_downloads_, that means it was cancelled; just
360 // ignore the notification.
361 if (active_downloads_.count(download_id) == 0)
362 return;
363
[email protected]adb2f3d12011-01-23 16:24:54364 DownloadItem* download = active_downloads_[download_id];
[email protected]a850ba42010-09-10 22:00:30365 download->OnAllDataSaved(size);
[email protected]9ccbb372008-10-10 18:50:32366
[email protected]b09f1282011-09-14 00:37:45367 delegate_->OnResponseCompleted(download, hash);
368
[email protected]adb2f3d12011-01-23 16:24:54369 MaybeCompleteDownload(download);
370}
[email protected]9ccbb372008-10-10 18:50:32371
[email protected]7d413112011-06-16 18:50:17372void DownloadManager::AssertQueueStateConsistent(DownloadItem* download) {
[email protected]5cd11b6e2011-06-10 20:30:59373 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
[email protected]7d413112011-06-16 18:50:17374 if (download->state() == DownloadItem::REMOVING) {
375 CHECK(!ContainsKey(downloads_, download));
376 CHECK(!ContainsKey(active_downloads_, download->id()));
377 CHECK(!ContainsKey(in_progress_, download->id()));
378 CHECK(!ContainsKey(history_downloads_, download->db_handle()));
379 return;
380 }
381
382 // Should be in downloads_ if we're not REMOVING.
383 CHECK(ContainsKey(downloads_, download));
384
385 // Check history_downloads_ consistency.
[email protected]2588ea9d2011-08-22 20:59:53386 if (download->db_handle() != DownloadItem::kUninitializedHandle) {
[email protected]7d413112011-06-16 18:50:17387 CHECK(ContainsKey(history_downloads_, download->db_handle()));
388 } else {
389 // TODO(rdsmith): Somewhat painful; make sure to disable in
390 // release builds after resolution of http://crbug.com/85408.
391 for (DownloadMap::iterator it = history_downloads_.begin();
392 it != history_downloads_.end(); ++it) {
393 CHECK(it->second != download);
394 }
395 }
396
[email protected]61b75a52011-08-29 16:34:34397 int64 state = download->state();
398 base::debug::Alias(&state);
[email protected]821960a2011-08-23 20:40:03399 if (ContainsKey(active_downloads_, download->id()))
400 CHECK_EQ(DownloadItem::IN_PROGRESS, download->state());
401 if (DownloadItem::IN_PROGRESS == download->state())
402 CHECK(ContainsKey(active_downloads_, download->id()));
[email protected]5cd11b6e2011-06-10 20:30:59403}
404
[email protected]adb2f3d12011-01-23 16:24:54405bool DownloadManager::IsDownloadReadyForCompletion(DownloadItem* download) {
406 // If we don't have all the data, the download is not ready for
407 // completion.
408 if (!download->all_data_saved())
409 return false;
[email protected]6a7fb042010-02-01 16:30:47410
[email protected]9d7ef802011-02-25 19:03:35411 // If the download is dangerous, but not yet validated, it's not ready for
412 // completion.
413 if (download->safety_state() == DownloadItem::DANGEROUS)
414 return false;
415
[email protected]adb2f3d12011-01-23 16:24:54416 // If the download isn't active (e.g. has been cancelled) it's not
417 // ready for completion.
418 if (active_downloads_.count(download->id()) == 0)
419 return false;
420
421 // If the download hasn't been inserted into the history system
422 // (which occurs strictly after file name determination, intermediate
423 // file rename, and UI display) then it's not ready for completion.
[email protected]2588ea9d2011-08-22 20:59:53424 if (download->db_handle() == DownloadItem::kUninitializedHandle)
[email protected]7054b592011-06-22 14:46:42425 return false;
426
427 return true;
[email protected]adb2f3d12011-01-23 16:24:54428}
429
430void DownloadManager::MaybeCompleteDownload(DownloadItem* download) {
431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
432 VLOG(20) << __FUNCTION__ << "()" << " download = "
433 << download->DebugString(false);
434
435 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32436 return;
[email protected]9ccbb372008-10-10 18:50:32437
[email protected]adb2f3d12011-01-23 16:24:54438 // TODO(rdsmith): DCHECK that we only pass through this point
439 // once per download. The natural way to do this is by a state
440 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56441
[email protected]adb2f3d12011-01-23 16:24:54442 // Confirm we're in the proper set of states to be here;
[email protected]9d7ef802011-02-25 19:03:35443 // in in_progress_, have all data, have a history handle, (validated or safe).
444 DCHECK_NE(DownloadItem::DANGEROUS, download->safety_state());
[email protected]adb2f3d12011-01-23 16:24:54445 DCHECK_EQ(1u, in_progress_.count(download->id()));
446 DCHECK(download->all_data_saved());
[email protected]2588ea9d2011-08-22 20:59:53447 DCHECK(download->db_handle() != DownloadItem::kUninitializedHandle);
[email protected]adb2f3d12011-01-23 16:24:54448 DCHECK_EQ(1u, history_downloads_.count(download->db_handle()));
449
450 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
451 << download->DebugString(false);
452
453 // Remove the id from in_progress
454 in_progress_.erase(download->id());
[email protected]5f8589fe2011-08-17 20:58:39455 UpdateDownloadProgress(); // Reflect removal from in_progress_.
[email protected]adb2f3d12011-01-23 16:24:54456
[email protected]2588ea9d2011-08-22 20:59:53457 delegate_->UpdateItemInPersistentStore(download);
[email protected]adb2f3d12011-01-23 16:24:54458
[email protected]f5920322011-03-24 20:34:16459 // Finish the download.
[email protected]48837962011-04-19 17:03:29460 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32461}
462
[email protected]cc3c7c092011-05-09 18:40:21463void DownloadManager::DownloadCompleted(int32 download_id) {
[email protected]70850c72011-01-11 17:31:27464 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21465 DownloadItem* download = GetDownloadItem(download_id);
466 DCHECK(download);
[email protected]2588ea9d2011-08-22 20:59:53467 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27468 active_downloads_.erase(download_id);
[email protected]821960a2011-08-23 20:40:03469 AssertQueueStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27470}
471
[email protected]f5920322011-03-24 20:34:16472void DownloadManager::OnDownloadRenamedToFinalName(int download_id,
473 const FilePath& full_path,
474 int uniquifier) {
[email protected]da6e3922010-11-24 21:45:50475 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
[email protected]f5920322011-03-24 20:34:16476 << " full_path = \"" << full_path.value() << "\""
477 << " uniquifier = " << uniquifier;
[email protected]ca4b5fa32010-10-09 12:42:18478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]f5920322011-03-24 20:34:16479
[email protected]2e030682010-07-23 19:45:36480 DownloadItem* item = GetDownloadItem(download_id);
481 if (!item)
482 return;
[email protected]6cade212008-12-03 00:32:22483
[email protected]8fa1eeb52011-04-13 14:18:02484 if (item->safety_state() == DownloadItem::SAFE) {
485 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice";
486 }
487
[email protected]c2475df2011-09-07 23:52:22488 BrowserThread::PostTask(
489 BrowserThread::FILE, FROM_HERE,
490 NewRunnableMethod(
491 file_manager_, &DownloadFileManager::CompleteDownload, download_id));
[email protected]9ccbb372008-10-10 18:50:32492
[email protected]f5920322011-03-24 20:34:16493 if (uniquifier)
494 item->set_path_uniquifier(uniquifier);
[email protected]9ccbb372008-10-10 18:50:32495
[email protected]f5920322011-03-24 20:34:16496 item->OnDownloadRenamedToFinalName(full_path);
[email protected]2588ea9d2011-08-22 20:59:53497 delegate_->UpdatePathForItemInPersistentStore(item, full_path);
initial.commit09911bf2008-07-26 23:55:29498}
499
[email protected]d8472d92011-08-26 20:15:20500void DownloadManager::CancelDownload(int32 download_id) {
[email protected]47a881b2011-08-29 22:59:21501 DownloadItem* download = GetActiveDownload(download_id);
502 // A cancel at the right time could remove the download from the
503 // |active_downloads_| map before we get here.
[email protected]d8472d92011-08-26 20:15:20504 if (!download)
initial.commit09911bf2008-07-26 23:55:29505 return;
initial.commit09911bf2008-07-26 23:55:29506
[email protected]d8472d92011-08-26 20:15:20507 download->Cancel(true);
508}
509
510void DownloadManager::DownloadCancelledInternal(DownloadItem* download) {
511 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20512
513 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50514 << " download = " << download->DebugString(true);
515
[email protected]47a881b2011-08-29 22:59:21516 RemoveFromActiveList(download);
517 // This function is called from the DownloadItem, so DI state
518 // should already have been updated.
519 AssertQueueStateConsistent(download);
initial.commit09911bf2008-07-26 23:55:29520
[email protected]d8472d92011-08-26 20:15:20521 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29522}
523
[email protected]bf68a00b2011-04-07 17:28:26524void DownloadManager::OnDownloadError(int32 download_id,
525 int64 size,
[email protected]33c6d3f12011-09-04 00:00:54526 net::Error error) {
[email protected]47a881b2011-08-29 22:59:21527 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
528
529 DownloadItem* download = GetActiveDownload(download_id);
530 if (!download)
531 return;
532
533 VLOG(20) << __FUNCTION__ << "()" << " Error " << error
534 << " at offset " << download->received_bytes()
535 << " size = " << size
536 << " download = " << download->DebugString(true);
537
538 RemoveFromActiveList(download);
539 download->Interrupted(size, error);
540 download->OffThreadCancel(file_manager_);
541}
542
543DownloadItem* DownloadManager::GetActiveDownload(int32 download_id) {
[email protected]bf68a00b2011-04-07 17:28:26544 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
545 DownloadMap::iterator it = active_downloads_.find(download_id);
[email protected]bf68a00b2011-04-07 17:28:26546 if (it == active_downloads_.end())
[email protected]47a881b2011-08-29 22:59:21547 return NULL;
[email protected]bf68a00b2011-04-07 17:28:26548
549 DownloadItem* download = it->second;
550
[email protected]47a881b2011-08-29 22:59:21551 DCHECK(download);
552 DCHECK_EQ(download_id, download->id());
[email protected]4cd82f72011-05-23 19:15:01553
[email protected]47a881b2011-08-29 22:59:21554 return download;
555}
[email protected]54610672011-07-18 18:24:43556
[email protected]47a881b2011-08-29 22:59:21557void DownloadManager::RemoveFromActiveList(DownloadItem* download) {
558 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
559 DCHECK(download);
560
[email protected]54610672011-07-18 18:24:43561 // Clean up will happen when the history system create callback runs if we
562 // don't have a valid db_handle yet.
[email protected]2588ea9d2011-08-22 20:59:53563 if (download->db_handle() != DownloadItem::kUninitializedHandle) {
[email protected]47a881b2011-08-29 22:59:21564 in_progress_.erase(download->id());
565 active_downloads_.erase(download->id());
[email protected]5f8589fe2011-08-17 20:58:39566 UpdateDownloadProgress(); // Reflect removal from in_progress_.
[email protected]2588ea9d2011-08-22 20:59:53567 delegate_->UpdateItemInPersistentStore(download);
[email protected]54610672011-07-18 18:24:43568 }
[email protected]bf68a00b2011-04-07 17:28:26569}
570
[email protected]5f8589fe2011-08-17 20:58:39571void DownloadManager::UpdateDownloadProgress() {
572 delegate_->DownloadProgressUpdated();
[email protected]6a7fb042010-02-01 16:30:47573}
574
[email protected]6d0146c2011-08-04 19:13:04575int DownloadManager::RemoveDownloadItems(
576 const DownloadVector& pending_deletes) {
577 if (pending_deletes.empty())
578 return 0;
579
580 // Delete from internal maps.
581 for (DownloadVector::const_iterator it = pending_deletes.begin();
582 it != pending_deletes.end();
583 ++it) {
584 DownloadItem* download = *it;
585 DCHECK(download);
586 history_downloads_.erase(download->db_handle());
587 save_page_downloads_.erase(download->id());
588 downloads_.erase(download);
589 }
590
591 // Tell observers to refresh their views.
592 NotifyModelChanged();
593
594 // Delete the download items themselves.
595 const int num_deleted = static_cast<int>(pending_deletes.size());
596 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
597 return num_deleted;
598}
599
[email protected]54610672011-07-18 18:24:43600void DownloadManager::RemoveDownload(int64 download_handle) {
601 DownloadMap::iterator it = history_downloads_.find(download_handle);
602 if (it == history_downloads_.end())
603 return;
604
605 // Make history update.
606 DownloadItem* download = it->second;
[email protected]2588ea9d2011-08-22 20:59:53607 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29608
609 // Remove from our tables and delete.
[email protected]6d0146c2011-08-04 19:13:04610 int downloads_count = RemoveDownloadItems(DownloadVector(1, download));
[email protected]f04182f32010-12-10 19:12:07611 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29612}
613
[email protected]e93d2822009-01-30 05:59:59614int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin,
615 const base::Time remove_end) {
[email protected]2588ea9d2011-08-22 20:59:53616 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29617
[email protected]a312a442010-12-15 23:40:33618 // All downloads visible to the user will be in the history,
619 // so scan that map.
[email protected]6d0146c2011-08-04 19:13:04620 DownloadVector pending_deletes;
621 for (DownloadMap::const_iterator it = history_downloads_.begin();
622 it != history_downloads_.end();
623 ++it) {
initial.commit09911bf2008-07-26 23:55:29624 DownloadItem* download = it->second;
initial.commit09911bf2008-07-26 23:55:29625 if (download->start_time() >= remove_begin &&
626 (remove_end.is_null() || download->start_time() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04627 (download->IsComplete() || download->IsCancelled())) {
[email protected]7d413112011-06-16 18:50:17628 AssertQueueStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28629 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29630 }
initial.commit09911bf2008-07-26 23:55:29631 }
[email protected]6d0146c2011-08-04 19:13:04632 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29633}
634
[email protected]e93d2822009-01-30 05:59:59635int DownloadManager::RemoveDownloads(const base::Time remove_begin) {
636 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29637}
638
[email protected]d41355e6f2009-04-07 21:21:12639int DownloadManager::RemoveAllDownloads() {
640 // The null times make the date range unbounded.
641 return RemoveDownloadsBetween(base::Time(), base::Time());
642}
643
initial.commit09911bf2008-07-26 23:55:29644// Initiate a download of a specific URL. We send the request to the
645// ResourceDispatcherHost, and let it send us responses like a regular
646// download.
647void DownloadManager::DownloadUrl(const GURL& url,
648 const GURL& referrer,
[email protected]c9825a42009-05-01 22:51:50649 const std::string& referrer_charset,
[email protected]57c6a652009-05-04 07:58:34650 TabContents* tab_contents) {
[email protected]ae8945192010-07-20 16:56:26651 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
652 tab_contents);
[email protected]6aa4a1c02010-01-15 18:49:58653}
654
655void DownloadManager::DownloadUrlToFile(const GURL& url,
656 const GURL& referrer,
657 const std::string& referrer_charset,
[email protected]8af9d032010-02-10 00:00:32658 const DownloadSaveInfo& save_info,
[email protected]6aa4a1c02010-01-15 18:49:58659 TabContents* tab_contents) {
[email protected]57c6a652009-05-04 07:58:34660 DCHECK(tab_contents);
[email protected]c79a0c02011-08-22 22:37:37661 ResourceDispatcherHost* resource_dispatcher_host =
662 content::GetContentClient()->browser()->GetResourceDispatcherHost();
[email protected]ed24fad2011-05-10 22:44:01663 // We send a pointer to content::ResourceContext, instead of the usual
664 // reference, so that a copy of the object isn't made.
[email protected]ca4b5fa32010-10-09 12:42:18665 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]a0ce3282011-08-19 20:49:52666 NewRunnableFunction(&BeginDownload,
[email protected]ae8945192010-07-20 16:56:26667 url,
668 referrer,
[email protected]ae8945192010-07-20 16:56:26669 save_info,
[email protected]c79a0c02011-08-22 22:37:37670 resource_dispatcher_host,
[email protected]ae8945192010-07-20 16:56:26671 tab_contents->GetRenderProcessHost()->id(),
672 tab_contents->render_view_host()->routing_id(),
[email protected]cafe4ad2011-07-28 18:34:56673 &tab_contents->browser_context()->
674 GetResourceContext()));
initial.commit09911bf2008-07-26 23:55:29675}
676
initial.commit09911bf2008-07-26 23:55:29677void DownloadManager::AddObserver(Observer* observer) {
678 observers_.AddObserver(observer);
679 observer->ModelChanged();
680}
681
682void DownloadManager::RemoveObserver(Observer* observer) {
683 observers_.RemoveObserver(observer);
684}
685
[email protected]073ed7b2010-09-27 09:20:02686bool DownloadManager::IsDownloadProgressKnown() {
687 for (DownloadMap::iterator i = in_progress_.begin();
688 i != in_progress_.end(); ++i) {
689 if (i->second->total_bytes() <= 0)
690 return false;
691 }
692
693 return true;
694}
695
696int64 DownloadManager::GetInProgressDownloadCount() {
697 return in_progress_.size();
698}
699
700int64 DownloadManager::GetReceivedDownloadBytes() {
701 DCHECK(IsDownloadProgressKnown());
702 int64 received_bytes = 0;
703 for (DownloadMap::iterator i = in_progress_.begin();
704 i != in_progress_.end(); ++i) {
705 received_bytes += i->second->received_bytes();
706 }
707 return received_bytes;
708}
709
710int64 DownloadManager::GetTotalDownloadBytes() {
711 DCHECK(IsDownloadProgressKnown());
712 int64 total_bytes = 0;
713 for (DownloadMap::iterator i = in_progress_.begin();
714 i != in_progress_.end(); ++i) {
715 total_bytes += i->second->total_bytes();
716 }
717 return total_bytes;
718}
719
[email protected]99cb7f82011-07-28 17:27:26720void DownloadManager::FileSelected(const FilePath& path, void* params) {
[email protected]4cd82f72011-05-23 19:15:01721 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
722
723 int32* id_ptr = reinterpret_cast<int32*>(params);
724 DCHECK(id_ptr != NULL);
725 int32 download_id = *id_ptr;
726 delete id_ptr;
727
728 DownloadItem* download = GetActiveDownloadItem(download_id);
729 if (!download)
730 return;
731 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
732 << " download = " << download->DebugString(true);
733
[email protected]da1a27b2011-07-29 23:16:33734 if (download->prompt_user_for_save_location())
[email protected]7ae7c2cb2009-01-06 23:31:41735 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35736
[email protected]4cd82f72011-05-23 19:15:01737 // Make sure the initial file name is set only once.
738 ContinueDownloadWithPath(download, path);
initial.commit09911bf2008-07-26 23:55:29739}
740
741void DownloadManager::FileSelectionCanceled(void* params) {
742 // The user didn't pick a place to save the file, so need to cancel the
743 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01744 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
745 int32* id_ptr = reinterpret_cast<int32*>(params);
746 DCHECK(id_ptr != NULL);
747 int32 download_id = *id_ptr;
748 delete id_ptr;
749
750 DownloadItem* download = GetActiveDownloadItem(download_id);
751 if (!download)
752 return;
753
754 VLOG(20) << __FUNCTION__ << "()"
755 << " download = " << download->DebugString(true);
756
[email protected]47a881b2011-08-29 22:59:21757 // TODO(ahendrickson) -- This currently has no effect, as the download is
758 // not put on the active list until the file selection is complete. Need
759 // to put it on the active list earlier in the process.
760 RemoveFromActiveList(download);
761
[email protected]d8472d92011-08-26 20:15:20762 download->OffThreadCancel(file_manager_);
[email protected]4cd82f72011-05-23 19:15:01763}
764
initial.commit09911bf2008-07-26 23:55:29765// Operations posted to us from the history service ----------------------------
766
767// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25768// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]2588ea9d2011-08-22 20:59:53769void DownloadManager::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25770 std::vector<DownloadPersistentStoreInfo>* entries) {
[email protected]d8472d92011-08-26 20:15:20771 // TODO(rdsmith): Remove this and related logic when
772 // http://crbug.com/84508 is fixed.
773 largest_db_handle_in_history_ = 0;
774
initial.commit09911bf2008-07-26 23:55:29775 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]aa033af2010-07-27 18:16:39776 DownloadItem* download = new DownloadItem(this, entries->at(i));
[email protected]d8472d92011-08-26 20:15:20777 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]821960a2011-08-23 20:40:03778 CHECK(!ContainsKey(history_downloads_, download->db_handle()));
[email protected]f04182f32010-12-10 19:12:07779 downloads_.insert(download);
780 history_downloads_[download->db_handle()] = download;
[email protected]da6e3922010-11-24 21:45:50781 VLOG(20) << __FUNCTION__ << "()" << i << ">"
782 << " download = " << download->DebugString(true);
[email protected]d8472d92011-08-26 20:15:20783
784 if (download->db_handle() > largest_db_handle_in_history_)
785 largest_db_handle_in_history_ = download->db_handle();
initial.commit09911bf2008-07-26 23:55:29786 }
[email protected]b0ab1d42010-02-24 19:29:28787 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48788 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29789}
790
[email protected]f9a45b02011-06-30 23:49:19791void DownloadManager::AddDownloadItemToHistory(DownloadItem* download,
792 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27793 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d2a8fb72010-01-21 05:31:42794
[email protected]1e9fe7ff2011-06-24 17:37:33795 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508
796 // is fixed.
[email protected]2588ea9d2011-08-22 20:59:53797 CHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]1e9fe7ff2011-06-24 17:37:33798
[email protected]2588ea9d2011-08-22 20:59:53799 DCHECK(download->db_handle() == DownloadItem::kUninitializedHandle);
[email protected]5bcd73eb2011-03-23 21:14:02800 download->set_db_handle(db_handle);
801
[email protected]d8472d92011-08-26 20:15:20802 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508
803 // is fixed.
[email protected]821960a2011-08-23 20:40:03804 CHECK(!ContainsKey(history_downloads_, download->db_handle()));
[email protected]5bcd73eb2011-03-23 21:14:02805 history_downloads_[download->db_handle()] = download;
[email protected]6d0146c2011-08-04 19:13:04806
807 // Show in the appropriate browser UI.
808 // This includes buttons to save or cancel, for a dangerous download.
809 ShowDownloadInBrowser(download);
810
811 // Inform interested objects about the new download.
812 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19813}
814
[email protected]2588ea9d2011-08-22 20:59:53815
816void DownloadManager::OnItemAddedToPersistentStore(int32 download_id,
817 int64 db_handle) {
818 if (save_page_downloads_.count(download_id)) {
819 OnSavePageItemAddedToPersistentStore(download_id, db_handle);
820 } else if (active_downloads_.count(download_id)) {
821 OnDownloadItemAddedToPersistentStore(download_id, db_handle);
822 }
823 // It's valid that we don't find a matching item, i.e. on shutdown.
824}
825
[email protected]f9a45b02011-06-30 23:49:19826// Once the new DownloadItem's creation info has been committed to the history
827// service, we associate the DownloadItem with the db handle, update our
828// 'history_downloads_' map and inform observers.
[email protected]2588ea9d2011-08-22 20:59:53829void DownloadManager::OnDownloadItemAddedToPersistentStore(int32 download_id,
830 int64 db_handle) {
[email protected]f9a45b02011-06-30 23:49:19831 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]19420cc2011-07-18 17:43:45832 DownloadItem* download = GetActiveDownloadItem(download_id);
[email protected]54610672011-07-18 18:24:43833 if (!download)
[email protected]19420cc2011-07-18 17:43:45834 return;
[email protected]54610672011-07-18 18:24:43835
836 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
837 << " download_id = " << download_id
838 << " download = " << download->DebugString(true);
[email protected]f9a45b02011-06-30 23:49:19839
[email protected]d8472d92011-08-26 20:15:20840 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]d8472d92011-08-26 20:15:20841 int64 largest_handle = largest_db_handle_in_history_;
842 base::debug::Alias(&largest_handle);
[email protected]61b75a52011-08-29 16:34:34843 CHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:20844
[email protected]f9a45b02011-06-30 23:49:19845 AddDownloadItemToHistory(download, db_handle);
initial.commit09911bf2008-07-26 23:55:29846
[email protected]54610672011-07-18 18:24:43847 // If the download is still in progress, try to complete it.
848 //
849 // Otherwise, download has been cancelled or interrupted before we've
850 // received the DB handle. We post one final message to the history
851 // service so that it can be properly in sync with the DownloadItem's
852 // completion status, and also inform any observers so that they get
853 // more than just the start notification.
854 if (download->IsInProgress()) {
855 MaybeCompleteDownload(download);
856 } else {
[email protected]d8472d92011-08-26 20:15:20857 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508
858 // is fixed.
[email protected]821960a2011-08-23 20:40:03859 CHECK(download->IsCancelled())
[email protected]54610672011-07-18 18:24:43860 << " download = " << download->DebugString(true);
861 in_progress_.erase(download_id);
862 active_downloads_.erase(download_id);
[email protected]2588ea9d2011-08-22 20:59:53863 delegate_->UpdateItemInPersistentStore(download);
[email protected]54610672011-07-18 18:24:43864 download->UpdateObservers();
865 }
initial.commit09911bf2008-07-26 23:55:29866}
867
[email protected]4cd82f72011-05-23 19:15:01868void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]8ddbd66a2010-05-21 16:38:34869 // The 'contents' may no longer exist if the user closed the tab before we
[email protected]99cb7f82011-07-28 17:27:26870 // get this start completion event.
[email protected]db6831a2011-06-09 21:08:28871 DownloadRequestHandle request_handle = download->request_handle();
[email protected]686493142011-07-15 21:47:22872 TabContents* content = request_handle.GetTabContents();
[email protected]99cb7f82011-07-28 17:27:26873
874 // If the contents no longer exists, we ask the embedder to suggest another
875 // tab.
[email protected]da1a27b2011-07-29 23:16:33876 if (!content)
[email protected]aa9881c2011-08-15 18:01:12877 content = delegate_->GetAlternativeTabContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:53878
[email protected]99cb7f82011-07-28 17:27:26879 if (content)
880 content->OnStartDownload(download);
[email protected]5e595482009-05-06 20:16:53881}
882
[email protected]6cade212008-12-03 00:32:22883// Clears the last download path, used to initialize "save as" dialogs.
[email protected]905a08d2008-11-19 07:24:12884void DownloadManager::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:41885 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:38886}
[email protected]b0ab1d42010-02-24 19:29:28887
888void DownloadManager::NotifyModelChanged() {
889 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged());
890}
891
[email protected]4cd82f72011-05-23 19:15:01892DownloadItem* DownloadManager::GetDownloadItem(int download_id) {
893 // The |history_downloads_| map is indexed by the download's db_handle,
894 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:07895 for (DownloadMap::iterator it = history_downloads_.begin();
896 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:36897 DownloadItem* item = it->second;
[email protected]4cd82f72011-05-23 19:15:01898 if (item->id() == download_id)
[email protected]2e030682010-07-23 19:45:36899 return item;
900 }
901 return NULL;
902}
903
[email protected]4cd82f72011-05-23 19:15:01904DownloadItem* DownloadManager::GetActiveDownloadItem(int download_id) {
905 DCHECK(ContainsKey(active_downloads_, download_id));
906 DownloadItem* download = active_downloads_[download_id];
907 DCHECK(download != NULL);
908 return download;
909}
910
[email protected]57fd1252010-12-23 17:24:09911// Confirm that everything in all maps is also in |downloads_|, and that
912// everything in |downloads_| is also in some other map.
[email protected]f04182f32010-12-10 19:12:07913void DownloadManager::AssertContainersConsistent() const {
914#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:09915 // Turn everything into sets.
[email protected]6d0146c2011-08-04 19:13:04916 const DownloadMap* input_maps[] = {&active_downloads_,
917 &history_downloads_,
918 &save_page_downloads_};
919 DownloadSet active_set, history_set, save_page_set;
920 DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set};
921 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:09922 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
923 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:04924 it != input_maps[i]->end(); ++it) {
925 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:07926 }
927 }
[email protected]57fd1252010-12-23 17:24:09928
929 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:09930 DownloadSet downloads_union;
931 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
932 DownloadSet remainder;
933 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
934 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
935 downloads_.begin(), downloads_.end(),
936 insert_it);
937 DCHECK(remainder.empty());
938 std::insert_iterator<DownloadSet>
939 insert_union(downloads_union, downloads_union.end());
940 std::set_union(downloads_union.begin(), downloads_union.end(),
941 all_sets[i]->begin(), all_sets[i]->end(),
942 insert_union);
943 }
944
945 // Is everything in downloads_ present in one of the other sets?
946 DownloadSet remainder;
947 std::insert_iterator<DownloadSet>
948 insert_remainder(remainder, remainder.begin());
949 std::set_difference(downloads_.begin(), downloads_.end(),
950 downloads_union.begin(), downloads_union.end(),
951 insert_remainder);
952 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:07953#endif
954}
955
[email protected]6d0146c2011-08-04 19:13:04956void DownloadManager::SavePageDownloadStarted(DownloadItem* download) {
957 DCHECK(!ContainsKey(save_page_downloads_, download->id()));
958 downloads_.insert(download);
959 save_page_downloads_[download->id()] = download;
960
961 // Add this entry to the history service.
962 // Additionally, the UI is notified in the callback.
[email protected]2588ea9d2011-08-22 20:59:53963 delegate_->AddItemToPersistentStore(download);
[email protected]6d0146c2011-08-04 19:13:04964}
965
966// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:53967// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:04968// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:53969// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
970// that the history event is update regardless of the order in which these two
971// events complete.
[email protected]6d0146c2011-08-04 19:13:04972// If something removes the download item from the download manager (Remove,
973// Shutdown) the result will be that the SavePage system will not be able to
974// properly update the download item (which no longer exists) or the download
975// history, but the action will complete properly anyway. This may lead to the
976// history entry being wrong on a reload of chrome (specifically in the case of
977// Initiation -> History Callback -> Removal -> Completion), but there's no way
978// to solve that without canceling on Remove (which would then update the DB).
979
[email protected]2588ea9d2011-08-22 20:59:53980void DownloadManager::OnSavePageItemAddedToPersistentStore(int32 download_id,
981 int64 db_handle) {
[email protected]6d0146c2011-08-04 19:13:04982 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
983
984 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
985 // This can happen if the download manager is shutting down and all maps
986 // have been cleared.
987 if (it == save_page_downloads_.end())
988 return;
989
990 DownloadItem* download = it->second;
991 if (!download) {
992 NOTREACHED();
993 return;
994 }
995
[email protected]d8472d92011-08-26 20:15:20996 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
[email protected]d8472d92011-08-26 20:15:20997 int64 largest_handle = largest_db_handle_in_history_;
998 base::debug::Alias(&largest_handle);
[email protected]61b75a52011-08-29 16:34:34999 CHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:201000
[email protected]6d0146c2011-08-04 19:13:041001 AddDownloadItemToHistory(download, db_handle);
1002
1003 // Finalize this download if it finished before the history callback.
1004 if (!download->IsInProgress())
1005 SavePageDownloadFinished(download);
1006}
1007
1008void DownloadManager::SavePageDownloadFinished(DownloadItem* download) {
[email protected]2588ea9d2011-08-22 20:59:531009 if (download->db_handle() != DownloadItem::kUninitializedHandle) {
1010 delegate_->UpdateItemInPersistentStore(download);
[email protected]6d0146c2011-08-04 19:13:041011 DCHECK(ContainsKey(save_page_downloads_, download->id()));
1012 save_page_downloads_.erase(download->id());
1013
1014 if (download->IsComplete())
1015 NotificationService::current()->Notify(
1016 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
1017 Source<DownloadManager>(this),
1018 Details<DownloadItem>(download));
1019 }
1020}