| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/ui_proxy_config_service.h" | 5 #include "chrome/browser/chromeos/ui_proxy_config_service.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/net/proxy_config_handler.h" |
| 11 #include "chrome/browser/chromeos/cros/network_library.h" | |
| 12 #include "chrome/browser/chromeos/cros/network_property_ui_data.h" | |
| 13 #include "chrome/browser/chromeos/proxy_config_service_impl.h" | 11 #include "chrome/browser/chromeos/proxy_config_service_impl.h" |
| 14 #include "chromeos/network/onc/onc_utils.h" | 12 #include "chromeos/network/network_state.h" |
| 13 #include "chromeos/network/network_state_handler.h" |
| 15 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 16 #include "net/proxy/proxy_config.h" | 15 #include "net/proxy/proxy_config.h" |
| 17 | 16 |
| 18 namespace chromeos { | 17 namespace chromeos { |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 const char* ModeToString(UIProxyConfig::Mode mode) { | 21 const char* ModeToString(UIProxyConfig::Mode mode) { |
| 23 switch (mode) { | 22 switch (mode) { |
| 24 case UIProxyConfig::MODE_DIRECT: | 23 case UIProxyConfig::MODE_DIRECT: |
| 25 return "direct"; | 24 return "direct"; |
| 26 case UIProxyConfig::MODE_AUTO_DETECT: | 25 case UIProxyConfig::MODE_AUTO_DETECT: |
| 27 return "auto-detect"; | 26 return "auto-detect"; |
| 28 case UIProxyConfig::MODE_PAC_SCRIPT: | 27 case UIProxyConfig::MODE_PAC_SCRIPT: |
| 29 return "pacurl"; | 28 return "pacurl"; |
| 30 case UIProxyConfig::MODE_SINGLE_PROXY: | 29 case UIProxyConfig::MODE_SINGLE_PROXY: |
| 31 return "single-proxy"; | 30 return "single-proxy"; |
| 32 case UIProxyConfig::MODE_PROXY_PER_SCHEME: | 31 case UIProxyConfig::MODE_PROXY_PER_SCHEME: |
| 33 return "proxy-per-scheme"; | 32 return "proxy-per-scheme"; |
| 34 } | 33 } |
| 35 NOTREACHED() << "Unrecognized mode type"; | 34 NOTREACHED() << "Unrecognized mode type"; |
| 36 return ""; | 35 return ""; |
| 37 } | 36 } |
| 38 | 37 |
| 39 bool ParseProxyConfig(const std::string& pref_proxy_config, | 38 // Writes the proxy config of |network| to |proxy_config|. Returns false if no |
| 40 net::ProxyConfig* proxy_config) { | 39 // proxy was configured for this network. |
| 41 if (pref_proxy_config.empty()) | 40 bool GetProxyConfig(const NetworkState& network, |
| 41 net::ProxyConfig* proxy_config) { |
| 42 scoped_ptr<ProxyConfigDictionary> proxy_dict = |
| 43 proxy_config::GetProxyConfigOfNetwork(network); |
| 44 if (!proxy_dict) |
| 42 return false; | 45 return false; |
| 43 | 46 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict, |
| 44 scoped_ptr<base::DictionaryValue> proxy_config_dict( | 47 proxy_config); |
| 45 chromeos::onc::ReadDictionaryFromJson(pref_proxy_config)); | |
| 46 if (!proxy_config_dict) { | |
| 47 LOG(WARNING) << "Failed to parse proxy config."; | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 ProxyConfigDictionary proxy_config_dict_wrapper(proxy_config_dict.get()); | |
| 52 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig( | |
| 53 proxy_config_dict_wrapper, | |
| 54 proxy_config); | |
| 55 } | 48 } |
| 56 | 49 |
| 57 // Returns true if proxy settings of |network| are editable. | 50 // Returns true if proxy settings of |network| are editable. |
| 58 bool IsNetworkProxySettingsEditable(const Network& network) { | 51 bool IsNetworkProxySettingsEditable(const NetworkState& network) { |
| 59 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); | 52 onc::ONCSource source = network.onc_source(); |
| 60 const base::DictionaryValue* onc = | 53 return source != onc::ONC_SOURCE_DEVICE_POLICY && |
| 61 network_library->FindOncForNetwork(network.unique_id()); | 54 source != onc::ONC_SOURCE_USER_POLICY; |
| 62 if (!onc) | |
| 63 return true; | |
| 64 | |
| 65 NetworkPropertyUIData proxy_settings_ui_data; | |
| 66 proxy_settings_ui_data.ParseOncProperty( | |
| 67 network.ui_data().onc_source(), | |
| 68 onc, | |
| 69 onc::network_config::kProxySettings); | |
| 70 return proxy_settings_ui_data.IsEditable(); | |
| 71 } | 55 } |
| 72 | 56 |
| 73 } // namespace | 57 } // namespace |
| 74 | 58 |
| 75 UIProxyConfigService::UIProxyConfigService() { | 59 UIProxyConfigService::UIProxyConfigService() { |
| 76 } | 60 } |
| 77 | 61 |
| 78 UIProxyConfigService::~UIProxyConfigService() { | 62 UIProxyConfigService::~UIProxyConfigService() { |
| 79 } | 63 } |
| 80 | 64 |
| 81 void UIProxyConfigService::SetPrefs(PrefService* pref_service) { | 65 void UIProxyConfigService::SetPrefs(PrefService* pref_service) { |
| 82 pref_service_ = pref_service; | 66 pref_service_ = pref_service; |
| 83 } | 67 } |
| 84 | 68 |
| 85 void UIProxyConfigService::SetCurrentNetwork( | 69 void UIProxyConfigService::SetCurrentNetwork( |
| 86 const std::string& current_network) { | 70 const std::string& current_network) { |
| 87 Network* network = NULL; | 71 const NetworkState* network = NULL; |
| 88 if (!current_network.empty()) { | 72 if (!current_network.empty()) { |
| 89 network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( | 73 network = NetworkHandler::Get()->network_state_handler()->GetNetworkState( |
| 90 current_network); | 74 current_network); |
| 91 LOG_IF(ERROR, !network) | 75 LOG_IF(ERROR, !network) |
| 92 << "Can't find requested network " << current_network; | 76 << "Can't find requested network " << current_network; |
| 93 } | 77 } |
| 94 current_ui_network_ = current_network; | 78 current_ui_network_ = current_network; |
| 95 if (!network) { | 79 if (!network) { |
| 96 current_ui_network_.clear(); | 80 current_ui_network_.clear(); |
| 97 current_ui_config_ = UIProxyConfig(); | 81 current_ui_config_ = UIProxyConfig(); |
| 98 return; | 82 return; |
| 99 } | 83 } |
| 100 | 84 |
| 101 DetermineEffectiveConfig(*network); | 85 DetermineEffectiveConfig(*network); |
| 102 VLOG(1) << "Current ui network: " | 86 VLOG(1) << "Current ui network: " |
| 103 << network->name() | 87 << network->name() |
| 104 << ", " << ModeToString(current_ui_config_.mode) << ", " | 88 << ", " << ModeToString(current_ui_config_.mode) << ", " |
| 105 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state) | 89 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state) |
| 106 << ", modifiable:" << current_ui_config_.user_modifiable; | 90 << ", modifiable:" << current_ui_config_.user_modifiable; |
| 107 } | 91 } |
| 108 | 92 |
| 109 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const { | 93 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const { |
| 110 *config = current_ui_config_; | 94 *config = current_ui_config_; |
| 111 } | 95 } |
| 112 | 96 |
| 113 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) { | 97 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) { |
| 114 current_ui_config_ = config; | 98 current_ui_config_ = config; |
| 115 if (current_ui_network_.empty()) | 99 if (current_ui_network_.empty()) |
| 116 return; | 100 return; |
| 117 | 101 |
| 118 // Update config to shill. | 102 const NetworkState* network = |
| 119 std::string value; | 103 NetworkHandler::Get()->network_state_handler()-> |
| 120 if (!current_ui_config_.SerializeForNetwork(&value)) | 104 GetNetworkState(current_ui_network_); |
| 121 return; | |
| 122 | |
| 123 VLOG(1) << "Set proxy for " << current_ui_network_ << " to " << value; | |
| 124 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM; | |
| 125 | |
| 126 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( | |
| 127 current_ui_network_); | |
| 128 if (!network) { | 105 if (!network) { |
| 129 LOG(ERROR) << "Can't find requested network " << current_ui_network_; | 106 LOG(ERROR) << "Can't find requested network " << current_ui_network_; |
| 130 return; | 107 return; |
| 131 } | 108 } |
| 132 network->SetProxyConfig(value); | 109 |
| 110 // Store config for this network. |
| 111 scoped_ptr<base::DictionaryValue> proxy_config_value( |
| 112 config.ToPrefProxyConfig()); |
| 113 ProxyConfigDictionary proxy_config_dict(proxy_config_value.get()); |
| 114 |
| 115 VLOG(1) << "Set proxy for " << current_ui_network_ |
| 116 << " to " << *proxy_config_value; |
| 117 |
| 118 proxy_config::SetProxyConfigOfNetwork(proxy_config_dict, *network); |
| 119 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM; |
| 133 } | 120 } |
| 134 | 121 |
| 135 void UIProxyConfigService::DetermineEffectiveConfig(const Network& network) { | 122 void UIProxyConfigService::DetermineEffectiveConfig( |
| 123 const NetworkState& network) { |
| 136 DCHECK(pref_service_); | 124 DCHECK(pref_service_); |
| 137 | 125 |
| 138 // Get prefs proxy config if available. | 126 // Get prefs proxy config if available. |
| 139 net::ProxyConfig pref_config; | 127 net::ProxyConfig pref_config; |
| 140 ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig( | 128 ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig( |
| 141 pref_service_, &pref_config); | 129 pref_service_, &pref_config); |
| 142 | 130 |
| 143 // Get network proxy config if available. | 131 // Get network proxy config if available. |
| 144 net::ProxyConfig network_config; | 132 net::ProxyConfig network_config; |
| 145 net::ProxyConfigService::ConfigAvailability network_availability = | 133 net::ProxyConfigService::ConfigAvailability network_availability = |
| 146 net::ProxyConfigService::CONFIG_UNSET; | 134 net::ProxyConfigService::CONFIG_UNSET; |
| 147 if (ParseProxyConfig(network.proxy_config(), &network_config)) { | 135 if (chromeos::GetProxyConfig(network, &network_config)) { |
| 148 // Network is private or shared with user using shared proxies. | 136 // Network is private or shared with user using shared proxies. |
| 149 VLOG(1) << this << ": using network proxy: " << network.proxy_config(); | 137 VLOG(1) << this << ": using network proxy: " << network.proxy_config(); |
| 150 network_availability = net::ProxyConfigService::CONFIG_VALID; | 138 network_availability = net::ProxyConfigService::CONFIG_VALID; |
| 151 } | 139 } |
| 152 | 140 |
| 153 // Determine effective proxy config, either from prefs or network. | 141 // Determine effective proxy config, either from prefs or network. |
| 154 ProxyPrefs::ConfigState effective_config_state; | 142 ProxyPrefs::ConfigState effective_config_state; |
| 155 net::ProxyConfig effective_config; | 143 net::ProxyConfig effective_config; |
| 156 ProxyConfigServiceImpl::GetEffectiveProxyConfig( | 144 ProxyConfigServiceImpl::GetEffectiveProxyConfig( |
| 157 pref_state, pref_config, | 145 pref_state, pref_config, |
| 158 network_availability, network_config, false, | 146 network_availability, network_config, false, |
| 159 &effective_config_state, &effective_config); | 147 &effective_config_state, &effective_config); |
| 160 | 148 |
| 161 // Store effective proxy into |current_ui_config_|. | 149 // Store effective proxy into |current_ui_config_|. |
| 162 current_ui_config_.FromNetProxyConfig(effective_config); | 150 current_ui_config_.FromNetProxyConfig(effective_config); |
| 163 current_ui_config_.state = effective_config_state; | 151 current_ui_config_.state = effective_config_state; |
| 164 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) { | 152 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) { |
| 165 current_ui_config_.user_modifiable = false; | 153 current_ui_config_.user_modifiable = false; |
| 166 } else if (!IsNetworkProxySettingsEditable(network)) { | 154 } else if (!IsNetworkProxySettingsEditable(network)) { |
| 167 // TODO(xiyuan): Figure out the right way to set config state for managed | 155 // TODO(xiyuan): Figure out the right way to set config state for managed |
| 168 // network. | 156 // network. |
| 169 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY; | 157 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY; |
| 170 current_ui_config_.user_modifiable = false; | 158 current_ui_config_.user_modifiable = false; |
| 171 } else { | 159 } else { |
| 172 current_ui_config_.user_modifiable = | 160 current_ui_config_.user_modifiable = |
| 173 !ProxyConfigServiceImpl::IgnoreProxy(pref_service_, | 161 !ProxyConfigServiceImpl::IgnoreProxy(pref_service_, |
| 174 network.profile_path(), | 162 network.profile_path(), |
| 175 network.ui_data().onc_source()); | 163 network.onc_source()); |
| 176 } | 164 } |
| 177 } | 165 } |
| 178 | 166 |
| 179 } // namespace chromeos | 167 } // namespace chromeos |
| OLD | NEW |