diff options
| author | Tobias Thierer <tobiast@google.com> | 2017-08-22 14:07:16 +0000 |
|---|---|---|
| committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-08-22 14:07:16 +0000 |
| commit | be87c9ca58bde5d82ec667c7f6e3140fdf264c2e (patch) | |
| tree | 821b7d82c158d70475ad19231a050fd8eea129e7 /core/java/android | |
| parent | bef2c91bb5efdfc7d480389df9e9b9556a218600 (diff) | |
| parent | 69b8b29c7b1be1454ba9b914b4f7697d03f9bc56 (diff) | |
Merge "Network: Use HttpURLConnectionFactory rather than OkHttp APIs"
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/net/Network.java | 66 |
1 files changed, 21 insertions, 45 deletions
diff --git a/core/java/android/net/Network.java b/core/java/android/net/Network.java index 0c0872a78f74..3c868c399b1f 100644 --- a/core/java/android/net/Network.java +++ b/core/java/android/net/Network.java @@ -22,6 +22,9 @@ import android.system.ErrnoException; import android.system.Os; import android.system.OsConstants; +import libcore.net.http.Dns; +import libcore.net.http.HttpURLConnectionFactory; + import java.io.FileDescriptor; import java.io.IOException; import java.net.DatagramSocket; @@ -35,18 +38,9 @@ import java.net.UnknownHostException; import java.net.URL; import java.net.URLConnection; import java.util.Arrays; -import java.util.List; import java.util.concurrent.TimeUnit; import javax.net.SocketFactory; -import com.android.okhttp.ConnectionPool; -import com.android.okhttp.Dns; -import com.android.okhttp.HttpHandler; -import com.android.okhttp.HttpsHandler; -import com.android.okhttp.OkHttpClient; -import com.android.okhttp.OkUrlFactory; -import com.android.okhttp.internal.Internal; - /** * Identifies a {@code Network}. This is supplied to applications via * {@link ConnectivityManager.NetworkCallback} in response to the active @@ -66,10 +60,9 @@ public class Network implements Parcelable { // Objects used to perform per-network operations such as getSocketFactory // and openConnection, and a lock to protect access to them. private volatile NetworkBoundSocketFactory mNetworkBoundSocketFactory = null; - // mLock should be used to control write access to mConnectionPool and mDns. - // maybeInitHttpClient() must be called prior to reading either variable. - private volatile ConnectionPool mConnectionPool = null; - private volatile Dns mDns = null; + // mLock should be used to control write access to mUrlConnectionFactory. + // maybeInitUrlConnectionFactory() must be called prior to reading this field. + private volatile HttpURLConnectionFactory mUrlConnectionFactory; private final Object mLock = new Object(); // Default connection pool values. These are evaluated at startup, just @@ -221,19 +214,19 @@ public class Network implements Parcelable { // will be instantiated in the near future with the same NetID. A good // solution would involve purging empty (or when all connections are timed // out) ConnectionPools. - private void maybeInitHttpClient() { + private void maybeInitUrlConnectionFactory() { synchronized (mLock) { - if (mDns == null) { - mDns = new Dns() { - @Override - public List<InetAddress> lookup(String hostname) throws UnknownHostException { - return Arrays.asList(Network.this.getAllByName(hostname)); - } - }; - } - if (mConnectionPool == null) { - mConnectionPool = new ConnectionPool(httpMaxConnections, + if (mUrlConnectionFactory == null) { + // Set configuration on the HttpURLConnectionFactory that will be good for all + // connections created by this Network. Configuration that might vary is left + // until openConnection() and passed as arguments. + Dns dnsLookup = hostname -> Arrays.asList(Network.this.getAllByName(hostname)); + HttpURLConnectionFactory urlConnectionFactory = new HttpURLConnectionFactory(); + urlConnectionFactory.setDns(dnsLookup); // Let traffic go via dnsLookup + // A private connection pool just for this Network. + urlConnectionFactory.setNewConnectionPool(httpMaxConnections, httpKeepAliveDurationMs, TimeUnit.MILLISECONDS); + mUrlConnectionFactory = urlConnectionFactory; } } } @@ -254,7 +247,7 @@ public class Network implements Parcelable { } // TODO: Should this be optimized to avoid fetching the global proxy for every request? final ProxyInfo proxyInfo = cm.getProxyForNetwork(this); - java.net.Proxy proxy = null; + final java.net.Proxy proxy; if (proxyInfo != null) { proxy = proxyInfo.makeProxy(); } else { @@ -276,26 +269,9 @@ public class Network implements Parcelable { */ public URLConnection openConnection(URL url, java.net.Proxy proxy) throws IOException { if (proxy == null) throw new IllegalArgumentException("proxy is null"); - maybeInitHttpClient(); - String protocol = url.getProtocol(); - OkUrlFactory okUrlFactory; - // TODO: HttpHandler creates OkUrlFactory instances that share the default ResponseCache. - // Could this cause unexpected behavior? - if (protocol.equals("http")) { - okUrlFactory = HttpHandler.createHttpOkUrlFactory(proxy); - } else if (protocol.equals("https")) { - okUrlFactory = HttpsHandler.createHttpsOkUrlFactory(proxy); - } else { - // OkHttp only supports HTTP and HTTPS and returns a null URLStreamHandler if - // passed another protocol. - throw new MalformedURLException("Invalid URL or unrecognized protocol " + protocol); - } - OkHttpClient client = okUrlFactory.client(); - client.setSocketFactory(getSocketFactory()).setConnectionPool(mConnectionPool); - // Let network traffic go via mDns - client.setDns(mDns); - - return okUrlFactory.open(url); + maybeInitUrlConnectionFactory(); + SocketFactory socketFactory = getSocketFactory(); + return mUrlConnectionFactory.openConnection(url, socketFactory, proxy); } /** |
