1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.webkit; 18 19 import android.webkit.WebSettings; 20 import android.webkit.WebView; 21 22 import androidx.annotation.AnyThread; 23 import androidx.annotation.IntDef; 24 import androidx.annotation.RequiresFeature; 25 import androidx.annotation.RestrictTo; 26 27 import org.jspecify.annotations.NonNull; 28 29 import java.lang.annotation.Retention; 30 import java.lang.annotation.RetentionPolicy; 31 import java.util.Set; 32 33 /** 34 * Manages settings state for all Service Workers. These settings are not tied to 35 * the lifetime of any WebView because service workers can outlive WebView instances. 36 * The settings are similar to {@link WebSettings} but only settings relevant to 37 * Service Workers are supported. 38 */ 39 @AnyThread 40 public abstract class ServiceWorkerWebSettingsCompat { 41 /** 42 */ 43 @RestrictTo(RestrictTo.Scope.LIBRARY) ServiceWorkerWebSettingsCompat()44 public ServiceWorkerWebSettingsCompat() {} 45 46 @RestrictTo(RestrictTo.Scope.LIBRARY) 47 @IntDef(value = { 48 WebSettings.LOAD_DEFAULT, 49 WebSettings.LOAD_CACHE_ELSE_NETWORK, 50 WebSettings.LOAD_NO_CACHE, 51 WebSettings.LOAD_CACHE_ONLY 52 }) 53 @Retention(RetentionPolicy.SOURCE) 54 public @interface CacheMode {} 55 56 /** 57 * Overrides the way the cache is used. 58 * 59 * <p> 60 * This method should only be called if 61 * {@link WebViewFeature#isFeatureSupported(String)} 62 * returns true for {@link WebViewFeature#SERVICE_WORKER_CACHE_MODE}. 63 * 64 * @param mode the mode to use. One of {@link WebSettings#LOAD_DEFAULT}, 65 * {@link WebSettings#LOAD_CACHE_ELSE_NETWORK}, {@link WebSettings#LOAD_NO_CACHE} 66 * or {@link WebSettings#LOAD_CACHE_ONLY}. The default value is 67 * {@link WebSettings#LOAD_DEFAULT}. 68 * @see WebSettings#setCacheMode 69 * @see #getCacheMode 70 */ 71 @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CACHE_MODE, 72 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") setCacheMode(@acheMode int mode)73 public abstract void setCacheMode(@CacheMode int mode); 74 75 /** 76 * Gets the current setting for overriding the cache mode. 77 * 78 * <p> 79 * This method should only be called if 80 * {@link WebViewFeature#isFeatureSupported(String)} 81 * returns true for {@link WebViewFeature#SERVICE_WORKER_CACHE_MODE}. 82 * 83 * @return the current setting for overriding the cache mode 84 * @see #setCacheMode 85 */ 86 @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CACHE_MODE, 87 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") getCacheMode()88 public abstract @CacheMode int getCacheMode(); 89 90 /** 91 * Enables or disables content URL access from Service Workers. 92 * 93 * <p> 94 * This method should only be called if 95 * {@link WebViewFeature#isFeatureSupported(String)} 96 * returns true for {@link WebViewFeature#SERVICE_WORKER_CONTENT_ACCESS}. 97 * 98 * @see WebSettings#setAllowContentAccess 99 * @see #getAllowContentAccess 100 */ 101 @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, 102 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") setAllowContentAccess(boolean allow)103 public abstract void setAllowContentAccess(boolean allow); 104 105 /** 106 * Gets whether Service Workers support content URL access. 107 * 108 * <p> 109 * This method should only be called if 110 * {@link WebViewFeature#isFeatureSupported(String)} 111 * returns true for {@link WebViewFeature#SERVICE_WORKER_CONTENT_ACCESS}. 112 * 113 * @see #setAllowContentAccess 114 */ 115 @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, 116 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") getAllowContentAccess()117 public abstract boolean getAllowContentAccess(); 118 119 /** 120 * 121 * Enables or disables file access within Service Workers. 122 * 123 * <p> 124 * This method should only be called if 125 * {@link WebViewFeature#isFeatureSupported(String)} 126 * returns true for {@link WebViewFeature#SERVICE_WORKER_FILE_ACCESS}. 127 * 128 * @see WebSettings#setAllowFileAccess 129 * @see #getAllowContentAccess 130 */ 131 @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS, 132 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") setAllowFileAccess(boolean allow)133 public abstract void setAllowFileAccess(boolean allow); 134 135 /** 136 * Gets whether Service Workers support file access. 137 * 138 * <p> 139 * This method should only be called if 140 * {@link WebViewFeature#isFeatureSupported(String)} 141 * returns true for {@link WebViewFeature#SERVICE_WORKER_FILE_ACCESS}. 142 * 143 * @see #setAllowFileAccess 144 */ 145 @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS, 146 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") getAllowFileAccess()147 public abstract boolean getAllowFileAccess(); 148 149 /** 150 * Sets whether Service Workers should not load resources from the network. 151 * 152 * <p> 153 * This method should only be called if 154 * {@link WebViewFeature#isFeatureSupported(String)} 155 * returns true for {@link WebViewFeature#SERVICE_WORKER_BLOCK_NETWORK_LOADS}. 156 * 157 * @param flag {@code true} means block network loads by the Service Workers 158 * @see WebSettings#setBlockNetworkLoads 159 * @see #getBlockNetworkLoads 160 */ 161 @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, 162 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") setBlockNetworkLoads(boolean flag)163 public abstract void setBlockNetworkLoads(boolean flag); 164 165 /** 166 * Gets whether Service Workers are prohibited from loading any resources from the network. 167 * 168 * <p> 169 * This method should only be called if 170 * {@link WebViewFeature#isFeatureSupported(String)} 171 * returns true for {@link WebViewFeature#SERVICE_WORKER_BLOCK_NETWORK_LOADS}. 172 * 173 * @return {@code true} if the Service Workers are not allowed to load any resources from the 174 * network 175 * @see #setBlockNetworkLoads 176 */ 177 @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, 178 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") getBlockNetworkLoads()179 public abstract boolean getBlockNetworkLoads(); 180 181 182 /** 183 * Get the currently configured allow-list of origins, which is guaranteed to receive the 184 * {@code X-Requested-With} HTTP header on requests from service workers. 185 * <p> 186 * Any origin <em>not</em> on this allow-list may not receive the header, depending on the 187 * current installed WebView provider. 188 * <p> 189 * The format of the strings in the allow-list follows the origin rules of 190 * {@link WebViewCompat#addWebMessageListener(WebView, String, Set, WebViewCompat.WebMessageListener)}. 191 * 192 * @return The configured set of allow-listed origins. 193 * @see #setRequestedWithHeaderOriginAllowList(Set) 194 * @see WebSettingsCompat#getRequestedWithHeaderOriginAllowList(WebSettings) 195 */ 196 @RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, 197 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") getRequestedWithHeaderOriginAllowList()198 public abstract @NonNull Set<String> getRequestedWithHeaderOriginAllowList(); 199 200 /** 201 * Set an allow-list of origins to receive the {@code X-Requested-With} HTTP header from 202 * service workers. 203 * <p> 204 * Historically, this header was sent on all requests from WebView, containing the 205 * app package name of the embedding app. Depending on the version of installed WebView, this 206 * may no longer be the case, as the header was deprecated in late 2022, and its use 207 * discontinued. 208 * <p> 209 * Apps can use this method to restore the legacy behavior for servers that still rely on 210 * the deprecated header, but it should not be used to identify the WebView to first-party 211 * servers under the control of the app developer. 212 * <p> 213 * The format of the strings in the allow-list follows the origin rules of 214 * {@link WebViewCompat#addWebMessageListener(WebView, String, Set, WebViewCompat.WebMessageListener)}. 215 * 216 * @param allowList Set of origins to allow-list. 217 * @see WebSettingsCompat#setRequestedWithHeaderOriginAllowList(WebSettings, Set) 218 * @throws IllegalArgumentException if the allow-list contains a malformed origin. 219 */ 220 @RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, 221 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") setRequestedWithHeaderOriginAllowList(@onNull Set<String> allowList)222 public abstract void setRequestedWithHeaderOriginAllowList(@NonNull Set<String> allowList); 223 } 224