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" |
| 30 #include "net/base/network_quality_estimator.h" | |
|
mef
2015/08/28 20:53:15
included into header?
bengr
2015/08/28 23:51:36
Oy. Sorry for the sloppiness. I should have checke
| |
| 27 #include "net/http/http_auth_handler_factory.h" | 31 #include "net/http/http_auth_handler_factory.h" |
| 28 #include "net/http/http_server_properties_manager.h" | 32 #include "net/http/http_server_properties_manager.h" |
| 29 #include "net/log/write_to_file_net_log_observer.h" | 33 #include "net/log/write_to_file_net_log_observer.h" |
| 30 #include "net/proxy/proxy_service.h" | 34 #include "net/proxy/proxy_service.h" |
| 31 #include "net/sdch/sdch_owner.h" | 35 #include "net/sdch/sdch_owner.h" |
| 32 #include "net/url_request/url_request_context.h" | 36 #include "net/url_request/url_request_context.h" |
| 33 #include "net/url_request/url_request_context_builder.h" | 37 #include "net/url_request/url_request_context_builder.h" |
| 34 #include "net/url_request/url_request_interceptor.h" | 38 #include "net/url_request/url_request_interceptor.h" |
| 35 | 39 |
| 36 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 40 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 network_thread_->StartWithOptions(options); | 136 network_thread_->StartWithOptions(options); |
| 133 } | 137 } |
| 134 | 138 |
| 135 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { | 139 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
| 136 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 140 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 137 | 141 |
| 138 if (http_server_properties_manager_) | 142 if (http_server_properties_manager_) |
| 139 http_server_properties_manager_->ShutdownOnPrefThread(); | 143 http_server_properties_manager_->ShutdownOnPrefThread(); |
| 140 if (pref_service_) | 144 if (pref_service_) |
| 141 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 } | |
| 142 StopNetLogOnNetworkThread(); | 150 StopNetLogOnNetworkThread(); |
| 143 } | 151 } |
| 144 | 152 |
| 145 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( | 153 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
| 146 JNIEnv* env, | 154 JNIEnv* env, |
| 147 jobject jcaller) { | 155 jobject jcaller) { |
| 148 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; | 156 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; |
| 149 jcaller_ref.Reset(env, jcaller); | 157 jcaller_ref.Reset(env, jcaller); |
| 150 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | 158 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
| 151 GetNetworkTaskRunner(), nullptr)); | 159 GetNetworkTaskRunner(), nullptr)); |
| 152 GetNetworkTaskRunner()->PostTask( | 160 GetNetworkTaskRunner()->PostTask( |
| 153 FROM_HERE, | 161 FROM_HERE, |
| 154 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, | 162 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
| 155 base::Unretained(this), Passed(&context_config_), | 163 base::Unretained(this), Passed(&context_config_), |
| 156 jcaller_ref)); | 164 jcaller_ref)); |
| 157 } | 165 } |
| 158 | 166 |
| 167 void CronetURLRequestContextAdapter:: | |
| 168 EnableNetworkQualityEstimatorOnNetworkThread(bool use_local_host_requests, | |
|
mef
2015/08/28 20:53:15
So, what happens if this is called twice?
bengr
2015/08/28 23:51:36
Nothing bad if this registers as an observer twice
| |
| 169 bool use_smaller_responses) { | |
| 170 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
| 171 DCHECK(!network_quality_estimator_); | |
| 172 network_quality_estimator_.reset(new net::NetworkQualityEstimator( | |
| 173 scoped_ptr<net::ExternalEstimateProvider>(), | |
| 174 std::map<std::string, std::string>())); | |
| 175 context_->set_network_quality_estimator(network_quality_estimator_.get()); | |
| 176 network_quality_estimator_->Configure(use_local_host_requests, | |
| 177 use_smaller_responses); | |
| 178 } | |
| 179 | |
| 180 void CronetURLRequestContextAdapter::EnableNetworkQualityEstimator( | |
| 181 JNIEnv* env, | |
| 182 jobject jcaller, | |
| 183 jboolean use_local_host_requests, | |
| 184 jboolean use_smaller_responses) { | |
| 185 PostTaskToNetworkThread( | |
| 186 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: | |
| 187 EnableNetworkQualityEstimatorOnNetworkThread, | |
| 188 base::Unretained(this), use_local_host_requests, | |
| 189 use_smaller_responses)); | |
| 190 } | |
| 191 | |
| 192 void CronetURLRequestContextAdapter::ProvideRTTObservationsOnNetworkThread( | |
| 193 bool should) { | |
|
mef
2015/08/28 20:53:15
DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThr
bengr
2015/08/28 23:51:36
Done.
| |
| 194 DCHECK(network_quality_estimator_); | |
|
mef
2015/08/28 20:53:15
what if EnableNetworkQualityEstimator was never ca
bengr
2015/08/28 23:51:36
I changed these to silently fail, so tests wouldn'
| |
| 195 if (should) | |
| 196 network_quality_estimator_->AddRTTObserver(this); | |
| 197 else | |
| 198 network_quality_estimator_->RemoveRTTObserver(this); | |
| 199 } | |
| 200 | |
| 201 void CronetURLRequestContextAdapter::ProvideRTTObservations(JNIEnv* env, | |
| 202 jobject jcaller, | |
| 203 bool should) { | |
| 204 PostTaskToNetworkThread(FROM_HERE, | |
| 205 base::Bind(&CronetURLRequestContextAdapter:: | |
| 206 ProvideRTTObservationsOnNetworkThread, | |
| 207 base::Unretained(this), should)); | |
| 208 } | |
| 209 | |
| 210 void CronetURLRequestContextAdapter:: | |
| 211 ProvideThroughputObservationsOnNetworkThread(bool should) { | |
| 212 DCHECK(network_quality_estimator_); | |
|
mef
2015/08/28 20:53:15
what if EnableNetworkQualityEstimator was never ca
bengr
2015/08/28 23:51:36
See above comment.
| |
| 213 if (should) | |
| 214 network_quality_estimator_->AddThroughputObserver(this); | |
| 215 else | |
| 216 network_quality_estimator_->RemoveThroughputObserver(this); | |
| 217 } | |
| 218 | |
| 219 void CronetURLRequestContextAdapter::ProvideThroughputObservations( | |
| 220 JNIEnv* env, | |
| 221 jobject jcaller, | |
| 222 bool should) { | |
| 223 PostTaskToNetworkThread( | |
| 224 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: | |
| 225 ProvideThroughputObservationsOnNetworkThread, | |
| 226 base::Unretained(this), should)); | |
| 227 } | |
| 228 | |
| 159 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 229 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| 160 scoped_ptr<URLRequestContextConfig> config, | 230 scoped_ptr<URLRequestContextConfig> config, |
| 161 const base::android::ScopedJavaGlobalRef<jobject>& | 231 const base::android::ScopedJavaGlobalRef<jobject>& |
| 162 jcronet_url_request_context) { | 232 jcronet_url_request_context) { |
| 163 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 233 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 164 DCHECK(!is_context_initialized_); | 234 DCHECK(!is_context_initialized_); |
| 165 DCHECK(proxy_config_service_); | 235 DCHECK(proxy_config_service_); |
| 166 // TODO(mmenke): Add method to have the builder enable SPDY. | 236 // TODO(mmenke): Add method to have the builder enable SPDY. |
| 167 net::URLRequestContextBuilder context_builder; | 237 net::URLRequestContextBuilder context_builder; |
| 168 | 238 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 net::AlternativeService alternative_service( | 344 net::AlternativeService alternative_service( |
| 275 net::AlternateProtocol::QUIC, "", | 345 net::AlternateProtocol::QUIC, "", |
| 276 static_cast<uint16>(quic_hint.alternate_port)); | 346 static_cast<uint16>(quic_hint.alternate_port)); |
| 277 context_->http_server_properties()->SetAlternativeService( | 347 context_->http_server_properties()->SetAlternativeService( |
| 278 quic_hint_host_port_pair, alternative_service, 1.0f, | 348 quic_hint_host_port_pair, alternative_service, 1.0f, |
| 279 base::Time::Max()); | 349 base::Time::Max()); |
| 280 } | 350 } |
| 281 } | 351 } |
| 282 | 352 |
| 283 JNIEnv* env = base::android::AttachCurrentThread(); | 353 JNIEnv* env = base::android::AttachCurrentThread(); |
| 354 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); | |
| 284 Java_CronetUrlRequestContext_initNetworkThread( | 355 Java_CronetUrlRequestContext_initNetworkThread( |
| 285 env, jcronet_url_request_context.obj()); | 356 env, jcronet_url_request_context.obj()); |
| 286 | 357 |
| 287 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 358 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
| 288 if (data_reduction_proxy_) | 359 if (data_reduction_proxy_) |
| 289 data_reduction_proxy_->Init(true, GetURLRequestContext()); | 360 data_reduction_proxy_->Init(true, GetURLRequestContext()); |
| 290 #endif | 361 #endif |
| 291 is_context_initialized_ = true; | 362 is_context_initialized_ = true; |
| 292 while (!tasks_waiting_for_context_.empty()) { | 363 while (!tasks_waiting_for_context_.empty()) { |
| 293 tasks_waiting_for_context_.front().Run(); | 364 tasks_waiting_for_context_.front().Run(); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 | 463 |
| 393 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { | 464 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { |
| 394 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 465 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 395 if (!file_thread_) { | 466 if (!file_thread_) { |
| 396 file_thread_.reset(new base::Thread("Network File Thread")); | 467 file_thread_.reset(new base::Thread("Network File Thread")); |
| 397 file_thread_->Start(); | 468 file_thread_->Start(); |
| 398 } | 469 } |
| 399 return file_thread_.get(); | 470 return file_thread_.get(); |
| 400 } | 471 } |
| 401 | 472 |
| 473 void CronetURLRequestContextAdapter::OnRTTObservation( | |
| 474 int32_t rtt_ms, | |
| 475 const base::TimeTicks& timestamp, | |
| 476 net::NetworkQualityEstimator::ObservationSource source) { | |
| 477 Java_CronetUrlRequestContext_onRTTObservation( | |
| 478 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), | |
| 479 rtt_ms, (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), | |
| 480 static_cast<jint>(source)); | |
| 481 } | |
| 482 | |
| 483 void CronetURLRequestContextAdapter::OnThroughputObservation( | |
| 484 int32_t throughput_kbps, | |
| 485 const base::TimeTicks& timestamp, | |
| 486 net::NetworkQualityEstimator::ObservationSource source) { | |
| 487 Java_CronetUrlRequestContext_onThroughputObservation( | |
| 488 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), | |
| 489 throughput_kbps, | |
| 490 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), | |
| 491 static_cast<jint>(source)); | |
| 492 } | |
| 493 | |
| 402 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | 494 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
| 403 // returns 0 otherwise. | 495 // returns 0 otherwise. |
| 404 static jlong CreateRequestContextAdapter(JNIEnv* env, | 496 static jlong CreateRequestContextAdapter(JNIEnv* env, |
| 405 jclass jcaller, | 497 jclass jcaller, |
| 406 jstring jconfig) { | 498 jstring jconfig) { |
| 407 std::string config_string = | 499 std::string config_string = |
| 408 base::android::ConvertJavaStringToUTF8(env, jconfig); | 500 base::android::ConvertJavaStringToUTF8(env, jconfig); |
| 409 scoped_ptr<URLRequestContextConfig> context_config( | 501 scoped_ptr<URLRequestContextConfig> context_config( |
| 410 new URLRequestContextConfig()); | 502 new URLRequestContextConfig()); |
| 411 if (!context_config->LoadFromJSON(config_string)) | 503 if (!context_config->LoadFromJSON(config_string)) |
| 412 return 0; | 504 return 0; |
| 413 | 505 |
| 414 CronetURLRequestContextAdapter* context_adapter = | 506 CronetURLRequestContextAdapter* context_adapter = |
| 415 new CronetURLRequestContextAdapter(context_config.Pass()); | 507 new CronetURLRequestContextAdapter(context_config.Pass()); |
| 416 return reinterpret_cast<jlong>(context_adapter); | 508 return reinterpret_cast<jlong>(context_adapter); |
| 417 } | 509 } |
| 418 | 510 |
| 419 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { | 511 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { |
| 420 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 512 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
| 421 // MinLogLevel is global, shared by all URLRequestContexts. | 513 // MinLogLevel is global, shared by all URLRequestContexts. |
| 422 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 514 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
| 423 return old_log_level; | 515 return old_log_level; |
| 424 } | 516 } |
| 425 | 517 |
| 426 } // namespace cronet | 518 } // namespace cronet |
| OLD | NEW |