OLD | NEW |
---|---|
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 14 matching lines...) Expand all Loading... | |
25 #include "content/browser/download/download_item_impl.h" | 25 #include "content/browser/download/download_item_impl.h" |
26 #include "content/browser/download/download_stats.h" | 26 #include "content/browser/download/download_stats.h" |
27 #include "content/browser/renderer_host/render_view_host_impl.h" | 27 #include "content/browser/renderer_host/render_view_host_impl.h" |
28 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 28 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
29 #include "content/browser/web_contents/web_contents_impl.h" | 29 #include "content/browser/web_contents/web_contents_impl.h" |
30 #include "content/public/browser/browser_context.h" | 30 #include "content/public/browser/browser_context.h" |
31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
32 #include "content/public/browser/content_browser_client.h" | 32 #include "content/public/browser/content_browser_client.h" |
33 #include "content/public/browser/download_interrupt_reasons.h" | 33 #include "content/public/browser/download_interrupt_reasons.h" |
34 #include "content/public/browser/download_manager_delegate.h" | 34 #include "content/public/browser/download_manager_delegate.h" |
35 #include "content/public/browser/download_persistent_store_info.h" | |
36 #include "content/public/browser/download_url_parameters.h" | 35 #include "content/public/browser/download_url_parameters.h" |
37 #include "content/public/browser/notification_service.h" | 36 #include "content/public/browser/notification_service.h" |
38 #include "content/public/browser/notification_types.h" | 37 #include "content/public/browser/notification_types.h" |
39 #include "content/public/browser/render_process_host.h" | 38 #include "content/public/browser/render_process_host.h" |
40 #include "content/public/browser/resource_context.h" | 39 #include "content/public/browser/resource_context.h" |
41 #include "content/public/browser/web_contents_delegate.h" | 40 #include "content/public/browser/web_contents_delegate.h" |
42 #include "net/base/load_flags.h" | 41 #include "net/base/load_flags.h" |
43 #include "net/base/upload_data.h" | 42 #include "net/base/upload_data.h" |
44 #include "net/url_request/url_request_context.h" | 43 #include "net/url_request/url_request_context.h" |
45 #include "webkit/glue/webkit_glue.h" | 44 #include "webkit/glue/webkit_glue.h" |
46 | 45 |
47 using content::BrowserThread; | 46 using content::BrowserThread; |
48 using content::DownloadId; | 47 using content::DownloadId; |
49 using content::DownloadItem; | 48 using content::DownloadItem; |
50 using content::DownloadPersistentStoreInfo; | |
51 using content::ResourceDispatcherHostImpl; | 49 using content::ResourceDispatcherHostImpl; |
52 using content::WebContents; | 50 using content::WebContents; |
53 | 51 |
54 namespace { | 52 namespace { |
55 | 53 |
56 // This is just used to remember which DownloadItems come from SavePage. | 54 // This is just used to remember which DownloadItems come from SavePage. |
57 class SavePageData : public base::SupportsUserData::Data { | 55 class SavePageData : public base::SupportsUserData::Data { |
58 public: | 56 public: |
59 // A spoonful of syntactic sugar. | 57 // A spoonful of syntactic sugar. |
60 static bool Get(DownloadItem* item) { | 58 static bool Get(DownloadItem* item) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 } | 160 } |
163 | 161 |
164 class DownloadItemFactoryImpl : public content::DownloadItemFactory { | 162 class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
165 public: | 163 public: |
166 DownloadItemFactoryImpl() {} | 164 DownloadItemFactoryImpl() {} |
167 virtual ~DownloadItemFactoryImpl() {} | 165 virtual ~DownloadItemFactoryImpl() {} |
168 | 166 |
169 virtual DownloadItemImpl* CreatePersistedItem( | 167 virtual DownloadItemImpl* CreatePersistedItem( |
170 DownloadItemImplDelegate* delegate, | 168 DownloadItemImplDelegate* delegate, |
171 content::DownloadId download_id, | 169 content::DownloadId download_id, |
172 const content::DownloadPersistentStoreInfo& info, | 170 const FilePath& path, |
171 const GURL& url, | |
172 const GURL& referrer_url, | |
173 const base::Time& start_time, | |
174 const base::Time& end_time, | |
175 int64 received_bytes, | |
176 int64 total_bytes, | |
177 DownloadItem::DownloadState state, | |
178 bool opened, | |
173 const net::BoundNetLog& bound_net_log) OVERRIDE { | 179 const net::BoundNetLog& bound_net_log) OVERRIDE { |
174 return new DownloadItemImpl(delegate, download_id, info, bound_net_log); | 180 return new DownloadItemImpl( |
181 delegate, | |
182 download_id, | |
183 path, | |
184 url, | |
185 referrer_url, | |
186 start_time, | |
187 end_time, | |
188 received_bytes, | |
189 total_bytes, | |
190 state, | |
191 opened, | |
192 bound_net_log); | |
175 } | 193 } |
176 | 194 |
177 virtual DownloadItemImpl* CreateActiveItem( | 195 virtual DownloadItemImpl* CreateActiveItem( |
178 DownloadItemImplDelegate* delegate, | 196 DownloadItemImplDelegate* delegate, |
179 const DownloadCreateInfo& info, | 197 const DownloadCreateInfo& info, |
180 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 198 scoped_ptr<DownloadRequestHandleInterface> request_handle, |
181 const net::BoundNetLog& bound_net_log) OVERRIDE { | 199 const net::BoundNetLog& bound_net_log) OVERRIDE { |
182 return new DownloadItemImpl(delegate, info, request_handle.Pass(), | 200 return new DownloadItemImpl(delegate, info, request_handle.Pass(), |
183 bound_net_log); | 201 bound_net_log); |
184 } | 202 } |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
304 // from the disk. This may or may not result in it being | 322 // from the disk. This may or may not result in it being |
305 // removed from the DownloadManager queues and deleted | 323 // removed from the DownloadManager queues and deleted |
306 // (specifically, DownloadManager::DownloadRemoved only | 324 // (specifically, DownloadManager::DownloadRemoved only |
307 // removes and deletes it if it's known to the history service) | 325 // removes and deletes it if it's known to the history service) |
308 // so the only thing we know after calling this function is that | 326 // so the only thing we know after calling this function is that |
309 // the download was deleted if-and-only-if it was removed | 327 // the download was deleted if-and-only-if it was removed |
310 // from all queues. | 328 // from all queues. |
311 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 329 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
312 } else if (download->IsPartialDownload()) { | 330 } else if (download->IsPartialDownload()) { |
313 download->Cancel(false); | 331 download->Cancel(false); |
314 if (delegate_) | |
315 delegate_->UpdateItemInPersistentStore(download); | |
316 } | 332 } |
317 } | 333 } |
318 | 334 |
319 // At this point, all dangerous downloads have had their files removed | 335 // At this point, all dangerous downloads have had their files removed |
320 // and all in progress downloads have been cancelled. We can now delete | 336 // and all in progress downloads have been cancelled. We can now delete |
321 // anything left. | 337 // anything left. |
322 | 338 |
323 active_downloads_.clear(); | 339 active_downloads_.clear(); |
324 STLDeleteValues(&downloads_); | 340 STLDeleteValues(&downloads_); |
325 downloads_.clear(); | 341 downloads_.clear(); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 } | 426 } |
411 } | 427 } |
412 | 428 |
413 void DownloadManagerImpl::OnDownloadTargetDetermined( | 429 void DownloadManagerImpl::OnDownloadTargetDetermined( |
414 int32 download_id, | 430 int32 download_id, |
415 const FilePath& target_path, | 431 const FilePath& target_path, |
416 DownloadItem::TargetDisposition disposition, | 432 DownloadItem::TargetDisposition disposition, |
417 content::DownloadDangerType danger_type, | 433 content::DownloadDangerType danger_type, |
418 const FilePath& intermediate_path) { | 434 const FilePath& intermediate_path) { |
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
420 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | 436 DownloadMap::iterator download_iter = downloads_.find(download_id); |
421 if (download_iter != active_downloads_.end()) { | 437 if (download_iter == downloads_.end()) |
422 // Once DownloadItem::OnDownloadTargetDetermined() is called, we expect a | 438 return; |
423 // DownloadRenamedToIntermediateName() callback. This is necessary for the | 439 |
424 // download to proceed. | 440 // Once DownloadItem::OnDownloadTargetDetermined() is called, we expect a |
425 download_iter->second->OnDownloadTargetDetermined( | 441 // DownloadRenamedToIntermediateName() callback. This is necessary for the |
426 target_path, disposition, danger_type, intermediate_path); | 442 // download to proceed. |
427 } | 443 DownloadItemImpl* item = download_iter->second; |
444 item->OnDownloadTargetDetermined( | |
445 target_path, disposition, danger_type, intermediate_path); | |
446 ShowDownloadInBrowser(item); | |
447 NotifyModelChanged(); | |
428 } | 448 } |
429 | 449 |
430 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { | 450 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
432 for (DownloadMap::iterator it = downloads_.begin(); | 452 for (DownloadMap::iterator it = downloads_.begin(); |
433 it != downloads_.end(); ++it) { | 453 it != downloads_.end(); ++it) { |
434 DownloadItemImpl* item = it->second; | 454 DownloadItemImpl* item = it->second; |
435 if (item->IsPersisted()) | 455 CheckForFileRemoval(item); |
436 CheckForFileRemoval(item); | |
437 } | 456 } |
438 } | 457 } |
439 | 458 |
440 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { | 459 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
442 if (download_item->IsComplete() && | 461 if (download_item->IsComplete() && |
443 !download_item->GetFileExternallyRemoved()) { | 462 !download_item->GetFileExternallyRemoved()) { |
444 BrowserThread::PostTask( | 463 BrowserThread::PostTask( |
445 BrowserThread::FILE, FROM_HERE, | 464 BrowserThread::FILE, FROM_HERE, |
446 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, | 465 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 return bound_net_log; | 513 return bound_net_log; |
495 } | 514 } |
496 | 515 |
497 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 516 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
498 const FilePath& main_file_path, | 517 const FilePath& main_file_path, |
499 const GURL& page_url, | 518 const GURL& page_url, |
500 const std::string& mime_type, | 519 const std::string& mime_type, |
501 DownloadItem::Observer* observer) { | 520 DownloadItem::Observer* observer) { |
502 net::BoundNetLog bound_net_log = | 521 net::BoundNetLog bound_net_log = |
503 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 522 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
504 DownloadItemImpl* download = factory_->CreateSavePageItem( | 523 DownloadItemImpl* download_item = factory_->CreateSavePageItem( |
505 this, | 524 this, |
506 main_file_path, | 525 main_file_path, |
507 page_url, | 526 page_url, |
508 GetNextId(), | 527 GetNextId(), |
509 mime_type, | 528 mime_type, |
510 bound_net_log); | 529 bound_net_log); |
511 | 530 download_item->AddObserver(observer); |
Randy Smith (Not in Mondays)
2012/09/25 18:09:20
You got rid of blank lines that I personally feel
benjhayden
2012/09/25 20:22:18
Done.
| |
512 download->AddObserver(observer); | 531 DCHECK(!ContainsKey(downloads_, download_item->GetId())); |
513 | 532 downloads_[download_item->GetId()] = download_item; |
514 DCHECK(!ContainsKey(downloads_, download->GetId())); | 533 DCHECK(!SavePageData::Get(download_item)); |
515 downloads_[download->GetId()] = download; | 534 new SavePageData(download_item); |
Randy Smith (Not in Mondays)
2012/09/25 18:09:20
I don't see this being used anywhere in this file
benjhayden
2012/09/25 20:22:18
Done.
| |
516 DCHECK(!SavePageData::Get(download)); | 535 DCHECK(SavePageData::Get(download_item)); |
517 new SavePageData(download); | 536 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( |
518 DCHECK(SavePageData::Get(download)); | 537 this, download_item)); |
519 | 538 OnDownloadTargetDetermined( |
Randy Smith (Not in Mondays)
2012/09/25 18:09:20
So. This starts a long cascade inside of Download
benjhayden
2012/09/25 20:22:18
Done.
Randy Smith (Not in Mondays)
2012/09/26 15:56:12
I don't see any modifications to the SavePackage d
| |
520 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 539 download_item->GetId(), |
521 | 540 main_file_path, |
522 // Will notify the observer in the callback. | 541 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
523 if (delegate_) | 542 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
524 delegate_->AddItemToPersistentStore(download); | 543 main_file_path); |
525 | 544 return download_item; |
526 return download; | |
527 } | 545 } |
528 | 546 |
529 void DownloadManagerImpl::UpdateDownload(int32 download_id, | 547 void DownloadManagerImpl::UpdateDownload(int32 download_id, |
530 int64 bytes_so_far, | 548 int64 bytes_so_far, |
531 int64 bytes_per_sec, | 549 int64 bytes_per_sec, |
532 const std::string& hash_state) { | 550 const std::string& hash_state) { |
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 551 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
534 DownloadMap::iterator it = active_downloads_.find(download_id); | 552 DownloadMap::iterator it = active_downloads_.find(download_id); |
535 if (it != active_downloads_.end()) { | 553 if (it != active_downloads_.end()) { |
536 DownloadItemImpl* download = it->second; | 554 DownloadItemImpl* download = it->second; |
537 if (download->IsInProgress()) { | 555 if (download->IsInProgress()) { |
538 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); | 556 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); |
539 if (delegate_) | |
540 delegate_->UpdateItemInPersistentStore(download); | |
541 } | 557 } |
542 } | 558 } |
543 } | 559 } |
544 | 560 |
545 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, | 561 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, |
546 int64 size, | 562 int64 size, |
547 const std::string& hash) { | 563 const std::string& hash) { |
548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 564 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
549 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | 565 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
550 << " size = " << size; | 566 << " size = " << size; |
551 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 567 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
552 | 568 |
553 // If it's not in active_downloads_, that means it was cancelled; just | 569 // If it's not in active_downloads_, that means it was cancelled; just |
554 // ignore the notification. | 570 // ignore the notification. |
555 if (active_downloads_.count(download_id) == 0) | 571 if (active_downloads_.count(download_id) == 0) |
556 return; | 572 return; |
557 | 573 |
558 DownloadItemImpl* download = active_downloads_[download_id]; | 574 DownloadItemImpl* download = active_downloads_[download_id]; |
559 download->OnAllDataSaved(size, hash); | 575 download->OnAllDataSaved(size, hash); |
560 MaybeCompleteDownload(download); | 576 MaybeCompleteDownload(download); |
561 } | 577 } |
562 | 578 |
563 void DownloadManagerImpl::AssertStateConsistent( | 579 void DownloadManagerImpl::AssertStateConsistent( |
564 DownloadItemImpl* download) const { | 580 DownloadItemImpl* download) const { |
565 CHECK(ContainsKey(downloads_, download->GetId())); | 581 CHECK(ContainsKey(downloads_, download->GetId())); |
566 | 582 |
567 int64 state = download->GetState(); | 583 int64 state = download->GetState(); |
568 base::debug::Alias(&state); | 584 base::debug::Alias(&state); |
569 if (ContainsKey(active_downloads_, download->GetId())) { | |
570 if (download->IsPersisted()) | |
571 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | |
572 if (DownloadItem::IN_PROGRESS != download->GetState()) | |
573 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle()); | |
574 } | |
575 if (DownloadItem::IN_PROGRESS == download->GetState()) | 585 if (DownloadItem::IN_PROGRESS == download->GetState()) |
576 CHECK(ContainsKey(active_downloads_, download->GetId())); | 586 CHECK(ContainsKey(active_downloads_, download->GetId())); |
577 } | 587 } |
578 | 588 |
579 bool DownloadManagerImpl::IsDownloadReadyForCompletion( | 589 bool DownloadManagerImpl::IsDownloadReadyForCompletion( |
580 DownloadItemImpl* download) { | 590 DownloadItemImpl* download) { |
581 // If we don't have all the data, the download is not ready for | 591 // If we don't have all the data, the download is not ready for |
582 // completion. | 592 // completion. |
583 if (!download->AllDataSaved()) | 593 if (!download->AllDataSaved()) |
584 return false; | 594 return false; |
585 | 595 |
586 // If the download is dangerous, but not yet validated, it's not ready for | 596 // If the download is dangerous, but not yet validated, it's not ready for |
587 // completion. | 597 // completion. |
588 if (download->GetSafetyState() == DownloadItem::DANGEROUS) | 598 if (download->GetSafetyState() == DownloadItem::DANGEROUS) |
589 return false; | 599 return false; |
590 | 600 |
591 // If the download isn't active (e.g. has been cancelled) it's not | 601 // If the download isn't active (e.g. has been cancelled) it's not |
592 // ready for completion. | 602 // ready for completion. |
593 if (active_downloads_.count(download->GetId()) == 0) | 603 if (active_downloads_.count(download->GetId()) == 0) |
594 return false; | 604 return false; |
595 | 605 |
596 // If the download hasn't been inserted into the history system | 606 // If the intermediate filename rename hasn't occurred, don't complete the |
597 // (which occurs strictly after file name determination, intermediate | 607 // download. |
598 // file rename, and UI display) then it's not ready for completion. | 608 if (download->GetTargetFilePath().DirName() != |
599 if (!download->IsPersisted()) | 609 download->GetFullPath().DirName()) |
610 return false; | |
611 | |
612 // If the target filename hasn't been determined, don't complete the download. | |
613 if (download->GetTargetFilePath().empty()) | |
600 return false; | 614 return false; |
601 | 615 |
602 return true; | 616 return true; |
603 } | 617 } |
604 | 618 |
605 // When SavePackage downloads MHTML to GData (see | 619 // When SavePackage downloads MHTML to GData (see |
606 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it | 620 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it |
607 // does for non-SavePackage downloads, but SavePackage downloads never satisfy | 621 // does for non-SavePackage downloads, but SavePackage downloads never satisfy |
608 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls | 622 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls |
609 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage | 623 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage |
(...skipping 13 matching lines...) Expand all Loading... | |
623 | 637 |
624 // TODO(rdsmith): DCHECK that we only pass through this point | 638 // TODO(rdsmith): DCHECK that we only pass through this point |
625 // once per download. The natural way to do this is by a state | 639 // once per download. The natural way to do this is by a state |
626 // transition on the DownloadItem. | 640 // transition on the DownloadItem. |
627 | 641 |
628 // Confirm we're in the proper set of states to be here; | 642 // Confirm we're in the proper set of states to be here; |
629 // have all data, have a history handle, (validated or safe). | 643 // have all data, have a history handle, (validated or safe). |
630 DCHECK(download->IsInProgress()); | 644 DCHECK(download->IsInProgress()); |
631 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); | 645 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); |
632 DCHECK(download->AllDataSaved()); | 646 DCHECK(download->AllDataSaved()); |
633 DCHECK(download->IsPersisted()); | |
634 | 647 |
635 // Give the delegate a chance to override. It's ok to keep re-setting the | 648 // Give the delegate a chance to override. It's ok to keep re-setting the |
636 // delegate's |complete_callback| cb as long as there isn't another call-point | 649 // delegate's |complete_callback| cb as long as there isn't another call-point |
637 // trying to set it to a different cb. TODO(benjhayden): Change the callback | 650 // trying to set it to a different cb. TODO(benjhayden): Change the callback |
638 // to point directly to the item instead of |this| when DownloadItem supports | 651 // to point directly to the item instead of |this| when DownloadItem supports |
639 // weak-ptrs. | 652 // weak-ptrs. |
640 if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind( | 653 if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind( |
641 &DownloadManagerImpl::MaybeCompleteDownloadById, | 654 &DownloadManagerImpl::MaybeCompleteDownloadById, |
642 this, download->GetId()))) | 655 this, download->GetId()))) |
643 return; | 656 return; |
644 | 657 |
645 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " | 658 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
646 << download->DebugString(false); | 659 << download->DebugString(false); |
647 | 660 |
648 if (delegate_) | |
649 delegate_->UpdateItemInPersistentStore(download); | |
650 download->OnDownloadCompleting(); | 661 download->OnDownloadCompleting(); |
651 } | 662 } |
652 | 663 |
653 void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { | 664 void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { |
654 if (ContainsKey(active_downloads_, download_id)) | 665 if (ContainsKey(active_downloads_, download_id)) |
655 MaybeCompleteDownload(active_downloads_[download_id]); | 666 MaybeCompleteDownload(active_downloads_[download_id]); |
656 } | 667 } |
657 | 668 |
658 void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { | 669 void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { |
659 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 670 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
660 DCHECK(download); | 671 DCHECK(download); |
661 if (delegate_) | |
662 delegate_->UpdateItemInPersistentStore(download); | |
663 active_downloads_.erase(download->GetId()); | 672 active_downloads_.erase(download->GetId()); |
664 AssertStateConsistent(download); | 673 AssertStateConsistent(download); |
665 } | 674 } |
666 | 675 |
667 void DownloadManagerImpl::CancelDownload(int32 download_id) { | 676 void DownloadManagerImpl::CancelDownload(int32 download_id) { |
668 // A cancel at the right time could remove the download from the | 677 // A cancel at the right time could remove the download from the |
669 // |active_downloads_| map before we get here. | 678 // |active_downloads_| map before we get here. |
670 if (ContainsKey(active_downloads_, download_id)) | 679 if (ContainsKey(active_downloads_, download_id)) |
671 active_downloads_[download_id]->Cancel(true); | 680 active_downloads_[download_id]->Cancel(true); |
672 } | 681 } |
(...skipping 19 matching lines...) Expand all Loading... | |
692 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 701 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
693 | 702 |
694 if (!ContainsKey(active_downloads_, download_id)) | 703 if (!ContainsKey(active_downloads_, download_id)) |
695 return; | 704 return; |
696 active_downloads_[download_id]->Interrupt(reason); | 705 active_downloads_[download_id]->Interrupt(reason); |
697 } | 706 } |
698 | 707 |
699 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 708 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
700 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 709 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
701 DCHECK(download); | 710 DCHECK(download); |
702 | 711 active_downloads_.erase(download->GetId()); |
703 // Clean up will happen when the history system create callback runs if we | |
704 // don't have a valid db_handle yet. | |
705 if (download->IsPersisted()) { | |
706 active_downloads_.erase(download->GetId()); | |
707 if (delegate_) | |
708 delegate_->UpdateItemInPersistentStore(download); | |
709 } | |
710 } | 712 } |
711 | 713 |
712 int DownloadManagerImpl::RemoveDownloadItems( | 714 int DownloadManagerImpl::RemoveDownloadItems( |
713 const DownloadItemImplVector& pending_deletes) { | 715 const DownloadItemImplVector& pending_deletes) { |
714 if (pending_deletes.empty()) | 716 if (pending_deletes.empty()) |
715 return 0; | 717 return 0; |
716 | 718 |
717 // Delete from internal maps. | 719 // Delete from internal maps. |
718 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 720 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
719 it != pending_deletes.end(); | 721 it != pending_deletes.end(); |
720 ++it) { | 722 ++it) { |
721 DownloadItemImpl* download = *it; | 723 DownloadItemImpl* download = *it; |
722 DCHECK(download); | 724 DCHECK(download); |
723 int32 download_id = download->GetId(); | 725 int32 download_id = download->GetId(); |
724 delete download; | 726 delete download; |
725 downloads_.erase(download_id); | 727 downloads_.erase(download_id); |
726 } | 728 } |
727 NotifyModelChanged(); | 729 NotifyModelChanged(); |
728 return static_cast<int>(pending_deletes.size()); | 730 return static_cast<int>(pending_deletes.size()); |
729 } | 731 } |
730 | 732 |
731 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { | 733 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
732 if (!download || | 734 if (!download || |
733 downloads_.find(download->GetId()) == downloads_.end()) | 735 downloads_.find(download->GetId()) == downloads_.end()) |
734 return; | 736 return; |
735 | 737 |
736 // TODO(benjhayden,rdsmith): Remove this. | |
737 if (!download->IsPersisted()) | |
738 return; | |
739 | |
740 // Make history update. | |
741 if (delegate_) | |
742 delegate_->RemoveItemFromPersistentStore(download); | |
743 | |
744 // Remove from our tables and delete. | 738 // Remove from our tables and delete. |
745 int downloads_count = | 739 int downloads_count = |
746 RemoveDownloadItems(DownloadItemImplVector(1, download)); | 740 RemoveDownloadItems(DownloadItemImplVector(1, download)); |
747 DCHECK_EQ(1, downloads_count); | 741 DCHECK_EQ(1, downloads_count); |
748 } | 742 } |
749 | 743 |
750 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, | 744 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, |
751 base::Time remove_end) { | 745 base::Time remove_end) { |
752 if (delegate_) | |
753 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); | |
754 | |
755 DownloadItemImplVector pending_deletes; | 746 DownloadItemImplVector pending_deletes; |
756 for (DownloadMap::const_iterator it = downloads_.begin(); | 747 for (DownloadMap::const_iterator it = downloads_.begin(); |
757 it != downloads_.end(); | 748 it != downloads_.end(); |
758 ++it) { | 749 ++it) { |
759 DownloadItemImpl* download = it->second; | 750 DownloadItemImpl* download = it->second; |
760 if (download->IsPersisted() && | 751 if (download->GetStartTime() >= remove_begin && |
761 download->GetStartTime() >= remove_begin && | |
762 (remove_end.is_null() || download->GetStartTime() < remove_end) && | 752 (remove_end.is_null() || download->GetStartTime() < remove_end) && |
763 (download->IsComplete() || download->IsCancelled())) { | 753 (download->IsComplete() || download->IsCancelled())) { |
764 AssertStateConsistent(download); | 754 AssertStateConsistent(download); |
765 download->NotifyRemoved(); | 755 download->NotifyRemoved(); |
766 pending_deletes.push_back(download); | 756 pending_deletes.push_back(download); |
767 } | 757 } |
768 } | 758 } |
769 return RemoveDownloadItems(pending_deletes); | 759 return RemoveDownloadItems(pending_deletes); |
770 } | 760 } |
771 | 761 |
(...skipping 26 matching lines...) Expand all Loading... | |
798 // observers. | 788 // observers. |
799 observer->ModelChanged(this); | 789 observer->ModelChanged(this); |
800 } | 790 } |
801 | 791 |
802 void DownloadManagerImpl::RemoveObserver(Observer* observer) { | 792 void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
803 observers_.RemoveObserver(observer); | 793 observers_.RemoveObserver(observer); |
804 } | 794 } |
805 | 795 |
806 // Operations posted to us from the history service ---------------------------- | 796 // Operations posted to us from the history service ---------------------------- |
807 | 797 |
808 // The history service has retrieved all download entries. 'entries' contains | 798 DownloadItem* DownloadManagerImpl::CreateDownloadItem( |
809 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 799 const FilePath& path, |
810 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 800 const GURL& url, |
811 std::vector<DownloadPersistentStoreInfo>* entries) { | 801 const GURL& referrer_url, |
812 history_size_ = entries->size(); | 802 const base::Time& start_time, |
813 for (size_t i = 0; i < entries->size(); ++i) { | 803 const base::Time& end_time, |
814 int64 db_handle = entries->at(i).db_handle; | 804 int64 received_bytes, |
815 base::debug::Alias(&db_handle); | 805 int64 total_bytes, |
816 | 806 DownloadItem::DownloadState state, |
817 net::BoundNetLog bound_net_log = | 807 bool opened) { |
818 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 808 DownloadItemImpl* item = factory_->CreatePersistedItem( |
819 DownloadItemImpl* download = factory_->CreatePersistedItem( | 809 this, |
820 this, GetNextId(), entries->at(i), bound_net_log); | 810 GetNextId(), |
821 DCHECK(!ContainsKey(downloads_, download->GetId())); | 811 path, |
822 downloads_[download->GetId()] = download; | 812 url, |
823 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 813 referrer_url, |
824 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 814 start_time, |
825 << " download = " << download->DebugString(true); | 815 end_time, |
826 } | 816 received_bytes, |
817 total_bytes, | |
818 state, | |
819 opened, | |
820 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD)); | |
821 DCHECK(!ContainsKey(downloads_, item->GetId())); | |
822 downloads_[item->GetId()] = item; | |
823 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); | |
824 VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true); | |
827 NotifyModelChanged(); | 825 NotifyModelChanged(); |
828 CheckForHistoryFilesRemoval(); | 826 return item; |
829 } | |
830 | |
831 void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItemImpl* download, | |
832 int64 db_handle) { | |
833 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
834 DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle); | |
835 DCHECK(!download->IsPersisted()); | |
836 download->SetDbHandle(db_handle); | |
837 download->SetIsPersisted(); | |
838 | |
839 download_stats::RecordHistorySize(history_size_); | |
840 // Not counting |download|. | |
841 ++history_size_; | |
842 | |
843 // Show in the appropriate browser UI. | |
844 // This includes buttons to save or cancel, for a dangerous download. | |
845 ShowDownloadInBrowser(download); | |
846 | |
847 // Inform interested objects about the new download. | |
848 NotifyModelChanged(); | |
849 } | |
850 | |
851 | |
852 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, | |
853 int64 db_handle) { | |
854 // It's valid that we don't find a matching item, i.e. on shutdown. | |
855 if (!ContainsKey(downloads_, download_id)) | |
856 return; | |
857 | |
858 DownloadItemImpl* item = downloads_[download_id]; | |
859 AddDownloadItemToHistory(item, db_handle); | |
860 if (SavePageData::Get(item)) { | |
861 OnSavePageItemAddedToPersistentStore(item); | |
862 } else { | |
863 OnDownloadItemAddedToPersistentStore(item); | |
864 } | |
865 } | |
866 | |
867 // Once the new DownloadItem has been committed to the persistent store, | |
868 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), | |
869 // show it in the browser (TODO(benjhayden) the ui should observe us instead), | |
870 // and notify observers (TODO(benjhayden) observers should be able to see the | |
871 // item when it's created so they can observe it directly. Are there any | |
872 // clients that actually need to know when the item is added to the history?). | |
873 void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore( | |
874 DownloadItemImpl* item) { | |
875 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
876 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << item->GetDbHandle() | |
877 << " download_id = " << item->GetId() | |
878 << " download = " << item->DebugString(true); | |
879 | |
880 // If the download is still in progress, try to complete it. | |
881 // | |
882 // Otherwise, download has been cancelled or interrupted before we've | |
883 // received the DB handle. We post one final message to the history | |
884 // service so that it can be properly in sync with the DownloadItem's | |
885 // completion status, and also inform any observers so that they get | |
886 // more than just the start notification. | |
887 if (item->IsInProgress()) { | |
888 MaybeCompleteDownload(item); | |
889 } else { | |
890 DCHECK(item->IsCancelled()); | |
891 active_downloads_.erase(item->GetId()); | |
892 if (delegate_) | |
893 delegate_->UpdateItemInPersistentStore(item); | |
894 item->UpdateObservers(); | |
895 } | |
896 } | 827 } |
897 | 828 |
898 void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { | 829 void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { |
899 // The 'contents' may no longer exist if the user closed the contents before | 830 // The 'contents' may no longer exist if the user closed the contents before |
900 // we get this start completion event. | 831 // we get this start completion event. |
901 WebContents* content = download->GetWebContents(); | 832 WebContents* content = download->GetWebContents(); |
902 | 833 |
903 // If the contents no longer exists, we ask the embedder to suggest another | 834 // If the contents no longer exists, we ask the embedder to suggest another |
904 // contents. | 835 // contents. |
905 if (!content && delegate_) | 836 if (!content && delegate_) |
(...skipping 15 matching lines...) Expand all Loading... | |
921 ++count; | 852 ++count; |
922 } | 853 } |
923 return count; | 854 return count; |
924 } | 855 } |
925 | 856 |
926 void DownloadManagerImpl::NotifyModelChanged() { | 857 void DownloadManagerImpl::NotifyModelChanged() { |
927 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this)); | 858 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this)); |
928 } | 859 } |
929 | 860 |
930 DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) { | 861 DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) { |
931 DownloadItem* download = GetDownload(download_id); | 862 return GetDownload(download_id); |
932 return (download && download->IsPersisted()) ? download : NULL; | |
933 } | 863 } |
934 | 864 |
935 DownloadItem* DownloadManagerImpl::GetDownload(int download_id) { | 865 DownloadItem* DownloadManagerImpl::GetDownload(int download_id) { |
936 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL; | 866 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL; |
937 } | 867 } |
938 | 868 |
939 void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) { | 869 void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) { |
940 for (DownloadMap::iterator it = downloads_.begin(); | 870 for (DownloadMap::iterator it = downloads_.begin(); |
941 it != downloads_.end(); ++it) { | 871 it != downloads_.end(); ++it) { |
942 downloads->push_back(it->second); | 872 downloads->push_back(it->second); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
976 DownloadSet remainder; | 906 DownloadSet remainder; |
977 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); | 907 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
978 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), | 908 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
979 all_downloads.begin(), all_downloads.end(), | 909 all_downloads.begin(), all_downloads.end(), |
980 insert_it); | 910 insert_it); |
981 DCHECK(remainder.empty()); | 911 DCHECK(remainder.empty()); |
982 } | 912 } |
983 #endif | 913 #endif |
984 } | 914 } |
985 | 915 |
986 // SavePackage will call SavePageDownloadFinished upon completion/cancellation. | |
987 // The history callback will call OnSavePageItemAddedToPersistentStore. | |
988 // If the download finishes before the history callback, | |
989 // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring | |
990 // that the history event is update regardless of the order in which these two | |
991 // events complete. | |
992 // If something removes the download item from the download manager (Remove, | |
993 // Shutdown) the result will be that the SavePage system will not be able to | |
994 // properly update the download item (which no longer exists) or the download | |
995 // history, but the action will complete properly anyway. This may lead to the | |
996 // history entry being wrong on a reload of chrome (specifically in the case of | |
997 // Initiation -> History Callback -> Removal -> Completion), but there's no way | |
998 // to solve that without canceling on Remove (which would then update the DB). | |
999 | |
1000 void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore( | |
1001 DownloadItemImpl* item) { | |
1002 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1003 | |
1004 // Finalize this download if it finished before the history callback. | |
1005 if (!item->IsInProgress()) | |
1006 SavePageDownloadFinished(item); | |
1007 } | |
1008 | |
1009 void DownloadManagerImpl::SavePageDownloadFinished( | |
1010 content::DownloadItem* download) { | |
1011 if (download->IsPersisted()) { | |
1012 if (delegate_) | |
1013 delegate_->UpdateItemInPersistentStore(download); | |
1014 } | |
1015 } | |
1016 | |
1017 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { | 916 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { |
1018 if (delegate_) | |
1019 delegate_->UpdateItemInPersistentStore(download); | |
1020 int num_unopened = 0; | 917 int num_unopened = 0; |
1021 for (DownloadMap::iterator it = downloads_.begin(); | 918 for (DownloadMap::iterator it = downloads_.begin(); |
1022 it != downloads_.end(); ++it) { | 919 it != downloads_.end(); ++it) { |
1023 DownloadItemImpl* item = it->second; | 920 DownloadItemImpl* item = it->second; |
1024 if (item->IsComplete() && | 921 if (item->IsComplete() && |
1025 !item->GetOpened()) | 922 !item->GetOpened()) |
1026 ++num_unopened; | 923 ++num_unopened; |
1027 } | 924 } |
1028 download_stats::RecordOpensOutstanding(num_unopened); | 925 download_stats::RecordOpensOutstanding(num_unopened); |
1029 } | 926 } |
1030 | 927 |
928 // TODO(benjhayden) MaybeCompleteDownload should be moved into DownloadItemImpl. | |
1031 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 929 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
1032 DownloadItemImpl* download) { | 930 DownloadItemImpl* download) { |
1033 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 931 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1034 // download->GetFullPath() is only expected to be meaningful after this | 932 MaybeCompleteDownload(download); |
1035 // callback is received. Therefore we can now add the download to a persistent | |
1036 // store. If the rename failed, we receive an OnDownloadInterrupted() call | |
1037 // before we receive the DownloadRenamedToIntermediateName() call. | |
1038 if (delegate_) { | |
1039 delegate_->AddItemToPersistentStore(download); | |
1040 } else { | |
1041 OnItemAddedToPersistentStore(download->GetId(), | |
1042 DownloadItem::kUninitializedHandle); | |
1043 } | |
1044 } | 933 } |
1045 | |
1046 void DownloadManagerImpl::DownloadRenamedToFinalName( | |
1047 DownloadItemImpl* download) { | |
1048 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1049 // If the rename failed, we receive an OnDownloadInterrupted() call before we | |
1050 // receive the DownloadRenamedToFinalName() call. | |
1051 if (delegate_) { | |
1052 delegate_->UpdatePathForItemInPersistentStore( | |
1053 download, download->GetFullPath()); | |
1054 } | |
1055 } | |
OLD | NEW |