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