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.
5 #include "chromeos/network/network_state_handler.h"
8 #include "base/format_macros.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/json/json_writer.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/metrics/histogram.h"
15 #include "base/stl_util.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/values.h"
19 #include "chromeos/network/device_state.h"
20 #include "chromeos/network/network_event_log.h"
21 #include "chromeos/network/network_state.h"
22 #include "chromeos/network/network_state_handler_observer.h"
23 #include "third_party/cros_system_api/dbus/service_constants.h"
29 bool ConnectionStateChanged(NetworkState
* network
,
30 const std::string
& prev_connection_state
,
31 bool prev_is_captive_portal
) {
32 return ((network
->connection_state() != prev_connection_state
) &&
33 !((network
->connection_state() == shill::kStateIdle
) &&
34 prev_connection_state
.empty())) ||
35 (network
->is_captive_portal() != prev_is_captive_portal
);
38 std::string
GetManagedStateLogType(const ManagedState
* state
) {
39 switch (state
->managed_type()) {
40 case ManagedState::MANAGED_TYPE_NETWORK
:
42 case ManagedState::MANAGED_TYPE_DEVICE
:
49 std::string
GetLogName(const ManagedState
* state
) {
52 return base::StringPrintf("%s (%s)", state
->name().c_str(),
53 state
->path().c_str());
56 std::string
ValueAsString(const base::Value
& value
) {
58 base::JSONWriter::WriteWithOptions(
59 &value
, base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES
, &vstr
);
60 return vstr
.empty() ? "''" : vstr
;
65 const char NetworkStateHandler::kDefaultCheckPortalList
[] =
66 "ethernet,wifi,cellular";
68 NetworkStateHandler::NetworkStateHandler() : network_list_sorted_(false) {
71 NetworkStateHandler::~NetworkStateHandler() {
72 FOR_EACH_OBSERVER(NetworkStateHandlerObserver
, observers_
, IsShuttingDown());
73 STLDeleteContainerPointers(network_list_
.begin(), network_list_
.end());
74 STLDeleteContainerPointers(device_list_
.begin(), device_list_
.end());
77 void NetworkStateHandler::InitShillPropertyHandler() {
78 shill_property_handler_
.reset(new internal::ShillPropertyHandler(this));
79 shill_property_handler_
->Init();
83 NetworkStateHandler
* NetworkStateHandler::InitializeForTest() {
84 NetworkStateHandler
* handler
= new NetworkStateHandler();
85 handler
->InitShillPropertyHandler();
89 void NetworkStateHandler::AddObserver(
90 NetworkStateHandlerObserver
* observer
,
91 const tracked_objects::Location
& from_here
) {
92 observers_
.AddObserver(observer
);
93 device_event_log::AddEntry(from_here
.file_name(), from_here
.line_number(),
94 device_event_log::LOG_TYPE_NETWORK
,
95 device_event_log::LOG_LEVEL_DEBUG
,
96 "NetworkStateHandler::AddObserver");
99 void NetworkStateHandler::RemoveObserver(
100 NetworkStateHandlerObserver
* observer
,
101 const tracked_objects::Location
& from_here
) {
102 observers_
.RemoveObserver(observer
);
103 device_event_log::AddEntry(from_here
.file_name(), from_here
.line_number(),
104 device_event_log::LOG_TYPE_NETWORK
,
105 device_event_log::LOG_LEVEL_DEBUG
,
106 "NetworkStateHandler::RemoveObserver");
109 NetworkStateHandler::TechnologyState
NetworkStateHandler::GetTechnologyState(
110 const NetworkTypePattern
& type
) const {
111 std::string technology
= GetTechnologyForType(type
);
112 TechnologyState state
;
113 if (shill_property_handler_
->IsTechnologyEnabled(technology
))
114 state
= TECHNOLOGY_ENABLED
;
115 else if (shill_property_handler_
->IsTechnologyEnabling(technology
))
116 state
= TECHNOLOGY_ENABLING
;
117 else if (shill_property_handler_
->IsTechnologyUninitialized(technology
))
118 state
= TECHNOLOGY_UNINITIALIZED
;
119 else if (shill_property_handler_
->IsTechnologyAvailable(technology
))
120 state
= TECHNOLOGY_AVAILABLE
;
122 state
= TECHNOLOGY_UNAVAILABLE
;
123 VLOG(2) << "GetTechnologyState: " << type
.ToDebugString() << " = " << state
;
127 void NetworkStateHandler::SetTechnologyEnabled(
128 const NetworkTypePattern
& type
,
130 const network_handler::ErrorCallback
& error_callback
) {
131 ScopedVector
<std::string
> technologies
= GetTechnologiesForType(type
);
132 for (ScopedVector
<std::string
>::iterator it
= technologies
.begin();
133 it
!= technologies
.end(); ++it
) {
134 std::string
* technology
= *it
;
136 if (!shill_property_handler_
->IsTechnologyAvailable(*technology
))
138 NET_LOG_USER("SetTechnologyEnabled",
139 base::StringPrintf("%s:%d", technology
->c_str(), enabled
));
140 shill_property_handler_
->SetTechnologyEnabled(*technology
, enabled
,
143 // Signal Device/Technology state changed.
144 NotifyDeviceListChanged();
147 const DeviceState
* NetworkStateHandler::GetDeviceState(
148 const std::string
& device_path
) const {
149 const DeviceState
* device
= GetModifiableDeviceState(device_path
);
150 if (device
&& !device
->update_received())
155 const DeviceState
* NetworkStateHandler::GetDeviceStateByType(
156 const NetworkTypePattern
& type
) const {
157 for (ManagedStateList::const_iterator iter
= device_list_
.begin();
158 iter
!= device_list_
.end(); ++iter
) {
159 ManagedState
* device
= *iter
;
160 if (!device
->update_received())
162 if (device
->Matches(type
))
163 return device
->AsDeviceState();
168 bool NetworkStateHandler::GetScanningByType(
169 const NetworkTypePattern
& type
) const {
170 for (ManagedStateList::const_iterator iter
= device_list_
.begin();
171 iter
!= device_list_
.end(); ++iter
) {
172 const DeviceState
* device
= (*iter
)->AsDeviceState();
174 if (!device
->update_received())
176 if (device
->Matches(type
) && device
->scanning())
182 const NetworkState
* NetworkStateHandler::GetNetworkState(
183 const std::string
& service_path
) const {
184 const NetworkState
* network
= GetModifiableNetworkState(service_path
);
185 if (network
&& !network
->update_received())
190 const NetworkState
* NetworkStateHandler::DefaultNetwork() const {
191 if (default_network_path_
.empty())
193 return GetNetworkState(default_network_path_
);
196 const NetworkState
* NetworkStateHandler::ConnectedNetworkByType(
197 const NetworkTypePattern
& type
) const {
198 // Active networks are always listed first by Shill so no need to sort.
199 for (ManagedStateList::const_iterator iter
= network_list_
.begin();
200 iter
!= network_list_
.end(); ++iter
) {
201 const NetworkState
* network
= (*iter
)->AsNetworkState();
203 if (!network
->update_received())
205 if (!network
->IsConnectedState())
206 break; // Connected networks are listed first.
207 if (network
->Matches(type
))
213 const NetworkState
* NetworkStateHandler::ConnectingNetworkByType(
214 const NetworkTypePattern
& type
) const {
215 // Active networks are always listed first by Shill so no need to sort.
216 for (ManagedStateList::const_iterator iter
= network_list_
.begin();
217 iter
!= network_list_
.end(); ++iter
) {
218 const NetworkState
* network
= (*iter
)->AsNetworkState();
220 if (!network
->update_received() || network
->IsConnectedState())
222 if (!network
->IsConnectingState())
223 break; // Connected and connecting networks are listed first.
224 if (network
->Matches(type
))
230 const NetworkState
* NetworkStateHandler::FirstNetworkByType(
231 const NetworkTypePattern
& type
) {
232 if (!network_list_sorted_
)
233 SortNetworkList(); // Sort to ensure visible networks are listed first.
234 for (ManagedStateList::const_iterator iter
= network_list_
.begin();
235 iter
!= network_list_
.end(); ++iter
) {
236 const NetworkState
* network
= (*iter
)->AsNetworkState();
238 if (!network
->update_received())
240 if (!network
->visible())
242 if (network
->Matches(type
))
248 std::string
NetworkStateHandler::FormattedHardwareAddressForType(
249 const NetworkTypePattern
& type
) const {
250 const DeviceState
* device
= nullptr;
251 const NetworkState
* network
= ConnectedNetworkByType(type
);
253 device
= GetDeviceState(network
->device_path());
255 device
= GetDeviceStateByType(type
);
257 return std::string();
258 return network_util::FormattedMacAddress(device
->mac_address());
261 void NetworkStateHandler::GetVisibleNetworkListByType(
262 const NetworkTypePattern
& type
,
263 NetworkStateList
* list
) {
264 GetNetworkListByType(type
, false /* configured_only */,
265 true /* visible_only */, 0 /* no limit */, list
);
268 void NetworkStateHandler::GetVisibleNetworkList(NetworkStateList
* list
) {
269 GetVisibleNetworkListByType(NetworkTypePattern::Default(), list
);
272 void NetworkStateHandler::GetNetworkListByType(const NetworkTypePattern
& type
,
273 bool configured_only
,
276 NetworkStateList
* list
) {
280 // Sort the network list if necessary.
281 if (!network_list_sorted_
)
283 for (ManagedStateList::const_iterator iter
= network_list_
.begin();
284 iter
!= network_list_
.end(); ++iter
) {
285 const NetworkState
* network
= (*iter
)->AsNetworkState();
287 if (!network
->update_received() || !network
->Matches(type
))
289 if (configured_only
&& !network
->IsInProfile())
291 if (visible_only
&& !network
->visible())
293 list
->push_back(network
);
294 if (limit
> 0 && ++count
>= limit
)
299 const NetworkState
* NetworkStateHandler::GetNetworkStateFromServicePath(
300 const std::string
& service_path
,
301 bool configured_only
) const {
302 ManagedState
* managed
=
303 GetModifiableManagedState(&network_list_
, service_path
);
306 const NetworkState
* network
= managed
->AsNetworkState();
308 if (!network
->update_received() ||
309 (configured_only
&& !network
->IsInProfile())) {
315 const NetworkState
* NetworkStateHandler::GetNetworkStateFromGuid(
316 const std::string
& guid
) const {
317 DCHECK(!guid
.empty());
318 for (ManagedStateList::const_iterator iter
= network_list_
.begin();
319 iter
!= network_list_
.end(); ++iter
) {
320 const NetworkState
* network
= (*iter
)->AsNetworkState();
321 if (network
->guid() == guid
)
327 void NetworkStateHandler::GetDeviceList(DeviceStateList
* list
) const {
328 GetDeviceListByType(NetworkTypePattern::Default(), list
);
331 void NetworkStateHandler::GetDeviceListByType(const NetworkTypePattern
& type
,
332 DeviceStateList
* list
) const {
335 for (ManagedStateList::const_iterator iter
= device_list_
.begin();
336 iter
!= device_list_
.end(); ++iter
) {
337 const DeviceState
* device
= (*iter
)->AsDeviceState();
339 if (device
->update_received() && device
->Matches(type
))
340 list
->push_back(device
);
344 void NetworkStateHandler::RequestScan() const {
345 NET_LOG_USER("RequestScan", "");
346 shill_property_handler_
->RequestScan();
349 void NetworkStateHandler::RequestUpdateForNetwork(
350 const std::string
& service_path
) {
351 NetworkState
* network
= GetModifiableNetworkState(service_path
);
353 network
->set_update_requested(true);
354 NET_LOG_EVENT("RequestUpdate", service_path
);
355 shill_property_handler_
->RequestProperties(ManagedState::MANAGED_TYPE_NETWORK
,
359 void NetworkStateHandler::ClearLastErrorForNetwork(
360 const std::string
& service_path
) {
361 NetworkState
* network
= GetModifiableNetworkState(service_path
);
363 network
->clear_last_error();
366 void NetworkStateHandler::SetCheckPortalList(
367 const std::string
& check_portal_list
) {
368 NET_LOG_EVENT("SetCheckPortalList", check_portal_list
);
369 shill_property_handler_
->SetCheckPortalList(check_portal_list
);
372 void NetworkStateHandler::SetWakeOnLanEnabled(bool enabled
) {
373 NET_LOG_EVENT("SetWakeOnLanEnabled",
374 base::StringPrintf("%s", enabled
? "true" : "false"));
375 shill_property_handler_
->SetWakeOnLanEnabled(enabled
);
378 const NetworkState
* NetworkStateHandler::GetEAPForEthernet(
379 const std::string
& service_path
) {
380 const NetworkState
* network
= GetNetworkState(service_path
);
382 NET_LOG_ERROR("GetEAPForEthernet", "Unknown service path " + service_path
);
385 if (network
->type() != shill::kTypeEthernet
) {
386 NET_LOG_ERROR("GetEAPForEthernet", "Not of type Ethernet: " + service_path
);
389 if (!network
->IsConnectedState())
392 // The same EAP service is shared for all ethernet services/devices.
393 // However EAP is used/enabled per device and only if the connection was
394 // successfully established.
395 const DeviceState
* device
= GetDeviceState(network
->device_path());
397 NET_LOG(ERROR
) << "GetEAPForEthernet: Unknown device "
398 << network
->device_path()
399 << " for connected ethernet service: " << service_path
;
402 if (!device
->eap_authentication_completed())
405 NetworkStateList list
;
406 GetNetworkListByType(NetworkTypePattern::Primitive(shill::kTypeEthernetEap
),
407 true /* configured_only */, false /* visible_only */,
408 1 /* limit */, &list
);
410 NET_LOG_ERROR("GetEAPForEthernet",
412 "Ethernet service %s connected using EAP, but no "
413 "EAP service found.",
414 service_path
.c_str()));
420 //------------------------------------------------------------------------------
421 // ShillPropertyHandler::Delegate overrides
423 void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type
,
424 const base::ListValue
& entries
) {
425 ManagedStateList
* managed_list
= GetManagedList(type
);
426 NET_LOG_DEBUG("UpdateManagedList: " + ManagedState::TypeToString(type
),
427 base::StringPrintf("%" PRIuS
, entries
.GetSize()));
428 // Create a map of existing entries. Assumes all entries in |managed_list|
430 typedef std::map
<std::string
, ManagedState
*> ManagedMap
;
431 ManagedMap managed_map
;
432 for (ManagedStateList::iterator iter
= managed_list
->begin();
433 iter
!= managed_list
->end(); ++iter
) {
434 ManagedState
* managed
= *iter
;
435 DCHECK(!ContainsKey(managed_map
, managed
->path()));
436 managed_map
[managed
->path()] = managed
;
438 // Clear the list (pointers are temporarily owned by managed_map).
439 managed_list
->clear();
440 // Updates managed_list and request updates for new entries.
441 std::set
<std::string
> list_entries
;
442 for (base::ListValue::const_iterator iter
= entries
.begin();
443 iter
!= entries
.end(); ++iter
) {
445 (*iter
)->GetAsString(&path
);
446 if (path
.empty() || path
== shill::kFlimflamServicePath
) {
447 NET_LOG_ERROR(base::StringPrintf("Bad path in list:%d", type
), path
);
450 ManagedMap::iterator found
= managed_map
.find(path
);
451 if (found
== managed_map
.end()) {
452 if (list_entries
.count(path
) != 0) {
453 NET_LOG_ERROR("Duplicate entry in list", path
);
456 ManagedState
* managed
= ManagedState::Create(type
, path
);
457 managed_list
->push_back(managed
);
459 managed_list
->push_back(found
->second
);
460 managed_map
.erase(found
);
462 list_entries
.insert(path
);
464 // Delete any remaining entries in managed_map.
465 STLDeleteContainerPairSecondPointers(managed_map
.begin(), managed_map
.end());
468 void NetworkStateHandler::ProfileListChanged() {
469 NET_LOG_EVENT("ProfileListChanged", "Re-Requesting Network Properties");
470 for (ManagedStateList::iterator iter
= network_list_
.begin();
471 iter
!= network_list_
.end(); ++iter
) {
472 NetworkState
* network
= (*iter
)->AsNetworkState();
474 shill_property_handler_
->RequestProperties(
475 ManagedState::MANAGED_TYPE_NETWORK
, network
->path());
479 void NetworkStateHandler::UpdateManagedStateProperties(
480 ManagedState::ManagedType type
,
481 const std::string
& path
,
482 const base::DictionaryValue
& properties
) {
483 ManagedStateList
* managed_list
= GetManagedList(type
);
484 ManagedState
* managed
= GetModifiableManagedState(managed_list
, path
);
486 // The network has been removed from the list of networks.
487 NET_LOG_DEBUG("UpdateManagedStateProperties: Not found", path
);
490 managed
->set_update_received();
492 std::string desc
= GetManagedStateLogType(managed
) + " Properties Received";
493 NET_LOG_DEBUG(desc
, GetLogName(managed
));
495 if (type
== ManagedState::MANAGED_TYPE_NETWORK
) {
496 UpdateNetworkStateProperties(managed
->AsNetworkState(), properties
);
499 for (base::DictionaryValue::Iterator
iter(properties
); !iter
.IsAtEnd();
501 managed
->PropertyChanged(iter
.key(), iter
.value());
503 managed
->InitialPropertiesReceived(properties
);
505 managed
->set_update_requested(false);
508 void NetworkStateHandler::UpdateNetworkStateProperties(
509 NetworkState
* network
,
510 const base::DictionaryValue
& properties
) {
512 bool network_property_updated
= false;
513 std::string prev_connection_state
= network
->connection_state();
514 bool prev_is_captive_portal
= network
->is_captive_portal();
515 for (base::DictionaryValue::Iterator
iter(properties
); !iter
.IsAtEnd();
517 if (network
->PropertyChanged(iter
.key(), iter
.value()))
518 network_property_updated
= true;
520 network_property_updated
|= network
->InitialPropertiesReceived(properties
);
522 network_list_sorted_
= false;
524 // Notify observers of NetworkState changes.
525 if (network_property_updated
|| network
->update_requested()) {
526 // Signal connection state changed after all properties have been updated.
527 if (ConnectionStateChanged(network
, prev_connection_state
,
528 prev_is_captive_portal
)) {
529 OnNetworkConnectionStateChanged(network
);
531 NET_LOG_EVENT("NetworkPropertiesUpdated", GetLogName(network
));
532 NotifyNetworkPropertiesUpdated(network
);
536 void NetworkStateHandler::UpdateNetworkServiceProperty(
537 const std::string
& service_path
,
538 const std::string
& key
,
539 const base::Value
& value
) {
540 SCOPED_NET_LOG_IF_SLOW();
541 bool changed
= false;
542 NetworkState
* network
= GetModifiableNetworkState(service_path
);
545 std::string prev_connection_state
= network
->connection_state();
546 bool prev_is_captive_portal
= network
->is_captive_portal();
547 std::string prev_profile_path
= network
->profile_path();
548 changed
|= network
->PropertyChanged(key
, value
);
552 if (key
== shill::kStateProperty
|| key
== shill::kVisibleProperty
) {
553 network_list_sorted_
= false;
554 if (ConnectionStateChanged(network
, prev_connection_state
,
555 prev_is_captive_portal
)) {
556 OnNetworkConnectionStateChanged(network
);
557 // If the connection state changes, other properties such as IPConfig
558 // may have changed, so request a full update.
559 RequestUpdateForNetwork(service_path
);
562 std::string value_str
;
563 value
.GetAsString(&value_str
);
564 // Some property changes are noisy and not interesting:
565 // * Wifi SignalStrength
566 // * WifiFrequencyList updates
567 // * Device property changes to "/" (occurs before a service is removed)
568 if (key
!= shill::kSignalStrengthProperty
&&
569 key
!= shill::kWifiFrequencyListProperty
&&
570 (key
!= shill::kDeviceProperty
|| value_str
!= "/")) {
571 std::string log_event
= "NetworkPropertyUpdated";
572 // Trigger a default network update for interesting changes only.
573 if (network
->path() == default_network_path_
) {
574 NotifyDefaultNetworkChanged(network
);
575 log_event
= "Default" + log_event
;
578 std::string detail
= network
->name() + "." + key
;
579 detail
+= " = " + ValueAsString(value
);
580 device_event_log::LogLevel log_level
;
581 if (key
== shill::kErrorProperty
|| key
== shill::kErrorDetailsProperty
) {
582 log_level
= device_event_log::LOG_LEVEL_ERROR
;
584 log_level
= device_event_log::LOG_LEVEL_EVENT
;
586 NET_LOG_LEVEL(log_level
, log_event
, detail
);
590 // All property updates signal 'NetworkPropertiesUpdated'.
591 NotifyNetworkPropertiesUpdated(network
);
593 // If added to a Profile, request a full update so that a NetworkState
595 if (prev_profile_path
.empty() && !network
->profile_path().empty())
596 RequestUpdateForNetwork(service_path
);
599 void NetworkStateHandler::UpdateDeviceProperty(const std::string
& device_path
,
600 const std::string
& key
,
601 const base::Value
& value
) {
602 SCOPED_NET_LOG_IF_SLOW();
603 DeviceState
* device
= GetModifiableDeviceState(device_path
);
606 if (!device
->PropertyChanged(key
, value
))
609 std::string detail
= device
->name() + "." + key
;
610 detail
+= " = " + ValueAsString(value
);
611 NET_LOG_EVENT("DevicePropertyUpdated", detail
);
613 NotifyDeviceListChanged();
614 NotifyDevicePropertiesUpdated(device
);
616 if (key
== shill::kScanningProperty
&& device
->scanning() == false) {
617 NotifyScanCompleted(device
);
619 if (key
== shill::kEapAuthenticationCompletedProperty
) {
620 // Notify a change for each Ethernet service using this device.
621 NetworkStateList ethernet_services
;
622 GetNetworkListByType(NetworkTypePattern::Ethernet(),
623 false /* configured_only */, false /* visible_only */,
624 0 /* no limit */, ðernet_services
);
625 for (NetworkStateList::const_iterator it
= ethernet_services
.begin();
626 it
!= ethernet_services
.end(); ++it
) {
627 const NetworkState
* ethernet_service
= *it
;
628 if (ethernet_service
->update_received() ||
629 ethernet_service
->device_path() != device
->path()) {
632 RequestUpdateForNetwork(ethernet_service
->path());
637 void NetworkStateHandler::UpdateIPConfigProperties(
638 ManagedState::ManagedType type
,
639 const std::string
& path
,
640 const std::string
& ip_config_path
,
641 const base::DictionaryValue
& properties
) {
642 if (type
== ManagedState::MANAGED_TYPE_NETWORK
) {
643 NetworkState
* network
= GetModifiableNetworkState(path
);
646 network
->IPConfigPropertiesChanged(properties
);
647 NotifyNetworkPropertiesUpdated(network
);
648 if (network
->path() == default_network_path_
)
649 NotifyDefaultNetworkChanged(network
);
650 } else if (type
== ManagedState::MANAGED_TYPE_DEVICE
) {
651 DeviceState
* device
= GetModifiableDeviceState(path
);
654 device
->IPConfigPropertiesChanged(ip_config_path
, properties
);
655 NotifyDevicePropertiesUpdated(device
);
656 if (!default_network_path_
.empty()) {
657 const NetworkState
* default_network
=
658 GetNetworkState(default_network_path_
);
659 if (default_network
&& default_network
->device_path() == path
)
660 NotifyDefaultNetworkChanged(default_network
);
665 void NetworkStateHandler::CheckPortalListChanged(
666 const std::string
& check_portal_list
) {
667 check_portal_list_
= check_portal_list
;
670 void NetworkStateHandler::TechnologyListChanged() {
671 // Eventually we would like to replace Technology state with Device state.
672 // For now, treat technology state changes as device list changes.
673 NotifyDeviceListChanged();
676 void NetworkStateHandler::ManagedStateListChanged(
677 ManagedState::ManagedType type
) {
678 SCOPED_NET_LOG_IF_SLOW();
679 if (type
== ManagedState::MANAGED_TYPE_NETWORK
) {
681 UpdateNetworkStats();
682 // Notify observers that the list of networks has changed.
683 NET_LOG_EVENT("NOTIFY:NetworkListChanged",
684 base::StringPrintf("Size:%" PRIuS
, network_list_
.size()));
685 FOR_EACH_OBSERVER(NetworkStateHandlerObserver
, observers_
,
686 NetworkListChanged());
687 } else if (type
== ManagedState::MANAGED_TYPE_DEVICE
) {
689 for (ManagedStateList::const_iterator iter
= device_list_
.begin();
690 iter
!= device_list_
.end(); ++iter
) {
691 if (iter
!= device_list_
.begin())
693 devices
+= (*iter
)->name();
695 NET_LOG_EVENT("DeviceList", devices
);
696 NotifyDeviceListChanged();
702 void NetworkStateHandler::SortNetworkList() {
703 // Note: usually active networks will precede inactive networks, however
704 // this may briefly be untrue during state transitions (e.g. a network may
705 // transition to idle before the list is updated).
706 ManagedStateList active
, non_wifi_visible
, wifi_visible
, hidden
, new_networks
;
707 for (ManagedStateList::iterator iter
= network_list_
.begin();
708 iter
!= network_list_
.end(); ++iter
) {
709 NetworkState
* network
= (*iter
)->AsNetworkState();
710 if (!network
->update_received()) {
711 new_networks
.push_back(network
);
714 if (network
->IsConnectedState() || network
->IsConnectingState()) {
715 active
.push_back(network
);
718 if (network
->visible()) {
719 if (NetworkTypePattern::WiFi().MatchesType(network
->type()))
720 wifi_visible
.push_back(network
);
722 non_wifi_visible
.push_back(network
);
724 hidden
.push_back(network
);
727 network_list_
.clear();
728 network_list_
.insert(network_list_
.end(), active
.begin(), active
.end());
729 network_list_
.insert(network_list_
.end(), non_wifi_visible
.begin(),
730 non_wifi_visible
.end());
731 network_list_
.insert(network_list_
.end(), wifi_visible
.begin(),
733 network_list_
.insert(network_list_
.end(), hidden
.begin(), hidden
.end());
734 network_list_
.insert(network_list_
.end(), new_networks
.begin(),
736 network_list_sorted_
= true;
739 void NetworkStateHandler::UpdateNetworkStats() {
740 size_t shared
= 0, unshared
= 0, visible
= 0;
741 for (ManagedStateList::iterator iter
= network_list_
.begin();
742 iter
!= network_list_
.end(); ++iter
) {
743 NetworkState
* network
= (*iter
)->AsNetworkState();
744 if (network
->visible())
746 if (network
->IsInProfile()) {
747 if (network
->IsPrivate())
753 UMA_HISTOGRAM_COUNTS_100("Networks.Visible", visible
);
754 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedShared", shared
);
755 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedUnshared", unshared
);
758 void NetworkStateHandler::DefaultNetworkServiceChanged(
759 const std::string
& service_path
) {
760 // Shill uses '/' for empty service path values; check explicitly for that.
761 const char* kEmptyServicePath
= "/";
762 std::string new_service_path
=
763 (service_path
!= kEmptyServicePath
) ? service_path
: "";
764 if (new_service_path
== default_network_path_
)
767 default_network_path_
= service_path
;
768 NET_LOG_EVENT("DefaultNetworkServiceChanged:", default_network_path_
);
769 const NetworkState
* network
= nullptr;
770 if (!default_network_path_
.empty()) {
771 network
= GetNetworkState(default_network_path_
);
773 // If NetworkState is not available yet, do not notify observers here,
774 // they will be notified when the state is received.
775 NET_LOG(EVENT
) << "Default NetworkState not available: "
776 << default_network_path_
;
780 if (network
&& !network
->IsConnectedState()) {
781 if (network
->IsConnectingState()) {
782 NET_LOG(EVENT
) << "DefaultNetwork is connecting: " << GetLogName(network
)
783 << ": " << network
->connection_state();
785 NET_LOG(ERROR
) << "DefaultNetwork in unexpected state: "
786 << GetLogName(network
) << ": "
787 << network
->connection_state();
789 // Do not notify observers here, the notification will occur when the
790 // connection state changes.
793 NotifyDefaultNetworkChanged(network
);
796 //------------------------------------------------------------------------------
799 void NetworkStateHandler::UpdateGuid(NetworkState
* network
) {
800 std::string specifier
= network
->GetSpecifier();
801 DCHECK(!specifier
.empty());
802 if (!network
->guid().empty()) {
803 // If the network is saved in a profile, remove the entry from the map.
804 // Otherwise ensure that the entry matches the specified GUID. (e.g. in
805 // case a visible network with a specified guid gets configured with a
807 if (network
->IsInProfile())
808 specifier_guid_map_
.erase(specifier
);
810 specifier_guid_map_
[specifier
] = network
->guid();
813 // Ensure that the NetworkState has a valid GUID.
815 SpecifierGuidMap::iterator iter
= specifier_guid_map_
.find(specifier
);
816 if (iter
!= specifier_guid_map_
.end()) {
819 guid
= base::GenerateGUID();
820 specifier_guid_map_
[specifier
] = guid
;
822 network
->SetGuid(guid
);
825 void NetworkStateHandler::NotifyDeviceListChanged() {
826 SCOPED_NET_LOG_IF_SLOW();
827 NET_LOG_DEBUG("NOTIFY:DeviceListChanged",
828 base::StringPrintf("Size:%" PRIuS
, device_list_
.size()));
829 FOR_EACH_OBSERVER(NetworkStateHandlerObserver
, observers_
,
830 DeviceListChanged());
833 DeviceState
* NetworkStateHandler::GetModifiableDeviceState(
834 const std::string
& device_path
) const {
835 ManagedState
* managed
= GetModifiableManagedState(&device_list_
, device_path
);
838 return managed
->AsDeviceState();
841 NetworkState
* NetworkStateHandler::GetModifiableNetworkState(
842 const std::string
& service_path
) const {
843 ManagedState
* managed
=
844 GetModifiableManagedState(&network_list_
, service_path
);
847 return managed
->AsNetworkState();
850 ManagedState
* NetworkStateHandler::GetModifiableManagedState(
851 const ManagedStateList
* managed_list
,
852 const std::string
& path
) const {
853 for (ManagedStateList::const_iterator iter
= managed_list
->begin();
854 iter
!= managed_list
->end(); ++iter
) {
855 ManagedState
* managed
= *iter
;
856 if (managed
->path() == path
)
862 NetworkStateHandler::ManagedStateList
* NetworkStateHandler::GetManagedList(
863 ManagedState::ManagedType type
) {
865 case ManagedState::MANAGED_TYPE_NETWORK
:
866 return &network_list_
;
867 case ManagedState::MANAGED_TYPE_DEVICE
:
868 return &device_list_
;
874 void NetworkStateHandler::OnNetworkConnectionStateChanged(
875 NetworkState
* network
) {
876 SCOPED_NET_LOG_IF_SLOW();
878 bool notify_default
= false;
879 if (network
->path() == default_network_path_
) {
880 if (network
->IsConnectedState()) {
881 notify_default
= true;
882 } else if (network
->IsConnectingState()) {
883 // Wait until the network is actually connected to notify that the default
885 NET_LOG(EVENT
) << "Default network is not connected: "
886 << GetLogName(network
)
887 << "State: " << network
->connection_state();
889 NET_LOG(ERROR
) << "Default network in unexpected state: "
890 << GetLogName(network
)
891 << "State: " << network
->connection_state();
892 default_network_path_
.clear();
894 NotifyDefaultNetworkChanged(nullptr);
897 std::string desc
= "NetworkConnectionStateChanged";
899 desc
= "Default" + desc
;
900 NET_LOG(EVENT
) << "NOTIFY: " << desc
<< ": " << GetLogName(network
) << ": "
901 << network
->connection_state();
902 FOR_EACH_OBSERVER(NetworkStateHandlerObserver
, observers_
,
903 NetworkConnectionStateChanged(network
));
905 NotifyDefaultNetworkChanged(network
);
908 void NetworkStateHandler::NotifyDefaultNetworkChanged(
909 const NetworkState
* default_network
) {
910 SCOPED_NET_LOG_IF_SLOW();
911 NET_LOG_EVENT("NOTIFY:DefaultNetworkChanged", GetLogName(default_network
));
912 FOR_EACH_OBSERVER(NetworkStateHandlerObserver
, observers_
,
913 DefaultNetworkChanged(default_network
));
916 void NetworkStateHandler::NotifyNetworkPropertiesUpdated(
917 const NetworkState
* network
) {
918 SCOPED_NET_LOG_IF_SLOW();
919 NET_LOG_DEBUG("NOTIFY:NetworkPropertiesUpdated", GetLogName(network
));
920 FOR_EACH_OBSERVER(NetworkStateHandlerObserver
, observers_
,
921 NetworkPropertiesUpdated(network
));
924 void NetworkStateHandler::NotifyDevicePropertiesUpdated(
925 const DeviceState
* device
) {
926 SCOPED_NET_LOG_IF_SLOW();
927 NET_LOG_DEBUG("NOTIFY:DevicePropertiesUpdated", GetLogName(device
));
928 FOR_EACH_OBSERVER(NetworkStateHandlerObserver
, observers_
,
929 DevicePropertiesUpdated(device
));
932 void NetworkStateHandler::NotifyScanCompleted(const DeviceState
* device
) {
933 SCOPED_NET_LOG_IF_SLOW();
934 NET_LOG_DEBUG("NOTIFY:ScanCompleted", GetLogName(device
));
935 FOR_EACH_OBSERVER(NetworkStateHandlerObserver
, observers_
,
936 ScanCompleted(device
));
939 std::string
NetworkStateHandler::GetTechnologyForType(
940 const NetworkTypePattern
& type
) const {
941 if (type
.MatchesType(shill::kTypeEthernet
))
942 return shill::kTypeEthernet
;
944 if (type
.MatchesType(shill::kTypeWifi
))
945 return shill::kTypeWifi
;
947 if (type
.Equals(NetworkTypePattern::Wimax()))
948 return shill::kTypeWimax
;
950 // Prefer Wimax over Cellular only if it's available.
951 if (type
.MatchesType(shill::kTypeWimax
) &&
952 shill_property_handler_
->IsTechnologyAvailable(shill::kTypeWimax
)) {
953 return shill::kTypeWimax
;
956 if (type
.MatchesType(shill::kTypeCellular
))
957 return shill::kTypeCellular
;
960 return std::string();
963 ScopedVector
<std::string
> NetworkStateHandler::GetTechnologiesForType(
964 const NetworkTypePattern
& type
) const {
965 ScopedVector
<std::string
> technologies
;
966 if (type
.MatchesType(shill::kTypeEthernet
))
967 technologies
.push_back(new std::string(shill::kTypeEthernet
));
968 if (type
.MatchesType(shill::kTypeWifi
))
969 technologies
.push_back(new std::string(shill::kTypeWifi
));
970 if (type
.MatchesType(shill::kTypeWimax
))
971 technologies
.push_back(new std::string(shill::kTypeWimax
));
972 if (type
.MatchesType(shill::kTypeCellular
))
973 technologies
.push_back(new std::string(shill::kTypeCellular
));
974 if (type
.MatchesType(shill::kTypeBluetooth
))
975 technologies
.push_back(new std::string(shill::kTypeBluetooth
));
976 if (type
.MatchesType(shill::kTypeVPN
))
977 technologies
.push_back(new std::string(shill::kTypeVPN
));
979 CHECK_GT(technologies
.size(), 0ul);
980 return technologies
.Pass();
983 } // namespace chromeos