| 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_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <queue> | 10 #include <queue> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/strings/string_util.h" |
| 18 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 19 #include "content/browser/byte_stream.h" | 20 #include "content/browser/byte_stream.h" |
| 20 #include "content/browser/download/download_create_info.h" | 21 #include "content/browser/download/download_create_info.h" |
| 21 #include "content/browser/download/download_destination_observer.h" | 22 #include "content/browser/download/download_destination_observer.h" |
| 22 #include "content/browser/download/download_file_factory.h" | 23 #include "content/browser/download/download_file_factory.h" |
| 23 #include "content/browser/download/download_item_impl_delegate.h" | 24 #include "content/browser/download/download_item_impl_delegate.h" |
| 24 #include "content/browser/download/download_request_handle.h" | 25 #include "content/browser/download/download_request_handle.h" |
| 25 #include "content/browser/download/mock_download_file.h" | 26 #include "content/browser/download/mock_download_file.h" |
| 26 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/download_interrupt_reasons.h" | 28 #include "content/public/browser/download_interrupt_reasons.h" |
| (...skipping 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 | 750 |
| 750 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 751 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
| 751 // Setting to NOT_DANGEROUS does not trigger a notification. | 752 // Setting to NOT_DANGEROUS does not trigger a notification. |
| 752 DownloadItemImpl* safe_item = CreateDownloadItem(); | 753 DownloadItemImpl* safe_item = CreateDownloadItem(); |
| 753 MockDownloadFile* download_file = | 754 MockDownloadFile* download_file = |
| 754 DoIntermediateRename(safe_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 755 DoIntermediateRename(safe_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 755 TestDownloadItemObserver safe_observer(safe_item); | 756 TestDownloadItemObserver safe_observer(safe_item); |
| 756 | 757 |
| 757 safe_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 758 safe_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 758 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); | 759 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); |
| 759 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 760 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 761 base::EmptyString()); |
| 760 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); | 762 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); |
| 761 CleanupItem(safe_item, download_file, DownloadItem::IN_PROGRESS); | 763 CleanupItem(safe_item, download_file, DownloadItem::IN_PROGRESS); |
| 762 | 764 |
| 763 // Setting to unsafe url or unsafe file should trigger a notification. | 765 // Setting to unsafe url or unsafe file should trigger a notification. |
| 764 DownloadItemImpl* unsafeurl_item = | 766 DownloadItemImpl* unsafeurl_item = |
| 765 CreateDownloadItem(); | 767 CreateDownloadItem(); |
| 766 download_file = | 768 download_file = |
| 767 DoIntermediateRename(unsafeurl_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 769 DoIntermediateRename(unsafeurl_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 768 TestDownloadItemObserver unsafeurl_observer(unsafeurl_item); | 770 TestDownloadItemObserver unsafeurl_observer(unsafeurl_item); |
| 769 | 771 |
| 770 unsafeurl_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 772 unsafeurl_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 771 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); | 773 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); |
| 772 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); | 774 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 775 base::EmptyString()); |
| 773 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); | 776 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); |
| 774 | 777 |
| 775 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) | 778 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) |
| 776 .WillOnce(Return(true)); | 779 .WillOnce(Return(true)); |
| 777 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)); | 780 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)); |
| 778 unsafeurl_item->ValidateDangerousDownload(); | 781 unsafeurl_item->ValidateDangerousDownload(); |
| 779 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); | 782 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); |
| 780 CleanupItem(unsafeurl_item, download_file, DownloadItem::IN_PROGRESS); | 783 CleanupItem(unsafeurl_item, download_file, DownloadItem::IN_PROGRESS); |
| 781 | 784 |
| 782 DownloadItemImpl* unsafefile_item = | 785 DownloadItemImpl* unsafefile_item = |
| 783 CreateDownloadItem(); | 786 CreateDownloadItem(); |
| 784 download_file = | 787 download_file = |
| 785 DoIntermediateRename(unsafefile_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 788 DoIntermediateRename(unsafefile_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 786 TestDownloadItemObserver unsafefile_observer(unsafefile_item); | 789 TestDownloadItemObserver unsafefile_observer(unsafefile_item); |
| 787 | 790 |
| 788 unsafefile_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 791 unsafefile_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 789 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); | 792 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); |
| 790 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 793 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 794 base::EmptyString()); |
| 791 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); | 795 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); |
| 792 | 796 |
| 793 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) | 797 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) |
| 794 .WillOnce(Return(true)); | 798 .WillOnce(Return(true)); |
| 795 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)); | 799 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)); |
| 796 unsafefile_item->ValidateDangerousDownload(); | 800 unsafefile_item->ValidateDangerousDownload(); |
| 797 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); | 801 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); |
| 798 CleanupItem(unsafefile_item, download_file, DownloadItem::IN_PROGRESS); | 802 CleanupItem(unsafefile_item, download_file, DownloadItem::IN_PROGRESS); |
| 799 } | 803 } |
| 800 | 804 |
| (...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1463 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), | 1467 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), |
| 1464 Return(false))) | 1468 Return(false))) |
| 1465 .WillOnce(Return(true)); | 1469 .WillOnce(Return(true)); |
| 1466 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 1470 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 1467 0, std::unique_ptr<crypto::SecureHash>()); | 1471 0, std::unique_ptr<crypto::SecureHash>()); |
| 1468 ASSERT_FALSE(delegate_callback.is_null()); | 1472 ASSERT_FALSE(delegate_callback.is_null()); |
| 1469 copy_delegate_callback = delegate_callback; | 1473 copy_delegate_callback = delegate_callback; |
| 1470 delegate_callback.Reset(); | 1474 delegate_callback.Reset(); |
| 1471 EXPECT_FALSE(item->IsDangerous()); | 1475 EXPECT_FALSE(item->IsDangerous()); |
| 1472 item->OnContentCheckCompleted( | 1476 item->OnContentCheckCompleted( |
| 1473 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1477 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1478 base::EmptyString()); |
| 1474 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1479 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1475 copy_delegate_callback.Run(); | 1480 copy_delegate_callback.Run(); |
| 1476 ASSERT_TRUE(delegate_callback.is_null()); | 1481 ASSERT_TRUE(delegate_callback.is_null()); |
| 1477 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1482 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1478 EXPECT_TRUE(item->IsDangerous()); | 1483 EXPECT_TRUE(item->IsDangerous()); |
| 1479 | 1484 |
| 1480 // Make sure the download doesn't complete until we've validated it. | 1485 // Make sure the download doesn't complete until we've validated it. |
| 1481 EXPECT_CALL(*download_file, | 1486 EXPECT_CALL(*download_file, |
| 1482 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1487 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1483 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1488 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| (...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2029 | 2034 |
| 2030 item_->Cancel(true); | 2035 item_->Cancel(true); |
| 2031 RunAllPendingInMessageLoops(); | 2036 RunAllPendingInMessageLoops(); |
| 2032 } | 2037 } |
| 2033 | 2038 |
| 2034 TEST(MockDownloadItem, Compiles) { | 2039 TEST(MockDownloadItem, Compiles) { |
| 2035 MockDownloadItem mock_item; | 2040 MockDownloadItem mock_item; |
| 2036 } | 2041 } |
| 2037 | 2042 |
| 2038 } // namespace content | 2043 } // namespace content |
| OLD | NEW |