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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_HISTORY_TOP_SITES_H_
6 #define CHROME_BROWSER_HISTORY_TOP_SITES_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "chrome/browser/history/history_service.h"
13 #include "components/history/core/browser/history_types.h"
14 #include "components/history/core/browser/top_sites_observer.h"
15 #include "components/history/core/common/thumbnail_score.h"
16 #include "third_party/skia/include/core/SkColor.h"
17 #include "ui/gfx/image/image.h"
24 class RefCountedBytes;
25 class RefCountedMemory;
32 // Interface for TopSites, which stores the data for the top "most visited"
33 // sites. This includes a cache of the most visited data from history, as well
34 // as the corresponding thumbnails of those sites.
36 // Some methods should only be called from the UI thread (see method
37 // descriptions below). All others are assumed to be threadsafe.
39 : public base::RefCountedThreadSafe<TopSites>,
40 public content::NotificationObserver {
44 // Initializes TopSites.
45 static TopSites* Create(Profile* profile, const base::FilePath& db_name);
47 // Sets the given thumbnail for the given URL. Returns true if the thumbnail
48 // was updated. False means either the URL wasn't known to us, or we felt
49 // that our current thumbnail was superior to the given one. Should be called
50 // from the UI thread.
51 virtual bool SetPageThumbnail(const GURL& url,
52 const gfx::Image& thumbnail,
53 const ThumbnailScore& score) = 0;
55 // While testing the history system, we want to set the thumbnail to a piece
57 virtual bool SetPageThumbnailToJPEGBytes(
59 const base::RefCountedMemory* memory,
60 const ThumbnailScore& score) = 0;
62 typedef base::Callback<void(const MostVisitedURLList&)>
63 GetMostVisitedURLsCallback;
65 // Returns a list of most visited URLs via a callback, if
66 // |include_forced_urls| is false includes only non-forced URLs. This may be
67 // invoked on any thread. NOTE: the callback is called immediately if we have
68 // the data cached. If data is not available yet, callback will later be
69 // posted to the thread called this function.
70 virtual void GetMostVisitedURLs(
71 const GetMostVisitedURLsCallback& callback,
72 bool include_forced_urls) = 0;
74 // Gets a thumbnail for a given page. Returns true iff we have the thumbnail.
75 // This may be invoked on any thread.
76 // If an exact thumbnail URL match fails, |prefix_match| specifies whether or
77 // not to try harder by matching the query thumbnail URL as URL prefix (as
78 // defined by UrlIsPrefix()).
79 // As this method may be invoked on any thread the ref count needs to be
80 // incremented before this method returns, so this takes a scoped_refptr*.
81 virtual bool GetPageThumbnail(
84 scoped_refptr<base::RefCountedMemory>* bytes) = 0;
86 // Get a thumbnail score for a given page. Returns true iff we have the
87 // thumbnail score. This may be invoked on any thread. The score will
88 // be copied to |score|.
89 virtual bool GetPageThumbnailScore(const GURL& url,
90 ThumbnailScore* score) = 0;
92 // Get a temporary thumbnail score for a given page. Returns true iff we
93 // have the thumbnail score. Useful when checking if we should update a
94 // thumbnail for a given page. The score will be copied to |score|.
95 virtual bool GetTemporaryPageThumbnailScore(const GURL& url,
96 ThumbnailScore* score) = 0;
98 // Asks TopSites to refresh what it thinks the top sites are. This may do
99 // nothing. Should be called from the UI thread.
100 virtual void SyncWithHistory() = 0;
104 // Returns true if there is at least one item in the blacklist.
105 virtual bool HasBlacklistedItems() const = 0;
107 // Add a URL to the blacklist. Should be called from the UI thread.
108 virtual void AddBlacklistedURL(const GURL& url) = 0;
110 // Removes a URL from the blacklist. Should be called from the UI thread.
111 virtual void RemoveBlacklistedURL(const GURL& url) = 0;
113 // Returns true if the URL is blacklisted. Should be called from the UI
115 virtual bool IsBlacklisted(const GURL& url) = 0;
117 // Clear the blacklist. Should be called from the UI thread.
118 virtual void ClearBlacklistedURLs() = 0;
120 // Shuts down top sites.
121 virtual void Shutdown() = 0;
123 // Query history service for the list of available thumbnails. Returns the
124 // task id for the request, or |base::CancelableTaskTracker::kBadTaskId| if a
125 // request could not be made. Public only for testing purposes.
126 virtual base::CancelableTaskTracker::TaskId StartQueryForMostVisited() = 0;
128 // Returns true if the given URL is known to the top sites service.
129 // This function also returns false if TopSites isn't loaded yet.
130 virtual bool IsKnownURL(const GURL& url) = 0;
132 // Follows the cached redirect chain to convert any URL to its
133 // canonical version. If no redirect chain is known for the URL,
134 // return it without modification.
135 virtual const std::string& GetCanonicalURLString(const GURL& url) const = 0;
137 // Returns true if the top sites list of non-forced URLs is full (i.e. we
138 // already have the maximum number of non-forced top sites). This function
139 // also returns false if TopSites isn't loaded yet.
140 virtual bool IsNonForcedFull() = 0;
142 // Returns true if the top sites list of forced URLs is full (i.e. we already
143 // have the maximum number of forced top sites). This function also returns
144 // false if TopSites isn't loaded yet.
145 virtual bool IsForcedFull() = 0;
147 virtual bool loaded() const = 0;
149 // Returns the set of prepopulate pages.
150 virtual MostVisitedURLList GetPrepopulatePages() = 0;
152 // Adds or updates a |url| for which we should force the capture of a
153 // thumbnail next time it's visited. If there is already a non-forced URL
154 // matching this |url| this call has no effect. Indicate this URL was laste
155 // forced at |time| so we can evict the older URLs when needed. Should be
156 // called from the UI thread.
157 virtual bool AddForcedURL(const GURL& url, const base::Time& time) = 0;
159 struct PrepopulatedPage {
160 // The string resource for the url.
162 // The string resource for the page title.
164 // The raw data resource for the favicon.
166 // The raw data resource for the thumbnail.
168 // The best color to highlight the page (should roughly match favicon).
172 // Add Observer to the list.
173 void AddObserver(TopSitesObserver* observer);
175 // Remove Observer from the list.
176 void RemoveObserver(TopSitesObserver* observer);
179 void NotifyTopSitesLoaded();
180 void NotifyTopSitesChanged();
184 ObserverList<TopSitesObserver> observer_list_;
185 friend class base::RefCountedThreadSafe<TopSites>;
188 #if defined(OS_ANDROID)
189 extern const TopSites::PrepopulatedPage kPrepopulatedPages[1];
191 extern const TopSites::PrepopulatedPage kPrepopulatedPages[2];
194 } // namespace history
196 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_H_