CronetEngine.Builder

public class CronetEngine.Builder


A builder for CronetEngines, which allows runtime configuration of CronetEngine. Configuration options are set on the builder and then build is called to create the CronetEngine.

Summary

Nested types

A class which provides a method for loading the cronet native library.

Constants

static final int

Setting to disable HTTP cache.

static final int

Setting to enable on-disk cache, including HTTP data.

static final int

Setting to enable on-disk cache, excluding HTTP data.

static final int

Setting to enable in-memory HTTP cache, including HTTP data.

Protected fields

JSONObject

JSON representation of the experimental options.

Public constructors

Builder(Context context)

Constructs a Builder object that facilitates creating a CronetEngine.

Public methods

CronetEngine.Builder
addPublicKeyPins(
    String hostName,
    Set<byte[]> pinsSha256,
    boolean includeSubdomains,
    Date expirationDate
)

Pins a set of public keys for a given host.

CronetEngine.Builder
addQuicHint(String host, int port, int alternatePort)

Adds hint that host supports QUIC.

CronetEngine

Build a CronetEngine using this builder's configuration.

CronetEngine.Builder
enableBrotli(boolean value)

Sets whether Brotli compression is enabled.

CronetEngine.Builder
enableHttp2(boolean value)

Sets whether HTTP/2 protocol is enabled.

CronetEngine.Builder
enableHttpCache(int cacheMode, long maxSize)

Enables or disables caching of HTTP data and other information like QUIC server information.

CronetEngine.Builder

Enables the network quality estimator, which collects and reports measurements of round trip time (RTT) and downstream throughput at various layers of the network stack.

CronetEngine.Builder

Enables or disables public key pinning bypass for local trust anchors.

CronetEngine.Builder
enableQuic(boolean value)

Sets whether QUIC protocol is enabled.

String

Constructs a User-Agent string including application name and version, system build version, model and id, and Cronet version.

CronetEngine.Builder
CronetEngine.Builder

Configures the behavior of connection migration.

CronetEngine.Builder

Configures the behavior of hostname lookup.

CronetEngine.Builder
CronetEngine.Builder

Sets a LibraryLoader to be used to load the native library.

CronetEngine.Builder

Configures proxying behavior.

CronetEngine.Builder
CronetEngine.Builder

Configures the behavior of Cronet when using QUIC.

CronetEngine.Builder

Sets directory for HTTP Cache and Cookie Storage.

CronetEngine.Builder
setThreadPriority(int priority)

This method is deprecated.

On modern versions of Cronet, this method does nothing.

CronetEngine.Builder
setUserAgent(String userAgent)

Overrides the User-Agent header for all requests.

Constants

HTTP_CACHE_DISABLED

public static final int HTTP_CACHE_DISABLED = 0

Setting to disable HTTP cache. Some data may still be temporarily stored in memory. Passed to enableHttpCache.

HTTP_CACHE_DISK

public static final int HTTP_CACHE_DISK = 3

Setting to enable on-disk cache, including HTTP data. setStoragePath must be called prior to passing this constant to enableHttpCache.

HTTP_CACHE_DISK_NO_HTTP

public static final int HTTP_CACHE_DISK_NO_HTTP = 2

Setting to enable on-disk cache, excluding HTTP data. setStoragePath must be called prior to passing this constant to enableHttpCache.

HTTP_CACHE_IN_MEMORY

public static final int HTTP_CACHE_IN_MEMORY = 1

Setting to enable in-memory HTTP cache, including HTTP data. Passed to enableHttpCache.

Protected fields

mParsedExperimentalOptions

protected JSONObject mParsedExperimentalOptions

JSON representation of the experimental options.

Public constructors

Builder

public Builder(Context context)

Constructs a Builder object that facilitates creating a CronetEngine. The default configuration enables HTTP/2 and QUIC, but disables the HTTP cache.

Parameters
Context context

Android Context, which is used by Builder to retrieve the application context. A reference to only the application context will be kept, so as to avoid extending the lifetime of context unnecessarily.

Public methods

addPublicKeyPins

public CronetEngine.Builder addPublicKeyPins(
    String hostName,
    Set<byte[]> pinsSha256,
    boolean includeSubdomains,
    Date expirationDate
)

Pins a set of public keys for a given host. By pinning a set of public keys, pinsSha256, communication with hostName is required to authenticate with a certificate with a public key from the set of pinned ones. An app can pin the public key of the root certificate, any of the intermediate certificates or the end-entry certificate. Authentication will fail and secure communication will not be established if none of the public keys is present in the host's certificate chain, even if the host attempts to authenticate with a certificate allowed by the device's trusted store of certificates.

Calling this method multiple times with the same host name overrides the previously set pins for the host.

More information about the public key pinning can be found in RFC 7469.

Parameters
String hostName

name of the host to which the public keys should be pinned. A host that consists only of digits and the dot character is treated as invalid.

Set<byte[]> pinsSha256

a set of pins. Each pin is the SHA-256 cryptographic hash of the DER-encoded ASN.1 representation of the Subject Public Key Info (SPKI) of the host's X.509 certificate. Use Certificate.getPublicKey() and Key.getEncoded() to obtain DER-encoded ASN.1 representation of the SPKI. Although, the method does not mandate the presence of the backup pin that can be used if the control of the primary private key has been lost, it is highly recommended to supply one.

boolean includeSubdomains

indicates whether the pinning policy should be applied to subdomains of hostName.

Date expirationDate

specifies the expiration date for the pins.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

Throws
java.lang.NullPointerException

if any of the input parameters are null.

java.lang.IllegalArgumentException

if the given host name is invalid or pinsSha256 contains a byte array that does not represent a valid SHA-256 hash.

addQuicHint

public CronetEngine.Builder addQuicHint(String host, int port, int alternatePort)

Adds hint that host supports QUIC. Note that enableHttpCache (HTTP_CACHE_DISK) is needed to take advantage of 0-RTT connection establishment between sessions.

Parameters
String host

hostname of the server that supports QUIC.

int port

host of the server that supports QUIC.

int alternatePort

alternate port to use for QUIC.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

build

public CronetEngine build()

Build a CronetEngine using this builder's configuration.

Returns
CronetEngine

constructed CronetEngine.

enableBrotli

public CronetEngine.Builder enableBrotli(boolean value)

Sets whether Brotli compression is enabled. If enabled, Brotli will be advertised in Accept-Encoding request headers. Defaults to disabled.

Parameters
boolean value

true to enable Brotli, false to disable.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

enableHttp2

public CronetEngine.Builder enableHttp2(boolean value)

Sets whether HTTP/2 protocol is enabled. Defaults to enabled.

Parameters
boolean value

true to enable HTTP/2, false to disable.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

enableHttpCache

public CronetEngine.Builder enableHttpCache(int cacheMode, long maxSize)

Enables or disables caching of HTTP data and other information like QUIC server information.

Parameters
int cacheMode

control location and type of cached data. Must be one of HTTP_CACHE_*.

long maxSize

maximum size in bytes used to cache data (advisory and maybe exceeded at times).

Returns
CronetEngine.Builder

the builder to facilitate chaining.

enableNetworkQualityEstimator

public CronetEngine.Builder enableNetworkQualityEstimator(boolean value)

Enables the network quality estimator, which collects and reports measurements of round trip time (RTT) and downstream throughput at various layers of the network stack. After enabling the estimator, listeners of RTT and throughput can be added with addRttListener and addThroughputListener and removed with removeRttListener and removeThroughputListener. The estimator uses memory and CPU only when enabled.

Parameters
boolean value

true to enable network quality estimator, false to disable.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

enablePublicKeyPinningBypassForLocalTrustAnchors

public CronetEngine.Builder enablePublicKeyPinningBypassForLocalTrustAnchors(boolean value)

Enables or disables public key pinning bypass for local trust anchors. Disabling the bypass for local trust anchors is highly discouraged since it may prohibit the app from communicating with the pinned hosts. E.g., a user may want to send all traffic through an SSL enabled proxy by changing the device proxy settings and adding the proxy certificate to the list of local trust anchor. Disabling the bypass will most likely prevent the app from sending any traffic to the pinned hosts. For more information see 'How does key pinning interact with local proxies and filters?' at https://www.chromium.org/Home/chromium-security/security-faq

Parameters
boolean value

true to enable the bypass, false to disable.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

enableQuic

public CronetEngine.Builder enableQuic(boolean value)

Sets whether QUIC protocol is enabled. Defaults to enabled. If QUIC is enabled, then QUIC User Agent Id containing application name and Cronet version is sent to the server.

Parameters
boolean value

true to enable QUIC, false to disable.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

getDefaultUserAgent

public String getDefaultUserAgent()

Constructs a User-Agent string including application name and version, system build version, model and id, and Cronet version.

Returns
String

User-Agent string.

setConnectionMigrationOptions

@ConnectionMigrationOptions.Experimental
public CronetEngine.Builder setConnectionMigrationOptions(
    ConnectionMigrationOptions connectionMigrationOptions
)

Configures the behavior of connection migration. For more details, see documentation of ConnectionMigrationOptions and the individual methods of ConnectionMigrationOptions.Builder.

Only relevant if enableQuic is enabled.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

setDnsOptions

@DnsOptions.Experimental
public CronetEngine.Builder setDnsOptions(DnsOptions dnsOptions)

Configures the behavior of hostname lookup. For more details, see documentation of DnsOptions and the individual methods of DnsOptions.Builder.

Only relevant if enableQuic is enabled.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

setLibraryLoader

public CronetEngine.Builder setLibraryLoader(CronetEngine.Builder.LibraryLoader loader)

Sets a LibraryLoader to be used to load the native library. If not set, the library will be loaded using loadLibrary.

Parameters
CronetEngine.Builder.LibraryLoader loader

LibraryLoader to be used to load the native library.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

setProxyOptions

public CronetEngine.Builder setProxyOptions(@NonNull ProxyOptions proxyOptions)

Configures proxying behavior. This affects, in different ways: connections establishment, UrlRequest and BidirectionalStream. For more details, refer to the documentation of Proxy.

This is not to be confused with proxy configuration that have been set up by: the user; or some enterprise profile configuration, or (most likely) some network autoconfiguration (e.g., Web Proxy Auto-Discovery Protocol). This is usually referred to as "system" proxy configuration. If present, respecting the system proxy configuration is often a requirement to obtain local and/or internet connectivity. CronetEngine already handles the system proxy configuration internally.

A proxy configuration defined via this API are refererred to as "app" proxy configuration. App and system proxy configuration are separate and, most importantly, differ. Currently, app and system proxy configurations are mutually exclusive: specifying ProxyOptions overrides the system proxy configuration, if present. This might cause connectivity problems in some scenarios where a system proxy configuration is present. In such scenarios, users might end up with no internet access, unless ProxyOptions has been configured with a final, null, fallback. Refer to ProxyOptions documentation.

Parameters
@NonNull ProxyOptions proxyOptions

ProxyOptions to be used for UrlRequest, BiridirectionalStream and connections established by the CronetEngine created by this builder.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

Throws
java.lang.UnsupportedOperationException

if the Cronet implementation being used is too old to support ProxyOptions.

setQuicOptions

@QuicOptions.Experimental
public CronetEngine.Builder setQuicOptions(QuicOptions quicOptions)

Configures the behavior of Cronet when using QUIC. For more details, see documentation of QuicOptions and the individual methods of QuicOptions.Builder.

Only relevant if enableQuic is enabled.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

setStoragePath

public CronetEngine.Builder setStoragePath(String value)

Sets directory for HTTP Cache and Cookie Storage. The directory must exist.

NOTE: Do not use the same storage directory with more than one CronetEngine at a time. Access to the storage directory does not support concurrent access by multiple CronetEngines.

Parameters
String value

path to existing directory.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

setThreadPriority

public CronetEngine.Builder setThreadPriority(int priority)

Sets the thread priority of Cronet's internal thread.

Parameters
int priority

the thread priority of Cronet's internal thread. A Linux priority level, from -20 for highest scheduling priority to 19 for lowest scheduling priority. For more information on values, see setThreadPriority and THREAD_PRIORITY_* values.

Returns
CronetEngine.Builder

the builder to facilitate chaining.

setUserAgent

public CronetEngine.Builder setUserAgent(String userAgent)

Overrides the User-Agent header for all requests. An explicitly set User-Agent header (set using addHeader) will override a value set using this function.

Parameters
String userAgent

the User-Agent string to use for all requests.

Returns
CronetEngine.Builder

the builder to facilitate chaining.