| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/ref_counted_memory.h" | 8 #include "base/ref_counted_memory.h" |
| 9 | 9 |
| 10 namespace history { | 10 namespace history { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 std::map<GURL, Images>::const_iterator found = | 41 std::map<GURL, Images>::const_iterator found = |
| 42 images_.find(GetCanonicalURL(url)); | 42 images_.find(GetCanonicalURL(url)); |
| 43 if (found != images_.end()) { | 43 if (found != images_.end()) { |
| 44 *bytes = found->second.thumbnail.get(); | 44 *bytes = found->second.thumbnail.get(); |
| 45 return true; | 45 return true; |
| 46 } | 46 } |
| 47 return false; | 47 return false; |
| 48 } | 48 } |
| 49 | 49 |
| 50 GURL TopSitesCache::GetCanonicalURL(const GURL& url) { | 50 GURL TopSitesCache::GetCanonicalURL(const GURL& url) { |
| 51 std::map<GURL, size_t>::const_iterator i = canonical_urls_.find(url); | 51 CanonicalURLs::iterator i = TopSitesCache::GetCanonicalURLsIterator(url); |
| 52 return i == canonical_urls_.end() ? url : top_sites_[i->second].url; | 52 return i == canonical_urls_.end() ? url : i->first.first->url; |
| 53 } | 53 } |
| 54 | 54 |
| 55 bool TopSitesCache::IsKnownURL(const GURL& url) { | 55 bool TopSitesCache::IsKnownURL(const GURL& url) { |
| 56 return canonical_urls_.find(url) != canonical_urls_.end(); | 56 return GetCanonicalURLsIterator(url) != canonical_urls_.end(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 size_t TopSitesCache::GetURLIndex(const GURL& url) { | 59 size_t TopSitesCache::GetURLIndex(const GURL& url) { |
| 60 DCHECK(IsKnownURL(url)); | 60 DCHECK(IsKnownURL(url)); |
| 61 return canonical_urls_[url]; | 61 return GetCanonicalURLsIterator(url)->second; |
| 62 } | 62 } |
| 63 | 63 |
| 64 void TopSitesCache::RemoveUnreferencedThumbnails() { | 64 void TopSitesCache::RemoveUnreferencedThumbnails() { |
| 65 for (URLToImagesMap::iterator i = images_.begin(); i != images_.end(); ) { | 65 for (URLToImagesMap::iterator i = images_.begin(); i != images_.end(); ) { |
| 66 if (IsKnownURL(i->first)) { | 66 if (IsKnownURL(i->first)) { |
| 67 ++i; | 67 ++i; |
| 68 } else { | 68 } else { |
| 69 URLToImagesMap::iterator next_i = i; | 69 URLToImagesMap::iterator next_i = i; |
| 70 ++next_i; | 70 ++next_i; |
| 71 images_.erase(i); | 71 images_.erase(i); |
| 72 i = next_i; | 72 i = next_i; |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 void TopSitesCache::GenerateCanonicalURLs() { | 77 void TopSitesCache::GenerateCanonicalURLs() { |
| 78 canonical_urls_.clear(); | 78 canonical_urls_.clear(); |
| 79 for (size_t i = 0; i < top_sites_.size(); i++) | 79 for (size_t i = 0; i < top_sites_.size(); i++) |
| 80 StoreRedirectChain(top_sites_[i].redirects, i); | 80 StoreRedirectChain(top_sites_[i].redirects, i); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void TopSitesCache::StoreRedirectChain(const RedirectList& redirects, | 83 void TopSitesCache::StoreRedirectChain(const RedirectList& redirects, |
| 84 size_t destination) { | 84 size_t destination) { |
| 85 // 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 |
| 86 // sites before the pinned site. | 86 // sites before the pinned site. |
| 87 | 87 |
| 88 // Map all the redirected URLs to the destination. | 88 // Map all the redirected URLs to the destination. |
| 89 for (size_t i = 0; i < redirects.size(); i++) { | 89 for (size_t i = 0; i < redirects.size(); i++) { |
| 90 // 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. |
| 91 if (canonical_urls_.find(redirects[i]) == canonical_urls_.end()) | 91 if (!IsKnownURL(redirects[i])) { |
| 92 canonical_urls_[redirects[i]] = destination; | 92 CanonicalURLEntry entry; |
| 93 entry.first = &(top_sites_[destination]); |
| 94 entry.second = i; |
| 95 canonical_urls_[entry] = destination; |
| 96 } |
| 93 } | 97 } |
| 94 } | 98 } |
| 95 | 99 |
| 100 TopSitesCache::CanonicalURLs::iterator TopSitesCache::GetCanonicalURLsIterator( |
| 101 const GURL& url) { |
| 102 MostVisitedURL most_visited_url; |
| 103 most_visited_url.redirects.push_back(url); |
| 104 CanonicalURLEntry entry; |
| 105 entry.first = &most_visited_url; |
| 106 entry.second = 0u; |
| 107 return canonical_urls_.find(entry); |
| 108 } |
| 109 |
| 96 } // namespace history | 110 } // namespace history |
| OLD | NEW |