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

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

Issue 7277073: Support for adding save page download items into downloads 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include "grit/theme_resources.h" 48 #include "grit/theme_resources.h"
49 #include "net/base/mime_util.h" 49 #include "net/base/mime_util.h"
50 #include "net/base/net_util.h" 50 #include "net/base/net_util.h"
51 #include "ui/base/l10n/l10n_util.h" 51 #include "ui/base/l10n/l10n_util.h"
52 #include "ui/base/resource/resource_bundle.h" 52 #include "ui/base/resource/resource_bundle.h"
53 53
54 DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater) 54 DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater)
55 : shutdown_needed_(false), 55 : shutdown_needed_(false),
56 profile_(NULL), 56 profile_(NULL),
57 file_manager_(NULL), 57 file_manager_(NULL),
58 status_updater_(status_updater->AsWeakPtr()) { 58 status_updater_(status_updater->AsWeakPtr()),
59 next_save_page_id_(0) {
59 if (status_updater_) 60 if (status_updater_)
60 status_updater_->AddDelegate(this); 61 status_updater_->AddDelegate(this);
61 } 62 }
62 63
63 DownloadManager::~DownloadManager() { 64 DownloadManager::~DownloadManager() {
64 DCHECK(!shutdown_needed_); 65 DCHECK(!shutdown_needed_);
65 if (status_updater_) 66 if (status_updater_)
66 status_updater_->RemoveDelegate(this); 67 status_updater_->RemoveDelegate(this);
67 } 68 }
68 69
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 // and all in progress downloads have been cancelled. We can now delete 116 // and all in progress downloads have been cancelled. We can now delete
116 // anything left. 117 // anything left.
117 118
118 // Copy downloads_ to separate container so as not to set off checks 119 // Copy downloads_ to separate container so as not to set off checks
119 // in DownloadItem destruction. 120 // in DownloadItem destruction.
120 DownloadSet downloads_to_delete; 121 DownloadSet downloads_to_delete;
121 downloads_to_delete.swap(downloads_); 122 downloads_to_delete.swap(downloads_);
122 123
123 in_progress_.clear(); 124 in_progress_.clear();
124 active_downloads_.clear(); 125 active_downloads_.clear();
126 save_page_downloads_.clear();
Randy Smith (Not in Mondays) 2011/07/15 17:58:39 If I'm reading the protocol right, save_page_downl
achuithb 2011/07/16 20:15:32 Didn't know that. I've deleted the line. Do you th
Randy Smith (Not in Mondays) 2011/07/17 18:25:47 Yes, if you would.
achuithb 2011/07/20 03:32:21 Done.
125 history_downloads_.clear(); 127 history_downloads_.clear();
126 #if !defined(NDEBUG) 128 #if !defined(NDEBUG)
127 save_page_as_downloads_.clear(); 129 save_page_as_downloads_.clear();
128 #endif 130 #endif
129 STLDeleteElements(&downloads_to_delete); 131 STLDeleteElements(&downloads_to_delete);
130 132
131 file_manager_ = NULL; 133 file_manager_ = NULL;
132 134
133 // Make sure the save as dialog doesn't notify us back if we're gone before 135 // Make sure the save as dialog doesn't notify us back if we're gone before
134 // it returns. 136 // it returns.
(...skipping 843 matching lines...) Expand 10 before | Expand all | Expand 10 after
978 int DownloadManager::RemoveAllDownloads() { 980 int DownloadManager::RemoveAllDownloads() {
979 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) { 981 if (this != profile_->GetOriginalProfile()->GetDownloadManager()) {
980 // This is an incognito downloader. Clear All should clear main download 982 // This is an incognito downloader. Clear All should clear main download
981 // manager as well. 983 // manager as well.
982 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads(); 984 profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads();
983 } 985 }
984 // The null times make the date range unbounded. 986 // The null times make the date range unbounded.
985 return RemoveDownloadsBetween(base::Time(), base::Time()); 987 return RemoveDownloadsBetween(base::Time(), base::Time());
986 } 988 }
987 989
988 void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download) { 990 void DownloadManager::SavePageDownloadStarted(DownloadItem* download) {
989 #if !defined(NDEBUG) 991 #if !defined(NDEBUG)
990 save_page_as_downloads_.insert(download); 992 save_page_as_downloads_.insert(download);
991 #endif 993 #endif
994 DCHECK(!ContainsKey(save_page_downloads_, download->id()));
992 downloads_.insert(download); 995 downloads_.insert(download);
993 // Add to history and notify observers. 996 save_page_downloads_[download->id()] = download;
994 AddDownloadItemToHistory(download, DownloadHistory::kUninitializedHandle); 997
995 NotifyModelChanged(); 998 // Add this entry to the history service.
999 // Additionally, the UI is notified in the callback.
1000 download_history_->AddEntry(download,
1001 NewCallback(this, &DownloadManager::OnSavePageDownloadEntryAdded));
996 } 1002 }
997 1003
998 // Initiate a download of a specific URL. We send the request to the 1004 // Initiate a download of a specific URL. We send the request to the
999 // ResourceDispatcherHost, and let it send us responses like a regular 1005 // ResourceDispatcherHost, and let it send us responses like a regular
1000 // download. 1006 // download.
1001 void DownloadManager::DownloadUrl(const GURL& url, 1007 void DownloadManager::DownloadUrl(const GURL& url,
1002 const GURL& referrer, 1008 const GURL& referrer,
1003 const std::string& referrer_charset, 1009 const std::string& referrer_charset,
1004 TabContents* tab_contents) { 1010 TabContents* tab_contents) {
1005 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(), 1011 DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(),
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 1196
1191 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508 1197 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/84508
1192 // is fixed. 1198 // is fixed.
1193 CHECK_NE(DownloadHistory::kUninitializedHandle, db_handle); 1199 CHECK_NE(DownloadHistory::kUninitializedHandle, db_handle);
1194 1200
1195 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); 1201 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle);
1196 download->set_db_handle(db_handle); 1202 download->set_db_handle(db_handle);
1197 1203
1198 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); 1204 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
1199 history_downloads_[download->db_handle()] = download; 1205 history_downloads_[download->db_handle()] = download;
1206
1207 // Show in the appropriate browser UI.
1208 // This includes buttons to save or cancel, for a dangerous download.
1209 ShowDownloadInBrowser(download);
1210
1211 // Inform interested objects about the new download.
1212 NotifyModelChanged();
1200 } 1213 }
1201 1214
1202 // Once the new DownloadItem's creation info has been committed to the history 1215 // Once the new DownloadItem's creation info has been committed to the history
1203 // service, we associate the DownloadItem with the db handle, update our 1216 // service, we associate the DownloadItem with the db handle, update our
1204 // 'history_downloads_' map and inform observers. 1217 // 'history_downloads_' map and inform observers.
1205 void DownloadManager::OnCreateDownloadEntryComplete(int32 download_id, 1218 void DownloadManager::OnCreateDownloadEntryComplete(int32 download_id,
1206 int64 db_handle) { 1219 int64 db_handle) {
1207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1208 DownloadItem* download = GetActiveDownloadItem(download_id); 1221 DownloadItem* download = GetActiveDownloadItem(download_id);
1209 if (!download) 1222 if (!download)
1210 return; 1223 return;
1211 1224
1212 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle 1225 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
1213 << " download_id = " << download_id 1226 << " download_id = " << download_id
1214 << " download = " << download->DebugString(true); 1227 << " download = " << download->DebugString(true);
1215 1228
1216 AddDownloadItemToHistory(download, db_handle); 1229 AddDownloadItemToHistory(download, db_handle);
1217 1230
1218 // Show in the appropriate browser UI.
1219 // This includes buttons to save or cancel, for a dangerous download.
1220 ShowDownloadInBrowser(download);
1221
1222 // Inform interested objects about the new download.
1223 NotifyModelChanged();
1224
1225 // If the download is still in progress, try to complete it. 1231 // If the download is still in progress, try to complete it.
1226 // 1232 //
1227 // Otherwise, download has been cancelled or interrupted before we've 1233 // Otherwise, download has been cancelled or interrupted before we've
1228 // received the DB handle. We post one final message to the history 1234 // received the DB handle. We post one final message to the history
1229 // service so that it can be properly in sync with the DownloadItem's 1235 // service so that it can be properly in sync with the DownloadItem's
1230 // completion status, and also inform any observers so that they get 1236 // completion status, and also inform any observers so that they get
1231 // more than just the start notification. 1237 // more than just the start notification.
1232 if (download->IsInProgress()) { 1238 if (download->IsInProgress()) {
1233 MaybeCompleteDownload(download); 1239 MaybeCompleteDownload(download);
1234 } else { 1240 } else {
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1360 observed_download_manager_->RemoveObserver(this); 1366 observed_download_manager_->RemoveObserver(this);
1361 } 1367 }
1362 1368
1363 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { 1369 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1364 observing_download_manager_->NotifyModelChanged(); 1370 observing_download_manager_->NotifyModelChanged();
1365 } 1371 }
1366 1372
1367 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { 1373 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1368 observed_download_manager_ = NULL; 1374 observed_download_manager_ = NULL;
1369 } 1375 }
1376
1377 void DownloadManager::OnSavePageDownloadEntryAdded(int32 download_id,
1378 int64 db_handle) {
1379 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1380
1381 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
1382 DCHECK(it != save_page_downloads_.end());
Randy Smith (Not in Mondays) 2011/07/15 17:58:39 I believe this DCHECK is inaccurate--specifically,
achuithb 2011/07/16 20:15:32 The item remains in save_page_downloads_ until bot
Randy Smith (Not in Mondays) 2011/07/17 18:25:47 Whoops, I'm sorry; I hadn't tracked that part of t
achuithb 2011/07/20 03:32:21 Ok, I've done what you suggest. I was following th
1383 if (it == save_page_downloads_.end())
1384 return;
1385
1386 DownloadItem* download = it->second;
1387 DCHECK(download);
1388 if (!download)
1389 return;
1390
1391 AddDownloadItemToHistory(download, db_handle);
1392
1393 // The download may have completed before we received the DB handle.
Randy Smith (Not in Mondays) 2011/07/15 17:58:39 Not requesting a change, just registering a disqui
achuithb 2011/07/16 20:15:32 I've been thinking about this, and no elegant solu
Randy Smith (Not in Mondays) 2011/07/17 18:25:47 Nope--else I would have. I came to the same concl
1394 if (!download->IsInProgress())
1395 SavePageDownloadFinished(download);
1396 }
1397
1398 void DownloadManager::SavePageDownloadFinished(DownloadItem* download) {
1399 if (download->db_handle() > DownloadHistory::kUninitializedHandle) {
1400 download_history_->UpdateEntry(download);
1401 DCHECK(ContainsKey(save_page_downloads_, download->id()));
1402 save_page_downloads_.erase(download->id());
1403 }
1404 }
1405
1406 int32 DownloadManager::GetNextSavePageId() {
benjhayden 2011/07/18 15:37:45 Apologies if this was addressed elsewhere, but how
achuithb 2011/07/18 18:33:18 Not addressed elsewhere. There's a GetNextId in b
benjhayden 2011/07/18 19:25:01 If the multi-thread accessibility is the only issu
achuith 2011/07/18 19:44:38 From reading the CL description, I feel like the i
1407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1408 return next_save_page_id_++;
1409 }
1410
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698