Home | History | Annotate | Download | only in webkit
      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 import android.webkit.JniUtil;
     22 
     23 /**
     24  * This class is a concrete implementation of StreamLoader that uses a
     25  * CacheResult as the source for the stream. The CacheResult stored mimetype
     26  * and encoding is added to the HTTP response headers.
     27  */
     28 class CacheLoader extends StreamLoader {
     29 
     30     CacheManager.CacheResult mCacheResult;  // Content source
     31 
     32     /**
     33      * Constructs a CacheLoader for use when loading content from the cache.
     34      *
     35      * @param loadListener LoadListener to pass the content to
     36      * @param result CacheResult used as the source for the content.
     37      */
     38     CacheLoader(LoadListener loadListener, CacheManager.CacheResult result) {
     39         super(loadListener);
     40 
     41         assert !JniUtil.useChromiumHttpStack();
     42 
     43         mCacheResult = result;
     44     }
     45 
     46     @Override
     47     protected boolean setupStreamAndSendStatus() {
     48         mDataStream = mCacheResult.inStream;
     49         mContentLength = mCacheResult.contentLength;
     50         mLoadListener.status(1, 1, mCacheResult.httpStatusCode, "OK");
     51         return true;
     52     }
     53 
     54     @Override
     55     protected void buildHeaders(Headers headers) {
     56         StringBuilder sb = new StringBuilder(mCacheResult.mimeType);
     57         if (!TextUtils.isEmpty(mCacheResult.encoding)) {
     58             sb.append(';');
     59             sb.append(mCacheResult.encoding);
     60         }
     61         headers.setContentType(sb.toString());
     62 
     63         if (!TextUtils.isEmpty(mCacheResult.location)) {
     64             headers.setLocation(mCacheResult.location);
     65         }
     66 
     67         if (!TextUtils.isEmpty(mCacheResult.expiresString)) {
     68             headers.setExpires(mCacheResult.expiresString);
     69         }
     70 
     71         if (!TextUtils.isEmpty(mCacheResult.contentdisposition)) {
     72             headers.setContentDisposition(mCacheResult.contentdisposition);
     73         }
     74 
     75         if (!TextUtils.isEmpty(mCacheResult.crossDomain)) {
     76             headers.setXPermittedCrossDomainPolicies(mCacheResult.crossDomain);
     77         }
     78     }
     79 }
     80