Home | History | Annotate | Download | only in internal
      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.annotation.SuppressLint;
     20 import android.webkit.ServiceWorkerController;
     21 
     22 import androidx.annotation.RequiresApi;
     23 import androidx.webkit.ServiceWorkerClientCompat;
     24 import androidx.webkit.ServiceWorkerControllerCompat;
     25 import androidx.webkit.ServiceWorkerWebSettingsCompat;
     26 
     27 import org.chromium.support_lib_boundary.ServiceWorkerControllerBoundaryInterface;
     28 import org.chromium.support_lib_boundary.util.BoundaryInterfaceReflectionUtil;
     29 
     30 /**
     31  * Implementation of {@link ServiceWorkerControllerCompat}.
     32  * This class uses either the framework, the WebView APK, or both, to implement
     33  * {@link ServiceWorkerControllerCompat} functionality.
     34  */
     35 public class ServiceWorkerControllerImpl extends ServiceWorkerControllerCompat {
     36     private ServiceWorkerController mFrameworksImpl;
     37     private ServiceWorkerControllerBoundaryInterface mBoundaryInterface;
     38     private final ServiceWorkerWebSettingsCompat mWebSettings;
     39 
     40     @SuppressLint("NewApi")
     41     public ServiceWorkerControllerImpl() {
     42         final WebViewFeatureInternal feature = WebViewFeatureInternal.SERVICE_WORKER_BASIC_USAGE;
     43         if (feature.isSupportedByFramework()) {
     44             mFrameworksImpl = ServiceWorkerController.getInstance();
     45             // The current WebView APK might not be compatible with the support library, so set the
     46             // boundary interface to null for now.
     47             mBoundaryInterface = null;
     48             mWebSettings = new ServiceWorkerWebSettingsImpl(
     49                     mFrameworksImpl.getServiceWorkerWebSettings());
     50         } else if (feature.isSupportedByWebView()) {
     51             mFrameworksImpl = null;
     52             mBoundaryInterface = WebViewGlueCommunicator.getFactory().getServiceWorkerController();
     53             mWebSettings = new ServiceWorkerWebSettingsImpl(
     54                     mBoundaryInterface.getServiceWorkerWebSettings());
     55         } else {
     56             throw WebViewFeatureInternal.getUnsupportedOperationException();
     57         }
     58     }
     59 
     60     @RequiresApi(24)
     61     private ServiceWorkerController getFrameworksImpl() {
     62         if (mFrameworksImpl == null) {
     63             mFrameworksImpl = ServiceWorkerController.getInstance();
     64         }
     65         return mFrameworksImpl;
     66     }
     67 
     68     private ServiceWorkerControllerBoundaryInterface getBoundaryInterface() {
     69         if (mBoundaryInterface == null) {
     70             mBoundaryInterface = WebViewGlueCommunicator.getFactory().getServiceWorkerController();
     71         }
     72         return mBoundaryInterface;
     73     }
     74 
     75     @Override
     76     public ServiceWorkerWebSettingsCompat getServiceWorkerWebSettings() {
     77         return mWebSettings;
     78     }
     79 
     80     @SuppressLint("NewApi")
     81     @Override
     82     public void setServiceWorkerClient(ServiceWorkerClientCompat client)  {
     83         final WebViewFeatureInternal feature = WebViewFeatureInternal.SERVICE_WORKER_BASIC_USAGE;
     84         if (feature.isSupportedByFramework()) {
     85             getFrameworksImpl().setServiceWorkerClient(new FrameworkServiceWorkerClient(client));
     86         } else if (feature.isSupportedByWebView()) {
     87             getBoundaryInterface().setServiceWorkerClient(
     88                     BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(
     89                             new ServiceWorkerClientAdapter(client)));
     90         } else {
     91             throw WebViewFeatureInternal.getUnsupportedOperationException();
     92         }
     93     }
     94 }
     95