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.internal; 18 19 import android.webkit.ServiceWorkerWebSettings; 20 import android.webkit.WebResourceError; 21 import android.webkit.WebResourceRequest; 22 import android.webkit.WebSettings; 23 24 import androidx.annotation.RequiresApi; 25 import androidx.webkit.WebResourceErrorCompat; 26 27 import org.chromium.support_lib_boundary.ServiceWorkerWebSettingsBoundaryInterface; 28 import org.chromium.support_lib_boundary.WebResourceRequestBoundaryInterface; 29 import org.chromium.support_lib_boundary.WebSettingsBoundaryInterface; 30 import org.chromium.support_lib_boundary.WebkitToCompatConverterBoundaryInterface; 31 import org.chromium.support_lib_boundary.util.BoundaryInterfaceReflectionUtil; 32 33 import java.lang.reflect.InvocationHandler; 34 35 /** 36 * A class providing functionality for converting android.webkit classes into support library 37 * classes. 38 */ 39 public class WebkitToCompatConverter { 40 private final WebkitToCompatConverterBoundaryInterface mImpl; 41 42 public WebkitToCompatConverter(WebkitToCompatConverterBoundaryInterface impl) { 43 mImpl = impl; 44 } 45 46 /** 47 * Return a WebSettingsAdapter linked to webSettings such that calls on either of those 48 * objects affect the other object. That WebSettingsAdapter can be used to implement 49 * {@link androidx.webkit.WebSettingsCompat}. 50 */ 51 public WebSettingsAdapter convertSettings(WebSettings webSettings) { 52 return new WebSettingsAdapter(BoundaryInterfaceReflectionUtil.castToSuppLibClass( 53 WebSettingsBoundaryInterface.class, mImpl.convertSettings(webSettings))); 54 } 55 56 /** 57 * Return a {@link WebResourceRequestAdapter} linked to the given {@link WebResourceRequest} so 58 * that calls on either of those objects affect the other object. 59 */ 60 public WebResourceRequestAdapter convertWebResourceRequest(WebResourceRequest request) { 61 return new WebResourceRequestAdapter(BoundaryInterfaceReflectionUtil.castToSuppLibClass( 62 WebResourceRequestBoundaryInterface.class, 63 mImpl.convertWebResourceRequest(request))); 64 } 65 66 /** 67 * Return a {@link ServiceWorkerWebSettingsBoundaryInterface} linked to the given 68 * {@link ServiceWorkerWebSettings} such that calls on either of those objects affect the other 69 * object. 70 */ 71 public InvocationHandler convertServiceWorkerSettings( 72 ServiceWorkerWebSettings settings) { 73 return mImpl.convertServiceWorkerSettings(settings); 74 } 75 76 /** 77 * Convert from an {@link InvocationHandler} representing an 78 * {@link androidx.webkit.ServiceWorkerWebSettingsCompat} into a 79 * {@link ServiceWorkerWebSettings}. 80 */ 81 @RequiresApi(24) 82 public ServiceWorkerWebSettings convertServiceWorkerSettings( 83 /* SupportLibServiceWorkerSettings */ InvocationHandler serviceWorkerSettings) { 84 return (ServiceWorkerWebSettings) mImpl.convertServiceWorkerSettings(serviceWorkerSettings); 85 } 86 87 /** 88 * Return a {@link InvocationHandler} linked to the given 89 * {@link WebResourceError}such that calls on either of those objects affect the other 90 * object. 91 */ 92 InvocationHandler convertWebResourceError(WebResourceError webResourceError) { 93 return mImpl.convertWebResourceError(webResourceError); 94 } 95 96 97 /** 98 * Convert from an {@link InvocationHandler} representing a {@link WebResourceErrorCompat} into 99 * a {@link WebResourceError}. 100 */ 101 @RequiresApi(23) 102 WebResourceError convertWebResourceError( 103 /* SupportLibWebResourceError */ InvocationHandler webResourceError) { 104 return (WebResourceError) mImpl.convertWebResourceError(webResourceError); 105 } 106 } 107