Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.net.urlconnection; | |
| 6 | |
| 7 import org.chromium.net.ExtendedResponseInfo; | |
| 8 import org.chromium.net.ResponseInfo; | |
| 9 import org.chromium.net.UrlRequest; | |
| 10 import org.chromium.net.UrlRequestContext; | |
| 11 import org.chromium.net.UrlRequestException; | |
| 12 import org.chromium.net.UrlRequestListener; | |
| 13 | |
| 14 import java.io.IOException; | |
| 15 import java.io.InputStream; | |
| 16 import java.net.HttpURLConnection; | |
| 17 import java.net.URL; | |
| 18 import java.nio.ByteBuffer; | |
| 19 import java.util.concurrent.Executor; | |
| 20 import java.util.concurrent.RejectedExecutionException; | |
| 21 | |
| 22 /** | |
| 23 * An implementation of HttpURLConnection that uses Cronet to send requests and | |
| 24 * receive response. This class inherits a {@code connected} field from the | |
| 25 * superclass. That field indicates whether a connection has ever been | |
| 26 * attempted. | |
| 27 */ | |
| 28 public class CronetHttpURLConnection extends HttpURLConnection { | |
| 29 private final UrlRequestContext mUrlRequestContext; | |
| 30 private final MessageLoop mMessageLoop; | |
| 31 private final UrlRequest mRequest; | |
| 32 | |
| 33 private CronetInputStream mInputStream; | |
| 34 private ResponseInfo mResponseInfo; | |
| 35 private ByteBuffer mResponseByteBuffer; | |
| 36 | |
| 37 protected CronetHttpURLConnection(URL url, | |
| 38 UrlRequestContext urlRequestContext) { | |
| 39 super(url); | |
| 40 mUrlRequestContext = urlRequestContext; | |
| 41 mMessageLoop = new MessageLoop(); | |
| 42 mRequest = mUrlRequestContext.createRequest(url.toString(), | |
| 43 new CronetUrlRequestListener(), new MessageLoopExecutor()); | |
| 44 mInputStream = new CronetInputStream(this); | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * Wrapper executor class which posts tasks to message loop. | |
| 49 */ | |
| 50 private class MessageLoopExecutor implements Executor { | |
| 51 public MessageLoopExecutor() { | |
| 52 } | |
| 53 | |
| 54 @Override | |
| 55 public void execute(Runnable command) | |
| 56 throws RejectedExecutionException { | |
| 57 try { | |
| 58 mMessageLoop.postTask(command); | |
|
mef
2014/12/01 18:28:54
I feel that referencing mMessageLoop from the pare
xunjieli
2014/12/01 19:19:52
Done.
| |
| 59 } catch (InterruptedException e) { | |
| 60 throw new RejectedExecutionException(e); | |
| 61 } | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Opens a connection to the resource. If the connect method is called when | |
| 67 * the connection has already been opened (indicated by the connected field | |
| 68 * having the value true), the call is ignored. | |
| 69 */ | |
| 70 @Override | |
| 71 public void connect() throws IOException { | |
| 72 if (connected) { | |
| 73 return; | |
| 74 } | |
| 75 connected = true; | |
| 76 mRequest.start(); | |
| 77 // Blocks until onResponseStarted or onFailed is called. | |
| 78 startMessageLoop(); | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * Releases this connection so that its resources may be either reused or | |
| 83 * closed. | |
| 84 */ | |
| 85 @Override | |
| 86 public void disconnect() { | |
| 87 // Disconnect before connection is made should have no effect. | |
| 88 if (connected) { | |
| 89 try { | |
| 90 mInputStream.close(); | |
| 91 } catch (IOException e) { | |
| 92 e.printStackTrace(); | |
| 93 } | |
| 94 mInputStream = null; | |
| 95 mRequest.cancel(); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 /** | |
| 100 * Returns the response message returned by the remote HTTP server. | |
| 101 */ | |
| 102 @Override | |
| 103 public String getResponseMessage() throws IOException { | |
| 104 connect(); | |
| 105 if (mResponseInfo == null) { | |
| 106 return ""; | |
| 107 } | |
| 108 return mResponseInfo.getHttpStatusText(); | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Returns the response code returned by the remote HTTP server. | |
| 113 */ | |
| 114 @Override | |
| 115 public int getResponseCode() throws IOException { | |
| 116 connect(); | |
| 117 if (mResponseInfo == null) { | |
| 118 return 0; | |
| 119 } | |
| 120 return mResponseInfo.getHttpStatusCode(); | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Returns an InputStream for reading data from the resource pointed by this | |
| 125 * URLConnection. | |
| 126 */ | |
| 127 @Override | |
| 128 public InputStream getInputStream() throws IOException { | |
| 129 connect(); | |
| 130 return mInputStream; | |
| 131 } | |
| 132 | |
| 133 /** | |
| 134 * Adds the given property to the request header. | |
| 135 */ | |
| 136 @Override | |
| 137 public final void addRequestProperty(String key, String value) { | |
| 138 // Note that Cronet right now does not allow setting multiple headers | |
| 139 // of the same key, see crbug.com/432719 for more details. | |
| 140 setRequestProperty(key, value); | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * Sets the value of the specified request header field. | |
| 145 */ | |
| 146 @Override | |
| 147 public final void setRequestProperty(String key, String value) { | |
| 148 if (connected) { | |
| 149 throw new IllegalStateException( | |
| 150 "Cannot set request property after connection is made"); | |
| 151 } | |
| 152 mRequest.addHeader(key, value); | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * Returns whether this connection uses a proxy server. | |
| 157 */ | |
| 158 @Override | |
| 159 public boolean usingProxy() { | |
| 160 // TODO(xunjieli): implement this. | |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 /** | |
| 165 * Used by {@link CronetInputStream} to get more data from the network | |
| 166 * stack. This should only be called after the request has started. Note | |
| 167 * that this call might block if there isn't any more data to be read. | |
| 168 */ | |
| 169 ByteBuffer getMoreData() throws IOException { | |
| 170 mResponseByteBuffer = null; | |
| 171 startMessageLoop(); | |
| 172 return mResponseByteBuffer; | |
| 173 } | |
| 174 | |
| 175 private class CronetUrlRequestListener implements UrlRequestListener { | |
| 176 public CronetUrlRequestListener() { | |
| 177 } | |
| 178 | |
| 179 @Override | |
| 180 public void onResponseStarted(UrlRequest request, ResponseInfo info) { | |
| 181 mResponseInfo = info; | |
| 182 // Quits the message loop since we have the headers now. | |
| 183 quitMessageLoop(); | |
| 184 } | |
| 185 | |
| 186 @Override | |
| 187 public void onDataReceived(UrlRequest request, ResponseInfo info, | |
| 188 ByteBuffer byteBuffer) { | |
| 189 mResponseInfo = info; | |
| 190 mResponseByteBuffer = ByteBuffer.allocate(byteBuffer.capacity()); | |
| 191 mResponseByteBuffer.put(byteBuffer); | |
| 192 mResponseByteBuffer.flip(); | |
| 193 quitMessageLoop(); | |
| 194 } | |
| 195 | |
| 196 @Override | |
| 197 public void onRedirect(UrlRequest request, ResponseInfo info, | |
| 198 String newLocationUrl) { | |
| 199 // TODO(xunjieli): Handle redirect and test it. | |
| 200 } | |
| 201 | |
| 202 @Override | |
| 203 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) { | |
| 204 mResponseInfo = info.getResponseInfo(); | |
| 205 setResponseDataCompleted(); | |
| 206 } | |
| 207 | |
| 208 @Override | |
| 209 public void onFailed(UrlRequest request, ResponseInfo info, | |
| 210 UrlRequestException exception) { | |
| 211 // TODO(xunjieli): Handle failure. | |
| 212 setResponseDataCompleted(); | |
| 213 } | |
| 214 | |
| 215 /** | |
| 216 * Notifies {@link #mInputStream} that transferring of response data has | |
| 217 * completed. | |
| 218 */ | |
| 219 private void setResponseDataCompleted() { | |
| 220 if (mInputStream != null) { | |
| 221 mInputStream.setResponseDataCompleted(); | |
| 222 } | |
| 223 quitMessageLoop(); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 /** | |
| 228 * Posts a quit task to the message loop. | |
| 229 */ | |
| 230 private void quitMessageLoop() { | |
| 231 try { | |
| 232 mMessageLoop.postQuitTask(); | |
| 233 } catch (InterruptedException e) { | |
| 234 // ignored. | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 /** | |
| 239 * Starts running the message Loop. | |
| 240 */ | |
| 241 private void startMessageLoop() throws IOException { | |
|
mef
2014/12/01 18:28:54
Doesn't it block until loop is done?
Would it mak
xunjieli
2014/12/01 19:19:52
Done.
| |
| 242 try { | |
| 243 mMessageLoop.loop(); | |
|
mef
2014/12/01 18:28:54
Should we rethrow all exceptions thrown from the l
xunjieli
2014/12/01 19:19:52
Done. I guess the caller will need to handle IOExc
| |
| 244 } catch (InterruptedException e) { | |
| 245 throw new IOException(e); | |
| 246 } | |
| 247 } | |
| 248 } | |
| OLD | NEW |