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 network_quality_estimator_->Configure(use_local_host_requests, |
| 176 use_smaller_responses); |
| 177 } |
| 178 |
| 179 void CronetURLRequestContextAdapter::EnableNetworkQualityEstimator( |
| 180 JNIEnv* env, |
| 181 jobject jcaller, |
| 182 jboolean use_local_host_requests, |
| 183 jboolean use_smaller_responses) { |
| 184 PostTaskToNetworkThread( |
| 185 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: |
| 186 EnableNetworkQualityEstimatorOnNetworkThread, |
| 187 base::Unretained(this), use_local_host_requests, |
| 188 use_smaller_responses)); |
| 189 } |
| 190 |
| 191 void CronetURLRequestContextAdapter::ProvideRTTObservationsOnNetworkThread( |
| 192 bool should) { |
| 193 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 194 if (!network_quality_estimator_) |
| 195 return; |
| 196 if (should) |
| 197 network_quality_estimator_->AddRTTObserver(this); |
| 198 else |
| 199 network_quality_estimator_->RemoveRTTObserver(this); |
| 200 } |
| 201 |
| 202 void CronetURLRequestContextAdapter::ProvideRTTObservations(JNIEnv* env, |
| 203 jobject jcaller, |
| 204 bool should) { |
| 205 PostTaskToNetworkThread(FROM_HERE, |
| 206 base::Bind(&CronetURLRequestContextAdapter:: |
| 207 ProvideRTTObservationsOnNetworkThread, |
| 208 base::Unretained(this), should)); |
| 209 } |
| 210 |
| 211 void CronetURLRequestContextAdapter:: |
| 212 ProvideThroughputObservationsOnNetworkThread(bool should) { |
| 213 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 214 if (!network_quality_estimator_) |
| 215 return; |
| 216 if (should) |
| 217 network_quality_estimator_->AddThroughputObserver(this); |
| 218 else |
| 219 network_quality_estimator_->RemoveThroughputObserver(this); |
| 220 } |
| 221 |
| 222 void CronetURLRequestContextAdapter::ProvideThroughputObservations( |
| 223 JNIEnv* env, |
| 224 jobject jcaller, |
| 225 bool should) { |
| 226 PostTaskToNetworkThread( |
| 227 FROM_HERE, base::Bind(&CronetURLRequestContextAdapter:: |
| 228 ProvideThroughputObservationsOnNetworkThread, |
| 229 base::Unretained(this), should)); |
| 230 } |
| 231 |
159 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 232 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
160 scoped_ptr<URLRequestContextConfig> config, | 233 scoped_ptr<URLRequestContextConfig> config, |
161 const base::android::ScopedJavaGlobalRef<jobject>& | 234 const base::android::ScopedJavaGlobalRef<jobject>& |
162 jcronet_url_request_context) { | 235 jcronet_url_request_context) { |
163 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 236 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
164 DCHECK(!is_context_initialized_); | 237 DCHECK(!is_context_initialized_); |
165 DCHECK(proxy_config_service_); | 238 DCHECK(proxy_config_service_); |
166 // TODO(mmenke): Add method to have the builder enable SPDY. | 239 // TODO(mmenke): Add method to have the builder enable SPDY. |
167 net::URLRequestContextBuilder context_builder; | 240 net::URLRequestContextBuilder context_builder; |
168 | 241 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 net::AlternativeService alternative_service( | 347 net::AlternativeService alternative_service( |
275 net::AlternateProtocol::QUIC, "", | 348 net::AlternateProtocol::QUIC, "", |
276 static_cast<uint16>(quic_hint.alternate_port)); | 349 static_cast<uint16>(quic_hint.alternate_port)); |
277 context_->http_server_properties()->SetAlternativeService( | 350 context_->http_server_properties()->SetAlternativeService( |
278 quic_hint_host_port_pair, alternative_service, 1.0f, | 351 quic_hint_host_port_pair, alternative_service, 1.0f, |
279 base::Time::Max()); | 352 base::Time::Max()); |
280 } | 353 } |
281 } | 354 } |
282 | 355 |
283 JNIEnv* env = base::android::AttachCurrentThread(); | 356 JNIEnv* env = base::android::AttachCurrentThread(); |
| 357 jcronet_url_request_context_.Reset(env, jcronet_url_request_context.obj()); |
284 Java_CronetUrlRequestContext_initNetworkThread( | 358 Java_CronetUrlRequestContext_initNetworkThread( |
285 env, jcronet_url_request_context.obj()); | 359 env, jcronet_url_request_context.obj()); |
286 | 360 |
287 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 361 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
288 if (data_reduction_proxy_) | 362 if (data_reduction_proxy_) |
289 data_reduction_proxy_->Init(true, GetURLRequestContext()); | 363 data_reduction_proxy_->Init(true, GetURLRequestContext()); |
290 #endif | 364 #endif |
291 is_context_initialized_ = true; | 365 is_context_initialized_ = true; |
292 while (!tasks_waiting_for_context_.empty()) { | 366 while (!tasks_waiting_for_context_.empty()) { |
293 tasks_waiting_for_context_.front().Run(); | 367 tasks_waiting_for_context_.front().Run(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 | 466 |
393 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { | 467 base::Thread* CronetURLRequestContextAdapter::GetFileThread() { |
394 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 468 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
395 if (!file_thread_) { | 469 if (!file_thread_) { |
396 file_thread_.reset(new base::Thread("Network File Thread")); | 470 file_thread_.reset(new base::Thread("Network File Thread")); |
397 file_thread_->Start(); | 471 file_thread_->Start(); |
398 } | 472 } |
399 return file_thread_.get(); | 473 return file_thread_.get(); |
400 } | 474 } |
401 | 475 |
| 476 void CronetURLRequestContextAdapter::OnRTTObservation( |
| 477 int32_t rtt_ms, |
| 478 const base::TimeTicks& timestamp, |
| 479 net::NetworkQualityEstimator::ObservationSource source) { |
| 480 Java_CronetUrlRequestContext_onRTTObservation( |
| 481 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), |
| 482 rtt_ms, (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), |
| 483 source); |
| 484 } |
| 485 |
| 486 void CronetURLRequestContextAdapter::OnThroughputObservation( |
| 487 int32_t throughput_kbps, |
| 488 const base::TimeTicks& timestamp, |
| 489 net::NetworkQualityEstimator::ObservationSource source) { |
| 490 Java_CronetUrlRequestContext_onThroughputObservation( |
| 491 base::android::AttachCurrentThread(), jcronet_url_request_context_.obj(), |
| 492 throughput_kbps, |
| 493 (timestamp - base::TimeTicks::UnixEpoch()).InMilliseconds(), source); |
| 494 } |
| 495 |
402 // Creates RequestContextAdater if config is valid URLRequestContextConfig, | 496 // Creates RequestContextAdater if config is valid URLRequestContextConfig, |
403 // returns 0 otherwise. | 497 // returns 0 otherwise. |
404 static jlong CreateRequestContextAdapter(JNIEnv* env, | 498 static jlong CreateRequestContextAdapter(JNIEnv* env, |
405 jclass jcaller, | 499 jclass jcaller, |
406 jstring jconfig) { | 500 jstring jconfig) { |
407 std::string config_string = | 501 std::string config_string = |
408 base::android::ConvertJavaStringToUTF8(env, jconfig); | 502 base::android::ConvertJavaStringToUTF8(env, jconfig); |
409 scoped_ptr<URLRequestContextConfig> context_config( | 503 scoped_ptr<URLRequestContextConfig> context_config( |
410 new URLRequestContextConfig()); | 504 new URLRequestContextConfig()); |
411 if (!context_config->LoadFromJSON(config_string)) | 505 if (!context_config->LoadFromJSON(config_string)) |
412 return 0; | 506 return 0; |
413 | 507 |
414 CronetURLRequestContextAdapter* context_adapter = | 508 CronetURLRequestContextAdapter* context_adapter = |
415 new CronetURLRequestContextAdapter(context_config.Pass()); | 509 new CronetURLRequestContextAdapter(context_config.Pass()); |
416 return reinterpret_cast<jlong>(context_adapter); | 510 return reinterpret_cast<jlong>(context_adapter); |
417 } | 511 } |
418 | 512 |
419 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { | 513 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { |
420 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 514 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
421 // MinLogLevel is global, shared by all URLRequestContexts. | 515 // MinLogLevel is global, shared by all URLRequestContexts. |
422 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 516 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
423 return old_log_level; | 517 return old_log_level; |
424 } | 518 } |
425 | 519 |
426 } // namespace cronet | 520 } // namespace cronet |
OLD | NEW |