Chromium Code Reviews| 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_state_handler.h" | 5 #include "chromeos/network/network_state_handler.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chromeos/network/device_state.h" | 12 #include "chromeos/network/device_state.h" |
| 13 #include "chromeos/network/managed_state.h" | 13 #include "chromeos/network/managed_state.h" |
| 14 #include "chromeos/network/network_event_log.h" | 14 #include "chromeos/network/network_event_log.h" |
| 15 #include "chromeos/network/network_state.h" | 15 #include "chromeos/network/network_state.h" |
| 16 #include "chromeos/network/network_state_handler_observer.h" | 16 #include "chromeos/network/network_state_handler_observer.h" |
| 17 #include "chromeos/network/shill_property_handler.h" | 17 #include "chromeos/network/shill_property_handler.h" |
| 18 #include "third_party/cros_system_api/dbus/service_constants.h" | 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | |
| 21 const char kLogModule[] = "NetworkPropertyHandler"; | 22 const char kLogModule[] = "NetworkPropertyHandler"; |
| 23 | |
| 24 // Returns true if |network->type()| == |match_type|, or it matches one of the | |
| 25 // following special match types: | |
| 26 // * kMatchTypeDefault matches any network (i.e. the first instance) | |
| 27 // * kMatchTypeNonVirtual matches non virtual networks | |
| 28 // * kMatchTypeWireless matches wireless networks | |
| 29 bool NetworkStateMatchesType(const chromeos::NetworkState* network, | |
| 30 const std::string& match_type) { | |
| 31 const std::string& type = network->type(); | |
| 32 return ((match_type == chromeos::NetworkStateHandler::kMatchTypeDefault) || | |
| 33 (match_type == type) || | |
| 34 (match_type == chromeos::NetworkStateHandler::kMatchTypeNonVirtual && | |
| 35 type != flimflam::kTypeVPN) || | |
| 36 (match_type == chromeos::NetworkStateHandler::kMatchTypeWireless && | |
| 37 type != flimflam::kTypeEthernet && type != flimflam::kTypeVPN)); | |
| 22 } | 38 } |
| 23 | 39 |
| 40 } // namespace | |
| 41 | |
| 24 namespace chromeos { | 42 namespace chromeos { |
| 25 | 43 |
| 44 const char NetworkStateHandler::kMatchTypeDefault[] = "default"; | |
| 45 const char NetworkStateHandler::kMatchTypeWireless[] = "wireless"; | |
| 46 const char NetworkStateHandler::kMatchTypeNonVirtual[] = "non-virtual"; | |
| 47 | |
| 26 static NetworkStateHandler* g_network_state_handler = NULL; | 48 static NetworkStateHandler* g_network_state_handler = NULL; |
| 27 | 49 |
| 28 NetworkStateHandler::NetworkStateHandler() { | 50 NetworkStateHandler::NetworkStateHandler() { |
| 29 } | 51 } |
| 30 | 52 |
| 31 NetworkStateHandler::~NetworkStateHandler() { | 53 NetworkStateHandler::~NetworkStateHandler() { |
| 32 STLDeleteContainerPointers(network_list_.begin(), network_list_.end()); | 54 STLDeleteContainerPointers(network_list_.begin(), network_list_.end()); |
| 33 STLDeleteContainerPointers(device_list_.begin(), device_list_.end()); | 55 STLDeleteContainerPointers(device_list_.begin(), device_list_.end()); |
| 34 } | 56 } |
| 35 | 57 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 return device->AsDeviceState(); | 123 return device->AsDeviceState(); |
| 102 } | 124 } |
| 103 return NULL; | 125 return NULL; |
| 104 } | 126 } |
| 105 | 127 |
| 106 const NetworkState* NetworkStateHandler::GetNetworkState( | 128 const NetworkState* NetworkStateHandler::GetNetworkState( |
| 107 const std::string& service_path) const { | 129 const std::string& service_path) const { |
| 108 return GetModifiableNetworkState(service_path); | 130 return GetModifiableNetworkState(service_path); |
| 109 } | 131 } |
| 110 | 132 |
| 111 const NetworkState* NetworkStateHandler::ActiveNetwork() const { | 133 const NetworkState* NetworkStateHandler::DefaultNetwork() const { |
| 112 if (network_list_.empty()) | 134 if (network_list_.empty()) |
| 113 return NULL; | 135 return NULL; |
| 114 const NetworkState* network = network_list_.front()->AsNetworkState(); | 136 const NetworkState* network = network_list_.front()->AsNetworkState(); |
| 115 DCHECK(network); | 137 DCHECK(network); |
| 116 if (!network->IsConnectedState()) | 138 if (!network->IsConnectedState()) |
| 117 return NULL; | 139 return NULL; |
| 118 return network; | 140 return network; |
| 119 } | 141 } |
| 120 | 142 |
| 121 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( | 143 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( |
| 122 const std::string& type) const { | 144 const std::string& type) const { |
| 123 for (ManagedStateList::const_iterator iter = network_list_.begin(); | 145 for (ManagedStateList::const_iterator iter = network_list_.begin(); |
| 124 iter != network_list_.end(); ++iter) { | 146 iter != network_list_.end(); ++iter) { |
| 125 const NetworkState* network = (*iter)->AsNetworkState(); | 147 const NetworkState* network = (*iter)->AsNetworkState(); |
| 126 DCHECK(network); | 148 DCHECK(network); |
| 127 if (!network->IsConnectedState()) | 149 if (!network->IsConnectedState()) |
| 128 break; // Connected networks are listed first. | 150 break; // Connected networks are listed first. |
| 129 if (network->type() == type) | 151 if (NetworkStateMatchesType(network, type)) |
| 130 return network; | 152 return network; |
| 131 } | 153 } |
| 132 return NULL; | 154 return NULL; |
| 133 } | 155 } |
| 134 | 156 |
| 135 const NetworkState* NetworkStateHandler::ConnectingNetworkByType( | 157 const NetworkState* NetworkStateHandler::ConnectingNetworkByType( |
| 136 const std::string& type) const { | 158 const std::string& type) const { |
| 137 for (ManagedStateList::const_iterator iter = network_list_.begin(); | 159 for (ManagedStateList::const_iterator iter = network_list_.begin(); |
| 138 iter != network_list_.end(); ++iter) { | 160 iter != network_list_.end(); ++iter) { |
| 139 const NetworkState* network = (*iter)->AsNetworkState(); | 161 const NetworkState* network = (*iter)->AsNetworkState(); |
| 140 DCHECK(network); | 162 DCHECK(network); |
| 141 if (network->IsConnectedState()) | 163 if (network->IsConnectedState()) |
| 142 continue; | 164 continue; |
| 143 if (!network->IsConnectingState()) | 165 if (!network->IsConnectingState()) |
| 144 break; // Connected and connecting networks are listed first. | 166 break; // Connected and connecting networks are listed first. |
| 145 if (network->type() == type || | 167 if (NetworkStateMatchesType(network, type)) |
| 146 (type.empty() && type != flimflam::kTypeEthernet)) { | |
| 147 return network; | 168 return network; |
| 148 } | |
| 149 } | 169 } |
| 150 return NULL; | 170 return NULL; |
| 151 } | 171 } |
| 152 | 172 |
| 153 std::string NetworkStateHandler::HardwareAddressForType( | 173 std::string NetworkStateHandler::HardwareAddressForType( |
| 154 const std::string& type) const { | 174 const std::string& type) const { |
| 155 std::string result; | 175 std::string result; |
| 156 const NetworkState* network = ConnectedNetworkByType(type); | 176 const NetworkState* network = ConnectedNetworkByType(type); |
| 157 if (network) { | 177 if (network) { |
| 158 const DeviceState* device = GetDeviceState(network->device_path()); | 178 const DeviceState* device = GetDeviceState(network->device_path()); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 | 298 |
| 279 void NetworkStateHandler::UpdateManagedStateProperties( | 299 void NetworkStateHandler::UpdateManagedStateProperties( |
| 280 ManagedState::ManagedType type, | 300 ManagedState::ManagedType type, |
| 281 const std::string& path, | 301 const std::string& path, |
| 282 const base::DictionaryValue& properties) { | 302 const base::DictionaryValue& properties) { |
| 283 ManagedState* managed = GetModifiableManagedState(GetManagedList(type), path); | 303 ManagedState* managed = GetModifiableManagedState(GetManagedList(type), path); |
| 284 if (!managed) { | 304 if (!managed) { |
| 285 LOG(ERROR) << "GetPropertiesCallback: " << path << " Not found!"; | 305 LOG(ERROR) << "GetPropertiesCallback: " << path << " Not found!"; |
| 286 return; | 306 return; |
| 287 } | 307 } |
| 288 bool network_property_changed = false; | 308 bool network_property_updated = false; |
| 289 for (base::DictionaryValue::Iterator iter(properties); | 309 for (base::DictionaryValue::Iterator iter(properties); |
| 290 iter.HasNext(); iter.Advance()) { | 310 iter.HasNext(); iter.Advance()) { |
| 291 if (type == ManagedState::MANAGED_TYPE_NETWORK) { | 311 if (type == ManagedState::MANAGED_TYPE_NETWORK) { |
| 292 if (ParseNetworkServiceProperty( | 312 if (ParseNetworkServiceProperty( |
| 293 managed->AsNetworkState(), iter.key(), iter.value())) { | 313 managed->AsNetworkState(), iter.key(), iter.value())) { |
| 294 network_property_changed = true; | 314 network_property_updated = true; |
| 295 } | 315 } |
| 296 } else { | 316 } else { |
| 297 managed->PropertyChanged(iter.key(), iter.value()); | 317 managed->PropertyChanged(iter.key(), iter.value()); |
| 298 } | 318 } |
| 299 } | 319 } |
| 300 // Notify observers. | 320 // Notify observers. |
| 301 if (network_property_changed) { | 321 if (network_property_updated) { |
| 302 NetworkState* network = managed->AsNetworkState(); | 322 NetworkState* network = managed->AsNetworkState(); |
| 303 DCHECK(network); | 323 DCHECK(network); |
| 304 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 324 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
| 305 NetworkServiceChanged(network)); | 325 NetworkPropertiesUpdated(network)); |
| 306 } | 326 } |
| 307 network_event_log::AddEntry( | 327 network_event_log::AddEntry( |
| 308 kLogModule, "PropertiesReceived", | 328 kLogModule, "PropertiesReceived", |
| 309 StringPrintf("%s (%s)", path.c_str(), managed->name().c_str())); | 329 StringPrintf("%s (%s)", path.c_str(), managed->name().c_str())); |
| 310 } | 330 } |
| 311 | 331 |
| 312 void NetworkStateHandler::UpdateNetworkServiceProperty( | 332 void NetworkStateHandler::UpdateNetworkServiceProperty( |
| 313 const std::string& service_path, | 333 const std::string& service_path, |
| 314 const std::string& key, | 334 const std::string& key, |
| 315 const base::Value& value) { | 335 const base::Value& value) { |
| 316 NetworkState* network = GetModifiableNetworkState(service_path); | 336 NetworkState* network = GetModifiableNetworkState(service_path); |
| 317 if (!network) | 337 if (!network) |
| 318 return; | 338 return; |
| 319 if (ParseNetworkServiceProperty(network, key, value)) { | 339 if (!ParseNetworkServiceProperty(network, key, value)) |
| 320 std::string detail = network->name() + "." + key; | 340 return; |
| 321 std::string vstr; | 341 std::string detail = network->name() + "." + key; |
| 322 if (value.GetAsString(&vstr)) | 342 std::string vstr; |
| 323 detail += " = " + vstr; | 343 if (value.GetAsString(&vstr)) |
| 324 network_event_log::AddEntry(kLogModule, "NetworkPropertyChanged", detail); | 344 detail += " = " + vstr; |
| 325 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 345 network_event_log::AddEntry(kLogModule, "NetworkPropertiesUpdated", detail); |
| 326 NetworkServiceChanged(network)); | 346 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
| 327 } | 347 NetworkPropertiesUpdated(network)); |
| 328 } | 348 } |
| 329 | 349 |
| 330 void NetworkStateHandler::UpdateNetworkServiceIPAddress( | 350 void NetworkStateHandler::UpdateNetworkServiceIPAddress( |
| 331 const std::string& service_path, | 351 const std::string& service_path, |
| 332 const std::string& ip_address) { | 352 const std::string& ip_address) { |
| 333 NetworkState* network = GetModifiableNetworkState(service_path); | 353 NetworkState* network = GetModifiableNetworkState(service_path); |
| 334 if (!network) | 354 if (!network) |
| 335 return; | 355 return; |
| 336 std::string detail = network->name() + ".IPAddress = " + ip_address; | 356 std::string detail = network->name() + ".IPAddress = " + ip_address; |
| 337 network_event_log::AddEntry(kLogModule, "NetworkIPChanged", detail); | 357 network_event_log::AddEntry(kLogModule, "NetworkIPChanged", detail); |
| 338 network->set_ip_address(ip_address); | 358 network->set_ip_address(ip_address); |
| 339 FOR_EACH_OBSERVER( | 359 FOR_EACH_OBSERVER( |
| 340 NetworkStateHandlerObserver, observers_, | 360 NetworkStateHandlerObserver, observers_, |
| 341 NetworkServiceChanged(network)); | 361 NetworkPropertiesUpdated(network)); |
| 342 } | 362 } |
| 343 | 363 |
| 344 void NetworkStateHandler::ManagerPropertyChanged() { | 364 void NetworkStateHandler::ManagerPropertyChanged() { |
| 345 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 365 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
| 346 NetworkManagerChanged()); | 366 NetworkManagerChanged()); |
| 347 } | 367 } |
| 348 | 368 |
| 349 void NetworkStateHandler::ManagedStateListChanged( | 369 void NetworkStateHandler::ManagedStateListChanged( |
| 350 ManagedState::ManagedType type) { | 370 ManagedState::ManagedType type) { |
| 351 if (type == ManagedState::MANAGED_TYPE_NETWORK) { | 371 if (type == ManagedState::MANAGED_TYPE_NETWORK) { |
| 352 // Notify observers that the list of networks has changed. | 372 // Notify observers that the list of networks has changed. |
| 353 NetworkStateList network_list; | 373 NetworkStateList network_list; |
| 354 GetNetworkList(&network_list); | 374 GetNetworkList(&network_list); |
| 355 network_event_log::AddEntry( | 375 network_event_log::AddEntry( |
| 356 kLogModule, "NetworkListChanged", | 376 kLogModule, "NetworkListChanged", |
| 357 StringPrintf("Size: %"PRIuS, network_list_.size())); | 377 StringPrintf("Size: %"PRIuS, network_list_.size())); |
| 358 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 378 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
| 359 NetworkListChanged(network_list)); | 379 NetworkListChanged(network_list)); |
| 360 // Update the active network and notify observers if it has changed. | 380 // The list order may have changed, so check if the default network changed. |
| 361 NetworkState* new_active_network = | 381 if (CheckDefaultNetworkChanged()) |
| 362 network_list_.empty() ? NULL : network_list_.front()->AsNetworkState(); | 382 OnDefaultNetworkChanged(); |
| 363 std::string new_active_network_path; | |
| 364 if (new_active_network) | |
| 365 new_active_network_path = new_active_network->path(); | |
| 366 if (new_active_network_path != active_network_path_) { | |
| 367 network_event_log::AddEntry( | |
| 368 kLogModule, "ActiveNetworkChanged", new_active_network_path); | |
| 369 active_network_path_ = new_active_network_path; | |
| 370 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
| 371 ActiveNetworkChanged(new_active_network)); | |
| 372 } | |
| 373 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { | 383 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { |
| 374 network_event_log::AddEntry( | 384 network_event_log::AddEntry( |
| 375 kLogModule, "DeviceListChanged", | 385 kLogModule, "DeviceListChanged", |
| 376 StringPrintf("Size: %"PRIuS, device_list_.size())); | 386 StringPrintf("Size: %"PRIuS, device_list_.size())); |
| 377 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 387 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
| 378 DeviceListChanged()); | 388 DeviceListChanged()); |
| 379 } else { | 389 } else { |
| 380 NOTREACHED(); | 390 NOTREACHED(); |
| 381 } | 391 } |
| 382 } | 392 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 } | 433 } |
| 424 NOTREACHED(); | 434 NOTREACHED(); |
| 425 return NULL; | 435 return NULL; |
| 426 } | 436 } |
| 427 | 437 |
| 428 bool NetworkStateHandler::ParseNetworkServiceProperty( | 438 bool NetworkStateHandler::ParseNetworkServiceProperty( |
| 429 NetworkState* network, | 439 NetworkState* network, |
| 430 const std::string& key, | 440 const std::string& key, |
| 431 const base::Value& value) { | 441 const base::Value& value) { |
| 432 DCHECK(network); | 442 DCHECK(network); |
| 443 std::string prev_connection_state = network->connection_state(); | |
| 433 if (!network->PropertyChanged(key, value)) | 444 if (!network->PropertyChanged(key, value)) |
| 434 return false; | 445 return false; |
| 435 if (network->path() == active_network_path_ && | 446 if (key == flimflam::kStateProperty && |
| 436 key == flimflam::kStateProperty) { | 447 network->connection_state() != prev_connection_state) { |
| 437 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 448 OnNetworkConnectionStateChanged(network); |
| 438 ActiveNetworkStateChanged(network)); | |
| 439 } | 449 } |
| 440 return true; | 450 return true; |
| 441 } | 451 } |
| 442 | 452 |
| 453 void NetworkStateHandler::OnNetworkConnectionStateChanged( | |
| 454 NetworkState* network) { | |
| 455 std::string desc = StringPrintf( | |
| 456 "%s: %s", network->path().c_str(), network->connection_state().c_str()); | |
| 457 network_event_log::AddEntry( | |
| 458 kLogModule, "NetworkConnectionStateChanged", desc); | |
| 459 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
| 460 NetworkConnectionStateChanged(network)); | |
| 461 if (CheckDefaultNetworkChanged() || | |
| 462 network->path() == default_network_path_) { | |
| 463 OnDefaultNetworkChanged(); | |
| 464 } | |
| 465 } | |
| 466 | |
| 467 bool NetworkStateHandler::CheckDefaultNetworkChanged() { | |
| 468 std::string new_default_network_path; | |
| 469 const NetworkState* new_default_network = DefaultNetwork(); | |
| 470 if (new_default_network) | |
| 471 new_default_network_path = new_default_network->path(); | |
| 472 if (new_default_network_path == default_network_path_) | |
| 473 return false; | |
| 474 default_network_path_ = new_default_network_path; | |
| 475 return true; | |
| 476 } | |
| 477 | |
| 478 void NetworkStateHandler::OnDefaultNetworkChanged() { | |
| 479 const NetworkState* default_network = DefaultNetwork(); | |
| 480 network_event_log::AddEntry( | |
|
Greg Spencer (Chromium)
2012/12/20 17:37:47
Do you want to use the NET_LOG macros here? You d
stevenjb
2012/12/20 17:46:26
As discussed in IM, we should make a pass through
| |
| 481 kLogModule, "DefaultNetworkChanged", default_network->path()); | |
| 482 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | |
| 483 DefaultNetworkChanged(default_network)); | |
| 484 } | |
| 485 | |
| 443 } // namespace chromeos | 486 } // namespace chromeos |
| OLD | NEW |