Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: components/cronet/android/cronet_url_request_context_adapter.cc

Issue 1429863008: [Cronet] Remove JSON serialization of CronetEngine.Builder (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add PKP Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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.
592 static void AddPkp(JNIEnv* env,
593 const JavaParamRef<jclass>& jcaller,
594 jlong jurl_request_context_config,
595 const JavaParamRef<jstring>& jhost,
596 const JavaParamRef<jobjectArray>& jhashes,
597 jboolean jinclude_subdomains,
598 jlong jexpiration_time) {
xunjieli 2015/12/04 19:19:13 Maybe add a comment here documenting what |jexpira
pauljensen 2015/12/07 13:53:34 Done.
599 URLRequestContextConfig* config =
600 reinterpret_cast<URLRequestContextConfig*>(jurl_request_context_config);
601 scoped_ptr<URLRequestContextConfig::Pkp> pkp(new URLRequestContextConfig::Pkp(
602 base::android::ConvertJavaStringToUTF8(env, jhost), jinclude_subdomains,
603 base::Time::UnixEpoch() +
604 base::TimeDelta::FromMilliseconds(jexpiration_time)));
605 size_t hash_count = env->GetArrayLength(jhashes);
606 for (size_t i = 0; i < hash_count; ++i) {
607 ScopedJavaLocalRef<jbyteArray> bytes_array(
608 env, static_cast<jbyteArray>(env->GetObjectArrayElement(jhashes, i)));
609 if (env->GetArrayLength(bytes_array.obj()) != sizeof(net::SHA256HashValue))
xunjieli 2015/12/04 19:19:13 Does this actually work? The size of the struct in
pauljensen 2015/12/07 13:53:34 Yes, this works. In fact SHA256HashValue exists t
610 continue;
611 jbyte* bytes = env->GetByteArrayElements(bytes_array.obj(), nullptr);
612 net::HashValue hash(*reinterpret_cast<net::SHA256HashValue*>(bytes));
xunjieli 2015/12/04 19:19:13 SHA256HashValue is a struct. Does the reinterpret_
pauljensen 2015/12/07 13:53:34 Yes, this works. I really want to avoid memcpy as
613 pkp->pin_hashes.push_back(hash);
614 env->ReleaseByteArrayElements(bytes_array.obj(), bytes, JNI_ABORT);
615 }
616 config->pkp_list.push_back(std::move(pkp));
617 }
618
551 // Creates RequestContextAdater if config is valid URLRequestContextConfig, 619 // Creates RequestContextAdater if config is valid URLRequestContextConfig,
552 // returns 0 otherwise. 620 // returns 0 otherwise.
553 static jlong CreateRequestContextAdapter(JNIEnv* env, 621 static jlong CreateRequestContextAdapter(JNIEnv* env,
554 const JavaParamRef<jclass>& jcaller, 622 const JavaParamRef<jclass>& jcaller,
555 const JavaParamRef<jstring>& jconfig) { 623 jlong jconfig) {
556 std::string config_string =
557 base::android::ConvertJavaStringToUTF8(env, jconfig);
558 scoped_ptr<URLRequestContextConfig> context_config( 624 scoped_ptr<URLRequestContextConfig> context_config(
559 new URLRequestContextConfig()); 625 reinterpret_cast<URLRequestContextConfig*>(jconfig));
560 if (!context_config->LoadFromJSON(config_string))
561 return 0;
562 626
563 CronetURLRequestContextAdapter* context_adapter = 627 CronetURLRequestContextAdapter* context_adapter =
564 new CronetURLRequestContextAdapter(context_config.Pass()); 628 new CronetURLRequestContextAdapter(context_config.Pass());
565 return reinterpret_cast<jlong>(context_adapter); 629 return reinterpret_cast<jlong>(context_adapter);
566 } 630 }
567 631
568 static jint SetMinLogLevel(JNIEnv* env, 632 static jint SetMinLogLevel(JNIEnv* env,
569 const JavaParamRef<jclass>& jcaller, 633 const JavaParamRef<jclass>& jcaller,
570 jint jlog_level) { 634 jint jlog_level) {
571 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); 635 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel());
572 // MinLogLevel is global, shared by all URLRequestContexts. 636 // MinLogLevel is global, shared by all URLRequestContexts.
573 logging::SetMinLogLevel(static_cast<int>(jlog_level)); 637 logging::SetMinLogLevel(static_cast<int>(jlog_level));
574 return old_log_level; 638 return old_log_level;
575 } 639 }
576 640
577 static ScopedJavaLocalRef<jbyteArray> GetHistogramDeltas( 641 static ScopedJavaLocalRef<jbyteArray> GetHistogramDeltas(
578 JNIEnv* env, 642 JNIEnv* env,
579 const JavaParamRef<jclass>& jcaller) { 643 const JavaParamRef<jclass>& jcaller) {
580 base::StatisticsRecorder::Initialize(); 644 base::StatisticsRecorder::Initialize();
581 std::vector<uint8> data; 645 std::vector<uint8> data;
582 if (!HistogramManager::GetInstance()->GetDeltas(&data)) 646 if (!HistogramManager::GetInstance()->GetDeltas(&data))
583 return ScopedJavaLocalRef<jbyteArray>(); 647 return ScopedJavaLocalRef<jbyteArray>();
584 return base::android::ToJavaByteArray(env, &data[0], data.size()); 648 return base::android::ToJavaByteArray(env, &data[0], data.size());
585 } 649 }
586 650
587 } // namespace cronet 651 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698