Profile


interface Profile


A Profile represents one browsing session for WebView.

You can have multiple profiles and each profile holds its own set of data. The creation and deletion of the Profile is being managed by ProfileStore.

Summary

Nested types

@Retention(value = RetentionPolicy.CLASS)
@Target(value = [ElementType.METHOD, ElementType.TYPE, ElementType.FIELD])
@RequiresOptIn(level = RequiresOptIn.Level.ERROR)
annotation Profile.ExperimentalUrlPrefetch

Denotes that the UrlPrefetch API surface is experimental.

@Retention(value = RetentionPolicy.CLASS)
@Target(value = [ElementType.METHOD, ElementType.TYPE, ElementType.FIELD])
@RequiresOptIn(level = RequiresOptIn.Level.ERROR)
annotation Profile.ExperimentalWarmUpRendererProcess

Denotes that the WarmUpRendererProcess API surface is experimental.

Constants

const String!

Represents the name of the default profile which can't be deleted.

Public functions

Unit
@RequiresFeature(name = WebViewFeature.ORIGIN_MATCHED_HEADERS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
clearAllOriginMatchedHeaders()

Remove any currently set headers from being applied to network requests.

Unit
@RequiresFeature(name = WebViewFeature.ORIGIN_MATCHED_HEADERS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
clearOriginMatchedHeader(headerName: String)

Removes the specified header from the set of headers attached to requests.

Unit
@RequiresFeature(name = WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
clearPrefetchAsync(
    url: String,
    callbackExecutor: Executor,
    operationCallback: OutcomeReceiverCompat<Void!, PrefetchException!>
)

Removes a cached prefetch response for the provided url if it exists, otherwise does nothing.

CookieManager
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getCookieManager()

Returns the profile's cookie manager.

GeolocationPermissions
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getGeolocationPermissions()

Returns the geolocation permissions of the profile.

String
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getName()
ServiceWorkerController
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getServiceWorkerController()

Returns the service worker controller of the profile.

WebStorage
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebStorage()

Returns the profile's web storage.

Unit
@RequiresFeature(name = WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
prefetchUrlAsync(
    url: String,
    cancellationSignal: CancellationSignal?,
    callbackExecutor: Executor,
    operationCallback: OutcomeReceiverCompat<Void!, PrefetchException!>
)

Starts a URL prefetch request.

Unit
@RequiresFeature(name = WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
prefetchUrlAsync(
    url: String,
    cancellationSignal: CancellationSignal?,
    callbackExecutor: Executor,
    speculativeLoadingParameters: SpeculativeLoadingParameters,
    operationCallback: OutcomeReceiverCompat<Void!, PrefetchException!>
)

Starts a URL prefetch request.

Unit
@RequiresFeature(name = WebViewFeature.ORIGIN_MATCHED_HEADERS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
setOriginMatchedHeader(
    headerName: String,
    headerValue: String,
    originRules: (Mutable)Set<String!>
)

Set a custom header to be applied to HTTP requests to the specified origins.

Unit
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING_CONFIG, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
setSpeculativeLoadingConfig(
    speculativeLoadingConfig: SpeculativeLoadingConfig
)

Sets the SpeculativeLoadingConfig for the current profile session.

Unit
@RequiresFeature(name = WebViewFeature.WARM_UP_RENDERER_PROCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalWarmUpRendererProcess
warmUpRendererProcess()

Initiates warm-up of the renderer process associated with this Profile.

Constants

DEFAULT_PROFILE_NAME

Added in 1.9.0
const val DEFAULT_PROFILE_NAME = "Default": String!

Represents the name of the default profile which can't be deleted.

Public functions

clearAllOriginMatchedHeaders

@RequiresFeature(name = WebViewFeature.ORIGIN_MATCHED_HEADERS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
fun clearAllOriginMatchedHeaders(): Unit

Remove any currently set headers from being applied to network requests.

clearOriginMatchedHeader

@RequiresFeature(name = WebViewFeature.ORIGIN_MATCHED_HEADERS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
fun clearOriginMatchedHeader(headerName: String): Unit

Removes the specified header from the set of headers attached to requests.

It is safe to call this method even if headerName has not previously been set via setOriginMatchedHeader

Parameters
headerName: String

Header to remove.

clearPrefetchAsync

Added in 1.13.0
@RequiresFeature(name = WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun clearPrefetchAsync(
    url: String,
    callbackExecutor: Executor,
    operationCallback: OutcomeReceiverCompat<Void!, PrefetchException!>
): Unit

Removes a cached prefetch response for the provided url if it exists, otherwise does nothing.

Calling this does not guarantee that the prefetched response will not be served to a WebView before it is cleared.

Parameters
url: String

the url associated with the prefetch request. Should be an exact match with the URL passed to prefetchUrlAsync.

callbackExecutor: Executor

the executor to resolve the callback with.

operationCallback: OutcomeReceiverCompat<Void!, PrefetchException!>

runs when the clear operation is complete Or and error occurred during it.

Throws
java.lang.IllegalArgumentException

if the url or callback is null.

getCookieManager

Added in 1.9.0
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
fun getCookieManager(): CookieManager

Returns the profile's cookie manager.

Can be called from any thread.

Throws
java.lang.IllegalStateException

if the profile has been deleted by deleteProfile}.

getGeolocationPermissions

Added in 1.9.0
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
fun getGeolocationPermissions(): GeolocationPermissions

Returns the geolocation permissions of the profile.

Can be called from any thread.

Throws
java.lang.IllegalStateException

if the profile has been deleted by deleteProfile}.

getName

Added in 1.9.0
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
fun getName(): String
Returns
String

the name of this Profile which was used to create the Profile from ProfileStore create methods.

getServiceWorkerController

Added in 1.9.0
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
fun getServiceWorkerController(): ServiceWorkerController

Returns the service worker controller of the profile.

Can be called from any thread.

Throws
java.lang.IllegalStateException

if the profile has been deleted by deleteProfile}.

getWebStorage

Added in 1.9.0
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
fun getWebStorage(): WebStorage

Returns the profile's web storage.

Can be called from any thread.

Throws
java.lang.IllegalStateException

if the profile has been deleted by deleteProfile}.

prefetchUrlAsync

Added in 1.13.0
@RequiresFeature(name = WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun prefetchUrlAsync(
    url: String,
    cancellationSignal: CancellationSignal?,
    callbackExecutor: Executor,
    operationCallback: OutcomeReceiverCompat<Void!, PrefetchException!>
): Unit

Starts a URL prefetch request. Must be called from the UI thread.

All WebViews associated with this Profile will use a URL request matching algorithm during execution of all variants of loadUrl for determining if there was already a prefetch request executed for the provided URL. This includes prefetches that are "in progress". If a prefetch is matched, WebView will leverage that for handling the URL, otherwise the URL will be handled normally (i.e. through a network request).

Applications will still be responsible for calling loadUrl to display web contents in a WebView.

NOTE: Additional headers passed to loadUrl are not considered in the matching algorithm for determining whether or not to serve a prefetched response to a navigation.

For max latency saving benefits, it is recommended to call this method as early as possible (i.e. before any WebView associated with this profile is created).

Only supports HTTPS scheme.

Parameters
url: String

the url associated with the prefetch request.

cancellationSignal: CancellationSignal?

will make the best effort to cancel an in-flight prefetch request, However cancellation is not guaranteed.

callbackExecutor: Executor

the executor to resolve the callback with.

operationCallback: OutcomeReceiverCompat<Void!, PrefetchException!>

callbacks for reporting result back to application.

Throws
java.lang.IllegalArgumentException

if the url or callback is null.

prefetchUrlAsync

Added in 1.13.0
@RequiresFeature(name = WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun prefetchUrlAsync(
    url: String,
    cancellationSignal: CancellationSignal?,
    callbackExecutor: Executor,
    speculativeLoadingParameters: SpeculativeLoadingParameters,
    operationCallback: OutcomeReceiverCompat<Void!, PrefetchException!>
): Unit

Starts a URL prefetch request. Must be called from the UI thread.

All WebViews associated with this Profile will use a URL request matching algorithm during execution of all variants of loadUrl for determining if there was already a prefetch request executed for the provided URL. This includes prefetches that are "in progress". If a prefetch is matched, WebView will leverage that for handling the URL, otherwise the URL will be handled normally (i.e. through a network request).

Applications will still be responsible for calling loadUrl to display web contents in a WebView.

NOTE: Additional headers passed to loadUrl are not considered in the matching algorithm for determining whether or not to serve a prefetched response to a navigation.

For max latency saving benefits, it is recommended to call this method as early as possible (i.e. before any WebView associated with this profile is created).

Only supports HTTPS scheme.

Parameters
url: String

the url associated with the prefetch request.

cancellationSignal: CancellationSignal?

will make the best effort to cancel an in-flight prefetch request, However cancellation is not guaranteed.

callbackExecutor: Executor

the executor to resolve the callback with.

speculativeLoadingParameters: SpeculativeLoadingParameters

parameters to customize the prefetch request.

operationCallback: OutcomeReceiverCompat<Void!, PrefetchException!>

callbacks for reporting result back to application.

Throws
java.lang.IllegalArgumentException

if the url or callback is null.

setOriginMatchedHeader

@RequiresFeature(name = WebViewFeature.ORIGIN_MATCHED_HEADERS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
fun setOriginMatchedHeader(
    headerName: String,
    headerValue: String,
    originRules: (Mutable)Set<String!>
): Unit

Set a custom header to be applied to HTTP requests to the specified origins.

It applies to all requests that are initiated after this method is called, including prefetch requests and requests sent from service workers. It does not apply the header to WebSocket requests.

Headers added through this API will be present in the set returned by getRequestHeaders provided in shouldInterceptRequest and shouldInterceptRequest.

Parameters
headerName: String

A valid HTTP header name string

headerValue: String

A valid HTTP value name string

originRules: (Mutable)Set<String!>

a set of origin rules following the same format as addWebMessageListener

Throws
java.lang.IllegalStateException

if the headerName has already been set. Use clearOriginMatchedHeader first in order to update the set header.

setSpeculativeLoadingConfig

Added in 1.13.0
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING_CONFIG, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun setSpeculativeLoadingConfig(
    speculativeLoadingConfig: SpeculativeLoadingConfig
): Unit

Sets the SpeculativeLoadingConfig for the current profile session. These configurations will be applied to any Prefetch requests made after they are set; they will not be applied to in-flight requests.

These configurations will be applied to any prefetch requests initiated by a prerender request. This applies specifically to WebViews that are associated with this Profile.

Parameters
speculativeLoadingConfig: SpeculativeLoadingConfig

the config to set for this profile session.

warmUpRendererProcess

@RequiresFeature(name = WebViewFeature.WARM_UP_RENDERER_PROCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalWarmUpRendererProcess
fun warmUpRendererProcess(): Unit

Initiates warm-up of the renderer process associated with this Profile.

If no renderer currently exists for the profile, this will kick off the process of starting one in the background. This call does not block or guarantee that the renderer will be fully started by the time it returns.

This can be used to reduce perceived latency when a renderer is needed shortly after.