Home | History | Annotate | Download | only in history
      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/common/cancelable_request.h"
     13 #include "chrome/browser/history/history_service.h"
     14 #include "chrome/browser/history/history_types.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"
     18 
     19 class GURL;
     20 class Profile;
     21 
     22 namespace base {
     23 class FilePath;
     24 class RefCountedBytes;
     25 class RefCountedMemory;
     26 }
     27 
     28 namespace history {
     29 
     30 class TopSitesCache;
     31 
     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.
     35 //
     36 // Some methods should only be called from the UI thread (see method
     37 // descriptions below). All others are assumed to be threadsafe.
     38 class TopSites
     39     : public base::RefCountedThreadSafe<TopSites>,
     40       public content::NotificationObserver {
     41  public:
     42   TopSites() {}
     43 
     44   // Initializes TopSites.
     45   static TopSites* Create(Profile* profile, const base::FilePath& db_name);
     46 
     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;
     54 
     55   // While testing the history system, we want to set the thumbnail to a piece
     56   // of static memory.
     57   virtual bool SetPageThumbnailToJPEGBytes(
     58       const GURL& url,
     59       const base::RefCountedMemory* memory,
     60       const ThumbnailScore& score) = 0;
     61 
     62   typedef base::Callback<void(const MostVisitedURLList&)>
     63       GetMostVisitedURLsCallback;
     64 
     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;
     73 
     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(
     82       const GURL& url,
     83       bool prefix_match,
     84       scoped_refptr<base::RefCountedMemory>* bytes) = 0;
     85 
     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;
     91 
     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;
     97 
     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;
    101 
    102   // Blacklisted URLs
    103 
    104   // Returns true if there is at least one item in the blacklist.
    105   virtual bool HasBlacklistedItems() const = 0;
    106 
    107   // Add a URL to the blacklist. Should be called from the UI thread.
    108   virtual void AddBlacklistedURL(const GURL& url) = 0;
    109 
    110   // Removes a URL from the blacklist. Should be called from the UI thread.
    111   virtual void RemoveBlacklistedURL(const GURL& url) = 0;
    112 
    113   // Returns true if the URL is blacklisted. Should be called from the UI
    114   // thread.
    115   virtual bool IsBlacklisted(const GURL& url) = 0;
    116 
    117   // Clear the blacklist. Should be called from the UI thread.
    118   virtual void ClearBlacklistedURLs() = 0;
    119 
    120   // Shuts down top sites.
    121   virtual void Shutdown() = 0;
    122 
    123   // Query history service for the list of available thumbnails. Returns the
    124   // handle for the request, or NULL if a request could not be made.
    125   // Public only for testing purposes.
    126   virtual CancelableRequestProvider::Handle StartQueryForMostVisited() = 0;
    127 
    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;
    131 
    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;
    136 
    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;
    141 
    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;
    146 
    147   virtual bool loaded() const = 0;
    148 
    149   // Returns the set of prepopulate pages.
    150   virtual MostVisitedURLList GetPrepopulatePages() = 0;
    151 
    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;
    158 
    159   struct PrepopulatedPage {
    160     // The string resource for the url.
    161     int url_id;
    162     // The string resource for the page title.
    163     int title_id;
    164     // The raw data resource for the favicon.
    165     int favicon_id;
    166     // The raw data resource for the thumbnail.
    167     int thumbnail_id;
    168     // The best color to highlight the page (should roughly match favicon).
    169     SkColor color;
    170   };
    171  protected:
    172   virtual ~TopSites() {}
    173 
    174  private:
    175   friend class base::RefCountedThreadSafe<TopSites>;
    176 };
    177 
    178 #if defined(OS_ANDROID)
    179 extern const TopSites::PrepopulatedPage kPrepopulatedPages[1];
    180 #else
    181 extern const TopSites::PrepopulatedPage kPrepopulatedPages[2];
    182 #endif
    183 
    184 }  // namespace history
    185 
    186 #endif  // CHROME_BROWSER_HISTORY_TOP_SITES_H_
    187