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 "chrome/browser/download/download_manager.h" | 5 #include "chrome/browser/download/download_manager.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/callback.h" | 8 #include "base/callback.h" |
8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
9 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
16 #include "base/synchronization/lock.h" | |
15 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
16 #include "base/task.h" | 18 #include "base/task.h" |
17 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
18 #include "build/build_config.h" | 20 #include "build/build_config.h" |
19 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/download/download_create_info.h" | 22 #include "chrome/browser/download/download_create_info.h" |
21 #include "chrome/browser/download/download_extensions.h" | 23 #include "chrome/browser/download/download_extensions.h" |
22 #include "chrome/browser/download/download_file_manager.h" | 24 #include "chrome/browser/download/download_file_manager.h" |
23 #include "chrome/browser/download/download_history.h" | 25 #include "chrome/browser/download/download_history.h" |
24 #include "chrome/browser/download/download_item.h" | 26 #include "chrome/browser/download/download_item.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
44 #include "grit/generated_resources.h" | 46 #include "grit/generated_resources.h" |
45 #include "grit/theme_resources.h" | 47 #include "grit/theme_resources.h" |
46 #include "net/base/mime_util.h" | 48 #include "net/base/mime_util.h" |
47 #include "net/base/net_util.h" | 49 #include "net/base/net_util.h" |
48 #include "ui/base/l10n/l10n_util.h" | 50 #include "ui/base/l10n/l10n_util.h" |
49 #include "ui/base/resource/resource_bundle.h" | 51 #include "ui/base/resource/resource_bundle.h" |
50 | 52 |
51 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, | 53 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, |
52 DownloadStatusUpdater* status_updater) | 54 DownloadStatusUpdater* status_updater) |
53 : shutdown_needed_(false), | 55 : shutdown_needed_(false), |
56 next_id_(0), | |
54 profile_(NULL), | 57 profile_(NULL), |
55 file_manager_(NULL), | 58 file_manager_(NULL), |
56 status_updater_(status_updater->AsWeakPtr()), | 59 status_updater_(status_updater->AsWeakPtr()), |
57 delegate_(delegate) { | 60 delegate_(delegate) { |
58 if (status_updater_) | 61 if (status_updater_) |
59 status_updater_->AddDelegate(this); | 62 status_updater_->AddDelegate(this); |
60 } | 63 } |
61 | 64 |
62 DownloadManager::~DownloadManager() { | 65 DownloadManager::~DownloadManager() { |
63 DCHECK(!shutdown_needed_); | 66 DCHECK(!shutdown_needed_); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 if (download_item->MatchesQuery(query_lower)) | 213 if (download_item->MatchesQuery(query_lower)) |
211 result->push_back(download_item); | 214 result->push_back(download_item); |
212 } | 215 } |
213 | 216 |
214 // If we have a parent profile, let it add its downloads to the results. | 217 // If we have a parent profile, let it add its downloads to the results. |
215 Profile* original_profile = profile_->GetOriginalProfile(); | 218 Profile* original_profile = profile_->GetOriginalProfile(); |
216 if (original_profile != profile_) | 219 if (original_profile != profile_) |
217 original_profile->GetDownloadManager()->SearchDownloads(query, result); | 220 original_profile->GetDownloadManager()->SearchDownloads(query, result); |
218 } | 221 } |
219 | 222 |
223 void DownloadManager::OnHistoryGetNextId(int next_id) { | |
224 DVLOG(1) << __FUNCTION__ << " " << next_id; | |
225 base::AutoLock lock(next_id_lock_); | |
226 // TODO(benjhayden) Delay Profile initialization until here, and set next_id_ | |
227 // = next_id. The '+=' works for now because these ids are not yet persisted | |
228 // to the database. GetNextId() can return 0 once, then this callback can | |
229 // increment next_id_, and the item whose id=0 won't clash with any other | |
230 // items even though there may be items loaded from the history because items | |
231 // from the history don't have valid ids. | |
Randy Smith (Not in Mondays)
2011/08/03 21:10:29
Actually, what we're counting on is that GetNextId
benjhayden
2011/08/04 17:15:00
Done.
| |
232 next_id_ += next_id; | |
233 } | |
234 | |
235 DownloadId DownloadManager::GetNextId() { | |
236 // May be called on any thread via the GetNextIdThunk. | |
237 // TODO(benjhayden) If otr, forward to parent DM. | |
238 base::AutoLock lock(next_id_lock_); | |
239 return DownloadId(this, next_id_++); | |
240 } | |
241 | |
242 DownloadManager::GetNextIdThunkType DownloadManager::GetNextIdThunk() { | |
243 // TODO(benjhayden) If otr, forward to parent DM. | |
244 return base::Bind(&DownloadManager::GetNextId, this); | |
245 } | |
246 | |
220 // Query the history service for information about all persisted downloads. | 247 // Query the history service for information about all persisted downloads. |
221 bool DownloadManager::Init(Profile* profile) { | 248 bool DownloadManager::Init(Profile* profile) { |
222 DCHECK(profile); | 249 DCHECK(profile); |
223 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 250 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
224 shutdown_needed_ = true; | 251 shutdown_needed_ = true; |
225 | 252 |
226 profile_ = profile; | 253 profile_ = profile; |
227 download_history_.reset(new DownloadHistory(profile)); | 254 download_history_.reset(new DownloadHistory(profile)); |
255 download_history_->GetNextId(NewCallback( | |
Randy Smith (Not in Mondays)
2011/08/03 21:10:29
You might want to note that this isn't guaranteed
benjhayden
2011/08/04 17:15:00
Done.
| |
256 this, &DownloadManager::OnHistoryGetNextId)); | |
228 download_history_->Load( | 257 download_history_->Load( |
229 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); | 258 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); |
230 | 259 |
231 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs())); | 260 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs())); |
232 | 261 |
233 // In test mode, there may be no ResourceDispatcherHost. In this case it's | 262 // In test mode, there may be no ResourceDispatcherHost. In this case it's |
234 // safe to avoid setting |file_manager_| because we only call a small set of | 263 // safe to avoid setting |file_manager_| because we only call a small set of |
235 // functions, none of which need it. | 264 // functions, none of which need it. |
236 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); | 265 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
237 if (rdh) { | 266 if (rdh) { |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
580 // name after user confirmation will be set from | 609 // name after user confirmation will be set from |
581 // DownloadItem::OnDownloadCompleting. | 610 // DownloadItem::OnDownloadCompleting. |
582 download_path = | 611 download_path = |
583 download_util::GetCrDownloadPath(download->full_path()); | 612 download_util::GetCrDownloadPath(download->full_path()); |
584 } | 613 } |
585 | 614 |
586 BrowserThread::PostTask( | 615 BrowserThread::PostTask( |
587 BrowserThread::FILE, FROM_HERE, | 616 BrowserThread::FILE, FROM_HERE, |
588 NewRunnableMethod( | 617 NewRunnableMethod( |
589 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, | 618 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, |
590 download->id(), download_path)); | 619 download->global_id(), download_path)); |
591 | 620 |
592 download->Rename(download_path); | 621 download->Rename(download_path); |
593 | 622 |
594 download_history_->AddEntry(download, | 623 download_history_->AddEntry(download, |
595 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); | 624 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); |
596 } | 625 } |
597 | 626 |
598 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { | 627 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
599 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 628 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
600 DownloadMap::iterator it = active_downloads_.find(download_id); | 629 DownloadMap::iterator it = active_downloads_.find(download_id); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
784 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 813 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
785 | 814 |
786 DownloadItem* item = GetDownloadItem(download_id); | 815 DownloadItem* item = GetDownloadItem(download_id); |
787 if (!item) | 816 if (!item) |
788 return; | 817 return; |
789 | 818 |
790 if (item->safety_state() == DownloadItem::SAFE) { | 819 if (item->safety_state() == DownloadItem::SAFE) { |
791 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; | 820 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
792 } | 821 } |
793 | 822 |
794 BrowserThread::PostTask( | 823 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
795 BrowserThread::FILE, FROM_HERE, | 824 file_manager_, |
796 NewRunnableMethod( | 825 &DownloadFileManager::CompleteDownload, |
797 file_manager_, &DownloadFileManager::CompleteDownload, download_id)); | 826 item->global_id())); |
798 | 827 |
799 if (uniquifier) | 828 if (uniquifier) |
800 item->set_path_uniquifier(uniquifier); | 829 item->set_path_uniquifier(uniquifier); |
801 | 830 |
802 item->OnDownloadRenamedToFinalName(full_path); | 831 item->OnDownloadRenamedToFinalName(full_path); |
803 download_history_->UpdateDownloadPath(item, full_path); | 832 download_history_->UpdateDownloadPath(item, full_path); |
804 } | 833 } |
805 | 834 |
806 void DownloadManager::DownloadCancelled(int32 download_id) { | 835 void DownloadManager::DownloadCancelled(int32 download_id) { |
807 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 836 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 15 matching lines...) Expand all Loading... | |
823 } | 852 } |
824 | 853 |
825 DownloadCancelledInternal(download_id, download->request_handle()); | 854 DownloadCancelledInternal(download_id, download->request_handle()); |
826 } | 855 } |
827 | 856 |
828 void DownloadManager::DownloadCancelledInternal( | 857 void DownloadManager::DownloadCancelledInternal( |
829 int download_id, const DownloadRequestHandle& request_handle) { | 858 int download_id, const DownloadRequestHandle& request_handle) { |
830 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 859 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
831 request_handle.CancelRequest(); | 860 request_handle.CancelRequest(); |
832 | 861 |
833 BrowserThread::PostTask( | 862 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
834 BrowserThread::FILE, FROM_HERE, | 863 file_manager_, &DownloadFileManager::CancelDownload, |
835 NewRunnableMethod( | 864 DownloadId(this, download_id))); |
836 file_manager_, &DownloadFileManager::CancelDownload, download_id)); | |
837 } | 865 } |
838 | 866 |
839 void DownloadManager::OnDownloadError(int32 download_id, | 867 void DownloadManager::OnDownloadError(int32 download_id, |
840 int64 size, | 868 int64 size, |
841 int os_error) { | 869 int os_error) { |
842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 870 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
843 DownloadMap::iterator it = active_downloads_.find(download_id); | 871 DownloadMap::iterator it = active_downloads_.find(download_id); |
844 // A cancel at the right time could remove the download from the | 872 // A cancel at the right time could remove the download from the |
845 // |active_downloads_| map before we get here. | 873 // |active_downloads_| map before we get here. |
846 if (it == active_downloads_.end()) | 874 if (it == active_downloads_.end()) |
(...skipping 12 matching lines...) Expand all Loading... | |
859 // | 887 // |
860 // Clean up will happen when the history system create callback runs if we | 888 // Clean up will happen when the history system create callback runs if we |
861 // don't have a valid db_handle yet. | 889 // don't have a valid db_handle yet. |
862 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { | 890 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
863 in_progress_.erase(download_id); | 891 in_progress_.erase(download_id); |
864 active_downloads_.erase(download_id); | 892 active_downloads_.erase(download_id); |
865 UpdateAppIcon(); // Reflect removal from in_progress_. | 893 UpdateAppIcon(); // Reflect removal from in_progress_. |
866 download_history_->UpdateEntry(download); | 894 download_history_->UpdateEntry(download); |
867 } | 895 } |
868 | 896 |
869 BrowserThread::PostTask( | 897 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
870 BrowserThread::FILE, FROM_HERE, | 898 file_manager_, |
871 NewRunnableMethod( | 899 &DownloadFileManager::CancelDownload, |
872 file_manager_, &DownloadFileManager::CancelDownload, download_id)); | 900 download->global_id())); |
873 } | 901 } |
874 | 902 |
875 void DownloadManager::UpdateAppIcon() { | 903 void DownloadManager::UpdateAppIcon() { |
876 if (status_updater_) | 904 if (status_updater_) |
877 status_updater_->Update(); | 905 status_updater_->Update(); |
878 } | 906 } |
879 | 907 |
880 void DownloadManager::RemoveDownload(int64 download_handle) { | 908 void DownloadManager::RemoveDownload(int64 download_handle) { |
881 DownloadMap::iterator it = history_downloads_.find(download_handle); | 909 DownloadMap::iterator it = history_downloads_.find(download_handle); |
882 if (it == history_downloads_.end()) | 910 if (it == history_downloads_.end()) |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1318 observed_download_manager_->RemoveObserver(this); | 1346 observed_download_manager_->RemoveObserver(this); |
1319 } | 1347 } |
1320 | 1348 |
1321 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1349 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
1322 observing_download_manager_->NotifyModelChanged(); | 1350 observing_download_manager_->NotifyModelChanged(); |
1323 } | 1351 } |
1324 | 1352 |
1325 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1353 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
1326 observed_download_manager_ = NULL; | 1354 observed_download_manager_ = NULL; |
1327 } | 1355 } |
OLD | NEW |