Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2bd4b5764150723b0d24751c1363849c08b15daa |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/feedback/ConnectivityChecker.java |
| @@ -0,0 +1,75 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.feedback; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.VisibleForTesting; |
| +import org.chromium.chrome.browser.profiles.Profile; |
| + |
| +/** |
| + * 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 long DEFAULT_TIMEOUT_MS = 10000; // 10 seconds. |
| + |
| + /** |
| + * A callback for whether the device is currently connected to the Internet. |
| + */ |
| + public interface ConnectivityCheckerCallback { |
| + /** |
| + * Called when the result of the connectivity check is ready. |
| + */ |
| + void onResult(boolean connected); |
| + } |
| + |
| + /** |
| + * 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 successfull. |
| + * |
| + * 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 successfull. |
| + * |
| + * This method takes ownership of the callback object until the callback has happened. |
| + * This method must be called on the main thread. |
| + * @param profile the context to do the check in. |
| + * @param url the URL to connect to. |
| + * @param callback the callback which will get the result. |
| + */ |
| + public static void checkConnectivity(Profile profile, ConnectivityCheckerCallback callback) { |
| + checkConnectivity(profile, DEFAULT_NO_CONTENT_URL, DEFAULT_TIMEOUT_MS, callback); |
| + } |
| + |
| + @VisibleForTesting |
| + static void checkConnectivity(Profile profile, String url, long timeoutMs, |
| + final ConnectivityCheckerCallback callback) { |
| + ThreadUtils.assertOnUiThread(); |
| + if (profile == null || url == null) { |
| + 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.
|
| + @Override |
| + public void run() { |
| + callback.onResult(false); |
| + } |
| + }); |
| + return; |
| + } |
| + nativeCheckConnectivity(profile, url, timeoutMs, callback); |
| + } |
| + |
| + @CalledByNative |
| + private static void executeCallback(Object callback, boolean connected) { |
| + ((ConnectivityCheckerCallback) callback).onResult(connected); |
| + } |
| + |
| + private ConnectivityChecker() {} |
| + |
| + private static native void nativeCheckConnectivity( |
| + Profile profile, String url, long timeoutMs, ConnectivityCheckerCallback callback); |
| +} |