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