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