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_state_handler.h" | |
| 6 | |
| 7 #include "base/stl_util.h" | |
| 8 #include "base/string_util.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chromeos/network/device_state.h" | |
| 11 #include "chromeos/network/managed_state.h" | |
| 12 #include "chromeos/network/network_state.h" | |
| 13 #include "chromeos/network/network_state_handler_impl.h" | |
| 14 #include "chromeos/network/network_state_handler_observer.h" | |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 16 | |
| 17 namespace chromeos { | |
| 18 | |
| 19 NetworkStateHandler::NetworkStateHandler() { | |
| 20 } | |
| 21 | |
| 22 NetworkStateHandler::~NetworkStateHandler() { | |
| 23 STLDeleteContainerPointers(network_list_.begin(), network_list_.end()); | |
| 24 STLDeleteContainerPointers(device_list_.begin(), device_list_.end()); | |
| 25 } | |
| 26 | |
| 27 void NetworkStateHandler::Init() { | |
| 28 impl_.reset(new internal::NetworkStateHandlerImpl(this)); | |
| 29 impl_->Init(); | |
| 30 } | |
| 31 | |
| 32 void NetworkStateHandler::AddObserver(NetworkStateHandlerObserver* observer) { | |
| 33 observers_.AddObserver(observer); | |
| 34 } | |
| 35 | |
| 36 void NetworkStateHandler::RemoveObserver( | |
| 37 NetworkStateHandlerObserver* observer) { | |
| 38 observers_.RemoveObserver(observer); | |
| 39 } | |
| 40 | |
| 41 bool NetworkStateHandler::TechnologyAvailable( | |
| 42 const std::string& technology) const { | |
| 43 return available_technologies_.find(technology) != | |
| 44 available_technologies_.end(); | |
| 45 } | |
| 46 | |
| 47 bool NetworkStateHandler::TechnologyEnabled( | |
| 48 const std::string& technology) const { | |
| 49 return enabled_technologies_.find(technology) != enabled_technologies_.end(); | |
| 50 } | |
| 51 | |
| 52 void NetworkStateHandler::SetTechnologyEnabled(const std::string& technology, | |
| 53 bool enabled) { | |
| 54 impl_->SetTechnologyEnabled(technology, enabled); | |
| 55 } | |
| 56 | |
| 57 const DeviceState* NetworkStateHandler::GetDeviceState( | |
| 58 const std::string& device_path) const { | |
| 59 return GetModifiableDeviceState(device_path); | |
| 60 } | |
| 61 | |
| 62 const DeviceState* NetworkStateHandler::GetDeviceStateByType( | |
| 63 const std::string& type) const { | |
| 64 for (ManagedStateList::const_iterator iter = device_list_.begin(); | |
| 65 iter != device_list_.end(); ++iter) { | |
| 66 ManagedState* device = *iter; | |
| 67 if (device->type() == type) | |
| 68 return device->AsDeviceState(); | |
| 69 } | |
| 70 return NULL; | |
| 71 } | |
| 72 | |
| 73 const NetworkState* NetworkStateHandler::GetNetworkState( | |
| 74 const std::string& service_path) const { | |
| 75 return GetModifiableNetworkState(service_path); | |
| 76 } | |
| 77 | |
| 78 const NetworkState* NetworkStateHandler::ActiveNetwork() const { | |
| 79 if (network_list_.empty()) | |
| 80 return NULL; | |
| 81 const NetworkState* network = network_list_.front()->AsNetworkState(); | |
| 82 DCHECK(network); | |
| 83 if (!network->IsConnectedState()) | |
| 84 return NULL; | |
| 85 return network; | |
| 86 } | |
| 87 | |
| 88 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( | |
| 89 const std::string& type) const { | |
| 90 for (ManagedStateList::const_iterator iter = network_list_.begin(); | |
| 91 iter != network_list_.end(); ++iter) { | |
| 92 const NetworkState* network = (*iter)->AsNetworkState(); | |
| 93 DCHECK(network); | |
| 94 if (!network->IsConnectedState()) | |
| 95 break; // Connected networks are listed first. | |
| 96 if (network->type() == type) | |
| 97 return network; | |
| 98 } | |
| 99 return NULL; | |
| 100 } | |
| 101 | |
| 102 const NetworkState* NetworkStateHandler::ConnectingNetworkByType( | |
| 103 const std::string& type) const { | |
| 104 for (ManagedStateList::const_iterator iter = network_list_.begin(); | |
| 105 iter != network_list_.end(); ++iter) { | |
| 106 const NetworkState* network = (*iter)->AsNetworkState(); | |
| 107 DCHECK(network); | |
| 108 if (network->IsConnectedState()) | |
| 109 continue; | |
| 110 if (!network->IsConnectingState()) | |
| 111 break; // Connected and connecting networks are listed first. | |
| 112 if (network->type() == type || | |
| 113 (type.empty() && type != flimflam::kTypeEthernet)) { | |
| 114 return network; | |
| 115 } | |
| 116 } | |
| 117 return NULL; | |
| 118 } | |
| 119 | |
| 120 std::string NetworkStateHandler::HardwareAddressForType( | |
| 121 const std::string& type) const { | |
| 122 std::string result; | |
| 123 const NetworkState* network = ConnectedNetworkByType(type); | |
| 124 if (network) { | |
| 125 const DeviceState* device = GetDeviceState(network->device_path()); | |
| 126 if (device) | |
| 127 result = device->mac_address(); | |
| 128 } | |
| 129 StringToUpperASCII(&result); | |
| 130 return result; | |
| 131 } | |
| 132 | |
| 133 std::string NetworkStateHandler::FormattedHardwareAddressForType( | |
| 134 const std::string& type) const { | |
| 135 std::string address = HardwareAddressForType(type); | |
| 136 if (address.size() % 2 != 0) | |
| 137 return address; | |
| 138 std::string result; | |
| 139 for (size_t i = 0; i < address.size(); ++i) { | |
| 140 if ((i != 0) && (i % 2 == 0)) | |
| 141 result.push_back(':'); | |
| 142 result.push_back(address[i]); | |
| 143 } | |
| 144 return result; | |
| 145 } | |
| 146 | |
| 147 void NetworkStateHandler::GetNetworkList(NetworkStateList* list) const { | |
| 148 DCHECK(list); | |
| 149 impl_->RequestScan(); | |
| 150 NetworkStateList result; | |
| 151 list->clear(); | |
| 152 for (ManagedStateList::const_iterator iter = network_list_.begin(); | |
| 153 iter != network_list_.end(); ++iter) { | |
| 154 const NetworkState* network = (*iter)->AsNetworkState(); | |
| 155 DCHECK(network); | |
| 156 list->push_back(network); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 //------------------------------------------------------------------------------ | |
| 161 // Private methods called by NetworkStateHandlerImpl | |
| 162 | |
| 163 void NetworkStateHandler::NotifyManagerChanged() { | |
| 164 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
| 165 NetworkManagerChanged()); | |
| 166 } | |
| 167 | |
| 168 DeviceState* NetworkStateHandler::GetModifiableDeviceState( | |
| 169 const std::string& device_path) const { | |
| 170 ManagedState* managed = GetModifiableManagedState(&device_list_, device_path); | |
| 171 if (!managed) | |
| 172 return NULL; | |
| 173 return managed->AsDeviceState(); | |
| 174 } | |
| 175 | |
| 176 NetworkState* NetworkStateHandler::GetModifiableNetworkState( | |
| 177 const std::string& service_path) const { | |
| 178 ManagedState* managed = | |
| 179 GetModifiableManagedState(&network_list_, service_path); | |
| 180 if (!managed) | |
| 181 return NULL; | |
| 182 return managed->AsNetworkState(); | |
| 183 } | |
| 184 | |
| 185 ManagedState* NetworkStateHandler::GetModifiableManagedState( | |
| 186 const ManagedStateList* managed_list, | |
| 187 const std::string& path) const { | |
| 188 for (ManagedStateList::const_iterator iter = managed_list->begin(); | |
| 189 iter != managed_list->end(); ++iter) { | |
| 190 ManagedState* managed = *iter; | |
| 191 if (managed->path() == path) | |
| 192 return managed; | |
| 193 } | |
| 194 return NULL; | |
| 195 } | |
| 196 | |
| 197 NetworkStateHandler::ManagedStateList* NetworkStateHandler::GetManagedList( | |
| 198 ManagedState::ManagedType type) { | |
| 199 switch(type) { | |
| 200 case ManagedState::MANAGED_TYPE_NETWORK: | |
| 201 return &network_list_; | |
| 202 case ManagedState::MANAGED_TYPE_DEVICE: | |
| 203 return &device_list_; | |
| 204 } | |
| 205 NOTREACHED(); | |
| 206 return NULL; | |
| 207 } | |
| 208 | |
| 209 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type, | |
| 210 const base::ListValue& entries) { | |
| 211 ManagedStateList* managed_list = GetManagedList(type); | |
| 212 VLOG(2) << "UpdateManagedList: " << type; | |
| 213 // Create a map of existing entries. | |
| 214 std::map<std::string, ManagedState*> managed_map; | |
| 215 for (ManagedStateList::iterator iter = managed_list->begin(); | |
| 216 iter != managed_list->end(); ++iter) { | |
| 217 ManagedState* managed = *iter; | |
| 218 managed_map[managed->path()] = managed; | |
| 219 } | |
| 220 // Clear the list (pointers are owned by managed_map). | |
| 221 managed_list->clear(); | |
| 222 // Updates managed_list and request updates for new entries. | |
| 223 for (base::ListValue::const_iterator iter = entries.begin(); | |
| 224 iter != entries.end(); ++iter) { | |
| 225 std::string path; | |
| 226 (*iter)->GetAsString(&path); | |
| 227 if (path.empty()) | |
| 228 continue; | |
| 229 std::map<std::string, ManagedState*>::iterator found = | |
| 230 managed_map.find(path); | |
| 231 bool request_properties = false; | |
| 232 if (found == managed_map.end()) { | |
| 233 request_properties = true; | |
| 234 managed_list->push_back(ManagedState::Create(type, path)); | |
| 235 } else { | |
| 236 ManagedState* managed = found->second; | |
| 237 managed_list->push_back(managed); | |
| 238 managed_map.erase(found); | |
| 239 } | |
| 240 if (request_properties) | |
| 241 impl_->RequestProperties(type, path); | |
| 242 } | |
| 243 // Delete any remaning entries in managed_map. | |
| 244 STLDeleteContainerPairSecondPointers(managed_map.begin(), managed_map.end()); | |
| 245 } | |
| 246 | |
| 247 void NetworkStateHandler::SetAvailableTechnologies( | |
| 248 const base::ListValue& technologies) { | |
| 249 available_technologies_.clear(); | |
| 250 for (base::ListValue::const_iterator iter = technologies.begin(); | |
| 251 iter != technologies.end(); ++iter) { | |
| 252 std::string technology; | |
| 253 (*iter)->GetAsString(&technology); | |
| 254 DCHECK(!technology.empty()); | |
| 255 available_technologies_.insert(technology); | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 void NetworkStateHandler::SetEnabledTechnologies( | |
| 260 const base::ListValue& technologies) { | |
| 261 enabled_technologies_.clear(); | |
| 262 for (base::ListValue::const_iterator iter = technologies.begin(); | |
| 263 iter != technologies.end(); ++iter) { | |
| 264 std::string technology; | |
| 265 (*iter)->GetAsString(&technology); | |
| 266 DCHECK(!technology.empty()); | |
| 267 enabled_technologies_.insert(technology); | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 void NetworkStateHandler::ParseProperties( | |
| 272 ManagedState::ManagedType type, | |
| 273 const std::string& path, | |
| 274 const base::DictionaryValue& properties) { | |
| 275 ManagedState* managed = GetModifiableManagedState(GetManagedList(type), path); | |
| 276 if (!managed) { | |
| 277 LOG(ERROR) << "GetPropertiesCallback: " << path << " Not found!"; | |
| 278 return; | |
| 279 } | |
| 280 for (base::DictionaryValue::Iterator iter(properties); | |
| 281 iter.HasNext(); iter.Advance()) { | |
| 282 // Handle IPConfig here since it requires additional complexity that is | |
| 283 // better handled here than in NetworkState::PropertyChanged (e.g. | |
| 284 // observer notification). | |
| 285 if (iter.key() == shill::kIPConfigProperty) { | |
| 286 DCHECK(managed->managed_type() == ManagedState::MANAGED_TYPE_NETWORK); | |
| 287 std::string ip_config_path; | |
| 288 if (!iter.value().GetAsString(&ip_config_path)) | |
| 289 NOTREACHED(); | |
| 290 impl_->RequestIPConfig(managed->path(), ip_config_path); | |
| 291 } else { | |
| 292 managed->PropertyChanged(iter.key(), iter.value()); | |
| 293 } | |
| 294 } | |
| 295 } | |
| 296 | |
| 297 void NetworkStateHandler::NetworkServicePropertyChanged( | |
| 298 const std::string& service_path, | |
| 299 const std::string& key, | |
| 300 const base::Value& value) { | |
| 301 NetworkState* network = GetModifiableNetworkState(service_path); | |
| 302 if (!network) | |
| 303 return; | |
| 304 if (network->PropertyChanged(key, value)) { | |
| 305 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
| 306 NetworkServiceChanged(network)); | |
| 307 if (network == network_list_.front() && key == flimflam::kStateProperty) { | |
| 308 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
| 309 ActiveNetworkStateChanged(network)); | |
| 310 } | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 void NetworkStateHandler::SetServiceIPAddress(const std::string& service_path, | |
|
pneubeck (no reviews)
2012/10/29 20:27:49
SetNetworkServiceIPAddress?
stevenjb
2012/10/30 00:40:25
Done.
| |
| 315 const std::string& ip_address) { | |
| 316 NetworkState* network = GetModifiableNetworkState(service_path); | |
| 317 if (!network) | |
| 318 return; | |
| 319 network->set_ip_address(ip_address); | |
| 320 FOR_EACH_OBSERVER( | |
| 321 NetworkStateHandlerObserver, observers_, | |
| 322 NetworkServiceChanged(network)); | |
| 323 } | |
| 324 | |
| 325 void NetworkStateHandler::NotifyNetworkServiceObservers() { | |
| 326 // Notify observers that the list of networks has changed. | |
| 327 NetworkStateList network_list; | |
| 328 GetNetworkList(&network_list); | |
| 329 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
| 330 NetworkListChanged(network_list)); | |
| 331 // Notify observers if the active network has changed. | |
| 332 NetworkState* new_active_network = | |
| 333 network_list_.empty() ? NULL : network_list_.front()->AsNetworkState(); | |
| 334 std::string new_active_network_path; | |
| 335 if (new_active_network) | |
| 336 new_active_network_path = new_active_network->path(); | |
| 337 if (new_active_network_path != active_network_path_) { | |
| 338 active_network_path_ = new_active_network_path; | |
| 339 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
| 340 ActiveNetworkChanged(new_active_network)); | |
| 341 } | |
| 342 } | |
| 343 | |
| 344 size_t NetworkStateHandler::NumObservedNetworksForTest() const { | |
| 345 return impl_->observed_networks().size(); | |
| 346 } | |
| 347 | |
| 348 } // namespace chromeos | |
| OLD | NEW |