Index: chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc |
diff --git a/chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc b/chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc |
index 4e31565c909d2fabbdc0af12c90faf790e31dcd0..27298540429ad95d42934dd9daae03681ae7a365 100644 |
--- a/chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc |
+++ b/chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc |
@@ -32,9 +32,6 @@ |
namespace { |
-// The default number of milliseconds to wait for the data download to complete. |
-const int kDataTimeoutInMilliseconds = 4000; |
- |
// Looks up the original, online URL of the site requested. The URL from the |
// WebContents may be a distilled article which is not appropriate for a home |
// screen shortcut. |
@@ -43,7 +40,7 @@ GURL GetShortcutUrl(content::BrowserContext* browser_context, |
return dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); |
} |
-InstallableParams ParamsToPerformInstallableCheck( |
+InstallableParams ParamsToPerformManifestAndIconFetch( |
int ideal_icon_size_in_px, |
int minimum_icon_size_in_px, |
int badge_size_in_px, |
@@ -51,16 +48,22 @@ InstallableParams ParamsToPerformInstallableCheck( |
InstallableParams params; |
params.ideal_primary_icon_size_in_px = ideal_icon_size_in_px; |
params.minimum_primary_icon_size_in_px = minimum_icon_size_in_px; |
- params.check_installable = check_webapk_compatibility; |
params.fetch_valid_primary_icon = true; |
if (check_webapk_compatibility) { |
+ params.fetch_valid_badge_icon = true; |
params.ideal_badge_icon_size_in_px = badge_size_in_px; |
params.minimum_badge_icon_size_in_px = badge_size_in_px; |
- params.fetch_valid_badge_icon = true; |
} |
return params; |
} |
+InstallableParams ParamsToPerformInstallableCheck( |
+ bool check_webapk_compatibility) { |
+ InstallableParams params; |
+ params.check_installable = check_webapk_compatibility; |
+ return params; |
+} |
+ |
} // namespace |
AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
@@ -70,9 +73,11 @@ AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
int ideal_splash_image_size_in_px, |
int minimum_splash_image_size_in_px, |
int badge_size_in_px, |
+ int data_timeout_ms, |
bool check_webapk_compatibility, |
Observer* observer) |
: content::WebContentsObserver(web_contents), |
+ installable_manager_(InstallableManager::FromWebContents(web_contents)), |
weak_observer_(observer), |
shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(), |
web_contents->GetLastCommittedURL())), |
@@ -81,10 +86,10 @@ AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
ideal_splash_image_size_in_px_(ideal_splash_image_size_in_px), |
minimum_splash_image_size_in_px_(minimum_splash_image_size_in_px), |
badge_size_in_px_(badge_size_in_px), |
+ data_timeout_ms_(data_timeout_ms), |
check_webapk_compatibility_(check_webapk_compatibility), |
is_waiting_for_web_application_info_(true), |
- is_installable_check_complete_(false), |
- is_icon_saved_(false) { |
+ is_installable_check_complete_(false) { |
DCHECK(minimum_icon_size_in_px <= ideal_icon_size_in_px); |
DCHECK(minimum_splash_image_size_in_px <= ideal_splash_image_size_in_px); |
@@ -135,24 +140,17 @@ void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( |
break; |
} |
- InstallableManager::CreateForWebContents(web_contents()); |
- InstallableManager* manager = |
- InstallableManager::FromWebContents(web_contents()); |
- DCHECK(manager); |
- |
// Kick off a timeout for downloading data. If we haven't finished within the |
// timeout, fall back to using a dynamically-generated launcher icon. |
data_timeout_timer_.Start( |
- FROM_HERE, base::TimeDelta::FromMilliseconds(kDataTimeoutInMilliseconds), |
+ FROM_HERE, base::TimeDelta::FromMilliseconds(data_timeout_ms_), |
base::Bind(&AddToHomescreenDataFetcher::OnDataTimedout, this)); |
- manager->GetData( |
- ParamsToPerformInstallableCheck(ideal_icon_size_in_px_, |
- minimum_icon_size_in_px_, |
- badge_size_in_px_, |
- check_webapk_compatibility_), |
- base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck, |
- this)); |
+ installable_manager_->GetData( |
+ ParamsToPerformManifestAndIconFetch( |
+ ideal_icon_size_in_px_, minimum_icon_size_in_px_, badge_size_in_px_, |
+ check_webapk_compatibility_), |
+ base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifestAndIcons, this)); |
} |
AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { |
@@ -187,48 +185,34 @@ void AddToHomescreenDataFetcher::OnDataTimedout() { |
weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
} |
- badge_icon_.reset(); |
- CreateLauncherIcon(SkBitmap()); |
+ CreateLauncherIcon(raw_primary_icon_); |
} |
-void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck( |
+void AddToHomescreenDataFetcher::OnDidGetManifestAndIcons( |
const InstallableData& data) { |
- data_timeout_timer_.Stop(); |
- badge_icon_.reset(); |
- |
if (!web_contents() || !weak_observer_ || is_installable_check_complete_) |
return; |
- is_installable_check_complete_ = true; |
- |
- bool webapk_compatible = false; |
- if (check_webapk_compatibility_) { |
- webapk_compatible = (data.error_code == NO_ERROR_DETECTED && |
- AreWebManifestUrlsWebApkCompatible(data.manifest)); |
- weak_observer_->OnDidDetermineWebApkCompatibility(webapk_compatible); |
- |
- if (webapk_compatible) { |
- // WebAPKs are wholly defined by the Web Manifest. Ignore the <meta> tag |
- // data received in OnDidGetWebApplicationInfo(). |
- shortcut_info_ = ShortcutInfo(GURL()); |
- } |
- } |
- |
if (!data.manifest.IsEmpty()) { |
base::RecordAction(base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
shortcut_info_.UpdateFromManifest(data.manifest); |
shortcut_info_.manifest_url = data.manifest_url; |
+ } |
- if (webapk_compatible) { |
- shortcut_info_.UpdateSource(ShortcutInfo::SOURCE_ADD_TO_HOMESCREEN_PWA); |
- |
- if (data.badge_icon && !data.badge_icon->drawsNothing()) { |
- shortcut_info_.best_badge_icon_url = data.badge_icon_url; |
- badge_icon_ = *data.badge_icon; |
- } |
- } |
+ // Do this after updating from the manifest for the case where a site has |
+ // a manifest with name and standalone specified, but no icons. |
+ if (data.manifest.IsEmpty() || !data.primary_icon) { |
+ if (check_webapk_compatibility_) |
+ weak_observer_->OnDidDetermineWebApkCompatibility(false); |
+ weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
+ data_timeout_timer_.Stop(); |
+ FetchFavicon(); |
+ return; |
} |
+ raw_primary_icon_ = *data.primary_icon; |
+ shortcut_info_.best_primary_icon_url = data.primary_icon_url; |
+ |
// Save the splash screen URL for the later download. |
shortcut_info_.splash_image_url = |
content::ManifestIconSelector::FindBestMatchingIcon( |
@@ -238,20 +222,41 @@ void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck( |
shortcut_info_.ideal_splash_image_size_in_px = ideal_splash_image_size_in_px_; |
shortcut_info_.minimum_splash_image_size_in_px = |
minimum_splash_image_size_in_px_; |
+ if (data.badge_icon) { |
+ shortcut_info_.best_badge_icon_url = data.badge_icon_url; |
+ badge_icon_ = *data.badge_icon; |
+ } |
- weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
+ installable_manager_->GetData( |
+ ParamsToPerformInstallableCheck(check_webapk_compatibility_), |
+ base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck, |
+ this)); |
+} |
- if (data.primary_icon) { |
- shortcut_info_.best_primary_icon_url = data.primary_icon_url; |
+void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck( |
+ const InstallableData& data) { |
+ data_timeout_timer_.Stop(); |
- if (webapk_compatible) |
- NotifyObserver(*data.primary_icon); |
- else |
- CreateLauncherIcon(*(data.primary_icon)); |
+ if (!web_contents() || !weak_observer_ || is_installable_check_complete_) |
return; |
+ |
+ is_installable_check_complete_ = true; |
+ |
+ bool webapk_compatible = false; |
+ if (check_webapk_compatibility_) { |
+ webapk_compatible = |
+ (data.error_code == NO_ERROR_DETECTED && data.is_installable && |
+ AreWebManifestUrlsWebApkCompatible(data.manifest)); |
+ weak_observer_->OnDidDetermineWebApkCompatibility(webapk_compatible); |
} |
- FetchFavicon(); |
+ weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
+ if (webapk_compatible) { |
+ shortcut_info_.UpdateSource(ShortcutInfo::SOURCE_ADD_TO_HOMESCREEN_PWA); |
+ NotifyObserver(raw_primary_icon_); |
+ } else { |
+ CreateLauncherIcon(raw_primary_icon_); |
+ } |
} |
void AddToHomescreenDataFetcher::FetchFavicon() { |
@@ -283,7 +288,7 @@ void AddToHomescreenDataFetcher::OnFaviconFetched( |
const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
- if (!web_contents() || !weak_observer_ || is_icon_saved_) |
+ if (!web_contents() || !weak_observer_) |
return; |
// The user is waiting for the icon to be processed before they can proceed |
@@ -304,17 +309,17 @@ SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconFromFaviconInBackground( |
const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
base::ThreadRestrictions::AssertIOAllowed(); |
- SkBitmap raw_icon; |
if (bitmap_result.is_valid()) { |
gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), |
- bitmap_result.bitmap_data->size(), &raw_icon); |
+ bitmap_result.bitmap_data->size(), |
+ &raw_primary_icon_); |
} |
shortcut_info_.best_primary_icon_url = bitmap_result.icon_url; |
- return CreateLauncherIconInBackground(raw_icon); |
+ return CreateLauncherIconInBackground(raw_primary_icon_); |
} |
-void AddToHomescreenDataFetcher::CreateLauncherIcon(const SkBitmap& raw_icon) { |
+void AddToHomescreenDataFetcher::CreateLauncherIcon(const SkBitmap& icon) { |
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
// The user is waiting for the icon to be processed before they can proceed |
@@ -326,20 +331,20 @@ void AddToHomescreenDataFetcher::CreateLauncherIcon(const SkBitmap& raw_icon) { |
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}, |
base::BindOnce( |
&AddToHomescreenDataFetcher::CreateLauncherIconInBackground, |
- base::Unretained(this), raw_icon), |
+ base::Unretained(this), icon), |
base::BindOnce(&AddToHomescreenDataFetcher::NotifyObserver, |
base::RetainedRef(this))); |
} |
SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconInBackground( |
- const SkBitmap& raw_icon) { |
+ const SkBitmap& icon) { |
base::ThreadRestrictions::AssertIOAllowed(); |
SkBitmap primary_icon; |
bool is_generated = false; |
if (weak_observer_) { |
primary_icon = weak_observer_->FinalizeLauncherIconInBackground( |
- raw_icon, shortcut_info_.url, &is_generated); |
+ icon, shortcut_info_.url, &is_generated); |
} |
if (is_generated) |
@@ -350,10 +355,9 @@ SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconInBackground( |
void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& primary_icon) { |
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
- if (!web_contents() || !weak_observer_ || is_icon_saved_) |
+ if (!web_contents() || !weak_observer_) |
return; |
- is_icon_saved_ = true; |
primary_icon_ = primary_icon; |
weak_observer_->OnDataAvailable(shortcut_info_, primary_icon_, badge_icon_); |
} |