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" |
11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "net/cert/cert_verifier.h" | 13 #include "net/cert/cert_verifier.h" |
14 #include "net/quic/quic_protocol.h" | 14 #include "net/quic/quic_protocol.h" |
15 #include "net/quic/quic_utils.h" | 15 #include "net/quic/quic_utils.h" |
16 #include "net/url_request/url_request_context_builder.h" | 16 #include "net/url_request/url_request_context_builder.h" |
17 | 17 |
18 namespace cronet { | 18 namespace cronet { |
19 | 19 |
20 namespace { | 20 URLRequestContextConfig::QuicHint::QuicHint(const std::string& host, |
21 | 21 int port, |
22 // Using a reference to scoped_ptr is unavoidable because of the semantics of | 22 int alternate_port) |
23 // RegisterCustomField. | 23 : host(host), port(port), alternate_port(alternate_port) {} |
24 // TODO(xunjieli): Remove this once crbug.com/544976 is fixed. | |
25 bool GetMockCertVerifierFromString( | |
26 const base::StringPiece& mock_cert_verifier_string, | |
27 scoped_ptr<net::CertVerifier>* result) { | |
28 int64 val; | |
29 bool success = base::StringToInt64(mock_cert_verifier_string, &val); | |
30 *result = make_scoped_ptr(reinterpret_cast<net::CertVerifier*>(val)); | |
31 return success; | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 #define DEFINE_CONTEXT_CONFIG(x) const char REQUEST_CONTEXT_CONFIG_##x[] = #x; | |
37 #include "components/cronet/url_request_context_config_list.h" | |
38 #undef DEFINE_CONTEXT_CONFIG | |
39 | |
40 URLRequestContextConfig::QuicHint::QuicHint() { | |
41 } | |
42 | 24 |
43 URLRequestContextConfig::QuicHint::~QuicHint() { | 25 URLRequestContextConfig::QuicHint::~QuicHint() { |
44 } | 26 } |
45 | 27 |
46 // static | 28 URLRequestContextConfig::URLRequestContextConfig( |
47 void URLRequestContextConfig::QuicHint::RegisterJSONConverter( | 29 bool enable_quic, |
48 base::JSONValueConverter<URLRequestContextConfig::QuicHint>* converter) { | 30 bool enable_spdy, |
49 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_QUIC_HINT_HOST, | 31 bool enable_sdch, |
50 &URLRequestContextConfig::QuicHint::host); | 32 HttpCacheType http_cache, |
51 converter->RegisterIntField( | 33 int http_cache_max_size, |
52 REQUEST_CONTEXT_CONFIG_QUIC_HINT_PORT, | 34 bool load_disable_cache, |
53 &URLRequestContextConfig::QuicHint::port); | 35 const std::string& storage_path, |
54 converter->RegisterIntField( | 36 const std::string& user_agent, |
55 REQUEST_CONTEXT_CONFIG_QUIC_HINT_ALT_PORT, | 37 const std::string& quic_connection_options, |
56 &URLRequestContextConfig::QuicHint::alternate_port); | 38 const std::string& data_reduction_proxy_key, |
57 } | 39 const std::string& data_reduction_primary_proxy, |
58 | 40 const std::string& data_reduction_fallback_proxy, |
59 URLRequestContextConfig::URLRequestContextConfig() {} | 41 const std::string& data_reduction_secure_proxy_check_url, |
| 42 scoped_ptr<net::CertVerifier> mock_cert_verifier) |
| 43 : enable_quic(enable_quic), |
| 44 enable_spdy(enable_spdy), |
| 45 enable_sdch(enable_sdch), |
| 46 http_cache(http_cache), |
| 47 http_cache_max_size(http_cache_max_size), |
| 48 load_disable_cache(load_disable_cache), |
| 49 storage_path(storage_path), |
| 50 user_agent(user_agent), |
| 51 quic_connection_options(quic_connection_options), |
| 52 data_reduction_proxy_key(data_reduction_proxy_key), |
| 53 data_reduction_primary_proxy(data_reduction_primary_proxy), |
| 54 data_reduction_fallback_proxy(data_reduction_fallback_proxy), |
| 55 data_reduction_secure_proxy_check_url( |
| 56 data_reduction_secure_proxy_check_url), |
| 57 mock_cert_verifier(mock_cert_verifier.Pass()) {} |
60 | 58 |
61 URLRequestContextConfig::~URLRequestContextConfig() {} | 59 URLRequestContextConfig::~URLRequestContextConfig() {} |
62 | 60 |
63 bool URLRequestContextConfig::LoadFromJSON(const std::string& config_string) { | |
64 scoped_ptr<base::Value> config_value = base::JSONReader::Read(config_string); | |
65 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { | |
66 DLOG(ERROR) << "Bad JSON: " << config_string; | |
67 return false; | |
68 } | |
69 | |
70 base::JSONValueConverter<URLRequestContextConfig> converter; | |
71 if (!converter.Convert(*config_value, this)) { | |
72 DLOG(ERROR) << "Bad Config: " << config_value; | |
73 return false; | |
74 } | |
75 return true; | |
76 } | |
77 | |
78 void URLRequestContextConfig::ConfigureURLRequestContextBuilder( | 61 void URLRequestContextConfig::ConfigureURLRequestContextBuilder( |
79 net::URLRequestContextBuilder* context_builder) { | 62 net::URLRequestContextBuilder* context_builder) { |
80 std::string config_cache; | 63 std::string config_cache; |
81 if (http_cache != REQUEST_CONTEXT_CONFIG_HTTP_CACHE_DISABLED) { | 64 if (http_cache != DISABLED) { |
82 net::URLRequestContextBuilder::HttpCacheParams cache_params; | 65 net::URLRequestContextBuilder::HttpCacheParams cache_params; |
83 if (http_cache == REQUEST_CONTEXT_CONFIG_HTTP_CACHE_DISK && | 66 if (http_cache == DISK && !storage_path.empty()) { |
84 !storage_path.empty()) { | |
85 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; | 67 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK; |
86 cache_params.path = base::FilePath(storage_path); | 68 cache_params.path = base::FilePath(storage_path); |
87 } else { | 69 } else { |
88 cache_params.type = | 70 cache_params.type = |
89 net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; | 71 net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; |
90 } | 72 } |
91 cache_params.max_size = http_cache_max_size; | 73 cache_params.max_size = http_cache_max_size; |
92 context_builder->EnableHttpCache(cache_params); | 74 context_builder->EnableHttpCache(cache_params); |
93 } else { | 75 } else { |
94 context_builder->DisableHttpCache(); | 76 context_builder->DisableHttpCache(); |
95 } | 77 } |
96 context_builder->set_user_agent(user_agent); | 78 context_builder->set_user_agent(user_agent); |
97 context_builder->SetSpdyAndQuicEnabled(enable_spdy, enable_quic); | 79 context_builder->SetSpdyAndQuicEnabled(enable_spdy, enable_quic); |
98 context_builder->set_quic_connection_options( | 80 context_builder->set_quic_connection_options( |
99 net::QuicUtils::ParseQuicConnectionOptions(quic_connection_options)); | 81 net::QuicUtils::ParseQuicConnectionOptions(quic_connection_options)); |
100 context_builder->set_sdch_enabled(enable_sdch); | 82 context_builder->set_sdch_enabled(enable_sdch); |
101 if (mock_cert_verifier) | 83 if (mock_cert_verifier) |
102 context_builder->SetCertVerifier(mock_cert_verifier.Pass()); | 84 context_builder->SetCertVerifier(mock_cert_verifier.Pass()); |
103 // TODO(mef): Use |config| to set cookies. | 85 // TODO(mef): Use |config| to set cookies. |
104 } | 86 } |
105 | 87 |
106 // static | |
107 void URLRequestContextConfig::RegisterJSONConverter( | |
108 base::JSONValueConverter<URLRequestContextConfig>* converter) { | |
109 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_USER_AGENT, | |
110 &URLRequestContextConfig::user_agent); | |
111 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_STORAGE_PATH, | |
112 &URLRequestContextConfig::storage_path); | |
113 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_ENABLE_QUIC, | |
114 &URLRequestContextConfig::enable_quic); | |
115 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_ENABLE_SPDY, | |
116 &URLRequestContextConfig::enable_spdy); | |
117 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_ENABLE_SDCH, | |
118 &URLRequestContextConfig::enable_sdch); | |
119 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_HTTP_CACHE, | |
120 &URLRequestContextConfig::http_cache); | |
121 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_LOAD_DISABLE_CACHE, | |
122 &URLRequestContextConfig::load_disable_cache); | |
123 converter->RegisterIntField(REQUEST_CONTEXT_CONFIG_HTTP_CACHE_MAX_SIZE, | |
124 &URLRequestContextConfig::http_cache_max_size); | |
125 converter->RegisterRepeatedMessage(REQUEST_CONTEXT_CONFIG_QUIC_HINTS, | |
126 &URLRequestContextConfig::quic_hints); | |
127 converter->RegisterStringField( | |
128 REQUEST_CONTEXT_CONFIG_QUIC_OPTIONS, | |
129 &URLRequestContextConfig::quic_connection_options); | |
130 converter->RegisterStringField( | |
131 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_PRIMARY_PROXY, | |
132 &URLRequestContextConfig::data_reduction_primary_proxy); | |
133 converter->RegisterStringField( | |
134 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_FALLBACK_PROXY, | |
135 &URLRequestContextConfig::data_reduction_fallback_proxy); | |
136 converter->RegisterStringField( | |
137 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_SECURE_PROXY_CHECK_URL, | |
138 &URLRequestContextConfig::data_reduction_secure_proxy_check_url); | |
139 converter->RegisterStringField( | |
140 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_PROXY_KEY, | |
141 &URLRequestContextConfig::data_reduction_proxy_key); | |
142 | |
143 // For Testing. | |
144 converter->RegisterCustomField<scoped_ptr<net::CertVerifier>>( | |
145 REQUEST_CONTEXT_CONFIG_MOCK_CERT_VERIFIER, | |
146 &URLRequestContextConfig::mock_cert_verifier, | |
147 &GetMockCertVerifierFromString); | |
148 } | |
149 | |
150 } // namespace cronet | 88 } // namespace cronet |
OLD | NEW |