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 |
| 11 import java.io.IOException; | 11 import java.io.IOException; |
| 12 import java.net.MalformedURLException; | 12 import java.net.MalformedURLException; |
| 13 import java.net.URL; | 13 import java.net.URL; |
| 14 import java.net.UnknownHostException; | 14 import java.net.UnknownHostException; |
| 15 import java.nio.ByteBuffer; | 15 import java.nio.ByteBuffer; |
| 16 import java.nio.channels.ReadableByteChannel; | 16 import java.nio.channels.ReadableByteChannel; |
| 17 import java.nio.channels.WritableByteChannel; | 17 import java.nio.channels.WritableByteChannel; |
| 18 import java.util.ArrayList; | |
| 18 import java.util.HashMap; | 19 import java.util.HashMap; |
| 20 import java.util.List; | |
| 19 import java.util.Map; | 21 import java.util.Map; |
| 20 import java.util.Map.Entry; | 22 import java.util.Map.Entry; |
| 21 | 23 |
| 22 /** | 24 /** |
| 23 * Network request using the native http stack implementation. | 25 * Network request using the native http stack implementation. |
| 24 */ | 26 */ |
| 25 @JNINamespace("cronet") | 27 @JNINamespace("cronet") |
| 26 public class UrlRequest { | 28 public class UrlRequest { |
| 27 private static final class ContextLock { | 29 private static final class ContextLock { |
| 28 } | 30 } |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 | 271 |
| 270 public String getContentType() { | 272 public String getContentType() { |
| 271 return mContentType; | 273 return mContentType; |
| 272 } | 274 } |
| 273 | 275 |
| 274 public String getHeader(String name) { | 276 public String getHeader(String name) { |
| 275 validateHeadersAvailable(); | 277 validateHeadersAvailable(); |
| 276 return nativeGetHeader(mUrlRequestPeer, name); | 278 return nativeGetHeader(mUrlRequestPeer, name); |
| 277 } | 279 } |
| 278 | 280 |
| 281 // All response headers. | |
| 282 public Map<String, List<String>> getAllHeaders() { | |
| 283 validateHeadersAvailable(); | |
| 284 ResponseHeadersMap result = new ResponseHeadersMap(); | |
| 285 nativeGetAllHeaders(mUrlRequestPeer, result); | |
| 286 return result; | |
| 287 } | |
| 288 | |
| 279 /** | 289 /** |
| 280 * A callback invoked when the first chunk of the response has arrived. | 290 * A callback invoked when the first chunk of the response has arrived. |
| 281 */ | 291 */ |
| 282 @CalledByNative | 292 @CalledByNative |
| 283 protected void onResponseStarted() { | 293 protected void onResponseStarted() { |
| 284 mContentType = nativeGetContentType(mUrlRequestPeer); | 294 mContentType = nativeGetContentType(mUrlRequestPeer); |
| 285 mContentLength = nativeGetContentLength(mUrlRequestPeer); | 295 mContentLength = nativeGetContentLength(mUrlRequestPeer); |
| 286 mHeadersAvailable = true; | 296 mHeadersAvailable = true; |
| 287 } | 297 } |
| 288 | 298 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 } catch (IOException e) { | 338 } catch (IOException e) { |
| 329 // Ignore | 339 // Ignore |
| 330 } | 340 } |
| 331 onRequestComplete(); | 341 onRequestComplete(); |
| 332 nativeDestroyRequestPeer(mUrlRequestPeer); | 342 nativeDestroyRequestPeer(mUrlRequestPeer); |
| 333 mUrlRequestPeer = 0; | 343 mUrlRequestPeer = 0; |
| 334 mRecycled = true; | 344 mRecycled = true; |
| 335 } | 345 } |
| 336 } | 346 } |
| 337 | 347 |
| 348 /** | |
| 349 * Appends header |name| with value |value| to |headersMap|. | |
| 350 */ | |
| 351 @SuppressWarnings("unused") | |
| 352 @CalledByNative | |
| 353 private void onAppendResponseHeader(ResponseHeadersMap headersMap, | |
| 354 String name, String value) { | |
| 355 if (!headersMap.containsKey(name)) { | |
| 356 headersMap.put(name, new ArrayList<String>()); | |
| 357 } | |
| 358 headersMap.get(name).add(value); | |
| 359 } | |
| 360 | |
| 338 private void validateNotRecycled() { | 361 private void validateNotRecycled() { |
| 339 if (mRecycled) { | 362 if (mRecycled) { |
| 340 throw new IllegalStateException("Accessing recycled request"); | 363 throw new IllegalStateException("Accessing recycled request"); |
| 341 } | 364 } |
| 342 } | 365 } |
| 343 | 366 |
| 344 private void validateNotStarted() { | 367 private void validateNotStarted() { |
| 345 if (mStarted) { | 368 if (mStarted) { |
| 346 throw new IllegalStateException("Request already started"); | 369 throw new IllegalStateException("Request already started"); |
| 347 } | 370 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 382 | 405 |
| 383 private native int nativeGetHttpStatusCode(long urlRequestPeer); | 406 private native int nativeGetHttpStatusCode(long urlRequestPeer); |
| 384 | 407 |
| 385 private native String nativeGetErrorString(long urlRequestPeer); | 408 private native String nativeGetErrorString(long urlRequestPeer); |
| 386 | 409 |
| 387 private native String nativeGetContentType(long urlRequestPeer); | 410 private native String nativeGetContentType(long urlRequestPeer); |
| 388 | 411 |
| 389 private native long nativeGetContentLength(long urlRequestPeer); | 412 private native long nativeGetContentLength(long urlRequestPeer); |
| 390 | 413 |
| 391 private native String nativeGetHeader(long urlRequestPeer, String name); | 414 private native String nativeGetHeader(long urlRequestPeer, String name); |
| 415 | |
| 416 private native void nativeGetAllHeaders(long urlRequestPeer, | |
| 417 ResponseHeadersMap headers); | |
| 418 | |
| 419 | |
| 420 private class ResponseHeadersMap extends HashMap<String, List<String> > { | |
| 421 | |
| 422 } | |
|
mmenke
2014/08/04 21:39:55
I believe this is generally considered an anti-pat
mef
2014/08/04 21:57:52
Yeah, I've started without it, but JNI generator w
| |
| 423 | |
|
mmenke
2014/08/04 21:39:55
nit: Remove extra whitespace.
mef
2014/08/04 21:57:53
Done.
| |
| 392 } | 424 } |
| OLD | NEW |