Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/browser/download/download_manager.cc

Issue 7212017: Add save package download items to history. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 .
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 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 int DownloadManager::RemoveAllDownloads() { 968 int DownloadManager::RemoveAllDownloads() {
969 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) { 969 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) {
970 // This is an incognito downloader. Clear All should clear main download 970 // This is an incognito downloader. Clear All should clear main download
971 // manager as well. 971 // manager as well.
972 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads(); 972 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads();
973 } 973 }
974 // The null times make the date range unbounded. 974 // The null times make the date range unbounded.
975 return RemoveDownloadsBetween(base::Time(), base::Time()); 975 return RemoveDownloadsBetween(base::Time(), base::Time());
976 } 976 }
977 977
978 void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download_item) { 978 void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download) {
979 #if !defined(NDEBUG) 979 #if !defined(NDEBUG)
980 save_page_as_downloads_.insert(download_item); 980 save_page_as_downloads_.insert(download);
981 #endif 981 #endif
982 downloads_.insert(download_item); 982 downloads_.insert(download);
983 // Add to history and notify observers.
984 AddDownloadItemToHistory(download, DownloadHistory::kUninitializedHandle);
985 NotifyModelChanged();
983 } 986 }
984 987
985 // Initiate a download of a specific URL. We send the request to the 988 // Initiate a download of a specific URL. We send the request to the
986 // ResourceDispatcherHost, and let it send us responses like a regular 989 // ResourceDispatcherHost, and let it send us responses like a regular
987 // download. 990 // download.
988 void DownloadManager::DownloadUrl(const GURL& url, 991 void DownloadManager::DownloadUrl(const GURL& url,
989 const GURL& referrer, 992 const GURL& referrer,
990 const std::string& referrer_charset, 993 const std::string& referrer_charset,
991 TabContents* tab_contents) { 994 TabContents* tab_contents) {
992 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(), 995 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
1156 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); 1159 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
1157 downloads_.insert(download); 1160 downloads_.insert(download);
1158 history_downloads_[download->db_handle()] = download; 1161 history_downloads_[download->db_handle()] = download;
1159 VLOG(20) << __FUNCTION__ << "()" << i << ">" 1162 VLOG(20) << __FUNCTION__ << "()" << i << ">"
1160 << " download = " << download->DebugString(true); 1163 << " download = " << download->DebugString(true);
1161 } 1164 }
1162 NotifyModelChanged(); 1165 NotifyModelChanged();
1163 CheckForHistoryFilesRemoval(); 1166 CheckForHistoryFilesRemoval();
1164 } 1167 }
1165 1168
1166 // Once the new DownloadItem's creation info has been committed to the history 1169 void DownloadManager::AddDownloadItemToHistory(DownloadItem* download,
1167 // service, we associate the DownloadItem with the db handle, update our 1170 int64 db_handle) {
1168 // 'history_downloads_' map and inform observers.
1169 void DownloadManager::OnCreateDownloadEntryComplete(int32 download_id,
1170 int64 db_handle) {
1171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1172 DownloadItem* download = GetActiveDownloadItem(download_id);
1173 if (!download)
1174 return;
1175
1176 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
1177 << " download_id = " << download_id
1178 << " download = " << download->DebugString(true);
1179 1172
1180 // It's not immediately obvious, but HistoryBackend::CreateDownload() can 1173 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
1181 // call this function with an invalid |db_handle|. For instance, this can 1174 // call this function with an invalid |db_handle|. For instance, this can
1182 // happen when the history database is offline. We cannot have multiple 1175 // happen when the history database is offline. We cannot have multiple
1183 // DownloadItems with the same invalid db_handle, so we need to assign a 1176 // DownloadItems with the same invalid db_handle, so we need to assign a
1184 // unique |db_handle| here. 1177 // unique |db_handle| here.
1185 if (db_handle == DownloadHistory::kUninitializedHandle) 1178 if (db_handle == DownloadHistory::kUninitializedHandle)
1186 db_handle = download_history_->GetNextFakeDbHandle(); 1179 db_handle = download_history_->GetNextFakeDbHandle();
1187 1180
1188 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508 1181 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508
1189 // is fixed. 1182 // is fixed.
1190 CHECK_NE(DownloadHistory::kUninitializedHandle, db_handle); 1183 CHECK_NE(DownloadHistory::kUninitializedHandle, db_handle);
1191 1184
1192 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); 1185 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle);
1193 download->set_db_handle(db_handle); 1186 download->set_db_handle(db_handle);
1194 1187
1195 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); 1188 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
1196 history_downloads_[download->db_handle()] = download; 1189 history_downloads_[download->db_handle()] = download;
1190 }
1191
1192 // Once the new DownloadItem's creation info has been committed to the history
1193 // service, we associate the DownloadItem with the db handle, update our
1194 // 'history_downloads_' map and inform observers.
1195 void DownloadManager::OnCreateDownloadEntryComplete(int32 download_id,
1196 int64 db_handle) {
1197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1198 DownloadItem* download = GetActiveDownloadItem(download_id);
1199 if (!download)
1200 return;
1201
1202 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
1203 << " download_id = " << download_id
1204 << " download = " << download->DebugString(true);
1205
1206 AddDownloadItemToHistory(download, db_handle);
1197 1207
1198 // Show in the appropriate browser UI. 1208 // Show in the appropriate browser UI.
1199 // This includes buttons to save or cancel, for a dangerous download. 1209 // This includes buttons to save or cancel, for a dangerous download.
1200 ShowDownloadInBrowser(download); 1210 ShowDownloadInBrowser(download);
1201 1211
1202 // Inform interested objects about the new download. 1212 // Inform interested objects about the new download.
1203 NotifyModelChanged(); 1213 NotifyModelChanged();
1204 1214
1205 // If the download is still in progress, try to complete it. 1215 // If the download is still in progress, try to complete it.
1206 // 1216 //
1207 // Otherwise, download has been cancelled or interrupted before we've 1217 // Otherwise, download has been cancelled or interrupted before we've
1208 // received the DB handle. We post one final message to the history 1218 // received the DB handle. We post one final message to the history
1209 // service so that it can be properly in sync with the DownloadItem's 1219 // service so that it can be properly in sync with the DownloadItem's
1210 // completion status, and also inform any observers so that they get 1220 // completion status, and also inform any observers so that they get
1211 // more than just the start notification. 1221 // more than just the start notification.
1212 if (download->IsInProgress()) { 1222 if (download->IsInProgress()) {
1213 MaybeCompleteDownload(download); 1223 MaybeCompleteDownload(download);
1214 } else { 1224 } else {
1215 DCHECK(download->IsCancelled()) 1225 DCHECK(download->IsCancelled())
1216 << " download = " << download->DebugString(true); 1226 << " download = " << download->DebugString(true);
1217 in_progress_.erase(download_id); 1227 in_progress_.erase(download_id);
1218 active_downloads_.erase(download_id); 1228 active_downloads_.erase(download_id);
1219 download_history_->UpdateEntry(download); 1229 download_history_->UpdateEntry(download);
1220 download->UpdateObservers(); 1230 download->UpdateObservers();
1221 } 1231 }
1222 } 1232 }
1223 1233
1224 void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) { 1234 void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) {
1225
1226 // The 'contents' may no longer exist if the user closed the tab before we 1235 // The 'contents' may no longer exist if the user closed the tab before we
1227 // get this start completion event. If it does, tell the origin TabContents 1236 // get this start completion event. If it does, tell the origin TabContents
1228 // to display its download shelf. 1237 // to display its download shelf.
1229 DownloadRequestHandle request_handle = download->request_handle(); 1238 DownloadRequestHandle request_handle = download->request_handle();
1230 TabContents* contents = request_handle.GetTabContents(); 1239 TabContents* contents = request_handle.GetTabContents();
1231 TabContentsWrapper* wrapper = NULL; 1240 TabContentsWrapper* wrapper = NULL;
1232 if (contents) 1241 if (contents)
1233 wrapper = TabContentsWrapper::GetCurrentWrapperForContents(contents); 1242 wrapper = TabContentsWrapper::GetCurrentWrapperForContents(contents);
1234 1243
1235 // If the contents no longer exists, we start the download in the last active 1244 // If the contents no longer exists, we start the download in the last active
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1341 observed_download_manager_->RemoveObserver(this); 1350 observed_download_manager_->RemoveObserver(this);
1342 } 1351 }
1343 1352
1344 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { 1353 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1345 observing_download_manager_->NotifyModelChanged(); 1354 observing_download_manager_->NotifyModelChanged();
1346 } 1355 }
1347 1356
1348 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { 1357 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1349 observed_download_manager_ = NULL; 1358 observed_download_manager_ = NULL;
1350 } 1359 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_manager.h ('k') | chrome/browser/download/save_page_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698