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