| 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 <map> | 10 #include <map> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.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/memory/weak_ptr.h" |
| 16 #include "base/task/cancelable_task_tracker.h" | 17 #include "base/task/cancelable_task_tracker.h" |
| 17 #include "components/favicon/core/favicon_driver_observer.h" | 18 #include "components/favicon/core/favicon_driver_observer.h" |
| 18 #include "components/favicon/core/favicon_url.h" | 19 #include "components/favicon/core/favicon_url.h" |
| 19 #include "components/favicon_base/favicon_callback.h" | 20 #include "components/favicon_base/favicon_callback.h" |
| 20 #include "ui/gfx/favicon_size.h" | 21 #include "ui/gfx/favicon_size.h" |
| 21 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 class SkBitmap; | 25 class SkBitmap; |
| 25 | 26 |
| 26 namespace favicon { | 27 namespace favicon { |
| 27 | 28 |
| 28 class FaviconDriver; | |
| 29 class FaviconService; | 29 class FaviconService; |
| 30 class TestFaviconHandler; | 30 class TestFaviconHandler; |
| 31 | 31 |
| 32 // FaviconHandler works with FaviconDriver to fetch the specific type of | 32 // FaviconHandler works with FaviconDriver to fetch the specific type of |
| 33 // favicon. | 33 // favicon. |
| 34 // | 34 // |
| 35 // FetchFavicon requests the favicon from the favicon service which in turn | 35 // FetchFavicon requests the favicon from the favicon service which in turn |
| 36 // requests the favicon from the history database. At this point | 36 // requests the favicon from the history database. At this point |
| 37 // we only know the URL of the page, and not necessarily the url of the | 37 // we only know the URL of the page, and not necessarily the url of the |
| 38 // favicon. To ensure we handle reloading stale favicons as well as | 38 // favicon. To ensure we handle reloading stale favicons as well as |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // | 70 // |
| 71 // When the renderer downloads favicons, it considers the entire list of | 71 // When the renderer downloads favicons, it considers the entire list of |
| 72 // favicon candidates, if |download_largest_favicon_| is true, the largest | 72 // favicon candidates, if |download_largest_favicon_| is true, the largest |
| 73 // favicon will be used, otherwise the one that best matches the preferred size | 73 // favicon will be used, otherwise the one that best matches the preferred size |
| 74 // is chosen (or the first one if there is no preferred size). Once the | 74 // is chosen (or the first one if there is no preferred size). Once the |
| 75 // matching favicon has been determined, SetFavicon is called which updates | 75 // matching favicon has been determined, SetFavicon is called which updates |
| 76 // the page's favicon and notifies the database to save the favicon. | 76 // the page's favicon and notifies the database to save the favicon. |
| 77 | 77 |
| 78 class FaviconHandler { | 78 class FaviconHandler { |
| 79 public: | 79 public: |
| 80 class Delegate { |
| 81 public: |
| 82 // Mimics WebContents::ImageDownloadCallback. |
| 83 typedef base::Callback<void( |
| 84 int id, |
| 85 int status_code, |
| 86 const GURL& image_url, |
| 87 const std::vector<SkBitmap>& bitmaps, |
| 88 const std::vector<gfx::Size>& original_bitmap_sizes)> |
| 89 ImageDownloadCallback; |
| 90 |
| 91 // Starts the download for the given favicon. When finished, the callback |
| 92 // is called with the results. Returns the unique id of the download |
| 93 // request, which will also be passed to the callback. In case of error, 0 |
| 94 // is returned and no callback will be called. |
| 95 // Bitmaps with pixel sizes larger than |max_bitmap_size| are filtered out |
| 96 // from the bitmap results. If there are no bitmap results <= |
| 97 // |max_bitmap_size|, the smallest bitmap is resized to |max_bitmap_size| |
| 98 // and is the only result. A |max_bitmap_size| of 0 means unlimited. |
| 99 virtual int DownloadImage(const GURL& url, |
| 100 int max_image_size, |
| 101 ImageDownloadCallback callback) = 0; |
| 102 |
| 103 // Returns whether the user is operating in an off-the-record context. |
| 104 virtual bool IsOffTheRecord() = 0; |
| 105 |
| 106 // Notifies that the favicon image has been updated. |
| 107 // See comment for FaviconDriverObserver::OnFaviconUpdated() for more |
| 108 // details. |
| 109 virtual void OnFaviconUpdated( |
| 110 const GURL& page_url, |
| 111 FaviconDriverObserver::NotificationIconType notification_icon_type, |
| 112 const GURL& icon_url, |
| 113 bool icon_url_changed, |
| 114 const gfx::Image& image) = 0; |
| 115 }; |
| 116 |
| 117 // |service| and |delegate| must not be nullptr and must outlive this object. |
| 80 FaviconHandler(FaviconService* service, | 118 FaviconHandler(FaviconService* service, |
| 81 FaviconDriver* driver, | 119 Delegate* delegate, |
| 82 FaviconDriverObserver::NotificationIconType handler_type); | 120 FaviconDriverObserver::NotificationIconType handler_type); |
| 121 // TODO(mastiz): Remove subclasses and instead inject a FaviconService, |
| 122 // mocked or facked. |
| 83 virtual ~FaviconHandler(); | 123 virtual ~FaviconHandler(); |
| 84 | 124 |
| 85 // Returns the bit mask of favicon_base::IconType based on the handler's type. | |
| 86 static int GetIconTypesFromHandlerType( | |
| 87 FaviconDriverObserver::NotificationIconType handler_type); | |
| 88 | |
| 89 // Initiates loading the favicon for the specified url. | 125 // Initiates loading the favicon for the specified url. |
| 90 void FetchFavicon(const GURL& url); | 126 void FetchFavicon(const GURL& url); |
| 91 | 127 |
| 92 // Message Handler. Must be public, because also called from | 128 // Message Handler. Must be public, because also called from |
| 93 // PrerenderContents. Collects the |image_urls| list. | 129 // PrerenderContents. Collects the |image_urls| list. |
| 94 void OnUpdateFaviconURL(const GURL& page_url, | 130 void OnUpdateFaviconURL(const GURL& page_url, |
| 95 const std::vector<favicon::FaviconURL>& candidates); | 131 const std::vector<favicon::FaviconURL>& candidates); |
| 96 | 132 |
| 97 // Called when the history request for favicon data mapped to |url_| has | |
| 98 // completed and the renderer has told us the icon URLs used by |url_| | |
| 99 void OnGotInitialHistoryDataAndIconURLCandidates(); | |
| 100 | |
| 101 // Message handler for ImageHostMsg_DidDownloadImage. Called when the image | |
| 102 // at |image_url| has been downloaded. | |
| 103 // |bitmaps| is a list of all the frames of the image at |image_url|. | |
| 104 // |original_bitmap_sizes| are the sizes of |bitmaps| before they were resized | |
| 105 // to the maximum bitmap size passed to DownloadFavicon(). | |
| 106 void OnDidDownloadFavicon( | |
| 107 int id, | |
| 108 const GURL& image_url, | |
| 109 const std::vector<SkBitmap>& bitmaps, | |
| 110 const std::vector<gfx::Size>& original_bitmap_sizes); | |
| 111 | |
| 112 // For testing. | 133 // For testing. |
| 113 const std::vector<favicon::FaviconURL>& image_urls() const { | 134 const std::vector<favicon::FaviconURL>& image_urls() const { |
| 114 return image_urls_; | 135 return image_urls_; |
| 115 } | 136 } |
| 116 | 137 |
| 117 // Returns whether the handler is waiting for a download to complete or for | 138 // Returns whether the handler is waiting for a download to complete or for |
| 118 // data from the FaviconService. Reserved for testing. | 139 // data from the FaviconService. Reserved for testing. |
| 119 bool HasPendingTasksForTest(); | 140 bool HasPendingTasksForTest(); |
| 120 | 141 |
| 121 protected: | 142 protected: |
| 122 // These virtual methods make FaviconHandler testable and are overridden by | 143 // These virtual methods make FaviconHandler testable and are overridden by |
| 123 // TestFaviconHandler. | 144 // TestFaviconHandler. |
| 124 | 145 |
| 125 // Asks the render to download favicon, returns the request id. | |
| 126 virtual int DownloadFavicon(const GURL& image_url, int max_bitmap_size); | |
| 127 | |
| 128 // Ask the favicon from history | 146 // Ask the favicon from history |
| 129 virtual void UpdateFaviconMappingAndFetch( | 147 virtual void UpdateFaviconMappingAndFetch( |
| 130 const GURL& page_url, | 148 const GURL& page_url, |
| 131 const GURL& icon_url, | 149 const GURL& icon_url, |
| 132 favicon_base::IconType icon_type, | 150 favicon_base::IconType icon_type, |
| 133 const favicon_base::FaviconResultsCallback& callback, | 151 const favicon_base::FaviconResultsCallback& callback, |
| 134 base::CancelableTaskTracker* tracker); | 152 base::CancelableTaskTracker* tracker); |
| 135 | 153 |
| 136 virtual void GetFaviconFromFaviconService( | 154 virtual void GetFaviconFromFaviconService( |
| 137 const GURL& icon_url, | 155 const GURL& icon_url, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 const gfx::Image& image, | 195 const gfx::Image& image, |
| 178 float score, | 196 float score, |
| 179 favicon_base::IconType icon_type); | 197 favicon_base::IconType icon_type); |
| 180 | 198 |
| 181 GURL image_url; | 199 GURL image_url; |
| 182 gfx::Image image; | 200 gfx::Image image; |
| 183 float score; | 201 float score; |
| 184 favicon_base::IconType icon_type; | 202 favicon_base::IconType icon_type; |
| 185 }; | 203 }; |
| 186 | 204 |
| 205 // Returns the bit mask of favicon_base::IconType based on the handler's type. |
| 206 static int GetIconTypesFromHandlerType( |
| 207 FaviconDriverObserver::NotificationIconType handler_type); |
| 208 |
| 187 // Get the maximal icon size in pixels for a icon of type |icon_type| for the | 209 // Get the maximal icon size in pixels for a icon of type |icon_type| for the |
| 188 // current platform. | 210 // current platform. |
| 189 static int GetMaximalIconSize(favicon_base::IconType icon_type); | 211 static int GetMaximalIconSize(favicon_base::IconType icon_type); |
| 190 | 212 |
| 213 // Called when the history request for favicon data mapped to |url_| has |
| 214 // completed and the renderer has told us the icon URLs used by |url_| |
| 215 void OnGotInitialHistoryDataAndIconURLCandidates(); |
| 216 |
| 191 // See description above class for details. | 217 // See description above class for details. |
| 192 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< | 218 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< |
| 193 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); | 219 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); |
| 194 | 220 |
| 195 // If the favicon currently mapped to |url_| has expired, downloads the | 221 // If the favicon currently mapped to |url_| has expired, downloads the |
| 196 // current candidate favicon from the renderer. Otherwise requests data for | 222 // current candidate favicon from the renderer. Otherwise requests data for |
| 197 // the current favicon from history. If data is requested from history, | 223 // the current favicon from history. If data is requested from history, |
| 198 // OnFaviconData() is called with the history data once it has been retrieved. | 224 // OnFaviconData() is called with the history data once it has been retrieved. |
| 199 void DownloadCurrentCandidateOrAskFaviconService(); | 225 void DownloadCurrentCandidateOrAskFaviconService(); |
| 200 | 226 |
| 201 // See description above class for details. | 227 // See description above class for details. |
| 202 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& | 228 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 203 favicon_bitmap_results); | 229 favicon_bitmap_results); |
| 204 | 230 |
| 205 // Schedules a download for the specified entry. This adds the request to | 231 // Schedules a download for the specified entry. This adds the request to |
| 206 // download_requests_. | 232 // download_requests_. |
| 207 void ScheduleDownload(const GURL& image_url, | 233 void ScheduleDownload(const GURL& image_url, |
| 208 favicon_base::IconType icon_type); | 234 favicon_base::IconType icon_type); |
| 209 | 235 |
| 236 // Triggered when a download of an image has finished. |
| 237 void DidDownloadFavicon(int id, |
| 238 int http_status_code, |
| 239 const GURL& image_url, |
| 240 const std::vector<SkBitmap>& bitmaps, |
| 241 const std::vector<gfx::Size>& original_bitmap_sizes); |
| 242 |
| 210 // Updates |favicon_candidate_| and returns true if it is an exact match. | 243 // Updates |favicon_candidate_| and returns true if it is an exact match. |
| 211 bool UpdateFaviconCandidate(const GURL& image_url, | 244 bool UpdateFaviconCandidate(const GURL& image_url, |
| 212 const gfx::Image& image, | 245 const gfx::Image& image, |
| 213 float score, | 246 float score, |
| 214 favicon_base::IconType icon_type); | 247 favicon_base::IconType icon_type); |
| 215 | 248 |
| 216 // Sets the image data for the favicon. | 249 // Sets the image data for the favicon. |
| 217 void SetFavicon(const GURL& icon_url, | 250 void SetFavicon(const GURL& icon_url, |
| 218 const gfx::Image& image, | 251 const gfx::Image& image, |
| 219 favicon_base::IconType icon_type); | 252 favicon_base::IconType icon_type); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 | 314 |
| 282 // The icon URL and the icon type of the favicon in the most recent | 315 // The icon URL and the icon type of the favicon in the most recent |
| 283 // FaviconDriver::OnFaviconAvailable() notification. | 316 // FaviconDriver::OnFaviconAvailable() notification. |
| 284 GURL notification_icon_url_; | 317 GURL notification_icon_url_; |
| 285 favicon_base::IconType notification_icon_type_; | 318 favicon_base::IconType notification_icon_type_; |
| 286 | 319 |
| 287 // The FaviconService which implements favicon operations. May be null during | 320 // The FaviconService which implements favicon operations. May be null during |
| 288 // testing. | 321 // testing. |
| 289 FaviconService* service_; | 322 FaviconService* service_; |
| 290 | 323 |
| 291 // This handler's driver, owns this object. | 324 // This handler's delegate. |
| 292 FaviconDriver* driver_; | 325 Delegate* delegate_; |
| 293 | 326 |
| 294 // The index of the favicon URL in |image_urls_| which is currently being | 327 // The index of the favicon URL in |image_urls_| which is currently being |
| 295 // requested from history or downloaded. | 328 // requested from history or downloaded. |
| 296 size_t current_candidate_index_; | 329 size_t current_candidate_index_; |
| 297 | 330 |
| 298 // Best image we've seen so far. As images are downloaded from the page they | 331 // Best image we've seen so far. As images are downloaded from the page they |
| 299 // are stored here. When there is an exact match, or no more images are | 332 // are stored here. When there is an exact match, or no more images are |
| 300 // available the favicon service and the current page are updated (assuming | 333 // available the favicon service and the current page are updated (assuming |
| 301 // the image is for a favicon). | 334 // the image is for a favicon). |
| 302 FaviconCandidate best_favicon_candidate_; | 335 FaviconCandidate best_favicon_candidate_; |
| 303 | 336 |
| 337 base::WeakPtrFactory<FaviconHandler> weak_ptr_factory_; |
| 338 |
| 304 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 339 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
| 305 }; | 340 }; |
| 306 | 341 |
| 307 } // namespace favicon | 342 } // namespace favicon |
| 308 | 343 |
| 309 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 344 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
| OLD | NEW |