| 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 if (proxy_config_dict->empty()) |
| 64 return false; |
| 65 |
| 66 ProxyConfigDictionary proxy_config_dict_wrapper(proxy_config_dict.get()); |
| 67 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig( |
| 68 proxy_config_dict_wrapper, |
| 69 proxy_config); |
| 70 } |
| 71 |
| 72 // Returns true if proxy settings of |network| are editable. |
| 73 bool IsNetworkProxySettingsEditable(const Network& network) { |
| 74 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); |
| 75 const base::DictionaryValue* onc = |
| 76 network_library->FindOncForNetwork(network.unique_id()); |
| 77 if (!onc) |
| 78 return true; |
| 79 |
| 80 NetworkPropertyUIData proxy_settings_ui_data; |
| 81 proxy_settings_ui_data.ParseOncProperty( |
| 82 network.ui_data().onc_source(), |
| 83 onc, |
| 84 onc::network_config::kProxySettings); |
| 85 return proxy_settings_ui_data.editable(); |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 UIProxyConfigService::UIProxyConfigService(PrefService* pref_service) |
| 91 : pref_service_(pref_service) { |
| 92 } |
| 93 |
| 94 UIProxyConfigService::~UIProxyConfigService() { |
| 95 } |
| 96 |
| 97 void UIProxyConfigService::SetCurrentNetwork( |
| 98 const std::string& current_network) { |
| 99 Network* network = NULL; |
| 100 if (!current_network.empty()) { |
| 101 network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( |
| 102 current_network); |
| 103 LOG_IF(ERROR, !network) |
| 104 << "Can't find requested network " << current_network; |
| 105 } |
| 106 current_ui_network_ = current_network; |
| 107 if (!network) { |
| 108 current_ui_network_.clear(); |
| 109 current_ui_config_ = UIProxyConfig(); |
| 110 return; |
| 111 } |
| 112 |
| 113 DetermineEffectiveConfig(*network); |
| 114 VLOG(1) << "Current ui network: " |
| 115 << network->name() |
| 116 << ", " << ModeToString(current_ui_config_.mode) |
| 117 << ", " << ProxyPrefs::ConfigStateToString(current_ui_config_.state) |
| 118 << ", modifiable:" << current_ui_config_.user_modifiable; |
| 119 // Notify observers. |
| 120 std::vector<base::Closure>::iterator iter = callbacks_.begin(); |
| 121 while (iter != callbacks_.end()) { |
| 122 if (iter->is_null()) { |
| 123 iter = callbacks_.erase(iter); |
| 124 } else { |
| 125 iter->Run(); |
| 126 ++iter; |
| 127 } |
| 128 } |
| 129 } |
| 130 |
| 131 void UIProxyConfigService::MakeActiveNetworkCurrent() { |
| 132 const Network* network = |
| 133 CrosLibrary::Get()->GetNetworkLibrary()->active_network(); |
| 134 SetCurrentNetwork(network ? network->service_path() : std::string()); |
| 135 } |
| 136 |
| 137 void UIProxyConfigService::GetCurrentNetworkName(std::string* network_name) { |
| 138 if (!network_name) |
| 139 return; |
| 140 network_name->clear(); |
| 141 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( |
| 142 current_ui_network_); |
| 143 if (!network) { |
| 144 LOG(ERROR) << "Can't find requested network " << current_ui_network_; |
| 145 return; |
| 146 } |
| 147 if (network->name().empty() && network->type() == chromeos::TYPE_ETHERNET) { |
| 148 *network_name = |
| 149 l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET); |
| 150 } else { |
| 151 *network_name = network->name(); |
| 152 } |
| 153 } |
| 154 |
| 155 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) { |
| 156 *config = current_ui_config_; |
| 157 } |
| 158 |
| 159 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) { |
| 160 current_ui_config_ = config; |
| 161 if (current_ui_network_.empty()) |
| 162 return; |
| 163 |
| 164 // Update config to shill. |
| 165 std::string value; |
| 166 if (!current_ui_config_.SerializeForNetwork(&value)) |
| 167 return; |
| 168 |
| 169 VLOG(1) << "Set proxy (mode=" << current_ui_config_.mode |
| 170 << ") for " << current_ui_network_; |
| 171 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM; |
| 172 |
| 173 // Set ProxyConfig of the current network. |
| 174 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( |
| 175 current_ui_network_); |
| 176 if (!network) { |
| 177 NOTREACHED() << "Can't find requested network " << current_ui_network_; |
| 178 return; |
| 179 } |
| 180 network->SetProxyConfig(value); |
| 181 VLOG(1) << "Set proxy for " |
| 182 << (network->name().empty() ? current_ui_network_ : network->name()) |
| 183 << ", value=" << value; |
| 184 } |
| 185 |
| 186 void UIProxyConfigService::AddNotificationCallback(base::Closure callback) { |
| 187 std::vector<base::Closure>::iterator iter = |
| 188 std::find_if(callbacks_.begin(), callbacks_.end(), |
| 189 ClosureEquals(callback)); |
| 190 if (iter == callbacks_.end()) |
| 191 callbacks_.push_back(callback); |
| 192 } |
| 193 |
| 194 void UIProxyConfigService::RemoveNotificationCallback(base::Closure callback) { |
| 195 std::vector<base::Closure>::iterator iter = |
| 196 std::find_if(callbacks_.begin(), callbacks_.end(), |
| 197 ClosureEquals(callback)); |
| 198 if (iter != callbacks_.end()) |
| 199 callbacks_.erase(iter); |
| 200 } |
| 201 |
| 202 void UIProxyConfigService::DetermineEffectiveConfig(const Network& network) { |
| 203 // Get prefs proxy config if available. |
| 204 net::ProxyConfig pref_config; |
| 205 ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig( |
| 206 pref_service_, |
| 207 &pref_config); |
| 208 |
| 209 // Get network proxy config if available. |
| 210 net::ProxyConfig network_config; |
| 211 net::ProxyConfigService::ConfigAvailability network_availability = |
| 212 net::ProxyConfigService::CONFIG_UNSET; |
| 213 if (ParseProxyConfig(network.proxy_config(), &network_config)) { |
| 214 // Network is private or shared with user using shared proxies. |
| 215 VLOG(1) << this << ": using network proxy: " << network.proxy_config(); |
| 216 network_availability = net::ProxyConfigService::CONFIG_VALID; |
| 217 } |
| 218 |
| 219 // Determine effective proxy config, either from prefs or network. |
| 220 ProxyPrefs::ConfigState effective_config_state; |
| 221 net::ProxyConfig effective_config; |
| 222 ProxyConfigServiceImpl::GetEffectiveProxyConfig( |
| 223 pref_state, pref_config, |
| 224 network_availability, network_config, false, |
| 225 &effective_config_state, &effective_config); |
| 226 |
| 227 // Store effective proxy into |current_ui_config_|. |
| 228 current_ui_config_.FromNetProxyConfig(effective_config); |
| 229 current_ui_config_.state = effective_config_state; |
| 230 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) { |
| 231 current_ui_config_.user_modifiable = false; |
| 232 } else if (!IsNetworkProxySettingsEditable(network)) { |
| 233 // TODO(xiyuan): Figure out the right way to set config state for managed |
| 234 // network. |
| 235 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY; |
| 236 current_ui_config_.user_modifiable = false; |
| 237 } else { |
| 238 current_ui_config_.user_modifiable = |
| 239 !ProxyConfigServiceImpl::IgnoreProxy(pref_service_, |
| 240 network.profile_path(), |
| 241 network.ui_data().onc_source()); |
| 242 } |
| 243 } |
| 244 |
| 245 } // namespace chromeos |
| OLD | NEW |