Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.net; | |
| 6 | |
| 7 import org.chromium.base.CalledByNative; | |
| 8 import org.chromium.base.JNINamespace; | |
| 9 | |
| 10 import java.nio.ByteBuffer; | |
| 11 import java.util.concurrent.Executor; | |
| 12 | |
| 13 /** | |
| 14 * Pass an upload body to a UrlRequest using an UploadDataProvider. | |
|
mef
2015/02/09 23:06:41
nit: an UrlRequest.
xunjieli
2015/02/10 14:37:16
I believe Matt's right, it is "a UrlRequest", sinc
mef
2015/02/10 14:47:38
Cool, I stand corrected.
| |
| 15 */ | |
| 16 @JNINamespace("cronet") | |
| 17 public class CronetUploadDataStream implements UploadDataSink { | |
| 18 // These are never changed, once a request starts. | |
| 19 private final Executor mExecutor; | |
| 20 private final UploadDataProvider mDataProvider; | |
| 21 private final long mLength; | |
| 22 private CronetUrlRequest mRequest; | |
| 23 | |
| 24 // Reusable read task, to reduce redundant memory allocation. | |
| 25 private final Runnable mReadTask = new Runnable() { | |
| 26 @Override | |
| 27 public void run() { | |
| 28 synchronized (mLock) { | |
| 29 if (mReading || mRewinding || mByteBuffer == null | |
| 30 || mUploadDataStreamDelegate == 0) { | |
| 31 throw new IllegalStateException( | |
| 32 "Unexpected readData call."); | |
| 33 } | |
| 34 mReading = true; | |
| 35 } | |
| 36 | |
| 37 try { | |
| 38 mDataProvider.read(CronetUploadDataStream.this, mByteBuffer); | |
| 39 } catch (Exception exception) { | |
| 40 onError(exception); | |
| 41 } | |
| 42 } | |
| 43 }; | |
| 44 | |
| 45 // ByteBuffer created in the native code and is passed to | |
| 46 // UploadDataProvider for reading. | |
|
mef
2015/02/09 23:06:41
I would add that it is valid from call to mDataPro
xunjieli
2015/02/10 14:37:17
Done.
| |
| 47 private ByteBuffer mByteBuffer = null; | |
| 48 | |
| 49 // Lock that protects all subsequent variables. The delegate has to be | |
| 50 // protected to ensure safe shutdown, mReading and mRewinding are protected | |
| 51 // to robustly detect getting read/rewind results more often than expected. | |
| 52 private final Object mLock = new Object(); | |
| 53 | |
| 54 // Native adapter delegate object, owned by the CronetUploadDataStream. | |
| 55 // It's only deleted after the native UploadDataStream object is destroyed. | |
| 56 // All access to the delegate is synchronized, for safe usage and cleanup. | |
| 57 private long mUploadDataStreamDelegate; | |
| 58 | |
| 59 private boolean mReading = false; | |
| 60 private boolean mRewinding = false; | |
| 61 private boolean mDestroyDelegatePostponed = false; | |
| 62 | |
| 63 public CronetUploadDataStream(UploadDataProvider dataProvider, | |
| 64 Executor executor) { | |
| 65 mExecutor = executor; | |
| 66 mDataProvider = dataProvider; | |
| 67 mLength = mDataProvider.getLength(); | |
| 68 } | |
| 69 | |
| 70 @SuppressWarnings("unused") | |
| 71 @CalledByNative | |
| 72 void readData(ByteBuffer byteBuffer) { | |
| 73 mByteBuffer = byteBuffer; | |
| 74 try { | |
| 75 mExecutor.execute(mReadTask); | |
| 76 } catch (Exception exception) { | |
| 77 onError(exception); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 // TODO(mmenke): Consider implementing a cancel method. | |
| 82 // currently wait for any pending read to complete. | |
| 83 | |
| 84 @SuppressWarnings("unused") | |
| 85 @CalledByNative | |
| 86 void rewind() { | |
| 87 Runnable task = new Runnable() { | |
| 88 @Override | |
| 89 public void run() { | |
| 90 synchronized (mLock) { | |
| 91 if (mReading || mRewinding | |
| 92 || mUploadDataStreamDelegate == 0) { | |
| 93 throw new IllegalStateException( | |
| 94 "Unexpected rewind call."); | |
| 95 } | |
| 96 mRewinding = true; | |
| 97 try { | |
|
mef
2015/02/09 23:06:41
Is there a particular reason why mDataProvider.rea
xunjieli
2015/02/10 14:37:16
I think both should be outside of the lock. Let me
| |
| 98 mDataProvider.rewind(CronetUploadDataStream.this); | |
| 99 } catch (Exception exception) { | |
| 100 onError(exception); | |
| 101 } | |
| 102 } | |
| 103 } | |
| 104 }; | |
| 105 | |
| 106 mExecutor.execute(task); | |
| 107 } | |
| 108 | |
| 109 @SuppressWarnings("unused") | |
| 110 @CalledByNative | |
| 111 void onAdapterDestroyed() { | |
| 112 Runnable task = new Runnable() { | |
| 113 @Override | |
| 114 public void run() { | |
| 115 destroyDelegate(); | |
| 116 } | |
| 117 }; | |
| 118 | |
| 119 mExecutor.execute(task); | |
| 120 } | |
| 121 | |
| 122 private void onError(Exception exception) { | |
| 123 synchronized (mLock) { | |
| 124 if (!mReading && !mRewinding) { | |
| 125 throw new IllegalStateException( | |
| 126 "There is no read or rewind in progress."); | |
| 127 } | |
| 128 mReading = false; | |
| 129 mRewinding = false; | |
| 130 mByteBuffer = null; | |
| 131 destroyDelegateIfPostponed(); | |
| 132 } | |
| 133 | |
| 134 // Just fail the request - simpler to fail directly, and | |
| 135 // UploadDataStream only supports failing during initialization, not | |
| 136 // while reading. This should be safe, even if we deleted the adapter, | |
| 137 // because in that case, the request has already been cancelled. | |
| 138 mRequest.onUploadException(exception); | |
| 139 } | |
| 140 | |
| 141 @Override | |
| 142 public void onReadSucceeded(boolean lastChunk) { | |
| 143 synchronized (mLock) { | |
| 144 if (!mReading) { | |
| 145 throw new IllegalStateException("Non-existent read succeeded."); | |
| 146 } | |
| 147 if (lastChunk && mLength >= 0) { | |
| 148 throw new IllegalArgumentException( | |
| 149 "Non-chunked upload can't have last chunk"); | |
| 150 } | |
| 151 int bytesRead = mByteBuffer.position(); | |
| 152 | |
| 153 mByteBuffer = null; | |
| 154 mReading = false; | |
| 155 | |
| 156 destroyDelegateIfPostponed(); | |
| 157 // Request may been canceled already. | |
| 158 if (mUploadDataStreamDelegate == 0) { | |
| 159 return; | |
| 160 } | |
| 161 nativeOnReadSucceeded(mUploadDataStreamDelegate, bytesRead, | |
| 162 lastChunk); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 public void onReadError(Exception exception) { | |
| 167 synchronized (mLock) { | |
| 168 if (!mReading) { | |
| 169 throw new IllegalStateException("Non-existent read failed."); | |
| 170 } | |
| 171 // Request may been canceled already. | |
| 172 if (mUploadDataStreamDelegate == 0) { | |
| 173 return; | |
| 174 } | |
| 175 onError(exception); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 public void onRewindSucceeded() { | |
| 180 synchronized (mLock) { | |
| 181 if (!mRewinding) { | |
| 182 throw new IllegalStateException( | |
| 183 "Non-existent rewind succeeded."); | |
| 184 } | |
| 185 mRewinding = false; | |
| 186 // Request may been canceled already. | |
| 187 if (mUploadDataStreamDelegate == 0) { | |
| 188 return; | |
| 189 } | |
| 190 nativeOnRewindSucceeded(mUploadDataStreamDelegate); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 public void onRewindError(Exception exception) { | |
| 195 synchronized (mLock) { | |
| 196 if (!mRewinding) { | |
| 197 throw new IllegalStateException("Non-existent rewind failed."); | |
| 198 } | |
| 199 // Request may been canceled already. | |
| 200 if (mUploadDataStreamDelegate == 0) { | |
| 201 return; | |
| 202 } | |
| 203 onError(exception); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 /** | |
| 208 * The delegate is owned by the CronetUploadDataStream, so it can be | |
| 209 * destroyed safely when there is no pending read; however, destruction is | |
| 210 * initiated by the destruction of the native UploadDataStream. | |
| 211 */ | |
| 212 private void destroyDelegate() { | |
| 213 synchronized (mLock) { | |
| 214 if (mReading) { | |
| 215 // Wait for the read to complete before destroy the delegate. | |
| 216 mDestroyDelegatePostponed = true; | |
| 217 return; | |
| 218 } | |
| 219 if (mUploadDataStreamDelegate == 0) { | |
| 220 return; | |
| 221 } | |
| 222 nativeDestroyDelegate(mUploadDataStreamDelegate); | |
| 223 mUploadDataStreamDelegate = 0; | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 /** | |
| 228 * Destroy the native delegate if the destruction is postponed due to a | |
| 229 * pending read, which has since completed. Caller needs to be on executor | |
| 230 * thread and acquire mLock. | |
| 231 */ | |
| 232 private void destroyDelegateIfPostponed() { | |
| 233 if (mReading) { | |
| 234 throw new IllegalStateException( | |
| 235 "Method should not be called when read has not completed."); | |
| 236 } | |
| 237 if (mDestroyDelegatePostponed) { | |
| 238 destroyDelegate(); | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 /** | |
| 243 * Creates native objects and attaches them to the underlying request | |
| 244 * adapter object. | |
| 245 * TODO(mmenke): If more types of native upload streams are needed, create | |
| 246 * an interface with just this method, to minimize CronetURLRequest's | |
| 247 * dependencies on each upload stream type. | |
| 248 */ | |
| 249 void attachToRequest(CronetUrlRequest request, long requestAdapter) { | |
| 250 if (mLength < 0) { | |
| 251 // TODO(mmenke): Add tests and remove this line. | |
| 252 throw new IllegalArgumentException( | |
| 253 "Chunked uploads not supported."); | |
| 254 } | |
| 255 mRequest = request; | |
| 256 mUploadDataStreamDelegate = | |
| 257 nativeAttachUploadDataToRequest(requestAdapter, mLength); | |
| 258 } | |
| 259 | |
| 260 /** | |
| 261 * Creates a native UploadDataStreamDelegate and UploadDataStreamAdapter | |
| 262 * for testing. | |
| 263 * @return the address of the native CronetUploadDataStreamAdapter object | |
| 264 */ | |
| 265 public long createAdapterForTesting() { | |
| 266 mUploadDataStreamDelegate = nativeCreateDelegateForTesting(); | |
| 267 return nativeCreateAdapterForTesting(mLength, mUploadDataStreamDelegate) ; | |
| 268 } | |
| 269 | |
| 270 // Native methods are implemented in upload_data_stream_adapter.cc. | |
| 271 | |
| 272 private native long nativeAttachUploadDataToRequest(long urlRequestAdapter, | |
| 273 long length); | |
| 274 | |
| 275 private native long nativeCreateDelegateForTesting(); | |
| 276 | |
| 277 private native long nativeCreateAdapterForTesting(long length, | |
| 278 long delegate); | |
| 279 | |
| 280 private native void nativeOnReadSucceeded(long uploadDataStreamDelegate, | |
| 281 int bytesRead, boolean finalChunk); | |
| 282 | |
| 283 private native void nativeOnRewindSucceeded(long uploadDataStreamDelegate); | |
| 284 | |
| 285 private static native void nativeDestroyDelegate( | |
| 286 long uploadDataStreamDelegate); | |
| 287 } | |
| OLD | NEW |