Home | History | Annotate | Download | only in history
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/history/top_sites_cache.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/memory/ref_counted_memory.h"
      9 
     10 namespace history {
     11 
     12 TopSitesCache::TopSitesCache() {
     13 }
     14 
     15 TopSitesCache::~TopSitesCache() {
     16 }
     17 
     18 void TopSitesCache::SetTopSites(const MostVisitedURLList& top_sites) {
     19   top_sites_ = top_sites;
     20   GenerateCanonicalURLs();
     21 }
     22 
     23 void TopSitesCache::SetThumbnails(const URLToImagesMap& images) {
     24   images_ = images;
     25 }
     26 
     27 Images* TopSitesCache::GetImage(const GURL& url) {
     28   return &images_[GetCanonicalURL(url)];
     29 }
     30 
     31 bool TopSitesCache::GetPageThumbnail(
     32     const GURL& url,
     33     scoped_refptr<base::RefCountedMemory>* bytes) {
     34   std::map<GURL, Images>::const_iterator found =
     35       images_.find(GetCanonicalURL(url));
     36   if (found != images_.end()) {
     37     base::RefCountedMemory* data = found->second.thumbnail.get();
     38     if (data) {
     39       *bytes = data;
     40       return true;
     41     }
     42   }
     43   return false;
     44 }
     45 
     46 bool TopSitesCache::GetPageThumbnailScore(const GURL& url,
     47                                           ThumbnailScore* score) {
     48   std::map<GURL, Images>::const_iterator found =
     49       images_.find(GetCanonicalURL(url));
     50   if (found != images_.end()) {
     51     *score = found->second.thumbnail_score;
     52     return true;
     53   }
     54   return false;
     55 }
     56 
     57 const GURL& TopSitesCache::GetCanonicalURL(const GURL& url) {
     58   CanonicalURLs::iterator i = TopSitesCache::GetCanonicalURLsIterator(url);
     59   return i == canonical_urls_.end() ? url : i->first.first->url;
     60 }
     61 
     62 bool TopSitesCache::IsKnownURL(const GURL& url) {
     63   return GetCanonicalURLsIterator(url) != canonical_urls_.end();
     64 }
     65 
     66 size_t TopSitesCache::GetURLIndex(const GURL& url) {
     67   DCHECK(IsKnownURL(url));
     68   return GetCanonicalURLsIterator(url)->second;
     69 }
     70 
     71 void TopSitesCache::GenerateCanonicalURLs() {
     72   canonical_urls_.clear();
     73   for (size_t i = 0; i < top_sites_.size(); i++)
     74     StoreRedirectChain(top_sites_[i].redirects, i);
     75 }
     76 
     77 void TopSitesCache::StoreRedirectChain(const RedirectList& redirects,
     78                                        size_t destination) {
     79   // redirects is empty if the user pinned a site and there are not enough top
     80   // sites before the pinned site.
     81 
     82   // Map all the redirected URLs to the destination.
     83   for (size_t i = 0; i < redirects.size(); i++) {
     84     // If this redirect is already known, don't replace it with a new one.
     85     if (!IsKnownURL(redirects[i])) {
     86       CanonicalURLEntry entry;
     87       entry.first = &(top_sites_[destination]);
     88       entry.second = i;
     89       canonical_urls_[entry] = destination;
     90     }
     91   }
     92 }
     93 
     94 TopSitesCache::CanonicalURLs::iterator TopSitesCache::GetCanonicalURLsIterator(
     95     const GURL& url) {
     96   MostVisitedURL most_visited_url;
     97   most_visited_url.redirects.push_back(url);
     98   CanonicalURLEntry entry;
     99   entry.first = &most_visited_url;
    100   entry.second = 0u;
    101   return canonical_urls_.find(entry);
    102 }
    103 
    104 }  // namespace history
    105