diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-24 18:35:49 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-24 18:35:49 +0000 |
commit | d0742d72600ba1eb33aff7d0ed43c8a118dcd141 (patch) | |
tree | 7f06db3fb511be1fc590c8519a096b331cb9881d /chrome/browser/history/top_sites_cache.cc | |
parent | 7ff65ca9f426a13726533d02f8edc6ce247064e2 (diff) | |
download | chromium_src-d0742d72600ba1eb33aff7d0ed43c8a118dcd141.zip chromium_src-d0742d72600ba1eb33aff7d0ed43c8a118dcd141.tar.gz chromium_src-d0742d72600ba1eb33aff7d0ed43c8a118dcd141.tar.bz2 |
Refactors TopSites so that it's hopefully easier to maintain and
doesn't suffer the plethora of threading issues that exist with the
current version. Here's the breakdown of what was refactored:
. TopSitesCache: Contains the most visited urls and thumbnails.
. TopSitesBackend: All mutations to topsites data end up calling into
the backend on the UI thread. TopSitesBackend processes the method on
the DB thread calling through to the TopSitesDatabase.
. TopSites: uses two TopSitesCache. One that contains the raw history
data, the other contains the processed data (pinned/blacklisted). The
processed cache can be accessed on any thread.
TopSites waits until history loads to know if it should migrate or use
it's own db. I could probably make these execute in parallel, but for
now this is how it works.
This patch also makes it so the dom ui accesses the thumbnails on the
IO thread.
BUG=56382
TEST=Make sure all your thumbnails are correctly updated and you don't
see problems.
Review URL: http://codereview.chromium.org/3440018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63687 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/history/top_sites_cache.cc')
-rw-r--r-- | chrome/browser/history/top_sites_cache.cc | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/chrome/browser/history/top_sites_cache.cc b/chrome/browser/history/top_sites_cache.cc new file mode 100644 index 0000000..9bae928 --- /dev/null +++ b/chrome/browser/history/top_sites_cache.cc @@ -0,0 +1,96 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/history/top_sites_cache.h" + +#include "base/logging.h" +#include "base/ref_counted_memory.h" + +namespace history { + +TopSitesCache::TopSitesCache() { +} + +TopSitesCache::~TopSitesCache() { +} + +void TopSitesCache::SetTopSites(const MostVisitedURLList& top_sites) { + top_sites_ = top_sites; + GenerateCanonicalURLs(); +} + +void TopSitesCache::SetThumbnails(const URLToImagesMap& images) { + images_ = images; +} + +void TopSitesCache::SetPageThumbnail(const GURL& url, + RefCountedBytes* thumbnail, + const ThumbnailScore& score) { + Images& img = images_[GetCanonicalURL(url)]; + img.thumbnail = thumbnail; + img.thumbnail_score = score; +} + +Images* TopSitesCache::GetImage(const GURL& url) { + return &images_[GetCanonicalURL(url)]; +} + +bool TopSitesCache::GetPageThumbnail(const GURL& url, + scoped_refptr<RefCountedBytes>* bytes) { + std::map<GURL, Images>::const_iterator found = + images_.find(GetCanonicalURL(url)); + if (found != images_.end()) { + *bytes = found->second.thumbnail.get(); + return true; + } + return false; +} + +GURL TopSitesCache::GetCanonicalURL(const GURL& url) { + std::map<GURL, size_t>::const_iterator i = canonical_urls_.find(url); + return i == canonical_urls_.end() ? url : top_sites_[i->second].url; +} + +bool TopSitesCache::IsKnownURL(const GURL& url) { + return canonical_urls_.find(url) != canonical_urls_.end(); +} + +size_t TopSitesCache::GetURLIndex(const GURL& url) { + DCHECK(IsKnownURL(url)); + return canonical_urls_[url]; +} + +void TopSitesCache::RemoveUnreferencedThumbnails() { + for (URLToImagesMap::iterator i = images_.begin(); i != images_.end(); ) { + if (IsKnownURL(i->first)) { + ++i; + } else { + URLToImagesMap::iterator next_i = i; + ++next_i; + images_.erase(i); + i = next_i; + } + } +} + +void TopSitesCache::GenerateCanonicalURLs() { + canonical_urls_.clear(); + for (size_t i = 0; i < top_sites_.size(); i++) + StoreRedirectChain(top_sites_[i].redirects, i); +} + +void TopSitesCache::StoreRedirectChain(const RedirectList& redirects, + size_t destination) { + // redirects is empty if the user pinned a site and there are not enough top + // sites before the pinned site. + + // Map all the redirected URLs to the destination. + for (size_t i = 0; i < redirects.size(); i++) { + // If this redirect is already known, don't replace it with a new one. + if (canonical_urls_.find(redirects[i]) == canonical_urls_.end()) + canonical_urls_[redirects[i]] = destination; + } +} + +} // namespace history |