| 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 <deque> |
| 9 #include <iterator> | 10 #include <iterator> |
| 10 #include <map> | 11 #include <map> |
| 11 #include <memory> | 12 #include <memory> |
| 12 #include <queue> | 13 #include <queue> |
| 13 #include <utility> | 14 #include <utility> |
| 15 #include <vector> |
| 14 | 16 |
| 15 #include "base/callback.h" | 17 #include "base/callback.h" |
| 16 #include "base/feature_list.h" | 18 #include "base/feature_list.h" |
| 17 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
| 18 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 19 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 20 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
| 21 #include "content/browser/byte_stream.h" | 23 #include "content/browser/byte_stream.h" |
| 22 #include "content/browser/download/download_create_info.h" | 24 #include "content/browser/download/download_create_info.h" |
| 23 #include "content/browser/download/download_destination_observer.h" | 25 #include "content/browser/download/download_destination_observer.h" |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 464 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 463 } | 465 } |
| 464 | 466 |
| 465 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { | 467 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { |
| 466 DownloadItemImpl* item = CreateDownloadItem(); | 468 DownloadItemImpl* item = CreateDownloadItem(); |
| 467 MockDownloadFile* download_file = | 469 MockDownloadFile* download_file = |
| 468 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 470 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 469 EXPECT_CALL(*download_file, Cancel()); | 471 EXPECT_CALL(*download_file, Cancel()); |
| 470 TestDownloadItemObserver observer(item); | 472 TestDownloadItemObserver observer(item); |
| 471 | 473 |
| 472 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_)) | 474 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(0); |
| 473 .Times(0); | |
| 474 | 475 |
| 475 item->DestinationObserverAsWeakPtr()->DestinationError( | 476 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 476 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, | 477 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, |
| 477 std::unique_ptr<crypto::SecureHash>()); | 478 std::unique_ptr<crypto::SecureHash>()); |
| 478 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 479 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 479 } | 480 } |
| 480 | 481 |
| 481 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 482 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
| 482 DownloadItemImpl* item = CreateDownloadItem(); | 483 DownloadItemImpl* item = CreateDownloadItem(); |
| 483 TestDownloadItemObserver observer(item); | 484 TestDownloadItemObserver observer(item); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 502 | 503 |
| 503 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 504 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
| 504 // Setting to NOT_DANGEROUS does not trigger a notification. | 505 // Setting to NOT_DANGEROUS does not trigger a notification. |
| 505 DownloadItemImpl* safe_item = CreateDownloadItem(); | 506 DownloadItemImpl* safe_item = CreateDownloadItem(); |
| 506 MockDownloadFile* download_file = | 507 MockDownloadFile* download_file = |
| 507 DoIntermediateRename(safe_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 508 DoIntermediateRename(safe_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 508 TestDownloadItemObserver safe_observer(safe_item); | 509 TestDownloadItemObserver safe_observer(safe_item); |
| 509 | 510 |
| 510 safe_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 511 safe_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 511 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); | 512 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); |
| 512 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 513 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, false); |
| 513 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); | 514 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); |
| 514 CleanupItem(safe_item, download_file, DownloadItem::IN_PROGRESS); | 515 CleanupItem(safe_item, download_file, DownloadItem::IN_PROGRESS); |
| 515 | 516 |
| 516 // Setting to unsafe url or unsafe file should trigger a notification. | 517 // Setting to unsafe url or unsafe file should trigger a notification. |
| 517 DownloadItemImpl* unsafeurl_item = CreateDownloadItem(); | 518 DownloadItemImpl* unsafeurl_item = CreateDownloadItem(); |
| 518 download_file = | 519 download_file = |
| 519 DoIntermediateRename(unsafeurl_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 520 DoIntermediateRename(unsafeurl_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 520 TestDownloadItemObserver unsafeurl_observer(unsafeurl_item); | 521 TestDownloadItemObserver unsafeurl_observer(unsafeurl_item); |
| 521 | 522 |
| 522 unsafeurl_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 523 unsafeurl_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 523 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); | 524 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); |
| 524 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); | 525 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, |
| 526 false); |
| 525 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); | 527 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); |
| 526 | 528 |
| 527 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) | 529 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) |
| 528 .WillOnce(Return(true)); | 530 .WillOnce(Return(true)); |
| 529 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)); | 531 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)); |
| 530 unsafeurl_item->ValidateDangerousDownload(); | 532 unsafeurl_item->ValidateDangerousDownload(); |
| 531 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); | 533 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); |
| 532 CleanupItem(unsafeurl_item, download_file, DownloadItem::IN_PROGRESS); | 534 CleanupItem(unsafeurl_item, download_file, DownloadItem::IN_PROGRESS); |
| 533 | 535 |
| 534 DownloadItemImpl* unsafefile_item = CreateDownloadItem(); | 536 DownloadItemImpl* unsafefile_item = CreateDownloadItem(); |
| 535 download_file = | 537 download_file = |
| 536 DoIntermediateRename(unsafefile_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 538 DoIntermediateRename(unsafefile_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 537 TestDownloadItemObserver unsafefile_observer(unsafefile_item); | 539 TestDownloadItemObserver unsafefile_observer(unsafefile_item); |
| 538 | 540 |
| 539 unsafefile_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 541 unsafefile_item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 540 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); | 542 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); |
| 541 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 543 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 544 false); |
| 542 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); | 545 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); |
| 543 | 546 |
| 544 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) | 547 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) |
| 545 .WillOnce(Return(true)); | 548 .WillOnce(Return(true)); |
| 546 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)); | 549 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)); |
| 547 unsafefile_item->ValidateDangerousDownload(); | 550 unsafefile_item->ValidateDangerousDownload(); |
| 548 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); | 551 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); |
| 549 CleanupItem(unsafefile_item, download_file, DownloadItem::IN_PROGRESS); | 552 CleanupItem(unsafefile_item, download_file, DownloadItem::IN_PROGRESS); |
| 550 } | 553 } |
| 551 | 554 |
| (...skipping 964 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1516 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1519 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
| 1517 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), | 1520 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), |
| 1518 Return(false))) | 1521 Return(false))) |
| 1519 .WillOnce(Return(true)); | 1522 .WillOnce(Return(true)); |
| 1520 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 1523 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 1521 0, std::unique_ptr<crypto::SecureHash>()); | 1524 0, std::unique_ptr<crypto::SecureHash>()); |
| 1522 ASSERT_FALSE(delegate_callback.is_null()); | 1525 ASSERT_FALSE(delegate_callback.is_null()); |
| 1523 copy_delegate_callback = delegate_callback; | 1526 copy_delegate_callback = delegate_callback; |
| 1524 delegate_callback.Reset(); | 1527 delegate_callback.Reset(); |
| 1525 EXPECT_FALSE(item->IsDangerous()); | 1528 EXPECT_FALSE(item->IsDangerous()); |
| 1526 item->OnContentCheckCompleted( | 1529 item->OnContentCheckCompleted(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1527 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1530 false); |
| 1528 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1531 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1529 copy_delegate_callback.Run(); | 1532 copy_delegate_callback.Run(); |
| 1530 ASSERT_TRUE(delegate_callback.is_null()); | 1533 ASSERT_TRUE(delegate_callback.is_null()); |
| 1531 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1534 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1532 EXPECT_TRUE(item->IsDangerous()); | 1535 EXPECT_TRUE(item->IsDangerous()); |
| 1533 | 1536 |
| 1534 // Make sure the download doesn't complete until we've validated it. | 1537 // Make sure the download doesn't complete until we've validated it. |
| 1535 EXPECT_CALL(*download_file, | 1538 EXPECT_CALL(*download_file, |
| 1536 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1539 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1537 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1540 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| (...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2107 | 2110 |
| 2108 item_->Cancel(true); | 2111 item_->Cancel(true); |
| 2109 RunAllPendingInMessageLoops(); | 2112 RunAllPendingInMessageLoops(); |
| 2110 } | 2113 } |
| 2111 | 2114 |
| 2112 TEST(MockDownloadItem, Compiles) { | 2115 TEST(MockDownloadItem, Compiles) { |
| 2113 MockDownloadItem mock_item; | 2116 MockDownloadItem mock_item; |
| 2114 } | 2117 } |
| 2115 | 2118 |
| 2116 } // namespace content | 2119 } // namespace content |
| OLD | NEW |