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 <map> | 7 #include <map> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
26 #include "base/values.h" | 26 #include "base/values.h" |
27 #include "components/cronet/histogram_manager.h" | 27 #include "components/cronet/histogram_manager.h" |
28 #include "components/cronet/url_request_context_config.h" | 28 #include "components/cronet/url_request_context_config.h" |
29 #include "jni/CronetUrlRequestContext_jni.h" | 29 #include "jni/CronetUrlRequestContext_jni.h" |
30 #include "net/base/external_estimate_provider.h" | 30 #include "net/base/external_estimate_provider.h" |
31 #include "net/base/load_flags.h" | 31 #include "net/base/load_flags.h" |
32 #include "net/base/net_errors.h" | 32 #include "net/base/net_errors.h" |
33 #include "net/base/net_util.h" | 33 #include "net/base/net_util.h" |
34 #include "net/base/network_delegate_impl.h" | 34 #include "net/base/network_delegate_impl.h" |
35 #include "net/cert/cert_verifier.h" | |
35 #include "net/http/http_auth_handler_factory.h" | 36 #include "net/http/http_auth_handler_factory.h" |
36 #include "net/http/http_server_properties_manager.h" | 37 #include "net/http/http_server_properties_manager.h" |
37 #include "net/log/write_to_file_net_log_observer.h" | 38 #include "net/log/write_to_file_net_log_observer.h" |
38 #include "net/proxy/proxy_config_service_android.h" | 39 #include "net/proxy/proxy_config_service_android.h" |
39 #include "net/proxy/proxy_service.h" | 40 #include "net/proxy/proxy_service.h" |
40 #include "net/sdch/sdch_owner.h" | 41 #include "net/sdch/sdch_owner.h" |
41 #include "net/url_request/url_request_context.h" | 42 #include "net/url_request/url_request_context.h" |
42 #include "net/url_request/url_request_context_builder.h" | 43 #include "net/url_request/url_request_context_builder.h" |
43 #include "net/url_request/url_request_interceptor.h" | 44 #include "net/url_request/url_request_interceptor.h" |
44 | 45 |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
377 net::AlternateProtocol::QUIC, "", | 378 net::AlternateProtocol::QUIC, "", |
378 static_cast<uint16>(quic_hint.alternate_port)); | 379 static_cast<uint16>(quic_hint.alternate_port)); |
379 context_->http_server_properties()->SetAlternativeService( | 380 context_->http_server_properties()->SetAlternativeService( |
380 quic_hint_host_port_pair, alternative_service, 1.0f, | 381 quic_hint_host_port_pair, alternative_service, 1.0f, |
381 base::Time::Max()); | 382 base::Time::Max()); |
382 } | 383 } |
383 } | 384 } |
384 | 385 |
385 // Iterate through PKP configuration for every host. | 386 // Iterate through PKP configuration for every host. |
386 for (const auto& pkp : config->pkp_list) { | 387 for (const auto& pkp : config->pkp_list) { |
387 // Convert the vector of hash strings from the config to | |
388 // a vector of HashValue objects. | |
389 net::HashValueVector hash_value_vector; | |
390 for (const auto& hash : pkp->pin_hashes) { | |
391 net::HashValue hash_value; | |
392 bool good_hash = hash_value.FromString(*hash); | |
393 if (good_hash) { | |
394 hash_value_vector.push_back(hash_value); | |
395 } else { | |
396 LOG(WARNING) << "Unable to add hash value " << *hash; | |
397 } | |
398 } | |
399 | |
400 // Add the host pinning. | 388 // Add the host pinning. |
401 context_->transport_security_state()->AddHPKP( | 389 context_->transport_security_state()->AddHPKP( |
402 pkp->host, pkp->expiration_date, pkp->include_subdomains, | 390 pkp->host, pkp->expiration_date, pkp->include_subdomains, |
403 hash_value_vector, GURL::EmptyGURL()); | 391 pkp->pin_hashes, GURL::EmptyGURL()); |
404 } | 392 } |
405 | 393 |
406 JNIEnv* env = base::android::AttachCurrentThread(); | 394 JNIEnv* env = base::android::AttachCurrentThread(); |
407 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); | 395 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); |
408 Java_CronetUrlRequestContext_initNetworkThread( | 396 Java_CronetUrlRequestContext_initNetworkThread( |
409 env, jcronet_url_request_context.obj()); | 397 env, jcronet_url_request_context.obj()); |
410 | 398 |
411 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 399 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
412 if (data_reduction_proxy_) | 400 if (data_reduction_proxy_) |
413 data_reduction_proxy_->Init(true, GetURLRequestContext()); | 401 data_reduction_proxy_->Init(true, GetURLRequestContext()); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
541 void CronetURLRequestContextAdapter::OnThroughputObservation( | 529 void CronetURLRequestContextAdapter::OnThroughputObservation( |
542 int32_t throughput_kbps, | 530 int32_t throughput_kbps, |
543 const base::TimeTicks& timestamp, | 531 const base::TimeTicks& timestamp, |
544 net::NetworkQualityEstimator::ObservationSource source) { | 532 net::NetworkQualityEstimator::ObservationSource source) { |
545 Java_CronetUrlRequestContext_onThroughputObservation( | 533 Java_CronetUrlRequestContext_onThroughputObservation( |
546 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), | 534 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), |
547 throughput_kbps, | 535 throughput_kbps, |
548 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), source); | 536 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), source); |
549 } | 537 } |
550 | 538 |
539 // Create a URLRequestContextConfig from the given parameters. | |
540 static jlong CreateRequestContextConfig( | |
541 JNIEnv* env, | |
542 const JavaParamRef<jclass>& jcaller, | |
543 const JavaParamRef<jstring>& juser_agent, | |
544 const JavaParamRef<jstring>& jstorage_path, | |
545 jboolean jquic_enabled, | |
546 jboolean jhttp2_enabled, | |
547 jboolean jsdch_enabled, | |
548 const JavaParamRef<jstring>& jdata_reduction_proxy_key, | |
549 const JavaParamRef<jstring>& jdata_reduction_proxy_primary_proxy, | |
550 const JavaParamRef<jstring>& jdata_reduction_proxy_fallback_proxy, | |
551 const JavaParamRef<jstring>& jdata_reduction_proxy_secure_proxy_check_url, | |
552 jboolean jdisable_cache, | |
553 jint jhttp_cache_mode, | |
554 jlong jhttp_cache_max_size, | |
555 const JavaParamRef<jstring>& jexperimental_quic_connection_options, | |
556 jlong jmock_cert_verifier) { | |
557 return reinterpret_cast<jlong>(new URLRequestContextConfig( | |
558 jquic_enabled, jhttp2_enabled, jsdch_enabled, | |
559 static_cast<URLRequestContextConfig::HttpCacheType>(jhttp_cache_mode), | |
560 jhttp_cache_max_size, jdisable_cache, | |
561 base::android::ConvertJavaStringToUTF8(env, jstorage_path), | |
562 base::android::ConvertJavaStringToUTF8(env, juser_agent), | |
563 base::android::ConvertJavaStringToUTF8( | |
564 env, jexperimental_quic_connection_options), | |
565 base::android::ConvertJavaStringToUTF8(env, jdata_reduction_proxy_key), | |
566 base::android::ConvertJavaStringToUTF8( | |
567 env, jdata_reduction_proxy_primary_proxy), | |
568 base::android::ConvertJavaStringToUTF8( | |
569 env, jdata_reduction_proxy_fallback_proxy), | |
570 base::android::ConvertJavaStringToUTF8( | |
571 env, jdata_reduction_proxy_secure_proxy_check_url), | |
572 make_scoped_ptr( | |
573 reinterpret_cast<net::CertVerifier*>(jmock_cert_verifier)))); | |
574 } | |
575 | |
576 // Add a QUIC hint to a URLRequestContextConfig. | |
577 static void AddQuicHint(JNIEnv* env, | |
578 const JavaParamRef<jclass>& jcaller, | |
579 jlong jurl_request_context_config, | |
580 const JavaParamRef<jstring>& jhost, | |
581 jint jport, | |
582 jint jalternate_port) { | |
583 URLRequestContextConfig* config = | |
584 reinterpret_cast<URLRequestContextConfig*>(jurl_request_context_config); | |
585 config->quic_hints.push_back( | |
586 make_scoped_ptr(new URLRequestContextConfig::QuicHint( | |
587 base::android::ConvertJavaStringToUTF8(env, jhost), jport, | |
588 jalternate_port))); | |
589 } | |
590 | |
591 // Add a public key ping to URLRequestContextConfig. | |
xunjieli
2015/12/07 15:28:02
nit: s/ping/pin
pauljensen
2015/12/07 16:25:14
Done.
| |
592 static void AddPkp(JNIEnv* env, | |
593 const JavaParamRef<jclass>& jcaller, | |
594 jlong jurl_request_context_config, | |
595 const JavaParamRef<jstring>& jhost, | |
596 // Array of byte[32] representing SHA256 key hashes. | |
xunjieli
2015/12/07 15:28:02
nit: I haven't seen this style of comments. Maybe
pauljensen
2015/12/07 16:25:14
Done.
| |
597 const JavaParamRef<jobjectArray>& jhashes, | |
598 jboolean jinclude_subdomains, | |
599 // Time pin expires, in milliseconds since | |
600 // Jan. 1, 1970, midnight GMT. | |
601 jlong jexpiration_time) { | |
602 URLRequestContextConfig* config = | |
603 reinterpret_cast<URLRequestContextConfig*>(jurl_request_context_config); | |
604 scoped_ptr<URLRequestContextConfig::Pkp> pkp(new URLRequestContextConfig::Pkp( | |
605 base::android::ConvertJavaStringToUTF8(env, jhost), jinclude_subdomains, | |
606 base::Time::UnixEpoch() + | |
607 base::TimeDelta::FromMilliseconds(jexpiration_time))); | |
608 size_t hash_count = env->GetArrayLength(jhashes); | |
609 for (size_t i = 0; i < hash_count; ++i) { | |
610 ScopedJavaLocalRef<jbyteArray> bytes_array( | |
611 env, static_cast<jbyteArray>(env->GetObjectArrayElement(jhashes, i))); | |
612 static_assert(std::is_pod<net::SHA256HashValue>::value, | |
613 "net::SHA256HashValue is not POD"); | |
614 static_assert(sizeof(net::SHA256HashValue) * CHAR_BIT == 256, | |
615 "net::SHA256HashValue contains overhead"); | |
616 if (env->GetArrayLength(bytes_array.obj()) != sizeof(net::SHA256HashValue)) | |
xunjieli
2015/12/07 15:28:02
Andrei's patch as a LOG statement. Maybe consider
pauljensen
2015/12/07 16:25:14
Done, upgraded to LOG(ERROR) as this is a serious
| |
617 continue; | |
618 jbyte* bytes = env->GetByteArrayElements(bytes_array.obj(), nullptr); | |
619 net::HashValue hash(*reinterpret_cast<net::SHA256HashValue*>(bytes)); | |
620 pkp->pin_hashes.push_back(hash); | |
621 env->ReleaseByteArrayElements(bytes_array.obj(), bytes, JNI_ABORT); | |
622 } | |
623 config->pkp_list.push_back(std::move(pkp)); | |
624 } | |
625 | |
551 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | 626 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
552 // returns 0 otherwise. | 627 // returns 0 otherwise. |
553 static jlong CreateRequestContextAdapter(JNIEnv* env, | 628 static jlong CreateRequestContextAdapter(JNIEnv* env, |
554 const JavaParamRef<jclass>& jcaller, | 629 const JavaParamRef<jclass>& jcaller, |
555 const JavaParamRef<jstring>& jconfig) { | 630 jlong jconfig) { |
556 std::string config_string = | |
557 base::android::ConvertJavaStringToUTF8(env, jconfig); | |
558 scoped_ptr<URLRequestContextConfig> context_config( | 631 scoped_ptr<URLRequestContextConfig> context_config( |
559 new URLRequestContextConfig()); | 632 reinterpret_cast<URLRequestContextConfig*>(jconfig)); |
560 if (!context_config->LoadFromJSON(config_string)) | |
561 return 0; | |
562 | 633 |
563 CronetURLRequestContextAdapter* context_adapter = | 634 CronetURLRequestContextAdapter* context_adapter = |
564 new CronetURLRequestContextAdapter(context_config.Pass()); | 635 new CronetURLRequestContextAdapter(context_config.Pass()); |
565 return reinterpret_cast<jlong>(context_adapter); | 636 return reinterpret_cast<jlong>(context_adapter); |
566 } | 637 } |
567 | 638 |
568 static jint SetMinLogLevel(JNIEnv* env, | 639 static jint SetMinLogLevel(JNIEnv* env, |
569 const JavaParamRef<jclass>& jcaller, | 640 const JavaParamRef<jclass>& jcaller, |
570 jint jlog_level) { | 641 jint jlog_level) { |
571 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 642 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
572 // MinLogLevel is global, shared by all URLRequestContexts. | 643 // MinLogLevel is global, shared by all URLRequestContexts. |
573 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 644 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
574 return old_log_level; | 645 return old_log_level; |
575 } | 646 } |
576 | 647 |
577 static ScopedJavaLocalRef<jbyteArray> GetHistogramDeltas( | 648 static ScopedJavaLocalRef<jbyteArray> GetHistogramDeltas( |
578 JNIEnv* env, | 649 JNIEnv* env, |
579 const JavaParamRef<jclass>& jcaller) { | 650 const JavaParamRef<jclass>& jcaller) { |
580 base::StatisticsRecorder::Initialize(); | 651 base::StatisticsRecorder::Initialize(); |
581 std::vector<uint8> data; | 652 std::vector<uint8> data; |
582 if (!HistogramManager::GetInstance()->GetDeltas(&data)) | 653 if (!HistogramManager::GetInstance()->GetDeltas(&data)) |
583 return ScopedJavaLocalRef<jbyteArray>(); | 654 return ScopedJavaLocalRef<jbyteArray>(); |
584 return base::android::ToJavaByteArray(env, &data[0], data.size()); | 655 return base::android::ToJavaByteArray(env, &data[0], data.size()); |
585 } | 656 } |
586 | 657 |
587 } // namespace cronet | 658 } // namespace cronet |
OLD | NEW |