ServiceWorkerWebSettingsCompat

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 functions

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 (Mutable)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 Unit
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAllowContentAccess(allow: Boolean)

Enables or disables content URL access from Service Workers.

abstract Unit
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAllowFileAccess(allow: Boolean)

Enables or disables file access within Service Workers.

abstract Unit
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setBlockNetworkLoads(flag: Boolean)

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

abstract Unit
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CACHE_MODE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setCacheMode(mode: Int)

Overrides the way the cache is used.

abstract Unit
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setRequestedWithHeaderOriginAllowList(allowList: (Mutable)Set<String!>)

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

Public functions

getAllowContentAccess

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

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")
abstract fun getAllowFileAccess(): Boolean

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")
abstract fun getBlockNetworkLoads(): Boolean

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")
abstract fun getCacheMode(): Int

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")
abstract fun getRequestedWithHeaderOriginAllowList(): (Mutable)Set<String!>

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
(Mutable)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")
abstract fun setAllowContentAccess(allow: Boolean): Unit

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")
abstract fun setAllowFileAccess(allow: Boolean): Unit

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")
abstract fun setBlockNetworkLoads(flag: Boolean): Unit

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

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")
abstract fun setCacheMode(mode: Int): Unit

Overrides the way the cache is used.

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

Parameters
mode: Int

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")
abstract fun setRequestedWithHeaderOriginAllowList(allowList: (Mutable)Set<String!>): Unit

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
allowList: (Mutable)Set<String!>

Set of origins to allow-list.

Throws
java.lang.IllegalArgumentException

if the allow-list contains a malformed origin.