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 "components/history/core/browser/expire_history_backend.h" | 5 #include "components/history/core/browser/expire_history_backend.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
20 #include "base/scoped_observer.h" | 20 #include "base/scoped_observer.h" |
21 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/test/scoped_feature_list.h" |
23 #include "base/test/scoped_task_environment.h" | 24 #include "base/test/scoped_task_environment.h" |
24 #include "components/history/core/browser/history_backend_client.h" | 25 #include "components/history/core/browser/history_backend_client.h" |
25 #include "components/history/core/browser/history_backend_notifier.h" | 26 #include "components/history/core/browser/history_backend_notifier.h" |
26 #include "components/history/core/browser/history_constants.h" | 27 #include "components/history/core/browser/history_constants.h" |
27 #include "components/history/core/browser/history_database.h" | 28 #include "components/history/core/browser/history_database.h" |
28 #include "components/history/core/browser/thumbnail_database.h" | 29 #include "components/history/core/browser/thumbnail_database.h" |
29 #include "components/history/core/browser/top_sites.h" | 30 #include "components/history/core/browser/top_sites.h" |
30 #include "components/history/core/browser/top_sites_impl.h" | 31 #include "components/history/core/browser/top_sites_impl.h" |
31 #include "components/history/core/browser/top_sites_observer.h" | 32 #include "components/history/core/browser/top_sites_observer.h" |
32 #include "components/history/core/common/thumbnail_score.h" | 33 #include "components/history/core/common/thumbnail_score.h" |
(...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 | 885 |
885 // Verify that the early expiration threshold was updated, since there are no | 886 // Verify that the early expiration threshold was updated, since there are no |
886 // AUTO_SUBFRAME visits in the given time range. | 887 // AUTO_SUBFRAME visits in the given time range. |
887 EXPECT_TRUE(now <= main_db_->GetEarlyExpirationThreshold()); | 888 EXPECT_TRUE(now <= main_db_->GetEarlyExpirationThreshold()); |
888 | 889 |
889 // Now, read all visits and verify that there's at least one. | 890 // Now, read all visits and verify that there's at least one. |
890 EXPECT_TRUE(all->Read(now, main_db_.get(), &visits, 1)); | 891 EXPECT_TRUE(all->Read(now, main_db_.get(), &visits, 1)); |
891 EXPECT_EQ(1U, visits.size()); | 892 EXPECT_EQ(1U, visits.size()); |
892 } | 893 } |
893 | 894 |
| 895 // Test that ClearOldOnDemandFavicons() deletes favicons associated only to |
| 896 // unstarred page URLs. |
| 897 TEST_F(ExpireHistoryTest, ClearOldOnDemandFaviconsDoesDeleteUnstarred) { |
| 898 base::test::ScopedFeatureList feature_list; |
| 899 feature_list.InitAndEnableFeature(internal::kClearOldOnDemandFavicons); |
| 900 |
| 901 // The blob does not encode any real bitmap, obviously. |
| 902 const unsigned char kBlob[] = "0"; |
| 903 scoped_refptr<base::RefCountedBytes> favicon( |
| 904 new base::RefCountedBytes(kBlob, sizeof(kBlob))); |
| 905 |
| 906 // Icon: old and not bookmarked case. |
| 907 GURL url("http://google.com/favicon.ico"); |
| 908 favicon_base::FaviconID icon_id = thumb_db_->AddFavicon( |
| 909 url, favicon_base::FAVICON, favicon, FaviconBitmapType::ON_DEMAND, |
| 910 base::Time::Now() - base::TimeDelta::FromDays(100), gfx::Size()); |
| 911 ASSERT_NE(0, icon_id); |
| 912 GURL page_url("http://google.com/"); |
| 913 ASSERT_NE(0, thumb_db_->AddIconMapping(page_url, icon_id)); |
| 914 |
| 915 expirer_.ClearOldOnDemandFavicons(base::Time::Now() - |
| 916 base::TimeDelta::FromDays(90)); |
| 917 |
| 918 // The icon gets deleted. |
| 919 EXPECT_FALSE(thumb_db_->GetIconMappingsForPageURL(page_url, nullptr)); |
| 920 EXPECT_FALSE(thumb_db_->GetFaviconHeader(icon_id, nullptr, nullptr)); |
| 921 EXPECT_FALSE(thumb_db_->GetFaviconBitmaps(icon_id, nullptr)); |
| 922 } |
| 923 |
| 924 // Test that ClearOldOnDemandFavicons() deletes favicons associated to at least |
| 925 // one starred page URL. |
| 926 TEST_F(ExpireHistoryTest, ClearOldOnDemandFaviconsDoesNotDeleteStarred) { |
| 927 base::test::ScopedFeatureList feature_list; |
| 928 feature_list.InitAndEnableFeature(internal::kClearOldOnDemandFavicons); |
| 929 |
| 930 // The blob does not encode any real bitmap, obviously. |
| 931 const unsigned char kBlob[] = "0"; |
| 932 scoped_refptr<base::RefCountedBytes> favicon( |
| 933 new base::RefCountedBytes(kBlob, sizeof(kBlob))); |
| 934 |
| 935 // Icon: old but bookmarked case. |
| 936 GURL url("http://google.com/favicon.ico"); |
| 937 favicon_base::FaviconID icon_id = thumb_db_->AddFavicon( |
| 938 url, favicon_base::FAVICON, favicon, FaviconBitmapType::ON_DEMAND, |
| 939 base::Time::Now() - base::TimeDelta::FromDays(100), gfx::Size()); |
| 940 ASSERT_NE(0, icon_id); |
| 941 GURL page_url1("http://google.com/1"); |
| 942 ASSERT_NE(0, thumb_db_->AddIconMapping(page_url1, icon_id)); |
| 943 StarURL(page_url1); |
| 944 GURL page_url2("http://google.com/2"); |
| 945 ASSERT_NE(0, thumb_db_->AddIconMapping(page_url2, icon_id)); |
| 946 |
| 947 expirer_.ClearOldOnDemandFavicons(base::Time::Now() - |
| 948 base::TimeDelta::FromDays(90)); |
| 949 |
| 950 // Nothing gets deleted. |
| 951 EXPECT_TRUE(thumb_db_->GetFaviconHeader(icon_id, nullptr, nullptr)); |
| 952 std::vector<FaviconBitmap> favicon_bitmaps; |
| 953 EXPECT_TRUE(thumb_db_->GetFaviconBitmaps(icon_id, &favicon_bitmaps)); |
| 954 EXPECT_EQ(1u, favicon_bitmaps.size()); |
| 955 std::vector<IconMapping> icon_mapping; |
| 956 EXPECT_TRUE(thumb_db_->GetIconMappingsForPageURL(page_url1, &icon_mapping)); |
| 957 EXPECT_TRUE(thumb_db_->GetIconMappingsForPageURL(page_url2, &icon_mapping)); |
| 958 EXPECT_EQ(2u, icon_mapping.size()); |
| 959 EXPECT_EQ(icon_id, icon_mapping[0].icon_id); |
| 960 EXPECT_EQ(icon_id, icon_mapping[1].icon_id); |
| 961 } |
| 962 |
| 963 // Test that ClearOldOnDemandFavicons() has effect if the last clearing was long |
| 964 // time age (such as 2 days ago). |
| 965 TEST_F(ExpireHistoryTest, ClearOldOnDemandFaviconsDoesDeleteAfterLongDelay) { |
| 966 base::test::ScopedFeatureList feature_list; |
| 967 feature_list.InitAndEnableFeature(internal::kClearOldOnDemandFavicons); |
| 968 |
| 969 // Previous clearing (2 days ago). |
| 970 expirer_.ClearOldOnDemandFavicons(base::Time::Now() - |
| 971 base::TimeDelta::FromDays(92)); |
| 972 |
| 973 // The blob does not encode any real bitmap, obviously. |
| 974 const unsigned char kBlob[] = "0"; |
| 975 scoped_refptr<base::RefCountedBytes> favicon( |
| 976 new base::RefCountedBytes(kBlob, sizeof(kBlob))); |
| 977 |
| 978 // Icon: old and not bookmarked case. |
| 979 GURL url("http://google.com/favicon.ico"); |
| 980 favicon_base::FaviconID icon_id = thumb_db_->AddFavicon( |
| 981 url, favicon_base::FAVICON, favicon, FaviconBitmapType::ON_DEMAND, |
| 982 base::Time::Now() - base::TimeDelta::FromDays(100), gfx::Size()); |
| 983 ASSERT_NE(0, icon_id); |
| 984 GURL page_url("http://google.com/"); |
| 985 ASSERT_NE(0, thumb_db_->AddIconMapping(page_url, icon_id)); |
| 986 |
| 987 expirer_.ClearOldOnDemandFavicons(base::Time::Now() - |
| 988 base::TimeDelta::FromDays(90)); |
| 989 |
| 990 // The icon gets deleted. |
| 991 EXPECT_FALSE(thumb_db_->GetIconMappingsForPageURL(page_url, nullptr)); |
| 992 EXPECT_FALSE(thumb_db_->GetFaviconHeader(icon_id, nullptr, nullptr)); |
| 993 EXPECT_FALSE(thumb_db_->GetFaviconBitmaps(icon_id, nullptr)); |
| 994 } |
| 995 |
| 996 // Test that ClearOldOnDemandFavicons() deletes favicons associated to at least |
| 997 // one starred page URL. |
| 998 TEST_F(ExpireHistoryTest, |
| 999 ClearOldOnDemandFaviconsDoesNotDeleteAfterShortDelay) { |
| 1000 base::test::ScopedFeatureList feature_list; |
| 1001 feature_list.InitAndEnableFeature(internal::kClearOldOnDemandFavicons); |
| 1002 |
| 1003 // Previous clearing (5 minutes ago). |
| 1004 expirer_.ClearOldOnDemandFavicons(base::Time::Now() - |
| 1005 base::TimeDelta::FromDays(90) - |
| 1006 base::TimeDelta::FromMinutes(5)); |
| 1007 |
| 1008 // The blob does not encode any real bitmap, obviously. |
| 1009 const unsigned char kBlob[] = "0"; |
| 1010 scoped_refptr<base::RefCountedBytes> favicon( |
| 1011 new base::RefCountedBytes(kBlob, sizeof(kBlob))); |
| 1012 |
| 1013 // Icon: old but bookmarked case. |
| 1014 GURL url("http://google.com/favicon.ico"); |
| 1015 favicon_base::FaviconID icon_id = thumb_db_->AddFavicon( |
| 1016 url, favicon_base::FAVICON, favicon, FaviconBitmapType::ON_DEMAND, |
| 1017 base::Time::Now() - base::TimeDelta::FromDays(100), gfx::Size()); |
| 1018 ASSERT_NE(0, icon_id); |
| 1019 GURL page_url1("http://google.com/1"); |
| 1020 ASSERT_NE(0, thumb_db_->AddIconMapping(page_url1, icon_id)); |
| 1021 GURL page_url2("http://google.com/2"); |
| 1022 ASSERT_NE(0, thumb_db_->AddIconMapping(page_url2, icon_id)); |
| 1023 |
| 1024 expirer_.ClearOldOnDemandFavicons(base::Time::Now() - |
| 1025 base::TimeDelta::FromDays(90)); |
| 1026 |
| 1027 // Nothing gets deleted. |
| 1028 EXPECT_TRUE(thumb_db_->GetFaviconHeader(icon_id, nullptr, nullptr)); |
| 1029 std::vector<FaviconBitmap> favicon_bitmaps; |
| 1030 EXPECT_TRUE(thumb_db_->GetFaviconBitmaps(icon_id, &favicon_bitmaps)); |
| 1031 EXPECT_EQ(1u, favicon_bitmaps.size()); |
| 1032 std::vector<IconMapping> icon_mapping; |
| 1033 EXPECT_TRUE(thumb_db_->GetIconMappingsForPageURL(page_url1, &icon_mapping)); |
| 1034 EXPECT_TRUE(thumb_db_->GetIconMappingsForPageURL(page_url2, &icon_mapping)); |
| 1035 EXPECT_EQ(2u, icon_mapping.size()); |
| 1036 EXPECT_EQ(icon_id, icon_mapping[0].icon_id); |
| 1037 EXPECT_EQ(icon_id, icon_mapping[1].icon_id); |
| 1038 } |
| 1039 |
894 // TODO(brettw) add some visits with no URL to make sure everything is updated | 1040 // TODO(brettw) add some visits with no URL to make sure everything is updated |
895 // properly. Have the visits also refer to nonexistent FTS rows. | 1041 // properly. Have the visits also refer to nonexistent FTS rows. |
896 // | 1042 // |
897 // Maybe also refer to invalid favicons. | 1043 // Maybe also refer to invalid favicons. |
898 | 1044 |
899 } // namespace history | 1045 } // namespace history |
OLD | NEW |