blob: 563e00e3ca6afdec91ca68deedbadceed6502776 [file] [log] [blame]
Jonathan Dixon74fc73f2013-10-18 16:51:53 -07001/*
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
17package android.webkit;
18
Nate Fischer3442c742017-09-08 17:02:00 -070019import android.annotation.Nullable;
Mathew Inwood42afea22018-08-16 19:18:28 +010020import android.annotation.UnsupportedAppUsage;
Nate Fischer3442c742017-09-08 17:02:00 -070021
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070022import java.io.File;
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070023import java.io.IOException;
24import java.io.InputStream;
25import java.io.OutputStream;
26import java.util.Map;
27
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070028/**
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
43public 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
Mathew Inwood42afea22018-08-16 19:18:28 +010054 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070055 int httpStatusCode;
Mathew Inwood42afea22018-08-16 19:18:28 +010056 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070057 long contentLength;
Mathew Inwood42afea22018-08-16 19:18:28 +010058 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070059 long expires;
Mathew Inwood42afea22018-08-16 19:18:28 +010060 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070061 String expiresString;
Mathew Inwood42afea22018-08-16 19:18:28 +010062 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070063 String localPath;
Mathew Inwood42afea22018-08-16 19:18:28 +010064 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070065 String lastModified;
Mathew Inwood42afea22018-08-16 19:18:28 +010066 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070067 String etag;
Mathew Inwood42afea22018-08-16 19:18:28 +010068 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070069 String mimeType;
Mathew Inwood42afea22018-08-16 19:18:28 +010070 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070071 String location;
Mathew Inwood42afea22018-08-16 19:18:28 +010072 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070073 String encoding;
Mathew Inwood42afea22018-08-16 19:18:28 +010074 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070075 String contentdisposition;
Mathew Inwood42afea22018-08-16 19:18:28 +010076 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070077 String crossDomain;
78
79 // these fields are NOT saved to the database
Mathew Inwood42afea22018-08-16 19:18:28 +010080 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070081 InputStream inStream;
Mathew Inwood42afea22018-08-16 19:18:28 +010082 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070083 OutputStream outStream;
Mathew Inwood42afea22018-08-16 19:18:28 +010084 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070085 File outFile;
86
87 /**
88 * Gets the status code of this cache entry.
89 *
90 * @return the status code of this cache entry
91 */
Mathew Inwood42afea22018-08-16 19:18:28 +010092 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -070093 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100102 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700103 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100114 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700115 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100125 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700126 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100136 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700137 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100147 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700148 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100157 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700158 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100167 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700168 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100178 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700179 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100188 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700189 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100200 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700201 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100212 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700213 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()!
Mathew Inwood42afea22018-08-16 19:18:28 +0100225 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700226 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100236 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700237 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 */
Mathew Inwood42afea22018-08-16 19:18:28 +0100246 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700247 public void setEncoding(String encoding) {
248 this.encoding = encoding;
249 }
250
251 /**
252 * @hide
253 */
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
Nate Fischer0a6140d2017-09-05 12:37:49 -0700265 * @deprecated This method no longer has any effect and always returns {@code null}.
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700266 */
267 @Deprecated
Nate Fischer3442c742017-09-08 17:02:00 -0700268 @Nullable
Mathew Inwood42afea22018-08-16 19:18:28 +0100269 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700270 public static File getCacheFileBaseDir() {
271 return null;
272 }
273
274 /**
275 * Gets whether the HTTP cache is disabled.
276 *
Nate Fischer0a6140d2017-09-05 12:37:49 -0700277 * @return {@code true} if the HTTP cache is disabled
278 * @deprecated This method no longer has any effect and always returns {@code false}.
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700279 */
280 @Deprecated
Mathew Inwood42afea22018-08-16 19:18:28 +0100281 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700282 public static boolean cacheDisabled() {
283 return false;
284 }
285
286 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -0700287 * Starts a cache transaction. Returns {@code true} if this is the only running
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700288 * transaction. Otherwise, this transaction is nested inside currently
Nate Fischer0a6140d2017-09-05 12:37:49 -0700289 * running transactions and {@code false} is returned.
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700290 *
Nate Fischer0a6140d2017-09-05 12:37:49 -0700291 * @return {@code true} if this is the only running transaction
292 * @deprecated This method no longer has any effect and always returns {@code false}.
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700293 */
294 @Deprecated
Mathew Inwood42afea22018-08-16 19:18:28 +0100295 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700296 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 *
Nate Fischer0a6140d2017-09-05 12:37:49 -0700304 * @return {@code true} if this was the only running transaction
305 * @deprecated This method no longer has any effect and always returns {@code false}.
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700306 */
307 @Deprecated
Mathew Inwood42afea22018-08-16 19:18:28 +0100308 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700309 public static boolean endCacheTransaction() {
310 return false;
311 }
312
313 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -0700314 * Gets the cache entry for the specified URL, or {@code null} if none is found.
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700315 * 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
Nate Fischer0a6140d2017-09-05 12:37:49 -0700324 * @deprecated This method no longer has any effect and always returns {@code null}.
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700325 */
326 @Deprecated
Nate Fischer3442c742017-09-08 17:02:00 -0700327 @Nullable
Mathew Inwood42afea22018-08-16 19:18:28 +0100328 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700329 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
Mathew Inwood42afea22018-08-16 19:18:28 +0100343 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700344 public static void saveCacheFile(String url, CacheResult cacheResult) {
345 saveCacheFile(url, 0, cacheResult);
346 }
347
Mathew Inwood42afea22018-08-16 19:18:28 +0100348 @UnsupportedAppUsage
Jonathan Dixon74fc73f2013-10-18 16:51:53 -0700349 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}