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/cros/cros_network_functions.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/string_tokenizer.h" | |
11 #include "base/stringprintf.h" | |
12 #include "base/values.h" | |
13 #include "chrome/browser/chromeos/cros/sms_watcher.h" | |
14 #include "chromeos/dbus/dbus_thread_manager.h" | |
15 #include "chromeos/dbus/shill_device_client.h" | |
16 #include "chromeos/dbus/shill_ipconfig_client.h" | |
17 #include "chromeos/dbus/shill_manager_client.h" | |
18 #include "chromeos/dbus/shill_network_client.h" | |
19 #include "chromeos/dbus/shill_profile_client.h" | |
20 #include "chromeos/dbus/shill_property_changed_observer.h" | |
21 #include "chromeos/dbus/shill_service_client.h" | |
22 #include "dbus/object_path.h" | |
23 #include "third_party/cros_system_api/dbus/service_constants.h" | |
24 | |
25 using base::DoNothing; | |
26 | |
27 namespace chromeos { | |
28 | |
29 namespace { | |
30 | |
31 // Class to watch network manager's properties without Libcros. | |
32 class NetworkManagerPropertiesWatcher | |
33 : public CrosNetworkWatcher, | |
34 public ShillPropertyChangedObserver { | |
35 public: | |
36 NetworkManagerPropertiesWatcher( | |
37 const NetworkPropertiesWatcherCallback& callback) | |
38 : callback_(callback) { | |
39 DBusThreadManager::Get()->GetShillManagerClient()-> | |
40 AddPropertyChangedObserver(this); | |
41 } | |
42 | |
43 virtual ~NetworkManagerPropertiesWatcher() { | |
44 DBusThreadManager::Get()->GetShillManagerClient()-> | |
45 RemovePropertyChangedObserver(this); | |
46 } | |
47 | |
48 virtual void OnPropertyChanged(const std::string& name, | |
49 const base::Value& value) { | |
50 callback_.Run(flimflam::kFlimflamServicePath, name, value); | |
51 } | |
52 private: | |
53 NetworkPropertiesWatcherCallback callback_; | |
54 }; | |
55 | |
56 // Class to watch network service's properties without Libcros. | |
57 class NetworkServicePropertiesWatcher | |
58 : public CrosNetworkWatcher, | |
59 public ShillPropertyChangedObserver { | |
60 public: | |
61 NetworkServicePropertiesWatcher( | |
62 const NetworkPropertiesWatcherCallback& callback, | |
63 const std::string& service_path) : service_path_(service_path), | |
64 callback_(callback) { | |
65 DBusThreadManager::Get()->GetShillServiceClient()-> | |
66 AddPropertyChangedObserver(dbus::ObjectPath(service_path), this); | |
67 } | |
68 | |
69 virtual ~NetworkServicePropertiesWatcher() { | |
70 DBusThreadManager::Get()->GetShillServiceClient()-> | |
71 RemovePropertyChangedObserver(dbus::ObjectPath(service_path_), this); | |
72 } | |
73 | |
74 virtual void OnPropertyChanged(const std::string& name, | |
75 const base::Value& value) { | |
76 callback_.Run(service_path_, name, value); | |
77 } | |
78 | |
79 private: | |
80 std::string service_path_; | |
81 NetworkPropertiesWatcherCallback callback_; | |
82 }; | |
83 | |
84 // Class to watch network device's properties without Libcros. | |
85 class NetworkDevicePropertiesWatcher | |
86 : public CrosNetworkWatcher, | |
87 public ShillPropertyChangedObserver { | |
88 public: | |
89 NetworkDevicePropertiesWatcher( | |
90 const NetworkPropertiesWatcherCallback& callback, | |
91 const std::string& device_path) : device_path_(device_path), | |
92 callback_(callback) { | |
93 DBusThreadManager::Get()->GetShillDeviceClient()-> | |
94 AddPropertyChangedObserver(dbus::ObjectPath(device_path), this); | |
95 } | |
96 | |
97 virtual ~NetworkDevicePropertiesWatcher() { | |
98 DBusThreadManager::Get()->GetShillDeviceClient()-> | |
99 RemovePropertyChangedObserver(dbus::ObjectPath(device_path_), this); | |
100 } | |
101 | |
102 virtual void OnPropertyChanged(const std::string& name, | |
103 const base::Value& value) { | |
104 callback_.Run(device_path_, name, value); | |
105 } | |
106 | |
107 private: | |
108 std::string device_path_; | |
109 NetworkPropertiesWatcherCallback callback_; | |
110 }; | |
111 | |
112 // Gets a string property from dictionary. | |
113 bool GetStringProperty(const base::DictionaryValue& dictionary, | |
114 const std::string& key, | |
115 std::string* out) { | |
116 const bool result = dictionary.GetStringWithoutPathExpansion(key, out); | |
117 LOG_IF(WARNING, !result) << "Cannnot get property " << key; | |
118 return result; | |
119 } | |
120 | |
121 // Gets an int64 property from dictionary. | |
122 bool GetInt64Property(const base::DictionaryValue& dictionary, | |
123 const std::string& key, | |
124 int64* out) { | |
125 // Int64 value is stored as a double because it cannot be fitted in int32. | |
126 double value_double = 0; | |
127 const bool result = dictionary.GetDoubleWithoutPathExpansion(key, | |
128 &value_double); | |
129 if (result) | |
130 *out = value_double; | |
131 else | |
132 LOG(WARNING) << "Cannnot get property " << key; | |
133 return result; | |
134 } | |
135 | |
136 // Gets a base::Time property from dictionary. | |
137 bool GetTimeProperty(const base::DictionaryValue& dictionary, | |
138 const std::string& key, | |
139 base::Time* out) { | |
140 int64 value_int64 = 0; | |
141 if (!GetInt64Property(dictionary, key, &value_int64)) | |
142 return false; | |
143 *out = base::Time::FromInternalValue(value_int64); | |
144 return true; | |
145 } | |
146 | |
147 // Does nothing. Used as a callback. | |
148 void DoNothingWithCallStatus(DBusMethodCallStatus call_status) {} | |
149 | |
150 // Ignores errors. | |
151 void IgnoreErrors(const std::string& error_name, | |
152 const std::string& error_message) {} | |
153 | |
154 // A callback used to implement CrosRequest*Properties functions. | |
155 void RunCallbackWithDictionaryValue(const NetworkPropertiesCallback& callback, | |
156 const std::string& path, | |
157 DBusMethodCallStatus call_status, | |
158 const base::DictionaryValue& value) { | |
159 callback.Run(path, call_status == DBUS_METHOD_CALL_SUCCESS ? &value : NULL); | |
160 } | |
161 | |
162 // A callback used to implement CrosRequest*Properties functions. | |
163 void RunCallbackWithDictionaryValueNoStatus( | |
164 const NetworkPropertiesCallback& callback, | |
165 const std::string& path, | |
166 const base::DictionaryValue& value) { | |
167 callback.Run(path, &value); | |
168 } | |
169 | |
170 // A callback used to implement the error callback for CrosRequest*Properties | |
171 // functions. | |
172 void RunCallbackWithDictionaryValueError( | |
173 const NetworkPropertiesCallback& callback, | |
174 const std::string& path, | |
175 const std::string& error_name, | |
176 const std::string& error_message) { | |
177 callback.Run(path, NULL); | |
178 } | |
179 | |
180 // Used as a callback for ShillManagerClient::GetService | |
181 void OnGetService(const NetworkPropertiesCallback& callback, | |
182 const dbus::ObjectPath& service_path) { | |
183 VLOG(1) << "OnGetServiceService: " << service_path.value(); | |
184 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
185 service_path, base::Bind(&RunCallbackWithDictionaryValue, | |
186 callback, | |
187 service_path.value())); | |
188 } | |
189 | |
190 // A callback used to call a NetworkOperationCallback on error. | |
191 void OnNetworkActionError(const NetworkOperationCallback& callback, | |
192 const std::string& path, | |
193 const std::string& error_name, | |
194 const std::string& error_message) { | |
195 if (error_name.empty()) | |
196 callback.Run(path, NETWORK_METHOD_ERROR_LOCAL, ""); | |
197 else | |
198 callback.Run(path, NETWORK_METHOD_ERROR_REMOTE, error_message); | |
199 } | |
200 | |
201 IPConfigType ParseIPConfigType(const std::string& type) { | |
202 if (type == flimflam::kTypeIPv4) | |
203 return IPCONFIG_TYPE_IPV4; | |
204 if (type == flimflam::kTypeIPv6) | |
205 return IPCONFIG_TYPE_IPV6; | |
206 if (type == flimflam::kTypeDHCP) | |
207 return IPCONFIG_TYPE_DHCP; | |
208 if (type == flimflam::kTypeBOOTP) | |
209 return IPCONFIG_TYPE_BOOTP; | |
210 if (type == flimflam::kTypeZeroConf) | |
211 return IPCONFIG_TYPE_ZEROCONF; | |
212 if (type == flimflam::kTypeDHCP6) | |
213 return IPCONFIG_TYPE_DHCP6; | |
214 if (type == flimflam::kTypePPP) | |
215 return IPCONFIG_TYPE_PPP; | |
216 return IPCONFIG_TYPE_UNKNOWN; | |
217 } | |
218 | |
219 // Converts a list of name servers to a string. | |
220 std::string ConvertNameSerersListToString(const base::ListValue& name_servers) { | |
221 std::string result; | |
222 for (size_t i = 0; i != name_servers.GetSize(); ++i) { | |
223 std::string name_server; | |
224 if (!name_servers.GetString(i, &name_server)) { | |
225 LOG(ERROR) << "name_servers[" << i << "] is not a string."; | |
226 continue; | |
227 } | |
228 if (!result.empty()) | |
229 result += ","; | |
230 result += name_server; | |
231 } | |
232 return result; | |
233 } | |
234 | |
235 // Gets NetworkIPConfigVector populated with data from a | |
236 // given DBus object path. | |
237 // | |
238 // returns true on success. | |
239 bool ParseIPConfig(const std::string& device_path, | |
240 const std::string& ipconfig_path, | |
241 NetworkIPConfigVector* ipconfig_vector) { | |
242 ShillIPConfigClient* ipconfig_client = | |
243 DBusThreadManager::Get()->GetShillIPConfigClient(); | |
244 // TODO(hashimoto): Remove this blocking D-Bus method call. crosbug.com/29902 | |
245 scoped_ptr<base::DictionaryValue> properties( | |
246 ipconfig_client->CallGetPropertiesAndBlock( | |
247 dbus::ObjectPath(ipconfig_path))); | |
248 if (!properties.get()) | |
249 return false; | |
250 | |
251 std::string type_string; | |
252 properties->GetStringWithoutPathExpansion(flimflam::kMethodProperty, | |
253 &type_string); | |
254 std::string address; | |
255 properties->GetStringWithoutPathExpansion(flimflam::kAddressProperty, | |
256 &address); | |
257 int32 prefix_len = 0; | |
258 properties->GetIntegerWithoutPathExpansion(flimflam::kPrefixlenProperty, | |
259 &prefix_len); | |
260 std::string gateway; | |
261 properties->GetStringWithoutPathExpansion(flimflam::kGatewayProperty, | |
262 &gateway); | |
263 base::ListValue* name_servers = NULL; | |
264 std::string name_servers_string; | |
265 // store nameservers as a comma delimited list | |
266 if (properties->GetListWithoutPathExpansion(flimflam::kNameServersProperty, | |
267 &name_servers)) { | |
268 name_servers_string = ConvertNameSerersListToString(*name_servers); | |
269 } else { | |
270 LOG(ERROR) << "Cannot get name servers."; | |
271 } | |
272 ipconfig_vector->push_back( | |
273 NetworkIPConfig(device_path, | |
274 ParseIPConfigType(type_string), | |
275 address, | |
276 CrosPrefixLengthToNetmask(prefix_len), | |
277 gateway, | |
278 name_servers_string)); | |
279 return true; | |
280 } | |
281 | |
282 void ListIPConfigsCallback(const NetworkGetIPConfigsCallback& callback, | |
283 const std::string& device_path, | |
284 DBusMethodCallStatus call_status, | |
285 const base::DictionaryValue& properties) { | |
286 NetworkIPConfigVector ipconfig_vector; | |
287 std::string hardware_address; | |
288 const ListValue* ips = NULL; | |
289 if (call_status != DBUS_METHOD_CALL_SUCCESS || | |
290 !properties.GetListWithoutPathExpansion(flimflam::kIPConfigsProperty, | |
291 &ips)) { | |
292 callback.Run(ipconfig_vector, hardware_address); | |
293 return; | |
294 } | |
295 | |
296 for (size_t i = 0; i < ips->GetSize(); i++) { | |
297 std::string ipconfig_path; | |
298 if (!ips->GetString(i, &ipconfig_path)) { | |
299 LOG(WARNING) << "Found NULL ip for device " << device_path; | |
300 continue; | |
301 } | |
302 ParseIPConfig(device_path, ipconfig_path, &ipconfig_vector); | |
303 } | |
304 // Get the hardware address as well. | |
305 properties.GetStringWithoutPathExpansion(flimflam::kAddressProperty, | |
306 &hardware_address); | |
307 | |
308 callback.Run(ipconfig_vector, hardware_address); | |
309 } | |
310 | |
311 } // namespace | |
312 | |
313 SMS::SMS() | |
314 : validity(0), | |
315 msgclass(0) { | |
316 } | |
317 | |
318 SMS::~SMS() {} | |
319 | |
320 bool CrosActivateCellularModem(const std::string& service_path, | |
321 const std::string& carrier) { | |
322 return DBusThreadManager::Get()->GetShillServiceClient()-> | |
323 CallActivateCellularModemAndBlock(dbus::ObjectPath(service_path), | |
324 carrier); | |
325 } | |
326 | |
327 void CrosSetNetworkServiceProperty(const std::string& service_path, | |
328 const std::string& property, | |
329 const base::Value& value) { | |
330 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( | |
331 dbus::ObjectPath(service_path), property, value, | |
332 base::Bind(&DoNothing), | |
333 base::Bind(&IgnoreErrors)); | |
334 } | |
335 | |
336 void CrosClearNetworkServiceProperty(const std::string& service_path, | |
337 const std::string& property) { | |
338 DBusThreadManager::Get()->GetShillServiceClient()->ClearProperty( | |
339 dbus::ObjectPath(service_path), property, base::Bind(&DoNothing), | |
340 base::Bind(&IgnoreErrors)); | |
341 } | |
342 | |
343 void CrosSetNetworkDeviceProperty(const std::string& device_path, | |
344 const std::string& property, | |
345 const base::Value& value) { | |
346 DBusThreadManager::Get()->GetShillDeviceClient()->SetProperty( | |
347 dbus::ObjectPath(device_path), property, value, base::Bind(&DoNothing), | |
348 base::Bind(&IgnoreErrors)); | |
349 } | |
350 | |
351 void CrosSetNetworkIPConfigProperty(const std::string& ipconfig_path, | |
352 const std::string& property, | |
353 const base::Value& value) { | |
354 DBusThreadManager::Get()->GetShillIPConfigClient()->SetProperty( | |
355 dbus::ObjectPath(ipconfig_path), property, value, | |
356 base::Bind(&DoNothingWithCallStatus)); | |
357 } | |
358 | |
359 void CrosSetNetworkManagerProperty(const std::string& property, | |
360 const base::Value& value) { | |
361 DBusThreadManager::Get()->GetShillManagerClient()->SetProperty( | |
362 property, value, base::Bind(&DoNothing), | |
363 base::Bind(&IgnoreErrors)); | |
364 } | |
365 | |
366 void CrosDeleteServiceFromProfile(const std::string& profile_path, | |
367 const std::string& service_path) { | |
368 DBusThreadManager::Get()->GetShillProfileClient()->DeleteEntry( | |
369 dbus::ObjectPath(profile_path), | |
370 service_path, | |
371 base::Bind(&DoNothing), | |
372 base::Bind(&IgnoreErrors)); | |
373 } | |
374 | |
375 CrosNetworkWatcher* CrosMonitorNetworkManagerProperties( | |
376 const NetworkPropertiesWatcherCallback& callback) { | |
377 return new NetworkManagerPropertiesWatcher(callback); | |
378 } | |
379 | |
380 CrosNetworkWatcher* CrosMonitorNetworkServiceProperties( | |
381 const NetworkPropertiesWatcherCallback& callback, | |
382 const std::string& service_path) { | |
383 return new NetworkServicePropertiesWatcher(callback, service_path); | |
384 } | |
385 | |
386 CrosNetworkWatcher* CrosMonitorNetworkDeviceProperties( | |
387 const NetworkPropertiesWatcherCallback& callback, | |
388 const std::string& device_path) { | |
389 return new NetworkDevicePropertiesWatcher(callback, device_path); | |
390 } | |
391 | |
392 CrosNetworkWatcher* CrosMonitorSMS(const std::string& modem_device_path, | |
393 MonitorSMSCallback callback) { | |
394 return new SMSWatcher(modem_device_path, callback); | |
395 } | |
396 | |
397 void CrosRequestNetworkServiceConnect( | |
398 const std::string& service_path, | |
399 const NetworkOperationCallback& callback) { | |
400 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | |
401 dbus::ObjectPath(service_path), | |
402 base::Bind(callback, service_path, NETWORK_METHOD_ERROR_NONE, | |
403 std::string()), | |
404 base::Bind(&OnNetworkActionError, callback, service_path)); | |
405 } | |
406 | |
407 void CrosRequestNetworkManagerProperties( | |
408 const NetworkPropertiesCallback& callback) { | |
409 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties( | |
410 base::Bind(&RunCallbackWithDictionaryValue, | |
411 callback, | |
412 flimflam::kFlimflamServicePath)); | |
413 } | |
414 | |
415 void CrosRequestNetworkServiceProperties( | |
416 const std::string& service_path, | |
417 const NetworkPropertiesCallback& callback) { | |
418 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
419 dbus::ObjectPath(service_path), | |
420 base::Bind(&RunCallbackWithDictionaryValue, callback, service_path)); | |
421 } | |
422 | |
423 void CrosRequestNetworkDeviceProperties( | |
424 const std::string& device_path, | |
425 const NetworkPropertiesCallback& callback) { | |
426 DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties( | |
427 dbus::ObjectPath(device_path), | |
428 base::Bind(&RunCallbackWithDictionaryValue, callback, device_path)); | |
429 } | |
430 | |
431 void CrosRequestNetworkProfileProperties( | |
432 const std::string& profile_path, | |
433 const NetworkPropertiesCallback& callback) { | |
434 DBusThreadManager::Get()->GetShillProfileClient()->GetProperties( | |
435 dbus::ObjectPath(profile_path), | |
436 base::Bind(&RunCallbackWithDictionaryValueNoStatus, | |
437 callback, profile_path), | |
438 base::Bind(&RunCallbackWithDictionaryValueError, callback, profile_path)); | |
439 } | |
440 | |
441 void CrosRequestNetworkProfileEntryProperties( | |
442 const std::string& profile_path, | |
443 const std::string& profile_entry_path, | |
444 const NetworkPropertiesCallback& callback) { | |
445 DBusThreadManager::Get()->GetShillProfileClient()->GetEntry( | |
446 dbus::ObjectPath(profile_path), | |
447 profile_entry_path, | |
448 base::Bind(&RunCallbackWithDictionaryValueNoStatus, | |
449 callback, | |
450 profile_entry_path), | |
451 base::Bind(&RunCallbackWithDictionaryValueError, | |
452 callback, | |
453 profile_entry_path)); | |
454 } | |
455 | |
456 void CrosRequestHiddenWifiNetworkProperties( | |
457 const std::string& ssid, | |
458 const std::string& security, | |
459 const NetworkPropertiesCallback& callback) { | |
460 base::DictionaryValue properties; | |
461 properties.SetWithoutPathExpansion( | |
462 flimflam::kModeProperty, | |
463 new base::StringValue(flimflam::kModeManaged)); | |
464 properties.SetWithoutPathExpansion( | |
465 flimflam::kTypeProperty, | |
466 new base::StringValue(flimflam::kTypeWifi)); | |
467 properties.SetWithoutPathExpansion( | |
468 flimflam::kSSIDProperty, | |
469 new base::StringValue(ssid)); | |
470 properties.SetWithoutPathExpansion( | |
471 flimflam::kSecurityProperty, | |
472 new base::StringValue(security)); | |
473 // shill.Manger.GetService() will apply the property changes in | |
474 // |properties| and return a new or existing service to OnGetService(). | |
475 // OnGetService will then call GetProperties which will then call callback. | |
476 DBusThreadManager::Get()->GetShillManagerClient()->GetService( | |
477 properties, base::Bind(&OnGetService, callback), | |
478 base::Bind(&IgnoreErrors)); | |
479 } | |
480 | |
481 void CrosRequestVirtualNetworkProperties( | |
482 const std::string& service_name, | |
483 const std::string& server_hostname, | |
484 const std::string& provider_type, | |
485 const NetworkPropertiesCallback& callback) { | |
486 base::DictionaryValue properties; | |
487 properties.SetWithoutPathExpansion( | |
488 flimflam::kTypeProperty, | |
489 new base::StringValue(flimflam::kTypeVPN)); | |
490 properties.SetWithoutPathExpansion( | |
491 flimflam::kProviderNameProperty, | |
492 new base::StringValue(service_name)); | |
493 properties.SetWithoutPathExpansion( | |
494 flimflam::kProviderHostProperty, | |
495 new base::StringValue(server_hostname)); | |
496 properties.SetWithoutPathExpansion( | |
497 flimflam::kProviderTypeProperty, | |
498 new base::StringValue(provider_type)); | |
499 // The actual value of Domain does not matter, so just use service_name. | |
500 properties.SetWithoutPathExpansion( | |
501 flimflam::kVPNDomainProperty, | |
502 new base::StringValue(service_name)); | |
503 | |
504 // shill.Manger.GetService() will apply the property changes in | |
505 // |properties| and pass a new or existing service to OnGetService(). | |
506 // OnGetService will then call GetProperties which will then call callback. | |
507 DBusThreadManager::Get()->GetShillManagerClient()->GetService( | |
508 properties, base::Bind(&OnGetService, callback), | |
509 base::Bind(&IgnoreErrors)); | |
510 } | |
511 | |
512 void CrosRequestNetworkServiceDisconnect(const std::string& service_path) { | |
513 DBusThreadManager::Get()->GetShillServiceClient()->Disconnect( | |
514 dbus::ObjectPath(service_path), base::Bind(&DoNothing), | |
515 base::Bind(&IgnoreErrors)); | |
516 } | |
517 | |
518 void CrosRequestRemoveNetworkService(const std::string& service_path) { | |
519 DBusThreadManager::Get()->GetShillServiceClient()->Remove( | |
520 dbus::ObjectPath(service_path), base::Bind(&DoNothing), | |
521 base::Bind(&IgnoreErrors)); | |
522 } | |
523 | |
524 void CrosRequestNetworkScan(const std::string& network_type) { | |
525 DBusThreadManager::Get()->GetShillManagerClient()->RequestScan( | |
526 network_type, base::Bind(&DoNothing), | |
527 base::Bind(&IgnoreErrors)); | |
528 } | |
529 | |
530 void CrosRequestNetworkDeviceEnable(const std::string& network_type, | |
531 bool enable) { | |
532 if (enable) { | |
533 DBusThreadManager::Get()->GetShillManagerClient()->EnableTechnology( | |
534 network_type, base::Bind(&DoNothing), | |
535 base::Bind(&IgnoreErrors)); | |
536 } else { | |
537 DBusThreadManager::Get()->GetShillManagerClient()->DisableTechnology( | |
538 network_type, base::Bind(&DoNothing), | |
539 base::Bind(&IgnoreErrors)); | |
540 } | |
541 } | |
542 | |
543 void CrosRequestRequirePin(const std::string& device_path, | |
544 const std::string& pin, | |
545 bool enable, | |
546 const NetworkOperationCallback& callback) { | |
547 DBusThreadManager::Get()->GetShillDeviceClient()->RequirePin( | |
548 dbus::ObjectPath(device_path), pin, enable, | |
549 base::Bind(callback, device_path, NETWORK_METHOD_ERROR_NONE, | |
550 std::string()), | |
551 base::Bind(&OnNetworkActionError, callback, device_path)); | |
552 } | |
553 | |
554 void CrosRequestEnterPin(const std::string& device_path, | |
555 const std::string& pin, | |
556 const NetworkOperationCallback& callback) { | |
557 DBusThreadManager::Get()->GetShillDeviceClient()->EnterPin( | |
558 dbus::ObjectPath(device_path), pin, | |
559 base::Bind(callback, device_path, NETWORK_METHOD_ERROR_NONE, | |
560 std::string()), | |
561 base::Bind(&OnNetworkActionError, callback, device_path)); | |
562 } | |
563 | |
564 void CrosRequestUnblockPin(const std::string& device_path, | |
565 const std::string& unblock_code, | |
566 const std::string& pin, | |
567 const NetworkOperationCallback& callback) { | |
568 DBusThreadManager::Get()->GetShillDeviceClient()->UnblockPin( | |
569 dbus::ObjectPath(device_path), unblock_code, pin, | |
570 base::Bind(callback, device_path, NETWORK_METHOD_ERROR_NONE, | |
571 std::string()), | |
572 base::Bind(&OnNetworkActionError, callback, device_path)); | |
573 } | |
574 | |
575 void CrosRequestChangePin(const std::string& device_path, | |
576 const std::string& old_pin, | |
577 const std::string& new_pin, | |
578 const NetworkOperationCallback& callback) { | |
579 DBusThreadManager::Get()->GetShillDeviceClient()->ChangePin( | |
580 dbus::ObjectPath(device_path), old_pin, new_pin, | |
581 base::Bind(callback, device_path, NETWORK_METHOD_ERROR_NONE, | |
582 std::string()), | |
583 base::Bind(&OnNetworkActionError, callback, device_path)); | |
584 } | |
585 | |
586 void CrosProposeScan(const std::string& device_path) { | |
587 DBusThreadManager::Get()->GetShillDeviceClient()->ProposeScan( | |
588 dbus::ObjectPath(device_path), base::Bind(&DoNothingWithCallStatus)); | |
589 } | |
590 | |
591 void CrosRequestCellularRegister(const std::string& device_path, | |
592 const std::string& network_id, | |
593 const NetworkOperationCallback& callback) { | |
594 DBusThreadManager::Get()->GetShillDeviceClient()->Register( | |
595 dbus::ObjectPath(device_path), network_id, | |
596 base::Bind(callback, device_path, NETWORK_METHOD_ERROR_NONE, | |
597 std::string()), | |
598 base::Bind(&OnNetworkActionError, callback, device_path)); | |
599 } | |
600 | |
601 bool CrosSetOfflineMode(bool offline) { | |
602 base::FundamentalValue value(offline); | |
603 DBusThreadManager::Get()->GetShillManagerClient()->SetProperty( | |
604 flimflam::kOfflineModeProperty, value, base::Bind(&DoNothing), | |
605 base::Bind(&IgnoreErrors)); | |
606 return true; | |
607 } | |
608 | |
609 void CrosListIPConfigs(const std::string& device_path, | |
610 const NetworkGetIPConfigsCallback& callback) { | |
611 const dbus::ObjectPath device_object_path(device_path); | |
612 DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties( | |
613 device_object_path, | |
614 base::Bind(&ListIPConfigsCallback, callback, device_path)); | |
615 } | |
616 | |
617 bool CrosListIPConfigsAndBlock(const std::string& device_path, | |
618 NetworkIPConfigVector* ipconfig_vector, | |
619 std::vector<std::string>* ipconfig_paths, | |
620 std::string* hardware_address) { | |
621 if (hardware_address) | |
622 hardware_address->clear(); | |
623 const dbus::ObjectPath device_object_path(device_path); | |
624 ShillDeviceClient* device_client = | |
625 DBusThreadManager::Get()->GetShillDeviceClient(); | |
626 // TODO(hashimoto): Remove this blocking D-Bus method call. | |
627 // crosbug.com/29902 | |
628 scoped_ptr<base::DictionaryValue> properties( | |
629 device_client->CallGetPropertiesAndBlock(device_object_path)); | |
630 if (!properties.get()) | |
631 return false; | |
632 | |
633 ListValue* ips = NULL; | |
634 if (!properties->GetListWithoutPathExpansion( | |
635 flimflam::kIPConfigsProperty, &ips)) | |
636 return false; | |
637 | |
638 for (size_t i = 0; i < ips->GetSize(); i++) { | |
639 std::string ipconfig_path; | |
640 if (!ips->GetString(i, &ipconfig_path)) { | |
641 LOG(WARNING) << "Found NULL ip for device " << device_path; | |
642 continue; | |
643 } | |
644 ParseIPConfig(device_path, ipconfig_path, ipconfig_vector); | |
645 if (ipconfig_paths) | |
646 ipconfig_paths->push_back(ipconfig_path); | |
647 } | |
648 // Store the hardware address as well. | |
649 if (hardware_address) | |
650 properties->GetStringWithoutPathExpansion(flimflam::kAddressProperty, | |
651 hardware_address); | |
652 return true; | |
653 } | |
654 | |
655 void CrosRequestIPConfigRefresh(const std::string& ipconfig_path) { | |
656 DBusThreadManager::Get()->GetShillIPConfigClient()->Refresh( | |
657 dbus::ObjectPath(ipconfig_path), | |
658 base::Bind(&DoNothingWithCallStatus)); | |
659 } | |
660 | |
661 bool CrosGetWifiAccessPoints(WifiAccessPointVector* result) { | |
662 scoped_ptr<base::DictionaryValue> manager_properties( | |
663 DBusThreadManager::Get()->GetShillManagerClient()-> | |
664 CallGetPropertiesAndBlock()); | |
665 if (!manager_properties.get()) { | |
666 LOG(WARNING) << "Couldn't read managers's properties"; | |
667 return false; | |
668 } | |
669 | |
670 base::ListValue* devices = NULL; | |
671 if (!manager_properties->GetListWithoutPathExpansion( | |
672 flimflam::kDevicesProperty, &devices)) { | |
673 LOG(WARNING) << flimflam::kDevicesProperty << " property not found"; | |
674 return false; | |
675 } | |
676 const base::Time now = base::Time::Now(); | |
677 bool found_at_least_one_device = false; | |
678 result->clear(); | |
679 for (size_t i = 0; i < devices->GetSize(); i++) { | |
680 std::string device_path; | |
681 if (!devices->GetString(i, &device_path)) { | |
682 LOG(WARNING) << "Couldn't get devices[" << i << "]"; | |
683 continue; | |
684 } | |
685 scoped_ptr<base::DictionaryValue> device_properties( | |
686 DBusThreadManager::Get()->GetShillDeviceClient()-> | |
687 CallGetPropertiesAndBlock(dbus::ObjectPath(device_path))); | |
688 if (!device_properties.get()) { | |
689 LOG(WARNING) << "Couldn't read device's properties " << device_path; | |
690 continue; | |
691 } | |
692 | |
693 base::ListValue* networks = NULL; | |
694 if (!device_properties->GetListWithoutPathExpansion( | |
695 flimflam::kNetworksProperty, &networks)) | |
696 continue; // Some devices do not list networks, e.g. ethernet. | |
697 | |
698 base::Value* device_powered_value = NULL; | |
699 bool device_powered = false; | |
700 if (device_properties->GetWithoutPathExpansion( | |
701 flimflam::kPoweredProperty, &device_powered_value) && | |
702 device_powered_value->GetAsBoolean(&device_powered) && | |
703 !device_powered) | |
704 continue; // Skip devices that are not powered up. | |
705 | |
706 int scan_interval = 0; | |
707 device_properties->GetIntegerWithoutPathExpansion( | |
708 flimflam::kScanIntervalProperty, &scan_interval); | |
709 | |
710 found_at_least_one_device = true; | |
711 for (size_t j = 0; j < networks->GetSize(); j++) { | |
712 std::string network_path; | |
713 if (!networks->GetString(j, &network_path)) { | |
714 LOG(WARNING) << "Couldn't get networks[" << j << "]"; | |
715 continue; | |
716 } | |
717 | |
718 scoped_ptr<base::DictionaryValue> network_properties( | |
719 DBusThreadManager::Get()->GetShillNetworkClient()-> | |
720 CallGetPropertiesAndBlock(dbus::ObjectPath(network_path))); | |
721 if (!network_properties.get()) { | |
722 LOG(WARNING) << "Couldn't read network's properties " << network_path; | |
723 continue; | |
724 } | |
725 | |
726 // Using the scan interval as a proxy for approximate age. | |
727 // TODO(joth): Replace with actual age, when available from dbus. | |
728 const int age_seconds = scan_interval; | |
729 WifiAccessPoint ap; | |
730 network_properties->GetStringWithoutPathExpansion( | |
731 flimflam::kAddressProperty, &ap.mac_address); | |
732 network_properties->GetStringWithoutPathExpansion( | |
733 flimflam::kNameProperty, &ap.name); | |
734 ap.timestamp = now - base::TimeDelta::FromSeconds(age_seconds); | |
735 network_properties->GetIntegerWithoutPathExpansion( | |
736 flimflam::kSignalStrengthProperty, &ap.signal_strength); | |
737 network_properties->GetIntegerWithoutPathExpansion( | |
738 flimflam::kWifiChannelProperty, &ap.channel); | |
739 result->push_back(ap); | |
740 } | |
741 } | |
742 if (!found_at_least_one_device) | |
743 return false; // No powered device found that has a 'Networks' array. | |
744 return true; | |
745 } | |
746 | |
747 void CrosConfigureService(const base::DictionaryValue& properties) { | |
748 DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService( | |
749 properties, base::Bind(&DoNothing), | |
750 base::Bind(&IgnoreErrors)); | |
751 } | |
752 | |
753 std::string CrosPrefixLengthToNetmask(int32 prefix_length) { | |
754 std::string netmask; | |
755 // Return the empty string for invalid inputs. | |
756 if (prefix_length < 0 || prefix_length > 32) | |
757 return netmask; | |
758 for (int i = 0; i < 4; i++) { | |
759 int remainder = 8; | |
760 if (prefix_length >= 8) { | |
761 prefix_length -= 8; | |
762 } else { | |
763 remainder = prefix_length; | |
764 prefix_length = 0; | |
765 } | |
766 if (i > 0) | |
767 netmask += "."; | |
768 int value = remainder == 0 ? 0 : | |
769 ((2L << (remainder - 1)) - 1) << (8 - remainder); | |
770 netmask += StringPrintf("%d", value); | |
771 } | |
772 return netmask; | |
773 } | |
774 | |
775 int32 CrosNetmaskToPrefixLength(const std::string& netmask) { | |
776 int count = 0; | |
777 int prefix_length = 0; | |
778 StringTokenizer t(netmask, "."); | |
779 while (t.GetNext()) { | |
780 // If there are more than 4 numbers, then it's invalid. | |
781 if (count == 4) | |
782 return -1; | |
783 | |
784 std::string token = t.token(); | |
785 // If we already found the last mask and the current one is not | |
786 // "0" then the netmask is invalid. For example, 255.224.255.0 | |
787 if (prefix_length / 8 != count) { | |
788 if (token != "0") | |
789 return -1; | |
790 } else if (token == "255") { | |
791 prefix_length += 8; | |
792 } else if (token == "254") { | |
793 prefix_length += 7; | |
794 } else if (token == "252") { | |
795 prefix_length += 6; | |
796 } else if (token == "248") { | |
797 prefix_length += 5; | |
798 } else if (token == "240") { | |
799 prefix_length += 4; | |
800 } else if (token == "224") { | |
801 prefix_length += 3; | |
802 } else if (token == "192") { | |
803 prefix_length += 2; | |
804 } else if (token == "128") { | |
805 prefix_length += 1; | |
806 } else if (token == "0") { | |
807 prefix_length += 0; | |
808 } else { | |
809 // mask is not a valid number. | |
810 return -1; | |
811 } | |
812 count++; | |
813 } | |
814 if (count < 4) | |
815 return -1; | |
816 return prefix_length; | |
817 } | |
818 | |
819 // Changes the active cellular carrier. | |
820 void CrosSetCarrier(const std::string& device_path, | |
821 const std::string& carrier, | |
822 const NetworkOperationCallback& callback) { | |
823 DBusThreadManager::Get()->GetShillDeviceClient()->SetCarrier( | |
824 dbus::ObjectPath(device_path), carrier, | |
825 base::Bind(callback, device_path, NETWORK_METHOD_ERROR_NONE, | |
826 std::string()), | |
827 base::Bind(&OnNetworkActionError, callback, device_path)); | |
828 } | |
829 | |
830 } // namespace chromeos | |
OLD | NEW |