1 // Copyright 2014 The Chromium Authors 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.net.urlconnection; 6 7 import org.chromium.net.ExperimentalCronetEngine; 8 9 import androidx.annotation.VisibleForTesting; 10 11 import java.io.IOException; 12 import java.net.Proxy; 13 import java.net.URL; 14 import java.net.URLConnection; 15 import java.net.URLStreamHandler; 16 17 /** 18 * A {@link URLStreamHandler} that handles HTTP and HTTPS connections. One can use this class to 19 * create {@link java.net.HttpURLConnection} instances implemented by Cronet; for example: <pre> 20 * 21 * CronetHttpURLStreamHandler streamHandler = new CronetHttpURLStreamHandler(myContext); 22 * HttpURLConnection connection = (HttpURLConnection)streamHandler.openConnection( 23 * new URL("http://chromium.org"));</pre> 24 * <b>Note:</b> Cronet's {@code HttpURLConnection} implementation is subject to some limitations 25 * listed {@link CronetURLStreamHandlerFactory here}. 26 */ 27 @VisibleForTesting 28 public class CronetHttpURLStreamHandler extends URLStreamHandler { 29 private final ExperimentalCronetEngine mCronetEngine; 30 CronetHttpURLStreamHandler(ExperimentalCronetEngine cronetEngine)31 public CronetHttpURLStreamHandler(ExperimentalCronetEngine cronetEngine) { 32 mCronetEngine = cronetEngine; 33 } 34 35 /** 36 * Establishes a new connection to the resource specified by the {@link URL} {@code url}. 37 * @return an {@link java.net.HttpURLConnection} instance implemented by Cronet. 38 */ 39 @Override openConnection(URL url)40 public URLConnection openConnection(URL url) throws IOException { 41 return mCronetEngine.openConnection(url); 42 } 43 44 /** 45 * Establishes a new connection to the resource specified by the {@link URL} {@code url} 46 * using the given proxy. 47 * @return an {@link java.net.HttpURLConnection} instance implemented by Cronet. 48 */ 49 @Override openConnection(URL url, Proxy proxy)50 public URLConnection openConnection(URL url, Proxy proxy) throws IOException { 51 return mCronetEngine.openConnection(url, proxy); 52 } 53 } 54