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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 | 173 |
174 const NetworkState* NetworkStateHandler::GetNetworkState( | 174 const NetworkState* NetworkStateHandler::GetNetworkState( |
175 const std::string& service_path) const { | 175 const std::string& service_path) const { |
176 const NetworkState* network = GetModifiableNetworkState(service_path); | 176 const NetworkState* network = GetModifiableNetworkState(service_path); |
177 if (network && !network->update_received()) | 177 if (network && !network->update_received()) |
178 return NULL; | 178 return NULL; |
179 return network; | 179 return network; |
180 } | 180 } |
181 | 181 |
182 const NetworkState* NetworkStateHandler::DefaultNetwork() const { | 182 const NetworkState* NetworkStateHandler::DefaultNetwork() const { |
183 if (network_list_.empty()) | 183 if (default_network_path_.empty()) |
184 return NULL; | 184 return NULL; |
185 const NetworkState* network = network_list_.front()->AsNetworkState(); | 185 return GetNetworkState(default_network_path_); |
186 DCHECK(network); | |
187 if (!network->update_received() || !network->IsConnectedState()) | |
188 return NULL; | |
189 return network; | |
190 } | 186 } |
191 | 187 |
192 const FavoriteState* NetworkStateHandler::DefaultFavoriteNetwork() const { | 188 const FavoriteState* NetworkStateHandler::DefaultFavoriteNetwork() const { |
193 const NetworkState* default_network = DefaultNetwork(); | 189 const NetworkState* default_network = DefaultNetwork(); |
194 if (!default_network) | 190 if (!default_network) |
195 return NULL; | 191 return NULL; |
196 const FavoriteState* default_favorite = | 192 const FavoriteState* default_favorite = |
197 GetFavoriteState(default_network->path()); | 193 GetFavoriteState(default_network->path()); |
198 DCHECK(default_favorite); | 194 DCHECK(default_favorite); |
199 DCHECK(default_favorite->update_received()); | 195 DCHECK(default_favorite->update_received()); |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 !iter.IsAtEnd(); iter.Advance()) { | 527 !iter.IsAtEnd(); iter.Advance()) { |
532 if (network->PropertyChanged(iter.key(), iter.value())) | 528 if (network->PropertyChanged(iter.key(), iter.value())) |
533 network_property_updated = true; | 529 network_property_updated = true; |
534 } | 530 } |
535 network_property_updated |= network->InitialPropertiesReceived(properties); | 531 network_property_updated |= network->InitialPropertiesReceived(properties); |
536 // Notify observers of NetworkState changes. | 532 // Notify observers of NetworkState changes. |
537 if (network_property_updated || network->update_requested()) { | 533 if (network_property_updated || network->update_requested()) { |
538 // Signal connection state changed after all properties have been updated. | 534 // Signal connection state changed after all properties have been updated. |
539 if (ConnectionStateChanged(network, prev_connection_state)) | 535 if (ConnectionStateChanged(network, prev_connection_state)) |
540 OnNetworkConnectionStateChanged(network); | 536 OnNetworkConnectionStateChanged(network); |
541 NetworkPropertiesUpdated(network); | 537 NET_LOG_EVENT("NetworkPropertiesUpdated", GetManagedStateLogName(network)); |
538 NotifyNetworkPropertiesUpdated(network); | |
542 } | 539 } |
543 } | 540 } |
544 | 541 |
545 void NetworkStateHandler::UpdateNetworkServiceProperty( | 542 void NetworkStateHandler::UpdateNetworkServiceProperty( |
546 const std::string& service_path, | 543 const std::string& service_path, |
547 const std::string& key, | 544 const std::string& key, |
548 const base::Value& value) { | 545 const base::Value& value) { |
549 // Update any associated FavoriteState. | 546 // Update any associated FavoriteState. |
550 ManagedState* favorite = | 547 ManagedState* favorite = |
551 GetModifiableManagedState(&favorite_list_, service_path); | 548 GetModifiableManagedState(&favorite_list_, service_path); |
(...skipping 21 matching lines...) Expand all Loading... | |
573 } else { | 570 } else { |
574 std::string value_str; | 571 std::string value_str; |
575 value.GetAsString(&value_str); | 572 value.GetAsString(&value_str); |
576 // Some property changes are noisy and not interesting: | 573 // Some property changes are noisy and not interesting: |
577 // * Wifi SignalStrength | 574 // * Wifi SignalStrength |
578 // * WifiFrequencyList updates | 575 // * WifiFrequencyList updates |
579 // * Device property changes to "/" (occurs before a service is removed) | 576 // * Device property changes to "/" (occurs before a service is removed) |
580 if (key != shill::kSignalStrengthProperty && | 577 if (key != shill::kSignalStrengthProperty && |
581 key != shill::kWifiFrequencyListProperty && | 578 key != shill::kWifiFrequencyListProperty && |
582 (key != shill::kDeviceProperty || value_str != "/")) { | 579 (key != shill::kDeviceProperty || value_str != "/")) { |
580 std::string log_event = "NetworkPropertyUpdated"; | |
583 // Trigger a default network update for interesting changes only. | 581 // Trigger a default network update for interesting changes only. |
584 if (network->path() == default_network_path_) | 582 if (network->path() == default_network_path_) { |
585 OnDefaultNetworkChanged(); | 583 NotifyDefaultNetworkChanged(network); |
586 // Log interesting event. | 584 log_event = "Default" + log_event; |
585 } | |
586 // Log event. | |
587 std::string detail = network->name() + "." + key; | 587 std::string detail = network->name() + "." + key; |
588 detail += " = " + network_event_log::ValueAsString(value); | 588 detail += " = " + network_event_log::ValueAsString(value); |
589 network_event_log::LogLevel log_level; | 589 network_event_log::LogLevel log_level; |
590 if (key == shill::kErrorProperty || key == shill::kErrorDetailsProperty) { | 590 if (key == shill::kErrorProperty || key == shill::kErrorDetailsProperty) { |
591 log_level = network_event_log::LOG_LEVEL_ERROR; | 591 log_level = network_event_log::LOG_LEVEL_ERROR; |
592 } else { | 592 } else { |
593 log_level = network_event_log::LOG_LEVEL_EVENT; | 593 log_level = network_event_log::LOG_LEVEL_EVENT; |
594 } | 594 } |
595 NET_LOG_LEVEL(log_level, "NetworkPropertyUpdated", detail); | 595 NET_LOG_LEVEL(log_level, log_event, detail); |
596 } | 596 } |
597 } | 597 } |
598 | 598 |
599 // All property updates signal 'NetworkPropertiesUpdated'. | 599 // All property updates signal 'NetworkPropertiesUpdated'. |
600 NetworkPropertiesUpdated(network); | 600 NotifyNetworkPropertiesUpdated(network); |
601 | 601 |
602 // If added to a Profile, request a full update so that a FavoriteState | 602 // If added to a Profile, request a full update so that a FavoriteState |
603 // gets created. | 603 // gets created. |
604 if (prev_profile_path.empty() && !network->profile_path().empty()) | 604 if (prev_profile_path.empty() && !network->profile_path().empty()) |
605 RequestUpdateForNetwork(service_path); | 605 RequestUpdateForNetwork(service_path); |
606 } | 606 } |
607 | 607 |
608 void NetworkStateHandler::UpdateDeviceProperty(const std::string& device_path, | 608 void NetworkStateHandler::UpdateDeviceProperty(const std::string& device_path, |
609 const std::string& key, | 609 const std::string& key, |
610 const base::Value& value) { | 610 const base::Value& value) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
650 } | 650 } |
651 | 651 |
652 void NetworkStateHandler::ManagedStateListChanged( | 652 void NetworkStateHandler::ManagedStateListChanged( |
653 ManagedState::ManagedType type) { | 653 ManagedState::ManagedType type) { |
654 if (type == ManagedState::MANAGED_TYPE_NETWORK) { | 654 if (type == ManagedState::MANAGED_TYPE_NETWORK) { |
655 // Notify observers that the list of networks has changed. | 655 // Notify observers that the list of networks has changed. |
656 NET_LOG_EVENT("NetworkListChanged", | 656 NET_LOG_EVENT("NetworkListChanged", |
657 base::StringPrintf("Size:%" PRIuS, network_list_.size())); | 657 base::StringPrintf("Size:%" PRIuS, network_list_.size())); |
658 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 658 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
659 NetworkListChanged()); | 659 NetworkListChanged()); |
660 // The list order may have changed, so check if the default network changed. | |
661 if (CheckDefaultNetworkChanged()) | |
662 OnDefaultNetworkChanged(); | |
663 // Update UMA stats. | 660 // Update UMA stats. |
664 UMA_HISTOGRAM_COUNTS_100("Networks.Visible", network_list_.size()); | 661 UMA_HISTOGRAM_COUNTS_100("Networks.Visible", network_list_.size()); |
665 } else if (type == ManagedState::MANAGED_TYPE_FAVORITE) { | 662 } else if (type == ManagedState::MANAGED_TYPE_FAVORITE) { |
666 NET_LOG_DEBUG("FavoriteListChanged", | 663 NET_LOG_DEBUG("FavoriteListChanged", |
667 base::StringPrintf("Size:%" PRIuS, favorite_list_.size())); | 664 base::StringPrintf("Size:%" PRIuS, favorite_list_.size())); |
668 // The FavoriteState list only changes when the NetworkState list changes, | 665 // The FavoriteState list only changes when the NetworkState list changes, |
669 // so no need to signal observers here again. | 666 // so no need to signal observers here again. |
670 | 667 |
671 // Update UMA stats. | 668 // Update UMA stats. |
672 size_t shared = 0, unshared = 0; | 669 size_t shared = 0, unshared = 0; |
673 for (ManagedStateList::iterator iter = favorite_list_.begin(); | 670 for (ManagedStateList::iterator iter = favorite_list_.begin(); |
674 iter != favorite_list_.end(); ++iter) { | 671 iter != favorite_list_.end(); ++iter) { |
675 FavoriteState* favorite = (*iter)->AsFavoriteState(); | 672 FavoriteState* favorite = (*iter)->AsFavoriteState(); |
676 if (!favorite->is_favorite()) | 673 if (!favorite->is_favorite()) |
677 continue; | 674 continue; |
678 if (favorite->IsPrivate()) | 675 if (favorite->IsPrivate()) |
679 ++unshared; | 676 ++unshared; |
680 else | 677 else |
681 ++shared; | 678 ++shared; |
682 } | 679 } |
683 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedShared", shared); | 680 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedShared", shared); |
684 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedUnshared", unshared); | 681 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedUnshared", unshared); |
685 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { | 682 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { |
686 NotifyDeviceListChanged(); | 683 NotifyDeviceListChanged(); |
687 } else { | 684 } else { |
688 NOTREACHED(); | 685 NOTREACHED(); |
689 } | 686 } |
690 } | 687 } |
691 | 688 |
689 void NetworkStateHandler::DefaultNetworkServiceChanged( | |
690 const std::string& service_path) { | |
691 const char* kEmptyServicePath = "/"; | |
pneubeck (no reviews)
2014/02/26 08:52:42
Better add a short comment why we are doing this.
stevenjb
2014/02/26 18:04:47
Done.
| |
692 if (service_path == kEmptyServicePath) | |
693 default_network_path_.clear(); | |
694 else | |
695 default_network_path_ = service_path; | |
pneubeck (no reviews)
2014/02/26 08:52:42
everything after this line should probably use def
stevenjb
2014/02/26 18:04:47
Yes indeed, thanks.
| |
696 NET_LOG_EVENT("DefaultNetworkServiceChanged", service_path); | |
697 const NetworkState* network = NULL; | |
698 if (!service_path.empty()) { | |
gauravsh
2014/02/26 00:44:32
Shouldn't this check also be service_path == kEmpt
stevenjb
2014/02/26 18:04:47
Ugh, thanks. We should use default_network_path_ b
| |
699 network = GetNetworkState(service_path); | |
700 if (!network) { | |
701 // If NetworkState is not available yet, do not notify observers here, | |
702 // they will be notified when the state is received. | |
703 NET_LOG_DEBUG("Default NetworkState not available", service_path); | |
704 return; | |
705 } | |
706 } | |
707 if (network && !network->IsConnectedState()) { | |
708 NET_LOG_ERROR( | |
709 "DefaultNetwork is not connected: " + network->connection_state(), | |
710 network->path()); | |
711 } | |
712 NotifyDefaultNetworkChanged(network); | |
713 } | |
714 | |
692 //------------------------------------------------------------------------------ | 715 //------------------------------------------------------------------------------ |
693 // Private methods | 716 // Private methods |
694 | 717 |
695 void NetworkStateHandler::NotifyDeviceListChanged() { | 718 void NetworkStateHandler::NotifyDeviceListChanged() { |
696 NET_LOG_DEBUG("NotifyDeviceListChanged", | 719 NET_LOG_DEBUG("NotifyDeviceListChanged", |
697 base::StringPrintf("Size:%" PRIuS, device_list_.size())); | 720 base::StringPrintf("Size:%" PRIuS, device_list_.size())); |
698 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 721 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
699 DeviceListChanged()); | 722 DeviceListChanged()); |
700 } | 723 } |
701 | 724 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
738 case ManagedState::MANAGED_TYPE_DEVICE: | 761 case ManagedState::MANAGED_TYPE_DEVICE: |
739 return &device_list_; | 762 return &device_list_; |
740 } | 763 } |
741 NOTREACHED(); | 764 NOTREACHED(); |
742 return NULL; | 765 return NULL; |
743 } | 766 } |
744 | 767 |
745 void NetworkStateHandler::OnNetworkConnectionStateChanged( | 768 void NetworkStateHandler::OnNetworkConnectionStateChanged( |
746 NetworkState* network) { | 769 NetworkState* network) { |
747 DCHECK(network); | 770 DCHECK(network); |
748 NET_LOG_EVENT("NetworkConnectionStateChanged", base::StringPrintf( | 771 std::string event = "NetworkConnectionStateChanged"; |
749 "%s:%s", GetManagedStateLogName(network).c_str(), | 772 if (network->path() == default_network_path_) { |
750 network->connection_state().c_str())); | 773 event = "Default" + event; |
774 if (!network->IsConnectedState()) { | |
775 NET_LOG_ERROR( | |
776 "DefaultNetwork is not connected: " + network->connection_state(), | |
777 network->path()); | |
778 } | |
779 } | |
780 NET_LOG_EVENT(event + ": " + network->connection_state(), | |
781 GetManagedStateLogName(network)); | |
751 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 782 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
752 NetworkConnectionStateChanged(network)); | 783 NetworkConnectionStateChanged(network)); |
753 if (CheckDefaultNetworkChanged() || network->path() == default_network_path_) | 784 if (network->path() == default_network_path_) |
754 OnDefaultNetworkChanged(); | 785 NotifyDefaultNetworkChanged(network); |
755 } | 786 } |
756 | 787 |
757 bool NetworkStateHandler::CheckDefaultNetworkChanged() { | 788 void NetworkStateHandler::NotifyDefaultNetworkChanged( |
758 std::string new_default_network_path; | 789 const NetworkState* default_network) { |
759 const NetworkState* new_default_network = DefaultNetwork(); | |
760 if (new_default_network) | |
761 new_default_network_path = new_default_network->path(); | |
762 if (new_default_network_path == default_network_path_) | |
763 return false; | |
764 default_network_path_ = new_default_network_path; | |
765 return true; | |
766 } | |
767 | |
768 void NetworkStateHandler::OnDefaultNetworkChanged() { | |
769 const NetworkState* default_network = DefaultNetwork(); | |
770 NET_LOG_EVENT("DefaultNetworkChanged", | |
771 GetManagedStateLogName(default_network)); | |
772 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 790 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
773 DefaultNetworkChanged(default_network)); | 791 DefaultNetworkChanged(default_network)); |
774 } | 792 } |
775 | 793 |
776 void NetworkStateHandler::NetworkPropertiesUpdated( | 794 void NetworkStateHandler::NotifyNetworkPropertiesUpdated( |
777 const NetworkState* network) { | 795 const NetworkState* network) { |
778 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 796 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
779 NetworkPropertiesUpdated(network)); | 797 NetworkPropertiesUpdated(network)); |
780 } | 798 } |
781 | 799 |
782 void NetworkStateHandler::ScanCompleted(const std::string& type) { | 800 void NetworkStateHandler::ScanCompleted(const std::string& type) { |
783 size_t num_callbacks = scan_complete_callbacks_.count(type); | 801 size_t num_callbacks = scan_complete_callbacks_.count(type); |
784 NET_LOG_EVENT("ScanCompleted", | 802 NET_LOG_EVENT("ScanCompleted", |
785 base::StringPrintf("%s:%" PRIuS, type.c_str(), num_callbacks)); | 803 base::StringPrintf("%s:%" PRIuS, type.c_str(), num_callbacks)); |
786 if (num_callbacks == 0) | 804 if (num_callbacks == 0) |
(...skipping 24 matching lines...) Expand all Loading... | |
811 } | 829 } |
812 | 830 |
813 if (type.MatchesType(shill::kTypeCellular)) | 831 if (type.MatchesType(shill::kTypeCellular)) |
814 return shill::kTypeCellular; | 832 return shill::kTypeCellular; |
815 | 833 |
816 NOTREACHED(); | 834 NOTREACHED(); |
817 return std::string(); | 835 return std::string(); |
818 } | 836 } |
819 | 837 |
820 } // namespace chromeos | 838 } // namespace chromeos |
OLD | NEW |