2 * Copyright (C) 2008 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.EventHandler;
20 import android.net.http.Headers;
21 import android.net.Uri;
24 * This class is a concrete implementation of StreamLoader that loads
27 class ContentLoader extends StreamLoader {
30 private String mContentType;
33 * Construct a ContentLoader with the specified content URI
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
39 ContentLoader(String rawUrl, LoadListener loadListener) {
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);
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;
64 protected boolean setupStreamAndSendStatus() {
65 Uri uri = Uri.parse(mUrl);
68 EventHandler.FILE_NOT_FOUND_ERROR,
70 com.android.internal.R.string.httpErrorBadUrl) +
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));
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));
91 protected void buildHeaders(Headers headers) {
92 if (mContentType != null) {
93 headers.setContentType("text/html");
95 // content can change, we don't want WebKit to cache it
96 headers.setCacheControl("no-store, no-cache");