top_sites_cache.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick// Copyright (c) 2012 The Chromium Authors. All rights reserved.
204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick// Use of this source code is governed by a BSD-style license that can be
304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick// found in the LICENSE file.
404317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick#ifndef CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_
604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick#define CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_
704317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
804317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick#include <map>
904317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick#include <utility>
1004317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
1104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick#include "base/memory/ref_counted.h"
1204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick#include "chrome/browser/history/history_types.h"
1304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
1404317cc618aeae28910916469e074d8ce0fcaa03Andrew Tricknamespace history {
1504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
1604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick// TopSitesCache caches the top sites and thumbnails for TopSites.
1704317cc618aeae28910916469e074d8ce0fcaa03Andrew Trickclass TopSitesCache {
18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth public:
190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth  TopSitesCache();
20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth  ~TopSitesCache();
2104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
22d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth  // The top sites.
2304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  void SetTopSites(const MostVisitedURLList& top_sites);
2404317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  const MostVisitedURLList& top_sites() const { return top_sites_; }
2504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
2604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // The thumbnails.
2704317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  void SetThumbnails(const URLToImagesMap& images);
2804317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  const URLToImagesMap& images() const { return images_; }
2904317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
3004317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Returns the thumbnail as an Image for the specified url. This adds an entry
3104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // for |url| if one has not yet been added.
3204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  Images* GetImage(const GURL& url);
3304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
3404317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Fetches the thumbnail for the specified url. Returns true if there is a
3504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // thumbnail for the specified url. It is possible for a URL to be in TopSites
3604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // but not have an thumbnail.
3704317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  bool GetPageThumbnail(const GURL& url,
3804317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick                        scoped_refptr<base::RefCountedMemory>* bytes);
3904317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
4004317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Fetches the thumbnail score for the specified url. Returns true if
4104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // there is a thumbnail score for the specified url.
4204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score);
4304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
4404317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Returns the canonical URL for |url|.
4504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  GURL GetCanonicalURL(const GURL& url);
4604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
4704317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Returns true if |url| is known.
4804317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  bool IsKnownURL(const GURL& url);
4904317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
5004317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Returns the index into |top_sites_| for |url|.
5104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  size_t GetURLIndex(const GURL& url);
5204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
5304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick private:
5404317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // The entries in CanonicalURLs, see CanonicalURLs for details. The second
5504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // argument gives the index of the URL into MostVisitedURLs redirects.
5604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  typedef std::pair<MostVisitedURL*, size_t> CanonicalURLEntry;
5704317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
5804317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Comparator used for CanonicalURLs.
5904317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  class CanonicalURLComparator {
6004317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick   public:
6104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick    bool operator()(const CanonicalURLEntry& e1,
6204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick                    const CanonicalURLEntry& e2) const {
6304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick      return e1.first->redirects[e1.second] < e2.first->redirects[e2.second];
6404317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick    }
6504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  };
6604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
6704317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // This is used to map from redirect url to the MostVisitedURL the redirect is
6804317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // from. Ideally this would be map<GURL, size_t> (second param indexing into
6904317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // top_sites_), but this results in duplicating all redirect urls. As some
7004317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // sites have a lot of redirects, we instead use the MostVisitedURL* and the
7104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // index of the redirect as the key, and the index into top_sites_ as the
7204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // value. This way we aren't duplicating GURLs. CanonicalURLComparator
7304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // enforces the ordering as if we were using GURLs.
7404317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  typedef std::map<CanonicalURLEntry, size_t,
7504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick                   CanonicalURLComparator> CanonicalURLs;
7604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
7704317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Generates the set of canonical urls from |top_sites_|.
7804317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  void GenerateCanonicalURLs();
7904317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
8004317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Stores a set of redirects. This is used by GenerateCanonicalURLs.
8104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  void StoreRedirectChain(const RedirectList& redirects, size_t destination);
8204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
8304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Returns the iterator into canconical_urls_ for the specified url.
8404317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  CanonicalURLs::iterator GetCanonicalURLsIterator(const GURL& url);
8504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
8604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // The top sites.
8771246fb830eb15cbf6f976de33f623b6bcb27b4bMatt Arsenault  MostVisitedURLList top_sites_;
8804317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
8904317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // The images. These map from canonical url to image.
9004317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  URLToImagesMap images_;
9104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
9204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // Generated from the redirects to and from the most visited pages. See
9304317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  // description above typedef for details.
9404317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  CanonicalURLs canonical_urls_;
9504317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
9604317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick  DISALLOW_COPY_AND_ASSIGN(TopSitesCache);
9704317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick};
9804317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
9904317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick}  // namespace history
10004317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick
10104317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick#endif  // CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_
10204317cc618aeae28910916469e074d8ce0fcaa03Andrew Trick