ServiceWorkerWebSettingsCompat


@AnyThread
public abstract class ServiceWorkerWebSettingsCompat


Manages settings state for all Service Workers. These settings are not tied to the lifetime of any WebView because service workers can outlive WebView instances. The settings are similar to WebSettings but only settings relevant to Service Workers are supported.

Summary

Public methods

abstract boolean
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getAllowContentAccess()

Gets whether Service Workers support content URL access.

abstract boolean
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getAllowFileAccess()

Gets whether Service Workers support file access.

abstract boolean
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getBlockNetworkLoads()

Gets whether Service Workers are prohibited from loading any resources from the network.

abstract int
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CACHE_MODE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getCacheMode()

Gets the current setting for overriding the cache mode.

abstract @NonNull Set<String>
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getRequestedWithHeaderOriginAllowList()

Get the currently configured allow-list of origins, which is guaranteed to receive the X-Requested-With HTTP header on requests from service workers.

abstract void
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAllowContentAccess(boolean allow)

Enables or disables content URL access from Service Workers.

abstract void
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAllowFileAccess(boolean allow)

Enables or disables file access within Service Workers.

abstract void
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setBlockNetworkLoads(boolean flag)

Sets whether Service Workers should not load resources from the network.

abstract void
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CACHE_MODE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setCacheMode(int mode)

Overrides the way the cache is used.

abstract void
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setRequestedWithHeaderOriginAllowList(@NonNull Set<String> allowList)

Set an allow-list of origins to receive the X-Requested-With HTTP header from service workers.

Public methods

getAllowContentAccess

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract boolean getAllowContentAccess()

Gets whether Service Workers support content URL access.

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

getAllowFileAccess

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract boolean getAllowFileAccess()

Gets whether Service Workers support file access.

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

getBlockNetworkLoads

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract boolean getBlockNetworkLoads()

Gets whether Service Workers are prohibited from loading any resources from the network.

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

Returns
boolean

true if the Service Workers are not allowed to load any resources from the network

getCacheMode

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CACHE_MODE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract int getCacheMode()

Gets the current setting for overriding the cache mode.

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

Returns
int

the current setting for overriding the cache mode

See also
setCacheMode

getRequestedWithHeaderOriginAllowList

Added in 1.6.0
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract @NonNull Set<StringgetRequestedWithHeaderOriginAllowList()

Get the currently configured allow-list of origins, which is guaranteed to receive the X-Requested-With HTTP header on requests from service workers.

Any origin not on this allow-list may not receive the header, depending on the current installed WebView provider.

The format of the strings in the allow-list follows the origin rules of addWebMessageListener.

Returns
@NonNull Set<String>

The configured set of allow-listed origins.

setAllowContentAccess

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void setAllowContentAccess(boolean allow)

Enables or disables content URL access from Service Workers.

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

setAllowFileAccess

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void setAllowFileAccess(boolean allow)

Enables or disables file access within Service Workers.

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

setBlockNetworkLoads

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void setBlockNetworkLoads(boolean flag)

Sets whether Service Workers should not load resources from the network.

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

Parameters
boolean flag

true means block network loads by the Service Workers

setCacheMode

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CACHE_MODE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void setCacheMode(int mode)

Overrides the way the cache is used.

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

Parameters
int mode

the mode to use. One of LOAD_DEFAULT, LOAD_CACHE_ELSE_NETWORK, LOAD_NO_CACHE or LOAD_CACHE_ONLY. The default value is LOAD_DEFAULT.

setRequestedWithHeaderOriginAllowList

Added in 1.6.0
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void setRequestedWithHeaderOriginAllowList(@NonNull Set<String> allowList)

Set an allow-list of origins to receive the X-Requested-With HTTP header from service workers.

Historically, this header was sent on all requests from WebView, containing the app package name of the embedding app. Depending on the version of installed WebView, this may no longer be the case, as the header was deprecated in late 2022, and its use discontinued.

Apps can use this method to restore the legacy behavior for servers that still rely on the deprecated header, but it should not be used to identify the WebView to first-party servers under the control of the app developer.

The format of the strings in the allow-list follows the origin rules of addWebMessageListener.

Parameters
@NonNull Set<String> allowList

Set of origins to allow-list.

Throws
java.lang.IllegalArgumentException

if the allow-list contains a malformed origin.