OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/chromeos/ui_proxy_config_service.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.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" |
| 14 #include "chromeos/network/onc/onc_utils.h" |
| 15 #include "grit/generated_resources.h" |
| 16 #include "net/proxy/proxy_config.h" |
| 17 #include "ui/base/l10n/l10n_util.h" |
| 18 |
| 19 namespace chromeos { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class ClosureEquals { |
| 24 public: |
| 25 explicit ClosureEquals(const base::Closure& closure) |
| 26 : closure_(closure) { } |
| 27 bool operator() (const base::Closure& other) { |
| 28 return closure_.Equals(other); |
| 29 } |
| 30 private: |
| 31 const base::Closure& closure_; |
| 32 }; |
| 33 |
| 34 const char* ModeToString(UIProxyConfig::Mode mode) { |
| 35 switch (mode) { |
| 36 case UIProxyConfig::MODE_DIRECT: |
| 37 return "direct"; |
| 38 case UIProxyConfig::MODE_AUTO_DETECT: |
| 39 return "auto-detect"; |
| 40 case UIProxyConfig::MODE_PAC_SCRIPT: |
| 41 return "pacurl"; |
| 42 case UIProxyConfig::MODE_SINGLE_PROXY: |
| 43 return "single-proxy"; |
| 44 case UIProxyConfig::MODE_PROXY_PER_SCHEME: |
| 45 return "proxy-per-scheme"; |
| 46 } |
| 47 NOTREACHED() << "Unrecognized mode type"; |
| 48 return ""; |
| 49 } |
| 50 |
| 51 bool ParseProxyConfig(const std::string& pref_proxy_config, |
| 52 net::ProxyConfig* proxy_config) { |
| 53 if (pref_proxy_config.empty()) |
| 54 return false; |
| 55 |
| 56 scoped_ptr<base::DictionaryValue> proxy_config_dict( |
| 57 chromeos::onc::ReadDictionaryFromJson(pref_proxy_config)); |
| 58 if (!proxy_config_dict) { |
| 59 LOG(WARNING) << "Failed to parse proxy config."; |
| 60 return false; |
| 61 } |
| 62 |
| 63 ProxyConfigDictionary proxy_config_dict_wrapper(proxy_config_dict.get()); |
| 64 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig( |
| 65 proxy_config_dict_wrapper, |
| 66 proxy_config); |
| 67 } |
| 68 |
| 69 // Returns true if proxy settings of |network| are editable. |
| 70 bool IsNetworkProxySettingsEditable(const Network& network) { |
| 71 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); |
| 72 const base::DictionaryValue* onc = |
| 73 network_library->FindOncForNetwork(network.unique_id()); |
| 74 if (!onc) |
| 75 return true; |
| 76 |
| 77 NetworkPropertyUIData proxy_settings_ui_data; |
| 78 proxy_settings_ui_data.ParseOncProperty( |
| 79 network.ui_data().onc_source(), |
| 80 onc, |
| 81 onc::network_config::kProxySettings); |
| 82 return proxy_settings_ui_data.IsEditable(); |
| 83 } |
| 84 |
| 85 } // namespace |
| 86 |
| 87 UIProxyConfigService::UIProxyConfigService(PrefService* pref_service) |
| 88 : pref_service_(pref_service) { |
| 89 } |
| 90 |
| 91 UIProxyConfigService::~UIProxyConfigService() { |
| 92 } |
| 93 |
| 94 void UIProxyConfigService::SetCurrentNetwork( |
| 95 const std::string& current_network) { |
| 96 Network* network = NULL; |
| 97 if (!current_network.empty()) { |
| 98 network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( |
| 99 current_network); |
| 100 LOG_IF(ERROR, !network) |
| 101 << "Can't find requested network " << current_network; |
| 102 } |
| 103 current_ui_network_ = current_network; |
| 104 if (!network) { |
| 105 current_ui_network_.clear(); |
| 106 current_ui_config_ = UIProxyConfig(); |
| 107 return; |
| 108 } |
| 109 |
| 110 DetermineEffectiveConfig(*network); |
| 111 VLOG(1) << "Current ui network: " |
| 112 << network->name() |
| 113 << ", " << ModeToString(current_ui_config_.mode) << ", " |
| 114 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state) |
| 115 << ", modifiable:" << current_ui_config_.user_modifiable; |
| 116 // Notify observers. |
| 117 std::vector<base::Closure>::iterator iter = callbacks_.begin(); |
| 118 while (iter != callbacks_.end()) { |
| 119 if (iter->is_null()) { |
| 120 iter = callbacks_.erase(iter); |
| 121 } else { |
| 122 iter->Run(); |
| 123 ++iter; |
| 124 } |
| 125 } |
| 126 } |
| 127 |
| 128 void UIProxyConfigService::MakeActiveNetworkCurrent() { |
| 129 const Network* network = |
| 130 CrosLibrary::Get()->GetNetworkLibrary()->active_network(); |
| 131 SetCurrentNetwork(network ? network->service_path() : std::string()); |
| 132 } |
| 133 |
| 134 void UIProxyConfigService::GetCurrentNetworkName(std::string* network_name) { |
| 135 if (!network_name) |
| 136 return; |
| 137 network_name->clear(); |
| 138 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( |
| 139 current_ui_network_); |
| 140 if (!network) { |
| 141 LOG(ERROR) << "Can't find requested network " << current_ui_network_; |
| 142 return; |
| 143 } |
| 144 if (network->name().empty() && network->type() == chromeos::TYPE_ETHERNET) { |
| 145 *network_name = |
| 146 l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET); |
| 147 } else { |
| 148 *network_name = network->name(); |
| 149 } |
| 150 } |
| 151 |
| 152 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) { |
| 153 *config = current_ui_config_; |
| 154 } |
| 155 |
| 156 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) { |
| 157 current_ui_config_ = config; |
| 158 if (current_ui_network_.empty()) |
| 159 return; |
| 160 |
| 161 // Update config to shill. |
| 162 std::string value; |
| 163 if (!current_ui_config_.SerializeForNetwork(&value)) |
| 164 return; |
| 165 |
| 166 VLOG(1) << "Set proxy (mode=" << current_ui_config_.mode |
| 167 << ") for " << current_ui_network_; |
| 168 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM; |
| 169 |
| 170 // Set ProxyConfig of the current network. |
| 171 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( |
| 172 current_ui_network_); |
| 173 if (!network) { |
| 174 NOTREACHED() << "Can't find requested network " << current_ui_network_; |
| 175 return; |
| 176 } |
| 177 network->SetProxyConfig(value); |
| 178 VLOG(1) << "Set proxy for " |
| 179 << (network->name().empty() ? current_ui_network_ : network->name()) |
| 180 << ", value=" << value; |
| 181 } |
| 182 |
| 183 void UIProxyConfigService::AddNotificationCallback(base::Closure callback) { |
| 184 std::vector<base::Closure>::iterator iter = std::find_if( |
| 185 callbacks_.begin(), callbacks_.end(), ClosureEquals(callback)); |
| 186 if (iter == callbacks_.end()) |
| 187 callbacks_.push_back(callback); |
| 188 } |
| 189 |
| 190 void UIProxyConfigService::RemoveNotificationCallback(base::Closure callback) { |
| 191 std::vector<base::Closure>::iterator iter = std::find_if( |
| 192 callbacks_.begin(), callbacks_.end(), ClosureEquals(callback)); |
| 193 if (iter != callbacks_.end()) |
| 194 callbacks_.erase(iter); |
| 195 } |
| 196 |
| 197 void UIProxyConfigService::DetermineEffectiveConfig(const Network& network) { |
| 198 // Get prefs proxy config if available. |
| 199 net::ProxyConfig pref_config; |
| 200 ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig( |
| 201 pref_service_, &pref_config); |
| 202 |
| 203 // Get network proxy config if available. |
| 204 net::ProxyConfig network_config; |
| 205 net::ProxyConfigService::ConfigAvailability network_availability = |
| 206 net::ProxyConfigService::CONFIG_UNSET; |
| 207 if (ParseProxyConfig(network.proxy_config(), &network_config)) { |
| 208 // Network is private or shared with user using shared proxies. |
| 209 VLOG(1) << this << ": using network proxy: " << network.proxy_config(); |
| 210 network_availability = net::ProxyConfigService::CONFIG_VALID; |
| 211 } |
| 212 |
| 213 // Determine effective proxy config, either from prefs or network. |
| 214 ProxyPrefs::ConfigState effective_config_state; |
| 215 net::ProxyConfig effective_config; |
| 216 ProxyConfigServiceImpl::GetEffectiveProxyConfig( |
| 217 pref_state, pref_config, |
| 218 network_availability, network_config, false, |
| 219 &effective_config_state, &effective_config); |
| 220 |
| 221 // Store effective proxy into |current_ui_config_|. |
| 222 current_ui_config_.FromNetProxyConfig(effective_config); |
| 223 current_ui_config_.state = effective_config_state; |
| 224 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) { |
| 225 current_ui_config_.user_modifiable = false; |
| 226 } else if (!IsNetworkProxySettingsEditable(network)) { |
| 227 // TODO(xiyuan): Figure out the right way to set config state for managed |
| 228 // network. |
| 229 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY; |
| 230 current_ui_config_.user_modifiable = false; |
| 231 } else { |
| 232 current_ui_config_.user_modifiable = |
| 233 !ProxyConfigServiceImpl::IgnoreProxy(pref_service_, |
| 234 network.profile_path(), |
| 235 network.ui_data().onc_source()); |
| 236 } |
| 237 } |
| 238 |
| 239 } // namespace chromeos |
OLD | NEW |