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></p>
37  */
38 public abstract class ServiceWorkerController {
39 
40     /**
41      * Returns the default ServiceWorkerController instance. At present there is
42      * only one ServiceWorkerController instance for all WebView instances,
43      * however this restriction may be relaxed in the future.
44      *
45      * @return the default ServiceWorkerController instance
46      */
47      @NonNull
getInstance()48      public static ServiceWorkerController getInstance() {
49          return WebViewFactory.getProvider().getServiceWorkerController();
50      }
51 
52     /**
53      * Gets the settings for all service workers.
54      *
55      * @return the current ServiceWorkerWebSettings
56      */
57     @NonNull
getServiceWorkerWebSettings()58     public abstract ServiceWorkerWebSettings getServiceWorkerWebSettings();
59 
60     /**
61      * Sets the client to capture service worker related callbacks.
62      *
63      * A {@link ServiceWorkerClient} should be set before any service workers are
64      * active, e.g. a safe place is before any WebView instances are created or
65      * pages loaded.
66      */
setServiceWorkerClient(@ullable ServiceWorkerClient client)67     public abstract void setServiceWorkerClient(@Nullable ServiceWorkerClient client);
68 }
69 
70