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