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

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

Issue 1777333002: [Cronet] Fix race in UploadDataStream.attachToRequest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
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
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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 } 292 }
293 } 293 }
294 294
295 /** 295 /**
296 * Creates native objects and attaches them to the underlying request 296 * Creates native objects and attaches them to the underlying request
297 * adapter object. 297 * adapter object.
298 * TODO(mmenke): If more types of native upload streams are needed, create 298 * TODO(mmenke): If more types of native upload streams are needed, create
299 * an interface with just this method, to minimize CronetURLRequest's 299 * an interface with just this method, to minimize CronetURLRequest's
300 * dependencies on each upload stream type. 300 * dependencies on each upload stream type.
301 */ 301 */
302 void attachToRequest(final CronetUrlRequest request, final long requestAdapt er, 302 void attachToRequest(final CronetUrlRequest request, final long requestAdapt er) {
xunjieli 2016/03/10 17:29:53 Could you make a comment here to say that this met
303 final Runnable afterAttachCallback) {
304 mRequest = request; 303 mRequest = request;
305 postTaskToExecutor(new Runnable() { 304 synchronized (mLock) {
306 @Override 305 mInWhichUserCallback = UserCallback.GET_LENGTH;
307 public void run() { 306 }
308 synchronized (mLock) { 307 try {
309 mInWhichUserCallback = UserCallback.GET_LENGTH; 308 mLength = mDataProvider.getLength();
310 } 309 } catch (Throwable t) {
311 try { 310 onError(t);
312 mLength = mDataProvider.getLength(); 311 }
313 } catch (Throwable t) { 312 synchronized (mLock) {
314 onError(t); 313 mInWhichUserCallback = UserCallback.NOT_IN_CALLBACK;
315 } 314 mUploadDataStreamAdapter = nativeAttachUploadDataToRequest(requestAd apter, mLength);
316 synchronized (mLock) { 315 }
317 mInWhichUserCallback = UserCallback.NOT_IN_CALLBACK;
318 mUploadDataStreamAdapter =
319 nativeAttachUploadDataToRequest(requestAdapter, mLen gth);
320 }
321 afterAttachCallback.run();
322 }
323 });
324 } 316 }
325 317
326 /** 318 /**
327 * Creates a native CronetUploadDataStreamAdapter and 319 * Creates a native CronetUploadDataStreamAdapter and
328 * CronetUploadDataStream for testing. 320 * CronetUploadDataStream for testing.
329 * @return the address of the native CronetUploadDataStream object. 321 * @return the address of the native CronetUploadDataStream object.
330 */ 322 */
331 @VisibleForTesting 323 @VisibleForTesting
332 long createUploadDataStreamForTesting() throws IOException { 324 long createUploadDataStreamForTesting() throws IOException {
333 mUploadDataStreamAdapter = nativeCreateAdapterForTesting(); 325 mUploadDataStreamAdapter = nativeCreateAdapterForTesting();
(...skipping 20 matching lines...) Expand all
354 @NativeClassQualifiedName("CronetUploadDataStreamAdapter") 346 @NativeClassQualifiedName("CronetUploadDataStreamAdapter")
355 private native void nativeOnReadSucceeded(long nativePtr, 347 private native void nativeOnReadSucceeded(long nativePtr,
356 int bytesRead, boolean finalChunk); 348 int bytesRead, boolean finalChunk);
357 349
358 @NativeClassQualifiedName("CronetUploadDataStreamAdapter") 350 @NativeClassQualifiedName("CronetUploadDataStreamAdapter")
359 private native void nativeOnRewindSucceeded(long nativePtr); 351 private native void nativeOnRewindSucceeded(long nativePtr);
360 352
361 @NativeClassQualifiedName("CronetUploadDataStreamAdapter") 353 @NativeClassQualifiedName("CronetUploadDataStreamAdapter")
362 private static native void nativeDestroy(long nativePtr); 354 private static native void nativeDestroy(long nativePtr);
363 } 355 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698