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;
18
19import android.annotation.SuppressLint;
20import android.webkit.WebResourceRequest;
21
22import androidx.annotation.NonNull;
23import androidx.annotation.RequiresFeature;
24import androidx.webkit.internal.WebResourceRequestAdapter;
25import androidx.webkit.internal.WebViewFeatureInternal;
26import androidx.webkit.internal.WebViewGlueCommunicator;
27
28// TODO(gsennton) add a test for this class
29
30/**
31 * Compatibility version of {@link WebResourceRequest}.
32 */
33public class WebResourceRequestCompat {
34
35    // Developers should not be able to instantiate this class.
36    private WebResourceRequestCompat() {}
37
38    /**
39     * Gets whether the request was a result of a server-side redirect.
40     *
41     * @return whether the request was a result of a server-side redirect.
42     */
43    @SuppressLint("NewApi")
44    @RequiresFeature(name = WebViewFeature.WEB_RESOURCE_REQUEST_IS_REDIRECT,
45            enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
46    public static boolean isRedirect(@NonNull WebResourceRequest request) {
47        WebViewFeatureInternal feature = WebViewFeatureInternal.WEB_RESOURCE_REQUEST_IS_REDIRECT;
48        if (feature.isSupportedByFramework()) {
49            return request.isRedirect();
50        } else if (feature.isSupportedByWebView()) {
51            return getAdapter(request).isRedirect();
52        } else {
53            throw WebViewFeatureInternal.getUnsupportedOperationException();
54        }
55    }
56
57    private static WebResourceRequestAdapter getAdapter(WebResourceRequest request) {
58        return WebViewGlueCommunicator.getCompatConverter().convertWebResourceRequest(request);
59    }
60}
61