Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import org.apache.http.conn.ConnectTimeoutException; | 7 import org.apache.http.conn.ConnectTimeoutException; |
| 8 import org.chromium.base.CalledByNative; | 8 import org.chromium.base.CalledByNative; |
| 9 import org.chromium.base.JNINamespace; | 9 import org.chromium.base.JNINamespace; |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 private final UrlRequestContext mRequestContext; | 32 private final UrlRequestContext mRequestContext; |
| 33 private final String mUrl; | 33 private final String mUrl; |
| 34 private final int mPriority; | 34 private final int mPriority; |
| 35 private final Map<String, String> mHeaders; | 35 private final Map<String, String> mHeaders; |
| 36 private final WritableByteChannel mSink; | 36 private final WritableByteChannel mSink; |
| 37 private Map<String, String> mAdditionalHeaders; | 37 private Map<String, String> mAdditionalHeaders; |
| 38 private String mUploadContentType; | 38 private String mUploadContentType; |
| 39 private String mMethod; | 39 private String mMethod; |
| 40 private byte[] mUploadData; | 40 private byte[] mUploadData; |
| 41 private ReadableByteChannel mUploadChannel; | 41 private ReadableByteChannel mUploadChannel; |
| 42 private WritableByteChannel mOutputChannel; | 42 private boolean mChunkedUpload; |
|
mef
2014/08/15 19:42:27
what happened to mOutputChannel?
mmenke
2014/08/15 19:54:52
See mSink?
mdumitrescu
2014/08/18 11:28:34
Yeah. mOutputChannel is never used - mSink is.
| |
| 43 private IOException mSinkException; | 43 private IOException mSinkException; |
| 44 private volatile boolean mStarted; | 44 private volatile boolean mStarted; |
| 45 private volatile boolean mCanceled; | 45 private volatile boolean mCanceled; |
| 46 private volatile boolean mRecycled; | 46 private volatile boolean mRecycled; |
| 47 private volatile boolean mFinished; | 47 private volatile boolean mFinished; |
| 48 private boolean mHeadersAvailable; | 48 private boolean mHeadersAvailable; |
| 49 private String mContentType; | 49 private String mContentType; |
| 50 private long mContentLength; | 50 private long mContentLength; |
| 51 private long mUploadContentLength; | 51 private long mUploadContentLength; |
| 52 private final ContextLock mLock; | 52 private final ContextLock mLock; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 * POST. | 103 * POST. |
| 104 * @param data The content that needs to be uploaded if this is a POST | 104 * @param data The content that needs to be uploaded if this is a POST |
| 105 * request. | 105 * request. |
| 106 */ | 106 */ |
| 107 public void setUploadData(String contentType, byte[] data) { | 107 public void setUploadData(String contentType, byte[] data) { |
| 108 synchronized (mLock) { | 108 synchronized (mLock) { |
| 109 validateNotStarted(); | 109 validateNotStarted(); |
| 110 mUploadContentType = contentType; | 110 mUploadContentType = contentType; |
| 111 mUploadData = data; | 111 mUploadData = data; |
| 112 mUploadChannel = null; | 112 mUploadChannel = null; |
| 113 mChunkedUpload = false; | |
| 113 } | 114 } |
| 114 } | 115 } |
| 115 | 116 |
| 116 /** | 117 /** |
| 117 * Sets a readable byte channel to upload as part of a POST request. | 118 * Sets a readable byte channel to upload as part of a POST request. |
| 118 * | 119 * |
| 119 * @param contentType MIME type of the post content or null if this is not a | 120 * @param contentType MIME type of the post content or null if this is not a |
| 120 * POST request. | 121 * POST request. |
| 121 * @param channel The channel to read to read upload data from if this is a | 122 * @param channel The channel to read to read upload data from if this is a |
| 122 * POST request. | 123 * POST request. |
| 123 * @param contentLength The length of data to upload. | 124 * @param contentLength The length of data to upload. |
| 124 */ | 125 */ |
| 125 public void setUploadChannel(String contentType, | 126 public void setUploadChannel(String contentType, |
| 126 ReadableByteChannel channel, long contentLength) { | 127 ReadableByteChannel channel, long contentLength) { |
| 127 synchronized (mLock) { | 128 synchronized (mLock) { |
| 128 validateNotStarted(); | 129 validateNotStarted(); |
| 129 mUploadContentType = contentType; | 130 mUploadContentType = contentType; |
| 130 mUploadChannel = channel; | 131 mUploadChannel = channel; |
| 131 mUploadContentLength = contentLength; | 132 mUploadContentLength = contentLength; |
| 132 mUploadData = null; | 133 mUploadData = null; |
| 134 mChunkedUpload = false; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 /** | |
| 139 * Sets this request up for chunked uploading. To upload data call | |
| 140 * {@link #appendChunk(ByteBuffer, boolean)} after {@link #start()}. | |
| 141 * | |
| 142 * @param contentType MIME type of the post content or null if this is not a | |
| 143 * POST request. | |
| 144 */ | |
| 145 public void setChunkedUpload(String contentType) { | |
| 146 synchronized (mLock) { | |
| 147 validateNotStarted(); | |
| 148 mUploadContentType = contentType; | |
| 149 mChunkedUpload = true; | |
| 150 mUploadData = null; | |
| 151 mUploadChannel = null; | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * Uploads a new chunk. Must have called {@link #setChunkedUpload(String)} | |
| 157 * and {@link #start()}. | |
| 158 * | |
| 159 * @param chunk The data, which will not be modified. It must not be empty | |
| 160 * and its current position must be zero. | |
| 161 * @param isLastChunk Whether this chunk is the last one. | |
| 162 */ | |
| 163 public void appendChunk(ByteBuffer chunk, boolean isLastChunk) | |
| 164 throws IOException { | |
| 165 if (!chunk.hasRemaining()) { | |
| 166 throw new IllegalArgumentException( | |
| 167 "Attempted to write empty buffer."); | |
| 168 } | |
| 169 if (chunk.position() != 0) { | |
| 170 throw new IllegalArgumentException("The position must be zero."); | |
| 171 } | |
| 172 synchronized (mLock) { | |
| 173 if (!mStarted) { | |
| 174 throw new IllegalStateException("Request not yet started."); | |
| 175 } | |
| 176 if (!mChunkedUpload) { | |
| 177 throw new IllegalStateException( | |
| 178 "Request not set for chunked uploadind."); | |
| 179 } | |
| 180 if (mUrlRequestAdapter == 0) { | |
| 181 throw new IOException("Native peer destroyed."); | |
| 182 } | |
| 183 nativeAppendChunk(mUrlRequestAdapter, chunk, chunk.limit(), | |
| 184 isLastChunk); | |
| 133 } | 185 } |
| 134 } | 186 } |
| 135 | 187 |
| 136 public void setHttpMethod(String method) { | 188 public void setHttpMethod(String method) { |
| 137 validateNotStarted(); | 189 validateNotStarted(); |
| 138 if (!("PUT".equals(method) || "POST".equals(method))) { | 190 if (!("PUT".equals(method) || "POST".equals(method))) { |
| 139 throw new IllegalArgumentException("Only PUT or POST are allowed."); | 191 throw new IllegalArgumentException("Only PUT or POST are allowed."); |
| 140 } | 192 } |
| 141 mMethod = method; | 193 mMethod = method; |
| 142 } | 194 } |
| 143 | 195 |
| 144 public WritableByteChannel getSink() { | 196 public WritableByteChannel getSink() { |
| 145 return mSink; | 197 return mSink; |
| 146 } | 198 } |
| 147 | 199 |
| 148 public void start() { | 200 public void start() { |
| 149 synchronized (mLock) { | 201 synchronized (mLock) { |
| 150 if (mCanceled) { | 202 if (mCanceled) { |
| 151 return; | 203 return; |
| 152 } | 204 } |
| 153 | 205 |
| 154 validateNotStarted(); | 206 validateNotStarted(); |
| 155 validateNotRecycled(); | 207 validateNotRecycled(); |
| 156 | 208 |
| 157 mStarted = true; | 209 mStarted = true; |
| 158 | 210 |
| 159 String method = mMethod; | 211 String method = mMethod; |
| 160 if (method == null && | 212 if (method == null && |
| 161 ((mUploadData != null && mUploadData.length > 0) || | 213 ((mUploadData != null && mUploadData.length > 0) || |
| 162 mUploadChannel != null)) { | 214 mUploadChannel != null || mChunkedUpload)) { |
| 163 // Default to POST if there is data to upload but no method was | 215 // Default to POST if there is data to upload but no method was |
| 164 // specified. | 216 // specified. |
| 165 method = "POST"; | 217 method = "POST"; |
| 166 } | 218 } |
| 167 | 219 |
| 168 if (method != null) { | 220 if (method != null) { |
| 169 nativeSetMethod(mUrlRequestAdapter, method); | 221 nativeSetMethod(mUrlRequestAdapter, method); |
| 170 } | 222 } |
| 171 | 223 |
| 172 if (mHeaders != null && !mHeaders.isEmpty()) { | 224 if (mHeaders != null && !mHeaders.isEmpty()) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 183 entry.getValue()); | 235 entry.getValue()); |
| 184 } | 236 } |
| 185 } | 237 } |
| 186 | 238 |
| 187 if (mUploadData != null && mUploadData.length > 0) { | 239 if (mUploadData != null && mUploadData.length > 0) { |
| 188 nativeSetUploadData(mUrlRequestAdapter, mUploadContentType, | 240 nativeSetUploadData(mUrlRequestAdapter, mUploadContentType, |
| 189 mUploadData); | 241 mUploadData); |
| 190 } else if (mUploadChannel != null) { | 242 } else if (mUploadChannel != null) { |
| 191 nativeSetUploadChannel(mUrlRequestAdapter, mUploadContentType, | 243 nativeSetUploadChannel(mUrlRequestAdapter, mUploadContentType, |
| 192 mUploadContentLength); | 244 mUploadContentLength); |
| 245 } else if (mChunkedUpload) { | |
| 246 nativeEnableChunkedUpload(mUrlRequestAdapter, | |
| 247 mUploadContentType); | |
| 193 } | 248 } |
| 194 | 249 |
| 195 nativeStart(mUrlRequestAdapter); | 250 nativeStart(mUrlRequestAdapter); |
| 196 } | 251 } |
| 197 } | 252 } |
| 198 | 253 |
| 199 public void cancel() { | 254 public void cancel() { |
| 200 synchronized (mLock) { | 255 synchronized (mLock) { |
| 201 if (mCanceled) { | 256 if (mCanceled) { |
| 202 return; | 257 return; |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 String value); | 473 String value); |
| 419 | 474 |
| 420 private native void nativeSetMethod(long urlRequestAdapter, String method); | 475 private native void nativeSetMethod(long urlRequestAdapter, String method); |
| 421 | 476 |
| 422 private native void nativeSetUploadData(long urlRequestAdapter, | 477 private native void nativeSetUploadData(long urlRequestAdapter, |
| 423 String contentType, byte[] content); | 478 String contentType, byte[] content); |
| 424 | 479 |
| 425 private native void nativeSetUploadChannel(long urlRequestAdapter, | 480 private native void nativeSetUploadChannel(long urlRequestAdapter, |
| 426 String contentType, long contentLength); | 481 String contentType, long contentLength); |
| 427 | 482 |
| 483 private native void nativeEnableChunkedUpload(long urlRequestAdapter, | |
| 484 String contentType); | |
| 485 | |
| 486 private native void nativeAppendChunk(long urlRequestAdapter, | |
| 487 ByteBuffer chunk, int chunkSize, boolean isLastChunk); | |
| 488 | |
| 428 private native void nativeStart(long urlRequestAdapter); | 489 private native void nativeStart(long urlRequestAdapter); |
| 429 | 490 |
| 430 private native void nativeCancel(long urlRequestAdapter); | 491 private native void nativeCancel(long urlRequestAdapter); |
| 431 | 492 |
| 432 private native void nativeDestroyRequestAdapter(long urlRequestAdapter); | 493 private native void nativeDestroyRequestAdapter(long urlRequestAdapter); |
| 433 | 494 |
| 434 private native int nativeGetErrorCode(long urlRequestAdapter); | 495 private native int nativeGetErrorCode(long urlRequestAdapter); |
| 435 | 496 |
| 436 private native int nativeGetHttpStatusCode(long urlRequestAdapter); | 497 private native int nativeGetHttpStatusCode(long urlRequestAdapter); |
| 437 | 498 |
| 438 private native String nativeGetErrorString(long urlRequestAdapter); | 499 private native String nativeGetErrorString(long urlRequestAdapter); |
| 439 | 500 |
| 440 private native String nativeGetContentType(long urlRequestAdapter); | 501 private native String nativeGetContentType(long urlRequestAdapter); |
| 441 | 502 |
| 442 private native long nativeGetContentLength(long urlRequestAdapter); | 503 private native long nativeGetContentLength(long urlRequestAdapter); |
| 443 | 504 |
| 444 private native String nativeGetHeader(long urlRequestAdapter, String name); | 505 private native String nativeGetHeader(long urlRequestAdapter, String name); |
| 445 | 506 |
| 446 private native void nativeGetAllHeaders(long urlRequestAdapter, | 507 private native void nativeGetAllHeaders(long urlRequestAdapter, |
| 447 ResponseHeadersMap headers); | 508 ResponseHeadersMap headers); |
| 448 | 509 |
| 449 // Explicit class to work around JNI-generator generics confusion. | 510 // Explicit class to work around JNI-generator generics confusion. |
| 450 private class ResponseHeadersMap extends HashMap<String, List<String>> { | 511 private class ResponseHeadersMap extends HashMap<String, List<String>> { |
| 451 } | 512 } |
| 452 } | 513 } |
| OLD | NEW |