Chromium Code Reviews| 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 "chromeos/network/network_device_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 10 #include "chromeos/dbus/shill_device_client.h" | |
| 11 #include "chromeos/dbus/shill_manager_client.h" | |
| 12 #include "chromeos/dbus/shill_network_client.h" | |
| 13 #include "chromeos/network/network_event_log.h" | |
| 14 #include "dbus/object_path.h" | |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 16 | |
| 17 namespace chromeos { | |
| 18 | |
| 19 //------------------------------------------------------------------------------ | |
| 20 // NetworkDeviceHandler public methods | |
| 21 | |
| 22 NetworkDeviceHandler::NetworkDeviceHandler() | |
| 23 : devices_ready_(false), | |
| 24 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 25 } | |
| 26 | |
| 27 NetworkDeviceHandler::~NetworkDeviceHandler() { | |
| 28 DBusThreadManager::Get()->GetShillManagerClient()-> | |
| 29 RemovePropertyChangedObserver(this); | |
| 30 } | |
| 31 | |
| 32 void NetworkDeviceHandler::Init() { | |
| 33 ShillManagerClient* shill_manager = | |
| 34 DBusThreadManager::Get()->GetShillManagerClient(); | |
| 35 shill_manager->GetProperties( | |
| 36 base::Bind(&NetworkDeviceHandler::ManagerPropertiesCallback, | |
| 37 weak_ptr_factory_.GetWeakPtr())); | |
| 38 shill_manager->AddPropertyChangedObserver(this); | |
| 39 } | |
| 40 | |
| 41 void NetworkDeviceHandler::AddObserver(Observer* observer) { | |
| 42 observers_.AddObserver(observer); | |
| 43 } | |
| 44 | |
| 45 void NetworkDeviceHandler::RemoveObserver(Observer* observer) { | |
| 46 observers_.RemoveObserver(observer); | |
| 47 } | |
| 48 | |
| 49 //------------------------------------------------------------------------------ | |
| 50 // ShillPropertyChangedObserver overrides | |
| 51 | |
| 52 void NetworkDeviceHandler::OnPropertyChanged(const std::string& key, | |
| 53 const base::Value& value) { | |
| 54 if (key != flimflam::kDevicesProperty) | |
| 55 return; | |
| 56 const base::ListValue* devices = NULL; | |
| 57 if (!value.GetAsList(&devices)) { | |
| 58 LOG(ERROR) << "Failed to parse Devices property."; | |
| 59 return; | |
| 60 } | |
| 61 DevicePropertyChanged(devices); | |
| 62 } | |
| 63 | |
| 64 //------------------------------------------------------------------------------ | |
| 65 // Private methods | |
| 66 | |
| 67 void NetworkDeviceHandler::ManagerPropertiesCallback( | |
| 68 DBusMethodCallStatus call_status, | |
| 69 const base::DictionaryValue& properties) { | |
| 70 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | |
| 71 LOG(ERROR) << "Failed to get Manager properties: " << call_status; | |
| 72 return; | |
| 73 } | |
| 74 const base::ListValue* devices = NULL; | |
| 75 if (!properties.GetListWithoutPathExpansion( | |
| 76 flimflam::kDevicesProperty, &devices)) { | |
| 77 LOG(WARNING) << "Devices property not found"; | |
| 78 return; | |
| 79 } | |
| 80 DevicePropertyChanged(devices); | |
| 81 } | |
| 82 | |
| 83 void NetworkDeviceHandler::DevicePropertyChanged( | |
| 84 const base::ListValue* devices) { | |
| 85 DCHECK(devices); | |
| 86 devices_ready_ = false; | |
| 87 devices_.clear(); | |
| 88 pending_device_paths_.clear(); | |
| 89 for (size_t i = 0; i < devices->GetSize(); i++) { | |
| 90 std::string device_path; | |
| 91 if (!devices->GetString(i, &device_path)) { | |
| 92 LOG(WARNING) << "Failed to parse device[" << i << "]"; | |
| 93 continue; | |
| 94 } | |
| 95 pending_device_paths_.insert(device_path); | |
| 96 DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties( | |
| 97 dbus::ObjectPath(device_path), | |
| 98 base::Bind(&NetworkDeviceHandler::DevicePropertiesCallback, | |
| 99 weak_ptr_factory_.GetWeakPtr(), | |
| 100 device_path)); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void NetworkDeviceHandler::DevicePropertiesCallback( | |
| 105 const std::string& device_path, | |
| 106 DBusMethodCallStatus call_status, | |
| 107 const base::DictionaryValue& properties) { | |
| 108 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | |
| 109 LOG(ERROR) << "Failed to get Device properties for " << device_path | |
| 110 << ": " << call_status; | |
| 111 DeviceReady(device_path); | |
| 112 return; | |
| 113 } | |
| 114 std::string type; | |
| 115 if (!properties.GetStringWithoutPathExpansion( | |
| 116 flimflam::kTypeProperty, &type)) { | |
| 117 LOG(WARNING) << "Failed to parse Type property for " << device_path; | |
| 118 DeviceReady(device_path); | |
| 119 return; | |
| 120 } | |
| 121 Device& device = devices_[device_path]; | |
| 122 device.type = type; | |
| 123 properties.GetBooleanWithoutPathExpansion( | |
| 124 flimflam::kPoweredProperty, &device.powered); | |
| 125 properties.GetBooleanWithoutPathExpansion( | |
| 126 flimflam::kScanningProperty, &device.scanning); | |
| 127 properties.GetIntegerWithoutPathExpansion( | |
| 128 flimflam::kScanIntervalProperty, &device.scan_interval); | |
| 129 | |
| 130 if (!device.powered || | |
| 131 (type != flimflam::kTypeWifi && type != flimflam::kTypeWimax)) { | |
| 132 DeviceReady(device_path); | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 // Get WifiAccessPoint data for each Network property (only for powered | |
| 137 // wifi devices). | |
|
Greg Spencer (Chromium)
2013/01/04 23:57:26
wifi --> wifi or wimax
stevenjb
2013/01/07 15:56:22
Done.
| |
| 138 const base::ListValue* networks = NULL; | |
| 139 if (!properties.GetListWithoutPathExpansion( | |
| 140 flimflam::kNetworksProperty, &networks)) { | |
| 141 LOG(WARNING) << "Failed to parse Networks property for " << device_path; | |
| 142 DeviceReady(device_path); | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 for (size_t i = 0; i < networks->GetSize(); ++i) { | |
| 147 std::string network_path; | |
| 148 if (!networks->GetString(i, &network_path)) { | |
| 149 LOG(WARNING) << "Failed tp parse Networks[" << i << "]" | |
| 150 << " for device: " << device_path; | |
| 151 continue; | |
| 152 } | |
| 153 pending_network_paths_[device_path].insert(network_path); | |
| 154 DBusThreadManager::Get()->GetShillNetworkClient()->GetProperties( | |
| 155 dbus::ObjectPath(device_path), | |
| 156 base::Bind(&NetworkDeviceHandler::NetworkPropertiesCallback, | |
| 157 weak_ptr_factory_.GetWeakPtr(), | |
| 158 device_path, | |
| 159 network_path)); | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 void NetworkDeviceHandler::NetworkPropertiesCallback( | |
| 164 const std::string& device_path, | |
| 165 const std::string& network_path, | |
| 166 DBusMethodCallStatus call_status, | |
| 167 const base::DictionaryValue& properties) { | |
| 168 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | |
| 169 LOG(ERROR) << "Failed to get Network properties for " << network_path | |
| 170 << ", for device: " << device_path << ": " << call_status; | |
| 171 DeviceNetworkReady(device_path, network_path); | |
| 172 return; | |
| 173 } | |
| 174 | |
| 175 // Using the scan interval as a proxy for approximate age. | |
| 176 // TODO(joth): Replace with actual age, when available from dbus. | |
| 177 Device& device = devices_[device_path]; | |
| 178 WifiAccessPoint& wap = device.wifi_access_points[network_path]; | |
| 179 properties.GetStringWithoutPathExpansion( | |
| 180 flimflam::kAddressProperty, &wap.mac_address); | |
| 181 properties.GetStringWithoutPathExpansion( | |
| 182 flimflam::kNameProperty, &wap.name); | |
| 183 int age_seconds = device.scan_interval; | |
| 184 wap.timestamp = base::Time::Now() - base::TimeDelta::FromSeconds(age_seconds); | |
| 185 properties.GetIntegerWithoutPathExpansion( | |
| 186 flimflam::kSignalStrengthProperty, &wap.signal_strength); | |
| 187 properties.GetIntegerWithoutPathExpansion( | |
| 188 flimflam::kWifiChannelProperty, &wap.channel); | |
| 189 DeviceNetworkReady(device_path, network_path); | |
| 190 } | |
| 191 | |
| 192 void NetworkDeviceHandler::DeviceNetworkReady(const std::string& device_path, | |
| 193 const std::string& network_path) { | |
| 194 pending_network_paths_[device_path].erase(network_path); | |
| 195 if (pending_network_paths_[device_path].empty()) | |
| 196 DeviceReady(device_path); | |
| 197 } | |
| 198 | |
| 199 void NetworkDeviceHandler::DeviceReady(const std::string& device_path) { | |
| 200 pending_device_paths_.erase(device_path); | |
| 201 if (pending_device_paths_.empty()) { | |
| 202 devices_ready_ = true; | |
| 203 FOR_EACH_OBSERVER(Observer, observers_, NetworkDevicesUpdated(devices_)); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 } // namespace chromeos | |
| OLD | NEW |