Chromium Code Reviews| Index: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java |
| diff --git a/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f45e494d878060a09a9549354e32d1e151c17790 |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java |
| @@ -0,0 +1,226 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net.urlconnection; |
| + |
| +import org.chromium.net.ExtendedResponseInfo; |
| +import org.chromium.net.ResponseInfo; |
| +import org.chromium.net.UrlRequest; |
| +import org.chromium.net.UrlRequestContext; |
| +import org.chromium.net.UrlRequestException; |
| +import org.chromium.net.UrlRequestListener; |
| + |
| +import java.io.IOException; |
| +import java.io.InputStream; |
| +import java.net.HttpURLConnection; |
| +import java.net.URL; |
| +import java.nio.ByteBuffer; |
| +import java.util.concurrent.BlockingQueue; |
| +import java.util.concurrent.Executor; |
| +import java.util.concurrent.LinkedBlockingQueue; |
| + |
| +/** |
| + * An implementation of HttpURLConnection that uses Cronet to send requests and |
| + * receive response. This class inherits a {@code connected} field from the |
| + * superclass. That field indicates whether a connection has ever been |
| + * attempted. |
| + */ |
| +public class CronetHttpURLConnection extends HttpURLConnection { |
| + private final UrlRequestContext mUrlRequestContext; |
| + private final MessageLoop mMessageLoop; |
| + private final BlockingQueue<MessageLoop.Message> mMessageQueue; |
| + |
| + private final UrlRequest mRequest; |
|
mmenke
2014/11/21 16:53:07
optional: Maybe another line break here, to empha
xunjieli
2014/11/21 20:30:55
Done.
|
| + private ResponseInfo mResponseInfo; |
| + private CronetInputStream mInputStream; |
| + private ByteBuffer mResponseByteBuffer; |
| + |
| + protected CronetHttpURLConnection(URL url, |
| + UrlRequestContext urlRequestContext) { |
| + super(url); |
| + mUrlRequestContext = urlRequestContext; |
| + mMessageQueue = new LinkedBlockingQueue<MessageLoop.Message>(); |
| + mMessageLoop = new MessageLoop(mMessageQueue); |
| + mRequest = mUrlRequestContext.createRequest(url.toString(), |
| + new CronetUrlRequestListener(), new HandlerThreadExecutor()); |
| + } |
| + |
| + /** |
| + * Wrapper executor class which posts tasks to current looper. |
| + */ |
| + private class HandlerThreadExecutor implements Executor { |
| + public HandlerThreadExecutor() { |
| + } |
| + |
| + @Override |
| + public void execute(Runnable command) { |
| + try { |
| + mMessageQueue.put(new MessageLoop.Message(command)); |
| + } catch (InterruptedException e) { |
| + e.printStackTrace(); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Opens a connection to the resource. If the connect method is called when |
| + * the connection has already been opened (indicated by the connected field |
| + * having the value true), the call is ignored. |
| + */ |
| + @Override |
| + public void connect() throws IOException { |
| + maybeStartRequest(); |
| + } |
| + |
| + /** |
| + * Releases this connection so that its resources may be either reused or |
| + * closed. |
| + */ |
| + @Override |
| + public void disconnect() { |
| + if (mInputStream != null) { |
| + try { |
| + mInputStream.close(); |
|
mmenke
2014/11/21 16:53:07
mInputStream = null, so we handle multiple calls?
xunjieli
2014/11/21 20:30:55
OkHttp does nothing if it is called multiple times
mmenke
2014/11/21 21:26:12
Hrm...ok, so looks like we're copying their behavi
|
| + } catch (IOException e) { |
| + e.printStackTrace(); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Returns the response message returned by the remote HTTP server. |
| + */ |
| + @Override |
| + public String getResponseMessage() { |
|
mmenke
2014/11/21 16:53:07
We don't have any tests exercise this.
xunjieli
2014/11/21 20:30:55
Done.
|
| + maybeStartRequest(); |
|
mmenke
2014/11/21 16:53:07
It's a pain, but we should probably test all these
xunjieli
2014/11/21 20:30:56
Acknowledged. Will keep this in mind and handle it
|
| + return mResponseInfo.getHttpStatusText(); |
| + } |
| + |
| + /** |
| + * Returns the response code returned by the remote HTTP server. |
| + */ |
| + @Override |
| + public int getResponseCode() { |
| + maybeStartRequest(); |
| + return mResponseInfo.getHttpStatusCode(); |
| + } |
| + |
| + /** |
| + * Returns an InputStream for reading data from the resource pointed by this |
| + * URLConnection. |
| + */ |
| + @Override |
| + public InputStream getInputStream() { |
| + if (mInputStream == null) { |
| + maybeStartRequest(); |
| + mInputStream = new CronetInputStream(this); |
| + } |
| + return mInputStream; |
| + } |
| + |
| + /** |
| + * Adds the given property to the request header. |
| + */ |
| + @Override |
| + public final void addRequestProperty(String key, String value) { |
| + setRequestProperty(key, value); |
| + } |
| + |
| + /** |
| + * Sets the value of the specified request header field. |
| + */ |
| + @Override |
| + public final void setRequestProperty(String key, String value) { |
| + if (connected) { |
| + throw new IllegalStateException( |
| + "Cannot set request property after connection is made"); |
| + } |
| + mRequest.addHeader(key, value); |
| + } |
| + |
| + /** |
| + * Returns whether this connection uses a proxy server or not. |
| + */ |
| + @Override |
| + public boolean usingProxy() { |
| + // TODO(xunjieli): implement this. |
| + return false; |
| + } |
| + |
| + /** |
| + * Used by {@link CronetInputStream} to request more data from the network |
| + * stack. Note that this call might block if there isn't any more data to be |
| + * read. |
|
mmenke
2014/11/21 16:53:07
Worth noting that this may only be called after th
xunjieli
2014/11/21 20:30:56
Done.
|
| + */ |
| + ByteBuffer requestData() { |
|
mmenke
2014/11/21 16:53:06
maybe requestMoreData? ("Request data" could be a
xunjieli
2014/11/21 20:30:56
Done.
|
| + mMessageLoop.loop(); |
| + return mResponseByteBuffer; |
| + } |
| + |
| + private class CronetUrlRequestListener implements UrlRequestListener { |
| + public CronetUrlRequestListener() { |
| + } |
| + |
| + @Override |
| + public void onResponseStarted(UrlRequest request, ResponseInfo info) { |
| + mResponseInfo = info; |
| + // Quits the message loop since we have the headers now. |
| + mMessageLoop.quit(); |
| + } |
| + |
| + @Override |
| + public void onDataReceived(UrlRequest request, ResponseInfo info, |
| + ByteBuffer byteBuffer) { |
| + mResponseInfo = info; |
| + mResponseByteBuffer = ByteBuffer.allocate(byteBuffer.capacity()); |
| + mResponseByteBuffer.put(byteBuffer); |
| + mResponseByteBuffer.rewind(); |
| + mMessageLoop.quit(); |
| + } |
| + |
| + @Override |
| + public void onRedirect(UrlRequest request, ResponseInfo info, |
| + String newLocationUrl) { |
| + // TODO(xunjieli): Handle redirect. |
| + mResponseInfo = info; |
| + } |
| + |
| + @Override |
| + public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) { |
| + setResponseDataCompleted(); |
| + } |
| + |
| + @Override |
| + public void onFailed(UrlRequest request, ResponseInfo info, |
| + UrlRequestException exception) { |
| + // TODO(xunjieli): Handle failure. |
| + setResponseDataCompleted(); |
| + } |
| + |
| + /** |
| + * Notifies {@link #mInputStream} that transferring of response data has |
| + * completed. |
| + */ |
| + private void setResponseDataCompleted() { |
| + if (mInputStream != null) { |
| + mInputStream.setResponseDataCompleted(); |
| + } |
| + mMessageLoop.quit(); |
| + } |
| + } |
| + |
| + /** |
| + * On first call, creates and starts UrlRequest, and waits until response |
| + * headers are received. Does nothing on subsequent calls. |
| + */ |
| + private void maybeStartRequest() { |
| + if (connected) { |
| + return; |
| + } |
| + connected = true; |
| + mRequest.start(); |
| + // Blocks until onResponseStarted or onFailed is called. |
| + mMessageLoop.loop(); |
| + } |
| +} |