| 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> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/prefs/pref_filter.h" | 17 #include "base/prefs/pref_filter.h" |
| 18 #include "base/prefs/pref_registry_simple.h" | 18 #include "base/prefs/pref_registry_simple.h" |
| 19 #include "base/prefs/pref_service.h" | 19 #include "base/prefs/pref_service.h" |
| 20 #include "base/prefs/pref_service_factory.h" | 20 #include "base/prefs/pref_service_factory.h" |
| 21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
| 22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "base/values.h" | 23 #include "base/values.h" |
| 24 #include "components/cronet/url_request_context_config.h" | 24 #include "components/cronet/url_request_context_config.h" |
| 25 #include "jni/CronetUrlRequestContext_jni.h" | 25 #include "jni/CronetUrlRequestContext_jni.h" |
| 26 #include "net/base/external_estimate_provider.h" | 26 #include "net/base/external_estimate_provider.h" |
| 27 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
| 28 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 29 #include "net/base/network_delegate_impl.h" | 29 #include "net/base/network_delegate_impl.h" |
| 30 #include "net/cert/cert_verifier.h" |
| 30 #include "net/http/http_auth_handler_factory.h" | 31 #include "net/http/http_auth_handler_factory.h" |
| 31 #include "net/http/http_server_properties_manager.h" | 32 #include "net/http/http_server_properties_manager.h" |
| 32 #include "net/log/write_to_file_net_log_observer.h" | 33 #include "net/log/write_to_file_net_log_observer.h" |
| 33 #include "net/proxy/proxy_config_service_android.h" | 34 #include "net/proxy/proxy_config_service_android.h" |
| 34 #include "net/proxy/proxy_service.h" | 35 #include "net/proxy/proxy_service.h" |
| 35 #include "net/sdch/sdch_owner.h" | 36 #include "net/sdch/sdch_owner.h" |
| 36 #include "net/url_request/url_request_context.h" | 37 #include "net/url_request/url_request_context.h" |
| 37 #include "net/url_request/url_request_context_builder.h" | 38 #include "net/url_request/url_request_context_builder.h" |
| 38 #include "net/url_request/url_request_interceptor.h" | 39 #include "net/url_request/url_request_interceptor.h" |
| 39 | 40 |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 void CronetURLRequestContextAdapter::ProvideThroughputObservations( | 232 void CronetURLRequestContextAdapter::ProvideThroughputObservations( |
| 232 JNIEnv* env, | 233 JNIEnv* env, |
| 233 jobject jcaller, | 234 jobject jcaller, |
| 234 bool should) { | 235 bool should) { |
| 235 PostTaskToNetworkThread( | 236 PostTaskToNetworkThread( |
| 236 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: | 237 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: |
| 237 ProvideThroughputObservationsOnNetworkThread, | 238 ProvideThroughputObservationsOnNetworkThread, |
| 238 base::Unretained(this), should)); | 239 base::Unretained(this), should)); |
| 239 } | 240 } |
| 240 | 241 |
| 242 void CronetURLRequestContextAdapter::SetMockCertVerifierForTesting( |
| 243 JNIEnv* env, |
| 244 jobject jcaller, |
| 245 jlong jmock_verifier) { |
| 246 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 247 // Not using RunTaskAfterContextInitOnNetworkThread so verifier can be set |
| 248 // before the context is initialized. |
| 249 GetNetworkTaskRunner()->PostTask( |
| 250 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: |
| 251 SetMockCertVerifierForTestingOnNetworkThread, |
| 252 base::Unretained(this), jmock_verifier)); |
| 253 } |
| 254 |
| 255 void CronetURLRequestContextAdapter:: |
| 256 SetMockCertVerifierForTestingOnNetworkThread(jlong jmock_verifier) { |
| 257 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 258 DCHECK(!context_builder_); |
| 259 |
| 260 context_builder_.reset(new net::URLRequestContextBuilder); |
| 261 net::CertVerifier* cert_verifier = |
| 262 reinterpret_cast<net::CertVerifier*>(jmock_verifier); |
| 263 context_builder_->set_cert_verifier(cert_verifier); |
| 264 } |
| 265 |
| 241 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 266 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| 242 scoped_ptr<URLRequestContextConfig> config, | 267 scoped_ptr<URLRequestContextConfig> config, |
| 243 const base::android::ScopedJavaGlobalRef<jobject>& | 268 const base::android::ScopedJavaGlobalRef<jobject>& |
| 244 jcronet_url_request_context) { | 269 jcronet_url_request_context) { |
| 245 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 270 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 246 DCHECK(!is_context_initialized_); | 271 DCHECK(!is_context_initialized_); |
| 247 DCHECK(proxy_config_service_); | 272 DCHECK(proxy_config_service_); |
| 248 // TODO(mmenke): Add method to have the builder enable SPDY. | 273 // TODO(mmenke): Add method to have the builder enable SPDY. |
| 249 net::URLRequestContextBuilder context_builder; | 274 if (!context_builder_) |
| 275 context_builder_.reset(new net::URLRequestContextBuilder()); |
| 250 | 276 |
| 251 net_log_.reset(new net::NetLog); | 277 net_log_.reset(new net::NetLog); |
| 252 scoped_ptr<net::NetworkDelegate> network_delegate(new BasicNetworkDelegate()); | 278 scoped_ptr<net::NetworkDelegate> network_delegate(new BasicNetworkDelegate()); |
| 253 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 279 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
| 254 DCHECK(!data_reduction_proxy_); | 280 DCHECK(!data_reduction_proxy_); |
| 255 // For now, the choice to enable the data reduction proxy happens once, | 281 // For now, the choice to enable the data reduction proxy happens once, |
| 256 // at initialization. It cannot be disabled thereafter. | 282 // at initialization. It cannot be disabled thereafter. |
| 257 if (!config->data_reduction_proxy_key.empty()) { | 283 if (!config->data_reduction_proxy_key.empty()) { |
| 258 data_reduction_proxy_.reset(new CronetDataReductionProxy( | 284 data_reduction_proxy_.reset(new CronetDataReductionProxy( |
| 259 config->data_reduction_proxy_key, config->data_reduction_primary_proxy, | 285 config->data_reduction_proxy_key, config->data_reduction_primary_proxy, |
| 260 config->data_reduction_fallback_proxy, | 286 config->data_reduction_fallback_proxy, |
| 261 config->data_reduction_secure_proxy_check_url, config->user_agent, | 287 config->data_reduction_secure_proxy_check_url, config->user_agent, |
| 262 GetNetworkTaskRunner(), net_log_.get())); | 288 GetNetworkTaskRunner(), net_log_.get())); |
| 263 network_delegate = | 289 network_delegate = |
| 264 data_reduction_proxy_->CreateNetworkDelegate(network_delegate.Pass()); | 290 data_reduction_proxy_->CreateNetworkDelegate(network_delegate.Pass()); |
| 265 ScopedVector<net::URLRequestInterceptor> interceptors; | 291 ScopedVector<net::URLRequestInterceptor> interceptors; |
| 266 interceptors.push_back(data_reduction_proxy_->CreateInterceptor()); | 292 interceptors.push_back(data_reduction_proxy_->CreateInterceptor()); |
| 267 context_builder.SetInterceptors(interceptors.Pass()); | 293 context_builder_->SetInterceptors(interceptors.Pass()); |
| 268 } | 294 } |
| 269 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) | 295 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) |
| 270 context_builder.set_network_delegate(network_delegate.Pass()); | 296 context_builder_->set_network_delegate(network_delegate.Pass()); |
| 271 context_builder.set_net_log(net_log_.get()); | 297 context_builder_->set_net_log(net_log_.get()); |
| 272 | 298 |
| 273 // Android provides a local HTTP proxy server that handles proxying when a PAC | 299 // Android provides a local HTTP proxy server that handles proxying when a PAC |
| 274 // URL is present. Create a proxy service without a resolver and rely on this | 300 // URL is present. Create a proxy service without a resolver and rely on this |
| 275 // local HTTP proxy. See: crbug.com/432539. | 301 // local HTTP proxy. See: crbug.com/432539. |
| 276 context_builder.set_proxy_service( | 302 context_builder_->set_proxy_service( |
| 277 net::ProxyService::CreateWithoutProxyResolver( | 303 net::ProxyService::CreateWithoutProxyResolver( |
| 278 proxy_config_service_.Pass(), net_log_.get())); | 304 proxy_config_service_.Pass(), net_log_.get())); |
| 279 config->ConfigureURLRequestContextBuilder(&context_builder); | 305 config->ConfigureURLRequestContextBuilder(context_builder_.get()); |
| 280 | 306 |
| 281 // Set up pref file if storage path is specified. | 307 // Set up pref file if storage path is specified. |
| 282 if (!config->storage_path.empty()) { | 308 if (!config->storage_path.empty()) { |
| 283 base::FilePath filepath(config->storage_path); | 309 base::FilePath filepath(config->storage_path); |
| 284 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); | 310 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); |
| 285 json_pref_store_ = new JsonPrefStore( | 311 json_pref_store_ = new JsonPrefStore( |
| 286 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); | 312 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); |
| 287 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); | 313 context_builder_->SetFileTaskRunner(GetFileThread()->task_runner()); |
| 288 | 314 |
| 289 // Set up HttpServerPropertiesManager. | 315 // Set up HttpServerPropertiesManager. |
| 290 base::PrefServiceFactory factory; | 316 base::PrefServiceFactory factory; |
| 291 factory.set_user_prefs(json_pref_store_); | 317 factory.set_user_prefs(json_pref_store_); |
| 292 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); | 318 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); |
| 293 registry->RegisterDictionaryPref(kHttpServerProperties, | 319 registry->RegisterDictionaryPref(kHttpServerProperties, |
| 294 new base::DictionaryValue()); | 320 new base::DictionaryValue()); |
| 295 pref_service_ = factory.Create(registry.get()).Pass(); | 321 pref_service_ = factory.Create(registry.get()).Pass(); |
| 296 | 322 |
| 297 scoped_ptr<net::HttpServerPropertiesManager> http_server_properties_manager( | 323 scoped_ptr<net::HttpServerPropertiesManager> http_server_properties_manager( |
| 298 new net::HttpServerPropertiesManager(pref_service_.get(), | 324 new net::HttpServerPropertiesManager(pref_service_.get(), |
| 299 kHttpServerProperties, | 325 kHttpServerProperties, |
| 300 GetNetworkTaskRunner())); | 326 GetNetworkTaskRunner())); |
| 301 http_server_properties_manager->InitializeOnNetworkThread(); | 327 http_server_properties_manager->InitializeOnNetworkThread(); |
| 302 http_server_properties_manager_ = http_server_properties_manager.get(); | 328 http_server_properties_manager_ = http_server_properties_manager.get(); |
| 303 context_builder.SetHttpServerProperties( | 329 context_builder_->SetHttpServerProperties( |
| 304 http_server_properties_manager.Pass()); | 330 http_server_properties_manager.Pass()); |
| 305 } | 331 } |
| 306 | 332 |
| 307 context_ = context_builder.Build().Pass(); | 333 context_ = context_builder_->Build().Pass(); |
| 308 | 334 |
| 309 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | | 335 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | |
| 310 net::LOAD_DO_NOT_SEND_COOKIES; | 336 net::LOAD_DO_NOT_SEND_COOKIES; |
| 311 if (config->load_disable_cache) | 337 if (config->load_disable_cache) |
| 312 default_load_flags_ |= net::LOAD_DISABLE_CACHE; | 338 default_load_flags_ |= net::LOAD_DISABLE_CACHE; |
| 313 | 339 |
| 314 if (config->enable_sdch) { | 340 if (config->enable_sdch) { |
| 315 DCHECK(context_->sdch_manager()); | 341 DCHECK(context_->sdch_manager()); |
| 316 sdch_owner_.reset( | 342 sdch_owner_.reset( |
| 317 new net::SdchOwner(context_->sdch_manager(), context_.get())); | 343 new net::SdchOwner(context_->sdch_manager(), context_.get())); |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 static jint SetMinLogLevel(JNIEnv* env, | 550 static jint SetMinLogLevel(JNIEnv* env, |
| 525 const JavaParamRef<jclass>& jcaller, | 551 const JavaParamRef<jclass>& jcaller, |
| 526 jint jlog_level) { | 552 jint jlog_level) { |
| 527 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 553 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
| 528 // MinLogLevel is global, shared by all URLRequestContexts. | 554 // MinLogLevel is global, shared by all URLRequestContexts. |
| 529 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 555 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
| 530 return old_log_level; | 556 return old_log_level; |
| 531 } | 557 } |
| 532 | 558 |
| 533 } // namespace cronet | 559 } // namespace cronet |
| OLD | NEW |