| 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_context_adapter.h" | 5 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/android/jni_android.h" | 14 #include "base/android/jni_android.h" |
| 15 #include "base/android/jni_array.h" | 15 #include "base/android/jni_array.h" |
| 16 #include "base/android/jni_string.h" | 16 #include "base/android/jni_string.h" |
| 17 #include "base/base64.h" |
| 17 #include "base/bind.h" | 18 #include "base/bind.h" |
| 18 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
| 19 #include "base/files/file_util.h" | 20 #include "base/files/file_util.h" |
| 20 #include "base/files/scoped_file.h" | 21 #include "base/files/scoped_file.h" |
| 21 #include "base/lazy_instance.h" | 22 #include "base/lazy_instance.h" |
| 22 #include "base/logging.h" | 23 #include "base/logging.h" |
| 23 #include "base/macros.h" | 24 #include "base/macros.h" |
| 24 #include "base/memory/ptr_util.h" | 25 #include "base/memory/ptr_util.h" |
| 25 #include "base/memory/scoped_vector.h" | 26 #include "base/memory/scoped_vector.h" |
| 26 #include "base/message_loop/message_loop.h" | 27 #include "base/message_loop/message_loop.h" |
| 27 #include "base/metrics/statistics_recorder.h" | 28 #include "base/metrics/statistics_recorder.h" |
| 28 #include "base/single_thread_task_runner.h" | 29 #include "base/single_thread_task_runner.h" |
| 29 #include "base/time/time.h" | 30 #include "base/time/time.h" |
| 30 #include "base/values.h" | 31 #include "base/values.h" |
| 31 #include "components/cronet/histogram_manager.h" | 32 #include "components/cronet/histogram_manager.h" |
| 32 #include "components/cronet/url_request_context_config.h" | 33 #include "components/cronet/url_request_context_config.h" |
| 33 #include "components/prefs/pref_change_registrar.h" | 34 #include "components/prefs/pref_change_registrar.h" |
| 34 #include "components/prefs/pref_filter.h" | 35 #include "components/prefs/pref_filter.h" |
| 35 #include "components/prefs/pref_registry_simple.h" | 36 #include "components/prefs/pref_registry_simple.h" |
| 36 #include "components/prefs/pref_service.h" | 37 #include "components/prefs/pref_service.h" |
| 37 #include "components/prefs/pref_service_factory.h" | 38 #include "components/prefs/pref_service_factory.h" |
| 38 #include "jni/CronetUrlRequestContext_jni.h" | 39 #include "jni/CronetUrlRequestContext_jni.h" |
| 39 #include "net/base/load_flags.h" | 40 #include "net/base/load_flags.h" |
| 40 #include "net/base/logging_network_change_observer.h" | 41 #include "net/base/logging_network_change_observer.h" |
| 41 #include "net/base/net_errors.h" | 42 #include "net/base/net_errors.h" |
| 42 #include "net/base/network_delegate_impl.h" | 43 #include "net/base/network_delegate_impl.h" |
| 43 #include "net/base/url_util.h" | 44 #include "net/base/url_util.h" |
| 45 #include "net/cert/caching_cert_verifier.h" |
| 44 #include "net/cert/cert_verifier.h" | 46 #include "net/cert/cert_verifier.h" |
| 45 #include "net/cookies/cookie_monster.h" | 47 #include "net/cookies/cookie_monster.h" |
| 48 #include "net/extras/cert/cert_verifier_cache_persister.h" |
| 46 #include "net/http/http_auth_handler_factory.h" | 49 #include "net/http/http_auth_handler_factory.h" |
| 47 #include "net/http/http_server_properties_manager.h" | 50 #include "net/http/http_server_properties_manager.h" |
| 48 #include "net/log/write_to_file_net_log_observer.h" | 51 #include "net/log/write_to_file_net_log_observer.h" |
| 49 #include "net/nqe/external_estimate_provider.h" | 52 #include "net/nqe/external_estimate_provider.h" |
| 50 #include "net/proxy/proxy_config_service_android.h" | 53 #include "net/proxy/proxy_config_service_android.h" |
| 51 #include "net/proxy/proxy_service.h" | 54 #include "net/proxy/proxy_service.h" |
| 52 #include "net/sdch/sdch_owner.h" | 55 #include "net/sdch/sdch_owner.h" |
| 53 #include "net/ssl/channel_id_service.h" | 56 #include "net/ssl/channel_id_service.h" |
| 54 #include "net/url_request/url_request_context.h" | 57 #include "net/url_request/url_request_context.h" |
| 55 #include "net/url_request/url_request_context_builder.h" | 58 #include "net/url_request/url_request_context_builder.h" |
| (...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 | 623 |
| 621 url::SchemeHostPort quic_server("https", canon_host, quic_hint.port); | 624 url::SchemeHostPort quic_server("https", canon_host, quic_hint.port); |
| 622 net::AlternativeService alternative_service( | 625 net::AlternativeService alternative_service( |
| 623 net::AlternateProtocol::QUIC, "", | 626 net::AlternateProtocol::QUIC, "", |
| 624 static_cast<uint16_t>(quic_hint.alternate_port)); | 627 static_cast<uint16_t>(quic_hint.alternate_port)); |
| 625 context_->http_server_properties()->SetAlternativeService( | 628 context_->http_server_properties()->SetAlternativeService( |
| 626 quic_server, alternative_service, base::Time::Max()); | 629 quic_server, alternative_service, base::Time::Max()); |
| 627 } | 630 } |
| 628 } | 631 } |
| 629 | 632 |
| 633 // If there is a cert_verifier, then pouplate it's cache with |
| 634 // |cert_verifier_data|. |
| 635 if (!config->cert_verifier_data.empty() && context_->cert_verifier()) { |
| 636 std::string data; |
| 637 if (base::Base64Decode(config->cert_verifier_data, &data)) { |
| 638 net::CertVerifierCachePersister persister( |
| 639 reinterpret_cast<net::CachingCertVerifier*>( |
| 640 context_->cert_verifier())); |
| 641 persister.LoadCache(data); |
| 642 } |
| 643 } |
| 644 |
| 630 // Iterate through PKP configuration for every host. | 645 // Iterate through PKP configuration for every host. |
| 631 for (const auto& pkp : config->pkp_list) { | 646 for (const auto& pkp : config->pkp_list) { |
| 632 // Add the host pinning. | 647 // Add the host pinning. |
| 633 context_->transport_security_state()->AddHPKP( | 648 context_->transport_security_state()->AddHPKP( |
| 634 pkp->host, pkp->expiration_date, pkp->include_subdomains, | 649 pkp->host, pkp->expiration_date, pkp->include_subdomains, |
| 635 pkp->pin_hashes, GURL::EmptyGURL()); | 650 pkp->pin_hashes, GURL::EmptyGURL()); |
| 636 } | 651 } |
| 637 | 652 |
| 638 JNIEnv* env = base::android::AttachCurrentThread(); | 653 JNIEnv* env = base::android::AttachCurrentThread(); |
| 639 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); | 654 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 | 729 |
| 715 void CronetURLRequestContextAdapter::StopNetLog( | 730 void CronetURLRequestContextAdapter::StopNetLog( |
| 716 JNIEnv* env, | 731 JNIEnv* env, |
| 717 const JavaParamRef<jobject>& jcaller) { | 732 const JavaParamRef<jobject>& jcaller) { |
| 718 PostTaskToNetworkThread( | 733 PostTaskToNetworkThread( |
| 719 FROM_HERE, | 734 FROM_HERE, |
| 720 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, | 735 base::Bind(&CronetURLRequestContextAdapter::StopNetLogOnNetworkThread, |
| 721 base::Unretained(this))); | 736 base::Unretained(this))); |
| 722 } | 737 } |
| 723 | 738 |
| 739 void CronetURLRequestContextAdapter::GetCertVerifierData( |
| 740 JNIEnv* env, |
| 741 const JavaParamRef<jobject>& jcaller) { |
| 742 PostTaskToNetworkThread( |
| 743 FROM_HERE, |
| 744 base::Bind( |
| 745 &CronetURLRequestContextAdapter::GetCertVerifierDataOnNetworkThread, |
| 746 base::Unretained(this))); |
| 747 } |
| 748 |
| 724 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( | 749 void CronetURLRequestContextAdapter::StartNetLogToFileOnNetworkThread( |
| 725 const std::string& file_name, bool log_all) { | 750 const std::string& file_name, bool log_all) { |
| 726 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 751 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 727 DCHECK(is_context_initialized_); | 752 DCHECK(is_context_initialized_); |
| 728 DCHECK(context_); | 753 DCHECK(context_); |
| 729 // Do nothing if already logging to a file. | 754 // Do nothing if already logging to a file. |
| 730 if (write_to_file_observer_) | 755 if (write_to_file_observer_) |
| 731 return; | 756 return; |
| 732 base::FilePath file_path(file_name); | 757 base::FilePath file_path(file_name); |
| 733 base::ScopedFILE file(base::OpenFile(file_path, "w")); | 758 base::ScopedFILE file(base::OpenFile(file_path, "w")); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 746 } | 771 } |
| 747 | 772 |
| 748 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { | 773 void CronetURLRequestContextAdapter::StopNetLogOnNetworkThread() { |
| 749 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 774 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 750 if (write_to_file_observer_) { | 775 if (write_to_file_observer_) { |
| 751 write_to_file_observer_->StopObserving(context_.get()); | 776 write_to_file_observer_->StopObserving(context_.get()); |
| 752 write_to_file_observer_.reset(); | 777 write_to_file_observer_.reset(); |
| 753 } | 778 } |
| 754 } | 779 } |
| 755 | 780 |
| 781 void CronetURLRequestContextAdapter::GetCertVerifierDataOnNetworkThread() { |
| 782 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 783 std::string encoded_data; |
| 784 if (is_context_initialized_ && context_->cert_verifier()) { |
| 785 std::string data; |
| 786 net::CertVerifierCachePersister persister( |
| 787 reinterpret_cast<net::CachingCertVerifier*>(context_->cert_verifier())); |
| 788 persister.SerializeCache(&data); |
| 789 base::Base64Encode(data, &encoded_data); |
| 790 } |
| 791 JNIEnv* env = base::android::AttachCurrentThread(); |
| 792 Java_CronetUrlRequestContext_onGetCertVerifierData( |
| 793 env, jcronet_url_request_context_.obj(), |
| 794 base::android::ConvertUTF8ToJavaString(env, encoded_data).obj()); |
| 795 } |
| 796 |
| 756 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { | 797 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { |
| 757 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 798 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 758 if (!file_thread_) { | 799 if (!file_thread_) { |
| 759 file_thread_.reset(new base::Thread("Network File Thread")); | 800 file_thread_.reset(new base::Thread("Network File Thread")); |
| 760 file_thread_->Start(); | 801 file_thread_->Start(); |
| 761 } | 802 } |
| 762 return file_thread_.get(); | 803 return file_thread_.get(); |
| 763 } | 804 } |
| 764 | 805 |
| 765 void CronetURLRequestContextAdapter::OnRTTObservation( | 806 void CronetURLRequestContextAdapter::OnRTTObservation( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 793 jboolean jhttp2_enabled, | 834 jboolean jhttp2_enabled, |
| 794 jboolean jsdch_enabled, | 835 jboolean jsdch_enabled, |
| 795 const JavaParamRef<jstring>& jdata_reduction_proxy_key, | 836 const JavaParamRef<jstring>& jdata_reduction_proxy_key, |
| 796 const JavaParamRef<jstring>& jdata_reduction_proxy_primary_proxy, | 837 const JavaParamRef<jstring>& jdata_reduction_proxy_primary_proxy, |
| 797 const JavaParamRef<jstring>& jdata_reduction_proxy_fallback_proxy, | 838 const JavaParamRef<jstring>& jdata_reduction_proxy_fallback_proxy, |
| 798 const JavaParamRef<jstring>& jdata_reduction_proxy_secure_proxy_check_url, | 839 const JavaParamRef<jstring>& jdata_reduction_proxy_secure_proxy_check_url, |
| 799 jboolean jdisable_cache, | 840 jboolean jdisable_cache, |
| 800 jint jhttp_cache_mode, | 841 jint jhttp_cache_mode, |
| 801 jlong jhttp_cache_max_size, | 842 jlong jhttp_cache_max_size, |
| 802 const JavaParamRef<jstring>& jexperimental_quic_connection_options, | 843 const JavaParamRef<jstring>& jexperimental_quic_connection_options, |
| 803 jlong jmock_cert_verifier) { | 844 jlong jmock_cert_verifier, |
| 845 const JavaParamRef<jstring>& jcert_verifier_data) { |
| 804 return reinterpret_cast<jlong>(new URLRequestContextConfig( | 846 return reinterpret_cast<jlong>(new URLRequestContextConfig( |
| 805 jquic_enabled, | 847 jquic_enabled, |
| 806 ConvertNullableJavaStringToUTF8(env, jquic_default_user_agent_id), | 848 ConvertNullableJavaStringToUTF8(env, jquic_default_user_agent_id), |
| 807 jhttp2_enabled, jsdch_enabled, | 849 jhttp2_enabled, jsdch_enabled, |
| 808 static_cast<URLRequestContextConfig::HttpCacheType>(jhttp_cache_mode), | 850 static_cast<URLRequestContextConfig::HttpCacheType>(jhttp_cache_mode), |
| 809 jhttp_cache_max_size, jdisable_cache, | 851 jhttp_cache_max_size, jdisable_cache, |
| 810 ConvertNullableJavaStringToUTF8(env, jstorage_path), | 852 ConvertNullableJavaStringToUTF8(env, jstorage_path), |
| 811 ConvertNullableJavaStringToUTF8(env, juser_agent), | 853 ConvertNullableJavaStringToUTF8(env, juser_agent), |
| 812 ConvertNullableJavaStringToUTF8(env, | 854 ConvertNullableJavaStringToUTF8(env, |
| 813 jexperimental_quic_connection_options), | 855 jexperimental_quic_connection_options), |
| 814 ConvertNullableJavaStringToUTF8(env, jdata_reduction_proxy_key), | 856 ConvertNullableJavaStringToUTF8(env, jdata_reduction_proxy_key), |
| 815 ConvertNullableJavaStringToUTF8(env, jdata_reduction_proxy_primary_proxy), | 857 ConvertNullableJavaStringToUTF8(env, jdata_reduction_proxy_primary_proxy), |
| 816 ConvertNullableJavaStringToUTF8(env, | 858 ConvertNullableJavaStringToUTF8(env, |
| 817 jdata_reduction_proxy_fallback_proxy), | 859 jdata_reduction_proxy_fallback_proxy), |
| 818 ConvertNullableJavaStringToUTF8( | 860 ConvertNullableJavaStringToUTF8( |
| 819 env, jdata_reduction_proxy_secure_proxy_check_url), | 861 env, jdata_reduction_proxy_secure_proxy_check_url), |
| 820 base::WrapUnique( | 862 base::WrapUnique( |
| 821 reinterpret_cast<net::CertVerifier*>(jmock_cert_verifier)))); | 863 reinterpret_cast<net::CertVerifier*>(jmock_cert_verifier)), |
| 864 ConvertNullableJavaStringToUTF8(env, jcert_verifier_data))); |
| 822 } | 865 } |
| 823 | 866 |
| 824 // Add a QUIC hint to a URLRequestContextConfig. | 867 // Add a QUIC hint to a URLRequestContextConfig. |
| 825 static void AddQuicHint(JNIEnv* env, | 868 static void AddQuicHint(JNIEnv* env, |
| 826 const JavaParamRef<jclass>& jcaller, | 869 const JavaParamRef<jclass>& jcaller, |
| 827 jlong jurl_request_context_config, | 870 jlong jurl_request_context_config, |
| 828 const JavaParamRef<jstring>& jhost, | 871 const JavaParamRef<jstring>& jhost, |
| 829 jint jport, | 872 jint jport, |
| 830 jint jalternate_port) { | 873 jint jalternate_port) { |
| 831 URLRequestContextConfig* config = | 874 URLRequestContextConfig* config = |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 904 JNIEnv* env, | 947 JNIEnv* env, |
| 905 const JavaParamRef<jclass>& jcaller) { | 948 const JavaParamRef<jclass>& jcaller) { |
| 906 base::StatisticsRecorder::Initialize(); | 949 base::StatisticsRecorder::Initialize(); |
| 907 std::vector<uint8_t> data; | 950 std::vector<uint8_t> data; |
| 908 if (!HistogramManager::GetInstance()->GetDeltas(&data)) | 951 if (!HistogramManager::GetInstance()->GetDeltas(&data)) |
| 909 return ScopedJavaLocalRef<jbyteArray>(); | 952 return ScopedJavaLocalRef<jbyteArray>(); |
| 910 return base::android::ToJavaByteArray(env, &data[0], data.size()); | 953 return base::android::ToJavaByteArray(env, &data[0], data.size()); |
| 911 } | 954 } |
| 912 | 955 |
| 913 } // namespace cronet | 956 } // namespace cronet |
| OLD | NEW |