diff options
author | shouqun.liu@intel.com <shouqun.liu@intel.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-12 08:43:26 +0000 |
---|---|---|
committer | shouqun.liu@intel.com <shouqun.liu@intel.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-12 08:43:26 +0000 |
commit | 2fe036455d7058577e3d38210eb67de3b88acc76 (patch) | |
tree | 5647cfba7f71ff316c25b00da88cf127e681d890 /net/android | |
parent | 387e543b11b8898524b9ab52bd5a9fb4c4a7df24 (diff) | |
download | chromium_src-2fe036455d7058577e3d38210eb67de3b88acc76.zip chromium_src-2fe036455d7058577e3d38210eb67de3b88acc76.tar.gz chromium_src-2fe036455d7058577e3d38210eb67de3b88acc76.tar.bz2 |
Implement net::GetNetworkList() for Android.
* Implement net::GetNetworkList() by java.net.NetworkInterface through JNI.
* Remove NetUtilTest.GetNetworkList from net_unittests_apk disabled list.
BUG=
TEST=net_unittests_apk --gtest-filter=NetUtilTest.GetNetworkList
Review URL: https://chromiumcodereview.appspot.com/10905207
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@156257 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/android')
-rw-r--r-- | net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java | 55 | ||||
-rw-r--r-- | net/android/network_library.cc | 7 | ||||
-rw-r--r-- | net/android/network_library.h | 6 |
3 files changed, 68 insertions, 0 deletions
diff --git a/net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java b/net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java index ef8fa03..28208a7 100644 --- a/net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java +++ b/net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java @@ -12,6 +12,8 @@ import android.util.Log; import org.chromium.base.CalledByNative; import org.chromium.base.CalledByNativeUnchecked; +import java.net.Inet6Address; +import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.URLConnection; @@ -87,6 +89,59 @@ class AndroidNetworkLibrary { } /** + * @return the network interfaces list (if any) string. The items in + * the list string are delimited by a semicolon ";", each item + * is a network interface name and address pair and formatted + * as "name,address". e.g. + * eth0,10.0.0.2;eth0,fe80::5054:ff:fe12:3456 + * represents a network list string which containts two items. + */ + @CalledByNative + static public String getNetworkList() { + Enumeration<NetworkInterface> list = null; + try { + list = NetworkInterface.getNetworkInterfaces(); + if (list == null) return ""; + } catch (SocketException e) { + Log.w(TAG, "Unable to get network interfaces: " + e); + return ""; + } + + StringBuilder result = new StringBuilder(); + while (list.hasMoreElements()) { + NetworkInterface netIf = list.nextElement(); + try { + // Skip loopback interfaces, and ones which are down. + if (!netIf.isUp() || netIf.isLoopback()) + continue; + Enumeration<InetAddress> addressList = netIf.getInetAddresses(); + while (addressList.hasMoreElements()) { + InetAddress address = addressList.nextElement(); + // Skip loopback addresses configured on non-loopback interfaces. + if (address.isLoopbackAddress()) + continue; + StringBuilder addressString = new StringBuilder(); + addressString.append(netIf.getName()); + addressString.append(","); + + String ipAddress = address.getHostAddress(); + if (address instanceof Inet6Address && ipAddress.contains("%")) { + ipAddress = ipAddress.substring(0, ipAddress.lastIndexOf("%")); + } + addressString.append(ipAddress); + + if (result.length() != 0) + result.append(";"); + result.append(addressString.toString()); + } + } catch (SocketException e) { + continue; + } + } + return result.toString(); + } + + /** * Validate the server's certificate chain is trusted. * * @param certChain The ASN.1 DER encoded bytes for certificates. diff --git a/net/android/network_library.cc b/net/android/network_library.cc index d518abaf..64f11aa 100644 --- a/net/android/network_library.cc +++ b/net/android/network_library.cc @@ -69,6 +69,13 @@ bool HaveOnlyLoopbackAddresses() { return Java_AndroidNetworkLibrary_haveOnlyLoopbackAddresses(env); } +std::string GetNetworkList() { + JNIEnv* env = AttachCurrentThread(); + ScopedJavaLocalRef<jstring> ret = + Java_AndroidNetworkLibrary_getNetworkList(env); + return ConvertJavaStringToUTF8(ret); +} + bool GetMimeTypeFromExtension(const std::string& extension, std::string* result) { JNIEnv* env = AttachCurrentThread(); diff --git a/net/android/network_library.h b/net/android/network_library.h index 26b326ab..a0cf7d3 100644 --- a/net/android/network_library.h +++ b/net/android/network_library.h @@ -44,6 +44,12 @@ bool StoreKeyPair(const uint8* public_key, // Also returns false if it cannot determine this. bool HaveOnlyLoopbackAddresses(); +// Return a string containing a list of network interfaces, each item is a +// network name and address pair. +// e.g. "eth0,10.0.0.2;eth0,fe80::5054:ff:fe12:3456" is a result string +// containing two items. +std::string GetNetworkList(); + // Get the mime type (if any) that is associated with the file extension. // Returns true if a corresponding mime type exists. bool GetMimeTypeFromExtension(const std::string& extension, |