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 | 9 |
10 namespace history { | 10 namespace history { |
(...skipping 13 matching lines...) Expand all Loading... |
24 clear_path_query_ref_.ClearQuery(); | 24 clear_path_query_ref_.ClearQuery(); |
25 clear_path_query_ref_.ClearRef(); | 25 clear_path_query_ref_.ClearRef(); |
26 clear_path_query_ref_.ClearPath(); | 26 clear_path_query_ref_.ClearPath(); |
27 } | 27 } |
28 | 28 |
29 TopSitesCache::~TopSitesCache() { | 29 TopSitesCache::~TopSitesCache() { |
30 } | 30 } |
31 | 31 |
32 void TopSitesCache::SetTopSites(const MostVisitedURLList& top_sites) { | 32 void TopSitesCache::SetTopSites(const MostVisitedURLList& top_sites) { |
33 top_sites_ = top_sites; | 33 top_sites_ = top_sites; |
| 34 CountForcedURLs(); |
34 GenerateCanonicalURLs(); | 35 GenerateCanonicalURLs(); |
35 } | 36 } |
36 | 37 |
37 void TopSitesCache::SetThumbnails(const URLToImagesMap& images) { | 38 void TopSitesCache::SetThumbnails(const URLToImagesMap& images) { |
38 images_ = images; | 39 images_ = images; |
39 } | 40 } |
40 | 41 |
41 Images* TopSitesCache::GetImage(const GURL& url) { | 42 Images* TopSitesCache::GetImage(const GURL& url) { |
42 return &images_[GetCanonicalURL(url)]; | 43 return &images_[GetCanonicalURL(url)]; |
43 } | 44 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 113 |
113 bool TopSitesCache::IsKnownURL(const GURL& url) const { | 114 bool TopSitesCache::IsKnownURL(const GURL& url) const { |
114 return GetCanonicalURLsIterator(url) != canonical_urls_.end(); | 115 return GetCanonicalURLsIterator(url) != canonical_urls_.end(); |
115 } | 116 } |
116 | 117 |
117 size_t TopSitesCache::GetURLIndex(const GURL& url) const { | 118 size_t TopSitesCache::GetURLIndex(const GURL& url) const { |
118 DCHECK(IsKnownURL(url)); | 119 DCHECK(IsKnownURL(url)); |
119 return GetCanonicalURLsIterator(url)->second; | 120 return GetCanonicalURLsIterator(url)->second; |
120 } | 121 } |
121 | 122 |
| 123 size_t TopSitesCache::GetNumNonForcedURLs() const { |
| 124 return top_sites_.size() - num_forced_urls_; |
| 125 } |
| 126 |
| 127 size_t TopSitesCache::GetNumForcedURLs() const { |
| 128 return num_forced_urls_; |
| 129 } |
| 130 |
| 131 void TopSitesCache::CountForcedURLs() { |
| 132 num_forced_urls_ = 0; |
| 133 while (num_forced_urls_ < top_sites_.size()) { |
| 134 // Forced sites are all at the beginning. |
| 135 if (top_sites_[num_forced_urls_].last_forced_time.is_null()) |
| 136 break; |
| 137 num_forced_urls_++; |
| 138 } |
| 139 // In debug, ensure the cache user has no forced URLs pass that point. |
| 140 if (DCHECK_IS_ON()) { |
| 141 for (size_t i = num_forced_urls_; i < top_sites_.size(); ++i) { |
| 142 DCHECK(top_sites_[i].last_forced_time.is_null()) |
| 143 << "All the forced URLs must appear before non-forced URLs."; |
| 144 } |
| 145 } |
| 146 } |
| 147 |
122 void TopSitesCache::GenerateCanonicalURLs() { | 148 void TopSitesCache::GenerateCanonicalURLs() { |
123 canonical_urls_.clear(); | 149 canonical_urls_.clear(); |
124 for (size_t i = 0; i < top_sites_.size(); i++) | 150 for (size_t i = 0; i < top_sites_.size(); i++) |
125 StoreRedirectChain(top_sites_[i].redirects, i); | 151 StoreRedirectChain(top_sites_[i].redirects, i); |
126 } | 152 } |
127 | 153 |
128 void TopSitesCache::StoreRedirectChain(const RedirectList& redirects, | 154 void TopSitesCache::StoreRedirectChain(const RedirectList& redirects, |
129 size_t destination) { | 155 size_t destination) { |
130 // |redirects| is empty if the user pinned a site and there are not enough top | 156 // |redirects| is empty if the user pinned a site and there are not enough top |
131 // sites before the pinned site. | 157 // sites before the pinned site. |
(...skipping 15 matching lines...) Expand all Loading... |
147 return canonical_urls_.find(CanonicalURLQuery(url).entry()); | 173 return canonical_urls_.find(CanonicalURLQuery(url).entry()); |
148 } | 174 } |
149 | 175 |
150 const GURL& TopSitesCache::GetURLFromIterator( | 176 const GURL& TopSitesCache::GetURLFromIterator( |
151 CanonicalURLs::const_iterator it) const { | 177 CanonicalURLs::const_iterator it) const { |
152 DCHECK(it != canonical_urls_.end()); | 178 DCHECK(it != canonical_urls_.end()); |
153 return it->first.first->redirects[it->first.second]; | 179 return it->first.first->redirects[it->first.second]; |
154 } | 180 } |
155 | 181 |
156 } // namespace history | 182 } // namespace history |
OLD | NEW |