Commit 4bb5896c authored by zpeng's avatar zpeng Committed by Commit bot

Skip installation process if WebAPK is already installed.

BUG=638614

Review-Url: https://codereview.chromium.org/2363183002
Cr-Commit-Position: refs/heads/master@{#422677}
parent 2a6b869d
......@@ -458,16 +458,22 @@ public class ShortcutHelper {
}
/**
* Returns true if WebAPKs are enabled and there is a WebAPK installed which can handle
* Returns the package name of the WebAPK if WebAPKs are enabled and there is an installed
* WebAPK which can handle {@link url}. Returns null otherwise.
*/
@CalledByNative
private static String queryWebApkPackage(String url) {
if (!ChromeWebApkHost.isEnabled()) return null;
return WebApkValidator.queryWebApkPackage(ContextUtils.getApplicationContext(), url);
}
/**
* Returns true if WebAPKs are enabled and there is an installed WebAPK which can handle
* {@link url}.
*/
@CalledByNative
private static boolean isWebApkInstalled(String url) {
if (!ChromeWebApkHost.isEnabled()) {
return false;
}
return WebApkValidator.queryWebApkPackage(ContextUtils.getApplicationContext(), url)
!= null;
return queryWebApkPackage(url) != null;
}
/**
......
......@@ -191,26 +191,36 @@ bool AppBannerInfoBarDelegateAndroid::AcceptWebApk(
return true;
}
// Request install the WebAPK.
install_state_ = INSTALLING;
TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED);
webapk::TrackInstallSource(webapk_install_source_);
AppBannerSettingsHelper::RecordBannerInstallEvent(
web_contents, shortcut_info_->url.spec(), AppBannerSettingsHelper::WEB);
// Check whether the WebAPK has been installed.
std::string installed_webapk_package_name =
ShortcutHelper::QueryWebApkPackage(web_contents->GetLastCommittedURL());
if (installed_webapk_package_name.empty()) {
// Request install the WebAPK.
install_state_ = INSTALLING;
TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED);
webapk::TrackInstallSource(webapk_install_source_);
AppBannerSettingsHelper::RecordBannerInstallEvent(
web_contents, shortcut_info_->url.spec(), AppBannerSettingsHelper::WEB);
Java_AppBannerInfoBarDelegateAndroid_setWebApkInstallingState(
env, java_delegate_, true);
UpdateInstallState(env, nullptr);
WebApkInstaller::FinishCallback callback =
base::Bind(&AppBannerInfoBarDelegateAndroid::OnWebApkInstallFinished,
weak_ptr_factory_.GetWeakPtr());
ShortcutHelper::InstallWebApkWithSkBitmap(web_contents->GetBrowserContext(),
*shortcut_info_,
*icon_.get(), callback);
SendBannerAccepted(web_contents, "web");
Java_AppBannerInfoBarDelegateAndroid_setWebApkInstallingState(
env, java_delegate_, true);
UpdateInstallState(env, nullptr);
WebApkInstaller::FinishCallback callback =
base::Bind(&AppBannerInfoBarDelegateAndroid::OnWebApkInstallFinished,
weak_ptr_factory_.GetWeakPtr());
ShortcutHelper::InstallWebApkWithSkBitmap(web_contents->GetBrowserContext(),
*shortcut_info_,
*icon_.get(), callback);
SendBannerAccepted(web_contents, "web");
// Prevent the infobar from disappearing, because the infobar will show
// "Adding" during the installation process.
return false;
}
// Prevent the infobar from disappearing, because the infobar will show
// "Adding" during the installation process.
// Bypass the installation since WebAPK is already installed.
TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED);
OnWebApkInstallFinished(true, installed_webapk_package_name);
return false;
}
......
......@@ -243,6 +243,22 @@ SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground(
: SkBitmap();
}
// static
std::string ShortcutHelper::QueryWebApkPackage(const GURL& url) {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jstring> java_url =
base::android::ConvertUTF8ToJavaString(env, url.spec());
ScopedJavaLocalRef<jstring> java_webapk_package_name =
Java_ShortcutHelper_queryWebApkPackage(env, java_url);
std::string webapk_package_name = "";
if (java_webapk_package_name.obj()) {
webapk_package_name = base::android::ConvertJavaStringToUTF8(
env, java_webapk_package_name);
}
return webapk_package_name;
}
// static
bool ShortcutHelper::IsWebApkInstalled(const GURL& url) {
JNIEnv* env = base::android::AttachCurrentThread();
......
......@@ -96,7 +96,11 @@ class ShortcutHelper {
const GURL& url,
bool* is_generated);
// Returns true if WebAPKs are enabled and there is a WebAPK installed which
// Returns the package name of the WebAPK if WebAPKs are enabled and there is
// an installed WebAPK which can handle |url|. Returns empty string otherwise.
static std::string QueryWebApkPackage(const GURL& url);
// Returns true if WebAPKs are enabled and there is an installed WebAPK which
// can handle |url|.
static bool IsWebApkInstalled(const GURL& url);
......
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