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 "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_file.h" | 11 #include "base/files/scoped_file.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/prefs/pref_filter.h" | 15 #include "base/prefs/pref_filter.h" |
| 16 #include "base/prefs/pref_registry_simple.h" | 16 #include "base/prefs/pref_registry_simple.h" |
| 17 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
| 18 #include "base/prefs/pref_service_factory.h" | 18 #include "base/prefs/pref_service_factory.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "components/cronet/url_request_context_config.h" | 22 #include "components/cronet/url_request_context_config.h" |
| 23 #include "jni/CronetUrlRequestContext_jni.h" | 23 #include "jni/CronetUrlRequestContext_jni.h" |
| 24 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
| 25 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| 26 #include "net/base/network_delegate_impl.h" | 26 #include "net/base/network_delegate_impl.h" |
| 27 #include "net/base/network_quality_estimator.h" | |
| 27 #include "net/http/http_auth_handler_factory.h" | 28 #include "net/http/http_auth_handler_factory.h" |
| 28 #include "net/http/http_server_properties_manager.h" | 29 #include "net/http/http_server_properties_manager.h" |
| 29 #include "net/log/write_to_file_net_log_observer.h" | 30 #include "net/log/write_to_file_net_log_observer.h" |
| 30 #include "net/proxy/proxy_service.h" | 31 #include "net/proxy/proxy_service.h" |
| 31 #include "net/sdch/sdch_owner.h" | 32 #include "net/sdch/sdch_owner.h" |
| 32 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
| 33 #include "net/url_request/url_request_context_builder.h" | 34 #include "net/url_request/url_request_context_builder.h" |
| 34 #include "net/url_request/url_request_interceptor.h" | 35 #include "net/url_request/url_request_interceptor.h" |
| 35 | 36 |
| 36 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 37 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 network_thread_->StartWithOptions(options); | 133 network_thread_->StartWithOptions(options); |
| 133 } | 134 } |
| 134 | 135 |
| 135 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { | 136 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
| 136 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 137 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 137 | 138 |
| 138 if (http_server_properties_manager_) | 139 if (http_server_properties_manager_) |
| 139 http_server_properties_manager_->ShutdownOnPrefThread(); | 140 http_server_properties_manager_->ShutdownOnPrefThread(); |
| 140 if (pref_service_) | 141 if (pref_service_) |
| 141 pref_service_->CommitPendingWrite(); | 142 pref_service_->CommitPendingWrite(); |
| 143 network_quality_estimator_->RemoveRTTObserver(this); | |
|
mef
2015/08/26 16:58:59
what if network_quality_estimator_ is null?
bengr
2015/08/27 16:44:34
Uggh. I wrote all of this when that wasn't possibl
| |
| 144 network_quality_estimator_->RemoveThroughputObserver(this); | |
| 142 StopNetLogOnNetworkThread(); | 145 StopNetLogOnNetworkThread(); |
| 143 } | 146 } |
| 144 | 147 |
| 145 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( | 148 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
| 146 JNIEnv* env, | 149 JNIEnv* env, |
| 147 jobject jcaller) { | 150 jobject jcaller) { |
| 148 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; | 151 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; |
| 149 jcaller_ref.Reset(env, jcaller); | 152 jcaller_ref.Reset(env, jcaller); |
| 150 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | 153 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
| 151 GetNetworkTaskRunner(), nullptr)); | 154 GetNetworkTaskRunner(), nullptr)); |
| 152 GetNetworkTaskRunner()->PostTask( | 155 GetNetworkTaskRunner()->PostTask( |
| 153 FROM_HERE, | 156 FROM_HERE, |
| 154 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, | 157 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
| 155 base::Unretained(this), Passed(&context_config_), | 158 base::Unretained(this), Passed(&context_config_), |
| 156 jcaller_ref)); | 159 jcaller_ref)); |
| 157 } | 160 } |
| 158 | 161 |
| 162 void CronetURLRequestContextAdapter:: | |
| 163 EnableNetworkQualityEstimatorOnNetworkThread(bool use_local_host_requests, | |
| 164 bool use_smaller_responses) { | |
| 165 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 166 scoped_ptr<net::ExternalEstimateProvider> external_estimate_provider; | |
|
mef
2015/08/26 16:58:59
uninitialized?
mef
2015/08/26 17:28:40
include header for net::ExternalEstimateProvider?
bengr
2015/08/27 16:44:34
Done.
bengr
2015/08/27 16:44:34
Done.
| |
| 167 std::map<std::string, std::string> network_quality_estimator_params; | |
|
mef
2015/08/26 16:58:59
#include <map>
bengr
2015/08/27 16:44:34
Done.
| |
| 168 network_quality_estimator_.reset(new net::NetworkQualityEstimator( | |
| 169 external_estimate_provider.Pass(), network_quality_estimator_params)); | |
| 170 context_->set_network_quality_estimator(network_quality_estimator_.get()); | |
| 171 network_quality_estimator_->Configure(use_local_host_requests, | |
| 172 use_smaller_responses); | |
| 173 } | |
| 174 | |
| 175 void CronetURLRequestContextAdapter::EnableNetworkQualityEstimator( | |
| 176 JNIEnv* env, | |
| 177 jobject jcaller, | |
| 178 jboolean use_local_host_requests, | |
| 179 jboolean use_smaller_responses) { | |
| 180 PostTaskToNetworkThread( | |
| 181 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: | |
| 182 EnableNetworkQualityEstimatorOnNetworkThread, | |
| 183 base::Unretained(this), use_local_host_requests, | |
| 184 use_smaller_responses)); | |
| 185 } | |
| 186 | |
| 187 void CronetURLRequestContextAdapter::ProvideRTTObservationsOnNetworkThread( | |
| 188 bool should) { | |
| 189 if (should) | |
| 190 network_quality_estimator_->AddRTTObserver(this); | |
|
mef
2015/08/26 16:58:59
is there any danger in doing it multiple times?
bengr
2015/08/27 16:44:34
Nope. No issue. The observer will only be added on
| |
| 191 else | |
| 192 network_quality_estimator_->RemoveRTTObserver(this); | |
| 193 } | |
| 194 | |
| 195 void CronetURLRequestContextAdapter::ProvideRTTObservations(JNIEnv* env, | |
| 196 jobject jcaller, | |
| 197 bool should) { | |
| 198 PostTaskToNetworkThread(FROM_HERE, | |
| 199 base::Bind(&CronetURLRequestContextAdapter:: | |
| 200 ProvideRTTObservationsOnNetworkThread, | |
| 201 base::Unretained(this), should)); | |
| 202 } | |
| 203 | |
| 204 void CronetURLRequestContextAdapter:: | |
| 205 ProvideThroughputObservationsOnNetworkThread(bool should) { | |
| 206 if (should) | |
| 207 network_quality_estimator_->AddThroughputObserver(this); | |
| 208 else | |
| 209 network_quality_estimator_->RemoveThroughputObserver(this); | |
| 210 } | |
| 211 | |
| 212 void CronetURLRequestContextAdapter::ProvideThroughputObservations( | |
| 213 JNIEnv* env, | |
| 214 jobject jcaller, | |
| 215 bool should) { | |
| 216 PostTaskToNetworkThread( | |
| 217 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: | |
| 218 ProvideThroughputObservationsOnNetworkThread, | |
| 219 base::Unretained(this), should)); | |
| 220 } | |
| 221 | |
| 159 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 222 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| 160 scoped_ptr<URLRequestContextConfig> config, | 223 scoped_ptr<URLRequestContextConfig> config, |
| 161 const base::android::ScopedJavaGlobalRef<jobject>& | 224 const base::android::ScopedJavaGlobalRef<jobject>& |
| 162 jcronet_url_request_context) { | 225 jcronet_url_request_context) { |
| 163 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 226 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 164 DCHECK(!is_context_initialized_); | 227 DCHECK(!is_context_initialized_); |
| 165 DCHECK(proxy_config_service_); | 228 DCHECK(proxy_config_service_); |
| 166 // TODO(mmenke): Add method to have the builder enable SPDY. | 229 // TODO(mmenke): Add method to have the builder enable SPDY. |
| 167 net::URLRequestContextBuilder context_builder; | 230 net::URLRequestContextBuilder context_builder; |
| 168 | 231 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 net::AlternativeService alternative_service( | 337 net::AlternativeService alternative_service( |
| 275 net::AlternateProtocol::QUIC, "", | 338 net::AlternateProtocol::QUIC, "", |
| 276 static_cast<uint16>(quic_hint.alternate_port)); | 339 static_cast<uint16>(quic_hint.alternate_port)); |
| 277 context_->http_server_properties()->SetAlternativeService( | 340 context_->http_server_properties()->SetAlternativeService( |
| 278 quic_hint_host_port_pair, alternative_service, 1.0f, | 341 quic_hint_host_port_pair, alternative_service, 1.0f, |
| 279 base::Time::Max()); | 342 base::Time::Max()); |
| 280 } | 343 } |
| 281 } | 344 } |
| 282 | 345 |
| 283 JNIEnv* env = base::android::AttachCurrentThread(); | 346 JNIEnv* env = base::android::AttachCurrentThread(); |
| 347 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); | |
| 284 Java_CronetUrlRequestContext_initNetworkThread( | 348 Java_CronetUrlRequestContext_initNetworkThread( |
| 285 env, jcronet_url_request_context.obj()); | 349 env, jcronet_url_request_context.obj()); |
| 286 | 350 |
| 287 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 351 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
| 288 if (data_reduction_proxy_) | 352 if (data_reduction_proxy_) |
| 289 data_reduction_proxy_->Init(true, GetURLRequestContext()); | 353 data_reduction_proxy_->Init(true, GetURLRequestContext()); |
| 290 #endif | 354 #endif |
| 291 is_context_initialized_ = true; | 355 is_context_initialized_ = true; |
| 292 while (!tasks_waiting_for_context_.empty()) { | 356 while (!tasks_waiting_for_context_.empty()) { |
| 293 tasks_waiting_for_context_.front().Run(); | 357 tasks_waiting_for_context_.front().Run(); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 | 456 |
| 393 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { | 457 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { |
| 394 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 458 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 395 if (!file_thread_) { | 459 if (!file_thread_) { |
| 396 file_thread_.reset(new base::Thread("Network File Thread")); | 460 file_thread_.reset(new base::Thread("Network File Thread")); |
| 397 file_thread_->Start(); | 461 file_thread_->Start(); |
| 398 } | 462 } |
| 399 return file_thread_.get(); | 463 return file_thread_.get(); |
| 400 } | 464 } |
| 401 | 465 |
| 466 void CronetURLRequestContextAdapter::OnRTTObservation( | |
| 467 int32_t rtt_ms, | |
| 468 const base::TimeTicks& timestamp, | |
| 469 net::NetworkQualityEstimator::ObservationSource source) { | |
| 470 Java_CronetUrlRequestContext_onRTTObservation( | |
| 471 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), | |
| 472 rtt_ms, (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), | |
| 473 static_cast<jint>(source)); | |
| 474 } | |
| 475 | |
| 476 void CronetURLRequestContextAdapter::OnThroughputObservation( | |
| 477 int32_t throughput_kbps, | |
| 478 const base::TimeTicks& timestamp, | |
| 479 net::NetworkQualityEstimator::ObservationSource source) { | |
| 480 Java_CronetUrlRequestContext_onThroughputObservation( | |
| 481 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), | |
| 482 throughput_kbps, | |
| 483 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), | |
| 484 static_cast<jint>(source)); | |
| 485 } | |
| 486 | |
| 402 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | 487 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
| 403 // returns 0 otherwise. | 488 // returns 0 otherwise. |
| 404 static jlong CreateRequestContextAdapter(JNIEnv* env, | 489 static jlong CreateRequestContextAdapter(JNIEnv* env, |
| 405 jclass jcaller, | 490 jclass jcaller, |
| 406 jstring jconfig) { | 491 jstring jconfig) { |
| 407 std::string config_string = | 492 std::string config_string = |
| 408 base::android::ConvertJavaStringToUTF8(env, jconfig); | 493 base::android::ConvertJavaStringToUTF8(env, jconfig); |
| 409 scoped_ptr<URLRequestContextConfig> context_config( | 494 scoped_ptr<URLRequestContextConfig> context_config( |
| 410 new URLRequestContextConfig()); | 495 new URLRequestContextConfig()); |
| 411 if (!context_config->LoadFromJSON(config_string)) | 496 if (!context_config->LoadFromJSON(config_string)) |
| 412 return 0; | 497 return 0; |
| 413 | 498 |
| 414 CronetURLRequestContextAdapter* context_adapter = | 499 CronetURLRequestContextAdapter* context_adapter = |
| 415 new CronetURLRequestContextAdapter(context_config.Pass()); | 500 new CronetURLRequestContextAdapter(context_config.Pass()); |
| 416 return reinterpret_cast<jlong>(context_adapter); | 501 return reinterpret_cast<jlong>(context_adapter); |
| 417 } | 502 } |
| 418 | 503 |
| 419 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { | 504 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { |
| 420 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 505 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
| 421 // MinLogLevel is global, shared by all URLRequestContexts. | 506 // MinLogLevel is global, shared by all URLRequestContexts. |
| 422 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 507 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
| 423 return old_log_level; | 508 return old_log_level; |
| 424 } | 509 } |
| 425 | 510 |
| 426 } // namespace cronet | 511 } // namespace cronet |
| OLD | NEW |