| 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 * @return the builder to facilitate chaining. |
| 168 */ |
| 169 public Builder setDisableAutoFlush() { |
| 170 mDisableAutoFlush = true; |
| 171 return this; |
| 172 } |
| 173 |
| 174 /** |
| 163 * Creates a {@link BidirectionalStream} using configuration from this | 175 * Creates a {@link BidirectionalStream} using configuration from this |
| 164 * {@link Builder}. The returned {@code BidirectionalStream} can then be
started | 176 * {@link Builder}. The returned {@code BidirectionalStream} can then be
started |
| 165 * by calling {@link BidirectionalStream#start}. | 177 * by calling {@link BidirectionalStream#start}. |
| 166 * | 178 * |
| 167 * @return constructed {@link BidirectionalStream} using configuration f
rom | 179 * @return constructed {@link BidirectionalStream} using configuration f
rom |
| 168 * this {@link Builder} | 180 * this {@link Builder} |
| 169 */ | 181 */ |
| 170 public BidirectionalStream build() { | 182 public BidirectionalStream build() { |
| 171 return mCronetEngine.createBidirectionalStream( | 183 return mCronetEngine.createBidirectionalStream(mUrl, mCallback, mExe
cutor, mHttpMethod, |
| 172 mUrl, mCallback, mExecutor, mHttpMethod, mRequestHeaders, mP
riority); | 184 mRequestHeaders, mPriority, mDisableAutoFlush); |
| 173 } | 185 } |
| 174 } | 186 } |
| 175 | 187 |
| 176 /** | 188 /** |
| 177 * Callback class used to receive callbacks from a {@link BidirectionalStrea
m}. | 189 * Callback class used to receive callbacks from a {@link BidirectionalStrea
m}. |
| 178 */ | 190 */ |
| 179 public abstract static class Callback { | 191 public abstract static class Callback { |
| 180 /** | 192 /** |
| 181 * Invoked when request headers are sent. Indicates that stream has init
iated the request. | 193 * Invoked when the stream is ready for reading and writing. |
| 182 * Consumer may call {@link BidirectionalStream#write write()} to start
writing data. | 194 * Consumer may call {@link BidirectionalStream#read read()} to start |
| 195 * reading. Consumer may call {@link BidirectionalStream#write write()} |
| 196 * to start writing data. |
| 183 * | 197 * |
| 184 * @param stream the stream on which request headers were sent | 198 * @param stream the stream that is ready |
| 185 */ | 199 */ |
| 186 public abstract void onRequestHeadersSent(BidirectionalStream stream); | 200 public abstract void onStreamReady(BidirectionalStream stream); |
| 187 | 201 |
| 188 /** | 202 /** |
| 189 * Invoked when initial response headers are received. Headers are avail
able from | 203 * Invoked when initial response headers are received. Headers are avail
able from |
| 190 * {@code info.}{@link UrlResponseInfo#getAllHeaders getAllHeaders()}. | 204 * {@code info.}{@link UrlResponseInfo#getAllHeaders getAllHeaders()}. |
| 191 * Consumer must call {@link BidirectionalStream#read read()} to start r
eading. | 205 * Consumer must call {@link BidirectionalStream#read read()} to start r
eading. |
| 192 * Consumer may call {@link BidirectionalStream#write write()} to start
writing or close the | 206 * Consumer may call {@link BidirectionalStream#write write()} to start
writing or close the |
| 193 * stream. | 207 * stream. |
| 194 * | 208 * |
| 195 * @param stream the stream on which response headers were received | 209 * @param stream the stream on which response headers were received |
| 196 * @param info the response information | 210 * @param info the response information |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 * position, limit, or data between its position and limit until | 347 * position, limit, or data between its position and limit until |
| 334 * {@link Callback#onReadCompleted onReadCompleted()}, {@link Callback#o
nCanceled | 348 * {@link Callback#onReadCompleted onReadCompleted()}, {@link Callback#o
nCanceled |
| 335 * onCanceled()}, {@link Callback#onSucceeded onSucceeded()} or {@link C
allback#onFailed | 349 * onCanceled()}, {@link Callback#onSucceeded onSucceeded()} or {@link C
allback#onFailed |
| 336 * onFailed()} are invoked. | 350 * onFailed()} are invoked. |
| 337 */ | 351 */ |
| 338 public abstract void read(ByteBuffer buffer); | 352 public abstract void read(ByteBuffer buffer); |
| 339 | 353 |
| 340 /** | 354 /** |
| 341 * Attempts to write data from the provided buffer into the stream. | 355 * Attempts to write data from the provided buffer into the stream. |
| 342 * Must only be called at most once in response to each invocation of the | 356 * Must only be called at most once in response to each invocation of the |
| 343 * {@link Callback#onRequestHeadersSent onRequestHeadersSent()} or {@link | 357 * {@link Callback#onStreamReady onStreamReady()} or {@link |
| 344 * Callback#onWriteCompleted onWriteCompleted()} methods of the {@link Callb
ack}. | 358 * Callback#onWriteCompleted onWriteCompleted()} methods of the {@link Callb
ack}. |
| 345 * Each call will result in an asynchronous call to one of the {@link Callba
ck Callback}'s | 359 * Each call will result in an asynchronous call to one of the {@link Callba
ck Callback}'s |
| 346 * {@link Callback#onWriteCompleted onWriteCompleted()} method if data | 360 * {@link Callback#onWriteCompleted onWriteCompleted()} method if data |
| 347 * is sent, its {@link Callback#onSucceeded onSucceeded()} method if stream
is closed, | 361 * is sent, its {@link Callback#onSucceeded onSucceeded()} method if stream
is closed, |
| 348 * or its {@link Callback#onFailed onFailed()} method if there's an error. | 362 * or its {@link Callback#onFailed onFailed()} method if there's an error. |
| 349 * | 363 * |
| 350 * An attempt to write data from {@code buffer} starting at {@code | 364 * An attempt to write data from {@code buffer} starting at {@code |
| 351 * buffer.position()} is begun. At most {@code buffer.remaining()} bytes are | 365 * buffer.position()} is begun. At most {@code buffer.remaining()} bytes are |
| 352 * written. {@code buffer.position()} is updated upon invocation of {@link | 366 * written. {@code buffer.position()} is updated upon invocation of {@link |
| 353 * Callback#onWriteCompleted onWriteCompleted()} to indicate how much data w
as written. | 367 * Callback#onWriteCompleted onWriteCompleted()} to indicate how much data w
as written. |
| 354 * | 368 * |
| 355 * @param buffer the {@link ByteBuffer} to write data from. Must be a | 369 * @param buffer the {@link ByteBuffer} to write data from. Must be a |
| 356 * direct ByteBuffer. The embedder must not read or modify buffer's | 370 * direct ByteBuffer. The embedder must not read or modify buffer's |
| 357 * position, limit, or data between its position and limit until | 371 * position, limit, or data between its position and limit until |
| 358 * {@link Callback#onWriteCompleted onWriteCompleted()}, {@link Callback
#onCanceled | 372 * {@link Callback#onWriteCompleted onWriteCompleted()}, {@link Callback
#onCanceled |
| 359 * onCanceled()}, {@link Callback#onSucceeded onSucceeded()} or {@link C
allback#onFailed | 373 * onCanceled()}, {@link Callback#onSucceeded onSucceeded()} or {@link C
allback#onFailed |
| 360 * onFailed()} are invoked. Can be empty when {@code endOfStream} is {@c
ode true}. | 374 * 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, | 375 * @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 | 376 * 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
. | 377 * stream or a fully closed stream if the remote side has already closed
. |
| 364 */ | 378 */ |
| 365 public abstract void write(ByteBuffer buffer, boolean endOfStream); | 379 public abstract void write(ByteBuffer buffer, boolean endOfStream); |
| 366 | 380 |
| 367 /** | 381 /** |
| 382 * Flushes pending writes. This method should only be invoked when auto |
| 383 * flush is disable through {@link Builder#setDisableAutoFlush}. |
| 384 */ |
| 385 public abstract void flush(); |
| 386 |
| 387 /** |
| 368 * Pings remote end-point. {@code callback} methods will be invoked on {@cod
e executor}. | 388 * Pings remote end-point. {@code callback} methods will be invoked on {@cod
e executor}. |
| 369 * | 389 * |
| 370 * @param callback the callback that will be invoked when ping succeeds or f
ails | 390 * @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 | 391 * @param executor the executor on which the callback will be invoked |
| 372 */ | 392 */ |
| 373 // TODO(mef): May be last thing to be implemented on Android. | 393 // TODO(mef): May be last thing to be implemented on Android. |
| 374 public abstract void ping(PingCallback callback, Executor executor); | 394 public abstract void ping(PingCallback callback, Executor executor); |
| 375 | 395 |
| 376 /** | 396 /** |
| 377 * Updates stream flow control window. | 397 * Updates stream flow control window. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 398 /** | 418 /** |
| 399 * Returns {@code true} if the stream was successfully started and is now | 419 * Returns {@code true} if the stream was successfully started and is now |
| 400 * done (succeeded, canceled, or failed). | 420 * done (succeeded, canceled, or failed). |
| 401 * | 421 * |
| 402 * @return {@code true} if the stream was successfully started and is now | 422 * @return {@code true} if the stream was successfully started and is now |
| 403 * done (completed, canceled, or failed), otherwise returns {@code f
alse} | 423 * done (completed, canceled, or failed), otherwise returns {@code f
alse} |
| 404 * to indicate stream is not yet started or is in progress. | 424 * to indicate stream is not yet started or is in progress. |
| 405 */ | 425 */ |
| 406 public abstract boolean isDone(); | 426 public abstract boolean isDone(); |
| 407 } | 427 } |
| OLD | NEW |