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/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/values.h" | |
9 #include "chromeos/network/network_event_log.h" | 8 #include "chromeos/network/network_event_log.h" |
10 #include "chromeos/network/network_profile_handler.h" | 9 #include "chromeos/network/network_profile_handler.h" |
11 #include "chromeos/network/network_type_pattern.h" | 10 #include "chromeos/network/network_type_pattern.h" |
12 #include "chromeos/network/network_util.h" | 11 #include "chromeos/network/network_util.h" |
| 12 #include "chromeos/network/onc/onc_utils.h" |
13 #include "chromeos/network/shill_property_util.h" | 13 #include "chromeos/network/shill_property_util.h" |
14 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const char kErrorUnknown[] = "Unknown"; | 18 const char kErrorUnknown[] = "Unknown"; |
19 | 19 |
20 bool ConvertListValueToStringVector(const base::ListValue& string_list, | 20 bool ConvertListValueToStringVector(const base::ListValue& string_list, |
21 std::vector<std::string>* result) { | 21 std::vector<std::string>* result) { |
22 for (size_t i = 0; i < string_list.GetSize(); ++i) { | 22 for (size_t i = 0; i < string_list.GetSize(); ++i) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 return false; | 55 return false; |
56 } | 56 } |
57 | 57 |
58 } // namespace | 58 } // namespace |
59 | 59 |
60 namespace chromeos { | 60 namespace chromeos { |
61 | 61 |
62 NetworkState::NetworkState(const std::string& path) | 62 NetworkState::NetworkState(const std::string& path) |
63 : ManagedState(MANAGED_TYPE_NETWORK, path), | 63 : ManagedState(MANAGED_TYPE_NETWORK, path), |
| 64 visible_(false), |
64 connectable_(false), | 65 connectable_(false), |
65 prefix_length_(0), | 66 prefix_length_(0), |
66 signal_strength_(0), | 67 signal_strength_(0), |
67 activate_over_non_cellular_networks_(false), | 68 activate_over_non_cellular_networks_(false), |
68 cellular_out_of_credits_(false), | 69 cellular_out_of_credits_(false), |
69 has_ca_cert_nss_(false) { | 70 has_ca_cert_nss_(false) { |
70 } | 71 } |
71 | 72 |
72 NetworkState::~NetworkState() { | 73 NetworkState::~NetworkState() { |
73 } | 74 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 } else if (key == shill::kDeviceProperty) { | 114 } else if (key == shill::kDeviceProperty) { |
114 return GetStringValue(key, value, &device_path_); | 115 return GetStringValue(key, value, &device_path_); |
115 } else if (key == shill::kGuidProperty) { | 116 } else if (key == shill::kGuidProperty) { |
116 return GetStringValue(key, value, &guid_); | 117 return GetStringValue(key, value, &guid_); |
117 } else if (key == shill::kProfileProperty) { | 118 } else if (key == shill::kProfileProperty) { |
118 return GetStringValue(key, value, &profile_path_); | 119 return GetStringValue(key, value, &profile_path_); |
119 } else if (key == shill::kActivateOverNonCellularNetworkProperty) { | 120 } else if (key == shill::kActivateOverNonCellularNetworkProperty) { |
120 return GetBooleanValue(key, value, &activate_over_non_cellular_networks_); | 121 return GetBooleanValue(key, value, &activate_over_non_cellular_networks_); |
121 } else if (key == shill::kOutOfCreditsProperty) { | 122 } else if (key == shill::kOutOfCreditsProperty) { |
122 return GetBooleanValue(key, value, &cellular_out_of_credits_); | 123 return GetBooleanValue(key, value, &cellular_out_of_credits_); |
| 124 } else if (key == shill::kProxyConfigProperty) { |
| 125 std::string proxy_config_str; |
| 126 if (!value.GetAsString(&proxy_config_str)) { |
| 127 NET_LOG_ERROR("Failed to parse " + key, path()); |
| 128 return false; |
| 129 } |
| 130 |
| 131 proxy_config_.Clear(); |
| 132 if (proxy_config_str.empty()) |
| 133 return true; |
| 134 |
| 135 scoped_ptr<base::DictionaryValue> proxy_config_dict( |
| 136 onc::ReadDictionaryFromJson(proxy_config_str)); |
| 137 if (proxy_config_dict) { |
| 138 // Warning: The DictionaryValue returned from |
| 139 // ReadDictionaryFromJson/JSONParser is an optimized derived class that |
| 140 // doesn't allow releasing ownership of nested values. A Swap in the wrong |
| 141 // order leads to memory access errors. |
| 142 proxy_config_.MergeDictionary(proxy_config_dict.get()); |
| 143 } else { |
| 144 NET_LOG_ERROR("Failed to parse " + key, path()); |
| 145 } |
| 146 return true; |
123 } | 147 } |
124 return false; | 148 return false; |
125 } | 149 } |
126 | 150 |
127 bool NetworkState::InitialPropertiesReceived( | 151 bool NetworkState::InitialPropertiesReceived( |
128 const base::DictionaryValue& properties) { | 152 const base::DictionaryValue& properties) { |
129 NET_LOG_DEBUG("InitialPropertiesReceived", path()); | 153 NET_LOG_DEBUG("InitialPropertiesReceived", path()); |
130 bool changed = false; | 154 bool changed = false; |
131 if (!properties.HasKey(shill::kTypeProperty)) { | 155 if (!properties.HasKey(shill::kTypeProperty)) { |
132 NET_LOG_ERROR("NetworkState has no type", | 156 NET_LOG_ERROR("NetworkState has no type", |
133 shill_property_util::GetNetworkIdFromProperties(properties)); | 157 shill_property_util::GetNetworkIdFromProperties(properties)); |
134 return false; | 158 return false; |
135 } | 159 } |
136 // Ensure that the network has a valid name. | 160 // Ensure that the network has a valid name. |
137 changed |= UpdateName(properties); | 161 changed |= UpdateName(properties); |
138 | 162 |
139 // Set the has_ca_cert_nss_ property. | 163 // Set the has_ca_cert_nss_ property. |
140 bool had_ca_cert_nss = has_ca_cert_nss_; | 164 bool had_ca_cert_nss = has_ca_cert_nss_; |
141 has_ca_cert_nss_ = IsCaCertNssSet(properties); | 165 has_ca_cert_nss_ = IsCaCertNssSet(properties); |
142 changed |= had_ca_cert_nss != has_ca_cert_nss_; | 166 changed |= had_ca_cert_nss != has_ca_cert_nss_; |
143 | 167 |
144 // By convention, all visible WiFi networks have a SignalStrength > 0. | 168 // By convention, all visible WiFi networks have a SignalStrength > 0. |
145 if (type() == shill::kTypeWifi) { | 169 if (visible() && type() == shill::kTypeWifi) { |
146 if (signal_strength_ <= 0) | 170 if (signal_strength_ <= 0) |
147 signal_strength_ = 1; | 171 signal_strength_ = 1; |
148 } | 172 } |
149 | 173 |
150 return changed; | 174 return changed; |
151 } | 175 } |
152 | 176 |
153 void NetworkState::GetStateProperties(base::DictionaryValue* dictionary) const { | 177 void NetworkState::GetStateProperties(base::DictionaryValue* dictionary) const { |
154 ManagedState::GetStateProperties(dictionary); | 178 ManagedState::GetStateProperties(dictionary); |
155 | 179 |
156 // Properties shared by all types. | 180 // Properties shared by all types. |
157 dictionary->SetStringWithoutPathExpansion(shill::kGuidProperty, guid()); | 181 dictionary->SetStringWithoutPathExpansion(shill::kGuidProperty, guid()); |
158 dictionary->SetStringWithoutPathExpansion(shill::kStateProperty, | |
159 connection_state()); | |
160 dictionary->SetStringWithoutPathExpansion(shill::kSecurityProperty, | 182 dictionary->SetStringWithoutPathExpansion(shill::kSecurityProperty, |
161 security()); | 183 security()); |
162 if (!error().empty()) | |
163 dictionary->SetStringWithoutPathExpansion(shill::kErrorProperty, error()); | |
164 | 184 |
| 185 if (visible()) { |
| 186 if (!error().empty()) |
| 187 dictionary->SetStringWithoutPathExpansion(shill::kErrorProperty, error()); |
| 188 dictionary->SetStringWithoutPathExpansion(shill::kStateProperty, |
| 189 connection_state()); |
| 190 } |
| 191 |
| 192 // Wireless properties |
165 if (!NetworkTypePattern::Wireless().MatchesType(type())) | 193 if (!NetworkTypePattern::Wireless().MatchesType(type())) |
166 return; | 194 return; |
167 | 195 |
168 // Wireless properties | 196 if (visible()) { |
169 dictionary->SetBooleanWithoutPathExpansion(shill::kConnectableProperty, | 197 dictionary->SetBooleanWithoutPathExpansion(shill::kConnectableProperty, |
170 connectable()); | 198 connectable()); |
171 dictionary->SetIntegerWithoutPathExpansion(shill::kSignalStrengthProperty, | 199 dictionary->SetIntegerWithoutPathExpansion(shill::kSignalStrengthProperty, |
172 signal_strength()); | 200 signal_strength()); |
| 201 } |
173 | 202 |
174 // Wifi properties | 203 // Wifi properties |
175 if (NetworkTypePattern::WiFi().MatchesType(type())) { | 204 if (NetworkTypePattern::WiFi().MatchesType(type())) { |
176 dictionary->SetStringWithoutPathExpansion(shill::kEapMethodProperty, | 205 dictionary->SetStringWithoutPathExpansion(shill::kEapMethodProperty, |
177 eap_method()); | 206 eap_method()); |
178 } | 207 } |
179 | 208 |
180 // Mobile properties | 209 // Mobile properties |
181 if (NetworkTypePattern::Mobile().MatchesType(type())) { | 210 if (NetworkTypePattern::Mobile().MatchesType(type())) { |
182 dictionary->SetStringWithoutPathExpansion( | 211 dictionary->SetStringWithoutPathExpansion( |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 } | 258 } |
230 } | 259 } |
231 } | 260 } |
232 | 261 |
233 bool NetworkState::RequiresActivation() const { | 262 bool NetworkState::RequiresActivation() const { |
234 return (type() == shill::kTypeCellular && | 263 return (type() == shill::kTypeCellular && |
235 activation_state() != shill::kActivationStateActivated && | 264 activation_state() != shill::kActivationStateActivated && |
236 activation_state() != shill::kActivationStateUnknown); | 265 activation_state() != shill::kActivationStateUnknown); |
237 } | 266 } |
238 | 267 |
| 268 std::string NetworkState::connection_state() const { |
| 269 if (!visible()) |
| 270 return shill::kStateDisconnect; |
| 271 return connection_state_; |
| 272 } |
| 273 |
239 bool NetworkState::IsConnectedState() const { | 274 bool NetworkState::IsConnectedState() const { |
240 return StateIsConnected(connection_state_); | 275 return visible() && StateIsConnected(connection_state_); |
241 } | 276 } |
242 | 277 |
243 bool NetworkState::IsConnectingState() const { | 278 bool NetworkState::IsConnectingState() const { |
244 return StateIsConnecting(connection_state_); | 279 return visible() && StateIsConnecting(connection_state_); |
| 280 } |
| 281 |
| 282 bool NetworkState::IsInProfile() const { |
| 283 // kTypeEthernetEap is always saved. We need this check because it does |
| 284 // not show up in the visible list, but its properties may not be available |
| 285 // when it first shows up in ServiceCompleteList. See crbug.com/355117. |
| 286 return !profile_path_.empty() || type() == shill::kTypeEthernetEap; |
245 } | 287 } |
246 | 288 |
247 bool NetworkState::IsPrivate() const { | 289 bool NetworkState::IsPrivate() const { |
248 return !profile_path_.empty() && | 290 return !profile_path_.empty() && |
249 profile_path_ != NetworkProfileHandler::GetSharedProfilePath(); | 291 profile_path_ != NetworkProfileHandler::GetSharedProfilePath(); |
250 } | 292 } |
251 | 293 |
252 std::string NetworkState::GetDnsServersAsString() const { | 294 std::string NetworkState::GetDnsServersAsString() const { |
253 std::string result; | 295 std::string result; |
254 for (size_t i = 0; i < dns_servers_.size(); ++i) { | 296 for (size_t i = 0; i < dns_servers_.size(); ++i) { |
255 if (i != 0) | 297 if (i != 0) |
256 result += ","; | 298 result += ","; |
257 result += dns_servers_[i]; | 299 result += dns_servers_[i]; |
258 } | 300 } |
259 return result; | 301 return result; |
260 } | 302 } |
261 | 303 |
262 std::string NetworkState::GetNetmask() const { | 304 std::string NetworkState::GetNetmask() const { |
263 return network_util::PrefixLengthToNetmask(prefix_length_); | 305 return network_util::PrefixLengthToNetmask(prefix_length_); |
264 } | 306 } |
265 | 307 |
| 308 std::string NetworkState::GetSpecifier() const { |
| 309 if (!update_received()) { |
| 310 NET_LOG_ERROR("GetSpecifier called before update", path()); |
| 311 return std::string(); |
| 312 } |
| 313 if (type() == shill::kTypeWifi) |
| 314 return name() + "_" + security_; |
| 315 if (!name().empty()) |
| 316 return name(); |
| 317 return type(); // For unnamed networks such as ethernet. |
| 318 } |
| 319 |
266 void NetworkState::SetGuid(const std::string& guid) { | 320 void NetworkState::SetGuid(const std::string& guid) { |
267 guid_ = guid; | 321 guid_ = guid; |
268 } | 322 } |
269 | 323 |
270 bool NetworkState::UpdateName(const base::DictionaryValue& properties) { | 324 bool NetworkState::UpdateName(const base::DictionaryValue& properties) { |
271 std::string updated_name = | 325 std::string updated_name = |
272 shill_property_util::GetNameFromProperties(path(), properties); | 326 shill_property_util::GetNameFromProperties(path(), properties); |
273 if (updated_name != name()) { | 327 if (updated_name != name()) { |
274 set_name(updated_name); | 328 set_name(updated_name); |
275 return true; | 329 return true; |
(...skipping 15 matching lines...) Expand all Loading... |
291 connection_state == shill::kStateCarrier); | 345 connection_state == shill::kStateCarrier); |
292 } | 346 } |
293 | 347 |
294 // static | 348 // static |
295 bool NetworkState::ErrorIsValid(const std::string& error) { | 349 bool NetworkState::ErrorIsValid(const std::string& error) { |
296 // Shill uses "Unknown" to indicate an unset or cleared error state. | 350 // Shill uses "Unknown" to indicate an unset or cleared error state. |
297 return !error.empty() && error != kErrorUnknown; | 351 return !error.empty() && error != kErrorUnknown; |
298 } | 352 } |
299 | 353 |
300 } // namespace chromeos | 354 } // namespace chromeos |
OLD | NEW |