1 /*
2  * Copyright (C) 2008 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.Nullable;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.os.Build;
22 import android.webkit.CacheManager.CacheResult;
23 
24 import java.util.Iterator;
25 import java.util.LinkedList;
26 import java.util.Map;
27 
28 /**
29  * @hide
30  * @deprecated This class was intended to be used by Gears. Since Gears was
31  * deprecated, so is this class.
32  */
33 @Deprecated
34 public final class UrlInterceptRegistry {
35 
36     private final static String LOGTAG = "intercept";
37 
38     private static boolean mDisabled = false;
39 
40     private static LinkedList mHandlerList;
41 
getHandlers()42     private static synchronized LinkedList getHandlers() {
43         if(mHandlerList == null)
44             mHandlerList = new LinkedList<UrlInterceptHandler>();
45         return mHandlerList;
46     }
47 
48     /**
49      * set the flag to control whether url intercept is enabled or disabled
50      *
51      * @param disabled {@code true} to disable the cache
52      *
53      * @hide
54      * @deprecated This class was intended to be used by Gears. Since Gears was
55      * deprecated, so is this class.
56      */
57     @Deprecated
58     @UnsupportedAppUsage
setUrlInterceptDisabled(boolean disabled)59     public static synchronized void setUrlInterceptDisabled(boolean disabled) {
60         mDisabled = disabled;
61     }
62 
63     /**
64      * get the state of the url intercept, enabled or disabled
65      *
66      * @return return if it is disabled
67      *
68      * @hide
69      * @deprecated This class was intended to be used by Gears. Since Gears was
70      * deprecated, so is this class.
71      */
72     @Deprecated
urlInterceptDisabled()73     public static synchronized boolean urlInterceptDisabled() {
74         return mDisabled;
75     }
76 
77     /**
78      * Register a new UrlInterceptHandler. This handler will be called
79      * before any that were previously registered.
80      *
81      * @param handler The new UrlInterceptHandler object
82      * @return {@code true} if the handler was not previously registered.
83      *
84      * @hide
85      * @deprecated This class was intended to be used by Gears. Since Gears was
86      * deprecated, so is this class.
87      */
88     @Deprecated
89     @UnsupportedAppUsage
registerHandler( UrlInterceptHandler handler)90     public static synchronized boolean registerHandler(
91             UrlInterceptHandler handler) {
92         if (!getHandlers().contains(handler)) {
93             getHandlers().addFirst(handler);
94             return true;
95         } else {
96             return false;
97         }
98     }
99 
100     /**
101      * Unregister a previously registered UrlInterceptHandler.
102      *
103      * @param handler A previously registered UrlInterceptHandler.
104      * @return {@code true} if the handler was found and removed from the list.
105      *
106      * @hide
107      * @deprecated This class was intended to be used by Gears. Since Gears was
108      * deprecated, so is this class.
109      */
110     @Deprecated
111     @UnsupportedAppUsage
unregisterHandler( UrlInterceptHandler handler)112     public static synchronized boolean unregisterHandler(
113             UrlInterceptHandler handler) {
114         return getHandlers().remove(handler);
115     }
116 
117     /**
118      * Given an url, returns the CacheResult of the first
119      * UrlInterceptHandler interested, or {@code null} if none are.
120      *
121      * @return A CacheResult containing surrogate content.
122      *
123      * @hide
124      * @deprecated This class was intended to be used by Gears. Since Gears was
125      * deprecated, so is this class.
126      */
127     @Deprecated
128     @Nullable
getSurrogate( String url, Map<String, String> headers)129     public static synchronized CacheResult getSurrogate(
130             String url, Map<String, String> headers) {
131         if (urlInterceptDisabled()) {
132             return null;
133         }
134         Iterator iter = getHandlers().listIterator();
135         while (iter.hasNext()) {
136             UrlInterceptHandler handler = (UrlInterceptHandler) iter.next();
137             CacheResult result = handler.service(url, headers);
138             if (result != null) {
139                 return result;
140             }
141         }
142         return null;
143     }
144 
145     /**
146      * Given an url, returns the PluginData of the first
147      * UrlInterceptHandler interested, or {@code null} if none are or if
148      * intercepts are disabled.
149      *
150      * @return A PluginData instance containing surrogate content.
151      *
152      * @hide
153      * @deprecated This class was intended to be used by Gears. Since Gears was
154      * deprecated, so is this class.
155      */
156     @Deprecated
157     @Nullable
158     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
getPluginData( String url, Map<String, String> headers)159     public static synchronized PluginData getPluginData(
160             String url, Map<String, String> headers) {
161         if (urlInterceptDisabled()) {
162             return null;
163         }
164         Iterator iter = getHandlers().listIterator();
165         while (iter.hasNext()) {
166             UrlInterceptHandler handler = (UrlInterceptHandler) iter.next();
167             PluginData data = handler.getPluginData(url, headers);
168             if (data != null) {
169                 return data;
170             }
171         }
172         return null;
173     }
174 }
175