Chromium Code Reviews| 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> | |
| 8 | |
| 7 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 14 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 15 #include "base/prefs/pref_filter.h" | 17 #include "base/prefs/pref_filter.h" |
| 16 #include "base/prefs/pref_registry_simple.h" | 18 #include "base/prefs/pref_registry_simple.h" |
| 17 #include "base/prefs/pref_service.h" | 19 #include "base/prefs/pref_service.h" |
| 18 #include "base/prefs/pref_service_factory.h" | 20 #include "base/prefs/pref_service_factory.h" |
| 19 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
| 20 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 21 #include "base/values.h" | 23 #include "base/values.h" |
| 22 #include "components/cronet/url_request_context_config.h" | 24 #include "components/cronet/url_request_context_config.h" |
| 23 #include "jni/CronetUrlRequestContext_jni.h" | 25 #include "jni/CronetUrlRequestContext_jni.h" |
| 26 #include "net/base/external_estimate_provider.h" | |
| 24 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
| 25 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 26 #include "net/base/network_delegate_impl.h" | 29 #include "net/base/network_delegate_impl.h" |
| 27 #include "net/http/http_auth_handler_factory.h" | 30 #include "net/http/http_auth_handler_factory.h" |
| 28 #include "net/http/http_server_properties_manager.h" | 31 #include "net/http/http_server_properties_manager.h" |
| 29 #include "net/log/write_to_file_net_log_observer.h" | 32 #include "net/log/write_to_file_net_log_observer.h" |
| 30 #include "net/proxy/proxy_config_service_android.h" | 33 #include "net/proxy/proxy_config_service_android.h" |
| 31 #include "net/proxy/proxy_service.h" | 34 #include "net/proxy/proxy_service.h" |
| 32 #include "net/sdch/sdch_owner.h" | 35 #include "net/sdch/sdch_owner.h" |
| 33 #include "net/url_request/url_request_context.h" | 36 #include "net/url_request/url_request_context.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 network_thread_->StartWithOptions(options); | 136 network_thread_->StartWithOptions(options); |
| 134 } | 137 } |
| 135 | 138 |
| 136 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { | 139 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
| 137 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 140 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 138 | 141 |
| 139 if (http_server_properties_manager_) | 142 if (http_server_properties_manager_) |
| 140 http_server_properties_manager_->ShutdownOnPrefThread(); | 143 http_server_properties_manager_->ShutdownOnPrefThread(); |
| 141 if (pref_service_) | 144 if (pref_service_) |
| 142 pref_service_->CommitPendingWrite(); | 145 pref_service_->CommitPendingWrite(); |
| 146 if (network_quality_estimator_) { | |
| 147 network_quality_estimator_->RemoveRTTObserver(this); | |
| 148 network_quality_estimator_->RemoveThroughputObserver(this); | |
| 149 } | |
| 143 StopNetLogOnNetworkThread(); | 150 StopNetLogOnNetworkThread(); |
| 144 } | 151 } |
| 145 | 152 |
| 146 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( | 153 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
| 147 JNIEnv* env, | 154 JNIEnv* env, |
| 148 jobject jcaller) { | 155 jobject jcaller) { |
| 149 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; | 156 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; |
| 150 jcaller_ref.Reset(env, jcaller); | 157 jcaller_ref.Reset(env, jcaller); |
| 151 proxy_config_service_ = net::ProxyService::CreateSystemProxyConfigService( | 158 proxy_config_service_ = net::ProxyService::CreateSystemProxyConfigService( |
| 152 GetNetworkTaskRunner(), nullptr /* Ignored on Android */); | 159 GetNetworkTaskRunner(), nullptr /* Ignored on Android */); |
| 153 net::ProxyConfigServiceAndroid* android_proxy_config_service = | 160 net::ProxyConfigServiceAndroid* android_proxy_config_service = |
| 154 static_cast<net::ProxyConfigServiceAndroid*>(proxy_config_service_.get()); | 161 static_cast<net::ProxyConfigServiceAndroid*>(proxy_config_service_.get()); |
| 155 // If a PAC URL is present, ignore it and use the address and port of | 162 // If a PAC URL is present, ignore it and use the address and port of |
| 156 // Android system's local HTTP proxy server. See: crbug.com/432539. | 163 // Android system's local HTTP proxy server. See: crbug.com/432539. |
| 157 // TODO(csharrison) Architect the wrapper better so we don't need to cast for | 164 // TODO(csharrison) Architect the wrapper better so we don't need to cast for |
| 158 // android ProxyConfigServices. | 165 // android ProxyConfigServices. |
| 159 android_proxy_config_service->set_exclude_pac_url(true); | 166 android_proxy_config_service->set_exclude_pac_url(true); |
| 160 GetNetworkTaskRunner()->PostTask( | 167 GetNetworkTaskRunner()->PostTask( |
| 161 FROM_HERE, | 168 FROM_HERE, |
| 162 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, | 169 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
| 163 base::Unretained(this), Passed(&context_config_), | 170 base::Unretained(this), Passed(&context_config_), |
| 164 jcaller_ref)); | 171 jcaller_ref)); |
| 165 } | 172 } |
| 166 | 173 |
| 174 void CronetURLRequestContextAdapter:: | |
| 175 EnableNetworkQualityEstimatorOnNetworkThread(bool use_local_host_requests, | |
| 176 bool use_smaller_responses) { | |
| 177 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 178 DCHECK(!network_quality_estimator_); | |
| 179 network_quality_estimator_.reset(new net::NetworkQualityEstimator( | |
| 180 scoped_ptr<net::ExternalEstimateProvider>(), | |
| 181 std::map<std::string, std::string>(), use_local_host_requests, | |
| 182 use_smaller_responses)); | |
| 183 context_->set_network_quality_estimator(network_quality_estimator_.get()); | |
| 184 } | |
| 185 | |
| 186 void CronetURLRequestContextAdapter::EnableNetworkQualityEstimator( | |
| 187 JNIEnv* env, | |
| 188 jobject jcaller, | |
| 189 jboolean use_local_host_requests, | |
| 190 jboolean use_smaller_responses) { | |
| 191 PostTaskToNetworkThread( | |
| 192 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: | |
| 193 EnableNetworkQualityEstimatorOnNetworkThread, | |
| 194 base::Unretained(this), use_local_host_requests, | |
| 195 use_smaller_responses)); | |
| 196 } | |
| 197 | |
| 198 void CronetURLRequestContextAdapter::ProvideRTTObservationsOnNetworkThread( | |
| 199 bool should) { | |
| 200 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 201 if (!network_quality_estimator_) | |
| 202 return; | |
| 203 if (should) | |
|
pauljensen
2015/10/06 19:10:29
nit: I think in net/ we generally use curly braces
bengr
2015/10/06 20:04:27
Done, though I see it both ways in net/. Style ind
| |
| 204 network_quality_estimator_->AddRTTObserver(this); | |
| 205 else | |
| 206 network_quality_estimator_->RemoveRTTObserver(this); | |
| 207 } | |
| 208 | |
| 209 void CronetURLRequestContextAdapter::ProvideRTTObservations(JNIEnv* env, | |
| 210 jobject jcaller, | |
| 211 bool should) { | |
| 212 PostTaskToNetworkThread(FROM_HERE, | |
| 213 base::Bind(&CronetURLRequestContextAdapter:: | |
| 214 ProvideRTTObservationsOnNetworkThread, | |
| 215 base::Unretained(this), should)); | |
| 216 } | |
| 217 | |
| 218 void CronetURLRequestContextAdapter:: | |
| 219 ProvideThroughputObservationsOnNetworkThread(bool should) { | |
| 220 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 221 if (!network_quality_estimator_) | |
| 222 return; | |
| 223 if (should) | |
| 224 network_quality_estimator_->AddThroughputObserver(this); | |
| 225 else | |
| 226 network_quality_estimator_->RemoveThroughputObserver(this); | |
| 227 } | |
| 228 | |
| 229 void CronetURLRequestContextAdapter::ProvideThroughputObservations( | |
| 230 JNIEnv* env, | |
| 231 jobject jcaller, | |
| 232 bool should) { | |
| 233 PostTaskToNetworkThread( | |
| 234 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: | |
| 235 ProvideThroughputObservationsOnNetworkThread, | |
| 236 base::Unretained(this), should)); | |
| 237 } | |
| 238 | |
| 167 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 239 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| 168 scoped_ptr<URLRequestContextConfig> config, | 240 scoped_ptr<URLRequestContextConfig> config, |
| 169 const base::android::ScopedJavaGlobalRef<jobject>& | 241 const base::android::ScopedJavaGlobalRef<jobject>& |
| 170 jcronet_url_request_context) { | 242 jcronet_url_request_context) { |
| 171 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 243 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 172 DCHECK(!is_context_initialized_); | 244 DCHECK(!is_context_initialized_); |
| 173 DCHECK(proxy_config_service_); | 245 DCHECK(proxy_config_service_); |
| 174 // TODO(mmenke): Add method to have the builder enable SPDY. | 246 // TODO(mmenke): Add method to have the builder enable SPDY. |
| 175 net::URLRequestContextBuilder context_builder; | 247 net::URLRequestContextBuilder context_builder; |
| 176 | 248 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 net::AlternativeService alternative_service( | 356 net::AlternativeService alternative_service( |
| 285 net::AlternateProtocol::QUIC, "", | 357 net::AlternateProtocol::QUIC, "", |
| 286 static_cast<uint16>(quic_hint.alternate_port)); | 358 static_cast<uint16>(quic_hint.alternate_port)); |
| 287 context_->http_server_properties()->SetAlternativeService( | 359 context_->http_server_properties()->SetAlternativeService( |
| 288 quic_hint_host_port_pair, alternative_service, 1.0f, | 360 quic_hint_host_port_pair, alternative_service, 1.0f, |
| 289 base::Time::Max()); | 361 base::Time::Max()); |
| 290 } | 362 } |
| 291 } | 363 } |
| 292 | 364 |
| 293 JNIEnv* env = base::android::AttachCurrentThread(); | 365 JNIEnv* env = base::android::AttachCurrentThread(); |
| 366 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); | |
| 294 Java_CronetUrlRequestContext_initNetworkThread( | 367 Java_CronetUrlRequestContext_initNetworkThread( |
| 295 env, jcronet_url_request_context.obj()); | 368 env, jcronet_url_request_context.obj()); |
| 296 | 369 |
| 297 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 370 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
| 298 if (data_reduction_proxy_) | 371 if (data_reduction_proxy_) |
| 299 data_reduction_proxy_->Init(true, GetURLRequestContext()); | 372 data_reduction_proxy_->Init(true, GetURLRequestContext()); |
| 300 #endif | 373 #endif |
| 301 is_context_initialized_ = true; | 374 is_context_initialized_ = true; |
| 302 while (!tasks_waiting_for_context_.empty()) { | 375 while (!tasks_waiting_for_context_.empty()) { |
| 303 tasks_waiting_for_context_.front().Run(); | 376 tasks_waiting_for_context_.front().Run(); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 402 | 475 |
| 403 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { | 476 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { |
| 404 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 477 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 405 if (!file_thread_) { | 478 if (!file_thread_) { |
| 406 file_thread_.reset(new base::Thread("Network File Thread")); | 479 file_thread_.reset(new base::Thread("Network File Thread")); |
| 407 file_thread_->Start(); | 480 file_thread_->Start(); |
| 408 } | 481 } |
| 409 return file_thread_.get(); | 482 return file_thread_.get(); |
| 410 } | 483 } |
| 411 | 484 |
| 485 void CronetURLRequestContextAdapter::OnRTTObservation( | |
| 486 int32_t rtt_ms, | |
| 487 const base::TimeTicks& timestamp, | |
| 488 net::NetworkQualityEstimator::ObservationSource source) { | |
| 489 Java_CronetUrlRequestContext_onRttObservation( | |
| 490 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), | |
| 491 rtt_ms, (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), | |
| 492 source); | |
| 493 } | |
| 494 | |
| 495 void CronetURLRequestContextAdapter::OnThroughputObservation( | |
| 496 int32_t throughput_kbps, | |
| 497 const base::TimeTicks& timestamp, | |
| 498 net::NetworkQualityEstimator::ObservationSource source) { | |
| 499 Java_CronetUrlRequestContext_onThroughputObservation( | |
| 500 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), | |
| 501 throughput_kbps, | |
| 502 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), source); | |
| 503 } | |
| 504 | |
| 412 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | 505 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
| 413 // returns 0 otherwise. | 506 // returns 0 otherwise. |
| 414 static jlong CreateRequestContextAdapter(JNIEnv* env, | 507 static jlong CreateRequestContextAdapter(JNIEnv* env, |
| 415 const JavaParamRef<jclass>& jcaller, | 508 const JavaParamRef<jclass>& jcaller, |
| 416 const JavaParamRef<jstring>& jconfig) { | 509 const JavaParamRef<jstring>& jconfig) { |
| 417 std::string config_string = | 510 std::string config_string = |
| 418 base::android::ConvertJavaStringToUTF8(env, jconfig); | 511 base::android::ConvertJavaStringToUTF8(env, jconfig); |
| 419 scoped_ptr<URLRequestContextConfig> context_config( | 512 scoped_ptr<URLRequestContextConfig> context_config( |
| 420 new URLRequestContextConfig()); | 513 new URLRequestContextConfig()); |
| 421 if (!context_config->LoadFromJSON(config_string)) | 514 if (!context_config->LoadFromJSON(config_string)) |
| 422 return 0; | 515 return 0; |
| 423 | 516 |
| 424 CronetURLRequestContextAdapter* context_adapter = | 517 CronetURLRequestContextAdapter* context_adapter = |
| 425 new CronetURLRequestContextAdapter(context_config.Pass()); | 518 new CronetURLRequestContextAdapter(context_config.Pass()); |
| 426 return reinterpret_cast<jlong>(context_adapter); | 519 return reinterpret_cast<jlong>(context_adapter); |
| 427 } | 520 } |
| 428 | 521 |
| 429 static jint SetMinLogLevel(JNIEnv* env, | 522 static jint SetMinLogLevel(JNIEnv* env, |
| 430 const JavaParamRef<jclass>& jcaller, | 523 const JavaParamRef<jclass>& jcaller, |
| 431 jint jlog_level) { | 524 jint jlog_level) { |
| 432 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 525 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
| 433 // MinLogLevel is global, shared by all URLRequestContexts. | 526 // MinLogLevel is global, shared by all URLRequestContexts. |
| 434 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 527 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
| 435 return old_log_level; | 528 return old_log_level; |
| 436 } | 529 } |
| 437 | 530 |
| 438 } // namespace cronet | 531 } // namespace cronet |
| OLD | NEW |