| 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_device_handler.h" | 5 #include "chromeos/network/network_device_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chromeos/dbus/dbus_thread_manager.h" | 9 #include "chromeos/dbus/dbus_thread_manager.h" |
| 10 #include "chromeos/dbus/shill_device_client.h" | 10 #include "chromeos/dbus/shill_device_client.h" |
| 11 #include "chromeos/dbus/shill_manager_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" | 12 #include "chromeos/network/network_event_log.h" |
| 13 #include "chromeos/network/network_handler_callbacks.h" |
| 14 #include "dbus/object_path.h" | 14 #include "dbus/object_path.h" |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" | 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 16 | 16 |
| 17 namespace { |
| 18 const char kLogModule[] = "NetworkDeviceHandler"; |
| 19 } |
| 20 |
| 17 namespace chromeos { | 21 namespace chromeos { |
| 18 | 22 |
| 23 static NetworkDeviceHandler* g_network_device_handler = NULL; |
| 24 |
| 19 //------------------------------------------------------------------------------ | 25 //------------------------------------------------------------------------------ |
| 20 // NetworkDeviceHandler public methods | 26 // NetworkDeviceHandler public methods |
| 21 | 27 |
| 22 NetworkDeviceHandler::NetworkDeviceHandler() | |
| 23 : devices_ready_(false), | |
| 24 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 25 } | |
| 26 | |
| 27 NetworkDeviceHandler::~NetworkDeviceHandler() { | 28 NetworkDeviceHandler::~NetworkDeviceHandler() { |
| 28 DBusThreadManager::Get()->GetShillManagerClient()-> | 29 DBusThreadManager::Get()->GetShillManagerClient()-> |
| 29 RemovePropertyChangedObserver(this); | 30 RemovePropertyChangedObserver(this); |
| 30 } | 31 } |
| 31 | 32 |
| 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) { | 33 void NetworkDeviceHandler::AddObserver(Observer* observer) { |
| 42 observers_.AddObserver(observer); | 34 observers_.AddObserver(observer); |
| 43 } | 35 } |
| 44 | 36 |
| 45 void NetworkDeviceHandler::RemoveObserver(Observer* observer) { | 37 void NetworkDeviceHandler::RemoveObserver(Observer* observer) { |
| 46 observers_.RemoveObserver(observer); | 38 observers_.RemoveObserver(observer); |
| 47 } | 39 } |
| 48 | 40 |
| 41 // static |
| 42 void NetworkDeviceHandler::Initialize() { |
| 43 CHECK(!g_network_device_handler); |
| 44 g_network_device_handler = new NetworkDeviceHandler(); |
| 45 g_network_device_handler->Init(); |
| 46 } |
| 47 |
| 48 // static |
| 49 void NetworkDeviceHandler::Shutdown() { |
| 50 CHECK(g_network_device_handler); |
| 51 delete g_network_device_handler; |
| 52 g_network_device_handler = NULL; |
| 53 } |
| 54 |
| 55 // static |
| 56 NetworkDeviceHandler* NetworkDeviceHandler::Get() { |
| 57 CHECK(g_network_device_handler) |
| 58 << "NetworkDeviceHandler::Get() called before Initialize()"; |
| 59 return g_network_device_handler; |
| 60 } |
| 61 |
| 49 //------------------------------------------------------------------------------ | 62 //------------------------------------------------------------------------------ |
| 50 // ShillPropertyChangedObserver overrides | 63 // ShillPropertyChangedObserver overrides |
| 51 | 64 |
| 52 void NetworkDeviceHandler::OnPropertyChanged(const std::string& key, | 65 void NetworkDeviceHandler::OnPropertyChanged(const std::string& key, |
| 53 const base::Value& value) { | 66 const base::Value& value) { |
| 54 if (key != flimflam::kDevicesProperty) | 67 if (key != flimflam::kDevicesProperty) |
| 55 return; | 68 return; |
| 56 const base::ListValue* devices = NULL; | 69 const base::ListValue* devices = NULL; |
| 57 if (!value.GetAsList(&devices)) { | 70 if (!value.GetAsList(&devices)) { |
| 58 LOG(ERROR) << "Failed to parse Devices property."; | 71 LOG(ERROR) << "Failed to parse Devices property."; |
| 59 return; | 72 return; |
| 60 } | 73 } |
| 61 DevicePropertyChanged(devices); | 74 DevicePropertyChanged(devices); |
| 62 } | 75 } |
| 63 | 76 |
| 64 //------------------------------------------------------------------------------ | 77 //------------------------------------------------------------------------------ |
| 65 // Private methods | 78 // Private methods |
| 66 | 79 |
| 80 NetworkDeviceHandler::NetworkDeviceHandler() |
| 81 : devices_ready_(false), |
| 82 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 83 } |
| 84 |
| 85 void NetworkDeviceHandler::Init() { |
| 86 ShillManagerClient* shill_manager = |
| 87 DBusThreadManager::Get()->GetShillManagerClient(); |
| 88 shill_manager->GetProperties( |
| 89 base::Bind(&NetworkDeviceHandler::ManagerPropertiesCallback, |
| 90 weak_ptr_factory_.GetWeakPtr())); |
| 91 shill_manager->AddPropertyChangedObserver(this); |
| 92 } |
| 93 |
| 67 void NetworkDeviceHandler::ManagerPropertiesCallback( | 94 void NetworkDeviceHandler::ManagerPropertiesCallback( |
| 68 DBusMethodCallStatus call_status, | 95 DBusMethodCallStatus call_status, |
| 69 const base::DictionaryValue& properties) { | 96 const base::DictionaryValue& properties) { |
| 70 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | 97 if (call_status != DBUS_METHOD_CALL_SUCCESS) { |
| 71 LOG(ERROR) << "Failed to get Manager properties: " << call_status; | 98 LOG(ERROR) << "Failed to get Manager properties: " << call_status; |
| 72 return; | 99 return; |
| 73 } | 100 } |
| 74 const base::ListValue* devices = NULL; | 101 const base::ListValue* devices = NULL; |
| 75 if (!properties.GetListWithoutPathExpansion( | 102 if (!properties.GetListWithoutPathExpansion( |
| 76 flimflam::kDevicesProperty, &devices)) { | 103 flimflam::kDevicesProperty, &devices)) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 101 } | 128 } |
| 102 } | 129 } |
| 103 | 130 |
| 104 void NetworkDeviceHandler::DevicePropertiesCallback( | 131 void NetworkDeviceHandler::DevicePropertiesCallback( |
| 105 const std::string& device_path, | 132 const std::string& device_path, |
| 106 DBusMethodCallStatus call_status, | 133 DBusMethodCallStatus call_status, |
| 107 const base::DictionaryValue& properties) { | 134 const base::DictionaryValue& properties) { |
| 108 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | 135 if (call_status != DBUS_METHOD_CALL_SUCCESS) { |
| 109 LOG(ERROR) << "Failed to get Device properties for " << device_path | 136 LOG(ERROR) << "Failed to get Device properties for " << device_path |
| 110 << ": " << call_status; | 137 << ": " << call_status; |
| 111 DeviceReady(device_path); | 138 } else { |
| 112 return; | 139 GetDeviceProperties(device_path, properties); |
| 113 } | 140 } |
| 141 pending_device_paths_.erase(device_path); |
| 142 if (pending_device_paths_.empty()) { |
| 143 devices_ready_ = true; |
| 144 FOR_EACH_OBSERVER(Observer, observers_, NetworkDevicesUpdated(devices_)); |
| 145 } |
| 146 } |
| 147 |
| 148 void NetworkDeviceHandler::GetDeviceProperties( |
| 149 const std::string& device_path, |
| 150 const base::DictionaryValue& properties) { |
| 114 std::string type; | 151 std::string type; |
| 115 if (!properties.GetStringWithoutPathExpansion( | 152 if (!properties.GetStringWithoutPathExpansion( |
| 116 flimflam::kTypeProperty, &type)) { | 153 flimflam::kTypeProperty, &type)) { |
| 117 LOG(WARNING) << "Failed to parse Type property for " << device_path; | 154 LOG(WARNING) << "Failed to parse Type property for " << device_path; |
| 118 DeviceReady(device_path); | |
| 119 return; | 155 return; |
| 120 } | 156 } |
| 121 Device& device = devices_[device_path]; | 157 Device& device = devices_[device_path]; |
| 122 device.type = type; | 158 device.type = type; |
| 123 properties.GetBooleanWithoutPathExpansion( | 159 properties.GetBooleanWithoutPathExpansion( |
| 124 flimflam::kPoweredProperty, &device.powered); | 160 flimflam::kPoweredProperty, &device.powered); |
| 125 properties.GetBooleanWithoutPathExpansion( | 161 properties.GetBooleanWithoutPathExpansion( |
| 126 flimflam::kScanningProperty, &device.scanning); | 162 flimflam::kScanningProperty, &device.scanning); |
| 127 properties.GetIntegerWithoutPathExpansion( | 163 properties.GetIntegerWithoutPathExpansion( |
| 128 flimflam::kScanIntervalProperty, &device.scan_interval); | 164 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 or wimax devices). | |
| 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 } | 165 } |
| 206 | 166 |
| 207 //------------------------------------------------------------------------------ | 167 //------------------------------------------------------------------------------ |
| 208 | 168 |
| 209 NetworkDeviceHandler::Device::Device() | 169 NetworkDeviceHandler::Device::Device() |
| 210 : powered(false), scanning(false), scan_interval(0) { | 170 : powered(false), scanning(false), scan_interval(0) { |
| 211 } | 171 } |
| 212 | 172 |
| 213 NetworkDeviceHandler::Device::~Device() { | 173 NetworkDeviceHandler::Device::~Device() { |
| 214 } | 174 } |
| 215 | 175 |
| 216 } // namespace chromeos | 176 } // namespace chromeos |
| OLD | NEW |