WebViewFeatureInternal.java revision db584b38b76c4b5fc9c267764807289ebb7df7a7
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 17package androidx.webkit.internal; 18 19import android.content.Context; 20import android.os.Build; 21import android.webkit.ValueCallback; 22import android.webkit.WebResourceRequest; 23import android.webkit.WebSettings; 24 25import androidx.webkit.ServiceWorkerClientCompat; 26import androidx.webkit.WebViewClientCompat; 27import androidx.webkit.WebViewCompat; 28import androidx.webkit.WebViewFeature; 29import androidx.webkit.WebViewFeature.WebViewSupportFeature; 30 31import java.util.List; 32 33/** 34 * Enum representing a WebView feature, this provides functionality for determining whether a 35 * feature is supported by the current framework and/or WebView APK. 36 */ 37public enum WebViewFeatureInternal { 38 /** 39 * This feature covers 40 * {@link androidx.webkit.WebViewCompat#postVisualStateCallback(android.webkit.WebView, long, 41 * androidx.webkit.WebViewCompat.VisualStateCallback)}, and 42 * {@link WebViewClientCompat#onPageCommitVisible(android.webkit.WebView, String)}. 43 */ 44 VISUAL_STATE_CALLBACK_FEATURE(WebViewFeature.VISUAL_STATE_CALLBACK, Build.VERSION_CODES.M), 45 46 /** 47 * This feature covers 48 * {@link androidx.webkit.WebSettingsCompat#getOffscreenPreRaster(WebSettings)}, and 49 * {@link androidx.webkit.WebSettingsCompat#setOffscreenPreRaster(WebSettings, boolean)}. 50 */ 51 OFF_SCREEN_PRERASTER(WebViewFeature.OFF_SCREEN_PRERASTER, Build.VERSION_CODES.M), 52 53 /** 54 * This feature covers 55 * {@link androidx.webkit.WebSettingsCompat#getSafeBrowsingEnabled(WebSettings)}, and 56 * {@link androidx.webkit.WebSettingsCompat#setSafeBrowsingEnabled(WebSettings, boolean)}. 57 */ 58 SAFE_BROWSING_ENABLE(WebViewFeature.SAFE_BROWSING_ENABLE, Build.VERSION_CODES.O), 59 60 /** 61 * This feature covers 62 * {@link androidx.webkit.WebSettingsCompat#getDisabledActionModeMenuItems(WebSettings)}, and 63 * {@link androidx.webkit.WebSettingsCompat#setDisabledActionModeMenuItems(WebSettings, int)}. 64 */ 65 DISABLED_ACTION_MODE_MENU_ITEMS(WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, 66 Build.VERSION_CODES.N), 67 68 /** 69 * This feature covers 70 * {@link androidx.webkit.WebViewCompat#startSafeBrowsing(Context, ValueCallback)}. 71 */ 72 START_SAFE_BROWSING(WebViewFeature.START_SAFE_BROWSING, Build.VERSION_CODES.O_MR1), 73 74 /** 75 * This feature covers 76 * {@link androidx.webkit.WebViewCompat#setSafeBrowsingWhitelist(List, ValueCallback)}. 77 */ 78 SAFE_BROWSING_WHITELIST(WebViewFeature.SAFE_BROWSING_WHITELIST, Build.VERSION_CODES.O_MR1), 79 80 /** 81 * This feature covers 82 * {@link WebViewCompat#getSafeBrowsingPrivacyPolicyUrl()}. 83 */ 84 SAFE_BROWSING_PRIVACY_POLICY_URL(WebViewFeature.SAFE_BROWSING_PRIVACY_POLICY_URL, 85 Build.VERSION_CODES.O_MR1), 86 87 /** 88 * This feature covers 89 * {@link androidx.webkit.ServiceWorkerControllerCompat#getInstance()}. 90 */ 91 SERVICE_WORKER_BASIC_USAGE(WebViewFeature.SERVICE_WORKER_BASIC_USAGE, Build.VERSION_CODES.N), 92 93 /** 94 * This feature covers 95 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#getCacheMode()}, and 96 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#setCacheMode(int)}. 97 */ 98 SERVICE_WORKER_CACHE_MODE(WebViewFeature.SERVICE_WORKER_CACHE_MODE, Build.VERSION_CODES.N), 99 100 /** 101 * This feature covers 102 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#getAllowContentAccess()}, and 103 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#setAllowContentAccess(boolean)}. 104 */ 105 SERVICE_WORKER_CONTENT_ACCESS(WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, 106 Build.VERSION_CODES.N), 107 108 /** 109 * This feature covers 110 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#getAllowFileAccess()}, and 111 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#setAllowFileAccess(boolean)}. 112 */ 113 SERVICE_WORKER_FILE_ACCESS(WebViewFeature.SERVICE_WORKER_FILE_ACCESS, Build.VERSION_CODES.N), 114 115 /** 116 * This feature covers 117 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#getBlockNetworkLoads()}, and 118 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#setBlockNetworkLoads(boolean)}. 119 */ 120 SERVICE_WORKER_BLOCK_NETWORK_LOADS(WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, 121 Build.VERSION_CODES.N), 122 123 /** 124 * This feature covers 125 * {@link ServiceWorkerClientCompat#shouldInterceptRequest(WebResourceRequest)}. 126 */ 127 SERVICE_WORKER_SHOULD_INTERCEPT_REQUEST(WebViewFeature.SERVICE_WORKER_SHOULD_INTERCEPT_REQUEST, 128 Build.VERSION_CODES.N), 129 130 /** 131 * This feature covers 132 * {@link WebViewClientCompat#onReceivedError(android.webkit.WebView, WebResourceRequest, 133 * WebResourceErrorCompat)}. 134 */ 135 RECEIVE_WEB_RESOURCE_ERROR(WebViewFeature.RECEIVE_WEB_RESOURCE_ERROR, Build.VERSION_CODES.M), 136 137 /** 138 * This feature covers 139 * {@link WebViewClientCompat#onReceivedHttpError(android.webkit.WebView, WebResourceRequest, 140 * WebResourceResponse)}. 141 */ 142 RECEIVE_HTTP_ERROR(WebViewFeature.RECEIVE_HTTP_ERROR, Build.VERSION_CODES.M), 143 144 /** 145 * This feature covers 146 * {@link WebViewClientCompat#shouldOverrideUrlLoading(android.webkit.WebView, 147 * WebResourceRequest)}. 148 */ 149 SHOULD_OVERRIDE_WITH_REDIRECTS(WebViewFeature.SHOULD_OVERRIDE_WITH_REDIRECTS, 150 Build.VERSION_CODES.N), 151 152 /** 153 * This feature covers 154 * WebViewClientCompat#onSafeBrowsingHit(android.webkit.WebView, 155 * WebResourceRequest, int, SafeBrowsingResponseCompat). 156 * 157 * TODO(ntfschr): turn this into a javadoc link once the method is implemented in 158 * http://ag/3858246. 159 */ 160 SAFE_BROWSING_HIT(WebViewFeature.SAFE_BROWSING_HIT, Build.VERSION_CODES.O_MR1); 161 162 private final String mFeatureValue; 163 private final int mOsVersion; 164 165 WebViewFeatureInternal(@WebViewSupportFeature String featureValue, int osVersion) { 166 mFeatureValue = featureValue; 167 mOsVersion = osVersion; 168 } 169 170 /** 171 * Return the {@link WebViewFeatureInternal} corresponding to {@param feature}. 172 */ 173 public static WebViewFeatureInternal getFeature(@WebViewSupportFeature String feature) { 174 for (WebViewFeatureInternal internalFeature : WebViewFeatureInternal.values()) { 175 if (internalFeature.mFeatureValue.equals(feature)) return internalFeature; 176 } 177 throw new RuntimeException("Unknown feature " + feature); 178 } 179 180 /** 181 * Return whether this {@link WebViewFeature} is supported by the framework of the current 182 * device. 183 */ 184 public boolean isSupportedByFramework() { 185 return Build.VERSION.SDK_INT >= mOsVersion; 186 } 187 188 /** 189 * Return whether this {@link WebViewFeature} is supported by the current WebView APK. 190 */ 191 public boolean isSupportedByWebView() { 192 String[] webviewFeatures = LAZY_HOLDER.WEBVIEW_APK_FEATURES; 193 for (String webviewFeature : webviewFeatures) { 194 if (webviewFeature.equals(mFeatureValue)) return true; 195 } 196 return false; 197 } 198 199 private static class LAZY_HOLDER { 200 static final String[] WEBVIEW_APK_FEATURES = 201 WebViewGlueCommunicator.getFactory().getWebViewFeatures(); 202 } 203 204 205 public static String[] getWebViewApkFeaturesForTesting() { 206 return LAZY_HOLDER.WEBVIEW_APK_FEATURES; 207 } 208 209 /** 210 * Utility method for throwing an exception explaining that the feature the app trying to use 211 * isn't supported. 212 */ 213 public static UnsupportedOperationException getUnsupportedOperationException() { 214 return new UnsupportedOperationException("This method is not supported by the current " 215 + "version of the framework and the current WebView APK"); 216 } 217} 218