| 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 9915636f241e48499c8874c53958b90b84505cec..039eddf33e8982a3ecddef390e37aaad99ab7461 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"
|
| @@ -20,11 +21,11 @@
|
| #include "chrome/common/web_application_info.h"
|
| #include "components/dom_distiller/core/url_utils.h"
|
| #include "components/favicon/core/favicon_service.h"
|
| +#include "components/favicon_base/favicon_types.h"
|
| #include "content/public/browser/browser_thread.h"
|
| #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 +34,38 @@
|
| #include "ui/gfx/favicon_size.h"
|
| #include "url/gurl.h"
|
|
|
| -using content::Manifest;
|
| +namespace {
|
| +
|
| +// Looks up the original, online URL of the site requested. The URL from the
|
| +// WebContents may be an offline page or a distilled article which is not
|
| +// appropriate for a home screen shortcut.
|
| +GURL GetShortcutUrl(content::BrowserContext* browser_context,
|
| + const GURL& actual_url) {
|
| + GURL original_url =
|
| + dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url);
|
| +
|
| + // If URL points to an offline content, get original URL.
|
| + GURL online_url =
|
| + offline_pages::OfflinePageUtils::MaybeGetOnlineURLForOfflineURL(
|
| + browser_context, original_url);
|
| + if (online_url.is_valid())
|
| + return online_url;
|
| +
|
| + return original_url;
|
| +}
|
| +
|
| +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,
|
| @@ -44,27 +76,28 @@ AddToHomescreenDataFetcher::AddToHomescreenDataFetcher(
|
| Observer* observer)
|
| : WebContentsObserver(web_contents),
|
| weak_observer_(observer),
|
| - is_waiting_for_web_application_info_(false),
|
| - is_icon_saved_(false),
|
| - is_ready_(false),
|
| - icon_timeout_timer_(false, false),
|
| - shortcut_info_(GetShortcutUrl(web_contents->GetURL())),
|
| + shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(),
|
| + web_contents->GetLastCommittedURL())),
|
| + data_timeout_timer_(false, false),
|
| ideal_icon_size_in_dp_(ideal_icon_size_in_dp),
|
| minimum_icon_size_in_dp_(minimum_icon_size_in_dp),
|
| ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp),
|
| - minimum_splash_image_size_in_dp_(minimum_splash_image_size_in_dp) {
|
| + minimum_splash_image_size_in_dp_(minimum_splash_image_size_in_dp),
|
| + is_waiting_for_web_application_info_(true),
|
| + is_icon_saved_(false),
|
| + is_ready_(false) {
|
| DCHECK(minimum_icon_size_in_dp <= ideal_icon_size_in_dp);
|
| DCHECK(minimum_splash_image_size_in_dp <= ideal_splash_image_size_in_dp);
|
|
|
| // Send a message to the renderer to retrieve information about the page.
|
| - is_waiting_for_web_application_info_ = true;
|
| Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id()));
|
| }
|
|
|
| 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 +134,58 @@ void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo(
|
| break;
|
| }
|
|
|
| - web_contents()->GetManifest(
|
| - base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this));
|
| + 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(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.
|
| + NotifyObserver(data.icon_url, *(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 +210,20 @@ 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};
|
| +
|
| 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.
|
| @@ -243,15 +276,6 @@ void AddToHomescreenDataFetcher::CreateLauncherIconInBackground(
|
| icon_bitmap));
|
| }
|
|
|
| -void AddToHomescreenDataFetcher::OnManifestIconFetched(const GURL& icon_url,
|
| - const SkBitmap& icon) {
|
| - if (icon.drawsNothing()) {
|
| - FetchFavicon();
|
| - return;
|
| - }
|
| - NotifyObserver(icon_url, icon);
|
| -}
|
| -
|
| void AddToHomescreenDataFetcher::NotifyObserver(const GURL& icon_url,
|
| const SkBitmap& bitmap) {
|
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
| @@ -264,17 +288,3 @@ void AddToHomescreenDataFetcher::NotifyObserver(const GURL& icon_url,
|
| is_ready_ = true;
|
| weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_);
|
| }
|
| -
|
| -GURL AddToHomescreenDataFetcher::GetShortcutUrl(const GURL& actual_url) {
|
| - GURL original_url =
|
| - dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url);
|
| -
|
| - // If URL points to an offline content, get original URL.
|
| - GURL online_url =
|
| - offline_pages::OfflinePageUtils::MaybeGetOnlineURLForOfflineURL(
|
| - web_contents()->GetBrowserContext(), original_url);
|
| - if (online_url.is_valid())
|
| - return online_url;
|
| -
|
| - return original_url;
|
| -}
|
|
|