| 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/memory/weak_ptr.h" |
| 17 #include "base/optional.h" |
| 17 #include "base/task/cancelable_task_tracker.h" | 18 #include "base/task/cancelable_task_tracker.h" |
| 18 #include "components/favicon/core/favicon_driver_observer.h" | 19 #include "components/favicon/core/favicon_driver_observer.h" |
| 20 #include "components/favicon/core/favicon_selector.h" |
| 19 #include "components/favicon/core/favicon_url.h" | 21 #include "components/favicon/core/favicon_url.h" |
| 20 #include "components/favicon_base/favicon_callback.h" | 22 #include "components/favicon_base/favicon_callback.h" |
| 21 #include "ui/gfx/favicon_size.h" | 23 #include "ui/gfx/favicon_size.h" |
| 22 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
| 23 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 24 | 26 |
| 25 class SkBitmap; | 27 class SkBitmap; |
| 26 | 28 |
| 27 namespace favicon { | 29 namespace favicon { |
| 28 | 30 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 // . Otherwise we ask the history database to update the mapping from | 63 // . Otherwise we ask the history database to update the mapping from |
| 62 // page url to favicon url and call us back with the favicon. Remember, it is | 64 // page url to favicon url and call us back with the favicon. Remember, it is |
| 63 // possible for the db to already have the favicon, just not the mapping | 65 // possible for the db to already have the favicon, just not the mapping |
| 64 // between page to favicon url. The callback for this is OnFaviconData. | 66 // between page to favicon url. The callback for this is OnFaviconData. |
| 65 // | 67 // |
| 66 // OnFaviconData either updates the favicon of the current page (if the | 68 // OnFaviconData either updates the favicon of the current page (if the |
| 67 // db knew about the favicon), or requests the renderer to download the | 69 // db knew about the favicon), or requests the renderer to download the |
| 68 // favicon. | 70 // favicon. |
| 69 // | 71 // |
| 70 // When the renderer downloads favicons, it considers the entire list of | 72 // When the renderer downloads favicons, it considers the entire list of |
| 71 // favicon candidates, if |download_largest_favicon_| is true, the largest | 73 // favicon candidates. Among those, the one that matches |target_spec_| |
| 72 // favicon will be used, otherwise the one that best matches the preferred size | 74 // is chosen first. Once the matching favicon has been determined, SetFavicon |
| 73 // is chosen (or the first one if there is no preferred size). Once the | 75 // is called which updates the page's favicon and notifies the database to save |
| 74 // matching favicon has been determined, SetFavicon is called which updates | 76 // the favicon. |
| 75 // the page's favicon and notifies the database to save the favicon. | |
| 76 | 77 |
| 77 class FaviconHandler { | 78 class FaviconHandler { |
| 78 public: | 79 public: |
| 79 class Delegate { | 80 class Delegate { |
| 80 public: | 81 public: |
| 81 // Mimics WebContents::ImageDownloadCallback. | 82 // Mimics WebContents::ImageDownloadCallback. |
| 82 typedef base::Callback<void( | 83 typedef base::Callback<void( |
| 83 int id, | 84 int id, |
| 84 int status_code, | 85 int status_code, |
| 85 const GURL& image_url, | 86 const GURL& image_url, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 ~FaviconHandler(); | 124 ~FaviconHandler(); |
| 124 | 125 |
| 125 // Initiates loading the favicon for the specified url. | 126 // Initiates loading the favicon for the specified url. |
| 126 void FetchFavicon(const GURL& url); | 127 void FetchFavicon(const GURL& url); |
| 127 | 128 |
| 128 // Message Handler. Must be public, because also called from | 129 // Message Handler. Must be public, because also called from |
| 129 // PrerenderContents. Collects the |image_urls| list. | 130 // PrerenderContents. Collects the |image_urls| list. |
| 130 void OnUpdateFaviconURL(const GURL& page_url, | 131 void OnUpdateFaviconURL(const GURL& page_url, |
| 131 const std::vector<favicon::FaviconURL>& candidates); | 132 const std::vector<favicon::FaviconURL>& candidates); |
| 132 | 133 |
| 133 // For testing. | |
| 134 const std::vector<favicon::FaviconURL>& image_urls() const { | |
| 135 return image_urls_; | |
| 136 } | |
| 137 | |
| 138 // Returns whether the handler is waiting for a download to complete or for | 134 // Returns whether the handler is waiting for a download to complete or for |
| 139 // data from the FaviconService. Reserved for testing. | 135 // data from the FaviconService. Reserved for testing. |
| 140 bool HasPendingTasksForTest(); | 136 bool HasPendingTasksForTest(); |
| 141 | 137 |
| 142 // Get the maximal icon size in pixels for a icon of type |icon_type| for the | |
| 143 // current platform. | |
| 144 static int GetMaximalIconSize(favicon_base::IconType icon_type); | |
| 145 | |
| 146 private: | 138 private: |
| 147 // Represents an in progress download of an image from the renderer. | 139 // Represents an in progress download of an image from the renderer. |
| 148 struct DownloadRequest { | 140 struct DownloadRequest { |
| 149 DownloadRequest(); | 141 DownloadRequest(); |
| 150 ~DownloadRequest(); | 142 ~DownloadRequest(); |
| 151 | 143 |
| 152 DownloadRequest(const GURL& image_url, favicon_base::IconType icon_type); | 144 DownloadRequest(const GURL& image_url, favicon_base::IconType icon_type); |
| 153 | 145 |
| 154 GURL image_url; | 146 GURL image_url; |
| 155 favicon_base::IconType icon_type; | 147 favicon_base::IconType icon_type; |
| 156 }; | 148 }; |
| 157 | 149 |
| 158 // Used to track a candidate for the favicon. | |
| 159 struct FaviconCandidate { | |
| 160 FaviconCandidate(); | |
| 161 ~FaviconCandidate(); | |
| 162 | |
| 163 FaviconCandidate(const GURL& image_url, | |
| 164 const gfx::Image& image, | |
| 165 float score, | |
| 166 favicon_base::IconType icon_type); | |
| 167 | |
| 168 GURL image_url; | |
| 169 gfx::Image image; | |
| 170 float score; | |
| 171 favicon_base::IconType icon_type; | |
| 172 }; | |
| 173 | |
| 174 // Returns the bit mask of favicon_base::IconType based on the handler's type. | 150 // Returns the bit mask of favicon_base::IconType based on the handler's type. |
| 175 static int GetIconTypesFromHandlerType( | 151 static int GetIconTypesFromHandlerType( |
| 176 FaviconDriverObserver::NotificationIconType handler_type); | 152 FaviconDriverObserver::NotificationIconType handler_type); |
| 177 | 153 |
| 178 // Called when the history request for favicon data mapped to |url_| has | 154 // Called when the history request for favicon data mapped to |url_| has |
| 179 // completed and the renderer has told us the icon URLs used by |url_| | 155 // completed and the renderer has told us the icon URLs used by |url_| |
| 180 void OnGotInitialHistoryDataAndIconURLCandidates(); | 156 void OnGotInitialHistoryDataAndIconURLCandidates(); |
| 181 | 157 |
| 182 // See description above class for details. | 158 // See description above class for details. |
| 183 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< | 159 void OnFaviconDataForInitialURLFromFaviconService(const std::vector< |
| 184 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); | 160 favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results); |
| 185 | 161 |
| 162 // Deques the next candidate to |current_candidate_| and starts processing it. |
| 186 // If the favicon currently mapped to |url_| has expired, downloads the | 163 // If the favicon currently mapped to |url_| has expired, downloads the |
| 187 // current candidate favicon from the renderer. Otherwise requests data for | 164 // current candidate favicon from the renderer. Otherwise requests data for |
| 188 // the current favicon from history. If data is requested from history, | 165 // the current favicon from history. If data is requested from history, |
| 189 // OnFaviconData() is called with the history data once it has been retrieved. | 166 // OnFaviconData() is called with the history data once it has been retrieved. |
| 190 void DownloadCurrentCandidateOrAskFaviconService(); | 167 void DownloadNextCandidateOrAskFaviconService(); |
| 191 | 168 |
| 192 // See description above class for details. | 169 // See description above class for details. |
| 193 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& | 170 void OnFaviconData(const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 194 favicon_bitmap_results); | 171 favicon_bitmap_results); |
| 195 | 172 |
| 196 // Schedules a download for the specified entry. This adds the request to | 173 // Schedules a download for the specified entry. This adds the request to |
| 197 // download_requests_. | 174 // download_requests_. |
| 198 void ScheduleDownload(const GURL& image_url, | 175 void ScheduleDownload(const GURL& image_url, |
| 199 favicon_base::IconType icon_type); | 176 favicon_base::IconType icon_type); |
| 200 | 177 |
| 201 // Triggered when a download of an image has finished. | 178 // Triggered when a download of an image has finished. |
| 202 void OnDidDownloadFavicon( | 179 void OnDidDownloadFavicon( |
| 203 int id, | 180 int id, |
| 204 int http_status_code, | 181 int http_status_code, |
| 205 const GURL& image_url, | 182 const GURL& image_url, |
| 206 const std::vector<SkBitmap>& bitmaps, | 183 const std::vector<SkBitmap>& bitmaps, |
| 207 const std::vector<gfx::Size>& original_bitmap_sizes); | 184 const std::vector<gfx::Size>& original_bitmap_sizes); |
| 208 | 185 |
| 209 bool ShouldSaveFavicon(); | 186 bool ShouldSaveFavicon(); |
| 210 | 187 |
| 211 // Updates |favicon_candidate_| and returns true if it is an exact match. | 188 // Updates candidate queues and returns true if no more candidates should be |
| 189 // processed (e.g. an exact match was found). |
| 212 bool UpdateFaviconCandidate(const GURL& image_url, | 190 bool UpdateFaviconCandidate(const GURL& image_url, |
| 213 const gfx::Image& image, | 191 const gfx::Image& image, |
| 214 float score, | 192 float score, |
| 215 favicon_base::IconType icon_type); | 193 favicon_base::IconType icon_type); |
| 216 | 194 |
| 217 // Sets the image data for the favicon. | 195 // Sets the image data for the favicon. |
| 218 void SetFavicon(const GURL& icon_url, | 196 void SetFavicon(const GURL& icon_url, |
| 219 const gfx::Image& image, | 197 const gfx::Image& image, |
| 220 favicon_base::IconType icon_type); | 198 favicon_base::IconType icon_type); |
| 221 | 199 |
| 222 // Notifies |driver_| that FaviconHandler found an icon which matches the | 200 // Notifies |driver_| that FaviconHandler found an icon which matches the |
| 223 // |handler_type_| criteria. NotifyFaviconUpdated() can be called multiple | 201 // |handler_type_| criteria. NotifyFaviconUpdated() can be called multiple |
| 224 // times for the same page if: | 202 // times for the same page if: |
| 225 // - a better match is found for |handler_type_| (e.g. newer bitmap data) | 203 // - a better match is found for |handler_type_| (e.g. newer bitmap data) |
| 226 // - Javascript changes the page's icon URLs. | 204 // - Javascript changes the page's icon URLs. |
| 227 void NotifyFaviconUpdated( | 205 void NotifyFaviconUpdated( |
| 228 const std::vector<favicon_base::FaviconRawBitmapResult>& | 206 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 229 favicon_bitmap_results); | 207 favicon_bitmap_results); |
| 230 void NotifyFaviconUpdated(const GURL& icon_url, | 208 void NotifyFaviconUpdated(const GURL& icon_url, |
| 231 favicon_base::IconType icon_type, | 209 favicon_base::IconType icon_type, |
| 232 const gfx::Image& image); | 210 const gfx::Image& image); |
| 233 | 211 |
| 234 // Return the current candidate if any. | |
| 235 favicon::FaviconURL* current_candidate() { | |
| 236 return current_candidate_index_ < image_urls_.size() | |
| 237 ? &image_urls_[current_candidate_index_] | |
| 238 : nullptr; | |
| 239 } | |
| 240 | |
| 241 // Returns the preferred size of the image. 0 means no preference (any size | 212 // Returns the preferred size of the image. 0 means no preference (any size |
| 242 // will do). | 213 // will do). |
| 243 int preferred_icon_size() const { | 214 int preferred_icon_size() const { |
| 244 return download_largest_icon_ ? 0 : gfx::kFaviconSize; | 215 return target_size_spec_.WantsBestBitmapOnly() ? 0 : gfx::kFaviconSize; |
| 245 } | 216 } |
| 246 | 217 |
| 247 // Used for FaviconService requests. | 218 // Used for FaviconService requests. |
| 248 base::CancelableTaskTracker cancelable_task_tracker_; | 219 base::CancelableTaskTracker cancelable_task_tracker_; |
| 249 | 220 |
| 250 FaviconDriverObserver::NotificationIconType handler_type_; | 221 FaviconDriverObserver::NotificationIconType handler_type_; |
| 251 | 222 |
| 252 // URL of the page we're requesting the favicon for. | 223 // URL of the page we're requesting the favicon for. |
| 253 GURL url_; | 224 GURL url_; |
| 254 | 225 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 267 // |image_urls_| one by one. | 238 // |image_urls_| one by one. |
| 268 bool redownload_icons_; | 239 bool redownload_icons_; |
| 269 | 240 |
| 270 // Requests to the renderer to download favicons. | 241 // Requests to the renderer to download favicons. |
| 271 typedef std::map<int, DownloadRequest> DownloadRequests; | 242 typedef std::map<int, DownloadRequest> DownloadRequests; |
| 272 DownloadRequests download_requests_; | 243 DownloadRequests download_requests_; |
| 273 | 244 |
| 274 // The combination of the supported icon types. | 245 // The combination of the supported icon types. |
| 275 const int icon_types_; | 246 const int icon_types_; |
| 276 | 247 |
| 277 // Whether the largest icon should be downloaded. | 248 // The target "bucket" or requirements that the FaviconHandler will try to |
| 278 const bool download_largest_icon_; | 249 // fulfill. |
| 250 const FaviconSelector::TargetSizeSpec target_size_spec_; |
| 251 std::unique_ptr<FaviconSelector> selector_; |
| 279 | 252 |
| 280 // The prioritized favicon candidates from the page back from the renderer. | 253 // Current candidate being processed. |
| 254 base::Optional<FaviconSelector::Candidate> current_candidate_; |
| 255 |
| 256 // Image of the best favicon processed so far. |
| 257 gfx::Image best_favicon_image_; |
| 258 |
| 259 // The filtered (per icon type) candidates provided by the page. |
| 281 std::vector<favicon::FaviconURL> image_urls_; | 260 std::vector<favicon::FaviconURL> image_urls_; |
| 282 | 261 |
| 283 // The icon URL and the icon type of the favicon in the most recent | 262 // The icon URL and the icon type of the favicon in the most recent |
| 284 // FaviconDriver::OnFaviconAvailable() notification. | 263 // FaviconDriver::OnFaviconAvailable() notification. |
| 285 GURL notification_icon_url_; | 264 GURL notification_icon_url_; |
| 286 favicon_base::IconType notification_icon_type_; | 265 favicon_base::IconType notification_icon_type_; |
| 287 | 266 |
| 288 // The FaviconService which implements favicon operations. May be null during | 267 // The FaviconService which implements favicon operations. May be null during |
| 289 // testing. | 268 // testing. |
| 290 FaviconService* service_; | 269 FaviconService* service_; |
| 291 | 270 |
| 292 // This handler's delegate. | 271 // This handler's delegate. |
| 293 Delegate* delegate_; | 272 Delegate* delegate_; |
| 294 | 273 |
| 295 // The index of the favicon URL in |image_urls_| which is currently being | |
| 296 // requested from history or downloaded. | |
| 297 size_t current_candidate_index_; | |
| 298 | |
| 299 // Best image we've seen so far. As images are downloaded from the page they | |
| 300 // are stored here. When there is an exact match, or no more images are | |
| 301 // available the favicon service and the current page are updated (assuming | |
| 302 // the image is for a favicon). | |
| 303 FaviconCandidate best_favicon_candidate_; | |
| 304 | |
| 305 base::WeakPtrFactory<FaviconHandler> weak_ptr_factory_; | 274 base::WeakPtrFactory<FaviconHandler> weak_ptr_factory_; |
| 306 | 275 |
| 307 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); | 276 DISALLOW_COPY_AND_ASSIGN(FaviconHandler); |
| 308 }; | 277 }; |
| 309 | 278 |
| 310 } // namespace favicon | 279 } // namespace favicon |
| 311 | 280 |
| 312 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ | 281 #endif // COMPONENTS_FAVICON_CORE_FAVICON_HANDLER_H_ |
| OLD | NEW |