| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_FAVICON_FAVICON_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ |
| 6 #define CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ | 6 #define CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 // | 36 // |
| 37 // FetchFavicon requests the favicon from the favicon service which in turn | 37 // FetchFavicon requests the favicon from the favicon service which in turn |
| 38 // requests the favicon from the history database. At this point | 38 // requests the favicon from the history database. At this point |
| 39 // we only know the URL of the page, and not necessarily the url of the | 39 // we only know the URL of the page, and not necessarily the url of the |
| 40 // favicon. To ensure we handle reloading stale favicons as well as | 40 // favicon. To ensure we handle reloading stale favicons as well as |
| 41 // reloading a favicon on page reload we always request the favicon from | 41 // reloading a favicon on page reload we always request the favicon from |
| 42 // history regardless of whether the NavigationEntry has a favicon. | 42 // history regardless of whether the NavigationEntry has a favicon. |
| 43 // | 43 // |
| 44 // After the navigation two types of events are delivered (which is | 44 // After the navigation two types of events are delivered (which is |
| 45 // first depends upon who is faster): notification from the history | 45 // first depends upon who is faster): notification from the history |
| 46 // db on our request for the favicon (OnFaviconDataForInitialURL), | 46 // db on our request for the favicon |
| 47 // or a message from the renderer giving us the URL of the favicon for | 47 // (OnFaviconDataForInitialURLFromFaviconService), or a message from the |
| 48 // the page (SetFaviconURL). | 48 // renderer giving us the URL of the favicon for the page (SetFaviconURL). |
| 49 // . If the history db has a valid up to date favicon for the page, we update | 49 // . If the history db has a valid up to date favicon for the page, we update |
| 50 // the NavigationEntry and use the favicon. | 50 // the NavigationEntry and use the favicon. |
| 51 // . When we receive the favicon url if it matches that of the NavigationEntry | 51 // . When we receive the favicon url if it matches that of the NavigationEntry |
| 52 // and the NavigationEntry's favicon is set, we do nothing (everything is | 52 // and the NavigationEntry's favicon is set, we do nothing (everything is |
| 53 // ok). | 53 // ok). |
| 54 // . On the other hand if the database does not know the favicon for url, or | 54 // . On the other hand if the database does not know the favicon for url, or |
| 55 // the favicon is out date, or the URL from the renderer does not match that | 55 // the favicon is out date, or the URL from the renderer does not match that |
| 56 // NavigationEntry we proceed to DownloadFaviconOrAskHistory. Before we | 56 // NavigationEntry we proceed to DownloadFaviconOrAskHistory. Before we |
| 57 // invoke DownloadFaviconOrAskHistory we wait until we've received both | 57 // invoke DownloadFaviconOrAskHistory we wait until we've received both |
| 58 // the favicon url and the callback from history. We wait to ensure we | 58 // the favicon url and the callback from history. We wait to ensure we |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 virtual int DownloadFavicon(const GURL& image_url, int max_bitmap_size); | 128 virtual int DownloadFavicon(const GURL& image_url, int max_bitmap_size); |
| 129 | 129 |
| 130 // Ask the favicon from history | 130 // Ask the favicon from history |
| 131 virtual void UpdateFaviconMappingAndFetch( | 131 virtual void UpdateFaviconMappingAndFetch( |
| 132 const GURL& page_url, | 132 const GURL& page_url, |
| 133 const GURL& icon_url, | 133 const GURL& icon_url, |
| 134 chrome::IconType icon_type, | 134 chrome::IconType icon_type, |
| 135 const FaviconService::FaviconResultsCallback& callback, | 135 const FaviconService::FaviconResultsCallback& callback, |
| 136 base::CancelableTaskTracker* tracker); | 136 base::CancelableTaskTracker* tracker); |
| 137 | 137 |
| 138 virtual void GetFavicon( | 138 virtual void GetFaviconFromFaviconService( |
| 139 const GURL& icon_url, | 139 const GURL& icon_url, |
| 140 chrome::IconType icon_type, | 140 chrome::IconType icon_type, |
| 141 const FaviconService::FaviconResultsCallback& callback, | 141 const FaviconService::FaviconResultsCallback& callback, |
| 142 base::CancelableTaskTracker* tracker); | 142 base::CancelableTaskTracker* tracker); |
| 143 | 143 |
| 144 virtual void GetFaviconForURL( | 144 virtual void GetFaviconForURLFromFaviconService( |
| 145 const GURL& page_url, | 145 const GURL& page_url, |
| 146 int icon_types, | 146 int icon_types, |
| 147 const FaviconService::FaviconResultsCallback& callback, | 147 const FaviconService::FaviconResultsCallback& callback, |
| 148 base::CancelableTaskTracker* tracker); | 148 base::CancelableTaskTracker* tracker); |
| 149 | 149 |
| 150 virtual void SetHistoryFavicons(const GURL& page_url, | 150 virtual void SetHistoryFavicons(const GURL& page_url, |
| 151 const GURL& icon_url, | 151 const GURL& icon_url, |
| 152 chrome::IconType icon_type, | 152 chrome::IconType icon_type, |
| 153 const gfx::Image& image); | 153 const gfx::Image& image); |
| 154 | 154 |
| 155 virtual FaviconService* GetFaviconService(); | 155 virtual FaviconService* GetFaviconService(); |
| 156 | 156 |
| 157 // Returns true if the favicon should be saved. | 157 // Returns true if the favicon should be saved. |
| 158 virtual bool ShouldSaveFavicon(const GURL& url); | 158 virtual bool ShouldSaveFavicon(const GURL& url); |
| 159 | 159 |
| 160 // Notifies the delegate that the favicon for the active entry was updated. | 160 // Notifies the delegate that the favicon for the active entry was updated. |
| 161 // |icon_url_changed| is true if a favicon with a different icon URL has been | 161 // |icon_url_changed| is true if a favicon with a different icon URL has been |
| 162 // selected since the previous call to NotifyFaviconUpdated(). | 162 // selected since the previous call to NotifyFaviconUpdated(). |
| 163 virtual void NotifyFaviconUpdated(bool icon_url_changed); | 163 virtual void NotifyFaviconUpdated(bool icon_url_changed); |
| 164 | 164 |
| 165 private: | 165 private: |
| 166 friend class TestFaviconHandler; // For testing | 166 friend class TestFaviconHandler; // For testing |
| 167 | 167 |
| 168 // Represents an in progress download of an image from the renderer. |
| 168 struct DownloadRequest { | 169 struct DownloadRequest { |
| 169 DownloadRequest(); | 170 DownloadRequest(); |
| 170 ~DownloadRequest(); | 171 ~DownloadRequest(); |
| 171 | 172 |
| 172 DownloadRequest(const GURL& url, | 173 DownloadRequest(const GURL& url, |
| 173 const GURL& image_url, | 174 const GURL& image_url, |
| 174 chrome::IconType icon_type); | 175 chrome::IconType icon_type); |
| 175 | 176 |
| 176 GURL url; | 177 GURL url; |
| 177 GURL image_url; | 178 GURL image_url; |
| 178 chrome::IconType icon_type; | 179 chrome::IconType icon_type; |
| 179 }; | 180 }; |
| 180 | 181 |
| 182 // Used to track a candidate for the favicon. |
| 181 struct FaviconCandidate { | 183 struct FaviconCandidate { |
| 182 FaviconCandidate(); | 184 FaviconCandidate(); |
| 183 ~FaviconCandidate(); | 185 ~FaviconCandidate(); |
| 184 | 186 |
| 185 FaviconCandidate(const GURL& url, | 187 FaviconCandidate(const GURL& url, |
| 186 const GURL& image_url, | 188 const GURL& image_url, |
| 187 const gfx::Image& image, | 189 const gfx::Image& image, |
| 188 float score, | 190 float score, |
| 189 chrome::IconType icon_type); | 191 chrome::IconType icon_type); |
| 190 | 192 |
| 191 GURL url; | 193 GURL url; |
| 192 GURL image_url; | 194 GURL image_url; |
| 193 gfx::Image image; | 195 gfx::Image image; |
| 194 float score; | 196 float score; |
| 195 chrome::IconType icon_type; | 197 chrome::IconType icon_type; |
| 196 }; | 198 }; |
| 197 | 199 |
| 198 // See description above class for details. | 200 // See description above class for details. |
| 199 void OnFaviconDataForInitialURL( | 201 void OnFaviconDataForInitialURLFromFaviconService( |
| 200 const std::vector<chrome::FaviconBitmapResult>& favicon_bitmap_results); | 202 const std::vector<chrome::FaviconBitmapResult>& favicon_bitmap_results); |
| 201 | 203 |
| 202 // If the favicon has expired, asks the renderer to download the favicon. | 204 // If the favicon has expired, asks the renderer to download the favicon. |
| 203 // Otherwise asks history to update the mapping between page url and icon | 205 // Otherwise asks history to update the mapping between page url and icon |
| 204 // url with a callback to OnFaviconData when done. | 206 // url with a callback to OnFaviconData when done. |
| 205 void DownloadFaviconOrAskHistory(const GURL& page_url, | 207 void DownloadFaviconOrAskFaviconService(const GURL& page_url, |
| 206 const GURL& icon_url, | 208 const GURL& icon_url, |
| 207 chrome::IconType icon_type); | 209 chrome::IconType icon_type); |
| 208 | 210 |
| 209 // See description above class for details. | 211 // See description above class for details. |
| 210 void OnFaviconData( | 212 void OnFaviconData( |
| 211 const std::vector<chrome::FaviconBitmapResult>& favicon_bitmap_results); | 213 const std::vector<chrome::FaviconBitmapResult>& favicon_bitmap_results); |
| 212 | 214 |
| 213 // Schedules a download for the specified entry. This adds the request to | 215 // Schedules a download for the specified entry. This adds the request to |
| 214 // download_requests_. | 216 // download_requests_. |
| 215 int ScheduleDownload(const GURL& url, | 217 int ScheduleDownload(const GURL& url, |
| 216 const GURL& image_url, | 218 const GURL& image_url, |
| 217 chrome::IconType icon_type); | 219 chrome::IconType icon_type); |
| 218 | 220 |
| 219 // Updates |favicon_candidate_| and returns true if it is an exact match. | 221 // Updates |favicon_candidate_| and returns true if it is an exact match. |
| 220 bool UpdateFaviconCandidate(const GURL& url, | 222 bool UpdateFaviconCandidate(const GURL& url, |
| 221 const GURL& image_url, | 223 const GURL& image_url, |
| 222 const gfx::Image& image, | 224 const gfx::Image& image, |
| 223 float score, | 225 float score, |
| 224 chrome::IconType icon_type); | 226 chrome::IconType icon_type); |
| 225 | 227 |
| 226 // Sets the image data for the favicon. | 228 // Sets the image data for the favicon. |
| 227 void SetFavicon(const GURL& url, | 229 void SetFavicon(const GURL& url, |
| 228 const GURL& icon_url, | 230 const GURL& icon_url, |
| 229 const gfx::Image& image, | 231 const gfx::Image& image, |
| 230 chrome::IconType icon_type); | 232 chrome::IconType icon_type); |
| 231 | 233 |
| 232 // Sets the favicon's data on the NavigationEntry. | 234 // Sets the favicon's data on the NavigationEntry. |
| 233 // If the WebContents has a delegate, it is invalidated (INVALIDATE_TYPE_TAB). | 235 // If the WebContents has a delegate, it is invalidated (INVALIDATE_TYPE_TAB). |
| 234 void UpdateFavicon(content::NavigationEntry* entry, | 236 void SetFaviconOnNavigationEntry( |
| 237 content::NavigationEntry* entry, |
| 235 const std::vector<chrome::FaviconBitmapResult>& favicon_bitmap_results); | 238 const std::vector<chrome::FaviconBitmapResult>& favicon_bitmap_results); |
| 236 void UpdateFavicon(content::NavigationEntry* entry, | 239 void SetFaviconOnNavigationEntry(content::NavigationEntry* entry, |
| 237 const GURL& icon_url, | 240 const GURL& icon_url, |
| 238 const gfx::Image& image); | 241 const gfx::Image& image); |
| 239 | 242 |
| 240 // Return the current candidate if any. | 243 // Return the current candidate if any. |
| 241 content::FaviconURL* current_candidate() { | 244 content::FaviconURL* current_candidate() { |
| 242 return (image_urls_.size() > 0) ? &image_urls_[0] : NULL; | 245 return (!image_urls_.empty()) ? &image_urls_.front() : NULL; |
| 243 } | 246 } |
| 244 | 247 |
| 245 // Returns the preferred_icon_size according icon_types_, 0 means no | 248 // Returns the preferred size of the image. 0 means no preference (any size |
| 246 // preference. | 249 // will do). |
| 247 int preferred_icon_size() { | 250 int preferred_icon_size() { |
| 248 #if defined(OS_ANDROID) | 251 #if defined(OS_ANDROID) |
| 249 return 0; | 252 return 0; |
| 250 #else | 253 #else |
| 251 return icon_types_ == chrome::FAVICON ? gfx::kFaviconSize : 0; | 254 return icon_types_ == chrome::FAVICON ? gfx::kFaviconSize : 0; |
| 252 #endif | 255 #endif |
| 253 } | 256 } |
| 254 | 257 |
| 255 // Used for FaviconService requests. | 258 // Used for FaviconService requests. |
| 256 base::CancelableTaskTracker cancelable_task_tracker_; | 259 base::CancelableTaskTracker cancelable_task_tracker_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 279 | 282 |
| 280 // The FaviconBitmapResults from history. | 283 // The FaviconBitmapResults from history. |
| 281 std::vector<chrome::FaviconBitmapResult> history_results_; | 284 std::vector<chrome::FaviconBitmapResult> history_results_; |
| 282 | 285 |
| 283 // The Profile associated with this handler. | 286 // The Profile associated with this handler. |
| 284 Profile* profile_; | 287 Profile* profile_; |
| 285 | 288 |
| 286 // This handler's delegate. | 289 // This handler's delegate. |
| 287 FaviconHandlerDelegate* delegate_; // weak | 290 FaviconHandlerDelegate* delegate_; // weak |
| 288 | 291 |
| 289 // Current favicon candidate. | 292 // Best image we've seen so far. As images are downloaded from the page they |
| 290 FaviconCandidate favicon_candidate_; | 293 // are stored here. When there is an exact match, or no more images are |
| 294 // available the favicon service and the NavigationEntry are updated (assuming |
| 295 // the image is for a favicon). |
| 296 FaviconCandidate best_favicon_candidate_; |
| 291 | 297 |
| 292 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 298 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
| 293 }; | 299 }; |
| 294 | 300 |
| 295 #endif // CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ | 301 #endif // CHROME_BROWSER_FAVICON_FAVICON_HANDLER_H_ |
| OLD | NEW |