From ced2f17446e45420a4d6072d67adce436e32a6de Mon Sep 17 00:00:00 2001 From: huangs Date: Wed, 25 Mar 2015 07:20:39 -0700 Subject: [Icons NTP] Refactoring: Moving FallbackIconService to components\favicon\core The class used to be in components\favicon_base. The DEP changes arise due to movement of fallback icon drawing code. BUG=467712 Review URL: https://codereview.chromium.org/1032683002 Cr-Commit-Position: refs/heads/master@{#322157} --- chrome/browser/ui/webui/fallback_icon_source.cc | 3 +- chrome/browser/ui/webui/fallback_icon_source.h | 4 +- chrome/browser/ui/webui/large_icon_source.cc | 3 +- chrome/browser/ui/webui/large_icon_source.h | 4 +- components/favicon.gypi | 2 + components/favicon/core/BUILD.gn | 2 + components/favicon/core/DEPS | 3 + components/favicon/core/fallback_icon_service.cc | 94 +++++++++++++++++++++++ components/favicon/core/fallback_icon_service.h | 49 ++++++++++++ components/favicon_base.gypi | 2 - components/favicon_base/BUILD.gn | 2 - components/favicon_base/DEPS | 1 - components/favicon_base/fallback_icon_service.cc | 97 ------------------------ components/favicon_base/fallback_icon_service.h | 51 ------------- 14 files changed, 156 insertions(+), 161 deletions(-) create mode 100644 components/favicon/core/fallback_icon_service.cc create mode 100644 components/favicon/core/fallback_icon_service.h delete mode 100644 components/favicon_base/fallback_icon_service.cc delete mode 100644 components/favicon_base/fallback_icon_service.h diff --git a/chrome/browser/ui/webui/fallback_icon_source.cc b/chrome/browser/ui/webui/fallback_icon_source.cc index 5ca4746..e568b9d 100644 --- a/chrome/browser/ui/webui/fallback_icon_source.cc +++ b/chrome/browser/ui/webui/fallback_icon_source.cc @@ -26,8 +26,7 @@ FallbackIconSource::FallbackIconSource() { #else font_list.push_back(l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY)); #endif - fallback_icon_service_.reset( - new favicon_base::FallbackIconService(font_list)); + fallback_icon_service_.reset(new FallbackIconService(font_list)); } FallbackIconSource::~FallbackIconSource() { diff --git a/chrome/browser/ui/webui/fallback_icon_source.h b/chrome/browser/ui/webui/fallback_icon_source.h index ba4843e..acc1d6b 100644 --- a/chrome/browser/ui/webui/fallback_icon_source.h +++ b/chrome/browser/ui/webui/fallback_icon_source.h @@ -6,7 +6,7 @@ #define CHROME_BROWSER_UI_WEBUI_FALLBACK_ICON_SOURCE_H_ #include "base/memory/scoped_ptr.h" -#include "components/favicon_base/fallback_icon_service.h" +#include "components/favicon/core/fallback_icon_service.h" #include "content/public/browser/url_data_source.h" // FallbackIconSource services explicit chrome:// requests for fallback icons. @@ -61,7 +61,7 @@ class FallbackIconSource : public content::URLDataSource { void SendDefaultResponse( const content::URLDataSource::GotDataCallback& callback); - scoped_ptr fallback_icon_service_; + scoped_ptr fallback_icon_service_; DISALLOW_COPY_AND_ASSIGN(FallbackIconSource); }; diff --git a/chrome/browser/ui/webui/large_icon_source.cc b/chrome/browser/ui/webui/large_icon_source.cc index 4dc270e..e28d342 100644 --- a/chrome/browser/ui/webui/large_icon_source.cc +++ b/chrome/browser/ui/webui/large_icon_source.cc @@ -51,8 +51,7 @@ LargeIconSource::LargeIconSource(Profile* profile) : profile_(profile) { #else font_list.push_back(l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY)); #endif - fallback_icon_service_.reset( - new favicon_base::FallbackIconService(font_list)); + fallback_icon_service_.reset(new FallbackIconService(font_list)); } LargeIconSource::~LargeIconSource() { diff --git a/chrome/browser/ui/webui/large_icon_source.h b/chrome/browser/ui/webui/large_icon_source.h index 7d5da6b..df3fad6 100644 --- a/chrome/browser/ui/webui/large_icon_source.h +++ b/chrome/browser/ui/webui/large_icon_source.h @@ -7,7 +7,7 @@ #include "base/memory/scoped_ptr.h" #include "base/task/cancelable_task_tracker.h" -#include "components/favicon_base/fallback_icon_service.h" +#include "components/favicon/core/fallback_icon_service.h" #include "components/favicon_base/favicon_types.h" #include "content/public/browser/url_data_source.h" @@ -73,7 +73,7 @@ class LargeIconSource : public content::URLDataSource { base::CancelableTaskTracker cancelable_task_tracker_; - scoped_ptr fallback_icon_service_; + scoped_ptr fallback_icon_service_; DISALLOW_COPY_AND_ASSIGN(LargeIconSource); }; diff --git a/components/favicon.gypi b/components/favicon.gypi index be91eeb..cddc746 100644 --- a/components/favicon.gypi +++ b/components/favicon.gypi @@ -19,6 +19,8 @@ ], 'sources': [ # Note: sources list duplicated in GN build. + 'favicon/core/fallback_icon_service.cc', + 'favicon/core/fallback_icon_service.h', 'favicon/core/favicon_client.h', 'favicon/core/favicon_driver.h', 'favicon/core/favicon_handler.cc', diff --git a/components/favicon/core/BUILD.gn b/components/favicon/core/BUILD.gn index f7ffefa..bbda145 100644 --- a/components/favicon/core/BUILD.gn +++ b/components/favicon/core/BUILD.gn @@ -4,6 +4,8 @@ static_library("core") { sources = [ + "fallback_icon_service.cc", + "fallback_icon_service.h", "favicon_client.h", "favicon_driver.h", "favicon_handler.cc", diff --git a/components/favicon/core/DEPS b/components/favicon/core/DEPS index 8a86e5b..ccb5231 100644 --- a/components/favicon/core/DEPS +++ b/components/favicon/core/DEPS @@ -2,6 +2,9 @@ include_rules = [ "+components/bookmarks/browser", "+components/history/core/browser", "+components/keyed_service/core", + "+net/base/registry_controlled_domains/registry_controlled_domain.h", "+skia", "+third_party/skia", + "+third_party/skia/include", + "+ui/gfx", ] diff --git a/components/favicon/core/fallback_icon_service.cc b/components/favicon/core/fallback_icon_service.cc new file mode 100644 index 0000000..e174818 --- /dev/null +++ b/components/favicon/core/fallback_icon_service.cc @@ -0,0 +1,94 @@ +// 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/core/fallback_icon_service.h" + +#include + +#include "base/i18n/case_conversion.h" +#include "base/strings/utf_string_conversions.h" +#include "components/favicon_base/fallback_icon_style.h" +#include "net/base/registry_controlled_domains/registry_controlled_domain.h" +#include "third_party/skia/include/core/SkPaint.h" +#include "ui/gfx/canvas.h" +#include "ui/gfx/codec/png_codec.h" +#include "ui/gfx/font_list.h" +#include "ui/gfx/geometry/rect.h" +#include "ui/gfx/geometry/size.h" +#include "url/gurl.h" + +namespace { + +// Arbitrary maximum icon size, can be reasonably increased if needed. +const int kMaxFallbackFaviconSize = 288; + +// Returns a single character to represent a page URL. To do this we take the +// first letter in a domain's name and make it upper case. +// TODO(huangs): Handle non-ASCII ("xn--") 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(domain.substr(0, 1))); +} + +} // namespace + +FallbackIconService::FallbackIconService( + const std::vector& font_list) + : font_list_(font_list) { +} + +FallbackIconService::~FallbackIconService() { +} + +std::vector FallbackIconService::RenderFallbackIconBitmap( + const GURL& icon_url, + int size, + const favicon_base::FallbackIconStyle& style) { + int size_to_use = std::min(kMaxFallbackFaviconSize, size); + gfx::Canvas canvas(gfx::Size(size_to_use, size_to_use), 1.0f, false); + DrawFallbackIcon(icon_url, size_to_use, style, &canvas); + + std::vector bitmap_data; + if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(), + false, &bitmap_data)) { + bitmap_data.clear(); + } + return bitmap_data; +} + +void FallbackIconService::DrawFallbackIcon( + const GURL& icon_url, + int size, + const favicon_base::FallbackIconStyle& style, + gfx::Canvas* canvas) { + const int kOffsetX = 0; + const int kOffsetY = 0; + SkPaint paint; + paint.setStyle(SkPaint::kFill_Style); + paint.setAntiAlias(true); + + // Draw a filled, colored rounded square. + paint.setColor(style.background_color); + int corner_radius = static_cast(size * style.roundness * 0.5 + 0.5); + canvas->DrawRoundRect( + gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint); + + // Draw text. + base::string16 icon_text = GetFallbackIconText(icon_url); + if (icon_text.empty()) + return; + int font_size = static_cast(size * style.font_size_ratio); + if (font_size <= 0) + return; + + // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache. + canvas->DrawStringRectWithFlags( + icon_text, + gfx::FontList(font_list_, gfx::Font::NORMAL, font_size), + style.text_color, + gfx::Rect(kOffsetX, kOffsetY, size, size), + gfx::Canvas::TEXT_ALIGN_CENTER); +} diff --git a/components/favicon/core/fallback_icon_service.h b/components/favicon/core/fallback_icon_service.h new file mode 100644 index 0000000..9b3f6ba --- /dev/null +++ b/components/favicon/core/fallback_icon_service.h @@ -0,0 +1,49 @@ +// 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. + +#ifndef COMPONENTS_FAVICON_CORE_FALLBACK_ICON_SERVICE_H_ +#define COMPONENTS_FAVICON_CORE_FALLBACK_ICON_SERVICE_H_ + +#include +#include + +#include "base/macros.h" + +class GURL; + +namespace gfx { +class Canvas; +} + +namespace favicon_base { +struct FallbackIconStyle; +} + +// A service to provide methods to render fallback favicons. +class FallbackIconService { + public: + explicit FallbackIconService(const std::vector& font_list); + ~FallbackIconService(); + + // Renders a fallback icon synchronously and returns the bitmap. Returns an + // empty std::vector on failure. |size| is icon width and height in pixels. + std::vector RenderFallbackIconBitmap( + const GURL& icon_url, + int size, + const favicon_base::FallbackIconStyle& style); + + private: + // Renders a fallback icon on |canvas| at position (|x|, |y|). |size| is icon + // width and height in pixels. + void DrawFallbackIcon(const GURL& icon_url, + int size, + const favicon_base::FallbackIconStyle& style, + gfx::Canvas* canvas); + + std::vector font_list_; + + DISALLOW_COPY_AND_ASSIGN(FallbackIconService); +}; + +#endif // COMPONENTS_FAVICON_CORE_FALLBACK_ICON_SERVICE_H_ diff --git a/components/favicon_base.gypi b/components/favicon_base.gypi index 0ea6331..852b9be 100644 --- a/components/favicon_base.gypi +++ b/components/favicon_base.gypi @@ -20,8 +20,6 @@ '../url/url.gyp:url_lib', ], 'sources': [ - 'favicon_base/fallback_icon_service.cc', - 'favicon_base/fallback_icon_service.h', 'favicon_base/fallback_icon_style.cc', 'favicon_base/fallback_icon_style.h', 'favicon_base/favicon_callback.h', diff --git a/components/favicon_base/BUILD.gn b/components/favicon_base/BUILD.gn index 07daa0a..2fcfb9c 100644 --- a/components/favicon_base/BUILD.gn +++ b/components/favicon_base/BUILD.gn @@ -4,8 +4,6 @@ source_set("favicon_base") { sources = [ - "fallback_icon_service.cc", - "fallback_icon_service.h", "fallback_icon_style.cc", "fallback_icon_style.h", "favicon_callback.h", diff --git a/components/favicon_base/DEPS b/components/favicon_base/DEPS index dc47487..0265afa 100644 --- a/components/favicon_base/DEPS +++ b/components/favicon_base/DEPS @@ -1,5 +1,4 @@ include_rules = [ - "+net/base/registry_controlled_domains/registry_controlled_domain.h", "+skia/ext", "+third_party/skia/include", "+ui/base", diff --git a/components/favicon_base/fallback_icon_service.cc b/components/favicon_base/fallback_icon_service.cc deleted file mode 100644 index adce72e..0000000 --- a/components/favicon_base/fallback_icon_service.cc +++ /dev/null @@ -1,97 +0,0 @@ -// 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 - -#include "base/i18n/case_conversion.h" -#include "base/strings/utf_string_conversions.h" -#include "components/favicon_base/fallback_icon_style.h" -#include "net/base/registry_controlled_domains/registry_controlled_domain.h" -#include "third_party/skia/include/core/SkPaint.h" -#include "ui/gfx/canvas.h" -#include "ui/gfx/codec/png_codec.h" -#include "ui/gfx/font_list.h" -#include "ui/gfx/geometry/rect.h" -#include "ui/gfx/geometry/size.h" -#include "url/gurl.h" - -namespace favicon_base { - -namespace { - -// Arbitrary maximum icon size, can be reasonably increased if needed. -const int kMaxFallbackFaviconSize = 288; - -// Returns a single character to represent a page URL. To do this we take the -// first letter in a domain's name and make it upper case. -// TODO(huangs): Handle non-ASCII ("xn--") 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(domain.substr(0, 1))); -} - -} // namespace - -FallbackIconService::FallbackIconService( - const std::vector& font_list) - : font_list_(font_list) { -} - -FallbackIconService::~FallbackIconService() { -} - -std::vector FallbackIconService::RenderFallbackIconBitmap( - const GURL& icon_url, - int size, - const FallbackIconStyle& style) { - int size_to_use = std::min(kMaxFallbackFaviconSize, size); - gfx::Canvas canvas(gfx::Size(size_to_use, size_to_use), 1.0f, false); - DrawFallbackIcon(icon_url, size_to_use, style, &canvas); - - std::vector bitmap_data; - if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(), - false, &bitmap_data)) { - bitmap_data.clear(); - } - return bitmap_data; -} - -void FallbackIconService::DrawFallbackIcon(const GURL& icon_url, - int size, - const FallbackIconStyle& style, - gfx::Canvas* canvas) { - const int kOffsetX = 0; - const int kOffsetY = 0; - SkPaint paint; - paint.setStyle(SkPaint::kFill_Style); - paint.setAntiAlias(true); - - // Draw a filled, colored rounded square. - paint.setColor(style.background_color); - int corner_radius = static_cast(size * style.roundness * 0.5 + 0.5); - canvas->DrawRoundRect( - gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint); - - // Draw text. - base::string16 icon_text = GetFallbackIconText(icon_url); - if (icon_text.empty()) - return; - int font_size = static_cast(size * style.font_size_ratio); - if (font_size <= 0) - return; - - // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache. - canvas->DrawStringRectWithFlags( - icon_text, - gfx::FontList(font_list_, gfx::Font::NORMAL, font_size), - style.text_color, - gfx::Rect(kOffsetX, kOffsetY, size, size), - gfx::Canvas::TEXT_ALIGN_CENTER); -} - -} // namespace favicon_base diff --git a/components/favicon_base/fallback_icon_service.h b/components/favicon_base/fallback_icon_service.h deleted file mode 100644 index cc98adf..0000000 --- a/components/favicon_base/fallback_icon_service.h +++ /dev/null @@ -1,51 +0,0 @@ -// 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. - -#ifndef COMPONENTS_FAVICON_BASE_FALLBACK_ICON_SERVICE_H_ -#define COMPONENTS_FAVICON_BASE_FALLBACK_ICON_SERVICE_H_ - -#include -#include - -#include "base/macros.h" - -class GURL; - -namespace gfx { -class Canvas; -} - -namespace favicon_base { - -struct FallbackIconStyle; - -// A service to provide methods to render fallback favicons. -class FallbackIconService { - public: - explicit FallbackIconService(const std::vector& font_list); - ~FallbackIconService(); - - // Renders a fallback icon synchronously and returns the bitmap. Returns an - // empty std::vector on failure. |size| is icon width and height in pixels. - std::vector RenderFallbackIconBitmap( - const GURL& icon_url, - int size, - const FallbackIconStyle& style); - - private: - // Renders a fallback icon on |canvas| at position (|x|, |y|). |size| is icon - // width and height in pixels. - void DrawFallbackIcon(const GURL& icon_url, - int size, - const FallbackIconStyle& style, - gfx::Canvas* canvas); - - std::vector font_list_; - - DISALLOW_COPY_AND_ASSIGN(FallbackIconService); -}; - -} // namespace favicon_base - -#endif // COMPONENTS_FAVICON_BASE_FALLBACK_ICON_SERVICE_H_ -- cgit v1.1