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_configuration_handler.h" | 5 #include "chromeos/network/network_configuration_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "chromeos/dbus/dbus_thread_manager.h" | 19 #include "chromeos/dbus/dbus_thread_manager.h" |
20 #include "chromeos/dbus/shill_manager_client.h" | 20 #include "chromeos/dbus/shill_manager_client.h" |
21 #include "chromeos/dbus/shill_profile_client.h" | 21 #include "chromeos/dbus/shill_profile_client.h" |
22 #include "chromeos/dbus/shill_service_client.h" | 22 #include "chromeos/dbus/shill_service_client.h" |
| 23 #include "chromeos/network/device_state.h" |
23 #include "chromeos/network/network_event_log.h" | 24 #include "chromeos/network/network_event_log.h" |
24 #include "chromeos/network/network_state_handler.h" | 25 #include "chromeos/network/network_state_handler.h" |
25 #include "chromeos/network/shill_property_util.h" | 26 #include "chromeos/network/shill_property_util.h" |
26 #include "dbus/object_path.h" | 27 #include "dbus/object_path.h" |
27 #include "third_party/cros_system_api/dbus/service_constants.h" | 28 #include "third_party/cros_system_api/dbus/service_constants.h" |
28 | 29 |
29 namespace chromeos { | 30 namespace chromeos { |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 // Strip surrounding "" from keys (if present). | 34 // Strip surrounding "" from keys (if present). |
34 std::string StripQuotations(const std::string& in_str) { | 35 std::string StripQuotations(const std::string& in_str) { |
35 size_t len = in_str.length(); | 36 size_t len = in_str.length(); |
36 if (len >= 2 && in_str[0] == '"' && in_str[len-1] == '"') | 37 if (len >= 2 && in_str[0] == '"' && in_str[len-1] == '"') |
37 return in_str.substr(1, len-2); | 38 return in_str.substr(1, len-2); |
38 return in_str; | 39 return in_str; |
39 } | 40 } |
40 | 41 |
41 void InvokeErrorCallback(const std::string& service_path, | 42 void InvokeErrorCallback(const std::string& service_path, |
42 const network_handler::ErrorCallback& error_callback, | 43 const network_handler::ErrorCallback& error_callback, |
43 const std::string& error_name) { | 44 const std::string& error_name) { |
44 std::string error_msg = "Config Error: " + error_name; | 45 std::string error_msg = "Config Error: " + error_name; |
45 NET_LOG_ERROR(error_msg, service_path); | 46 NET_LOG_ERROR(error_msg, service_path); |
46 network_handler::RunErrorCallback( | 47 network_handler::RunErrorCallback( |
47 error_callback, service_path, error_name, error_msg); | 48 error_callback, service_path, error_name, error_msg); |
48 } | 49 } |
49 | 50 |
50 void GetPropertiesCallback( | |
51 const network_handler::DictionaryResultCallback& callback, | |
52 const network_handler::ErrorCallback& error_callback, | |
53 const std::string& service_path, | |
54 DBusMethodCallStatus call_status, | |
55 const base::DictionaryValue& properties) { | |
56 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | |
57 // Because network services are added and removed frequently, we will see | |
58 // failures regularly, so don't log these. | |
59 network_handler::RunErrorCallback(error_callback, | |
60 service_path, | |
61 network_handler::kDBusFailedError, | |
62 network_handler::kDBusFailedErrorMessage); | |
63 return; | |
64 } | |
65 if (callback.is_null()) | |
66 return; | |
67 | |
68 // Get the correct name from WifiHex if necessary. | |
69 scoped_ptr<base::DictionaryValue> properties_copy(properties.DeepCopy()); | |
70 std::string name = | |
71 shill_property_util::GetNameFromProperties(service_path, properties); | |
72 if (!name.empty()) | |
73 properties_copy->SetStringWithoutPathExpansion(shill::kNameProperty, name); | |
74 callback.Run(service_path, *properties_copy.get()); | |
75 } | |
76 | |
77 void SetNetworkProfileErrorCallback( | 51 void SetNetworkProfileErrorCallback( |
78 const std::string& service_path, | 52 const std::string& service_path, |
79 const std::string& profile_path, | 53 const std::string& profile_path, |
80 const network_handler::ErrorCallback& error_callback, | 54 const network_handler::ErrorCallback& error_callback, |
81 const std::string& dbus_error_name, | 55 const std::string& dbus_error_name, |
82 const std::string& dbus_error_message) { | 56 const std::string& dbus_error_message) { |
83 network_handler::ShillErrorCallbackFunction( | 57 network_handler::ShillErrorCallbackFunction( |
84 "Config.SetNetworkProfile Failed: " + profile_path, | 58 "Config.SetNetworkProfile Failed: " + profile_path, |
85 service_path, error_callback, | 59 service_path, error_callback, |
86 dbus_error_name, dbus_error_message); | 60 dbus_error_name, dbus_error_message); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 std::map<std::string, std::string> profile_delete_entries_; | 175 std::map<std::string, std::string> profile_delete_entries_; |
202 | 176 |
203 DISALLOW_COPY_AND_ASSIGN(ProfileEntryDeleter); | 177 DISALLOW_COPY_AND_ASSIGN(ProfileEntryDeleter); |
204 }; | 178 }; |
205 | 179 |
206 // NetworkConfigurationHandler | 180 // NetworkConfigurationHandler |
207 | 181 |
208 void NetworkConfigurationHandler::GetProperties( | 182 void NetworkConfigurationHandler::GetProperties( |
209 const std::string& service_path, | 183 const std::string& service_path, |
210 const network_handler::DictionaryResultCallback& callback, | 184 const network_handler::DictionaryResultCallback& callback, |
211 const network_handler::ErrorCallback& error_callback) const { | 185 const network_handler::ErrorCallback& error_callback) { |
212 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | 186 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( |
213 dbus::ObjectPath(service_path), | 187 dbus::ObjectPath(service_path), |
214 base::Bind(&GetPropertiesCallback, | 188 base::Bind(&NetworkConfigurationHandler::GetPropertiesCallback, |
215 callback, error_callback, service_path)); | 189 AsWeakPtr(), callback, error_callback, service_path)); |
216 } | 190 } |
217 | 191 |
218 void NetworkConfigurationHandler::SetProperties( | 192 void NetworkConfigurationHandler::SetProperties( |
219 const std::string& service_path, | 193 const std::string& service_path, |
220 const base::DictionaryValue& properties, | 194 const base::DictionaryValue& properties, |
221 const base::Closure& callback, | 195 const base::Closure& callback, |
222 const network_handler::ErrorCallback& error_callback) { | 196 const network_handler::ErrorCallback& error_callback) { |
223 if (properties.empty()) { | 197 if (properties.empty()) { |
224 if (!callback.is_null()) | 198 if (!callback.is_null()) |
225 callback.Run(); | 199 callback.Run(); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 | 336 |
363 void NetworkConfigurationHandler::ProfileEntryDeleterCompleted( | 337 void NetworkConfigurationHandler::ProfileEntryDeleterCompleted( |
364 const std::string& service_path) { | 338 const std::string& service_path) { |
365 std::map<std::string, ProfileEntryDeleter*>::iterator iter = | 339 std::map<std::string, ProfileEntryDeleter*>::iterator iter = |
366 profile_entry_deleters_.find(service_path); | 340 profile_entry_deleters_.find(service_path); |
367 DCHECK(iter != profile_entry_deleters_.end()); | 341 DCHECK(iter != profile_entry_deleters_.end()); |
368 delete iter->second; | 342 delete iter->second; |
369 profile_entry_deleters_.erase(iter); | 343 profile_entry_deleters_.erase(iter); |
370 } | 344 } |
371 | 345 |
| 346 void NetworkConfigurationHandler::GetPropertiesCallback( |
| 347 const network_handler::DictionaryResultCallback& callback, |
| 348 const network_handler::ErrorCallback& error_callback, |
| 349 const std::string& service_path, |
| 350 DBusMethodCallStatus call_status, |
| 351 const base::DictionaryValue& properties) { |
| 352 if (call_status != DBUS_METHOD_CALL_SUCCESS) { |
| 353 // Because network services are added and removed frequently, we will see |
| 354 // failures regularly, so don't log these. |
| 355 network_handler::RunErrorCallback(error_callback, |
| 356 service_path, |
| 357 network_handler::kDBusFailedError, |
| 358 network_handler::kDBusFailedErrorMessage); |
| 359 return; |
| 360 } |
| 361 if (callback.is_null()) |
| 362 return; |
| 363 |
| 364 // Get the correct name from WifiHex if necessary. |
| 365 scoped_ptr<base::DictionaryValue> properties_copy(properties.DeepCopy()); |
| 366 std::string name = |
| 367 shill_property_util::GetNameFromProperties(service_path, properties); |
| 368 if (!name.empty()) |
| 369 properties_copy->SetStringWithoutPathExpansion(shill::kNameProperty, name); |
| 370 |
| 371 // Get the IPConfig properties from the device and store them in "IPConfigs" |
| 372 // (plural) in the properties dictionary. |
| 373 std::string device; |
| 374 properties_copy->GetStringWithoutPathExpansion( |
| 375 shill::kDeviceProperty, &device); |
| 376 const DeviceState* device_state = |
| 377 network_state_handler_->GetDeviceState(device); |
| 378 if (device_state) { |
| 379 // Convert IPConfig dictionary to ListValue |
| 380 base::ListValue* ip_configs = new base::ListValue; |
| 381 for (base::DictionaryValue::Iterator iter(device_state->ip_configs()); |
| 382 !iter.IsAtEnd(); iter.Advance()) { |
| 383 ip_configs->Append(iter.value().DeepCopy()); |
| 384 } |
| 385 properties_copy->SetWithoutPathExpansion( |
| 386 shill::kIPConfigsProperty, ip_configs); |
| 387 } else { |
| 388 NET_LOG_ERROR("GetPropertiesCallback: no device: " + device, service_path); |
| 389 } |
| 390 callback.Run(service_path, *properties_copy.get()); |
| 391 } |
| 392 |
372 void NetworkConfigurationHandler::SetPropertiesSuccessCallback( | 393 void NetworkConfigurationHandler::SetPropertiesSuccessCallback( |
373 const std::string& service_path, | 394 const std::string& service_path, |
374 const base::Closure& callback) { | 395 const base::Closure& callback) { |
375 if (!callback.is_null()) | 396 if (!callback.is_null()) |
376 callback.Run(); | 397 callback.Run(); |
377 network_state_handler_->RequestUpdateForNetwork(service_path); | 398 network_state_handler_->RequestUpdateForNetwork(service_path); |
378 } | 399 } |
379 | 400 |
380 void NetworkConfigurationHandler::SetPropertiesErrorCallback( | 401 void NetworkConfigurationHandler::SetPropertiesErrorCallback( |
381 const std::string& service_path, | 402 const std::string& service_path, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 | 450 |
430 // static | 451 // static |
431 NetworkConfigurationHandler* NetworkConfigurationHandler::InitializeForTest( | 452 NetworkConfigurationHandler* NetworkConfigurationHandler::InitializeForTest( |
432 NetworkStateHandler* network_state_handler) { | 453 NetworkStateHandler* network_state_handler) { |
433 NetworkConfigurationHandler* handler = new NetworkConfigurationHandler(); | 454 NetworkConfigurationHandler* handler = new NetworkConfigurationHandler(); |
434 handler->Init(network_state_handler); | 455 handler->Init(network_state_handler); |
435 return handler; | 456 return handler; |
436 } | 457 } |
437 | 458 |
438 } // namespace chromeos | 459 } // namespace chromeos |
OLD | NEW |