Home | History | Annotate | Download | only in webkit
      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.annotation.SuppressLint;
     20 import android.webkit.WebResourceRequest;
     21 
     22 import androidx.annotation.NonNull;
     23 import androidx.annotation.RequiresFeature;
     24 import androidx.webkit.internal.WebResourceRequestAdapter;
     25 import androidx.webkit.internal.WebViewFeatureInternal;
     26 import androidx.webkit.internal.WebViewGlueCommunicator;
     27 
     28 // TODO(gsennton) add a test for this class
     29 
     30 /**
     31  * Compatibility version of {@link WebResourceRequest}.
     32  */
     33 public 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