Commit 8e9c8333 authored by Robbie McElrath's avatar Robbie McElrath Committed by Commit Bot

[WebLayer] Move Site Settings favicon lookup to SiteSettingsClient

This CL moves favicon loading into SiteSettingsClient so uses of the
currently uncomponentized FaviconHelper can be encapsulated in
ChromeSiteSettingsClient. The SiteSettingsClient method defined here may
change or go away in the future depending on how we componentize
FaviconHelper.

Bug: 1058597
Change-Id: I27f8ec33e18942a55e15865d4fd21280c187e4f6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2136283
Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
Reviewed-by: default avatarFinnur Thorarinsson <finnur@chromium.org>
Cr-Commit-Position: refs/heads/master@{#758984}
parent c67c5b5e
......@@ -271,8 +271,8 @@ public class ChosenObjectSettings extends SiteSettingsPreferenceFragment {
for (int i = 0; i < mSites.size() && i < mObjectInfos.size(); ++i) {
Website site = mSites.get(i);
ChosenObjectInfo info = mObjectInfos.get(i);
WebsitePreference preference =
new WebsitePreference(getStyledContext(), site, mCategory);
WebsitePreference preference = new WebsitePreference(
getStyledContext(), getSiteSettingsClient(), site, mCategory);
preference.getExtras().putSerializable(SingleWebsiteSettings.EXTRA_SITE, site);
preference.setFragment(SingleWebsiteSettings.class.getCanonicalName());
......
......@@ -5,12 +5,16 @@
package org.chromium.chrome.browser.site_settings;
import android.app.Activity;
import android.graphics.Bitmap;
import androidx.preference.Preference;
import org.chromium.base.Callback;
import org.chromium.chrome.browser.help.HelpAndFeedback;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.settings.ChromeManagedPreferenceDelegate;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper.FaviconImageCallback;
import org.chromium.components.browser_ui.settings.ManagedPreferenceDelegate;
/**
......@@ -37,4 +41,42 @@ public class ChromeSiteSettingsClient implements SiteSettingsClient {
HelpAndFeedback.getInstance().show(
currentActivity, helpContext, Profile.getLastUsedRegularProfile(), null);
}
@Override
public void getLocalFaviconImageForURL(
String faviconUrl, int faviconSizePx, Callback<Bitmap> callback) {
new FaviconLoader(faviconUrl, faviconSizePx, callback);
}
/**
* A helper class that groups a FaviconHelper with its corresponding Callback.
*
* This object is kept alive by being passed to the native
* FaviconHelper.getLocalFaviconImageForURL. Its reference will be released after the callback
* has been called.
*/
private static class FaviconLoader implements FaviconImageCallback {
// Loads the favicons asynchronously.
private final FaviconHelper mFaviconHelper;
private final Callback<Bitmap> mCallback;
private FaviconLoader(String faviconUrl, int faviconSizePx, Callback<Bitmap> callback) {
mCallback = callback;
mFaviconHelper = new FaviconHelper();
// TODO(https://crbug.com/1048632): Use the current profile (i.e., regular profile or
// incognito profile) instead of always using regular profile. It works correctly now,
// but it is not safe.
if (!mFaviconHelper.getLocalFaviconImageForURL(
Profile.getLastUsedRegularProfile(), faviconUrl, faviconSizePx, this)) {
onFaviconAvailable(/*image=*/null, faviconUrl);
}
}
@Override
public void onFaviconAvailable(Bitmap image, String iconUrl) {
mFaviconHelper.destroy();
mCallback.onResult(image);
}
}
}
......@@ -720,7 +720,8 @@ public class SingleCategorySettings extends SiteSettingsPreferenceFragment
// Find origins matching the current search.
for (Website site : sites) {
if (mSearch == null || mSearch.isEmpty() || site.getTitle().contains(mSearch)) {
websites.add(new WebsitePreference(getStyledContext(), site, mCategory));
websites.add(new WebsitePreference(
getStyledContext(), getSiteSettingsClient(), site, mCategory));
}
}
......
......@@ -5,7 +5,9 @@
package org.chromium.chrome.browser.site_settings;
import android.app.Activity;
import android.graphics.Bitmap;
import org.chromium.base.Callback;
import org.chromium.components.browser_ui.settings.ManagedPreferenceDelegate;
/**
......@@ -23,4 +25,18 @@ public interface SiteSettingsClient {
* @see org.chromium.chrome.browser.help.HelpAndFeedback#show
*/
void launchHelpAndFeedbackActivity(Activity currentActivity, String helpContext);
/**
* Asynchronously looks up the locally cached favicon image for the given URL.
*
* @param faviconUrl The URL of the page to get the favicon for. If a favicon for the full URL
* can't be found, the favicon for its host will be used as a fallback.
* @param faviconSizePx The expected size of the favicon in pixels. If a favicon of this size
* doesn't exist, the cached favicon closest to this size will be resized to a square of
* this dimension.
* @param callback A callback that will be called with the favicon bitmap, or null if no
* favicon could be found locally.
*/
void getLocalFaviconImageForURL(
String faviconUrl, int faviconSizePx, Callback<Bitmap> callback);
}
......@@ -19,9 +19,6 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper.FaviconImageCallback;
import org.chromium.components.browser_ui.settings.ChromeImageViewPreference;
import org.chromium.components.favicon.FaviconFallbackGenerator;
......@@ -31,15 +28,13 @@ import org.chromium.components.favicon.FaviconFallbackGenerator;
* of the preference. See {@link ChromeImageViewPreference} for more details on how this icon
* can be used.
*/
class WebsitePreference extends ChromeImageViewPreference implements FaviconImageCallback {
class WebsitePreference extends ChromeImageViewPreference {
private final SiteSettingsClient mSiteSettingsClient;
private final Website mSite;
private final SiteSettingsCategory mCategory;
private static final int TEXT_SIZE_SP = 13;
// Loads the favicons asynchronously.
private FaviconHelper mFaviconHelper;
// Whether the favicon has been fetched already.
private boolean mFaviconFetched;
......@@ -53,8 +48,10 @@ class WebsitePreference extends ChromeImageViewPreference implements FaviconImag
private int mFaviconSizePx;
WebsitePreference(Context context, Website site, SiteSettingsCategory category) {
WebsitePreference(Context context, SiteSettingsClient siteSettingsClient, Website site,
SiteSettingsCategory category) {
super(context);
mSiteSettingsClient = siteSettingsClient;
mSite = site;
mCategory = category;
setWidgetLayoutResource(R.layout.website_features);
......@@ -85,26 +82,6 @@ class WebsitePreference extends ChromeImageViewPreference implements FaviconImag
return mSite;
}
@Override
public void onFaviconAvailable(Bitmap image, String iconUrl) {
mFaviconHelper.destroy();
mFaviconHelper = null;
Resources resources = getContext().getResources();
if (image == null) {
// Invalid favicon, produce a generic one.
float density = resources.getDisplayMetrics().density;
int faviconSizeDp = Math.round(mFaviconSizePx / density);
FaviconFallbackGenerator faviconGenerator =
new FaviconFallbackGenerator(resources, faviconSizeDp, faviconSizeDp,
Math.round(FAVICON_CORNER_RADIUS_FRACTION * faviconSizeDp),
FAVICON_BACKGROUND_COLOR,
Math.round(FAVICON_TEXT_SIZE_FRACTION * faviconSizeDp));
image = faviconGenerator.generateIconForUrl(faviconUrl());
}
setIcon(new BitmapDrawable(resources, image));
}
/**
* Returns the url of the site to fetch a favicon for.
*/
......@@ -166,14 +143,8 @@ class WebsitePreference extends ChromeImageViewPreference implements FaviconImag
if (!mFaviconFetched) {
// Start the favicon fetching. Will respond in onFaviconAvailable.
mFaviconHelper = new FaviconHelper();
// TODO(https://crbug.com/1048632): Use the current profile (i.e., regular profile or
// incognito profile) instead of always using regular profile. It works correctly now,
// but it is not safe.
if (!mFaviconHelper.getLocalFaviconImageForURL(
Profile.getLastUsedRegularProfile(), faviconUrl(), mFaviconSizePx, this)) {
onFaviconAvailable(null, null);
}
mSiteSettingsClient.getLocalFaviconImageForURL(
faviconUrl(), mFaviconSizePx, this::onFaviconAvailable);
mFaviconFetched = true;
}
......@@ -182,4 +153,21 @@ class WebsitePreference extends ChromeImageViewPreference implements FaviconImag
View iconView = holder.findViewById(android.R.id.icon);
iconView.setPadding(iconPadding, iconPadding, iconPadding, iconPadding);
}
private void onFaviconAvailable(Bitmap image) {
Resources resources = getContext().getResources();
if (image == null) {
// Invalid favicon, produce a generic one.
float density = resources.getDisplayMetrics().density;
int faviconSizeDp = Math.round(mFaviconSizePx / density);
FaviconFallbackGenerator faviconGenerator =
new FaviconFallbackGenerator(resources, faviconSizeDp, faviconSizeDp,
Math.round(FAVICON_CORNER_RADIUS_FRACTION * faviconSizeDp),
FAVICON_BACKGROUND_COLOR,
Math.round(FAVICON_TEXT_SIZE_FRACTION * faviconSizeDp));
image = faviconGenerator.generateIconForUrl(faviconUrl());
}
setIcon(new BitmapDrawable(resources, image));
}
}
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