summaryrefslogtreecommitdiffstats
path: root/chrome/browser/history
diff options
context:
space:
mode:
authornshkrob@chromium.org <nshkrob@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-02 22:19:07 +0000
committernshkrob@chromium.org <nshkrob@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-02 22:19:07 +0000
commit7f22e72403cf15277253e12be647de40d8001f0c (patch)
tree59d49634e6c062554dcfbaf364a57453035717e7 /chrome/browser/history
parent4f5193277bb84f43d0fbafb42e1442ac77ad47c3 (diff)
downloadchromium_src-7f22e72403cf15277253e12be647de40d8001f0c.zip
chromium_src-7f22e72403cf15277253e12be647de40d8001f0c.tar.gz
chromium_src-7f22e72403cf15277253e12be647de40d8001f0c.tar.bz2
1. Write thumbnail to the database on SetPageThumbnail.
2. Store the thumbnail score in the database. BUG=None TEST=TopSitesTest Review URL: http://codereview.chromium.org/2499003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@48777 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/history')
-rw-r--r--chrome/browser/history/top_sites.cc27
-rw-r--r--chrome/browser/history/top_sites.h11
-rw-r--r--chrome/browser/history/top_sites_database.cc33
-rw-r--r--chrome/browser/history/top_sites_unittest.cc51
4 files changed, 101 insertions, 21 deletions
diff --git a/chrome/browser/history/top_sites.cc b/chrome/browser/history/top_sites.cc
index d8ea651..2e4a39c 100644
--- a/chrome/browser/history/top_sites.cc
+++ b/chrome/browser/history/top_sites.cc
@@ -47,12 +47,14 @@ void TopSites::ReadDatabase() {
MostVisitedURL url = top_sites_[i];
Images thumbnail;
if (db_->GetPageThumbnail(url, &thumbnail)) {
- SetPageThumbnail(url.url, thumbnail.thumbnail, thumbnail.thumbnail_score);
+ SetPageThumbnailNoDB(url.url, thumbnail.thumbnail,
+ thumbnail.thumbnail_score);
}
}
}
-// Public wrapper that encodes the bitmap into RefCountedBytes.
+// Public function that encodes the bitmap into RefCountedBytes and
+// updates the database.
bool TopSites::SetPageThumbnail(const GURL& url,
const SkBitmap& thumbnail,
const ThumbnailScore& score) {
@@ -66,13 +68,26 @@ bool TopSites::SetPageThumbnail(const GURL& url,
&thumbnail_data->data);
if (!encoded)
return false;
- return SetPageThumbnail(url, thumbnail_data, score);
+ if (!SetPageThumbnailNoDB(url, thumbnail_data, score))
+ return false;
+
+ // Update the database.
+ if (!db_.get())
+ return true;
+ std::map<GURL, size_t>::iterator found = canonical_urls_.find(url);
+ if (found == canonical_urls_.end())
+ return false;
+ size_t index = found->second;
+
+ MostVisitedURL& most_visited = top_sites_[index];
+ db_->SetPageThumbnail(most_visited, index, top_images_[most_visited.url]);
+ return true;
}
// private
-bool TopSites::SetPageThumbnail(const GURL& url,
- const RefCountedBytes* thumbnail_data,
- const ThumbnailScore& score) {
+bool TopSites::SetPageThumbnailNoDB(const GURL& url,
+ const RefCountedBytes* thumbnail_data,
+ const ThumbnailScore& score) {
AutoLock lock(lock_);
std::map<GURL, size_t>::iterator found = canonical_urls_.find(url);
diff --git a/chrome/browser/history/top_sites.h b/chrome/browser/history/top_sites.h
index 4f6f854..d05afb1 100644
--- a/chrome/browser/history/top_sites.h
+++ b/chrome/browser/history/top_sites.h
@@ -91,11 +91,12 @@ class TopSites : public base::RefCountedThreadSafe<TopSites> {
~TopSites();
- // Called by the public SetPageThumbnail. Takes RefCountedBytes
- // rather than a SkBitmap.
- bool SetPageThumbnail(const GURL& url,
- const RefCountedBytes* thumbnail_data,
- const ThumbnailScore& score);
+ // Sets the thumbnail without writing to the database. Useful when
+ // reading last known top sites from the DB.
+ // Returns true if the thumbnail was set, false if the existing one is better.
+ bool SetPageThumbnailNoDB(const GURL& url,
+ const RefCountedBytes* thumbnail_data,
+ const ThumbnailScore& score);
void StartQueryForMostVisited();
diff --git a/chrome/browser/history/top_sites_database.cc b/chrome/browser/history/top_sites_database.cc
index 5b13d61..537b19f 100644
--- a/chrome/browser/history/top_sites_database.cc
+++ b/chrome/browser/history/top_sites_database.cc
@@ -34,7 +34,11 @@ bool TopSitesDatabaseImpl::InitThumbnailTable() {
"url_rank INTEGER ,"
"title LONGVARCHAR,"
"thumbnail BLOB,"
- "redirects LONGVARCHAR)")) {
+ "redirects LONGVARCHAR,"
+ "boring_score DOUBLE DEFAULT 1.0, "
+ "good_clipping INTEGER DEFAULT 0, "
+ "at_top INTEGER DEFAULT 0, "
+ "last_updated INTEGER DEFAULT 0) ")) {
LOG(WARNING) << db_.GetErrorMessage();
return false;
}
@@ -101,8 +105,9 @@ void TopSitesDatabaseImpl::SetPageThumbnail(const MostVisitedURL& url,
sql::Statement statement(db_.GetCachedStatement(
SQL_FROM_HERE,
"INSERT OR REPLACE INTO thumbnails "
- "(url, url_rank, title, thumbnail, redirects) "
- "VALUES (?, ?, ?, ?, ?)")); // TODO(Nik): add the rest of the schema.
+ "(url, url_rank, title, thumbnail, redirects, "
+ "boring_score, good_clipping, at_top, last_updated) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"));
if (!statement)
return;
@@ -114,6 +119,11 @@ void TopSitesDatabaseImpl::SetPageThumbnail(const MostVisitedURL& url,
static_cast<int>(thumbnail.thumbnail->data.size()));
}
statement.BindString(4, GetRedirects(url));
+ const ThumbnailScore& score = thumbnail.thumbnail_score;
+ statement.BindDouble(5, score.boring_score);
+ statement.BindBool(6, score.good_clipping);
+ statement.BindBool(7, score.at_top);
+ statement.BindInt64(8, score.time_at_snapshot.ToInternalValue());
if (!statement.Run())
NOTREACHED() << db_.GetErrorMessage();
@@ -166,23 +176,28 @@ void TopSitesDatabaseImpl::SetPageThumbnail(const MostVisitedURL& url,
bool TopSitesDatabaseImpl::GetPageThumbnail(const MostVisitedURL& url,
TopSites::Images* thumbnail) {
- sql::Statement select_statement(db_.GetCachedStatement(
+ sql::Statement statement(db_.GetCachedStatement(
SQL_FROM_HERE,
- "SELECT thumbnail "
+ "SELECT thumbnail, boring_score, good_clipping, at_top, last_updated "
"FROM thumbnails WHERE url=?"));
- if (!select_statement) {
+ if (!statement) {
LOG(WARNING) << db_.GetErrorMessage();
return false;
}
- select_statement.BindString(0, url.url.spec());
- if (!select_statement.Step())
+ statement.BindString(0, url.url.spec());
+ if (!statement.Step())
return false;
std::vector<unsigned char> data;
- select_statement.ColumnBlobAsVector(0, &data);
+ statement.ColumnBlobAsVector(0, &data);
thumbnail->thumbnail = RefCountedBytes::TakeVector(&data);
+ thumbnail->thumbnail_score.boring_score = statement.ColumnDouble(1);
+ thumbnail->thumbnail_score.good_clipping = statement.ColumnBool(2);
+ thumbnail->thumbnail_score.at_top = statement.ColumnBool(3);
+ thumbnail->thumbnail_score.time_at_snapshot =
+ base::Time::FromInternalValue(statement.ColumnInt64(4));
return true;
}
diff --git a/chrome/browser/history/top_sites_unittest.cc b/chrome/browser/history/top_sites_unittest.cc
index 7463075..0cf282a 100644
--- a/chrome/browser/history/top_sites_unittest.cc
+++ b/chrome/browser/history/top_sites_unittest.cc
@@ -9,6 +9,7 @@
#include "chrome/browser/history/top_sites_database.h"
#include "chrome/test/testing_profile.h"
#include "chrome/tools/profiles/thumbnail-inl.h"
+#include "gfx/codec/jpeg_codec.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
@@ -30,6 +31,7 @@ class TopSitesTest : public testing::Test {
FilePath& file_name() { return file_name_; }
RefCountedBytes* google_thumbnail() { return google_thumbnail_; }
RefCountedBytes* random_thumbnail() { return random_thumbnail_; }
+ RefCountedBytes* weewar_thumbnail() { return weewar_thumbnail_; }
virtual void SetUp() {
profile_.reset(new TestingProfile);
@@ -43,8 +45,12 @@ class TopSitesTest : public testing::Test {
std::vector<unsigned char> google_data(kGoogleThumbnail,
kGoogleThumbnail +
sizeof(kGoogleThumbnail));
+ std::vector<unsigned char> weewar_data(kWeewarThumbnail,
+ kWeewarThumbnail +
+ sizeof(kWeewarThumbnail));
random_thumbnail_ = new RefCountedBytes(random_data);
google_thumbnail_ = new RefCountedBytes(google_data);
+ weewar_thumbnail_ = new RefCountedBytes(weewar_data);
}
virtual void TearDown() {
@@ -81,6 +87,7 @@ class TopSitesTest : public testing::Test {
FilePath file_name_; // Database filename.
scoped_refptr<RefCountedBytes> google_thumbnail_;
scoped_refptr<RefCountedBytes> random_thumbnail_;
+ scoped_refptr<RefCountedBytes> weewar_thumbnail_;
DISALLOW_COPY_AND_ASSIGN(TopSitesTest);
};
@@ -193,6 +200,8 @@ static bool ThumbnailsAreEqual(RefCountedBytes* t1,
RefCountedBytes* t2) {
if (!t1 || !t2)
return false;
+ if (t1->data.size() != t2->data.size())
+ return false;
return std::equal(t1->data.begin(),
t1->data.end(),
t2->data.begin());
@@ -549,13 +558,53 @@ TEST_F(TopSitesTest, RealDatabase) {
hs.AppendMockPage(news_url, news_title);
top_sites().SetMockHistoryService(&hs);
- // This writes the new data to the DB.
+ // This requests data from History Service and writes it to the DB.
top_sites().StartQueryForMostVisited();
result = db->GetTopURLs();
ASSERT_EQ(2u, result.size());
EXPECT_EQ(google_title, result[0].title);
EXPECT_EQ(news_title, result[1].title);
+
+ scoped_ptr<SkBitmap> weewar_bitmap(
+ gfx::JPEGCodec::Decode(weewar_thumbnail()->front(),
+ weewar_thumbnail()->size()));
+
+ base::Time now = base::Time::Now();
+ ThumbnailScore low_score(1.0, true, true, now);
+ ThumbnailScore medium_score(0.5, true, true, now);
+ ThumbnailScore high_score(0.0, true, true, now);
+
+ // 1. Set to weewar. (Writes the thumbnail to the DB.)
+ EXPECT_TRUE(top_sites().SetPageThumbnail(google1_url,
+ *weewar_bitmap,
+ medium_score));
+ RefCountedBytes* out_1;
+ TopSites::Images out_2;
+ top_sites().GetPageThumbnail(google1_url, &out_1);
+ db->GetPageThumbnail(url2, &out_2);
+ EXPECT_TRUE(ThumbnailsAreEqual(out_1, out_2.thumbnail));
+
+ scoped_ptr<SkBitmap> google_bitmap(
+ gfx::JPEGCodec::Decode(google_thumbnail()->front(),
+ google_thumbnail()->size()));
+
+ // 2. Set to google - low score.
+ EXPECT_FALSE(top_sites().SetPageThumbnail(google1_url,
+ *google_bitmap,
+ low_score));
+
+ // 3. Set to google - high score.
+ EXPECT_TRUE(top_sites().SetPageThumbnail(google1_url,
+ *google_bitmap,
+ high_score));
+ // Check that the thumbnail was updated.
+ top_sites().GetPageThumbnail(google1_url, &out_1);
+ EXPECT_FALSE(ThumbnailsAreEqual(out_1, out_2.thumbnail));
+ // Read the new thumbnail from the DB - should match what's in TopSites.
+ db->GetPageThumbnail(url2, &out_2);
+ EXPECT_TRUE(ThumbnailsAreEqual(out_1, out_2.thumbnail));
+ EXPECT_TRUE(high_score.Equals(out_2.thumbnail_score));
}
} // namespace history