1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/history/top_sites_cache.h"
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
namespace history {
TopSitesCache::CanonicalURLQuery::CanonicalURLQuery(const GURL& url) {
most_visited_url_.redirects.push_back(url);
entry_.first = &most_visited_url_;
entry_.second = 0u;
}
TopSitesCache::CanonicalURLQuery::~CanonicalURLQuery() {
}
TopSitesCache::TopSitesCache() {
clear_query_ref_.ClearQuery();
clear_query_ref_.ClearRef();
clear_path_query_ref_.ClearQuery();
clear_path_query_ref_.ClearRef();
clear_path_query_ref_.ClearPath();
}
TopSitesCache::~TopSitesCache() {
}
void TopSitesCache::SetTopSites(const MostVisitedURLList& top_sites) {
top_sites_ = top_sites;
CountForcedURLs();
GenerateCanonicalURLs();
}
void TopSitesCache::SetThumbnails(const URLToImagesMap& images) {
images_ = images;
}
Images* TopSitesCache::GetImage(const GURL& url) {
return &images_[GetCanonicalURL(url)];
}
bool TopSitesCache::GetPageThumbnail(
const GURL& url,
scoped_refptr<base::RefCountedMemory>* bytes) const {
std::map<GURL, Images>::const_iterator found =
images_.find(GetCanonicalURL(url));
if (found != images_.end()) {
base::RefCountedMemory* data = found->second.thumbnail.get();
if (data) {
*bytes = data;
return true;
}
}
return false;
}
bool TopSitesCache::GetPageThumbnailScore(const GURL& url,
ThumbnailScore* score) const {
std::map<GURL, Images>::const_iterator found =
images_.find(GetCanonicalURL(url));
if (found != images_.end()) {
*score = found->second.thumbnail_score;
return true;
}
return false;
}
const GURL& TopSitesCache::GetCanonicalURL(const GURL& url) const {
CanonicalURLs::const_iterator it = GetCanonicalURLsIterator(url);
return it == canonical_urls_.end() ? url : it->first.first->url;
}
GURL TopSitesCache::GetGeneralizedCanonicalURL(const GURL& url) const {
CanonicalURLs::const_iterator it_hi =
canonical_urls_.lower_bound(CanonicalURLQuery(url).entry());
if (it_hi != canonical_urls_.end()) {
// Test match ignoring "?query#ref". This also handles exact match.
if (url.ReplaceComponents(clear_query_ref_) ==
GetURLFromIterator(it_hi).ReplaceComponents(clear_query_ref_)) {
return it_hi->first.first->url;
}
}
// Everything on or after |it_hi| is irrelevant.
GURL base_url(url.ReplaceComponents(clear_path_query_ref_));
CanonicalURLs::const_iterator it_lo =
canonical_urls_.lower_bound(CanonicalURLQuery(base_url).entry());
if (it_lo == canonical_urls_.end())
return GURL::EmptyGURL();
GURL compare_url_lo(GetURLFromIterator(it_lo));
if (!HaveSameSchemeHostAndPort(base_url, compare_url_lo) ||
!IsPathPrefix(base_url.path(), compare_url_lo.path())) {
return GURL::EmptyGURL();
}
// Everything before |it_lo| is irrelevant.
// Search in [|it_lo|, |it_hi|) in reversed order. The first URL found that's
// a prefix of |url| (ignoring "?query#ref") would be returned.
for (CanonicalURLs::const_iterator it = it_hi; it != it_lo;) {
--it;
GURL compare_url(GetURLFromIterator(it));
DCHECK(HaveSameSchemeHostAndPort(compare_url, url));
if (IsPathPrefix(compare_url.path(), url.path()))
return it->first.first->url;
}
return GURL::EmptyGURL();
}
bool TopSitesCache::IsKnownURL(const GURL& url) const {
return GetCanonicalURLsIterator(url) != canonical_urls_.end();
}
size_t TopSitesCache::GetURLIndex(const GURL& url) const {
DCHECK(IsKnownURL(url));
return GetCanonicalURLsIterator(url)->second;
}
size_t TopSitesCache::GetNumNonForcedURLs() const {
return top_sites_.size() - num_forced_urls_;
}
size_t TopSitesCache::GetNumForcedURLs() const {
return num_forced_urls_;
}
void TopSitesCache::CountForcedURLs() {
num_forced_urls_ = 0;
while (num_forced_urls_ < top_sites_.size()) {
// Forced sites are all at the beginning.
if (top_sites_[num_forced_urls_].last_forced_time.is_null())
break;
num_forced_urls_++;
}
#if DCHECK_IS_ON
// In debug, ensure the cache user has no forced URLs pass that point.
for (size_t i = num_forced_urls_; i < top_sites_.size(); ++i) {
DCHECK(top_sites_[i].last_forced_time.is_null())
<< "All the forced URLs must appear before non-forced URLs.";
}
#endif
}
void TopSitesCache::GenerateCanonicalURLs() {
canonical_urls_.clear();
for (size_t i = 0; i < top_sites_.size(); i++)
StoreRedirectChain(top_sites_[i].redirects, i);
}
void TopSitesCache::StoreRedirectChain(const RedirectList& redirects,
size_t destination) {
// |redirects| is empty if the user pinned a site and there are not enough top
// sites before the pinned site.
// Map all the redirected URLs to the destination.
for (size_t i = 0; i < redirects.size(); i++) {
// If this redirect is already known, don't replace it with a new one.
if (!IsKnownURL(redirects[i])) {
CanonicalURLEntry entry;
entry.first = &(top_sites_[destination]);
entry.second = i;
canonical_urls_[entry] = destination;
}
}
}
TopSitesCache::CanonicalURLs::const_iterator
TopSitesCache::GetCanonicalURLsIterator(const GURL& url) const {
return canonical_urls_.find(CanonicalURLQuery(url).entry());
}
const GURL& TopSitesCache::GetURLFromIterator(
CanonicalURLs::const_iterator it) const {
DCHECK(it != canonical_urls_.end());
return it->first.first->redirects[it->first.second];
}
} // namespace history
|