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
|
// 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.
#ifndef CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_
#define CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_
#pragma once
#include <map>
#include <utility>
#include "base/memory/ref_counted.h"
#include "chrome/browser/history/history_types.h"
namespace history {
// TopSitesCache caches the top sites and thumbnails for TopSites.
class TopSitesCache {
public:
TopSitesCache();
~TopSitesCache();
// The top sites.
void SetTopSites(const MostVisitedURLList& top_sites);
const MostVisitedURLList& top_sites() const { return top_sites_; }
// The thumbnails.
void SetThumbnails(const URLToImagesMap& images);
const URLToImagesMap& images() const { return images_; }
// Returns the thumbnail as an Image for the specified url. This adds an entry
// for |url| if one has not yet been added.
Images* GetImage(const GURL& url);
// Fetches the thumbnail for the specified url. Returns true if there is a
// thumbnail for the specified url. It is possible for a URL to be in TopSites
// but not have an thumbnail.
bool GetPageThumbnail(const GURL& url,
scoped_refptr<base::RefCountedMemory>* bytes);
// Fetches the thumbnail score for the specified url. Returns true if
// there is a thumbnail score for the specified url.
bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score);
// Returns the canonical URL for |url|.
GURL GetCanonicalURL(const GURL& url);
// Returns true if |url| is known.
bool IsKnownURL(const GURL& url);
// Returns the index into |top_sites_| for |url|.
size_t GetURLIndex(const GURL& url);
private:
// The entries in CanonicalURLs, see CanonicalURLs for details. The second
// argument gives the index of the URL into MostVisitedURLs redirects.
typedef std::pair<MostVisitedURL*, size_t> CanonicalURLEntry;
// Comparator used for CanonicalURLs.
class CanonicalURLComparator {
public:
bool operator()(const CanonicalURLEntry& e1,
const CanonicalURLEntry& e2) const {
return e1.first->redirects[e1.second] < e2.first->redirects[e2.second];
}
};
// This is used to map from redirect url to the MostVisitedURL the redirect is
// from. Ideally this would be map<GURL, size_t> (second param indexing into
// top_sites_), but this results in duplicating all redirect urls. As some
// sites have a lot of redirects, we instead use the MostVisitedURL* and the
// index of the redirect as the key, and the index into top_sites_ as the
// value. This way we aren't duplicating GURLs. CanonicalURLComparator
// enforces the ordering as if we were using GURLs.
typedef std::map<CanonicalURLEntry, size_t,
CanonicalURLComparator> CanonicalURLs;
// Generates the set of canonical urls from |top_sites_|.
void GenerateCanonicalURLs();
// Stores a set of redirects. This is used by GenerateCanonicalURLs.
void StoreRedirectChain(const RedirectList& redirects, size_t destination);
// Returns the iterator into canconical_urls_ for the specified url.
CanonicalURLs::iterator GetCanonicalURLsIterator(const GURL& url);
// The top sites.
MostVisitedURLList top_sites_;
// The images. These map from canonical url to image.
URLToImagesMap images_;
// Generated from the redirects to and from the most visited pages. See
// description above typedef for details.
CanonicalURLs canonical_urls_;
DISALLOW_COPY_AND_ASSIGN(TopSitesCache);
};
} // namespace history
#endif // CHROME_BROWSER_HISTORY_TOP_SITES_CACHE_H_
|