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. | |
| 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 private ByteBuffer mByteBuffer = null; | |
| 46 | |
| 47 // Lock that protects all subsequent variables. The delegate has to be | |
| 48 // protected to ensure safe shutdown, mReading and mRewinding are protected | |
| 49 // to robustly detect getting read/rewind results more often than expected. | |
| 50 private final Object mLock = new Object(); | |
| 51 | |
| 52 // Native adapter object, owned by the CronetUploadDataStream. It's only | |
| 
 
xunjieli
2015/02/04 18:50:30
The name is confusing. Can I refer to mUploadDataS
 
 | |
| 53 // deleted after the native UploadDataStream object is destroyed. All | |
| 54 // access to the adapter is synchronized, for safe usage and cleanup. | |
| 55 private long mUploadDataStreamDelegate; | |
| 56 | |
| 57 private boolean mReading = false; | |
| 58 private boolean mRewinding = false; | |
| 59 private boolean mDestroyAdapterPostponed = false; | |
| 60 | |
| 61 public CronetUploadDataStream(UploadDataProvider dataProvider, | |
| 62 Executor executor) { | |
| 63 mExecutor = executor; | |
| 64 mDataProvider = dataProvider; | |
| 65 mLength = mDataProvider.getLength(); | |
| 66 } | |
| 67 | |
| 68 @SuppressWarnings("unused") | |
| 69 @CalledByNative | |
| 70 void readData(ByteBuffer byteBuffer) { | |
| 71 mByteBuffer = byteBuffer; | |
| 72 try { | |
| 73 mExecutor.execute(mReadTask); | |
| 74 } catch (Exception exception) { | |
| 75 onError(exception); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 // TODO(mmenke): Consider implementing a cancel method. | |
| 80 // currently wait for any pending read to complete. | |
| 81 | |
| 82 @SuppressWarnings("unused") | |
| 83 @CalledByNative | |
| 84 void rewind() { | |
| 85 Runnable task = new Runnable() { | |
| 86 @Override | |
| 87 public void run() { | |
| 88 synchronized (mLock) { | |
| 89 if (mReading || mRewinding | |
| 90 || mUploadDataStreamDelegate == 0) { | |
| 91 throw new IllegalStateException( | |
| 92 "Unexpected rewind call."); | |
| 93 } | |
| 94 mRewinding = true; | |
| 95 try { | |
| 96 mDataProvider.rewind(CronetUploadDataStream.this); | |
| 97 } catch (Exception exception) { | |
| 98 onError(exception); | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 }; | |
| 103 | |
| 104 mExecutor.execute(task); | |
| 105 } | |
| 106 | |
| 107 @SuppressWarnings("unused") | |
| 108 @CalledByNative | |
| 109 void onAdapterDestroyed() { | |
| 110 Runnable task = new Runnable() { | |
| 111 @Override | |
| 112 public void run() { | |
| 113 destroyAdapter(); | |
| 114 } | |
| 115 }; | |
| 116 | |
| 117 mExecutor.execute(task); | |
| 118 } | |
| 119 | |
| 120 private void onError(Exception exception) { | |
| 121 synchronized (mLock) { | |
| 
 
mmenke
2015/02/04 16:46:00
Maybe throw an exception if !mReading and !mRewind
 
xunjieli
2015/02/04 18:50:30
Done.
 
 | |
| 122 mReading = false; | |
| 123 mRewinding = false; | |
| 124 mByteBuffer = null; | |
| 125 destroyAdapterIfPostponed(); | |
| 126 } | |
| 127 | |
| 128 // Just fail the request - simpler to fail directly, and | |
| 129 // UploadDataStream only supports failing during initialization, not | |
| 130 // while reading. | |
| 131 mRequest.onUploadException(exception); | |
| 
 
mmenke
2015/02/04 16:46:00
This should be safe, even if we just deleted the a
 
xunjieli
2015/02/04 18:50:29
Done.
 
 | |
| 132 } | |
| 133 | |
| 134 @Override | |
| 135 public void onReadSucceeded(boolean lastChunk) { | |
| 136 synchronized (mLock) { | |
| 137 if (!mReading) { | |
| 138 throw new IllegalStateException("Non-existent read succeeded."); | |
| 139 } | |
| 140 if (lastChunk && mLength >= 0) { | |
| 141 throw new IllegalArgumentException( | |
| 142 "Non-chunked upload can't have last chunk"); | |
| 143 } | |
| 144 int position = mByteBuffer.position(); | |
| 145 int limit = mByteBuffer.limit(); | |
| 146 | |
| 147 mByteBuffer = null; | |
| 148 mReading = false; | |
| 149 | |
| 150 destroyAdapterIfPostponed(); | |
| 151 // Request may been canceled already. | |
| 152 if (mUploadDataStreamDelegate == 0) { | |
| 153 return; | |
| 154 } | |
| 155 nativeOnReadSucceeded(mUploadDataStreamDelegate, position, limit, | |
| 156 lastChunk); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 public void onReadError(Exception exception) { | |
| 161 synchronized (mLock) { | |
| 162 if (!mReading) { | |
| 163 throw new IllegalStateException("Non-existent read failed."); | |
| 164 } | |
| 165 // Request may been canceled already. | |
| 166 if (mUploadDataStreamDelegate == 0) { | |
| 167 return; | |
| 168 } | |
| 169 onError(exception); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 public void onRewindSucceeded() { | |
| 174 synchronized (mLock) { | |
| 175 if (!mRewinding) { | |
| 176 throw new IllegalStateException( | |
| 177 "Non-existent rewind succeeded."); | |
| 178 } | |
| 179 mRewinding = false; | |
| 180 // Request may been canceled already. | |
| 181 if (mUploadDataStreamDelegate == 0) { | |
| 182 return; | |
| 183 } | |
| 184 nativeOnRewindSucceeded(mUploadDataStreamDelegate); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 public void onRewindError(Exception exception) { | |
| 189 synchronized (mLock) { | |
| 190 if (!mRewinding) { | |
| 191 throw new IllegalStateException("Non-existent rewind failed."); | |
| 192 } | |
| 193 // Request may been canceled already. | |
| 194 if (mUploadDataStreamDelegate == 0) { | |
| 195 return; | |
| 196 } | |
| 197 onError(exception); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 /** | |
| 202 * The adapter is owned by the CronetUploadDataStream, so it can be | |
| 203 * destroyed safely when there is no pending read; however, destruction is | |
| 204 * initiated by the destruction of the native UploadDataStream. | |
| 205 */ | |
| 206 private void destroyAdapter() { | |
| 207 synchronized (mLock) { | |
| 208 if (mReading) { | |
| 209 // Wait for the read to complete before destroy the adapter. | |
| 210 mDestroyAdapterPostponed = true; | |
| 
 
mmenke
2015/02/04 16:46:00
Should probably have a test for this - using your
 
 | |
| 211 return; | |
| 212 } | |
| 213 if (mUploadDataStreamDelegate == 0) { | |
| 214 return; | |
| 215 } | |
| 216 nativeDestroyDelegate(mUploadDataStreamDelegate); | |
| 217 mUploadDataStreamDelegate = 0; | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 /** | |
| 222 * Destroy the native adapter if the destruction is postponed due to a | |
| 223 * pending read, which has since completed. Caller needs to be on executor | |
| 224 * thread and acquire mLock. | |
| 225 */ | |
| 226 private void destroyAdapterIfPostponed() { | |
| 227 if (mReading) { | |
| 228 throw new IllegalStateException( | |
| 229 "Method should not be called when read has not completed."); | |
| 230 } | |
| 231 if (mDestroyAdapterPostponed) { | |
| 232 destroyAdapter(); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 /** | |
| 237 * Creates native objects and attaches them to the underlying request | |
| 238 * adapter object. | |
| 239 * TODO(mmenke): If more types of native upload streams are needed, create | |
| 240 * an interface with just this method, to minimize CronetURLRequest's | |
| 241 * dependencies on each upload stream type. | |
| 242 */ | |
| 243 void attachToRequest(CronetUrlRequest request, long requestAdapter) { | |
| 244 if (mLength < 0) { | |
| 245 // TODO(mmenke): Add tests and remove this line. | |
| 246 throw new IllegalArgumentException( | |
| 247 "Chunked uploads not supported."); | |
| 248 } | |
| 249 mRequest = request; | |
| 250 mUploadDataStreamDelegate = | |
| 251 nativeAttachUploadDataToRequest(requestAdapter, mLength); | |
| 252 } | |
| 253 | |
| 254 /** | |
| 255 * Creates a native UploadDataStreamDelegate and UploadDataStreamAdapter | |
| 256 * for testing. | |
| 257 */ | |
| 258 public long createAdapter() { | |
| 259 mUploadDataStreamDelegate = nativeCreateDelegateForTesting(); | |
| 260 return nativeCreateAdapterForTesting(mLength, mUploadDataStreamDelegate) ; | |
| 261 } | |
| 262 | |
| 263 // Native methods are implemented in upload_data_stream_adapter.cc. | |
| 264 | |
| 265 private native long nativeAttachUploadDataToRequest(long urlRequestAdapter, | |
| 266 long length); | |
| 267 | |
| 268 private native long nativeCreateDelegateForTesting(); | |
| 269 | |
| 270 private native long nativeCreateAdapterForTesting(long length, | |
| 271 long delegate); | |
| 272 | |
| 273 private native void nativeOnReadSucceeded(long uploadDataStreamDelegate, | |
| 274 int position, int limit, boolean finalChunk); | |
| 275 | |
| 276 private native void nativeOnRewindSucceeded(long uploadDataStreamDelegate); | |
| 277 | |
| 278 private static native void nativeDestroyDelegate( | |
| 279 long uploadDataStreamDelegate); | |
| 280 } | |
| OLD | NEW |