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