| Index: chrome/android/java/src/org/chromium/chrome/browser/feedback/ConnectivityChecker.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/feedback/ConnectivityChecker.java b/chrome/android/java/src/org/chromium/chrome/browser/feedback/ConnectivityChecker.java
|
| index 4c370f847e7d93a16e7d6c5a10e0cd33b0b5bbcb..2af5bd9bcb30c62da2a9d6b0114591fc99f61835 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/feedback/ConnectivityChecker.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/feedback/ConnectivityChecker.java
|
| @@ -4,18 +4,31 @@
|
|
|
| package org.chromium.chrome.browser.feedback;
|
|
|
| +import android.os.AsyncTask;
|
| +
|
| import org.chromium.base.CalledByNative;
|
| import org.chromium.base.JNINamespace;
|
| +import org.chromium.base.Log;
|
| import org.chromium.base.ThreadUtils;
|
| import org.chromium.base.VisibleForTesting;
|
| import org.chromium.chrome.browser.profiles.Profile;
|
|
|
| +import java.io.IOException;
|
| +import java.net.HttpURLConnection;
|
| +import java.net.MalformedURLException;
|
| +import java.net.URL;
|
| +
|
| /**
|
| * A utility class for checking if the device is currently connected to the Internet.
|
| */
|
| @JNINamespace("chrome::android")
|
| public final class ConnectivityChecker {
|
| - private static final String DEFAULT_NO_CONTENT_URL = "http://clients4.google.com/generate_204";
|
| + private static final String TAG = "ConnectivityChecker";
|
| +
|
| + private static final String DEFAULT_HTTP_NO_CONTENT_URL =
|
| + "http://clients4.google.com/generate_204";
|
| + private static final String DEFAULT_HTTPS_NO_CONTENT_URL =
|
| + "https://clients4.google.com/generate_204";
|
|
|
| /**
|
| * A callback for whether the device is currently connected to the Internet.
|
| @@ -29,8 +42,69 @@ public final class ConnectivityChecker {
|
|
|
| /**
|
| * Starts an asynchronous request for checking whether the device is currently connected to the
|
| - * Internet. The result passed to the callback denotes whether the attempt to connect to the
|
| - * server was successful.
|
| + * Internet using the Android system network stack. The result passed to the callback denotes
|
| + * whether the attempt to connect to the server was successful.
|
| + *
|
| + * If the profile or URL is invalid, the callback will be called with false.
|
| + * The server reply for the URL must respond with HTTP 204 without any redirects for the
|
| + * connectivity check to be successful.
|
| + *
|
| + * This method takes ownership of the callback object until the callback has happened.
|
| + * This method must be called on the main thread.
|
| + * @param timeoutMs number of milliseconds to wait before giving up waiting for a connection.
|
| + * @param callback the callback which will get the result.
|
| + */
|
| + public static void checkConnectivitySystemNetworkStack(
|
| + boolean useHttps, int timeoutMs, final ConnectivityCheckerCallback callback) {
|
| + try {
|
| + URL url = useHttps ? new URL(DEFAULT_HTTPS_NO_CONTENT_URL)
|
| + : new URL(DEFAULT_HTTP_NO_CONTENT_URL);
|
| + checkConnectivitySystemNetworkStack(url, timeoutMs, callback);
|
| + } catch (MalformedURLException e) {
|
| + Log.w(TAG, "Failed to predefined URL: " + e);
|
| + ThreadUtils.postOnUiThread(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + callback.onResult(false);
|
| + }
|
| + });
|
| + }
|
| + }
|
| +
|
| + static void checkConnectivitySystemNetworkStack(
|
| + final URL url, final int timeoutMs, final ConnectivityCheckerCallback callback) {
|
| + new AsyncTask<String, Void, Boolean>() {
|
| + @Override
|
| + protected Boolean doInBackground(String... strings) {
|
| + try {
|
| + HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
| + conn.setInstanceFollowRedirects(false);
|
| + conn.setRequestMethod("GET");
|
| + conn.setDoInput(false);
|
| + conn.setDoOutput(false);
|
| + conn.setConnectTimeout(timeoutMs);
|
| + conn.setReadTimeout(timeoutMs);
|
| +
|
| + conn.connect();
|
| + int responseCode = conn.getResponseCode();
|
| + return responseCode == HttpURLConnection.HTTP_NO_CONTENT;
|
| + } catch (IOException e) {
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + @Override
|
| + protected void onPostExecute(Boolean connected) {
|
| + callback.onResult(connected);
|
| + }
|
| + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
| + }
|
| +
|
| + /**
|
| + * Starts an asynchronous request for checking whether the device is currently connected to the
|
| + * Internet using the Chrome network stack. The result passed to the callback denotes whether
|
| + *the
|
| + * attempt to connect to the server was successful.
|
| *
|
| * If the profile or URL is invalid, the callback will be called with false.
|
| * The server reply for the URL must respond with HTTP 204 without any redirects for the
|
| @@ -42,13 +116,14 @@ public final class ConnectivityChecker {
|
| * @param timeoutMs number of milliseconds to wait before giving up waiting for a connection.
|
| * @param callback the callback which will get the result.
|
| */
|
| - public static void checkConnectivity(
|
| - Profile profile, long timeoutMs, ConnectivityCheckerCallback callback) {
|
| - checkConnectivity(profile, DEFAULT_NO_CONTENT_URL, timeoutMs, callback);
|
| + public static void checkConnectivityChromeNetworkStack(Profile profile, boolean useHttps,
|
| + long timeoutMs, ConnectivityCheckerCallback callback) {
|
| + String url = useHttps ? DEFAULT_HTTPS_NO_CONTENT_URL : DEFAULT_HTTP_NO_CONTENT_URL;
|
| + checkConnectivityChromeNetworkStack(profile, url, timeoutMs, callback);
|
| }
|
|
|
| @VisibleForTesting
|
| - static void checkConnectivity(
|
| + static void checkConnectivityChromeNetworkStack(
|
| Profile profile, String url, long timeoutMs, ConnectivityCheckerCallback callback) {
|
| ThreadUtils.assertOnUiThread();
|
| nativeCheckConnectivity(profile, url, timeoutMs, callback);
|
|
|