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 "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.
h" | 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.
h" |
6 | 6 |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "base/prefs/scoped_user_pref_update.h" | 8 #include "base/prefs/scoped_user_pref_update.h" |
9 #include "base/sequenced_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/values.h" |
11 #include "chrome/browser/prefs/proxy_prefs.h" | 12 #include "chrome/browser/prefs/proxy_prefs.h" |
12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" |
13 #include "net/proxy/proxy_config.h" | 15 #include "net/proxy/proxy_config.h" |
14 #include "net/proxy/proxy_info.h" | 16 #include "net/proxy/proxy_info.h" |
| 17 #include "net/proxy/proxy_list.h" |
15 #include "net/proxy/proxy_service.h" | 18 #include "net/proxy/proxy_service.h" |
16 | 19 |
17 DataReductionProxyChromeConfigurator::DataReductionProxyChromeConfigurator( | 20 DataReductionProxyChromeConfigurator::DataReductionProxyChromeConfigurator( |
18 PrefService* prefs, | 21 PrefService* prefs, |
19 scoped_refptr<base::SequencedTaskRunner> network_task_runner) | 22 scoped_refptr<base::SequencedTaskRunner> network_task_runner) |
20 : prefs_(prefs), network_task_runner_(network_task_runner) { | 23 : prefs_(prefs), network_task_runner_(network_task_runner) { |
21 } | 24 } |
22 | 25 |
23 DataReductionProxyChromeConfigurator::~DataReductionProxyChromeConfigurator() { | 26 DataReductionProxyChromeConfigurator::~DataReductionProxyChromeConfigurator() { |
24 } | 27 } |
25 | 28 |
| 29 // static |
| 30 void DataReductionProxyChromeConfigurator::DisableInProxyConfigPref( |
| 31 PrefService* prefs) { |
| 32 DCHECK(prefs); |
| 33 DictionaryPrefUpdate update(prefs, prefs::kProxy); |
| 34 base::DictionaryValue* dict = update.Get(); |
| 35 std::string mode; |
| 36 dict->GetString("mode", &mode); |
| 37 std::string server; |
| 38 dict->GetString("server", &server); |
| 39 net::ProxyConfig::ProxyRules proxy_rules; |
| 40 proxy_rules.ParseFromString(server); |
| 41 // The data reduction proxy uses MODE_FIXED_SERVERS. |
| 42 if (mode != ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS) |
| 43 || !ContainsDataReductionProxy(proxy_rules)) { |
| 44 return; |
| 45 } |
| 46 dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); |
| 47 dict->SetString("server", ""); |
| 48 dict->SetString("bypass_list", ""); |
| 49 } |
| 50 |
26 void DataReductionProxyChromeConfigurator::Enable( | 51 void DataReductionProxyChromeConfigurator::Enable( |
27 bool primary_restricted, | 52 bool primary_restricted, |
28 bool fallback_restricted, | 53 bool fallback_restricted, |
29 const std::string& primary_origin, | 54 const std::string& primary_origin, |
30 const std::string& fallback_origin, | 55 const std::string& fallback_origin, |
31 const std::string& ssl_origin) { | 56 const std::string& ssl_origin) { |
32 DCHECK(prefs_); | 57 DCHECK(prefs_); |
33 DictionaryPrefUpdate update(prefs_, prefs::kProxy); | 58 DictionaryPrefUpdate update(prefs_, prefs::kProxy); |
34 // TODO(bengr): Consider relying on the proxy config for all data reduction | 59 // TODO(bengr): Consider relying on the proxy config for all data reduction |
35 // proxy configuration. | 60 // proxy configuration. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 config.set_id(unused_id); | 103 config.set_id(unused_id); |
79 network_task_runner_->PostTask( | 104 network_task_runner_->PostTask( |
80 FROM_HERE, | 105 FROM_HERE, |
81 base::Bind( | 106 base::Bind( |
82 &DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIO, | 107 &DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIO, |
83 base::Unretained(this), | 108 base::Unretained(this), |
84 config)); | 109 config)); |
85 } | 110 } |
86 | 111 |
87 void DataReductionProxyChromeConfigurator::Disable() { | 112 void DataReductionProxyChromeConfigurator::Disable() { |
88 DCHECK(prefs_); | 113 DisableInProxyConfigPref(prefs_); |
89 DictionaryPrefUpdate update(prefs_, prefs::kProxy); | |
90 base::DictionaryValue* dict = update.Get(); | |
91 dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | |
92 dict->SetString("server", ""); | |
93 dict->SetString("bypass_list", ""); | |
94 net::ProxyConfig config = net::ProxyConfig::CreateDirect(); | 114 net::ProxyConfig config = net::ProxyConfig::CreateDirect(); |
95 network_task_runner_->PostTask( | 115 network_task_runner_->PostTask( |
96 FROM_HERE, | 116 FROM_HERE, |
97 base::Bind( | 117 base::Bind( |
98 &DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIO, | 118 &DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIO, |
99 base::Unretained(this), | 119 base::Unretained(this), |
100 config)); | 120 config)); |
101 } | 121 } |
102 | 122 |
103 void DataReductionProxyChromeConfigurator::AddHostPatternToBypass( | 123 void DataReductionProxyChromeConfigurator::AddHostPatternToBypass( |
104 const std::string& pattern) { | 124 const std::string& pattern) { |
105 bypass_rules_.push_back(pattern); | 125 bypass_rules_.push_back(pattern); |
106 } | 126 } |
107 | 127 |
108 void DataReductionProxyChromeConfigurator::AddURLPatternToBypass( | 128 void DataReductionProxyChromeConfigurator::AddURLPatternToBypass( |
109 const std::string& pattern) { | 129 const std::string& pattern) { |
110 size_t pos = pattern.find('/'); | 130 size_t pos = pattern.find('/'); |
111 if (pattern.find('/', pos + 1) == pos + 1) | 131 if (pattern.find('/', pos + 1) == pos + 1) |
112 pos = pattern.find('/', pos + 2); | 132 pos = pattern.find('/', pos + 2); |
113 | 133 |
114 std::string host_pattern; | 134 std::string host_pattern; |
115 if (pos != std::string::npos) | 135 if (pos != std::string::npos) |
116 host_pattern = pattern.substr(0, pos); | 136 host_pattern = pattern.substr(0, pos); |
117 else | 137 else |
118 host_pattern = pattern; | 138 host_pattern = pattern; |
119 | 139 |
120 AddHostPatternToBypass(host_pattern); | 140 AddHostPatternToBypass(host_pattern); |
121 } | 141 } |
122 | 142 |
| 143 // static |
| 144 bool DataReductionProxyChromeConfigurator::ContainsDataReductionProxy( |
| 145 const net::ProxyConfig::ProxyRules& proxy_rules) { |
| 146 data_reduction_proxy::DataReductionProxyParams params( |
| 147 data_reduction_proxy::DataReductionProxyParams:: |
| 148 kAllowAllProxyConfigurations); |
| 149 if (proxy_rules.type != net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME) |
| 150 return false; |
| 151 |
| 152 const net::ProxyList* https_proxy_list = |
| 153 proxy_rules.MapUrlSchemeToProxyList("https"); |
| 154 if (https_proxy_list && !https_proxy_list->IsEmpty() && |
| 155 // Sufficient to check only the first proxy. |
| 156 params.IsDataReductionProxy(https_proxy_list->Get().host_port_pair(), |
| 157 NULL)) { |
| 158 return true; |
| 159 } |
| 160 |
| 161 const net::ProxyList* http_proxy_list = |
| 162 proxy_rules.MapUrlSchemeToProxyList("http"); |
| 163 if (http_proxy_list && !http_proxy_list->IsEmpty() && |
| 164 // Sufficient to check only the first proxy. |
| 165 params.IsDataReductionProxy(http_proxy_list->Get().host_port_pair(), |
| 166 NULL)) { |
| 167 return true; |
| 168 } |
| 169 |
| 170 return false; |
| 171 } |
| 172 |
123 void DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIO( | 173 void DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIO( |
124 const net::ProxyConfig& config) { | 174 const net::ProxyConfig& config) { |
125 config_ = config; | 175 config_ = config; |
126 } | 176 } |
127 | 177 |
128 const net::ProxyConfig& | 178 const net::ProxyConfig& |
129 DataReductionProxyChromeConfigurator::GetProxyConfigOnIO() const { | 179 DataReductionProxyChromeConfigurator::GetProxyConfigOnIO() const { |
130 return config_; | 180 return config_; |
131 } | 181 } |
OLD | NEW |