OLD | NEW |
---|---|
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; | |
8 | |
7 import org.chromium.base.CalledByNative; | 9 import org.chromium.base.CalledByNative; |
8 import org.chromium.base.JNINamespace; | 10 import org.chromium.base.JNINamespace; |
11 import org.chromium.base.Log; | |
9 import org.chromium.base.ThreadUtils; | 12 import org.chromium.base.ThreadUtils; |
10 import org.chromium.base.VisibleForTesting; | 13 import org.chromium.base.VisibleForTesting; |
11 import org.chromium.chrome.browser.profiles.Profile; | 14 import org.chromium.chrome.browser.profiles.Profile; |
12 | 15 |
16 import java.io.IOException; | |
17 import java.net.HttpURLConnection; | |
18 import java.net.MalformedURLException; | |
19 import java.net.URL; | |
20 | |
13 /** | 21 /** |
14 * A utility class for checking if the device is currently connected to the Inte rnet. | 22 * A utility class for checking if the device is currently connected to the Inte rnet. |
15 */ | 23 */ |
16 @JNINamespace("chrome::android") | 24 @JNINamespace("chrome::android") |
17 public final class ConnectivityChecker { | 25 public final class ConnectivityChecker { |
18 private static final String DEFAULT_NO_CONTENT_URL = "http://clients4.google .com/generate_204"; | 26 private static final String TAG = "ConnectivityChecker"; |
27 | |
28 private static final String DEFAULT_HTTP_NO_CONTENT_URL = | |
29 "http://clients4.google.com/generate_204"; | |
30 private static final String DEFAULT_HTTPS_NO_CONTENT_URL = | |
31 "https://clients4.google.com/generate_204"; | |
19 | 32 |
20 /** | 33 /** |
21 * A callback for whether the device is currently connected to the Internet. | 34 * A callback for whether the device is currently connected to the Internet. |
22 */ | 35 */ |
23 public interface ConnectivityCheckerCallback { | 36 public interface ConnectivityCheckerCallback { |
24 /** | 37 /** |
25 * Called when the result of the connectivity check is ready. | 38 * Called when the result of the connectivity check is ready. |
26 */ | 39 */ |
27 void onResult(boolean connected); | 40 void onResult(boolean connected); |
28 } | 41 } |
29 | 42 |
30 /** | 43 /** |
31 * Starts an asynchronous request for checking whether the device is current ly connected to the | 44 * Starts an asynchronous request for checking whether the device is current ly connected to the |
32 * Internet. The result passed to the callback denotes whether the attempt t o connect to the | 45 * Internet using the Android system network stack. The result passed to the callback denotes |
33 * server was successful. | 46 * whether the attempt to connect to the server was successful. |
34 * | 47 * |
35 * If the profile or URL is invalid, the callback will be called with false. | 48 * If the profile or URL is invalid, the callback will be called with false. |
36 * The server reply for the URL must respond with HTTP 204 without any redir ects for the | 49 * The server reply for the URL must respond with HTTP 204 without any redir ects for the |
50 * connectivity check to be successful. | |
51 * | |
52 * This method takes ownership of the callback object until the callback has happened. | |
53 * 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
| |
54 * @param timeoutMs number of milliseconds to wait before giving up waiting for a connection. | |
55 * @param callback the callback which will get the result. | |
56 */ | |
57 public static void checkConnectivitySystemNetworkStack( | |
58 boolean useHttps, int timeoutMs, final ConnectivityCheckerCallback c allback) { | |
59 try { | |
60 URL url = useHttps ? new URL(DEFAULT_HTTPS_NO_CONTENT_URL) | |
61 : new URL(DEFAULT_HTTP_NO_CONTENT_URL); | |
62 checkConnectivitySystemNetworkStack(url, timeoutMs, callback); | |
63 } catch (MalformedURLException e) { | |
64 Log.w(TAG, "Failed to predefined URL: " + e); | |
65 ThreadUtils.postOnUiThread(new Runnable() { | |
66 @Override | |
67 public void run() { | |
68 callback.onResult(false); | |
69 } | |
70 }); | |
71 } | |
72 } | |
73 | |
74 static void checkConnectivitySystemNetworkStack( | |
75 final URL url, final int timeoutMs, final ConnectivityCheckerCallbac k callback) { | |
76 new AsyncTask<String, Void, Boolean>() { | |
77 @Override | |
78 protected Boolean doInBackground(String... strings) { | |
79 try { | |
80 HttpURLConnection conn = (HttpURLConnection) url.openConnect ion(); | |
81 conn.setInstanceFollowRedirects(false); | |
82 conn.setRequestMethod("GET"); | |
83 conn.setDoInput(false); | |
84 conn.setDoOutput(false); | |
85 conn.setConnectTimeout(timeoutMs); | |
86 conn.setReadTimeout(timeoutMs); | |
87 | |
88 conn.connect(); | |
89 int responseCode = conn.getResponseCode(); | |
90 return responseCode == HttpURLConnection.HTTP_NO_CONTENT; | |
91 } catch (IOException e) { | |
92 return false; | |
93 } | |
94 } | |
95 | |
96 @Override | |
97 protected void onPostExecute(Boolean connected) { | |
98 callback.onResult(connected); | |
99 } | |
100 }.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.
| |
101 } | |
102 | |
103 /** | |
104 * Starts an asynchronous request for checking whether the device is current ly connected to the | |
105 * Internet using the Chrome network stack. The result passed to the callbac k denotes whether | |
106 *the | |
107 * attempt to connect to the server was successful. | |
108 * | |
109 * If the profile or URL is invalid, the callback will be called with false. | |
110 * The server reply for the URL must respond with HTTP 204 without any redir ects for the | |
37 * connectivity check to be successful. | 111 * connectivity check to be successful. |
38 * | 112 * |
39 * This method takes ownership of the callback object until the callback has happened. | 113 * This method takes ownership of the callback object until the callback has happened. |
40 * This method must be called on the main thread. | 114 * This method must be called on the main thread. |
41 * @param profile the context to do the check in. | 115 * @param profile the context to do the check in. |
42 * @param timeoutMs number of milliseconds to wait before giving up waiting for a connection. | 116 * @param timeoutMs number of milliseconds to wait before giving up waiting for a connection. |
43 * @param callback the callback which will get the result. | 117 * @param callback the callback which will get the result. |
44 */ | 118 */ |
45 public static void checkConnectivity( | 119 public static void checkConnectivityChromeNetworkStack(Profile profile, bool ean useHttps, |
46 Profile profile, long timeoutMs, ConnectivityCheckerCallback callbac k) { | 120 long timeoutMs, ConnectivityCheckerCallback callback) { |
47 checkConnectivity(profile, DEFAULT_NO_CONTENT_URL, timeoutMs, callback); | 121 String url = useHttps ? DEFAULT_HTTPS_NO_CONTENT_URL : DEFAULT_HTTP_NO_C ONTENT_URL; |
122 checkConnectivityChromeNetworkStack(profile, url, timeoutMs, callback); | |
48 } | 123 } |
49 | 124 |
50 @VisibleForTesting | 125 @VisibleForTesting |
51 static void checkConnectivity( | 126 static void checkConnectivityChromeNetworkStack( |
52 Profile profile, String url, long timeoutMs, ConnectivityCheckerCall back callback) { | 127 Profile profile, String url, long timeoutMs, ConnectivityCheckerCall back callback) { |
53 ThreadUtils.assertOnUiThread(); | 128 ThreadUtils.assertOnUiThread(); |
54 nativeCheckConnectivity(profile, url, timeoutMs, callback); | 129 nativeCheckConnectivity(profile, url, timeoutMs, callback); |
55 } | 130 } |
56 | 131 |
57 @CalledByNative | 132 @CalledByNative |
58 private static void executeCallback(Object callback, boolean connected) { | 133 private static void executeCallback(Object callback, boolean connected) { |
59 ((ConnectivityCheckerCallback) callback).onResult(connected); | 134 ((ConnectivityCheckerCallback) callback).onResult(connected); |
60 } | 135 } |
61 | 136 |
62 private ConnectivityChecker() {} | 137 private ConnectivityChecker() {} |
63 | 138 |
64 private static native void nativeCheckConnectivity( | 139 private static native void nativeCheckConnectivity( |
65 Profile profile, String url, long timeoutMs, ConnectivityCheckerCall back callback); | 140 Profile profile, String url, long timeoutMs, ConnectivityCheckerCall back callback); |
66 } | 141 } |
OLD | NEW |