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 CHROME_BROWSER_HISTORY_TOP_SITES_IMPL_H_ | 5 #ifndef CHROME_BROWSER_HISTORY_TOP_SITES_IMPL_H_ |
6 #define CHROME_BROWSER_HISTORY_TOP_SITES_IMPL_H_ | 6 #define CHROME_BROWSER_HISTORY_TOP_SITES_IMPL_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // to internal state happen on the UI thread and are scheduled to update the | 48 // to internal state happen on the UI thread and are scheduled to update the |
49 // db using TopSitesBackend. | 49 // db using TopSitesBackend. |
50 class TopSitesImpl : public TopSites { | 50 class TopSitesImpl : public TopSites { |
51 public: | 51 public: |
52 explicit TopSitesImpl(Profile* profile); | 52 explicit TopSitesImpl(Profile* profile); |
53 | 53 |
54 // Initializes TopSitesImpl. | 54 // Initializes TopSitesImpl. |
55 void Init(const base::FilePath& db_name); | 55 void Init(const base::FilePath& db_name); |
56 | 56 |
57 virtual bool SetPageThumbnail(const GURL& url, | 57 virtual bool SetPageThumbnail(const GURL& url, |
58 const gfx::Image& thumbnail, | 58 const gfx::Image& thumbnail, |
59 const ThumbnailScore& score) OVERRIDE; | 59 const ThumbnailScore& score) OVERRIDE; |
60 virtual bool SetPageThumbnailToJPEGBytes( | 60 virtual bool SetPageThumbnailToJPEGBytes( |
61 const GURL& url, | 61 const GURL& url, |
62 const base::RefCountedMemory* memory, | 62 const base::RefCountedMemory* memory, |
63 const ThumbnailScore& score) OVERRIDE; | 63 const ThumbnailScore& score) OVERRIDE; |
64 virtual void GetMostVisitedURLs( | 64 virtual void GetMostVisitedURLs( |
65 const GetMostVisitedURLsCallback& callback) OVERRIDE; | 65 const GetMostVisitedURLsCallback& callback, |
| 66 bool include_forced_urls) OVERRIDE; |
66 virtual bool GetPageThumbnail( | 67 virtual bool GetPageThumbnail( |
67 const GURL& url, | 68 const GURL& url, |
68 bool prefix_match, | 69 bool prefix_match, |
69 scoped_refptr<base::RefCountedMemory>* bytes) OVERRIDE; | 70 scoped_refptr<base::RefCountedMemory>* bytes) OVERRIDE; |
70 virtual bool GetPageThumbnailScore(const GURL& url, | 71 virtual bool GetPageThumbnailScore(const GURL& url, |
71 ThumbnailScore* score) OVERRIDE; | 72 ThumbnailScore* score) OVERRIDE; |
72 virtual bool GetTemporaryPageThumbnailScore(const GURL& url, | 73 virtual bool GetTemporaryPageThumbnailScore(const GURL& url, |
73 ThumbnailScore* score) OVERRIDE; | 74 ThumbnailScore* score) OVERRIDE; |
74 virtual void SyncWithHistory() OVERRIDE; | 75 virtual void SyncWithHistory() OVERRIDE; |
75 virtual bool HasBlacklistedItems() const OVERRIDE; | 76 virtual bool HasBlacklistedItems() const OVERRIDE; |
76 virtual void AddBlacklistedURL(const GURL& url) OVERRIDE; | 77 virtual void AddBlacklistedURL(const GURL& url) OVERRIDE; |
77 virtual void RemoveBlacklistedURL(const GURL& url) OVERRIDE; | 78 virtual void RemoveBlacklistedURL(const GURL& url) OVERRIDE; |
78 virtual bool IsBlacklisted(const GURL& url) OVERRIDE; | 79 virtual bool IsBlacklisted(const GURL& url) OVERRIDE; |
79 virtual void ClearBlacklistedURLs() OVERRIDE; | 80 virtual void ClearBlacklistedURLs() OVERRIDE; |
80 virtual void Shutdown() OVERRIDE; | 81 virtual void Shutdown() OVERRIDE; |
81 virtual CancelableRequestProvider::Handle StartQueryForMostVisited() OVERRIDE; | 82 virtual CancelableRequestProvider::Handle StartQueryForMostVisited() OVERRIDE; |
82 virtual bool IsKnownURL(const GURL& url) OVERRIDE; | 83 virtual bool IsKnownURL(const GURL& url) OVERRIDE; |
83 virtual const std::string& GetCanonicalURLString( | 84 virtual const std::string& GetCanonicalURLString( |
84 const GURL& url) const OVERRIDE; | 85 const GURL& url) const OVERRIDE; |
85 virtual bool IsFull() OVERRIDE; | 86 virtual bool IsNonForcedFull() OVERRIDE; |
| 87 virtual bool IsForcedFull() OVERRIDE; |
86 virtual MostVisitedURLList GetPrepopulatePages() OVERRIDE; | 88 virtual MostVisitedURLList GetPrepopulatePages() OVERRIDE; |
87 virtual bool loaded() const OVERRIDE; | 89 virtual bool loaded() const OVERRIDE; |
88 | 90 |
89 protected: | 91 protected: |
90 virtual ~TopSitesImpl(); | 92 virtual ~TopSitesImpl(); |
91 | 93 |
92 private: | 94 private: |
93 friend class TopSitesImplTest; | 95 friend class TopSitesImplTest; |
94 FRIEND_TEST_ALL_PREFIXES(TopSitesImplTest, DiffMostVisited); | 96 FRIEND_TEST_ALL_PREFIXES(TopSitesImplTest, DiffMostVisited); |
| 97 FRIEND_TEST_ALL_PREFIXES(TopSitesImplTest, DiffMostVisitedWithForced); |
| 98 |
| 99 typedef base::Callback<void(const MostVisitedURLList&, |
| 100 const MostVisitedURLList&)> PendingCallback; |
95 | 101 |
96 typedef std::pair<GURL, Images> TempImage; | 102 typedef std::pair<GURL, Images> TempImage; |
97 typedef std::list<TempImage> TempImages; | 103 typedef std::list<TempImage> TempImages; |
| 104 typedef std::vector<PendingCallback> PendingCallbacks; |
98 | 105 |
99 // Generates the diff of things that happened between "old" and "new." | 106 // Generates the diff of things that happened between "old" and "new." |
100 // | 107 // |
| 108 // This treats forced URLs separately than non-forced URLs. |
| 109 // |
101 // The URLs that are in "new" but not "old" will be have their index into | 110 // The URLs that are in "new" but not "old" will be have their index into |
102 // "new" put in |added_urls|. The URLs that are in "old" but not "new" will | 111 // "new" put in |added_urls|. The non-forced URLs that are in "old" but not |
103 // have their index into "old" put into |deleted_urls|. | 112 // "new" will have their index into "old" put into |deleted_urls|. |
104 // | 113 // |
105 // URLs appearing in both old and new lists but having different indices will | 114 // URLs appearing in both old and new lists but having different indices will |
106 // have their index into "new" be put into |moved_urls|. | 115 // have their index into "new" be put into |moved_urls|. |
107 static void DiffMostVisited(const MostVisitedURLList& old_list, | 116 static void DiffMostVisited(const MostVisitedURLList& old_list, |
108 const MostVisitedURLList& new_list, | 117 const MostVisitedURLList& new_list, |
109 TopSitesDelta* delta); | 118 TopSitesDelta* delta); |
110 | 119 |
111 // Sets the thumbnail without writing to the database. Useful when | 120 // Sets the thumbnail without writing to the database. Useful when |
112 // reading last known top sites from the DB. | 121 // reading last known top sites from the DB. |
113 // Returns true if the thumbnail was set, false if the existing one is better. | 122 // Returns true if the thumbnail was set, false if the existing one is better. |
(...skipping 25 matching lines...) Expand all Loading... |
139 void TimerFired(); | 148 void TimerFired(); |
140 | 149 |
141 // Finds the given URL in the redirect chain for the given TopSite, and | 150 // Finds the given URL in the redirect chain for the given TopSite, and |
142 // returns the distance from the destination in hops that the given URL is. | 151 // returns the distance from the destination in hops that the given URL is. |
143 // The URL is assumed to be in the list. The destination is 0. | 152 // The URL is assumed to be in the list. The destination is 0. |
144 static int GetRedirectDistanceForURL(const MostVisitedURL& most_visited, | 153 static int GetRedirectDistanceForURL(const MostVisitedURL& most_visited, |
145 const GURL& url); | 154 const GURL& url); |
146 | 155 |
147 // Add prepopulated pages: 'welcome to Chrome' and themes gallery to |urls|. | 156 // Add prepopulated pages: 'welcome to Chrome' and themes gallery to |urls|. |
148 // Returns true if any pages were added. | 157 // Returns true if any pages were added. |
149 bool AddPrepopulatedPages(MostVisitedURLList* urls); | 158 bool AddPrepopulatedPages(MostVisitedURLList* urls, |
| 159 size_t num_forced_urls); |
| 160 |
| 161 // Add all the forced URLs from |cache_| into |new_list|, making sure not to |
| 162 // add any URL that's already in |new_list|'s non-forced URLs. The forced URLs |
| 163 // in |cache_| and |new_list| are assumed to appear at the front of the list |
| 164 // and be sorted in increasing |last_forced_time|. This will still be true |
| 165 // after the call. If the list of forced URLs overflows the older ones are |
| 166 // dropped. Returns the number of forced URLs after the merge. |
| 167 size_t MergeCachedForcedURLs(MostVisitedURLList* new_list); |
150 | 168 |
151 // Takes |urls|, produces it's copy in |out| after removing blacklisted URLs. | 169 // Takes |urls|, produces it's copy in |out| after removing blacklisted URLs. |
| 170 // Also ensures we respect the maximum number of forced URLs and non-forced |
| 171 // URLs. |
152 void ApplyBlacklist(const MostVisitedURLList& urls, MostVisitedURLList* out); | 172 void ApplyBlacklist(const MostVisitedURLList& urls, MostVisitedURLList* out); |
153 | 173 |
154 // Returns an MD5 hash of the URL. Hashing is required for blacklisted URLs. | 174 // Returns an MD5 hash of the URL. Hashing is required for blacklisted URLs. |
155 std::string GetURLHash(const GURL& url); | 175 std::string GetURLHash(const GURL& url); |
156 | 176 |
157 // Returns the delay until the next update of history is needed. | 177 // Returns the delay until the next update of history is needed. |
158 // Uses num_urls_changed | 178 // Uses num_urls_changed |
159 base::TimeDelta GetUpdateDelay(); | 179 base::TimeDelta GetUpdateDelay(); |
160 | 180 |
161 // Implementation of content::NotificationObserver. | 181 // Implementation of content::NotificationObserver. |
162 virtual void Observe(int type, | 182 virtual void Observe(int type, |
163 const content::NotificationSource& source, | 183 const content::NotificationSource& source, |
164 const content::NotificationDetails& details) OVERRIDE; | 184 const content::NotificationDetails& details) OVERRIDE; |
165 | 185 |
166 // Resets top_sites_ and updates the db (in the background). All mutations to | 186 // Updates URLs in |cache_| and the db (in the background). |
167 // top_sites_ *must* go through this. Should be called from the UI thread. | 187 // The non-forced URLs in |new_top_sites| replace those in |cache_|. |
| 188 // The forced URLs of |new_top_sites| are merged with those in |cache_|, |
| 189 // if the list of forced URLs overflows, the oldest ones are dropped. |
| 190 // All mutations to cache_ *must* go through this. Should |
| 191 // be called from the UI thread. |
168 void SetTopSites(const MostVisitedURLList& new_top_sites); | 192 void SetTopSites(const MostVisitedURLList& new_top_sites); |
169 | 193 |
170 // Returns the number of most visted results to request from history. This | 194 // Returns the number of most visited results to request from history. This |
171 // changes depending upon how many urls have been blacklisted. Should be | 195 // changes depending upon how many urls have been blacklisted. Should be |
172 // called from the UI thread. | 196 // called from the UI thread. |
173 int num_results_to_request_from_history() const; | 197 int num_results_to_request_from_history() const; |
174 | 198 |
175 // Invoked when transitioning to LOADED. Notifies any queued up callbacks. | 199 // Invoked when transitioning to LOADED. Notifies any queued up callbacks. |
176 // Should be called from the UI thread. | 200 // Should be called from the UI thread. |
177 void MoveStateToLoaded(); | 201 void MoveStateToLoaded(); |
178 | 202 |
179 void ResetThreadSafeCache(); | 203 void ResetThreadSafeCache(); |
180 | 204 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 size_t last_num_urls_changed_; | 251 size_t last_num_urls_changed_; |
228 | 252 |
229 // The pending requests for the top sites list. Can only be non-empty at | 253 // The pending requests for the top sites list. Can only be non-empty at |
230 // startup. After we read the top sites from the DB, we'll always have a | 254 // startup. After we read the top sites from the DB, we'll always have a |
231 // cached list and be able to run callbacks immediately. | 255 // cached list and be able to run callbacks immediately. |
232 PendingCallbacks pending_callbacks_; | 256 PendingCallbacks pending_callbacks_; |
233 | 257 |
234 // Stores thumbnails for unknown pages. When SetPageThumbnail is | 258 // Stores thumbnails for unknown pages. When SetPageThumbnail is |
235 // called, if we don't know about that URL yet and we don't have | 259 // called, if we don't know about that URL yet and we don't have |
236 // enough Top Sites (new profile), we store it until the next | 260 // enough Top Sites (new profile), we store it until the next |
237 // SetTopSites call. | 261 // SetNonForcedTopSites call. |
238 TempImages temp_images_; | 262 TempImages temp_images_; |
239 | 263 |
240 // URL List of prepopulated page. | 264 // URL List of prepopulated page. |
241 std::vector<GURL> prepopulated_page_urls_; | 265 std::vector<GURL> prepopulated_page_urls_; |
242 | 266 |
243 // Are we loaded? | 267 // Are we loaded? |
244 bool loaded_; | 268 bool loaded_; |
245 | 269 |
246 DISALLOW_COPY_AND_ASSIGN(TopSitesImpl); | 270 DISALLOW_COPY_AND_ASSIGN(TopSitesImpl); |
247 }; | 271 }; |
248 | 272 |
249 } // namespace history | 273 } // namespace history |
250 | 274 |
251 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_IMPL_H_ | 275 #endif // CHROME_BROWSER_HISTORY_TOP_SITES_IMPL_H_ |
OLD | NEW |