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 "chromeos/network/shill_property_util.h" | 5 #include "chromeos/network/shill_property_util.h" |
6 | 6 |
7 #include "base/i18n/icu_encoding_detection.h" | 7 #include "base/i18n/icu_encoding_detection.h" |
8 #include "base/i18n/icu_string_conversions.h" | 8 #include "base/i18n/icu_string_conversions.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/strings/utf_string_conversion_utils.h" | 13 #include "base/strings/utf_string_conversion_utils.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chromeos/network/network_event_log.h" | 15 #include "chromeos/device_event_log.h" |
16 #include "chromeos/network/network_ui_data.h" | 16 #include "chromeos/network/network_ui_data.h" |
17 #include "chromeos/network/onc/onc_utils.h" | 17 #include "chromeos/network/onc/onc_utils.h" |
18 #include "third_party/cros_system_api/dbus/service_constants.h" | 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
19 | 19 |
20 namespace chromeos { | 20 namespace chromeos { |
21 | 21 |
22 namespace shill_property_util { | 22 namespace shill_property_util { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 | 59 |
60 } // namespace | 60 } // namespace |
61 | 61 |
62 void SetSSID(const std::string ssid, base::DictionaryValue* properties) { | 62 void SetSSID(const std::string ssid, base::DictionaryValue* properties) { |
63 std::string hex_ssid = base::HexEncode(ssid.c_str(), ssid.size()); | 63 std::string hex_ssid = base::HexEncode(ssid.c_str(), ssid.size()); |
64 properties->SetStringWithoutPathExpansion(shill::kWifiHexSsid, hex_ssid); | 64 properties->SetStringWithoutPathExpansion(shill::kWifiHexSsid, hex_ssid); |
65 } | 65 } |
66 | 66 |
67 std::string GetSSIDFromProperties(const base::DictionaryValue& properties, | 67 std::string GetSSIDFromProperties(const base::DictionaryValue& properties, |
| 68 bool verbose_logging, |
68 bool* unknown_encoding) { | 69 bool* unknown_encoding) { |
69 bool verbose_logging = false; | 70 if (unknown_encoding) |
70 if (unknown_encoding) { | |
71 *unknown_encoding = false; | 71 *unknown_encoding = false; |
72 verbose_logging = true; | |
73 } | |
74 | 72 |
75 // Get name for debugging. | 73 // Get name for debugging. |
76 std::string name; | 74 std::string name; |
77 properties.GetStringWithoutPathExpansion(shill::kNameProperty, &name); | 75 properties.GetStringWithoutPathExpansion(shill::kNameProperty, &name); |
78 | 76 |
79 std::string hex_ssid; | 77 std::string hex_ssid; |
80 properties.GetStringWithoutPathExpansion(shill::kWifiHexSsid, &hex_ssid); | 78 properties.GetStringWithoutPathExpansion(shill::kWifiHexSsid, &hex_ssid); |
81 | 79 |
82 if (hex_ssid.empty()) { | 80 if (hex_ssid.empty()) { |
| 81 // Note: use VLOG here to avoid spamming the event log. |
83 if (verbose_logging) | 82 if (verbose_logging) |
84 NET_LOG_DEBUG("GetSSIDFromProperties: No HexSSID set.", name); | 83 NET_LOG(DEBUG) << "GetSSIDFromProperties: No HexSSID set: " << name; |
85 return std::string(); | 84 return std::string(); |
86 } | 85 } |
87 | 86 |
88 std::string ssid; | 87 std::string ssid; |
89 std::vector<uint8> raw_ssid_bytes; | 88 std::vector<uint8> raw_ssid_bytes; |
90 if (base::HexStringToBytes(hex_ssid, &raw_ssid_bytes)) { | 89 if (base::HexStringToBytes(hex_ssid, &raw_ssid_bytes)) { |
91 ssid = std::string(raw_ssid_bytes.begin(), raw_ssid_bytes.end()); | 90 ssid = std::string(raw_ssid_bytes.begin(), raw_ssid_bytes.end()); |
92 if (verbose_logging) { | 91 if (verbose_logging) { |
93 NET_LOG_DEBUG(base::StringPrintf("GetSSIDFromProperties: %s, SSID: %s", | 92 NET_LOG(DEBUG) << "GetSSIDFromProperties: " << name |
94 hex_ssid.c_str(), ssid.c_str()), name); | 93 << " HexSsid=" << hex_ssid << " SSID=" << ssid; |
95 } | 94 } |
96 } else { | 95 } else { |
97 NET_LOG_ERROR( | 96 NET_LOG(ERROR) << "GetSSIDFromProperties: " << name |
98 base::StringPrintf("GetSSIDFromProperties: Error processing: %s", | 97 << " Error processing HexSsid: " << hex_ssid; |
99 hex_ssid.c_str()), name); | |
100 return std::string(); | 98 return std::string(); |
101 } | 99 } |
102 | 100 |
103 if (base::IsStringUTF8(ssid)) | 101 if (base::IsStringUTF8(ssid)) |
104 return ssid; | 102 return ssid; |
105 | 103 |
106 // Detect encoding and convert to UTF-8. | 104 // Detect encoding and convert to UTF-8. |
107 std::string encoding; | 105 std::string encoding; |
108 if (!base::DetectEncoding(ssid, &encoding)) { | 106 if (!base::DetectEncoding(ssid, &encoding)) { |
109 // TODO(stevenjb): This is currently experimental. If we find a case where | 107 // TODO(stevenjb): This is currently experimental. If we find a case where |
110 // base::DetectEncoding() fails, we need to figure out whether we can use | 108 // base::DetectEncoding() fails, we need to figure out whether we can use |
111 // country_code with ConvertToUtf8(). crbug.com/233267. | 109 // country_code with ConvertToUtf8(). crbug.com/233267. |
112 properties.GetStringWithoutPathExpansion(shill::kCountryProperty, | 110 properties.GetStringWithoutPathExpansion(shill::kCountryProperty, |
113 &encoding); | 111 &encoding); |
114 } | 112 } |
115 std::string utf8_ssid; | 113 std::string utf8_ssid; |
116 if (!encoding.empty() && | 114 if (!encoding.empty() && |
117 base::ConvertToUtf8AndNormalize(ssid, encoding, &utf8_ssid)) { | 115 base::ConvertToUtf8AndNormalize(ssid, encoding, &utf8_ssid)) { |
118 if (utf8_ssid != ssid) { | 116 if (utf8_ssid != ssid) { |
119 if (verbose_logging) { | 117 if (verbose_logging) { |
120 NET_LOG_DEBUG( | 118 NET_LOG(DEBUG) << "GetSSIDFromProperties: " << name |
121 base::StringPrintf("GetSSIDFromProperties: Encoding=%s: %s", | 119 << " Encoding=" << encoding << " SSID=" << ssid |
122 encoding.c_str(), utf8_ssid.c_str()), name); | 120 << " UTF8 SSID=" << utf8_ssid; |
123 } | 121 } |
124 } | 122 } |
125 return utf8_ssid; | 123 return utf8_ssid; |
126 } | 124 } |
127 | 125 |
128 if (unknown_encoding) | 126 if (unknown_encoding) |
129 *unknown_encoding = true; | 127 *unknown_encoding = true; |
130 if (verbose_logging) { | 128 if (verbose_logging) { |
131 NET_LOG_DEBUG( | 129 NET_LOG(DEBUG) << "GetSSIDFromProperties: " << name |
132 base::StringPrintf("GetSSIDFromProperties: Unrecognized Encoding=%s", | 130 << " Unrecognized Encoding=" << encoding; |
133 encoding.c_str()), name); | |
134 } | 131 } |
135 return ssid; | 132 return ssid; |
136 } | 133 } |
137 | 134 |
138 std::string GetNetworkIdFromProperties( | 135 std::string GetNetworkIdFromProperties( |
139 const base::DictionaryValue& properties) { | 136 const base::DictionaryValue& properties) { |
140 if (properties.empty()) | 137 if (properties.empty()) |
141 return "EmptyProperties"; | 138 return "EmptyProperties"; |
142 std::string result; | 139 std::string result; |
143 if (properties.GetStringWithoutPathExpansion(shill::kGuidProperty, &result)) | 140 if (properties.GetStringWithoutPathExpansion(shill::kGuidProperty, &result)) |
144 return result; | 141 return result; |
145 if (properties.GetStringWithoutPathExpansion(shill::kSSIDProperty, &result)) | 142 if (properties.GetStringWithoutPathExpansion(shill::kSSIDProperty, &result)) |
146 return result; | 143 return result; |
147 if (properties.GetStringWithoutPathExpansion(shill::kNameProperty, &result)) | 144 if (properties.GetStringWithoutPathExpansion(shill::kNameProperty, &result)) |
148 return result; | 145 return result; |
149 std::string type = "UnknownType"; | 146 std::string type = "UnknownType"; |
150 properties.GetStringWithoutPathExpansion(shill::kTypeProperty, &type); | 147 properties.GetStringWithoutPathExpansion(shill::kTypeProperty, &type); |
151 return "Unidentified " + type; | 148 return "Unidentified " + type; |
152 } | 149 } |
153 | 150 |
154 std::string GetNameFromProperties(const std::string& service_path, | 151 std::string GetNameFromProperties(const std::string& service_path, |
155 const base::DictionaryValue& properties) { | 152 const base::DictionaryValue& properties) { |
156 std::string name; | 153 std::string name; |
157 properties.GetStringWithoutPathExpansion(shill::kNameProperty, &name); | 154 properties.GetStringWithoutPathExpansion(shill::kNameProperty, &name); |
158 | 155 |
159 std::string validated_name = ValidateUTF8(name); | 156 std::string validated_name = ValidateUTF8(name); |
160 if (validated_name != name) { | 157 if (validated_name != name) { |
161 NET_LOG_DEBUG("GetNameFromProperties", | 158 NET_LOG(DEBUG) << "GetNameFromProperties: " << service_path |
162 base::StringPrintf("Validated name %s: UTF8: %s", | 159 << " Validated name=" << validated_name << " name=" << name; |
163 service_path.c_str(), | |
164 validated_name.c_str())); | |
165 } | 160 } |
166 | 161 |
167 std::string type; | 162 std::string type; |
168 properties.GetStringWithoutPathExpansion(shill::kTypeProperty, &type); | 163 properties.GetStringWithoutPathExpansion(shill::kTypeProperty, &type); |
169 if (type.empty()) { | 164 if (type.empty()) { |
170 NET_LOG_ERROR("GetNameFromProperties: No type", service_path); | 165 NET_LOG(ERROR) << "GetNameFromProperties: " << service_path << " No type."; |
171 return validated_name; | 166 return validated_name; |
172 } | 167 } |
173 if (!NetworkTypePattern::WiFi().MatchesType(type)) | 168 if (!NetworkTypePattern::WiFi().MatchesType(type)) |
174 return validated_name; | 169 return validated_name; |
175 | 170 |
176 bool unknown_ssid_encoding = false; | 171 bool unknown_ssid_encoding = false; |
177 std::string ssid = GetSSIDFromProperties(properties, &unknown_ssid_encoding); | 172 std::string ssid = GetSSIDFromProperties( |
178 if (ssid.empty()) | 173 properties, true /* verbose_logging */, &unknown_ssid_encoding); |
179 NET_LOG_ERROR("GetNameFromProperties", "No SSID set: " + service_path); | 174 if (ssid.empty()) { |
| 175 NET_LOG(ERROR) << "GetNameFromProperties: " << service_path |
| 176 << " No SSID set"; |
| 177 } |
180 | 178 |
181 // Use |validated_name| if |ssid| is empty. | 179 // Use |validated_name| if |ssid| is empty. |
182 // And if the encoding of the SSID is unknown, use |ssid|, which contains raw | 180 // And if the encoding of the SSID is unknown, use |ssid|, which contains raw |
183 // bytes in that case, only if |validated_name| is empty. | 181 // bytes in that case, only if |validated_name| is empty. |
184 if (ssid.empty() || (unknown_ssid_encoding && !validated_name.empty())) | 182 if (ssid.empty() || (unknown_ssid_encoding && !validated_name.empty())) |
185 return validated_name; | 183 return validated_name; |
186 | 184 |
187 if (ssid != validated_name) { | 185 if (ssid != validated_name) { |
188 NET_LOG_DEBUG("GetNameFromProperties", | 186 NET_LOG(DEBUG) << "GetNameFromProperties: " << service_path |
189 base::StringPrintf("%s: SSID: %s, Name: %s", | 187 << " SSID=" << ssid << " Validated name=" << validated_name; |
190 service_path.c_str(), | |
191 ssid.c_str(), | |
192 validated_name.c_str())); | |
193 } | 188 } |
194 return ssid; | 189 return ssid; |
195 } | 190 } |
196 | 191 |
197 scoped_ptr<NetworkUIData> GetUIDataFromValue(const base::Value& ui_data_value) { | 192 scoped_ptr<NetworkUIData> GetUIDataFromValue(const base::Value& ui_data_value) { |
198 std::string ui_data_str; | 193 std::string ui_data_str; |
199 if (!ui_data_value.GetAsString(&ui_data_str)) | 194 if (!ui_data_value.GetAsString(&ui_data_str)) |
200 return scoped_ptr<NetworkUIData>(); | 195 return scoped_ptr<NetworkUIData>(); |
201 if (ui_data_str.empty()) | 196 if (ui_data_str.empty()) |
202 return make_scoped_ptr(new NetworkUIData()); | 197 return make_scoped_ptr(new NetworkUIData()); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 | 253 |
259 // VPN Provider values are read from the "Provider" dictionary, but written | 254 // VPN Provider values are read from the "Provider" dictionary, but written |
260 // with the keys "Provider.Type" and "Provider.Host". | 255 // with the keys "Provider.Type" and "Provider.Host". |
261 // TODO(pneubeck): Simplify this once http://crbug.com/381135 is fixed. | 256 // TODO(pneubeck): Simplify this once http://crbug.com/381135 is fixed. |
262 std::string vpn_provider_type; | 257 std::string vpn_provider_type; |
263 std::string vpn_provider_host; | 258 std::string vpn_provider_host; |
264 if (properties_read_from_shill) { | 259 if (properties_read_from_shill) { |
265 const base::DictionaryValue* provider_properties = NULL; | 260 const base::DictionaryValue* provider_properties = NULL; |
266 if (!service_properties.GetDictionaryWithoutPathExpansion( | 261 if (!service_properties.GetDictionaryWithoutPathExpansion( |
267 shill::kProviderProperty, &provider_properties)) { | 262 shill::kProviderProperty, &provider_properties)) { |
268 NET_LOG_ERROR("Missing VPN provider dict", | 263 NET_LOG(ERROR) << "Missing VPN provider dict: " |
269 GetNetworkIdFromProperties(service_properties)); | 264 << GetNetworkIdFromProperties(service_properties); |
270 } | 265 } |
271 provider_properties->GetStringWithoutPathExpansion(shill::kTypeProperty, | 266 provider_properties->GetStringWithoutPathExpansion(shill::kTypeProperty, |
272 &vpn_provider_type); | 267 &vpn_provider_type); |
273 provider_properties->GetStringWithoutPathExpansion(shill::kHostProperty, | 268 provider_properties->GetStringWithoutPathExpansion(shill::kHostProperty, |
274 &vpn_provider_host); | 269 &vpn_provider_host); |
275 } else { | 270 } else { |
276 service_properties.GetStringWithoutPathExpansion( | 271 service_properties.GetStringWithoutPathExpansion( |
277 shill::kProviderTypeProperty, &vpn_provider_type); | 272 shill::kProviderTypeProperty, &vpn_provider_type); |
278 service_properties.GetStringWithoutPathExpansion( | 273 service_properties.GetStringWithoutPathExpansion( |
279 shill::kProviderHostProperty, &vpn_provider_host); | 274 shill::kProviderHostProperty, &vpn_provider_host); |
280 } | 275 } |
281 success &= !vpn_provider_type.empty(); | 276 success &= !vpn_provider_type.empty(); |
282 dest->SetStringWithoutPathExpansion(shill::kProviderTypeProperty, | 277 dest->SetStringWithoutPathExpansion(shill::kProviderTypeProperty, |
283 vpn_provider_type); | 278 vpn_provider_type); |
284 | 279 |
285 success &= !vpn_provider_host.empty(); | 280 success &= !vpn_provider_host.empty(); |
286 dest->SetStringWithoutPathExpansion(shill::kProviderHostProperty, | 281 dest->SetStringWithoutPathExpansion(shill::kProviderHostProperty, |
287 vpn_provider_host); | 282 vpn_provider_host); |
288 } else if (type == shill::kTypeEthernet || type == shill::kTypeEthernetEap) { | 283 } else if (type == shill::kTypeEthernet || type == shill::kTypeEthernetEap) { |
289 // Ethernet and EthernetEAP don't have any additional identifying | 284 // Ethernet and EthernetEAP don't have any additional identifying |
290 // properties. | 285 // properties. |
291 } else { | 286 } else { |
292 NOTREACHED() << "Unsupported network type " << type; | 287 NOTREACHED() << "Unsupported network type " << type; |
293 success = false; | 288 success = false; |
294 } | 289 } |
295 if (!success) { | 290 if (!success) { |
296 NET_LOG_ERROR("Missing required properties", | 291 NET_LOG(ERROR) << "Missing required properties: " |
297 GetNetworkIdFromProperties(service_properties)); | 292 << GetNetworkIdFromProperties(service_properties); |
298 } | 293 } |
299 return success; | 294 return success; |
300 } | 295 } |
301 | 296 |
302 bool DoIdentifyingPropertiesMatch(const base::DictionaryValue& new_properties, | 297 bool DoIdentifyingPropertiesMatch(const base::DictionaryValue& new_properties, |
303 const base::DictionaryValue& old_properties) { | 298 const base::DictionaryValue& old_properties) { |
304 base::DictionaryValue new_identifying; | 299 base::DictionaryValue new_identifying; |
305 if (!CopyIdentifyingProperties( | 300 if (!CopyIdentifyingProperties( |
306 new_properties, | 301 new_properties, |
307 false /* properties were not read from Shill */, | 302 false /* properties were not read from Shill */, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 LOG(WARNING) | 350 LOG(WARNING) |
356 << "Provider name and country not defined, using code instead: " | 351 << "Provider name and country not defined, using code instead: " |
357 << *home_provider_id; | 352 << *home_provider_id; |
358 } | 353 } |
359 return true; | 354 return true; |
360 } | 355 } |
361 | 356 |
362 } // namespace shill_property_util | 357 } // namespace shill_property_util |
363 | 358 |
364 } // namespace chromeos | 359 } // namespace chromeos |
OLD | NEW |