summaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorbeaudoin <beaudoin@chromium.org>2015-04-22 10:12:14 -0700
committerCommit bot <commit-bot@chromium.org>2015-04-22 17:13:08 +0000
commit3e75e59228ae3c4388379ed90ebea2e30ca0c646 (patch)
tree53afc9924fb90f3521f711b59262e692263d1d9c /components
parent5d19e06a5124f148e79838966cf9c9bfe68d3de6 (diff)
downloadchromium_src-3e75e59228ae3c4388379ed90ebea2e30ca0c646.zip
chromium_src-3e75e59228ae3c4388379ed90ebea2e30ca0c646.tar.gz
chromium_src-3e75e59228ae3c4388379ed90ebea2e30ca0c646.tar.bz2
[Icons NTP] Refactor large_icon_source to extract the logic shared between desktop and Android to a new large_icon_service.
This is required since the Android implementation of the icon-based NTP will rely on custom Java code to render the fallback. As a result we want to share the logic needed to retrieve large icons and to compute the fallback style without needing them to go through the full-featured chrome://large-icon that performs the rendering of fallback icons. The Java code will hook directly into the large_icon_service. This CL also fixes a bug, making sure only non-square icons can be returned as large icons. Besides this, this CL doesn't change the behavior but will make it possible to add the large icon selection logic to large_icon_service where it can be shared with the Android code. BUG=467712 Review URL: https://codereview.chromium.org/1092873002 Cr-Commit-Position: refs/heads/master@{#326325}
Diffstat (limited to 'components')
-rw-r--r--components/favicon.gypi2
-rw-r--r--components/favicon/core/BUILD.gn2
-rw-r--r--components/favicon/core/large_icon_service.cc77
-rw-r--r--components/favicon/core/large_icon_service.h65
-rw-r--r--components/favicon_base/fallback_icon_style.cc34
-rw-r--r--components/favicon_base/fallback_icon_style.h8
-rw-r--r--components/favicon_base/favicon_callback.h6
-rw-r--r--components/favicon_base/favicon_types.cc12
-rw-r--r--components/favicon_base/favicon_types.h19
9 files changed, 217 insertions, 8 deletions
diff --git a/components/favicon.gypi b/components/favicon.gypi
index d150316..d2f9195 100644
--- a/components/favicon.gypi
+++ b/components/favicon.gypi
@@ -36,6 +36,8 @@
'favicon/core/favicon_service.h',
'favicon/core/favicon_url.cc',
'favicon/core/favicon_url.h',
+ 'favicon/core/large_icon_service.cc',
+ 'favicon/core/large_icon_service.h',
],
'include_dirs': [
'..',
diff --git a/components/favicon/core/BUILD.gn b/components/favicon/core/BUILD.gn
index 7f9b762..f4e8a15e 100644
--- a/components/favicon/core/BUILD.gn
+++ b/components/favicon/core/BUILD.gn
@@ -19,6 +19,8 @@ static_library("core") {
"favicon_service.h",
"favicon_url.cc",
"favicon_url.h",
+ "large_icon_service.cc",
+ "large_icon_service.h",
]
deps = [
diff --git a/components/favicon/core/large_icon_service.cc b/components/favicon/core/large_icon_service.cc
new file mode 100644
index 0000000..702761c
--- /dev/null
+++ b/components/favicon/core/large_icon_service.cc
@@ -0,0 +1,77 @@
+// 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/large_icon_service.h"
+
+#include "components/favicon/core/favicon_service.h"
+#include "components/favicon_base/fallback_icon_style.h"
+#include "components/favicon_base/favicon_types.h"
+
+namespace favicon {
+
+LargeIconService::LargeIconService(FaviconService* favicon_service)
+ : favicon_service_(favicon_service) {
+ large_icon_types_.push_back(favicon_base::IconType::FAVICON);
+ large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON);
+ large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON);
+}
+
+LargeIconService::~LargeIconService() {
+}
+
+base::CancelableTaskTracker::TaskId
+ LargeIconService::GetLargeIconOrFallbackStyle(
+ const GURL& page_url,
+ int desired_size_in_pixel,
+ const favicon_base::LargeIconCallback& callback,
+ base::CancelableTaskTracker* tracker) {
+ // TODO(beaudoin): For now this is just a wrapper around
+ // GetLargestRawFaviconForPageURL. Add the logic required to select the best
+ // possible large icon. Also add logic to fetch-on-demand when the URL of
+ // a large icon is known but its bitmap is not available.
+ return favicon_service_->GetLargestRawFaviconForPageURL(
+ page_url,
+ large_icon_types_,
+ desired_size_in_pixel,
+ base::Bind(&LargeIconService::RunLargeIconCallback,
+ base::Unretained(this), callback, desired_size_in_pixel),
+ tracker);
+}
+
+void LargeIconService::RunLargeIconCallback(
+ const favicon_base::LargeIconCallback& callback,
+ int desired_size_in_pixel,
+ const favicon_base::FaviconRawBitmapResult& bitmap_result) {
+ // If there are no bitmaps, return a result with an empty |bitmap| and a
+ // default |fallback_icon_style|.
+ favicon_base::LargeIconResult result;
+ if (!bitmap_result.is_valid()) {
+ callback.Run(result);
+ return;
+ }
+
+ // If there is a bitmap but it's smaller than the requested size or
+ // non-square, compute its dominant color and use it as background in
+ // |fallback_icon_style|.
+ if (bitmap_result.pixel_size.width() < desired_size_in_pixel ||
+ bitmap_result.pixel_size.height() < desired_size_in_pixel ||
+ bitmap_result.pixel_size.width() != bitmap_result.pixel_size.height()) {
+ // TODO(beaudoin): Resize the icon if it's large enough. Alternatively,
+ // return it and let the HTML resize it.
+ result.fallback_icon_style.reset(new favicon_base::FallbackIconStyle());
+ favicon_base::SetDominantColorAsBackground(
+ bitmap_result.bitmap_data, result.fallback_icon_style.get());
+ callback.Run(result);
+ return;
+ }
+
+ // The bitmap is square and at least as large as the requested one, return
+ // it.
+ // TODO(beaudoin): Resize the icon if it's too large. Alternatively, return
+ // it and let the HTML resize it.
+ result.bitmap = bitmap_result;
+ callback.Run(result);
+}
+
+} // namespace favicon
diff --git a/components/favicon/core/large_icon_service.h b/components/favicon/core/large_icon_service.h
new file mode 100644
index 0000000..1063fd7
--- /dev/null
+++ b/components/favicon/core/large_icon_service.h
@@ -0,0 +1,65 @@
+// Copyright (c) 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_LARGE_ICON_SERVICE_H_
+#define COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_
+
+#include <vector>
+
+#include "base/task/cancelable_task_tracker.h"
+#include "components/favicon_base/favicon_callback.h"
+#include "components/keyed_service/core/keyed_service.h"
+
+class GURL;
+
+namespace favicon_base {
+struct FaviconRawBitmapResult;
+}
+
+namespace favicon {
+
+class FaviconService;
+
+// The large icon service provides methods to access large icons. It relies on
+// the favicon service.
+class LargeIconService : public KeyedService {
+ public:
+ explicit LargeIconService(FaviconService* favicon_service);
+
+ ~LargeIconService() override;
+
+ // Requests the best large icon for the page at |page_url| given the requested
+ // |desired_size_in_pixel|. If no good large icon can be found, returns the
+ // fallback style to use, for which the background is set to the dominant
+ // color of a smaller icon when one is available. This function returns the
+ // style of the fallback icon rather than the rendered version so that clients
+ // can render the icon themselves.
+ base::CancelableTaskTracker::TaskId GetLargeIconOrFallbackStyle(
+ const GURL& page_url,
+ int desired_size_in_pixel,
+ const favicon_base::LargeIconCallback& callback,
+ base::CancelableTaskTracker* tracker);
+
+ private:
+ // Intermediate callback for GetLargeIconOrFallbackStyle(). Ensures the large
+ // icon is at least the desired size, if not compute the icon fallback style
+ // and use it to invoke |callback|.
+ void RunLargeIconCallback(
+ const favicon_base::LargeIconCallback& callback,
+ int desired_size_in_pixel,
+ const favicon_base::FaviconRawBitmapResult& bitmap_result);
+
+ FaviconService* favicon_service_;
+
+ // A pre-populated list of the types of icon files to consider when looking
+ // for large icons. This is an optimization over populating an icon type
+ // vector on each request.
+ std::vector<int> large_icon_types_;
+
+ DISALLOW_COPY_AND_ASSIGN(LargeIconService);
+};
+
+} // namespace favicon
+
+#endif // COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_
diff --git a/components/favicon_base/fallback_icon_style.cc b/components/favicon_base/fallback_icon_style.cc
index e620613..d3e395b 100644
--- a/components/favicon_base/fallback_icon_style.cc
+++ b/components/favicon_base/fallback_icon_style.cc
@@ -4,6 +4,9 @@
#include "components/favicon_base/fallback_icon_style.h"
+#include <algorithm>
+
+#include "ui/gfx/color_analysis.h"
#include "ui/gfx/color_utils.h"
namespace favicon_base {
@@ -12,14 +15,19 @@ namespace {
// Luminance threshold for background color determine whether to use dark or
// light text color.
-int kDarkTextLuminanceThreshold = 190;
+const int kDarkTextLuminanceThreshold = 190;
+
+// The maximum luminance of the background color to ensure light text is
+// readable.
+const double kMaxBackgroundColorLuminance = 0.67;
// Default values for FallbackIconStyle.
-SkColor kDefaultBackgroundColor = SkColorSetRGB(0x80, 0x80, 0x80);
-SkColor kDefaultTextColorDark = SK_ColorBLACK;
-SkColor kDefaultTextColorLight = SK_ColorWHITE;
-double kDefaultFontSizeRatio = 0.8;
-double kDefaultRoundness = 0.125; // 1 / 8.
+const SkColor kDefaultBackgroundColor = SkColorSetRGB(0x78, 0x78, 0x78);
+const SkColor kDefaultTextColorDark = SK_ColorBLACK;
+const SkColor kDefaultTextColorLight = SK_ColorWHITE;
+const double kDefaultFontSizeRatio = 0.44;
+const double kDefaultRoundness = 0; // Square. Round corners are applied
+ // externally (Javascript or Java).
} // namespace
@@ -45,4 +53,18 @@ bool ValidateFallbackIconStyle(const FallbackIconStyle& style) {
style.roundness >= 0.0 && style.roundness <= 1.0;
}
+void SetDominantColorAsBackground(
+ const scoped_refptr<base::RefCountedMemory>& bitmap_data,
+ FallbackIconStyle* style) {
+ SkColor dominant_color =
+ color_utils::CalculateKMeanColorOfPNG(bitmap_data);
+ // Assumes |style.text_color| is light, and clamps luminance down to a
+ // reasonable maximum value so text is readable.
+ color_utils::HSL color_hsl;
+ color_utils::SkColorToHSL(dominant_color, &color_hsl);
+ color_hsl.l = std::min(color_hsl.l, kMaxBackgroundColorLuminance);
+ style->background_color =
+ color_utils::HSLToSkColor(color_hsl, SK_AlphaOPAQUE);
+}
+
} // namespace favicon_base
diff --git a/components/favicon_base/fallback_icon_style.h b/components/favicon_base/fallback_icon_style.h
index 097636b..84842ed 100644
--- a/components/favicon_base/fallback_icon_style.h
+++ b/components/favicon_base/fallback_icon_style.h
@@ -5,6 +5,7 @@
#ifndef COMPONENTS_FAVICON_BASE_FALLBACK_ICON_STYLE_H_
#define COMPONENTS_FAVICON_BASE_FALLBACK_ICON_STYLE_H_
+#include "base/memory/ref_counted_memory.h"
#include "third_party/skia/include/core/SkColor.h"
namespace favicon_base {
@@ -37,6 +38,13 @@ void MatchFallbackIconTextColorAgainstBackgroundColor(FallbackIconStyle* style);
// Returns whether |style| values are within bounds.
bool ValidateFallbackIconStyle(const FallbackIconStyle& style);
+// Set |style|'s background color to the dominant color of |bitmap_data|,
+// clamping luminance down to a reasonable maximum value so that light text is
+// readable.
+void SetDominantColorAsBackground(
+ const scoped_refptr<base::RefCountedMemory>& bitmap_data,
+ FallbackIconStyle* style);
+
} // namespace favicon_base
#endif // COMPONENTS_FAVICON_BASE_FALLBACK_ICON_STYLE_H_
diff --git a/components/favicon_base/favicon_callback.h b/components/favicon_base/favicon_callback.h
index 7616a11..96029f1 100644
--- a/components/favicon_base/favicon_callback.h
+++ b/components/favicon_base/favicon_callback.h
@@ -13,6 +13,7 @@ namespace favicon_base {
struct FaviconRawBitmapResult;
struct FaviconImageResult;
+struct LargeIconResult;
// Callback for functions that can be used to return a |gfx::Image| and the
// |GURL| it is loaded from. They are returned as a |FaviconImageResult| object.
@@ -29,6 +30,11 @@ typedef base::Callback<void(const FaviconRawBitmapResult&)>
typedef base::Callback<void(const std::vector<FaviconRawBitmapResult>&)>
FaviconResultsCallback;
+// Callback for functions returning data for a large icon. |LargeIconResult|
+// will contain either the raw bitmap for a large icon or the style of the
+// fallback to use if a sufficiently large icon could not be found.
+typedef base::Callback<void(const LargeIconResult&)> LargeIconCallback;
+
} // namespace favicon_base
#endif // COMPONENTS_FAVICON_BASE_FAVICON_CALLBACK_H_
diff --git a/components/favicon_base/favicon_types.cc b/components/favicon_base/favicon_types.cc
index 0188f1e..e69f1d7 100644
--- a/components/favicon_base/favicon_types.cc
+++ b/components/favicon_base/favicon_types.cc
@@ -4,6 +4,8 @@
#include "components/favicon_base/favicon_types.h"
+#include "components/favicon_base/fallback_icon_style.h"
+
namespace favicon_base {
// ---------------------------------------------------------
@@ -20,7 +22,13 @@ FaviconRawBitmapResult::FaviconRawBitmapResult()
: expired(false), icon_type(INVALID_ICON) {
}
-FaviconRawBitmapResult::~FaviconRawBitmapResult() {
-}
+FaviconRawBitmapResult::~FaviconRawBitmapResult() {}
+
+// --------------------------------------------------------
+// LargeIconResult
+
+LargeIconResult::LargeIconResult() {}
+
+LargeIconResult::~LargeIconResult() {}
} // namespace favicon_base
diff --git a/components/favicon_base/favicon_types.h b/components/favicon_base/favicon_types.h
index ea64754..f7c812a 100644
--- a/components/favicon_base/favicon_types.h
+++ b/components/favicon_base/favicon_types.h
@@ -6,12 +6,15 @@
#define COMPONENTS_FAVICON_BASE_FAVICON_TYPES_H_
#include "base/memory/ref_counted_memory.h"
+#include "base/memory/scoped_ptr.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"
namespace favicon_base {
+struct FallbackIconStyle;
+
typedef int64 FaviconID;
// Defines the icon types. They are also stored in icon_type field of favicons
@@ -72,6 +75,22 @@ struct FaviconRawBitmapResult {
// HistoryBackend::SetFavicons().
typedef FaviconRawBitmapResult FaviconRawBitmapData;
+// Result returned by LargeIconService::GetLargeIconOrFallbackStyle(). Contains
+// either the bitmap data if the favicon database has a sufficiently large
+// favicon bitmap and the style of the fallback icon otherwise.
+struct LargeIconResult {
+ LargeIconResult();
+ ~LargeIconResult();
+
+ // The bitmap from the favicon database if the database has a sufficiently
+ // large one.
+ FaviconRawBitmapResult bitmap;
+
+ // The fallback icon style if a sufficiently large icon isn't available. This
+ // uses the dominant color of a smaller icon as the background if available.
+ scoped_ptr<FallbackIconStyle> fallback_icon_style;
+};
+
} // namespace favicon_base
#endif // COMPONENTS_FAVICON_BASE_FAVICON_TYPES_H_