OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 { |
11 | 11 |
12 TopSitesCache::TopSitesCache() { | 12 TopSitesCache::TopSitesCache() { |
13 } | 13 } |
14 | 14 |
15 TopSitesCache::~TopSitesCache() { | 15 TopSitesCache::~TopSitesCache() { |
16 } | 16 } |
17 | 17 |
18 void TopSitesCache::SetTopSites(const MostVisitedURLList& top_sites) { | 18 void TopSitesCache::SetTopSites(const MostVisitedURLList& top_sites) { |
19 top_sites_ = top_sites; | 19 top_sites_ = top_sites; |
20 GenerateCanonicalURLs(); | 20 GenerateCanonicalURLs(); |
21 } | 21 } |
22 | 22 |
23 void TopSitesCache::SetThumbnails(const URLToImagesMap& images) { | 23 void TopSitesCache::SetThumbnails(const URLToImagesMap& images) { |
24 images_ = images; | 24 images_ = images; |
25 } | 25 } |
26 | 26 |
27 void TopSitesCache::SetPageThumbnail(const GURL& url, | |
28 RefCountedBytes* thumbnail, | |
29 const ThumbnailScore& score) { | |
30 Images& img = images_[GetCanonicalURL(url)]; | |
31 img.thumbnail = thumbnail; | |
32 img.thumbnail_score = score; | |
33 } | |
34 | |
35 Images* TopSitesCache::GetImage(const GURL& url) { | 27 Images* TopSitesCache::GetImage(const GURL& url) { |
36 return &images_[GetCanonicalURL(url)]; | 28 return &images_[GetCanonicalURL(url)]; |
37 } | 29 } |
38 | 30 |
39 bool TopSitesCache::GetPageThumbnail(const GURL& url, | 31 bool TopSitesCache::GetPageThumbnail(const GURL& url, |
40 scoped_refptr<RefCountedBytes>* bytes) { | 32 scoped_refptr<RefCountedBytes>* bytes) { |
41 std::map<GURL, Images>::const_iterator found = | 33 std::map<GURL, Images>::const_iterator found = |
42 images_.find(GetCanonicalURL(url)); | 34 images_.find(GetCanonicalURL(url)); |
43 if (found != images_.end()) { | 35 if (found != images_.end()) { |
44 RefCountedBytes* data = found->second.thumbnail.get(); | 36 RefCountedBytes* data = found->second.thumbnail.get(); |
(...skipping 23 matching lines...) Expand all Loading... |
68 | 60 |
69 bool TopSitesCache::IsKnownURL(const GURL& url) { | 61 bool TopSitesCache::IsKnownURL(const GURL& url) { |
70 return GetCanonicalURLsIterator(url) != canonical_urls_.end(); | 62 return GetCanonicalURLsIterator(url) != canonical_urls_.end(); |
71 } | 63 } |
72 | 64 |
73 size_t TopSitesCache::GetURLIndex(const GURL& url) { | 65 size_t TopSitesCache::GetURLIndex(const GURL& url) { |
74 DCHECK(IsKnownURL(url)); | 66 DCHECK(IsKnownURL(url)); |
75 return GetCanonicalURLsIterator(url)->second; | 67 return GetCanonicalURLsIterator(url)->second; |
76 } | 68 } |
77 | 69 |
78 void TopSitesCache::RemoveUnreferencedThumbnails() { | |
79 for (URLToImagesMap::iterator i = images_.begin(); i != images_.end(); ) { | |
80 if (IsKnownURL(i->first)) { | |
81 ++i; | |
82 } else { | |
83 URLToImagesMap::iterator next_i = i; | |
84 ++next_i; | |
85 images_.erase(i); | |
86 i = next_i; | |
87 } | |
88 } | |
89 } | |
90 | |
91 void TopSitesCache::GenerateCanonicalURLs() { | 70 void TopSitesCache::GenerateCanonicalURLs() { |
92 canonical_urls_.clear(); | 71 canonical_urls_.clear(); |
93 for (size_t i = 0; i < top_sites_.size(); i++) | 72 for (size_t i = 0; i < top_sites_.size(); i++) |
94 StoreRedirectChain(top_sites_[i].redirects, i); | 73 StoreRedirectChain(top_sites_[i].redirects, i); |
95 } | 74 } |
96 | 75 |
97 void TopSitesCache::StoreRedirectChain(const RedirectList& redirects, | 76 void TopSitesCache::StoreRedirectChain(const RedirectList& redirects, |
98 size_t destination) { | 77 size_t destination) { |
99 // redirects is empty if the user pinned a site and there are not enough top | 78 // redirects is empty if the user pinned a site and there are not enough top |
100 // sites before the pinned site. | 79 // sites before the pinned site. |
(...skipping 14 matching lines...) Expand all Loading... |
115 const GURL& url) { | 94 const GURL& url) { |
116 MostVisitedURL most_visited_url; | 95 MostVisitedURL most_visited_url; |
117 most_visited_url.redirects.push_back(url); | 96 most_visited_url.redirects.push_back(url); |
118 CanonicalURLEntry entry; | 97 CanonicalURLEntry entry; |
119 entry.first = &most_visited_url; | 98 entry.first = &most_visited_url; |
120 entry.second = 0u; | 99 entry.second = 0u; |
121 return canonical_urls_.find(entry); | 100 return canonical_urls_.find(entry); |
122 } | 101 } |
123 | 102 |
124 } // namespace history | 103 } // namespace history |
OLD | NEW |