| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 COMPONENTS_HISTORY_CORE_BROWSER_TOP_SITES_IMPL_H_ | 5 #ifndef COMPONENTS_HISTORY_CORE_BROWSER_TOP_SITES_IMPL_H_ |
| 6 #define COMPONENTS_HISTORY_CORE_BROWSER_TOP_SITES_IMPL_H_ | 6 #define COMPONENTS_HISTORY_CORE_BROWSER_TOP_SITES_IMPL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/scoped_observer.h" | 20 #include "base/scoped_observer.h" |
| 21 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 22 #include "base/task/cancelable_task_tracker.h" | 22 #include "base/task/cancelable_task_tracker.h" |
| 23 #include "base/threading/thread_checker.h" | 23 #include "base/threading/thread_checker.h" |
| 24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "base/timer/timer.h" | 25 #include "base/timer/timer.h" |
| 26 #include "components/history/core/browser/history_service.h" | |
| 27 #include "components/history/core/browser/history_service_observer.h" | 26 #include "components/history/core/browser/history_service_observer.h" |
| 28 #include "components/history/core/browser/history_types.h" | 27 #include "components/history/core/browser/history_types.h" |
| 29 #include "components/history/core/browser/page_usage_data.h" | |
| 30 #include "components/history/core/browser/top_sites.h" | 28 #include "components/history/core/browser/top_sites.h" |
| 31 #include "components/history/core/browser/top_sites_backend.h" | 29 #include "components/history/core/browser/top_sites_backend.h" |
| 32 #include "components/history/core/common/thumbnail_score.h" | 30 #include "components/history/core/common/thumbnail_score.h" |
| 33 #include "third_party/skia/include/core/SkColor.h" | 31 #include "third_party/skia/include/core/SkColor.h" |
| 34 #include "ui/gfx/image/image.h" | 32 #include "ui/gfx/image/image.h" |
| 35 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 36 | 34 |
| 37 class PrefRegistrySimple; | 35 class PrefRegistrySimple; |
| 38 class PrefService; | 36 class PrefService; |
| 39 | 37 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // The URLs that are in "new" but not "old" will be have their index into | 139 // The URLs that are in "new" but not "old" will be have their index into |
| 142 // "new" put in |added_urls|. The non-forced URLs that are in "old" but not | 140 // "new" put in |added_urls|. The non-forced URLs that are in "old" but not |
| 143 // "new" will have their index into "old" put into |deleted_urls|. | 141 // "new" will have their index into "old" put into |deleted_urls|. |
| 144 // | 142 // |
| 145 // URLs appearing in both old and new lists but having different indices will | 143 // URLs appearing in both old and new lists but having different indices will |
| 146 // have their index into "new" be put into |moved_urls|. | 144 // have their index into "new" be put into |moved_urls|. |
| 147 static void DiffMostVisited(const MostVisitedURLList& old_list, | 145 static void DiffMostVisited(const MostVisitedURLList& old_list, |
| 148 const MostVisitedURLList& new_list, | 146 const MostVisitedURLList& new_list, |
| 149 TopSitesDelta* delta); | 147 TopSitesDelta* delta); |
| 150 | 148 |
| 151 // Sets the thumbnail without writing to the database. Useful when | 149 // Sets the thumbnail without writing to the database. |
| 152 // reading last known top sites from the DB. | |
| 153 // Returns true if the thumbnail was set, false if the existing one is better. | 150 // Returns true if the thumbnail was set, false if the existing one is better. |
| 154 bool SetPageThumbnailNoDB(const GURL& url, | 151 bool SetPageThumbnailNoDB(const GURL& url, |
| 155 const base::RefCountedMemory* thumbnail_data, | 152 const base::RefCountedMemory* thumbnail_data, |
| 156 const ThumbnailScore& score); | 153 const ThumbnailScore& score); |
| 157 | 154 |
| 158 // A version of SetPageThumbnail that takes RefCountedBytes as | 155 // A version of SetPageThumbnail that takes RefCountedBytes as |
| 159 // returned by HistoryService. | 156 // returned by HistoryService. |
| 160 bool SetPageThumbnailEncoded(const GURL& url, | 157 bool SetPageThumbnailEncoded(const GURL& url, |
| 161 const base::RefCountedMemory* thumbnail, | 158 const base::RefCountedMemory* thumbnail, |
| 162 const ThumbnailScore& score); | 159 const ThumbnailScore& score); |
| 163 | 160 |
| 164 // Encodes the bitmap to bytes for storage to the db. Returns true if the | 161 // Encodes the bitmap to bytes for storage to the db. Returns true if the |
| 165 // bitmap was successfully encoded. | 162 // bitmap was successfully encoded. |
| 166 static bool EncodeBitmap(const gfx::Image& bitmap, | 163 static bool EncodeBitmap(const gfx::Image& bitmap, |
| 167 scoped_refptr<base::RefCountedBytes>* bytes); | 164 scoped_refptr<base::RefCountedBytes>* bytes); |
| 168 | 165 |
| 169 // Removes the cached thumbnail for url. Does nothing if |url| if not cached | 166 // Removes the cached thumbnail for |url|. Does nothing if |url| is not cached |
| 170 // in |temp_images_|. | 167 // in |temp_images_|. |
| 171 void RemoveTemporaryThumbnailByURL(const GURL& url); | 168 void RemoveTemporaryThumbnailByURL(const GURL& url); |
| 172 | 169 |
| 173 // Add a thumbnail for an unknown url. See temp_thumbnails_map_. | 170 // Add a thumbnail for an unknown url. See |temp_images_|. |
| 174 void AddTemporaryThumbnail(const GURL& url, | 171 void AddTemporaryThumbnail(const GURL& url, |
| 175 const base::RefCountedMemory* thumbnail, | 172 base::RefCountedMemory* thumbnail, |
| 176 const ThumbnailScore& score); | 173 const ThumbnailScore& score); |
| 177 | 174 |
| 178 // Called by our timer. Starts the query for the most visited sites. | 175 // Called by our timer. Starts the query for the most visited sites. |
| 179 void TimerFired(); | 176 void TimerFired(); |
| 180 | 177 |
| 181 // Finds the given URL in the redirect chain for the given TopSite, and | 178 // Finds the given URL in the redirect chain for the given TopSite, and |
| 182 // returns the distance from the destination in hops that the given URL is. | 179 // returns the distance from the destination in hops that the given URL is. |
| 183 // The URL is assumed to be in the list. The destination is 0. | 180 // The URL is assumed to be in the list. The destination is 0. |
| 184 static int GetRedirectDistanceForURL(const MostVisitedURL& most_visited, | 181 static int GetRedirectDistanceForURL(const MostVisitedURL& most_visited, |
| 185 const GURL& url); | 182 const GURL& url); |
| 186 | 183 |
| 187 // Add prepopulated pages: 'welcome to Chrome' and themes gallery to |urls|. | 184 // Add prepopulated pages: 'welcome to Chrome' and themes gallery to |urls|. |
| 188 // Returns true if any pages were added. | 185 // Returns true if any pages were added. |
| 189 bool AddPrepopulatedPages(MostVisitedURLList* urls, | 186 bool AddPrepopulatedPages(MostVisitedURLList* urls, |
| 190 size_t num_forced_urls); | 187 size_t num_forced_urls) const; |
| 191 | 188 |
| 192 // Add all the forced URLs from |cache_| into |new_list|, making sure not to | 189 // Add all the forced URLs from |cache_| into |new_list|, making sure not to |
| 193 // add any URL that's already in |new_list|'s non-forced URLs. The forced URLs | 190 // add any URL that's already in |new_list|'s non-forced URLs. The forced URLs |
| 194 // in |cache_| and |new_list| are assumed to appear at the front of the list | 191 // in |cache_| and |new_list| are assumed to appear at the front of the list |
| 195 // and be sorted in increasing |last_forced_time|. This will still be true | 192 // and be sorted in increasing |last_forced_time|. This will still be true |
| 196 // after the call. If the list of forced URLs overflows the older ones are | 193 // after the call. If the list of forced URLs overflows the older ones are |
| 197 // dropped. Returns the number of forced URLs after the merge. | 194 // dropped. Returns the number of forced URLs after the merge. |
| 198 size_t MergeCachedForcedURLs(MostVisitedURLList* new_list); | 195 size_t MergeCachedForcedURLs(MostVisitedURLList* new_list) const; |
| 199 | 196 |
| 200 // Takes |urls|, produces it's copy in |out| after removing blacklisted URLs. | 197 // Takes |urls|, produces it's copy in |out| after removing blacklisted URLs. |
| 201 // Also ensures we respect the maximum number of forced URLs and non-forced | 198 // Also ensures we respect the maximum number of forced URLs and non-forced |
| 202 // URLs. | 199 // URLs. |
| 203 void ApplyBlacklist(const MostVisitedURLList& urls, MostVisitedURLList* out); | 200 void ApplyBlacklist(const MostVisitedURLList& urls, MostVisitedURLList* out); |
| 204 | 201 |
| 205 // Returns an MD5 hash of the URL. Hashing is required for blacklisted URLs. | 202 // Returns an MD5 hash of the URL. Hashing is required for blacklisted URLs. |
| 206 std::string GetURLHash(const GURL& url); | 203 static std::string GetURLHash(const GURL& url); |
| 207 | 204 |
| 208 // Returns the delay until the next update of history is needed. | 205 // Returns the delay until the next update of history is needed. |
| 209 // Uses num_urls_changed | 206 // Uses |last_num_urls_changed_|. |
| 210 base::TimeDelta GetUpdateDelay(); | 207 base::TimeDelta GetUpdateDelay() const; |
| 211 | 208 |
| 212 // Updates URLs in |cache_| and the db (in the background). | 209 // Updates URLs in |cache_| and the db (in the background). |
| 213 // The non-forced URLs in |new_top_sites| replace those in |cache_|. | 210 // The non-forced URLs in |new_top_sites| replace those in |cache_|. |
| 214 // The forced URLs of |new_top_sites| are merged with those in |cache_|, | 211 // The forced URLs of |new_top_sites| are merged with those in |cache_|, |
| 215 // if the list of forced URLs overflows, the oldest ones are dropped. | 212 // if the list of forced URLs overflows, the oldest ones are dropped. |
| 216 // All mutations to cache_ *must* go through this. Should | 213 // All mutations to cache_ *must* go through this. Should |
| 217 // be called from the UI thread. | 214 // be called from the UI thread. |
| 218 void SetTopSites(const MostVisitedURLList& new_top_sites, | 215 void SetTopSites(const MostVisitedURLList& new_top_sites, |
| 219 const CallLocation location); | 216 const CallLocation location); |
| 220 | 217 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 251 | 248 |
| 252 // Ensures that non thread-safe methods are called on the correct thread. | 249 // Ensures that non thread-safe methods are called on the correct thread. |
| 253 base::ThreadChecker thread_checker_; | 250 base::ThreadChecker thread_checker_; |
| 254 | 251 |
| 255 scoped_refptr<TopSitesBackend> backend_; | 252 scoped_refptr<TopSitesBackend> backend_; |
| 256 | 253 |
| 257 // The top sites data. | 254 // The top sites data. |
| 258 std::unique_ptr<TopSitesCache> cache_; | 255 std::unique_ptr<TopSitesCache> cache_; |
| 259 | 256 |
| 260 // Copy of the top sites data that may be accessed on any thread (assuming | 257 // Copy of the top sites data that may be accessed on any thread (assuming |
| 261 // you hold |lock_|). The data in |thread_safe_cache_| has blacklisted and | 258 // you hold |lock_|). The data in |thread_safe_cache_| has blacklisted urls |
| 262 // pinned urls applied (|cache_| does not). | 259 // applied (|cache_| does not). |
| 263 std::unique_ptr<TopSitesCache> thread_safe_cache_; | 260 std::unique_ptr<TopSitesCache> thread_safe_cache_; |
| 264 | 261 |
| 265 // Lock used to access |thread_safe_cache_|. | 262 // Lock used to access |thread_safe_cache_|. |
| 266 mutable base::Lock lock_; | 263 mutable base::Lock lock_; |
| 267 | 264 |
| 268 // Task tracker for history and backend requests. | 265 // Task tracker for history and backend requests. |
| 269 base::CancelableTaskTracker cancelable_task_tracker_; | 266 base::CancelableTaskTracker cancelable_task_tracker_; |
| 270 | 267 |
| 271 // Timer that asks history for the top sites. This is used to make sure our | 268 // Timer that asks history for the top sites. This is used to make sure our |
| 272 // data stays in sync with history. | 269 // data stays in sync with history. |
| 273 base::OneShotTimer timer_; | 270 base::OneShotTimer timer_; |
| 274 | 271 |
| 275 // The time we started |timer_| at. Only valid if |timer_| is running. | 272 // The time we started |timer_| at. Only valid if |timer_| is running. |
| 276 base::TimeTicks timer_start_time_; | 273 base::TimeTicks timer_start_time_; |
| 277 | 274 |
| 278 // The number of URLs changed on the last update. | 275 // The number of URLs changed on the last update. |
| 279 size_t last_num_urls_changed_; | 276 size_t last_num_urls_changed_; |
| 280 | 277 |
| 281 // The pending requests for the top sites list. Can only be non-empty at | 278 // The pending requests for the top sites list. Can only be non-empty at |
| 282 // startup. After we read the top sites from the DB, we'll always have a | 279 // startup. After we read the top sites from the DB, we'll always have a |
| 283 // cached list and be able to run callbacks immediately. | 280 // cached list and be able to run callbacks immediately. |
| 284 PendingCallbacks pending_callbacks_; | 281 PendingCallbacks pending_callbacks_; |
| 285 | 282 |
| 286 // Stores thumbnails for unknown pages. When SetPageThumbnail is | 283 // Stores thumbnails for unknown pages. When SetPageThumbnail is |
| 287 // called, if we don't know about that URL yet and we don't have | 284 // called, if we don't know about that URL yet and we don't have |
| 288 // enough Top Sites (new profile), we store it until the next | 285 // enough Top Sites (new profile), we store it until the next |
| 289 // SetNonForcedTopSites call. | 286 // SetTopSites call. |
| 290 TempImages temp_images_; | 287 TempImages temp_images_; |
| 291 | 288 |
| 292 // URL List of prepopulated page. | 289 // URL List of prepopulated page. |
| 293 PrepopulatedPageList prepopulated_pages_; | 290 const PrepopulatedPageList prepopulated_pages_; |
| 294 | 291 |
| 295 // PrefService holding the NTP URL blacklist dictionary. Must outlive | 292 // PrefService holding the NTP URL blacklist dictionary. Must outlive |
| 296 // TopSitesImpl. | 293 // TopSitesImpl. |
| 297 PrefService* pref_service_; | 294 PrefService* pref_service_; |
| 298 | 295 |
| 299 // HistoryService that TopSitesImpl can query. May be null, but if defined it | 296 // HistoryService that TopSitesImpl can query. May be null, but if defined it |
| 300 // must outlive TopSitesImpl. | 297 // must outlive TopSitesImpl. |
| 301 HistoryService* history_service_; | 298 HistoryService* history_service_; |
| 302 | 299 |
| 303 // Can URL be added to the history? | 300 // Can URL be added to the history? |
| 304 CanAddURLToHistoryFn can_add_url_to_history_; | 301 CanAddURLToHistoryFn can_add_url_to_history_; |
| 305 | 302 |
| 306 // Are we loaded? | 303 // Are we loaded? |
| 307 bool loaded_; | 304 bool loaded_; |
| 308 | 305 |
| 309 // Have the SetTopSites execution time related histograms been recorded? | 306 // Have the SetTopSites execution time related histograms been recorded? |
| 310 // The histogram should only be recorded once for each Chrome execution. | 307 // The histogram should only be recorded once for each Chrome execution. |
| 311 static bool histogram_recorded_; | 308 static bool histogram_recorded_; |
| 312 | 309 |
| 313 ScopedObserver<HistoryService, HistoryServiceObserver> | 310 ScopedObserver<HistoryService, HistoryServiceObserver> |
| 314 history_service_observer_; | 311 history_service_observer_; |
| 315 | 312 |
| 316 DISALLOW_COPY_AND_ASSIGN(TopSitesImpl); | 313 DISALLOW_COPY_AND_ASSIGN(TopSitesImpl); |
| 317 }; | 314 }; |
| 318 | 315 |
| 319 } // namespace history | 316 } // namespace history |
| 320 | 317 |
| 321 #endif // COMPONENTS_HISTORY_CORE_BROWSER_TOP_SITES_IMPL_H_ | 318 #endif // COMPONENTS_HISTORY_CORE_BROWSER_TOP_SITES_IMPL_H_ |
| OLD | NEW |