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.content.Context; 20import android.net.http.EventHandler; 21import android.net.http.Headers; 22import android.net.Uri; 23 24import java.io.File; 25import java.io.FileInputStream; 26 27/** 28 * This class is a concrete implementation of StreamLoader that loads 29 * "content:" URIs 30 */ 31class ContentLoader extends StreamLoader { 32 33 private String mUrl; 34 private Context mContext; 35 private String mContentType; 36 37 /** 38 * Construct a ContentLoader with the specified content URI 39 * 40 * @param rawUrl "content:" url pointing to content to be loaded. This url 41 * is the same url passed in to the WebView. 42 * @param loadListener LoadListener to pass the content to 43 * @param context Context to use to access the asset. 44 */ 45 ContentLoader(String rawUrl, LoadListener loadListener, Context context) { 46 super(loadListener); 47 mContext = context; 48 49 /* strip off mimetype */ 50 int mimeIndex = rawUrl.lastIndexOf('?'); 51 if (mimeIndex != -1) { 52 mUrl = rawUrl.substring(0, mimeIndex); 53 mContentType = rawUrl.substring(mimeIndex + 1); 54 } else { 55 mUrl = rawUrl; 56 } 57 58 } 59 60 @Override 61 protected boolean setupStreamAndSendStatus() { 62 Uri uri = Uri.parse(mUrl); 63 if (uri == null) { 64 mHandler.error( 65 EventHandler.FILE_NOT_FOUND_ERROR, 66 mContext.getString( 67 com.android.internal.R.string.httpErrorBadUrl) + 68 " " + mUrl); 69 return false; 70 } 71 72 try { 73 mDataStream = mContext.getContentResolver().openInputStream(uri); 74 mHandler.status(1, 1, 0, "OK"); 75 } catch (java.io.FileNotFoundException ex) { 76 mHandler.error( 77 EventHandler.FILE_NOT_FOUND_ERROR, 78 mContext.getString( 79 com.android.internal.R.string.httpErrorFileNotFound) + 80 " " + ex.getMessage()); 81 return false; 82 83 } catch (java.io.IOException ex) { 84 mHandler.error( 85 EventHandler.FILE_ERROR, 86 mContext.getString( 87 com.android.internal.R.string.httpErrorFileNotFound) + 88 " " + ex.getMessage()); 89 return false; 90 } catch (RuntimeException ex) { 91 // readExceptionWithFileNotFoundExceptionFromParcel in DatabaseUtils 92 // can throw a serial of RuntimeException. Catch them all here. 93 mHandler.error( 94 EventHandler.FILE_ERROR, 95 mContext.getString( 96 com.android.internal.R.string.httpErrorFileNotFound) + 97 " " + ex.getMessage()); 98 return false; 99 } 100 return true; 101 } 102 103 @Override 104 protected void buildHeaders(Headers headers) { 105 if (mContentType != null) { 106 headers.setContentType("text/html"); 107 } 108 // override the cache-control header set by StreamLoader as content can 109 // change, we don't want WebKit to cache it 110 headers.setCacheControl("no-store, no-cache"); 111 } 112 113 /** 114 * Construct a ContentLoader and instruct it to start loading. 115 * 116 * @param url "content:" url pointing to content to be loaded 117 * @param loadListener LoadListener to pass the content to 118 * @param context Context to use to access the asset. 119 */ 120 public static void requestUrl(String url, LoadListener loadListener, 121 Context context) { 122 ContentLoader loader = new ContentLoader(url, loadListener, context); 123 loader.load(); 124 } 125 126} 127