19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* 29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project 39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License. 69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at 79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and 149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License. 159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.webkit; 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context; 209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.net.http.Headers; 219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log; 22f2bfe2bc05322a754c5a5d4e16ca182303ca72aaSteve Block 239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.File; 249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileInputStream; 259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileNotFoundException; 269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.IOException; 279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.InputStream; 289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.OutputStream; 299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Map; 309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3145a9a14006214e6478311ffcb980e766702d5a76Doug Zongker 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/** 33303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Manages the HTTP cache used by an application's {@link WebView} instances. 34c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick * @deprecated Access to the HTTP cache will be removed in a future release. 35f4912580e6adc90ab37b07b8108c7334f359e317Kristian Monsen * @hide Since {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} 369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 37303bc083c5a158ff240be658ac30d201cad56a18Steve Block// The class CacheManager provides the persistent cache of content that is 38303bc083c5a158ff240be658ac30d201cad56a18Steve Block// received over the network. The component handles parsing of HTTP headers and 39303bc083c5a158ff240be658ac30d201cad56a18Steve Block// utilizes the relevant cache headers to determine if the content should be 40303bc083c5a158ff240be658ac30d201cad56a18Steve Block// stored and if so, how long it is valid for. Network requests are provided to 41303bc083c5a158ff240be658ac30d201cad56a18Steve Block// this component and if they can not be resolved by the cache, the HTTP headers 42303bc083c5a158ff240be658ac30d201cad56a18Steve Block// are attached, as appropriate, to the request for revalidation of content. The 43303bc083c5a158ff240be658ac30d201cad56a18Steve Block// class also manages the cache size. 44303bc083c5a158ff240be658ac30d201cad56a18Steve Block// 45303bc083c5a158ff240be658ac30d201cad56a18Steve Block// CacheManager may only be used if your activity contains a WebView. 46c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick@Deprecated 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic final class CacheManager { 489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static final String LOGTAG = "cache"; 509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project static final String HEADER_KEY_IFMODIFIEDSINCE = "if-modified-since"; 529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project static final String HEADER_KEY_IFNONEMATCH = "if-none-match"; 539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static File mBaseDir; 559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 56f0c443deca49d597c8268ef3b0f7198976073241Ben Murdoch /** 57303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Represents a resource stored in the HTTP cache. Instances of this class 58303bc083c5a158ff240be658ac30d201cad56a18Steve Block * can be obtained by calling 59303bc083c5a158ff240be658ac30d201cad56a18Steve Block * {@link CacheManager#getCacheFile CacheManager.getCacheFile(String, Map<String, String>))}. 604e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 61c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick * @deprecated Access to the HTTP cache will be removed in a future release. 62f0c443deca49d597c8268ef3b0f7198976073241Ben Murdoch */ 63c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick @Deprecated 649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static class CacheResult { 659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // these fields are saved to the database 669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int httpStatusCode; 679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project long contentLength; 689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project long expires; 69e64c5567de20d06ac7ed1f5a01f018991cd40a52Grace Kloba String expiresString; 709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project String localPath; 719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project String lastModified; 729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project String etag; 739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project String mimeType; 749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project String location; 759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project String encoding; 760b956e1353a691674cb22c899c5a444b92532b60Grace Kloba String contentdisposition; 7760708a75120c4469dc2683485301ff9ee3b022e0Leon Clarke String crossDomain; 789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // these fields are NOT saved to the database 809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project InputStream inStream; 819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project OutputStream outStream; 829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project File outFile; 839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 84303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 85303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the status code of this cache entry. 864e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 874e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the status code of this cache entry 88303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public int getHttpStatusCode() { 909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return httpStatusCode; 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 93303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 94303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the content length of this cache entry. 954e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 964e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the content length of this cache entry 97303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public long getContentLength() { 999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return contentLength; 1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 102303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 103303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the path of the file used to store the content of this cache 104303bc083c5a158ff240be658ac30d201cad56a18Steve Block * entry, relative to the base directory of the cache. See 105303bc083c5a158ff240be658ac30d201cad56a18Steve Block * {@link CacheManager#getCacheFileBaseDir CacheManager.getCacheFileBaseDir()}. 1064e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1074e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the path of the file used to store this cache entry 108303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getLocalPath() { 1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return localPath; 1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 113303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 114303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the expiry date of this cache entry, expressed in milliseconds 115303bc083c5a158ff240be658ac30d201cad56a18Steve Block * since midnight, January 1, 1970 UTC. 1164e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1174e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the expiry date of this cache entry 118303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public long getExpires() { 1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return expires; 1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 123303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 124303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the expiry date of this cache entry, expressed as a string. 1254e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1264e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the expiry date of this cache entry 127303bc083c5a158ff240be658ac30d201cad56a18Steve Block * 128303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 129e64c5567de20d06ac7ed1f5a01f018991cd40a52Grace Kloba public String getExpiresString() { 130e64c5567de20d06ac7ed1f5a01f018991cd40a52Grace Kloba return expiresString; 131e64c5567de20d06ac7ed1f5a01f018991cd40a52Grace Kloba } 132e64c5567de20d06ac7ed1f5a01f018991cd40a52Grace Kloba 133303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 134303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the date at which this cache entry was last modified, expressed 135303bc083c5a158ff240be658ac30d201cad56a18Steve Block * as a string. 1364e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1374e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the date at which this cache entry was last modified 138303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getLastModified() { 1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return lastModified; 1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 143303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 144303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the entity tag of this cache entry. 1454e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1464e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the entity tag of this cache entry 147303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getETag() { 1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return etag; 1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 152303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 153303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the MIME type of this cache entry. 1544e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1554e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the MIME type of this cache entry 156303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getMimeType() { 1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mimeType; 1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 161303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 162303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the value of the HTTP 'Location' header with which this cache 163303bc083c5a158ff240be658ac30d201cad56a18Steve Block * entry was received. 1644e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1654e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the HTTP 'Location' header for this cache entry 166303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getLocation() { 1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return location; 1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 171303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 172303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the encoding of this cache entry. 1734e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1744e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the encoding of this cache entry 175303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String getEncoding() { 1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return encoding; 1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 180303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 181303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the value of the HTTP 'Content-Disposition' header with which 182303bc083c5a158ff240be658ac30d201cad56a18Steve Block * this cache entry was received. 1834e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1844e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the HTTP 'Content-Disposition' header for this cache entry 185303bc083c5a158ff240be658ac30d201cad56a18Steve Block * 186303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 1870b956e1353a691674cb22c899c5a444b92532b60Grace Kloba public String getContentDisposition() { 1880b956e1353a691674cb22c899c5a444b92532b60Grace Kloba return contentdisposition; 1890b956e1353a691674cb22c899c5a444b92532b60Grace Kloba } 1900b956e1353a691674cb22c899c5a444b92532b60Grace Kloba 191303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 192303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the input stream to the content of this cache entry, to allow 193303bc083c5a158ff240be658ac30d201cad56a18Steve Block * content to be read. See 194303bc083c5a158ff240be658ac30d201cad56a18Steve Block * {@link CacheManager#getCacheFile CacheManager.getCacheFile(String, Map<String, String>)}. 1954e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 1964e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return an input stream to the content of this cache entry 197303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public InputStream getInputStream() { 1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return inStream; 2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 202303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 203303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets an output stream to the content of this cache entry, to allow 204303bc083c5a158ff240be658ac30d201cad56a18Steve Block * content to be written. See 205303bc083c5a158ff240be658ac30d201cad56a18Steve Block * {@link CacheManager#saveCacheFile CacheManager.saveCacheFile(String, CacheResult)}. 2064e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 2074e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return an output stream to the content of this cache entry 208303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 209303bc083c5a158ff240be658ac30d201cad56a18Steve Block // Note that this is always null for objects returned by getCacheFile()! 2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public OutputStream getOutputStream() { 2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return outStream; 2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 214303bc083c5a158ff240be658ac30d201cad56a18Steve Block 215303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 216303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Sets an input stream to the content of this cache entry. 2174e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 2184e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @param stream an input stream to the content of this cache entry 219303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setInputStream(InputStream stream) { 2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project this.inStream = stream; 2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 224303bc083c5a158ff240be658ac30d201cad56a18Steve Block /** 225303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Sets the encoding of this cache entry. 2264e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 2274e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @param encoding the encoding of this cache entry 228303bc083c5a158ff240be658ac30d201cad56a18Steve Block */ 2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setEncoding(String encoding) { 2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project this.encoding = encoding; 2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 232b67529b905440e2ba550742773b927abad882c19Iain Merrick 233b67529b905440e2ba550742773b927abad882c19Iain Merrick /** 234b67529b905440e2ba550742773b927abad882c19Iain Merrick * @hide 235b67529b905440e2ba550742773b927abad882c19Iain Merrick */ 236b67529b905440e2ba550742773b927abad882c19Iain Merrick public void setContentLength(long contentLength) { 237b67529b905440e2ba550742773b927abad882c19Iain Merrick this.contentLength = contentLength; 238b67529b905440e2ba550742773b927abad882c19Iain Merrick } 2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 242303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Initializes the HTTP cache. This method must be called before any 243303bc083c5a158ff240be658ac30d201cad56a18Steve Block * CacheManager methods are used. Note that this is called automatically 244303bc083c5a158ff240be658ac30d201cad56a18Steve Block * when a {@link WebView} is created. 2454e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 2464e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @param context the application context 2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project static void init(Context context) { 2490acb1c32fa002a648c8090f622b0094f406d5411Steve Block // This isn't actually where the real cache lives, but where we put files for the 2500acb1c32fa002a648c8090f622b0094f406d5411Steve Block // purpose of getCacheFile(). 2510acb1c32fa002a648c8090f622b0094f406d5411Steve Block mBaseDir = new File(context.getCacheDir(), "webviewCacheChromiumStaging"); 2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!mBaseDir.exists()) { 2530acb1c32fa002a648c8090f622b0094f406d5411Steve Block mBaseDir.mkdirs(); 2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 258303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets the base directory in which the files used to store the contents of 259303bc083c5a158ff240be658ac30d201cad56a18Steve Block * cache entries are placed. See 260303bc083c5a158ff240be658ac30d201cad56a18Steve Block * {@link CacheManager.CacheResult#getLocalPath CacheManager.CacheResult.getLocalPath()}. 2614e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 2624e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the base directory of the cache 263c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick * @deprecated Access to the HTTP cache will be removed in a future release. 2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 265c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick @Deprecated 2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static File getCacheFileBaseDir() { 2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mBaseDir; 2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 271303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Gets whether the HTTP cache is disabled. 2724e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 2734e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return true if the HTTP cache is disabled 274c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick * @deprecated Access to the HTTP cache will be removed in a future release. 2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 276c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick @Deprecated 2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static boolean cacheDisabled() { 2781e17ecae25c8b56db6d168851b858aa3ef9a3b6aSteve Block return false; 2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2812036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba /** 282303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Starts a cache transaction. Returns true if this is the only running 283303bc083c5a158ff240be658ac30d201cad56a18Steve Block * transaction. Otherwise, this transaction is nested inside currently 284303bc083c5a158ff240be658ac30d201cad56a18Steve Block * running transactions and false is returned. 2854e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 2864e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return true if this is the only running transaction 2874e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @deprecated This method no longer has any effect and always returns false. 2882036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba */ 2892036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba @Deprecated 2902036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba public static boolean startCacheTransaction() { 2912036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba return false; 2922036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba } 2932036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba 2942036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba /** 295303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Ends the innermost cache transaction and returns whether this was the 296303bc083c5a158ff240be658ac30d201cad56a18Steve Block * only running transaction. 2974e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 2984e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return true if this was the only running transaction 2994e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @deprecated This method no longer has any effect and always returns false. 3002036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba */ 3012036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba @Deprecated 3022036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba public static boolean endCacheTransaction() { 3032036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba return false; 3042036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba } 3052036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba 3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 307ffefba15e3a011836f037c185d4909a59995ff32Steve Block * Gets the cache entry for the specified URL, or null if none is found. 308ffefba15e3a011836f037c185d4909a59995ff32Steve Block * If a non-null value is provided for the HTTP headers map, and the cache 309ffefba15e3a011836f037c185d4909a59995ff32Steve Block * entry needs validation, appropriate headers will be added to the map. 310ffefba15e3a011836f037c185d4909a59995ff32Steve Block * The input stream of the CacheEntry object should be closed by the caller 311ffefba15e3a011836f037c185d4909a59995ff32Steve Block * when access to the underlying file is no longer required. 3124e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 3134e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @param url the URL for which a cache entry is requested 3144e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @param headers a map from HTTP header name to value, to be populated 315ffefba15e3a011836f037c185d4909a59995ff32Steve Block * for the returned cache entry 3164e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return the cache entry for the specified URL 317c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick * @deprecated Access to the HTTP cache will be removed in a future release. 3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 319c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick @Deprecated 3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static CacheResult getCacheFile(String url, 3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Map<String, String> headers) { 3228c92c39b858ae73a1b08ed698887efa98ced987cGrace Kloba return getCacheFile(url, 0, headers); 3238c92c39b858ae73a1b08ed698887efa98ced987cGrace Kloba } 3248c92c39b858ae73a1b08ed698887efa98ced987cGrace Kloba 3250acb1c32fa002a648c8090f622b0094f406d5411Steve Block static CacheResult getCacheFile(String url, long postIdentifier, 3260acb1c32fa002a648c8090f622b0094f406d5411Steve Block Map<String, String> headers) { 327ffefba15e3a011836f037c185d4909a59995ff32Steve Block CacheResult result = nativeGetCacheResult(url); 328ffefba15e3a011836f037c185d4909a59995ff32Steve Block if (result == null) { 3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 331ffefba15e3a011836f037c185d4909a59995ff32Steve Block // A temporary local file will have been created native side and localPath set 332ffefba15e3a011836f037c185d4909a59995ff32Steve Block // appropriately. 333ffefba15e3a011836f037c185d4909a59995ff32Steve Block File src = new File(mBaseDir, result.localPath); 334ffefba15e3a011836f037c185d4909a59995ff32Steve Block try { 335ffefba15e3a011836f037c185d4909a59995ff32Steve Block // Open the file here so that even if it is deleted, the content 336ffefba15e3a011836f037c185d4909a59995ff32Steve Block // is still readable by the caller until close() is called. 337ffefba15e3a011836f037c185d4909a59995ff32Steve Block result.inStream = new FileInputStream(src); 338ffefba15e3a011836f037c185d4909a59995ff32Steve Block } catch (FileNotFoundException e) { 339ffefba15e3a011836f037c185d4909a59995ff32Steve Block Log.v(LOGTAG, "getCacheFile(): Failed to open file: " + e); 340ffefba15e3a011836f037c185d4909a59995ff32Steve Block // TODO: The files in the cache directory can be removed by the 341ffefba15e3a011836f037c185d4909a59995ff32Steve Block // system. If it is gone, what should we do? 342ffefba15e3a011836f037c185d4909a59995ff32Steve Block return null; 343808751fe7ac16bf7224cba284a318695d8093355Steve Block } 3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 34567ba204aa23e7d5a96ad241a1623e44976b51741Steve Block // A null value for headers is used by CACHE_MODE_CACHE_ONLY to imply 34667ba204aa23e7d5a96ad241a1623e44976b51741Steve Block // that we should provide the cache result even if it is expired. 34767ba204aa23e7d5a96ad241a1623e44976b51741Steve Block // Note that a negative expires value means a time in the far future. 3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (headers != null && result.expires >= 0 3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project && result.expires <= System.currentTimeMillis()) { 3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (result.lastModified == null && result.etag == null) { 3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 35367ba204aa23e7d5a96ad241a1623e44976b51741Steve Block // Return HEADER_KEY_IFNONEMATCH or HEADER_KEY_IFMODIFIEDSINCE 35467ba204aa23e7d5a96ad241a1623e44976b51741Steve Block // for requesting validation. 3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (result.etag != null) { 3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project headers.put(HEADER_KEY_IFNONEMATCH, result.etag); 3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (result.lastModified != null) { 3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project headers.put(HEADER_KEY_IFMODIFIEDSINCE, result.lastModified); 3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3632e5c150e746647a1ce5c10e1708debbf06c45ea7Derek Sollenberger if (DebugFlags.CACHE_MANAGER) { 3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Log.v(LOGTAG, "getCacheFile for url " + url); 3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return result; 3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3714e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * Given a URL and its full headers, gets a CacheResult if a local cache 3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * can be stored. Otherwise returns null. The mimetype is passed in so that 3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the function can use the mimetype that will be passed to WebCore which 3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * could be different from the mimetype defined in the headers. 3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * forceCache is for out-of-package callers to force creation of a 3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * CacheResult, and is used to supply surrogate responses for URL 3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * interception. 3784e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 3794e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return a CacheResult for a given URL 3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 381303bc083c5a158ff240be658ac30d201cad56a18Steve Block static CacheResult createCacheFile(String url, int statusCode, 3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Headers headers, String mimeType, boolean forceCache) { 3830acb1c32fa002a648c8090f622b0094f406d5411Steve Block // This method is public but hidden. We break functionality. 3840acb1c32fa002a648c8090f622b0094f406d5411Steve Block return null; 3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 388303bc083c5a158ff240be658ac30d201cad56a18Steve Block * Adds a cache entry to the HTTP cache for the specicifed URL. Also closes 389303bc083c5a158ff240be658ac30d201cad56a18Steve Block * the cache entry's output stream. 3904e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * 3914e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @param url the URL for which the cache entry should be added 3924e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @param cacheResult the cache entry to add 393c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick * @deprecated Access to the HTTP cache will be removed in a future release. 3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 395c96235deb9f4d08285f3b1a2c28ea9f771b40f47Iain Merrick @Deprecated 396303bc083c5a158ff240be658ac30d201cad56a18Steve Block public static void saveCacheFile(String url, CacheResult cacheResult) { 397303bc083c5a158ff240be658ac30d201cad56a18Steve Block saveCacheFile(url, 0, cacheResult); 3988c92c39b858ae73a1b08ed698887efa98ced987cGrace Kloba } 3998c92c39b858ae73a1b08ed698887efa98ced987cGrace Kloba 4008c92c39b858ae73a1b08ed698887efa98ced987cGrace Kloba static void saveCacheFile(String url, long postIdentifier, 4018c92c39b858ae73a1b08ed698887efa98ced987cGrace Kloba CacheResult cacheRet) { 4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project cacheRet.outStream.close(); 4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (IOException e) { 4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return; 4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4080acb1c32fa002a648c8090f622b0094f406d5411Steve Block // This method is exposed in the public API but the API provides no 4090acb1c32fa002a648c8090f622b0094f406d5411Steve Block // way to obtain a new CacheResult object with a non-null output 4100acb1c32fa002a648c8090f622b0094f406d5411Steve Block // stream ... 4110acb1c32fa002a648c8090f622b0094f406d5411Steve Block // - CacheResult objects returned by getCacheFile() have a null 4120acb1c32fa002a648c8090f622b0094f406d5411Steve Block // output stream. 4130acb1c32fa002a648c8090f622b0094f406d5411Steve Block // - new CacheResult objects have a null output stream and no 4140acb1c32fa002a648c8090f622b0094f406d5411Steve Block // setter is provided. 4150acb1c32fa002a648c8090f622b0094f406d5411Steve Block // Since this method throws a null pointer exception in this case, 4160acb1c32fa002a648c8090f622b0094f406d5411Steve Block // it is effectively useless from the point of view of the public 4170acb1c32fa002a648c8090f622b0094f406d5411Steve Block // API. 4180acb1c32fa002a648c8090f622b0094f406d5411Steve Block // 4190acb1c32fa002a648c8090f622b0094f406d5411Steve Block // With the Chromium HTTP stack we continue to throw the same 4200acb1c32fa002a648c8090f622b0094f406d5411Steve Block // exception for 'backwards compatibility' with the Android HTTP 4210acb1c32fa002a648c8090f622b0094f406d5411Steve Block // stack. 4220acb1c32fa002a648c8090f622b0094f406d5411Steve Block // 4230acb1c32fa002a648c8090f622b0094f406d5411Steve Block // This method is not used from within this package, and for public API 4240acb1c32fa002a648c8090f622b0094f406d5411Steve Block // use, we should already have thrown an exception above. 4250acb1c32fa002a648c8090f622b0094f406d5411Steve Block assert false; 426998c05b3b5db124a31da1ac38af0e97bca114122Grace Kloba } 427998c05b3b5db124a31da1ac38af0e97bca114122Grace Kloba 4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 4294e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * Removes all cache files. 43067ba204aa23e7d5a96ad241a1623e44976b51741Steve Block * 4314e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block * @return whether the removal succeeded 4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project static boolean removeAllCacheFiles() { 4342036dbab1726c34953360a7a56d6b9ef1f2aa7ddGrace Kloba // delete cache files in a separate thread to not block UI. 4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project final Runnable clearCache = new Runnable() { 4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void run() { 4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // delete all cache files 4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project try { 4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project String[] files = mBaseDir.list(); 4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // if mBaseDir doesn't exist, files can be null. 4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (files != null) { 4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project for (int i = 0; i < files.length; i++) { 443543221fc4b9dd16db2c687cd59f1eeea8d89c5a5Cary Clark File f = new File(mBaseDir, files[i]); 444543221fc4b9dd16db2c687cd59f1eeea8d89c5a5Cary Clark if (!f.delete()) { 445543221fc4b9dd16db2c687cd59f1eeea8d89c5a5Cary Clark Log.e(LOGTAG, f.getPath() + " delete failed."); 446543221fc4b9dd16db2c687cd59f1eeea8d89c5a5Cary Clark } 4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } catch (SecurityException e) { 4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Ignore SecurityExceptions. 4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project }; 4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project new Thread(clearCache).start(); 4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return true; 4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 458e8492473a94e827b9a73f1fa4f5741e85c0e832cSteve Block private static native CacheResult nativeGetCacheResult(String url); 4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 460