OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 package org.chromium.net; |
| 5 |
| 6 import android.content.Context; |
| 7 import android.support.annotation.VisibleForTesting; |
| 8 |
| 9 import java.io.IOException; |
| 10 import java.net.Proxy; |
| 11 import java.net.URL; |
| 12 import java.net.URLConnection; |
| 13 import java.util.Date; |
| 14 import java.util.Set; |
| 15 import java.util.concurrent.Executor; |
| 16 |
| 17 /** |
| 18 * {@link CronetEngine} that exposes experimental features. Use {@link Builder}
to build an |
| 19 * instance of this class. Every instance of {@link CronetEngine} can be casted
to an instance |
| 20 * of this class. |
| 21 * |
| 22 * {@hide since this class exposes experimental features that should be hidden.} |
| 23 */ |
| 24 public abstract class ExperimentalCronetEngine extends CronetEngine { |
| 25 /** |
| 26 * Builder for building {@link ExperimentalCronetEngine}. |
| 27 */ |
| 28 public static class Builder extends CronetEngine.Builder { |
| 29 /** |
| 30 * Default config enables SPDY, disables QUIC, SDCH and HTTP cache. |
| 31 * |
| 32 * @param context Android {@link Context} for engine to use. |
| 33 */ |
| 34 public Builder(Context context) { |
| 35 super(context); |
| 36 } |
| 37 |
| 38 /** |
| 39 * Enables the network quality estimator, which collects and reports |
| 40 * measurements of round trip time (RTT) and downstream throughput at |
| 41 * various layers of the network stack. After enabling the estimator, |
| 42 * listeners of RTT and throughput can be added with |
| 43 * {@link #addRttListener} and {@link #addThroughputListener} and |
| 44 * removed with {@link #removeRttListener} and |
| 45 * {@link #removeThroughputListener}. The estimator uses memory and CPU |
| 46 * only when enabled. |
| 47 * @param value {@code true} to enable network quality estimator, |
| 48 * {@code false} to disable. |
| 49 * @return the builder to facilitate chaining. |
| 50 */ |
| 51 public Builder enableNetworkQualityEstimator(boolean value) { |
| 52 mBuilderDelegate.enableNetworkQualityEstimator(value); |
| 53 return this; |
| 54 } |
| 55 |
| 56 /** |
| 57 * Initializes CachingCertVerifier's cache with certVerifierData which h
as |
| 58 * the results of certificate verification. |
| 59 * @param certVerifierData a serialized representation of certificate |
| 60 * verification results. |
| 61 * @return the builder to facilitate chaining. |
| 62 */ |
| 63 public Builder setCertVerifierData(String certVerifierData) { |
| 64 mBuilderDelegate.setCertVerifierData(certVerifierData); |
| 65 return this; |
| 66 } |
| 67 |
| 68 /** |
| 69 * Overrides |
| 70 * <a href="https://developer.chrome.com/multidevice/data-compression"> |
| 71 * Data Reduction Proxy</a> configuration parameters with a primary |
| 72 * proxy name, fallback proxy name, and a secure proxy check URL. Proxie
s |
| 73 * are specified as [scheme://]host[:port]. Used for testing. |
| 74 * @param primaryProxy the primary data reduction proxy to use. |
| 75 * @param fallbackProxy a fallback data reduction proxy to use. |
| 76 * @param secureProxyCheckUrl a URL to fetch to determine if using a sec
ure |
| 77 * proxy is allowed. |
| 78 * @return the builder to facilitate chaining. |
| 79 */ |
| 80 public Builder setDataReductionProxyOptions( |
| 81 String primaryProxy, String fallbackProxy, String secureProxyChe
ckUrl) { |
| 82 mBuilderDelegate.setDataReductionProxyOptions( |
| 83 primaryProxy, fallbackProxy, secureProxyCheckUrl); |
| 84 return this; |
| 85 } |
| 86 |
| 87 /** |
| 88 * Sets whether the resulting {@link CronetEngine} uses an |
| 89 * implementation based on the system's |
| 90 * {@link java.net.HttpURLConnection} implementation, or if this is |
| 91 * only done as a backup if the native implementation fails to load. |
| 92 * Defaults to disabled. |
| 93 * @param value {@code true} makes the resulting {@link CronetEngine} |
| 94 * use an implementation based on the system's |
| 95 * {@link java.net.HttpURLConnection} implementation |
| 96 * without trying to load the native implementation. |
| 97 * {@code false} makes the resulting {@code CronetEngine} |
| 98 * use the native implementation, or if that fails to load, |
| 99 * falls back to an implementation based on the system's |
| 100 * {@link java.net.HttpURLConnection} implementation. |
| 101 * @return the builder to facilitate chaining. |
| 102 */ |
| 103 public Builder enableLegacyMode(boolean value) { |
| 104 mBuilderDelegate.enableLegacyMode(value); |
| 105 return this; |
| 106 } |
| 107 |
| 108 /** |
| 109 * Enables |
| 110 * <a href="https://developer.chrome.com/multidevice/data-compression">D
ata |
| 111 * Reduction Proxy</a>. Defaults to disabled. |
| 112 * @param key key to use when authenticating with the proxy. |
| 113 * @return the builder to facilitate chaining. |
| 114 */ |
| 115 public Builder enableDataReductionProxy(String key) { |
| 116 mBuilderDelegate.enableDataReductionProxy(key); |
| 117 return this; |
| 118 } |
| 119 |
| 120 @VisibleForTesting |
| 121 ICronetEngineBuilder getBuilderDelegate() { |
| 122 return mBuilderDelegate; |
| 123 } |
| 124 |
| 125 // To support method chaining, override superclass methods to return an |
| 126 // instance of this class instead of the parent. |
| 127 |
| 128 @Override |
| 129 public Builder setUserAgent(String userAgent) { |
| 130 super.setUserAgent(userAgent); |
| 131 return this; |
| 132 } |
| 133 |
| 134 @Override |
| 135 public Builder setStoragePath(String value) { |
| 136 super.setStoragePath(value); |
| 137 return this; |
| 138 } |
| 139 |
| 140 @Override |
| 141 public Builder setLibraryLoader(LibraryLoader loader) { |
| 142 super.setLibraryLoader(loader); |
| 143 return this; |
| 144 } |
| 145 |
| 146 @Override |
| 147 public Builder enableQuic(boolean value) { |
| 148 super.enableQuic(value); |
| 149 return this; |
| 150 } |
| 151 |
| 152 @Override |
| 153 public Builder enableHttp2(boolean value) { |
| 154 super.enableHttp2(value); |
| 155 return this; |
| 156 } |
| 157 |
| 158 @Override |
| 159 public Builder enableSdch(boolean value) { |
| 160 super.enableSdch(value); |
| 161 return this; |
| 162 } |
| 163 |
| 164 @Override |
| 165 public Builder enableHttpCache(int cacheMode, long maxSize) { |
| 166 super.enableHttpCache(cacheMode, maxSize); |
| 167 return this; |
| 168 } |
| 169 |
| 170 @Override |
| 171 public Builder addQuicHint(String host, int port, int alternatePort) { |
| 172 super.addQuicHint(host, port, alternatePort); |
| 173 return this; |
| 174 } |
| 175 |
| 176 @Override |
| 177 public Builder addPublicKeyPins(String hostName, Set<byte[]> pinsSha256, |
| 178 boolean includeSubdomains, Date expirationDate) { |
| 179 super.addPublicKeyPins(hostName, pinsSha256, includeSubdomains, expi
rationDate); |
| 180 return this; |
| 181 } |
| 182 |
| 183 @Override |
| 184 public Builder enablePublicKeyPinningBypassForLocalTrustAnchors(boolean
value) { |
| 185 super.enablePublicKeyPinningBypassForLocalTrustAnchors(value); |
| 186 return this; |
| 187 } |
| 188 |
| 189 @Override |
| 190 public Builder setExperimentalOptions(String options) { |
| 191 super.setExperimentalOptions(options); |
| 192 return this; |
| 193 } |
| 194 |
| 195 @Override |
| 196 public ExperimentalCronetEngine build() { |
| 197 return mBuilderDelegate.build(); |
| 198 } |
| 199 } |
| 200 |
| 201 /** |
| 202 * Creates a builder for {@link BidirectionalStream} objects. All callbacks
for |
| 203 * generated {@code BidirectionalStream} objects will be invoked on |
| 204 * {@code executor}. {@code executor} must not run tasks on the |
| 205 * current thread, otherwise the networking operations may block and excepti
ons |
| 206 * may be thrown at shutdown time. |
| 207 * |
| 208 * @param url URL for the generated streams. |
| 209 * @param callback the {@link BidirectionalStream.Callback} object that gets
invoked upon |
| 210 * different events occurring. |
| 211 * @param executor the {@link Executor} on which {@code callback} methods wi
ll be invoked. |
| 212 * |
| 213 * @return the created builder. |
| 214 */ |
| 215 public abstract ExperimentalBidirectionalStream.Builder newBidirectionalStre
amBuilder( |
| 216 String url, BidirectionalStream.Callback callback, Executor executor
); |
| 217 |
| 218 @Override |
| 219 public abstract ExperimentalUrlRequest.Builder newUrlRequestBuilder( |
| 220 String url, UrlRequest.Callback callback, Executor executor); |
| 221 |
| 222 /** |
| 223 * Starts NetLog logging to a specified directory with a bounded size. The N
etLog will contain |
| 224 * events emitted by all live CronetEngines. The NetLog is useful for debugg
ing. |
| 225 * The log can be viewed by stitching the files using net/log/stitch_net_log
_files.py and |
| 226 * using a Chrome browser navigated to chrome://net-internals/#import |
| 227 * @param dirPath the directory where the log files will be created. It must
already exist. |
| 228 * NetLog files must not already exist in the directory. If activ
ely logging, |
| 229 * this method is ignored. |
| 230 * @param logAll {@code true} to include basic events, user cookies, |
| 231 * credentials and all transferred bytes in the log. This option
presents a |
| 232 * privacy risk, since it exposes the user's credentials, and sho
uld only be |
| 233 * used with the user's consent and in situations where the log w
on't be public. |
| 234 * {@code false} to just include basic events. |
| 235 * @param maxSize the maximum total disk space in bytes that should be used
by NetLog. Actual |
| 236 * disk space usage may exceed this limit slightly. |
| 237 */ |
| 238 public abstract void startNetLogToDisk(String dirPath, boolean logAll, int m
axSize); |
| 239 |
| 240 /** |
| 241 * Returns the effective connection type computed by the network quality |
| 242 * estimator. |
| 243 */ |
| 244 public abstract int getEffectiveConnectionType(); |
| 245 |
| 246 /** |
| 247 * Configures the network quality estimator for testing. This must be called |
| 248 * before round trip time and throughput listeners are added, and after the |
| 249 * network quality estimator has been enabled. |
| 250 * @param useLocalHostRequests include requests to localhost in estimates. |
| 251 * @param useSmallerResponses include small responses in throughput |
| 252 * estimates. |
| 253 */ |
| 254 public abstract void configureNetworkQualityEstimatorForTesting( |
| 255 boolean useLocalHostRequests, boolean useSmallerResponses); |
| 256 |
| 257 /** |
| 258 * Registers a listener that gets called whenever the network quality |
| 259 * estimator witnesses a sample round trip time. This must be called |
| 260 * after {@link Builder#enableNetworkQualityEstimator}, and with throw an |
| 261 * exception otherwise. Round trip times may be recorded at various layers |
| 262 * of the network stack, including TCP, QUIC, and at the URL request layer. |
| 263 * The listener is called on the {@link java.util.concurrent.Executor} that |
| 264 * is passed to {@link Builder#enableNetworkQualityEstimator}. |
| 265 * @param listener the listener of round trip times. |
| 266 */ |
| 267 public abstract void addRttListener(NetworkQualityRttListener listener); |
| 268 |
| 269 /** |
| 270 * Removes a listener of round trip times if previously registered with |
| 271 * {@link #addRttListener}. This should be called after a |
| 272 * {@link NetworkQualityRttListener} is added in order to stop receiving |
| 273 * observations. |
| 274 * @param listener the listener of round trip times. |
| 275 */ |
| 276 public abstract void removeRttListener(NetworkQualityRttListener listener); |
| 277 |
| 278 /** |
| 279 * Registers a listener that gets called whenever the network quality |
| 280 * estimator witnesses a sample throughput measurement. This must be called |
| 281 * after {@link Builder#enableNetworkQualityEstimator}. Throughput observati
ons |
| 282 * are computed by measuring bytes read over the active network interface |
| 283 * at times when at least one URL response is being received. The listener |
| 284 * is called on the {@link java.util.concurrent.Executor} that is passed to |
| 285 * {@link Builder#enableNetworkQualityEstimator}. |
| 286 * @param listener the listener of throughput. |
| 287 */ |
| 288 public abstract void addThroughputListener(NetworkQualityThroughputListener
listener); |
| 289 |
| 290 /** |
| 291 * Removes a listener of throughput. This should be called after a |
| 292 * {@link NetworkQualityThroughputListener} is added with |
| 293 * {@link #addThroughputListener} in order to stop receiving observations. |
| 294 * @param listener the listener of throughput. |
| 295 */ |
| 296 public abstract void removeThroughputListener(NetworkQualityThroughputListen
er listener); |
| 297 |
| 298 /** |
| 299 * Establishes a new connection to the resource specified by the {@link URL}
{@code url} |
| 300 * using the given proxy. |
| 301 * <p> |
| 302 * <b>Note:</b> Cronet's {@link java.net.HttpURLConnection} implementation i
s subject to certain |
| 303 * limitations, see {@link #createURLStreamHandlerFactory} for details. |
| 304 * |
| 305 * @param url URL of resource to connect to. |
| 306 * @param proxy proxy to use when establishing connection. |
| 307 * @return an {@link java.net.HttpURLConnection} instance implemented by thi
s CronetEngine. |
| 308 * @throws IOException if an error occurs while opening the connection. |
| 309 */ |
| 310 // TODO(pauljensen): Expose once implemented, http://crbug.com/418111 |
| 311 public abstract URLConnection openConnection(URL url, Proxy proxy) throws IO
Exception; |
| 312 |
| 313 /** |
| 314 * Registers a listener that gets called after the end of each request with
the request info. |
| 315 * |
| 316 * <p>The listener is called on an {@link java.util.concurrent.Executor} pro
vided by the |
| 317 * listener. |
| 318 * |
| 319 * @param listener the listener for finished requests. |
| 320 */ |
| 321 public abstract void addRequestFinishedListener(RequestFinishedInfo.Listener
listener); |
| 322 |
| 323 /** |
| 324 * Removes a finished request listener. |
| 325 * |
| 326 * @param listener the listener to remove. |
| 327 */ |
| 328 public abstract void removeRequestFinishedListener(RequestFinishedInfo.Liste
ner listener); |
| 329 |
| 330 /** |
| 331 * Returns serialized representation of certificate verifier's cache |
| 332 * which contains the list of hosts/certificates and the certificate |
| 333 * verification results. May block until data is received from the network |
| 334 * thread (will timeout after the specified timeout). In case of timeout, it |
| 335 * returns the previous saved value. |
| 336 * |
| 337 * @param timeout in milliseconds. If timeout is 0, it will use default valu
e. |
| 338 * @return serialized representation of certificate verification results |
| 339 * data. |
| 340 */ |
| 341 public abstract String getCertVerifierData(long timeout); |
| 342 |
| 343 /** |
| 344 * Returns the HTTP RTT estimate (in milliseconds) computed by the network |
| 345 * quality estimator. Set to |
| 346 * {@link RttThroughputValues.INVALID_RTT_THROUGHPUT} if a valid value |
| 347 * is unavailable. This must be called after |
| 348 * {@link Builder#enableNetworkQualityEstimator}, and will throw an |
| 349 * exception otherwise. |
| 350 * @return Estimate of the HTTP RTT in milliseconds. |
| 351 */ |
| 352 public abstract int getHttpRttMs(); |
| 353 |
| 354 /** |
| 355 * Returns the transport RTT estimate (in milliseconds) computed by the |
| 356 * network quality estimator. Set to |
| 357 * {@link RttThroughputValues.INVALID_RTT_THROUGHPUT} if a valid value is |
| 358 * unavailable. This must be called after |
| 359 * {@link Builder#enableNetworkQualityEstimator}, and will throw an |
| 360 * exception otherwise. |
| 361 * @return Estimate of the transport RTT in milliseconds. |
| 362 */ |
| 363 public abstract int getTransportRttMs(); |
| 364 |
| 365 /** |
| 366 * Returns the downstream throughput estimate (in kilobits per second) |
| 367 * computed by the network quality estimator. Set to |
| 368 * {@link RttThroughputValues.INVALID_RTT_THROUGHPUT} if a valid value is |
| 369 * unavailable. This must be called after |
| 370 * {@link Builder#enableNetworkQualityEstimator}, and will |
| 371 * throw an exception otherwise. |
| 372 * @return Estimate of the downstream throughput in kilobits per second. |
| 373 */ |
| 374 public abstract int getDownstreamThroughputKbps(); |
| 375 } |
OLD | NEW |