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" | |
36 #include "net/http/http_auth_handler_factory.h" | 35 #include "net/http/http_auth_handler_factory.h" |
37 #include "net/http/http_server_properties_manager.h" | 36 #include "net/http/http_server_properties_manager.h" |
38 #include "net/log/write_to_file_net_log_observer.h" | 37 #include "net/log/write_to_file_net_log_observer.h" |
39 #include "net/proxy/proxy_config_service_android.h" | 38 #include "net/proxy/proxy_config_service_android.h" |
40 #include "net/proxy/proxy_service.h" | 39 #include "net/proxy/proxy_service.h" |
41 #include "net/sdch/sdch_owner.h" | 40 #include "net/sdch/sdch_owner.h" |
42 #include "net/url_request/url_request_context.h" | 41 #include "net/url_request/url_request_context.h" |
43 #include "net/url_request/url_request_context_builder.h" | 42 #include "net/url_request/url_request_context_builder.h" |
44 #include "net/url_request/url_request_interceptor.h" | 43 #include "net/url_request/url_request_interceptor.h" |
45 | 44 |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 net::AlternateProtocol::QUIC, "", | 377 net::AlternateProtocol::QUIC, "", |
379 static_cast<uint16>(quic_hint.alternate_port)); | 378 static_cast<uint16>(quic_hint.alternate_port)); |
380 context_->http_server_properties()->SetAlternativeService( | 379 context_->http_server_properties()->SetAlternativeService( |
381 quic_hint_host_port_pair, alternative_service, 1.0f, | 380 quic_hint_host_port_pair, alternative_service, 1.0f, |
382 base::Time::Max()); | 381 base::Time::Max()); |
383 } | 382 } |
384 } | 383 } |
385 | 384 |
386 // Iterate through PKP configuration for every host. | 385 // Iterate through PKP configuration for every host. |
387 for (const auto& pkp : config->pkp_list) { | 386 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 |
388 // Add the host pinning. | 400 // Add the host pinning. |
389 context_->transport_security_state()->AddHPKP( | 401 context_->transport_security_state()->AddHPKP( |
390 pkp->host, pkp->expiration_date, pkp->include_subdomains, | 402 pkp->host, pkp->expiration_date, pkp->include_subdomains, |
391 pkp->pin_hashes, GURL::EmptyGURL()); | 403 hash_value_vector, GURL::EmptyGURL()); |
392 } | 404 } |
393 | 405 |
394 JNIEnv* env = base::android::AttachCurrentThread(); | 406 JNIEnv* env = base::android::AttachCurrentThread(); |
395 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); | 407 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); |
396 Java_CronetUrlRequestContext_initNetworkThread( | 408 Java_CronetUrlRequestContext_initNetworkThread( |
397 env, jcronet_url_request_context.obj()); | 409 env, jcronet_url_request_context.obj()); |
398 | 410 |
399 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 411 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
400 if (data_reduction_proxy_) | 412 if (data_reduction_proxy_) |
401 data_reduction_proxy_->Init(true, GetURLRequestContext()); | 413 data_reduction_proxy_->Init(true, GetURLRequestContext()); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 void CronetURLRequestContextAdapter::OnThroughputObservation( | 541 void CronetURLRequestContextAdapter::OnThroughputObservation( |
530 int32_t throughput_kbps, | 542 int32_t throughput_kbps, |
531 const base::TimeTicks& timestamp, | 543 const base::TimeTicks& timestamp, |
532 net::NetworkQualityEstimator::ObservationSource source) { | 544 net::NetworkQualityEstimator::ObservationSource source) { |
533 Java_CronetUrlRequestContext_onThroughputObservation( | 545 Java_CronetUrlRequestContext_onThroughputObservation( |
534 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), | 546 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), |
535 throughput_kbps, | 547 throughput_kbps, |
536 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), source); | 548 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), source); |
537 } | 549 } |
538 | 550 |
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 pin to URLRequestContextConfig. | |
592 // |jhost| is the host to apply the pin to. | |
593 // |jhashes| is an array of jbyte[32] representing SHA256 key hashes. | |
594 // |jinclude_subdomains| indicates if pin should be applied to subdomains. | |
595 // |jexpiration_time| is the time that the pin expires, in milliseconds since | |
596 // Jan. 1, 1970, midnight GMT. | |
597 static void AddPkp(JNIEnv* env, | |
598 const JavaParamRef<jclass>& jcaller, | |
599 jlong jurl_request_context_config, | |
600 const JavaParamRef<jstring>& jhost, | |
601 const JavaParamRef<jobjectArray>& jhashes, | |
602 jboolean jinclude_subdomains, | |
603 jlong jexpiration_time) { | |
604 URLRequestContextConfig* config = | |
605 reinterpret_cast<URLRequestContextConfig*>(jurl_request_context_config); | |
606 scoped_ptr<URLRequestContextConfig::Pkp> pkp(new URLRequestContextConfig::Pkp( | |
607 base::android::ConvertJavaStringToUTF8(env, jhost), jinclude_subdomains, | |
608 base::Time::UnixEpoch() + | |
609 base::TimeDelta::FromMilliseconds(jexpiration_time))); | |
610 size_t hash_count = env->GetArrayLength(jhashes); | |
611 for (size_t i = 0; i < hash_count; ++i) { | |
612 ScopedJavaLocalRef<jbyteArray> bytes_array( | |
613 env, static_cast<jbyteArray>(env->GetObjectArrayElement(jhashes, i))); | |
614 static_assert(std::is_pod<net::SHA256HashValue>::value, | |
615 "net::SHA256HashValue is not POD"); | |
616 static_assert(sizeof(net::SHA256HashValue) * CHAR_BIT == 256, | |
617 "net::SHA256HashValue contains overhead"); | |
618 if (env->GetArrayLength(bytes_array.obj()) != | |
619 sizeof(net::SHA256HashValue)) { | |
620 LOG(ERROR) << "Unable to add public key hash value."; | |
621 continue; | |
622 } | |
623 jbyte* bytes = env->GetByteArrayElements(bytes_array.obj(), nullptr); | |
624 net::HashValue hash(*reinterpret_cast<net::SHA256HashValue*>(bytes)); | |
625 pkp->pin_hashes.push_back(hash); | |
626 env->ReleaseByteArrayElements(bytes_array.obj(), bytes, JNI_ABORT); | |
627 } | |
628 config->pkp_list.push_back(std::move(pkp)); | |
629 } | |
630 | |
631 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | 551 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
632 // returns 0 otherwise. | 552 // returns 0 otherwise. |
633 static jlong CreateRequestContextAdapter(JNIEnv* env, | 553 static jlong CreateRequestContextAdapter(JNIEnv* env, |
634 const JavaParamRef<jclass>& jcaller, | 554 const JavaParamRef<jclass>& jcaller, |
635 jlong jconfig) { | 555 const JavaParamRef<jstring>& jconfig) { |
| 556 std::string config_string = |
| 557 base::android::ConvertJavaStringToUTF8(env, jconfig); |
636 scoped_ptr<URLRequestContextConfig> context_config( | 558 scoped_ptr<URLRequestContextConfig> context_config( |
637 reinterpret_cast<URLRequestContextConfig*>(jconfig)); | 559 new URLRequestContextConfig()); |
| 560 if (!context_config->LoadFromJSON(config_string)) |
| 561 return 0; |
638 | 562 |
639 CronetURLRequestContextAdapter* context_adapter = | 563 CronetURLRequestContextAdapter* context_adapter = |
640 new CronetURLRequestContextAdapter(context_config.Pass()); | 564 new CronetURLRequestContextAdapter(context_config.Pass()); |
641 return reinterpret_cast<jlong>(context_adapter); | 565 return reinterpret_cast<jlong>(context_adapter); |
642 } | 566 } |
643 | 567 |
644 static jint SetMinLogLevel(JNIEnv* env, | 568 static jint SetMinLogLevel(JNIEnv* env, |
645 const JavaParamRef<jclass>& jcaller, | 569 const JavaParamRef<jclass>& jcaller, |
646 jint jlog_level) { | 570 jint jlog_level) { |
647 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 571 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
648 // MinLogLevel is global, shared by all URLRequestContexts. | 572 // MinLogLevel is global, shared by all URLRequestContexts. |
649 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 573 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
650 return old_log_level; | 574 return old_log_level; |
651 } | 575 } |
652 | 576 |
653 static ScopedJavaLocalRef<jbyteArray> GetHistogramDeltas( | 577 static ScopedJavaLocalRef<jbyteArray> GetHistogramDeltas( |
654 JNIEnv* env, | 578 JNIEnv* env, |
655 const JavaParamRef<jclass>& jcaller) { | 579 const JavaParamRef<jclass>& jcaller) { |
656 base::StatisticsRecorder::Initialize(); | 580 base::StatisticsRecorder::Initialize(); |
657 std::vector<uint8> data; | 581 std::vector<uint8> data; |
658 if (!HistogramManager::GetInstance()->GetDeltas(&data)) | 582 if (!HistogramManager::GetInstance()->GetDeltas(&data)) |
659 return ScopedJavaLocalRef<jbyteArray>(); | 583 return ScopedJavaLocalRef<jbyteArray>(); |
660 return base::android::ToJavaByteArray(env, &data[0], data.size()); | 584 return base::android::ToJavaByteArray(env, &data[0], data.size()); |
661 } | 585 } |
662 | 586 |
663 } // namespace cronet | 587 } // namespace cronet |
OLD | NEW |