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

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

Issue 1212633005: [Feedback] Start using enum for connectivity check results (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename result and re to results and result in ConnectivityTask Created 5 years, 5 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.feedback; 5 package org.chromium.chrome.browser.feedback;
6 6
7 import android.os.AsyncTask; 7 import android.os.AsyncTask;
8 8
9 import org.chromium.base.CalledByNative; 9 import org.chromium.base.CalledByNative;
10 import org.chromium.base.JNINamespace; 10 import org.chromium.base.JNINamespace;
11 import org.chromium.base.Log; 11 import org.chromium.base.Log;
12 import org.chromium.base.ThreadUtils; 12 import org.chromium.base.ThreadUtils;
13 import org.chromium.base.VisibleForTesting; 13 import org.chromium.base.VisibleForTesting;
14 import org.chromium.chrome.browser.profiles.Profile; 14 import org.chromium.chrome.browser.profiles.Profile;
15 15
16 import java.io.IOException; 16 import java.io.IOException;
17 import java.net.HttpURLConnection; 17 import java.net.HttpURLConnection;
18 import java.net.MalformedURLException; 18 import java.net.MalformedURLException;
19 import java.net.ProtocolException;
20 import java.net.SocketTimeoutException;
19 import java.net.URL; 21 import java.net.URL;
20 22
21 /** 23 /**
22 * A utility class for checking if the device is currently connected to the Inte rnet. 24 * A utility class for checking if the device is currently connected to the Inte rnet.
23 */ 25 */
24 @JNINamespace("chrome::android") 26 @JNINamespace("chrome::android")
25 public final class ConnectivityChecker { 27 public final class ConnectivityChecker {
26 private static final String TAG = "Feedback"; 28 private static final String TAG = "cr.feedback";
27 29
28 private static final String DEFAULT_HTTP_NO_CONTENT_URL = 30 private static final String DEFAULT_HTTP_NO_CONTENT_URL =
29 "http://clients4.google.com/generate_204"; 31 "http://clients4.google.com/generate_204";
30 private static final String DEFAULT_HTTPS_NO_CONTENT_URL = 32 private static final String DEFAULT_HTTPS_NO_CONTENT_URL =
31 "https://clients4.google.com/generate_204"; 33 "https://clients4.google.com/generate_204";
32 34
33 private static String sHttpNoContentUrl = DEFAULT_HTTP_NO_CONTENT_URL; 35 private static String sHttpNoContentUrl = DEFAULT_HTTP_NO_CONTENT_URL;
34 private static String sHttpsNoContentUrl = DEFAULT_HTTPS_NO_CONTENT_URL; 36 private static String sHttpsNoContentUrl = DEFAULT_HTTPS_NO_CONTENT_URL;
35 37
36 /** 38 /**
37 * A callback for whether the device is currently connected to the Internet. 39 * A callback for whether the device is currently connected to the Internet.
38 */ 40 */
39 public interface ConnectivityCheckerCallback { 41 public interface ConnectivityCheckerCallback {
40 /** 42 /**
41 * Called when the result of the connectivity check is ready. 43 * Called when the result of the connectivity check is ready.
42 */ 44 */
43 void onResult(boolean connected); 45 void onResult(int result);
44 } 46 }
45 47
46 @VisibleForTesting 48 @VisibleForTesting
47 static void overrideUrlsForTest(String httpUrl, String httpsUrl) { 49 static void overrideUrlsForTest(String httpUrl, String httpsUrl) {
48 ThreadUtils.assertOnUiThread(); 50 ThreadUtils.assertOnUiThread();
49 sHttpNoContentUrl = httpUrl; 51 sHttpNoContentUrl = httpUrl;
50 sHttpsNoContentUrl = httpsUrl; 52 sHttpsNoContentUrl = httpsUrl;
51 } 53 }
52 54
55 private static void postResult(final ConnectivityCheckerCallback callback, f inal int result) {
56 ThreadUtils.postOnUiThread(new Runnable() {
57 @Override
58 public void run() {
59 callback.onResult(result);
60 }
61 });
62 }
63
53 /** 64 /**
54 * Starts an asynchronous request for checking whether the device is current ly connected to the 65 * Starts an asynchronous request for checking whether the device is current ly connected to the
55 * Internet using the Android system network stack. The result passed to the callback denotes 66 * Internet using the Android system network stack. The result passed to the callback denotes
56 * whether the attempt to connect to the server was successful. 67 * whether the attempt to connect to the server was successful.
57 * 68 *
58 * If the profile or URL is invalid, the callback will be called with false. 69 * If the profile or URL is invalid, the callback will be called with false.
59 * The server reply for the URL must respond with HTTP 204 without any redir ects for the 70 * The server reply for the URL must respond with HTTP 204 without any redir ects for the
60 * connectivity check to be successful. 71 * connectivity check to be successful.
61 * 72 *
62 * This method takes ownership of the callback object until the callback has happened. 73 * This method takes ownership of the callback object until the callback has happened.
63 * This method must be called on the main thread. 74 * This method must be called on the main thread.
64 * @param timeoutMs number of milliseconds to wait before giving up waiting for a connection. 75 * @param timeoutMs number of milliseconds to wait before giving up waiting for a connection.
65 * @param callback the callback which will get the result. 76 * @param callback the callback which will get the result.
66 */ 77 */
67 public static void checkConnectivitySystemNetworkStack( 78 public static void checkConnectivitySystemNetworkStack(
68 boolean useHttps, int timeoutMs, final ConnectivityCheckerCallback c allback) { 79 boolean useHttps, int timeoutMs, ConnectivityCheckerCallback callbac k) {
69 try { 80 String url = useHttps ? sHttpsNoContentUrl : sHttpNoContentUrl;
70 URL url = useHttps ? new URL(sHttpsNoContentUrl) : new URL(sHttpNoCo ntentUrl); 81 checkConnectivitySystemNetworkStack(url, timeoutMs, callback);
71 checkConnectivitySystemNetworkStack(url, timeoutMs, callback);
72 } catch (MalformedURLException e) {
73 Log.w(TAG, "Failed to predefined URL: " + e);
74 ThreadUtils.postOnUiThread(new Runnable() {
75 @Override
76 public void run() {
77 callback.onResult(false);
78 }
79 });
80 }
81 } 82 }
82 83
83 static void checkConnectivitySystemNetworkStack( 84 static void checkConnectivitySystemNetworkStack(
84 final URL url, final int timeoutMs, final ConnectivityCheckerCallbac k callback) { 85 String urlStr, final int timeoutMs, final ConnectivityCheckerCallbac k callback) {
85 new AsyncTask<String, Void, Boolean>() { 86 if (!nativeIsUrlValid(urlStr)) {
87 Log.w(TAG, "Predefined URL invalid.");
88 postResult(callback, ConnectivityCheckResult.ERROR);
89 return;
90 }
91 final URL url;
92 try {
93 url = new URL(urlStr);
94 } catch (MalformedURLException e) {
95 Log.w(TAG, "Failed to parse predefined URL: " + e);
96 postResult(callback, ConnectivityCheckResult.ERROR);
97 return;
98 }
99 new AsyncTask<String, Void, Integer>() {
86 @Override 100 @Override
87 protected Boolean doInBackground(String... strings) { 101 protected Integer doInBackground(String... strings) {
88 try { 102 try {
89 HttpURLConnection conn = (HttpURLConnection) url.openConnect ion(); 103 HttpURLConnection conn = (HttpURLConnection) url.openConnect ion();
90 conn.setInstanceFollowRedirects(false); 104 conn.setInstanceFollowRedirects(false);
91 conn.setRequestMethod("GET"); 105 conn.setRequestMethod("GET");
92 conn.setDoInput(false); 106 conn.setDoInput(false);
93 conn.setDoOutput(false); 107 conn.setDoOutput(false);
94 conn.setConnectTimeout(timeoutMs); 108 conn.setConnectTimeout(timeoutMs);
95 conn.setReadTimeout(timeoutMs); 109 conn.setReadTimeout(timeoutMs);
96 110
97 conn.connect(); 111 conn.connect();
98 int responseCode = conn.getResponseCode(); 112 int responseCode = conn.getResponseCode();
99 return responseCode == HttpURLConnection.HTTP_NO_CONTENT; 113 if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
114 return ConnectivityCheckResult.CONNECTED;
115 } else {
116 return ConnectivityCheckResult.NOT_CONNECTED;
117 }
118 } catch (SocketTimeoutException e) {
119 return ConnectivityCheckResult.TIMEOUT;
120 } catch (ProtocolException e) {
121 return ConnectivityCheckResult.ERROR;
100 } catch (IOException e) { 122 } catch (IOException e) {
101 return false; 123 return ConnectivityCheckResult.NOT_CONNECTED;
102 } 124 }
103 } 125 }
104 126
105 @Override 127 @Override
106 protected void onPostExecute(Boolean connected) { 128 protected void onPostExecute(Integer result) {
107 callback.onResult(connected); 129 callback.onResult(result);
108 } 130 }
109 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 131 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
110 } 132 }
111 133
112 /** 134 /**
113 * Starts an asynchronous request for checking whether the device is current ly connected to the 135 * Starts an asynchronous request for checking whether the device is current ly connected to the
114 * Internet using the Chrome network stack. The result passed to the callbac k denotes whether 136 * Internet using the Chrome network stack. The result passed to the callbac k denotes whether
115 *the 137 *the
116 * attempt to connect to the server was successful. 138 * attempt to connect to the server was successful.
117 * 139 *
(...skipping 14 matching lines...) Expand all
132 } 154 }
133 155
134 @VisibleForTesting 156 @VisibleForTesting
135 static void checkConnectivityChromeNetworkStack( 157 static void checkConnectivityChromeNetworkStack(
136 Profile profile, String url, long timeoutMs, ConnectivityCheckerCall back callback) { 158 Profile profile, String url, long timeoutMs, ConnectivityCheckerCall back callback) {
137 ThreadUtils.assertOnUiThread(); 159 ThreadUtils.assertOnUiThread();
138 nativeCheckConnectivity(profile, url, timeoutMs, callback); 160 nativeCheckConnectivity(profile, url, timeoutMs, callback);
139 } 161 }
140 162
141 @CalledByNative 163 @CalledByNative
142 private static void executeCallback(Object callback, boolean connected) { 164 private static void executeCallback(Object callback, int result) {
143 ((ConnectivityCheckerCallback) callback).onResult(connected); 165 ((ConnectivityCheckerCallback) callback).onResult(result);
144 } 166 }
145 167
146 private ConnectivityChecker() {} 168 private ConnectivityChecker() {}
147 169
148 private static native void nativeCheckConnectivity( 170 private static native void nativeCheckConnectivity(
149 Profile profile, String url, long timeoutMs, ConnectivityCheckerCall back callback); 171 Profile profile, String url, long timeoutMs, ConnectivityCheckerCall back callback);
172
173 private static native boolean nativeIsUrlValid(String url);
150 } 174 }
OLDNEW
« no previous file with comments | « chrome/android/BUILD.gn ('k') | chrome/android/java/src/org/chromium/chrome/browser/feedback/ConnectivityTask.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698