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>())); |
| 182 context_->set_network_quality_estimator(network_quality_estimator_.get()); |
| 183 // These are set to true only for tests. |
| 184 if (use_local_host_requests || use_smaller_responses) { |
| 185 network_quality_estimator_->ConfigureForTests(use_local_host_requests, |
| 186 use_smaller_responses); |
| 187 } |
| 188 } |
| 189 |
| 190 void CronetURLRequestContextAdapter::EnableNetworkQualityEstimator( |
| 191 JNIEnv* env, |
| 192 jobject jcaller, |
| 193 jboolean use_local_host_requests, |
| 194 jboolean use_smaller_responses) { |
| 195 PostTaskToNetworkThread( |
| 196 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: |
| 197 EnableNetworkQualityEstimatorOnNetworkThread, |
| 198 base::Unretained(this), use_local_host_requests, |
| 199 use_smaller_responses)); |
| 200 } |
| 201 |
| 202 void CronetURLRequestContextAdapter::ProvideRTTObservationsOnNetworkThread( |
| 203 bool should) { |
| 204 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 205 if (!network_quality_estimator_) |
| 206 return; |
| 207 if (should) |
| 208 network_quality_estimator_->AddRTTObserver(this); |
| 209 else |
| 210 network_quality_estimator_->RemoveRTTObserver(this); |
| 211 } |
| 212 |
| 213 void CronetURLRequestContextAdapter::ProvideRTTObservations(JNIEnv* env, |
| 214 jobject jcaller, |
| 215 bool should) { |
| 216 PostTaskToNetworkThread(FROM_HERE, |
| 217 base::Bind(&CronetURLRequestContextAdapter:: |
| 218 ProvideRTTObservationsOnNetworkThread, |
| 219 base::Unretained(this), should)); |
| 220 } |
| 221 |
| 222 void CronetURLRequestContextAdapter:: |
| 223 ProvideThroughputObservationsOnNetworkThread(bool should) { |
| 224 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 225 if (!network_quality_estimator_) |
| 226 return; |
| 227 if (should) |
| 228 network_quality_estimator_->AddThroughputObserver(this); |
| 229 else |
| 230 network_quality_estimator_->RemoveThroughputObserver(this); |
| 231 } |
| 232 |
| 233 void CronetURLRequestContextAdapter::ProvideThroughputObservations( |
| 234 JNIEnv* env, |
| 235 jobject jcaller, |
| 236 bool should) { |
| 237 PostTaskToNetworkThread( |
| 238 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: |
| 239 ProvideThroughputObservationsOnNetworkThread, |
| 240 base::Unretained(this), should)); |
| 241 } |
| 242 |
167 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 243 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
168 scoped_ptr<URLRequestContextConfig> config, | 244 scoped_ptr<URLRequestContextConfig> config, |
169 const base::android::ScopedJavaGlobalRef<jobject>& | 245 const base::android::ScopedJavaGlobalRef<jobject>& |
170 jcronet_url_request_context) { | 246 jcronet_url_request_context) { |
171 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 247 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
172 DCHECK(!is_context_initialized_); | 248 DCHECK(!is_context_initialized_); |
173 DCHECK(proxy_config_service_); | 249 DCHECK(proxy_config_service_); |
174 // TODO(mmenke): Add method to have the builder enable SPDY. | 250 // TODO(mmenke): Add method to have the builder enable SPDY. |
175 net::URLRequestContextBuilder context_builder; | 251 net::URLRequestContextBuilder context_builder; |
176 | 252 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 net::AlternativeService alternative_service( | 360 net::AlternativeService alternative_service( |
285 net::AlternateProtocol::QUIC, "", | 361 net::AlternateProtocol::QUIC, "", |
286 static_cast<uint16>(quic_hint.alternate_port)); | 362 static_cast<uint16>(quic_hint.alternate_port)); |
287 context_->http_server_properties()->SetAlternativeService( | 363 context_->http_server_properties()->SetAlternativeService( |
288 quic_hint_host_port_pair, alternative_service, 1.0f, | 364 quic_hint_host_port_pair, alternative_service, 1.0f, |
289 base::Time::Max()); | 365 base::Time::Max()); |
290 } | 366 } |
291 } | 367 } |
292 | 368 |
293 JNIEnv* env = base::android::AttachCurrentThread(); | 369 JNIEnv* env = base::android::AttachCurrentThread(); |
| 370 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); |
294 Java_CronetUrlRequestContext_initNetworkThread( | 371 Java_CronetUrlRequestContext_initNetworkThread( |
295 env, jcronet_url_request_context.obj()); | 372 env, jcronet_url_request_context.obj()); |
296 | 373 |
297 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 374 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
298 if (data_reduction_proxy_) | 375 if (data_reduction_proxy_) |
299 data_reduction_proxy_->Init(true, GetURLRequestContext()); | 376 data_reduction_proxy_->Init(true, GetURLRequestContext()); |
300 #endif | 377 #endif |
301 is_context_initialized_ = true; | 378 is_context_initialized_ = true; |
302 while (!tasks_waiting_for_context_.empty()) { | 379 while (!tasks_waiting_for_context_.empty()) { |
303 tasks_waiting_for_context_.front().Run(); | 380 tasks_waiting_for_context_.front().Run(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 | 479 |
403 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { | 480 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { |
404 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 481 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
405 if (!file_thread_) { | 482 if (!file_thread_) { |
406 file_thread_.reset(new base::Thread("Network File Thread")); | 483 file_thread_.reset(new base::Thread("Network File Thread")); |
407 file_thread_->Start(); | 484 file_thread_->Start(); |
408 } | 485 } |
409 return file_thread_.get(); | 486 return file_thread_.get(); |
410 } | 487 } |
411 | 488 |
| 489 void CronetURLRequestContextAdapter::OnRTTObservation( |
| 490 int32_t rtt_ms, |
| 491 const base::TimeTicks& timestamp, |
| 492 net::NetworkQualityEstimator::ObservationSource source) { |
| 493 Java_CronetUrlRequestContext_onRttObservation( |
| 494 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), |
| 495 rtt_ms, (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), |
| 496 source); |
| 497 } |
| 498 |
| 499 void CronetURLRequestContextAdapter::OnThroughputObservation( |
| 500 int32_t throughput_kbps, |
| 501 const base::TimeTicks& timestamp, |
| 502 net::NetworkQualityEstimator::ObservationSource source) { |
| 503 Java_CronetUrlRequestContext_onThroughputObservation( |
| 504 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), |
| 505 throughput_kbps, |
| 506 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), source); |
| 507 } |
| 508 |
412 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | 509 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
413 // returns 0 otherwise. | 510 // returns 0 otherwise. |
414 static jlong CreateRequestContextAdapter(JNIEnv* env, | 511 static jlong CreateRequestContextAdapter(JNIEnv* env, |
415 const JavaParamRef<jclass>& jcaller, | 512 const JavaParamRef<jclass>& jcaller, |
416 const JavaParamRef<jstring>& jconfig) { | 513 const JavaParamRef<jstring>& jconfig) { |
417 std::string config_string = | 514 std::string config_string = |
418 base::android::ConvertJavaStringToUTF8(env, jconfig); | 515 base::android::ConvertJavaStringToUTF8(env, jconfig); |
419 scoped_ptr<URLRequestContextConfig> context_config( | 516 scoped_ptr<URLRequestContextConfig> context_config( |
420 new URLRequestContextConfig()); | 517 new URLRequestContextConfig()); |
421 if (!context_config->LoadFromJSON(config_string)) | 518 if (!context_config->LoadFromJSON(config_string)) |
422 return 0; | 519 return 0; |
423 | 520 |
424 CronetURLRequestContextAdapter* context_adapter = | 521 CronetURLRequestContextAdapter* context_adapter = |
425 new CronetURLRequestContextAdapter(context_config.Pass()); | 522 new CronetURLRequestContextAdapter(context_config.Pass()); |
426 return reinterpret_cast<jlong>(context_adapter); | 523 return reinterpret_cast<jlong>(context_adapter); |
427 } | 524 } |
428 | 525 |
429 static jint SetMinLogLevel(JNIEnv* env, | 526 static jint SetMinLogLevel(JNIEnv* env, |
430 const JavaParamRef<jclass>& jcaller, | 527 const JavaParamRef<jclass>& jcaller, |
431 jint jlog_level) { | 528 jint jlog_level) { |
432 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 529 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
433 // MinLogLevel is global, shared by all URLRequestContexts. | 530 // MinLogLevel is global, shared by all URLRequestContexts. |
434 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 531 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
435 return old_log_level; | 532 return old_log_level; |
436 } | 533 } |
437 | 534 |
438 } // namespace cronet | 535 } // namespace cronet |
OLD | NEW |