blob: d13210aa2a0b84c353e965a7ecada217fde88261 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.net.http.EventHandler;
20import android.net.http.Headers;
21import android.net.Uri;
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023/**
24 * This class is a concrete implementation of StreamLoader that loads
25 * "content:" URIs
26 */
27class ContentLoader extends StreamLoader {
28
29 private String mUrl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 */
Patrick Scott8af3cfc2010-02-02 11:19:25 -050039 ContentLoader(String rawUrl, LoadListener loadListener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 super(loadListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
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
Cary Clark4e441b72009-09-01 11:40:12 -040053 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 @Override
64 protected boolean setupStreamAndSendStatus() {
65 Uri uri = Uri.parse(mUrl);
66 if (uri == null) {
Grace Klobaac75f562010-02-03 10:24:06 -080067 mLoadListener.error(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 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);
Grace Klobaac75f562010-02-03 10:24:06 -080077 mLoadListener.status(1, 1, 200, "OK");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 } catch (java.io.FileNotFoundException ex) {
Grace Klobaac75f562010-02-03 10:24:06 -080079 mLoadListener.error(EventHandler.FILE_NOT_FOUND_ERROR, errString(ex));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 return false;
81 } catch (RuntimeException ex) {
82 // readExceptionWithFileNotFoundExceptionFromParcel in DatabaseUtils
83 // can throw a serial of RuntimeException. Catch them all here.
Grace Klobaac75f562010-02-03 10:24:06 -080084 mLoadListener.error(EventHandler.FILE_ERROR, errString(ex));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 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 }
Grace Klobac3fa0982009-08-21 17:30:44 -070095 // content can change, we don't want WebKit to cache it
Grace Klobae0e37bc2009-03-24 17:39:42 -070096 headers.setCacheControl("no-store, no-cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098}