CronetEngine.Builder

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

const Int

Setting to disable HTTP cache.

const Int

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

const Int

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

const Int

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

Public constructors

Builder(context: Context!)

Constructs a Builder object that facilitates creating a CronetEngine.

Public functions

CronetEngine.Builder!
addPublicKeyPins(
    hostName: String!,
    pinsSha256: (Mutable)Set<ByteArray!>!,
    includeSubdomains: Boolean,
    expirationDate: Date!
)

Pins a set of public keys for a given host.

CronetEngine.Builder!
addQuicHint(host: String!, port: Int, alternatePort: Int)

Adds hint that host supports QUIC.

CronetEngine!

Build a CronetEngine using this builder's configuration.

CronetEngine.Builder!

Sets whether Brotli compression is enabled.

CronetEngine.Builder!

Sets whether HTTP/2 protocol is enabled.

CronetEngine.Builder!
enableHttpCache(cacheMode: Int, maxSize: Long)

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!

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!

This function is deprecated.

On modern versions of Cronet, this method does nothing.

CronetEngine.Builder!
setUserAgent(userAgent: String!)

Overrides the User-Agent header for all requests.

Protected properties

JSONObject!

JSON representation of the experimental options.

Constants

HTTP_CACHE_DISABLED

const val HTTP_CACHE_DISABLED = 0: Int

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

HTTP_CACHE_DISK

const val HTTP_CACHE_DISK = 3: Int

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

const val HTTP_CACHE_DISK_NO_HTTP = 2: Int

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

HTTP_CACHE_IN_MEMORY

const val HTTP_CACHE_IN_MEMORY = 1: Int

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

Public constructors

Builder

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 functions

addPublicKeyPins

fun addPublicKeyPins(
    hostName: String!,
    pinsSha256: (Mutable)Set<ByteArray!>!,
    includeSubdomains: Boolean,
    expirationDate: Date!
): CronetEngine.Builder!

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
hostName: String!

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.

pinsSha256: (Mutable)Set<ByteArray!>!

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.

includeSubdomains: Boolean

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

expirationDate: Date!

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

fun addQuicHint(host: String!, port: Int, alternatePort: Int): CronetEngine.Builder!

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
host: String!

hostname of the server that supports QUIC.

port: Int

host of the server that supports QUIC.

alternatePort: Int

alternate port to use for QUIC.

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

build

fun build(): CronetEngine!

Build a CronetEngine using this builder's configuration.

Returns
CronetEngine!

constructed CronetEngine.

enableBrotli

fun enableBrotli(value: Boolean): CronetEngine.Builder!

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

Parameters
value: Boolean

true to enable Brotli, false to disable.

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

enableHttp2

fun enableHttp2(value: Boolean): CronetEngine.Builder!

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

Parameters
value: Boolean

true to enable HTTP/2, false to disable.

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

enableHttpCache

fun enableHttpCache(cacheMode: Int, maxSize: Long): CronetEngine.Builder!

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

Parameters
cacheMode: Int

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

maxSize: Long

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

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

enableNetworkQualityEstimator

fun enableNetworkQualityEstimator(value: Boolean): 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. 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
value: Boolean

true to enable network quality estimator, false to disable.

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

enablePublicKeyPinningBypassForLocalTrustAnchors

fun enablePublicKeyPinningBypassForLocalTrustAnchors(value: Boolean): CronetEngine.Builder!

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
value: Boolean

true to enable the bypass, false to disable.

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

enableQuic

fun enableQuic(value: Boolean): CronetEngine.Builder!

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
value: Boolean

true to enable QUIC, false to disable.

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

getDefaultUserAgent

fun getDefaultUserAgent(): String!

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
fun setConnectionMigrationOptions(
    connectionMigrationOptions: ConnectionMigrationOptions!
): CronetEngine.Builder!

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
fun setDnsOptions(dnsOptions: DnsOptions!): CronetEngine.Builder!

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

fun setLibraryLoader(loader: CronetEngine.Builder.LibraryLoader!): CronetEngine.Builder!

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

Parameters
loader: CronetEngine.Builder.LibraryLoader!

LibraryLoader to be used to load the native library.

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

setProxyOptions

fun setProxyOptions(proxyOptions: ProxyOptions): CronetEngine.Builder!

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
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
fun setQuicOptions(quicOptionsBuilder: QuicOptions.Builder!): CronetEngine.Builder!
See also
setQuicOptions

setQuicOptions

@QuicOptions.Experimental
fun setQuicOptions(quicOptions: QuicOptions!): CronetEngine.Builder!

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

fun setStoragePath(value: String!): CronetEngine.Builder!

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
value: String!

path to existing directory.

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

setThreadPriority

fun setThreadPriority(priority: Int): CronetEngine.Builder!

Sets the thread priority of Cronet's internal thread.

Parameters
priority: Int

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

fun setUserAgent(userAgent: String!): CronetEngine.Builder!

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
userAgent: String!

the User-Agent string to use for all requests.

Returns
CronetEngine.Builder!

the builder to facilitate chaining.

Protected properties

mParsedExperimentalOptions

protected val mParsedExperimentalOptionsJSONObject!

JSON representation of the experimental options.