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..b6100ce4883664bd7f40506b8802bc5b02841aab |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java |
| @@ -0,0 +1,207 @@ |
| +// 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 android.os.Handler; |
| +import android.os.Looper; |
| + |
| +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.Executor; |
| + |
| +/** |
| + * 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 UrlRequest mRequest; |
| + |
| + private ResponseInfo mResponseInfo; |
| + |
| + private InputStream mResponseBody; |
| + |
| + protected CronetHttpURLConnection(URL url, |
| + UrlRequestContext urlRequestContext) { |
| + super(url); |
| + mUrlRequestContext = urlRequestContext; |
| + } |
| + |
| + /** |
| + * Wrapper executor class which posts tasks to current looper. |
| + */ |
| + private static class HandlerThreadExecutor implements Executor { |
|
mef
2014/11/13 19:16:17
Maybe we should consider passing Looper to Async A
|
| + private final Handler mHandler; |
| + |
| + public HandlerThreadExecutor() { |
| + if (Looper.myLooper() == null) { |
| + Looper.prepare(); |
| + } |
| + mHandler = new Handler(Looper.myLooper()); |
| + } |
| + |
| + @Override |
| + public void execute(Runnable command) { |
| + mHandler.post(command); |
| + } |
| + } |
| + |
| + /** |
| + * Opens a connection to the resource. |
| + */ |
| + @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 (mResponseBody != null) { |
| + try { |
| + mResponseBody.close(); |
| + } catch (IOException e) { |
| + e.printStackTrace(); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Returns the response message returned by the remote HTTP server. |
| + */ |
| + @Override |
| + public String getResponseMessage() { |
| + maybeStartRequest(); |
| + 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() { |
| + maybeStartRequest(); |
| + return mResponseBody; |
| + } |
| + |
| + /** |
| + * Adds the given property to the request header. |
| + */ |
| + @Override |
| + public final void addRequestProperty(String key, String value) { |
|
mef
2014/11/13 19:16:17
I guess 'add' should allow multiple headers with t
xunjieli
2014/11/13 19:24:11
Yes, that's the desired behavior. But due to a lim
|
| + 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"); |
| + } |
| + maybeCreateRequest(); |
| + mRequest.addHeader(key, value); |
| + } |
| + |
| + /** |
| + * Returns whether this connection uses a proxy server or not. |
| + */ |
| + @Override |
| + public boolean usingProxy() { |
| + // TODO(xunjieli): implement this. |
| + return false; |
| + } |
| + |
| + private class CronetUrlRequestListener implements UrlRequestListener { |
| + |
| + public CronetUrlRequestListener() { |
| + } |
| + |
| + @Override |
| + public void onResponseStarted(UrlRequest request, ResponseInfo info) { |
| + mResponseInfo = info; |
| + } |
| + |
| + @Override |
| + public void onDataReceived(UrlRequest request, ResponseInfo info, |
| + ByteBuffer byteBuffer) { |
| + mResponseInfo = info; |
| + // TODO(xunjieli): handle streaming. |
| + mResponseBody = new ByteBufferInputStream(byteBuffer); |
|
mef
2014/11/13 19:16:17
Yeah, this is wrong. You need to copy data from |b
xunjieli
2014/11/13 19:24:11
Acknowledged. Will do.
|
| + } |
| + |
| + @Override |
| + public void onRedirect(UrlRequest request, ResponseInfo info, |
| + String newLocationUrl) { |
| + // TODO(xunjieli): handle redirect. |
| + mResponseInfo = info; |
| + } |
| + |
| + @Override |
| + public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) { |
| + Looper.myLooper().quit(); |
| + } |
| + |
| + @Override |
| + public void onFailed(UrlRequest request, ResponseInfo info, |
| + UrlRequestException exception) { |
| + // TODO(xunjieli): handle failure. |
| + Looper.myLooper().quit(); |
| + } |
| + } |
| + |
| + /** |
| + * Maybe starts {@code mRequest}, and waits for it to complete. |
| + */ |
| + private void maybeStartRequest() { |
| + if (connected) { |
| + return; |
| + } |
| + maybeCreateRequest(); |
| + mRequest.start(); |
| + connected = true; |
| + Looper.loop(); |
|
mmenke
2014/11/13 19:31:27
Problem: This is noticeable to the embedder, and
|
| + } |
| + |
| + /** |
| + * Maybe creates {@code mRequest} if it is null. |
| + */ |
| + private void maybeCreateRequest() { |
| + if (mRequest != null) { |
| + return; |
| + } |
| + mRequest = mUrlRequestContext.createRequest(url.toString(), |
| + new CronetUrlRequestListener(), new HandlerThreadExecutor()); |
| + } |
| +} |