The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.webkit; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.net.http.EventHandler; |
| 20 | import android.net.http.Headers; |
| 21 | import android.net.Uri; |
| 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | /** |
| 24 | * This class is a concrete implementation of StreamLoader that loads |
| 25 | * "content:" URIs |
| 26 | */ |
| 27 | class ContentLoader extends StreamLoader { |
| 28 | |
| 29 | private String mUrl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | */ |
Patrick Scott | 8af3cfc | 2010-02-02 11:19:25 -0500 | [diff] [blame] | 39 | ContentLoader(String rawUrl, LoadListener loadListener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | super(loadListener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Cary Clark | 4e441b7 | 2009-09-01 11:40:12 -0400 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | @Override |
| 64 | protected boolean setupStreamAndSendStatus() { |
| 65 | Uri uri = Uri.parse(mUrl); |
| 66 | if (uri == null) { |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 67 | mLoadListener.error( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 77 | mLoadListener.status(1, 1, 200, "OK"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | } catch (java.io.FileNotFoundException ex) { |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 79 | mLoadListener.error(EventHandler.FILE_NOT_FOUND_ERROR, errString(ex)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | return false; |
| 81 | } catch (RuntimeException ex) { |
| 82 | // readExceptionWithFileNotFoundExceptionFromParcel in DatabaseUtils |
| 83 | // can throw a serial of RuntimeException. Catch them all here. |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 84 | mLoadListener.error(EventHandler.FILE_ERROR, errString(ex)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |
Grace Kloba | c3fa098 | 2009-08-21 17:30:44 -0700 | [diff] [blame] | 95 | // content can change, we don't want WebKit to cache it |
Grace Kloba | e0e37bc | 2009-03-24 17:39:42 -0700 | [diff] [blame] | 96 | headers.setCacheControl("no-store, no-cache"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |