Home | History | Annotate | Download | only in history
      1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_
      6 #define CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_
      7 #pragma once
      8 
      9 #include <algorithm>
     10 #include <map>
     11 #include <string>
     12 
     13 #include "base/memory/ref_counted.h"
     14 #include "chrome/browser/history/history_types.h"
     15 
     16 class RefCountedBytes;
     17 
     18 namespace history {
     19 
     20 // TopSitesCache caches the top sites and thumbnails for TopSites.
     21 class TopSitesCache {
     22  public:
     23   TopSitesCache();
     24   ~TopSitesCache();
     25 
     26   // The top sites.
     27   void SetTopSites(const MostVisitedURLList& top_sites);
     28   const MostVisitedURLList& top_sites() const { return top_sites_; }
     29 
     30   // The thumbnails.
     31   void SetThumbnails(const URLToImagesMap& images);
     32   const URLToImagesMap& images() const { return images_; }
     33 
     34   // Set a thumbnail.
     35   void SetPageThumbnail(const GURL& url,
     36                         RefCountedBytes* thumbnail,
     37                         const ThumbnailScore& score);
     38 
     39   // Returns the thumbnail as an Image for the specified url. This adds an entry
     40   // for |url| if one has not yet been added.
     41   Images* GetImage(const GURL& url);
     42 
     43   // Fetches the thumbnail for the specified url. Returns true if there is a
     44   // thumbnail for the specified url.
     45   bool GetPageThumbnail(const GURL& url,
     46                         scoped_refptr<RefCountedBytes>* bytes);
     47 
     48   // Fetches the thumbnail score for the specified url. Returns true if
     49   // there is a thumbnail score for the specified url.
     50   bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score);
     51 
     52   // Returns the canonical URL for |url|.
     53   GURL GetCanonicalURL(const GURL& url);
     54 
     55   // Returns true if |url| is known.
     56   bool IsKnownURL(const GURL& url);
     57 
     58   // Returns the index into |top_sites_| for |url|.
     59   size_t GetURLIndex(const GURL& url);
     60 
     61   // Removes any thumbnails that are no longer referenced by the top sites.
     62   void RemoveUnreferencedThumbnails();
     63 
     64  private:
     65   // The entries in CanonicalURLs, see CanonicalURLs for details. The second
     66   // argument gives the index of the URL into MostVisitedURLs redirects.
     67   typedef std::pair<MostVisitedURL*, size_t> CanonicalURLEntry;
     68 
     69   // Comparator used for CanonicalURLs.
     70   class CanonicalURLComparator {
     71    public:
     72     bool operator()(const CanonicalURLEntry& e1,
     73                     const CanonicalURLEntry& e2) const {
     74       return e1.first->redirects[e1.second] < e2.first->redirects[e2.second];
     75     }
     76   };
     77 
     78   // This is used to map from redirect url to the MostVisitedURL the redirect is
     79   // from. Ideally this would be map<GURL, size_t> (second param indexing into
     80   // top_sites_), but this results in duplicating all redirect urls. As some
     81   // sites have a lot of redirects, we instead use the MostVisitedURL* and the
     82   // index of the redirect as the key, and the index into top_sites_ as the
     83   // value. This way we aren't duplicating GURLs. CanonicalURLComparator
     84   // enforces the ordering as if we were using GURLs.
     85   typedef std::map<CanonicalURLEntry, size_t,
     86                    CanonicalURLComparator> CanonicalURLs;
     87 
     88   // Generates the set of canonical urls from |top_sites_|.
     89   void GenerateCanonicalURLs();
     90 
     91   // Stores a set of redirects. This is used by GenerateCanonicalURLs.
     92   void StoreRedirectChain(const RedirectList& redirects, size_t destination);
     93 
     94   // Returns the iterator into canconical_urls_ for the specified url.
     95   CanonicalURLs::iterator GetCanonicalURLsIterator(const GURL& url);
     96 
     97   // The top sites.
     98   MostVisitedURLList top_sites_;
     99 
    100   // The images. These map from canonical url to image.
    101   URLToImagesMap images_;
    102 
    103   // Generated from the redirects to and from the most visited pages. See
    104   // description above typedef for details.
    105   CanonicalURLs canonical_urls_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(TopSitesCache);
    108 };
    109 
    110 }  // namespace history
    111 
    112 #endif  // CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_
    113