diff options
| author | 2017-08-22 15:16:31 +0000 | |
|---|---|---|
| committer | 2017-08-22 15:16:31 +0000 | |
| commit | 0aa55f7b2cc187d55147aaf54ab77a0592febb77 (patch) | |
| tree | 84cdd0ffbb0b1a2134295f7bbc274e55ab7594e5 | |
| parent | dc46874c512936812fff68c36545178160d4adba (diff) | |
| parent | 70b000e6e28e3222453ae781e73e55451a149e11 (diff) | |
Merge "Network: Use HttpURLConnectionFactory rather than OkHttp APIs" am: be87c9ca58
am: 70b000e6e2
Change-Id: I0e15e22f4e1561102012b012cdea74db63ac2163
| -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); } /** |