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 "chrome/common/thumbnail_score.h" 16 #include "third_party/skia/include/core/SkColor.h" 17 #include "ui/gfx/image/image.h" 18 #include "url/gurl.h" 19 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 typedef std::vector<GetMostVisitedURLsCallback> PendingCallbacks; 65 66 // Returns a list of most visited URLs via a callback. This may be invoked on 67 // any thread. 68 // NOTE: the callback is called immediately if we have the data cached. If 69 // data is not available yet, callback will later be posted to the thread 70 // called this function. 71 virtual void GetMostVisitedURLs( 72 const GetMostVisitedURLsCallback& callback) = 0; 73 74 // Get a thumbnail for a given page. Returns true iff we have the thumbnail. 75 // This may be invoked on any thread. 76 // As this method may be invoked on any thread the ref count needs to be 77 // incremented before this method returns, so this takes a scoped_refptr*. 78 virtual bool GetPageThumbnail( 79 const GURL& url, scoped_refptr<base::RefCountedMemory>* bytes) = 0; 80 81 // Get a thumbnail score for a given page. Returns true iff we have the 82 // thumbnail score. This may be invoked on any thread. The score will 83 // be copied to |score|. 84 virtual bool GetPageThumbnailScore(const GURL& url, 85 ThumbnailScore* score) = 0; 86 87 // Get a temporary thumbnail score for a given page. Returns true iff we 88 // have the thumbnail score. Useful when checking if we should update a 89 // thumbnail for a given page. The score will be copied to |score|. 90 virtual bool GetTemporaryPageThumbnailScore(const GURL& url, 91 ThumbnailScore* score) = 0; 92 93 // Invoked from History if migration is needed. If this is invoked it will 94 // be before HistoryLoaded is invoked. Should be called from the UI thread. 95 virtual void MigrateFromHistory() = 0; 96 97 // Invoked with data from migrating thumbnails out of history. Should be 98 // called from the UI thread. 99 virtual void FinishHistoryMigration(const ThumbnailMigration& data) = 0; 100 101 // Invoked from history when it finishes loading. If MigrateFromHistory was 102 // not invoked at this point then we load from the top sites service. Should 103 // be called from the UI thread. 104 virtual void HistoryLoaded() = 0; 105 106 // Asks TopSites to refresh what it thinks the top sites are. This may do 107 // nothing. Should be called from the UI thread. 108 virtual void SyncWithHistory() = 0; 109 110 // Blacklisted URLs 111 112 // Returns true if there is at least one item in the blacklist. 113 virtual bool HasBlacklistedItems() const = 0; 114 115 // Add a URL to the blacklist. Should be called from the UI thread. 116 virtual void AddBlacklistedURL(const GURL& url) = 0; 117 118 // Removes a URL from the blacklist. Should be called from the UI thread. 119 virtual void RemoveBlacklistedURL(const GURL& url) = 0; 120 121 // Returns true if the URL is blacklisted. Should be called from the UI 122 // thread. 123 virtual bool IsBlacklisted(const GURL& url) = 0; 124 125 // Clear the blacklist. Should be called from the UI thread. 126 virtual void ClearBlacklistedURLs() = 0; 127 128 // Shuts down top sites. 129 virtual void Shutdown() = 0; 130 131 // Query history service for the list of available thumbnails. Returns the 132 // handle for the request, or NULL if a request could not be made. 133 // Public only for testing purposes. 134 virtual CancelableRequestProvider::Handle StartQueryForMostVisited() = 0; 135 136 // Returns true if the given URL is known to the top sites service. 137 // This function also returns false if TopSites isn't loaded yet. 138 virtual bool IsKnownURL(const GURL& url) = 0; 139 140 // Follows the cached redirect chain to convert any URL to its 141 // canonical version. If no redirect chain is known for the URL, 142 // return it without modification. 143 virtual const std::string& GetCanonicalURLString(const GURL& url) const = 0; 144 145 // Returns true if the top sites list is full (i.e. we already have the 146 // maximum number of top sites). This function also returns false if 147 // TopSites isn't loaded yet. 148 virtual bool IsFull() = 0; 149 150 virtual bool loaded() const = 0; 151 152 // Returns the set of prepopulate pages. 153 virtual MostVisitedURLList GetPrepopulatePages() = 0; 154 155 struct PrepopulatedPage { 156 // The string resource for the url. 157 int url_id; 158 // The string resource for the page title. 159 int title_id; 160 // The raw data resource for the favicon. 161 int favicon_id; 162 // The raw data resource for the thumbnail. 163 int thumbnail_id; 164 // The best color to highlight the page (should roughly match favicon). 165 SkColor color; 166 }; 167 protected: 168 virtual ~TopSites() {} 169 170 private: 171 friend class base::RefCountedThreadSafe<TopSites>; 172 }; 173 174 #if defined(OS_ANDROID) 175 extern const TopSites::PrepopulatedPage kPrepopulatedPages[1]; 176 #else 177 extern const TopSites::PrepopulatedPage kPrepopulatedPages[2]; 178 #endif 179 180 } // namespace history 181 182 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_H_ 183