Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/CronetUploadDataStream.java

Issue 1801943002: [Cronet] Fix race in UploadDataStream.attachToRequest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2661
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.util.Log; 7 import android.util.Log;
8 8
9 import org.chromium.base.VisibleForTesting; 9 import org.chromium.base.VisibleForTesting;
10 import org.chromium.base.annotations.CalledByNative; 10 import org.chromium.base.annotations.CalledByNative;
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 public void onRewindError(Exception exception) { 225 public void onRewindError(Exception exception) {
226 synchronized (mLock) { 226 synchronized (mLock) {
227 checkState(UserCallback.REWIND); 227 checkState(UserCallback.REWIND);
228 onError(exception); 228 onError(exception);
229 } 229 }
230 } 230 }
231 231
232 /** 232 /**
233 * Posts task to application Executor. 233 * Posts task to application Executor.
234 */ 234 */
235 private void postTaskToExecutor(Runnable task) { 235 void postTaskToExecutor(Runnable task) {
236 try { 236 try {
237 mExecutor.execute(task); 237 mExecutor.execute(task);
238 } catch (Throwable e) { 238 } catch (Throwable e) {
239 // Just fail the request. The request is smart enough to handle the 239 // Just fail the request. The request is smart enough to handle the
240 // case where it was already canceled by the embedder. 240 // case where it was already canceled by the embedder.
241 mRequest.onUploadException(e); 241 mRequest.onUploadException(e);
242 } 242 }
243 } 243 }
244 244
245 /** 245 /**
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 throw new IllegalStateException( 286 throw new IllegalStateException(
287 "Method should not be called when read has not completed ."); 287 "Method should not be called when read has not completed .");
288 } 288 }
289 if (mDestroyAdapterPostponed) { 289 if (mDestroyAdapterPostponed) {
290 destroyAdapter(); 290 destroyAdapter();
291 } 291 }
292 } 292 }
293 } 293 }
294 294
295 /** 295 /**
296 * Initializes upload length by getting it from data provider. Always called
297 * on executor thread to allow getLength() to block and/or report errors.
298 * If data provider throws an exception, then it is reported to the request.
299 * No native calls to urlRequest are allowed as this is done before request
300 * start, so native object may not exist.
301 */
302 void initializeWithRequest(final CronetUrlRequest urlRequest) {
303 synchronized (mLock) {
304 mRequest = urlRequest;
305 mInWhichUserCallback = UserCallback.GET_LENGTH;
306 }
307 try {
308 mLength = mDataProvider.getLength();
309 } catch (Throwable t) {
310 onError(t);
311 }
312 synchronized (mLock) {
313 mInWhichUserCallback = UserCallback.NOT_IN_CALLBACK;
314 }
315 }
316
317 /**
296 * Creates native objects and attaches them to the underlying request 318 * Creates native objects and attaches them to the underlying request
297 * adapter object. 319 * adapter object. Always called on executor thread.
298 * TODO(mmenke): If more types of native upload streams are needed, create
299 * an interface with just this method, to minimize CronetURLRequest's
300 * dependencies on each upload stream type.
301 */ 320 */
302 void attachToRequest(final CronetUrlRequest request, final long requestAdapt er, 321 void attachNativeAdapterToRequest(final long requestAdapter) {
303 final Runnable afterAttachCallback) { 322 synchronized (mLock) {
304 mRequest = request; 323 mUploadDataStreamAdapter = nativeAttachUploadDataToRequest(requestAd apter, mLength);
305 postTaskToExecutor(new Runnable() { 324 }
306 @Override
307 public void run() {
308 synchronized (mLock) {
309 mInWhichUserCallback = UserCallback.GET_LENGTH;
310 }
311 try {
312 mLength = mDataProvider.getLength();
313 } catch (Throwable t) {
314 onError(t);
315 }
316 synchronized (mLock) {
317 mInWhichUserCallback = UserCallback.NOT_IN_CALLBACK;
318 mUploadDataStreamAdapter =
319 nativeAttachUploadDataToRequest(requestAdapter, mLen gth);
320 }
321 afterAttachCallback.run();
322 }
323 });
324 } 325 }
325 326
326 /** 327 /**
327 * Creates a native CronetUploadDataStreamAdapter and 328 * Creates a native CronetUploadDataStreamAdapter and
328 * CronetUploadDataStream for testing. 329 * CronetUploadDataStream for testing.
329 * @return the address of the native CronetUploadDataStream object. 330 * @return the address of the native CronetUploadDataStream object.
330 */ 331 */
331 @VisibleForTesting 332 @VisibleForTesting
332 long createUploadDataStreamForTesting() throws IOException { 333 long createUploadDataStreamForTesting() throws IOException {
333 mUploadDataStreamAdapter = nativeCreateAdapterForTesting(); 334 synchronized (mLock) {
334 mLength = mDataProvider.getLength(); 335 mUploadDataStreamAdapter = nativeCreateAdapterForTesting();
335 return nativeCreateUploadDataStreamForTesting(mLength, 336 mLength = mDataProvider.getLength();
336 mUploadDataStreamAdapter); 337 return nativeCreateUploadDataStreamForTesting(mLength, mUploadDataSt reamAdapter);
338 }
337 } 339 }
338 340
339 @VisibleForTesting 341 @VisibleForTesting
340 void setOnDestroyedCallbackForTesting(Runnable onDestroyedCallbackForTesting ) { 342 void setOnDestroyedCallbackForTesting(Runnable onDestroyedCallbackForTesting ) {
341 mOnDestroyedCallbackForTesting = onDestroyedCallbackForTesting; 343 mOnDestroyedCallbackForTesting = onDestroyedCallbackForTesting;
342 } 344 }
343 345
344 // Native methods are implemented in upload_data_stream_adapter.cc. 346 // Native methods are implemented in upload_data_stream_adapter.cc.
345 347
346 private native long nativeAttachUploadDataToRequest(long urlRequestAdapter, 348 private native long nativeAttachUploadDataToRequest(long urlRequestAdapter,
347 long length); 349 long length);
348 350
349 private native long nativeCreateAdapterForTesting(); 351 private native long nativeCreateAdapterForTesting();
350 352
351 private native long nativeCreateUploadDataStreamForTesting(long length, 353 private native long nativeCreateUploadDataStreamForTesting(long length,
352 long adapter); 354 long adapter);
353 355
354 @NativeClassQualifiedName("CronetUploadDataStreamAdapter") 356 @NativeClassQualifiedName("CronetUploadDataStreamAdapter")
355 private native void nativeOnReadSucceeded(long nativePtr, 357 private native void nativeOnReadSucceeded(long nativePtr,
356 int bytesRead, boolean finalChunk); 358 int bytesRead, boolean finalChunk);
357 359
358 @NativeClassQualifiedName("CronetUploadDataStreamAdapter") 360 @NativeClassQualifiedName("CronetUploadDataStreamAdapter")
359 private native void nativeOnRewindSucceeded(long nativePtr); 361 private native void nativeOnRewindSucceeded(long nativePtr);
360 362
361 @NativeClassQualifiedName("CronetUploadDataStreamAdapter") 363 @NativeClassQualifiedName("CronetUploadDataStreamAdapter")
362 private static native void nativeDestroy(long nativePtr); 364 private static native void nativeDestroy(long nativePtr);
363 } 365 }
OLDNEW
« no previous file with comments | « no previous file | components/cronet/android/java/src/org/chromium/net/CronetUrlRequest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698