Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chromeos/network/network_state.h" | 5 #include "chromeos/network/network_state.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "chromeos/device_event_log.h" | 9 #include "chromeos/device_event_log.h" |
| 10 #include "chromeos/network/network_profile_handler.h" | 10 #include "chromeos/network/network_profile_handler.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 std::vector<std::string>* result) { | 22 std::vector<std::string>* result) { |
| 23 for (size_t i = 0; i < string_list.GetSize(); ++i) { | 23 for (size_t i = 0; i < string_list.GetSize(); ++i) { |
| 24 std::string str; | 24 std::string str; |
| 25 if (!string_list.GetString(i, &str)) | 25 if (!string_list.GetString(i, &str)) |
| 26 return false; | 26 return false; |
| 27 result->push_back(str); | 27 result->push_back(str); |
| 28 } | 28 } |
| 29 return true; | 29 return true; |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool IsCaptivePortalState(const base::DictionaryValue& properties, bool log) { | |
| 33 std::string state; | |
| 34 properties.GetStringWithoutPathExpansion(shill::kStateProperty, &state); | |
| 35 if (state == shill::kStatePortal) | |
|
pneubeck (no reviews)
2015/01/24 10:06:56
shouldn't this be negated?
if (state != kStatePor
stevenjb
2015/01/26 20:16:30
Yes, thanks. Too many balls in the air.
| |
| 36 return false; | |
| 37 std::string portal_detection_phase, portal_detection_status; | |
| 38 if (!properties.GetStringWithoutPathExpansion( | |
| 39 shill::kPortalDetectionFailedPhaseProperty, | |
| 40 &portal_detection_phase) || | |
| 41 !properties.GetStringWithoutPathExpansion( | |
| 42 shill::kPortalDetectionFailedStatusProperty, | |
| 43 &portal_detection_status)) { | |
| 44 // If Shill (or a stub) has not set PortalDetectionFailedStatus | |
| 45 // or PortalDetectionFailedPhase, assume we are in captive portal state. | |
| 46 return true; | |
| 47 } | |
| 48 bool is_captive_portal = | |
|
pneubeck (no reviews)
2015/01/24 10:06:56
please add a comment what the rational of this is,
stevenjb
2015/01/26 20:16:29
Done.
| |
| 49 portal_detection_status == shill::kPortalDetectionStatusFailure && | |
| 50 portal_detection_phase == shill::kPortalDetectionPhaseContent; | |
| 51 if (!log) | |
| 52 return is_captive_portal; | |
| 53 | |
| 54 std::string name; | |
| 55 properties.GetStringWithoutPathExpansion(shill::kNameProperty, &name); | |
| 56 if (name.empty()) | |
| 57 properties.GetStringWithoutPathExpansion(shill::kSSIDProperty, &name); | |
| 58 if (portal_detection_status != shill::kPortalDetectionStatusFailure || | |
|
pneubeck (no reviews)
2015/01/24 10:06:56
nit: condition can be replaced by !is_captive_port
stevenjb
2015/01/26 20:16:29
Ditto, thanks, done.
| |
| 59 portal_detection_phase != shill::kPortalDetectionPhaseContent) { | |
| 60 NET_LOG(EVENT) << "State is 'portal' but not in captive portal state:" | |
| 61 << " name=" << name << " status=" << portal_detection_status | |
| 62 << " phase=" << portal_detection_phase; | |
| 63 } else { | |
| 64 NET_LOG(EVENT) << "Network is in captive portal state: " << name; | |
| 65 } | |
| 66 return is_captive_portal; | |
| 67 } | |
| 68 | |
| 32 } // namespace | 69 } // namespace |
| 33 | 70 |
| 34 namespace chromeos { | 71 namespace chromeos { |
| 35 | 72 |
| 36 NetworkState::NetworkState(const std::string& path) | 73 NetworkState::NetworkState(const std::string& path) |
| 37 : ManagedState(MANAGED_TYPE_NETWORK, path), | 74 : ManagedState(MANAGED_TYPE_NETWORK, path), |
| 38 visible_(false), | 75 visible_(false), |
| 76 prefix_length_(0), | |
| 39 connectable_(false), | 77 connectable_(false), |
| 40 prefix_length_(0), | 78 is_captive_portal_(false), |
| 41 signal_strength_(0), | 79 signal_strength_(0), |
| 42 cellular_out_of_credits_(false) { | 80 cellular_out_of_credits_(false) { |
| 43 } | 81 } |
| 44 | 82 |
| 45 NetworkState::~NetworkState() { | 83 NetworkState::~NetworkState() { |
| 46 } | 84 } |
| 47 | 85 |
| 48 bool NetworkState::PropertyChanged(const std::string& key, | 86 bool NetworkState::PropertyChanged(const std::string& key, |
| 49 const base::Value& value) { | 87 const base::Value& value) { |
| 50 // Keep care that these properties are the same as in |GetProperties|. | 88 // Keep care that these properties are the same as in |GetProperties|. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 NET_LOG(ERROR) << "NetworkState has no type: " | 172 NET_LOG(ERROR) << "NetworkState has no type: " |
| 135 << shill_property_util::GetNetworkIdFromProperties( | 173 << shill_property_util::GetNetworkIdFromProperties( |
| 136 properties); | 174 properties); |
| 137 return false; | 175 return false; |
| 138 } | 176 } |
| 139 | 177 |
| 140 // By convention, all visible WiFi and WiMAX networks have a | 178 // By convention, all visible WiFi and WiMAX networks have a |
| 141 // SignalStrength > 0. | 179 // SignalStrength > 0. |
| 142 if ((type() == shill::kTypeWifi || type() == shill::kTypeWimax) && | 180 if ((type() == shill::kTypeWifi || type() == shill::kTypeWimax) && |
| 143 visible() && signal_strength_ <= 0) { | 181 visible() && signal_strength_ <= 0) { |
| 144 signal_strength_ = 1; | 182 signal_strength_ = 1; |
| 145 } | 183 } |
| 146 | 184 |
| 185 // Any change to connection state will trigger a complete property update, | |
| 186 // so we update is_captive_portal_ here. | |
| 187 is_captive_portal_ = IsCaptivePortalState(properties, true /* log */); | |
| 188 | |
| 147 // Ensure that the network has a valid name. | 189 // Ensure that the network has a valid name. |
| 148 return UpdateName(properties); | 190 return UpdateName(properties); |
| 149 } | 191 } |
| 150 | 192 |
| 151 void NetworkState::GetStateProperties(base::DictionaryValue* dictionary) const { | 193 void NetworkState::GetStateProperties(base::DictionaryValue* dictionary) const { |
| 152 ManagedState::GetStateProperties(dictionary); | 194 ManagedState::GetStateProperties(dictionary); |
| 153 | 195 |
| 154 // Properties shared by all types. | 196 // Properties shared by all types. |
| 155 dictionary->SetStringWithoutPathExpansion(shill::kGuidProperty, guid()); | 197 dictionary->SetStringWithoutPathExpansion(shill::kGuidProperty, guid()); |
| 156 dictionary->SetStringWithoutPathExpansion(shill::kSecurityClassProperty, | 198 dictionary->SetStringWithoutPathExpansion(shill::kSecurityClassProperty, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 175 } | 217 } |
| 176 | 218 |
| 177 // Wifi properties | 219 // Wifi properties |
| 178 if (NetworkTypePattern::WiFi().MatchesType(type())) { | 220 if (NetworkTypePattern::WiFi().MatchesType(type())) { |
| 179 dictionary->SetStringWithoutPathExpansion(shill::kEapMethodProperty, | 221 dictionary->SetStringWithoutPathExpansion(shill::kEapMethodProperty, |
| 180 eap_method()); | 222 eap_method()); |
| 181 } | 223 } |
| 182 | 224 |
| 183 // Mobile properties | 225 // Mobile properties |
| 184 if (NetworkTypePattern::Mobile().MatchesType(type())) { | 226 if (NetworkTypePattern::Mobile().MatchesType(type())) { |
| 185 dictionary->SetStringWithoutPathExpansion( | 227 dictionary->SetStringWithoutPathExpansion(shill::kNetworkTechnologyProperty, |
| 186 shill::kNetworkTechnologyProperty, | 228 network_technology()); |
| 187 network_technology()); | |
| 188 dictionary->SetStringWithoutPathExpansion(shill::kActivationStateProperty, | 229 dictionary->SetStringWithoutPathExpansion(shill::kActivationStateProperty, |
| 189 activation_state()); | 230 activation_state()); |
| 190 dictionary->SetStringWithoutPathExpansion(shill::kRoamingStateProperty, | 231 dictionary->SetStringWithoutPathExpansion(shill::kRoamingStateProperty, |
| 191 roaming()); | 232 roaming()); |
| 192 dictionary->SetBooleanWithoutPathExpansion(shill::kOutOfCreditsProperty, | 233 dictionary->SetBooleanWithoutPathExpansion(shill::kOutOfCreditsProperty, |
| 193 cellular_out_of_credits()); | 234 cellular_out_of_credits()); |
| 194 } | 235 } |
| 195 } | 236 } |
| 196 | 237 |
| 197 void NetworkState::IPConfigPropertiesChanged( | 238 void NetworkState::IPConfigPropertiesChanged( |
| 198 const base::DictionaryValue& properties) { | 239 const base::DictionaryValue& properties) { |
| 199 for (base::DictionaryValue::Iterator iter(properties); | 240 for (base::DictionaryValue::Iterator iter(properties); !iter.IsAtEnd(); |
| 200 !iter.IsAtEnd(); iter.Advance()) { | 241 iter.Advance()) { |
| 201 std::string key = iter.key(); | 242 std::string key = iter.key(); |
| 202 const base::Value& value = iter.value(); | 243 const base::Value& value = iter.value(); |
| 203 | 244 |
| 204 if (key == shill::kAddressProperty) { | 245 if (key == shill::kAddressProperty) { |
| 205 GetStringValue(key, value, &ip_address_); | 246 GetStringValue(key, value, &ip_address_); |
| 206 } else if (key == shill::kGatewayProperty) { | 247 } else if (key == shill::kGatewayProperty) { |
| 207 GetStringValue(key, value, &gateway_); | 248 GetStringValue(key, value, &gateway_); |
| 208 } else if (key == shill::kNameServersProperty) { | 249 } else if (key == shill::kNameServersProperty) { |
| 209 const base::ListValue* dns_servers; | 250 const base::ListValue* dns_servers; |
| 210 if (value.GetAsList(&dns_servers)) { | 251 if (value.GetAsList(&dns_servers)) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 | 296 |
| 256 bool NetworkState::IsInProfile() const { | 297 bool NetworkState::IsInProfile() const { |
| 257 // kTypeEthernetEap is always saved. We need this check because it does | 298 // kTypeEthernetEap is always saved. We need this check because it does |
| 258 // not show up in the visible list, but its properties may not be available | 299 // not show up in the visible list, but its properties may not be available |
| 259 // when it first shows up in ServiceCompleteList. See crbug.com/355117. | 300 // when it first shows up in ServiceCompleteList. See crbug.com/355117. |
| 260 return !profile_path_.empty() || type() == shill::kTypeEthernetEap; | 301 return !profile_path_.empty() || type() == shill::kTypeEthernetEap; |
| 261 } | 302 } |
| 262 | 303 |
| 263 bool NetworkState::IsPrivate() const { | 304 bool NetworkState::IsPrivate() const { |
| 264 return !profile_path_.empty() && | 305 return !profile_path_.empty() && |
| 265 profile_path_ != NetworkProfileHandler::GetSharedProfilePath(); | 306 profile_path_ != NetworkProfileHandler::GetSharedProfilePath(); |
| 266 } | 307 } |
| 267 | 308 |
| 268 std::string NetworkState::GetDnsServersAsString() const { | 309 std::string NetworkState::GetDnsServersAsString() const { |
| 269 std::string result; | 310 std::string result; |
| 270 for (size_t i = 0; i < dns_servers_.size(); ++i) { | 311 for (size_t i = 0; i < dns_servers_.size(); ++i) { |
| 271 if (i != 0) | 312 if (i != 0) |
| 272 result += ","; | 313 result += ","; |
| 273 result += dns_servers_[i]; | 314 result += dns_servers_[i]; |
| 274 } | 315 } |
| 275 return result; | 316 return result; |
| 276 } | 317 } |
| 277 | 318 |
| 278 std::string NetworkState::GetNetmask() const { | 319 std::string NetworkState::GetNetmask() const { |
| 279 return network_util::PrefixLengthToNetmask(prefix_length_); | 320 return network_util::PrefixLengthToNetmask(prefix_length_); |
| 280 } | 321 } |
| 281 | 322 |
| 282 std::string NetworkState::GetSpecifier() const { | 323 std::string NetworkState::GetSpecifier() const { |
| 283 if (!update_received()) { | 324 if (!update_received()) { |
| 284 NET_LOG(ERROR) << "GetSpecifier called before update: " << path(); | 325 NET_LOG(ERROR) << "GetSpecifier called before update: " << path(); |
| 285 return std::string(); | 326 return std::string(); |
| 286 } | 327 } |
| 287 if (type() == shill::kTypeWifi) | 328 if (type() == shill::kTypeWifi) |
| 288 return name() + "_" + security_class_; | 329 return name() + "_" + security_class_; |
| 289 if (!name().empty()) | 330 if (!name().empty()) |
| 290 return name(); | 331 return name(); |
| 291 return type(); // For unnamed networks such as ethernet. | 332 return type(); // For unnamed networks such as ethernet. |
| 292 } | 333 } |
| 293 | 334 |
| 294 void NetworkState::SetGuid(const std::string& guid) { | 335 void NetworkState::SetGuid(const std::string& guid) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 313 } | 354 } |
| 314 | 355 |
| 315 // static | 356 // static |
| 316 bool NetworkState::StateIsConnecting(const std::string& connection_state) { | 357 bool NetworkState::StateIsConnecting(const std::string& connection_state) { |
| 317 return (connection_state == shill::kStateAssociation || | 358 return (connection_state == shill::kStateAssociation || |
| 318 connection_state == shill::kStateConfiguration || | 359 connection_state == shill::kStateConfiguration || |
| 319 connection_state == shill::kStateCarrier); | 360 connection_state == shill::kStateCarrier); |
| 320 } | 361 } |
| 321 | 362 |
| 322 // static | 363 // static |
| 364 bool NetworkState::NetworkStateIsCaptivePortal( | |
| 365 const base::DictionaryValue& shill_properties) { | |
| 366 return IsCaptivePortalState(shill_properties, false /* log */); | |
| 367 } | |
| 368 | |
| 369 // static | |
| 323 bool NetworkState::ErrorIsValid(const std::string& error) { | 370 bool NetworkState::ErrorIsValid(const std::string& error) { |
| 324 // Shill uses "Unknown" to indicate an unset or cleared error state. | 371 // Shill uses "Unknown" to indicate an unset or cleared error state. |
| 325 return !error.empty() && error != kErrorUnknown; | 372 return !error.empty() && error != kErrorUnknown; |
| 326 } | 373 } |
| 327 | 374 |
| 328 } // namespace chromeos | 375 } // namespace chromeos |
| OLD | NEW |