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 15 matching lines...) Expand all Loading... |
26 #include "content/browser/download/download_item_impl.h" | 26 #include "content/browser/download/download_item_impl.h" |
27 #include "content/browser/download/download_stats.h" | 27 #include "content/browser/download/download_stats.h" |
28 #include "content/browser/renderer_host/render_view_host_impl.h" | 28 #include "content/browser/renderer_host/render_view_host_impl.h" |
29 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 29 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
30 #include "content/browser/web_contents/web_contents_impl.h" | 30 #include "content/browser/web_contents/web_contents_impl.h" |
31 #include "content/public/browser/browser_context.h" | 31 #include "content/public/browser/browser_context.h" |
32 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
33 #include "content/public/browser/content_browser_client.h" | 33 #include "content/public/browser/content_browser_client.h" |
34 #include "content/public/browser/download_interrupt_reasons.h" | 34 #include "content/public/browser/download_interrupt_reasons.h" |
35 #include "content/public/browser/download_manager_delegate.h" | 35 #include "content/public/browser/download_manager_delegate.h" |
36 #include "content/public/browser/download_persistent_store_info.h" | |
37 #include "content/public/browser/download_url_parameters.h" | 36 #include "content/public/browser/download_url_parameters.h" |
38 #include "content/public/browser/notification_service.h" | 37 #include "content/public/browser/notification_service.h" |
39 #include "content/public/browser/notification_types.h" | 38 #include "content/public/browser/notification_types.h" |
40 #include "content/public/browser/render_process_host.h" | 39 #include "content/public/browser/render_process_host.h" |
41 #include "content/public/browser/resource_context.h" | 40 #include "content/public/browser/resource_context.h" |
42 #include "content/public/browser/web_contents_delegate.h" | 41 #include "content/public/browser/web_contents_delegate.h" |
43 #include "net/base/load_flags.h" | 42 #include "net/base/load_flags.h" |
44 #include "net/base/upload_data.h" | 43 #include "net/base/upload_data.h" |
45 #include "net/url_request/url_request_context.h" | 44 #include "net/url_request/url_request_context.h" |
46 #include "webkit/glue/webkit_glue.h" | 45 #include "webkit/glue/webkit_glue.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } | 133 } |
135 | 134 |
136 class DownloadItemFactoryImpl : public DownloadItemFactory { | 135 class DownloadItemFactoryImpl : public DownloadItemFactory { |
137 public: | 136 public: |
138 DownloadItemFactoryImpl() {} | 137 DownloadItemFactoryImpl() {} |
139 virtual ~DownloadItemFactoryImpl() {} | 138 virtual ~DownloadItemFactoryImpl() {} |
140 | 139 |
141 virtual DownloadItemImpl* CreatePersistedItem( | 140 virtual DownloadItemImpl* CreatePersistedItem( |
142 DownloadItemImplDelegate* delegate, | 141 DownloadItemImplDelegate* delegate, |
143 DownloadId download_id, | 142 DownloadId download_id, |
144 const DownloadPersistentStoreInfo& info, | 143 const FilePath& path, |
| 144 const GURL& url, |
| 145 const GURL& referrer_url, |
| 146 const base::Time& start_time, |
| 147 const base::Time& end_time, |
| 148 int64 received_bytes, |
| 149 int64 total_bytes, |
| 150 DownloadItem::DownloadState state, |
| 151 bool opened, |
145 const net::BoundNetLog& bound_net_log) OVERRIDE { | 152 const net::BoundNetLog& bound_net_log) OVERRIDE { |
146 return new DownloadItemImpl(delegate, download_id, info, bound_net_log); | 153 return new DownloadItemImpl( |
| 154 delegate, |
| 155 download_id, |
| 156 path, |
| 157 url, |
| 158 referrer_url, |
| 159 start_time, |
| 160 end_time, |
| 161 received_bytes, |
| 162 total_bytes, |
| 163 state, |
| 164 opened, |
| 165 bound_net_log); |
147 } | 166 } |
148 | 167 |
149 virtual DownloadItemImpl* CreateActiveItem( | 168 virtual DownloadItemImpl* CreateActiveItem( |
150 DownloadItemImplDelegate* delegate, | 169 DownloadItemImplDelegate* delegate, |
151 const DownloadCreateInfo& info, | 170 const DownloadCreateInfo& info, |
152 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 171 scoped_ptr<DownloadRequestHandleInterface> request_handle, |
153 const net::BoundNetLog& bound_net_log) OVERRIDE { | 172 const net::BoundNetLog& bound_net_log) OVERRIDE { |
154 return new DownloadItemImpl(delegate, info, request_handle.Pass(), | 173 return new DownloadItemImpl(delegate, info, request_handle.Pass(), |
155 bound_net_log); | 174 bound_net_log); |
156 } | 175 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 // from the disk. This may or may not result in it being | 292 // from the disk. This may or may not result in it being |
274 // removed from the DownloadManager queues and deleted | 293 // removed from the DownloadManager queues and deleted |
275 // (specifically, DownloadManager::DownloadRemoved only | 294 // (specifically, DownloadManager::DownloadRemoved only |
276 // removes and deletes it if it's known to the history service) | 295 // removes and deletes it if it's known to the history service) |
277 // so the only thing we know after calling this function is that | 296 // so the only thing we know after calling this function is that |
278 // the download was deleted if-and-only-if it was removed | 297 // the download was deleted if-and-only-if it was removed |
279 // from all queues. | 298 // from all queues. |
280 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 299 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
281 } else if (download->IsPartialDownload()) { | 300 } else if (download->IsPartialDownload()) { |
282 download->Cancel(false); | 301 download->Cancel(false); |
283 if (delegate_) | |
284 delegate_->UpdateItemInPersistentStore(download); | |
285 } | 302 } |
286 } | 303 } |
287 | 304 |
288 // At this point, all dangerous downloads have had their files removed | 305 // At this point, all dangerous downloads have had their files removed |
289 // and all in progress downloads have been cancelled. We can now delete | 306 // and all in progress downloads have been cancelled. We can now delete |
290 // anything left. | 307 // anything left. |
291 | 308 |
292 // We delete the downloads before clearing the active_downloads_ map | 309 // We delete the downloads before clearing the active_downloads_ map |
293 // so that downloads in the COMPLETING_INTERNAL state (which will have | 310 // so that downloads in the COMPLETING_INTERNAL state (which will have |
294 // ignored the Cancel() above) will still show up in active_downloads_ | 311 // ignored the Cancel() above) will still show up in active_downloads_ |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 367 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
351 | 368 |
352 return download; | 369 return download; |
353 } | 370 } |
354 | 371 |
355 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { | 372 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 373 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
357 for (DownloadMap::iterator it = downloads_.begin(); | 374 for (DownloadMap::iterator it = downloads_.begin(); |
358 it != downloads_.end(); ++it) { | 375 it != downloads_.end(); ++it) { |
359 DownloadItemImpl* item = it->second; | 376 DownloadItemImpl* item = it->second; |
360 if (item->IsPersisted()) | 377 CheckForFileRemoval(item); |
361 CheckForFileRemoval(item); | |
362 } | 378 } |
363 } | 379 } |
364 | 380 |
365 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { | 381 void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
366 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
367 if (download_item->IsComplete() && | 383 if (download_item->IsComplete() && |
368 !download_item->GetFileExternallyRemoved()) { | 384 !download_item->GetFileExternallyRemoved()) { |
369 BrowserThread::PostTask( | 385 BrowserThread::PostTask( |
370 BrowserThread::FILE, FROM_HERE, | 386 BrowserThread::FILE, FROM_HERE, |
371 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, | 387 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 return download; | 432 return download; |
417 } | 433 } |
418 | 434 |
419 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 435 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
420 const FilePath& main_file_path, | 436 const FilePath& main_file_path, |
421 const GURL& page_url, | 437 const GURL& page_url, |
422 const std::string& mime_type, | 438 const std::string& mime_type, |
423 DownloadItem::Observer* observer) { | 439 DownloadItem::Observer* observer) { |
424 net::BoundNetLog bound_net_log = | 440 net::BoundNetLog bound_net_log = |
425 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 441 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
426 DownloadItemImpl* download = item_factory_->CreateSavePageItem( | 442 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( |
427 this, | 443 this, |
428 main_file_path, | 444 main_file_path, |
429 page_url, | 445 page_url, |
430 GetNextId(), | 446 GetNextId(), |
431 mime_type, | 447 mime_type, |
432 bound_net_log); | 448 bound_net_log); |
433 | 449 |
434 download->AddObserver(observer); | 450 download_item->AddObserver(observer); |
| 451 DCHECK(!ContainsKey(downloads_, download_item->GetId())); |
| 452 downloads_[download_item->GetId()] = download_item; |
| 453 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( |
| 454 this, download_item)); |
435 | 455 |
436 DCHECK(!ContainsKey(downloads_, download->GetId())); | 456 // TODO(asanka): Make the ui an observer. |
437 downloads_[download->GetId()] = download; | 457 ShowDownloadInBrowser(download_item); |
438 | 458 |
439 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 459 return download_item; |
440 | |
441 // Will notify the observer in the callback. | |
442 if (delegate_) | |
443 delegate_->AddItemToPersistentStore(download); | |
444 | |
445 return download; | |
446 } | 460 } |
447 | 461 |
448 void DownloadManagerImpl::AssertStateConsistent( | 462 void DownloadManagerImpl::AssertStateConsistent( |
449 DownloadItemImpl* download) const { | 463 DownloadItemImpl* download) const { |
450 CHECK(ContainsKey(downloads_, download->GetId())); | 464 CHECK(ContainsKey(downloads_, download->GetId())); |
451 | 465 |
452 int64 state = download->GetState(); | 466 int64 state = download->GetState(); |
453 base::debug::Alias(&state); | 467 base::debug::Alias(&state); |
454 if (ContainsKey(active_downloads_, download->GetId())) { | |
455 if (download->IsPersisted()) | |
456 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | |
457 if (DownloadItem::IN_PROGRESS != download->GetState()) | |
458 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle()); | |
459 } | |
460 if (DownloadItem::IN_PROGRESS == download->GetState()) | 468 if (DownloadItem::IN_PROGRESS == download->GetState()) |
461 CHECK(ContainsKey(active_downloads_, download->GetId())); | 469 CHECK(ContainsKey(active_downloads_, download->GetId())); |
462 } | 470 } |
463 | 471 |
464 void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { | 472 void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { |
465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 473 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
466 DCHECK(download); | 474 DCHECK(download); |
467 if (delegate_) | |
468 delegate_->UpdateItemInPersistentStore(download); | |
469 active_downloads_.erase(download->GetId()); | 475 active_downloads_.erase(download->GetId()); |
470 AssertStateConsistent(download); | 476 AssertStateConsistent(download); |
471 } | 477 } |
472 | 478 |
473 void DownloadManagerImpl::CancelDownload(int32 download_id) { | 479 void DownloadManagerImpl::CancelDownload(int32 download_id) { |
474 // A cancel at the right time could remove the download from the | 480 // A cancel at the right time could remove the download from the |
475 // |active_downloads_| map before we get here. | 481 // |active_downloads_| map before we get here. |
476 if (ContainsKey(active_downloads_, download_id)) | 482 if (ContainsKey(active_downloads_, download_id)) |
477 active_downloads_[download_id]->Cancel(true); | 483 active_downloads_[download_id]->Cancel(true); |
478 } | 484 } |
479 | 485 |
480 void DownloadManagerImpl::UpdatePersistence(DownloadItemImpl* download) { | |
481 if (delegate_) | |
482 delegate_->UpdateItemInPersistentStore(download); | |
483 } | |
484 | |
485 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { | 486 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
487 | 488 |
488 VLOG(20) << __FUNCTION__ << "()" | 489 VLOG(20) << __FUNCTION__ << "()" |
489 << " download = " << download->DebugString(true); | 490 << " download = " << download->DebugString(true); |
490 | 491 |
491 RemoveFromActiveList(download); | 492 RemoveFromActiveList(download); |
492 // This function is called from the DownloadItem, so DI state | 493 // This function is called from the DownloadItem, so DI state |
493 // should already have been updated. | 494 // should already have been updated. |
494 AssertStateConsistent(download); | 495 AssertStateConsistent(download); |
495 } | 496 } |
496 | 497 |
497 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 498 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
498 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 499 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
499 DCHECK(download); | 500 DCHECK(download); |
500 | 501 active_downloads_.erase(download->GetId()); |
501 // Clean up will happen when the history system create callback runs if we | |
502 // don't have a valid db_handle yet. | |
503 if (download->IsPersisted()) { | |
504 active_downloads_.erase(download->GetId()); | |
505 if (delegate_) | |
506 delegate_->UpdateItemInPersistentStore(download); | |
507 } | |
508 } | 502 } |
509 | 503 |
510 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( | 504 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
511 scoped_ptr<DownloadItemFactory> item_factory) { | 505 scoped_ptr<DownloadItemFactory> item_factory) { |
512 item_factory_ = item_factory.Pass(); | 506 item_factory_ = item_factory.Pass(); |
513 } | 507 } |
514 | 508 |
515 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( | 509 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
516 scoped_ptr<DownloadFileFactory> file_factory) { | 510 scoped_ptr<DownloadFileFactory> file_factory) { |
517 file_factory_ = file_factory.Pass(); | 511 file_factory_ = file_factory.Pass(); |
(...skipping 20 matching lines...) Expand all Loading... |
538 } | 532 } |
539 NotifyModelChanged(); | 533 NotifyModelChanged(); |
540 return static_cast<int>(pending_deletes.size()); | 534 return static_cast<int>(pending_deletes.size()); |
541 } | 535 } |
542 | 536 |
543 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { | 537 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
544 if (!download || | 538 if (!download || |
545 downloads_.find(download->GetId()) == downloads_.end()) | 539 downloads_.find(download->GetId()) == downloads_.end()) |
546 return; | 540 return; |
547 | 541 |
548 // TODO(benjhayden,rdsmith): Remove this. | |
549 if (!download->IsPersisted()) | |
550 return; | |
551 | |
552 // Make history update. | |
553 if (delegate_) | |
554 delegate_->RemoveItemFromPersistentStore(download); | |
555 | |
556 // Remove from our tables and delete. | 542 // Remove from our tables and delete. |
557 int downloads_count = | 543 int downloads_count = |
558 RemoveDownloadItems(DownloadItemImplVector(1, download)); | 544 RemoveDownloadItems(DownloadItemImplVector(1, download)); |
559 DCHECK_EQ(1, downloads_count); | 545 DCHECK_EQ(1, downloads_count); |
560 } | 546 } |
561 | 547 |
562 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, | 548 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, |
563 base::Time remove_end) { | 549 base::Time remove_end) { |
564 if (delegate_) | |
565 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); | |
566 | |
567 DownloadItemImplVector pending_deletes; | 550 DownloadItemImplVector pending_deletes; |
568 for (DownloadMap::const_iterator it = downloads_.begin(); | 551 for (DownloadMap::const_iterator it = downloads_.begin(); |
569 it != downloads_.end(); | 552 it != downloads_.end(); |
570 ++it) { | 553 ++it) { |
571 DownloadItemImpl* download = it->second; | 554 DownloadItemImpl* download = it->second; |
572 if (download->IsPersisted() && | 555 if (download->GetStartTime() >= remove_begin && |
573 download->GetStartTime() >= remove_begin && | |
574 (remove_end.is_null() || download->GetStartTime() < remove_end) && | 556 (remove_end.is_null() || download->GetStartTime() < remove_end) && |
575 (download->IsComplete() || download->IsCancelled())) { | 557 (download->IsComplete() || download->IsCancelled())) { |
576 AssertStateConsistent(download); | 558 AssertStateConsistent(download); |
577 download->NotifyRemoved(); | 559 download->NotifyRemoved(); |
578 pending_deletes.push_back(download); | 560 pending_deletes.push_back(download); |
579 } | 561 } |
580 } | 562 } |
581 return RemoveDownloadItems(pending_deletes); | 563 return RemoveDownloadItems(pending_deletes); |
582 } | 564 } |
583 | 565 |
(...skipping 24 matching lines...) Expand all Loading... |
608 // TODO: It is the responsibility of the observers to query the | 590 // TODO: It is the responsibility of the observers to query the |
609 // DownloadManager. Remove the following call from here and update all | 591 // DownloadManager. Remove the following call from here and update all |
610 // observers. | 592 // observers. |
611 observer->ModelChanged(this); | 593 observer->ModelChanged(this); |
612 } | 594 } |
613 | 595 |
614 void DownloadManagerImpl::RemoveObserver(Observer* observer) { | 596 void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
615 observers_.RemoveObserver(observer); | 597 observers_.RemoveObserver(observer); |
616 } | 598 } |
617 | 599 |
618 // Operations posted to us from the history service ---------------------------- | 600 DownloadItem* DownloadManagerImpl::CreateDownloadItem( |
619 | 601 const FilePath& path, |
620 // The history service has retrieved all download entries. 'entries' contains | 602 const GURL& url, |
621 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 603 const GURL& referrer_url, |
622 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 604 const base::Time& start_time, |
623 std::vector<DownloadPersistentStoreInfo>* entries) { | 605 const base::Time& end_time, |
624 history_size_ = entries->size(); | 606 int64 received_bytes, |
625 for (size_t i = 0; i < entries->size(); ++i) { | 607 int64 total_bytes, |
626 int64 db_handle = entries->at(i).db_handle; | 608 DownloadItem::DownloadState state, |
627 base::debug::Alias(&db_handle); | 609 bool opened) { |
628 | 610 DownloadItemImpl* item = item_factory_->CreatePersistedItem( |
629 net::BoundNetLog bound_net_log = | 611 this, |
630 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 612 GetNextId(), |
631 DownloadItemImpl* download = item_factory_->CreatePersistedItem( | 613 path, |
632 this, GetNextId(), entries->at(i), bound_net_log); | 614 url, |
633 DCHECK(!ContainsKey(downloads_, download->GetId())); | 615 referrer_url, |
634 downloads_[download->GetId()] = download; | 616 start_time, |
635 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 617 end_time, |
636 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 618 received_bytes, |
637 << " download = " << download->DebugString(true); | 619 total_bytes, |
638 } | 620 state, |
| 621 opened, |
| 622 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD)); |
| 623 DCHECK(!ContainsKey(downloads_, item->GetId())); |
| 624 downloads_[item->GetId()] = item; |
| 625 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); |
| 626 VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true); |
639 NotifyModelChanged(); | 627 NotifyModelChanged(); |
640 CheckForHistoryFilesRemoval(); | 628 return item; |
641 } | 629 } |
642 | 630 |
643 void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItemImpl* download, | 631 // TODO(asanka) Move into an observer. |
644 int64 db_handle) { | |
645 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
646 DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle); | |
647 DCHECK(!download->IsPersisted()); | |
648 download->SetDbHandle(db_handle); | |
649 download->SetIsPersisted(); | |
650 | |
651 RecordHistorySize(history_size_); | |
652 // Not counting |download|. | |
653 ++history_size_; | |
654 | |
655 // Show in the appropriate browser UI. | |
656 // This includes buttons to save or cancel, for a dangerous download. | |
657 ShowDownloadInBrowser(download); | |
658 | |
659 // Inform interested objects about the new download. | |
660 NotifyModelChanged(); | |
661 } | |
662 | |
663 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, | |
664 int64 db_handle) { | |
665 // It's valid that we don't find a matching item, i.e. on shutdown. | |
666 if (!ContainsKey(downloads_, download_id)) | |
667 return; | |
668 | |
669 DownloadItemImpl* item = downloads_[download_id]; | |
670 AddDownloadItemToHistory(item, db_handle); | |
671 if (item->IsSavePackageDownload()) { | |
672 OnSavePageItemAddedToPersistentStore(item); | |
673 } else { | |
674 OnDownloadItemAddedToPersistentStore(item); | |
675 } | |
676 } | |
677 | |
678 // Once the new DownloadItem has been committed to the persistent store, | |
679 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), | |
680 // show it in the browser (TODO(benjhayden) the ui should observe us instead), | |
681 // and notify observers (TODO(benjhayden) observers should be able to see the | |
682 // item when it's created so they can observe it directly. Are there any | |
683 // clients that actually need to know when the item is added to the history?). | |
684 void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore( | |
685 DownloadItemImpl* item) { | |
686 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
687 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << item->GetDbHandle() | |
688 << " download_id = " << item->GetId() | |
689 << " download = " << item->DebugString(true); | |
690 | |
691 // If the download is still in progress, try to complete it. | |
692 // | |
693 // Otherwise, download has been cancelled or interrupted before we've | |
694 // received the DB handle. We post one final message to the history | |
695 // service so that it can be properly in sync with the DownloadItem's | |
696 // completion status, and also inform any observers so that they get | |
697 // more than just the start notification. | |
698 if (item->IsInProgress()) { | |
699 item->MaybeCompleteDownload(); | |
700 } else { | |
701 DCHECK(item->IsCancelled()); | |
702 active_downloads_.erase(item->GetId()); | |
703 if (delegate_) | |
704 delegate_->UpdateItemInPersistentStore(item); | |
705 item->UpdateObservers(); | |
706 } | |
707 } | |
708 | |
709 void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { | 632 void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { |
710 // The 'contents' may no longer exist if the user closed the contents before | 633 // The 'contents' may no longer exist if the user closed the contents before |
711 // we get this start completion event. | 634 // we get this start completion event. |
712 WebContents* content = download->GetWebContents(); | 635 WebContents* content = download->GetWebContents(); |
713 | 636 |
714 // If the contents no longer exists, we ask the embedder to suggest another | 637 // If the contents no longer exists, we ask the embedder to suggest another |
715 // contents. | 638 // contents. |
716 if (!content && delegate_) | 639 if (!content && delegate_) |
717 content = delegate_->GetAlternativeWebContentsToNotifyForDownload(); | 640 content = delegate_->GetAlternativeWebContentsToNotifyForDownload(); |
718 | 641 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 DownloadSet remainder; | 699 DownloadSet remainder; |
777 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); | 700 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
778 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), | 701 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
779 all_downloads.begin(), all_downloads.end(), | 702 all_downloads.begin(), all_downloads.end(), |
780 insert_it); | 703 insert_it); |
781 DCHECK(remainder.empty()); | 704 DCHECK(remainder.empty()); |
782 } | 705 } |
783 #endif | 706 #endif |
784 } | 707 } |
785 | 708 |
786 // SavePackage will call SavePageDownloadFinished upon completion/cancellation. | |
787 // The history callback will call OnSavePageItemAddedToPersistentStore. | |
788 // If the download finishes before the history callback, | |
789 // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring | |
790 // that the history event is update regardless of the order in which these two | |
791 // events complete. | |
792 // If something removes the download item from the download manager (Remove, | |
793 // Shutdown) the result will be that the SavePage system will not be able to | |
794 // properly update the download item (which no longer exists) or the download | |
795 // history, but the action will complete properly anyway. This may lead to the | |
796 // history entry being wrong on a reload of chrome (specifically in the case of | |
797 // Initiation -> History Callback -> Removal -> Completion), but there's no way | |
798 // to solve that without canceling on Remove (which would then update the DB). | |
799 | |
800 void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore( | |
801 DownloadItemImpl* item) { | |
802 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
803 | |
804 // Finalize this download if it finished before the history callback. | |
805 if (!item->IsInProgress()) | |
806 SavePageDownloadFinished(item); | |
807 } | |
808 | |
809 void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) { | |
810 if (download->IsPersisted()) { | |
811 if (delegate_) | |
812 delegate_->UpdateItemInPersistentStore(download); | |
813 } | |
814 } | |
815 | |
816 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { | 709 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { |
817 if (delegate_) | |
818 delegate_->UpdateItemInPersistentStore(download); | |
819 int num_unopened = 0; | 710 int num_unopened = 0; |
820 for (DownloadMap::iterator it = downloads_.begin(); | 711 for (DownloadMap::iterator it = downloads_.begin(); |
821 it != downloads_.end(); ++it) { | 712 it != downloads_.end(); ++it) { |
822 DownloadItemImpl* item = it->second; | 713 DownloadItemImpl* item = it->second; |
823 if (item->IsComplete() && | 714 if (item->IsComplete() && |
824 !item->GetOpened()) | 715 !item->GetOpened()) |
825 ++num_unopened; | 716 ++num_unopened; |
826 } | 717 } |
827 RecordOpensOutstanding(num_unopened); | 718 RecordOpensOutstanding(num_unopened); |
828 } | 719 } |
829 | 720 |
830 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | |
831 DownloadItemImpl* download) { | |
832 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
833 // download->GetFullPath() is only expected to be meaningful after this | |
834 // callback is received. Therefore we can now add the download to a persistent | |
835 // store. If the rename failed, we processed an interrupt | |
836 // before we receive the DownloadRenamedToIntermediateName() call. | |
837 if (delegate_) { | |
838 delegate_->AddItemToPersistentStore(download); | |
839 } else { | |
840 OnItemAddedToPersistentStore(download->GetId(), | |
841 DownloadItem::kUninitializedHandle); | |
842 } | |
843 } | |
844 | |
845 void DownloadManagerImpl::DownloadRenamedToFinalName( | |
846 DownloadItemImpl* download) { | |
847 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
848 // If the rename failed, we processed an interrupt before we get here. | |
849 if (delegate_) { | |
850 delegate_->UpdatePathForItemInPersistentStore( | |
851 download, download->GetFullPath()); | |
852 } | |
853 } | |
854 | |
855 } // namespace content | 721 } // namespace content |
OLD | NEW |