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 #include "chrome/browser/history/top_sites_cache.h" | 5 #include "chrome/browser/history/top_sites_cache.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
| 9 #include "url/gurl.h" |
9 | 10 |
10 namespace history { | 11 namespace history { |
11 | 12 |
12 TopSitesCache::TopSitesCache() { | 13 TopSitesCache::TopSitesCache() { |
13 } | 14 } |
14 | 15 |
15 TopSitesCache::~TopSitesCache() { | 16 TopSitesCache::~TopSitesCache() { |
16 } | 17 } |
17 | 18 |
18 void TopSitesCache::SetTopSites(const MostVisitedURLList& top_sites) { | 19 void TopSitesCache::SetTopSites(const MostVisitedURLList& top_sites) { |
19 top_sites_ = top_sites; | 20 top_sites_ = top_sites; |
20 GenerateCanonicalURLs(); | 21 GenerateCanonicalURLs(); |
21 } | 22 } |
22 | 23 |
23 void TopSitesCache::SetThumbnails(const URLToImagesMap& images) { | 24 void TopSitesCache::SetThumbnails(const URLToImagesMap& images) { |
24 images_ = images; | 25 images_ = images; |
25 } | 26 } |
26 | 27 |
27 Images* TopSitesCache::GetImage(const GURL& url) { | 28 Images* TopSitesCache::GetImage(const GURL& url) { |
28 return &images_[GetCanonicalURL(url)]; | 29 return &images_[GetCanonicalURL(url)]; |
29 } | 30 } |
30 | 31 |
31 bool TopSitesCache::GetPageThumbnail( | 32 bool TopSitesCache::GetPageThumbnail( |
32 const GURL& url, | 33 const GURL& url, |
33 scoped_refptr<base::RefCountedMemory>* bytes) { | 34 scoped_refptr<base::RefCountedMemory>* bytes) const { |
34 std::map<GURL, Images>::const_iterator found = | 35 std::map<GURL, Images>::const_iterator found = |
35 images_.find(GetCanonicalURL(url)); | 36 images_.find(GetCanonicalURL(url)); |
36 if (found != images_.end()) { | 37 if (found != images_.end()) { |
37 base::RefCountedMemory* data = found->second.thumbnail.get(); | 38 base::RefCountedMemory* data = found->second.thumbnail.get(); |
38 if (data) { | 39 if (data) { |
39 *bytes = data; | 40 *bytes = data; |
40 return true; | 41 return true; |
41 } | 42 } |
42 } | 43 } |
43 return false; | 44 return false; |
44 } | 45 } |
45 | 46 |
46 bool TopSitesCache::GetPageThumbnailScore(const GURL& url, | 47 bool TopSitesCache::GetPageThumbnailScore(const GURL& url, |
47 ThumbnailScore* score) { | 48 ThumbnailScore* score) const { |
48 std::map<GURL, Images>::const_iterator found = | 49 std::map<GURL, Images>::const_iterator found = |
49 images_.find(GetCanonicalURL(url)); | 50 images_.find(GetCanonicalURL(url)); |
50 if (found != images_.end()) { | 51 if (found != images_.end()) { |
51 *score = found->second.thumbnail_score; | 52 *score = found->second.thumbnail_score; |
52 return true; | 53 return true; |
53 } | 54 } |
54 return false; | 55 return false; |
55 } | 56 } |
56 | 57 |
57 const GURL& TopSitesCache::GetCanonicalURL(const GURL& url) { | 58 const GURL& TopSitesCache::GetCanonicalURL(const GURL& url) const { |
58 CanonicalURLs::iterator i = GetCanonicalURLsIterator(url); | 59 CanonicalURLs::const_iterator it = GetCanonicalURLsIterator(url); |
59 return i == canonical_urls_.end() ? url : i->first.first->url; | 60 return it == canonical_urls_.end() ? url : it->first.first->url; |
60 } | 61 } |
61 | 62 |
62 const GURL& TopSitesCache::GetCanonicalURLForPrefix(const GURL& url) { | 63 const GURL& TopSitesCache::GetCanonicalURLForPrefix(const GURL& url) const { |
63 CanonicalURLs::iterator i = GetCanonicalURLsIteratorForPrefix(url); | 64 CanonicalURLs::const_iterator it = GetCanonicalURLsIteratorForPrefix(url); |
64 return i == canonical_urls_.end() ? url : i->first.first->url; | 65 return it == canonical_urls_.end() ? url : it->first.first->url; |
65 } | 66 } |
66 | 67 |
67 bool TopSitesCache::IsKnownURL(const GURL& url) { | 68 bool TopSitesCache::IsKnownURL(const GURL& url) const { |
68 return GetCanonicalURLsIterator(url) != canonical_urls_.end(); | 69 return GetCanonicalURLsIterator(url) != canonical_urls_.end(); |
69 } | 70 } |
70 | 71 |
71 size_t TopSitesCache::GetURLIndex(const GURL& url) { | 72 size_t TopSitesCache::GetURLIndex(const GURL& url) const { |
72 DCHECK(IsKnownURL(url)); | 73 DCHECK(IsKnownURL(url)); |
73 return GetCanonicalURLsIterator(url)->second; | 74 return GetCanonicalURLsIterator(url)->second; |
74 } | 75 } |
75 | 76 |
76 void TopSitesCache::GenerateCanonicalURLs() { | 77 void TopSitesCache::GenerateCanonicalURLs() { |
77 canonical_urls_.clear(); | 78 canonical_urls_.clear(); |
78 for (size_t i = 0; i < top_sites_.size(); i++) | 79 for (size_t i = 0; i < top_sites_.size(); i++) |
79 StoreRedirectChain(top_sites_[i].redirects, i); | 80 StoreRedirectChain(top_sites_[i].redirects, i); |
80 } | 81 } |
81 | 82 |
82 void TopSitesCache::StoreRedirectChain(const RedirectList& redirects, | 83 void TopSitesCache::StoreRedirectChain(const RedirectList& redirects, |
83 size_t destination) { | 84 size_t destination) { |
84 // |redirects| is empty if the user pinned a site and there are not enough top | 85 // |redirects| is empty if the user pinned a site and there are not enough top |
85 // sites before the pinned site. | 86 // sites before the pinned site. |
86 | 87 |
87 // Map all the redirected URLs to the destination. | 88 // Map all the redirected URLs to the destination. |
88 for (size_t i = 0; i < redirects.size(); i++) { | 89 for (size_t i = 0; i < redirects.size(); i++) { |
89 // If this redirect is already known, don't replace it with a new one. | 90 // If this redirect is already known, don't replace it with a new one. |
90 if (!IsKnownURL(redirects[i])) { | 91 if (!IsKnownURL(redirects[i])) { |
91 CanonicalURLEntry entry; | 92 CanonicalURLEntry entry; |
92 entry.first = &(top_sites_[destination]); | 93 entry.first = &(top_sites_[destination]); |
93 entry.second = i; | 94 entry.second = i; |
94 canonical_urls_[entry] = destination; | 95 canonical_urls_[entry] = destination; |
95 } | 96 } |
96 } | 97 } |
97 } | 98 } |
98 | 99 |
99 TopSitesCache::CanonicalURLs::iterator TopSitesCache::GetCanonicalURLsIterator( | 100 TopSitesCache::CanonicalURLs::const_iterator |
100 const GURL& url) { | 101 TopSitesCache::GetCanonicalURLsIterator(const GURL& url) const { |
101 MostVisitedURL most_visited_url; | 102 MostVisitedURL most_visited_url; |
102 most_visited_url.redirects.push_back(url); | 103 most_visited_url.redirects.push_back(url); |
103 CanonicalURLEntry entry; | 104 CanonicalURLEntry entry; |
104 entry.first = &most_visited_url; | 105 entry.first = &most_visited_url; |
105 entry.second = 0u; | 106 entry.second = 0u; |
106 return canonical_urls_.find(entry); | 107 return canonical_urls_.find(entry); |
107 } | 108 } |
108 | 109 |
109 TopSitesCache::CanonicalURLs::iterator | 110 TopSitesCache::CanonicalURLs::const_iterator |
110 TopSitesCache::GetCanonicalURLsIteratorForPrefix(const GURL& prefix_url) { | 111 TopSitesCache::GetCanonicalURLsIteratorForPrefix(const GURL& prefix_url) |
| 112 const { |
111 MostVisitedURL most_visited_url; | 113 MostVisitedURL most_visited_url; |
112 most_visited_url.redirects.push_back(prefix_url); | 114 most_visited_url.redirects.push_back(prefix_url); |
113 CanonicalURLEntry entry; | 115 CanonicalURLEntry entry; |
114 entry.first = &most_visited_url; | 116 entry.first = &most_visited_url; |
115 entry.second = 0u; | 117 entry.second = 0u; |
116 | 118 |
117 // Perform effective binary search for URL prefix search. | 119 // Perform effective binary search for URL prefix search. |
118 TopSitesCache::CanonicalURLs::iterator it = | 120 TopSitesCache::CanonicalURLs::const_iterator it = |
119 canonical_urls_.lower_bound(entry); | 121 canonical_urls_.lower_bound(entry); |
120 // Perform prefix match. | 122 // Perform prefix match. |
121 if (it != canonical_urls_.end()) { | 123 if (it != canonical_urls_.end()) { |
122 const GURL& comp_url = it->first.first->redirects[it->first.second]; | 124 const GURL& comp_url = it->first.first->redirects[it->first.second]; |
123 if (!UrlIsPrefix(prefix_url, comp_url)) | 125 if (!UrlIsPrefix(prefix_url, comp_url)) |
124 it = canonical_urls_.end(); | 126 it = canonical_urls_.end(); |
125 } | 127 } |
126 return it; | 128 return it; |
127 } | 129 } |
128 | 130 |
129 } // namespace history | 131 } // namespace history |
OLD | NEW |