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 19 matching lines...) Expand all Loading... |
537 downloads_.erase(download_id); | 531 downloads_.erase(download_id); |
538 } | 532 } |
539 return static_cast<int>(pending_deletes.size()); | 533 return static_cast<int>(pending_deletes.size()); |
540 } | 534 } |
541 | 535 |
542 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { | 536 void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
543 if (!download || | 537 if (!download || |
544 downloads_.find(download->GetId()) == downloads_.end()) | 538 downloads_.find(download->GetId()) == downloads_.end()) |
545 return; | 539 return; |
546 | 540 |
547 // TODO(benjhayden,rdsmith): Remove this. | |
548 if (!download->IsPersisted()) | |
549 return; | |
550 | |
551 // Make history update. | |
552 if (delegate_) | |
553 delegate_->RemoveItemFromPersistentStore(download); | |
554 | |
555 // Remove from our tables and delete. | 541 // Remove from our tables and delete. |
556 int downloads_count = | 542 int downloads_count = |
557 RemoveDownloadItems(DownloadItemImplVector(1, download)); | 543 RemoveDownloadItems(DownloadItemImplVector(1, download)); |
558 DCHECK_EQ(1, downloads_count); | 544 DCHECK_EQ(1, downloads_count); |
559 } | 545 } |
560 | 546 |
561 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, | 547 int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, |
562 base::Time remove_end) { | 548 base::Time remove_end) { |
563 if (delegate_) | |
564 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); | |
565 | |
566 DownloadItemImplVector pending_deletes; | 549 DownloadItemImplVector pending_deletes; |
567 for (DownloadMap::const_iterator it = downloads_.begin(); | 550 for (DownloadMap::const_iterator it = downloads_.begin(); |
568 it != downloads_.end(); | 551 it != downloads_.end(); |
569 ++it) { | 552 ++it) { |
570 DownloadItemImpl* download = it->second; | 553 DownloadItemImpl* download = it->second; |
571 if (download->IsPersisted() && | 554 if (download->GetStartTime() >= remove_begin && |
572 download->GetStartTime() >= remove_begin && | |
573 (remove_end.is_null() || download->GetStartTime() < remove_end) && | 555 (remove_end.is_null() || download->GetStartTime() < remove_end) && |
574 (download->IsComplete() || download->IsCancelled())) { | 556 (download->IsComplete() || download->IsCancelled())) { |
575 AssertStateConsistent(download); | 557 AssertStateConsistent(download); |
576 download->NotifyRemoved(); | 558 download->NotifyRemoved(); |
577 pending_deletes.push_back(download); | 559 pending_deletes.push_back(download); |
578 } | 560 } |
579 } | 561 } |
580 return RemoveDownloadItems(pending_deletes); | 562 return RemoveDownloadItems(pending_deletes); |
581 } | 563 } |
582 | 564 |
(...skipping 20 matching lines...) Expand all Loading... |
603 } | 585 } |
604 | 586 |
605 void DownloadManagerImpl::AddObserver(Observer* observer) { | 587 void DownloadManagerImpl::AddObserver(Observer* observer) { |
606 observers_.AddObserver(observer); | 588 observers_.AddObserver(observer); |
607 } | 589 } |
608 | 590 |
609 void DownloadManagerImpl::RemoveObserver(Observer* observer) { | 591 void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
610 observers_.RemoveObserver(observer); | 592 observers_.RemoveObserver(observer); |
611 } | 593 } |
612 | 594 |
613 // Operations posted to us from the history service ---------------------------- | 595 DownloadItem* DownloadManagerImpl::CreateDownloadItem( |
614 | 596 const FilePath& path, |
615 // The history service has retrieved all download entries. 'entries' contains | 597 const GURL& url, |
616 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 598 const GURL& referrer_url, |
617 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 599 const base::Time& start_time, |
618 std::vector<DownloadPersistentStoreInfo>* entries) { | 600 const base::Time& end_time, |
619 history_size_ = entries->size(); | 601 int64 received_bytes, |
620 for (size_t i = 0; i < entries->size(); ++i) { | 602 int64 total_bytes, |
621 int64 db_handle = entries->at(i).db_handle; | 603 DownloadItem::DownloadState state, |
622 base::debug::Alias(&db_handle); | 604 bool opened) { |
623 | 605 DownloadItemImpl* item = item_factory_->CreatePersistedItem( |
624 net::BoundNetLog bound_net_log = | 606 this, |
625 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 607 GetNextId(), |
626 DownloadItemImpl* download = item_factory_->CreatePersistedItem( | 608 path, |
627 this, GetNextId(), entries->at(i), bound_net_log); | 609 url, |
628 DCHECK(!ContainsKey(downloads_, download->GetId())); | 610 referrer_url, |
629 downloads_[download->GetId()] = download; | 611 start_time, |
630 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 612 end_time, |
631 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 613 received_bytes, |
632 << " download = " << download->DebugString(true); | 614 total_bytes, |
633 } | 615 state, |
634 CheckForHistoryFilesRemoval(); | 616 opened, |
| 617 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD)); |
| 618 DCHECK(!ContainsKey(downloads_, item->GetId())); |
| 619 downloads_[item->GetId()] = item; |
| 620 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); |
| 621 VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true); |
| 622 return item; |
635 } | 623 } |
636 | 624 |
637 void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItemImpl* download, | 625 // TODO(asanka) Move into an observer. |
638 int64 db_handle) { | |
639 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
640 DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle); | |
641 DCHECK(!download->IsPersisted()); | |
642 download->SetDbHandle(db_handle); | |
643 download->SetIsPersisted(); | |
644 | |
645 RecordHistorySize(history_size_); | |
646 // Not counting |download|. | |
647 ++history_size_; | |
648 | |
649 // Show in the appropriate browser UI. | |
650 // This includes buttons to save or cancel, for a dangerous download. | |
651 ShowDownloadInBrowser(download); | |
652 } | |
653 | |
654 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, | |
655 int64 db_handle) { | |
656 // It's valid that we don't find a matching item, i.e. on shutdown. | |
657 if (!ContainsKey(downloads_, download_id)) | |
658 return; | |
659 | |
660 DownloadItemImpl* item = downloads_[download_id]; | |
661 AddDownloadItemToHistory(item, db_handle); | |
662 if (item->IsSavePackageDownload()) { | |
663 OnSavePageItemAddedToPersistentStore(item); | |
664 } else { | |
665 OnDownloadItemAddedToPersistentStore(item); | |
666 } | |
667 } | |
668 | |
669 // Once the new DownloadItem has been committed to the persistent store, | |
670 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), | |
671 // show it in the browser (TODO(benjhayden) the ui should observe us instead), | |
672 // and notify observers (TODO(benjhayden) observers should be able to see the | |
673 // item when it's created so they can observe it directly. Are there any | |
674 // clients that actually need to know when the item is added to the history?). | |
675 void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore( | |
676 DownloadItemImpl* item) { | |
677 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
678 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << item->GetDbHandle() | |
679 << " download_id = " << item->GetId() | |
680 << " download = " << item->DebugString(true); | |
681 | |
682 // If the download is still in progress, try to complete it. | |
683 // | |
684 // Otherwise, download has been cancelled or interrupted before we've | |
685 // received the DB handle. We post one final message to the history | |
686 // service so that it can be properly in sync with the DownloadItem's | |
687 // completion status, and also inform any observers so that they get | |
688 // more than just the start notification. | |
689 if (item->IsInProgress()) { | |
690 item->MaybeCompleteDownload(); | |
691 } else { | |
692 DCHECK(item->IsCancelled()); | |
693 active_downloads_.erase(item->GetId()); | |
694 if (delegate_) | |
695 delegate_->UpdateItemInPersistentStore(item); | |
696 item->UpdateObservers(); | |
697 } | |
698 } | |
699 | |
700 void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { | 626 void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { |
701 // The 'contents' may no longer exist if the user closed the contents before | 627 // The 'contents' may no longer exist if the user closed the contents before |
702 // we get this start completion event. | 628 // we get this start completion event. |
703 WebContents* content = download->GetWebContents(); | 629 WebContents* content = download->GetWebContents(); |
704 | 630 |
705 // If the contents no longer exists, we ask the embedder to suggest another | 631 // If the contents no longer exists, we ask the embedder to suggest another |
706 // contents. | 632 // contents. |
707 if (!content && delegate_) | 633 if (!content && delegate_) |
708 content = delegate_->GetAlternativeWebContentsToNotifyForDownload(); | 634 content = delegate_->GetAlternativeWebContentsToNotifyForDownload(); |
709 | 635 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 DownloadSet remainder; | 689 DownloadSet remainder; |
764 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); | 690 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
765 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), | 691 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
766 all_downloads.begin(), all_downloads.end(), | 692 all_downloads.begin(), all_downloads.end(), |
767 insert_it); | 693 insert_it); |
768 DCHECK(remainder.empty()); | 694 DCHECK(remainder.empty()); |
769 } | 695 } |
770 #endif | 696 #endif |
771 } | 697 } |
772 | 698 |
773 // SavePackage will call SavePageDownloadFinished upon completion/cancellation. | |
774 // The history callback will call OnSavePageItemAddedToPersistentStore. | |
775 // If the download finishes before the history callback, | |
776 // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring | |
777 // that the history event is update regardless of the order in which these two | |
778 // events complete. | |
779 // If something removes the download item from the download manager (Remove, | |
780 // Shutdown) the result will be that the SavePage system will not be able to | |
781 // properly update the download item (which no longer exists) or the download | |
782 // history, but the action will complete properly anyway. This may lead to the | |
783 // history entry being wrong on a reload of chrome (specifically in the case of | |
784 // Initiation -> History Callback -> Removal -> Completion), but there's no way | |
785 // to solve that without canceling on Remove (which would then update the DB). | |
786 | |
787 void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore( | |
788 DownloadItemImpl* item) { | |
789 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
790 | |
791 // Finalize this download if it finished before the history callback. | |
792 if (!item->IsInProgress()) | |
793 SavePageDownloadFinished(item); | |
794 } | |
795 | |
796 void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) { | |
797 if (download->IsPersisted()) { | |
798 if (delegate_) | |
799 delegate_->UpdateItemInPersistentStore(download); | |
800 } | |
801 } | |
802 | |
803 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { | 699 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { |
804 if (delegate_) | |
805 delegate_->UpdateItemInPersistentStore(download); | |
806 int num_unopened = 0; | 700 int num_unopened = 0; |
807 for (DownloadMap::iterator it = downloads_.begin(); | 701 for (DownloadMap::iterator it = downloads_.begin(); |
808 it != downloads_.end(); ++it) { | 702 it != downloads_.end(); ++it) { |
809 DownloadItemImpl* item = it->second; | 703 DownloadItemImpl* item = it->second; |
810 if (item->IsComplete() && | 704 if (item->IsComplete() && |
811 !item->GetOpened()) | 705 !item->GetOpened()) |
812 ++num_unopened; | 706 ++num_unopened; |
813 } | 707 } |
814 RecordOpensOutstanding(num_unopened); | 708 RecordOpensOutstanding(num_unopened); |
815 } | 709 } |
816 | 710 |
817 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | |
818 DownloadItemImpl* download) { | |
819 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
820 // download->GetFullPath() is only expected to be meaningful after this | |
821 // callback is received. Therefore we can now add the download to a persistent | |
822 // store. If the rename failed, we processed an interrupt | |
823 // before we receive the DownloadRenamedToIntermediateName() call. | |
824 if (delegate_) { | |
825 delegate_->AddItemToPersistentStore(download); | |
826 } else { | |
827 OnItemAddedToPersistentStore(download->GetId(), | |
828 DownloadItem::kUninitializedHandle); | |
829 } | |
830 } | |
831 | |
832 void DownloadManagerImpl::DownloadRenamedToFinalName( | |
833 DownloadItemImpl* download) { | |
834 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
835 // If the rename failed, we processed an interrupt before we get here. | |
836 if (delegate_) { | |
837 delegate_->UpdatePathForItemInPersistentStore( | |
838 download, download->GetFullPath()); | |
839 } | |
840 } | |
841 | |
842 } // namespace content | 711 } // namespace content |
OLD | NEW |