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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_model.h" | 14 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 16 #include "chrome/browser/favicon/favicon_service_factory.h" | 16 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 17 #include "chrome/browser/favicon/select_favicon_frames.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/common/icon_messages.h" | 19 #include "chrome/common/icon_messages.h" |
| 19 #include "content/public/browser/favicon_status.h" | 20 #include "content/public/browser/favicon_status.h" |
| 20 #include "content/public/browser/navigation_entry.h" | 21 #include "content/public/browser/navigation_entry.h" |
| 21 #include "skia/ext/image_operations.h" | 22 #include "skia/ext/image_operations.h" |
| 22 #include "ui/gfx/codec/png_codec.h" | 23 #include "ui/gfx/codec/png_codec.h" |
| 23 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
| 25 #include "ui/gfx/image/image_skia.h" | |
| 24 #include "ui/gfx/image/image_util.h" | 26 #include "ui/gfx/image/image_util.h" |
| 25 | 27 |
| 26 using content::NavigationEntry; | 28 using content::NavigationEntry; |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 // Returns history::IconType the given icon_type corresponds to. | 32 // Returns history::IconType the given icon_type corresponds to. |
| 31 history::IconType ToHistoryIconType(FaviconURL::IconType icon_type) { | 33 history::IconType ToHistoryIconType(FaviconURL::IconType icon_type) { |
| 32 switch (icon_type) { | 34 switch (icon_type) { |
| 33 case FaviconURL::FAVICON: | 35 case FaviconURL::FAVICON: |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 if (UrlMatches(url, url_) && icon_type == history::FAVICON) { | 227 if (UrlMatches(url, url_) && icon_type == history::FAVICON) { |
| 226 NavigationEntry* entry = GetEntry(); | 228 NavigationEntry* entry = GetEntry(); |
| 227 if (entry) { | 229 if (entry) { |
| 228 entry->GetFavicon().url = image_url; | 230 entry->GetFavicon().url = image_url; |
| 229 UpdateFavicon(entry, &sized_image); | 231 UpdateFavicon(entry, &sized_image); |
| 230 } | 232 } |
| 231 } | 233 } |
| 232 } | 234 } |
| 233 | 235 |
| 234 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 236 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
| 235 scoped_refptr<base::RefCountedMemory> data) { | 237 const std::vector<history::FaviconBitmapResult>& favicon_bitmap_results) { |
|
pkotwicz
2012/09/05 14:08:46
Nit:
Can we put the code for converting from a Fav
Nico
2012/09/05 14:48:54
Done.
| |
| 236 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), | 238 std::vector<SkBitmap> bitmaps; |
| 237 data->size())); | 239 for (size_t i = 0; i < favicon_bitmap_results.size(); ++i) { |
| 238 UpdateFavicon(entry, image.get()); | 240 SkBitmap bitmap; |
| 241 if (gfx::PNGCodec::Decode(favicon_bitmap_results[i].bitmap_data->front(), | |
| 242 favicon_bitmap_results[i].bitmap_data->size(), | |
| 243 &bitmap)) { | |
| 244 bitmaps.push_back(bitmap); | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 if (bitmaps.empty()) | |
| 249 return; | |
| 250 | |
| 251 gfx::ImageSkia resized_image_skia = SelectFaviconFrames(bitmaps, | |
| 252 ui::GetSupportedScaleFactors(), preferred_icon_size(), NULL); | |
| 253 gfx::Image resized_image(resized_image_skia); | |
| 254 UpdateFavicon(entry, &resized_image); | |
| 239 } | 255 } |
| 240 | 256 |
| 241 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, | 257 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, |
| 242 const gfx::Image* image) { | 258 const gfx::Image* image) { |
| 243 // No matter what happens, we need to mark the favicon as being set. | 259 // No matter what happens, we need to mark the favicon as being set. |
| 244 entry->GetFavicon().valid = true; | 260 entry->GetFavicon().valid = true; |
| 245 | 261 |
| 246 if (!image) | 262 if (!image) |
| 247 return; | 263 return; |
| 248 | 264 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 !entry->GetFavicon().valid && | 450 !entry->GetFavicon().valid && |
| 435 (!current_candidate() || | 451 (!current_candidate() || |
| 436 DoUrlAndIconMatch(*current_candidate(), | 452 DoUrlAndIconMatch(*current_candidate(), |
| 437 bitmap_result.icon_url, bitmap_result.icon_type))) { | 453 bitmap_result.icon_url, bitmap_result.icon_type))) { |
| 438 // The db knows the favicon (although it may be out of date) and the entry | 454 // The db knows the favicon (although it may be out of date) and the entry |
| 439 // doesn't have an icon. Set the favicon now, and if the favicon turns out | 455 // doesn't have an icon. Set the favicon now, and if the favicon turns out |
| 440 // to be expired (or the wrong url) we'll fetch later on. This way the | 456 // to be expired (or the wrong url) we'll fetch later on. This way the |
| 441 // user doesn't see a flash of the default favicon. | 457 // user doesn't see a flash of the default favicon. |
| 442 entry->GetFavicon().url = bitmap_result.icon_url; | 458 entry->GetFavicon().url = bitmap_result.icon_url; |
| 443 if (bitmap_result.is_valid()) | 459 if (bitmap_result.is_valid()) |
| 444 UpdateFavicon(entry, bitmap_result.bitmap_data); | 460 UpdateFavicon(entry, favicon_bitmap_results); |
| 445 entry->GetFavicon().valid = true; | 461 entry->GetFavicon().valid = true; |
| 446 } | 462 } |
| 447 | 463 |
| 448 if (has_results && !bitmap_result.expired) { | 464 if (has_results && !bitmap_result.expired) { |
| 449 if (current_candidate() && | 465 if (current_candidate() && |
| 450 !DoUrlAndIconMatch(*current_candidate(), | 466 !DoUrlAndIconMatch(*current_candidate(), |
| 451 bitmap_result.icon_url, bitmap_result.icon_type)) { | 467 bitmap_result.icon_url, bitmap_result.icon_type)) { |
| 452 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will | 468 // Mapping in the database is wrong. DownloadFavIconOrAskHistory will |
| 453 // update the mapping for this url and download the favicon if we don't | 469 // update the mapping for this url and download the favicon if we don't |
| 454 // already have it. | 470 // already have it. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 if (has_results) | 525 if (has_results) |
| 510 bitmap_result = favicon_bitmap_results[0]; | 526 bitmap_result = favicon_bitmap_results[0]; |
| 511 | 527 |
| 512 // No need to update the favicon url. By the time we get here | 528 // No need to update the favicon url. By the time we get here |
| 513 // UpdateFaviconURL will have set the favicon url. | 529 // UpdateFaviconURL will have set the favicon url. |
| 514 if (has_results && bitmap_result.icon_type == history::FAVICON) { | 530 if (has_results && bitmap_result.icon_type == history::FAVICON) { |
| 515 if (bitmap_result.is_valid()) { | 531 if (bitmap_result.is_valid()) { |
| 516 // There is a favicon, set it now. If expired we'll download the current | 532 // There is a favicon, set it now. If expired we'll download the current |
| 517 // one again, but at least the user will get some icon instead of the | 533 // one again, but at least the user will get some icon instead of the |
| 518 // default and most likely the current one is fine anyway. | 534 // default and most likely the current one is fine anyway. |
| 519 UpdateFavicon(entry, bitmap_result.bitmap_data); | 535 UpdateFavicon(entry, favicon_bitmap_results); |
| 520 } | 536 } |
| 521 if (HasExpiredFaviconResult(favicon_bitmap_results)) { | 537 if (HasExpiredFaviconResult(favicon_bitmap_results)) { |
| 522 // The favicon is out of date. Request the current one. | 538 // The favicon is out of date. Request the current one. |
| 523 ScheduleDownload(entry->GetURL(), entry->GetFavicon().url, | 539 ScheduleDownload(entry->GetURL(), entry->GetFavicon().url, |
| 524 preferred_icon_size(), | 540 preferred_icon_size(), |
| 525 history::FAVICON, | 541 history::FAVICON, |
| 526 FaviconTabHelper::ImageDownloadCallback()); | 542 FaviconTabHelper::ImageDownloadCallback()); |
| 527 } | 543 } |
| 528 } else if (current_candidate() && | 544 } else if (current_candidate() && |
| 529 (!has_results || HasExpiredFaviconResult(favicon_bitmap_results) || | 545 (!has_results || HasExpiredFaviconResult(favicon_bitmap_results) || |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 563 int height = bitmap.height(); | 579 int height = bitmap.height(); |
| 564 if (width > 0 && height > 0) { | 580 if (width > 0 && height > 0) { |
| 565 gfx::CalculateFaviconTargetSize(&width, &height); | 581 gfx::CalculateFaviconTargetSize(&width, &height); |
| 566 return gfx::Image(skia::ImageOperations::Resize( | 582 return gfx::Image(skia::ImageOperations::Resize( |
| 567 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 583 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
| 568 width, height)); | 584 width, height)); |
| 569 } | 585 } |
| 570 | 586 |
| 571 return image; | 587 return image; |
| 572 } | 588 } |
| OLD | NEW |