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

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

Issue 148133007: [Downloads] Always call DM::StartDownload() for explicit downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 delegate_->Shutdown(); 349 delegate_->Shutdown();
350 delegate_ = NULL; 350 delegate_ = NULL;
351 } 351 }
352 352
353 void DownloadManagerImpl::StartDownload( 353 void DownloadManagerImpl::StartDownload(
354 scoped_ptr<DownloadCreateInfo> info, 354 scoped_ptr<DownloadCreateInfo> info,
355 scoped_ptr<ByteStreamReader> stream, 355 scoped_ptr<ByteStreamReader> stream,
356 const DownloadUrlParameters::OnStartedCallback& on_started) { 356 const DownloadUrlParameters::OnStartedCallback& on_started) {
357 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 357 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
358 DCHECK(info); 358 DCHECK(info);
359 // |stream| is only non-nil if the download request was successful.
360 DCHECK(info->interrupt_reason != DOWNLOAD_INTERRUPT_REASON_NONE ||
361 stream.get());
359 uint32 download_id = info->download_id; 362 uint32 download_id = info->download_id;
360 const bool new_download = (download_id == content::DownloadItem::kInvalidId); 363 const bool new_download = (download_id == content::DownloadItem::kInvalidId);
361 base::Callback<void(uint32)> got_id(base::Bind( 364 base::Callback<void(uint32)> got_id(base::Bind(
362 &DownloadManagerImpl::StartDownloadWithId, 365 &DownloadManagerImpl::StartDownloadWithId,
363 weak_factory_.GetWeakPtr(), 366 weak_factory_.GetWeakPtr(),
364 base::Passed(info.Pass()), 367 base::Passed(info.Pass()),
365 base::Passed(stream.Pass()), 368 base::Passed(stream.Pass()),
366 on_started, 369 on_started,
367 new_download)); 370 new_download));
368 if (new_download) { 371 if (new_download) {
369 GetNextId(got_id); 372 GetNextId(got_id);
370 } else { 373 } else {
371 got_id.Run(download_id); 374 got_id.Run(download_id);
372 } 375 }
373 } 376 }
374 377
375 void DownloadManagerImpl::StartDownloadWithId( 378 void DownloadManagerImpl::StartDownloadWithId(
376 scoped_ptr<DownloadCreateInfo> info, 379 scoped_ptr<DownloadCreateInfo> info,
377 scoped_ptr<ByteStreamReader> stream, 380 scoped_ptr<ByteStreamReader> byte_stream,
378 const DownloadUrlParameters::OnStartedCallback& on_started, 381 const DownloadUrlParameters::OnStartedCallback& on_started,
379 bool new_download, 382 bool new_download,
380 uint32 id) { 383 uint32 id) {
381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 384 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
382 DCHECK_NE(content::DownloadItem::kInvalidId, id); 385 DCHECK_NE(content::DownloadItem::kInvalidId, id);
383 DownloadItemImpl* download = NULL; 386 DownloadItemImpl* download = NULL;
384 if (new_download) { 387 if (new_download) {
385 download = CreateActiveItem(id, *info); 388 download = CreateActiveItem(id, *info);
386 } else { 389 } else {
387 DownloadMap::iterator item_iterator = downloads_.find(id); 390 DownloadMap::iterator item_iterator = downloads_.find(id);
388 // Trying to resume an interrupted download. 391 // Trying to resume an interrupted download.
389 if (item_iterator == downloads_.end() || 392 if (item_iterator == downloads_.end() ||
390 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) { 393 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) {
391 // If the download is no longer known to the DownloadManager, then it was 394 // If the download is no longer known to the DownloadManager, then it was
392 // removed after it was resumed. Ignore. If the download is cancelled 395 // removed after it was resumed. Ignore. If the download is cancelled
393 // while resuming, then also ignore the request. 396 // while resuming, then also ignore the request.
394 info->request_handle.CancelRequest(); 397 info->request_handle.CancelRequest();
395 if (!on_started.is_null()) 398 if (!on_started.is_null())
396 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); 399 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
397 return; 400 return;
398 } 401 }
399 download = item_iterator->second; 402 download = item_iterator->second;
400 DCHECK_EQ(DownloadItem::INTERRUPTED, download->GetState()); 403 DCHECK_EQ(DownloadItem::INTERRUPTED, download->GetState());
401 download->MergeOriginInfoOnResume(*info);
402 } 404 }
403 405
404 base::FilePath default_download_directory; 406 base::FilePath default_download_directory;
405 if (delegate_) { 407 if (delegate_) {
406 base::FilePath website_save_directory; // Unused 408 base::FilePath website_save_directory; // Unused
407 bool skip_dir_check = false; // Unused 409 bool skip_dir_check = false; // Unused
408 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, 410 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
409 &default_download_directory, &skip_dir_check); 411 &default_download_directory, &skip_dir_check);
410 } 412 }
411 413
412 // Create the download file and start the download. 414 scoped_ptr<DownloadFile> download_file;
413 scoped_ptr<DownloadFile> download_file(
414 file_factory_->CreateFile(
415 info->save_info.Pass(), default_download_directory,
416 info->url(), info->referrer_url,
417 delegate_ && delegate_->GenerateFileHash(),
418 stream.Pass(), download->GetBoundNetLog(),
419 download->DestinationObserverAsWeakPtr()));
420 415
421 // Attach the client ID identifying the app to the AV system. 416 if (info->interrupt_reason == DOWNLOAD_INTERRUPT_REASON_NONE) {
422 if (download_file.get() && delegate_) { 417 DCHECK(byte_stream.get());
423 download_file->SetClientGuid( 418 // Create the download file and start the download.
424 delegate_->ApplicationClientIdForFileScanning()); 419 download_file.reset(
420 file_factory_->CreateFile(*info->save_info,
421 default_download_directory,
422 info->url(),
423 info->referrer_url,
424 delegate_ && delegate_->GenerateFileHash(),
425 info->save_info->file_stream.Pass(),
Randy Smith (Not in Mondays) 2014/03/18 21:07:28 Just curious: Why make this an explicit argument?
asanka 2014/03/19 20:37:06 The ownership of FileStream is being passed into t
426 byte_stream.Pass(),
427 download->GetBoundNetLog(),
428 download->DestinationObserverAsWeakPtr()));
429
430 // Attach the client ID identifying the app to the AV system.
431 if (download_file.get() && delegate_) {
432 download_file->SetClientGuid(
433 delegate_->ApplicationClientIdForFileScanning());
434 }
425 } 435 }
426 436
437 // OnDownloadCreated notification needs to be issued before Start() is called
438 // so that observers can choose to observe the initial state transition that
439 // happens during DownloadItem::Start(). Active DownloadItems are created in
440 // an IN_PROGRESS state. If the download is already interrupted, then Start()
441 // will transition the download to INTERRUPTED. This is currently the only
442 // distinction between a download created in response to an interrupted
443 // request, and an interrupted download being recreated from history.
Randy Smith (Not in Mondays) 2014/03/18 21:07:28 How are you dealing with the issue in the deleted
Randy Smith (Not in Mondays) 2014/03/18 21:07:28 As an alternative to the above comment, we could j
Randy Smith (Not in Mondays) 2014/03/18 21:07:28 The above thoughts bring up a side issue: When we'
asanka 2014/03/19 20:37:06 I've been mulling a STARTING state as mentioned el
asanka 2014/03/19 20:37:06 Yeah. The consumer is supposed to see an OnDownloa
444 if (new_download)
445 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
446
427 scoped_ptr<DownloadRequestHandleInterface> req_handle( 447 scoped_ptr<DownloadRequestHandleInterface> req_handle(
428 new DownloadRequestHandle(info->request_handle)); 448 new DownloadRequestHandle(info->request_handle));
429 download->Start(download_file.Pass(), req_handle.Pass()); 449 download->Start(download_file.Pass(), req_handle.Pass(), *info);
430
431 // For interrupted downloads, Start() will transition the state to
432 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated().
433 // For new downloads, we notify here, rather than earlier, so that
434 // the download_file is bound to download and all the usual
435 // setters (e.g. Cancel) work.
436 if (new_download)
437 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
438 450
439 if (!on_started.is_null()) 451 if (!on_started.is_null())
440 on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE); 452 on_started.Run(download, info->interrupt_reason);
Randy Smith (Not in Mondays) 2014/03/18 21:07:28 nit: This violates the contract on started_cb desc
asanka 2014/03/19 20:37:06 Done.
441 } 453 }
442 454
443 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { 455 void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
444 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
445 for (DownloadMap::iterator it = downloads_.begin(); 457 for (DownloadMap::iterator it = downloads_.begin();
446 it != downloads_.end(); ++it) { 458 it != downloads_.end(); ++it) {
447 DownloadItemImpl* item = it->second; 459 DownloadItemImpl* item = it->second;
448 CheckForFileRemoval(item); 460 CheckForFileRemoval(item);
449 } 461 }
450 } 462 }
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 if (delegate_) 720 if (delegate_)
709 delegate_->OpenDownload(download); 721 delegate_->OpenDownload(download);
710 } 722 }
711 723
712 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { 724 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
713 if (delegate_) 725 if (delegate_)
714 delegate_->ShowDownloadInShell(download); 726 delegate_->ShowDownloadInShell(download);
715 } 727 }
716 728
717 } // namespace content 729 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698