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

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

Issue 945843003: [Cronet] Do not call into native adapter after it is destroyed in ChromiumUrlRequest.java (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Do null check on java side and cache status code and text Created 5 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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.apache.http.conn.ConnectTimeoutException; 9 import org.apache.http.conn.ConnectTimeoutException;
10 import org.chromium.base.CalledByNative; 10 import org.chromium.base.CalledByNative;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 private long mUploadContentLength; 54 private long mUploadContentLength;
55 private final HttpUrlRequestListener mListener; 55 private final HttpUrlRequestListener mListener;
56 private boolean mBufferFullResponse; 56 private boolean mBufferFullResponse;
57 private long mOffset; 57 private long mOffset;
58 private long mContentLength; 58 private long mContentLength;
59 private long mContentLengthLimit; 59 private long mContentLengthLimit;
60 private boolean mCancelIfContentLengthOverLimit; 60 private boolean mCancelIfContentLengthOverLimit;
61 private boolean mContentLengthOverLimit; 61 private boolean mContentLengthOverLimit;
62 private boolean mSkippingToOffset; 62 private boolean mSkippingToOffset;
63 private long mSize; 63 private long mSize;
64
64 // Indicates whether redirects have been disabled. 65 // Indicates whether redirects have been disabled.
65 private boolean mDisableRedirects; 66 private boolean mDisableRedirects;
67
68 // Caches the http status code. Default to 0.
69 private int mHttpStatusCode = 0;
70
71 // Caches the http status text. Default to null.
72 private String mHttpStatusText;
73
74 // Caches the native error code. Default to no error.
75 private int mErrorCode = ChromiumUrlRequestError.SUCCESS;
76
77 // Caches the native error string. Default to null.
78 private String mErrorString;
79
66 private final Object mLock = new Object(); 80 private final Object mLock = new Object();
67 81
68 public ChromiumUrlRequest(ChromiumUrlRequestContext requestContext, 82 public ChromiumUrlRequest(ChromiumUrlRequestContext requestContext,
69 String url, int priority, Map<String, String> headers, 83 String url, int priority, Map<String, String> headers,
70 HttpUrlRequestListener listener) { 84 HttpUrlRequestListener listener) {
71 this(requestContext, url, priority, headers, 85 this(requestContext, url, priority, headers,
72 new ChunkedWritableByteChannel(), listener); 86 new ChunkedWritableByteChannel(), listener);
73 mBufferFullResponse = true; 87 mBufferFullResponse = true;
74 } 88 }
75 89
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 } 139 }
126 140
127 @Override 141 @Override
128 public void setContentLengthLimit(long limit, boolean cancelEarly) { 142 public void setContentLengthLimit(long limit, boolean cancelEarly) {
129 mContentLengthLimit = limit; 143 mContentLengthLimit = limit;
130 mCancelIfContentLengthOverLimit = cancelEarly; 144 mCancelIfContentLengthOverLimit = cancelEarly;
131 } 145 }
132 146
133 @Override 147 @Override
134 public int getHttpStatusCode() { 148 public int getHttpStatusCode() {
135 int httpStatusCode = nativeGetHttpStatusCode(mUrlRequestAdapter); 149 if (mUrlRequestAdapter != 0) {
150 mHttpStatusCode = nativeGetHttpStatusCode(mUrlRequestAdapter);
mmenke 2015/02/25 23:43:57 Can we populate this in onResponseStarted uncondit
miloslav 2015/02/26 15:56:49 +1 for setting in onResponseStarted.
xunjieli 2015/02/26 17:54:00 Done. Good idea! thanks!
xunjieli 2015/02/26 17:54:00 Done.
151 }
136 152
137 // TODO(mef): Investigate the following: 153 // TODO(mef): Investigate the following:
138 // If we have been able to successfully resume a previously interrupted 154 // If we have been able to successfully resume a previously interrupted
139 // download, the status code will be 206, not 200. Since the rest of the 155 // download, the status code will be 206, not 200. Since the rest of the
140 // application is expecting 200 to indicate success, we need to fake it. 156 // application is expecting 200 to indicate success, we need to fake it.
141 if (httpStatusCode == 206) { 157 if (mHttpStatusCode == 206) {
142 httpStatusCode = 200; 158 mHttpStatusCode = 200;
143 } 159 }
144 return httpStatusCode; 160 return mHttpStatusCode;
145 } 161 }
146 162
147 @Override 163 @Override
148 public String getHttpStatusText() { 164 public String getHttpStatusText() {
miloslav 2015/02/26 15:56:49 As above. Also, it would be nice if you change the
xunjieli 2015/02/26 17:54:00 Done.
149 return nativeGetHttpStatusText(mUrlRequestAdapter); 165 if (mUrlRequestAdapter != 0) {
166 mHttpStatusText = nativeGetHttpStatusText(mUrlRequestAdapter);
167 }
168 return mHttpStatusText;
150 } 169 }
151 170
152 /** 171 /**
153 * Returns an exception if any, or null if the request was completed 172 * Returns an exception if any, or null if the request was completed
154 * successfully. 173 * successfully.
155 */ 174 */
156 @Override 175 @Override
157 public IOException getException() { 176 public IOException getException() {
158 if (mSinkException != null) { 177 if (mSinkException != null) {
159 return mSinkException; 178 return mSinkException;
160 } 179 }
161 180
162 validateNotRecycled(); 181 validateNotRecycled();
163 182
164 int errorCode = nativeGetErrorCode(mUrlRequestAdapter); 183 if (mUrlRequestAdapter != 0) {
165 switch (errorCode) { 184 mErrorCode = nativeGetErrorCode(mUrlRequestAdapter);
mmenke 2015/02/25 23:43:57 Can we populate this and mErrorString in whatever
mmenke 2015/02/25 23:45:37 Also, if we care about this case, seems a problem
xunjieli 2015/02/26 17:54:00 Acknowledged.
xunjieli 2015/02/26 17:54:00 Done. Thanks for clarifying about validateNotRecyc
185 }
186
187 switch (mErrorCode) {
166 case ChromiumUrlRequestError.SUCCESS: 188 case ChromiumUrlRequestError.SUCCESS:
167 if (mContentLengthOverLimit) { 189 if (mContentLengthOverLimit) {
168 return new ResponseTooLargeException(); 190 return new ResponseTooLargeException();
169 } 191 }
170 return null; 192 return null;
171 case ChromiumUrlRequestError.UNKNOWN: 193 case ChromiumUrlRequestError.UNKNOWN:
172 return new IOException( 194 if (mUrlRequestAdapter != 0) {
173 nativeGetErrorString(mUrlRequestAdapter)); 195 mErrorString = nativeGetErrorString(mUrlRequestAdapter);
196 }
197 return new IOException(mErrorString);
174 case ChromiumUrlRequestError.MALFORMED_URL: 198 case ChromiumUrlRequestError.MALFORMED_URL:
175 return new MalformedURLException("Malformed URL: " + mUrl); 199 return new MalformedURLException("Malformed URL: " + mUrl);
176 case ChromiumUrlRequestError.CONNECTION_TIMED_OUT: 200 case ChromiumUrlRequestError.CONNECTION_TIMED_OUT:
177 return new ConnectTimeoutException("Connection timed out"); 201 return new ConnectTimeoutException("Connection timed out");
178 case ChromiumUrlRequestError.UNKNOWN_HOST: 202 case ChromiumUrlRequestError.UNKNOWN_HOST:
179 String host; 203 String host;
180 try { 204 try {
181 host = new URL(mUrl).getHost(); 205 host = new URL(mUrl).getHost();
182 } catch (MalformedURLException e) { 206 } catch (MalformedURLException e) {
183 host = mUrl; 207 host = mUrl;
184 } 208 }
185 return new UnknownHostException("Unknown host: " + host); 209 return new UnknownHostException("Unknown host: " + host);
186 case ChromiumUrlRequestError.TOO_MANY_REDIRECTS: 210 case ChromiumUrlRequestError.TOO_MANY_REDIRECTS:
187 return new IOException("Request failed because there were too " 211 return new IOException("Request failed because there were too "
188 + "many redirects or redirects have been disabled"); 212 + "many redirects or redirects have been disabled");
189 default: 213 default:
190 throw new IllegalStateException( 214 throw new IllegalStateException(
191 "Unrecognized error code: " + errorCode); 215 "Unrecognized error code: " + mErrorCode);
192 } 216 }
193 } 217 }
194 218
195 @Override 219 @Override
196 public ByteBuffer getByteBuffer() { 220 public ByteBuffer getByteBuffer() {
197 return ((ChunkedWritableByteChannel) getSink()).getByteBuffer(); 221 return ((ChunkedWritableByteChannel) getSink()).getByteBuffer();
198 } 222 }
199 223
200 @Override 224 @Override
201 public byte[] getResponseAsBytes() { 225 public byte[] getResponseAsBytes() {
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 private native void nativeGetAllHeaders(long urlRequestAdapter, 744 private native void nativeGetAllHeaders(long urlRequestAdapter,
721 ResponseHeadersMap headers); 745 ResponseHeadersMap headers);
722 746
723 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); 747 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter);
724 748
725 // Explicit class to work around JNI-generator generics confusion. 749 // Explicit class to work around JNI-generator generics confusion.
726 private static class ResponseHeadersMap extends 750 private static class ResponseHeadersMap extends
727 HashMap<String, List<String>> { 751 HashMap<String, List<String>> {
728 } 752 }
729 } 753 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698