Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/download/download_manager.h" | 5 #include "content/browser/download/download_manager.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 9 #include "base/callback.h" | 10 #include "base/callback.h" |
| 10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 11 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/stringprintf.h" | |
| 16 #include "base/synchronization/lock.h" | |
| 17 #include "base/sys_string_conversions.h" | |
| 14 #include "base/task.h" | 18 #include "base/task.h" |
| 15 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 16 #include "content/browser/browser_context.h" | 20 #include "content/browser/browser_context.h" |
| 17 #include "content/browser/browser_thread.h" | 21 #include "content/browser/browser_thread.h" |
| 18 #include "content/browser/content_browser_client.h" | 22 #include "content/browser/content_browser_client.h" |
| 19 #include "content/browser/download/download_create_info.h" | 23 #include "content/browser/download/download_create_info.h" |
| 20 #include "content/browser/download/download_file_manager.h" | 24 #include "content/browser/download/download_file_manager.h" |
| 21 #include "content/browser/download/download_item.h" | 25 #include "content/browser/download/download_item.h" |
| 22 #include "content/browser/download/download_manager_delegate.h" | 26 #include "content/browser/download/download_manager_delegate.h" |
| 23 #include "content/browser/download/download_persistent_store_info.h" | 27 #include "content/browser/download/download_persistent_store_info.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 43 url, referrer, save_info, true, render_process_id, render_view_id, | 47 url, referrer, save_info, true, render_process_id, render_view_id, |
| 44 *context); | 48 *context); |
| 45 } | 49 } |
| 46 | 50 |
| 47 } // namespace | 51 } // namespace |
| 48 | 52 |
| 49 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, | 53 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, |
| 50 DownloadStatusUpdater* status_updater) | 54 DownloadStatusUpdater* status_updater) |
| 51 : shutdown_needed_(false), | 55 : shutdown_needed_(false), |
| 52 browser_context_(NULL), | 56 browser_context_(NULL), |
| 57 next_id_(0), | |
| 53 file_manager_(NULL), | 58 file_manager_(NULL), |
| 54 status_updater_(status_updater->AsWeakPtr()), | 59 status_updater_((status_updater != NULL) |
| 60 ? status_updater->AsWeakPtr() | |
| 61 : base::WeakPtr<DownloadStatusUpdater>()), | |
| 55 delegate_(delegate), | 62 delegate_(delegate), |
| 56 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { | 63 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { |
| 57 if (status_updater_) | 64 // NOTE(benjhayden): status_updater may be NULL when using |
| 65 // TestingBrowserProcess. | |
| 66 if (status_updater_.get() != NULL) | |
| 58 status_updater_->AddDelegate(this); | 67 status_updater_->AddDelegate(this); |
| 59 } | 68 } |
| 60 | 69 |
| 61 DownloadManager::~DownloadManager() { | 70 DownloadManager::~DownloadManager() { |
| 62 DCHECK(!shutdown_needed_); | 71 DCHECK(!shutdown_needed_); |
| 63 if (status_updater_) | 72 if (status_updater_.get() != NULL) |
| 64 status_updater_->RemoveDelegate(this); | 73 status_updater_->RemoveDelegate(this); |
| 65 } | 74 } |
| 66 | 75 |
| 67 void DownloadManager::Shutdown() { | 76 void DownloadManager::Shutdown() { |
| 68 VLOG(20) << __FUNCTION__ << "()" | 77 VLOG(20) << __FUNCTION__ << "()" |
| 69 << " shutdown_needed_ = " << shutdown_needed_; | 78 << " shutdown_needed_ = " << shutdown_needed_; |
| 70 if (!shutdown_needed_) | 79 if (!shutdown_needed_) |
| 71 return; | 80 return; |
| 72 shutdown_needed_ = false; | 81 shutdown_needed_ = false; |
| 73 | 82 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 in_progress_.clear(); | 130 in_progress_.clear(); |
| 122 active_downloads_.clear(); | 131 active_downloads_.clear(); |
| 123 history_downloads_.clear(); | 132 history_downloads_.clear(); |
| 124 STLDeleteElements(&downloads_to_delete); | 133 STLDeleteElements(&downloads_to_delete); |
| 125 | 134 |
| 126 DCHECK(save_page_downloads_.empty()); | 135 DCHECK(save_page_downloads_.empty()); |
| 127 | 136 |
| 128 file_manager_ = NULL; | 137 file_manager_ = NULL; |
| 129 delegate_->Shutdown(); | 138 delegate_->Shutdown(); |
| 130 | 139 |
| 131 shutdown_needed_ = false; | 140 shutdown_needed_ = false; |
|
Randy Smith (Not in Mondays)
2011/09/09 20:56:00
Why eliminate this line? My memory is that we rel
benjhayden
2011/09/09 21:13:33
It's redundant to L81.
I'd rather keep the one on
| |
| 132 } | 141 } |
| 133 | 142 |
| 134 void DownloadManager::GetTemporaryDownloads( | 143 void DownloadManager::GetTemporaryDownloads( |
| 135 const FilePath& dir_path, DownloadVector* result) { | 144 const FilePath& dir_path, DownloadVector* result) { |
| 136 DCHECK(result); | 145 DCHECK(result); |
| 137 | 146 |
| 138 for (DownloadMap::iterator it = history_downloads_.begin(); | 147 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 139 it != history_downloads_.end(); ++it) { | 148 it != history_downloads_.end(); ++it) { |
| 140 if (it->second->is_temporary() && | 149 if (it->second->is_temporary() && |
| 141 it->second->full_path().DirName() == dir_path) | 150 it->second->full_path().DirName() == dir_path) |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 170 // The Incognito Downloads page will get the list of non-Incognito downloads | 179 // The Incognito Downloads page will get the list of non-Incognito downloads |
| 171 // from its parent profile. | 180 // from its parent profile. |
| 172 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) | 181 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) |
| 173 continue; | 182 continue; |
| 174 | 183 |
| 175 if (download_item->MatchesQuery(query_lower)) | 184 if (download_item->MatchesQuery(query_lower)) |
| 176 result->push_back(download_item); | 185 result->push_back(download_item); |
| 177 } | 186 } |
| 178 } | 187 } |
| 179 | 188 |
| 189 void DownloadManager::OnPersistentStoreGetNextId(int next_id) { | |
| 190 DVLOG(1) << __FUNCTION__ << " " << next_id; | |
| 191 base::AutoLock lock(next_id_lock_); | |
| 192 // TODO(benjhayden) Delay Profile initialization until here, and set next_id_ | |
| 193 // = next_id. The '+=' works for now because these ids are not yet persisted | |
| 194 // to the database. GetNextId() can allocate zero or more ids starting from 0, | |
| 195 // then this callback can increment next_id_, and the items with lower ids | |
| 196 // won't clash with any other items even though there may be items loaded from | |
| 197 // the history because items from the history don't have valid ids. | |
| 198 next_id_ += next_id; | |
| 199 } | |
| 200 | |
| 201 DownloadId DownloadManager::GetNextId() { | |
| 202 // May be called on any thread via the GetNextIdThunk. | |
| 203 // TODO(benjhayden) If otr, forward to parent DM. | |
| 204 base::AutoLock lock(next_id_lock_); | |
| 205 return DownloadId(this, next_id_++); | |
| 206 } | |
| 207 | |
| 208 DownloadManager::GetNextIdThunkType DownloadManager::GetNextIdThunk() { | |
| 209 // TODO(benjhayden) If otr, forward to parent DM. | |
| 210 return base::Bind(&DownloadManager::GetNextId, this); | |
| 211 } | |
| 212 | |
| 180 // Query the history service for information about all persisted downloads. | 213 // Query the history service for information about all persisted downloads. |
| 181 bool DownloadManager::Init(content::BrowserContext* browser_context) { | 214 bool DownloadManager::Init(content::BrowserContext* browser_context) { |
| 182 DCHECK(browser_context); | 215 DCHECK(browser_context); |
| 183 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 216 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 184 shutdown_needed_ = true; | 217 shutdown_needed_ = true; |
| 185 | 218 |
| 186 browser_context_ = browser_context; | 219 browser_context_ = browser_context; |
| 187 | 220 |
| 188 // In test mode, there may be no ResourceDispatcherHost. In this case it's | 221 // 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 | 222 // safe to avoid setting |file_manager_| because we only call a small set of |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 | 354 |
| 322 // Rename to intermediate name. | 355 // Rename to intermediate name. |
| 323 FilePath download_path; | 356 FilePath download_path; |
| 324 if (!delegate_->OverrideIntermediatePath(download, &download_path)) | 357 if (!delegate_->OverrideIntermediatePath(download, &download_path)) |
| 325 download_path = download->full_path(); | 358 download_path = download->full_path(); |
| 326 | 359 |
| 327 BrowserThread::PostTask( | 360 BrowserThread::PostTask( |
| 328 BrowserThread::FILE, FROM_HERE, | 361 BrowserThread::FILE, FROM_HERE, |
| 329 NewRunnableMethod( | 362 NewRunnableMethod( |
| 330 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, | 363 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, |
| 331 download->id(), download_path)); | 364 download->global_id(), download_path)); |
| 332 | 365 |
| 333 download->Rename(download_path); | 366 download->Rename(download_path); |
| 334 | 367 |
| 335 delegate_->AddItemToPersistentStore(download); | 368 delegate_->AddItemToPersistentStore(download); |
| 336 } | 369 } |
| 337 | 370 |
| 338 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { | 371 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
| 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 340 DownloadMap::iterator it = active_downloads_.find(download_id); | 373 DownloadMap::iterator it = active_downloads_.find(download_id); |
| 341 if (it != active_downloads_.end()) { | 374 if (it != active_downloads_.end()) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 509 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 477 | 510 |
| 478 DownloadItem* item = GetDownloadItem(download_id); | 511 DownloadItem* item = GetDownloadItem(download_id); |
| 479 if (!item) | 512 if (!item) |
| 480 return; | 513 return; |
| 481 | 514 |
| 482 if (item->safety_state() == DownloadItem::SAFE) { | 515 if (item->safety_state() == DownloadItem::SAFE) { |
| 483 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; | 516 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
| 484 } | 517 } |
| 485 | 518 |
| 486 BrowserThread::PostTask( | 519 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
| 487 BrowserThread::FILE, FROM_HERE, | 520 file_manager_, |
| 488 NewRunnableMethod( | 521 &DownloadFileManager::CompleteDownload, |
| 489 file_manager_, &DownloadFileManager::CompleteDownload, download_id)); | 522 item->global_id())); |
| 490 | 523 |
| 491 if (uniquifier) | 524 if (uniquifier) |
| 492 item->set_path_uniquifier(uniquifier); | 525 item->set_path_uniquifier(uniquifier); |
| 493 | 526 |
| 494 item->OnDownloadRenamedToFinalName(full_path); | 527 item->OnDownloadRenamedToFinalName(full_path); |
| 495 delegate_->UpdatePathForItemInPersistentStore(item, full_path); | 528 delegate_->UpdatePathForItemInPersistentStore(item, full_path); |
| 496 } | 529 } |
| 497 | 530 |
| 498 void DownloadManager::CancelDownload(int32 download_id) { | 531 void DownloadManager::CancelDownload(int32 download_id) { |
| 499 DownloadItem* download = GetActiveDownload(download_id); | 532 DownloadItem* download = GetActiveDownload(download_id); |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1009 DCHECK(ContainsKey(save_page_downloads_, download->id())); | 1042 DCHECK(ContainsKey(save_page_downloads_, download->id())); |
| 1010 save_page_downloads_.erase(download->id()); | 1043 save_page_downloads_.erase(download->id()); |
| 1011 | 1044 |
| 1012 if (download->IsComplete()) | 1045 if (download->IsComplete()) |
| 1013 NotificationService::current()->Notify( | 1046 NotificationService::current()->Notify( |
| 1014 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 1047 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 1015 Source<DownloadManager>(this), | 1048 Source<DownloadManager>(this), |
| 1016 Details<DownloadItem>(download)); | 1049 Details<DownloadItem>(download)); |
| 1017 } | 1050 } |
| 1018 } | 1051 } |
| OLD | NEW |