Commit 9c44cfcd authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

favicons: adds GetFaviconsLastUpdatedBefore()

To FaviconDatabase. I'm going to use this in weblayer code
to expire favicons. WebLayer doesn't use history, but needs
a way to prune the database.

BUG=1076463
TEST=covered by tests

Change-Id: If74a389a947eb465447009c620e0988f9545ab91
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2354998
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarPeter Kotwicz <pkotwicz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798209}
parent e210b61d
...@@ -843,6 +843,23 @@ bool FaviconDatabase::HasMappingFor(favicon_base::FaviconID id) { ...@@ -843,6 +843,23 @@ bool FaviconDatabase::HasMappingFor(favicon_base::FaviconID id) {
return statement.Step(); return statement.Step();
} }
std::vector<favicon_base::FaviconID>
FaviconDatabase::GetFaviconsLastUpdatedBefore(base::Time time, int max_count) {
sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
"SELECT icon_id "
"FROM favicon_bitmaps "
"WHERE last_updated < ? "
"ORDER BY last_updated ASC "
"LIMIT ?"));
statement.BindInt64(0, time.ToDeltaSinceWindowsEpoch().InMicroseconds());
statement.BindInt64(
1, max_count == 0 ? std::numeric_limits<int64_t>::max() : max_count);
std::vector<favicon_base::FaviconID> ids;
while (statement.Step())
ids.push_back(statement.ColumnInt64(0));
return ids;
}
bool FaviconDatabase::InitIconMappingEnumerator( bool FaviconDatabase::InitIconMappingEnumerator(
favicon_base::IconType type, favicon_base::IconType type,
IconMappingEnumerator* enumerator) { IconMappingEnumerator* enumerator) {
......
...@@ -222,6 +222,12 @@ class FaviconDatabase { ...@@ -222,6 +222,12 @@ class FaviconDatabase {
// Checks whether a favicon is used by any URLs in the database. // Checks whether a favicon is used by any URLs in the database.
bool HasMappingFor(favicon_base::FaviconID id); bool HasMappingFor(favicon_base::FaviconID id);
// Returns the ids of favicons which were last updated before |time|. This
// returns at most |max_count| ids.
std::vector<favicon_base::FaviconID> GetFaviconsLastUpdatedBefore(
base::Time time,
int max_count);
// The class to enumerate icon mappings. Use InitIconMappingEnumerator to // The class to enumerate icon mappings. Use InitIconMappingEnumerator to
// initialize. // initialize.
class IconMappingEnumerator { class IconMappingEnumerator {
......
...@@ -1376,4 +1376,54 @@ TEST(FaviconDatabaseIconTypeTest, ShouldBeBackwardCompatible) { ...@@ -1376,4 +1376,54 @@ TEST(FaviconDatabaseIconTypeTest, ShouldBeBackwardCompatible) {
FaviconDatabase::FromPersistedIconType(16)); FaviconDatabase::FromPersistedIconType(16));
} }
TEST_F(FaviconDatabaseTest, GetFaviconsLastUpdatedBefore) {
FaviconDatabase db;
ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
db.BeginTransaction();
std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
// Add two favicons, 10 seconds apart. |time1| is after |time2|.
GURL url("http://google.com");
const base::Time time1 = base::Time::Now();
favicon_base::FaviconID id1 =
db.AddFavicon(url, favicon_base::IconType::kTouchIcon, favicon,
FaviconBitmapType::ON_VISIT, time1, gfx::Size());
EXPECT_NE(0u, id1);
const base::Time time2 = time1 - base::TimeDelta::FromSeconds(10);
favicon_base::FaviconID id2 =
db.AddFavicon(url, favicon_base::IconType::kTouchIcon, favicon,
FaviconBitmapType::ON_VISIT, time2, gfx::Size());
EXPECT_NE(0u, id2);
EXPECT_NE(id1, id2);
// There should be no favicons before |time2|.
EXPECT_TRUE(db.GetFaviconsLastUpdatedBefore(
time2 - base::TimeDelta::FromSeconds(1), 10)
.empty());
// Requesting a time after |time2| should return |id2|.
auto ids = db.GetFaviconsLastUpdatedBefore(
time2 + base::TimeDelta::FromSeconds(1), 10);
ASSERT_EQ(1u, ids.size());
EXPECT_EQ(id2, ids[0]);
// There should two favicons when using a time after |time1|.
ids = db.GetFaviconsLastUpdatedBefore(time1 + base::TimeDelta::FromSeconds(1),
10);
ASSERT_EQ(2u, ids.size());
// |id2| is before |id1|, so it should be returned first.
EXPECT_EQ(id2, ids[0]);
EXPECT_EQ(id1, ids[1]);
// Repeat previous, but cap the max at 1.
ids = db.GetFaviconsLastUpdatedBefore(time1 + base::TimeDelta::FromSeconds(1),
1);
ASSERT_EQ(1u, ids.size());
// |id2| is before |id1|, so it should be returned first.
EXPECT_EQ(id2, ids[0]);
}
} // namespace favicon } // namespace favicon
...@@ -19,7 +19,7 @@ namespace favicon_base { ...@@ -19,7 +19,7 @@ namespace favicon_base {
struct FallbackIconStyle; struct FallbackIconStyle;
typedef int64_t FaviconID; using FaviconID = int64_t;
// Defines the icon types. // Defines the icon types.
// //
...@@ -95,7 +95,7 @@ struct FaviconRawBitmapResult { ...@@ -95,7 +95,7 @@ struct FaviconRawBitmapResult {
// Define type with same structure as FaviconRawBitmapResult for passing data to // Define type with same structure as FaviconRawBitmapResult for passing data to
// HistoryBackend::SetFavicons(). // HistoryBackend::SetFavicons().
typedef FaviconRawBitmapResult FaviconRawBitmapData; using FaviconRawBitmapData = FaviconRawBitmapResult;
// Result returned by LargeIconService::GetLargeIconOrFallbackStyle(). Contains // Result returned by LargeIconService::GetLargeIconOrFallbackStyle(). Contains
// either the bitmap data if the favicon database has a sufficiently large // either the bitmap data if the favicon database has a sufficiently large
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment