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" |
| 6 |
5 #include <stddef.h> | 7 #include <stddef.h> |
6 #include <stdint.h> | 8 #include <stdint.h> |
7 | |
8 #include <set> | 9 #include <set> |
9 #include <string> | 10 #include <string> |
| 11 #include <utility> |
10 | 12 |
11 #include "base/bind.h" | 13 #include "base/bind.h" |
12 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
13 #include "base/macros.h" | 15 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
16 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
17 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
18 #include "base/strings/string16.h" | 20 #include "base/strings/string16.h" |
19 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
20 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
21 #include "build/build_config.h" | 23 #include "build/build_config.h" |
22 #include "content/browser/byte_stream.h" | 24 #include "content/browser/byte_stream.h" |
23 #include "content/browser/download/download_create_info.h" | 25 #include "content/browser/download/download_create_info.h" |
24 #include "content/browser/download/download_file_factory.h" | 26 #include "content/browser/download/download_file_factory.h" |
25 #include "content/browser/download/download_item_factory.h" | 27 #include "content/browser/download/download_item_factory.h" |
26 #include "content/browser/download/download_item_impl.h" | 28 #include "content/browser/download/download_item_impl.h" |
27 #include "content/browser/download/download_item_impl_delegate.h" | 29 #include "content/browser/download/download_item_impl_delegate.h" |
28 #include "content/browser/download/download_manager_impl.h" | |
29 #include "content/browser/download/download_request_handle.h" | 30 #include "content/browser/download/download_request_handle.h" |
30 #include "content/browser/download/mock_download_file.h" | 31 #include "content/browser/download/mock_download_file.h" |
31 #include "content/public/browser/browser_context.h" | 32 #include "content/public/browser/browser_context.h" |
32 #include "content/public/browser/download_interrupt_reasons.h" | 33 #include "content/public/browser/download_interrupt_reasons.h" |
33 #include "content/public/browser/download_item.h" | 34 #include "content/public/browser/download_item.h" |
34 #include "content/public/browser/download_manager_delegate.h" | 35 #include "content/public/browser/download_manager_delegate.h" |
35 #include "content/public/browser/zoom_level_delegate.h" | 36 #include "content/public/browser/zoom_level_delegate.h" |
36 #include "content/public/test/mock_download_item.h" | 37 #include "content/public/test/mock_download_item.h" |
37 #include "content/public/test/test_browser_context.h" | 38 #include "content/public/test/test_browser_context.h" |
38 #include "content/public/test/test_browser_thread.h" | 39 #include "content/public/test/test_browser_thread.h" |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 new StrictMock<MockDownloadManagerDelegate>); | 474 new StrictMock<MockDownloadManagerDelegate>); |
474 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) | 475 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) |
475 .WillOnce(Return()); | 476 .WillOnce(Return()); |
476 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); | 477 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); |
477 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) | 478 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) |
478 .WillRepeatedly(Return(false)); | 479 .WillRepeatedly(Return(false)); |
479 | 480 |
480 download_manager_.reset(new DownloadManagerImpl( | 481 download_manager_.reset(new DownloadManagerImpl( |
481 NULL, mock_browser_context_.get())); | 482 NULL, mock_browser_context_.get())); |
482 download_manager_->SetDownloadItemFactoryForTesting( | 483 download_manager_->SetDownloadItemFactoryForTesting( |
483 scoped_ptr<DownloadItemFactory>( | 484 scoped_ptr<DownloadItemFactory>(mock_download_item_factory_.get())); |
484 mock_download_item_factory_.get()).Pass()); | |
485 download_manager_->SetDownloadFileFactoryForTesting( | 485 download_manager_->SetDownloadFileFactoryForTesting( |
486 scoped_ptr<DownloadFileFactory>( | 486 scoped_ptr<DownloadFileFactory>(mock_download_file_factory_.get())); |
487 mock_download_file_factory_.get()).Pass()); | |
488 observer_.reset(new MockDownloadManagerObserver()); | 487 observer_.reset(new MockDownloadManagerObserver()); |
489 download_manager_->AddObserver(observer_.get()); | 488 download_manager_->AddObserver(observer_.get()); |
490 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); | 489 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); |
491 download_urls_.push_back(GURL("http://www.url1.com")); | 490 download_urls_.push_back(GURL("http://www.url1.com")); |
492 download_urls_.push_back(GURL("http://www.url2.com")); | 491 download_urls_.push_back(GURL("http://www.url2.com")); |
493 download_urls_.push_back(GURL("http://www.url3.com")); | 492 download_urls_.push_back(GURL("http://www.url3.com")); |
494 download_urls_.push_back(GURL("http://www.url4.com")); | 493 download_urls_.push_back(GURL("http://www.url4.com")); |
495 } | 494 } |
496 | 495 |
497 void TearDown() override { | 496 void TearDown() override { |
(...skipping 25 matching lines...) Expand all Loading... |
523 uint32_t id = next_download_id_; | 522 uint32_t id = next_download_id_; |
524 ++next_download_id_; | 523 ++next_download_id_; |
525 info.request_handle.reset(new DownloadRequestHandle); | 524 info.request_handle.reset(new DownloadRequestHandle); |
526 download_manager_->CreateActiveItem(id, info); | 525 download_manager_->CreateActiveItem(id, info); |
527 DCHECK(mock_download_item_factory_->GetItem(id)); | 526 DCHECK(mock_download_item_factory_->GetItem(id)); |
528 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); | 527 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); |
529 // Satisfy expectation. If the item is created in StartDownload(), | 528 // Satisfy expectation. If the item is created in StartDownload(), |
530 // we call Start on it immediately, so we need to set that expectation | 529 // we call Start on it immediately, so we need to set that expectation |
531 // in the factory. | 530 // in the factory. |
532 scoped_ptr<DownloadRequestHandleInterface> req_handle; | 531 scoped_ptr<DownloadRequestHandleInterface> req_handle; |
533 item.Start(scoped_ptr<DownloadFile>(), req_handle.Pass()); | 532 item.Start(scoped_ptr<DownloadFile>(), std::move(req_handle)); |
534 DCHECK(id < download_urls_.size()); | 533 DCHECK(id < download_urls_.size()); |
535 EXPECT_CALL(item, GetURL()).WillRepeatedly(ReturnRef(download_urls_[id])); | 534 EXPECT_CALL(item, GetURL()).WillRepeatedly(ReturnRef(download_urls_[id])); |
536 | 535 |
537 return item; | 536 return item; |
538 } | 537 } |
539 | 538 |
540 MockDownloadItemImpl& GetMockDownloadItem(int id) { | 539 MockDownloadItemImpl& GetMockDownloadItem(int id) { |
541 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); | 540 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); |
542 | 541 |
543 DCHECK(itemp); | 542 DCHECK(itemp); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 EXPECT_CALL(GetMockDownloadManagerDelegate(), | 623 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
625 ApplicationClientIdForFileScanning()) | 624 ApplicationClientIdForFileScanning()) |
626 .WillRepeatedly(Return("client-id")); | 625 .WillRepeatedly(Return("client-id")); |
627 MockDownloadFile* mock_file = new MockDownloadFile; | 626 MockDownloadFile* mock_file = new MockDownloadFile; |
628 EXPECT_CALL(*mock_file, SetClientGuid("client-id")); | 627 EXPECT_CALL(*mock_file, SetClientGuid("client-id")); |
629 EXPECT_CALL(*mock_download_file_factory_.get(), | 628 EXPECT_CALL(*mock_download_file_factory_.get(), |
630 MockCreateFile(Ref(*info->save_info.get()), _, _, _, true, | 629 MockCreateFile(Ref(*info->save_info.get()), _, _, _, true, |
631 stream.get(), _, _)) | 630 stream.get(), _, _)) |
632 .WillOnce(Return(mock_file)); | 631 .WillOnce(Return(mock_file)); |
633 | 632 |
634 download_manager_->StartDownload( | 633 download_manager_->StartDownload(std::move(info), std::move(stream), |
635 info.Pass(), stream.Pass(), DownloadUrlParameters::OnStartedCallback()); | 634 DownloadUrlParameters::OnStartedCallback()); |
636 EXPECT_TRUE(download_manager_->GetDownload(local_id)); | 635 EXPECT_TRUE(download_manager_->GetDownload(local_id)); |
637 } | 636 } |
638 | 637 |
639 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate | 638 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate |
640 // blocks starting. | 639 // blocks starting. |
641 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { | 640 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { |
642 // Put a mock we have a handle to on the download manager. | 641 // Put a mock we have a handle to on the download manager. |
643 MockDownloadItemImpl& item(AddItemToManager()); | 642 MockDownloadItemImpl& item(AddItemToManager()); |
644 EXPECT_CALL(item, GetState()) | 643 EXPECT_CALL(item, GetState()) |
645 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS)); | 644 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS)); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 EXPECT_CALL(GetMockDownloadItem(0), Remove()); | 720 EXPECT_CALL(GetMockDownloadItem(0), Remove()); |
722 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); | 721 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); |
723 | 722 |
724 url::Origin origin_to_clear(download_urls_[0]); | 723 url::Origin origin_to_clear(download_urls_[0]); |
725 int remove_count = download_manager_->RemoveDownloadsByOriginAndTime( | 724 int remove_count = download_manager_->RemoveDownloadsByOriginAndTime( |
726 origin_to_clear, base::Time(), base::Time::Max()); | 725 origin_to_clear, base::Time(), base::Time::Max()); |
727 EXPECT_EQ(remove_count, 1); | 726 EXPECT_EQ(remove_count, 1); |
728 } | 727 } |
729 | 728 |
730 } // namespace content | 729 } // namespace content |
OLD | NEW |