Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 android.support.annotation.IntDef; | 7 import android.support.annotation.IntDef; |
| 8 | 8 |
| 9 import java.lang.annotation.Retention; | 9 import java.lang.annotation.Retention; |
| 10 import java.lang.annotation.RetentionPolicy; | 10 import java.lang.annotation.RetentionPolicy; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 private final Executor mExecutor; | 40 private final Executor mExecutor; |
| 41 // List of request headers, stored as header field name and value pairs. | 41 // List of request headers, stored as header field name and value pairs. |
| 42 private final ArrayList<Map.Entry<String, String>> mRequestHeaders = | 42 private final ArrayList<Map.Entry<String, String>> mRequestHeaders = |
| 43 new ArrayList<Map.Entry<String, String>>(); | 43 new ArrayList<Map.Entry<String, String>>(); |
| 44 | 44 |
| 45 // HTTP method for the request. Default to POST. | 45 // HTTP method for the request. Default to POST. |
| 46 private String mHttpMethod = "POST"; | 46 private String mHttpMethod = "POST"; |
| 47 // Priority of the stream. Default is medium. | 47 // Priority of the stream. Default is medium. |
| 48 @StreamPriority private int mPriority = STREAM_PRIORITY_MEDIUM; | 48 @StreamPriority private int mPriority = STREAM_PRIORITY_MEDIUM; |
| 49 | 49 |
| 50 private boolean mDisableAutoFlush; | |
| 51 | |
| 50 /** | 52 /** |
| 51 * Creates a builder for {@link BidirectionalStream} objects. All callba cks for | 53 * Creates a builder for {@link BidirectionalStream} objects. All callba cks for |
| 52 * generated {@code BidirectionalStream} objects will be invoked on | 54 * generated {@code BidirectionalStream} objects will be invoked on |
| 53 * {@code executor}. {@code executor} must not run tasks on the | 55 * {@code executor}. {@code executor} must not run tasks on the |
| 54 * current thread, otherwise the networking operations may block and exc eptions | 56 * current thread, otherwise the networking operations may block and exc eptions |
| 55 * may be thrown at shutdown time. | 57 * may be thrown at shutdown time. |
| 56 * | 58 * |
| 57 * @param url the URL for the generated stream | 59 * @param url the URL for the generated stream |
| 58 * @param callback the {@link Callback} object that gets invoked upon di fferent events | 60 * @param callback the {@link Callback} object that gets invoked upon di fferent events |
| 59 * occuring | 61 * occuring |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 * @param priority priority of the stream which should be one of the | 155 * @param priority priority of the stream which should be one of the |
| 154 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values. | 156 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values. |
| 155 * @return the builder to facilitate chaining. | 157 * @return the builder to facilitate chaining. |
| 156 */ | 158 */ |
| 157 public Builder setPriority(@StreamPriority int priority) { | 159 public Builder setPriority(@StreamPriority int priority) { |
| 158 mPriority = priority; | 160 mPriority = priority; |
| 159 return this; | 161 return this; |
| 160 } | 162 } |
| 161 | 163 |
| 162 /** | 164 /** |
| 165 * By default, data is flushed after every {@link #write write()}. This | |
| 166 * is to disable auto flush. | |
| 167 */ | |
| 168 public Builder setDisableAutoFlush() { | |
|
kapishnikov
2016/04/11 23:09:41
I would change the method to accept the boolean pa
xunjieli
2016/04/12 18:01:48
Done. Good idea!
| |
| 169 mDisableAutoFlush = true; | |
| 170 return this; | |
| 171 } | |
| 172 | |
| 173 /** | |
| 163 * Creates a {@link BidirectionalStream} using configuration from this | 174 * Creates a {@link BidirectionalStream} using configuration from this |
| 164 * {@link Builder}. The returned {@code BidirectionalStream} can then be started | 175 * {@link Builder}. The returned {@code BidirectionalStream} can then be started |
| 165 * by calling {@link BidirectionalStream#start}. | 176 * by calling {@link BidirectionalStream#start}. |
| 166 * | 177 * |
| 167 * @return constructed {@link BidirectionalStream} using configuration f rom | 178 * @return constructed {@link BidirectionalStream} using configuration f rom |
| 168 * this {@link Builder} | 179 * this {@link Builder} |
| 169 */ | 180 */ |
| 170 public BidirectionalStream build() { | 181 public BidirectionalStream build() { |
| 171 return mCronetEngine.createBidirectionalStream( | 182 return mCronetEngine.createBidirectionalStream(mUrl, mCallback, mExe cutor, mHttpMethod, |
| 172 mUrl, mCallback, mExecutor, mHttpMethod, mRequestHeaders, mP riority); | 183 mRequestHeaders, mPriority, mDisableAutoFlush); |
| 173 } | 184 } |
| 174 } | 185 } |
| 175 | 186 |
| 176 /** | 187 /** |
| 177 * Callback class used to receive callbacks from a {@link BidirectionalStrea m}. | 188 * Callback class used to receive callbacks from a {@link BidirectionalStrea m}. |
| 178 */ | 189 */ |
| 179 public abstract static class Callback { | 190 public abstract static class Callback { |
| 180 /** | 191 /** |
| 181 * Invoked when request headers are sent. Indicates that stream has init iated the request. | 192 * Invoked when the stream is ready for reading and writing. |
| 182 * Consumer may call {@link BidirectionalStream#write write()} to start writing data. | 193 * Consumer may call {@link BidirectionalStream#read read()} to start |
|
kapishnikov
2016/04/11 23:09:41
Does it mean that the consumer does not need to wa
xunjieli
2016/04/12 18:01:48
Yes, that is allowed.
| |
| 194 * reading. Consumer may call {@link BidirectionalStream#write write()} | |
| 195 * to start writing data. | |
| 183 * | 196 * |
| 184 * @param stream the stream on which request headers were sent | 197 * @param stream the stream that is ready |
| 185 */ | 198 */ |
| 186 public abstract void onRequestHeadersSent(BidirectionalStream stream); | 199 public abstract void onStreamReady(BidirectionalStream stream); |
| 187 | 200 |
| 188 /** | 201 /** |
| 189 * Invoked when initial response headers are received. Headers are avail able from | 202 * Invoked when initial response headers are received. Headers are avail able from |
| 190 * {@code info.}{@link UrlResponseInfo#getAllHeaders getAllHeaders()}. | 203 * {@code info.}{@link UrlResponseInfo#getAllHeaders getAllHeaders()}. |
| 191 * Consumer must call {@link BidirectionalStream#read read()} to start r eading. | 204 * Consumer must call {@link BidirectionalStream#read read()} to start r eading. |
|
kapishnikov
2016/04/11 23:09:41
Javadoc says that the consumer must call read(). W
xunjieli
2016/04/12 18:01:48
Good catch. I think the Javadoc should read "may"
| |
| 192 * Consumer may call {@link BidirectionalStream#write write()} to start writing or close the | 205 * Consumer may call {@link BidirectionalStream#write write()} to start writing or close the |
| 193 * stream. | 206 * stream. |
| 194 * | 207 * |
| 195 * @param stream the stream on which response headers were received | 208 * @param stream the stream on which response headers were received |
| 196 * @param info the response information | 209 * @param info the response information |
| 197 */ | 210 */ |
| 198 public abstract void onResponseHeadersReceived( | 211 public abstract void onResponseHeadersReceived( |
| 199 BidirectionalStream stream, UrlResponseInfo info); | 212 BidirectionalStream stream, UrlResponseInfo info); |
| 200 | 213 |
| 201 /** | 214 /** |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 | 321 |
| 309 /** | 322 /** |
| 310 * Starts the stream, all callbacks go to the {@code callback} argument pass ed to {@link | 323 * Starts the stream, all callbacks go to the {@code callback} argument pass ed to {@link |
| 311 * BidirectionalStream.Builder}'s constructor. Should only be called once. | 324 * BidirectionalStream.Builder}'s constructor. Should only be called once. |
| 312 */ | 325 */ |
| 313 public abstract void start(); | 326 public abstract void start(); |
| 314 | 327 |
| 315 /** | 328 /** |
| 316 * Reads data from the stream into the provided buffer. | 329 * Reads data from the stream into the provided buffer. |
| 317 * Must only be called at most once in response to each invocation of the | 330 * Must only be called at most once in response to each invocation of the |
| 318 * {@link Callback#onResponseHeadersReceived onResponseHeadersReceived()} an d {@link | 331 * {@link Callback#onResponseHeadersReceived onResponseHeadersReceived()} an d {@link |
|
kapishnikov
2016/04/11 23:09:41
Related to the previous comments. Does not mention
xunjieli
2016/04/12 18:01:48
Done.
| |
| 319 * Callback#onReadCompleted onReadCompleted()} methods of the {@link | 332 * Callback#onReadCompleted onReadCompleted()} methods of the {@link |
| 320 * Callback}. Each call will result in an invocation of one of the | 333 * Callback}. Each call will result in an invocation of one of the |
| 321 * {@link Callback Callback}'s {@link Callback#onReadCompleted onReadComplet ed()} | 334 * {@link Callback Callback}'s {@link Callback#onReadCompleted onReadComplet ed()} |
| 322 * method if data is read, its {@link Callback#onSucceeded onSucceeded()} me thod if | 335 * method if data is read, its {@link Callback#onSucceeded onSucceeded()} me thod if |
| 323 * the stream is closed, or its {@link Callback#onFailed onFailed()} method if | 336 * the stream is closed, or its {@link Callback#onFailed onFailed()} method if |
| 324 * there's an error. | 337 * there's an error. |
| 325 * | 338 * |
| 326 * An attempt to read data into {@code buffer} starting at {@code | 339 * An attempt to read data into {@code buffer} starting at {@code |
| 327 * buffer.position()} is begun. At most {@code buffer.remaining()} bytes are | 340 * buffer.position()} is begun. At most {@code buffer.remaining()} bytes are |
| 328 * read. {@code buffer.position()} is updated upon invocation of {@link | 341 * read. {@code buffer.position()} is updated upon invocation of {@link |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 358 * {@link Callback#onWriteCompleted onWriteCompleted()}, {@link Callback #onCanceled | 371 * {@link Callback#onWriteCompleted onWriteCompleted()}, {@link Callback #onCanceled |
| 359 * onCanceled()}, {@link Callback#onSucceeded onSucceeded()} or {@link C allback#onFailed | 372 * onCanceled()}, {@link Callback#onSucceeded onSucceeded()} or {@link C allback#onFailed |
| 360 * onFailed()} are invoked. Can be empty when {@code endOfStream} is {@c ode true}. | 373 * onFailed()} are invoked. Can be empty when {@code endOfStream} is {@c ode true}. |
| 361 * @param endOfStream if {@code true}, then {@code buffer} is the last buffe r to be written, | 374 * @param endOfStream if {@code true}, then {@code buffer} is the last buffe r to be written, |
| 362 * and once written, stream is closed from the client side, resulting in half-closed | 375 * and once written, stream is closed from the client side, resulting in half-closed |
| 363 * stream or a fully closed stream if the remote side has already closed . | 376 * stream or a fully closed stream if the remote side has already closed . |
| 364 */ | 377 */ |
| 365 public abstract void write(ByteBuffer buffer, boolean endOfStream); | 378 public abstract void write(ByteBuffer buffer, boolean endOfStream); |
| 366 | 379 |
| 367 /** | 380 /** |
| 381 * Flushes pending writes. This method should only be invoked when auto | |
| 382 * flush is disable through {@link Builder#setDisableAutoFlush}. | |
|
kapishnikov
2016/04/11 23:09:41
disable => disabled
xunjieli
2016/04/12 18:01:48
Done.
| |
| 383 */ | |
| 384 public abstract void flush(); | |
| 385 | |
| 386 /** | |
| 368 * Pings remote end-point. {@code callback} methods will be invoked on {@cod e executor}. | 387 * Pings remote end-point. {@code callback} methods will be invoked on {@cod e executor}. |
| 369 * | 388 * |
| 370 * @param callback the callback that will be invoked when ping succeeds or f ails | 389 * @param callback the callback that will be invoked when ping succeeds or f ails |
| 371 * @param executor the executor on which the callback will be invoked | 390 * @param executor the executor on which the callback will be invoked |
| 372 */ | 391 */ |
| 373 // TODO(mef): May be last thing to be implemented on Android. | 392 // TODO(mef): May be last thing to be implemented on Android. |
| 374 public abstract void ping(PingCallback callback, Executor executor); | 393 public abstract void ping(PingCallback callback, Executor executor); |
| 375 | 394 |
| 376 /** | 395 /** |
| 377 * Updates stream flow control window. | 396 * Updates stream flow control window. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 398 /** | 417 /** |
| 399 * Returns {@code true} if the stream was successfully started and is now | 418 * Returns {@code true} if the stream was successfully started and is now |
| 400 * done (succeeded, canceled, or failed). | 419 * done (succeeded, canceled, or failed). |
| 401 * | 420 * |
| 402 * @return {@code true} if the stream was successfully started and is now | 421 * @return {@code true} if the stream was successfully started and is now |
| 403 * done (completed, canceled, or failed), otherwise returns {@code f alse} | 422 * done (completed, canceled, or failed), otherwise returns {@code f alse} |
| 404 * to indicate stream is not yet started or is in progress. | 423 * to indicate stream is not yet started or is in progress. |
| 405 */ | 424 */ |
| 406 public abstract boolean isDone(); | 425 public abstract boolean isDone(); |
| 407 } | 426 } |
| OLD | NEW |