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 androidx.annotation.NonNull; 20 import androidx.annotation.Nullable; 21 import androidx.annotation.RequiresFeature; 22 import androidx.annotation.RestrictTo; 23 import androidx.webkit.internal.ServiceWorkerControllerImpl; 24 25 /** 26 * Manages Service Workers used by WebView. 27 * 28 * <p>Example usage: 29 * <pre class="prettyprint"> 30 * ServiceWorkerControllerCompat swController = ServiceWorkerControllerCompat.getInstance(); 31 * swController.setServiceWorkerClient(new ServiceWorkerClientCompat() { 32 * {@literal @}Override 33 * public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) { 34 * // Capture request here and generate response or allow pass-through 35 * // by returning null. 36 * return null; 37 * } 38 * }); 39 * </pre> 40 */ 41 public abstract class ServiceWorkerControllerCompat { 42 /** 43 * 44 * @hide Don't allow apps to sub-class this class. 45 */ 46 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) ServiceWorkerControllerCompat()47 public ServiceWorkerControllerCompat() {} 48 49 /** 50 * Returns the default ServiceWorkerController instance. At present there is 51 * only one ServiceWorkerController instance for all WebView instances, 52 * however this restriction may be relaxed in the future. 53 * 54 * @return the default ServiceWorkerController instance 55 */ 56 @NonNull 57 @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BASIC_USAGE, 58 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") getInstance()59 public static ServiceWorkerControllerCompat getInstance() { 60 return LAZY_HOLDER.INSTANCE; 61 } 62 63 private static class LAZY_HOLDER { 64 static final ServiceWorkerControllerCompat INSTANCE = new ServiceWorkerControllerImpl(); 65 } 66 67 /** 68 * 69 * Gets the settings for all service workers. 70 * 71 * @return the current {@link ServiceWorkerWebSettingsCompat} 72 * 73 */ 74 @NonNull getServiceWorkerWebSettings()75 public abstract ServiceWorkerWebSettingsCompat getServiceWorkerWebSettings(); 76 77 /** 78 * 79 * Sets the client to capture service worker related callbacks. 80 * 81 * A {@link ServiceWorkerClientCompat} should be set before any service workers are 82 * active, e.g. a safe place is before any WebView instances are created or 83 * pages loaded. 84 * 85 */ setServiceWorkerClient(@ullable ServiceWorkerClientCompat client)86 public abstract void setServiceWorkerClient(@Nullable ServiceWorkerClientCompat client); 87 } 88