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/callback.h" | 7 #include "base/callback.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 if (download->save_as()) { | 517 if (download->save_as()) { |
518 // We must ask the user for the place to put the download. | 518 // We must ask the user for the place to put the download. |
519 if (!select_file_dialog_.get()) | 519 if (!select_file_dialog_.get()) |
520 select_file_dialog_ = SelectFileDialog::Create(this); | 520 select_file_dialog_ = SelectFileDialog::Create(this); |
521 | 521 |
522 DownloadRequestHandle request_handle = download->request_handle(); | 522 DownloadRequestHandle request_handle = download->request_handle(); |
523 TabContents* contents = request_handle.GetTabContents(); | 523 TabContents* contents = request_handle.GetTabContents(); |
524 SelectFileDialog::FileTypeInfo file_type_info; | 524 SelectFileDialog::FileTypeInfo file_type_info; |
525 FilePath::StringType extension = suggested_path.Extension(); | 525 FilePath::StringType extension = suggested_path.Extension(); |
526 if (!extension.empty()) { | 526 if (!extension.empty()) { |
527 extension.erase(extension.begin()); // drop the . | 527 extension.erase(extension.begin()); // drop the . |
achuithb
2011/06/20 22:52:03
fix lint error.
| |
528 file_type_info.extensions.resize(1); | 528 file_type_info.extensions.resize(1); |
529 file_type_info.extensions[0].push_back(extension); | 529 file_type_info.extensions[0].push_back(extension); |
530 } | 530 } |
531 file_type_info.include_all_files = true; | 531 file_type_info.include_all_files = true; |
532 gfx::NativeWindow owning_window = | 532 gfx::NativeWindow owning_window = |
533 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL; | 533 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL; |
534 // |id_ptr| will be deleted in either FileSelected() or | 534 // |id_ptr| will be deleted in either FileSelected() or |
535 // FileSelectionCancelled(). | 535 // FileSelectionCancelled(). |
536 int32* id_ptr = new int32; | 536 int32* id_ptr = new int32; |
537 *id_ptr = download_id; | 537 *id_ptr = download_id; |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
965 int DownloadManager::RemoveAllDownloads() { | 965 int DownloadManager::RemoveAllDownloads() { |
966 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) { | 966 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) { |
967 // This is an incognito downloader. Clear All should clear main download | 967 // This is an incognito downloader. Clear All should clear main download |
968 // manager as well. | 968 // manager as well. |
969 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads(); | 969 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads(); |
970 } | 970 } |
971 // The null times make the date range unbounded. | 971 // The null times make the date range unbounded. |
972 return RemoveDownloadsBetween(base::Time(), base::Time()); | 972 return RemoveDownloadsBetween(base::Time(), base::Time()); |
973 } | 973 } |
974 | 974 |
975 void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download_item) { | 975 void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download) { |
achuithb
2011/06/20 22:52:03
rename for consistency.
| |
976 #if !defined(NDEBUG) | 976 #if !defined(NDEBUG) |
977 save_page_as_downloads_.insert(download_item); | 977 save_page_as_downloads_.insert(download); |
978 #endif | 978 #endif |
979 downloads_.insert(download_item); | 979 downloads_.insert(download); |
980 #if defined(OS_CHROMEOS) | |
981 // Add to history and notify the cros download panel. | |
982 AddDownloadItemToHistory(download, DownloadHistory::kUninitializedHandle); | |
983 NotifyModelChanged(); | |
984 #endif | |
980 } | 985 } |
981 | 986 |
982 // Initiate a download of a specific URL. We send the request to the | 987 // Initiate a download of a specific URL. We send the request to the |
983 // ResourceDispatcherHost, and let it send us responses like a regular | 988 // ResourceDispatcherHost, and let it send us responses like a regular |
984 // download. | 989 // download. |
985 void DownloadManager::DownloadUrl(const GURL& url, | 990 void DownloadManager::DownloadUrl(const GURL& url, |
986 const GURL& referrer, | 991 const GURL& referrer, |
987 const std::string& referrer_charset, | 992 const std::string& referrer_charset, |
988 TabContents* tab_contents) { | 993 TabContents* tab_contents) { |
989 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(), | 994 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(), |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1153 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); | 1158 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
1154 downloads_.insert(download); | 1159 downloads_.insert(download); |
1155 history_downloads_[download->db_handle()] = download; | 1160 history_downloads_[download->db_handle()] = download; |
1156 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 1161 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
1157 << " download = " << download->DebugString(true); | 1162 << " download = " << download->DebugString(true); |
1158 } | 1163 } |
1159 NotifyModelChanged(); | 1164 NotifyModelChanged(); |
1160 CheckForHistoryFilesRemoval(); | 1165 CheckForHistoryFilesRemoval(); |
1161 } | 1166 } |
1162 | 1167 |
1163 // Once the new DownloadItem's creation info has been committed to the history | 1168 void DownloadManager::AddDownloadItemToHistory(DownloadItem* download, |
1164 // service, we associate the DownloadItem with the db handle, update our | 1169 int64 db_handle) { |
1165 // 'history_downloads_' map and inform observers. | |
1166 void DownloadManager::OnCreateDownloadEntryComplete(int32 download_id, | |
1167 int64 db_handle) { | |
1168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1169 DownloadItem* download = GetActiveDownloadItem(download_id); | |
1170 if (!download) | |
1171 return; | |
1172 | |
1173 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle | |
1174 << " download_id = " << download_id | |
1175 << " download = " << download->DebugString(true); | |
1176 | 1171 |
1177 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 1172 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
1178 // call this function with an invalid |db_handle|. For instance, this can | 1173 // call this function with an invalid |db_handle|. For instance, this can |
1179 // happen when the history database is offline. We cannot have multiple | 1174 // happen when the history database is offline. We cannot have multiple |
1180 // DownloadItems with the same invalid db_handle, so we need to assign a | 1175 // DownloadItems with the same invalid db_handle, so we need to assign a |
1181 // unique |db_handle| here. | 1176 // unique |db_handle| here. |
1182 if (db_handle == DownloadHistory::kUninitializedHandle) | 1177 if (db_handle == DownloadHistory::kUninitializedHandle) |
1183 db_handle = download_history_->GetNextFakeDbHandle(); | 1178 db_handle = download_history_->GetNextFakeDbHandle(); |
1184 | 1179 |
1185 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); | 1180 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); |
1186 download->set_db_handle(db_handle); | 1181 download->set_db_handle(db_handle); |
1187 | 1182 |
1188 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); | 1183 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
1189 history_downloads_[download->db_handle()] = download; | 1184 history_downloads_[download->db_handle()] = download; |
1185 } | |
1186 | |
1187 // Once the new DownloadItem's creation info has been committed to the history | |
1188 // service, we associate the DownloadItem with the db handle, update our | |
1189 // 'history_downloads_' map and inform observers. | |
1190 void DownloadManager::OnCreateDownloadEntryComplete(int32 download_id, | |
1191 int64 db_handle) { | |
1192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1193 DownloadItem* download = GetActiveDownloadItem(download_id); | |
1194 if (!download) | |
1195 return; | |
1196 | |
1197 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle | |
1198 << " download_id = " << download_id | |
1199 << " download = " << download->DebugString(true); | |
1200 | |
1201 AddDownloadItemToHistory(download, db_handle); | |
1190 | 1202 |
1191 // Show in the appropriate browser UI. | 1203 // Show in the appropriate browser UI. |
1192 // This includes buttons to save or cancel, for a dangerous download. | 1204 // This includes buttons to save or cancel, for a dangerous download. |
1193 ShowDownloadInBrowser(download); | 1205 ShowDownloadInBrowser(download); |
1194 | 1206 |
1195 // Inform interested objects about the new download. | 1207 // Inform interested objects about the new download. |
1196 NotifyModelChanged(); | 1208 NotifyModelChanged(); |
1197 | 1209 |
1198 // If the download is still in progress, try to complete it. | 1210 // If the download is still in progress, try to complete it. |
1199 // | 1211 // |
1200 // Otherwise, download has been cancelled or interrupted before we've | 1212 // Otherwise, download has been cancelled or interrupted before we've |
1201 // received the DB handle. We post one final message to the history | 1213 // received the DB handle. We post one final message to the history |
1202 // service so that it can be properly in sync with the DownloadItem's | 1214 // service so that it can be properly in sync with the DownloadItem's |
1203 // completion status, and also inform any observers so that they get | 1215 // completion status, and also inform any observers so that they get |
1204 // more than just the start notification. | 1216 // more than just the start notification. |
1205 if (download->IsInProgress()) { | 1217 if (download->IsInProgress()) { |
1206 MaybeCompleteDownload(download); | 1218 MaybeCompleteDownload(download); |
1207 } else { | 1219 } else { |
1208 DCHECK(download->IsCancelled()) | 1220 DCHECK(download->IsCancelled()) |
1209 << " download = " << download->DebugString(true); | 1221 << " download = " << download->DebugString(true); |
1210 in_progress_.erase(download_id); | 1222 in_progress_.erase(download_id); |
1211 active_downloads_.erase(download_id); | 1223 active_downloads_.erase(download_id); |
1212 download_history_->UpdateEntry(download); | 1224 download_history_->UpdateEntry(download); |
1213 download->UpdateObservers(); | 1225 download->UpdateObservers(); |
1214 } | 1226 } |
1215 } | 1227 } |
1216 | 1228 |
1217 void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) { | 1229 void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) { |
1218 | |
1219 // The 'contents' may no longer exist if the user closed the tab before we | 1230 // The 'contents' may no longer exist if the user closed the tab before we |
achuithb
2011/06/20 22:52:03
fix lint error
| |
1220 // get this start completion event. If it does, tell the origin TabContents | 1231 // get this start completion event. If it does, tell the origin TabContents |
1221 // to display its download shelf. | 1232 // to display its download shelf. |
1222 DownloadRequestHandle request_handle = download->request_handle(); | 1233 DownloadRequestHandle request_handle = download->request_handle(); |
1223 TabContents* contents = request_handle.GetTabContents(); | 1234 TabContents* contents = request_handle.GetTabContents(); |
1224 TabContentsWrapper* wrapper = NULL; | 1235 TabContentsWrapper* wrapper = NULL; |
1225 if (contents) | 1236 if (contents) |
1226 wrapper = TabContentsWrapper::GetCurrentWrapperForContents(contents); | 1237 wrapper = TabContentsWrapper::GetCurrentWrapperForContents(contents); |
1227 | 1238 |
1228 // If the contents no longer exists, we start the download in the last active | 1239 // If the contents no longer exists, we start the download in the last active |
1229 // browser. This is not ideal but better than fully hiding the download from | 1240 // browser. This is not ideal but better than fully hiding the download from |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1334 observed_download_manager_->RemoveObserver(this); | 1345 observed_download_manager_->RemoveObserver(this); |
1335 } | 1346 } |
1336 | 1347 |
1337 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1348 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
1338 observing_download_manager_->NotifyModelChanged(); | 1349 observing_download_manager_->NotifyModelChanged(); |
1339 } | 1350 } |
1340 | 1351 |
1341 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1352 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
1342 observed_download_manager_ = NULL; | 1353 observed_download_manager_ = NULL; |
1343 } | 1354 } |
OLD | NEW |