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 android.webkit.WebSettings;
20 
21 import androidx.annotation.IntDef;
22 import androidx.annotation.RequiresFeature;
23 import androidx.annotation.RestrictTo;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * Manages settings state for all Service Workers. These settings are not tied to
30  * the lifetime of any WebView because service workers can outlive WebView instances.
31  * The settings are similar to {@link WebSettings} but only settings relevant to
32  * Service Workers are supported.
33  */
34 public abstract class ServiceWorkerWebSettingsCompat {
35     /**
36      * @hide Don't allow apps to sub-class this class.
37      */
38     @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
ServiceWorkerWebSettingsCompat()39     public ServiceWorkerWebSettingsCompat() {}
40 
41     /** @hide */
42     @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
43     @IntDef(value = {
44             WebSettings.LOAD_DEFAULT,
45             WebSettings.LOAD_CACHE_ELSE_NETWORK,
46             WebSettings.LOAD_NO_CACHE,
47             WebSettings.LOAD_CACHE_ONLY
48     })
49     @Retention(RetentionPolicy.SOURCE)
50     public @interface CacheMode {}
51 
52     /**
53      *
54      * Overrides the way the cache is used, see {@link WebSettings#setCacheMode}.
55      *
56      * @param mode the mode to use. One of {@link WebSettings#LOAD_DEFAULT},
57      * {@link WebSettings#LOAD_CACHE_ELSE_NETWORK}, {@link WebSettings#LOAD_NO_CACHE}
58      * or {@link WebSettings#LOAD_CACHE_ONLY}. The default value is
59      * {@link WebSettings#LOAD_DEFAULT}.
60      *
61      */
62     @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CACHE_MODE,
63             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setCacheMode(@acheMode int mode)64     public abstract void setCacheMode(@CacheMode int mode);
65 
66     /**
67      *
68      * Gets the current setting for overriding the cache mode.
69      *
70      * @return the current setting for overriding the cache mode
71      * @see #setCacheMode
72      *
73      */
74     @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CACHE_MODE,
75             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getCacheMode()76     public abstract @CacheMode int getCacheMode();
77 
78     /**
79      *
80      * Enables or disables content URL access from Service Workers, see
81      * {@link WebSettings#setAllowContentAccess}.
82      *
83      */
84     @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS,
85             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAllowContentAccess(boolean allow)86     public abstract void setAllowContentAccess(boolean allow);
87 
88     /**
89      *
90      * Gets whether Service Workers support content URL access.
91      *
92      * @see #setAllowContentAccess
93      *
94      */
95     @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS,
96             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getAllowContentAccess()97     public abstract boolean getAllowContentAccess();
98 
99     /**
100      *
101      * Enables or disables file access within Service Workers, see
102      * {@link WebSettings#setAllowFileAccess}.
103      *
104      */
105     @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS,
106             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAllowFileAccess(boolean allow)107     public abstract void setAllowFileAccess(boolean allow);
108 
109     /**
110      *
111      * Gets whether Service Workers support file access.
112      *
113      * @see #setAllowFileAccess
114      *
115      */
116     @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_FILE_ACCESS,
117             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getAllowFileAccess()118     public abstract boolean getAllowFileAccess();
119 
120     /**
121      *
122      * Sets whether Service Workers should not load resources from the network,
123      * see {@link WebSettings#setBlockNetworkLoads}.
124      *
125      * @param flag {@code true} means block network loads by the Service Workers
126      *
127      */
128     @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS,
129             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setBlockNetworkLoads(boolean flag)130     public abstract void setBlockNetworkLoads(boolean flag);
131 
132     /**
133      *
134      * Gets whether Service Workers are prohibited from loading any resources from the network.
135      *
136      * @return {@code true} if the Service Workers are not allowed to load any resources from the
137      * network
138      * @see #setBlockNetworkLoads
139      *
140      */
141     @RequiresFeature(name = WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS,
142             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getBlockNetworkLoads()143     public abstract boolean getBlockNetworkLoads();
144 }
145