Chromium Code Reviews| Index: components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java |
| diff --git a/components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java |
| index 3a7776dc8520cbabc219fde0c6515e7d1499e808..43ea816664630ebd19454579c433382844fe824d 100644 |
| --- a/components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java |
| +++ b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java |
| @@ -4,38 +4,194 @@ |
| package org.chromium.net; |
| +import android.util.Log; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| + |
| +import java.net.URL; |
| +import java.nio.ByteBuffer; |
| +import java.util.ArrayList; |
| +import java.util.HashMap; |
| +import java.util.List; |
| +import java.util.Map; |
| +import java.util.concurrent.Executor; |
| +import java.util.concurrent.atomic.AtomicBoolean; |
| + |
| /** |
| * UrlRequest using Chromium HTTP stack implementation. |
| */ |
| -public class CronetUrlRequest implements UrlRequest { |
| +@JNINamespace("cronet") |
| +final class CronetUrlRequest implements UrlRequest { |
| + /** Native adapter object, owned by UrlRequest. */ |
| + private long mUrlRequestAdapter; |
| + private final CronetUrlRequestContext mRequestContext; |
| + private final List<String> mUrlChain = new ArrayList<String>(); |
|
xunjieli
2014/10/23 15:51:22
mUrlChain is changed in onRedirect(). Is it okay t
mef
2014/10/24 03:31:45
Good question. Compiler doesn't complain, but I'm
mmenke
2014/10/24 03:46:19
Regardless of guidelines, I think it makes sense j
mef
2014/10/27 21:00:02
On a closer look we don't pass mUrlChain to listen
|
| + private final int mPriority; |
| + private final UrlRequestListener mListener; |
| + private final Executor mExecutor; |
| + private NativeResponseInfo mResponseInfo; |
| + private boolean mStarted = false; |
| + private AtomicBoolean mCanceled = new AtomicBoolean(false); |
| + private OnDataReceivedRunnable mOnDataReceivedTask; |
| + |
| + final class OnDataReceivedRunnable implements Runnable { |
| + ByteBuffer mByteBuffer; |
| + public void run() { |
| + if (isCanceled()) { |
| + return; |
| + } |
| + try { |
| + mListener.onDataReceived(CronetUrlRequest.this, |
| + mResponseInfo, mByteBuffer); |
| + mByteBuffer = null; |
| + if (!isCanceled()) { |
| + nativeReceiveData(mUrlRequestAdapter); |
| + } |
| + } catch (Exception e) { |
| + onCalledByNativeException(e); |
| + } |
| + } |
| + } |
| + |
| + final class NativeResponseInfo implements ResponseInfo { |
| + private final String[] mUrlChain; |
|
xunjieli
2014/10/23 15:51:22
nit: this variable has the same name as the one in
mef
2014/10/24 03:31:44
Done.
|
| + private final int mHttpStatusCode; |
| + private final ResponseHeadersMap mAllHeaders = new ResponseHeadersMap(); |
| + private final boolean mWasCached; |
| + private final String mNegotiatedProtocol; |
| + |
| + NativeResponseInfo(String[] urlChain, int httpStatusCode, |
| + boolean wasCached, String negotiatedProtocol) { |
| + mUrlChain = urlChain; |
| + mHttpStatusCode = httpStatusCode; |
| + mWasCached = wasCached; |
| + mNegotiatedProtocol = negotiatedProtocol; |
| + } |
| + |
| + @Override |
| + public String getUrl() { |
| + return mUrlChain[mUrlChain.length - 1]; |
| + } |
| + |
| + @Override |
| + public String[] getUrlChain() { |
| + return mUrlChain; |
| + } |
| + |
| + @Override |
| + public int getHttpStatusCode() { |
| + return mHttpStatusCode; |
| + } |
| + |
| + @Override |
| + public Map<String, List<String>> getAllHeaders() { |
| + return mAllHeaders; |
| + } |
| + |
| + @Override |
| + public boolean wasCached() { |
| + return mWasCached; |
| + } |
| + |
| + @Override |
| + public String getNegotiatedProtocol() { |
| + return mNegotiatedProtocol; |
| + } |
| + }; |
| + |
| + final class NativeExtendedResponseInfo implements ExtendedResponseInfo { |
| + ResponseInfo mResponseInfo; |
| + private long mTotalReceivedBytes = 0; |
| + |
| + NativeExtendedResponseInfo(ResponseInfo responseInfo, |
| + long totalReceivedBytes) { |
| + mResponseInfo = responseInfo; |
| + mTotalReceivedBytes = totalReceivedBytes; |
| + } |
| + |
| + @Override |
| + public ResponseInfo getResponseInfo() { |
| + return mResponseInfo; |
| + } |
| + |
| + @Override |
| + public long getTotalReceivedBytes() { |
| + return mTotalReceivedBytes; |
| + } |
| + }; |
| + |
| + |
| + CronetUrlRequest(CronetUrlRequestContext requestContext, |
|
xunjieli
2014/10/23 15:51:22
Just curious. Why is it that CronetUrlRequest cons
mmenke
2014/10/23 21:36:42
In Java, by default, methods and variables are sco
xunjieli
2014/10/23 21:44:46
I see, so we want to force clients to create a req
mef
2014/10/24 03:31:44
Acknowledged.
|
| + String url, int priority, |
| + UrlRequestListener listener, |
| + Executor executor) { |
| + if (requestContext == null) { |
| + throw new NullPointerException("Context is required"); |
| + } |
| + if (url == null) { |
| + throw new NullPointerException("URL is required"); |
| + } |
| + if (listener == null) { |
| + throw new NullPointerException("Listener is required"); |
| + } |
| + if (executor == null) { |
| + throw new NullPointerException("Executor is required"); |
| + } |
| + |
| + mRequestContext = requestContext; |
| + mUrlChain.add(url); |
| + mPriority = convertRequestPriority(priority); |
| + mUrlRequestAdapter = nativeCreateRequestAdapter( |
| + mRequestContext.getCronetUrlRequestContextAdapter(), |
| + url, |
| + mPriority); |
| + mListener = listener; |
| + mExecutor = executor; |
| + } |
| + |
| @Override |
| public void setHttpMethod(String method) { |
| - |
| + checkNotStartedOrDone(); |
| + if (method == null) { |
| + throw new NullPointerException("Method is required."); |
| + } |
| + nativeSetHttpMethod(mUrlRequestAdapter, method); |
| } |
| @Override |
| public void addHeader(String header, String value) { |
| - |
| + checkNotStartedOrDone(); |
| + if (header == null || value == null) { |
| + throw new NullPointerException("Invalid header."); |
| + } |
| + if (!nativeAddHeader(mUrlRequestAdapter, header, value)) { |
| + throw new IllegalArgumentException("Invalid header."); |
| + } |
| } |
| @Override |
| - public void start(UrlRequestListener listener) { |
| - |
| + public void start() { |
| + checkNotStartedOrDone(); |
| + mStarted = true; |
| + nativeStart(mUrlRequestAdapter); |
| } |
| @Override |
| public void cancel() { |
| - |
| + if (mCanceled.compareAndSet(false, true)) { |
| + nativeCancel(mUrlRequestAdapter); |
| + } |
| } |
| @Override |
| public boolean isCanceled() { |
| - return false; |
| + return mCanceled.get(); |
| } |
| @Override |
| public void pause() { |
| - |
| + throw new UnsupportedOperationException("Not implemented yet"); |
| } |
| @Override |
| @@ -45,6 +201,279 @@ public class CronetUrlRequest implements UrlRequest { |
| @Override |
| public void resume() { |
| + throw new UnsupportedOperationException("Not implemented yet"); |
| + } |
| + |
| + private void postAppTask(Runnable task) { |
|
xunjieli
2014/10/23 14:04:33
nit: the name "postAppTask" sounds a little vague
mef
2014/10/24 03:31:45
Done.
|
| + mExecutor.execute(task); |
| + } |
| + |
| + private static int convertRequestPriority(int priority) { |
| + switch (priority) { |
| + case REQUEST_PRIORITY_IDLE: |
| + return ChromiumUrlRequestPriority.IDLE; |
| + case REQUEST_PRIORITY_LOWEST: |
| + return ChromiumUrlRequestPriority.LOWEST; |
| + case REQUEST_PRIORITY_LOW: |
| + return ChromiumUrlRequestPriority.LOW; |
| + case REQUEST_PRIORITY_MEDIUM: |
| + return ChromiumUrlRequestPriority.MEDIUM; |
| + case REQUEST_PRIORITY_HIGHEST: |
| + return ChromiumUrlRequestPriority.HIGHEST; |
| + default: |
| + return ChromiumUrlRequestPriority.MEDIUM; |
| + } |
| + } |
| + |
| + private NativeResponseInfo prepareResponseInfo() { |
| + NativeResponseInfo responseInfo = new NativeResponseInfo( |
| + mUrlChain.toArray(new String[mUrlChain.size()]), |
| + nativeGetHttpStatusCode(mUrlRequestAdapter), |
| + nativeGetWasCached(mUrlRequestAdapter), |
| + nativeGetNegotiatedProtocol(mUrlRequestAdapter)); |
| + nativePopulateResponseHeaders(mUrlRequestAdapter, |
| + responseInfo.mAllHeaders); |
| + return responseInfo; |
| + } |
| + |
| + private void checkNotStartedOrDone() { |
| + if (mStarted) { |
| + throw new IllegalStateException("Request is already started."); |
| + } |
| + if (mUrlRequestAdapter == 0) { |
| + throw new IllegalStateException("Request is already destroyed."); |
| + } |
| + } |
| + |
| + private void destroyRequestAdapter() { |
| + if (mUrlRequestAdapter != 0) { |
| + nativeDestroyRequestAdapter(mUrlRequestAdapter); |
| + mUrlRequestAdapter = 0; |
| + } else { |
| + throw new IllegalStateException("Request is already destroyed."); |
| + } |
| + } |
| + |
| + /** |
| + * If @CalledByNative method throws an exception, request gets cancelled |
| + * and exception could be retrieved from request using getException(). |
| + */ |
| + private void onCalledByNativeException(Exception e) { |
| + UrlRequestException requestError = new UrlRequestException( |
| + "CalledByNative method has thrown an exception", e); |
| + Log.e(CronetUrlRequestContext.LOG_TAG, |
| + "Exception in CalledByNative method", e); |
|
xunjieli
2014/10/23 15:51:22
nit: indent.
mef
2014/10/24 03:31:45
Done.
|
| + try { |
| + cancel(); |
| + mListener.onError(this, mResponseInfo, requestError); |
| + } catch (Exception cancelException) { |
| + Log.e(CronetUrlRequestContext.LOG_TAG, |
| + "Exception trying to cancel request", cancelException); |
|
xunjieli
2014/10/23 15:51:21
nit: indent.
mef
2014/10/24 03:31:45
Done.
|
| + } |
| + } |
| + |
| + //////////////////////////////////////////////// |
| + // Private methods called by the native code. |
| + //////////////////////////////////////////////// |
| + |
| + /** |
| + * Called before following redirects. The redirect will automatically be |
|
xunjieli
2014/10/23 14:04:33
nit: there are two spaces after "."
mef
2014/10/24 03:31:45
Done.
|
| + * followed, unless the request is paused or cancelled during this |
| + * callback. If the redirect response has a body, it will be ignored. |
| + * This will only be called between start and onResponseStarted. |
| + * |
| + * @param newLocation Location where request is redirected. |
| + */ |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void onRedirect(final String newLocation) { |
| + mUrlChain.add(newLocation); |
| + final NativeResponseInfo responseInfo = prepareResponseInfo(); |
| + Runnable task = new Runnable() { |
| + public void run() { |
| + if (isCanceled()) { |
| + return; |
| + } |
| + try { |
| + mListener.onRedirect(CronetUrlRequest.this, responseInfo, |
| + new URL(newLocation)); |
| + if (!isCanceled()) { |
| + nativeFollowDeferredRedirect(mUrlRequestAdapter); |
| + } |
| + } catch (Exception e) { |
| + onCalledByNativeException(e); |
| + } |
| + } |
| + }; |
| + postAppTask(task); |
| + } |
| + |
| + /** |
| + * Called when the final set of headers, after all redirects, |
| + * is received. Can only be called once for each request. |
| + */ |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void onResponseStarted() { |
| + mResponseInfo = prepareResponseInfo(); |
| + Runnable task = new Runnable() { |
| + public void run() { |
| + if (isCanceled()) { |
| + return; |
| + } |
| + try { |
| + mListener.onResponseStarted(CronetUrlRequest.this, |
| + mResponseInfo); |
| + if (!isCanceled()) { |
| + nativeReceiveData(mUrlRequestAdapter); |
| + } |
| + } catch (Exception e) { |
| + onCalledByNativeException(e); |
| + } |
| + } |
| + }; |
| + postAppTask(task); |
| + } |
| + |
| + /** |
| + * Called whenever data is received. The ByteBuffer remains |
| + * valid only until listener callback. Or if the callback |
| + * pauses the request, it remains valid until the request is resumed. |
| + * Cancelling the request also invalidates the buffer. |
| + * |
| + * @param byteBuffer Received data. |
| + */ |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void onDataReceived(final ByteBuffer byteBuffer) { |
| + if (mOnDataReceivedTask == null) { |
| + mOnDataReceivedTask = new OnDataReceivedRunnable(); |
| + } |
| + mOnDataReceivedTask.mByteBuffer = byteBuffer; |
| + postAppTask(mOnDataReceivedTask); |
| + } |
| + |
| + /** |
| + * Called when request is canceled, no callbacks are called. |
| + */ |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void onCanceled() { |
| + Runnable task = new Runnable() { |
| + public void run() { |
| + destroyRequestAdapter(); |
| + } |
| + }; |
| + postAppTask(task); |
| + } |
| + |
| + /** |
| + * Called when request is complete, no callbacks will be called afterwards. |
| + */ |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void onComplete() { |
| + final NativeExtendedResponseInfo extendedResponseInfo = |
| + new NativeExtendedResponseInfo(mResponseInfo, |
| + nativeGetTotalReceivedBytes(mUrlRequestAdapter)); |
|
xunjieli
2014/10/23 15:51:22
nit: indent. nativeGetTotalReceivedBytes should be
mef
2014/10/24 03:31:45
Hmm, I'm not sure, it wouldn't fit there, would it
|
| + Runnable task = new Runnable() { |
| + public void run() { |
| + if (isCanceled()) { |
| + return; |
| + } |
| + try { |
| + mListener.onComplete(CronetUrlRequest.this, |
| + extendedResponseInfo); |
| + } catch (Exception e) { |
| + Log.e(CronetUrlRequestContext.LOG_TAG, |
| + "Exception in onComplete method", e); |
|
xunjieli
2014/10/23 15:51:22
nit: indent.
mef
2014/10/24 03:31:44
Done.
|
| + } |
| + destroyRequestAdapter(); |
| + } |
| + }; |
| + postAppTask(task); |
| + } |
| + |
| + /** |
| + * Called when error has occured, no callbacks will be called afterwards. |
| + * |
| + * @param nativeError native net error code. |
| + * @param errorString textual representation of the error code. |
| + */ |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void onError(final int nativeError, final String errorString) { |
| + Runnable task = new Runnable() { |
| + public void run() { |
| + if (isCanceled()) { |
| + return; |
| + } |
| + try { |
| + UrlRequestException requestError = new UrlRequestException( |
| + "Exception in CronetUrlRequest: " + errorString, |
| + nativeError); |
| + mListener.onError(CronetUrlRequest.this, |
| + mResponseInfo, |
| + requestError); |
| + destroyRequestAdapter(); |
| + } catch (Exception e) { |
| + onCalledByNativeException(e); |
| + } |
| + } |
| + }; |
| + postAppTask(task); |
| + } |
| + |
| + /** |
| + * Appends header |name| with value |value| to |headersMap|. |
| + */ |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void onAppendResponseHeader(ResponseHeadersMap headersMap, |
| + String name, String value) { |
| + try { |
| + if (!headersMap.containsKey(name)) { |
| + headersMap.put(name, new ArrayList<String>()); |
| + } |
| + headersMap.get(name).add(value); |
| + } catch (Exception e) { |
| + onCalledByNativeException(e); |
| + } |
| + } |
| + |
| + // Native methods are implemented in cronet_url_request.cc. |
| + |
| + private native long nativeCreateRequestAdapter( |
| + long urlRequestContextAdapter, String url, int priority); |
| + |
| + private native boolean nativeAddHeader(long urlRequestAdapter, String name, |
| + String value); |
| + |
| + private native void nativeSetHttpMethod(long urlRequestAdapter, |
| + String method); |
| + |
| + private native void nativeStart(long urlRequestAdapter); |
| + |
| + private native void nativeCancel(long urlRequestAdapter); |
| + |
| + private native void nativeDestroyRequestAdapter(long urlRequestAdapter); |
| + |
| + private native void nativeFollowDeferredRedirect(long urlRequestAdapter); |
| + |
| + private native void nativeReceiveData(long urlRequestAdapter); |
| + |
| + private native int nativeGetHttpStatusCode(long urlRequestAdapter); |
| + |
| + private native void nativePopulateResponseHeaders(long urlRequestAdapter, |
| + ResponseHeadersMap headers); |
| + |
| + private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); |
| + |
| + private native boolean nativeGetWasCached(long urlRequestAdapter); |
| + |
| + private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); |
| + // Explicit class to work around JNI-generator generics confusion. |
| + private class ResponseHeadersMap extends HashMap<String, List<String>> { |
| } |
| } |