Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/favicon_base/fallback_icon_service.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/i18n/case_conversion.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/message_loop/message_loop_proxy.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "components/favicon_base/favicon_types.h" | |
| 13 #include "grit/platform_locale_settings.h" | |
| 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
| 15 #include "third_party/skia/include/core/SkPaint.h" | |
| 16 #include "ui/base/l10n/l10n_util.h" | |
| 17 #include "ui/gfx/canvas.h" | |
| 18 #include "ui/gfx/codec/png_codec.h" | |
| 19 #include "ui/gfx/favicon_size.h" | |
| 20 #include "ui/gfx/font_list.h" | |
| 21 #include "ui/gfx/geometry/rect.h" | |
| 22 #include "ui/gfx/geometry/size.h" | |
| 23 #include "ui/gfx/image/image_skia.h" | |
| 24 #include "url/gurl.h" | |
| 25 | |
| 26 namespace favicon_base { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 // Returns a single character to represent a fallback URL. To do this we take | |
| 31 // the first letter in a domain's name and make it upper case. Currently we do | |
| 32 // not handle non-ASCII domain names. | |
| 33 base::string16 GetFallbackIconText(const GURL& url) { | |
| 34 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry( | |
| 35 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
| 36 return domain.empty() ? base::string16() : | |
| 37 base::i18n::ToUpper(base::ASCIIToUTF16(std::string(1, domain[0]))); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 FallbackIconService::FallbackIconService() { | |
| 43 std::string font_name = | |
| 44 l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY); | |
| 45 #if defined(OS_CHROMEOS) | |
| 46 const std::string kChromeOSFontFamily = "Noto Sans"; | |
| 47 font_name = kChromeOSFontFamily; | |
| 48 #endif | |
| 49 fallback_icon_font_list_.push_back(font_name); | |
|
pkotwicz
2015/01/19 04:32:02
You should cache the gfx::FontList. Creating the g
huangs
2015/01/20 22:20:41
Would this add extra memory footprint, and would t
| |
| 50 } | |
| 51 | |
| 52 FallbackIconService::~FallbackIconService() { | |
| 53 } | |
| 54 | |
| 55 void FallbackIconService::DrawFallbackIcon(const GURL& icon_url, | |
| 56 int x, | |
| 57 int y, | |
| 58 int size_in_pixels, | |
| 59 const FallbackIconSpecs& specs, | |
| 60 gfx::Canvas* canvas) { | |
| 61 SkPaint paint; | |
| 62 paint.setStyle(SkPaint::kFill_Style); | |
| 63 paint.setAntiAlias(true); | |
| 64 | |
| 65 // Draw a filled, colored rounded square. | |
| 66 paint.setColor(specs.background_color); | |
| 67 int corner_radius = static_cast<int>( | |
| 68 size_in_pixels * specs.corner_radius_ratio + 0.5); | |
| 69 canvas->DrawRoundRect(gfx::Rect(x, y, size_in_pixels, size_in_pixels), | |
| 70 corner_radius, paint); | |
| 71 | |
| 72 // Draw text. | |
| 73 base::string16 icon_text = GetFallbackIconText(icon_url); | |
| 74 if (!icon_text.empty()) { | |
| 75 int font_size = static_cast<int>(size_in_pixels * specs.font_size_ratio); | |
| 76 if (font_size > 0) { | |
| 77 canvas->DrawStringRectWithFlags( | |
| 78 icon_text, | |
| 79 gfx::FontList(fallback_icon_font_list_, gfx::Font::NORMAL, font_size), | |
| 80 specs.text_color, | |
| 81 gfx::Rect(0, 0, size_in_pixels, size_in_pixels), | |
| 82 gfx::Canvas::TEXT_ALIGN_CENTER); | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 base::CancelableTaskTracker::TaskId FallbackIconService::GetFallbackIcon( | |
| 88 const GURL& icon_url, | |
| 89 int size_in_pixels, | |
| 90 const FallbackIconSpecs& specs, | |
| 91 const FaviconResultsCallback& callback, | |
| 92 base::CancelableTaskTracker* tracker) { | |
| 93 | |
| 94 FaviconRawBitmapResult result; | |
|
pkotwicz
2015/01/19 04:36:22
Having the fallback icon codepath separate from th
huangs
2015/01/20 22:20:41
Done.
| |
| 95 result.expired = false; | |
| 96 result.pixel_size.SetSize(size_in_pixels, size_in_pixels); | |
| 97 result.icon_url = icon_url; | |
| 98 result.icon_type = FAVICON; | |
| 99 | |
| 100 gfx::Canvas canvas(gfx::Size(size_in_pixels, size_in_pixels), 1.0f, false); | |
| 101 DrawFallbackIcon(icon_url, 0, 0, size_in_pixels, specs, &canvas); | |
| 102 | |
| 103 std::vector<unsigned char> bitmap_data; | |
| 104 if (gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(), | |
| 105 false, &bitmap_data)) { | |
| 106 result.bitmap_data = base::RefCountedBytes::TakeVector(&bitmap_data); | |
| 107 } | |
| 108 | |
| 109 return tracker->PostTask( | |
| 110 base::MessageLoopProxy::current().get(), | |
| 111 FROM_HERE, | |
| 112 Bind(callback, std::vector<FaviconRawBitmapResult>(1, result))); | |
| 113 } | |
| 114 | |
| 115 } // namespace favicon_base | |
| OLD | NEW |