| 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/cronet_url_request_adapter.h" | 5 #include "components/cronet/android/cronet_url_request_adapter.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "net/http/http_status_code.h" | 22 #include "net/http/http_status_code.h" |
| 23 #include "net/http/http_util.h" | 23 #include "net/http/http_util.h" |
| 24 #include "net/ssl/ssl_info.h" | 24 #include "net/ssl/ssl_info.h" |
| 25 #include "net/url_request/redirect_info.h" | 25 #include "net/url_request/redirect_info.h" |
| 26 #include "net/url_request/url_request_context.h" | 26 #include "net/url_request/url_request_context.h" |
| 27 | 27 |
| 28 using base::android::ConvertUTF8ToJavaString; | 28 using base::android::ConvertUTF8ToJavaString; |
| 29 | 29 |
| 30 namespace cronet { | 30 namespace cronet { |
| 31 | 31 |
| 32 namespace { |
| 33 |
| 34 // Error codes for the most popular network stack error codes. |
| 35 // For descriptions see corresponding constants in UrlRequestException.java. |
| 36 // A Java counterpart will be generated for this enum. |
| 37 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net |
| 38 enum UrlRequestError { |
| 39 LISTENER_EXCEPTION_THROWN, |
| 40 HOSTNAME_NOT_RESOLVED, |
| 41 INTERNET_DISCONNECTED, |
| 42 NETWORK_CHANGED, |
| 43 TIMED_OUT, |
| 44 CONNECTION_CLOSED, |
| 45 CONNECTION_TIMED_OUT, |
| 46 CONNECTION_REFUSED, |
| 47 CONNECTION_RESET, |
| 48 ADDRESS_UNREACHABLE, |
| 49 OTHER, |
| 50 }; |
| 51 |
| 52 // Converts most popular net::ERR_* values to counterparts accessible in Java. |
| 53 UrlRequestError NetErrorToUrlRequestError(int net_error) { |
| 54 switch (net_error) { |
| 55 case net::ERR_NAME_NOT_RESOLVED: |
| 56 return HOSTNAME_NOT_RESOLVED; |
| 57 case net::ERR_INTERNET_DISCONNECTED: |
| 58 return INTERNET_DISCONNECTED; |
| 59 case net::ERR_NETWORK_CHANGED: |
| 60 return NETWORK_CHANGED; |
| 61 case net::ERR_TIMED_OUT: |
| 62 return TIMED_OUT; |
| 63 case net::ERR_CONNECTION_CLOSED: |
| 64 return CONNECTION_CLOSED; |
| 65 case net::ERR_CONNECTION_TIMED_OUT: |
| 66 return CONNECTION_TIMED_OUT; |
| 67 case net::ERR_CONNECTION_REFUSED: |
| 68 return CONNECTION_REFUSED; |
| 69 case net::ERR_CONNECTION_RESET: |
| 70 return CONNECTION_RESET; |
| 71 case net::ERR_ADDRESS_UNREACHABLE: |
| 72 return ADDRESS_UNREACHABLE; |
| 73 default: |
| 74 return OTHER; |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 32 // Explicitly register static JNI functions. | 79 // Explicitly register static JNI functions. |
| 33 bool CronetUrlRequestAdapterRegisterJni(JNIEnv* env) { | 80 bool CronetUrlRequestAdapterRegisterJni(JNIEnv* env) { |
| 34 return RegisterNativesImpl(env); | 81 return RegisterNativesImpl(env); |
| 35 } | 82 } |
| 36 | 83 |
| 37 static jlong CreateRequestAdapter(JNIEnv* env, | 84 static jlong CreateRequestAdapter(JNIEnv* env, |
| 38 const JavaParamRef<jobject>& jurl_request, | 85 const JavaParamRef<jobject>& jurl_request, |
| 39 jlong jurl_request_context_adapter, | 86 jlong jurl_request_context_adapter, |
| 40 const JavaParamRef<jstring>& jurl_string, | 87 const JavaParamRef<jstring>& jurl_string, |
| 41 jint jpriority) { | 88 jint jpriority) { |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 330 |
| 284 void CronetURLRequestAdapter::OnSSLCertificateError( | 331 void CronetURLRequestAdapter::OnSSLCertificateError( |
| 285 net::URLRequest* request, | 332 net::URLRequest* request, |
| 286 const net::SSLInfo& ssl_info, | 333 const net::SSLInfo& ssl_info, |
| 287 bool fatal) { | 334 bool fatal) { |
| 288 DCHECK(context_->IsOnNetworkThread()); | 335 DCHECK(context_->IsOnNetworkThread()); |
| 289 request->Cancel(); | 336 request->Cancel(); |
| 290 int net_error = net::MapCertStatusToNetError(ssl_info.cert_status); | 337 int net_error = net::MapCertStatusToNetError(ssl_info.cert_status); |
| 291 JNIEnv* env = base::android::AttachCurrentThread(); | 338 JNIEnv* env = base::android::AttachCurrentThread(); |
| 292 cronet::Java_CronetUrlRequest_onError( | 339 cronet::Java_CronetUrlRequest_onError( |
| 293 env, owner_.obj(), net_error, | 340 env, owner_.obj(), NetErrorToUrlRequestError(net_error), net_error, |
| 294 ConvertUTF8ToJavaString(env, net::ErrorToString(net_error)).obj(), | 341 ConvertUTF8ToJavaString(env, net::ErrorToString(net_error)).obj(), |
| 295 request->GetTotalReceivedBytes()); | 342 request->GetTotalReceivedBytes()); |
| 296 } | 343 } |
| 297 | 344 |
| 298 void CronetURLRequestAdapter::OnResponseStarted(net::URLRequest* request) { | 345 void CronetURLRequestAdapter::OnResponseStarted(net::URLRequest* request) { |
| 299 DCHECK(context_->IsOnNetworkThread()); | 346 DCHECK(context_->IsOnNetworkThread()); |
| 300 if (MaybeReportError(request)) | 347 if (MaybeReportError(request)) |
| 301 return; | 348 return; |
| 302 JNIEnv* env = base::android::AttachCurrentThread(); | 349 JNIEnv* env = base::android::AttachCurrentThread(); |
| 303 cronet::Java_CronetUrlRequest_onResponseStarted( | 350 cronet::Java_CronetUrlRequest_onResponseStarted( |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 bool CronetURLRequestAdapter::MaybeReportError(net::URLRequest* request) const { | 452 bool CronetURLRequestAdapter::MaybeReportError(net::URLRequest* request) const { |
| 406 DCHECK_NE(net::URLRequestStatus::IO_PENDING, url_request_->status().status()); | 453 DCHECK_NE(net::URLRequestStatus::IO_PENDING, url_request_->status().status()); |
| 407 DCHECK_EQ(request, url_request_.get()); | 454 DCHECK_EQ(request, url_request_.get()); |
| 408 if (url_request_->status().is_success()) | 455 if (url_request_->status().is_success()) |
| 409 return false; | 456 return false; |
| 410 int net_error = url_request_->status().error(); | 457 int net_error = url_request_->status().error(); |
| 411 VLOG(1) << "Error " << net::ErrorToString(net_error) | 458 VLOG(1) << "Error " << net::ErrorToString(net_error) |
| 412 << " on chromium request: " << initial_url_.possibly_invalid_spec(); | 459 << " on chromium request: " << initial_url_.possibly_invalid_spec(); |
| 413 JNIEnv* env = base::android::AttachCurrentThread(); | 460 JNIEnv* env = base::android::AttachCurrentThread(); |
| 414 cronet::Java_CronetUrlRequest_onError( | 461 cronet::Java_CronetUrlRequest_onError( |
| 415 env, owner_.obj(), net_error, | 462 env, owner_.obj(), NetErrorToUrlRequestError(net_error), net_error, |
| 416 ConvertUTF8ToJavaString(env, net::ErrorToString(net_error)).obj(), | 463 ConvertUTF8ToJavaString(env, net::ErrorToString(net_error)).obj(), |
| 417 request->GetTotalReceivedBytes()); | 464 request->GetTotalReceivedBytes()); |
| 418 return true; | 465 return true; |
| 419 } | 466 } |
| 420 | 467 |
| 421 } // namespace cronet | 468 } // namespace cronet |
| OLD | NEW |