Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / favicon / core / fallback_icon_service.cc
blob69cf7ff8a05736226009b5cfc8754ab2b8059208
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/core/fallback_icon_service.h"
7 #include <algorithm>
9 #include "components/favicon/core/fallback_icon_client.h"
10 #include "components/favicon_base/fallback_icon_style.h"
11 #include "third_party/skia/include/core/SkPaint.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/codec/png_codec.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/size.h"
17 #include "url/gurl.h"
19 namespace favicon {
20 namespace {
22 // Arbitrary maximum icon size, can be reasonably increased if needed.
23 const int kMaxFallbackFaviconSize = 288;
25 } // namespace
27 FallbackIconService::FallbackIconService(
28 FallbackIconClient* fallback_icon_client)
29 : fallback_icon_client_(fallback_icon_client) {
32 FallbackIconService::~FallbackIconService() {
35 std::vector<unsigned char> FallbackIconService::RenderFallbackIconBitmap(
36 const GURL& icon_url,
37 int size,
38 const favicon_base::FallbackIconStyle& style) {
39 int size_to_use = std::min(kMaxFallbackFaviconSize, size);
40 gfx::Canvas canvas(gfx::Size(size_to_use, size_to_use), 1.0f, false);
41 DrawFallbackIcon(icon_url, size_to_use, style, &canvas);
43 std::vector<unsigned char> bitmap_data;
44 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(),
45 false, &bitmap_data)) {
46 bitmap_data.clear();
48 return bitmap_data;
51 void FallbackIconService::DrawFallbackIcon(
52 const GURL& icon_url,
53 int size,
54 const favicon_base::FallbackIconStyle& style,
55 gfx::Canvas* canvas) {
56 const int kOffsetX = 0;
57 const int kOffsetY = 0;
58 SkPaint paint;
59 paint.setStyle(SkPaint::kFill_Style);
60 paint.setAntiAlias(true);
62 // Draw a filled, colored rounded square.
63 paint.setColor(style.background_color);
64 int corner_radius = static_cast<int>(size * style.roundness * 0.5 + 0.5);
65 canvas->DrawRoundRect(
66 gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint);
68 // Draw text.
69 base::string16 icon_text =
70 fallback_icon_client_->GetFallbackIconText(icon_url);
71 if (icon_text.empty())
72 return;
73 int font_size = static_cast<int>(size * style.font_size_ratio);
74 if (font_size <= 0)
75 return;
77 canvas->DrawStringRectWithFlags(
78 icon_text,
79 gfx::FontList(fallback_icon_client_->GetFontNameList(), gfx::Font::NORMAL,
80 font_size),
81 style.text_color,
82 gfx::Rect(kOffsetX, kOffsetY, size, size),
83 gfx::Canvas::TEXT_ALIGN_CENTER);
86 } // namespace favicon