Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4821)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/feedback/ConnectivityChecker.java

Issue 1147533002: Add connectivity check using system stack. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/android/javatests_shell/src/org/chromium/chrome/browser/feedback/ConnectivityCheckerTest.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..9c3faa8057517ec89e1330f33860dd9a6e756a30 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.
Yaron 2015/05/15 15:57:50 If it must be called on UI thread why are you post
nyquist 2015/05/15 16:32:18 Because I believe that as a caller of this API, it
Yaron 2015/05/15 17:06:04 Fair enough
+ * @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);
+ }
+ }.execute();
Yaron 2015/05/15 15:57:50 can you use THREAD_POOL_EXECUTOR to not block the
nyquist 2015/05/15 16:32:18 Sorry. Yes! Done.
+ }
+
+ /**
+ * 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);
« no previous file with comments | « no previous file | chrome/android/javatests_shell/src/org/chromium/chrome/browser/feedback/ConnectivityCheckerTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698