| 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 <limits.h> | 7 #include <limits.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "base/metrics/histogram_macros.h" | 31 #include "base/metrics/histogram_macros.h" |
| 32 #include "base/metrics/statistics_recorder.h" | 32 #include "base/metrics/statistics_recorder.h" |
| 33 #include "base/single_thread_task_runner.h" | 33 #include "base/single_thread_task_runner.h" |
| 34 #include "base/threading/thread_task_runner_handle.h" | 34 #include "base/threading/thread_task_runner_handle.h" |
| 35 #include "base/time/time.h" | 35 #include "base/time/time.h" |
| 36 #include "base/values.h" | 36 #include "base/values.h" |
| 37 #include "components/cronet/android/cert/cert_verifier_cache_serializer.h" | 37 #include "components/cronet/android/cert/cert_verifier_cache_serializer.h" |
| 38 #include "components/cronet/android/cert/proto/cert_verification.pb.h" | 38 #include "components/cronet/android/cert/proto/cert_verification.pb.h" |
| 39 #include "components/cronet/android/cronet_library_loader.h" | 39 #include "components/cronet/android/cronet_library_loader.h" |
| 40 #include "components/cronet/histogram_manager.h" | 40 #include "components/cronet/histogram_manager.h" |
| 41 #include "components/cronet/host_cache_persistence_manager.h" |
| 41 #include "components/cronet/url_request_context_config.h" | 42 #include "components/cronet/url_request_context_config.h" |
| 42 #include "components/prefs/pref_change_registrar.h" | 43 #include "components/prefs/pref_change_registrar.h" |
| 43 #include "components/prefs/pref_filter.h" | 44 #include "components/prefs/pref_filter.h" |
| 44 #include "components/prefs/pref_registry.h" | 45 #include "components/prefs/pref_registry.h" |
| 45 #include "components/prefs/pref_registry_simple.h" | 46 #include "components/prefs/pref_registry_simple.h" |
| 46 #include "components/prefs/pref_service.h" | 47 #include "components/prefs/pref_service.h" |
| 47 #include "components/prefs/pref_service_factory.h" | 48 #include "components/prefs/pref_service_factory.h" |
| 48 #include "jni/CronetUrlRequestContext_jni.h" | 49 #include "jni/CronetUrlRequestContext_jni.h" |
| 49 #include "net/base/load_flags.h" | 50 #include "net/base/load_flags.h" |
| 50 #include "net/base/logging_network_change_observer.h" | 51 #include "net/base/logging_network_change_observer.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 // so network change event are logged just once in the NetLog. | 108 // so network change event are logged just once in the NetLog. |
| 108 std::unique_ptr<net::LoggingNetworkChangeObserver> net_change_logger_; | 109 std::unique_ptr<net::LoggingNetworkChangeObserver> net_change_logger_; |
| 109 | 110 |
| 110 DISALLOW_COPY_AND_ASSIGN(NetLogWithNetworkChangeEvents); | 111 DISALLOW_COPY_AND_ASSIGN(NetLogWithNetworkChangeEvents); |
| 111 }; | 112 }; |
| 112 | 113 |
| 113 // Use a global NetLog instance. See crbug.com/486120. | 114 // Use a global NetLog instance. See crbug.com/486120. |
| 114 static base::LazyInstance<NetLogWithNetworkChangeEvents>::Leaky g_net_log = | 115 static base::LazyInstance<NetLogWithNetworkChangeEvents>::Leaky g_net_log = |
| 115 LAZY_INSTANCE_INITIALIZER; | 116 LAZY_INSTANCE_INITIALIZER; |
| 116 | 117 |
| 117 const char kHttpServerProperties[] = "net.http_server_properties"; | 118 // Name of the pref used for host cache persistence. |
| 118 const char kNetworkQualities[] = "net.network_qualities"; | 119 const char kHostCachePref[] = "net.host_cache"; |
| 120 // Name of the pref used for HTTP server properties persistence. |
| 121 const char kHttpServerPropertiesPref[] = "net.http_server_properties"; |
| 122 // Name of the pref used for NQE persistence. |
| 123 const char kNetworkQualitiesPref[] = "net.network_qualities"; |
| 119 // Current version of disk storage. | 124 // Current version of disk storage. |
| 120 const int32_t kStorageVersion = 1; | 125 const int32_t kStorageVersion = 1; |
| 121 // Version number used when the version of disk storage is unknown. | 126 // Version number used when the version of disk storage is unknown. |
| 122 const uint32_t kStorageVersionUnknown = 0; | 127 const uint32_t kStorageVersionUnknown = 0; |
| 123 // Name of preference directory. | 128 // Name of preference directory. |
| 124 const char kPrefsDirectoryName[] = "prefs"; | 129 const char kPrefsDirectoryName[] = "prefs"; |
| 125 // Name of preference file. | 130 // Name of preference file. |
| 126 const char kPrefsFileName[] = "local_prefs.json"; | 131 const char kPrefsFileName[] = "local_prefs.json"; |
| 127 | 132 |
| 128 // Connects the HttpServerPropertiesManager's storage to the prefs. | 133 // Connects the HttpServerPropertiesManager's storage to the prefs. |
| 129 class PrefServiceAdapter | 134 class PrefServiceAdapter |
| 130 : public net::HttpServerPropertiesManager::PrefDelegate { | 135 : public net::HttpServerPropertiesManager::PrefDelegate { |
| 131 public: | 136 public: |
| 132 explicit PrefServiceAdapter(PrefService* pref_service) | 137 explicit PrefServiceAdapter(PrefService* pref_service) |
| 133 : pref_service_(pref_service), path_(kHttpServerProperties) { | 138 : pref_service_(pref_service), path_(kHttpServerPropertiesPref) { |
| 134 pref_change_registrar_.Init(pref_service_); | 139 pref_change_registrar_.Init(pref_service_); |
| 135 } | 140 } |
| 136 | 141 |
| 137 ~PrefServiceAdapter() override {} | 142 ~PrefServiceAdapter() override {} |
| 138 | 143 |
| 139 // PrefDelegate implementation. | 144 // PrefDelegate implementation. |
| 140 bool HasServerProperties() override { | 145 bool HasServerProperties() override { |
| 141 return pref_service_->HasPrefPath(path_); | 146 return pref_service_->HasPrefPath(path_); |
| 142 } | 147 } |
| 143 const base::DictionaryValue& GetServerProperties() const override { | 148 const base::DictionaryValue& GetServerProperties() const override { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 173 weak_ptr_factory_(this) { | 178 weak_ptr_factory_(this) { |
| 174 DCHECK(pref_service_); | 179 DCHECK(pref_service_); |
| 175 } | 180 } |
| 176 | 181 |
| 177 ~NetworkQualitiesPrefDelegateImpl() override {} | 182 ~NetworkQualitiesPrefDelegateImpl() override {} |
| 178 | 183 |
| 179 // net::NetworkQualitiesPrefsManager::PrefDelegate implementation. | 184 // net::NetworkQualitiesPrefsManager::PrefDelegate implementation. |
| 180 void SetDictionaryValue(const base::DictionaryValue& value) override { | 185 void SetDictionaryValue(const base::DictionaryValue& value) override { |
| 181 DCHECK(thread_checker_.CalledOnValidThread()); | 186 DCHECK(thread_checker_.CalledOnValidThread()); |
| 182 | 187 |
| 183 pref_service_->Set(kNetworkQualities, value); | 188 pref_service_->Set(kNetworkQualitiesPref, value); |
| 184 if (lossy_prefs_writing_task_posted_) | 189 if (lossy_prefs_writing_task_posted_) |
| 185 return; | 190 return; |
| 186 | 191 |
| 187 // Post the task that schedules the writing of the lossy prefs. | 192 // Post the task that schedules the writing of the lossy prefs. |
| 188 lossy_prefs_writing_task_posted_ = true; | 193 lossy_prefs_writing_task_posted_ = true; |
| 189 | 194 |
| 190 // Delay after which the task that schedules the writing of the lossy prefs. | 195 // Delay after which the task that schedules the writing of the lossy prefs. |
| 191 // This is needed in case the writing of the lossy prefs is not scheduled | 196 // This is needed in case the writing of the lossy prefs is not scheduled |
| 192 // automatically. The delay was chosen so that it is large enough that it | 197 // automatically. The delay was chosen so that it is large enough that it |
| 193 // does not affect the startup performance. | 198 // does not affect the startup performance. |
| 194 static const int32_t kUpdatePrefsDelaySeconds = 10; | 199 static const int32_t kUpdatePrefsDelaySeconds = 10; |
| 195 | 200 |
| 196 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 201 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 197 FROM_HERE, | 202 FROM_HERE, |
| 198 base::Bind( | 203 base::Bind( |
| 199 &NetworkQualitiesPrefDelegateImpl::SchedulePendingLossyWrites, | 204 &NetworkQualitiesPrefDelegateImpl::SchedulePendingLossyWrites, |
| 200 weak_ptr_factory_.GetWeakPtr()), | 205 weak_ptr_factory_.GetWeakPtr()), |
| 201 base::TimeDelta::FromSeconds(kUpdatePrefsDelaySeconds)); | 206 base::TimeDelta::FromSeconds(kUpdatePrefsDelaySeconds)); |
| 202 } | 207 } |
| 203 std::unique_ptr<base::DictionaryValue> GetDictionaryValue() override { | 208 std::unique_ptr<base::DictionaryValue> GetDictionaryValue() override { |
| 204 DCHECK(thread_checker_.CalledOnValidThread()); | 209 DCHECK(thread_checker_.CalledOnValidThread()); |
| 205 UMA_HISTOGRAM_EXACT_LINEAR("NQE.Prefs.ReadCount", 1, 2); | 210 UMA_HISTOGRAM_EXACT_LINEAR("NQE.Prefs.ReadCount", 1, 2); |
| 206 return pref_service_->GetDictionary(kNetworkQualities)->CreateDeepCopy(); | 211 return pref_service_->GetDictionary(kNetworkQualitiesPref) |
| 212 ->CreateDeepCopy(); |
| 207 } | 213 } |
| 208 | 214 |
| 209 private: | 215 private: |
| 210 // Schedules the writing of the lossy prefs. | 216 // Schedules the writing of the lossy prefs. |
| 211 void SchedulePendingLossyWrites() { | 217 void SchedulePendingLossyWrites() { |
| 212 DCHECK(thread_checker_.CalledOnValidThread()); | 218 DCHECK(thread_checker_.CalledOnValidThread()); |
| 213 UMA_HISTOGRAM_EXACT_LINEAR("NQE.Prefs.WriteCount", 1, 2); | 219 UMA_HISTOGRAM_EXACT_LINEAR("NQE.Prefs.WriteCount", 1, 2); |
| 214 pref_service_->SchedulePendingLossyWrites(); | 220 pref_service_->SchedulePendingLossyWrites(); |
| 215 lossy_prefs_writing_task_posted_ = false; | 221 lossy_prefs_writing_task_posted_ = false; |
| 216 } | 222 } |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 // Make sure storage directory has correct version. | 642 // Make sure storage directory has correct version. |
| 637 InitializeStorageDirectory(storage_path); | 643 InitializeStorageDirectory(storage_path); |
| 638 base::FilePath filepath = | 644 base::FilePath filepath = |
| 639 storage_path.Append(FILE_PATH_LITERAL(kPrefsDirectoryName)) | 645 storage_path.Append(FILE_PATH_LITERAL(kPrefsDirectoryName)) |
| 640 .Append(FILE_PATH_LITERAL(kPrefsFileName)); | 646 .Append(FILE_PATH_LITERAL(kPrefsFileName)); |
| 641 json_pref_store_ = | 647 json_pref_store_ = |
| 642 new JsonPrefStore(filepath, GetFileThread()->task_runner(), | 648 new JsonPrefStore(filepath, GetFileThread()->task_runner(), |
| 643 std::unique_ptr<PrefFilter>()); | 649 std::unique_ptr<PrefFilter>()); |
| 644 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); | 650 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); |
| 645 | 651 |
| 646 // Set up HttpServerPropertiesManager. | 652 // Register prefs and set up the PrefService. |
| 647 PrefServiceFactory factory; | 653 PrefServiceFactory factory; |
| 648 factory.set_user_prefs(json_pref_store_); | 654 factory.set_user_prefs(json_pref_store_); |
| 649 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); | 655 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); |
| 650 registry->RegisterDictionaryPref(kHttpServerProperties, | 656 registry->RegisterDictionaryPref(kHttpServerPropertiesPref, |
| 651 base::MakeUnique<base::DictionaryValue>()); | 657 base::MakeUnique<base::DictionaryValue>()); |
| 652 if (config->enable_network_quality_estimator) { | 658 if (config->enable_network_quality_estimator) { |
| 653 // Use lossy prefs to limit the overhead of reading/writing the prefs. | 659 // Use lossy prefs to limit the overhead of reading/writing the prefs. |
| 654 registry->RegisterDictionaryPref(kNetworkQualities, | 660 registry->RegisterDictionaryPref(kNetworkQualitiesPref, |
| 655 PrefRegistry::LOSSY_PREF); | 661 PrefRegistry::LOSSY_PREF); |
| 656 } | 662 } |
| 663 if (config->enable_host_cache_persistence) { |
| 664 registry->RegisterListPref(kHostCachePref); |
| 665 } |
| 657 pref_service_ = factory.Create(registry.get()); | 666 pref_service_ = factory.Create(registry.get()); |
| 658 | 667 |
| 668 // Set up the HttpServerPropertiesManager. |
| 659 std::unique_ptr<net::HttpServerPropertiesManager> | 669 std::unique_ptr<net::HttpServerPropertiesManager> |
| 660 http_server_properties_manager(new net::HttpServerPropertiesManager( | 670 http_server_properties_manager(new net::HttpServerPropertiesManager( |
| 661 new PrefServiceAdapter(pref_service_.get()), | 671 new PrefServiceAdapter(pref_service_.get()), |
| 662 base::ThreadTaskRunnerHandle::Get(), GetNetworkTaskRunner(), | 672 base::ThreadTaskRunnerHandle::Get(), GetNetworkTaskRunner(), |
| 663 g_net_log.Get().net_log())); | 673 g_net_log.Get().net_log())); |
| 664 http_server_properties_manager->InitializeOnNetworkSequence(); | 674 http_server_properties_manager->InitializeOnNetworkSequence(); |
| 665 http_server_properties_manager_ = http_server_properties_manager.get(); | 675 http_server_properties_manager_ = http_server_properties_manager.get(); |
| 666 context_builder.SetHttpServerProperties( | 676 context_builder.SetHttpServerProperties( |
| 667 std::move(http_server_properties_manager)); | 677 std::move(http_server_properties_manager)); |
| 668 } | 678 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 pref_service_.get())); | 712 pref_service_.get())); |
| 703 network_qualities_prefs_manager_->InitializeOnNetworkThread( | 713 network_qualities_prefs_manager_->InitializeOnNetworkThread( |
| 704 network_quality_estimator_.get()); | 714 network_quality_estimator_.get()); |
| 705 } | 715 } |
| 706 context_builder.set_network_quality_estimator( | 716 context_builder.set_network_quality_estimator( |
| 707 network_quality_estimator_.get()); | 717 network_quality_estimator_.get()); |
| 708 } | 718 } |
| 709 | 719 |
| 710 context_ = context_builder.Build(); | 720 context_ = context_builder.Build(); |
| 711 | 721 |
| 722 // Set up host cache persistence if it's enabled. Happens after building the |
| 723 // URLRequestContext to get access to the HostCache. |
| 724 if (pref_service_ && config->enable_host_cache_persistence) { |
| 725 net::HostCache* host_cache = context_->host_resolver()->GetHostCache(); |
| 726 host_cache_persistence_manager_ = |
| 727 base::MakeUnique<HostCachePersistenceManager>( |
| 728 host_cache, pref_service_.get(), kHostCachePref, |
| 729 base::TimeDelta::FromMilliseconds( |
| 730 config->host_cache_persistence_delay_ms)); |
| 731 } |
| 732 |
| 712 context_->set_check_cleartext_permitted(true); | 733 context_->set_check_cleartext_permitted(true); |
| 713 context_->set_enable_brotli(config->enable_brotli); | 734 context_->set_enable_brotli(config->enable_brotli); |
| 714 | 735 |
| 715 if (config->load_disable_cache) | 736 if (config->load_disable_cache) |
| 716 default_load_flags_ |= net::LOAD_DISABLE_CACHE; | 737 default_load_flags_ |= net::LOAD_DISABLE_CACHE; |
| 717 | 738 |
| 718 if (config->enable_sdch) { | 739 if (config->enable_sdch) { |
| 719 DCHECK(context_->sdch_manager()); | 740 DCHECK(context_->sdch_manager()); |
| 720 sdch_owner_.reset( | 741 sdch_owner_.reset( |
| 721 new net::SdchOwner(context_->sdch_manager(), context_.get())); | 742 new net::SdchOwner(context_->sdch_manager(), context_.get())); |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1171 JNIEnv* env, | 1192 JNIEnv* env, |
| 1172 const JavaParamRef<jclass>& jcaller) { | 1193 const JavaParamRef<jclass>& jcaller) { |
| 1173 DCHECK(base::StatisticsRecorder::IsActive()); | 1194 DCHECK(base::StatisticsRecorder::IsActive()); |
| 1174 std::vector<uint8_t> data; | 1195 std::vector<uint8_t> data; |
| 1175 if (!HistogramManager::GetInstance()->GetDeltas(&data)) | 1196 if (!HistogramManager::GetInstance()->GetDeltas(&data)) |
| 1176 return ScopedJavaLocalRef<jbyteArray>(); | 1197 return ScopedJavaLocalRef<jbyteArray>(); |
| 1177 return base::android::ToJavaByteArray(env, &data[0], data.size()); | 1198 return base::android::ToJavaByteArray(env, &data[0], data.size()); |
| 1178 } | 1199 } |
| 1179 | 1200 |
| 1180 } // namespace cronet | 1201 } // namespace cronet |
| OLD | NEW |