Commit e0df3c81 authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

favicons: refactor common functions

This moves some duplicate code to CoreFaviconService where it can
be shared.

BUG=1076463
TEST=none

Change-Id: I0f6a26221acf189e57cd12a16d2624bd598917b8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2339388
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#795202}
parent 78321b3d
......@@ -4,6 +4,7 @@
static_library("core") {
sources = [
"core_favicon_service.cc",
"core_favicon_service.h",
"fallback_url_util.cc",
"fallback_url_util.h",
......
// Copyright 2020 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/core_favicon_service.h"
#include "base/stl_util.h"
#include "components/favicon_base/favicon_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
namespace favicon {
// static
std::vector<int> CoreFaviconService::GetPixelSizesForFaviconScales(
int size_in_dip) {
// NOTE: GetFaviconScales() always returns 1x on android.
std::vector<float> scales = favicon_base::GetFaviconScales();
std::vector<int> sizes_in_pixel;
for (float scale : scales)
sizes_in_pixel.push_back(std::ceil(size_in_dip * scale));
return sizes_in_pixel;
}
// static
std::vector<SkBitmap> CoreFaviconService::ExtractSkBitmapsToStore(
const gfx::Image& image) {
gfx::ImageSkia image_skia = image.AsImageSkia();
image_skia.EnsureRepsForSupportedScales();
std::vector<SkBitmap> bitmaps;
const std::vector<float> favicon_scales = favicon_base::GetFaviconScales();
for (const gfx::ImageSkiaRep& rep : image_skia.image_reps()) {
// Only save images with a supported sale.
if (base::Contains(favicon_scales, rep.scale()))
bitmaps.push_back(rep.GetBitmap());
}
return bitmaps;
}
} // namespace favicon
......@@ -5,6 +5,8 @@
#ifndef COMPONENTS_FAVICON_CORE_CORE_FAVICON_SERVICE_H_
#define COMPONENTS_FAVICON_CORE_CORE_FAVICON_SERVICE_H_
#include <vector>
#include "base/callback.h"
#include "base/containers/flat_set.h"
#include "base/task/cancelable_task_tracker.h"
......@@ -14,6 +16,11 @@
#include "components/keyed_service/core/keyed_service.h"
class GURL;
class SkBitmap;
namespace gfx {
class Image;
}
namespace favicon {
......@@ -97,6 +104,14 @@ class CoreFaviconService : public KeyedService {
virtual void UnableToDownloadFavicon(const GURL& icon_url) = 0;
virtual void ClearUnableToDownloadFavicons() = 0;
virtual bool WasUnableToDownloadFavicon(const GURL& icon_url) const = 0;
protected:
// Returns a vector of pixel edge sizes from |size_in_dip| and
// GetFaviconScales().
static std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip);
// Returns a vector of the bitmaps to store for the specified image.
static std::vector<SkBitmap> ExtractSkBitmapsToStore(const gfx::Image& image);
};
} // namespace favicon
......
......@@ -20,39 +20,9 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"
namespace favicon {
namespace {
// Returns a vector of pixel edge sizes from |size_in_dip| and
// favicon_base::GetFaviconScales().
std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) {
std::vector<float> scales = favicon_base::GetFaviconScales();
std::vector<int> sizes_in_pixel;
for (size_t i = 0; i < scales.size(); ++i) {
sizes_in_pixel.push_back(std::ceil(size_in_dip * scales[i]));
}
return sizes_in_pixel;
}
std::vector<SkBitmap> ExtractSkBitmapsToStore(const gfx::Image& image) {
gfx::ImageSkia image_skia = image.AsImageSkia();
image_skia.EnsureRepsForSupportedScales();
const std::vector<gfx::ImageSkiaRep>& image_reps = image_skia.image_reps();
std::vector<SkBitmap> bitmaps;
const std::vector<float> favicon_scales = favicon_base::GetFaviconScales();
for (size_t i = 0; i < image_reps.size(); ++i) {
// Don't save if the scale isn't one of supported favicon scales.
if (!base::Contains(favicon_scales, image_reps[i].scale()))
continue;
bitmaps.push_back(image_reps[i].GetBitmap());
}
return bitmaps;
}
} // namespace
FaviconServiceImpl::FaviconServiceImpl(
std::unique_ptr<FaviconClient> favicon_client,
......
......@@ -11,15 +11,12 @@
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/hash/hash.h"
#include "base/stl_util.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "components/favicon_base/favicon_util.h"
#include "components/favicon_base/select_favicon_frames.h"
#include "content/public/common/url_constants.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"
#include "weblayer/browser/favicon/favicon_backend_wrapper.h"
#include "weblayer/browser/favicon/favicon_service_impl_observer.h"
......@@ -27,35 +24,6 @@
namespace weblayer {
namespace {
// TODO(sky): refactor this to common place (also in
// favicon::FaviconServiceImpl).
// Returns a vector of pixel edge sizes from |size_in_dip| and
// favicon_base::GetFaviconScales().
std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) {
// NOTE: GetFaviconScales() always returns 1x on android.
std::vector<float> scales = favicon_base::GetFaviconScales();
std::vector<int> sizes_in_pixel;
for (float scale : scales)
sizes_in_pixel.push_back(std::ceil(size_in_dip * scale));
return sizes_in_pixel;
}
// TODO(sky): refactor this to common place (also in
// favicon::FaviconServiceImpl).
std::vector<SkBitmap> ExtractSkBitmapsToStore(const gfx::Image& image) {
gfx::ImageSkia image_skia = image.AsImageSkia();
image_skia.EnsureRepsForSupportedScales();
std::vector<SkBitmap> bitmaps;
const std::vector<float> favicon_scales = favicon_base::GetFaviconScales();
for (const gfx::ImageSkiaRep& rep : image_skia.image_reps()) {
// Don't save if the scale isn't one of supported favicon scales.
if (!base::Contains(favicon_scales, rep.scale()))
continue;
bitmaps.push_back(rep.GetBitmap());
}
return bitmaps;
}
bool CanAddUrl(const GURL& url) {
if (!url.is_valid())
return false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment