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