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")Gets whether Service Workers support content URL access. |
abstract boolean |
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Gets whether Service Workers support file access. |
abstract boolean |
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")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")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")This method is deprecated. The origin trial to disable the X-Requested-With feature has ended, so this API now just returns an empty set. |
abstract boolean |
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Returns whether cookie access during request intercept is enabled. |
abstract void |
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Enables or disables content URL access from Service Workers. |
abstract void |
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Enables or disables file access within Service Workers. |
abstract void |
@RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")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")Overrides the way the cache is used. |
abstract void |
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")This method controls if the relevant |
abstract void |
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")This method is deprecated. The origin trial to disable the X-Requested-With feature has ended, so this API no longer does anything. |
Public methods
getAllowContentAccess
@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.
| See also | |
|---|---|
setAllowContentAccess |
getAllowFileAccess
@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.
| See also | |
|---|---|
setAllowFileAccess |
getBlockNetworkLoads
@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 |
|
| See also | |
|---|---|
setBlockNetworkLoads |
getCacheMode
@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
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract @NonNull Set<String>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.
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.
isIncludeCookiesOnShouldInterceptRequestEnabled
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract boolean isIncludeCookiesOnShouldInterceptRequestEnabled()
Returns whether cookie access during request intercept is enabled.
This method should only be called if isFeatureSupported returns true for COOKIE_INTERCEPT.
setAllowContentAccess
@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
@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.
| See also | |
|---|---|
setAllowFileAccess |
|
getAllowContentAccess |
setBlockNetworkLoads
@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 |
|
| See also | |
|---|---|
setBlockNetworkLoads |
|
getBlockNetworkLoads |
setCacheMode
@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 |
| See also | |
|---|---|
setCacheMode |
|
getCacheMode |
setIncludeCookiesOnShouldInterceptRequestEnabled
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract void setIncludeCookiesOnShouldInterceptRequestEnabled(boolean enabled)
This method controls if the relevant Cookie header will be added to the WebResourceRequest object passed to shouldInterceptRequest. It also enables the use of setCookies, which will otherwise be ignored.
Prefer using this method over calling CookieManager as part of intercepting requests if it is necessary to access cookies, as this approach will provide the correct set of cookies for the request.
This method should only be called if isFeatureSupported returns true for COOKIE_INTERCEPT.
| Parameters | |
|---|---|
boolean enabled |
Whether cookie access during request intercept should be enabled. |
setRequestedWithHeaderOriginAllowList
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public abstract voidsetRequestedWithHeaderOriginAllowList(@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.
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if the allow-list contains a malformed origin. |