Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/download/download_manager.h" | 5 #include "chrome/browser/download/download_manager.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 | 115 |
| 116 // At this point, all dangerous downloads have had their files removed | 116 // At this point, all dangerous downloads have had their files removed |
| 117 // and all in progress downloads have been cancelled. We can now delete | 117 // and all in progress downloads have been cancelled. We can now delete |
| 118 // anything left. | 118 // anything left. |
| 119 STLDeleteElements(&downloads_); | 119 STLDeleteElements(&downloads_); |
| 120 | 120 |
| 121 // And clear all non-owning containers. | 121 // And clear all non-owning containers. |
| 122 in_progress_.clear(); | 122 in_progress_.clear(); |
| 123 active_downloads_.clear(); | |
| 123 #if !defined(NDEBUG) | 124 #if !defined(NDEBUG) |
| 124 save_page_as_downloads_.clear(); | 125 save_page_as_downloads_.clear(); |
| 125 #endif | 126 #endif |
| 126 | 127 |
| 127 file_manager_ = NULL; | 128 file_manager_ = NULL; |
| 128 | 129 |
| 129 // Make sure the save as dialog doesn't notify us back if we're gone before | 130 // Make sure the save as dialog doesn't notify us back if we're gone before |
| 130 // it returns. | 131 // it returns. |
| 131 if (select_file_dialog_.get()) | 132 if (select_file_dialog_.get()) |
| 132 select_file_dialog_->ListenerDestroyed(); | 133 select_file_dialog_->ListenerDestroyed(); |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 420 AttachDownloadItem(info, info->suggested_path); | 421 AttachDownloadItem(info, info->suggested_path); |
| 421 } | 422 } |
| 422 } | 423 } |
| 423 | 424 |
| 424 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { | 425 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
| 425 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 426 | 427 |
| 427 DownloadItem* download = new DownloadItem(this, *info, | 428 DownloadItem* download = new DownloadItem(this, *info, |
| 428 profile_->IsOffTheRecord()); | 429 profile_->IsOffTheRecord()); |
| 429 DCHECK(!ContainsKey(in_progress_, info->download_id)); | 430 DCHECK(!ContainsKey(in_progress_, info->download_id)); |
| 431 DCHECK(!ContainsKey(active_downloads_, info->download_id)); | |
| 430 downloads_.insert(download); | 432 downloads_.insert(download); |
| 433 active_downloads_[info->download_id] = download; | |
| 431 } | 434 } |
| 432 | 435 |
| 433 void DownloadManager::AttachDownloadItem(DownloadCreateInfo* info, | 436 void DownloadManager::AttachDownloadItem(DownloadCreateInfo* info, |
| 434 const FilePath& target_path) { | 437 const FilePath& target_path) { |
| 435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 438 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 436 | 439 |
| 437 scoped_ptr<DownloadCreateInfo> infop(info); | 440 scoped_ptr<DownloadCreateInfo> infop(info); |
| 438 info->path = target_path; | 441 info->path = target_path; |
| 439 | 442 |
| 440 // NOTE(ahendrickson) We will be adding a new map |active_downloads_|, into | 443 // NOTE(ahendrickson) Eventually |active_downloads_| will replace |
| 441 // which we will be adding the download as soon as it's created. This will | 444 // |in_progress_|, but we don't want to change the semantics yet. |
|
Randy Smith (Not in Mondays)
2010/12/29 15:58:11
Just FYI: I think we'll be in a position to yank i
| |
| 442 // make this loop unnecessary. | |
| 443 // Eventually |active_downloads_| will replace |in_progress_|, but we don't | |
| 444 // want to change the semantics yet. | |
| 445 DCHECK(!ContainsKey(in_progress_, info->download_id)); | 445 DCHECK(!ContainsKey(in_progress_, info->download_id)); |
| 446 DownloadItem* download = NULL; | 446 DCHECK(ContainsKey(active_downloads_, info->download_id)); |
| 447 for (std::set<DownloadItem*>::iterator i = downloads_.begin(); | 447 DownloadItem* download = active_downloads_[info->download_id]; |
| 448 i != downloads_.end(); ++i) { | |
| 449 DownloadItem* item = (*i); | |
| 450 if (item && (item->id() == info->download_id)) { | |
| 451 download = item; | |
| 452 break; | |
| 453 } | |
| 454 } | |
| 455 DCHECK(download != NULL); | 448 DCHECK(download != NULL); |
| 449 DCHECK(downloads_.find(download) != downloads_.end()); | |
| 450 | |
| 456 download->SetFileCheckResults(info->path, | 451 download->SetFileCheckResults(info->path, |
| 457 info->is_dangerous, | 452 info->is_dangerous, |
| 458 info->path_uniquifier, | 453 info->path_uniquifier, |
| 459 info->prompt_user_for_save_location, | 454 info->prompt_user_for_save_location, |
| 460 info->is_extension_install, | 455 info->is_extension_install, |
| 461 info->original_name); | 456 info->original_name); |
| 462 in_progress_[info->download_id] = download; | 457 in_progress_[info->download_id] = download; |
| 463 | 458 |
| 464 bool download_finished = ContainsKey(pending_finished_downloads_, | 459 bool download_finished = ContainsKey(pending_finished_downloads_, |
| 465 info->download_id); | 460 info->download_id); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 pending_finished_downloads_[info->download_id]); | 494 pending_finished_downloads_[info->download_id]); |
| 500 } | 495 } |
| 501 | 496 |
| 502 download_history_->AddEntry(*info, download, | 497 download_history_->AddEntry(*info, download, |
| 503 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); | 498 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); |
| 504 | 499 |
| 505 UpdateAppIcon(); | 500 UpdateAppIcon(); |
| 506 } | 501 } |
| 507 | 502 |
| 508 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { | 503 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
| 509 DownloadMap::iterator it = in_progress_.find(download_id); | 504 DownloadMap::iterator it = active_downloads_.find(download_id); |
| 510 if (it != in_progress_.end()) { | 505 if (it != active_downloads_.end()) { |
| 511 DownloadItem* download = it->second; | 506 DownloadItem* download = it->second; |
| 512 download->Update(size); | 507 download->Update(size); |
| 513 download_history_->UpdateEntry(download); | 508 download_history_->UpdateEntry(download); |
| 514 } | 509 } |
| 515 UpdateAppIcon(); | 510 UpdateAppIcon(); |
| 516 } | 511 } |
| 517 | 512 |
| 518 void DownloadManager::OnAllDataSaved(int32 download_id, int64 size) { | 513 void DownloadManager::OnAllDataSaved(int32 download_id, int64 size) { |
| 519 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | 514 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 520 << " size = " << size; | 515 << " size = " << size; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 546 | 541 |
| 547 VLOG(20) << __FUNCTION__ << "()" | 542 VLOG(20) << __FUNCTION__ << "()" |
| 548 << " download = " << download->DebugString(true); | 543 << " download = " << download->DebugString(true); |
| 549 | 544 |
| 550 download->OnAllDataSaved(size); | 545 download->OnAllDataSaved(size); |
| 551 | 546 |
| 552 // Clean up will happen when the history system create callback runs if we | 547 // Clean up will happen when the history system create callback runs if we |
| 553 // don't have a valid db_handle yet. | 548 // don't have a valid db_handle yet. |
| 554 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { | 549 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
| 555 in_progress_.erase(it); | 550 in_progress_.erase(it); |
| 551 active_downloads_.erase(download_id); | |
|
Randy Smith (Not in Mondays)
2010/12/29 15:58:11
It looks like the semantics for active_downloads_
ahendrickson
2011/01/01 18:15:59
Done.
| |
| 556 download_history_->UpdateEntry(download); | 552 download_history_->UpdateEntry(download); |
| 557 } | 553 } |
| 558 | 554 |
| 559 UpdateAppIcon(); | 555 UpdateAppIcon(); |
| 560 | 556 |
| 561 // If this a dangerous download not yet validated by the user, don't do | 557 // If this a dangerous download not yet validated by the user, don't do |
| 562 // anything. When the user notifies us, it will trigger a call to | 558 // anything. When the user notifies us, it will trigger a call to |
| 563 // ProceedWithFinishedDangerousDownload. | 559 // ProceedWithFinishedDangerousDownload. |
| 564 if (download->safety_state() == DownloadItem::DANGEROUS) { | 560 if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 565 return; | 561 return; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 654 return; | 650 return; |
| 655 DownloadItem* download = it->second; | 651 DownloadItem* download = it->second; |
| 656 | 652 |
| 657 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | 653 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 658 << " download = " << download->DebugString(true); | 654 << " download = " << download->DebugString(true); |
| 659 | 655 |
| 660 // Clean up will happen when the history system create callback runs if we | 656 // Clean up will happen when the history system create callback runs if we |
| 661 // don't have a valid db_handle yet. | 657 // don't have a valid db_handle yet. |
| 662 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { | 658 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
| 663 in_progress_.erase(it); | 659 in_progress_.erase(it); |
| 660 active_downloads_.erase(download_id); | |
| 664 download_history_->UpdateEntry(download); | 661 download_history_->UpdateEntry(download); |
| 665 } | 662 } |
| 666 | 663 |
| 667 DownloadCancelledInternal(download_id, | 664 DownloadCancelledInternal(download_id, |
| 668 download->render_process_id(), | 665 download->render_process_id(), |
| 669 download->request_id()); | 666 download->request_id()); |
| 670 UpdateAppIcon(); | 667 UpdateAppIcon(); |
| 671 } | 668 } |
| 672 | 669 |
| 673 void DownloadManager::DownloadCancelledInternal(int download_id, | 670 void DownloadManager::DownloadCancelledInternal(int download_id, |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 976 db_handle = download_history_->GetNextFakeDbHandle(); | 973 db_handle = download_history_->GetNextFakeDbHandle(); |
| 977 | 974 |
| 978 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); | 975 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); |
| 979 download->set_db_handle(db_handle); | 976 download->set_db_handle(db_handle); |
| 980 | 977 |
| 981 // Insert into our full map. | 978 // Insert into our full map. |
| 982 DCHECK(history_downloads_.find(download->db_handle()) == | 979 DCHECK(history_downloads_.find(download->db_handle()) == |
| 983 history_downloads_.end()); | 980 history_downloads_.end()); |
| 984 history_downloads_[download->db_handle()] = download; | 981 history_downloads_[download->db_handle()] = download; |
| 985 | 982 |
| 986 // Show in the appropropriate browser UI. | 983 // Show in the appropriate browser UI. |
| 987 ShowDownloadInBrowser(info, download); | 984 ShowDownloadInBrowser(info, download); |
| 988 | 985 |
| 989 // Inform interested objects about the new download. | 986 // Inform interested objects about the new download. |
| 990 NotifyModelChanged(); | 987 NotifyModelChanged(); |
| 991 | 988 |
| 992 // If this download has been completed before we've received the db handle, | 989 // If this download has been completed before we've received the db handle, |
| 993 // post one final message to the history service so that it can be properly | 990 // post one final message to the history service so that it can be properly |
| 994 // in sync with the DownloadItem's completion status, and also inform any | 991 // in sync with the DownloadItem's completion status, and also inform any |
| 995 // observers so that they get more than just the start notification. | 992 // observers so that they get more than just the start notification. |
| 996 if (download->state() != DownloadItem::IN_PROGRESS) { | 993 if (download->state() != DownloadItem::IN_PROGRESS) { |
| 997 in_progress_.erase(it); | 994 in_progress_.erase(it); |
| 995 active_downloads_.erase(info.download_id); | |
| 998 download_history_->UpdateEntry(download); | 996 download_history_->UpdateEntry(download); |
| 999 download->UpdateObservers(); | 997 download->UpdateObservers(); |
| 1000 } | 998 } |
| 1001 | 999 |
| 1002 UpdateAppIcon(); | 1000 UpdateAppIcon(); |
| 1003 } | 1001 } |
| 1004 | 1002 |
| 1005 void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info, | 1003 void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info, |
| 1006 DownloadItem* download) { | 1004 DownloadItem* download) { |
| 1007 // The 'contents' may no longer exist if the user closed the tab before we | 1005 // The 'contents' may no longer exist if the user closed the tab before we |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1041 } | 1039 } |
| 1042 return NULL; | 1040 return NULL; |
| 1043 } | 1041 } |
| 1044 | 1042 |
| 1045 // Confirm that everything in all maps is also in |downloads_|, and that | 1043 // Confirm that everything in all maps is also in |downloads_|, and that |
| 1046 // everything in |downloads_| is also in some other map. | 1044 // everything in |downloads_| is also in some other map. |
| 1047 void DownloadManager::AssertContainersConsistent() const { | 1045 void DownloadManager::AssertContainersConsistent() const { |
| 1048 #if !defined(NDEBUG) | 1046 #if !defined(NDEBUG) |
| 1049 // Turn everything into sets. | 1047 // Turn everything into sets. |
| 1050 DownloadSet in_progress_set, history_set; | 1048 DownloadSet in_progress_set, history_set; |
| 1051 const DownloadMap* input_maps[] = {&in_progress_, &history_downloads_}; | 1049 const DownloadMap* input_maps[] = {&active_downloads_, &history_downloads_}; |
| 1052 DownloadSet* local_sets[] = {&in_progress_set, &history_set}; | 1050 DownloadSet* local_sets[] = {&in_progress_set, &history_set}; |
| 1053 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(local_sets)); | 1051 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(local_sets)); |
| 1054 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) { | 1052 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) { |
| 1055 for (DownloadMap::const_iterator it = input_maps[i]->begin(); | 1053 for (DownloadMap::const_iterator it = input_maps[i]->begin(); |
| 1056 it != input_maps[i]->end(); it++) { | 1054 it != input_maps[i]->end(); it++) { |
| 1057 local_sets[i]->insert(&*it->second); | 1055 local_sets[i]->insert(&*it->second); |
| 1058 } | 1056 } |
| 1059 } | 1057 } |
| 1060 | 1058 |
| 1061 // Check if each set is fully present in downloads, and create a union. | 1059 // Check if each set is fully present in downloads, and create a union. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1108 observed_download_manager_->RemoveObserver(this); | 1106 observed_download_manager_->RemoveObserver(this); |
| 1109 } | 1107 } |
| 1110 | 1108 |
| 1111 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1109 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
| 1112 observing_download_manager_->NotifyModelChanged(); | 1110 observing_download_manager_->NotifyModelChanged(); |
| 1113 } | 1111 } |
| 1114 | 1112 |
| 1115 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1113 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
| 1116 observed_download_manager_ = NULL; | 1114 observed_download_manager_ = NULL; |
| 1117 } | 1115 } |
| OLD | NEW |