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 "chrome/browser/prefs/proxy_prefs.h" 12 #include "chrome/common/pref_names.h" 13 #include "net/proxy/proxy_config.h" 14 #include "net/proxy/proxy_info.h" 15 #include "net/proxy/proxy_service.h" 16 17 DataReductionProxyChromeConfigurator::DataReductionProxyChromeConfigurator( 18 PrefService* prefs, 19 scoped_refptr<base::SequencedTaskRunner> network_task_runner) 20 : prefs_(prefs), network_task_runner_(network_task_runner) { 21 } 22 23 DataReductionProxyChromeConfigurator::~DataReductionProxyChromeConfigurator() { 24 } 25 26 void DataReductionProxyChromeConfigurator::Enable( 27 bool primary_restricted, 28 bool fallback_restricted, 29 const std::string& primary_origin, 30 const std::string& fallback_origin, 31 const std::string& ssl_origin) { 32 DCHECK(prefs_); 33 DictionaryPrefUpdate update(prefs_, prefs::kProxy); 34 // TODO(bengr): Consider relying on the proxy config for all data reduction 35 // proxy configuration. 36 base::DictionaryValue* dict = update.Get(); 37 38 std::vector<std::string> proxies; 39 if (!primary_restricted) { 40 std::string trimmed_primary; 41 base::TrimString(primary_origin, "/", &trimmed_primary); 42 if (!trimmed_primary.empty()) 43 proxies.push_back(trimmed_primary); 44 } 45 if (!fallback_restricted) { 46 std::string trimmed_fallback; 47 base::TrimString(fallback_origin, "/", &trimmed_fallback); 48 if (!trimmed_fallback.empty()) 49 proxies.push_back(trimmed_fallback); 50 } 51 if (proxies.empty()) { 52 std::string mode; 53 // If already in a disabled mode, do nothing. 54 if (dict->GetString("mode", &mode)) 55 if (ProxyModeToString(ProxyPrefs::MODE_SYSTEM) == mode) 56 return; 57 Disable(); 58 return; 59 } 60 61 std::string trimmed_ssl; 62 base::TrimString(ssl_origin, "/", &trimmed_ssl); 63 64 std::string server = "http=" + JoinString(proxies, ",") + ",direct://;" 65 + (ssl_origin.empty() ? "" : ("https=" + trimmed_ssl + ",direct://;")); 66 67 dict->SetString("server", server); 68 dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); 69 dict->SetString("bypass_list", JoinString(bypass_rules_, ", ")); 70 71 net::ProxyConfig config; 72 config.proxy_rules().ParseFromString(server); 73 config.proxy_rules().bypass_rules.ParseFromString( 74 JoinString(bypass_rules_, ", ")); 75 // The ID is set to a bogus value. It cannot be left uninitialized, else the 76 // config will return invalid. 77 net::ProxyConfig::ID unused_id = 1; 78 config.set_id(unused_id); 79 network_task_runner_->PostTask( 80 FROM_HERE, 81 base::Bind( 82 &DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIO, 83 base::Unretained(this), 84 config)); 85 } 86 87 void DataReductionProxyChromeConfigurator::Disable() { 88 DCHECK(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(); 95 network_task_runner_->PostTask( 96 FROM_HERE, 97 base::Bind( 98 &DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIO, 99 base::Unretained(this), 100 config)); 101 } 102 103 void DataReductionProxyChromeConfigurator::AddHostPatternToBypass( 104 const std::string& pattern) { 105 bypass_rules_.push_back(pattern); 106 } 107 108 void DataReductionProxyChromeConfigurator::AddURLPatternToBypass( 109 const std::string& pattern) { 110 size_t pos = pattern.find('/'); 111 if (pattern.find('/', pos + 1) == pos + 1) 112 pos = pattern.find('/', pos + 2); 113 114 std::string host_pattern; 115 if (pos != std::string::npos) 116 host_pattern = pattern.substr(0, pos); 117 else 118 host_pattern = pattern; 119 120 AddHostPatternToBypass(host_pattern); 121 } 122 123 void DataReductionProxyChromeConfigurator::UpdateProxyConfigOnIO( 124 const net::ProxyConfig& config) { 125 config_ = config; 126 } 127 128 const net::ProxyConfig& 129 DataReductionProxyChromeConfigurator::GetProxyConfigOnIO() const { 130 return config_; 131 } 132