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.SystemClock; | 7 import android.os.SystemClock; |
8 | 8 |
9 import org.chromium.base.Log; | 9 import org.chromium.base.Log; |
10 import org.chromium.base.ThreadUtils; | 10 import org.chromium.base.ThreadUtils; |
11 import org.chromium.base.VisibleForTesting; | 11 import org.chromium.base.VisibleForTesting; |
12 import org.chromium.chrome.browser.profiles.Profile; | 12 import org.chromium.chrome.browser.profiles.Profile; |
| 13 import org.chromium.net.ConnectionType; |
| 14 import org.chromium.net.NetworkChangeNotifier; |
13 | 15 |
14 import java.util.Collections; | 16 import java.util.Collections; |
15 import java.util.EnumMap; | 17 import java.util.EnumMap; |
16 import java.util.HashMap; | 18 import java.util.HashMap; |
17 import java.util.Map; | 19 import java.util.Map; |
18 | 20 |
19 import javax.annotation.Nullable; | 21 import javax.annotation.Nullable; |
20 | 22 |
21 /** | 23 /** |
22 * A utility class for checking if the device is currently connected to the Inte
rnet by using | 24 * A utility class for checking if the device is currently connected to the Inte
rnet by using |
(...skipping 12 matching lines...) Expand all Loading... |
35 | 37 |
36 /** | 38 /** |
37 * The key for the data describing how long time from the connection check w
as started, | 39 * The key for the data describing how long time from the connection check w
as started, |
38 * until the data was collected. This is to better understand the connection
data. | 40 * until the data was collected. This is to better understand the connection
data. |
39 * This string is user visible. | 41 * This string is user visible. |
40 */ | 42 */ |
41 @VisibleForTesting | 43 @VisibleForTesting |
42 static final String CONNECTION_CHECK_ELAPSED_KEY = "Connection check elapsed
(ms)"; | 44 static final String CONNECTION_CHECK_ELAPSED_KEY = "Connection check elapsed
(ms)"; |
43 | 45 |
44 /** | 46 /** |
| 47 * The key for the data describing the current connection type. |
| 48 * This string is user visible. |
| 49 */ |
| 50 @VisibleForTesting |
| 51 static final String CONNECTION_TYPE_KEY = "Connection type"; |
| 52 |
| 53 /** |
| 54 * The key for the data describing the bandwidth of the current connection.
The data is |
| 55 * represented as Mbps. |
| 56 * This string is user visible. |
| 57 */ |
| 58 @VisibleForTesting |
| 59 static final String CONNECTION_BANDWIDTH_KEY = "Connection bandwidth (Mbps)"
; |
| 60 |
| 61 /** |
45 * The key for the data describing whether Chrome was able to successfully c
onnect to the | 62 * The key for the data describing whether Chrome was able to successfully c
onnect to the |
46 * HTTP connection check URL using the Chrome network stack. | 63 * HTTP connection check URL using the Chrome network stack. |
47 * This string is user visible. | 64 * This string is user visible. |
48 */ | 65 */ |
49 @VisibleForTesting | 66 @VisibleForTesting |
50 static final String CHROME_HTTP_KEY = "HTTP connection check (Chrome network
stack)"; | 67 static final String CHROME_HTTP_KEY = "HTTP connection check (Chrome network
stack)"; |
51 | 68 |
52 /** | 69 /** |
53 * The key for the data describing whether Chrome was able to successfully c
onnect to the | 70 * The key for the data describing whether Chrome was able to successfully c
onnect to the |
54 * HTTPS connection check URL using the Chrome network stack. | 71 * HTTPS connection check URL using the Chrome network stack. |
(...skipping 11 matching lines...) Expand all Loading... |
66 static final String SYSTEM_HTTP_KEY = "HTTP connection check (Android networ
k stack)"; | 83 static final String SYSTEM_HTTP_KEY = "HTTP connection check (Android networ
k stack)"; |
67 | 84 |
68 /** | 85 /** |
69 * The key for the data describing whether Chrome was able to successfully c
onnect to the | 86 * The key for the data describing whether Chrome was able to successfully c
onnect to the |
70 * HTTPS connection check URL using the Android network stack. | 87 * HTTPS connection check URL using the Android network stack. |
71 * This string is user visible. | 88 * This string is user visible. |
72 */ | 89 */ |
73 @VisibleForTesting | 90 @VisibleForTesting |
74 static final String SYSTEM_HTTPS_KEY = "HTTPS connection check (Android netw
ork stack)"; | 91 static final String SYSTEM_HTTPS_KEY = "HTTPS connection check (Android netw
ork stack)"; |
75 | 92 |
76 private static String getHumanReadableString(Type type) { | 93 private static String getHumanReadableType(Type type) { |
77 switch (type) { | 94 switch (type) { |
78 case CHROME_HTTP: | 95 case CHROME_HTTP: |
79 return CHROME_HTTP_KEY; | 96 return CHROME_HTTP_KEY; |
80 case CHROME_HTTPS: | 97 case CHROME_HTTPS: |
81 return CHROME_HTTPS_KEY; | 98 return CHROME_HTTPS_KEY; |
82 case SYSTEM_HTTP: | 99 case SYSTEM_HTTP: |
83 return SYSTEM_HTTP_KEY; | 100 return SYSTEM_HTTP_KEY; |
84 case SYSTEM_HTTPS: | 101 case SYSTEM_HTTPS: |
85 return SYSTEM_HTTPS_KEY; | 102 return SYSTEM_HTTPS_KEY; |
86 default: | 103 default: |
87 throw new IllegalArgumentException("Unknown connection type: " +
type); | 104 throw new IllegalArgumentException("Unknown connection type: " +
type); |
88 } | 105 } |
89 } | 106 } |
90 | 107 |
91 static String getHumanReadableString(int result) { | 108 static String getHumanReadableResult(int result) { |
92 switch (result) { | 109 switch (result) { |
93 case ConnectivityCheckResult.UNKNOWN: | 110 case ConnectivityCheckResult.UNKNOWN: |
94 return "UNKNOWN"; | 111 return "UNKNOWN"; |
95 case ConnectivityCheckResult.CONNECTED: | 112 case ConnectivityCheckResult.CONNECTED: |
96 return "CONNECTED"; | 113 return "CONNECTED"; |
97 case ConnectivityCheckResult.NOT_CONNECTED: | 114 case ConnectivityCheckResult.NOT_CONNECTED: |
98 return "NOT_CONNECTED"; | 115 return "NOT_CONNECTED"; |
99 case ConnectivityCheckResult.TIMEOUT: | 116 case ConnectivityCheckResult.TIMEOUT: |
100 return "TIMEOUT"; | 117 return "TIMEOUT"; |
101 case ConnectivityCheckResult.ERROR: | 118 case ConnectivityCheckResult.ERROR: |
102 return "ERROR"; | 119 return "ERROR"; |
103 default: | 120 default: |
104 throw new IllegalArgumentException("Unknown result value: " + re
sult); | 121 throw new IllegalArgumentException("Unknown result value: " + re
sult); |
105 } | 122 } |
106 } | 123 } |
107 | 124 |
| 125 static String getHumanReadableConnectionType(int connectionType) { |
| 126 switch (connectionType) { |
| 127 case ConnectionType.CONNECTION_UNKNOWN: |
| 128 return "Unknown"; |
| 129 case ConnectionType.CONNECTION_ETHERNET: |
| 130 return "Ethernet"; |
| 131 case ConnectionType.CONNECTION_WIFI: |
| 132 return "WiFi"; |
| 133 case ConnectionType.CONNECTION_2G: |
| 134 return "2G"; |
| 135 case ConnectionType.CONNECTION_3G: |
| 136 return "3G"; |
| 137 case ConnectionType.CONNECTION_4G: |
| 138 return "4G"; |
| 139 case ConnectionType.CONNECTION_NONE: |
| 140 return "NONE"; |
| 141 case ConnectionType.CONNECTION_BLUETOOTH: |
| 142 return "Bluetooth"; |
| 143 default: |
| 144 return "Unknown connection type " + connectionType; |
| 145 } |
| 146 } |
| 147 |
108 /** | 148 /** |
109 * ConnectivityResult is the callback for when the result of a connectivity
check is ready. | 149 * ConnectivityResult is the callback for when the result of a connectivity
check is ready. |
110 */ | 150 */ |
111 interface ConnectivityResult { | 151 interface ConnectivityResult { |
112 /** | 152 /** |
113 * Called when the FeedbackData is ready. | 153 * Called when the FeedbackData is ready. |
114 */ | 154 */ |
115 void onResult(FeedbackData feedbackData); | 155 void onResult(FeedbackData feedbackData); |
116 } | 156 } |
117 | 157 |
118 /** | 158 /** |
119 * FeedbackData contains the set of information that is to be included in a
feedback report. | 159 * FeedbackData contains the set of information that is to be included in a
feedback report. |
120 */ | 160 */ |
121 static final class FeedbackData { | 161 static final class FeedbackData { |
122 private final Map<Type, Integer> mConnections; | 162 private final Map<Type, Integer> mConnections; |
123 private final int mTimeoutMs; | 163 private final int mTimeoutMs; |
124 private final long mElapsedTimeMs; | 164 private final long mElapsedTimeMs; |
| 165 private final int mConnectionType; |
| 166 private final double mBandwidthInMbps; |
125 | 167 |
126 FeedbackData(Map<Type, Integer> connections, int timeoutMs, long elapsed
TimeMs) { | 168 FeedbackData(Map<Type, Integer> connections, int timeoutMs, long elapsed
TimeMs, |
| 169 int connectionType, double bandwidthInMbps) { |
127 mConnections = connections; | 170 mConnections = connections; |
128 mTimeoutMs = timeoutMs; | 171 mTimeoutMs = timeoutMs; |
129 mElapsedTimeMs = elapsedTimeMs; | 172 mElapsedTimeMs = elapsedTimeMs; |
| 173 mConnectionType = connectionType; |
| 174 mBandwidthInMbps = bandwidthInMbps; |
130 } | 175 } |
131 | 176 |
132 /** | 177 /** |
133 * @return a {@link Map} with information about connection status for di
fferent connection | 178 * @return a {@link Map} with information about connection status for di
fferent connection |
134 * types. | 179 * types. |
135 */ | 180 */ |
136 @VisibleForTesting | 181 @VisibleForTesting |
137 Map<Type, Integer> getConnections() { | 182 Map<Type, Integer> getConnections() { |
138 return Collections.unmodifiableMap(mConnections); | 183 return Collections.unmodifiableMap(mConnections); |
139 } | 184 } |
(...skipping 13 matching lines...) Expand all Loading... |
153 long getElapsedTimeMs() { | 198 long getElapsedTimeMs() { |
154 return mElapsedTimeMs; | 199 return mElapsedTimeMs; |
155 } | 200 } |
156 | 201 |
157 /** | 202 /** |
158 * @return a {@link Map} with all the data fields for this feedback. | 203 * @return a {@link Map} with all the data fields for this feedback. |
159 */ | 204 */ |
160 Map<String, String> toMap() { | 205 Map<String, String> toMap() { |
161 Map<String, String> map = new HashMap<>(); | 206 Map<String, String> map = new HashMap<>(); |
162 for (Map.Entry<Type, Integer> entry : mConnections.entrySet()) { | 207 for (Map.Entry<Type, Integer> entry : mConnections.entrySet()) { |
163 map.put(getHumanReadableString(entry.getKey()), | 208 map.put(getHumanReadableType(entry.getKey()), |
164 getHumanReadableString(entry.getValue())); | 209 getHumanReadableResult(entry.getValue())); |
165 } | 210 } |
166 map.put(CONNECTION_CHECK_TIMEOUT_KEY, String.valueOf(mTimeoutMs)); | 211 map.put(CONNECTION_CHECK_TIMEOUT_KEY, String.valueOf(mTimeoutMs)); |
167 map.put(CONNECTION_CHECK_ELAPSED_KEY, String.valueOf(mElapsedTimeMs)
); | 212 map.put(CONNECTION_CHECK_ELAPSED_KEY, String.valueOf(mElapsedTimeMs)
); |
| 213 map.put(CONNECTION_TYPE_KEY, getHumanReadableConnectionType(mConnect
ionType)); |
| 214 map.put(CONNECTION_BANDWIDTH_KEY, String.valueOf(mBandwidthInMbps)); |
168 return map; | 215 return map; |
169 } | 216 } |
170 } | 217 } |
171 | 218 |
172 /** | 219 /** |
173 * The type of network stack and connectivity check this result is about. | 220 * The type of network stack and connectivity check this result is about. |
174 */ | 221 */ |
175 public enum Type { CHROME_HTTP, CHROME_HTTPS, SYSTEM_HTTP, SYSTEM_HTTPS } | 222 public enum Type { CHROME_HTTP, CHROME_HTTPS, SYSTEM_HTTP, SYSTEM_HTTPS } |
176 | 223 |
177 private class SingleTypeTask implements ConnectivityChecker.ConnectivityChec
kerCallback { | 224 private class SingleTypeTask implements ConnectivityChecker.ConnectivityChec
kerCallback { |
(...skipping 26 matching lines...) Expand all Loading... |
204 ConnectivityChecker.checkConnectivitySystemNetworkStack(true
, timeoutMs, this); | 251 ConnectivityChecker.checkConnectivitySystemNetworkStack(true
, timeoutMs, this); |
205 break; | 252 break; |
206 default: | 253 default: |
207 Log.e(TAG, "Failed to recognize type " + mType); | 254 Log.e(TAG, "Failed to recognize type " + mType); |
208 } | 255 } |
209 } | 256 } |
210 | 257 |
211 @Override | 258 @Override |
212 public void onResult(int result) { | 259 public void onResult(int result) { |
213 ThreadUtils.assertOnUiThread(); | 260 ThreadUtils.assertOnUiThread(); |
214 Log.v(TAG, "Got result for " + getHumanReadableString(mType) + ": re
sult = " | 261 Log.v(TAG, "Got result for " + getHumanReadableType(mType) + ": resu
lt = " |
215 + getHumanReadableString(result)); | 262 + getHumanReadableResult(result)); |
216 mResult.put(mType, result); | 263 mResult.put(mType, result); |
217 if (isDone()) postCallbackResult(); | 264 if (isDone()) postCallbackResult(); |
218 } | 265 } |
219 | 266 |
220 private void postCallbackResult() { | 267 private void postCallbackResult() { |
221 if (mCallback == null) return; | 268 if (mCallback == null) return; |
222 ThreadUtils.postOnUiThread(new Runnable() { | 269 ThreadUtils.postOnUiThread(new Runnable() { |
223 @Override | 270 @Override |
224 public void run() { | 271 public void run() { |
225 mCallback.onResult(get()); | 272 mCallback.onResult(get()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 Map<Type, Integer> result = new EnumMap<Type, Integer>(Type.class); | 315 Map<Type, Integer> result = new EnumMap<Type, Integer>(Type.class); |
269 // Ensure the map is filled with a result for all {@link Type}s. | 316 // Ensure the map is filled with a result for all {@link Type}s. |
270 for (Type type : Type.values()) { | 317 for (Type type : Type.values()) { |
271 if (mResult.containsKey(type)) { | 318 if (mResult.containsKey(type)) { |
272 result.put(type, mResult.get(type)); | 319 result.put(type, mResult.get(type)); |
273 } else { | 320 } else { |
274 result.put(type, ConnectivityCheckResult.UNKNOWN); | 321 result.put(type, ConnectivityCheckResult.UNKNOWN); |
275 } | 322 } |
276 } | 323 } |
277 long elapsedTimeMs = SystemClock.elapsedRealtime() - mStartCheckTimeMs; | 324 long elapsedTimeMs = SystemClock.elapsedRealtime() - mStartCheckTimeMs; |
278 return new FeedbackData(result, mTimeoutMs, elapsedTimeMs); | 325 NetworkChangeNotifier ncn = NetworkChangeNotifier.getInstance(); |
| 326 int connectionType = ncn.getCurrentConnectionType(); |
| 327 double bandwidthInMbps = ncn.getCurrentMaxBandwidthInMbps(); |
| 328 return new FeedbackData(result, mTimeoutMs, elapsedTimeMs, connectionTyp
e, bandwidthInMbps); |
279 } | 329 } |
280 | 330 |
281 /** | 331 /** |
282 * Starts an asynchronous request for checking whether the device is current
ly connected to the | 332 * Starts an asynchronous request for checking whether the device is current
ly connected to the |
283 * Internet using both the Chrome and the Android system network stack. | 333 * Internet using both the Chrome and the Android system network stack. |
284 * | 334 * |
285 * The result will be given back in the {@link ConnectivityResult} callback
that is passed in, | 335 * The result will be given back in the {@link ConnectivityResult} callback
that is passed in, |
286 * either when all results have been gathered successfully or if a timeout h
appened. The result | 336 * either when all results have been gathered successfully or if a timeout h
appened. The result |
287 * can also be retrieved by calling {@link #get}, and this call must happen
from the main | 337 * can also be retrieved by calling {@link #get}, and this call must happen
from the main |
288 * thread. {@link #isDone} can be used to see if all requests have been comp
leted. It is OK to | 338 * thread. {@link #isDone} can be used to see if all requests have been comp
leted. It is OK to |
289 * get the result before {@link #isDone()} returns true. | 339 * get the result before {@link #isDone()} returns true. |
290 * | 340 * |
291 * @param profile the context to do the check in. | 341 * @param profile the context to do the check in. |
292 * @param timeoutMs number of milliseconds to wait before giving up waiting
for a connection. | 342 * @param timeoutMs number of milliseconds to wait before giving up waiting
for a connection. |
293 * @param callback the callback for the result. May be null. | 343 * @param callback the callback for the result. May be null. |
294 * @return a ConnectivityTask to retrieve the results. | 344 * @return a ConnectivityTask to retrieve the results. |
295 */ | 345 */ |
296 public static ConnectivityTask create( | 346 public static ConnectivityTask create( |
297 Profile profile, int timeoutMs, @Nullable ConnectivityResult callbac
k) { | 347 Profile profile, int timeoutMs, @Nullable ConnectivityResult callbac
k) { |
298 ThreadUtils.assertOnUiThread(); | 348 ThreadUtils.assertOnUiThread(); |
299 return new ConnectivityTask(profile, timeoutMs, callback); | 349 return new ConnectivityTask(profile, timeoutMs, callback); |
300 } | 350 } |
301 } | 351 } |
OLD | NEW |