| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.
h" | |
| 6 | |
| 7 #include "base/prefs/pref_service.h" | |
| 8 #include "base/prefs/scoped_user_pref_update.h" | |
| 9 #include "base/sequenced_task_runner.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/prefs/proxy_prefs.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event
_store.h" | |
| 15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" | |
| 16 #include "net/proxy/proxy_config.h" | |
| 17 #include "net/proxy/proxy_info.h" | |
| 18 #include "net/proxy/proxy_list.h" | |
| 19 #include "net/proxy/proxy_service.h" | |
| 20 | |
| 21 DataReductionProxyChromeConfigurator::DataReductionProxyChromeConfigurator( | |
| 22 PrefService* prefs, | |
| 23 scoped_refptr<base::SequencedTaskRunner> network_task_runner, | |
| 24 net::NetLog* net_log, | |
| 25 data_reduction_proxy::DataReductionProxyEventStore* event_store) | |
| 26 : prefs_(prefs), | |
| 27 network_task_runner_(network_task_runner), | |
| 28 net_log_(net_log), | |
| 29 data_reduction_proxy_event_store_(event_store) { | |
| 30 DCHECK(prefs); | |
| 31 DCHECK(network_task_runner.get()); | |
| 32 DCHECK(net_log); | |
| 33 DCHECK(event_store); | |
| 34 } | |
| 35 | |
| 36 DataReductionProxyChromeConfigurator::~DataReductionProxyChromeConfigurator() { | |
| 37 } | |
| 38 | |
| 39 // static | |
| 40 void DataReductionProxyChromeConfigurator::DisableInProxyConfigPref( | |
| 41 PrefService* prefs) { | |
| 42 DCHECK(prefs); | |
| 43 DictionaryPrefUpdate update(prefs, prefs::kProxy); | |
| 44 base::DictionaryValue* dict = update.Get(); | |
| 45 std::string mode; | |
| 46 dict->GetString("mode", &mode); | |
| 47 std::string server; | |
| 48 dict->GetString("server", &server); | |
| 49 net::ProxyConfig::ProxyRules proxy_rules; | |
| 50 proxy_rules.ParseFromString(server); | |
| 51 // The data reduction proxy uses MODE_FIXED_SERVERS. | |
| 52 if (mode != ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS) | |
| 53 || !ContainsDataReductionProxy(proxy_rules)) { | |
| 54 return; | |
| 55 } | |
| 56 dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | |
| 57 dict->SetString("server", ""); | |
| 58 dict->SetString("bypass_list", ""); | |
| 59 } | |
| 60 | |
| 61 void DataReductionProxyChromeConfigurator::Enable( | |
| 62 bool primary_restricted, | |
| 63 bool fallback_restricted, | |
| 64 const std::string& primary_origin, | |
| 65 const std::string& fallback_origin, | |
| 66 const std::string& ssl_origin) { | |
| 67 DCHECK(prefs_); | |
| 68 DictionaryPrefUpdate update(prefs_, prefs::kProxy); | |
| 69 // TODO(bengr): Consider relying on the proxy config for all data reduction | |
| 70 // proxy configuration. | |
| 71 base::DictionaryValue* dict = update.Get(); | |
| 72 | |
| 73 std::vector<std::string> proxies; | |
| 74 if (!primary_restricted) { | |
| 75 std::string trimmed_primary; | |
| 76 base::TrimString(primary_origin, "/", &trimmed_primary); | |
| 77 if (!trimmed_primary.empty()) | |
| 78 proxies.push_back(trimmed_primary); | |
| 79 } | |
| 80 if (!fallback_restricted) { | |
| 81 std::string trimmed_fallback; | |
| 82 base::TrimString(fallback_origin, "/", &trimmed_fallback); | |
| 83 if (!trimmed_fallback.empty()) | |
| 84 proxies.push_back(trimmed_fallback); | |
| 85 } | |
| 86 if (proxies.empty()) { | |
| 87 std::string mode; | |
| 88 // If already in a disabled mode, do nothing. | |
| 89 if (dict->GetString("mode", &mode)) | |
| 90 if (ProxyModeToString(ProxyPrefs::MODE_SYSTEM) == mode) | |
| 91 return; | |
| 92 Disable(); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 std::string trimmed_ssl; | |
| 97 base::TrimString(ssl_origin, "/", &trimmed_ssl); | |
| 98 | |
| 99 std::string server = "http=" + JoinString(proxies, ",") + ",direct://;" | |
| 100 + (ssl_origin.empty() ? "" : ("https=" + trimmed_ssl + ",direct://;")); | |
| 101 | |
| 102 dict->SetString("server", server); | |
| 103 dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); | |
| 104 dict->SetString("bypass_list", JoinString(bypass_rules_, ", ")); | |
| 105 | |
| 106 net::ProxyConfig config; | |
| 107 config.proxy_rules().ParseFromString(server); | |
| 108 config.proxy_rules().bypass_rules.ParseFromString( | |
| 109 JoinString(bypass_rules_, ", ")); | |
| 110 // The ID is set to a bogus value. It cannot be left uninitialized, else the | |
| 111 // config will return invalid. | |
| 112 net::ProxyConfig::ID unused_id = 1; | |
| 113 config.set_id(unused_id); | |
| 114 data_reduction_proxy_event_store_->AddProxyEnabledEvent( | |
| 115 net_log_, primary_restricted, fallback_restricted, primary_origin, | |
| 116 fallback_origin, ssl_origin); | |
| 117 network_task_runner_->PostTask( | |
| 118 FROM_HERE, | |
| 119 base::Bind( | |
| 120 &DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIOThread, | |
| 121 base::Unretained(this), | |
| 122 config)); | |
| 123 } | |
| 124 | |
| 125 void DataReductionProxyChromeConfigurator::Disable() { | |
| 126 DisableInProxyConfigPref(prefs_); | |
| 127 net::ProxyConfig config = net::ProxyConfig::CreateDirect(); | |
| 128 data_reduction_proxy_event_store_->AddProxyDisabledEvent(net_log_); | |
| 129 network_task_runner_->PostTask( | |
| 130 FROM_HERE, | |
| 131 base::Bind( | |
| 132 &DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIOThread, | |
| 133 base::Unretained(this), | |
| 134 config)); | |
| 135 } | |
| 136 | |
| 137 void DataReductionProxyChromeConfigurator::AddHostPatternToBypass( | |
| 138 const std::string& pattern) { | |
| 139 bypass_rules_.push_back(pattern); | |
| 140 } | |
| 141 | |
| 142 void DataReductionProxyChromeConfigurator::AddURLPatternToBypass( | |
| 143 const std::string& pattern) { | |
| 144 size_t pos = pattern.find('/'); | |
| 145 if (pattern.find('/', pos + 1) == pos + 1) | |
| 146 pos = pattern.find('/', pos + 2); | |
| 147 | |
| 148 std::string host_pattern; | |
| 149 if (pos != std::string::npos) | |
| 150 host_pattern = pattern.substr(0, pos); | |
| 151 else | |
| 152 host_pattern = pattern; | |
| 153 | |
| 154 AddHostPatternToBypass(host_pattern); | |
| 155 } | |
| 156 | |
| 157 // static | |
| 158 bool DataReductionProxyChromeConfigurator::ContainsDataReductionProxy( | |
| 159 const net::ProxyConfig::ProxyRules& proxy_rules) { | |
| 160 data_reduction_proxy::DataReductionProxyParams params( | |
| 161 data_reduction_proxy::DataReductionProxyParams:: | |
| 162 kAllowAllProxyConfigurations); | |
| 163 if (proxy_rules.type != net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME) | |
| 164 return false; | |
| 165 | |
| 166 const net::ProxyList* https_proxy_list = | |
| 167 proxy_rules.MapUrlSchemeToProxyList("https"); | |
| 168 if (https_proxy_list && !https_proxy_list->IsEmpty() && | |
| 169 // Sufficient to check only the first proxy. | |
| 170 params.IsDataReductionProxy(https_proxy_list->Get().host_port_pair(), | |
| 171 NULL)) { | |
| 172 return true; | |
| 173 } | |
| 174 | |
| 175 const net::ProxyList* http_proxy_list = | |
| 176 proxy_rules.MapUrlSchemeToProxyList("http"); | |
| 177 if (http_proxy_list && !http_proxy_list->IsEmpty() && | |
| 178 // Sufficient to check only the first proxy. | |
| 179 params.IsDataReductionProxy(http_proxy_list->Get().host_port_pair(), | |
| 180 NULL)) { | |
| 181 return true; | |
| 182 } | |
| 183 | |
| 184 return false; | |
| 185 } | |
| 186 | |
| 187 void DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIOThread( | |
| 188 const net::ProxyConfig& config) { | |
| 189 config_ = config; | |
| 190 } | |
| 191 | |
| 192 const net::ProxyConfig& | |
| 193 DataReductionProxyChromeConfigurator::GetProxyConfigOnIOThread() const { | |
| 194 return config_; | |
| 195 } | |
| OLD | NEW |