diff options
author | meelapshah@chromium.org <meelapshah@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-17 17:27:19 +0000 |
---|---|---|
committer | meelapshah@chromium.org <meelapshah@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-17 17:27:19 +0000 |
commit | 4233940a69ea3ab796b34f419e62d4b31959708e (patch) | |
tree | 011c4a249f9599d316b424abb2012060e53a9ed2 /chrome | |
parent | 0f71843bc4772a7074536f10db5d505dd7c6a50e (diff) | |
download | chromium_src-4233940a69ea3ab796b34f419e62d4b31959708e.zip chromium_src-4233940a69ea3ab796b34f419e62d4b31959708e.tar.gz chromium_src-4233940a69ea3ab796b34f419e62d4b31959708e.tar.bz2 |
Qurey redirects when a thumbnail is set.
Update list of most visited URLs on an exponential timer, resetting when the history is cleared.
Review URL: http://codereview.chromium.org/155619
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20964 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/tab_contents/tab_contents.cc | 3 | ||||
-rw-r--r-- | chrome/browser/thumbnail_store.cc | 86 | ||||
-rw-r--r-- | chrome/browser/thumbnail_store.h | 32 | ||||
-rw-r--r-- | chrome/browser/thumbnail_store_unittest.cc | 11 |
4 files changed, 107 insertions, 25 deletions
diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc index 21ccbe1..38b7a3e 100644 --- a/chrome/browser/tab_contents/tab_contents.cc +++ b/chrome/browser/tab_contents/tab_contents.cc @@ -1889,7 +1889,8 @@ void TabContents::UpdateThumbnail(const GURL& url, // Tell History about this thumbnail if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) { if (!profile()->IsOffTheRecord()) - profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap, score); + profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap, + score, true); } else { HistoryService* hs; if (!profile()->IsOffTheRecord() && diff --git a/chrome/browser/thumbnail_store.cc b/chrome/browser/thumbnail_store.cc index 9ad570e..9c8c315 100644 --- a/chrome/browser/thumbnail_store.cc +++ b/chrome/browser/thumbnail_store.cc @@ -49,14 +49,18 @@ void ThumbnailStore::Init(const FilePath& db_name, // Get the list of most visited URLs and redirect information from the // HistoryService. - timer_.Start(base::TimeDelta::FromMinutes(30), this, - &ThumbnailStore::UpdateURLData); + seconds_to_next_update_ = kInitialUpdateIntervalSecs; UpdateURLData(); + + // Register to get notified when the history is cleared. + registrar_.Add(this, NotificationType::HISTORY_URLS_DELETED, + Source<Profile>(profile)); } bool ThumbnailStore::SetPageThumbnail(const GURL& url, const SkBitmap& thumbnail, - const ThumbnailScore& score) { + const ThumbnailScore& score, + bool fetch_redirects) { if (!cache_.get()) return false; @@ -86,6 +90,12 @@ bool ThumbnailStore::SetPageThumbnail(const GURL& url, // Update the cache_ with the new thumbnail. (*cache_)[url] = CacheEntry(jpeg_data, score, true); + // Get redirects for this URL. + if (fetch_redirects) { + hs_->QueryRedirectsTo(url, &consumer_, + NewCallback(this, &ThumbnailStore::OnRedirectsForURLAvailable)); + } + return true; } @@ -122,6 +132,47 @@ bool ThumbnailStore::GetPageThumbnail( return true; } +void ThumbnailStore::OnRedirectsForURLAvailable( + HistoryService::Handle handle, + GURL url, + bool success, + history::RedirectList* redirects) { + if (!success) + return; + + if (redirects->empty()) { + (*redirect_urls_)[url] = new RefCountedVector<GURL>; + } else { + const GURL start_url = redirects->back(); + std::reverse(redirects->begin(), redirects->end() - 1); + *(redirects->end() - 1) = url; + (*redirect_urls_)[start_url] = new RefCountedVector<GURL>(*redirects); + } +} + +void ThumbnailStore::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type.value != NotificationType::HISTORY_URLS_DELETED) { + NOTREACHED(); + return; + } + + Details<history::URLsDeletedDetails> url_details(details); + // If all history was cleared, clear all of our data and reset the update + // timer. + if (url_details->all_history) { + most_visited_urls_.reset(); + redirect_urls_.reset(); + cache_.reset(); + + timer_.Stop(); + seconds_to_next_update_ = kInitialUpdateIntervalSecs; + timer_.Start(base::TimeDelta::FromSeconds(seconds_to_next_update_), this, + &ThumbnailStore::UpdateURLData); + } +} + void ThumbnailStore::UpdateURLData() { int result_count = ThumbnailStore::kMaxCacheSize + url_blacklist_->GetSize(); hs_->QueryTopURLsAndRedirects(result_count, &consumer_, @@ -136,16 +187,24 @@ void ThumbnailStore::OnURLDataAvailable(std::vector<GURL>* urls, most_visited_urls_.reset(new std::vector<GURL>(*urls)); redirect_urls_.reset(new history::RedirectMap(*redirects)); CleanCacheData(); + + // Schedule the next update. + if (seconds_to_next_update_ < kMaxUpdateIntervalSecs) + seconds_to_next_update_ *= 2; + timer_.Start(base::TimeDelta::FromSeconds(seconds_to_next_update_), this, + &ThumbnailStore::UpdateURLData); } void ThumbnailStore::CleanCacheData() { if (!cache_.get()) return; + scoped_refptr<RefCountedVector<GURL> > urls_to_delete = + new RefCountedVector<GURL>; + // For each URL in the cache, search the RedirectMap for the originating URL. // If this URL is blacklisted or not in the most visited list, delete the // thumbnail data for it from the cache and from disk in the background. - scoped_refptr<RefCountedVector<GURL> > old_urls = new RefCountedVector<GURL>; for (Cache::iterator cache_it = cache_->begin(); cache_it != cache_->end();) { const GURL* url = NULL; @@ -160,21 +219,20 @@ void ThumbnailStore::CleanCacheData() { } if (url == NULL || IsURLBlacklisted(*url) || !IsPopular(*url)) { - old_urls->data.push_back(cache_it->first); + urls_to_delete->data.push_back(cache_it->first); cache_->erase(cache_it++); } else { - cache_it++; + ++cache_it; } } - if (old_urls->data.size()) { - g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE, - NewRunnableMethod(this, &ThumbnailStore::CommitCacheToDB, old_urls)); - } + g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE, + NewRunnableMethod(this, &ThumbnailStore::CommitCacheToDB, + urls_to_delete)); } void ThumbnailStore::CommitCacheToDB( - scoped_refptr<RefCountedVector<GURL> > stale_urls) const { + scoped_refptr<RefCountedVector<GURL> > urls_to_delete) const { if (!db_) return; @@ -182,9 +240,9 @@ void ThumbnailStore::CommitCacheToDB( DCHECK(rv == SQLITE_OK) << "Failed to begin transaction"; // Delete old thumbnails. - if (stale_urls.get()) { - for (std::vector<GURL>::iterator it = stale_urls->data.begin(); - it != stale_urls->data.end(); ++it) { + if (urls_to_delete.get()) { + for (std::vector<GURL>::iterator it = urls_to_delete->data.begin(); + it != urls_to_delete->data.end(); ++it) { SQLITE_UNIQUE_STATEMENT(statement, *statement_cache_, "DELETE FROM thumbnails WHERE url=?"); statement->bind_string(0, it->spec()); diff --git a/chrome/browser/thumbnail_store.h b/chrome/browser/thumbnail_store.h index 0ec81f4..d68965e 100644 --- a/chrome/browser/thumbnail_store.h +++ b/chrome/browser/thumbnail_store.h @@ -16,6 +16,7 @@ #include "chrome/browser/cancelable_request.h" #include "chrome/browser/history/history.h" #include "chrome/browser/history/url_database.h" // For DBCloseScoper +#include "chrome/common/notification_service.h" #include "chrome/common/pref_names.h" #include "chrome/common/ref_counted_util.h" #include "chrome/common/sqlite_compiled_statement.h" @@ -34,7 +35,8 @@ class Time; // This storage interface provides storage for the thumbnails used // by the new_tab_ui. -class ThumbnailStore : public base::RefCountedThreadSafe<ThumbnailStore> { +class ThumbnailStore : public base::RefCountedThreadSafe<ThumbnailStore>, + public NotificationObserver { public: ThumbnailStore(); ~ThumbnailStore(); @@ -46,7 +48,8 @@ class ThumbnailStore : public base::RefCountedThreadSafe<ThumbnailStore> { // Stores the given thumbnail and score with the associated url in the cache. bool SetPageThumbnail(const GURL& url, const SkBitmap& thumbnail, - const ThumbnailScore& score); + const ThumbnailScore& score, + bool fetch_redirects); // for debugging // Sets *data to point to the thumbnail for the given url. // Returns false if no thumbnail available. @@ -76,6 +79,11 @@ class ThumbnailStore : public base::RefCountedThreadSafe<ThumbnailStore> { // Data structure used to store thumbnail data in memory. typedef std::map<GURL, CacheEntry> Cache; + // NotificationObserver implementation + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + // Most visited URLs and their redirect lists ------------------------------- // Query the HistoryService for the most visited URLs and the most recent @@ -87,6 +95,15 @@ class ThumbnailStore : public base::RefCountedThreadSafe<ThumbnailStore> { void OnURLDataAvailable(std::vector<GURL>* urls, history::RedirectMap* redirects); + // The callback for the redirects request to the HistoryService made in + // SetPageThumbnail. If we have a redirect chain A -> B -> C, this function + // will be called with url=C and redirects = {B -> A}. This information gets + // inserted into the RedirectMap as A => {B -> C}. + void OnRedirectsForURLAvailable(HistoryService::Handle handle, + GURL url, + bool success, + history::RedirectList* redirects); + // Remove stale data -------------------------------------------------------- // Remove entries from the in memory thumbnail cache and redirect lists @@ -113,7 +130,7 @@ class ThumbnailStore : public base::RefCountedThreadSafe<ThumbnailStore> { // Delete each URL in the given vector from the DB and write all dirty // cache entries to the DB. void CommitCacheToDB( - scoped_refptr<RefCountedVector<GURL> > stale_urls) const; + scoped_refptr<RefCountedVector<GURL> > urls_to_delete) const; // Decide whether to store data --------------------------------------------- @@ -155,12 +172,17 @@ class ThumbnailStore : public base::RefCountedThreadSafe<ThumbnailStore> { scoped_ptr<history::RedirectMap> redirect_urls_; // Timer on which UpdateURLData runs. - base::RepeatingTimer<ThumbnailStore> timer_; + base::OneShotTimer<ThumbnailStore> timer_; + int seconds_to_next_update_; // Consumer for queries to the HistoryService. CancelableRequestConsumer consumer_; - static const unsigned int kMaxCacheSize = 45; + NotificationRegistrar registrar_; + + static const unsigned int kMaxCacheSize = 24; + static const int64 kInitialUpdateIntervalSecs = 20; + static const int64 kMaxUpdateIntervalSecs = 3600; DISALLOW_COPY_AND_ASSIGN(ThumbnailStore); }; diff --git a/chrome/browser/thumbnail_store_unittest.cc b/chrome/browser/thumbnail_store_unittest.cc index 8d3cb8a..483cdf1 100644 --- a/chrome/browser/thumbnail_store_unittest.cc +++ b/chrome/browser/thumbnail_store_unittest.cc @@ -145,8 +145,8 @@ TEST_F(ThumbnailStoreTest, UpdateThumbnail) { // store_ google_ with a low score, then weewar_ with a higher score // and check that weewar_ overwrote google_. - EXPECT_TRUE(store_->SetPageThumbnail(url_, *google_, score_)); - EXPECT_TRUE(store_->SetPageThumbnail(url_, *weewar_, score2)); + EXPECT_TRUE(store_->SetPageThumbnail(url_, *google_, score_, false)); + EXPECT_TRUE(store_->SetPageThumbnail(url_, *weewar_, score2, false)); EXPECT_TRUE(store_->GetPageThumbnail(url_, &read_image)); EXPECT_EQ(read_image->data.size(), jpeg_weewar_->data.size()); @@ -165,7 +165,7 @@ TEST_F(ThumbnailStoreTest, RetrieveFromCache) { // Store a thumbnail into the cache and retrieve it. - EXPECT_TRUE(store_->SetPageThumbnail(url_, *google_, score_)); + EXPECT_TRUE(store_->SetPageThumbnail(url_, *google_, score_, false)); EXPECT_TRUE(store_->GetPageThumbnail(url_, &read_image)); EXPECT_TRUE(score_.Equals((*store_->cache_)[url_].score_)); EXPECT_TRUE(read_image->data.size() == jpeg_google_->data.size()); @@ -176,7 +176,7 @@ TEST_F(ThumbnailStoreTest, RetrieveFromCache) { } TEST_F(ThumbnailStoreTest, RetrieveFromDisk) { - EXPECT_TRUE(store_->SetPageThumbnail(url_, *google_, score_)); + EXPECT_TRUE(store_->SetPageThumbnail(url_, *google_, score_, false)); // Write the thumbnail to disk and retrieve it. @@ -216,7 +216,8 @@ TEST_F(ThumbnailStoreTest, FollowRedirects) { store_->most_visited_urls_->push_back(my_url); - EXPECT_TRUE(store_->SetPageThumbnail(GURL("google.com"), *google_, score_)); + EXPECT_TRUE(store_->SetPageThumbnail(GURL("google.com"), *google_, score_, + false)); EXPECT_TRUE(store_->GetPageThumbnail(my_url, &read_image)); read_image->Release(); |