1 /*
2  * Copyright (C) 2016 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 android.webkit;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 /**
23  * Manages Service Workers used by WebView.
24  *
25  * <p>Example usage:
26  * <pre class="prettyprint">
27  * ServiceWorkerController swController = ServiceWorkerController.getInstance();
28  * swController.setServiceWorkerClient(new ServiceWorkerClient() {
29  *   {@literal @}Override
30  *   public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
31  *     // Capture request here and generate response or allow pass-through
32  *     // by returning null.
33  *     return null;
34  *   }
35  * });
36  * </pre>
37  */
38 public abstract class ServiceWorkerController {
39 
40     /**
41      * @deprecated This class should not be constructed by applications, use {@link #getInstance()}
42      * instead to fetch the singleton instance.
43      */
44     // TODO(ntfschr): mark this as @SystemApi after a year.
45     @Deprecated
ServiceWorkerController()46     public ServiceWorkerController() {}
47 
48     /**
49      * Returns the default ServiceWorkerController instance. At present there is
50      * only one ServiceWorkerController instance for all WebView instances,
51      * however this restriction may be relaxed in the future.
52      *
53      * @return the default ServiceWorkerController instance
54      */
55      @NonNull
getInstance()56      public static ServiceWorkerController getInstance() {
57          return WebViewFactory.getProvider().getServiceWorkerController();
58      }
59 
60     /**
61      * Gets the settings for all service workers.
62      *
63      * @return the current ServiceWorkerWebSettings
64      */
65     @NonNull
getServiceWorkerWebSettings()66     public abstract ServiceWorkerWebSettings getServiceWorkerWebSettings();
67 
68     /**
69      * Sets the client to capture service worker related callbacks.
70      *
71      * A {@link ServiceWorkerClient} should be set before any service workers are
72      * active, e.g. a safe place is before any WebView instances are created or
73      * pages loaded.
74      */
setServiceWorkerClient(@ullable ServiceWorkerClient client)75     public abstract void setServiceWorkerClient(@Nullable ServiceWorkerClient client);
76 }
77 
78