Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 | |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 20 | import android.compat.annotation.UnsupportedAppUsage; |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 21 | import android.webkit.CacheManager.CacheResult; |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 | |
| 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 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 50 | * @param disabled {@code true} to disable the cache |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 |
Mathew Inwood | 62d83fb | 2018-08-16 19:09:48 +0100 | [diff] [blame] | 57 | @UnsupportedAppUsage |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 81 | * @return {@code true} if the handler was not previously registered. |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 |
Mathew Inwood | 62d83fb | 2018-08-16 19:09:48 +0100 | [diff] [blame] | 88 | @UnsupportedAppUsage |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 103 | * @return {@code true} if the handler was found and removed from the list. |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 |
Mathew Inwood | 62d83fb | 2018-08-16 19:09:48 +0100 | [diff] [blame] | 110 | @UnsupportedAppUsage |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 118 | * UrlInterceptHandler interested, or {@code null} if none are. |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 127 | @Nullable |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 146 | * UrlInterceptHandler interested, or {@code null} if none are or if |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 156 | @Nullable |
Mathew Inwood | 62d83fb | 2018-08-16 19:09:48 +0100 | [diff] [blame] | 157 | @UnsupportedAppUsage |
Jonathan Dixon | 74fc73f | 2013-10-18 16:51:53 -0700 | [diff] [blame] | 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 | } |