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 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 |
551 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | 631 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
552 // returns 0 otherwise. | 632 // returns 0 otherwise. |
553 static jlong CreateRequestContextAdapter(JNIEnv* env, | 633 static jlong CreateRequestContextAdapter(JNIEnv* env, |
554 const JavaParamRef<jclass>& jcaller, | 634 const JavaParamRef<jclass>& jcaller, |
555 const JavaParamRef<jstring>& jconfig) { | 635 jlong jconfig) { |
556 std::string config_string = | |
557 base::android::ConvertJavaStringToUTF8(env, jconfig); | |
558 scoped_ptr<URLRequestContextConfig> context_config( | 636 scoped_ptr<URLRequestContextConfig> context_config( |
559 new URLRequestContextConfig()); | 637 reinterpret_cast<URLRequestContextConfig*>(jconfig)); |
560 if (!context_config->LoadFromJSON(config_string)) | |
561 return 0; | |
562 | 638 |
563 CronetURLRequestContextAdapter* context_adapter = | 639 CronetURLRequestContextAdapter* context_adapter = |
564 new CronetURLRequestContextAdapter(context_config.Pass()); | 640 new CronetURLRequestContextAdapter(context_config.Pass()); |
565 return reinterpret_cast<jlong>(context_adapter); | 641 return reinterpret_cast<jlong>(context_adapter); |
566 } | 642 } |
567 | 643 |
568 static jint SetMinLogLevel(JNIEnv* env, | 644 static jint SetMinLogLevel(JNIEnv* env, |
569 const JavaParamRef<jclass>& jcaller, | 645 const JavaParamRef<jclass>& jcaller, |
570 jint jlog_level) { | 646 jint jlog_level) { |
571 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 647 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
572 // MinLogLevel is global, shared by all URLRequestContexts. | 648 // MinLogLevel is global, shared by all URLRequestContexts. |
573 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 649 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
574 return old_log_level; | 650 return old_log_level; |
575 } | 651 } |
576 | 652 |
577 static ScopedJavaLocalRef<jbyteArray> GetHistogramDeltas( | 653 static ScopedJavaLocalRef<jbyteArray> GetHistogramDeltas( |
578 JNIEnv* env, | 654 JNIEnv* env, |
579 const JavaParamRef<jclass>& jcaller) { | 655 const JavaParamRef<jclass>& jcaller) { |
580 base::StatisticsRecorder::Initialize(); | 656 base::StatisticsRecorder::Initialize(); |
581 std::vector<uint8> data; | 657 std::vector<uint8> data; |
582 if (!HistogramManager::GetInstance()->GetDeltas(&data)) | 658 if (!HistogramManager::GetInstance()->GetDeltas(&data)) |
583 return ScopedJavaLocalRef<jbyteArray>(); | 659 return ScopedJavaLocalRef<jbyteArray>(); |
584 return base::android::ToJavaByteArray(env, &data[0], data.size()); | 660 return base::android::ToJavaByteArray(env, &data[0], data.size()); |
585 } | 661 } |
586 | 662 |
587 } // namespace cronet | 663 } // namespace cronet |
OLD | NEW |