15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_HISTORY_TOP_SITES_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_HISTORY_TOP_SITES_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/history/history_service.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/history/core/browser/history_types.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/history/core/browser/top_sites_observer.h"
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/history/core/common/thumbnail_score.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkColor.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/image/image.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class GURL;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Profile;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FilePath;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RefCountedBytes;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RefCountedMemory;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace history {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TopSitesCache;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Interface for TopSites, which stores the data for the top "most visited"
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// sites. This includes a cache of the most visited data from history, as well
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// as the corresponding thumbnails of those sites.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Some methods should only be called from the UI thread (see method
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// descriptions below). All others are assumed to be threadsafe.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TopSites
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : public base::RefCountedThreadSafe<TopSites>,
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      public content::NotificationObserver {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  TopSites();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes TopSites.
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static TopSites* Create(Profile* profile, const base::FilePath& db_name);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the given thumbnail for the given URL. Returns true if the thumbnail
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // was updated. False means either the URL wasn't known to us, or we felt
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // that our current thumbnail was superior to the given one. Should be called
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // from the UI thread.
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool SetPageThumbnail(const GURL& url,
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                const gfx::Image& thumbnail,
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                const ThumbnailScore& score) = 0;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // While testing the history system, we want to set the thumbnail to a piece
56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // of static memory.
57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  virtual bool SetPageThumbnailToJPEGBytes(
58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      const GURL& url,
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      const base::RefCountedMemory* memory,
60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      const ThumbnailScore& score) = 0;
61a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef base::Callback<void(const MostVisitedURLList&)>
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetMostVisitedURLsCallback;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns a list of most visited URLs via a callback, if
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |include_forced_urls| is false includes only non-forced URLs. This may be
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // invoked on any thread. NOTE: the callback is called immediately if we have
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // the data cached. If data is not available yet, callback will later be
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // posted to the thread called this function.
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void GetMostVisitedURLs(
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const GetMostVisitedURLsCallback& callback,
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bool include_forced_urls) = 0;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Gets a thumbnail for a given page. Returns true iff we have the thumbnail.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This may be invoked on any thread.
76d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // If an exact thumbnail URL match fails, |prefix_match| specifies whether or
77d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // not to try harder by matching the query thumbnail URL as URL prefix (as
78d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // defined by UrlIsPrefix()).
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // As this method may be invoked on any thread the ref count needs to be
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // incremented before this method returns, so this takes a scoped_refptr*.
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool GetPageThumbnail(
82d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      const GURL& url,
83d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      bool prefix_match,
84d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      scoped_refptr<base::RefCountedMemory>* bytes) = 0;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get a thumbnail score for a given page. Returns true iff we have the
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thumbnail score.  This may be invoked on any thread. The score will
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be copied to |score|.
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool GetPageThumbnailScore(const GURL& url,
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                     ThumbnailScore* score) = 0;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get a temporary thumbnail score for a given page. Returns true iff we
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // have the thumbnail score. Useful when checking if we should update a
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thumbnail for a given page. The score will be copied to |score|.
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool GetTemporaryPageThumbnailScore(const GURL& url,
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                              ThumbnailScore* score) = 0;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Asks TopSites to refresh what it thinks the top sites are. This may do
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // nothing. Should be called from the UI thread.
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void SyncWithHistory() = 0;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Blacklisted URLs
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if there is at least one item in the blacklist.
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool HasBlacklistedItems() const = 0;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Add a URL to the blacklist. Should be called from the UI thread.
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void AddBlacklistedURL(const GURL& url) = 0;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Removes a URL from the blacklist. Should be called from the UI thread.
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void RemoveBlacklistedURL(const GURL& url) = 0;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns true if the URL is blacklisted. Should be called from the UI
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // thread.
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool IsBlacklisted(const GURL& url) = 0;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Clear the blacklist. Should be called from the UI thread.
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void ClearBlacklistedURLs() = 0;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Shuts down top sites.
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void Shutdown() = 0;
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Query history service for the list of available thumbnails. Returns the
1245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // task id for the request, or |base::CancelableTaskTracker::kBadTaskId| if a
1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // request could not be made. Public only for testing purposes.
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual base::CancelableTaskTracker::TaskId StartQueryForMostVisited() = 0;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the given URL is known to the top sites service.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function also returns false if TopSites isn't loaded yet.
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool IsKnownURL(const GURL& url) = 0;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Follows the cached redirect chain to convert any URL to its
1337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // canonical version. If no redirect chain is known for the URL,
1347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // return it without modification.
1357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual const std::string& GetCanonicalURLString(const GURL& url) const = 0;
1367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns true if the top sites list of non-forced URLs is full (i.e. we
138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // already have the maximum number of non-forced top sites).  This function
139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // also returns false if TopSites isn't loaded yet.
140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual bool IsNonForcedFull() = 0;
141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns true if the top sites list of forced URLs is full (i.e. we already
143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // have the maximum number of forced top sites).  This function also returns
144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // false if TopSites isn't loaded yet.
145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual bool IsForcedFull() = 0;
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool loaded() const = 0;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the set of prepopulate pages.
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual MostVisitedURLList GetPrepopulatePages() = 0;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Adds or updates a |url| for which we should force the capture of a
153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // thumbnail next time it's visited. If there is already a non-forced URL
154f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // matching this |url| this call has no effect. Indicate this URL was laste
155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // forced at |time| so we can evict the older URLs when needed. Should be
156f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // called from the UI thread.
157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual bool AddForcedURL(const GURL& url, const base::Time& time) = 0;
158f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct PrepopulatedPage {
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The string resource for the url.
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int url_id;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The string resource for the page title.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int title_id;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The raw data resource for the favicon.
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int favicon_id;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The raw data resource for the thumbnail.
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int thumbnail_id;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The best color to highlight the page (should roughly match favicon).
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SkColor color;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Add Observer to the list.
1731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void AddObserver(TopSitesObserver* observer);
1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Remove Observer from the list.
1761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void RemoveObserver(TopSitesObserver* observer);
1771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void NotifyTopSitesLoaded();
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void NotifyTopSitesChanged();
1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual ~TopSites();
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ObserverList<TopSitesObserver> observer_list_;
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class base::RefCountedThreadSafe<TopSites>;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_ANDROID)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const TopSites::PrepopulatedPage kPrepopulatedPages[1];
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const TopSites::PrepopulatedPage kPrepopulatedPages[2];
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace history
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_HISTORY_TOP_SITES_H_
197