| 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 #include "components/favicon/core/favicon_handler.h" | 5 #include "components/favicon/core/favicon_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 const gfx::Image& image, | 203 const gfx::Image& image, |
| 204 float score, | 204 float score, |
| 205 favicon_base::IconType icon_type) | 205 favicon_base::IconType icon_type) |
| 206 : image_url(image_url), | 206 : image_url(image_url), |
| 207 image(image), | 207 image(image), |
| 208 score(score), | 208 score(score), |
| 209 icon_type(icon_type) {} | 209 icon_type(icon_type) {} |
| 210 | 210 |
| 211 //////////////////////////////////////////////////////////////////////////////// | 211 //////////////////////////////////////////////////////////////////////////////// |
| 212 | 212 |
| 213 FaviconHandler::FaviconHandler(FaviconService* service, | 213 FaviconHandler::FaviconHandler( |
| 214 FaviconDriver* driver, | 214 FaviconService* service, |
| 215 Type handler_type) | 215 FaviconDriver* driver, |
| 216 : got_favicon_from_history_(false), | 216 FaviconDriverObserver::NotificationIconType handler_type) |
| 217 : handler_type_(handler_type), |
| 218 got_favicon_from_history_(false), |
| 217 initial_history_result_expired_or_incomplete_(false), | 219 initial_history_result_expired_or_incomplete_(false), |
| 218 redownload_icons_(false), | 220 redownload_icons_(false), |
| 219 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), | 221 icon_types_(FaviconHandler::GetIconTypesFromHandlerType(handler_type)), |
| 220 download_largest_icon_(handler_type == LARGEST_FAVICON || | 222 download_largest_icon_( |
| 221 handler_type == LARGEST_TOUCH), | 223 handler_type == FaviconDriverObserver::NON_TOUCH_LARGEST || |
| 224 handler_type == FaviconDriverObserver::TOUCH_LARGEST), |
| 222 notification_icon_type_(favicon_base::INVALID_ICON), | 225 notification_icon_type_(favicon_base::INVALID_ICON), |
| 223 service_(service), | 226 service_(service), |
| 224 driver_(driver), | 227 driver_(driver), |
| 225 current_candidate_index_(0u) { | 228 current_candidate_index_(0u) { |
| 226 DCHECK(driver_); | 229 DCHECK(driver_); |
| 227 } | 230 } |
| 228 | 231 |
| 229 FaviconHandler::~FaviconHandler() { | 232 FaviconHandler::~FaviconHandler() { |
| 230 } | 233 } |
| 231 | 234 |
| 232 // static | 235 // static |
| 233 int FaviconHandler::GetIconTypesFromHandlerType( | 236 int FaviconHandler::GetIconTypesFromHandlerType( |
| 234 FaviconHandler::Type handler_type) { | 237 FaviconDriverObserver::NotificationIconType handler_type) { |
| 235 switch (handler_type) { | 238 switch (handler_type) { |
| 236 case FAVICON: | 239 case FaviconDriverObserver::NON_TOUCH_16_DIP: |
| 237 case LARGEST_FAVICON: | 240 case FaviconDriverObserver::NON_TOUCH_LARGEST: |
| 238 return favicon_base::FAVICON; | 241 return favicon_base::FAVICON; |
| 239 case LARGEST_TOUCH: | 242 case FaviconDriverObserver::TOUCH_LARGEST: |
| 240 return favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON; | 243 return favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON; |
| 241 } | 244 } |
| 242 return 0; | 245 return 0; |
| 243 } | 246 } |
| 244 | 247 |
| 245 void FaviconHandler::FetchFavicon(const GURL& url) { | 248 void FaviconHandler::FetchFavicon(const GURL& url) { |
| 246 cancelable_task_tracker_.TryCancelAll(); | 249 cancelable_task_tracker_.TryCancelAll(); |
| 247 | 250 |
| 248 url_ = url; | 251 url_ = url; |
| 249 | 252 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 } | 316 } |
| 314 return exact_match; | 317 return exact_match; |
| 315 } | 318 } |
| 316 | 319 |
| 317 void FaviconHandler::SetFavicon(const GURL& icon_url, | 320 void FaviconHandler::SetFavicon(const GURL& icon_url, |
| 318 const gfx::Image& image, | 321 const gfx::Image& image, |
| 319 favicon_base::IconType icon_type) { | 322 favicon_base::IconType icon_type) { |
| 320 if (ShouldSaveFavicon()) | 323 if (ShouldSaveFavicon()) |
| 321 SetHistoryFavicons(url_, icon_url, icon_type, image); | 324 SetHistoryFavicons(url_, icon_url, icon_type, image); |
| 322 | 325 |
| 323 NotifyFaviconAvailable(icon_url, icon_type, image); | 326 NotifyFaviconUpdated(icon_url, icon_type, image); |
| 324 } | 327 } |
| 325 | 328 |
| 326 void FaviconHandler::NotifyFaviconAvailable( | 329 void FaviconHandler::NotifyFaviconUpdated( |
| 327 const std::vector<favicon_base::FaviconRawBitmapResult>& | 330 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 328 favicon_bitmap_results) { | 331 favicon_bitmap_results) { |
| 329 if (favicon_bitmap_results.empty()) | 332 if (favicon_bitmap_results.empty()) |
| 330 return; | 333 return; |
| 331 | 334 |
| 332 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( | 335 gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs( |
| 333 favicon_bitmap_results, | 336 favicon_bitmap_results, |
| 334 favicon_base::GetFaviconScales(), | 337 favicon_base::GetFaviconScales(), |
| 335 preferred_icon_size()); | 338 preferred_icon_size()); |
| 336 // The history service sends back results for a single icon URL and icon | 339 // The history service sends back results for a single icon URL and icon |
| 337 // type, so it does not matter which result we get |icon_url| and |icon_type| | 340 // type, so it does not matter which result we get |icon_url| and |icon_type| |
| 338 // from. | 341 // from. |
| 339 const GURL icon_url = favicon_bitmap_results[0].icon_url; | 342 const GURL icon_url = favicon_bitmap_results[0].icon_url; |
| 340 favicon_base::IconType icon_type = favicon_bitmap_results[0].icon_type; | 343 favicon_base::IconType icon_type = favicon_bitmap_results[0].icon_type; |
| 341 NotifyFaviconAvailable(icon_url, icon_type, resized_image); | 344 NotifyFaviconUpdated(icon_url, icon_type, resized_image); |
| 342 } | 345 } |
| 343 | 346 |
| 344 void FaviconHandler::NotifyFaviconAvailable(const GURL& icon_url, | 347 void FaviconHandler::NotifyFaviconUpdated(const GURL& icon_url, |
| 345 favicon_base::IconType icon_type, | 348 favicon_base::IconType icon_type, |
| 346 const gfx::Image& image) { | 349 const gfx::Image& image) { |
| 350 if (image.IsEmpty()) |
| 351 return; |
| 352 |
| 347 gfx::Image image_with_adjusted_colorspace = image; | 353 gfx::Image image_with_adjusted_colorspace = image; |
| 348 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); | 354 favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace); |
| 349 | 355 |
| 350 bool is_active_favicon = !download_largest_icon_; | 356 driver_->OnFaviconUpdated(url_, handler_type_, icon_url, |
| 351 | 357 icon_url != notification_icon_url_, |
| 352 driver_->OnFaviconAvailable( | 358 image_with_adjusted_colorspace); |
| 353 url_, icon_url, image_with_adjusted_colorspace, is_active_favicon); | |
| 354 | 359 |
| 355 notification_icon_url_ = icon_url; | 360 notification_icon_url_ = icon_url; |
| 356 notification_icon_type_ = icon_type; | 361 notification_icon_type_ = icon_type; |
| 357 } | 362 } |
| 358 | 363 |
| 359 void FaviconHandler::OnUpdateFaviconURL( | 364 void FaviconHandler::OnUpdateFaviconURL( |
| 360 const GURL& page_url, | 365 const GURL& page_url, |
| 361 const std::vector<FaviconURL>& candidates) { | 366 const std::vector<FaviconURL>& candidates) { |
| 362 if (page_url != url_) | 367 if (page_url != url_) |
| 363 return; | 368 return; |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 redownload_icons_ = initial_history_result_expired_or_incomplete_ && | 587 redownload_icons_ = initial_history_result_expired_or_incomplete_ && |
| 583 !favicon_bitmap_results.empty(); | 588 !favicon_bitmap_results.empty(); |
| 584 | 589 |
| 585 if (has_valid_result && | 590 if (has_valid_result && |
| 586 (!current_candidate() || | 591 (!current_candidate() || |
| 587 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 592 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
| 588 // The db knows the favicon (although it may be out of date) and the entry | 593 // The db knows the favicon (although it may be out of date) and the entry |
| 589 // doesn't have an icon. Set the favicon now, and if the favicon turns out | 594 // doesn't have an icon. Set the favicon now, and if the favicon turns out |
| 590 // to be expired (or the wrong url) we'll fetch later on. This way the | 595 // to be expired (or the wrong url) we'll fetch later on. This way the |
| 591 // user doesn't see a flash of the default favicon. | 596 // user doesn't see a flash of the default favicon. |
| 592 | 597 NotifyFaviconUpdated(favicon_bitmap_results); |
| 593 NotifyFaviconAvailable(favicon_bitmap_results); | |
| 594 } | 598 } |
| 595 | 599 |
| 596 if (current_candidate()) | 600 if (current_candidate()) |
| 597 OnGotInitialHistoryDataAndIconURLCandidates(); | 601 OnGotInitialHistoryDataAndIconURLCandidates(); |
| 598 } | 602 } |
| 599 | 603 |
| 600 void FaviconHandler::DownloadCurrentCandidateOrAskFaviconService() { | 604 void FaviconHandler::DownloadCurrentCandidateOrAskFaviconService() { |
| 601 GURL icon_url = current_candidate()->icon_url; | 605 GURL icon_url = current_candidate()->icon_url; |
| 602 favicon_base::IconType icon_type = current_candidate()->icon_type; | 606 favicon_base::IconType icon_type = current_candidate()->icon_type; |
| 603 | 607 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 633 bool has_valid_result = HasValidResult(favicon_bitmap_results); | 637 bool has_valid_result = HasValidResult(favicon_bitmap_results); |
| 634 bool has_expired_or_incomplete_result = | 638 bool has_expired_or_incomplete_result = |
| 635 !has_valid_result || HasExpiredOrIncompleteResult(preferred_icon_size(), | 639 !has_valid_result || HasExpiredOrIncompleteResult(preferred_icon_size(), |
| 636 favicon_bitmap_results); | 640 favicon_bitmap_results); |
| 637 | 641 |
| 638 if (has_valid_result) { | 642 if (has_valid_result) { |
| 639 // There is a valid favicon. Notify any observers. It is useful to notify | 643 // There is a valid favicon. Notify any observers. It is useful to notify |
| 640 // the observers even if the favicon is expired or incomplete (incorrect | 644 // the observers even if the favicon is expired or incomplete (incorrect |
| 641 // size) because temporarily showing the user an expired favicon or | 645 // size) because temporarily showing the user an expired favicon or |
| 642 // streched favicon is preferable to showing the user the default favicon. | 646 // streched favicon is preferable to showing the user the default favicon. |
| 643 NotifyFaviconAvailable(favicon_bitmap_results); | 647 NotifyFaviconUpdated(favicon_bitmap_results); |
| 644 } | 648 } |
| 645 | 649 |
| 646 if (!current_candidate() || | 650 if (!current_candidate() || |
| 647 (has_results && | 651 (has_results && |
| 648 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { | 652 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
| 649 // The icon URLs have been updated since the favicon data was requested. | 653 // The icon URLs have been updated since the favicon data was requested. |
| 650 return; | 654 return; |
| 651 } | 655 } |
| 652 | 656 |
| 653 if (has_expired_or_incomplete_result) { | 657 if (has_expired_or_incomplete_result) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 672 // If DownloadFavicon() did not start a download, it returns a download id | 676 // If DownloadFavicon() did not start a download, it returns a download id |
| 673 // of 0. We still need to call OnDidDownloadFavicon() because the method is | 677 // of 0. We still need to call OnDidDownloadFavicon() because the method is |
| 674 // responsible for initiating the data request for the next candidate. | 678 // responsible for initiating the data request for the next candidate. |
| 675 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), | 679 OnDidDownloadFavicon(download_id, image_url, std::vector<SkBitmap>(), |
| 676 std::vector<gfx::Size>()); | 680 std::vector<gfx::Size>()); |
| 677 | 681 |
| 678 } | 682 } |
| 679 } | 683 } |
| 680 | 684 |
| 681 } // namespace favicon | 685 } // namespace favicon |
| OLD | NEW |