Commit 0f70028f authored by Xi Han's avatar Xi Han Committed by Commit Bot

Webapp offline dialog handles more network errors

Currently the offline dialog only handles ERR_INTERNET_DISCONNECTED error, and
we would like it to catch up more network errors. A generic message is added
to shown on the dialog.

Bug: 789566
Change-Id: I9c36319241f8754822e6336eca8ae13379edc764
Reviewed-on: https://chromium-review.googlesource.com/803835
Commit-Queue: Xi Han <hanxi@chromium.org>
Reviewed-by: default avatarPeter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarIlya Sherman <isherman@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524066}
parent c06b3100
...@@ -229,6 +229,11 @@ public class WebApkUma { ...@@ -229,6 +229,11 @@ public class WebApkUma {
RecordHistogram.recordBooleanHistogram("WebApk.WebApkService.BindSuccess", bindSucceeded); RecordHistogram.recordBooleanHistogram("WebApk.WebApkService.BindSuccess", bindSucceeded);
} }
/** Records the network error code caught when a WebAPK is launched. */
public static void recordNetworkErrorWhenLaunch(int errorCode) {
RecordHistogram.recordSparseSlowlyHistogram("WebApk.Launch.NetworkError", -errorCode);
}
/** /**
* Log necessary disk usage and cache size UMAs when WebAPK installation fails. * Log necessary disk usage and cache size UMAs when WebAPK installation fails.
*/ */
......
...@@ -7,11 +7,11 @@ package org.chromium.chrome.browser.webapps; ...@@ -7,11 +7,11 @@ package org.chromium.chrome.browser.webapps;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import org.chromium.base.ApiCompatibilityUtils; import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.net.NetError;
/** /**
* A dialog to notify users that WebAPKs and Trusted Web Activities need a network connection to * A dialog to notify users that WebAPKs and Trusted Web Activities need a network connection to
...@@ -25,19 +25,17 @@ public class WebappOfflineDialog { ...@@ -25,19 +25,17 @@ public class WebappOfflineDialog {
* @param activity Activity that will be used for {@link Dialog#show()}. * @param activity Activity that will be used for {@link Dialog#show()}.
* @param appName The name of the Android native client for which the dialog is shown. * @param appName The name of the Android native client for which the dialog is shown.
* @param isWebApk Whether the app above is a WebAPK. * @param isWebApk Whether the app above is a WebAPK.
* @param errorCode The network error code.
*/ */
public void show(final Activity activity, String appName, boolean isWebApk) { public void show(final Activity activity, String appName, boolean isWebApk, int errorCode) {
@StringRes int messageID = isWebApk
? R.string.webapk_offline_dialog : R.string.webapp_twa_offline_dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.AlertDialogTheme); AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.AlertDialogTheme);
builder.setMessage(activity.getString(messageID, appName)) builder.setMessage(getErrorDescription(activity, appName, isWebApk, errorCode))
.setPositiveButton(R.string.ok, .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
new DialogInterface.OnClickListener() { @Override
@Override public void onClick(DialogInterface dialog, int which) {
public void onClick(DialogInterface dialog, int which) { ApiCompatibilityUtils.finishAndRemoveTask(activity);
ApiCompatibilityUtils.finishAndRemoveTask(activity); }
} });
});
mDialog = builder.create(); mDialog = builder.create();
mDialog.setCanceledOnTouchOutside(false); mDialog.setCanceledOnTouchOutside(false);
...@@ -48,4 +46,19 @@ public class WebappOfflineDialog { ...@@ -48,4 +46,19 @@ public class WebappOfflineDialog {
public void cancel() { public void cancel() {
mDialog.cancel(); mDialog.cancel();
} }
private String getErrorDescription(
Activity activity, String appName, boolean isWebApk, int errorCode) {
switch (errorCode) {
case NetError.ERR_INTERNET_DISCONNECTED:
int messageID = isWebApk ? R.string.webapk_offline_dialog
: R.string.webapp_twa_offline_dialog;
return activity.getString(messageID, appName);
case NetError.ERR_TUNNEL_CONNECTION_FAILED:
return activity.getString(
R.string.webapp_network_error_message_tunnel_connection_failed);
default:
return activity.getString(R.string.webapp_cannot_connect_to_site);
}
}
} }
...@@ -18,6 +18,7 @@ import org.chromium.base.ContextUtils; ...@@ -18,6 +18,7 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.compositor.CompositorViewHolder; import org.chromium.chrome.browser.compositor.CompositorViewHolder;
import org.chromium.chrome.browser.metrics.WebApkUma;
import org.chromium.chrome.browser.metrics.WebappUma; import org.chromium.chrome.browser.metrics.WebappUma;
import org.chromium.chrome.browser.tab.EmptyTabObserver; import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
...@@ -28,6 +29,9 @@ import org.chromium.net.NetworkChangeNotifier; ...@@ -28,6 +29,9 @@ import org.chromium.net.NetworkChangeNotifier;
/** Shows and hides splash screen. */ /** Shows and hides splash screen. */
class WebappSplashScreenController extends EmptyTabObserver { class WebappSplashScreenController extends EmptyTabObserver {
// No error.
public static final int ERROR_OK = 0;
/** Used to schedule splash screen hiding. */ /** Used to schedule splash screen hiding. */
private CompositorViewHolder mCompositorViewHolder; private CompositorViewHolder mCompositorViewHolder;
...@@ -144,24 +148,24 @@ class WebappSplashScreenController extends EmptyTabObserver { ...@@ -144,24 +148,24 @@ class WebappSplashScreenController extends EmptyTabObserver {
boolean isErrorPage, boolean hasCommitted, boolean isSameDocument, boolean isErrorPage, boolean hasCommitted, boolean isSameDocument,
boolean isFragmentNavigation, Integer pageTransition, int errorCode, boolean isFragmentNavigation, Integer pageTransition, int errorCode,
int httpStatusCode) { int httpStatusCode) {
if (mActivityType == WebappActivity.ACTIVITY_TYPE_WEBAPP) return; if (mActivityType == WebappActivity.ACTIVITY_TYPE_WEBAPP || !isInMainFrame) return;
mErrorCode = errorCode; mErrorCode = errorCode;
switch (mErrorCode) { switch (mErrorCode) {
case NetError.ERR_NETWORK_CHANGED: case ERROR_OK:
onNetworkChanged(tab);
break;
case NetError.ERR_INTERNET_DISCONNECTED:
onNetworkDisconnected(tab);
break;
default:
if (mOfflineDialog != null) { if (mOfflineDialog != null) {
mOfflineDialog.cancel(); mOfflineDialog.cancel();
mOfflineDialog = null; mOfflineDialog = null;
} }
break; break;
case NetError.ERR_NETWORK_CHANGED:
onNetworkChanged(tab);
break;
default:
onNetworkError(tab, errorCode);
break;
} }
WebApkUma.recordNetworkErrorWhenLaunch(-errorCode);
} }
protected boolean canHideSplashScreen() { protected boolean canHideSplashScreen() {
...@@ -181,7 +185,7 @@ class WebappSplashScreenController extends EmptyTabObserver { ...@@ -181,7 +185,7 @@ class WebappSplashScreenController extends EmptyTabObserver {
mAllowReloads = false; mAllowReloads = false;
} }
private void onNetworkDisconnected(final Tab tab) { private void onNetworkError(final Tab tab, int errorCode) {
if (mOfflineDialog != null || tab.getActivity() == null) return; if (mOfflineDialog != null || tab.getActivity() == null) return;
final NetworkChangeNotifier.ConnectionTypeObserver observer = final NetworkChangeNotifier.ConnectionTypeObserver observer =
...@@ -199,8 +203,8 @@ class WebappSplashScreenController extends EmptyTabObserver { ...@@ -199,8 +203,8 @@ class WebappSplashScreenController extends EmptyTabObserver {
NetworkChangeNotifier.addConnectionTypeObserver(observer); NetworkChangeNotifier.addConnectionTypeObserver(observer);
mOfflineDialog = new WebappOfflineDialog(); mOfflineDialog = new WebappOfflineDialog();
mOfflineDialog.show( mOfflineDialog.show(tab.getActivity(), mAppName,
tab.getActivity(), mAppName, mActivityType == WebappActivity.ACTIVITY_TYPE_WEBAPK); mActivityType == WebappActivity.ACTIVITY_TYPE_WEBAPK, errorCode);
} }
/** Sets the splash screen layout and sets the splash screen's title and icon. */ /** Sets the splash screen layout and sets the splash screen's title and icon. */
......
...@@ -3049,6 +3049,12 @@ You must have Bluetooth and Location turned on in order to use the Physical Web. ...@@ -3049,6 +3049,12 @@ You must have Bluetooth and Location turned on in order to use the Physical Web.
<message name="IDS_WEBAPK_NOTIFICATION_CHANNEL_NAME" desc="The visible name of the notification channel of WebAPKs on Android O+."> <message name="IDS_WEBAPK_NOTIFICATION_CHANNEL_NAME" desc="The visible name of the notification channel of WebAPKs on Android O+.">
General General
</message> </message>
<message name="IDS_WEBAPP_CANNOT_CONNECT_TO_SITE" desc="The message on the dialog shown when launching a WebAPK or a TWA with network failure.">
Can't connect to the site
</message>
<message name="IDS_WEBAPP_NETWORK_ERROR_MESSAGE_TUNNEL_CONNECTION_FAILED" desc="The error message for ERROR_TUNNEL_CONNECTION_FAILED.">
Establishing a tunnel via proxy server failed
</message>
<!-- Keyboard shortcuts in Android N--> <!-- Keyboard shortcuts in Android N-->
<message name="IDS_KEYBOARD_SHORTCUT_OPEN_NEW_TAB" desc="A text label that appears next to the keyboard shortcut to open a new tab in Chrome. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=55]"> <message name="IDS_KEYBOARD_SHORTCUT_OPEN_NEW_TAB" desc="A text label that appears next to the keyboard shortcut to open a new tab in Chrome. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=55]">
......
...@@ -94504,6 +94504,15 @@ http://cs/file:chrome/histograms.xml - but prefer this file for new entries. ...@@ -94504,6 +94504,15 @@ http://cs/file:chrome/histograms.xml - but prefer this file for new entries.
</summary> </summary>
</histogram> </histogram>
<histogram name="WebApk.Launch.NetworkError" enum="NetErrorCodes">
<owner>hanxi@chromium.org</owner>
<owner>pkotwicz@chromium.org</owner>
<owner>yfriedman@chromium.org</owner>
<summary>
Records the error codes when a WebAPK is launched with network failures.
</summary>
</histogram>
<histogram name="WebApk.LaunchInterval" units="ms"> <histogram name="WebApk.LaunchInterval" units="ms">
<owner>hanxi@chromium.org</owner> <owner>hanxi@chromium.org</owner>
<owner>pkotwicz@chromium.org</owner> <owner>pkotwicz@chromium.org</owner>
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