1 /* 2 * Copyright (C) 2006 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 19 import android.annotation.Nullable; 20 import android.annotation.UnsupportedAppUsage; 21 22 import java.io.File; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.io.OutputStream; 26 import java.util.Map; 27 28 /** 29 * Manages the HTTP cache used by an application's {@link WebView} instances. 30 * @deprecated Access to the HTTP cache will be removed in a future release. 31 * @hide Since {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} 32 */ 33 // The class CacheManager provides the persistent cache of content that is 34 // received over the network. The component handles parsing of HTTP headers and 35 // utilizes the relevant cache headers to determine if the content should be 36 // stored and if so, how long it is valid for. Network requests are provided to 37 // this component and if they can not be resolved by the cache, the HTTP headers 38 // are attached, as appropriate, to the request for revalidation of content. The 39 // class also manages the cache size. 40 // 41 // CacheManager may only be used if your activity contains a WebView. 42 @Deprecated 43 public final class CacheManager { 44 /** 45 * Represents a resource stored in the HTTP cache. Instances of this class 46 * can be obtained by calling 47 * {@link CacheManager#getCacheFile CacheManager.getCacheFile(String, Map<String, String>))}. 48 * 49 * @deprecated Access to the HTTP cache will be removed in a future release. 50 */ 51 @Deprecated 52 public static class CacheResult { 53 // these fields are saved to the database 54 @UnsupportedAppUsage 55 int httpStatusCode; 56 @UnsupportedAppUsage 57 long contentLength; 58 @UnsupportedAppUsage 59 long expires; 60 @UnsupportedAppUsage 61 String expiresString; 62 @UnsupportedAppUsage 63 String localPath; 64 @UnsupportedAppUsage 65 String lastModified; 66 @UnsupportedAppUsage 67 String etag; 68 @UnsupportedAppUsage 69 String mimeType; 70 @UnsupportedAppUsage 71 String location; 72 @UnsupportedAppUsage 73 String encoding; 74 @UnsupportedAppUsage 75 String contentdisposition; 76 @UnsupportedAppUsage 77 String crossDomain; 78 79 // these fields are NOT saved to the database 80 @UnsupportedAppUsage 81 InputStream inStream; 82 @UnsupportedAppUsage 83 OutputStream outStream; 84 @UnsupportedAppUsage 85 File outFile; 86 87 /** 88 * Gets the status code of this cache entry. 89 * 90 * @return the status code of this cache entry 91 */ 92 @UnsupportedAppUsage getHttpStatusCode()93 public int getHttpStatusCode() { 94 return httpStatusCode; 95 } 96 97 /** 98 * Gets the content length of this cache entry. 99 * 100 * @return the content length of this cache entry 101 */ 102 @UnsupportedAppUsage getContentLength()103 public long getContentLength() { 104 return contentLength; 105 } 106 107 /** 108 * Gets the path of the file used to store the content of this cache 109 * entry, relative to the base directory of the cache. See 110 * {@link CacheManager#getCacheFileBaseDir CacheManager.getCacheFileBaseDir()}. 111 * 112 * @return the path of the file used to store this cache entry 113 */ 114 @UnsupportedAppUsage getLocalPath()115 public String getLocalPath() { 116 return localPath; 117 } 118 119 /** 120 * Gets the expiry date of this cache entry, expressed in milliseconds 121 * since midnight, January 1, 1970 UTC. 122 * 123 * @return the expiry date of this cache entry 124 */ 125 @UnsupportedAppUsage getExpires()126 public long getExpires() { 127 return expires; 128 } 129 130 /** 131 * Gets the expiry date of this cache entry, expressed as a string. 132 * 133 * @return the expiry date of this cache entry 134 * 135 */ 136 @UnsupportedAppUsage getExpiresString()137 public String getExpiresString() { 138 return expiresString; 139 } 140 141 /** 142 * Gets the date at which this cache entry was last modified, expressed 143 * as a string. 144 * 145 * @return the date at which this cache entry was last modified 146 */ 147 @UnsupportedAppUsage getLastModified()148 public String getLastModified() { 149 return lastModified; 150 } 151 152 /** 153 * Gets the entity tag of this cache entry. 154 * 155 * @return the entity tag of this cache entry 156 */ 157 @UnsupportedAppUsage getETag()158 public String getETag() { 159 return etag; 160 } 161 162 /** 163 * Gets the MIME type of this cache entry. 164 * 165 * @return the MIME type of this cache entry 166 */ 167 @UnsupportedAppUsage getMimeType()168 public String getMimeType() { 169 return mimeType; 170 } 171 172 /** 173 * Gets the value of the HTTP 'Location' header with which this cache 174 * entry was received. 175 * 176 * @return the HTTP 'Location' header for this cache entry 177 */ 178 @UnsupportedAppUsage getLocation()179 public String getLocation() { 180 return location; 181 } 182 183 /** 184 * Gets the encoding of this cache entry. 185 * 186 * @return the encoding of this cache entry 187 */ 188 @UnsupportedAppUsage getEncoding()189 public String getEncoding() { 190 return encoding; 191 } 192 193 /** 194 * Gets the value of the HTTP 'Content-Disposition' header with which 195 * this cache entry was received. 196 * 197 * @return the HTTP 'Content-Disposition' header for this cache entry 198 * 199 */ 200 @UnsupportedAppUsage getContentDisposition()201 public String getContentDisposition() { 202 return contentdisposition; 203 } 204 205 /** 206 * Gets the input stream to the content of this cache entry, to allow 207 * content to be read. See 208 * {@link CacheManager#getCacheFile CacheManager.getCacheFile(String, Map<String, String>)}. 209 * 210 * @return an input stream to the content of this cache entry 211 */ 212 @UnsupportedAppUsage getInputStream()213 public InputStream getInputStream() { 214 return inStream; 215 } 216 217 /** 218 * Gets an output stream to the content of this cache entry, to allow 219 * content to be written. See 220 * {@link CacheManager#saveCacheFile CacheManager.saveCacheFile(String, CacheResult)}. 221 * 222 * @return an output stream to the content of this cache entry 223 */ 224 // Note that this is always null for objects returned by getCacheFile()! 225 @UnsupportedAppUsage getOutputStream()226 public OutputStream getOutputStream() { 227 return outStream; 228 } 229 230 231 /** 232 * Sets an input stream to the content of this cache entry. 233 * 234 * @param stream an input stream to the content of this cache entry 235 */ 236 @UnsupportedAppUsage setInputStream(InputStream stream)237 public void setInputStream(InputStream stream) { 238 this.inStream = stream; 239 } 240 241 /** 242 * Sets the encoding of this cache entry. 243 * 244 * @param encoding the encoding of this cache entry 245 */ 246 @UnsupportedAppUsage setEncoding(String encoding)247 public void setEncoding(String encoding) { 248 this.encoding = encoding; 249 } 250 251 /** 252 * @hide 253 */ setContentLength(long contentLength)254 public void setContentLength(long contentLength) { 255 this.contentLength = contentLength; 256 } 257 } 258 259 /** 260 * Gets the base directory in which the files used to store the contents of 261 * cache entries are placed. See 262 * {@link CacheManager.CacheResult#getLocalPath CacheManager.CacheResult.getLocalPath()}. 263 * 264 * @return the base directory of the cache 265 * @deprecated This method no longer has any effect and always returns {@code null}. 266 */ 267 @Deprecated 268 @Nullable 269 @UnsupportedAppUsage getCacheFileBaseDir()270 public static File getCacheFileBaseDir() { 271 return null; 272 } 273 274 /** 275 * Gets whether the HTTP cache is disabled. 276 * 277 * @return {@code true} if the HTTP cache is disabled 278 * @deprecated This method no longer has any effect and always returns {@code false}. 279 */ 280 @Deprecated 281 @UnsupportedAppUsage cacheDisabled()282 public static boolean cacheDisabled() { 283 return false; 284 } 285 286 /** 287 * Starts a cache transaction. Returns {@code true} if this is the only running 288 * transaction. Otherwise, this transaction is nested inside currently 289 * running transactions and {@code false} is returned. 290 * 291 * @return {@code true} if this is the only running transaction 292 * @deprecated This method no longer has any effect and always returns {@code false}. 293 */ 294 @Deprecated 295 @UnsupportedAppUsage startCacheTransaction()296 public static boolean startCacheTransaction() { 297 return false; 298 } 299 300 /** 301 * Ends the innermost cache transaction and returns whether this was the 302 * only running transaction. 303 * 304 * @return {@code true} if this was the only running transaction 305 * @deprecated This method no longer has any effect and always returns {@code false}. 306 */ 307 @Deprecated 308 @UnsupportedAppUsage endCacheTransaction()309 public static boolean endCacheTransaction() { 310 return false; 311 } 312 313 /** 314 * Gets the cache entry for the specified URL, or {@code null} if none is found. 315 * If a non-null value is provided for the HTTP headers map, and the cache 316 * entry needs validation, appropriate headers will be added to the map. 317 * The input stream of the CacheEntry object should be closed by the caller 318 * when access to the underlying file is no longer required. 319 * 320 * @param url the URL for which a cache entry is requested 321 * @param headers a map from HTTP header name to value, to be populated 322 * for the returned cache entry 323 * @return the cache entry for the specified URL 324 * @deprecated This method no longer has any effect and always returns {@code null}. 325 */ 326 @Deprecated 327 @Nullable 328 @UnsupportedAppUsage getCacheFile(String url, Map<String, String> headers)329 public static CacheResult getCacheFile(String url, 330 Map<String, String> headers) { 331 return null; 332 } 333 334 /** 335 * Adds a cache entry to the HTTP cache for the specicifed URL. Also closes 336 * the cache entry's output stream. 337 * 338 * @param url the URL for which the cache entry should be added 339 * @param cacheResult the cache entry to add 340 * @deprecated Access to the HTTP cache will be removed in a future release. 341 */ 342 @Deprecated 343 @UnsupportedAppUsage saveCacheFile(String url, CacheResult cacheResult)344 public static void saveCacheFile(String url, CacheResult cacheResult) { 345 saveCacheFile(url, 0, cacheResult); 346 } 347 348 @UnsupportedAppUsage saveCacheFile(String url, long postIdentifier, CacheResult cacheRet)349 static void saveCacheFile(String url, long postIdentifier, 350 CacheResult cacheRet) { 351 try { 352 cacheRet.outStream.close(); 353 } catch (IOException e) { 354 return; 355 } 356 357 // This method is exposed in the public API but the API provides no 358 // way to obtain a new CacheResult object with a non-null output 359 // stream ... 360 // - CacheResult objects returned by getCacheFile() have a null 361 // output stream. 362 // - new CacheResult objects have a null output stream and no 363 // setter is provided. 364 // Since this method throws a null pointer exception in this case, 365 // it is effectively useless from the point of view of the public 366 // API. 367 // 368 // With the Chromium HTTP stack we continue to throw the same 369 // exception for 'backwards compatibility' with the Android HTTP 370 // stack. 371 // 372 // This method is not used from within this package, and for public API 373 // use, we should already have thrown an exception above. 374 assert false; 375 } 376 } 377