Commit f557bc87 authored by Alexey Baskakov's avatar Alexey Baskakov Committed by Commit Bot

dpwa: Reintroduce GenerateIconLetterFromUrl fallback to generate icons.

We still need it: the title can be empty in some scenarios.

Looks like we need to generate some icon based on url
to show this icon for a user in web app install dialog, where
user should enter a non-empty title.

1) WebAppInstallTask::OnIconsRetrievedShowDialog()
2) FilterAndResizeIconsGenerateMissing()
3) Show dialog with the generated icon, `title` is still empty here.

This is a partial revert of
https://chromium-review.googlesource.com/c/chromium/src/+/2312056

The title populating code is left there in unit tests.

Bug: 1109540, 1103570
Change-Id: Ia281397d3483ad08d02ae4a9f0e2ca60bd34e859
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2319493Reviewed-by: default avatarAlan Cutter <alancutter@chromium.org>
Commit-Queue: Alexey Baskakov <loyso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791776}
parent c803826e
...@@ -177,6 +177,27 @@ SkBitmap GenerateBitmap(SquareSizePx output_size, ...@@ -177,6 +177,27 @@ SkBitmap GenerateBitmap(SquareSizePx output_size,
return dst; return dst;
} }
base::char16 GenerateIconLetterFromUrl(const GURL& app_url) {
std::string app_url_part = " ";
const std::string domain_and_registry =
net::registry_controlled_domains::GetDomainAndRegistry(
app_url,
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
if (!domain_and_registry.empty()) {
app_url_part = domain_and_registry;
} else if (app_url.has_host()) {
app_url_part = app_url.host();
}
// Translate punycode into unicode before retrieving the first letter.
const base::string16 string_for_display =
url_formatter::IDNToUnicode(app_url_part);
base::char16 icon_letter = base::i18n::ToUpper(string_for_display)[0];
return icon_letter;
}
base::char16 GenerateIconLetterFromAppName(const base::string16& app_name) { base::char16 GenerateIconLetterFromAppName(const base::string16& app_name) {
CHECK(!app_name.empty()); CHECK(!app_name.empty());
return base::i18n::ToUpper(app_name)[0]; return base::i18n::ToUpper(app_name)[0];
......
...@@ -55,6 +55,10 @@ SkBitmap GenerateBitmap(SquareSizePx output_size, ...@@ -55,6 +55,10 @@ SkBitmap GenerateBitmap(SquareSizePx output_size,
SkColor color, SkColor color,
base::char16 icon_letter); base::char16 icon_letter);
// Returns the first letter from |app_url| that will be painted on the generated
// icon.
base::char16 GenerateIconLetterFromUrl(const GURL& app_url);
// Returns the first letter from |app_name| that will be painted on the // Returns the first letter from |app_name| that will be painted on the
// generated icon. // generated icon.
base::char16 GenerateIconLetterFromAppName(const base::string16& app_name); base::char16 GenerateIconLetterFromAppName(const base::string16& app_name);
......
...@@ -354,6 +354,17 @@ TEST_F(WebAppIconGeneratorTest, IconsResizedWhenOnlyAGigantorOneIsProvided) { ...@@ -354,6 +354,17 @@ TEST_F(WebAppIconGeneratorTest, IconsResizedWhenOnlyAGigantorOneIsProvided) {
TestIconGeneration(icon_size::k512, 0, 3); TestIconGeneration(icon_size::k512, 0, 3);
} }
TEST_F(WebAppIconGeneratorTest, GenerateIconLetterFromUrl) {
// ASCII:
EXPECT_EQ('E', GenerateIconLetterFromUrl(GURL("http://example.com")));
// Cyrillic capital letter ZHE for something like https://zhuk.rf:
EXPECT_EQ(0x0416,
GenerateIconLetterFromUrl(GURL("https://xn--f1ai0a.xn--p1ai/")));
// Arabic:
EXPECT_EQ(0x0645,
GenerateIconLetterFromUrl(GURL("http://xn--mgbh0fb.example/")));
}
TEST_F(WebAppIconGeneratorTest, GenerateIconLetterFromAppName) { TEST_F(WebAppIconGeneratorTest, GenerateIconLetterFromAppName) {
// ASCII Encoding // ASCII Encoding
EXPECT_EQ('T', EXPECT_EQ('T',
......
...@@ -282,9 +282,10 @@ void FilterAndResizeIconsGenerateMissing(WebApplicationInfo* web_app_info, ...@@ -282,9 +282,10 @@ void FilterAndResizeIconsGenerateMissing(WebApplicationInfo* web_app_info,
} }
FilterSquareIconsFromBitmaps(web_app_info->icon_bitmaps_any, &square_icons); FilterSquareIconsFromBitmaps(web_app_info->icon_bitmaps_any, &square_icons);
DCHECK(!web_app_info->title.empty()); base::char16 icon_letter =
base::char16 icon_letter = GenerateIconLetterFromAppName(web_app_info->title); web_app_info->title.empty()
? GenerateIconLetterFromUrl(web_app_info->app_url)
: GenerateIconLetterFromAppName(web_app_info->title);
web_app_info->generated_icon_color = SK_ColorTRANSPARENT; web_app_info->generated_icon_color = SK_ColorTRANSPARENT;
// TODO(https://crbug.com/1029223): Don't resize before writing to disk, it's // TODO(https://crbug.com/1029223): Don't resize before writing to disk, it's
// not necessary and would simplify this code path to remove. // not necessary and would simplify this code path to remove.
......
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