blob: 7bcd50dd46776a3c84d5d5e657ae49f16286edf1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.webkit;
18
Patrick Scott8af3cfc2010-02-02 11:19:25 -050019import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.net.http.EventHandler;
21import android.net.http.Headers;
22import android.os.Handler;
23import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25import java.io.IOException;
26import java.io.InputStream;
27
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028/**
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 Klobaac75f562010-02-03 10:24:06 -080046abstract class StreamLoader implements Handler.Callback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
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 Scott8af3cfc2010-02-02 11:19:25 -050053 protected final Context mContext;
Grace Klobaac75f562010-02-03 10:24:06 -080054 protected final LoadListener mLoadListener; // loader class
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 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 Klobaac75f562010-02-03 10:24:06 -080059 // Handler which will be initialized in the thread where load() is called.
60 private Handler mHandler;
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 /**
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 Klobaac75f562010-02-03 10:24:06 -080070 mLoadListener = loadlistener;
Patrick Scott8af3cfc2010-02-02 11:19:25 -050071 mContext = loadlistener.getContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 }
73
74 /**
75 * This method is called when the derived class should setup mDataStream,
Grace Klobaac75f562010-02-03 10:24:06 -080076 * and call mLoadListener.status() to indicate that the load can occur. If it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 * 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 Klobaac75f562010-02-03 10:24:06 -080092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * Calling this method starts the load of the content for this StreamLoader.
Grace Klobaac75f562010-02-03 10:24:06 -080094 * 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 Project9066cfe2009-03-03 19:31:44 -080096 */
Grace Klobaac75f562010-02-03 10:24:06 -080097 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 Project9066cfe2009-03-03 19:31:44 -0800106 } 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 Klobaac75f562010-02-03 10:24:06 -0800113 while (!sendData() && !mLoadListener.cancelled());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 closeStreamAndSendEndData();
Grace Klobaac75f562010-02-03 10:24:06 -0800115 mLoadListener.loadSynchronousMessages();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
117 }
118 }
119
Grace Klobaac75f562010-02-03 10:24:06 -0800120 public boolean handleMessage(Message msg) {
121 if (mLoadListener.isSynchronous()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 throw new AssertionError();
123 }
Grace Klobaac75f562010-02-03 10:24:06 -0800124 if (mLoadListener.cancelled()) {
Patrick Scotte82dc422009-04-29 10:21:57 -0400125 closeStreamAndSendEndData();
Grace Klobaac75f562010-02-03 10:24:06 -0800126 return true;
Patrick Scotte82dc422009-04-29 10:21:57 -0400127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 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 Klobaac75f562010-02-03 10:24:06 -0800134 mHandler.sendEmptyMessage(MSG_HEADERS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136 break;
137 case MSG_HEADERS:
138 sendHeaders();
Grace Klobaac75f562010-02-03 10:24:06 -0800139 mHandler.sendEmptyMessage(MSG_DATA);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 break;
141 case MSG_DATA:
142 if (sendData()) {
Grace Klobaac75f562010-02-03 10:24:06 -0800143 mHandler.sendEmptyMessage(MSG_END);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 } else {
Grace Klobaac75f562010-02-03 10:24:06 -0800145 mHandler.sendEmptyMessage(MSG_DATA);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
147 break;
148 case MSG_END:
149 closeStreamAndSendEndData();
150 break;
151 default:
Grace Klobaac75f562010-02-03 10:24:06 -0800152 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 }
Grace Klobaac75f562010-02-03 10:24:06 -0800154 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
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 Project9066cfe2009-03-03 19:31:44 -0800165 buildHeaders(headers);
Grace Klobaac75f562010-02-03 10:24:06 -0800166 mLoadListener.headers(headers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
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 Klobaac75f562010-02-03 10:24:06 -0800181 mLoadListener.data(mData, amount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 return false;
183 }
184 } catch (IOException ex) {
Grace Klobaac75f562010-02-03 10:24:06 -0800185 mLoadListener.error(EventHandler.FILE_ERROR, ex.getMessage());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
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 Klobaac75f562010-02-03 10:24:06 -0800202 mLoadListener.endData();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204}