Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "components/cronet/android/chromium_url_request.h" | 5 #include "components/cronet/android/chromium_url_request.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "components/cronet/android/url_request_adapter.h" | 10 #include "components/cronet/android/url_request_adapter.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 case REQUEST_PRIORITY_HIGHEST: | 33 case REQUEST_PRIORITY_HIGHEST: |
| 34 return net::HIGHEST; | 34 return net::HIGHEST; |
| 35 default: | 35 default: |
| 36 return net::LOWEST; | 36 return net::LOWEST; |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 void SetPostContentType(JNIEnv* env, | 40 void SetPostContentType(JNIEnv* env, |
| 41 URLRequestAdapter* request_adapter, | 41 URLRequestAdapter* request_adapter, |
| 42 jstring content_type) { | 42 jstring content_type) { |
| 43 DCHECK(request_adapter); | |
|
xunjieli
2015/02/20 22:39:18
Moving the DCHECK to caller, to make it clearer th
| |
| 44 | |
| 45 std::string method_post("POST"); | 43 std::string method_post("POST"); |
| 46 request_adapter->SetMethod(method_post); | 44 request_adapter->SetMethod(method_post); |
| 47 | 45 |
| 48 std::string content_type_header("Content-Type"); | 46 std::string content_type_header("Content-Type"); |
| 49 std::string content_type_string(ConvertJavaStringToUTF8(env, content_type)); | 47 std::string content_type_string(ConvertJavaStringToUTF8(env, content_type)); |
| 50 | 48 |
| 51 request_adapter->AddHeader(content_type_header, content_type_string); | 49 request_adapter->AddHeader(content_type_header, content_type_string); |
| 52 } | 50 } |
| 53 | 51 |
| 54 // A delegate of URLRequestAdapter that delivers callbacks to the Java layer. | 52 // A delegate of URLRequestAdapter that delivers callbacks to the Java layer. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 request_adapter->SetMethod(method); | 155 request_adapter->SetMethod(method); |
| 158 } | 156 } |
| 159 | 157 |
| 160 static void SetUploadData(JNIEnv* env, | 158 static void SetUploadData(JNIEnv* env, |
| 161 jobject jcaller, | 159 jobject jcaller, |
| 162 jlong jurl_request_adapter, | 160 jlong jurl_request_adapter, |
| 163 jstring jcontent_type, | 161 jstring jcontent_type, |
| 164 jbyteArray jcontent) { | 162 jbyteArray jcontent) { |
| 165 URLRequestAdapter* request_adapter = | 163 URLRequestAdapter* request_adapter = |
| 166 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 164 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 165 DCHECK(request_adapter); | |
| 167 SetPostContentType(env, request_adapter, jcontent_type); | 166 SetPostContentType(env, request_adapter, jcontent_type); |
| 168 | 167 |
| 169 if (jcontent != NULL) { | 168 if (jcontent != NULL) { |
| 170 jsize size = env->GetArrayLength(jcontent); | 169 jsize size = env->GetArrayLength(jcontent); |
| 171 if (size > 0) { | 170 if (size > 0) { |
| 172 jbyte* content_bytes = env->GetByteArrayElements(jcontent, NULL); | 171 jbyte* content_bytes = env->GetByteArrayElements(jcontent, NULL); |
| 173 request_adapter->SetUploadContent( | 172 request_adapter->SetUploadContent( |
| 174 reinterpret_cast<const char*>(content_bytes), size); | 173 reinterpret_cast<const char*>(content_bytes), size); |
| 175 env->ReleaseByteArrayElements(jcontent, content_bytes, 0); | 174 env->ReleaseByteArrayElements(jcontent, content_bytes, 0); |
| 176 } | 175 } |
| 177 } | 176 } |
| 178 } | 177 } |
| 179 | 178 |
| 180 static void SetUploadChannel(JNIEnv* env, | 179 static void SetUploadChannel(JNIEnv* env, |
| 181 jobject jcaller, | 180 jobject jcaller, |
| 182 jlong jurl_request_adapter, | 181 jlong jurl_request_adapter, |
| 183 jstring jcontent_type, | 182 jstring jcontent_type, |
| 184 jlong jcontent_length) { | 183 jlong jcontent_length) { |
| 185 URLRequestAdapter* request_adapter = | 184 URLRequestAdapter* request_adapter = |
| 186 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 185 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 186 DCHECK(request_adapter); | |
| 187 SetPostContentType(env, request_adapter, jcontent_type); | 187 SetPostContentType(env, request_adapter, jcontent_type); |
| 188 | 188 |
| 189 request_adapter->SetUploadChannel(env, jcontent_length); | 189 request_adapter->SetUploadChannel(env, jcontent_length); |
| 190 } | 190 } |
| 191 | 191 |
| 192 static void EnableChunkedUpload(JNIEnv* env, | 192 static void EnableChunkedUpload(JNIEnv* env, |
| 193 jobject jcaller, | 193 jobject jcaller, |
| 194 jlong jurl_request_adapter, | 194 jlong jurl_request_adapter, |
| 195 jstring jcontent_type) { | 195 jstring jcontent_type) { |
| 196 URLRequestAdapter* request_adapter = | 196 URLRequestAdapter* request_adapter = |
| 197 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 197 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 198 DCHECK(request_adapter); | |
| 198 SetPostContentType(env, request_adapter, jcontent_type); | 199 SetPostContentType(env, request_adapter, jcontent_type); |
| 199 | 200 |
| 200 request_adapter->EnableChunkedUpload(); | 201 request_adapter->EnableChunkedUpload(); |
| 201 } | 202 } |
| 202 | 203 |
| 203 static void AppendChunk(JNIEnv* env, | 204 static void AppendChunk(JNIEnv* env, |
| 204 jobject jcaller, | 205 jobject jcaller, |
| 205 jlong jurl_request_adapter, | 206 jlong jurl_request_adapter, |
| 206 jobject jchunk_byte_buffer, | 207 jobject jchunk_byte_buffer, |
| 207 jint jchunk_size, | 208 jint jchunk_size, |
| 208 jboolean jis_last_chunk) { | 209 jboolean jis_last_chunk) { |
| 209 URLRequestAdapter* request_adapter = | 210 URLRequestAdapter* request_adapter = |
| 210 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 211 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 212 DCHECK(request_adapter); | |
| 211 DCHECK(jchunk_byte_buffer); | 213 DCHECK(jchunk_byte_buffer); |
| 212 | 214 |
| 213 void* chunk = env->GetDirectBufferAddress(jchunk_byte_buffer); | 215 void* chunk = env->GetDirectBufferAddress(jchunk_byte_buffer); |
| 214 request_adapter->AppendChunk(reinterpret_cast<const char*>(chunk), | 216 request_adapter->AppendChunk(reinterpret_cast<const char*>(chunk), |
| 215 jchunk_size, jis_last_chunk); | 217 jchunk_size, jis_last_chunk); |
| 216 } | 218 } |
| 217 | 219 |
| 218 /* synchronized */ | 220 /* synchronized */ |
| 219 static void Start(JNIEnv* env, jobject jcaller, jlong jurl_request_adapter) { | 221 static void Start(JNIEnv* env, jobject jcaller, jlong jurl_request_adapter) { |
| 220 URLRequestAdapter* request_adapter = | 222 URLRequestAdapter* request_adapter = |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 239 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 241 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 240 if (request_adapter != NULL) | 242 if (request_adapter != NULL) |
| 241 request_adapter->Cancel(); | 243 request_adapter->Cancel(); |
| 242 } | 244 } |
| 243 | 245 |
| 244 static jint GetErrorCode(JNIEnv* env, | 246 static jint GetErrorCode(JNIEnv* env, |
| 245 jobject jcaller, | 247 jobject jcaller, |
| 246 jlong jurl_request_adapter) { | 248 jlong jurl_request_adapter) { |
| 247 URLRequestAdapter* request_adapter = | 249 URLRequestAdapter* request_adapter = |
| 248 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 250 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 251 if (request_adapter == NULL) | |
|
mmenke
2015/02/20 23:19:42
I think these checks make a little more sense Java
xunjieli
2015/02/25 21:31:28
Done.
| |
| 252 return REQUEST_ERROR_UNKNOWN; | |
| 249 int error_code = request_adapter->error_code(); | 253 int error_code = request_adapter->error_code(); |
| 250 switch (error_code) { | 254 switch (error_code) { |
| 251 // TODO(mef): Investigate returning success on positive values, too, as | 255 // TODO(mef): Investigate returning success on positive values, too, as |
| 252 // they technically indicate success. | 256 // they technically indicate success. |
| 253 case net::OK: | 257 case net::OK: |
| 254 return REQUEST_ERROR_SUCCESS; | 258 return REQUEST_ERROR_SUCCESS; |
| 255 | 259 |
| 256 // TODO(mef): Investigate this. The fact is that Chrome does not do this, | 260 // TODO(mef): Investigate this. The fact is that Chrome does not do this, |
| 257 // and this library is not just being used for downloads. | 261 // and this library is not just being used for downloads. |
| 258 | 262 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 279 return REQUEST_ERROR_TOO_MANY_REDIRECTS; | 283 return REQUEST_ERROR_TOO_MANY_REDIRECTS; |
| 280 } | 284 } |
| 281 return REQUEST_ERROR_UNKNOWN; | 285 return REQUEST_ERROR_UNKNOWN; |
| 282 } | 286 } |
| 283 | 287 |
| 284 static jstring GetErrorString(JNIEnv* env, | 288 static jstring GetErrorString(JNIEnv* env, |
| 285 jobject jcaller, | 289 jobject jcaller, |
| 286 jlong jurl_request_adapter) { | 290 jlong jurl_request_adapter) { |
| 287 URLRequestAdapter* request_adapter = | 291 URLRequestAdapter* request_adapter = |
| 288 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 292 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 293 if (request_adapter == NULL) | |
| 294 return ConvertUTF8ToJavaString(env, "").Release(); | |
| 289 int error_code = request_adapter->error_code(); | 295 int error_code = request_adapter->error_code(); |
| 290 char buffer[200]; | 296 char buffer[200]; |
| 291 std::string error_string = net::ErrorToString(error_code); | 297 std::string error_string = net::ErrorToString(error_code); |
| 292 snprintf(buffer, | 298 snprintf(buffer, |
| 293 sizeof(buffer), | 299 sizeof(buffer), |
| 294 "System error: %s(%d)", | 300 "System error: %s(%d)", |
| 295 error_string.c_str(), | 301 error_string.c_str(), |
| 296 error_code); | 302 error_code); |
| 297 return ConvertUTF8ToJavaString(env, buffer).Release(); | 303 return ConvertUTF8ToJavaString(env, buffer).Release(); |
| 298 } | 304 } |
| 299 | 305 |
| 300 static jint GetHttpStatusCode(JNIEnv* env, | 306 static jint GetHttpStatusCode(JNIEnv* env, |
| 301 jobject jcaller, | 307 jobject jcaller, |
| 302 jlong jurl_request_adapter) { | 308 jlong jurl_request_adapter) { |
| 303 URLRequestAdapter* request_adapter = | 309 URLRequestAdapter* request_adapter = |
| 304 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 310 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 311 if (request_adapter == NULL) | |
| 312 return 0; | |
| 305 return request_adapter->http_status_code(); | 313 return request_adapter->http_status_code(); |
| 306 } | 314 } |
| 307 | 315 |
| 308 static jstring GetHttpStatusText(JNIEnv* env, | 316 static jstring GetHttpStatusText(JNIEnv* env, |
| 309 jobject jcaller, | 317 jobject jcaller, |
| 310 jlong jurl_request_adapter) { | 318 jlong jurl_request_adapter) { |
| 311 URLRequestAdapter* request_adapter = | 319 URLRequestAdapter* request_adapter = |
| 312 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 320 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 321 if (request_adapter == NULL) | |
| 322 return ConvertUTF8ToJavaString(env, "").Release(); | |
| 313 return ConvertUTF8ToJavaString(env, request_adapter->http_status_text()) | 323 return ConvertUTF8ToJavaString(env, request_adapter->http_status_text()) |
| 314 .Release(); | 324 .Release(); |
| 315 } | 325 } |
| 316 | 326 |
| 317 static jstring GetContentType(JNIEnv* env, | 327 static jstring GetContentType(JNIEnv* env, |
| 318 jobject jcaller, | 328 jobject jcaller, |
| 319 jlong jurl_request_adapter) { | 329 jlong jurl_request_adapter) { |
| 320 URLRequestAdapter* request_adapter = | 330 URLRequestAdapter* request_adapter = |
| 321 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 331 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 322 if (request_adapter == NULL) | 332 if (request_adapter == NULL) |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 342 static jstring GetHeader(JNIEnv* env, | 352 static jstring GetHeader(JNIEnv* env, |
| 343 jobject jcaller, | 353 jobject jcaller, |
| 344 jlong jurl_request_adapter, | 354 jlong jurl_request_adapter, |
| 345 jstring jheader_name) { | 355 jstring jheader_name) { |
| 346 URLRequestAdapter* request_adapter = | 356 URLRequestAdapter* request_adapter = |
| 347 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 357 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 348 if (request_adapter == NULL) | 358 if (request_adapter == NULL) |
| 349 return NULL; | 359 return NULL; |
| 350 std::string header_name = ConvertJavaStringToUTF8(env, jheader_name); | 360 std::string header_name = ConvertJavaStringToUTF8(env, jheader_name); |
| 351 std::string header_value = request_adapter->GetHeader(header_name); | 361 std::string header_value = request_adapter->GetHeader(header_name); |
| 352 if (!header_value.empty()) { | 362 if (!header_value.empty()) |
| 353 return ConvertUTF8ToJavaString(env, header_value.c_str()).Release(); | 363 return ConvertUTF8ToJavaString(env, header_value.c_str()).Release(); |
| 354 } else { | 364 return NULL; |
| 355 return NULL; | |
| 356 } | |
| 357 } | 365 } |
| 358 | 366 |
| 359 static void GetAllHeaders(JNIEnv* env, | 367 static void GetAllHeaders(JNIEnv* env, |
| 360 jobject jcaller, | 368 jobject jcaller, |
| 361 jlong jurl_request_adapter, | 369 jlong jurl_request_adapter, |
| 362 jobject jheaders_map) { | 370 jobject jheaders_map) { |
| 363 URLRequestAdapter* request_adapter = | 371 URLRequestAdapter* request_adapter = |
| 364 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 372 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 365 if (request_adapter == NULL) | 373 if (request_adapter == NULL) |
| 366 return; | 374 return; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 return ConvertUTF8ToJavaString(env, "").Release(); | 406 return ConvertUTF8ToJavaString(env, "").Release(); |
| 399 | 407 |
| 400 std::string negotiated_protocol = request_adapter->GetNegotiatedProtocol(); | 408 std::string negotiated_protocol = request_adapter->GetNegotiatedProtocol(); |
| 401 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()).Release(); | 409 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()).Release(); |
| 402 } | 410 } |
| 403 | 411 |
| 404 static void DisableRedirects(JNIEnv* env, jobject jcaller, | 412 static void DisableRedirects(JNIEnv* env, jobject jcaller, |
| 405 jlong jrequest_adapter) { | 413 jlong jrequest_adapter) { |
| 406 URLRequestAdapter* request_adapter = | 414 URLRequestAdapter* request_adapter = |
| 407 reinterpret_cast<URLRequestAdapter*>(jrequest_adapter); | 415 reinterpret_cast<URLRequestAdapter*>(jrequest_adapter); |
| 408 if (request_adapter != NULL) | 416 DCHECK(request_adapter); |
|
xunjieli
2015/02/20 22:39:18
This method should only be called when the adapter
| |
| 409 request_adapter->DisableRedirects(); | 417 request_adapter->DisableRedirects(); |
| 410 } | 418 } |
| 411 | 419 |
| 412 } // namespace cronet | 420 } // namespace cronet |
| OLD | NEW |