Chromium Code Reviews| Index: components/favicon_base/fallback_icon_service.cc |
| diff --git a/components/favicon_base/fallback_icon_service.cc b/components/favicon_base/fallback_icon_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1aa6b2376ee19d807332e8455bfc4f68989636a7 |
| --- /dev/null |
| +++ b/components/favicon_base/fallback_icon_service.cc |
| @@ -0,0 +1,115 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/favicon_base/fallback_icon_service.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/i18n/case_conversion.h" |
| +#include "base/location.h" |
| +#include "base/message_loop/message_loop_proxy.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "components/favicon_base/favicon_types.h" |
| +#include "grit/platform_locale_settings.h" |
| +#include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| +#include "third_party/skia/include/core/SkPaint.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/gfx/codec/png_codec.h" |
| +#include "ui/gfx/favicon_size.h" |
| +#include "ui/gfx/font_list.h" |
| +#include "ui/gfx/geometry/rect.h" |
| +#include "ui/gfx/geometry/size.h" |
| +#include "ui/gfx/image/image_skia.h" |
| +#include "url/gurl.h" |
| + |
| +namespace favicon_base { |
| + |
| +namespace { |
| + |
| +// Returns a single character to represent a fallback URL. To do this we take |
| +// the first letter in a domain's name and make it upper case. Currently we do |
| +// not handle non-ASCII domain names. |
| +base::string16 GetFallbackIconText(const GURL& url) { |
| + std::string domain = net::registry_controlled_domains::GetDomainAndRegistry( |
| + url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| + return domain.empty() ? base::string16() : |
| + base::i18n::ToUpper(base::ASCIIToUTF16(std::string(1, domain[0]))); |
| +} |
| + |
| +} // namespace |
| + |
| +FallbackIconService::FallbackIconService() { |
| + std::string font_name = |
| + l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY); |
| +#if defined(OS_CHROMEOS) |
| + const std::string kChromeOSFontFamily = "Noto Sans"; |
| + font_name = kChromeOSFontFamily; |
| +#endif |
| + 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
|
| +} |
| + |
| +FallbackIconService::~FallbackIconService() { |
| +} |
| + |
| +void FallbackIconService::DrawFallbackIcon(const GURL& icon_url, |
| + int x, |
| + int y, |
| + int size_in_pixels, |
| + const FallbackIconSpecs& specs, |
| + gfx::Canvas* canvas) { |
| + SkPaint paint; |
| + paint.setStyle(SkPaint::kFill_Style); |
| + paint.setAntiAlias(true); |
| + |
| + // Draw a filled, colored rounded square. |
| + paint.setColor(specs.background_color); |
| + int corner_radius = static_cast<int>( |
| + size_in_pixels * specs.corner_radius_ratio + 0.5); |
| + canvas->DrawRoundRect(gfx::Rect(x, y, size_in_pixels, size_in_pixels), |
| + corner_radius, paint); |
| + |
| + // Draw text. |
| + base::string16 icon_text = GetFallbackIconText(icon_url); |
| + if (!icon_text.empty()) { |
| + int font_size = static_cast<int>(size_in_pixels * specs.font_size_ratio); |
| + if (font_size > 0) { |
| + canvas->DrawStringRectWithFlags( |
| + icon_text, |
| + gfx::FontList(fallback_icon_font_list_, gfx::Font::NORMAL, font_size), |
| + specs.text_color, |
| + gfx::Rect(0, 0, size_in_pixels, size_in_pixels), |
| + gfx::Canvas::TEXT_ALIGN_CENTER); |
| + } |
| + } |
| +} |
| + |
| +base::CancelableTaskTracker::TaskId FallbackIconService::GetFallbackIcon( |
| + const GURL& icon_url, |
| + int size_in_pixels, |
| + const FallbackIconSpecs& specs, |
| + const FaviconResultsCallback& callback, |
| + base::CancelableTaskTracker* tracker) { |
| + |
| + 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.
|
| + result.expired = false; |
| + result.pixel_size.SetSize(size_in_pixels, size_in_pixels); |
| + result.icon_url = icon_url; |
| + result.icon_type = FAVICON; |
| + |
| + gfx::Canvas canvas(gfx::Size(size_in_pixels, size_in_pixels), 1.0f, false); |
| + DrawFallbackIcon(icon_url, 0, 0, size_in_pixels, specs, &canvas); |
| + |
| + std::vector<unsigned char> bitmap_data; |
| + if (gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(), |
| + false, &bitmap_data)) { |
| + result.bitmap_data = base::RefCountedBytes::TakeVector(&bitmap_data); |
| + } |
| + |
| + return tracker->PostTask( |
| + base::MessageLoopProxy::current().get(), |
| + FROM_HERE, |
| + Bind(callback, std::vector<FaviconRawBitmapResult>(1, result))); |
| +} |
| + |
| +} // namespace favicon_base |