| 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 "chrome/browser/download/download_util.h" | 20 #include "chrome/browser/download/download_util.h" |
| 17 #include "content/browser/browser_context.h" | 21 #include "content/browser/browser_context.h" |
| 18 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
| 19 #include "content/browser/content_browser_client.h" | 23 #include "content/browser/content_browser_client.h" |
| 20 #include "content/browser/download/download_create_info.h" | 24 #include "content/browser/download/download_create_info.h" |
| 21 #include "content/browser/download/download_file_manager.h" | 25 #include "content/browser/download/download_file_manager.h" |
| 22 #include "content/browser/download/download_item.h" | 26 #include "content/browser/download/download_item.h" |
| 23 #include "content/browser/download/download_manager_delegate.h" | 27 #include "content/browser/download/download_manager_delegate.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 44 url, referrer, save_info, true, render_process_id, render_view_id, | 48 url, referrer, save_info, true, render_process_id, render_view_id, |
| 45 *context); | 49 *context); |
| 46 } | 50 } |
| 47 | 51 |
| 48 } // namespace | 52 } // namespace |
| 49 | 53 |
| 50 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, | 54 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, |
| 51 DownloadStatusUpdater* status_updater) | 55 DownloadStatusUpdater* status_updater) |
| 52 : shutdown_needed_(false), | 56 : shutdown_needed_(false), |
| 53 browser_context_(NULL), | 57 browser_context_(NULL), |
| 58 next_id_(0), |
| 54 file_manager_(NULL), | 59 file_manager_(NULL), |
| 55 status_updater_(status_updater->AsWeakPtr()), | 60 status_updater_(status_updater->AsWeakPtr()), |
| 56 delegate_(delegate) { | 61 delegate_(delegate) { |
| 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_); |
| 63 if (status_updater_) | 68 if (status_updater_) |
| (...skipping 106 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 // name after user confirmation will be set from | 360 // name after user confirmation will be set from |
| 332 // DownloadItem::OnDownloadCompleting. | 361 // DownloadItem::OnDownloadCompleting. |
| 333 download_path = | 362 download_path = |
| 334 download_util::GetCrDownloadPath(download->full_path()); | 363 download_util::GetCrDownloadPath(download->full_path()); |
| 335 } | 364 } |
| 336 | 365 |
| 337 BrowserThread::PostTask( | 366 BrowserThread::PostTask( |
| 338 BrowserThread::FILE, FROM_HERE, | 367 BrowserThread::FILE, FROM_HERE, |
| 339 NewRunnableMethod( | 368 NewRunnableMethod( |
| 340 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, | 369 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, |
| 341 download->id(), download_path)); | 370 download->global_id(), download_path)); |
| 342 | 371 |
| 343 download->Rename(download_path); | 372 download->Rename(download_path); |
| 344 | 373 |
| 345 delegate_->AddItemToPersistentStore(download); | 374 delegate_->AddItemToPersistentStore(download); |
| 346 } | 375 } |
| 347 | 376 |
| 348 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { | 377 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
| 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 378 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 350 DownloadMap::iterator it = active_downloads_.find(download_id); | 379 DownloadMap::iterator it = active_downloads_.find(download_id); |
| 351 if (it != active_downloads_.end()) { | 380 if (it != active_downloads_.end()) { |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 500 | 529 |
| 501 DownloadItem* item = GetDownloadItem(download_id); | 530 DownloadItem* item = GetDownloadItem(download_id); |
| 502 if (!item) | 531 if (!item) |
| 503 return; | 532 return; |
| 504 | 533 |
| 505 if (item->safety_state() == DownloadItem::SAFE) { | 534 if (item->safety_state() == DownloadItem::SAFE) { |
| 506 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; | 535 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
| 507 } | 536 } |
| 508 | 537 |
| 509 BrowserThread::PostTask( | 538 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
| 510 BrowserThread::FILE, FROM_HERE, | 539 file_manager_, |
| 511 NewRunnableMethod( | 540 &DownloadFileManager::CompleteDownload, |
| 512 file_manager_, &DownloadFileManager::CompleteDownload, download_id)); | 541 item->global_id())); |
| 513 | 542 |
| 514 if (uniquifier) | 543 if (uniquifier) |
| 515 item->set_path_uniquifier(uniquifier); | 544 item->set_path_uniquifier(uniquifier); |
| 516 | 545 |
| 517 item->OnDownloadRenamedToFinalName(full_path); | 546 item->OnDownloadRenamedToFinalName(full_path); |
| 518 delegate_->UpdatePathForItemInPersistentStore(item, full_path); | 547 delegate_->UpdatePathForItemInPersistentStore(item, full_path); |
| 519 } | 548 } |
| 520 | 549 |
| 521 void DownloadManager::DownloadCancelled(int32 download_id) { | 550 void DownloadManager::DownloadCancelled(int32 download_id) { |
| 522 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 551 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 538 } | 567 } |
| 539 | 568 |
| 540 DownloadCancelledInternal(download_id, download->request_handle()); | 569 DownloadCancelledInternal(download_id, download->request_handle()); |
| 541 } | 570 } |
| 542 | 571 |
| 543 void DownloadManager::DownloadCancelledInternal( | 572 void DownloadManager::DownloadCancelledInternal( |
| 544 int download_id, const DownloadRequestHandle& request_handle) { | 573 int download_id, const DownloadRequestHandle& request_handle) { |
| 545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 574 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 546 request_handle.CancelRequest(); | 575 request_handle.CancelRequest(); |
| 547 | 576 |
| 548 BrowserThread::PostTask( | 577 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
| 549 BrowserThread::FILE, FROM_HERE, | 578 file_manager_, &DownloadFileManager::CancelDownload, |
| 550 NewRunnableMethod( | 579 DownloadId(this, download_id))); |
| 551 file_manager_, &DownloadFileManager::CancelDownload, download_id)); | |
| 552 } | 580 } |
| 553 | 581 |
| 554 void DownloadManager::OnDownloadError(int32 download_id, | 582 void DownloadManager::OnDownloadError(int32 download_id, |
| 555 int64 size, | 583 int64 size, |
| 556 int os_error) { | 584 int os_error) { |
| 557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 585 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 558 DownloadMap::iterator it = active_downloads_.find(download_id); | 586 DownloadMap::iterator it = active_downloads_.find(download_id); |
| 559 // A cancel at the right time could remove the download from the | 587 // A cancel at the right time could remove the download from the |
| 560 // |active_downloads_| map before we get here. | 588 // |active_downloads_| map before we get here. |
| 561 if (it == active_downloads_.end()) | 589 if (it == active_downloads_.end()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 574 // | 602 // |
| 575 // Clean up will happen when the history system create callback runs if we | 603 // Clean up will happen when the history system create callback runs if we |
| 576 // don't have a valid db_handle yet. | 604 // don't have a valid db_handle yet. |
| 577 if (download->db_handle() != DownloadItem::kUninitializedHandle) { | 605 if (download->db_handle() != DownloadItem::kUninitializedHandle) { |
| 578 in_progress_.erase(download_id); | 606 in_progress_.erase(download_id); |
| 579 active_downloads_.erase(download_id); | 607 active_downloads_.erase(download_id); |
| 580 UpdateDownloadProgress(); // Reflect removal from in_progress_. | 608 UpdateDownloadProgress(); // Reflect removal from in_progress_. |
| 581 delegate_->UpdateItemInPersistentStore(download); | 609 delegate_->UpdateItemInPersistentStore(download); |
| 582 } | 610 } |
| 583 | 611 |
| 584 BrowserThread::PostTask( | 612 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
| 585 BrowserThread::FILE, FROM_HERE, | 613 file_manager_, |
| 586 NewRunnableMethod( | 614 &DownloadFileManager::CancelDownload, |
| 587 file_manager_, &DownloadFileManager::CancelDownload, download_id)); | 615 download->global_id())); |
| 588 } | 616 } |
| 589 | 617 |
| 590 void DownloadManager::UpdateDownloadProgress() { | 618 void DownloadManager::UpdateDownloadProgress() { |
| 591 delegate_->DownloadProgressUpdated(); | 619 delegate_->DownloadProgressUpdated(); |
| 592 } | 620 } |
| 593 | 621 |
| 594 int DownloadManager::RemoveDownloadItems( | 622 int DownloadManager::RemoveDownloadItems( |
| 595 const DownloadVector& pending_deletes) { | 623 const DownloadVector& pending_deletes) { |
| 596 if (pending_deletes.empty()) | 624 if (pending_deletes.empty()) |
| 597 return 0; | 625 return 0; |
| (...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1003 DCHECK(ContainsKey(save_page_downloads_, download->id())); | 1031 DCHECK(ContainsKey(save_page_downloads_, download->id())); |
| 1004 save_page_downloads_.erase(download->id()); | 1032 save_page_downloads_.erase(download->id()); |
| 1005 | 1033 |
| 1006 if (download->IsComplete()) | 1034 if (download->IsComplete()) |
| 1007 NotificationService::current()->Notify( | 1035 NotificationService::current()->Notify( |
| 1008 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 1036 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 1009 Source<DownloadManager>(this), | 1037 Source<DownloadManager>(this), |
| 1010 Details<DownloadItem>(download)); | 1038 Details<DownloadItem>(download)); |
| 1011 } | 1039 } |
| 1012 } | 1040 } |
| OLD | NEW |