Chromium Code Reviews| 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 COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 5 #ifndef COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
| 6 #define COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 6 #define COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 | 123 |
| 124 // Initiates loading the favicon for the specified url. | 124 // Initiates loading the favicon for the specified url. |
| 125 void FetchFavicon(const GURL& url); | 125 void FetchFavicon(const GURL& url); |
| 126 | 126 |
| 127 // Message Handler. Must be public, because also called from | 127 // Message Handler. Must be public, because also called from |
| 128 // PrerenderContents. Collects the |image_urls| list. | 128 // PrerenderContents. Collects the |image_urls| list. |
| 129 void OnUpdateFaviconURL(const GURL& page_url, | 129 void OnUpdateFaviconURL(const GURL& page_url, |
| 130 const std::vector<favicon::FaviconURL>& candidates); | 130 const std::vector<favicon::FaviconURL>& candidates); |
| 131 | 131 |
| 132 // For testing. | 132 // For testing. |
| 133 const std::vector<favicon::FaviconURL>& image_urls() const { | 133 const std::vector<GURL> GetIconURLs() const; |
| 134 return image_urls_; | |
| 135 } | |
| 136 | 134 |
| 137 // Returns whether the handler is waiting for a download to complete or for | 135 // Returns whether the handler is waiting for a download to complete or for |
| 138 // data from the FaviconService. Reserved for testing. | 136 // data from the FaviconService. Reserved for testing. |
| 139 bool HasPendingTasksForTest(); | 137 bool HasPendingTasksForTest(); |
| 140 | 138 |
| 141 // Get the maximal icon size in pixels for a icon of type |icon_type| for the | 139 // Get the maximal icon size in pixels for a handler of type |handler_type|. |
| 142 // current platform. | 140 static int GetMaximalIconSize( |
| 143 static int GetMaximalIconSize(favicon_base::IconType icon_type); | 141 FaviconDriverObserver::NotificationIconType handler_type); |
| 144 | 142 |
| 145 private: | 143 private: |
| 146 // Used to track a candidate for the favicon. | 144 // Used to track a candidate for the favicon. |
| 147 struct FaviconCandidate { | 145 struct FaviconCandidate { |
| 148 FaviconCandidate(); | 146 // Builds a scored candidate by selecting the best bitmap size. |
|
pkotwicz
2017/03/27 00:49:06
Nit: size -> sizes
mastiz
2017/03/27 10:09:36
Done.
| |
| 149 ~FaviconCandidate(); | 147 static FaviconCandidate FromFaviconURL( |
| 148 const favicon::FaviconURL& favicon_url, | |
| 149 const std::vector<int>& desired_pixel_sizes); | |
| 150 | 150 |
| 151 FaviconCandidate(const GURL& image_url, | 151 static bool Equals(const FaviconCandidate& lhs, |
| 152 const gfx::Image& image, | 152 const FaviconCandidate& rhs) { |
| 153 float score, | 153 return lhs.icon_url == rhs.icon_url && lhs.icon_type == rhs.icon_type && |
| 154 favicon_base::IconType icon_type); | 154 lhs.score == rhs.score; |
| 155 } | |
| 155 | 156 |
| 156 GURL image_url; | 157 // Compare function used for std::stable_sort to sort in descending order. |
| 158 static bool CompareScore(const FaviconCandidate& lhs, | |
| 159 const FaviconCandidate& rhs) { | |
| 160 return lhs.score > rhs.score; | |
| 161 } | |
| 162 | |
| 163 GURL icon_url; | |
| 164 favicon_base::IconType icon_type = favicon_base::INVALID_ICON; | |
| 165 float score = 0; | |
| 166 }; | |
| 167 | |
| 168 struct DownloadedFavicon { | |
| 169 FaviconCandidate candidate; | |
| 157 gfx::Image image; | 170 gfx::Image image; |
| 158 float score; | |
| 159 favicon_base::IconType icon_type; | |
| 160 }; | 171 }; |
| 161 | 172 |
| 162 // Returns the bit mask of favicon_base::IconType based on the handler's type. | 173 // Returns the bit mask of favicon_base::IconType based on the handler's type. |
| 163 static int GetIconTypesFromHandlerType( | 174 static int GetIconTypesFromHandlerType( |
| 164 FaviconDriverObserver::NotificationIconType handler_type); | 175 FaviconDriverObserver::NotificationIconType handler_type); |
| 165 | 176 |
| 166 // Called when the history request for favicon data mapped to |url_| has | 177 // Called when the history request for favicon data mapped to |url_| has |
| 167 // completed and the renderer has told us the icon URLs used by |url_| | 178 // completed and the renderer has told us the icon URLs used by |url_| |
| 168 void OnGotInitialHistoryDataAndIconURLCandidates(); | 179 void OnGotInitialHistoryDataAndIconURLCandidates(); |
| 169 | 180 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 190 void OnDidDownloadFavicon( | 201 void OnDidDownloadFavicon( |
| 191 favicon_base::IconType icon_type, | 202 favicon_base::IconType icon_type, |
| 192 int id, | 203 int id, |
| 193 int http_status_code, | 204 int http_status_code, |
| 194 const GURL& image_url, | 205 const GURL& image_url, |
| 195 const std::vector<SkBitmap>& bitmaps, | 206 const std::vector<SkBitmap>& bitmaps, |
| 196 const std::vector<gfx::Size>& original_bitmap_sizes); | 207 const std::vector<gfx::Size>& original_bitmap_sizes); |
| 197 | 208 |
| 198 bool ShouldSaveFavicon(); | 209 bool ShouldSaveFavicon(); |
| 199 | 210 |
| 200 // Updates |favicon_candidate_| and returns true if it is an exact match. | 211 // Updates |best_favicon_| and returns true if it was considered a satisfying |
| 201 bool UpdateFaviconCandidate(const GURL& image_url, | 212 // image (e.g. exact size match). |
| 202 const gfx::Image& image, | 213 bool UpdateFaviconCandidate(const DownloadedFavicon& downloaded_favicon); |
| 203 float score, | |
| 204 favicon_base::IconType icon_type); | |
| 205 | 214 |
| 206 // Sets the image data for the favicon. | 215 // Sets the image data for the favicon. |
| 207 void SetFavicon(const GURL& icon_url, | 216 void SetFavicon(const GURL& icon_url, |
| 208 const gfx::Image& image, | 217 const gfx::Image& image, |
| 209 favicon_base::IconType icon_type); | 218 favicon_base::IconType icon_type); |
| 210 | 219 |
| 211 // Notifies |driver_| that FaviconHandler found an icon which matches the | 220 // Notifies |driver_| that FaviconHandler found an icon which matches the |
| 212 // |handler_type_| criteria. NotifyFaviconUpdated() can be called multiple | 221 // |handler_type_| criteria. NotifyFaviconUpdated() can be called multiple |
| 213 // times for the same page if: | 222 // times for the same page if: |
| 214 // - a better match is found for |handler_type_| (e.g. newer bitmap data) | 223 // - a better match is found for |handler_type_| (e.g. newer bitmap data) |
| 215 // - Javascript changes the page's icon URLs. | 224 // - Javascript changes the page's icon URLs. |
| 216 void NotifyFaviconUpdated( | 225 void NotifyFaviconUpdated( |
| 217 const std::vector<favicon_base::FaviconRawBitmapResult>& | 226 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 218 favicon_bitmap_results); | 227 favicon_bitmap_results); |
| 219 void NotifyFaviconUpdated(const GURL& icon_url, | 228 void NotifyFaviconUpdated(const GURL& icon_url, |
| 220 favicon_base::IconType icon_type, | 229 favicon_base::IconType icon_type, |
| 221 const gfx::Image& image); | 230 const gfx::Image& image); |
| 222 | 231 |
| 223 // Return the current candidate if any. | 232 // Return the current candidate if any. |
| 224 favicon::FaviconURL* current_candidate() { | 233 const FaviconCandidate* current_candidate() const { |
| 225 return current_candidate_index_ < image_urls_.size() | 234 return current_candidate_index_ < candidates_.size() |
| 226 ? &image_urls_[current_candidate_index_] | 235 ? &candidates_[current_candidate_index_] |
| 227 : nullptr; | 236 : nullptr; |
| 228 } | 237 } |
| 229 | 238 |
| 230 // Returns the preferred size of the image. 0 means no preference (any size | 239 // Returns the preferred size of the image. 0 means no preference (any size |
| 231 // will do). | 240 // will do). |
| 232 int preferred_icon_size() const { | 241 int preferred_icon_size() const { |
| 233 return download_largest_icon_ ? 0 : gfx::kFaviconSize; | 242 return download_largest_icon_ ? 0 : gfx::kFaviconSize; |
| 234 } | 243 } |
| 235 | 244 |
| 236 // Used for FaviconService requests. | 245 // Used for FaviconService requests. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 260 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> | 269 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> |
| 261 download_request_; | 270 download_request_; |
| 262 | 271 |
| 263 // The combination of the supported icon types. | 272 // The combination of the supported icon types. |
| 264 const int icon_types_; | 273 const int icon_types_; |
| 265 | 274 |
| 266 // Whether the largest icon should be downloaded. | 275 // Whether the largest icon should be downloaded. |
| 267 const bool download_largest_icon_; | 276 const bool download_largest_icon_; |
| 268 | 277 |
| 269 // The prioritized favicon candidates from the page back from the renderer. | 278 // The prioritized favicon candidates from the page back from the renderer. |
| 270 std::vector<favicon::FaviconURL> image_urls_; | 279 std::vector<FaviconCandidate> candidates_; |
| 271 | 280 |
| 272 // The icon URL and the icon type of the favicon in the most recent | 281 // The icon URL and the icon type of the favicon in the most recent |
| 273 // FaviconDriver::OnFaviconAvailable() notification. | 282 // FaviconDriver::OnFaviconAvailable() notification. |
| 274 GURL notification_icon_url_; | 283 GURL notification_icon_url_; |
| 275 favicon_base::IconType notification_icon_type_; | 284 favicon_base::IconType notification_icon_type_; |
| 276 | 285 |
| 277 // The FaviconService which implements favicon operations. May be null during | 286 // The FaviconService which implements favicon operations. May be null during |
| 278 // testing. | 287 // testing. |
| 279 FaviconService* service_; | 288 FaviconService* service_; |
| 280 | 289 |
| 281 // This handler's delegate. | 290 // This handler's delegate. |
| 282 Delegate* delegate_; | 291 Delegate* delegate_; |
| 283 | 292 |
| 284 // The index of the favicon URL in |image_urls_| which is currently being | 293 // The index of the favicon URL in |image_urls_| which is currently being |
| 285 // requested from history or downloaded. | 294 // requested from history or downloaded. |
| 286 size_t current_candidate_index_; | 295 size_t current_candidate_index_; |
| 287 | 296 |
| 288 // Best image we've seen so far. As images are downloaded from the page they | 297 // Best image we've seen so far. As images are downloaded from the page they |
| 289 // are stored here. When there is an exact match, or no more images are | 298 // are stored here. When a satisfying icon is found (as defined in |
| 290 // available the favicon service and the current page are updated (assuming | 299 // UpdateFaviconCandidate()), the favicon service and the delegate are |
| 291 // the image is for a favicon). | 300 // notified. |
| 292 FaviconCandidate best_favicon_candidate_; | 301 DownloadedFavicon best_favicon_; |
| 293 | 302 |
| 294 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 303 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
| 295 }; | 304 }; |
| 296 | 305 |
| 297 } // namespace favicon | 306 } // namespace favicon |
| 298 | 307 |
| 299 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 308 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
| OLD | NEW |