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

Side by Side Diff: content/browser/download/download_manager_impl.cc

Issue 10344024: Rewrite download manager unit to be actual unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Incorporated John's comment. Created 8 years, 6 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/download/download_manager_impl.h" 5 #include "content/browser/download/download_manager_impl.h"
6 6
7 #include <iterator> 7 #include <iterator>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 129
130 void EnsureNoPendingDownloadJobsOnIO(bool* result) { 130 void EnsureNoPendingDownloadJobsOnIO(bool* result) {
131 scoped_refptr<DownloadFileManager> download_file_manager = 131 scoped_refptr<DownloadFileManager> download_file_manager =
132 ResourceDispatcherHostImpl::Get()->download_file_manager(); 132 ResourceDispatcherHostImpl::Get()->download_file_manager();
133 BrowserThread::PostTask( 133 BrowserThread::PostTask(
134 BrowserThread::FILE, FROM_HERE, 134 BrowserThread::FILE, FROM_HERE,
135 base::Bind(&EnsureNoPendingDownloadsOnFile, 135 base::Bind(&EnsureNoPendingDownloadsOnFile,
136 download_file_manager, result)); 136 download_file_manager, result));
137 } 137 }
138 138
139 class DownloadItemFactoryImpl : public content::DownloadItemFactory {
140 public:
141 DownloadItemFactoryImpl() {}
142 virtual ~DownloadItemFactoryImpl() {}
143
144 virtual content::DownloadItem* CreatePersistedItem(
145 DownloadItemImpl::Delegate* delegate,
146 content::DownloadId download_id,
147 const content::DownloadPersistentStoreInfo& info,
148 const net::BoundNetLog& bound_net_log) OVERRIDE;
149
150 virtual content::DownloadItem* CreateActiveItem(
151 DownloadItemImpl::Delegate* delegate,
152 const DownloadCreateInfo& info,
153 DownloadRequestHandleInterface* request_handle,
154 bool is_otr,
155 const net::BoundNetLog& bound_net_log) OVERRIDE;
156
157 virtual content::DownloadItem* CreateSavePageItem(
158 DownloadItemImpl::Delegate* delegate,
159 const FilePath& path,
160 const GURL& url,
161 bool is_otr,
162 content::DownloadId download_id,
163 const std::string& mime_type,
164 const net::BoundNetLog& bound_net_log) OVERRIDE;
165 };
166
167
168 content::DownloadItem* DownloadItemFactoryImpl::CreatePersistedItem(
jam 2012/06/07 21:35:41 this is personal style so feel free to ignore, but
Randy Smith (Not in Mondays) 2012/06/07 23:27:21 Done. I have a strong preference for fewer lines
169 DownloadItemImpl::Delegate* delegate,
170 content::DownloadId download_id,
171 const content::DownloadPersistentStoreInfo& info,
172 const net::BoundNetLog& bound_net_log) {
173 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
174 }
175
176 content::DownloadItem* DownloadItemFactoryImpl::CreateActiveItem(
177 DownloadItemImpl::Delegate* delegate,
178 const DownloadCreateInfo& info,
179 DownloadRequestHandleInterface* request_handle,
180 bool is_otr,
181 const net::BoundNetLog& bound_net_log) {
182 return new DownloadItemImpl(delegate, info, request_handle, is_otr,
183 bound_net_log);
184 }
185
186 content::DownloadItem* DownloadItemFactoryImpl::CreateSavePageItem(
187 DownloadItemImpl::Delegate* delegate,
188 const FilePath& path,
189 const GURL& url,
190 bool is_otr,
191 content::DownloadId download_id,
192 const std::string& mime_type,
193 const net::BoundNetLog& bound_net_log) {
194 return new DownloadItemImpl(delegate, path, url, is_otr, download_id,
195 mime_type, bound_net_log);
196 }
197
139 } // namespace 198 } // namespace
140 199
141 namespace content { 200 namespace content {
142 201
143 // static 202 // static
144 DownloadManager* DownloadManager::Create( 203 DownloadManager* DownloadManager::Create(
145 content::DownloadManagerDelegate* delegate, 204 content::DownloadManagerDelegate* delegate,
146 net::NetLog* net_log) { 205 net::NetLog* net_log) {
147 return new DownloadManagerImpl(delegate, net_log); 206 // Find the DownloadFileManager for use when creating the DownloadManager.
207 // Note that unit tests should construct based on the underlying
208 // constructor, so ResourceDispatcherHostImpl & DownloadFileManager
209 // should always be present when using this code.
210 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
211 DownloadFileManager* file_manager = NULL;
212 DCHECK(rdh);
213 file_manager = rdh->download_file_manager();
214 DCHECK(file_manager);
215
216 return new DownloadManagerImpl(delegate, file_manager, NULL, net_log);
148 } 217 }
149 218
150 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { 219 bool DownloadManager::EnsureNoPendingDownloadsForTesting() {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
152 bool result = true; 221 bool result = true;
153 BrowserThread::PostTask( 222 BrowserThread::PostTask(
154 BrowserThread::IO, FROM_HERE, 223 BrowserThread::IO, FROM_HERE,
155 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); 224 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result));
156 MessageLoop::current()->Run(); 225 MessageLoop::current()->Run();
157 return result; 226 return result;
158 } 227 }
159 228
160 } // namespace content 229 } // namespace content
161 230
162 DownloadManagerImpl::DownloadManagerImpl( 231 DownloadManagerImpl::DownloadManagerImpl(
163 content::DownloadManagerDelegate* delegate, 232 content::DownloadManagerDelegate* delegate,
233 DownloadFileManager* file_manager,
234 content::DownloadItemFactory *factory,
164 net::NetLog* net_log) 235 net::NetLog* net_log)
165 : shutdown_needed_(false), 236 : factory_(factory),
166 browser_context_(NULL), 237 shutdown_needed_(false),
167 file_manager_(NULL), 238 browser_context_(NULL),
168 delegate_(delegate), 239 file_manager_(file_manager),
169 net_log_(net_log) { 240 delegate_(delegate),
241 net_log_(net_log) {
242 if (!factory_.get())
243 factory_.reset(new DownloadItemFactoryImpl());
170 } 244 }
171 245
172 DownloadManagerImpl::~DownloadManagerImpl() { 246 DownloadManagerImpl::~DownloadManagerImpl() {
173 DCHECK(!shutdown_needed_); 247 DCHECK(!shutdown_needed_);
174 } 248 }
175 249
176 DownloadId DownloadManagerImpl::GetNextId() { 250 DownloadId DownloadManagerImpl::GetNextId() {
177 return delegate_->GetNextId(); 251 return delegate_->GetNextId();
178 } 252 }
179 253
180 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) { 254 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) {
181 return delegate_->ShouldOpenDownload(item); 255 return delegate_->ShouldOpenDownload(item);
182 } 256 }
183 257
184 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { 258 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
185 return delegate_->ShouldOpenFileBasedOnExtension(path); 259 return delegate_->ShouldOpenFileBasedOnExtension(path);
186 } 260 }
187 261
188 void DownloadManagerImpl::Shutdown() { 262 void DownloadManagerImpl::Shutdown() {
189 VLOG(20) << __FUNCTION__ << "()" 263 VLOG(20) << __FUNCTION__ << "()"
190 << " shutdown_needed_ = " << shutdown_needed_; 264 << " shutdown_needed_ = " << shutdown_needed_;
191 if (!shutdown_needed_) 265 if (!shutdown_needed_)
192 return; 266 return;
193 shutdown_needed_ = false; 267 shutdown_needed_ = false;
194 268
195 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); 269 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
196 // TODO(benjhayden): Consider clearing observers_. 270 // TODO(benjhayden): Consider clearing observers_.
197 271
198 if (file_manager_) { 272 DCHECK(file_manager_);
199 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 273 BrowserThread::PostTask(
200 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, 274 BrowserThread::FILE, FROM_HERE,
201 file_manager_, make_scoped_refptr(this))); 275 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
202 } 276 file_manager_, make_scoped_refptr(this)));
203 277
204 AssertContainersConsistent(); 278 AssertContainersConsistent();
205 279
206 // Go through all downloads in downloads_. Dangerous ones we need to 280 // Go through all downloads in downloads_. Dangerous ones we need to
207 // remove on disk, and in progress ones we need to cancel. 281 // remove on disk, and in progress ones we need to cancel.
208 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { 282 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
209 DownloadItem* download = *it; 283 DownloadItem* download = *it;
210 284
211 // Save iterator from potential erases in this set done by called code. 285 // Save iterator from potential erases in this set done by called code.
212 // Iterators after an erasure point are still valid for lists and 286 // Iterators after an erasure point are still valid for lists and
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 // The Incognito Downloads page will get the list of non-Incognito downloads 367 // The Incognito Downloads page will get the list of non-Incognito downloads
294 // from its parent profile. 368 // from its parent profile.
295 if (browser_context_->IsOffTheRecord() != download_item->IsOtr()) 369 if (browser_context_->IsOffTheRecord() != download_item->IsOtr())
296 continue; 370 continue;
297 371
298 if (download_item->MatchesQuery(query_lower)) 372 if (download_item->MatchesQuery(query_lower))
299 result->push_back(download_item); 373 result->push_back(download_item);
300 } 374 }
301 } 375 }
302 376
303 // Query the history service for information about all persisted downloads.
304 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { 377 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
305 DCHECK(browser_context); 378 DCHECK(browser_context);
306 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; 379 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
307 shutdown_needed_ = true; 380 shutdown_needed_ = true;
308 381
309 browser_context_ = browser_context; 382 browser_context_ = browser_context;
310 383
311 // In test mode, there may be no ResourceDispatcherHostImpl. In this case
312 // it's safe to avoid setting |file_manager_| because we only call a small
313 // set of functions, none of which need it.
314 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
315 if (rdh) {
316 file_manager_ = rdh->download_file_manager();
317 DCHECK(file_manager_);
318 }
319
320 return true; 384 return true;
321 } 385 }
322 386
323 // We have received a message from DownloadFileManager about a new download. 387 // We have received a message from DownloadFileManager about a new download.
324 void DownloadManagerImpl::StartDownload(int32 download_id) { 388 void DownloadManagerImpl::StartDownload(int32 download_id) {
325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
326 390
327 if (delegate_->ShouldStartDownload(download_id)) 391 if (delegate_->ShouldStartDownload(download_id))
328 RestartDownload(download_id); 392 RestartDownload(download_id);
329 } 393 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 FilePath DownloadManagerImpl::LastDownloadPath() { 465 FilePath DownloadManagerImpl::LastDownloadPath() {
402 return last_download_path_; 466 return last_download_path_;
403 } 467 }
404 468
405 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( 469 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem(
406 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { 470 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) {
407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 471 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
408 472
409 net::BoundNetLog bound_net_log = 473 net::BoundNetLog bound_net_log =
410 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); 474 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
411 DownloadItem* download = new DownloadItemImpl( 475 DownloadItem* download = factory_->CreateActiveItem(
412 this, *info, new DownloadRequestHandle(request_handle), 476 this, *info, new DownloadRequestHandle(request_handle),
413 browser_context_->IsOffTheRecord(), bound_net_log); 477 browser_context_->IsOffTheRecord(), bound_net_log);
414 int32 download_id = info->download_id.local(); 478 int32 download_id = info->download_id.local();
415 479
416 DCHECK(!ContainsKey(active_downloads_, download_id)); 480 DCHECK(!ContainsKey(active_downloads_, download_id));
417 downloads_.insert(download); 481 downloads_.insert(download);
418 active_downloads_[download_id] = download; 482 active_downloads_[download_id] = download;
419 483
420 return bound_net_log; 484 return bound_net_log;
421 } 485 }
422 486
423 DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem( 487 DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem(
424 const FilePath& main_file_path, 488 const FilePath& main_file_path,
425 const GURL& page_url, 489 const GURL& page_url,
426 bool is_otr, 490 bool is_otr,
427 const std::string& mime_type, 491 const std::string& mime_type,
428 DownloadItem::Observer* observer) { 492 DownloadItem::Observer* observer) {
429 net::BoundNetLog bound_net_log = 493 net::BoundNetLog bound_net_log =
430 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); 494 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
431 DownloadItem* download = new DownloadItemImpl( 495 DownloadItem* download = factory_->CreateSavePageItem(
432 this, 496 this,
433 main_file_path, 497 main_file_path,
434 page_url, 498 page_url,
435 is_otr, 499 is_otr,
436 GetNextId(), 500 GetNextId(),
437 mime_type, 501 mime_type,
438 bound_net_log); 502 bound_net_log);
439 503
440 download->AddObserver(observer); 504 download->AddObserver(observer);
441 505
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 710 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
647 711
648 VLOG(20) << __FUNCTION__ << "()" 712 VLOG(20) << __FUNCTION__ << "()"
649 << " download = " << download->DebugString(true); 713 << " download = " << download->DebugString(true);
650 714
651 RemoveFromActiveList(download); 715 RemoveFromActiveList(download);
652 // This function is called from the DownloadItem, so DI state 716 // This function is called from the DownloadItem, so DI state
653 // should already have been updated. 717 // should already have been updated.
654 AssertStateConsistent(download); 718 AssertStateConsistent(download);
655 719
656 if (file_manager_) 720 DCHECK(file_manager_);
657 download->OffThreadCancel(file_manager_); 721 download->OffThreadCancel(file_manager_);
658 } 722 }
659 723
660 void DownloadManagerImpl::OnDownloadInterrupted( 724 void DownloadManagerImpl::OnDownloadInterrupted(
661 int32 download_id, 725 int32 download_id,
662 int64 size, 726 int64 size,
663 const std::string& hash_state, 727 const std::string& hash_state,
664 content::DownloadInterruptReason reason) { 728 content::DownloadInterruptReason reason) {
665 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 729 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
666 730
667 DownloadItem* download = GetActiveDownload(download_id); 731 DownloadItem* download = GetActiveDownload(download_id);
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). 916 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
853 void DownloadManagerImpl::OnPersistentStoreQueryComplete( 917 void DownloadManagerImpl::OnPersistentStoreQueryComplete(
854 std::vector<DownloadPersistentStoreInfo>* entries) { 918 std::vector<DownloadPersistentStoreInfo>* entries) {
855 for (size_t i = 0; i < entries->size(); ++i) { 919 for (size_t i = 0; i < entries->size(); ++i) {
856 int64 db_handle = entries->at(i).db_handle; 920 int64 db_handle = entries->at(i).db_handle;
857 base::debug::Alias(&db_handle); 921 base::debug::Alias(&db_handle);
858 DCHECK(!ContainsKey(history_downloads_, db_handle)); 922 DCHECK(!ContainsKey(history_downloads_, db_handle));
859 923
860 net::BoundNetLog bound_net_log = 924 net::BoundNetLog bound_net_log =
861 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); 925 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
862 DownloadItem* download = new DownloadItemImpl( 926 DownloadItem* download = factory_->CreatePersistedItem(
863 this, GetNextId(), entries->at(i), bound_net_log); 927 this, GetNextId(), entries->at(i), bound_net_log);
864 downloads_.insert(download); 928 downloads_.insert(download);
865 history_downloads_[download->GetDbHandle()] = download; 929 history_downloads_[download->GetDbHandle()] = download;
866 VLOG(20) << __FUNCTION__ << "()" << i << ">" 930 VLOG(20) << __FUNCTION__ << "()" << i << ">"
867 << " download = " << download->DebugString(true); 931 << " download = " << download->DebugString(true);
868 } 932 }
869 NotifyModelChanged(); 933 NotifyModelChanged();
870 CheckForHistoryFilesRemoval(); 934 CheckForHistoryFilesRemoval();
871 } 935 }
872 936
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 } 1180 }
1117 1181
1118 void DownloadManagerImpl::DownloadRenamedToFinalName( 1182 void DownloadManagerImpl::DownloadRenamedToFinalName(
1119 DownloadItem* download) { 1183 DownloadItem* download) {
1120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1121 // If the rename failed, we receive an OnDownloadInterrupted() call before we 1185 // If the rename failed, we receive an OnDownloadInterrupted() call before we
1122 // receive the DownloadRenamedToFinalName() call. 1186 // receive the DownloadRenamedToFinalName() call.
1123 delegate_->UpdatePathForItemInPersistentStore(download, 1187 delegate_->UpdatePathForItemInPersistentStore(download,
1124 download->GetFullPath()); 1188 download->GetFullPath());
1125 } 1189 }
1126
1127 void DownloadManagerImpl::SetFileManagerForTesting(
1128 DownloadFileManager* file_manager) {
1129 file_manager_ = file_manager;
1130 }
OLDNEW
« no previous file with comments | « content/browser/download/download_manager_impl.h ('k') | content/browser/download/download_manager_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698