OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / core / java / android / webkit / CacheLoader.java
1 /*
2  * Copyright (C) 2007 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.net.http.Headers;
20 import android.text.TextUtils;
21
22 /**
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.
26  */
27 class CacheLoader extends StreamLoader {
28
29     CacheManager.CacheResult mCacheResult;  // Content source
30
31     /**
32      * Constructs a CacheLoader for use when loading content from the cache.
33      *
34      * @param loadListener LoadListener to pass the content to
35      * @param result CacheResult used as the source for the content.
36      */
37     CacheLoader(LoadListener loadListener, CacheManager.CacheResult result) {
38         super(loadListener);
39         mCacheResult = result;
40     }
41
42     @Override
43     protected boolean setupStreamAndSendStatus() {
44         mDataStream = mCacheResult.inStream;
45         mContentLength = mCacheResult.contentLength;
46         mLoadListener.status(1, 1, mCacheResult.httpStatusCode, "OK");
47         return true;
48     }
49
50     @Override
51     protected void buildHeaders(Headers headers) {
52         StringBuilder sb = new StringBuilder(mCacheResult.mimeType);
53         if (!TextUtils.isEmpty(mCacheResult.encoding)) {
54             sb.append(';');
55             sb.append(mCacheResult.encoding);
56         }
57         headers.setContentType(sb.toString());
58
59         if (!TextUtils.isEmpty(mCacheResult.location)) {
60             headers.setLocation(mCacheResult.location);
61         }
62
63         if (!TextUtils.isEmpty(mCacheResult.expiresString)) {
64             headers.setExpires(mCacheResult.expiresString);
65         }
66
67         if (!TextUtils.isEmpty(mCacheResult.contentdisposition)) {
68             headers.setContentDisposition(mCacheResult.contentdisposition);
69         }
70
71         if (!TextUtils.isEmpty(mCacheResult.crossDomain)) {
72             headers.setXPermittedCrossDomainPolicies(mCacheResult.crossDomain);
73         }
74     }
75 }