Adding Test Fixture for initial test cases for the App Remoting Test Driver. Also...
[chromium-blink-merge.git] / components / favicon_base / fallback_icon_service.cc
blobadce72e105f6a1fce671c62fb27dabcecfb0ef87
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.
5 #include "components/favicon_base/fallback_icon_service.h"
7 #include <algorithm>
9 #include "base/i18n/case_conversion.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/favicon_base/fallback_icon_style.h"
12 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
13 #include "third_party/skia/include/core/SkPaint.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/codec/png_codec.h"
16 #include "ui/gfx/font_list.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/geometry/size.h"
19 #include "url/gurl.h"
21 namespace favicon_base {
23 namespace {
25 // Arbitrary maximum icon size, can be reasonably increased if needed.
26 const int kMaxFallbackFaviconSize = 288;
28 // Returns a single character to represent a page URL. To do this we take the
29 // first letter in a domain's name and make it upper case.
30 // TODO(huangs): Handle non-ASCII ("xn--") domain names.
31 base::string16 GetFallbackIconText(const GURL& url) {
32 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
33 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
34 return domain.empty() ? base::string16() :
35 base::i18n::ToUpper(base::ASCIIToUTF16(domain.substr(0, 1)));
38 } // namespace
40 FallbackIconService::FallbackIconService(
41 const std::vector<std::string>& font_list)
42 : font_list_(font_list) {
45 FallbackIconService::~FallbackIconService() {
48 std::vector<unsigned char> FallbackIconService::RenderFallbackIconBitmap(
49 const GURL& icon_url,
50 int size,
51 const FallbackIconStyle& style) {
52 int size_to_use = std::min(kMaxFallbackFaviconSize, size);
53 gfx::Canvas canvas(gfx::Size(size_to_use, size_to_use), 1.0f, false);
54 DrawFallbackIcon(icon_url, size_to_use, style, &canvas);
56 std::vector<unsigned char> bitmap_data;
57 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(),
58 false, &bitmap_data)) {
59 bitmap_data.clear();
61 return bitmap_data;
64 void FallbackIconService::DrawFallbackIcon(const GURL& icon_url,
65 int size,
66 const FallbackIconStyle& style,
67 gfx::Canvas* canvas) {
68 const int kOffsetX = 0;
69 const int kOffsetY = 0;
70 SkPaint paint;
71 paint.setStyle(SkPaint::kFill_Style);
72 paint.setAntiAlias(true);
74 // Draw a filled, colored rounded square.
75 paint.setColor(style.background_color);
76 int corner_radius = static_cast<int>(size * style.roundness * 0.5 + 0.5);
77 canvas->DrawRoundRect(
78 gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint);
80 // Draw text.
81 base::string16 icon_text = GetFallbackIconText(icon_url);
82 if (icon_text.empty())
83 return;
84 int font_size = static_cast<int>(size * style.font_size_ratio);
85 if (font_size <= 0)
86 return;
88 // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache.
89 canvas->DrawStringRectWithFlags(
90 icon_text,
91 gfx::FontList(font_list_, gfx::Font::NORMAL, font_size),
92 style.text_color,
93 gfx::Rect(kOffsetX, kOffsetY, size, size),
94 gfx::Canvas::TEXT_ALIGN_CENTER);
97 } // namespace favicon_base