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 org.chromium.base.Log; | 7 import org.chromium.base.Log; |
8 import org.chromium.base.VisibleForTesting; | 8 import org.chromium.base.VisibleForTesting; |
9 import org.chromium.base.annotations.CalledByNative; | 9 import org.chromium.base.annotations.CalledByNative; |
10 import org.chromium.base.annotations.JNINamespace; | 10 import org.chromium.base.annotations.JNINamespace; |
11 import org.chromium.base.annotations.NativeClassQualifiedName; | 11 import org.chromium.base.annotations.NativeClassQualifiedName; |
12 | 12 |
13 import java.nio.ByteBuffer; | 13 import java.nio.ByteBuffer; |
14 import java.util.AbstractMap; | 14 import java.util.AbstractMap; |
15 import java.util.ArrayList; | 15 import java.util.ArrayList; |
16 import java.util.Arrays; | 16 import java.util.Arrays; |
17 import java.util.LinkedList; | |
17 import java.util.List; | 18 import java.util.List; |
18 import java.util.Map; | 19 import java.util.Map; |
19 import java.util.concurrent.Executor; | 20 import java.util.concurrent.Executor; |
20 import java.util.concurrent.RejectedExecutionException; | 21 import java.util.concurrent.RejectedExecutionException; |
21 | 22 |
22 import javax.annotation.concurrent.GuardedBy; | 23 import javax.annotation.concurrent.GuardedBy; |
23 | 24 |
24 /** | 25 /** |
25 * {@link BidirectionalStream} implementation using Chromium network stack. | 26 * {@link BidirectionalStream} implementation using Chromium network stack. |
26 * All @CalledByNative methods are called on the native network thread | 27 * All @CalledByNative methods are called on the native network thread |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 WRITING_DONE, | 63 WRITING_DONE, |
63 } | 64 } |
64 | 65 |
65 private final CronetUrlRequestContext mRequestContext; | 66 private final CronetUrlRequestContext mRequestContext; |
66 private final Executor mExecutor; | 67 private final Executor mExecutor; |
67 private final Callback mCallback; | 68 private final Callback mCallback; |
68 private final String mInitialUrl; | 69 private final String mInitialUrl; |
69 private final int mInitialPriority; | 70 private final int mInitialPriority; |
70 private final String mInitialMethod; | 71 private final String mInitialMethod; |
71 private final String mRequestHeaders[]; | 72 private final String mRequestHeaders[]; |
73 private final boolean mDisableAutoFlush; | |
72 | 74 |
73 /* | 75 /* |
74 * Synchronizes access to mNativeStream, mReadState and mWriteState. | 76 * Synchronizes access to mNativeStream, mReadState and mWriteState. |
75 */ | 77 */ |
76 private final Object mNativeStreamLock = new Object(); | 78 private final Object mNativeStreamLock = new Object(); |
77 | 79 |
80 @GuardedBy("mNativeStreamLock") | |
81 // Only relevant if mDisableAutoFlush is true. | |
82 private final LinkedList<ByteBuffer> mPendingData; | |
83 | |
84 @GuardedBy("mNativeStreamLock") | |
85 // Only relevant if mDisableAutoFlush is true. | |
kapishnikov
2016/04/11 23:09:41
Would it be worth to unify the implementation of a
xunjieli
2016/04/12 18:01:48
Done. Although partially. I think we probably shou
| |
86 private boolean mEndOfStreamWritten; | |
87 | |
78 /* Native BidirectionalStream object, owned by CronetBidirectionalStream. */ | 88 /* Native BidirectionalStream object, owned by CronetBidirectionalStream. */ |
79 @GuardedBy("mNativeStreamLock") | 89 @GuardedBy("mNativeStreamLock") |
80 private long mNativeStream; | 90 private long mNativeStream; |
81 | 91 |
82 /** | 92 /** |
83 * Read state is tracking reading flow. | 93 * Read state is tracking reading flow. |
84 * / <--- READING <--- \ | 94 * / <--- READING <--- \ |
85 * | | | 95 * | | |
86 * \ / | 96 * \ / |
87 * NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS | 97 * NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 } | 184 } |
175 mCallback.onWriteCompleted(CronetBidirectionalStream.this, mResp onseInfo, buffer); | 185 mCallback.onWriteCompleted(CronetBidirectionalStream.this, mResp onseInfo, buffer); |
176 } catch (Exception e) { | 186 } catch (Exception e) { |
177 onCallbackException(e); | 187 onCallbackException(e); |
178 } | 188 } |
179 } | 189 } |
180 } | 190 } |
181 | 191 |
182 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url , | 192 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url , |
183 @BidirectionalStream.Builder.StreamPriority int priority, Callback c allback, | 193 @BidirectionalStream.Builder.StreamPriority int priority, Callback c allback, |
184 Executor executor, String httpMethod, List<Map.Entry<String, String> > requestHeaders) { | 194 Executor executor, String httpMethod, List<Map.Entry<String, String> > requestHeaders, |
195 boolean disableAutoFlush) { | |
185 mRequestContext = requestContext; | 196 mRequestContext = requestContext; |
186 mInitialUrl = url; | 197 mInitialUrl = url; |
187 mInitialPriority = convertStreamPriority(priority); | 198 mInitialPriority = convertStreamPriority(priority); |
188 mCallback = callback; | 199 mCallback = callback; |
189 mExecutor = executor; | 200 mExecutor = executor; |
190 mInitialMethod = httpMethod; | 201 mInitialMethod = httpMethod; |
191 mRequestHeaders = stringsFromHeaderList(requestHeaders); | 202 mRequestHeaders = stringsFromHeaderList(requestHeaders); |
203 mDisableAutoFlush = disableAutoFlush; | |
204 if (mDisableAutoFlush) { | |
205 mPendingData = new LinkedList<ByteBuffer>(); | |
206 } else { | |
207 mPendingData = null; | |
208 } | |
192 } | 209 } |
193 | 210 |
194 @Override | 211 @Override |
195 public void start() { | 212 public void start() { |
196 synchronized (mNativeStreamLock) { | 213 synchronized (mNativeStreamLock) { |
197 if (mReadState != State.NOT_STARTED) { | 214 if (mReadState != State.NOT_STARTED) { |
198 throw new IllegalStateException("Stream is already started."); | 215 throw new IllegalStateException("Stream is already started."); |
199 } | 216 } |
200 try { | 217 try { |
201 mNativeStream = nativeCreateBidirectionalStream( | 218 mNativeStream = nativeCreateBidirectionalStream( |
202 mRequestContext.getUrlRequestContextAdapter()); | 219 mRequestContext.getUrlRequestContextAdapter(), mDisableA utoFlush); |
203 mRequestContext.onRequestStarted(); | 220 mRequestContext.onRequestStarted(); |
204 // Non-zero startResult means an argument error. | 221 // Non-zero startResult means an argument error. |
205 int startResult = nativeStart(mNativeStream, mInitialUrl, mIniti alPriority, | 222 int startResult = nativeStart(mNativeStream, mInitialUrl, mIniti alPriority, |
206 mInitialMethod, mRequestHeaders, !doesMethodAllowWriteDa ta(mInitialMethod)); | 223 mInitialMethod, mRequestHeaders, !doesMethodAllowWriteDa ta(mInitialMethod)); |
207 if (startResult == -1) { | 224 if (startResult == -1) { |
208 throw new IllegalArgumentException("Invalid http method " + mInitialMethod); | 225 throw new IllegalArgumentException("Invalid http method " + mInitialMethod); |
209 } | 226 } |
210 if (startResult > 0) { | 227 if (startResult > 0) { |
211 int headerPos = startResult - 1; | 228 int headerPos = startResult - 1; |
212 throw new IllegalArgumentException("Invalid header " | 229 throw new IllegalArgumentException("Invalid header " |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 } | 263 } |
247 } | 264 } |
248 | 265 |
249 @Override | 266 @Override |
250 public void write(ByteBuffer buffer, boolean endOfStream) { | 267 public void write(ByteBuffer buffer, boolean endOfStream) { |
251 synchronized (mNativeStreamLock) { | 268 synchronized (mNativeStreamLock) { |
252 Preconditions.checkDirect(buffer); | 269 Preconditions.checkDirect(buffer); |
253 if (!buffer.hasRemaining() && !endOfStream) { | 270 if (!buffer.hasRemaining() && !endOfStream) { |
254 throw new IllegalArgumentException("Empty buffer before end of s tream."); | 271 throw new IllegalArgumentException("Empty buffer before end of s tream."); |
255 } | 272 } |
273 if (isDoneLocked()) { | |
kapishnikov
2016/04/11 23:09:42
Should we throw an IllegalStateException if there
xunjieli
2016/04/12 18:01:48
Done.
| |
274 return; | |
275 } | |
276 | |
277 if (mDisableAutoFlush) { | |
278 mPendingData.add(buffer); | |
279 if (endOfStream) { | |
280 mEndOfStreamWritten = true; | |
281 } | |
282 return; | |
283 } | |
256 if (mWriteState != State.WAITING_FOR_WRITE) { | 284 if (mWriteState != State.WAITING_FOR_WRITE) { |
257 throw new IllegalStateException("Unexpected write attempt."); | 285 throw new IllegalStateException("Unexpected write attempt."); |
258 } | 286 } |
259 if (isDoneLocked()) { | |
260 return; | |
261 } | |
262 if (mOnWriteCompletedTask == null) { | 287 if (mOnWriteCompletedTask == null) { |
263 mOnWriteCompletedTask = new OnWriteCompletedRunnable(); | 288 mOnWriteCompletedTask = new OnWriteCompletedRunnable(); |
264 } | 289 } |
265 mOnWriteCompletedTask.mEndOfStream = endOfStream; | |
266 mWriteState = State.WRITING; | 290 mWriteState = State.WRITING; |
267 if (!nativeWriteData( | 291 if (!nativeWriteData( |
268 mNativeStream, buffer, buffer.position(), buffer.limit() , endOfStream)) { | 292 mNativeStream, buffer, buffer.position(), buffer.limit() , endOfStream)) { |
269 // Still waiting on write. This is just to have consistent | 293 // Still waiting on write. This is just to have consistent |
270 // behavior with the other error cases. | 294 // behavior with the other error cases. |
271 mWriteState = State.WAITING_FOR_WRITE; | 295 mWriteState = State.WAITING_FOR_WRITE; |
272 throw new IllegalArgumentException("Unable to call native write" ); | 296 throw new IllegalArgumentException("Unable to call native write" ); |
273 } | 297 } |
274 } | 298 } |
275 } | 299 } |
276 | 300 |
277 @Override | 301 @Override |
302 public void flush() { | |
303 synchronized (mNativeStreamLock) { | |
304 if (!mDisableAutoFlush) { | |
305 throw new IllegalStateException("flush is called when auto flush is not disabled."); | |
306 } | |
307 | |
308 int size = mPendingData.size(); | |
309 ByteBuffer[] buffers = new ByteBuffer[size]; | |
310 int[] positions = new int[size]; | |
311 int[] limits = new int[size]; | |
312 for (int i = 0; i < size; i++) { | |
313 ByteBuffer buffer = mPendingData.poll(); | |
314 buffers[i] = buffer; | |
315 positions[i] = buffer.position(); | |
316 limits[i] = buffer.limit(); | |
317 } | |
318 assert mPendingData.isEmpty(); | |
319 if (!nativeWritevData(mNativeStream, buffers, positions, limits, mEn dOfStreamWritten)) { | |
320 throw new IllegalArgumentException("Unable to call native write" ); | |
321 } | |
322 } | |
323 } | |
324 | |
325 @Override | |
278 public void ping(PingCallback callback, Executor executor) { | 326 public void ping(PingCallback callback, Executor executor) { |
279 // TODO(mef): May be last thing to be implemented on Android. | 327 // TODO(mef): May be last thing to be implemented on Android. |
280 throw new UnsupportedOperationException("ping is not supported yet."); | 328 throw new UnsupportedOperationException("ping is not supported yet."); |
281 } | 329 } |
282 | 330 |
283 @Override | 331 @Override |
284 public void windowUpdate(int windowSizeIncrement) { | 332 public void windowUpdate(int windowSizeIncrement) { |
285 // TODO(mef): Understand the needs and semantics of this method. | 333 // TODO(mef): Understand the needs and semantics of this method. |
286 throw new UnsupportedOperationException("windowUpdate is not supported y et."); | 334 throw new UnsupportedOperationException("windowUpdate is not supported y et."); |
287 } | 335 } |
(...skipping 16 matching lines...) Expand all Loading... | |
304 } | 352 } |
305 } | 353 } |
306 | 354 |
307 @GuardedBy("mNativeStreamLock") | 355 @GuardedBy("mNativeStreamLock") |
308 private boolean isDoneLocked() { | 356 private boolean isDoneLocked() { |
309 return mReadState != State.NOT_STARTED && mNativeStream == 0; | 357 return mReadState != State.NOT_STARTED && mNativeStream == 0; |
310 } | 358 } |
311 | 359 |
312 @SuppressWarnings("unused") | 360 @SuppressWarnings("unused") |
313 @CalledByNative | 361 @CalledByNative |
314 private void onRequestHeadersSent() { | 362 private void onStreamReady() { |
315 postTaskToExecutor(new Runnable() { | 363 postTaskToExecutor(new Runnable() { |
316 public void run() { | 364 public void run() { |
317 synchronized (mNativeStreamLock) { | 365 synchronized (mNativeStreamLock) { |
318 if (isDoneLocked()) { | 366 if (isDoneLocked()) { |
319 return; | 367 return; |
320 } | 368 } |
321 if (doesMethodAllowWriteData(mInitialMethod)) { | 369 if (doesMethodAllowWriteData(mInitialMethod)) { |
322 mWriteState = State.WAITING_FOR_WRITE; | 370 mWriteState = State.WAITING_FOR_WRITE; |
323 } else { | 371 } else { |
324 mWriteState = State.WRITING_DONE; | 372 mWriteState = State.WRITING_DONE; |
325 } | 373 } |
326 } | 374 } |
327 | 375 |
328 try { | 376 try { |
329 mCallback.onRequestHeadersSent(CronetBidirectionalStream.thi s); | 377 mCallback.onStreamReady(CronetBidirectionalStream.this); |
330 } catch (Exception e) { | 378 } catch (Exception e) { |
331 onCallbackException(e); | 379 onCallbackException(e); |
332 } | 380 } |
333 } | 381 } |
334 }); | 382 }); |
335 } | 383 } |
336 | 384 |
337 /** | 385 /** |
338 * Called when the final set of headers, after all redirects, | 386 * Called when the final set of headers, after all redirects, |
339 * is received. Can only be called once for each stream. | 387 * is received. Can only be called once for each stream. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 } | 432 } |
385 byteBuffer.position(initialPosition + bytesRead); | 433 byteBuffer.position(initialPosition + bytesRead); |
386 assert mOnReadCompletedTask.mByteBuffer == null; | 434 assert mOnReadCompletedTask.mByteBuffer == null; |
387 mOnReadCompletedTask.mByteBuffer = byteBuffer; | 435 mOnReadCompletedTask.mByteBuffer = byteBuffer; |
388 mOnReadCompletedTask.mEndOfStream = (bytesRead == 0); | 436 mOnReadCompletedTask.mEndOfStream = (bytesRead == 0); |
389 postTaskToExecutor(mOnReadCompletedTask); | 437 postTaskToExecutor(mOnReadCompletedTask); |
390 } | 438 } |
391 | 439 |
392 @SuppressWarnings("unused") | 440 @SuppressWarnings("unused") |
393 @CalledByNative | 441 @CalledByNative |
394 private void onWriteCompleted( | 442 private void onWriteCompleted(final ByteBuffer byteBuffer, int initialPositi on, |
395 final ByteBuffer byteBuffer, int initialPosition, int initialLimit) { | 443 int initialLimit, boolean endOfStream) { |
396 if (byteBuffer.position() != initialPosition || byteBuffer.limit() != in itialLimit) { | 444 if (byteBuffer.position() != initialPosition || byteBuffer.limit() != in itialLimit) { |
397 failWithException( | 445 failWithException( |
398 new CronetException("ByteBuffer modified externally during w rite", null)); | 446 new CronetException("ByteBuffer modified externally during w rite", null)); |
399 return; | 447 return; |
400 } | 448 } |
401 // Current implementation always writes the complete buffer. | 449 // Current implementation always writes the complete buffer. |
402 byteBuffer.position(byteBuffer.limit()); | 450 byteBuffer.position(byteBuffer.limit()); |
403 assert mOnWriteCompletedTask.mByteBuffer == null; | 451 assert mOnWriteCompletedTask.mByteBuffer == null; |
404 mOnWriteCompletedTask.mByteBuffer = byteBuffer; | 452 mOnWriteCompletedTask.mByteBuffer = byteBuffer; |
453 mOnWriteCompletedTask.mEndOfStream = endOfStream; | |
405 postTaskToExecutor(mOnWriteCompletedTask); | 454 postTaskToExecutor(mOnWriteCompletedTask); |
406 } | 455 } |
407 | 456 |
408 @SuppressWarnings("unused") | 457 @SuppressWarnings("unused") |
409 @CalledByNative | 458 @CalledByNative |
459 private void onWritevCompleted(final ByteBuffer[] byteBuffers, int[] initial Positions, | |
kapishnikov
2016/04/11 23:09:42
It looks that onWritevCompleted() is called after
xunjieli
2016/04/12 18:01:48
As discussed, we don't have the signal about each
| |
460 int[] initialLimits, boolean endOfStream) { | |
461 assert byteBuffers.length == initialPositions.length; | |
462 assert byteBuffers.length == initialLimits.length; | |
463 assert initialPositions.length == initialLimits.length; | |
464 for (int i = 0; i < byteBuffers.length; i++) { | |
465 ByteBuffer buffer = byteBuffers[i]; | |
466 if (buffer.position() != initialPositions[i] || buffer.limit() != in itialLimits[i]) { | |
467 failWithException( | |
468 new CronetException("ByteBuffer modified externally duri ng write", null)); | |
469 return; | |
470 } | |
471 // Current implementation always writes the complete buffer. | |
472 buffer.position(buffer.limit()); | |
473 OnWriteCompletedRunnable runnable = new OnWriteCompletedRunnable(); | |
474 runnable.mByteBuffer = buffer; | |
475 runnable.mEndOfStream = endOfStream; | |
476 postTaskToExecutor(runnable); | |
477 } | |
478 } | |
479 | |
480 @SuppressWarnings("unused") | |
481 @CalledByNative | |
410 private void onResponseTrailersReceived(String[] trailers) { | 482 private void onResponseTrailersReceived(String[] trailers) { |
411 final UrlResponseInfo.HeaderBlock trailersBlock = | 483 final UrlResponseInfo.HeaderBlock trailersBlock = |
412 new UrlResponseInfo.HeaderBlock(headersListFromStrings(trailers) ); | 484 new UrlResponseInfo.HeaderBlock(headersListFromStrings(trailers) ); |
413 postTaskToExecutor(new Runnable() { | 485 postTaskToExecutor(new Runnable() { |
414 public void run() { | 486 public void run() { |
415 synchronized (mNativeStreamLock) { | 487 synchronized (mNativeStreamLock) { |
416 if (isDoneLocked()) { | 488 if (isDoneLocked()) { |
417 return; | 489 return; |
418 } | 490 } |
419 } | 491 } |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
611 */ | 683 */ |
612 private void failWithException(final CronetException exception) { | 684 private void failWithException(final CronetException exception) { |
613 postTaskToExecutor(new Runnable() { | 685 postTaskToExecutor(new Runnable() { |
614 public void run() { | 686 public void run() { |
615 failWithExceptionOnExecutor(exception); | 687 failWithExceptionOnExecutor(exception); |
616 } | 688 } |
617 }); | 689 }); |
618 } | 690 } |
619 | 691 |
620 // Native methods are implemented in cronet_bidirectional_stream_adapter.cc. | 692 // Native methods are implemented in cronet_bidirectional_stream_adapter.cc. |
621 private native long nativeCreateBidirectionalStream(long urlRequestContextAd apter); | 693 private native long nativeCreateBidirectionalStream( |
694 long urlRequestContextAdapter, boolean disableAutoFlush); | |
622 | 695 |
623 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 696 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
624 private native int nativeStart(long nativePtr, String url, int priority, Str ing method, | 697 private native int nativeStart(long nativePtr, String url, int priority, Str ing method, |
625 String[] headers, boolean endOfStream); | 698 String[] headers, boolean endOfStream); |
626 | 699 |
627 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 700 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
628 private native boolean nativeReadData( | 701 private native boolean nativeReadData( |
629 long nativePtr, ByteBuffer byteBuffer, int position, int limit); | 702 long nativePtr, ByteBuffer byteBuffer, int position, int limit); |
630 | 703 |
631 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 704 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
632 private native boolean nativeWriteData( | 705 private native boolean nativeWriteData( |
633 long nativePtr, ByteBuffer byteBuffer, int position, int limit, bool ean endOfStream); | 706 long nativePtr, ByteBuffer byteBuffer, int position, int limit, bool ean endOfStream); |
634 | 707 |
635 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 708 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
709 private native boolean nativeWritevData(long nativePtr, ByteBuffer[] buffers , int[] positions, | |
710 int[] limits, boolean endOfStream); | |
711 | |
712 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | |
636 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); | 713 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); |
637 } | 714 } |
OLD | NEW |