2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.webkit;
19 import android.net.http.Headers;
20 import android.text.TextUtils;
23 * This class is a concrete implementation of StreamLoader that uses a
24 * CacheResult as the source for the stream. The CacheResult stored mimetype
25 * and encoding is added to the HTTP response headers.
27 class CacheLoader extends StreamLoader {
29 CacheManager.CacheResult mCacheResult; // Content source
32 * Constructs a CacheLoader for use when loading content from the cache.
34 * @param loadListener LoadListener to pass the content to
35 * @param result CacheResult used as the source for the content.
37 CacheLoader(LoadListener loadListener, CacheManager.CacheResult result) {
39 mCacheResult = result;
43 protected boolean setupStreamAndSendStatus() {
44 mDataStream = mCacheResult.inStream;
45 mContentLength = mCacheResult.contentLength;
46 mLoadListener.status(1, 1, mCacheResult.httpStatusCode, "OK");
51 protected void buildHeaders(Headers headers) {
52 StringBuilder sb = new StringBuilder(mCacheResult.mimeType);
53 if (!TextUtils.isEmpty(mCacheResult.encoding)) {
55 sb.append(mCacheResult.encoding);
57 headers.setContentType(sb.toString());
59 if (!TextUtils.isEmpty(mCacheResult.location)) {
60 headers.setLocation(mCacheResult.location);
63 if (!TextUtils.isEmpty(mCacheResult.expiresString)) {
64 headers.setExpires(mCacheResult.expiresString);
67 if (!TextUtils.isEmpty(mCacheResult.contentdisposition)) {
68 headers.setContentDisposition(mCacheResult.contentdisposition);
71 if (!TextUtils.isEmpty(mCacheResult.crossDomain)) {
72 headers.setXPermittedCrossDomainPolicies(mCacheResult.crossDomain);