1/* 2 * Copyright (C) 2008 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 19import android.net.http.EventHandler; 20import android.net.http.Headers; 21import android.net.Uri; 22 23/** 24 * This class is a concrete implementation of StreamLoader that loads 25 * "content:" URIs 26 */ 27class ContentLoader extends StreamLoader { 28 29 private String mUrl; 30 private String mContentType; 31 32 /** 33 * Construct a ContentLoader with the specified content URI 34 * 35 * @param rawUrl "content:" url pointing to content to be loaded. This url 36 * is the same url passed in to the WebView. 37 * @param loadListener LoadListener to pass the content to 38 */ 39 ContentLoader(String rawUrl, LoadListener loadListener) { 40 super(loadListener); 41 42 /* strip off mimetype */ 43 int mimeIndex = rawUrl.lastIndexOf('?'); 44 if (mimeIndex != -1) { 45 mUrl = rawUrl.substring(0, mimeIndex); 46 mContentType = rawUrl.substring(mimeIndex + 1); 47 } else { 48 mUrl = rawUrl; 49 } 50 51 } 52 53 private String errString(Exception ex) { 54 String exMessage = ex.getMessage(); 55 String errString = mContext.getString( 56 com.android.internal.R.string.httpErrorFileNotFound); 57 if (exMessage != null) { 58 errString += " " + exMessage; 59 } 60 return errString; 61 } 62 63 @Override 64 protected boolean setupStreamAndSendStatus() { 65 Uri uri = Uri.parse(mUrl); 66 if (uri == null) { 67 mLoadListener.error( 68 EventHandler.FILE_NOT_FOUND_ERROR, 69 mContext.getString( 70 com.android.internal.R.string.httpErrorBadUrl) + 71 " " + mUrl); 72 return false; 73 } 74 75 try { 76 mDataStream = mContext.getContentResolver().openInputStream(uri); 77 mLoadListener.status(1, 1, 200, "OK"); 78 } catch (java.io.FileNotFoundException ex) { 79 mLoadListener.error(EventHandler.FILE_NOT_FOUND_ERROR, errString(ex)); 80 return false; 81 } catch (RuntimeException ex) { 82 // readExceptionWithFileNotFoundExceptionFromParcel in DatabaseUtils 83 // can throw a serial of RuntimeException. Catch them all here. 84 mLoadListener.error(EventHandler.FILE_ERROR, errString(ex)); 85 return false; 86 } 87 return true; 88 } 89 90 @Override 91 protected void buildHeaders(Headers headers) { 92 if (mContentType != null) { 93 headers.setContentType("text/html"); 94 } 95 // content can change, we don't want WebKit to cache it 96 headers.setCacheControl("no-store, no-cache"); 97 } 98} 99