blob: d7e0715a3770991d8c4681eafed4c7f0dcb4958f [file] [log] [blame]
Tim Volodine28c83562016-01-20 19:23:03 +00001/*
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
17package android.webkit;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21
22/**
23 * Manages Service Workers used by WebView.
Tim Volodineb90f5382016-04-29 12:44:41 +010024 *
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 * });
Nate Fischerf02f8462017-09-11 15:16:33 -070036 * </pre>
Tim Volodine28c83562016-01-20 19:23:03 +000037 */
38public abstract class ServiceWorkerController {
39
40 /**
Nate Fischer1e13fae2018-09-25 18:27:25 -070041 * @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
46 public ServiceWorkerController() {}
47
48 /**
Tim Volodine28c83562016-01-20 19:23:03 +000049 * 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 *
Tim Volodineb90f5382016-04-29 12:44:41 +010053 * @return the default ServiceWorkerController instance
Tim Volodine28c83562016-01-20 19:23:03 +000054 */
55 @NonNull
56 public static ServiceWorkerController getInstance() {
57 return WebViewFactory.getProvider().getServiceWorkerController();
58 }
59
60 /**
61 * Gets the settings for all service workers.
62 *
Tim Volodineb90f5382016-04-29 12:44:41 +010063 * @return the current ServiceWorkerWebSettings
Tim Volodine28c83562016-01-20 19:23:03 +000064 */
65 @NonNull
66 public abstract ServiceWorkerWebSettings getServiceWorkerWebSettings();
67
68 /**
69 * Sets the client to capture service worker related callbacks.
Tim Volodineb90f5382016-04-29 12:44:41 +010070 *
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.
Tim Volodine28c83562016-01-20 19:23:03 +000074 */
75 public abstract void setServiceWorkerClient(@Nullable ServiceWorkerClient client);
76}
77