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