| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_HISTORY_TOP_SITES_H_ | 5 #ifndef CHROME_BROWSER_HISTORY_TOP_SITES_H_ |
| 6 #define CHROME_BROWSER_HISTORY_TOP_SITES_H_ | 6 #define CHROME_BROWSER_HISTORY_TOP_SITES_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/lock.h" | 14 #include "base/lock.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "base/ref_counted.h" | 16 #include "base/ref_counted.h" |
| 17 #include "base/ref_counted_memory.h" | 17 #include "base/ref_counted_memory.h" |
| 18 #include "chrome/browser/cancelable_request.h" | 18 #include "chrome/browser/cancelable_request.h" |
| 19 #include "chrome/browser/history/history_types.h" | 19 #include "chrome/browser/history/history_types.h" |
| 20 #include "chrome/browser/history/history.h" | 20 #include "chrome/browser/history/history.h" |
| 21 #include "chrome/browser/history/page_usage_data.h" | 21 #include "chrome/browser/history/page_usage_data.h" |
| 22 #include "chrome/common/notification_service.h" |
| 22 #include "chrome/common/thumbnail_score.h" | 23 #include "chrome/common/thumbnail_score.h" |
| 23 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
| 24 | 25 |
| 25 class SkBitmap; | 26 class SkBitmap; |
| 26 class Profile; | 27 class Profile; |
| 27 | 28 |
| 28 namespace history { | 29 namespace history { |
| 29 | 30 |
| 30 class TopSitesBackend; | 31 class TopSitesBackend; |
| 31 class TopSitesDatabase; | 32 class TopSitesDatabase; |
| 32 class TopSitesTest; | 33 class TopSitesTest; |
| 33 | 34 |
| 34 typedef std::vector<MostVisitedURL> MostVisitedURLList; | 35 typedef std::vector<MostVisitedURL> MostVisitedURLList; |
| 35 | 36 |
| 36 // Stores the data for the top "most visited" sites. This includes a cache of | 37 // Stores the data for the top "most visited" sites. This includes a cache of |
| 37 // the most visited data from history, as well as the corresponding thumbnails | 38 // the most visited data from history, as well as the corresponding thumbnails |
| 38 // of those sites. | 39 // of those sites. |
| 39 // | 40 // |
| 40 // This class IS threadsafe. It is designed to be used from the UI thread of | 41 // This class IS threadsafe. It is designed to be used from the UI thread of |
| 41 // the browser (where history requests must be kicked off and received from) | 42 // the browser (where history requests must be kicked off and received from) |
| 42 // and from the I/O thread (where new tab page requests come in). Handling the | 43 // and from the I/O thread (where new tab page requests come in). Handling the |
| 43 // new tab page requests on the I/O thread without proxying to the UI thread is | 44 // new tab page requests on the I/O thread without proxying to the UI thread is |
| 44 // a nontrivial performance win, especially when the browser is starting and | 45 // a nontrivial performance win, especially when the browser is starting and |
| 45 // the UI thread is busy. | 46 // the UI thread is busy. |
| 46 class TopSites : public base::RefCountedThreadSafe<TopSites> { | 47 class TopSites : public NotificationObserver, |
| 48 public base::RefCountedThreadSafe<TopSites> { |
| 47 public: | 49 public: |
| 48 explicit TopSites(Profile* profile); | 50 explicit TopSites(Profile* profile); |
| 49 | 51 |
| 50 class MockHistoryService { | 52 class MockHistoryService { |
| 51 // A mockup of a HistoryService used for testing TopSites. | 53 // A mockup of a HistoryService used for testing TopSites. |
| 52 public: | 54 public: |
| 53 virtual HistoryService::Handle QueryMostVisitedURLs( | 55 virtual HistoryService::Handle QueryMostVisitedURLs( |
| 54 int result_count, int days_back, | 56 int result_count, int days_back, |
| 55 CancelableRequestConsumerBase* consumer, | 57 CancelableRequestConsumerBase* consumer, |
| 56 HistoryService::QueryMostVisitedURLsCallback* callback) = 0; | 58 HistoryService::QueryMostVisitedURLsCallback* callback) = 0; |
| 57 virtual ~MockHistoryService() {} | 59 virtual ~MockHistoryService() {} |
| 58 }; | 60 }; |
| 59 | 61 |
| 60 struct Images { | 62 struct Images { |
| 61 scoped_refptr<RefCountedBytes> thumbnail; | 63 scoped_refptr<RefCountedBytes> thumbnail; |
| 62 ThumbnailScore thumbnail_score; | 64 ThumbnailScore thumbnail_score; |
| 63 | 65 |
| 64 // TODO(brettw): this will eventually store the favicon. | 66 // TODO(brettw): this will eventually store the favicon. |
| 65 // scoped_refptr<RefCountedBytes> favicon; | 67 // scoped_refptr<RefCountedBytes> favicon; |
| 66 }; | 68 }; |
| 67 | 69 |
| 68 // Initializes TopSites. | 70 // Initializes TopSites. |
| 69 void Init(); | 71 void Init(const FilePath& db_name); |
| 70 | 72 |
| 71 // Sets the given thumbnail for the given URL. Returns true if the thumbnail | 73 // Sets the given thumbnail for the given URL. Returns true if the thumbnail |
| 72 // was updated. False means either the URL wasn't known to us, or we felt | 74 // was updated. False means either the URL wasn't known to us, or we felt |
| 73 // that our current thumbnail was superior to the given one. | 75 // that our current thumbnail was superior to the given one. |
| 74 bool SetPageThumbnail(const GURL& url, | 76 bool SetPageThumbnail(const GURL& url, |
| 75 const SkBitmap& thumbnail, | 77 const SkBitmap& thumbnail, |
| 76 const ThumbnailScore& score); | 78 const ThumbnailScore& score); |
| 77 | 79 |
| 78 // Returns a list of most visited URLs or an empty list if it's not | 80 // Returns a list of most visited URLs or an empty list if it's not |
| 79 // cached yet. | 81 // cached yet. |
| 80 MostVisitedURLList GetMostVisitedURLs(); | 82 MostVisitedURLList GetMostVisitedURLs(); |
| 81 | 83 |
| 82 // Get a thumbnail for a given page. Returns true iff we have the thumbnail. | 84 // Get a thumbnail for a given page. Returns true iff we have the thumbnail. |
| 83 bool GetPageThumbnail(const GURL& url, RefCountedBytes** data) const; | 85 bool GetPageThumbnail(const GURL& url, RefCountedBytes** data) const; |
| 84 | 86 |
| 85 private: | 87 private: |
| 86 friend class base::RefCountedThreadSafe<TopSites>; | 88 friend class base::RefCountedThreadSafe<TopSites>; |
| 87 friend class TopSitesTest; | 89 friend class TopSitesTest; |
| 88 friend class TopSitesTest_GetMostVisited_Test; | 90 friend class TopSitesTest_GetMostVisited_Test; |
| 89 friend class TopSitesTest_RealDatabase_Test; | 91 friend class TopSitesTest_RealDatabase_Test; |
| 90 friend class TopSitesTest_MockDatabase_Test; | 92 friend class TopSitesTest_MockDatabase_Test; |
| 93 friend class TopSitesTest_DeleteNotifications_Test; |
| 94 friend class TopSitesTest_GetUpdateDelay_Test; |
| 91 | 95 |
| 92 ~TopSites(); | 96 ~TopSites(); |
| 93 | 97 |
| 94 // Sets the thumbnail without writing to the database. Useful when | 98 // Sets the thumbnail without writing to the database. Useful when |
| 95 // reading last known top sites from the DB. | 99 // reading last known top sites from the DB. |
| 96 // Returns true if the thumbnail was set, false if the existing one is better. | 100 // Returns true if the thumbnail was set, false if the existing one is better. |
| 97 bool SetPageThumbnailNoDB(const GURL& url, | 101 bool SetPageThumbnailNoDB(const GURL& url, |
| 98 const RefCountedBytes* thumbnail_data, | 102 const RefCountedBytes* thumbnail_data, |
| 99 const ThumbnailScore& score); | 103 const ThumbnailScore& score); |
| 100 | 104 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 std::vector<size_t>* deleted_urls, | 147 std::vector<size_t>* deleted_urls, |
| 144 std::vector<size_t>* moved_urls); | 148 std::vector<size_t>* moved_urls); |
| 145 | 149 |
| 146 // Reads the database from disk. Called on startup to get the last | 150 // Reads the database from disk. Called on startup to get the last |
| 147 // known top sites. | 151 // known top sites. |
| 148 void ReadDatabase(); | 152 void ReadDatabase(); |
| 149 | 153 |
| 150 // For testing with a HistoryService mock. | 154 // For testing with a HistoryService mock. |
| 151 void SetMockHistoryService(MockHistoryService* mhs); | 155 void SetMockHistoryService(MockHistoryService* mhs); |
| 152 | 156 |
| 157 // Implementation of NotificationObserver. |
| 158 virtual void Observe(NotificationType type, |
| 159 const NotificationSource& source, |
| 160 const NotificationDetails& details); |
| 161 |
| 162 // Returns the delay until the next update of history is needed. |
| 163 // Uses num_urls_changed |
| 164 base::TimeDelta GetUpdateDelay(); |
| 165 |
| 153 Profile* profile_; | 166 Profile* profile_; |
| 154 // A mockup to use for testing. If NULL, use the real HistoryService | 167 // A mockup to use for testing. If NULL, use the real HistoryService |
| 155 // from the profile_. See SetMockHistoryService. | 168 // from the profile_. See SetMockHistoryService. |
| 156 MockHistoryService* mock_history_service_; | 169 MockHistoryService* mock_history_service_; |
| 157 CancelableRequestConsumerTSimple<PageUsageData*> cancelable_consumer_; | 170 CancelableRequestConsumerTSimple<PageUsageData*> cancelable_consumer_; |
| 158 mutable Lock lock_; | 171 mutable Lock lock_; |
| 159 | 172 |
| 160 // The cached version of the top sites. The 0th item in this vector is the | 173 // The cached version of the top sites. The 0th item in this vector is the |
| 161 // #1 site. | 174 // #1 site. |
| 162 MostVisitedURLList top_sites_; | 175 MostVisitedURLList top_sites_; |
| 163 | 176 |
| 164 // The images corresponding to the top_sites. This is indexed by the URL of | 177 // The images corresponding to the top_sites. This is indexed by the URL of |
| 165 // the top site, so this doesn't have to be shuffled around when the ordering | 178 // the top site, so this doesn't have to be shuffled around when the ordering |
| 166 // changes of the top sites. Some top_sites_ entries may not have images. | 179 // changes of the top sites. Some top_sites_ entries may not have images. |
| 167 std::map<GURL, Images> top_images_; | 180 std::map<GURL, Images> top_images_; |
| 168 | 181 |
| 169 // Generated from the redirects to and from the most visited pages, this | 182 // Generated from the redirects to and from the most visited pages, this |
| 170 // maps the redirects to the index into top_sites_ that contains it. | 183 // maps the redirects to the index into top_sites_ that contains it. |
| 171 std::map<GURL, size_t> canonical_urls_; | 184 std::map<GURL, size_t> canonical_urls_; |
| 172 | 185 |
| 173 // Timer for updating TopSites data. | 186 // Timer for updating TopSites data. |
| 174 base::OneShotTimer<TopSites> timer_; | 187 base::OneShotTimer<TopSites> timer_; |
| 175 | 188 |
| 176 scoped_ptr<TopSitesDatabase> db_; | 189 scoped_ptr<TopSitesDatabase> db_; |
| 190 FilePath db_path_; |
| 191 |
| 192 NotificationRegistrar registrar_; |
| 193 |
| 194 // The number of URLs changed on the last update. |
| 195 size_t last_num_urls_changed_; |
| 177 | 196 |
| 178 // TODO(brettw): use the blacklist. | 197 // TODO(brettw): use the blacklist. |
| 179 // std::set<GURL> blacklist_; | 198 // std::set<GURL> blacklist_; |
| 180 | 199 |
| 181 DISALLOW_COPY_AND_ASSIGN(TopSites); | 200 DISALLOW_COPY_AND_ASSIGN(TopSites); |
| 182 }; | 201 }; |
| 183 | 202 |
| 184 } // namespace history | 203 } // namespace history |
| 185 | 204 |
| 186 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_H_ | 205 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_H_ |
| OLD | NEW |