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/url_request_context_config.h" | 5 #include "components/cronet/url_request_context_config.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 // TODO(xunjieli): Refactor constants in io_thread.cc. | 22 // TODO(xunjieli): Refactor constants in io_thread.cc. |
23 const char kQuicFieldTrialName[] = "QUIC"; | 23 const char kQuicFieldTrialName[] = "QUIC"; |
24 const char kQuicConnectionOptions[] = "connection_options"; | 24 const char kQuicConnectionOptions[] = "connection_options"; |
25 const char kQuicStoreServerConfigsInProperties[] = | 25 const char kQuicStoreServerConfigsInProperties[] = |
26 "store_server_configs_in_properties"; | 26 "store_server_configs_in_properties"; |
27 const char kQuicDelayTcpRace[] = "delay_tcp_race"; | 27 const char kQuicDelayTcpRace[] = "delay_tcp_race"; |
28 const char kQuicMaxNumberOfLossyConnections[] = | 28 const char kQuicMaxNumberOfLossyConnections[] = |
29 "max_number_of_lossy_connections"; | 29 "max_number_of_lossy_connections"; |
30 const char kQuicPacketLossThreshold[] = "packet_loss_threshold"; | 30 const char kQuicPacketLossThreshold[] = "packet_loss_threshold"; |
31 | 31 |
| 32 // Using a reference to scoped_ptr is unavoidable because of the semantics of |
| 33 // RegisterCustomField. |
| 34 // TODO(xunjieli): Remove this once crbug.com/544976 is fixed. |
| 35 bool GetMockCertVerifierFromString( |
| 36 const base::StringPiece& mock_cert_verifier_string, |
| 37 scoped_ptr<net::CertVerifier>* result) { |
| 38 int64 val; |
| 39 bool success = base::StringToInt64(mock_cert_verifier_string, &val); |
| 40 *result = make_scoped_ptr(reinterpret_cast<net::CertVerifier*>(val)); |
| 41 return success; |
| 42 } |
| 43 |
32 void ParseAndSetExperimentalOptions( | 44 void ParseAndSetExperimentalOptions( |
33 const std::string& experimental_options, | 45 const std::string& experimental_options, |
34 net::URLRequestContextBuilder* context_builder) { | 46 net::URLRequestContextBuilder* context_builder) { |
35 if (experimental_options.empty()) | 47 if (experimental_options.empty()) |
36 return; | 48 return; |
37 | 49 |
38 DVLOG(1) << "Experimental Options:" << experimental_options; | 50 DVLOG(1) << "Experimental Options:" << experimental_options; |
39 scoped_ptr<base::Value> options = | 51 scoped_ptr<base::Value> options = |
40 base::JSONReader::Read(experimental_options); | 52 base::JSONReader::Read(experimental_options); |
41 | 53 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 96 |
85 double quic_packet_loss_threshold = 0.0; | 97 double quic_packet_loss_threshold = 0.0; |
86 if (quic_args->GetDouble(kQuicPacketLossThreshold, | 98 if (quic_args->GetDouble(kQuicPacketLossThreshold, |
87 &quic_packet_loss_threshold)) { | 99 &quic_packet_loss_threshold)) { |
88 context_builder->set_quic_packet_loss_threshold( | 100 context_builder->set_quic_packet_loss_threshold( |
89 quic_packet_loss_threshold); | 101 quic_packet_loss_threshold); |
90 } | 102 } |
91 } | 103 } |
92 } | 104 } |
93 | 105 |
| 106 bool GetTimeFromDouble(const base::Value* json_value, base::Time* time) { |
| 107 double time_double; |
| 108 bool success = json_value->GetAsDouble(&time_double); |
| 109 if (success) { |
| 110 *time = base::Time::FromDoubleT(time_double); |
| 111 } |
| 112 return success; |
| 113 } |
| 114 |
94 } // namespace | 115 } // namespace |
95 | 116 |
96 URLRequestContextConfig::QuicHint::QuicHint(const std::string& host, | 117 #define DEFINE_CONTEXT_CONFIG(x) const char REQUEST_CONTEXT_CONFIG_##x[] = #x; |
97 int port, | 118 #include "components/cronet/url_request_context_config_list.h" |
98 int alternate_port) | 119 #undef DEFINE_CONTEXT_CONFIG |
99 : host(host), port(port), alternate_port(alternate_port) {} | 120 |
| 121 URLRequestContextConfig::QuicHint::QuicHint() {} |
100 | 122 |
101 URLRequestContextConfig::QuicHint::~QuicHint() {} | 123 URLRequestContextConfig::QuicHint::~QuicHint() {} |
102 | 124 |
103 URLRequestContextConfig::Pkp::Pkp(const std::string& host, | 125 // static |
104 bool include_subdomains, | 126 void URLRequestContextConfig::QuicHint::RegisterJSONConverter( |
105 const base::Time& expiration_date) | 127 base::JSONValueConverter<URLRequestContextConfig::QuicHint>* converter) { |
106 : host(host), | 128 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_QUIC_HINT_HOST, |
107 include_subdomains(include_subdomains), | 129 &URLRequestContextConfig::QuicHint::host); |
108 expiration_date(expiration_date) {} | 130 converter->RegisterIntField( |
| 131 REQUEST_CONTEXT_CONFIG_QUIC_HINT_PORT, |
| 132 &URLRequestContextConfig::QuicHint::port); |
| 133 converter->RegisterIntField( |
| 134 REQUEST_CONTEXT_CONFIG_QUIC_HINT_ALT_PORT, |
| 135 &URLRequestContextConfig::QuicHint::alternate_port); |
| 136 } |
| 137 |
| 138 URLRequestContextConfig::Pkp::Pkp() {} |
109 | 139 |
110 URLRequestContextConfig::Pkp::~Pkp() {} | 140 URLRequestContextConfig::Pkp::~Pkp() {} |
111 | 141 |
112 URLRequestContextConfig::URLRequestContextConfig( | 142 // static |
113 bool enable_quic, | 143 void URLRequestContextConfig::Pkp::RegisterJSONConverter( |
114 bool enable_spdy, | 144 base::JSONValueConverter<URLRequestContextConfig::Pkp>* converter) { |
115 bool enable_sdch, | 145 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_PKP_HOST, |
116 HttpCacheType http_cache, | 146 &URLRequestContextConfig::Pkp::host); |
117 int http_cache_max_size, | 147 converter->RegisterRepeatedString(REQUEST_CONTEXT_CONFIG_PKP_PIN_HASHES, |
118 bool load_disable_cache, | 148 &URLRequestContextConfig::Pkp::pin_hashes); |
119 const std::string& storage_path, | 149 converter->RegisterBoolField( |
120 const std::string& user_agent, | 150 REQUEST_CONTEXT_CONFIG_PKP_INCLUDE_SUBDOMAINS, |
121 const std::string& experimental_options, | 151 &URLRequestContextConfig::Pkp::include_subdomains); |
122 const std::string& data_reduction_proxy_key, | 152 converter->RegisterCustomValueField<base::Time>( |
123 const std::string& data_reduction_primary_proxy, | 153 REQUEST_CONTEXT_CONFIG_PKP_EXPIRATION_DATE, |
124 const std::string& data_reduction_fallback_proxy, | 154 &URLRequestContextConfig::Pkp::expiration_date, &GetTimeFromDouble); |
125 const std::string& data_reduction_secure_proxy_check_url, | 155 } |
126 scoped_ptr<net::CertVerifier> mock_cert_verifier) | 156 |
127 : enable_quic(enable_quic), | 157 URLRequestContextConfig::URLRequestContextConfig() {} |
128 enable_spdy(enable_spdy), | |
129 enable_sdch(enable_sdch), | |
130 http_cache(http_cache), | |
131 http_cache_max_size(http_cache_max_size), | |
132 load_disable_cache(load_disable_cache), | |
133 storage_path(storage_path), | |
134 user_agent(user_agent), | |
135 experimental_options(experimental_options), | |
136 data_reduction_proxy_key(data_reduction_proxy_key), | |
137 data_reduction_primary_proxy(data_reduction_primary_proxy), | |
138 data_reduction_fallback_proxy(data_reduction_fallback_proxy), | |
139 data_reduction_secure_proxy_check_url( | |
140 data_reduction_secure_proxy_check_url), | |
141 mock_cert_verifier(std::move(mock_cert_verifier)) {} | |
142 | 158 |
143 URLRequestContextConfig::~URLRequestContextConfig() {} | 159 URLRequestContextConfig::~URLRequestContextConfig() {} |
144 | 160 |
| 161 bool URLRequestContextConfig::LoadFromJSON(const std::string& config_string) { |
| 162 scoped_ptr<base::Value> config_value = base::JSONReader::Read(config_string); |
| 163 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 164 DLOG(ERROR) << "Bad JSON: " << config_string; |
| 165 return false; |
| 166 } |
| 167 |
| 168 base::JSONValueConverter<URLRequestContextConfig> converter; |
| 169 if (!converter.Convert(*config_value, this)) { |
| 170 DLOG(ERROR) << "Bad Config: " << config_value; |
| 171 return false; |
| 172 } |
| 173 return true; |
| 174 } |
| 175 |
145 void URLRequestContextConfig::ConfigureURLRequestContextBuilder( | 176 void URLRequestContextConfig::ConfigureURLRequestContextBuilder( |
146 net::URLRequestContextBuilder* context_builder) { | 177 net::URLRequestContextBuilder* context_builder) { |
147 std::string config_cache; | 178 std::string config_cache; |
148 if (http_cache != DISABLED) { | 179 if (http_cache != REQUEST_CONTEXT_CONFIG_HTTP_CACHE_DISABLED) { |
149 net::URLRequestContextBuilder::HttpCacheParams cache_params; | 180 net::URLRequestContextBuilder::HttpCacheParams cache_params; |
150 if (http_cache == DISK && !storage_path.empty()) { | 181 if (http_cache == REQUEST_CONTEXT_CONFIG_HTTP_CACHE_DISK && |
| 182 !storage_path.empty()) { |
151 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; | 183 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; |
152 cache_params.path = base::FilePath(storage_path); | 184 cache_params.path = base::FilePath(storage_path); |
153 } else { | 185 } else { |
154 cache_params.type = | 186 cache_params.type = |
155 net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; | 187 net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; |
156 } | 188 } |
157 cache_params.max_size = http_cache_max_size; | 189 cache_params.max_size = http_cache_max_size; |
158 context_builder->EnableHttpCache(cache_params); | 190 context_builder->EnableHttpCache(cache_params); |
159 } else { | 191 } else { |
160 context_builder->DisableHttpCache(); | 192 context_builder->DisableHttpCache(); |
161 } | 193 } |
162 context_builder->set_user_agent(user_agent); | 194 context_builder->set_user_agent(user_agent); |
163 context_builder->SetSpdyAndQuicEnabled(enable_spdy, enable_quic); | 195 context_builder->SetSpdyAndQuicEnabled(enable_spdy, enable_quic); |
164 context_builder->set_sdch_enabled(enable_sdch); | 196 context_builder->set_sdch_enabled(enable_sdch); |
165 | 197 |
166 ParseAndSetExperimentalOptions(experimental_options, context_builder); | 198 ParseAndSetExperimentalOptions(experimental_options, context_builder); |
167 | 199 |
168 if (mock_cert_verifier) | 200 if (mock_cert_verifier) |
169 context_builder->SetCertVerifier(mock_cert_verifier.Pass()); | 201 context_builder->SetCertVerifier(mock_cert_verifier.Pass()); |
170 // TODO(mef): Use |config| to set cookies. | 202 // TODO(mef): Use |config| to set cookies. |
171 } | 203 } |
172 | 204 |
| 205 // static |
| 206 void URLRequestContextConfig::RegisterJSONConverter( |
| 207 base::JSONValueConverter<URLRequestContextConfig>* converter) { |
| 208 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_USER_AGENT, |
| 209 &URLRequestContextConfig::user_agent); |
| 210 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_STORAGE_PATH, |
| 211 &URLRequestContextConfig::storage_path); |
| 212 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_ENABLE_QUIC, |
| 213 &URLRequestContextConfig::enable_quic); |
| 214 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_ENABLE_SPDY, |
| 215 &URLRequestContextConfig::enable_spdy); |
| 216 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_ENABLE_SDCH, |
| 217 &URLRequestContextConfig::enable_sdch); |
| 218 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_HTTP_CACHE, |
| 219 &URLRequestContextConfig::http_cache); |
| 220 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_LOAD_DISABLE_CACHE, |
| 221 &URLRequestContextConfig::load_disable_cache); |
| 222 converter->RegisterIntField(REQUEST_CONTEXT_CONFIG_HTTP_CACHE_MAX_SIZE, |
| 223 &URLRequestContextConfig::http_cache_max_size); |
| 224 converter->RegisterRepeatedMessage(REQUEST_CONTEXT_CONFIG_QUIC_HINTS, |
| 225 &URLRequestContextConfig::quic_hints); |
| 226 converter->RegisterStringField( |
| 227 REQUEST_CONTEXT_CONFIG_EXPERIMENTAL_OPTIONS, |
| 228 &URLRequestContextConfig::experimental_options); |
| 229 converter->RegisterStringField( |
| 230 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_PRIMARY_PROXY, |
| 231 &URLRequestContextConfig::data_reduction_primary_proxy); |
| 232 converter->RegisterStringField( |
| 233 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_FALLBACK_PROXY, |
| 234 &URLRequestContextConfig::data_reduction_fallback_proxy); |
| 235 converter->RegisterStringField( |
| 236 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_SECURE_PROXY_CHECK_URL, |
| 237 &URLRequestContextConfig::data_reduction_secure_proxy_check_url); |
| 238 converter->RegisterStringField( |
| 239 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_PROXY_KEY, |
| 240 &URLRequestContextConfig::data_reduction_proxy_key); |
| 241 converter->RegisterRepeatedMessage(REQUEST_CONTEXT_CONFIG_PKP_LIST, |
| 242 &URLRequestContextConfig::pkp_list); |
| 243 |
| 244 // For Testing. |
| 245 converter->RegisterCustomField<scoped_ptr<net::CertVerifier>>( |
| 246 REQUEST_CONTEXT_CONFIG_MOCK_CERT_VERIFIER, |
| 247 &URLRequestContextConfig::mock_cert_verifier, |
| 248 &GetMockCertVerifierFromString); |
| 249 } |
| 250 |
173 } // namespace cronet | 251 } // namespace cronet |
OLD | NEW |