The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Patrick Scott | 8af3cfc | 2010-02-02 11:19:25 -0500 | [diff] [blame] | 19 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.net.http.EventHandler; |
| 21 | import android.net.http.Headers; |
| 22 | import android.os.Handler; |
| 23 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | /** |
| 29 | * This abstract class is used for all content loaders that rely on streaming |
| 30 | * content into the rendering engine loading framework. |
| 31 | * |
| 32 | * The class implements a state machine to load the content into the frame in |
| 33 | * a similar manor to the way content arrives from the network. The class uses |
| 34 | * messages to move from one state to the next, which enables async. loading of |
| 35 | * the streamed content. |
| 36 | * |
| 37 | * Classes that inherit from this class must implement two methods, the first |
| 38 | * method is used to setup the InputStream and notify the loading framework if |
| 39 | * it can load it's content. The other method allows the derived class to add |
| 40 | * additional HTTP headers to the response. |
| 41 | * |
| 42 | * By default, content loaded with a StreamLoader is marked with a HTTP header |
| 43 | * that indicates the content should not be cached. |
| 44 | * |
| 45 | */ |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 46 | abstract class StreamLoader implements Handler.Callback { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | private static final int MSG_STATUS = 100; // Send status to loader |
| 49 | private static final int MSG_HEADERS = 101; // Send headers to loader |
| 50 | private static final int MSG_DATA = 102; // Send data to loader |
| 51 | private static final int MSG_END = 103; // Send endData to loader |
| 52 | |
Patrick Scott | 8af3cfc | 2010-02-02 11:19:25 -0500 | [diff] [blame] | 53 | protected final Context mContext; |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 54 | protected final LoadListener mLoadListener; // loader class |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | protected InputStream mDataStream; // stream to read data from |
| 56 | protected long mContentLength; // content length of data |
| 57 | private byte [] mData; // buffer to pass data to loader with. |
| 58 | |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 59 | // Handler which will be initialized in the thread where load() is called. |
| 60 | private Handler mHandler; |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Constructor. Although this class calls the LoadListener, it only calls |
| 64 | * the EventHandler Interface methods. LoadListener concrete class is used |
| 65 | * to avoid the penality of calling an interface. |
| 66 | * |
| 67 | * @param loadlistener The LoadListener to call with the data. |
| 68 | */ |
| 69 | StreamLoader(LoadListener loadlistener) { |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 70 | mLoadListener = loadlistener; |
Patrick Scott | 8af3cfc | 2010-02-02 11:19:25 -0500 | [diff] [blame] | 71 | mContext = loadlistener.getContext(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * This method is called when the derived class should setup mDataStream, |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 76 | * and call mLoadListener.status() to indicate that the load can occur. If it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | * fails to setup, it should still call status() with the error code. |
| 78 | * |
| 79 | * @return true if stream was successfully setup |
| 80 | */ |
| 81 | protected abstract boolean setupStreamAndSendStatus(); |
| 82 | |
| 83 | /** |
| 84 | * This method is called when the headers are about to be sent to the |
| 85 | * load framework. The derived class has the opportunity to add addition |
| 86 | * headers. |
| 87 | * |
| 88 | * @param headers Map of HTTP headers that will be sent to the loader. |
| 89 | */ |
| 90 | abstract protected void buildHeaders(Headers headers); |
| 91 | |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 92 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | * Calling this method starts the load of the content for this StreamLoader. |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 94 | * This method simply creates a Handler in the current thread and posts a |
| 95 | * message to send the status and returns immediately. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | */ |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 97 | final void load() { |
| 98 | synchronized (this) { |
| 99 | if (mHandler == null) { |
| 100 | mHandler = new Handler(this); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (!mLoadListener.isSynchronous()) { |
| 105 | mHandler.sendEmptyMessage(MSG_STATUS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | } else { |
| 107 | // Load the stream synchronously. |
| 108 | if (setupStreamAndSendStatus()) { |
| 109 | // We were able to open the stream, create the array |
| 110 | // to pass data to the loader |
| 111 | mData = new byte[8192]; |
| 112 | sendHeaders(); |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 113 | while (!sendData() && !mLoadListener.cancelled()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | closeStreamAndSendEndData(); |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 115 | mLoadListener.loadSynchronousMessages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 120 | public boolean handleMessage(Message msg) { |
| 121 | if (mLoadListener.isSynchronous()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | throw new AssertionError(); |
| 123 | } |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 124 | if (mLoadListener.cancelled()) { |
Patrick Scott | e82dc42 | 2009-04-29 10:21:57 -0400 | [diff] [blame] | 125 | closeStreamAndSendEndData(); |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 126 | return true; |
Patrick Scott | e82dc42 | 2009-04-29 10:21:57 -0400 | [diff] [blame] | 127 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | switch(msg.what) { |
| 129 | case MSG_STATUS: |
| 130 | if (setupStreamAndSendStatus()) { |
| 131 | // We were able to open the stream, create the array |
| 132 | // to pass data to the loader |
| 133 | mData = new byte[8192]; |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 134 | mHandler.sendEmptyMessage(MSG_HEADERS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | } |
| 136 | break; |
| 137 | case MSG_HEADERS: |
| 138 | sendHeaders(); |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 139 | mHandler.sendEmptyMessage(MSG_DATA); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | break; |
| 141 | case MSG_DATA: |
| 142 | if (sendData()) { |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 143 | mHandler.sendEmptyMessage(MSG_END); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } else { |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 145 | mHandler.sendEmptyMessage(MSG_DATA); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
| 147 | break; |
| 148 | case MSG_END: |
| 149 | closeStreamAndSendEndData(); |
| 150 | break; |
| 151 | default: |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 152 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 154 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Construct the headers and pass them to the EventHandler. |
| 159 | */ |
| 160 | private void sendHeaders() { |
| 161 | Headers headers = new Headers(); |
| 162 | if (mContentLength > 0) { |
| 163 | headers.setContentLength(mContentLength); |
| 164 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | buildHeaders(headers); |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 166 | mLoadListener.headers(headers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Read data from the stream and pass it to the EventHandler. |
| 171 | * If an error occurs reading the stream, then an error is sent to the |
| 172 | * EventHandler, and moves onto the next state - end of data. |
| 173 | * @return True if all the data has been read. False if sendData should be |
| 174 | * called again. |
| 175 | */ |
| 176 | private boolean sendData() { |
| 177 | if (mDataStream != null) { |
| 178 | try { |
| 179 | int amount = mDataStream.read(mData); |
| 180 | if (amount > 0) { |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 181 | mLoadListener.data(mData, amount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | } catch (IOException ex) { |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 185 | mLoadListener.error(EventHandler.FILE_ERROR, ex.getMessage()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Close the stream and inform the EventHandler that load is complete. |
| 193 | */ |
| 194 | private void closeStreamAndSendEndData() { |
| 195 | if (mDataStream != null) { |
| 196 | try { |
| 197 | mDataStream.close(); |
| 198 | } catch (IOException ex) { |
| 199 | // ignore. |
| 200 | } |
| 201 | } |
Grace Kloba | ac75f56 | 2010-02-03 10:24:06 -0800 | [diff] [blame] | 202 | mLoadListener.endData(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |