PrefetchCache


@Profile.ExperimentalUrlPrefetch
class PrefetchCache


PrefetchCache manages the configuration of the prefetch cache for a Profile.

It allows applications to tune the behavior of prefetch cache by setting limits such as the maximum number of prefetches and the time-to-live for prefetched responses. These configurations are applied to all WebViews associated with the profile.

Use getPrefetchCache to obtain the PrefetchCache instance for a specific profile.

Summary

Public functions

Unit
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
clearMaxPrefetches()

Resets the maximum number of prefetches for the current browsing session to system default.

Unit
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
clearPrefetchTtl()

Resets the maximum prefetch Time-to-Live (TTL) in seconds for the current browsing session to the system default.

Int
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
getMaxPrefetches()

Returns maximum prefetches set for this Profile.

Int
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
getPrefetchTtlSeconds()

Returns Prefetch TTL in Seconds set for this Profile.

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

Starts a URL prefetch request.

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

Starts a URL prefetch request.

Unit
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
setMaxPrefetches(maxPrefetches: @IntRange(from = 1) Int)

Sets the maximum number of prefetches for the current browsing session.

Unit
@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
setPrefetchTtlSeconds(prefetchTtlSeconds: @IntRange(from = 1) Int)

Sets the maximum prefetch Time-to-Live (TTL) in seconds for the current browsing session.

Public functions

clearMaxPrefetches

@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun clearMaxPrefetches(): Unit

Resets the maximum number of prefetches for the current browsing session to system default.

This configuration will be applied to any prefetch requests made after they are set; they will not be applied to in-flight requests.

This configuration will be applied to WebViews that are associated with the Profile that owns this PrefetchCache.

This method should only be called if isFeatureSupported returns true for PREFETCH_CACHE_V1.

Throws
java.lang.UnsupportedOperationException

if the PREFETCH_CACHE_V1 feature is not supported.

clearPrefetchTtl

@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun clearPrefetchTtl(): Unit

Resets the maximum prefetch Time-to-Live (TTL) in seconds for the current browsing session to the system default.

This configuration will be applied to any prefetch requests made after they are set; they will not be applied to in-flight requests.

This configuration will be applied to WebViews that are associated with the Profile that owns this PrefetchCache.

This method should only be called if isFeatureSupported returns true for PREFETCH_CACHE_V1.

Throws
java.lang.UnsupportedOperationException

if the PREFETCH_CACHE_V1 feature is not supported.

getMaxPrefetches

@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun getMaxPrefetches(): Int

Returns maximum prefetches set for this Profile.

This method should only be called if isFeatureSupported returns true for PREFETCH_CACHE_V1.

Throws
java.lang.UnsupportedOperationException

if the PREFETCH_CACHE_V1 feature is not supported.

getPrefetchTtlSeconds

@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun getPrefetchTtlSeconds(): Int

Returns Prefetch TTL in Seconds set for this Profile.

This method should only be called if isFeatureSupported returns true for PREFETCH_CACHE_V1.

Throws
java.lang.UnsupportedOperationException

if the PREFETCH_CACHE_V1 feature is not supported.

prefetchUrlAsync

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

Starts a URL prefetch request.

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.

This method should only be called if isFeatureSupported returns true for PROFILE_URL_PREFETCH.

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. If null, the callback will be executed on the main thread.

outcomeReceiver: WebViewOutcomeReceiver<Void?, PrefetchException!>

callbacks for reporting result back to application.

Throws
java.lang.IllegalArgumentException

if the url or callback is null.

java.lang.UnsupportedOperationException

if the PROFILE_URL_PREFETCH feature is not supported.

prefetchUrlAsync

@RequiresFeature(name = WebViewFeature.PROFILE_URL_PREFETCH, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@AnyThread
@Profile.ExperimentalUrlPrefetch
fun prefetchUrlAsync(
    url: String,
    cancellationSignal: CancellationSignal?,
    callbackExecutor: Executor?,
    prefetchParameters: PrefetchParameters,
    outcomeReceiver: WebViewOutcomeReceiver<Void?, PrefetchException!>
): Unit

Starts a URL prefetch request.

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.

This method should only be called if isFeatureSupported returns true for PROFILE_URL_PREFETCH.

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. If null, the callback will be executed on the main thread.

prefetchParameters: PrefetchParameters

parameters to customize the prefetch request.

outcomeReceiver: WebViewOutcomeReceiver<Void?, PrefetchException!>

callbacks for reporting result back to application.

Throws
java.lang.IllegalArgumentException

if the url or callback is null.

java.lang.UnsupportedOperationException

if the PROFILE_URL_PREFETCH feature is not supported.

setMaxPrefetches

@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun setMaxPrefetches(maxPrefetches: @IntRange(from = 1) Int): Unit

Sets the maximum number of prefetches for the current browsing session.

This configuration will be applied to any prefetch requests made after they are set; they will not be applied to in-flight requests.

This configuration will be applied to WebViews that are associated with the Profile that owns this PrefetchCache.

This method should only be called if isFeatureSupported returns true for PREFETCH_CACHE_V1.

Parameters
maxPrefetches: @IntRange(from = 1) Int

the maximum number of prefetches to allow.

Throws
java.lang.UnsupportedOperationException

if the PREFETCH_CACHE_V1 feature is not supported.

setPrefetchTtlSeconds

@RequiresFeature(name = WebViewFeature.PREFETCH_CACHE_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
@Profile.ExperimentalUrlPrefetch
fun setPrefetchTtlSeconds(prefetchTtlSeconds: @IntRange(from = 1) Int): Unit

Sets the maximum prefetch Time-to-Live (TTL) in seconds for the current browsing 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 WebViews that are associated with the Profile that owns this PrefetchCache.

This method should only be called if isFeatureSupported returns true for PREFETCH_CACHE_V1.

Parameters
prefetchTtlSeconds: @IntRange(from = 1) Int

the TTL in seconds.

Throws
java.lang.UnsupportedOperationException

if the PREFETCH_CACHE_V1 feature is not supported.