Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / cronet / url_request_context_config.cc
blobbfb8722767034a665d84d900017d0d628cd8fa59
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
3 // found in the LICENSE file.
5 #include "components/cronet/url_request_context_config.h"
7 #include "base/json/json_reader.h"
8 #include "base/values.h"
9 #include "net/quic/quic_protocol.h"
10 #include "net/quic/quic_utils.h"
11 #include "net/url_request/url_request_context_builder.h"
13 namespace cronet {
15 #define DEFINE_CONTEXT_CONFIG(x) const char REQUEST_CONTEXT_CONFIG_##x[] = #x;
16 #include "components/cronet/url_request_context_config_list.h"
17 #undef DEFINE_CONTEXT_CONFIG
19 URLRequestContextConfig::QuicHint::QuicHint() {
22 URLRequestContextConfig::QuicHint::~QuicHint() {
25 // static
26 void URLRequestContextConfig::QuicHint::RegisterJSONConverter(
27 base::JSONValueConverter<URLRequestContextConfig::QuicHint>* converter) {
28 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_QUIC_HINT_HOST,
29 &URLRequestContextConfig::QuicHint::host);
30 converter->RegisterIntField(
31 REQUEST_CONTEXT_CONFIG_QUIC_HINT_PORT,
32 &URLRequestContextConfig::QuicHint::port);
33 converter->RegisterIntField(
34 REQUEST_CONTEXT_CONFIG_QUIC_HINT_ALT_PORT,
35 &URLRequestContextConfig::QuicHint::alternate_port);
38 URLRequestContextConfig::URLRequestContextConfig() {
41 URLRequestContextConfig::~URLRequestContextConfig() {
44 bool URLRequestContextConfig::LoadFromJSON(const std::string& config_string) {
45 scoped_ptr<base::Value> config_value = base::JSONReader::Read(config_string);
46 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) {
47 DLOG(ERROR) << "Bad JSON: " << config_string;
48 return false;
51 base::JSONValueConverter<URLRequestContextConfig> converter;
52 if (!converter.Convert(*config_value, this)) {
53 DLOG(ERROR) << "Bad Config: " << config_value;
54 return false;
56 return true;
59 void URLRequestContextConfig::ConfigureURLRequestContextBuilder(
60 net::URLRequestContextBuilder* context_builder) {
61 std::string config_cache;
62 if (http_cache != REQUEST_CONTEXT_CONFIG_HTTP_CACHE_DISABLED) {
63 net::URLRequestContextBuilder::HttpCacheParams cache_params;
64 if (http_cache == REQUEST_CONTEXT_CONFIG_HTTP_CACHE_DISK &&
65 !storage_path.empty()) {
66 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK;
67 cache_params.path = base::FilePath(storage_path);
68 } else {
69 cache_params.type =
70 net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY;
72 cache_params.max_size = http_cache_max_size;
73 context_builder->EnableHttpCache(cache_params);
74 } else {
75 context_builder->DisableHttpCache();
77 context_builder->set_user_agent(user_agent);
78 context_builder->SetSpdyAndQuicEnabled(enable_spdy, enable_quic);
79 context_builder->set_quic_connection_options(
80 net::QuicUtils::ParseQuicConnectionOptions(quic_connection_options));
81 context_builder->set_sdch_enabled(enable_sdch);
82 #if defined(CRONET_TEST)
83 // Enable insecure quic only if Cronet is built for testing.
84 // TODO(xunjieli): Remove once crbug.com/514629 is fixed.
85 context_builder->set_enable_insecure_quic(true);
86 #endif
87 // TODO(mef): Use |config| to set cookies.
90 // static
91 void URLRequestContextConfig::RegisterJSONConverter(
92 base::JSONValueConverter<URLRequestContextConfig>* converter) {
93 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_USER_AGENT,
94 &URLRequestContextConfig::user_agent);
95 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_STORAGE_PATH,
96 &URLRequestContextConfig::storage_path);
97 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_ENABLE_QUIC,
98 &URLRequestContextConfig::enable_quic);
99 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_ENABLE_SPDY,
100 &URLRequestContextConfig::enable_spdy);
101 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_ENABLE_SDCH,
102 &URLRequestContextConfig::enable_sdch);
103 converter->RegisterStringField(REQUEST_CONTEXT_CONFIG_HTTP_CACHE,
104 &URLRequestContextConfig::http_cache);
105 converter->RegisterBoolField(REQUEST_CONTEXT_CONFIG_LOAD_DISABLE_CACHE,
106 &URLRequestContextConfig::load_disable_cache);
107 converter->RegisterIntField(REQUEST_CONTEXT_CONFIG_HTTP_CACHE_MAX_SIZE,
108 &URLRequestContextConfig::http_cache_max_size);
109 converter->RegisterRepeatedMessage(REQUEST_CONTEXT_CONFIG_QUIC_HINTS,
110 &URLRequestContextConfig::quic_hints);
111 converter->RegisterStringField(
112 REQUEST_CONTEXT_CONFIG_QUIC_OPTIONS,
113 &URLRequestContextConfig::quic_connection_options);
114 converter->RegisterStringField(
115 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_PRIMARY_PROXY,
116 &URLRequestContextConfig::data_reduction_primary_proxy);
117 converter->RegisterStringField(
118 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_FALLBACK_PROXY,
119 &URLRequestContextConfig::data_reduction_fallback_proxy);
120 converter->RegisterStringField(
121 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_SECURE_PROXY_CHECK_URL,
122 &URLRequestContextConfig::data_reduction_secure_proxy_check_url);
123 converter->RegisterStringField(
124 REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_PROXY_KEY,
125 &URLRequestContextConfig::data_reduction_proxy_key);
128 } // namespace cronet