| 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> |
| 11 | 11 |
| 12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
| 13 #include "base/cancelable_callback.h" | 13 #include "base/cancelable_callback.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/task/cancelable_task_tracker.h" | 16 #include "base/task/cancelable_task_tracker.h" |
| 17 #include "components/favicon/core/favicon_driver_observer.h" | 17 #include "components/favicon/core/favicon_driver_observer.h" |
| 18 #include "components/favicon/core/favicon_url.h" | 18 #include "components/favicon/core/favicon_url.h" |
| 19 #include "components/favicon_base/favicon_callback.h" | 19 #include "components/favicon_base/favicon_callback.h" |
| 20 #include "ui/gfx/favicon_size.h" | 20 #include "ui/gfx/favicon_size.h" |
| 21 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 class SkBitmap; | 24 class SkBitmap; |
| 25 | 25 |
| 26 namespace base { |
| 27 struct Feature; |
| 28 } // namespace base |
| 29 |
| 26 namespace favicon { | 30 namespace favicon { |
| 27 | 31 |
| 28 class FaviconService; | 32 class FaviconService; |
| 29 | 33 |
| 34 extern const base::Feature kFaviconsFromWebManifest; |
| 35 |
| 30 // FaviconHandler works with FaviconDriver to fetch the specific type of | 36 // FaviconHandler works with FaviconDriver to fetch the specific type of |
| 31 // favicon. | 37 // favicon. |
| 32 // | 38 // |
| 33 // FetchFavicon requests the favicon from the favicon service which in turn | 39 // FetchFavicon requests the favicon from the favicon service which in turn |
| 34 // requests the favicon from the history database. At this point | 40 // requests the favicon from the history database. At this point |
| 35 // we only know the URL of the page, and not necessarily the url of the | 41 // we only know the URL of the page, and not necessarily the url of the |
| 36 // favicon. To ensure we handle reloading stale favicons as well as | 42 // favicon. To ensure we handle reloading stale favicons as well as |
| 37 // reloading a favicon on page reload we always request the favicon from | 43 // reloading a favicon on page reload we always request the favicon from |
| 38 // history regardless of whether the active favicon is valid. | 44 // history regardless of whether the active favicon is valid. |
| 39 // | 45 // |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 public: | 98 public: |
| 93 // Mimics WebContents::ImageDownloadCallback. | 99 // Mimics WebContents::ImageDownloadCallback. |
| 94 typedef base::Callback<void( | 100 typedef base::Callback<void( |
| 95 int id, | 101 int id, |
| 96 int status_code, | 102 int status_code, |
| 97 const GURL& image_url, | 103 const GURL& image_url, |
| 98 const std::vector<SkBitmap>& bitmaps, | 104 const std::vector<SkBitmap>& bitmaps, |
| 99 const std::vector<gfx::Size>& original_bitmap_sizes)> | 105 const std::vector<gfx::Size>& original_bitmap_sizes)> |
| 100 ImageDownloadCallback; | 106 ImageDownloadCallback; |
| 101 | 107 |
| 108 typedef base::Callback<void( |
| 109 const std::vector<favicon::FaviconURL>& favicons)> |
| 110 ManifestDownloadCallback; |
| 111 |
| 102 // Starts the download for the given favicon. When finished, the callback | 112 // Starts the download for the given favicon. When finished, the callback |
| 103 // is called with the results. Returns the unique id of the download | 113 // is called with the results. Returns the unique id of the download |
| 104 // request, which will also be passed to the callback. In case of error, 0 | 114 // request, which will also be passed to the callback. In case of error, 0 |
| 105 // is returned and no callback will be called. | 115 // is returned and no callback will be called. |
| 106 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out | 116 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out |
| 107 // from the bitmap results. If there are no bitmap results <= | 117 // from the bitmap results. If there are no bitmap results <= |
| 108 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| | 118 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| |
| 109 // and is the only result. A |max_bitmap_size| of 0 means unlimited. | 119 // and is the only result. A |max_bitmap_size| of 0 means unlimited. |
| 110 virtual int DownloadImage(const GURL& url, | 120 virtual int DownloadImage(const GURL& url, |
| 111 int max_image_size, | 121 int max_image_size, |
| 112 ImageDownloadCallback callback) = 0; | 122 ImageDownloadCallback callback) = 0; |
| 113 | 123 |
| 124 // Downloads a WebManifest and returns the favicons listed there. |
| 125 virtual void DownloadManifest(const GURL& url, |
| 126 ManifestDownloadCallback callback) = 0; |
| 127 |
| 114 // Returns whether the user is operating in an off-the-record context. | 128 // Returns whether the user is operating in an off-the-record context. |
| 115 virtual bool IsOffTheRecord() = 0; | 129 virtual bool IsOffTheRecord() = 0; |
| 116 | 130 |
| 117 // Returns whether |url| is bookmarked. | 131 // Returns whether |url| is bookmarked. |
| 118 virtual bool IsBookmarked(const GURL& url) = 0; | 132 virtual bool IsBookmarked(const GURL& url) = 0; |
| 119 | 133 |
| 120 // Notifies that the favicon image has been updated. Most delegates | 134 // Notifies that the favicon image has been updated. Most delegates |
| 121 // propagate the notification to FaviconDriverObserver::OnFaviconUpdated(). | 135 // propagate the notification to FaviconDriverObserver::OnFaviconUpdated(). |
| 122 // See its documentation for details. | 136 // See its documentation for details. |
| 123 virtual void OnFaviconUpdated( | 137 virtual void OnFaviconUpdated( |
| 124 const GURL& page_url, | 138 const GURL& page_url, |
| 125 FaviconDriverObserver::NotificationIconType notification_icon_type, | 139 FaviconDriverObserver::NotificationIconType notification_icon_type, |
| 126 const GURL& icon_url, | 140 const GURL& icon_url, |
| 127 bool icon_url_changed, | 141 bool icon_url_changed, |
| 128 const gfx::Image& image) = 0; | 142 const gfx::Image& image) = 0; |
| 129 }; | 143 }; |
| 130 | 144 |
| 131 // |service| and |delegate| must not be nullptr and must outlive this class. | 145 // |service| and |delegate| must not be nullptr and must outlive this class. |
| 132 FaviconHandler(FaviconService* service, | 146 FaviconHandler(FaviconService* service, |
| 133 Delegate* delegate, | 147 Delegate* delegate, |
| 134 FaviconDriverObserver::NotificationIconType handler_type); | 148 FaviconDriverObserver::NotificationIconType handler_type); |
| 135 ~FaviconHandler(); | 149 ~FaviconHandler(); |
| 136 | 150 |
| 137 // Initiates loading the favicon for the specified url. | 151 // Initiates loading the favicon for the specified url. |
| 138 void FetchFavicon(const GURL& url); | 152 void FetchFavicon(const GURL& url); |
| 139 | 153 |
| 140 // Message Handler. Must be public, because also called from | 154 // Collects the candidate favicons as listed in the HTML head, as well as |
| 141 // PrerenderContents. Collects the |image_urls| list. | 155 // the WebManifest URL if available (or empty URL otherwise). |
| 142 void OnUpdateCandidates(const GURL& page_url, | 156 void OnUpdateCandidates(const GURL& page_url, |
| 143 const std::vector<favicon::FaviconURL>& candidates); | 157 const std::vector<favicon::FaviconURL>& candidates, |
| 158 const GURL& manifest_url); |
| 159 |
| 160 // Returns the supported icon types, inferred from the handler type as passed |
| 161 // in the constructor. |
| 162 int icon_types() const { return icon_types_; } |
| 144 | 163 |
| 145 // For testing. | 164 // For testing. |
| 146 const std::vector<GURL> GetIconURLs() const; | 165 const std::vector<GURL> GetIconURLs() const; |
| 147 | 166 |
| 148 // Returns whether the handler is waiting for a download to complete or for | 167 // Returns whether the handler is waiting for a download to complete or for |
| 149 // data from the FaviconService. Reserved for testing. | 168 // data from the FaviconService. Reserved for testing. |
| 150 bool HasPendingTasksForTest(); | 169 bool HasPendingTasksForTest(); |
| 151 | 170 |
| 152 // Get the maximal icon size in pixels for a handler of type |handler_type|. | 171 // Get the maximal icon size in pixels for a handler of type |handler_type|. |
| 153 static int GetMaximalIconSize( | 172 static int GetMaximalIconSize( |
| 154 FaviconDriverObserver::NotificationIconType handler_type); | 173 FaviconDriverObserver::NotificationIconType handler_type); |
| 155 | 174 |
| 156 private: | 175 private: |
| 157 // Used to track a candidate for the favicon. | 176 // Used to track a candidate for the favicon. |
| 158 struct FaviconCandidate { | 177 struct FaviconCandidate { |
| 159 // Builds a scored candidate by selecting the best bitmap sizes. | 178 // Builds a scored candidate by selecting the best bitmap sizes. |
| 160 static FaviconCandidate FromFaviconURL( | 179 static FaviconCandidate FromFaviconURL( |
| 161 const favicon::FaviconURL& favicon_url, | 180 const favicon::FaviconURL& favicon_url, |
| 162 const std::vector<int>& desired_pixel_sizes); | 181 const std::vector<int>& desired_pixel_sizes); |
| 163 | 182 |
| 164 friend bool operator==(const FaviconCandidate& lhs, | |
| 165 const FaviconCandidate& rhs) { | |
| 166 return lhs.icon_url == rhs.icon_url && lhs.icon_type == rhs.icon_type && | |
| 167 lhs.score == rhs.score; | |
| 168 } | |
| 169 | |
| 170 // Compare function used for std::stable_sort to sort in descending order. | 183 // Compare function used for std::stable_sort to sort in descending order. |
| 171 static bool CompareScore(const FaviconCandidate& lhs, | 184 static bool CompareScore(const FaviconCandidate& lhs, |
| 172 const FaviconCandidate& rhs) { | 185 const FaviconCandidate& rhs) { |
| 173 return lhs.score > rhs.score; | 186 return lhs.score > rhs.score; |
| 174 } | 187 } |
| 175 | 188 |
| 176 GURL icon_url; | 189 GURL icon_url; |
| 177 favicon_base::IconType icon_type = favicon_base::INVALID_ICON; | 190 favicon_base::IconType icon_type = favicon_base::INVALID_ICON; |
| 178 float score = 0; | 191 float score = 0; |
| 179 }; | 192 }; |
| 180 | 193 |
| 181 struct DownloadedFavicon { | 194 struct DownloadedFavicon { |
| 182 FaviconCandidate candidate; | 195 FaviconCandidate candidate; |
| 183 gfx::Image image; | 196 gfx::Image image; |
| 184 }; | 197 }; |
| 185 | 198 |
| 186 // Returns the bit mask of favicon_base::IconType based on the handler's type. | 199 // Returns the bit mask of favicon_base::IconType based on the handler's type. |
| 187 static int GetIconTypesFromHandlerType( | 200 static int GetIconTypesFromHandlerType( |
| 188 FaviconDriverObserver::NotificationIconType handler_type); | 201 FaviconDriverObserver::NotificationIconType handler_type); |
| 189 | 202 |
| 203 // Called with the result of looking up cached icon data for the manifest's |
| 204 // URL, which is used as icon URL. |
| 205 void OnFaviconDataForManifestFromFaviconService( |
| 206 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 207 favicon_bitmap_results); |
| 208 |
| 209 // Called when the dowloading of a manifest completes. |
| 210 void OnDidDownloadManifest(const std::vector<FaviconURL>& candidates); |
| 211 |
| 212 // Called when the actual list of favicon candidates to be processed is |
| 213 // available, which can be either icon URLs listed in the HTML head instead |
| 214 // or, if a Web Manifest was provided, the list of icons there. |
| 215 void OnGotFinalIconURLCandidates(const std::vector<FaviconURL>& candidates); |
| 216 |
| 190 // Called when the history request for favicon data mapped to |url_| has | 217 // Called when the history request for favicon data mapped to |url_| has |
| 191 // completed and the renderer has told us the icon URLs used by |url_| | 218 // completed and the renderer has told us the icon URLs used by |url_| |
| 192 void OnGotInitialHistoryDataAndIconURLCandidates(); | 219 void OnGotInitialHistoryDataAndIconURLCandidates(); |
| 193 | 220 |
| 194 // See description above class for details. | 221 // See description above class for details. |
| 195 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< | 222 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< |
| 196 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); | 223 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); |
| 197 | 224 |
| 198 // If the favicon currently mapped to |url_| has expired, downloads the | 225 // If the favicon currently mapped to |url_| has expired, downloads the |
| 199 // current candidate favicon from the renderer. Otherwise requests data for | 226 // current candidate favicon from the renderer. Otherwise requests data for |
| 200 // the current favicon from history. If data is requested from history, | 227 // the current favicon from history. If data is requested from history, |
| 201 // OnFaviconData() is called with the history data once it has been retrieved. | 228 // OnFaviconData() is called with the history data once it has been retrieved. |
| 202 void DownloadCurrentCandidateOrAskFaviconService(); | 229 void DownloadCurrentCandidateOrAskFaviconService(); |
| 203 | 230 |
| 231 // Requests the favicon for |icon_url| from the favicon service. Unless in |
| 232 // incognito, it also updates the page URL (url_) to |icon_url| mappings. |
| 233 void GetFaviconAndUpdateMappingsUnlessIncognito( |
| 234 const GURL& icon_url, |
| 235 favicon_base::IconType icon_type, |
| 236 const favicon_base::FaviconResultsCallback& callback); |
| 237 |
| 204 // See description above class for details. | 238 // See description above class for details. |
| 205 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& | 239 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 206 favicon_bitmap_results); | 240 favicon_bitmap_results); |
| 207 | 241 |
| 208 // Schedules a download for the specified entry. This adds the request to | 242 // Schedules a download for the specified entry. This adds the request to |
| 209 // image_download_requests_. | 243 // image_download_requests_. |
| 210 void ScheduleImageDownload(const GURL& image_url, | 244 void ScheduleImageDownload(const GURL& image_url, |
| 211 favicon_base::IconType icon_type); | 245 favicon_base::IconType icon_type); |
| 212 | 246 |
| 213 // Triggered when a download of an image has finished. | 247 // Triggered when a download of an image has finished. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 // to be incomplete. If true, it means there is a favicon mapped to |url_| in | 310 // to be incomplete. If true, it means there is a favicon mapped to |url_| in |
| 277 // history, but we need to redownload the icon URLs because the icon in the | 311 // history, but we need to redownload the icon URLs because the icon in the |
| 278 // database has expired or the data in the database is incomplete. | 312 // database has expired or the data in the database is incomplete. |
| 279 bool initial_history_result_expired_or_incomplete_; | 313 bool initial_history_result_expired_or_incomplete_; |
| 280 | 314 |
| 281 // Whether FaviconHandler should ignore history state and determine the | 315 // Whether FaviconHandler should ignore history state and determine the |
| 282 // optimal icon URL out of |image_urls_| for |url_| by downloading | 316 // optimal icon URL out of |image_urls_| for |url_| by downloading |
| 283 // |image_urls_| one by one. | 317 // |image_urls_| one by one. |
| 284 bool redownload_icons_; | 318 bool redownload_icons_; |
| 285 | 319 |
| 320 // Requests to the renderer to download a manifest. |
| 321 base::CancelableCallback<Delegate::ManifestDownloadCallback::RunType> |
| 322 manifest_download_request_; |
| 323 |
| 286 // Requests to the renderer to download favicons. | 324 // Requests to the renderer to download favicons. |
| 287 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> | 325 base::CancelableCallback<Delegate::ImageDownloadCallback::RunType> |
| 288 image_download_request_; | 326 image_download_request_; |
| 289 | 327 |
| 290 // The combination of the supported icon types. | 328 // The combination of the supported icon types. |
| 291 const int icon_types_; | 329 const int icon_types_; |
| 292 | 330 |
| 293 // Whether the largest icon should be downloaded. | 331 // Whether the largest icon should be downloaded. |
| 294 const bool download_largest_icon_; | 332 const bool download_largest_icon_; |
| 295 | 333 |
| 334 // The manifest URL from the renderer (or empty URL if none). |
| 335 GURL manifest_url_; |
| 336 |
| 337 // Original list of candidates provided to OnUpdateCandidates(), stored to |
| 338 // be able to fall back to, in case a manifest was provided and downloading it |
| 339 // failed (or provided no icons). |
| 340 std::vector<FaviconURL> non_manifest_original_candidates_; |
| 341 |
| 296 // The prioritized favicon candidates from the page back from the renderer. | 342 // The prioritized favicon candidates from the page back from the renderer. |
| 297 std::vector<FaviconCandidate> candidates_; | 343 std::vector<FaviconCandidate> candidates_; |
| 298 | 344 |
| 299 // The icon URL and the icon type of the favicon in the most recent | 345 // The icon URL and the icon type of the favicon in the most recent |
| 300 // FaviconDriver::OnFaviconAvailable() notification. | 346 // FaviconDriver::OnFaviconAvailable() notification. |
| 301 GURL notification_icon_url_; | 347 GURL notification_icon_url_; |
| 302 favicon_base::IconType notification_icon_type_; | 348 favicon_base::IconType notification_icon_type_; |
| 303 | 349 |
| 304 // The FaviconService which implements favicon operations. May be null during | 350 // The FaviconService which implements favicon operations. May be null during |
| 305 // testing. | 351 // testing. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 321 // UpdateFaviconCandidate()), the favicon service and the delegate are | 367 // UpdateFaviconCandidate()), the favicon service and the delegate are |
| 322 // notified. | 368 // notified. |
| 323 DownloadedFavicon best_favicon_; | 369 DownloadedFavicon best_favicon_; |
| 324 | 370 |
| 325 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 371 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
| 326 }; | 372 }; |
| 327 | 373 |
| 328 } // namespace favicon | 374 } // namespace favicon |
| 329 | 375 |
| 330 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 376 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
| OLD | NEW |