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 <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "chrome/browser/history/history_types.h" | 12 #include "chrome/browser/history/history_types.h" |
| 13 #include "chrome/browser/history/url_utils.h" |
13 | 14 |
14 namespace history { | 15 namespace history { |
15 | 16 |
| 17 // TopSiteCache caches thumbnails for visited pages. Retrieving thumbnails from |
| 18 // a given input URL is a two-stage process: |
| 19 // |
| 20 // input URL --(map 1)--> canonical URL --(map 2)--> image. |
| 21 // |
| 22 // (map 1) searches input URL in |canonical_urls_|. canonical URL is |
| 23 // assigned to the resulting value if found; else input URL. |
| 24 // |
| 25 // (map 2) simply looks up canonical URL in |images_|. |
| 26 // |
| 27 // TopSiteCache also provides GetCanonicalURLsIteratorForPrefix(), which is an |
| 28 // alternative implementation of (map 1) that does the following: |
| 29 // - if canonical URL is a key in |canonical_urls_|, return the value. |
| 30 // - else if canonical URL is a "URL prefix" (see comment in url_utils.h) of |
| 31 // some key in |canonical_urls_|, return the value corresponding to the key. |
| 32 // - else return input URL. |
| 33 |
16 // TopSitesCache caches the top sites and thumbnails for TopSites. | 34 // TopSitesCache caches the top sites and thumbnails for TopSites. |
17 class TopSitesCache { | 35 class TopSitesCache { |
18 public: | 36 public: |
19 TopSitesCache(); | 37 TopSitesCache(); |
20 ~TopSitesCache(); | 38 ~TopSitesCache(); |
21 | 39 |
22 // The top sites. | 40 // The top sites. |
23 void SetTopSites(const MostVisitedURLList& top_sites); | 41 void SetTopSites(const MostVisitedURLList& top_sites); |
24 const MostVisitedURLList& top_sites() const { return top_sites_; } | 42 const MostVisitedURLList& top_sites() const { return top_sites_; } |
25 | 43 |
(...skipping 11 matching lines...) Expand all Loading... |
37 bool GetPageThumbnail(const GURL& url, | 55 bool GetPageThumbnail(const GURL& url, |
38 scoped_refptr<base::RefCountedMemory>* bytes); | 56 scoped_refptr<base::RefCountedMemory>* bytes); |
39 | 57 |
40 // Fetches the thumbnail score for the specified url. Returns true if | 58 // Fetches the thumbnail score for the specified url. Returns true if |
41 // there is a thumbnail score for the specified url. | 59 // there is a thumbnail score for the specified url. |
42 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score); | 60 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score); |
43 | 61 |
44 // Returns the canonical URL for |url|. | 62 // Returns the canonical URL for |url|. |
45 const GURL& GetCanonicalURL(const GURL& url); | 63 const GURL& GetCanonicalURL(const GURL& url); |
46 | 64 |
| 65 // Returns the canonical URL for |url_prefix| that matches by prefix. |
| 66 // Multiple matches exst, returns the canonical URL for the first |
| 67 // matching entry under lexicographical order. |
| 68 const GURL& GetCanonicalURLForPrefix(const GURL& url_prefix); |
| 69 |
47 // Returns true if |url| is known. | 70 // Returns true if |url| is known. |
48 bool IsKnownURL(const GURL& url); | 71 bool IsKnownURL(const GURL& url); |
49 | 72 |
50 // Returns the index into |top_sites_| for |url|. | 73 // Returns the index into |top_sites_| for |url|. |
51 size_t GetURLIndex(const GURL& url); | 74 size_t GetURLIndex(const GURL& url); |
52 | 75 |
53 private: | 76 private: |
54 // The entries in CanonicalURLs, see CanonicalURLs for details. The second | 77 // The entries in CanonicalURLs, see CanonicalURLs for details. The second |
55 // argument gives the index of the URL into MostVisitedURLs redirects. | 78 // argument gives the index of the URL into MostVisitedURLs redirects. |
56 typedef std::pair<MostVisitedURL*, size_t> CanonicalURLEntry; | 79 typedef std::pair<MostVisitedURL*, size_t> CanonicalURLEntry; |
57 | 80 |
58 // Comparator used for CanonicalURLs. | 81 // Comparator used for CanonicalURLs. |
59 class CanonicalURLComparator { | 82 class CanonicalURLComparator { |
60 public: | 83 public: |
61 bool operator()(const CanonicalURLEntry& e1, | 84 bool operator()(const CanonicalURLEntry& e1, |
62 const CanonicalURLEntry& e2) const { | 85 const CanonicalURLEntry& e2) const { |
63 return e1.first->redirects[e1.second] < e2.first->redirects[e2.second]; | 86 return CanonicalURLStringCompare(e1.first->redirects[e1.second].spec(), |
| 87 e2.first->redirects[e2.second].spec()); |
64 } | 88 } |
65 }; | 89 }; |
66 | 90 |
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|. |
84 CanonicalURLs::iterator GetCanonicalURLsIterator(const GURL& url); | 108 CanonicalURLs::iterator GetCanonicalURLsIterator(const GURL& url); |
85 | 109 |
| 110 // Returns the first iterator into |canonical_urls_| for which |prefix_url| |
| 111 // is a URL prefix. Returns |canonical_urls_.end()| if no match is found. |
| 112 CanonicalURLs::iterator GetCanonicalURLsIteratorForPrefix( |
| 113 const GURL& prefix_url); |
| 114 |
86 // The top sites. | 115 // The top sites. |
87 MostVisitedURLList top_sites_; | 116 MostVisitedURLList top_sites_; |
88 | 117 |
89 // The images. These map from canonical url to image. | 118 // The images. These map from canonical url to image. |
90 URLToImagesMap images_; | 119 URLToImagesMap images_; |
91 | 120 |
92 // Generated from the redirects to and from the most visited pages. See | 121 // Generated from the redirects to and from the most visited pages. See |
93 // description above typedef for details. | 122 // description above typedef for details. |
94 CanonicalURLs canonical_urls_; | 123 CanonicalURLs canonical_urls_; |
95 | 124 |
96 DISALLOW_COPY_AND_ASSIGN(TopSitesCache); | 125 DISALLOW_COPY_AND_ASSIGN(TopSitesCache); |
97 }; | 126 }; |
98 | 127 |
99 } // namespace history | 128 } // namespace history |
100 | 129 |
101 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ | 130 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ |
OLD | NEW |