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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 * @param priority priority of the stream which should be one of the | 154 * @param priority priority of the stream which should be one of the |
153 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values. | 155 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values. |
154 * @return the builder to facilitate chaining. | 156 * @return the builder to facilitate chaining. |
155 */ | 157 */ |
156 public Builder setPriority(@StreamPriority int priority) { | 158 public Builder setPriority(@StreamPriority int priority) { |
157 mPriority = priority; | 159 mPriority = priority; |
158 return this; | 160 return this; |
159 } | 161 } |
160 | 162 |
161 /** | 163 /** |
| 164 * Disables or enables auto flush. By default, data is flushed after |
| 165 * every {@link #write write()}. If the auto flush is disabled, the |
| 166 * client should explicitly call {@link #flush flush()} to flush the dat
a. |
| 167 * |
| 168 * @param disableAutoFlush if true, auto flush will be disabled. |
| 169 * @return the builder to facilitate chaining. |
| 170 */ |
| 171 public Builder disableAutoFlush(boolean disableAutoFlush) { |
| 172 mDisableAutoFlush = disableAutoFlush; |
| 173 return this; |
| 174 } |
| 175 |
| 176 /** |
162 * Creates a {@link BidirectionalStream} using configuration from this | 177 * Creates a {@link BidirectionalStream} using configuration from this |
163 * {@link Builder}. The returned {@code BidirectionalStream} can then be
started | 178 * {@link Builder}. The returned {@code BidirectionalStream} can then be
started |
164 * by calling {@link BidirectionalStream#start}. | 179 * by calling {@link BidirectionalStream#start}. |
165 * | 180 * |
166 * @return constructed {@link BidirectionalStream} using configuration f
rom | 181 * @return constructed {@link BidirectionalStream} using configuration f
rom |
167 * this {@link Builder} | 182 * this {@link Builder} |
168 */ | 183 */ |
169 public BidirectionalStream build() { | 184 public BidirectionalStream build() { |
170 return mCronetEngine.createBidirectionalStream( | 185 return mCronetEngine.createBidirectionalStream(mUrl, mCallback, mExe
cutor, mHttpMethod, |
171 mUrl, mCallback, mExecutor, mHttpMethod, mRequestHeaders, mP
riority); | 186 mRequestHeaders, mPriority, mDisableAutoFlush); |
172 } | 187 } |
173 } | 188 } |
174 | 189 |
175 /** | 190 /** |
176 * Callback class used to receive callbacks from a {@link BidirectionalStrea
m}. | 191 * Callback class used to receive callbacks from a {@link BidirectionalStrea
m}. |
177 */ | 192 */ |
178 public abstract static class Callback { | 193 public abstract static class Callback { |
179 /** | 194 /** |
180 * Invoked when request headers are sent. Indicates that stream has init
iated the request. | 195 * Invoked when the stream is ready for reading and writing. |
| 196 * Consumer may call {@link BidirectionalStream#read read()} to start re
ading data. |
181 * Consumer may call {@link BidirectionalStream#write write()} to start
writing data. | 197 * Consumer may call {@link BidirectionalStream#write write()} to start
writing data. |
182 * | 198 * |
183 * @param stream the stream on which request headers were sent | 199 * @param stream the stream that is ready. |
184 */ | 200 */ |
185 public abstract void onRequestHeadersSent(BidirectionalStream stream); | 201 public abstract void onStreamReady(BidirectionalStream stream); |
186 | 202 |
187 /** | 203 /** |
188 * 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 |
189 * {@code info.}{@link UrlResponseInfo#getAllHeaders getAllHeaders()}. | 205 * {@code info.}{@link UrlResponseInfo#getAllHeaders getAllHeaders()}. |
190 * Consumer must call {@link BidirectionalStream#read read()} to start r
eading. | 206 * Consumer may call {@link BidirectionalStream#read read()} to start re
ading. |
191 * 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 |
192 * stream. | 208 * stream. |
193 * | 209 * |
194 * @param stream the stream on which response headers were received | 210 * @param stream the stream on which response headers were received. |
195 * @param info the response information | 211 * @param info the response information. |
196 */ | 212 */ |
197 public abstract void onResponseHeadersReceived( | 213 public abstract void onResponseHeadersReceived( |
198 BidirectionalStream stream, UrlResponseInfo info); | 214 BidirectionalStream stream, UrlResponseInfo info); |
199 | 215 |
200 /** | 216 /** |
201 * Invoked when data is read into the buffer passed to {@link Bidirectio
nalStream#read | 217 * Invoked when data is read into the buffer passed to {@link Bidirectio
nalStream#read |
202 * read()}. Only part of the buffer may be populated. To continue readin
g, call {@link | 218 * read()}. Only part of the buffer may be populated. To continue readin
g, call {@link |
203 * BidirectionalStream#read read()}. It may be invoked after {@code | 219 * BidirectionalStream#read read()}. It may be invoked after {@code |
204 * onResponseTrailersReceived()}, if there was pending read data before
trailers were | 220 * onResponseTrailersReceived()}, if there was pending read data before
trailers were |
205 * received. | 221 * received. |
206 * | 222 * |
207 * @param stream the stream on which the read completed | 223 * @param stream the stream on which the read completed |
208 * @param info the response information | 224 * @param info the response information |
209 * @param buffer the buffer that was passed to {@link BidirectionalStrea
m#read read()}, | 225 * @param buffer the buffer that was passed to {@link BidirectionalStrea
m#read read()}, |
210 * now containing the received data. The buffer's position is set to
the end of | 226 * now containing the received data. The buffer's limit is not chang
ed. |
211 * the received data. If position is not updated, it means the remot
e side has signaled | 227 * The buffer's position is set to the end of the received data. If
position is not |
212 * that it will send no more data; future calls to {@code read()} wi
ll result in the | 228 * updated, it means the remote side has signaled that it will send
no more data. |
213 * same {@link #onReadCompleted onReadCompleted()} callback or {@lin
k #onSucceeded | 229 * @param endOfStream if true, this is the last read data, remote will n
ot send more data, |
214 * onSucceeded()} callback if {@link BidirectionalStream#write write
()} was invoked with | 230 * and the read side is closed. |
215 * {@code endOfStream} set to {@code true}. The buffer's limit is no
t changed. | 231 * |
216 */ | 232 */ |
217 public abstract void onReadCompleted( | 233 public abstract void onReadCompleted(BidirectionalStream stream, UrlResp
onseInfo info, |
218 BidirectionalStream stream, UrlResponseInfo info, ByteBuffer buf
fer); | 234 ByteBuffer buffer, boolean endOfStream); |
219 | 235 |
220 /** | 236 /** |
221 * Invoked when data passed to {@link BidirectionalStream#write write()}
is sent. The | 237 * Invoked when data passed to {@link BidirectionalStream#write write()}
is sent. The |
222 * buffer's position is updated to the end of the sent data. The buffer'
s limit is not | 238 * buffer's position is updated to the end of the sent data. The buffer'
s limit is not |
223 * changed. Not all available data may have been sent, so the buffer's p
osition is not | 239 * changed. Not all available data may have been sent, so the buffer's p
osition is not |
224 * necessarily equal to its limit. To continue writing, call | 240 * necessarily equal to its limit. To continue writing, call |
225 * {@link BidirectionalStream#write write()}. | 241 * {@link BidirectionalStream#write write()}. |
226 * | 242 * |
227 * @param stream the stream on which the write completed | 243 * @param stream the stream on which the write completed |
228 * @param info the response information | 244 * @param info the response information |
229 * @param buffer the buffer that was passed to {@link BidirectionalStrea
m#write write()}. | 245 * @param buffer the buffer that was passed to {@link BidirectionalStrea
m#write write()}. |
230 * The buffer's position is set to the end of the sent data. The buf
fer's limit | 246 * The buffer's position is set to the end of the sent data. The buf
fer's limit |
231 * is not changed. | 247 * is not changed. |
| 248 * @param endOfStream the endOfStream flag that was passed to the corres
ponding |
| 249 * {@link BidirectionalStream#write write()}. If true, the write sid
e is closed. |
232 */ | 250 */ |
233 public abstract void onWriteCompleted( | 251 public abstract void onWriteCompleted(BidirectionalStream stream, UrlRes
ponseInfo info, |
234 BidirectionalStream stream, UrlResponseInfo info, ByteBuffer buf
fer); | 252 ByteBuffer buffer, boolean endOfStream); |
235 | 253 |
236 /** | 254 /** |
237 * Invoked when trailers are received before closing the stream. Only in
voked | 255 * Invoked when trailers are received before closing the stream. Only in
voked |
238 * when server sends trailers, which it may not. May be invoked while th
ere is read data | 256 * when server sends trailers, which it may not. May be invoked while th
ere is read data |
239 * remaining in local buffer. | 257 * remaining in local buffer. |
240 * | 258 * |
241 * Default implementation takes no action. | 259 * Default implementation takes no action. |
242 * | 260 * |
243 * @param stream the stream on which response trailers were received | 261 * @param stream the stream on which response trailers were received |
244 * @param info the response information | 262 * @param info the response information |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 } | 324 } |
307 | 325 |
308 /** | 326 /** |
309 * Starts the stream, all callbacks go to the {@code callback} argument pass
ed to {@link | 327 * Starts the stream, all callbacks go to the {@code callback} argument pass
ed to {@link |
310 * BidirectionalStream.Builder}'s constructor. Should only be called once. | 328 * BidirectionalStream.Builder}'s constructor. Should only be called once. |
311 */ | 329 */ |
312 public abstract void start(); | 330 public abstract void start(); |
313 | 331 |
314 /** | 332 /** |
315 * Reads data from the stream into the provided buffer. | 333 * Reads data from the stream into the provided buffer. |
316 * Must only be called at most once in response to each invocation of the | 334 * Can only be called at most once in response to each invocation of the |
| 335 * {@link Callback#onStreamReady onStreamReady()}/ |
317 * {@link Callback#onResponseHeadersReceived onResponseHeadersReceived()} an
d {@link | 336 * {@link Callback#onResponseHeadersReceived onResponseHeadersReceived()} an
d {@link |
318 * Callback#onReadCompleted onReadCompleted()} methods of the {@link | 337 * Callback#onReadCompleted onReadCompleted()} methods of the {@link |
319 * Callback}. Each call will result in an invocation of one of the | 338 * Callback}. Each call will result in an invocation of one of the |
320 * {@link Callback Callback}'s {@link Callback#onReadCompleted onReadComplet
ed()} | 339 * {@link Callback Callback}'s {@link Callback#onReadCompleted onReadComplet
ed()} |
321 * method if data is read, its {@link Callback#onSucceeded onSucceeded()} me
thod if | 340 * method if data is read, or its {@link Callback#onFailed onFailed()} metho
d if |
322 * the stream is closed, or its {@link Callback#onFailed onFailed()} method
if | |
323 * there's an error. | 341 * there's an error. |
324 * | 342 * |
325 * An attempt to read data into {@code buffer} starting at {@code | 343 * An attempt to read data into {@code buffer} starting at {@code |
326 * buffer.position()} is begun. At most {@code buffer.remaining()} bytes are | 344 * buffer.position()} is begun. At most {@code buffer.remaining()} bytes are |
327 * read. {@code buffer.position()} is updated upon invocation of {@link | 345 * read. {@code buffer.position()} is updated upon invocation of {@link |
328 * Callback#onReadCompleted onReadCompleted()} to indicate how much data was
read. | 346 * Callback#onReadCompleted onReadCompleted()} to indicate how much data was
read. |
329 * | 347 * |
330 * @param buffer the {@link ByteBuffer} to read data into. Must be a | 348 * @param buffer the {@link ByteBuffer} to read data into. Must be a |
331 * direct ByteBuffer. The embedder must not read or modify buffer's | 349 * direct ByteBuffer. The embedder must not read or modify buffer's |
332 * position, limit, or data between its position and limit until | 350 * position, limit, or data between its position and limit until |
333 * {@link Callback#onReadCompleted onReadCompleted()}, {@link Callback#o
nCanceled | 351 * {@link Callback#onReadCompleted onReadCompleted()}, {@link Callback#o
nCanceled |
334 * onCanceled()}, {@link Callback#onSucceeded onSucceeded()} or {@link C
allback#onFailed | 352 * onCanceled()}, or {@link Callback#onFailed onFailed()} are invoked. |
335 * onFailed()} are invoked. | |
336 */ | 353 */ |
337 public abstract void read(ByteBuffer buffer); | 354 public abstract void read(ByteBuffer buffer); |
338 | 355 |
339 /** | 356 /** |
340 * Attempts to write data from the provided buffer into the stream. | 357 * Attempts to write data from the provided buffer into the stream. |
341 * Must only be called at most once in response to each invocation of the | 358 * If auto flush is disabled, data will be sent only after {@link #flush flu
sh()} is called. |
342 * {@link Callback#onRequestHeadersSent onRequestHeadersSent()} or {@link | 359 * Each call will result in an invocation of one of the |
343 * Callback#onWriteCompleted onWriteCompleted()} methods of the {@link Callb
ack}. | 360 * {@link Callback Callback}'s {@link Callback#onWriteCompleted onWriteCompl
eted()} |
344 * Each call will result in an asynchronous call to one of the {@link Callba
ck Callback}'s | 361 * method if data is sent, or its {@link Callback#onFailed onFailed()} metho
d if |
345 * {@link Callback#onWriteCompleted onWriteCompleted()} method if data | 362 * there's an error. |
346 * is sent, its {@link Callback#onSucceeded onSucceeded()} method if stream
is closed, | |
347 * or its {@link Callback#onFailed onFailed()} method if there's an error. | |
348 * | 363 * |
349 * An attempt to write data from {@code buffer} starting at {@code | 364 * An attempt to write data from {@code buffer} starting at {@code |
350 * buffer.position()} is begun. At most {@code buffer.remaining()} bytes are | 365 * buffer.position()} is begun. At most {@code buffer.remaining()} bytes are |
351 * written. {@code buffer.position()} is updated upon invocation of {@link | 366 * written. {@code buffer.position()} is updated upon invocation of {@link |
352 * Callback#onWriteCompleted onWriteCompleted()} to indicate how much data w
as written. | 367 * Callback#onWriteCompleted onWriteCompleted()} to indicate how much data w
as written. |
353 * | 368 * |
354 * @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 |
355 * direct ByteBuffer. The embedder must not read or modify buffer's | 370 * direct ByteBuffer. The embedder must not read or modify buffer's |
356 * position, limit, or data between its position and limit until | 371 * position, limit, or data between its position and limit until |
357 * {@link Callback#onWriteCompleted onWriteCompleted()}, {@link Callback
#onCanceled | 372 * {@link Callback#onWriteCompleted onWriteCompleted()}, {@link Callback
#onCanceled |
358 * onCanceled()}, {@link Callback#onSucceeded onSucceeded()} or {@link C
allback#onFailed | 373 * onCanceled()}, or {@link Callback#onFailed onFailed()} are invoked. C
an be empty |
359 * onFailed()} are invoked. Can be empty when {@code endOfStream} is {@c
ode true}. | 374 * when {@code endOfStream} is {@code true}. |
360 * @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, |
361 * 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 |
362 * 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
. |
363 */ | 378 */ |
364 public abstract void write(ByteBuffer buffer, boolean endOfStream); | 379 public abstract void write(ByteBuffer buffer, boolean endOfStream); |
365 | 380 |
366 /** | 381 /** |
| 382 * Flushes pending writes. This method should only be invoked when auto |
| 383 * flush is disabled through {@link Builder#disableAutoFlush}. |
| 384 */ |
| 385 public abstract void flush(); |
| 386 |
| 387 /** |
367 * 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}. |
368 * | 389 * |
369 * @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 |
370 * @param executor the executor on which the callback will be invoked | 391 * @param executor the executor on which the callback will be invoked |
371 */ | 392 */ |
372 // TODO(mef): May be last thing to be implemented on Android. | 393 // TODO(mef): May be last thing to be implemented on Android. |
373 public abstract void ping(PingCallback callback, Executor executor); | 394 public abstract void ping(PingCallback callback, Executor executor); |
374 | 395 |
375 /** | 396 /** |
376 * Updates stream flow control window. | 397 * Updates stream flow control window. |
(...skipping 20 matching lines...) Expand all Loading... |
397 /** | 418 /** |
398 * 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 |
399 * done (succeeded, canceled, or failed). | 420 * done (succeeded, canceled, or failed). |
400 * | 421 * |
401 * @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 |
402 * done (completed, canceled, or failed), otherwise returns {@code f
alse} | 423 * done (completed, canceled, or failed), otherwise returns {@code f
alse} |
403 * to indicate stream is not yet started or is in progress. | 424 * to indicate stream is not yet started or is in progress. |
404 */ | 425 */ |
405 public abstract boolean isDone(); | 426 public abstract boolean isDone(); |
406 } | 427 } |
OLD | NEW |