| 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 <deque> |
| 10 #include <iterator> | 10 #include <iterator> |
| (...skipping 798 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 | 809 |
| 810 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 810 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 811 EXPECT_EQ(1, observer.interrupt_count()); | 811 EXPECT_EQ(1, observer.interrupt_count()); |
| 812 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); | 812 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); |
| 813 } | 813 } |
| 814 | 814 |
| 815 // If the download attempts to resume and the resumption request fails, the | 815 // If the download attempts to resume and the resumption request fails, the |
| 816 // subsequent Start() call shouldn't update the origin state (URL redirect | 816 // subsequent Start() call shouldn't update the origin state (URL redirect |
| 817 // chains, Content-Disposition, download URL, etc..) | 817 // chains, Content-Disposition, download URL, etc..) |
| 818 TEST_F(DownloadItemTest, FailedResumptionDoesntUpdateOriginState) { | 818 TEST_F(DownloadItemTest, FailedResumptionDoesntUpdateOriginState) { |
| 819 constexpr int kFirstResponseCode = 200; |
| 819 const char kContentDisposition[] = "attachment; filename=foo"; | 820 const char kContentDisposition[] = "attachment; filename=foo"; |
| 820 const char kFirstETag[] = "ABC"; | 821 const char kFirstETag[] = "ABC"; |
| 821 const char kFirstLastModified[] = "Yesterday"; | 822 const char kFirstLastModified[] = "Yesterday"; |
| 822 const char kFirstURL[] = "http://www.example.com/download"; | 823 const char kFirstURL[] = "http://www.example.com/download"; |
| 823 const char kMimeType[] = "text/css"; | 824 const char kMimeType[] = "text/css"; |
| 825 create_info()->response_code = kFirstResponseCode; |
| 824 create_info()->content_disposition = kContentDisposition; | 826 create_info()->content_disposition = kContentDisposition; |
| 825 create_info()->etag = kFirstETag; | 827 create_info()->etag = kFirstETag; |
| 826 create_info()->last_modified = kFirstLastModified; | 828 create_info()->last_modified = kFirstLastModified; |
| 827 create_info()->url_chain.push_back(GURL(kFirstURL)); | 829 create_info()->url_chain.push_back(GURL(kFirstURL)); |
| 828 create_info()->mime_type = kMimeType; | 830 create_info()->mime_type = kMimeType; |
| 829 | 831 |
| 830 DownloadItemImpl* item = CreateDownloadItem(); | 832 DownloadItemImpl* item = CreateDownloadItem(); |
| 831 MockDownloadFile* download_file = | 833 MockDownloadFile* download_file = |
| 832 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 834 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 835 EXPECT_EQ(kFirstResponseCode, item->GetResponseCode()); |
| 833 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); | 836 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); |
| 834 EXPECT_EQ(kFirstETag, item->GetETag()); | 837 EXPECT_EQ(kFirstETag, item->GetETag()); |
| 835 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); | 838 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); |
| 836 EXPECT_EQ(kFirstURL, item->GetURL().spec()); | 839 EXPECT_EQ(kFirstURL, item->GetURL().spec()); |
| 837 EXPECT_EQ(kMimeType, item->GetMimeType()); | 840 EXPECT_EQ(kMimeType, item->GetMimeType()); |
| 838 | 841 |
| 839 EXPECT_CALL(*mock_delegate(), | 842 EXPECT_CALL(*mock_delegate(), |
| 840 MockResumeInterruptedDownload( | 843 MockResumeInterruptedDownload( |
| 841 AllOf(Property(&DownloadUrlParameters::file_path, | 844 AllOf(Property(&DownloadUrlParameters::file_path, |
| 842 Property(&base::FilePath::value, | 845 Property(&base::FilePath::value, |
| 843 kDummyIntermediatePath)), | 846 kDummyIntermediatePath)), |
| 844 Property(&DownloadUrlParameters::offset, 1)), | 847 Property(&DownloadUrlParameters::offset, 1)), |
| 845 _)); | 848 _)); |
| 846 EXPECT_CALL(*download_file, Detach()); | 849 EXPECT_CALL(*download_file, Detach()); |
| 847 item->DestinationObserverAsWeakPtr()->DestinationError( | 850 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 848 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, | 851 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, |
| 849 std::unique_ptr<crypto::SecureHash>()); | 852 std::unique_ptr<crypto::SecureHash>()); |
| 850 RunAllPendingInMessageLoops(); | 853 RunAllPendingInMessageLoops(); |
| 851 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 854 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 852 | 855 |
| 853 // Now change the create info. The changes should not cause the DownloadItem | 856 // Now change the create info. The changes should not cause the DownloadItem |
| 854 // to be updated. | 857 // to be updated. |
| 858 constexpr int kSecondResponseCode = 418; |
| 855 const char kSecondContentDisposition[] = "attachment; filename=bar"; | 859 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
| 856 const char kSecondETag[] = "123"; | 860 const char kSecondETag[] = "123"; |
| 857 const char kSecondLastModified[] = "Today"; | 861 const char kSecondLastModified[] = "Today"; |
| 858 const char kSecondURL[] = "http://example.com/another-download"; | 862 const char kSecondURL[] = "http://example.com/another-download"; |
| 859 const char kSecondMimeType[] = "text/html"; | 863 const char kSecondMimeType[] = "text/html"; |
| 864 create_info()->response_code = kSecondResponseCode; |
| 860 create_info()->content_disposition = kSecondContentDisposition; | 865 create_info()->content_disposition = kSecondContentDisposition; |
| 861 create_info()->etag = kSecondETag; | 866 create_info()->etag = kSecondETag; |
| 862 create_info()->last_modified = kSecondLastModified; | 867 create_info()->last_modified = kSecondLastModified; |
| 863 create_info()->url_chain.clear(); | 868 create_info()->url_chain.clear(); |
| 864 create_info()->url_chain.push_back(GURL(kSecondURL)); | 869 create_info()->url_chain.push_back(GURL(kSecondURL)); |
| 865 create_info()->mime_type = kSecondMimeType; | 870 create_info()->mime_type = kSecondMimeType; |
| 866 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; | 871 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; |
| 867 create_info()->save_info->file_path = base::FilePath(kDummyIntermediatePath); | 872 create_info()->save_info->file_path = base::FilePath(kDummyIntermediatePath); |
| 868 create_info()->save_info->offset = 1; | 873 create_info()->save_info->offset = 1; |
| 869 | 874 |
| 870 // Calling Start() with a response indicating failure shouldn't cause a target | 875 // Calling Start() with a response indicating failure shouldn't cause a target |
| 871 // update, nor should it result in discarding the intermediate file. | 876 // update, nor should it result in discarding the intermediate file. |
| 872 DownloadTargetCallback target_callback; | 877 DownloadTargetCallback target_callback; |
| 873 download_file = CallDownloadItemStart(item, &target_callback); | 878 download_file = CallDownloadItemStart(item, &target_callback); |
| 874 ASSERT_FALSE(target_callback.is_null()); | 879 ASSERT_FALSE(target_callback.is_null()); |
| 875 target_callback.Run(base::FilePath(kDummyTargetPath), | 880 target_callback.Run(base::FilePath(kDummyTargetPath), |
| 876 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 881 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 877 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 882 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 878 base::FilePath(kDummyIntermediatePath), | 883 base::FilePath(kDummyIntermediatePath), |
| 879 DOWNLOAD_INTERRUPT_REASON_NONE); | 884 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 880 RunAllPendingInMessageLoops(); | 885 RunAllPendingInMessageLoops(); |
| 881 | 886 |
| 887 EXPECT_EQ(kFirstResponseCode, item->GetResponseCode()); |
| 882 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); | 888 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); |
| 883 EXPECT_EQ(kFirstETag, item->GetETag()); | 889 EXPECT_EQ(kFirstETag, item->GetETag()); |
| 884 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); | 890 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); |
| 885 EXPECT_EQ(kFirstURL, item->GetURL().spec()); | 891 EXPECT_EQ(kFirstURL, item->GetURL().spec()); |
| 886 EXPECT_EQ(kMimeType, item->GetMimeType()); | 892 EXPECT_EQ(kMimeType, item->GetMimeType()); |
| 887 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 893 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 888 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); | 894 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); |
| 889 EXPECT_EQ(kDummyIntermediatePath, item->GetFullPath().value()); | 895 EXPECT_EQ(kDummyIntermediatePath, item->GetFullPath().value()); |
| 890 EXPECT_EQ(1, item->GetReceivedBytes()); | 896 EXPECT_EQ(1, item->GetReceivedBytes()); |
| 891 } | 897 } |
| 892 | 898 |
| 893 // If the download resumption request succeeds, the origin state should be | 899 // If the download resumption request succeeds, the origin state should be |
| 894 // updated. | 900 // updated. |
| 895 TEST_F(DownloadItemTest, SucceededResumptionUpdatesOriginState) { | 901 TEST_F(DownloadItemTest, SucceededResumptionUpdatesOriginState) { |
| 902 constexpr int kFirstResponseCode = 200; |
| 896 const char kContentDisposition[] = "attachment; filename=foo"; | 903 const char kContentDisposition[] = "attachment; filename=foo"; |
| 897 const char kFirstETag[] = "ABC"; | 904 const char kFirstETag[] = "ABC"; |
| 898 const char kFirstLastModified[] = "Yesterday"; | 905 const char kFirstLastModified[] = "Yesterday"; |
| 899 const char kFirstURL[] = "http://www.example.com/download"; | 906 const char kFirstURL[] = "http://www.example.com/download"; |
| 900 const char kMimeType[] = "text/css"; | 907 const char kMimeType[] = "text/css"; |
| 908 create_info()->response_code = kFirstResponseCode; |
| 901 create_info()->content_disposition = kContentDisposition; | 909 create_info()->content_disposition = kContentDisposition; |
| 902 create_info()->etag = kFirstETag; | 910 create_info()->etag = kFirstETag; |
| 903 create_info()->last_modified = kFirstLastModified; | 911 create_info()->last_modified = kFirstLastModified; |
| 904 create_info()->url_chain.push_back(GURL(kFirstURL)); | 912 create_info()->url_chain.push_back(GURL(kFirstURL)); |
| 905 create_info()->mime_type = kMimeType; | 913 create_info()->mime_type = kMimeType; |
| 906 | 914 |
| 907 DownloadItemImpl* item = CreateDownloadItem(); | 915 DownloadItemImpl* item = CreateDownloadItem(); |
| 908 MockDownloadFile* download_file = | 916 MockDownloadFile* download_file = |
| 909 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 917 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 910 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); | 918 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); |
| 911 EXPECT_CALL(*download_file, Detach()); | 919 EXPECT_CALL(*download_file, Detach()); |
| 912 item->DestinationObserverAsWeakPtr()->DestinationError( | 920 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 913 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 921 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
| 914 std::unique_ptr<crypto::SecureHash>()); | 922 std::unique_ptr<crypto::SecureHash>()); |
| 915 RunAllPendingInMessageLoops(); | 923 RunAllPendingInMessageLoops(); |
| 916 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 924 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 917 | 925 |
| 918 // Now change the create info. The changes should not cause the DownloadItem | 926 // Now change the create info. The changes should not cause the DownloadItem |
| 919 // to be updated. | 927 // to be updated. |
| 928 constexpr int kSecondResponseCode = 201; |
| 920 const char kSecondContentDisposition[] = "attachment; filename=bar"; | 929 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
| 921 const char kSecondETag[] = "123"; | 930 const char kSecondETag[] = "123"; |
| 922 const char kSecondLastModified[] = "Today"; | 931 const char kSecondLastModified[] = "Today"; |
| 923 const char kSecondURL[] = "http://example.com/another-download"; | 932 const char kSecondURL[] = "http://example.com/another-download"; |
| 924 const char kSecondMimeType[] = "text/html"; | 933 const char kSecondMimeType[] = "text/html"; |
| 934 create_info()->response_code = kSecondResponseCode; |
| 925 create_info()->content_disposition = kSecondContentDisposition; | 935 create_info()->content_disposition = kSecondContentDisposition; |
| 926 create_info()->etag = kSecondETag; | 936 create_info()->etag = kSecondETag; |
| 927 create_info()->last_modified = kSecondLastModified; | 937 create_info()->last_modified = kSecondLastModified; |
| 928 create_info()->url_chain.clear(); | 938 create_info()->url_chain.clear(); |
| 929 create_info()->url_chain.push_back(GURL(kSecondURL)); | 939 create_info()->url_chain.push_back(GURL(kSecondURL)); |
| 930 create_info()->mime_type = kSecondMimeType; | 940 create_info()->mime_type = kSecondMimeType; |
| 931 | 941 |
| 932 DownloadTargetCallback target_callback; | 942 DownloadTargetCallback target_callback; |
| 933 download_file = CallDownloadItemStart(item, &target_callback); | 943 download_file = CallDownloadItemStart(item, &target_callback); |
| 934 | 944 |
| 945 EXPECT_EQ(kSecondResponseCode, item->GetResponseCode()); |
| 935 EXPECT_EQ(kSecondContentDisposition, item->GetContentDisposition()); | 946 EXPECT_EQ(kSecondContentDisposition, item->GetContentDisposition()); |
| 936 EXPECT_EQ(kSecondETag, item->GetETag()); | 947 EXPECT_EQ(kSecondETag, item->GetETag()); |
| 937 EXPECT_EQ(kSecondLastModified, item->GetLastModifiedTime()); | 948 EXPECT_EQ(kSecondLastModified, item->GetLastModifiedTime()); |
| 938 EXPECT_EQ(kSecondURL, item->GetURL().spec()); | 949 EXPECT_EQ(kSecondURL, item->GetURL().spec()); |
| 939 EXPECT_EQ(kSecondMimeType, item->GetMimeType()); | 950 EXPECT_EQ(kSecondMimeType, item->GetMimeType()); |
| 940 | 951 |
| 941 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); | 952 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 942 } | 953 } |
| 943 | 954 |
| 944 // Ensure when strong validators changed on resumption, the received | 955 // Ensure when strong validators changed on resumption, the received |
| (...skipping 1326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2271 | 2282 |
| 2272 item_->Cancel(true); | 2283 item_->Cancel(true); |
| 2273 RunAllPendingInMessageLoops(); | 2284 RunAllPendingInMessageLoops(); |
| 2274 } | 2285 } |
| 2275 | 2286 |
| 2276 TEST(MockDownloadItem, Compiles) { | 2287 TEST(MockDownloadItem, Compiles) { |
| 2277 MockDownloadItem mock_item; | 2288 MockDownloadItem mock_item; |
| 2278 } | 2289 } |
| 2279 | 2290 |
| 2280 } // namespace content | 2291 } // namespace content |
| OLD | NEW |