| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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; |
| 53 | 53 |
| 54 /** | 54 /** |
| 55 * Native peer object, owned by UrlRequest. | 55 * Native adapter object, owned by UrlRequest. |
| 56 */ | 56 */ |
| 57 private long mUrlRequestPeer; | 57 private long mUrlRequestAdapter; |
| 58 | 58 |
| 59 /** | 59 /** |
| 60 * Constructor. | 60 * Constructor. |
| 61 * | 61 * |
| 62 * @param requestContext The context. | 62 * @param requestContext The context. |
| 63 * @param url The URL. | 63 * @param url The URL. |
| 64 * @param priority Request priority, e.g. {@link #REQUEST_PRIORITY_MEDIUM}. | 64 * @param priority Request priority, e.g. {@link #REQUEST_PRIORITY_MEDIUM}. |
| 65 * @param headers HTTP headers. | 65 * @param headers HTTP headers. |
| 66 * @param sink The output channel into which downloaded content will be | 66 * @param sink The output channel into which downloaded content will be |
| 67 * written. | 67 * written. |
| 68 */ | 68 */ |
| 69 public UrlRequest(UrlRequestContext requestContext, String url, | 69 public UrlRequest(UrlRequestContext requestContext, String url, |
| 70 int priority, Map<String, String> headers, | 70 int priority, Map<String, String> headers, |
| 71 WritableByteChannel sink) { | 71 WritableByteChannel sink) { |
| 72 if (requestContext == null) { | 72 if (requestContext == null) { |
| 73 throw new NullPointerException("Context is required"); | 73 throw new NullPointerException("Context is required"); |
| 74 } | 74 } |
| 75 if (url == null) { | 75 if (url == null) { |
| 76 throw new NullPointerException("URL is required"); | 76 throw new NullPointerException("URL is required"); |
| 77 } | 77 } |
| 78 mRequestContext = requestContext; | 78 mRequestContext = requestContext; |
| 79 mUrl = url; | 79 mUrl = url; |
| 80 mPriority = priority; | 80 mPriority = priority; |
| 81 mHeaders = headers; | 81 mHeaders = headers; |
| 82 mSink = sink; | 82 mSink = sink; |
| 83 mLock = new ContextLock(); | 83 mLock = new ContextLock(); |
| 84 mUrlRequestPeer = nativeCreateRequestPeer( | 84 mUrlRequestAdapter = nativeCreateRequestAdapter( |
| 85 mRequestContext.getUrlRequestContextPeer(), mUrl, mPriority); | 85 mRequestContext.getUrlRequestContextAdapter(), mUrl, mPriority); |
| 86 } | 86 } |
| 87 | 87 |
| 88 /** | 88 /** |
| 89 * Adds a request header. | 89 * Adds a request header. |
| 90 */ | 90 */ |
| 91 public void addHeader(String header, String value) { | 91 public void addHeader(String header, String value) { |
| 92 validateNotStarted(); | 92 validateNotStarted(); |
| 93 if (mAdditionalHeaders == null) { | 93 if (mAdditionalHeaders == null) { |
| 94 mAdditionalHeaders = new HashMap<String, String>(); | 94 mAdditionalHeaders = new HashMap<String, String>(); |
| 95 } | 95 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 mUploadContentType = contentType; | 129 mUploadContentType = contentType; |
| 130 mUploadChannel = channel; | 130 mUploadChannel = channel; |
| 131 mUploadContentLength = contentLength; | 131 mUploadContentLength = contentLength; |
| 132 mUploadData = null; | 132 mUploadData = null; |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 public void setHttpMethod(String method) { | 136 public void setHttpMethod(String method) { |
| 137 validateNotStarted(); | 137 validateNotStarted(); |
| 138 if (!("PUT".equals(method) || "POST".equals(method))) { | 138 if (!("PUT".equals(method) || "POST".equals(method))) { |
| 139 throw new IllegalArgumentException("Only PUT and POST are allowed.")
; | 139 throw new IllegalArgumentException("Only PUT or POST are allowed."); |
| 140 } | 140 } |
| 141 mMethod = method; | 141 mMethod = method; |
| 142 } | 142 } |
| 143 | 143 |
| 144 public WritableByteChannel getSink() { | 144 public WritableByteChannel getSink() { |
| 145 return mSink; | 145 return mSink; |
| 146 } | 146 } |
| 147 | 147 |
| 148 public void start() { | 148 public void start() { |
| 149 synchronized (mLock) { | 149 synchronized (mLock) { |
| 150 if (mCanceled) { | 150 if (mCanceled) { |
| 151 return; | 151 return; |
| 152 } | 152 } |
| 153 | 153 |
| 154 validateNotStarted(); | 154 validateNotStarted(); |
| 155 validateNotRecycled(); | 155 validateNotRecycled(); |
| 156 | 156 |
| 157 mStarted = true; | 157 mStarted = true; |
| 158 | 158 |
| 159 String method = mMethod; | 159 String method = mMethod; |
| 160 if (method == null && | 160 if (method == null && |
| 161 ((mUploadData != null && mUploadData.length > 0) || | 161 ((mUploadData != null && mUploadData.length > 0) || |
| 162 mUploadChannel != null)) { | 162 mUploadChannel != null)) { |
| 163 // Default to POST if there is data to upload but no method was | 163 // Default to POST if there is data to upload but no method was |
| 164 // specified. | 164 // specified. |
| 165 method = "POST"; | 165 method = "POST"; |
| 166 } | 166 } |
| 167 | 167 |
| 168 if (method != null) { | 168 if (method != null) { |
| 169 nativeSetMethod(mUrlRequestPeer, method); | 169 nativeSetMethod(mUrlRequestAdapter, method); |
| 170 } | 170 } |
| 171 | 171 |
| 172 if (mHeaders != null && !mHeaders.isEmpty()) { | 172 if (mHeaders != null && !mHeaders.isEmpty()) { |
| 173 for (Entry<String, String> entry : mHeaders.entrySet()) { | 173 for (Entry<String, String> entry : mHeaders.entrySet()) { |
| 174 nativeAddHeader(mUrlRequestPeer, entry.getKey(), | 174 nativeAddHeader(mUrlRequestAdapter, entry.getKey(), |
| 175 entry.getValue()); | 175 entry.getValue()); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 if (mAdditionalHeaders != null) { | 179 if (mAdditionalHeaders != null) { |
| 180 for (Entry<String, String> entry : | 180 for (Entry<String, String> entry : |
| 181 mAdditionalHeaders.entrySet()) { | 181 mAdditionalHeaders.entrySet()) { |
| 182 nativeAddHeader(mUrlRequestPeer, entry.getKey(), | 182 nativeAddHeader(mUrlRequestAdapter, entry.getKey(), |
| 183 entry.getValue()); | 183 entry.getValue()); |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 | 186 |
| 187 if (mUploadData != null && mUploadData.length > 0) { | 187 if (mUploadData != null && mUploadData.length > 0) { |
| 188 nativeSetUploadData(mUrlRequestPeer, mUploadContentType, | 188 nativeSetUploadData(mUrlRequestAdapter, mUploadContentType, |
| 189 mUploadData); | 189 mUploadData); |
| 190 } else if (mUploadChannel != null) { | 190 } else if (mUploadChannel != null) { |
| 191 nativeSetUploadChannel(mUrlRequestPeer, mUploadContentType, | 191 nativeSetUploadChannel(mUrlRequestAdapter, mUploadContentType, |
| 192 mUploadContentLength); | 192 mUploadContentLength); |
| 193 } | 193 } |
| 194 | 194 |
| 195 nativeStart(mUrlRequestPeer); | 195 nativeStart(mUrlRequestAdapter); |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 | 198 |
| 199 public void cancel() { | 199 public void cancel() { |
| 200 synchronized (mLock) { | 200 synchronized (mLock) { |
| 201 if (mCanceled) { | 201 if (mCanceled) { |
| 202 return; | 202 return; |
| 203 } | 203 } |
| 204 | 204 |
| 205 mCanceled = true; | 205 mCanceled = true; |
| 206 | 206 |
| 207 if (!mRecycled) { | 207 if (!mRecycled) { |
| 208 nativeCancel(mUrlRequestPeer); | 208 nativeCancel(mUrlRequestAdapter); |
| 209 } | 209 } |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 | 212 |
| 213 public boolean isCanceled() { | 213 public boolean isCanceled() { |
| 214 synchronized (mLock) { | 214 synchronized (mLock) { |
| 215 return mCanceled; | 215 return mCanceled; |
| 216 } | 216 } |
| 217 } | 217 } |
| 218 | 218 |
| 219 public boolean isRecycled() { | 219 public boolean isRecycled() { |
| 220 synchronized (mLock) { | 220 synchronized (mLock) { |
| 221 return mRecycled; | 221 return mRecycled; |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 | 224 |
| 225 /** | 225 /** |
| 226 * Returns an exception if any, or null if the request was completed | 226 * Returns an exception if any, or null if the request was completed |
| 227 * successfully. | 227 * successfully. |
| 228 */ | 228 */ |
| 229 public IOException getException() { | 229 public IOException getException() { |
| 230 if (mSinkException != null) { | 230 if (mSinkException != null) { |
| 231 return mSinkException; | 231 return mSinkException; |
| 232 } | 232 } |
| 233 | 233 |
| 234 validateNotRecycled(); | 234 validateNotRecycled(); |
| 235 | 235 |
| 236 int errorCode = nativeGetErrorCode(mUrlRequestPeer); | 236 int errorCode = nativeGetErrorCode(mUrlRequestAdapter); |
| 237 switch (errorCode) { | 237 switch (errorCode) { |
| 238 case UrlRequestError.SUCCESS: | 238 case UrlRequestError.SUCCESS: |
| 239 return null; | 239 return null; |
| 240 case UrlRequestError.UNKNOWN: | 240 case UrlRequestError.UNKNOWN: |
| 241 return new IOException(nativeGetErrorString(mUrlRequestPeer)); | 241 return new IOException( |
| 242 nativeGetErrorString(mUrlRequestAdapter)); |
| 242 case UrlRequestError.MALFORMED_URL: | 243 case UrlRequestError.MALFORMED_URL: |
| 243 return new MalformedURLException("Malformed URL: " + mUrl); | 244 return new MalformedURLException("Malformed URL: " + mUrl); |
| 244 case UrlRequestError.CONNECTION_TIMED_OUT: | 245 case UrlRequestError.CONNECTION_TIMED_OUT: |
| 245 return new ConnectTimeoutException("Connection timed out"); | 246 return new ConnectTimeoutException("Connection timed out"); |
| 246 case UrlRequestError.UNKNOWN_HOST: | 247 case UrlRequestError.UNKNOWN_HOST: |
| 247 String host; | 248 String host; |
| 248 try { | 249 try { |
| 249 host = new URL(mUrl).getHost(); | 250 host = new URL(mUrl).getHost(); |
| 250 } catch (MalformedURLException e) { | 251 } catch (MalformedURLException e) { |
| 251 host = mUrl; | 252 host = mUrl; |
| 252 } | 253 } |
| 253 return new UnknownHostException("Unknown host: " + host); | 254 return new UnknownHostException("Unknown host: " + host); |
| 254 default: | 255 default: |
| 255 throw new IllegalStateException( | 256 throw new IllegalStateException( |
| 256 "Unrecognized error code: " + errorCode); | 257 "Unrecognized error code: " + errorCode); |
| 257 } | 258 } |
| 258 } | 259 } |
| 259 | 260 |
| 260 public int getHttpStatusCode() { | 261 public int getHttpStatusCode() { |
| 261 return nativeGetHttpStatusCode(mUrlRequestPeer); | 262 return nativeGetHttpStatusCode(mUrlRequestAdapter); |
| 262 } | 263 } |
| 263 | 264 |
| 264 /** | 265 /** |
| 265 * Content length as reported by the server. May be -1 or incorrect if the | 266 * Content length as reported by the server. May be -1 or incorrect if the |
| 266 * server returns the wrong number, which happens even with Google servers. | 267 * server returns the wrong number, which happens even with Google servers. |
| 267 */ | 268 */ |
| 268 public long getContentLength() { | 269 public long getContentLength() { |
| 269 return mContentLength; | 270 return mContentLength; |
| 270 } | 271 } |
| 271 | 272 |
| 272 public String getContentType() { | 273 public String getContentType() { |
| 273 return mContentType; | 274 return mContentType; |
| 274 } | 275 } |
| 275 | 276 |
| 276 public String getHeader(String name) { | 277 public String getHeader(String name) { |
| 277 validateHeadersAvailable(); | 278 validateHeadersAvailable(); |
| 278 return nativeGetHeader(mUrlRequestPeer, name); | 279 return nativeGetHeader(mUrlRequestAdapter, name); |
| 279 } | 280 } |
| 280 | 281 |
| 281 // All response headers. | 282 // All response headers. |
| 282 public Map<String, List<String>> getAllHeaders() { | 283 public Map<String, List<String>> getAllHeaders() { |
| 283 validateHeadersAvailable(); | 284 validateHeadersAvailable(); |
| 284 ResponseHeadersMap result = new ResponseHeadersMap(); | 285 ResponseHeadersMap result = new ResponseHeadersMap(); |
| 285 nativeGetAllHeaders(mUrlRequestPeer, result); | 286 nativeGetAllHeaders(mUrlRequestAdapter, result); |
| 286 return result; | 287 return result; |
| 287 } | 288 } |
| 288 | 289 |
| 289 /** | 290 /** |
| 290 * A callback invoked when the first chunk of the response has arrived. | 291 * A callback invoked when the first chunk of the response has arrived. |
| 291 */ | 292 */ |
| 292 @CalledByNative | 293 @CalledByNative |
| 293 protected void onResponseStarted() { | 294 protected void onResponseStarted() { |
| 294 mContentType = nativeGetContentType(mUrlRequestPeer); | 295 mContentType = nativeGetContentType(mUrlRequestAdapter); |
| 295 mContentLength = nativeGetContentLength(mUrlRequestPeer); | 296 mContentLength = nativeGetContentLength(mUrlRequestAdapter); |
| 296 mHeadersAvailable = true; | 297 mHeadersAvailable = true; |
| 297 } | 298 } |
| 298 | 299 |
| 299 /** | 300 /** |
| 300 * A callback invoked when the response has been fully consumed. | 301 * A callback invoked when the response has been fully consumed. |
| 301 */ | 302 */ |
| 302 protected void onRequestComplete() { | 303 protected void onRequestComplete() { |
| 303 } | 304 } |
| 304 | 305 |
| 305 /** | 306 /** |
| (...skipping 26 matching lines...) Expand all Loading... |
| 332 | 333 |
| 333 if (mRecycled) { | 334 if (mRecycled) { |
| 334 return; | 335 return; |
| 335 } | 336 } |
| 336 try { | 337 try { |
| 337 mSink.close(); | 338 mSink.close(); |
| 338 } catch (IOException e) { | 339 } catch (IOException e) { |
| 339 // Ignore | 340 // Ignore |
| 340 } | 341 } |
| 341 onRequestComplete(); | 342 onRequestComplete(); |
| 342 nativeDestroyRequestPeer(mUrlRequestPeer); | 343 nativeDestroyRequestAdapter(mUrlRequestAdapter); |
| 343 mUrlRequestPeer = 0; | 344 mUrlRequestAdapter = 0; |
| 344 mRecycled = true; | 345 mRecycled = true; |
| 345 } | 346 } |
| 346 } | 347 } |
| 347 | 348 |
| 348 /** | 349 /** |
| 349 * Appends header |name| with value |value| to |headersMap|. | 350 * Appends header |name| with value |value| to |headersMap|. |
| 350 */ | 351 */ |
| 351 @SuppressWarnings("unused") | 352 @SuppressWarnings("unused") |
| 352 @CalledByNative | 353 @CalledByNative |
| 353 private void onAppendResponseHeader(ResponseHeadersMap headersMap, | 354 private void onAppendResponseHeader(ResponseHeadersMap headersMap, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 private void validateHeadersAvailable() { | 404 private void validateHeadersAvailable() { |
| 404 if (!mHeadersAvailable) { | 405 if (!mHeadersAvailable) { |
| 405 throw new IllegalStateException("Response headers not available"); | 406 throw new IllegalStateException("Response headers not available"); |
| 406 } | 407 } |
| 407 } | 408 } |
| 408 | 409 |
| 409 public String getUrl() { | 410 public String getUrl() { |
| 410 return mUrl; | 411 return mUrl; |
| 411 } | 412 } |
| 412 | 413 |
| 413 private native long nativeCreateRequestPeer(long urlRequestContextPeer, | 414 private native long nativeCreateRequestAdapter( |
| 414 String url, int priority); | 415 long urlRequestContextAdapter, String url, int priority); |
| 415 | 416 |
| 416 private native void nativeAddHeader(long urlRequestPeer, String name, | 417 private native void nativeAddHeader(long urlRequestAdapter, String name, |
| 417 String value); | 418 String value); |
| 418 | 419 |
| 419 private native void nativeSetMethod(long urlRequestPeer, String method); | 420 private native void nativeSetMethod(long urlRequestAdapter, String method); |
| 420 | 421 |
| 421 private native void nativeSetUploadData(long urlRequestPeer, | 422 private native void nativeSetUploadData(long urlRequestAdapter, |
| 422 String contentType, byte[] content); | 423 String contentType, byte[] content); |
| 423 | 424 |
| 424 private native void nativeSetUploadChannel(long urlRequestPeer, | 425 private native void nativeSetUploadChannel(long urlRequestAdapter, |
| 425 String contentType, long contentLength); | 426 String contentType, long contentLength); |
| 426 | 427 |
| 427 private native void nativeStart(long urlRequestPeer); | 428 private native void nativeStart(long urlRequestAdapter); |
| 428 | 429 |
| 429 private native void nativeCancel(long urlRequestPeer); | 430 private native void nativeCancel(long urlRequestAdapter); |
| 430 | 431 |
| 431 private native void nativeDestroyRequestPeer(long urlRequestPeer); | 432 private native void nativeDestroyRequestAdapter(long urlRequestAdapter); |
| 432 | 433 |
| 433 private native int nativeGetErrorCode(long urlRequestPeer); | 434 private native int nativeGetErrorCode(long urlRequestAdapter); |
| 434 | 435 |
| 435 private native int nativeGetHttpStatusCode(long urlRequestPeer); | 436 private native int nativeGetHttpStatusCode(long urlRequestAdapter); |
| 436 | 437 |
| 437 private native String nativeGetErrorString(long urlRequestPeer); | 438 private native String nativeGetErrorString(long urlRequestAdapter); |
| 438 | 439 |
| 439 private native String nativeGetContentType(long urlRequestPeer); | 440 private native String nativeGetContentType(long urlRequestAdapter); |
| 440 | 441 |
| 441 private native long nativeGetContentLength(long urlRequestPeer); | 442 private native long nativeGetContentLength(long urlRequestAdapter); |
| 442 | 443 |
| 443 private native String nativeGetHeader(long urlRequestPeer, String name); | 444 private native String nativeGetHeader(long urlRequestAdapter, String name); |
| 444 | 445 |
| 445 private native void nativeGetAllHeaders(long urlRequestPeer, | 446 private native void nativeGetAllHeaders(long urlRequestAdapter, |
| 446 ResponseHeadersMap headers); | 447 ResponseHeadersMap headers); |
| 447 | 448 |
| 448 // Explicit class to work around JNI-generator generics confusion. | 449 // Explicit class to work around JNI-generator generics confusion. |
| 449 private class ResponseHeadersMap extends HashMap<String, List<String>> { | 450 private class ResponseHeadersMap extends HashMap<String, List<String>> { |
| 450 } | 451 } |
| 451 } | 452 } |
| OLD | NEW |