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 | |
34 // The entries in CanonicalURLs, see CanonicalURLs for details. The second | |
35 // argument gives the index of the URL into MostVisitedURLs redirects. | |
36 typedef std::pair<MostVisitedURL*, size_t> CanonicalURLEntry; | |
sky
2013/09/12 21:22:18
Why is this public?
huangs
2013/09/12 23:33:47
A relic from the previous change. Moved back into
| |
37 | |
16 // TopSitesCache caches the top sites and thumbnails for TopSites. | 38 // TopSitesCache caches the top sites and thumbnails for TopSites. |
17 class TopSitesCache { | 39 class TopSitesCache { |
18 public: | 40 public: |
19 TopSitesCache(); | 41 TopSitesCache(); |
20 ~TopSitesCache(); | 42 ~TopSitesCache(); |
21 | 43 |
22 // The top sites. | 44 // The top sites. |
23 void SetTopSites(const MostVisitedURLList& top_sites); | 45 void SetTopSites(const MostVisitedURLList& top_sites); |
24 const MostVisitedURLList& top_sites() const { return top_sites_; } | 46 const MostVisitedURLList& top_sites() const { return top_sites_; } |
25 | 47 |
26 // The thumbnails. | 48 // The thumbnails. |
27 void SetThumbnails(const URLToImagesMap& images); | 49 void SetThumbnails(const URLToImagesMap& images); |
28 const URLToImagesMap& images() const { return images_; } | 50 const URLToImagesMap& images() const { return images_; } |
29 | 51 |
30 // Returns the thumbnail as an Image for the specified url. This adds an entry | 52 // Returns the thumbnail as an Image for the specified url. This adds an entry |
31 // for |url| if one has not yet been added. | 53 // for |url| if one has not yet been added. |
32 Images* GetImage(const GURL& url); | 54 Images* GetImage(const GURL& url); |
33 | 55 |
34 // Fetches the thumbnail for the specified url. Returns true if there is a | 56 // 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 | 57 // thumbnail for the specified url. It is possible for a URL to be in TopSites |
36 // but not have an thumbnail. | 58 // but not have an thumbnail. |
37 bool GetPageThumbnail(const GURL& url, | 59 bool GetPageThumbnail(const GURL& url, |
38 scoped_refptr<base::RefCountedMemory>* bytes); | 60 scoped_refptr<base::RefCountedMemory>* bytes); |
39 | 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| that matches by prefix. | |
sky
2013/09/12 21:22:18
What's with [all the brackets] ?
huangs
2013/09/12 23:33:47
The purpose is to highlight proper terms -- but I
| |
70 // Multiple matches exst, returns the [canonical URL] for the first | |
71 // matching entry under lexicographical order. | |
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 |
53 private: | 80 private: |
54 // The entries in CanonicalURLs, see CanonicalURLs for details. The second | 81 // Comparator used for CanonicalURLs. Its main purpose |
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 { | 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]; | |
sky
2013/09/12 21:22:18
Why nuke the inlining here?
huangs
2013/09/12 23:33:47
Need to call CanonicalURLStringCompare(), and I wa
| |
64 } | |
65 }; | 86 }; |
66 | 87 |
67 // This is used to map from redirect url to the MostVisitedURL the redirect is | 88 // 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 | 89 // 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 | 90 // 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 | 91 // 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 | 92 // 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 | 93 // value. This way we aren't duplicating GURLs. CanonicalURLComparator |
73 // enforces the ordering as if we were using GURLs. | 94 // enforces the ordering as if we were using GURLs. |
74 typedef std::map<CanonicalURLEntry, size_t, | 95 typedef std::map<CanonicalURLEntry, size_t, |
75 CanonicalURLComparator> CanonicalURLs; | 96 CanonicalURLComparator> CanonicalURLs; |
76 | 97 |
77 // Generates the set of canonical urls from |top_sites_|. | 98 // Generates the set of canonical urls from |top_sites_|. |
78 void GenerateCanonicalURLs(); | 99 void GenerateCanonicalURLs(); |
79 | 100 |
80 // Stores a set of redirects. This is used by GenerateCanonicalURLs. | 101 // Stores a set of redirects. This is used by GenerateCanonicalURLs. |
81 void StoreRedirectChain(const RedirectList& redirects, size_t destination); | 102 void StoreRedirectChain(const RedirectList& redirects, size_t destination); |
82 | 103 |
83 // Returns the iterator into canconical_urls_ for the specified url. | 104 // Returns the iterator into canonical_urls_ for the |url|. |
sky
2013/09/12 21:22:18
Style here is || around references to parameters a
huangs
2013/09/12 23:33:47
Done.
| |
84 CanonicalURLs::iterator GetCanonicalURLsIterator(const GURL& url); | 105 CanonicalURLs::iterator GetCanonicalURLsIterator(const GURL& url); |
85 | 106 |
107 // Returns the first iterator into canonical_urls_ for whcih |previx_url| | |
sky
2013/09/12 21:22:18
|canonical_urls_|.
previx->prefix
huangs
2013/09/12 23:33:47
Done, also using the term "URL prefix" for consist
| |
108 // is a path-prefix. | |
109 CanonicalURLs::iterator GetCanonicalURLsIteratorForPrefix( | |
110 const GURL& prefix_url); | |
111 | |
86 // The top sites. | 112 // The top sites. |
87 MostVisitedURLList top_sites_; | 113 MostVisitedURLList top_sites_; |
88 | 114 |
89 // The images. These map from canonical url to image. | 115 // The images. These map from canonical url to image. |
90 URLToImagesMap images_; | 116 URLToImagesMap images_; |
91 | 117 |
92 // Generated from the redirects to and from the most visited pages. See | 118 // Generated from the redirects to and from the most visited pages. See |
93 // description above typedef for details. | 119 // description above typedef for details. |
94 CanonicalURLs canonical_urls_; | 120 CanonicalURLs canonical_urls_; |
95 | 121 |
96 DISALLOW_COPY_AND_ASSIGN(TopSitesCache); | 122 DISALLOW_COPY_AND_ASSIGN(TopSitesCache); |
97 }; | 123 }; |
98 | 124 |
99 } // namespace history | 125 } // namespace history |
100 | 126 |
101 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ | 127 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_ |
OLD | NEW |