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.annotation.Nullable;
20import android.webkit.CacheManager.CacheResult;
21import android.webkit.PluginData;
22import android.webkit.UrlInterceptHandler;
23
24import java.util.Iterator;
25import java.util.LinkedList;
26import 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
34public 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
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    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
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    public static synchronized boolean registerHandler(
89            UrlInterceptHandler handler) {
90        if (!getHandlers().contains(handler)) {
91            getHandlers().addFirst(handler);
92            return true;
93        } else {
94            return false;
95        }
96    }
97
98    /**
99     * Unregister a previously registered UrlInterceptHandler.
100     *
101     * @param handler A previously registered UrlInterceptHandler.
102     * @return {@code true} if the handler was found and removed from the list.
103     *
104     * @hide
105     * @deprecated This class was intended to be used by Gears. Since Gears was
106     * deprecated, so is this class.
107     */
108    @Deprecated
109    public static synchronized boolean unregisterHandler(
110            UrlInterceptHandler handler) {
111        return getHandlers().remove(handler);
112    }
113
114    /**
115     * Given an url, returns the CacheResult of the first
116     * UrlInterceptHandler interested, or {@code null} if none are.
117     *
118     * @return A CacheResult containing surrogate content.
119     *
120     * @hide
121     * @deprecated This class was intended to be used by Gears. Since Gears was
122     * deprecated, so is this class.
123     */
124    @Deprecated
125    @Nullable
126    public static synchronized CacheResult getSurrogate(
127            String url, Map<String, String> headers) {
128        if (urlInterceptDisabled()) {
129            return null;
130        }
131        Iterator iter = getHandlers().listIterator();
132        while (iter.hasNext()) {
133            UrlInterceptHandler handler = (UrlInterceptHandler) iter.next();
134            CacheResult result = handler.service(url, headers);
135            if (result != null) {
136                return result;
137            }
138        }
139        return null;
140    }
141
142    /**
143     * Given an url, returns the PluginData of the first
144     * UrlInterceptHandler interested, or {@code null} if none are or if
145     * intercepts are disabled.
146     *
147     * @return A PluginData instance containing surrogate content.
148     *
149     * @hide
150     * @deprecated This class was intended to be used by Gears. Since Gears was
151     * deprecated, so is this class.
152     */
153    @Deprecated
154    @Nullable
155    public static synchronized PluginData getPluginData(
156            String url, Map<String, String> headers) {
157        if (urlInterceptDisabled()) {
158            return null;
159        }
160        Iterator iter = getHandlers().listIterator();
161        while (iter.hasNext()) {
162            UrlInterceptHandler handler = (UrlInterceptHandler) iter.next();
163            PluginData data = handler.getPluginData(url, headers);
164            if (data != null) {
165                return data;
166            }
167        }
168        return null;
169    }
170}
171