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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 net::AlternativeService alternative_service( | 350 net::AlternativeService alternative_service( |
275 net::AlternateProtocol::QUIC, "", | 351 net::AlternateProtocol::QUIC, "", |
276 static_cast<uint16>(quic_hint.alternate_port)); | 352 static_cast<uint16>(quic_hint.alternate_port)); |
277 context_->http_server_properties()->SetAlternativeService( | 353 context_->http_server_properties()->SetAlternativeService( |
278 quic_hint_host_port_pair, alternative_service, 1.0f, | 354 quic_hint_host_port_pair, alternative_service, 1.0f, |
279 base::Time::Max()); | 355 base::Time::Max()); |
280 } | 356 } |
281 } | 357 } |
282 | 358 |
283 JNIEnv* env = base::android::AttachCurrentThread(); | 359 JNIEnv* env = base::android::AttachCurrentThread(); |
| 360 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); |
284 Java_CronetUrlRequestContext_initNetworkThread( | 361 Java_CronetUrlRequestContext_initNetworkThread( |
285 env, jcronet_url_request_context.obj()); | 362 env, jcronet_url_request_context.obj()); |
286 | 363 |
287 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 364 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
288 if (data_reduction_proxy_) | 365 if (data_reduction_proxy_) |
289 data_reduction_proxy_->Init(true, GetURLRequestContext()); | 366 data_reduction_proxy_->Init(true, GetURLRequestContext()); |
290 #endif | 367 #endif |
291 is_context_initialized_ = true; | 368 is_context_initialized_ = true; |
292 while (!tasks_waiting_for_context_.empty()) { | 369 while (!tasks_waiting_for_context_.empty()) { |
293 tasks_waiting_for_context_.front().Run(); | 370 tasks_waiting_for_context_.front().Run(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 | 469 |
393 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { | 470 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { |
394 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 471 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
395 if (!file_thread_) { | 472 if (!file_thread_) { |
396 file_thread_.reset(new base::Thread("Network File Thread")); | 473 file_thread_.reset(new base::Thread("Network File Thread")); |
397 file_thread_->Start(); | 474 file_thread_->Start(); |
398 } | 475 } |
399 return file_thread_.get(); | 476 return file_thread_.get(); |
400 } | 477 } |
401 | 478 |
| 479 void CronetURLRequestContextAdapter::OnRTTObservation( |
| 480 int32_t rtt_ms, |
| 481 const base::TimeTicks& timestamp, |
| 482 net::NetworkQualityEstimator::ObservationSource source) { |
| 483 Java_CronetUrlRequestContext_onRTTObservation( |
| 484 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), |
| 485 rtt_ms, (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), |
| 486 source); |
| 487 } |
| 488 |
| 489 void CronetURLRequestContextAdapter::OnThroughputObservation( |
| 490 int32_t throughput_kbps, |
| 491 const base::TimeTicks& timestamp, |
| 492 net::NetworkQualityEstimator::ObservationSource source) { |
| 493 Java_CronetUrlRequestContext_onThroughputObservation( |
| 494 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), |
| 495 throughput_kbps, |
| 496 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), source); |
| 497 } |
| 498 |
402 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | 499 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
403 // returns 0 otherwise. | 500 // returns 0 otherwise. |
404 static jlong CreateRequestContextAdapter(JNIEnv* env, | 501 static jlong CreateRequestContextAdapter(JNIEnv* env, |
405 jclass jcaller, | 502 jclass jcaller, |
406 jstring jconfig) { | 503 jstring jconfig) { |
407 std::string config_string = | 504 std::string config_string = |
408 base::android::ConvertJavaStringToUTF8(env, jconfig); | 505 base::android::ConvertJavaStringToUTF8(env, jconfig); |
409 scoped_ptr<URLRequestContextConfig> context_config( | 506 scoped_ptr<URLRequestContextConfig> context_config( |
410 new URLRequestContextConfig()); | 507 new URLRequestContextConfig()); |
411 if (!context_config->LoadFromJSON(config_string)) | 508 if (!context_config->LoadFromJSON(config_string)) |
412 return 0; | 509 return 0; |
413 | 510 |
414 CronetURLRequestContextAdapter* context_adapter = | 511 CronetURLRequestContextAdapter* context_adapter = |
415 new CronetURLRequestContextAdapter(context_config.Pass()); | 512 new CronetURLRequestContextAdapter(context_config.Pass()); |
416 return reinterpret_cast<jlong>(context_adapter); | 513 return reinterpret_cast<jlong>(context_adapter); |
417 } | 514 } |
418 | 515 |
419 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { | 516 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { |
420 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 517 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
421 // MinLogLevel is global, shared by all URLRequestContexts. | 518 // MinLogLevel is global, shared by all URLRequestContexts. |
422 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 519 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
423 return old_log_level; | 520 return old_log_level; |
424 } | 521 } |
425 | 522 |
426 } // namespace cronet | 523 } // namespace cronet |
OLD | NEW |