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