WebViewFeatureInternal.java revision 826eeefd318cd30068ec731619ade6f5da7e7c4d
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.WebSettings; 23 24import androidx.webkit.WebViewCompat; 25import androidx.webkit.WebViewFeature; 26import androidx.webkit.WebViewFeature.WebViewSupportFeature; 27 28import java.util.List; 29 30/** 31 * Enum representing a WebView feature, this provides functionality for determining whether a 32 * feature is supported by the current framework and/or WebView APK. 33 */ 34public enum WebViewFeatureInternal { 35 /** 36 * This feature covers 37 * {@link androidx.webkit.WebViewCompat#postVisualStateCallback(android.webkit.WebView, long, 38 * androidx.webkit.WebViewCompat.VisualStateCallback)}. 39 */ 40 VISUAL_STATE_CALLBACK_FEATURE(WebViewFeature.VISUAL_STATE_CALLBACK, Build.VERSION_CODES.M), 41 42 /** 43 * This feature covers 44 * {@link androidx.webkit.WebSettingsCompat#getOffscreenPreRaster(WebSettings)}, and 45 * {@link androidx.webkit.WebSettingsCompat#setOffscreenPreRaster(WebSettings, boolean)}. 46 */ 47 OFF_SCREEN_PRERASTER(WebViewFeature.OFF_SCREEN_PRERASTER, Build.VERSION_CODES.M), 48 49 /** 50 * This feature covers 51 * {@link androidx.webkit.WebSettingsCompat#getSafeBrowsingEnabled(WebSettings)}, and 52 * {@link androidx.webkit.WebSettingsCompat#setSafeBrowsingEnabled(WebSettings, boolean)}. 53 */ 54 SAFE_BROWSING_ENABLE(WebViewFeature.SAFE_BROWSING_ENABLE, Build.VERSION_CODES.O), 55 56 /** 57 * This feature covers 58 * {@link androidx.webkit.WebSettingsCompat#getDisabledActionModeMenuItems(WebSettings)}, and 59 * {@link androidx.webkit.WebSettingsCompat#setDisabledActionModeMenuItems(WebSettings, int)}. 60 */ 61 DISABLED_ACTION_MODE_MENU_ITEMS(WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, 62 Build.VERSION_CODES.N), 63 64 /** 65 * This feature covers 66 * {@link androidx.webkit.WebViewCompat#startSafeBrowsing(Context, ValueCallback)}. 67 */ 68 START_SAFE_BROWSING(WebViewFeature.START_SAFE_BROWSING, Build.VERSION_CODES.O_MR1), 69 70 /** 71 * This feature covers 72 * {@link androidx.webkit.WebViewCompat#setSafeBrowsingWhitelist(List, ValueCallback)}. 73 */ 74 SAFE_BROWSING_WHITELIST(WebViewFeature.SAFE_BROWSING_WHITELIST, Build.VERSION_CODES.O_MR1), 75 76 /** 77 * This feature covers 78 * {@link WebViewCompat#getSafeBrowsingPrivacyPolicyUrl()}. 79 */ 80 SAFE_BROWSING_PRIVACY_POLICY_URL(WebViewFeature.SAFE_BROWSING_PRIVACY_POLICY_URL, 81 Build.VERSION_CODES.O_MR1), 82 83 /** 84 * This feature covers 85 * {@link androidx.webkit.ServiceWorkerControllerCompat#getInstance()}. 86 */ 87 SERVICE_WORKER_BASIC_USAGE(WebViewFeature.SERVICE_WORKER_BASIC_USAGE, Build.VERSION_CODES.N), 88 89 /** 90 * This feature covers 91 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#getCacheMode()}, and 92 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#setCacheMode(int)}. 93 */ 94 SERVICE_WORKER_CACHE_MODE(WebViewFeature.SERVICE_WORKER_CACHE_MODE, Build.VERSION_CODES.N), 95 96 /** 97 * This feature covers 98 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#getAllowContentAccess()}, and 99 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#setAllowContentAccess(boolean)}. 100 */ 101 SERVICE_WORKER_CONTENT_ACCESS(WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS, 102 Build.VERSION_CODES.N), 103 104 /** 105 * This feature covers 106 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#getAllowFileAccess()}, and 107 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#setAllowFileAccess(boolean)}. 108 */ 109 SERVICE_WORKER_FILE_ACCESS(WebViewFeature.SERVICE_WORKER_FILE_ACCESS, Build.VERSION_CODES.N), 110 111 /** 112 * This feature covers 113 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#getBlockNetworkLoads()}, and 114 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat#setBlockNetworkLoads(boolean)}. 115 */ 116 SERVICE_WORKER_BLOCK_NETWORK_LOADS(WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS, 117 Build.VERSION_CODES.N); 118 119 private final String mFeatureValue; 120 private final int mOsVersion; 121 122 WebViewFeatureInternal(@WebViewSupportFeature String featureValue, int osVersion) { 123 mFeatureValue = featureValue; 124 mOsVersion = osVersion; 125 } 126 127 /** 128 * Return the {@link WebViewFeatureInternal} corresponding to {@param feature}. 129 */ 130 public static WebViewFeatureInternal getFeature(@WebViewSupportFeature String feature) { 131 for (WebViewFeatureInternal internalFeature : WebViewFeatureInternal.values()) { 132 if (internalFeature.mFeatureValue.equals(feature)) return internalFeature; 133 } 134 throw new RuntimeException("Unknown feature " + feature); 135 } 136 137 /** 138 * Return whether this {@link WebViewFeature} is supported by the framework of the current 139 * device. 140 */ 141 public boolean isSupportedByFramework() { 142 return Build.VERSION.SDK_INT >= mOsVersion; 143 } 144 145 /** 146 * Return whether this {@link WebViewFeature} is supported by the current WebView APK. 147 */ 148 public boolean isSupportedByWebView() { 149 String[] webviewFeatures = LAZY_HOLDER.WEBVIEW_APK_FEATURES; 150 for (String webviewFeature : webviewFeatures) { 151 if (webviewFeature.equals(mFeatureValue)) return true; 152 } 153 return false; 154 } 155 156 private static class LAZY_HOLDER { 157 static final String[] WEBVIEW_APK_FEATURES = 158 WebViewGlueCommunicator.getFactory().getWebViewFeatures(); 159 } 160 161 162 public static String[] getWebViewApkFeaturesForTesting() { 163 return LAZY_HOLDER.WEBVIEW_APK_FEATURES; 164 } 165 166 /** 167 * Utility method for throwing an exception explaining that the feature the app trying to use 168 * isn't supported. 169 */ 170 public static UnsupportedOperationException getUnsupportedOperationException() { 171 return new UnsupportedOperationException("This method is not supported by the current " 172 + "version of the framework and the current WebView APK"); 173 } 174} 175