OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.feedback; | |
6 | |
7 import org.chromium.base.CalledByNative; | |
8 import org.chromium.base.JNINamespace; | |
9 import org.chromium.base.ThreadUtils; | |
10 import org.chromium.base.VisibleForTesting; | |
11 import org.chromium.chrome.browser.profiles.Profile; | |
12 | |
13 /** | |
14 * A utility class for checking if the device is currently connected to the Inte rnet. | |
15 */ | |
16 @JNINamespace("chrome::android") | |
17 public final class ConnectivityChecker { | |
18 private static final String DEFAULT_NO_CONTENT_URL = "http://clients4.google .com/generate_204"; | |
19 private static final long DEFAULT_TIMEOUT_MS = 10000; // 10 seconds. | |
20 | |
21 /** | |
22 * A callback for whether the device is currently connected to the Internet. | |
23 */ | |
24 public interface ConnectivityCheckerCallback { | |
25 /** | |
26 * Called when the result of the connectivity check is ready. | |
27 */ | |
28 void onResult(boolean connected); | |
29 } | |
30 | |
31 /** | |
32 * Starts an asynchronous request for checking whether the device is current ly connected to the | |
33 * Internet. The result passed to the callback denotes whether the attempt t o connect to the | |
34 * server was successfull. | |
35 * | |
36 * If the profile or URL is invalid, the callback will be called with false. | |
37 * The server reply for the URL must respond with HTTP 204 without any redir ects for the | |
38 * connectivity check to be successfull. | |
39 * | |
40 * This method takes ownership of the callback object until the callback has happened. | |
41 * This method must be called on the main thread. | |
42 * @param profile the context to do the check in. | |
43 * @param url the URL to connect to. | |
44 * @param callback the callback which will get the result. | |
45 */ | |
46 public static void checkConnectivity(Profile profile, ConnectivityCheckerCal lback callback) { | |
47 checkConnectivity(profile, DEFAULT_NO_CONTENT_URL, DEFAULT_TIMEOUT_MS, c allback); | |
48 } | |
49 | |
50 @VisibleForTesting | |
51 static void checkConnectivity(Profile profile, String url, long timeoutMs, | |
52 final ConnectivityCheckerCallback callback) { | |
53 ThreadUtils.assertOnUiThread(); | |
54 if (profile == null || url == null) { | |
55 ThreadUtils.runOnUiThread(new Runnable() { | |
cjhopman
2015/05/07 00:09:09
will the callback always be called on the ui threa
nyquist
2015/05/07 05:35:46
Yes. Both the URLFetcher and the timer for timeout
cjhopman
2015/05/08 18:58:39
Sure, but I was thinking that handling null profil
nyquist
2015/05/12 00:42:30
Done.
| |
56 @Override | |
57 public void run() { | |
58 callback.onResult(false); | |
59 } | |
60 }); | |
61 return; | |
62 } | |
63 nativeCheckConnectivity(profile, url, timeoutMs, callback); | |
64 } | |
65 | |
66 @CalledByNative | |
67 private static void executeCallback(Object callback, boolean connected) { | |
68 ((ConnectivityCheckerCallback) callback).onResult(connected); | |
69 } | |
70 | |
71 private ConnectivityChecker() {} | |
72 | |
73 private static native void nativeCheckConnectivity( | |
74 Profile profile, String url, long timeoutMs, ConnectivityCheckerCall back callback); | |
75 } | |
OLD | NEW |