| 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->AsWeakPtr()), |
| 55 delegate_(delegate), | 60 delegate_(delegate), |
| 56 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { | 61 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { |
| 57 if (status_updater_) | 62 if (status_updater_) |
| 58 status_updater_->AddDelegate(this); | 63 status_updater_->AddDelegate(this); |
| 59 } | 64 } |
| 60 | 65 |
| 61 DownloadManager::~DownloadManager() { | 66 DownloadManager::~DownloadManager() { |
| 62 DCHECK(!shutdown_needed_); | 67 DCHECK(!shutdown_needed_); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 // The Incognito Downloads page will get the list of non-Incognito downloads | 175 // The Incognito Downloads page will get the list of non-Incognito downloads |
| 171 // from its parent profile. | 176 // from its parent profile. |
| 172 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) | 177 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) |
| 173 continue; | 178 continue; |
| 174 | 179 |
| 175 if (download_item->MatchesQuery(query_lower)) | 180 if (download_item->MatchesQuery(query_lower)) |
| 176 result->push_back(download_item); | 181 result->push_back(download_item); |
| 177 } | 182 } |
| 178 } | 183 } |
| 179 | 184 |
| 185 void DownloadManager::OnPersistentStoreGetNextId(int next_id) { |
| 186 DVLOG(1) << __FUNCTION__ << " " << next_id; |
| 187 base::AutoLock lock(next_id_lock_); |
| 188 // TODO(benjhayden) Delay Profile initialization until here, and set next_id_ |
| 189 // = next_id. The '+=' works for now because these ids are not yet persisted |
| 190 // to the database. GetNextId() can allocate zero or more ids starting from 0, |
| 191 // then this callback can increment next_id_, and the items with lower ids |
| 192 // won't clash with any other items even though there may be items loaded from |
| 193 // the history because items from the history don't have valid ids. |
| 194 next_id_ += next_id; |
| 195 } |
| 196 |
| 197 DownloadId DownloadManager::GetNextId() { |
| 198 // May be called on any thread via the GetNextIdThunk. |
| 199 // TODO(benjhayden) If otr, forward to parent DM. |
| 200 base::AutoLock lock(next_id_lock_); |
| 201 return DownloadId(this, next_id_++); |
| 202 } |
| 203 |
| 204 DownloadManager::GetNextIdThunkType DownloadManager::GetNextIdThunk() { |
| 205 // TODO(benjhayden) If otr, forward to parent DM. |
| 206 return base::Bind(&DownloadManager::GetNextId, this); |
| 207 } |
| 208 |
| 180 // Query the history service for information about all persisted downloads. | 209 // Query the history service for information about all persisted downloads. |
| 181 bool DownloadManager::Init(content::BrowserContext* browser_context) { | 210 bool DownloadManager::Init(content::BrowserContext* browser_context) { |
| 182 DCHECK(browser_context); | 211 DCHECK(browser_context); |
| 183 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 212 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 184 shutdown_needed_ = true; | 213 shutdown_needed_ = true; |
| 185 | 214 |
| 186 browser_context_ = browser_context; | 215 browser_context_ = browser_context; |
| 187 | 216 |
| 188 // In test mode, there may be no ResourceDispatcherHost. In this case it's | 217 // 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 | 218 // 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 | 350 |
| 322 // Rename to intermediate name. | 351 // Rename to intermediate name. |
| 323 FilePath download_path; | 352 FilePath download_path; |
| 324 if (!delegate_->OverrideIntermediatePath(download, &download_path)) | 353 if (!delegate_->OverrideIntermediatePath(download, &download_path)) |
| 325 download_path = download->full_path(); | 354 download_path = download->full_path(); |
| 326 | 355 |
| 327 BrowserThread::PostTask( | 356 BrowserThread::PostTask( |
| 328 BrowserThread::FILE, FROM_HERE, | 357 BrowserThread::FILE, FROM_HERE, |
| 329 NewRunnableMethod( | 358 NewRunnableMethod( |
| 330 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, | 359 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, |
| 331 download->id(), download_path)); | 360 download->global_id(), download_path)); |
| 332 | 361 |
| 333 download->Rename(download_path); | 362 download->Rename(download_path); |
| 334 | 363 |
| 335 delegate_->AddItemToPersistentStore(download); | 364 delegate_->AddItemToPersistentStore(download); |
| 336 } | 365 } |
| 337 | 366 |
| 338 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { | 367 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
| 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 368 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 340 DownloadMap::iterator it = active_downloads_.find(download_id); | 369 DownloadMap::iterator it = active_downloads_.find(download_id); |
| 341 if (it != active_downloads_.end()) { | 370 if (it != active_downloads_.end()) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 519 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 491 | 520 |
| 492 DownloadItem* item = GetDownloadItem(download_id); | 521 DownloadItem* item = GetDownloadItem(download_id); |
| 493 if (!item) | 522 if (!item) |
| 494 return; | 523 return; |
| 495 | 524 |
| 496 if (item->safety_state() == DownloadItem::SAFE) { | 525 if (item->safety_state() == DownloadItem::SAFE) { |
| 497 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; | 526 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
| 498 } | 527 } |
| 499 | 528 |
| 500 BrowserThread::PostTask( | 529 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
| 501 BrowserThread::FILE, FROM_HERE, | 530 file_manager_, |
| 502 NewRunnableMethod( | 531 &DownloadFileManager::CompleteDownload, |
| 503 file_manager_, &DownloadFileManager::CompleteDownload, download_id)); | 532 item->global_id())); |
| 504 | 533 |
| 505 if (uniquifier) | 534 if (uniquifier) |
| 506 item->set_path_uniquifier(uniquifier); | 535 item->set_path_uniquifier(uniquifier); |
| 507 | 536 |
| 508 item->OnDownloadRenamedToFinalName(full_path); | 537 item->OnDownloadRenamedToFinalName(full_path); |
| 509 delegate_->UpdatePathForItemInPersistentStore(item, full_path); | 538 delegate_->UpdatePathForItemInPersistentStore(item, full_path); |
| 510 } | 539 } |
| 511 | 540 |
| 512 void DownloadManager::CancelDownload(int32 download_id) { | 541 void DownloadManager::CancelDownload(int32 download_id) { |
| 513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 542 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 // | 593 // |
| 565 // Clean up will happen when the history system create callback runs if we | 594 // Clean up will happen when the history system create callback runs if we |
| 566 // don't have a valid db_handle yet. | 595 // don't have a valid db_handle yet. |
| 567 if (download->db_handle() != DownloadItem::kUninitializedHandle) { | 596 if (download->db_handle() != DownloadItem::kUninitializedHandle) { |
| 568 in_progress_.erase(download_id); | 597 in_progress_.erase(download_id); |
| 569 active_downloads_.erase(download_id); | 598 active_downloads_.erase(download_id); |
| 570 UpdateDownloadProgress(); // Reflect removal from in_progress_. | 599 UpdateDownloadProgress(); // Reflect removal from in_progress_. |
| 571 delegate_->UpdateItemInPersistentStore(download); | 600 delegate_->UpdateItemInPersistentStore(download); |
| 572 } | 601 } |
| 573 | 602 |
| 574 BrowserThread::PostTask( | 603 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
| 575 BrowserThread::FILE, FROM_HERE, | 604 file_manager_, |
| 576 NewRunnableMethod( | 605 &DownloadFileManager::CancelDownload, |
| 577 file_manager_, &DownloadFileManager::CancelDownload, download_id)); | 606 download->global_id())); |
| 578 } | 607 } |
| 579 | 608 |
| 580 void DownloadManager::UpdateDownloadProgress() { | 609 void DownloadManager::UpdateDownloadProgress() { |
| 581 delegate_->DownloadProgressUpdated(); | 610 delegate_->DownloadProgressUpdated(); |
| 582 } | 611 } |
| 583 | 612 |
| 584 int DownloadManager::RemoveDownloadItems( | 613 int DownloadManager::RemoveDownloadItems( |
| 585 const DownloadVector& pending_deletes) { | 614 const DownloadVector& pending_deletes) { |
| 586 if (pending_deletes.empty()) | 615 if (pending_deletes.empty()) |
| 587 return 0; | 616 return 0; |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 DCHECK(ContainsKey(save_page_downloads_, download->id())); | 1044 DCHECK(ContainsKey(save_page_downloads_, download->id())); |
| 1016 save_page_downloads_.erase(download->id()); | 1045 save_page_downloads_.erase(download->id()); |
| 1017 | 1046 |
| 1018 if (download->IsComplete()) | 1047 if (download->IsComplete()) |
| 1019 NotificationService::current()->Notify( | 1048 NotificationService::current()->Notify( |
| 1020 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 1049 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 1021 Source<DownloadManager>(this), | 1050 Source<DownloadManager>(this), |
| 1022 Details<DownloadItem>(download)); | 1051 Details<DownloadItem>(download)); |
| 1023 } | 1052 } |
| 1024 } | 1053 } |
| OLD | NEW |