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 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 22 matching lines...) Expand all Loading... | |
| 49 /* There is no more data to read and stream is half-closed by the remote side. */ | 50 /* There is no more data to read and stream is half-closed by the remote side. */ |
| 50 READING_DONE, | 51 READING_DONE, |
| 51 /* Stream is canceled. */ | 52 /* Stream is canceled. */ |
| 52 CANCELED, | 53 CANCELED, |
| 53 /* Error has occured, stream is closed. */ | 54 /* Error has occured, stream is closed. */ |
| 54 ERROR, | 55 ERROR, |
| 55 /* Reading and writing are done, and the stream is closed successfully. */ | 56 /* Reading and writing are done, and the stream is closed successfully. */ |
| 56 SUCCESS, | 57 SUCCESS, |
| 57 /* Waiting for {@code write()} to be called. */ | 58 /* Waiting for {@code write()} to be called. */ |
| 58 WAITING_FOR_WRITE, | 59 WAITING_FOR_WRITE, |
| 59 /* Writing to the remote, {@code onWriteCompleted()} callback will be ca lled when done. */ | 60 /* |
| 61 * Writing to the remote, {@code onWriteCompleted()} callback will be ca lled when done. | |
| 62 * This state is only applicable when {@code mDisableAutoFlush} is false . | |
| 63 */ | |
| 60 WRITING, | 64 WRITING, |
|
kapishnikov
2016/04/19 03:22:22
Do we need this state? If it is not really used, I
xunjieli
2016/04/19 19:30:03
Yes, we need this state to detect that there is on
| |
| 61 /* There is no more data to write and stream is half-closed by the local side. */ | 65 /* There is no more data to write and stream is half-closed by the local side. */ |
| 62 WRITING_DONE, | 66 WRITING_DONE, |
| 63 } | 67 } |
| 64 | 68 |
| 65 private final CronetUrlRequestContext mRequestContext; | 69 private final CronetUrlRequestContext mRequestContext; |
| 66 private final Executor mExecutor; | 70 private final Executor mExecutor; |
| 67 private final Callback mCallback; | 71 private final Callback mCallback; |
| 68 private final String mInitialUrl; | 72 private final String mInitialUrl; |
| 69 private final int mInitialPriority; | 73 private final int mInitialPriority; |
| 70 private final String mInitialMethod; | 74 private final String mInitialMethod; |
| 71 private final String mRequestHeaders[]; | 75 private final String mRequestHeaders[]; |
| 76 private final boolean mDisableAutoFlush; | |
| 72 | 77 |
| 73 /* | 78 /* |
| 74 * Synchronizes access to mNativeStream, mReadState and mWriteState. | 79 * Synchronizes access to mNativeStream, mReadState and mWriteState. |
| 75 */ | 80 */ |
| 76 private final Object mNativeStreamLock = new Object(); | 81 private final Object mNativeStreamLock = new Object(); |
| 77 | 82 |
| 83 @GuardedBy("mNativeStreamLock") | |
| 84 private final LinkedList<ByteBuffer> mPendingData; | |
| 85 | |
| 86 @GuardedBy("mNativeStreamLock") | |
| 87 // Whether an end-of-stream flag is passed in through write(). | |
| 88 private boolean mEndOfStreamWritten; | |
| 89 | |
| 78 /* Native BidirectionalStream object, owned by CronetBidirectionalStream. */ | 90 /* Native BidirectionalStream object, owned by CronetBidirectionalStream. */ |
| 79 @GuardedBy("mNativeStreamLock") | 91 @GuardedBy("mNativeStreamLock") |
| 80 private long mNativeStream; | 92 private long mNativeStream; |
| 81 | 93 |
| 82 /** | 94 /** |
| 83 * Read state is tracking reading flow. | 95 * Read state is tracking reading flow. |
| 84 * / <--- READING <--- \ | 96 * / <--- READING <--- \ |
| 85 * | | | 97 * | | |
| 86 * \ / | 98 * \ / |
| 87 * NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS | 99 * NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS |
| 88 */ | 100 */ |
| 89 @GuardedBy("mNativeStreamLock") | 101 @GuardedBy("mNativeStreamLock") |
| 90 private State mReadState = State.NOT_STARTED; | 102 private State mReadState = State.NOT_STARTED; |
| 91 | 103 |
| 92 /** | 104 /** |
| 93 * Write state is tracking writing flow. | 105 * Write state is tracking writing flow. |
| 94 * / <--- WRITING <--- \ | 106 * / <--- WRITING (if !mDisableAutoFlush)<--- \ |
| 95 * | | | 107 * | | |
| 96 * \ / | 108 * \ / |
| 97 * NOT_STARTED -> STARTED --> WAITING_FOR_WRITE -> WRITING_DONE -> SUCCESS | 109 * NOT_STARTED -> STARTED --> -------------WAITING_FOR_WRITE --------> WRITI NG_DONE -> SUCCESS |
| 98 */ | 110 */ |
| 99 @GuardedBy("mNativeStreamLock") | 111 @GuardedBy("mNativeStreamLock") |
| 100 private State mWriteState = State.NOT_STARTED; | 112 private State mWriteState = State.NOT_STARTED; |
| 101 | 113 |
| 102 private UrlResponseInfo mResponseInfo; | 114 private UrlResponseInfo mResponseInfo; |
| 103 | 115 |
| 104 /* | 116 /* |
| 105 * OnReadCompleted callback is repeatedly invoked when each read is complete d, so it | 117 * OnReadCompleted callback is repeatedly invoked when each read is complete d, so it |
| 106 * is cached as a member variable. | 118 * is cached as a member variable. |
| 107 */ | 119 */ |
| 108 private OnReadCompletedRunnable mOnReadCompletedTask; | 120 private OnReadCompletedRunnable mOnReadCompletedTask; |
| 109 | 121 |
| 110 /* | |
| 111 * OnWriteCompleted callback is repeatedly invoked when each write is comple ted, so it | |
| 112 * is cached as a member variable. | |
| 113 */ | |
| 114 private OnWriteCompletedRunnable mOnWriteCompletedTask; | |
| 115 | |
| 116 private Runnable mOnDestroyedCallbackForTesting; | 122 private Runnable mOnDestroyedCallbackForTesting; |
| 117 | 123 |
| 118 private final class OnReadCompletedRunnable implements Runnable { | 124 private final class OnReadCompletedRunnable implements Runnable { |
| 119 // Buffer passed back from current invocation of onReadCompleted. | 125 // Buffer passed back from current invocation of onReadCompleted. |
| 120 ByteBuffer mByteBuffer; | 126 ByteBuffer mByteBuffer; |
| 121 // End of stream flag from current invocation of onReadCompleted. | 127 // End of stream flag from current invocation of onReadCompleted. |
| 122 boolean mEndOfStream; | 128 boolean mEndOfStream; |
| 123 | 129 |
| 124 @Override | 130 @Override |
| 125 public void run() { | 131 public void run() { |
| 126 try { | 132 try { |
| 127 // Null out mByteBuffer, to pass buffer ownership to callback or release if done. | 133 // Null out mByteBuffer, to pass buffer ownership to callback or release if done. |
| 128 ByteBuffer buffer = mByteBuffer; | 134 ByteBuffer buffer = mByteBuffer; |
| 129 mByteBuffer = null; | 135 mByteBuffer = null; |
| 130 synchronized (mNativeStreamLock) { | 136 synchronized (mNativeStreamLock) { |
| 131 if (isDoneLocked()) { | 137 if (isDoneLocked()) { |
| 132 return; | 138 return; |
| 133 } | 139 } |
| 134 if (mEndOfStream) { | 140 if (mEndOfStream) { |
| 135 mReadState = State.READING_DONE; | 141 mReadState = State.READING_DONE; |
| 136 if (maybeSucceedLocked()) { | 142 if (maybeSucceedLocked()) { |
| 137 return; | 143 return; |
|
kapishnikov
2016/04/19 03:22:22
I think we should always call mCallback.onReadComp
xunjieli
2016/04/19 19:30:03
Done. I agree. Although this might break gRPC, dep
| |
| 138 } | 144 } |
| 139 } else { | 145 } else { |
| 140 mReadState = State.WAITING_FOR_READ; | 146 mReadState = State.WAITING_FOR_READ; |
| 141 } | 147 } |
| 142 } | 148 } |
| 143 mCallback.onReadCompleted(CronetBidirectionalStream.this, mRespo nseInfo, buffer); | 149 mCallback.onReadCompleted(CronetBidirectionalStream.this, mRespo nseInfo, buffer); |
| 144 } catch (Exception e) { | 150 } catch (Exception e) { |
| 145 onCallbackException(e); | 151 onCallbackException(e); |
| 146 } | 152 } |
| 147 } | 153 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 158 try { | 164 try { |
| 159 // Null out mByteBuffer, to pass buffer ownership to callback or release if done. | 165 // Null out mByteBuffer, to pass buffer ownership to callback or release if done. |
| 160 ByteBuffer buffer = mByteBuffer; | 166 ByteBuffer buffer = mByteBuffer; |
| 161 mByteBuffer = null; | 167 mByteBuffer = null; |
| 162 synchronized (mNativeStreamLock) { | 168 synchronized (mNativeStreamLock) { |
| 163 if (isDoneLocked()) { | 169 if (isDoneLocked()) { |
| 164 return; | 170 return; |
| 165 } | 171 } |
| 166 if (mEndOfStream) { | 172 if (mEndOfStream) { |
| 167 mWriteState = State.WRITING_DONE; | 173 mWriteState = State.WRITING_DONE; |
| 168 if (maybeSucceedLocked()) { | 174 // Maybe post an onSucceeded callback to be executed aft er run() completes. |
| 169 return; | 175 maybeSucceedLocked(); |
|
kapishnikov
2016/04/19 03:22:22
The sequence of calls should be mCallback.onWriteC
xunjieli
2016/04/19 19:30:03
No, it isn't possible. onSucceeded is posted async
kapishnikov
2016/04/19 19:56:39
Do we specify anywhere that the executor should be
| |
| 170 } | |
| 171 } else { | 176 } else { |
| 172 mWriteState = State.WAITING_FOR_WRITE; | 177 mWriteState = State.WAITING_FOR_WRITE; |
| 173 } | 178 } |
| 174 } | 179 } |
| 175 mCallback.onWriteCompleted(CronetBidirectionalStream.this, mResp onseInfo, buffer); | 180 mCallback.onWriteCompleted(CronetBidirectionalStream.this, mResp onseInfo, buffer); |
| 176 } catch (Exception e) { | 181 } catch (Exception e) { |
| 177 onCallbackException(e); | 182 onCallbackException(e); |
| 178 } | 183 } |
| 179 } | 184 } |
| 180 } | 185 } |
| 181 | 186 |
| 182 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url , | 187 CronetBidirectionalStream(CronetUrlRequestContext requestContext, String url , |
| 183 @BidirectionalStream.Builder.StreamPriority int priority, Callback c allback, | 188 @BidirectionalStream.Builder.StreamPriority int priority, Callback c allback, |
| 184 Executor executor, String httpMethod, List<Map.Entry<String, String> > requestHeaders) { | 189 Executor executor, String httpMethod, List<Map.Entry<String, String> > requestHeaders, |
| 190 boolean disableAutoFlush) { | |
| 185 mRequestContext = requestContext; | 191 mRequestContext = requestContext; |
| 186 mInitialUrl = url; | 192 mInitialUrl = url; |
| 187 mInitialPriority = convertStreamPriority(priority); | 193 mInitialPriority = convertStreamPriority(priority); |
| 188 mCallback = callback; | 194 mCallback = callback; |
| 189 mExecutor = executor; | 195 mExecutor = executor; |
| 190 mInitialMethod = httpMethod; | 196 mInitialMethod = httpMethod; |
| 191 mRequestHeaders = stringsFromHeaderList(requestHeaders); | 197 mRequestHeaders = stringsFromHeaderList(requestHeaders); |
| 198 mDisableAutoFlush = disableAutoFlush; | |
| 199 mPendingData = new LinkedList<ByteBuffer>(); | |
| 192 } | 200 } |
| 193 | 201 |
| 194 @Override | 202 @Override |
| 195 public void start() { | 203 public void start() { |
| 196 synchronized (mNativeStreamLock) { | 204 synchronized (mNativeStreamLock) { |
| 197 if (mReadState != State.NOT_STARTED) { | 205 if (mReadState != State.NOT_STARTED) { |
| 198 throw new IllegalStateException("Stream is already started."); | 206 throw new IllegalStateException("Stream is already started."); |
| 199 } | 207 } |
| 200 try { | 208 try { |
| 201 mNativeStream = nativeCreateBidirectionalStream( | 209 mNativeStream = nativeCreateBidirectionalStream( |
| 202 mRequestContext.getUrlRequestContextAdapter()); | 210 mRequestContext.getUrlRequestContextAdapter(), mDisableA utoFlush); |
| 203 mRequestContext.onRequestStarted(); | 211 mRequestContext.onRequestStarted(); |
| 204 // Non-zero startResult means an argument error. | 212 // Non-zero startResult means an argument error. |
| 205 int startResult = nativeStart(mNativeStream, mInitialUrl, mIniti alPriority, | 213 int startResult = nativeStart(mNativeStream, mInitialUrl, mIniti alPriority, |
| 206 mInitialMethod, mRequestHeaders, !doesMethodAllowWriteDa ta(mInitialMethod)); | 214 mInitialMethod, mRequestHeaders, !doesMethodAllowWriteDa ta(mInitialMethod)); |
| 207 if (startResult == -1) { | 215 if (startResult == -1) { |
| 208 throw new IllegalArgumentException("Invalid http method " + mInitialMethod); | 216 throw new IllegalArgumentException("Invalid http method " + mInitialMethod); |
| 209 } | 217 } |
| 210 if (startResult > 0) { | 218 if (startResult > 0) { |
| 211 int headerPos = startResult - 1; | 219 int headerPos = startResult - 1; |
| 212 throw new IllegalArgumentException("Invalid header " | 220 throw new IllegalArgumentException("Invalid header " |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 } | 254 } |
| 247 } | 255 } |
| 248 | 256 |
| 249 @Override | 257 @Override |
| 250 public void write(ByteBuffer buffer, boolean endOfStream) { | 258 public void write(ByteBuffer buffer, boolean endOfStream) { |
| 251 synchronized (mNativeStreamLock) { | 259 synchronized (mNativeStreamLock) { |
| 252 Preconditions.checkDirect(buffer); | 260 Preconditions.checkDirect(buffer); |
| 253 if (!buffer.hasRemaining() && !endOfStream) { | 261 if (!buffer.hasRemaining() && !endOfStream) { |
| 254 throw new IllegalArgumentException("Empty buffer before end of s tream."); | 262 throw new IllegalArgumentException("Empty buffer before end of s tream."); |
| 255 } | 263 } |
| 264 if (mEndOfStreamWritten) { | |
| 265 throw new IllegalArgumentException("Write after writing end of s tream."); | |
| 266 } | |
| 256 if (mWriteState != State.WAITING_FOR_WRITE) { | 267 if (mWriteState != State.WAITING_FOR_WRITE) { |
| 257 throw new IllegalStateException("Unexpected write attempt."); | 268 throw new IllegalStateException("Unexpected write attempt."); |
| 258 } | 269 } |
| 259 if (isDoneLocked()) { | 270 if (isDoneLocked()) { |
| 260 return; | 271 return; |
| 261 } | 272 } |
| 262 if (mOnWriteCompletedTask == null) { | 273 mPendingData.add(buffer); |
| 263 mOnWriteCompletedTask = new OnWriteCompletedRunnable(); | 274 if (endOfStream) { |
| 275 mEndOfStreamWritten = true; | |
| 264 } | 276 } |
| 265 mOnWriteCompletedTask.mEndOfStream = endOfStream; | 277 if (mDisableAutoFlush) { |
| 278 return; | |
| 279 } | |
| 266 mWriteState = State.WRITING; | 280 mWriteState = State.WRITING; |
|
kapishnikov
2016/04/19 03:22:22
If we remove WRITING state, we can write:
if (!mDi
xunjieli
2016/04/19 19:30:03
State.WRITING is needed to detect that there isn't
| |
| 267 if (!nativeWriteData( | 281 flushLocked(); |
| 268 mNativeStream, buffer, buffer.position(), buffer.limit() , endOfStream)) { | |
| 269 // Still waiting on write. This is just to have consistent | |
| 270 // behavior with the other error cases. | |
| 271 mWriteState = State.WAITING_FOR_WRITE; | |
| 272 throw new IllegalArgumentException("Unable to call native write" ); | |
| 273 } | |
| 274 } | 282 } |
| 275 } | 283 } |
| 276 | 284 |
| 285 @Override | |
| 286 public void flush() { | |
| 287 synchronized (mNativeStreamLock) { | |
| 288 flushLocked(); | |
| 289 } | |
| 290 } | |
| 291 | |
| 292 @SuppressWarnings("GuardedByChecker") | |
| 293 private void flushLocked() { | |
| 294 if (mPendingData.isEmpty()) { | |
| 295 // No-op if there is nothing to write. | |
| 296 return; | |
| 297 } | |
| 298 int size = mPendingData.size(); | |
| 299 ByteBuffer[] buffers = new ByteBuffer[size]; | |
| 300 int[] positions = new int[size]; | |
| 301 int[] limits = new int[size]; | |
| 302 for (int i = 0; i < size; i++) { | |
| 303 ByteBuffer buffer = mPendingData.poll(); | |
| 304 buffers[i] = buffer; | |
| 305 positions[i] = buffer.position(); | |
| 306 limits[i] = buffer.limit(); | |
| 307 } | |
| 308 assert mPendingData.isEmpty(); | |
| 309 if (!nativeWritevData(mNativeStream, buffers, positions, limits, mEndOfS treamWritten)) { | |
| 310 throw new IllegalArgumentException("Unable to call native write"); | |
|
kapishnikov
2016/04/19 03:22:22
Should we change the state to ERROR here?
xunjieli
2016/04/19 19:30:03
Done.
| |
| 311 } | |
| 312 } | |
| 313 | |
| 277 @Override | 314 @Override |
| 278 public void ping(PingCallback callback, Executor executor) { | 315 public void ping(PingCallback callback, Executor executor) { |
| 279 // TODO(mef): May be last thing to be implemented on Android. | 316 // TODO(mef): May be last thing to be implemented on Android. |
| 280 throw new UnsupportedOperationException("ping is not supported yet."); | 317 throw new UnsupportedOperationException("ping is not supported yet."); |
| 281 } | 318 } |
| 282 | 319 |
| 283 @Override | 320 @Override |
| 284 public void windowUpdate(int windowSizeIncrement) { | 321 public void windowUpdate(int windowSizeIncrement) { |
| 285 // TODO(mef): Understand the needs and semantics of this method. | 322 // TODO(mef): Understand the needs and semantics of this method. |
| 286 throw new UnsupportedOperationException("windowUpdate is not supported y et."); | 323 throw new UnsupportedOperationException("windowUpdate is not supported y et."); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 304 } | 341 } |
| 305 } | 342 } |
| 306 | 343 |
| 307 @GuardedBy("mNativeStreamLock") | 344 @GuardedBy("mNativeStreamLock") |
| 308 private boolean isDoneLocked() { | 345 private boolean isDoneLocked() { |
| 309 return mReadState != State.NOT_STARTED && mNativeStream == 0; | 346 return mReadState != State.NOT_STARTED && mNativeStream == 0; |
| 310 } | 347 } |
| 311 | 348 |
| 312 @SuppressWarnings("unused") | 349 @SuppressWarnings("unused") |
| 313 @CalledByNative | 350 @CalledByNative |
| 314 private void onRequestHeadersSent() { | 351 private void onStreamReady() { |
| 315 postTaskToExecutor(new Runnable() { | 352 postTaskToExecutor(new Runnable() { |
| 316 public void run() { | 353 public void run() { |
| 317 synchronized (mNativeStreamLock) { | 354 synchronized (mNativeStreamLock) { |
| 318 if (isDoneLocked()) { | 355 if (isDoneLocked()) { |
| 319 return; | 356 return; |
| 320 } | 357 } |
| 321 if (doesMethodAllowWriteData(mInitialMethod)) { | 358 if (doesMethodAllowWriteData(mInitialMethod)) { |
| 322 mWriteState = State.WAITING_FOR_WRITE; | 359 mWriteState = State.WAITING_FOR_WRITE; |
| 323 } else { | 360 } else { |
| 324 mWriteState = State.WRITING_DONE; | 361 mWriteState = State.WRITING_DONE; |
| 325 } | 362 } |
| 326 } | 363 } |
| 327 | 364 |
| 328 try { | 365 try { |
| 329 mCallback.onRequestHeadersSent(CronetBidirectionalStream.thi s); | 366 mCallback.onStreamReady(CronetBidirectionalStream.this); |
| 330 } catch (Exception e) { | 367 } catch (Exception e) { |
| 331 onCallbackException(e); | 368 onCallbackException(e); |
| 332 } | 369 } |
| 333 } | 370 } |
| 334 }); | 371 }); |
| 335 } | 372 } |
| 336 | 373 |
| 337 /** | 374 /** |
| 338 * Called when the final set of headers, after all redirects, | 375 * Called when the final set of headers, after all redirects, |
| 339 * is received. Can only be called once for each stream. | 376 * is received. Can only be called once for each stream. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 } | 421 } |
| 385 byteBuffer.position(initialPosition + bytesRead); | 422 byteBuffer.position(initialPosition + bytesRead); |
| 386 assert mOnReadCompletedTask.mByteBuffer == null; | 423 assert mOnReadCompletedTask.mByteBuffer == null; |
| 387 mOnReadCompletedTask.mByteBuffer = byteBuffer; | 424 mOnReadCompletedTask.mByteBuffer = byteBuffer; |
| 388 mOnReadCompletedTask.mEndOfStream = (bytesRead == 0); | 425 mOnReadCompletedTask.mEndOfStream = (bytesRead == 0); |
| 389 postTaskToExecutor(mOnReadCompletedTask); | 426 postTaskToExecutor(mOnReadCompletedTask); |
| 390 } | 427 } |
| 391 | 428 |
| 392 @SuppressWarnings("unused") | 429 @SuppressWarnings("unused") |
| 393 @CalledByNative | 430 @CalledByNative |
| 394 private void onWriteCompleted( | 431 private void onWritevCompleted(final ByteBuffer[] byteBuffers, int[] initial Positions, |
| 395 final ByteBuffer byteBuffer, int initialPosition, int initialLimit) { | 432 int[] initialLimits, boolean endOfStream) { |
| 396 if (byteBuffer.position() != initialPosition || byteBuffer.limit() != in itialLimit) { | 433 assert byteBuffers.length == initialPositions.length; |
| 397 failWithException( | 434 assert byteBuffers.length == initialLimits.length; |
| 398 new CronetException("ByteBuffer modified externally during w rite", null)); | 435 assert initialPositions.length == initialLimits.length; |
| 399 return; | 436 for (int i = 0; i < byteBuffers.length; i++) { |
| 437 ByteBuffer buffer = byteBuffers[i]; | |
| 438 if (buffer.position() != initialPositions[i] || buffer.limit() != in itialLimits[i]) { | |
| 439 failWithException( | |
| 440 new CronetException("ByteBuffer modified externally duri ng write", null)); | |
| 441 return; | |
| 442 } | |
| 443 // Current implementation always writes the complete buffer. | |
| 444 buffer.position(buffer.limit()); | |
| 445 OnWriteCompletedRunnable runnable = new OnWriteCompletedRunnable(); | |
| 446 runnable.mByteBuffer = buffer; | |
| 447 runnable.mEndOfStream = endOfStream; | |
| 448 postTaskToExecutor(runnable); | |
| 400 } | 449 } |
| 401 // Current implementation always writes the complete buffer. | |
| 402 byteBuffer.position(byteBuffer.limit()); | |
| 403 assert mOnWriteCompletedTask.mByteBuffer == null; | |
| 404 mOnWriteCompletedTask.mByteBuffer = byteBuffer; | |
| 405 postTaskToExecutor(mOnWriteCompletedTask); | |
| 406 } | 450 } |
| 407 | 451 |
| 408 @SuppressWarnings("unused") | 452 @SuppressWarnings("unused") |
| 409 @CalledByNative | 453 @CalledByNative |
| 410 private void onResponseTrailersReceived(String[] trailers) { | 454 private void onResponseTrailersReceived(String[] trailers) { |
| 411 final UrlResponseInfo.HeaderBlock trailersBlock = | 455 final UrlResponseInfo.HeaderBlock trailersBlock = |
| 412 new UrlResponseInfo.HeaderBlock(headersListFromStrings(trailers) ); | 456 new UrlResponseInfo.HeaderBlock(headersListFromStrings(trailers) ); |
| 413 postTaskToExecutor(new Runnable() { | 457 postTaskToExecutor(new Runnable() { |
| 414 public void run() { | 458 public void run() { |
| 415 synchronized (mNativeStreamLock) { | 459 synchronized (mNativeStreamLock) { |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 611 */ | 655 */ |
| 612 private void failWithException(final CronetException exception) { | 656 private void failWithException(final CronetException exception) { |
| 613 postTaskToExecutor(new Runnable() { | 657 postTaskToExecutor(new Runnable() { |
| 614 public void run() { | 658 public void run() { |
| 615 failWithExceptionOnExecutor(exception); | 659 failWithExceptionOnExecutor(exception); |
| 616 } | 660 } |
| 617 }); | 661 }); |
| 618 } | 662 } |
| 619 | 663 |
| 620 // Native methods are implemented in cronet_bidirectional_stream_adapter.cc. | 664 // Native methods are implemented in cronet_bidirectional_stream_adapter.cc. |
| 621 private native long nativeCreateBidirectionalStream(long urlRequestContextAd apter); | 665 private native long nativeCreateBidirectionalStream( |
| 666 long urlRequestContextAdapter, boolean disableAutoFlush); | |
| 622 | 667 |
| 623 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 668 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
| 624 private native int nativeStart(long nativePtr, String url, int priority, Str ing method, | 669 private native int nativeStart(long nativePtr, String url, int priority, Str ing method, |
| 625 String[] headers, boolean endOfStream); | 670 String[] headers, boolean endOfStream); |
| 626 | 671 |
| 627 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 672 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
| 628 private native boolean nativeReadData( | 673 private native boolean nativeReadData( |
| 629 long nativePtr, ByteBuffer byteBuffer, int position, int limit); | 674 long nativePtr, ByteBuffer byteBuffer, int position, int limit); |
| 630 | 675 |
| 631 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 676 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
| 632 private native boolean nativeWriteData( | 677 private native boolean nativeWritevData(long nativePtr, ByteBuffer[] buffers , int[] positions, |
| 633 long nativePtr, ByteBuffer byteBuffer, int position, int limit, bool ean endOfStream); | 678 int[] limits, boolean endOfStream); |
| 634 | 679 |
| 635 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") | 680 @NativeClassQualifiedName("CronetBidirectionalStreamAdapter") |
| 636 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); | 681 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); |
| 637 } | 682 } |
| OLD | NEW |