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 20 matching lines...) Expand all Loading... | |
45 #include "googleurl/src/gurl.h" | 47 #include "googleurl/src/gurl.h" |
46 #include "grit/generated_resources.h" | 48 #include "grit/generated_resources.h" |
47 #include "grit/theme_resources.h" | 49 #include "grit/theme_resources.h" |
48 #include "net/base/mime_util.h" | 50 #include "net/base/mime_util.h" |
49 #include "net/base/net_util.h" | 51 #include "net/base/net_util.h" |
50 #include "ui/base/l10n/l10n_util.h" | 52 #include "ui/base/l10n/l10n_util.h" |
51 #include "ui/base/resource/resource_bundle.h" | 53 #include "ui/base/resource/resource_bundle.h" |
52 | 54 |
53 DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater) | 55 DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater) |
54 : shutdown_needed_(false), | 56 : shutdown_needed_(false), |
57 next_id_(0), | |
55 profile_(NULL), | 58 profile_(NULL), |
56 file_manager_(NULL), | 59 file_manager_(NULL), |
57 status_updater_(status_updater->AsWeakPtr()) { | 60 status_updater_(status_updater->AsWeakPtr()) { |
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_); |
64 if (status_updater_) | 67 if (status_updater_) |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 if (download_item->MatchesQuery(query_lower)) | 218 if (download_item->MatchesQuery(query_lower)) |
216 result->push_back(download_item); | 219 result->push_back(download_item); |
217 } | 220 } |
218 | 221 |
219 // If we have a parent profile, let it add its downloads to the results. | 222 // If we have a parent profile, let it add its downloads to the results. |
220 Profile* original_profile = profile_->GetOriginalProfile(); | 223 Profile* original_profile = profile_->GetOriginalProfile(); |
221 if (original_profile != profile_) | 224 if (original_profile != profile_) |
222 original_profile->GetDownloadManager()->SearchDownloads(query, result); | 225 original_profile->GetDownloadManager()->SearchDownloads(query, result); |
223 } | 226 } |
224 | 227 |
228 void DownloadManager::OnHistoryGetNextId(int next_id) { | |
229 DVLOG(1) << __FUNCTION__ << " " << next_id; | |
230 base::AutoLock _(next_id_lock_); | |
Randy Smith (Not in Mondays)
2011/07/28 21:03:16
I puzzled over this for a while before I got it.
benjhayden
2011/08/03 17:44:46
Done.
| |
231 // TODO(benjhayden) Delay Profile initialization until here, and set next_id_ | |
232 // = next_id. | |
Randy Smith (Not in Mondays)
2011/07/28 21:03:16
A bit more of a comment as to why this actually ma
benjhayden
2011/08/03 17:44:46
Done.
| |
233 next_id_ += next_id; | |
234 } | |
235 | |
236 DownloadId DownloadManager::GetNextId() { | |
237 // May be called on any thread via the GetNextIdThunk. | |
238 // TODO(benjhayden) If otr, forward to parent DM. | |
239 base::AutoLock _(next_id_lock_); | |
240 return DownloadId(this, next_id_++); | |
241 } | |
242 | |
243 DownloadManager::GetNextIdThunkType DownloadManager::GetNextIdThunk() { | |
244 // TODO(benjhayden) If otr, forward to parent DM. | |
245 return base::Bind(&DownloadManager::GetNextId, this); | |
246 } | |
247 | |
225 // Query the history service for information about all persisted downloads. | 248 // Query the history service for information about all persisted downloads. |
226 bool DownloadManager::Init(Profile* profile) { | 249 bool DownloadManager::Init(Profile* profile) { |
227 DCHECK(profile); | 250 DCHECK(profile); |
228 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 251 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
229 shutdown_needed_ = true; | 252 shutdown_needed_ = true; |
230 | 253 |
231 profile_ = profile; | 254 profile_ = profile; |
232 download_history_.reset(new DownloadHistory(profile)); | 255 download_history_.reset(new DownloadHistory(profile)); |
256 download_history_->GetNextId(NewCallback( | |
257 this, &DownloadManager::OnHistoryGetNextId)); | |
233 download_history_->Load( | 258 download_history_->Load( |
234 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); | 259 NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); |
235 | 260 |
236 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs())); | 261 download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs())); |
237 | 262 |
238 // In test mode, there may be no ResourceDispatcherHost. In this case it's | 263 // In test mode, there may be no ResourceDispatcherHost. In this case it's |
239 // safe to avoid setting |file_manager_| because we only call a small set of | 264 // safe to avoid setting |file_manager_| because we only call a small set of |
240 // functions, none of which need it. | 265 // functions, none of which need it. |
241 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); | 266 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
242 if (rdh) { | 267 if (rdh) { |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
599 // name after user confirmation will be set from | 624 // name after user confirmation will be set from |
600 // DownloadItem::OnDownloadCompleting. | 625 // DownloadItem::OnDownloadCompleting. |
601 download_path = | 626 download_path = |
602 download_util::GetCrDownloadPath(download->full_path()); | 627 download_util::GetCrDownloadPath(download->full_path()); |
603 } | 628 } |
604 | 629 |
605 BrowserThread::PostTask( | 630 BrowserThread::PostTask( |
606 BrowserThread::FILE, FROM_HERE, | 631 BrowserThread::FILE, FROM_HERE, |
607 NewRunnableMethod( | 632 NewRunnableMethod( |
608 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, | 633 file_manager_, &DownloadFileManager::RenameInProgressDownloadFile, |
609 download->id(), download_path)); | 634 download->gid(), download_path)); |
610 | 635 |
611 download->Rename(download_path); | 636 download->Rename(download_path); |
612 | 637 |
613 download_history_->AddEntry(download, | 638 download_history_->AddEntry(download, |
614 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); | 639 NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); |
615 } | 640 } |
616 | 641 |
617 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { | 642 void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
618 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 643 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
619 DownloadMap::iterator it = active_downloads_.find(download_id); | 644 DownloadMap::iterator it = active_downloads_.find(download_id); |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
806 if (!item) | 831 if (!item) |
807 return; | 832 return; |
808 | 833 |
809 if (item->safety_state() == DownloadItem::SAFE) { | 834 if (item->safety_state() == DownloadItem::SAFE) { |
810 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; | 835 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
811 } | 836 } |
812 | 837 |
813 BrowserThread::PostTask( | 838 BrowserThread::PostTask( |
814 BrowserThread::FILE, FROM_HERE, | 839 BrowserThread::FILE, FROM_HERE, |
815 NewRunnableMethod( | 840 NewRunnableMethod( |
816 file_manager_, &DownloadFileManager::CompleteDownload, download_id)); | 841 file_manager_, &DownloadFileManager::CompleteDownload, item->gid())); |
817 | 842 |
818 if (uniquifier) | 843 if (uniquifier) |
819 item->set_path_uniquifier(uniquifier); | 844 item->set_path_uniquifier(uniquifier); |
820 | 845 |
821 item->OnDownloadRenamedToFinalName(full_path); | 846 item->OnDownloadRenamedToFinalName(full_path); |
822 download_history_->UpdateDownloadPath(item, full_path); | 847 download_history_->UpdateDownloadPath(item, full_path); |
823 } | 848 } |
824 | 849 |
825 void DownloadManager::DownloadCancelled(int32 download_id) { | 850 void DownloadManager::DownloadCancelled(int32 download_id) { |
826 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 851 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 15 matching lines...) Expand all Loading... | |
842 } | 867 } |
843 | 868 |
844 DownloadCancelledInternal(download_id, download->request_handle()); | 869 DownloadCancelledInternal(download_id, download->request_handle()); |
845 } | 870 } |
846 | 871 |
847 void DownloadManager::DownloadCancelledInternal( | 872 void DownloadManager::DownloadCancelledInternal( |
848 int download_id, const DownloadRequestHandle& request_handle) { | 873 int download_id, const DownloadRequestHandle& request_handle) { |
849 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 874 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
850 request_handle.CancelRequest(); | 875 request_handle.CancelRequest(); |
851 | 876 |
852 BrowserThread::PostTask( | 877 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
853 BrowserThread::FILE, FROM_HERE, | 878 file_manager_, &DownloadFileManager::CancelDownload, |
854 NewRunnableMethod( | 879 DownloadId(this, download_id))); |
855 file_manager_, &DownloadFileManager::CancelDownload, download_id)); | |
856 } | 880 } |
857 | 881 |
858 void DownloadManager::OnDownloadError(int32 download_id, | 882 void DownloadManager::OnDownloadError(int32 download_id, |
859 int64 size, | 883 int64 size, |
860 int os_error) { | 884 int os_error) { |
861 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 885 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
862 DownloadMap::iterator it = active_downloads_.find(download_id); | 886 DownloadMap::iterator it = active_downloads_.find(download_id); |
863 // A cancel at the right time could remove the download from the | 887 // A cancel at the right time could remove the download from the |
864 // |active_downloads_| map before we get here. | 888 // |active_downloads_| map before we get here. |
865 if (it == active_downloads_.end()) | 889 if (it == active_downloads_.end()) |
(...skipping 12 matching lines...) Expand all Loading... | |
878 // | 902 // |
879 // Clean up will happen when the history system create callback runs if we | 903 // Clean up will happen when the history system create callback runs if we |
880 // don't have a valid db_handle yet. | 904 // don't have a valid db_handle yet. |
881 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { | 905 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
882 in_progress_.erase(download_id); | 906 in_progress_.erase(download_id); |
883 active_downloads_.erase(download_id); | 907 active_downloads_.erase(download_id); |
884 UpdateAppIcon(); // Reflect removal from in_progress_. | 908 UpdateAppIcon(); // Reflect removal from in_progress_. |
885 download_history_->UpdateEntry(download); | 909 download_history_->UpdateEntry(download); |
886 } | 910 } |
887 | 911 |
888 BrowserThread::PostTask( | 912 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
889 BrowserThread::FILE, FROM_HERE, | 913 file_manager_, &DownloadFileManager::CancelDownload, download->gid())); |
890 NewRunnableMethod( | |
891 file_manager_, &DownloadFileManager::CancelDownload, download_id)); | |
892 } | 914 } |
893 | 915 |
894 void DownloadManager::UpdateAppIcon() { | 916 void DownloadManager::UpdateAppIcon() { |
895 if (status_updater_) | 917 if (status_updater_) |
896 status_updater_->Update(); | 918 status_updater_->Update(); |
897 } | 919 } |
898 | 920 |
899 void DownloadManager::RemoveDownload(int64 download_handle) { | 921 void DownloadManager::RemoveDownload(int64 download_handle) { |
900 DownloadMap::iterator it = history_downloads_.find(download_handle); | 922 DownloadMap::iterator it = history_downloads_.find(download_handle); |
901 if (it == history_downloads_.end()) | 923 if (it == history_downloads_.end()) |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1343 observed_download_manager_->RemoveObserver(this); | 1365 observed_download_manager_->RemoveObserver(this); |
1344 } | 1366 } |
1345 | 1367 |
1346 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1368 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
1347 observing_download_manager_->NotifyModelChanged(); | 1369 observing_download_manager_->NotifyModelChanged(); |
1348 } | 1370 } |
1349 | 1371 |
1350 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1372 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
1351 observed_download_manager_ = NULL; | 1373 observed_download_manager_ = NULL; |
1352 } | 1374 } |
OLD | NEW |