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. | |
57 class SavePageData : public base::SupportsUserData::Data { | |
58 public: | |
59 // A spoonful of syntactic sugar. | |
60 static bool Get(DownloadItem* item) { | |
61 return item->GetUserData(kKey) != NULL; | |
62 } | |
63 | |
64 explicit SavePageData(DownloadItem* item) { | |
65 item->SetUserData(kKey, this); | |
66 } | |
67 | |
68 virtual ~SavePageData() {} | |
69 | |
70 private: | |
71 static const char kKey[]; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(SavePageData); | |
74 }; | |
75 | |
76 const char SavePageData::kKey[] = "DownloadItem SavePageData"; | |
77 | |
78 void BeginDownload(content::DownloadUrlParameters* params) { | 54 void BeginDownload(content::DownloadUrlParameters* params) { |
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
80 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and | 56 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and |
81 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so | 57 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so |
82 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. | 58 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. |
83 scoped_ptr<net::URLRequest> request( | 59 scoped_ptr<net::URLRequest> request( |
84 params->resource_context()->GetRequestContext()->CreateRequest( | 60 params->resource_context()->GetRequestContext()->CreateRequest( |
85 params->url(), NULL)); | 61 params->url(), NULL)); |
86 request->set_referrer(params->referrer().url.spec()); | 62 request->set_referrer(params->referrer().url.spec()); |
87 webkit_glue::ConfigureURLRequestForReferrerPolicy( | 63 webkit_glue::ConfigureURLRequestForReferrerPolicy( |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 138 } |
163 | 139 |
164 class DownloadItemFactoryImpl : public content::DownloadItemFactory { | 140 class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
165 public: | 141 public: |
166 DownloadItemFactoryImpl() {} | 142 DownloadItemFactoryImpl() {} |
167 virtual ~DownloadItemFactoryImpl() {} | 143 virtual ~DownloadItemFactoryImpl() {} |
168 | 144 |
169 virtual DownloadItemImpl* CreatePersistedItem( | 145 virtual DownloadItemImpl* CreatePersistedItem( |
170 DownloadItemImplDelegate* delegate, | 146 DownloadItemImplDelegate* delegate, |
171 content::DownloadId download_id, | 147 content::DownloadId download_id, |
172 const content::DownloadPersistentStoreInfo& info, | 148 const FilePath& path, |
| 149 const GURL& url, |
| 150 const GURL& referrer_url, |
| 151 const base::Time& start_time, |
| 152 const base::Time& end_time, |
| 153 int64 received_bytes, |
| 154 int64 total_bytes, |
| 155 DownloadItem::DownloadState state, |
| 156 bool opened, |
173 const net::BoundNetLog& bound_net_log) OVERRIDE { | 157 const net::BoundNetLog& bound_net_log) OVERRIDE { |
174 return new DownloadItemImpl(delegate, download_id, info, bound_net_log); | 158 return new DownloadItemImpl( |
| 159 delegate, |
| 160 download_id, |
| 161 path, |
| 162 url, |
| 163 referrer_url, |
| 164 start_time, |
| 165 end_time, |
| 166 received_bytes, |
| 167 total_bytes, |
| 168 state, |
| 169 opened, |
| 170 bound_net_log); |
175 } | 171 } |
176 | 172 |
177 virtual DownloadItemImpl* CreateActiveItem( | 173 virtual DownloadItemImpl* CreateActiveItem( |
178 DownloadItemImplDelegate* delegate, | 174 DownloadItemImplDelegate* delegate, |
179 const DownloadCreateInfo& info, | 175 const DownloadCreateInfo& info, |
180 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 176 scoped_ptr<DownloadRequestHandleInterface> request_handle, |
181 const net::BoundNetLog& bound_net_log) OVERRIDE { | 177 const net::BoundNetLog& bound_net_log) OVERRIDE { |
182 return new DownloadItemImpl(delegate, info, request_handle.Pass(), | 178 return new DownloadItemImpl(delegate, info, request_handle.Pass(), |
183 bound_net_log); | 179 bound_net_log); |
184 } | 180 } |
(...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 | 300 // from the disk. This may or may not result in it being |
305 // removed from the DownloadManager queues and deleted | 301 // removed from the DownloadManager queues and deleted |
306 // (specifically, DownloadManager::DownloadRemoved only | 302 // (specifically, DownloadManager::DownloadRemoved only |
307 // removes and deletes it if it's known to the history service) | 303 // 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 | 304 // so the only thing we know after calling this function is that |
309 // the download was deleted if-and-only-if it was removed | 305 // the download was deleted if-and-only-if it was removed |
310 // from all queues. | 306 // from all queues. |
311 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 307 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
312 } else if (download->IsPartialDownload()) { | 308 } else if (download->IsPartialDownload()) { |
313 download->Cancel(false); | 309 download->Cancel(false); |
314 if (delegate_) | |
315 delegate_->UpdateItemInPersistentStore(download); | |
316 } | 310 } |
317 } | 311 } |
318 | 312 |
319 // At this point, all dangerous downloads have had their files removed | 313 // At this point, all dangerous downloads have had their files removed |
320 // and all in progress downloads have been cancelled. We can now delete | 314 // and all in progress downloads have been cancelled. We can now delete |
321 // anything left. | 315 // anything left. |
322 | 316 |
323 active_downloads_.clear(); | 317 active_downloads_.clear(); |
324 STLDeleteValues(&downloads_); | 318 STLDeleteValues(&downloads_); |
325 downloads_.clear(); | 319 downloads_.clear(); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 } | 404 } |
411 } | 405 } |
412 | 406 |
413 void DownloadManagerImpl::OnDownloadTargetDetermined( | 407 void DownloadManagerImpl::OnDownloadTargetDetermined( |
414 int32 download_id, | 408 int32 download_id, |
415 const FilePath& target_path, | 409 const FilePath& target_path, |
416 DownloadItem::TargetDisposition disposition, | 410 DownloadItem::TargetDisposition disposition, |
417 content::DownloadDangerType danger_type, | 411 content::DownloadDangerType danger_type, |
418 const FilePath& intermediate_path) { | 412 const FilePath& intermediate_path) { |
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
420 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | 414 DownloadMap::iterator download_iter = downloads_.find(download_id); |
421 if (download_iter != active_downloads_.end()) { | 415 if (download_iter == downloads_.end()) |
422 // Once DownloadItem::OnDownloadTargetDetermined() is called, we expect a | 416 return; |
423 // DownloadRenamedToIntermediateName() callback. This is necessary for the | 417 |
424 // download to proceed. | 418 // Once DownloadItem::OnDownloadTargetDetermined() is called, we expect a |
425 download_iter->second->OnDownloadTargetDetermined( | 419 // DownloadRenamedToIntermediateName() callback. This is necessary for the |
426 target_path, disposition, danger_type, intermediate_path); | 420 // download to proceed. |
427 } | 421 DownloadItemImpl* item = download_iter->second; |
| 422 item->OnDownloadTargetDetermined( |
| 423 target_path, disposition, danger_type, intermediate_path); |
| 424 ShowDownloadInBrowser(item); |
| 425 NotifyModelChanged(); |
428 } | 426 } |
429 | 427 |
430 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { | 428 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
432 for (DownloadMap::iterator it = downloads_.begin(); | 430 for (DownloadMap::iterator it = downloads_.begin(); |
433 it != downloads_.end(); ++it) { | 431 it != downloads_.end(); ++it) { |
434 DownloadItemImpl* item = it->second; | 432 DownloadItemImpl* item = it->second; |
435 if (item->IsPersisted()) | 433 CheckForFileRemoval(item); |
436 CheckForFileRemoval(item); | |
437 } | 434 } |
438 } | 435 } |
439 | 436 |
440 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { | 437 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 438 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
442 if (download_item->IsComplete() && | 439 if (download_item->IsComplete() && |
443 !download_item->GetFileExternallyRemoved()) { | 440 !download_item->GetFileExternallyRemoved()) { |
444 BrowserThread::PostTask( | 441 BrowserThread::PostTask( |
445 BrowserThread::FILE, FROM_HERE, | 442 BrowserThread::FILE, FROM_HERE, |
446 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, | 443 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 return bound_net_log; | 491 return bound_net_log; |
495 } | 492 } |
496 | 493 |
497 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 494 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
498 const FilePath& main_file_path, | 495 const FilePath& main_file_path, |
499 const GURL& page_url, | 496 const GURL& page_url, |
500 const std::string& mime_type, | 497 const std::string& mime_type, |
501 DownloadItem::Observer* observer) { | 498 DownloadItem::Observer* observer) { |
502 net::BoundNetLog bound_net_log = | 499 net::BoundNetLog bound_net_log = |
503 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 500 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
504 DownloadItemImpl* download = factory_->CreateSavePageItem( | 501 DownloadItemImpl* download_item = factory_->CreateSavePageItem( |
505 this, | 502 this, |
506 main_file_path, | 503 main_file_path, |
507 page_url, | 504 page_url, |
508 GetNextId(), | 505 GetNextId(), |
509 mime_type, | 506 mime_type, |
510 bound_net_log); | 507 bound_net_log); |
511 | 508 |
512 download->AddObserver(observer); | 509 download_item->AddObserver(observer); |
| 510 DCHECK(!ContainsKey(downloads_, download_item->GetId())); |
| 511 downloads_[download_item->GetId()] = download_item; |
| 512 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( |
| 513 this, download_item)); |
513 | 514 |
514 DCHECK(!ContainsKey(downloads_, download->GetId())); | 515 // TODO(asanka): Make the ui an observer. |
515 downloads_[download->GetId()] = download; | 516 ShowDownloadInBrowser(download_item); |
516 DCHECK(!SavePageData::Get(download)); | |
517 new SavePageData(download); | |
518 DCHECK(SavePageData::Get(download)); | |
519 | 517 |
520 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 518 return download_item; |
521 | |
522 // Will notify the observer in the callback. | |
523 if (delegate_) | |
524 delegate_->AddItemToPersistentStore(download); | |
525 | |
526 return download; | |
527 } | 519 } |
528 | 520 |
529 void DownloadManagerImpl::UpdateDownload(int32 download_id, | 521 void DownloadManagerImpl::UpdateDownload(int32 download_id, |
530 int64 bytes_so_far, | 522 int64 bytes_so_far, |
531 int64 bytes_per_sec, | 523 int64 bytes_per_sec, |
532 const std::string& hash_state) { | 524 const std::string& hash_state) { |
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 525 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
534 DownloadMap::iterator it = active_downloads_.find(download_id); | 526 DownloadMap::iterator it = active_downloads_.find(download_id); |
535 if (it != active_downloads_.end()) { | 527 if (it != active_downloads_.end()) { |
536 DownloadItemImpl* download = it->second; | 528 DownloadItemImpl* download = it->second; |
537 if (download->IsInProgress()) { | 529 if (download->IsInProgress()) { |
538 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); | 530 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); |
539 if (delegate_) | |
540 delegate_->UpdateItemInPersistentStore(download); | |
541 } | 531 } |
542 } | 532 } |
543 } | 533 } |
544 | 534 |
545 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, | 535 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, |
546 int64 size, | 536 int64 size, |
547 const std::string& hash) { | 537 const std::string& hash) { |
548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 538 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
549 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | 539 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
550 << " size = " << size; | 540 << " size = " << size; |
551 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 541 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
552 | 542 |
553 // If it's not in active_downloads_, that means it was cancelled; just | 543 // If it's not in active_downloads_, that means it was cancelled; just |
554 // ignore the notification. | 544 // ignore the notification. |
555 if (active_downloads_.count(download_id) == 0) | 545 if (active_downloads_.count(download_id) == 0) |
556 return; | 546 return; |
557 | 547 |
558 DownloadItemImpl* download = active_downloads_[download_id]; | 548 DownloadItemImpl* download = active_downloads_[download_id]; |
559 download->OnAllDataSaved(size, hash); | 549 download->OnAllDataSaved(size, hash); |
560 MaybeCompleteDownload(download); | 550 MaybeCompleteDownload(download); |
561 } | 551 } |
562 | 552 |
563 void DownloadManagerImpl::AssertStateConsistent( | 553 void DownloadManagerImpl::AssertStateConsistent( |
564 DownloadItemImpl* download) const { | 554 DownloadItemImpl* download) const { |
565 CHECK(ContainsKey(downloads_, download->GetId())); | 555 CHECK(ContainsKey(downloads_, download->GetId())); |
566 | 556 |
567 int64 state = download->GetState(); | 557 int64 state = download->GetState(); |
568 base::debug::Alias(&state); | 558 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()) | 559 if (DownloadItem::IN_PROGRESS == download->GetState()) |
576 CHECK(ContainsKey(active_downloads_, download->GetId())); | 560 CHECK(ContainsKey(active_downloads_, download->GetId())); |
577 } | 561 } |
578 | 562 |
579 bool DownloadManagerImpl::IsDownloadReadyForCompletion( | 563 bool DownloadManagerImpl::IsDownloadReadyForCompletion( |
580 DownloadItemImpl* download) { | 564 DownloadItemImpl* download) { |
581 // If we don't have all the data, the download is not ready for | 565 // If we don't have all the data, the download is not ready for |
582 // completion. | 566 // completion. |
583 if (!download->AllDataSaved()) | 567 if (!download->AllDataSaved()) |
584 return false; | 568 return false; |
585 | 569 |
586 // If the download is dangerous, but not yet validated, it's not ready for | 570 // If the download is dangerous, but not yet validated, it's not ready for |
587 // completion. | 571 // completion. |
588 if (download->GetSafetyState() == DownloadItem::DANGEROUS) | 572 if (download->GetSafetyState() == DownloadItem::DANGEROUS) |
589 return false; | 573 return false; |
590 | 574 |
591 // If the download isn't active (e.g. has been cancelled) it's not | 575 // If the download isn't active (e.g. has been cancelled) it's not |
592 // ready for completion. | 576 // ready for completion. |
593 if (active_downloads_.count(download->GetId()) == 0) | 577 if (active_downloads_.count(download->GetId()) == 0) |
594 return false; | 578 return false; |
595 | 579 |
596 // If the download hasn't been inserted into the history system | 580 // If the intermediate filename rename hasn't occurred, don't complete the |
597 // (which occurs strictly after file name determination, intermediate | 581 // download. |
598 // file rename, and UI display) then it's not ready for completion. | 582 if (download->GetTargetFilePath().DirName() != |
599 if (!download->IsPersisted()) | 583 download->GetFullPath().DirName()) |
| 584 return false; |
| 585 |
| 586 // If the target filename hasn't been determined, don't complete the download. |
| 587 if (download->GetTargetFilePath().empty()) |
600 return false; | 588 return false; |
601 | 589 |
602 return true; | 590 return true; |
603 } | 591 } |
604 | 592 |
605 // When SavePackage downloads MHTML to GData (see | 593 // When SavePackage downloads MHTML to GData (see |
606 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it | 594 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it |
607 // does for non-SavePackage downloads, but SavePackage downloads never satisfy | 595 // does for non-SavePackage downloads, but SavePackage downloads never satisfy |
608 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls | 596 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls |
609 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage | 597 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage |
(...skipping 13 matching lines...) Expand all Loading... |
623 | 611 |
624 // TODO(rdsmith): DCHECK that we only pass through this point | 612 // TODO(rdsmith): DCHECK that we only pass through this point |
625 // once per download. The natural way to do this is by a state | 613 // once per download. The natural way to do this is by a state |
626 // transition on the DownloadItem. | 614 // transition on the DownloadItem. |
627 | 615 |
628 // Confirm we're in the proper set of states to be here; | 616 // Confirm we're in the proper set of states to be here; |
629 // have all data, have a history handle, (validated or safe). | 617 // have all data, have a history handle, (validated or safe). |
630 DCHECK(download->IsInProgress()); | 618 DCHECK(download->IsInProgress()); |
631 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); | 619 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); |
632 DCHECK(download->AllDataSaved()); | 620 DCHECK(download->AllDataSaved()); |
633 DCHECK(download->IsPersisted()); | |
634 | 621 |
635 // Give the delegate a chance to override. It's ok to keep re-setting the | 622 // 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 | 623 // 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 | 624 // 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 | 625 // to point directly to the item instead of |this| when DownloadItem supports |
639 // weak-ptrs. | 626 // weak-ptrs. |
640 if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind( | 627 if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind( |
641 &DownloadManagerImpl::MaybeCompleteDownloadById, | 628 &DownloadManagerImpl::MaybeCompleteDownloadById, |
642 this, download->GetId()))) | 629 this, download->GetId()))) |
643 return; | 630 return; |
644 | 631 |
645 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " | 632 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
646 << download->DebugString(false); | 633 << download->DebugString(false); |
647 | 634 |
648 if (delegate_) | |
649 delegate_->UpdateItemInPersistentStore(download); | |
650 download->OnDownloadCompleting(); | 635 download->OnDownloadCompleting(); |
651 } | 636 } |
652 | 637 |
653 void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { | 638 void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { |
654 if (ContainsKey(active_downloads_, download_id)) | 639 if (ContainsKey(active_downloads_, download_id)) |
655 MaybeCompleteDownload(active_downloads_[download_id]); | 640 MaybeCompleteDownload(active_downloads_[download_id]); |
656 } | 641 } |
657 | 642 |
658 void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { | 643 void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { |
659 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 644 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
660 DCHECK(download); | 645 DCHECK(download); |
661 if (delegate_) | |
662 delegate_->UpdateItemInPersistentStore(download); | |
663 active_downloads_.erase(download->GetId()); | 646 active_downloads_.erase(download->GetId()); |
664 AssertStateConsistent(download); | 647 AssertStateConsistent(download); |
665 } | 648 } |
666 | 649 |
667 void DownloadManagerImpl::CancelDownload(int32 download_id) { | 650 void DownloadManagerImpl::CancelDownload(int32 download_id) { |
668 // A cancel at the right time could remove the download from the | 651 // A cancel at the right time could remove the download from the |
669 // |active_downloads_| map before we get here. | 652 // |active_downloads_| map before we get here. |
670 if (ContainsKey(active_downloads_, download_id)) | 653 if (ContainsKey(active_downloads_, download_id)) |
671 active_downloads_[download_id]->Cancel(true); | 654 active_downloads_[download_id]->Cancel(true); |
672 } | 655 } |
(...skipping 19 matching lines...) Expand all Loading... |
692 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 675 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
693 | 676 |
694 if (!ContainsKey(active_downloads_, download_id)) | 677 if (!ContainsKey(active_downloads_, download_id)) |
695 return; | 678 return; |
696 active_downloads_[download_id]->Interrupt(reason); | 679 active_downloads_[download_id]->Interrupt(reason); |
697 } | 680 } |
698 | 681 |
699 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 682 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
700 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 683 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
701 DCHECK(download); | 684 DCHECK(download); |
702 | 685 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 } | 686 } |
711 | 687 |
712 int DownloadManagerImpl::RemoveDownloadItems( | 688 int DownloadManagerImpl::RemoveDownloadItems( |
713 const DownloadItemImplVector& pending_deletes) { | 689 const DownloadItemImplVector& pending_deletes) { |
714 if (pending_deletes.empty()) | 690 if (pending_deletes.empty()) |
715 return 0; | 691 return 0; |
716 | 692 |
717 // Delete from internal maps. | 693 // Delete from internal maps. |
718 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 694 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
719 it != pending_deletes.end(); | 695 it != pending_deletes.end(); |
720 ++it) { | 696 ++it) { |
721 DownloadItemImpl* download = *it; | 697 DownloadItemImpl* download = *it; |
722 DCHECK(download); | 698 DCHECK(download); |
723 int32 download_id = download->GetId(); | 699 int32 download_id = download->GetId(); |
724 delete download; | 700 delete download; |
725 downloads_.erase(download_id); | 701 downloads_.erase(download_id); |
726 } | 702 } |
727 NotifyModelChanged(); | 703 NotifyModelChanged(); |
728 return static_cast<int>(pending_deletes.size()); | 704 return static_cast<int>(pending_deletes.size()); |
729 } | 705 } |
730 | 706 |
731 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { | 707 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
732 if (!download || | 708 if (!download || |
733 downloads_.find(download->GetId()) == downloads_.end()) | 709 downloads_.find(download->GetId()) == downloads_.end()) |
734 return; | 710 return; |
735 | 711 |
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. | 712 // Remove from our tables and delete. |
745 int downloads_count = | 713 int downloads_count = |
746 RemoveDownloadItems(DownloadItemImplVector(1, download)); | 714 RemoveDownloadItems(DownloadItemImplVector(1, download)); |
747 DCHECK_EQ(1, downloads_count); | 715 DCHECK_EQ(1, downloads_count); |
748 } | 716 } |
749 | 717 |
750 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, | 718 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, |
751 base::Time remove_end) { | 719 base::Time remove_end) { |
752 if (delegate_) | |
753 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); | |
754 | |
755 DownloadItemImplVector pending_deletes; | 720 DownloadItemImplVector pending_deletes; |
756 for (DownloadMap::const_iterator it = downloads_.begin(); | 721 for (DownloadMap::const_iterator it = downloads_.begin(); |
757 it != downloads_.end(); | 722 it != downloads_.end(); |
758 ++it) { | 723 ++it) { |
759 DownloadItemImpl* download = it->second; | 724 DownloadItemImpl* download = it->second; |
760 if (download->IsPersisted() && | 725 if (download->GetStartTime() >= remove_begin && |
761 download->GetStartTime() >= remove_begin && | |
762 (remove_end.is_null() || download->GetStartTime() < remove_end) && | 726 (remove_end.is_null() || download->GetStartTime() < remove_end) && |
763 (download->IsComplete() || download->IsCancelled())) { | 727 (download->IsComplete() || download->IsCancelled())) { |
764 AssertStateConsistent(download); | 728 AssertStateConsistent(download); |
765 download->NotifyRemoved(); | 729 download->NotifyRemoved(); |
766 pending_deletes.push_back(download); | 730 pending_deletes.push_back(download); |
767 } | 731 } |
768 } | 732 } |
769 return RemoveDownloadItems(pending_deletes); | 733 return RemoveDownloadItems(pending_deletes); |
770 } | 734 } |
771 | 735 |
(...skipping 26 matching lines...) Expand all Loading... |
798 // observers. | 762 // observers. |
799 observer->ModelChanged(this); | 763 observer->ModelChanged(this); |
800 } | 764 } |
801 | 765 |
802 void DownloadManagerImpl::RemoveObserver(Observer* observer) { | 766 void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
803 observers_.RemoveObserver(observer); | 767 observers_.RemoveObserver(observer); |
804 } | 768 } |
805 | 769 |
806 // Operations posted to us from the history service ---------------------------- | 770 // Operations posted to us from the history service ---------------------------- |
807 | 771 |
808 // The history service has retrieved all download entries. 'entries' contains | 772 DownloadItem* DownloadManagerImpl::CreateDownloadItem( |
809 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 773 const FilePath& path, |
810 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 774 const GURL& url, |
811 std::vector<DownloadPersistentStoreInfo>* entries) { | 775 const GURL& referrer_url, |
812 history_size_ = entries->size(); | 776 const base::Time& start_time, |
813 for (size_t i = 0; i < entries->size(); ++i) { | 777 const base::Time& end_time, |
814 int64 db_handle = entries->at(i).db_handle; | 778 int64 received_bytes, |
815 base::debug::Alias(&db_handle); | 779 int64 total_bytes, |
816 | 780 DownloadItem::DownloadState state, |
817 net::BoundNetLog bound_net_log = | 781 bool opened) { |
818 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 782 DownloadItemImpl* item = factory_->CreatePersistedItem( |
819 DownloadItemImpl* download = factory_->CreatePersistedItem( | 783 this, |
820 this, GetNextId(), entries->at(i), bound_net_log); | 784 GetNextId(), |
821 DCHECK(!ContainsKey(downloads_, download->GetId())); | 785 path, |
822 downloads_[download->GetId()] = download; | 786 url, |
823 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 787 referrer_url, |
824 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 788 start_time, |
825 << " download = " << download->DebugString(true); | 789 end_time, |
826 } | 790 received_bytes, |
| 791 total_bytes, |
| 792 state, |
| 793 opened, |
| 794 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD)); |
| 795 DCHECK(!ContainsKey(downloads_, item->GetId())); |
| 796 downloads_[item->GetId()] = item; |
| 797 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); |
| 798 VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true); |
827 NotifyModelChanged(); | 799 NotifyModelChanged(); |
828 CheckForHistoryFilesRemoval(); | 800 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 } | 801 } |
897 | 802 |
898 void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { | 803 void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { |
899 // The 'contents' may no longer exist if the user closed the contents before | 804 // The 'contents' may no longer exist if the user closed the contents before |
900 // we get this start completion event. | 805 // we get this start completion event. |
901 WebContents* content = download->GetWebContents(); | 806 WebContents* content = download->GetWebContents(); |
902 | 807 |
903 // If the contents no longer exists, we ask the embedder to suggest another | 808 // If the contents no longer exists, we ask the embedder to suggest another |
904 // contents. | 809 // contents. |
905 if (!content && delegate_) | 810 if (!content && delegate_) |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
965 DownloadSet remainder; | 870 DownloadSet remainder; |
966 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); | 871 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
967 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), | 872 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
968 all_downloads.begin(), all_downloads.end(), | 873 all_downloads.begin(), all_downloads.end(), |
969 insert_it); | 874 insert_it); |
970 DCHECK(remainder.empty()); | 875 DCHECK(remainder.empty()); |
971 } | 876 } |
972 #endif | 877 #endif |
973 } | 878 } |
974 | 879 |
975 // SavePackage will call SavePageDownloadFinished upon completion/cancellation. | |
976 // The history callback will call OnSavePageItemAddedToPersistentStore. | |
977 // If the download finishes before the history callback, | |
978 // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring | |
979 // that the history event is update regardless of the order in which these two | |
980 // events complete. | |
981 // If something removes the download item from the download manager (Remove, | |
982 // Shutdown) the result will be that the SavePage system will not be able to | |
983 // properly update the download item (which no longer exists) or the download | |
984 // history, but the action will complete properly anyway. This may lead to the | |
985 // history entry being wrong on a reload of chrome (specifically in the case of | |
986 // Initiation -> History Callback -> Removal -> Completion), but there's no way | |
987 // to solve that without canceling on Remove (which would then update the DB). | |
988 | |
989 void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore( | |
990 DownloadItemImpl* item) { | |
991 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
992 | |
993 // Finalize this download if it finished before the history callback. | |
994 if (!item->IsInProgress()) | |
995 SavePageDownloadFinished(item); | |
996 } | |
997 | |
998 void DownloadManagerImpl::SavePageDownloadFinished( | |
999 content::DownloadItem* download) { | |
1000 if (download->IsPersisted()) { | |
1001 if (delegate_) | |
1002 delegate_->UpdateItemInPersistentStore(download); | |
1003 } | |
1004 } | |
1005 | |
1006 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { | 880 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { |
1007 if (delegate_) | |
1008 delegate_->UpdateItemInPersistentStore(download); | |
1009 int num_unopened = 0; | 881 int num_unopened = 0; |
1010 for (DownloadMap::iterator it = downloads_.begin(); | 882 for (DownloadMap::iterator it = downloads_.begin(); |
1011 it != downloads_.end(); ++it) { | 883 it != downloads_.end(); ++it) { |
1012 DownloadItemImpl* item = it->second; | 884 DownloadItemImpl* item = it->second; |
1013 if (item->IsComplete() && | 885 if (item->IsComplete() && |
1014 !item->GetOpened()) | 886 !item->GetOpened()) |
1015 ++num_unopened; | 887 ++num_unopened; |
1016 } | 888 } |
1017 download_stats::RecordOpensOutstanding(num_unopened); | 889 download_stats::RecordOpensOutstanding(num_unopened); |
1018 } | 890 } |
1019 | 891 |
| 892 // TODO(benjhayden) MaybeCompleteDownload should be moved into DownloadItemImpl. |
1020 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 893 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
1021 DownloadItemImpl* download) { | 894 DownloadItemImpl* download) { |
1022 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 895 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1023 // download->GetFullPath() is only expected to be meaningful after this | 896 MaybeCompleteDownload(download); |
1024 // callback is received. Therefore we can now add the download to a persistent | |
1025 // store. If the rename failed, we receive an OnDownloadInterrupted() call | |
1026 // before we receive the DownloadRenamedToIntermediateName() call. | |
1027 if (delegate_) { | |
1028 delegate_->AddItemToPersistentStore(download); | |
1029 } else { | |
1030 OnItemAddedToPersistentStore(download->GetId(), | |
1031 DownloadItem::kUninitializedHandle); | |
1032 } | |
1033 } | 897 } |
1034 | |
1035 void DownloadManagerImpl::DownloadRenamedToFinalName( | |
1036 DownloadItemImpl* download) { | |
1037 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1038 // If the rename failed, we receive an OnDownloadInterrupted() call before we | |
1039 // receive the DownloadRenamedToFinalName() call. | |
1040 if (delegate_) { | |
1041 delegate_->UpdatePathForItemInPersistentStore( | |
1042 download, download->GetFullPath()); | |
1043 } | |
1044 } | |
OLD | NEW |