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