Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ | 5 #ifndef CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ |
| 6 #define CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ | 6 #define CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "chrome/browser/history/history_types.h" | 13 #include "chrome/browser/history/history_types.h" |
| 13 | 14 |
| 14 namespace history { | 15 namespace history { |
| 15 | 16 |
| 17 // The entries in CanonicalURLs, see CanonicalURLs for details. The second | |
| 18 // argument gives the index of the URL into MostVisitedURLs redirects. | |
| 19 typedef std::pair<MostVisitedURL*, size_t> CanonicalURLEntry; | |
| 20 | |
| 21 // Defines the "path-prefix" compare of two URLs | |
| 22 // Comparator used for CanonicalURLs. | |
| 23 class CanonicalURLComparator { | |
|
beaudoin
2013/09/04 16:46:10
Any reason to make that visible and right under hi
huangs
2013/09/04 23:27:39
Done; made this an inner class of TopSitesCache.
| |
| 24 public: | |
| 25 bool operator()(const CanonicalURLEntry& e1, | |
| 26 const CanonicalURLEntry& e2) const; | |
| 27 static bool CompareString(const std::string& s1, | |
| 28 const std::string& s2); | |
| 29 }; | |
| 30 | |
| 16 // TopSitesCache caches the top sites and thumbnails for TopSites. | 31 // TopSitesCache caches the top sites and thumbnails for TopSites. |
| 17 class TopSitesCache { | 32 class TopSitesCache { |
| 18 public: | 33 public: |
| 19 TopSitesCache(); | 34 TopSitesCache(); |
| 20 ~TopSitesCache(); | 35 ~TopSitesCache(); |
| 21 | 36 |
| 22 // The top sites. | 37 // The top sites. |
| 23 void SetTopSites(const MostVisitedURLList& top_sites); | 38 void SetTopSites(const MostVisitedURLList& top_sites); |
| 24 const MostVisitedURLList& top_sites() const { return top_sites_; } | 39 const MostVisitedURLList& top_sites() const { return top_sites_; } |
| 25 | 40 |
| 26 // The thumbnails. | 41 // The thumbnails. |
| 27 void SetThumbnails(const URLToImagesMap& images); | 42 void SetThumbnails(const URLToImagesMap& images); |
| 28 const URLToImagesMap& images() const { return images_; } | 43 const URLToImagesMap& images() const { return images_; } |
| 29 | 44 |
| 30 // Returns the thumbnail as an Image for the specified url. This adds an entry | 45 // Returns the thumbnail as an Image for the specified url. This adds an entry |
| 31 // for |url| if one has not yet been added. | 46 // for |url| if one has not yet been added. |
| 32 Images* GetImage(const GURL& url); | 47 Images* GetImage(const GURL& url); |
| 33 | 48 |
| 34 // Fetches the thumbnail for the specified url. Returns true if there is a | 49 // 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 | 50 // thumbnail for the specified url. It is possible for a URL to be in TopSites |
| 36 // but not have an thumbnail. | 51 // but not have an thumbnail. |
| 37 bool GetPageThumbnail(const GURL& url, | 52 bool GetPageThumbnail(const GURL& url, |
| 38 scoped_refptr<base::RefCountedMemory>* bytes); | 53 scoped_refptr<base::RefCountedMemory>* bytes); |
| 39 | 54 |
| 55 // Similar to GetPageThumbnail(), but rather than requiring an exact match, | |
| 56 // only requires the specified URL to be the prefix of a URL in TopSites. | |
| 57 // If multiple matches exist, returns the thumbnail for the first match in | |
| 58 // lexicographical order. | |
| 59 bool GetPageThumbnailForPrefix(const GURL& prefix_url, | |
| 60 scoped_refptr<base::RefCountedMemory>* bytes); | |
| 61 | |
| 40 // Fetches the thumbnail score for the specified url. Returns true if | 62 // Fetches the thumbnail score for the specified url. Returns true if |
| 41 // there is a thumbnail score for the specified url. | 63 // there is a thumbnail score for the specified url. |
| 42 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score); | 64 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score); |
| 43 | 65 |
| 44 // Returns the canonical URL for |url|. | 66 // Returns the canonical URL for |url|. |
| 45 const GURL& GetCanonicalURL(const GURL& url); | 67 const GURL& GetCanonicalURL(const GURL& url); |
| 46 | 68 |
| 69 // Returns the canonical URL for |url_prefix|. If |url_prefix| matches | |
| 70 // multiple entries then returns the canonical URL for the first entry in | |
| 71 // lexicographical. | |
|
beaudoin
2013/09/04 16:46:10
lexicographical order.
huangs
2013/09/04 23:27:39
Done.
| |
| 72 const GURL& GetCanonicalURLForPrefix(const GURL& url_prefix); | |
| 73 | |
| 47 // Returns true if |url| is known. | 74 // Returns true if |url| is known. |
| 48 bool IsKnownURL(const GURL& url); | 75 bool IsKnownURL(const GURL& url); |
| 49 | 76 |
| 50 // Returns the index into |top_sites_| for |url|. | 77 // Returns the index into |top_sites_| for |url|. |
| 51 size_t GetURLIndex(const GURL& url); | 78 size_t GetURLIndex(const GURL& url); |
| 52 | 79 |
| 80 // Returns whether or not |url1| is a prefix of |url2|. Criteria: | |
| 81 // - Scheme, host, port: exact match required. | |
| 82 // - Path: considered as a list of path components (e.g., ["a", "bb"] in | |
| 83 // "/a/bb"), and |url1|'s list must be a prefix of |url2|'s list. | |
| 84 // - Query: ignored. | |
| 85 // Note that "http://www.google.com/test" is NOT a prefix of | |
| 86 // "http://www.google.com/testing", although "test" is a prefix of "testing". | |
| 87 // Also, "http://www.google.com" will not match "http://www.google.com:80". | |
| 88 static bool UrlIsPrefix(const GURL& url1, const GURL& url2); | |
| 89 | |
| 53 private: | 90 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 | 91 // 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 | 92 // 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 | 93 // 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 | 94 // 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 | 95 // 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 | 96 // value. This way we aren't duplicating GURLs. CanonicalURLComparator |
| 73 // enforces the ordering as if we were using GURLs. | 97 // enforces the ordering as if we were using GURLs. |
| 74 typedef std::map<CanonicalURLEntry, size_t, | 98 typedef std::map<CanonicalURLEntry, size_t, |
| 75 CanonicalURLComparator> CanonicalURLs; | 99 CanonicalURLComparator> CanonicalURLs; |
| 76 | 100 |
| 77 // Generates the set of canonical urls from |top_sites_|. | 101 // Generates the set of canonical urls from |top_sites_|. |
| 78 void GenerateCanonicalURLs(); | 102 void GenerateCanonicalURLs(); |
| 79 | 103 |
| 80 // Stores a set of redirects. This is used by GenerateCanonicalURLs. | 104 // Stores a set of redirects. This is used by GenerateCanonicalURLs. |
| 81 void StoreRedirectChain(const RedirectList& redirects, size_t destination); | 105 void StoreRedirectChain(const RedirectList& redirects, size_t destination); |
| 82 | 106 |
| 83 // Returns the iterator into canconical_urls_ for the specified url. | 107 // Returns the iterator into canonical_urls_ for the |url|. If |match_prefix| |
| 84 CanonicalURLs::iterator GetCanonicalURLsIterator(const GURL& url); | 108 // is true, then returns the first iterator into canonical_urls_ that has |
| 109 // |url| as a path-prefix. | |
| 110 CanonicalURLs::iterator GetCanonicalURLsIterator(const GURL& url, | |
| 111 bool match_prefix); | |
| 85 | 112 |
| 86 // The top sites. | 113 // The top sites. |
| 87 MostVisitedURLList top_sites_; | 114 MostVisitedURLList top_sites_; |
| 88 | 115 |
| 89 // The images. These map from canonical url to image. | 116 // The images. These map from canonical url to image. |
| 90 URLToImagesMap images_; | 117 URLToImagesMap images_; |
| 91 | 118 |
| 92 // Generated from the redirects to and from the most visited pages. See | 119 // Generated from the redirects to and from the most visited pages. See |
| 93 // description above typedef for details. | 120 // description above typedef for details. |
| 94 CanonicalURLs canonical_urls_; | 121 CanonicalURLs canonical_urls_; |
| 95 | 122 |
| 96 DISALLOW_COPY_AND_ASSIGN(TopSitesCache); | 123 DISALLOW_COPY_AND_ASSIGN(TopSitesCache); |
| 97 }; | 124 }; |
| 98 | 125 |
| 99 } // namespace history | 126 } // namespace history |
| 100 | 127 |
| 101 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ | 128 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ |
| OLD | NEW |