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 #include "chrome/browser/favicon/favicon_handler.h" | 5 #include "chrome/browser/favicon/favicon_handler.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 return history::INVALID_ICON; | 46 return history::INVALID_ICON; |
| 47 } | 47 } |
| 48 | 48 |
| 49 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, | 49 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, |
| 50 const GURL& url, | 50 const GURL& url, |
| 51 history::IconType icon_type) { | 51 history::IconType icon_type) { |
| 52 return favicon_url.icon_url == url && | 52 return favicon_url.icon_url == url && |
| 53 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type); | 53 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Returns true if all of the icon URLs and icon types in |bitmap_results| are | |
| 57 // identical and if they match the icon URL and icon type in |favicon_url|. | |
| 58 // Returns false if |bitmap_results| is empty. | |
| 59 bool DoUrlsAndIconsMatch( | |
| 60 const FaviconURL& favicon_url, | |
| 61 const std::vector<history::FaviconBitmapResult>& bitmap_results) { | |
| 62 if (bitmap_results.empty()) | |
| 63 return false; | |
| 64 | |
| 65 history::IconType icon_type = ToHistoryIconType(favicon_url.icon_type); | |
| 66 | |
| 67 for (size_t i = 0; i < bitmap_results.size(); ++i) { | |
| 68 if (favicon_url.icon_url != bitmap_results[i].icon_url || | |
| 69 icon_type != bitmap_results[i].icon_type) { | |
| 70 return false; | |
| 71 } | |
| 72 } | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 56 std::string UrlWithoutFragment(const GURL& gurl) { | 76 std::string UrlWithoutFragment(const GURL& gurl) { |
| 57 GURL::Replacements replacements; | 77 GURL::Replacements replacements; |
| 58 replacements.ClearRef(); | 78 replacements.ClearRef(); |
| 59 return gurl.ReplaceComponents(replacements).spec(); | 79 return gurl.ReplaceComponents(replacements).spec(); |
| 60 } | 80 } |
| 61 | 81 |
| 62 bool UrlMatches(const GURL& gurl_a, const GURL& gurl_b) { | 82 bool UrlMatches(const GURL& gurl_a, const GURL& gurl_b) { |
| 63 return UrlWithoutFragment(gurl_a) == UrlWithoutFragment(gurl_b); | 83 return UrlWithoutFragment(gurl_a) == UrlWithoutFragment(gurl_b); |
| 64 } | 84 } |
| 65 | 85 |
| 66 // Returns true if at least one of the bitmaps in |favicon_bitmap_results| is | 86 // Returns true if at least one of the bitmaps in |favicon_bitmap_results| is |
| 67 // expired. | 87 // expired. |
| 68 bool HasExpiredFaviconResult( | 88 bool HasExpiredResult( |
|
rjkroege
2012/09/07 16:45:46
why change the name if the function is the same?
| |
| 69 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results) { | 89 const std::vector<history::FaviconBitmapResult>& bitmap_results) { |
| 70 for (size_t i = 0; i < favicon_bitmap_results.size(); ++i) { | 90 for (size_t i = 0; i < bitmap_results.size(); ++i) { |
| 71 if (favicon_bitmap_results[i].expired) | 91 if (bitmap_results[i].expired) |
| 72 return true; | 92 return true; |
| 73 } | 93 } |
| 74 return false; | 94 return false; |
| 95 } | |
| 96 | |
| 97 // Returns true if at least one of |bitmap_results| is valid. | |
| 98 bool HasValidResult( | |
| 99 const std::vector<history::FaviconBitmapResult>& bitmap_results) { | |
| 100 for (size_t i = 0; i < bitmap_results.size(); ++i) { | |
| 101 if (bitmap_results[i].is_valid()) | |
| 102 return true; | |
| 103 } | |
| 104 return false; | |
| 75 } | 105 } |
| 76 | 106 |
| 77 } // namespace | 107 } // namespace |
| 78 | 108 |
| 79 //////////////////////////////////////////////////////////////////////////////// | 109 //////////////////////////////////////////////////////////////////////////////// |
| 80 | 110 |
| 81 FaviconHandler::DownloadRequest::DownloadRequest() | 111 FaviconHandler::DownloadRequest::DownloadRequest() |
| 82 : icon_type(history::INVALID_ICON) { | 112 : icon_type(history::INVALID_ICON) { |
| 83 } | 113 } |
| 84 | 114 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 } | 232 } |
| 203 if (update_candidate) { | 233 if (update_candidate) { |
| 204 favicon_candidate_ = FaviconCandidate( | 234 favicon_candidate_ = FaviconCandidate( |
| 205 url, image_url, image, score, icon_type); | 235 url, image_url, image, score, icon_type); |
| 206 } | 236 } |
| 207 return exact_match; | 237 return exact_match; |
| 208 } | 238 } |
| 209 | 239 |
| 210 void FaviconHandler::SetFavicon( | 240 void FaviconHandler::SetFavicon( |
| 211 const GURL& url, | 241 const GURL& url, |
| 212 const GURL& image_url, | 242 const GURL& icon_url, |
| 213 const gfx::Image& image, | 243 const gfx::Image& image, |
| 214 history::IconType icon_type) { | 244 history::IconType icon_type) { |
| 215 SkBitmap bitmap = *(image.ToSkBitmap()); | 245 SkBitmap bitmap = *(image.ToSkBitmap()); |
| 216 const gfx::Image& sized_image = (preferred_icon_size() == 0 || | |
| 217 (preferred_icon_size() == bitmap.width() && | |
| 218 preferred_icon_size() == bitmap.height())) ? | |
| 219 image : ResizeFaviconIfNeeded(image); | |
| 220 | 246 |
| 221 if (GetFaviconService() && ShouldSaveFavicon(url)) { | 247 if (GetFaviconService() && ShouldSaveFavicon(url)) { |
| 222 std::vector<unsigned char> image_data; | 248 std::vector<unsigned char> image_data; |
| 223 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) | 249 if (gfx::PNGEncodedDataFromImage(image, &image_data)) |
| 224 SetHistoryFavicon(url, image_url, image_data, icon_type); | 250 SetHistoryFavicon(url, icon_url, image_data, icon_type); |
|
rjkroege
2012/09/07 16:45:46
when do we actually get a sized version? Have you
pkotwicz
2012/09/07 17:25:39
The resizing occurs in FaviconTabHelper::OnDidDown
| |
| 225 } | 251 } |
| 226 | 252 |
| 227 if (UrlMatches(url, url_) && icon_type == history::FAVICON) { | 253 if (UrlMatches(url, url_) && icon_type == history::FAVICON) { |
| 228 NavigationEntry* entry = GetEntry(); | 254 NavigationEntry* entry = GetEntry(); |
| 229 if (entry) { | 255 if (entry) { |
| 230 entry->GetFavicon().url = image_url; | 256 entry->GetFavicon().url = icon_url; |
| 231 UpdateFavicon(entry, &sized_image); | 257 UpdateFavicon(entry, &image); |
| 232 } | 258 } |
| 233 } | 259 } |
| 234 } | 260 } |
| 235 | 261 |
| 236 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 262 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
| 237 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results) { | 263 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results) { |
| 238 gfx::Image resized_image = FaviconUtil::SelectFaviconFramesFromPNGs( | 264 gfx::Image resized_image = FaviconUtil::SelectFaviconFramesFromPNGs( |
| 239 favicon_bitmap_results, | 265 favicon_bitmap_results, |
| 240 ui::GetSupportedScaleFactors(), | 266 ui::GetSupportedScaleFactors(), |
| 241 preferred_icon_size()); | 267 preferred_icon_size()); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 422 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, | 448 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, |
| 423 history::IconURLSizesMap icon_url_sizes) { | 449 history::IconURLSizesMap icon_url_sizes) { |
| 424 NavigationEntry* entry = GetEntry(); | 450 NavigationEntry* entry = GetEntry(); |
| 425 if (!entry) | 451 if (!entry) |
| 426 return; | 452 return; |
| 427 | 453 |
| 428 got_favicon_from_history_ = true; | 454 got_favicon_from_history_ = true; |
| 429 history_results_ = favicon_bitmap_results; | 455 history_results_ = favicon_bitmap_results; |
| 430 | 456 |
| 431 bool has_results = !favicon_bitmap_results.empty(); | 457 bool has_results = !favicon_bitmap_results.empty(); |
| 432 favicon_expired_ = (has_results && | 458 favicon_expired_ = (has_results && HasExpiredResult(favicon_bitmap_results)); |
| 433 HasExpiredFaviconResult(favicon_bitmap_results)); | |
| 434 | 459 |
| 435 history::FaviconBitmapResult bitmap_result; | 460 if (has_results && icon_types_ == history::FAVICON && |
| 436 if (has_results) | |
| 437 bitmap_result = favicon_bitmap_results[0]; | |
| 438 | |
| 439 if (has_results && bitmap_result.icon_type == history::FAVICON && | |
| 440 !entry->GetFavicon().valid && | 461 !entry->GetFavicon().valid && |
| 441 (!current_candidate() || | 462 (!current_candidate() || |
| 442 DoUrlAndIconMatch(*current_candidate(), | 463 DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) { |
| 443 bitmap_result.icon_url, bitmap_result.icon_type))) { | |
| 444 // The db knows the favicon (although it may be out of date) and the entry | 464 // The db knows the favicon (although it may be out of date) and the entry |
| 445 // doesn't have an icon. Set the favicon now, and if the favicon turns out | 465 // doesn't have an icon. Set the favicon now, and if the favicon turns out |
| 446 // to be expired (or the wrong url) we'll fetch later on. This way the | 466 // to be expired (or the wrong url) we'll fetch later on. This way the |
| 447 // user doesn't see a flash of the default favicon. | 467 // user doesn't see a flash of the default favicon. |
| 448 entry->GetFavicon().url = bitmap_result.icon_url; | 468 |
| 449 if (bitmap_result.is_valid()) | 469 // The history service sends back results for a single icon URL, so it does |
| 470 // not matter which result we get the |icon_url| from. | |
| 471 entry->GetFavicon().url = favicon_bitmap_results[0].icon_url; | |
| 472 if (HasValidResult(favicon_bitmap_results)) | |
| 450 UpdateFavicon(entry, favicon_bitmap_results); | 473 UpdateFavicon(entry, favicon_bitmap_results); |
| 451 entry->GetFavicon().valid = true; | 474 entry->GetFavicon().valid = true; |
| 452 } | 475 } |
| 453 | 476 |
| 454 if (has_results && !bitmap_result.expired) { | 477 if (has_results && !HasExpiredResult(favicon_bitmap_results)) { |
| 455 if (current_candidate() && | 478 if (current_candidate() && |
| 456 !DoUrlAndIconMatch(*current_candidate(), | 479 !DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)) { |
| 457 bitmap_result.icon_url, bitmap_result.icon_type)) { | |
| 458 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will | 480 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will |
| 459 // update the mapping for this url and download the favicon if we don't | 481 // update the mapping for this url and download the favicon if we don't |
| 460 // already have it. | 482 // already have it. |
| 461 DownloadFaviconOrAskHistory(entry->GetURL(), | 483 DownloadFaviconOrAskHistory(entry->GetURL(), |
| 462 current_candidate()->icon_url, | 484 current_candidate()->icon_url, |
| 463 static_cast<history::IconType>(current_candidate()->icon_type)); | 485 static_cast<history::IconType>(current_candidate()->icon_type)); |
| 464 } | 486 } |
| 465 } else if (current_candidate()) { | 487 } else if (current_candidate()) { |
| 466 // We know the official url for the favicon, by either don't have the | 488 // We know the official url for the favicon, by either don't have the |
| 467 // favicon or its expired. Continue on to DownloadFaviconOrAskHistory to | 489 // favicon or its expired. Continue on to DownloadFaviconOrAskHistory to |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 504 | 526 |
| 505 void FaviconHandler::OnFaviconData( | 527 void FaviconHandler::OnFaviconData( |
| 506 FaviconService::Handle handle, | 528 FaviconService::Handle handle, |
| 507 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, | 529 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, |
| 508 history::IconURLSizesMap icon_url_sizes) { | 530 history::IconURLSizesMap icon_url_sizes) { |
| 509 NavigationEntry* entry = GetEntry(); | 531 NavigationEntry* entry = GetEntry(); |
| 510 if (!entry) | 532 if (!entry) |
| 511 return; | 533 return; |
| 512 | 534 |
| 513 bool has_results = !favicon_bitmap_results.empty(); | 535 bool has_results = !favicon_bitmap_results.empty(); |
| 514 history::FaviconBitmapResult bitmap_result; | |
| 515 if (has_results) | |
| 516 bitmap_result = favicon_bitmap_results[0]; | |
| 517 | 536 |
| 518 // No need to update the favicon url. By the time we get here | 537 // No need to update the favicon url. By the time we get here |
| 519 // UpdateFaviconURL will have set the favicon url. | 538 // UpdateFaviconURL will have set the favicon url. |
| 520 if (has_results && bitmap_result.icon_type == history::FAVICON) { | 539 if (has_results && icon_types_ == history::FAVICON) { |
| 521 if (bitmap_result.is_valid()) { | 540 // There is a favicon, set it now. If expired we'll download the current |
| 522 // There is a favicon, set it now. If expired we'll download the current | 541 // one again, but at least the user will get some icon instead of the |
| 523 // one again, but at least the user will get some icon instead of the | 542 // default and most likely the current one is fine anyway. |
| 524 // default and most likely the current one is fine anyway. | 543 if (HasValidResult(favicon_bitmap_results)) |
| 525 UpdateFavicon(entry, favicon_bitmap_results); | 544 UpdateFavicon(entry, favicon_bitmap_results); |
| 526 } | 545 if (HasExpiredResult(favicon_bitmap_results)) { |
| 527 if (HasExpiredFaviconResult(favicon_bitmap_results)) { | |
| 528 // The favicon is out of date. Request the current one. | 546 // The favicon is out of date. Request the current one. |
| 529 ScheduleDownload(entry->GetURL(), entry->GetFavicon().url, | 547 ScheduleDownload(entry->GetURL(), entry->GetFavicon().url, |
| 530 preferred_icon_size(), | 548 preferred_icon_size(), |
| 531 history::FAVICON, | 549 history::FAVICON, |
| 532 FaviconTabHelper::ImageDownloadCallback()); | 550 FaviconTabHelper::ImageDownloadCallback()); |
| 533 } | 551 } |
| 534 } else if (current_candidate() && | 552 } else if (current_candidate() && |
| 535 (!has_results || HasExpiredFaviconResult(favicon_bitmap_results) || | 553 (!has_results || HasExpiredResult(favicon_bitmap_results) || |
| 536 !(DoUrlAndIconMatch(*current_candidate(), bitmap_result.icon_url, | 554 !(DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results)))) { |
| 537 bitmap_result.icon_type)))) { | |
| 538 // We don't know the favicon, it is out of date or its type is not same as | 555 // We don't know the favicon, it is out of date or its type is not same as |
| 539 // one got from page. Request the current one. | 556 // one got from page. Request the current one. |
| 540 ScheduleDownload(entry->GetURL(), current_candidate()->icon_url, | 557 ScheduleDownload(entry->GetURL(), current_candidate()->icon_url, |
| 541 preferred_icon_size(), | 558 preferred_icon_size(), |
| 542 ToHistoryIconType(current_candidate()->icon_type), | 559 ToHistoryIconType(current_candidate()->icon_type), |
| 543 FaviconTabHelper::ImageDownloadCallback()); | 560 FaviconTabHelper::ImageDownloadCallback()); |
| 544 } | 561 } |
| 545 history_results_ = favicon_bitmap_results; | 562 history_results_ = favicon_bitmap_results; |
| 546 } | 563 } |
| 547 | 564 |
| 548 int FaviconHandler::ScheduleDownload( | 565 int FaviconHandler::ScheduleDownload( |
| 549 const GURL& url, | 566 const GURL& url, |
| 550 const GURL& image_url, | 567 const GURL& image_url, |
| 551 int image_size, | 568 int image_size, |
| 552 history::IconType icon_type, | 569 history::IconType icon_type, |
| 553 const FaviconTabHelper::ImageDownloadCallback& callback) { | 570 const FaviconTabHelper::ImageDownloadCallback& callback) { |
| 554 const int download_id = DownloadFavicon(image_url, image_size); | 571 const int download_id = DownloadFavicon(image_url, image_size); |
| 555 if (download_id) { | 572 if (download_id) { |
| 556 // Download ids should be unique. | 573 // Download ids should be unique. |
| 557 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 574 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
| 558 download_requests_[download_id] = | 575 download_requests_[download_id] = |
| 559 DownloadRequest(url, image_url, callback, icon_type); | 576 DownloadRequest(url, image_url, callback, icon_type); |
| 560 } | 577 } |
| 561 | 578 |
| 562 return download_id; | 579 return download_id; |
| 563 } | 580 } |
| 564 | |
| 565 gfx::Image FaviconHandler::ResizeFaviconIfNeeded(const gfx::Image& image) { | |
| 566 // Get an SkBitmap from the gfx::Image. | |
| 567 SkBitmap bitmap = *image.ToSkBitmap(); | |
| 568 int width = bitmap.width(); | |
| 569 int height = bitmap.height(); | |
| 570 if (width > 0 && height > 0) { | |
| 571 gfx::CalculateFaviconTargetSize(&width, &height); | |
| 572 return gfx::Image(skia::ImageOperations::Resize( | |
| 573 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | |
| 574 width, height)); | |
| 575 } | |
| 576 | |
| 577 return image; | |
| 578 } | |
| OLD | NEW |