Chromium Code Reviews| 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 0ffe0adf17e0b10b6f999a73923cb72753a5f130..aaf0978cbc66f9324b6c449607d2c9fba3c46ca2 100644 |
| --- a/chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc |
| +++ b/chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc |
| @@ -4,15 +4,16 @@ |
| #include "chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h" |
| +#include <vector> |
| + |
| #include "base/bind.h" |
| #include "base/callback.h" |
| #include "base/location.h" |
| #include "base/strings/string16.h" |
| -#include "base/task/cancelable_task_tracker.h" |
| #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| #include "chrome/browser/android/shortcut_helper.h" |
| #include "chrome/browser/favicon/favicon_service_factory.h" |
| -#include "chrome/browser/manifest/manifest_icon_downloader.h" |
| +#include "chrome/browser/installable/installable_manager.h" |
| #include "chrome/browser/manifest/manifest_icon_selector.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/chrome_constants.h" |
| @@ -24,7 +25,6 @@ |
| #include "content/public/browser/user_metrics.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/browser/web_contents_observer.h" |
| -#include "content/public/common/frame_navigate_params.h" |
| #include "content/public/common/manifest.h" |
| #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScreenOrientationLockType.h" |
| #include "ui/display/display.h" |
| @@ -33,7 +33,20 @@ |
| #include "ui/gfx/favicon_size.h" |
| #include "url/gurl.h" |
| -using content::Manifest; |
| +namespace { |
| + |
| +InstallableParams ParamsToPerformInstallableCheck(int ideal_icon_size_in_dp, |
| + int minimum_icon_size_in_dp) { |
| + // TODO(hanxi): change check_installable to true for WebAPKs. |
| + InstallableParams params; |
| + params.ideal_icon_size_in_dp = ideal_icon_size_in_dp; |
| + params.minimum_icon_size_in_dp = minimum_icon_size_in_dp; |
| + params.check_installable = false; |
| + params.fetch_valid_icon = true; |
| + return params; |
| +} |
| + |
| +} // namespace |
| AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| content::WebContents* web_contents, |
| @@ -47,7 +60,7 @@ AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| is_waiting_for_web_application_info_(false), |
| is_icon_saved_(false), |
| is_ready_(false), |
| - icon_timeout_timer_(false, false), |
| + data_timeout_timer_(false, false), |
| shortcut_info_(GetShortcutUrl(web_contents->GetURL())), |
| ideal_icon_size_in_dp_(ideal_icon_size_in_dp), |
| minimum_icon_size_in_dp_(minimum_icon_size_in_dp), |
| @@ -64,7 +77,8 @@ AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( |
| const WebApplicationInfo& received_web_app_info) { |
| is_waiting_for_web_application_info_ = false; |
| - if (!web_contents() || !weak_observer_) return; |
| + if (!web_contents() || !weak_observer_) |
| + return; |
| // Sanitize received_web_app_info. |
| WebApplicationInfo web_app_info = received_web_app_info; |
| @@ -101,57 +115,59 @@ void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( |
| break; |
| } |
| - web_contents()->GetManifest( |
| - base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this)); |
| + InstallableManager::CreateForWebContents(web_contents()); |
|
gone
2016/08/10 22:31:58
Is this a no-op if it's already happened once?
dominickn
2016/08/10 23:31:46
Yes, this does nothing if the manager has already
|
| + 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(4000), |
| + base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this, |
| + favicon_base::FaviconRawBitmapResult())); |
| + |
| + manager->GetData( |
| + ParamsToPerformInstallableCheck(ideal_icon_size_in_dp_, |
| + minimum_icon_size_in_dp_), |
| + base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck, |
| + this)); |
| } |
| -void AddToHomescreenDataFetcher::OnDidGetManifest( |
| - const GURL& manifest_url, |
| - const content::Manifest& manifest) { |
| - if (!web_contents() || !weak_observer_) return; |
| +void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck( |
| + const InstallableData& data) { |
| + if (!web_contents() || !weak_observer_) |
| + return; |
| - if (!manifest.IsEmpty()) { |
| + if (!data.manifest.IsEmpty()) { |
| content::RecordAction( |
| base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
| - shortcut_info_.UpdateFromManifest(manifest); |
| - shortcut_info_.manifest_url = manifest_url; |
| - } |
| - |
| - GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( |
| - manifest.icons, ideal_icon_size_in_dp_, minimum_icon_size_in_dp_); |
| - |
| - // If fetching the Manifest icon fails, fallback to the best favicon |
| - // for the page. |
| - if (!ManifestIconDownloader::Download( |
| - web_contents(), |
| - icon_src, |
| - ideal_icon_size_in_dp_, |
| - minimum_icon_size_in_dp_, |
| - base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, |
| - this, icon_src))) { |
| - FetchFavicon(); |
| + shortcut_info_.UpdateFromManifest(data.manifest); |
| + shortcut_info_.manifest_url = data.manifest_url; |
| } |
| // Save the splash screen URL for the later download. |
| splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon( |
| - manifest.icons, ideal_splash_image_size_in_dp_, |
| + data.manifest.icons, ideal_splash_image_size_in_dp_, |
| minimum_splash_image_size_in_dp_); |
| weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
| - // Kick off a timeout for downloading the icon. If an icon isn't set within |
| - // the timeout, fall back to using a dynamically-generated launcher icon. |
| - icon_timeout_timer_.Start(FROM_HERE, |
| - base::TimeDelta::FromMilliseconds(3000), |
| - base::Bind( |
| - &AddToHomescreenDataFetcher::OnFaviconFetched, |
| - this, |
| - favicon_base::FaviconRawBitmapResult())); |
| + if (data.icon && !data.icon->drawsNothing()) { |
| + // TODO(hanxi): implement WebAPK path if shortcut_info_.url has a secure |
| + // scheme and data.is_installable is true. |
| + shortcut_info_.icon_url = data.icon_url; |
| + NotifyObserver(*(data.icon)); |
| + return; |
| + } |
| + |
| + FetchFavicon(); |
| } |
| bool AddToHomescreenDataFetcher::OnMessageReceived( |
| const IPC::Message& message) { |
| - if (!is_waiting_for_web_application_info_) return false; |
| + if (!is_waiting_for_web_application_info_) |
| + return false; |
| bool handled = true; |
| @@ -176,21 +192,19 @@ base::Closure AddToHomescreenDataFetcher::FetchSplashScreenImageCallback( |
| } |
| void AddToHomescreenDataFetcher::FetchFavicon() { |
| - if (!web_contents() || !weak_observer_) return; |
| - |
| - Profile* profile = |
| - Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| + if (!web_contents() || !weak_observer_) |
| + return; |
| // Grab the best, largest icon we can find to represent this bookmark. |
| // TODO(dfalcantara): Try combining with the new BookmarksHandler once its |
| // rewrite is further along. |
| - std::vector<int> icon_types; |
| - icon_types.push_back(favicon_base::FAVICON); |
| - icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | |
| - favicon_base::TOUCH_ICON); |
| + std::vector<int> icon_types = { |
| + favicon_base::FAVICON, |
| + favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON}; |
|
gone
2016/08/10 22:31:58
Should }; go on the next line down? Honestly unsu
dominickn
2016/08/10 23:31:46
This is what clang-format gave me! Seems weird, bu
|
| favicon::FaviconService* favicon_service = |
| - FaviconServiceFactory::GetForProfile(profile, |
| - ServiceAccessType::EXPLICIT_ACCESS); |
| + FaviconServiceFactory::GetForProfile( |
| + Profile::FromBrowserContext(web_contents()->GetBrowserContext()), |
| + ServiceAccessType::EXPLICIT_ACCESS); |
| // Using favicon if its size is not smaller than platform required size, |
| // otherwise using the largest icon among all avaliable icons. |
| @@ -221,7 +235,8 @@ void AddToHomescreenDataFetcher::OnFaviconFetched( |
| void AddToHomescreenDataFetcher::CreateLauncherIcon( |
| const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| - if (!web_contents() || !weak_observer_) return; |
| + if (!web_contents() || !weak_observer_) |
| + return; |
| DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| SkBitmap icon_bitmap; |
| @@ -245,16 +260,6 @@ void AddToHomescreenDataFetcher::CreateLauncherIcon( |
| icon_bitmap)); |
| } |
| -void AddToHomescreenDataFetcher::OnManifestIconFetched(const GURL& icon_url, |
| - const SkBitmap& icon) { |
| - if (icon.drawsNothing()) { |
| - FetchFavicon(); |
| - return; |
| - } |
| - shortcut_info_.icon_url = icon_url; |
| - NotifyObserver(icon); |
| -} |
| - |
| void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| if (!web_contents() || !weak_observer_ || is_icon_saved_) |