WebViewFeatureInternal.java revision 17fc27f19b672a56f01c5f20773fb1047876646d
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     * {@link WebViewClientCompat#onSafeBrowsingHit(android.webkit.WebView,
155     * WebResourceRequest, int, SafeBrowsingResponseCompat)}.
156     */
157    SAFE_BROWSING_HIT(WebViewFeature.SAFE_BROWSING_HIT, Build.VERSION_CODES.O_MR1);
158
159    private final String mFeatureValue;
160    private final int mOsVersion;
161
162    WebViewFeatureInternal(@WebViewSupportFeature String featureValue, int osVersion) {
163        mFeatureValue = featureValue;
164        mOsVersion = osVersion;
165    }
166
167    /**
168     * Return the {@link WebViewFeatureInternal} corresponding to {@param feature}.
169     */
170    public static WebViewFeatureInternal getFeature(@WebViewSupportFeature String feature) {
171        for (WebViewFeatureInternal internalFeature : WebViewFeatureInternal.values()) {
172            if (internalFeature.mFeatureValue.equals(feature)) return internalFeature;
173        }
174        throw new RuntimeException("Unknown feature " + feature);
175    }
176
177    /**
178     * Return whether this {@link WebViewFeature} is supported by the framework of the current
179     * device.
180     */
181    public boolean isSupportedByFramework() {
182        return Build.VERSION.SDK_INT >= mOsVersion;
183    }
184
185    /**
186     * Return whether this {@link WebViewFeature} is supported by the current WebView APK.
187     */
188    public boolean isSupportedByWebView() {
189        String[] webviewFeatures = LAZY_HOLDER.WEBVIEW_APK_FEATURES;
190        for (String webviewFeature : webviewFeatures) {
191            if (webviewFeature.equals(mFeatureValue)) return true;
192        }
193        return false;
194    }
195
196    private static class LAZY_HOLDER {
197        static final String[] WEBVIEW_APK_FEATURES =
198                WebViewGlueCommunicator.getFactory().getWebViewFeatures();
199    }
200
201
202    public static String[] getWebViewApkFeaturesForTesting() {
203        return LAZY_HOLDER.WEBVIEW_APK_FEATURES;
204    }
205
206    /**
207     * Utility method for throwing an exception explaining that the feature the app trying to use
208     * isn't supported.
209     */
210    public static UnsupportedOperationException getUnsupportedOperationException() {
211        return new UnsupportedOperationException("This method is not supported by the current "
212                + "version of the framework and the current WebView APK");
213    }
214}
215