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/guid.h" | |
9 #include "base/location.h" | 10 #include "base/location.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "chromeos/network/device_state.h" | 17 #include "chromeos/network/device_state.h" |
17 #include "chromeos/network/favorite_state.h" | 18 #include "chromeos/network/favorite_state.h" |
18 #include "chromeos/network/managed_state.h" | 19 #include "chromeos/network/managed_state.h" |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 const NetworkState* NetworkStateHandler::DefaultNetwork() const { | 188 const NetworkState* NetworkStateHandler::DefaultNetwork() const { |
188 if (default_network_path_.empty()) | 189 if (default_network_path_.empty()) |
189 return NULL; | 190 return NULL; |
190 return GetNetworkState(default_network_path_); | 191 return GetNetworkState(default_network_path_); |
191 } | 192 } |
192 | 193 |
193 const FavoriteState* NetworkStateHandler::DefaultFavoriteNetwork() const { | 194 const FavoriteState* NetworkStateHandler::DefaultFavoriteNetwork() const { |
194 const NetworkState* default_network = DefaultNetwork(); | 195 const NetworkState* default_network = DefaultNetwork(); |
195 if (!default_network) | 196 if (!default_network) |
196 return NULL; | 197 return NULL; |
197 const FavoriteState* default_favorite = | 198 const FavoriteState* default_favorite = GetFavoriteStateFromServicePath( |
198 GetFavoriteState(default_network->path()); | 199 default_network->path(), true /* configured_only */); |
199 DCHECK(default_network->type() != shill::kTypeWifi || | 200 DCHECK(default_network->type() != shill::kTypeWifi || default_favorite) |
200 default_favorite) << "No favorite for: " << default_network->path(); | 201 << "No favorite for: " << default_network->path(); |
201 DCHECK(!default_favorite || default_favorite->update_received()) | 202 DCHECK(!default_favorite || default_favorite->update_received()) |
202 << "No update received for: " << default_network->path(); | 203 << "No update received for: " << default_network->path(); |
203 return default_favorite; | 204 return default_favorite; |
204 } | 205 } |
205 | 206 |
206 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( | 207 const NetworkState* NetworkStateHandler::ConnectedNetworkByType( |
207 const NetworkTypePattern& type) const { | 208 const NetworkTypePattern& type) const { |
208 for (ManagedStateList::const_iterator iter = network_list_.begin(); | 209 for (ManagedStateList::const_iterator iter = network_list_.begin(); |
209 iter != network_list_.end(); ++iter) { | 210 iter != network_list_.end(); ++iter) { |
210 const NetworkState* network = (*iter)->AsNetworkState(); | 211 const NetworkState* network = (*iter)->AsNetworkState(); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 | 303 |
303 void NetworkStateHandler::GetFavoriteListByType(const NetworkTypePattern& type, | 304 void NetworkStateHandler::GetFavoriteListByType(const NetworkTypePattern& type, |
304 FavoriteStateList* list) const { | 305 FavoriteStateList* list) const { |
305 DCHECK(list); | 306 DCHECK(list); |
306 FavoriteStateList result; | 307 FavoriteStateList result; |
307 list->clear(); | 308 list->clear(); |
308 for (ManagedStateList::const_iterator iter = favorite_list_.begin(); | 309 for (ManagedStateList::const_iterator iter = favorite_list_.begin(); |
309 iter != favorite_list_.end(); ++iter) { | 310 iter != favorite_list_.end(); ++iter) { |
310 const FavoriteState* favorite = (*iter)->AsFavoriteState(); | 311 const FavoriteState* favorite = (*iter)->AsFavoriteState(); |
311 DCHECK(favorite); | 312 DCHECK(favorite); |
312 if (favorite->update_received() && favorite->IsFavorite() && | 313 if (favorite->update_received() && favorite->IsInProfile() && |
313 favorite->Matches(type)) { | 314 favorite->Matches(type)) { |
314 list->push_back(favorite); | 315 list->push_back(favorite); |
315 } | 316 } |
316 } | 317 } |
317 } | 318 } |
318 | 319 |
319 const FavoriteState* NetworkStateHandler::GetFavoriteState( | 320 const FavoriteState* NetworkStateHandler::GetFavoriteStateFromServicePath( |
320 const std::string& service_path) const { | 321 const std::string& service_path, |
322 bool configured_only) const { | |
321 ManagedState* managed = | 323 ManagedState* managed = |
322 GetModifiableManagedState(&favorite_list_, service_path); | 324 GetModifiableManagedState(&favorite_list_, service_path); |
323 if (!managed) | 325 if (!managed) |
324 return NULL; | 326 return NULL; |
325 const FavoriteState* favorite = managed->AsFavoriteState(); | 327 const FavoriteState* favorite = managed->AsFavoriteState(); |
326 DCHECK(favorite); | 328 DCHECK(favorite); |
327 if (!favorite->update_received() || !favorite->IsFavorite()) | 329 if (!favorite->update_received() || |
330 (configured_only && !favorite->IsInProfile())) { | |
328 return NULL; | 331 return NULL; |
332 } | |
329 return favorite; | 333 return favorite; |
330 } | 334 } |
331 | 335 |
336 const FavoriteState* NetworkStateHandler::GetFavoriteStateFromGuid( | |
337 const std::string& guid) const { | |
338 DCHECK(!guid.empty()); | |
339 for (ManagedStateList::const_iterator iter = favorite_list_.begin(); | |
340 iter != favorite_list_.end(); ++iter) { | |
341 const FavoriteState* favorite = (*iter)->AsFavoriteState(); | |
342 if (favorite->guid() == guid) | |
343 return favorite; | |
344 } | |
345 return NULL; | |
346 } | |
347 | |
332 void NetworkStateHandler::RequestScan() const { | 348 void NetworkStateHandler::RequestScan() const { |
333 NET_LOG_USER("RequestScan", ""); | 349 NET_LOG_USER("RequestScan", ""); |
334 shill_property_handler_->RequestScan(); | 350 shill_property_handler_->RequestScan(); |
335 } | 351 } |
336 | 352 |
337 void NetworkStateHandler::WaitForScan(const std::string& type, | 353 void NetworkStateHandler::WaitForScan(const std::string& type, |
338 const base::Closure& callback) { | 354 const base::Closure& callback) { |
339 scan_complete_callbacks_[type].push_back(callback); | 355 scan_complete_callbacks_[type].push_back(callback); |
340 if (!GetScanningByType(NetworkTypePattern::Primitive(type))) | 356 if (!GetScanningByType(NetworkTypePattern::Primitive(type))) |
341 RequestScan(); | 357 RequestScan(); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
482 ManagedState* managed = GetModifiableManagedState(managed_list, path); | 498 ManagedState* managed = GetModifiableManagedState(managed_list, path); |
483 if (!managed) { | 499 if (!managed) { |
484 if (type != ManagedState::MANAGED_TYPE_FAVORITE) { | 500 if (type != ManagedState::MANAGED_TYPE_FAVORITE) { |
485 // The network has been removed from the list of visible networks. | 501 // The network has been removed from the list of visible networks. |
486 NET_LOG_DEBUG("UpdateManagedStateProperties: Not found", path); | 502 NET_LOG_DEBUG("UpdateManagedStateProperties: Not found", path); |
487 return; | 503 return; |
488 } | 504 } |
489 // A Favorite may not have been created yet if it was added later (e.g. | 505 // A Favorite may not have been created yet if it was added later (e.g. |
490 // through ConfigureService) since ServiceCompleteList updates are not | 506 // through ConfigureService) since ServiceCompleteList updates are not |
491 // emitted. Add and update the state here. | 507 // emitted. Add and update the state here. |
492 managed = new FavoriteState(path); | 508 managed = ManagedState::Create(type, path); |
493 managed_list->push_back(managed); | 509 managed_list->push_back(managed); |
494 } | 510 } |
495 managed->set_update_received(); | 511 managed->set_update_received(); |
496 | 512 |
497 std::string desc = GetManagedStateLogType(managed) + " Properties Received"; | 513 std::string desc = GetManagedStateLogType(managed) + " Properties Received"; |
498 NET_LOG_DEBUG(desc, GetManagedStateLogName(managed)); | 514 NET_LOG_DEBUG(desc, GetManagedStateLogName(managed)); |
499 | 515 |
500 if (type == ManagedState::MANAGED_TYPE_NETWORK) { | 516 if (type == ManagedState::MANAGED_TYPE_NETWORK) { |
501 UpdateNetworkStateProperties(managed->AsNetworkState(), properties); | 517 UpdateNetworkStateProperties(managed->AsNetworkState(), properties); |
502 } else { | 518 } else { |
503 // Device, Favorite | 519 // Device, Favorite |
504 for (base::DictionaryValue::Iterator iter(properties); | 520 for (base::DictionaryValue::Iterator iter(properties); |
505 !iter.IsAtEnd(); iter.Advance()) { | 521 !iter.IsAtEnd(); iter.Advance()) { |
506 managed->PropertyChanged(iter.key(), iter.value()); | 522 managed->PropertyChanged(iter.key(), iter.value()); |
507 } | 523 } |
508 managed->InitialPropertiesReceived(properties); | 524 managed->InitialPropertiesReceived(properties); |
509 } | 525 } |
526 UpdateGuid(managed); | |
510 managed->set_update_requested(false); | 527 managed->set_update_requested(false); |
511 } | 528 } |
512 | 529 |
513 void NetworkStateHandler::UpdateNetworkStateProperties( | 530 void NetworkStateHandler::UpdateNetworkStateProperties( |
514 NetworkState* network, | 531 NetworkState* network, |
515 const base::DictionaryValue& properties) { | 532 const base::DictionaryValue& properties) { |
516 DCHECK(network); | 533 DCHECK(network); |
517 bool network_property_updated = false; | 534 bool network_property_updated = false; |
518 std::string prev_connection_state = network->connection_state(); | 535 std::string prev_connection_state = network->connection_state(); |
519 for (base::DictionaryValue::Iterator iter(properties); | 536 for (base::DictionaryValue::Iterator iter(properties); |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
674 NET_LOG_DEBUG("FavoriteListChanged", | 691 NET_LOG_DEBUG("FavoriteListChanged", |
675 base::StringPrintf("Size:%" PRIuS, favorite_list_.size())); | 692 base::StringPrintf("Size:%" PRIuS, favorite_list_.size())); |
676 // The FavoriteState list only changes when the NetworkState list changes, | 693 // The FavoriteState list only changes when the NetworkState list changes, |
677 // so no need to signal observers here again. | 694 // so no need to signal observers here again. |
678 | 695 |
679 // Update UMA stats. | 696 // Update UMA stats. |
680 size_t shared = 0, unshared = 0; | 697 size_t shared = 0, unshared = 0; |
681 for (ManagedStateList::iterator iter = favorite_list_.begin(); | 698 for (ManagedStateList::iterator iter = favorite_list_.begin(); |
682 iter != favorite_list_.end(); ++iter) { | 699 iter != favorite_list_.end(); ++iter) { |
683 FavoriteState* favorite = (*iter)->AsFavoriteState(); | 700 FavoriteState* favorite = (*iter)->AsFavoriteState(); |
684 if (!favorite->IsFavorite()) | 701 if (!favorite->IsInProfile()) |
685 continue; | 702 continue; |
686 if (favorite->IsPrivate()) | 703 if (favorite->IsPrivate()) |
687 ++unshared; | 704 ++unshared; |
688 else | 705 else |
689 ++shared; | 706 ++shared; |
690 } | 707 } |
691 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedShared", shared); | 708 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedShared", shared); |
692 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedUnshared", unshared); | 709 UMA_HISTOGRAM_COUNTS_100("Networks.RememberedUnshared", unshared); |
693 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { | 710 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { |
694 NotifyDeviceListChanged(); | 711 NotifyDeviceListChanged(); |
(...skipping 26 matching lines...) Expand all Loading... | |
721 NET_LOG_ERROR( | 738 NET_LOG_ERROR( |
722 "DefaultNetwork is not connected: " + network->connection_state(), | 739 "DefaultNetwork is not connected: " + network->connection_state(), |
723 network->path()); | 740 network->path()); |
724 } | 741 } |
725 NotifyDefaultNetworkChanged(network); | 742 NotifyDefaultNetworkChanged(network); |
726 } | 743 } |
727 | 744 |
728 //------------------------------------------------------------------------------ | 745 //------------------------------------------------------------------------------ |
729 // Private methods | 746 // Private methods |
730 | 747 |
748 void NetworkStateHandler::UpdateGuid(ManagedState* managed) { | |
749 if (managed->managed_type() == ManagedState::MANAGED_TYPE_FAVORITE) { | |
750 FavoriteState* favorite = managed->AsFavoriteState(); | |
751 std::string specifier = favorite->GetSpecifier(); | |
752 if (!favorite->guid().empty()) { | |
753 // If the favorite is saved in a profile, remove the entry from the map. | |
754 // Otherwise ensure that the entry matches the specified GUID. | |
755 if (favorite->IsInProfile()) | |
756 specifier_guid_map_.erase(specifier); | |
757 else | |
758 specifier_guid_map_[specifier] = favorite->guid(); | |
759 return; | |
760 } | |
761 // Ensure that the FavoriteState has a valid GUID. | |
762 std::string guid; | |
763 SpecifierGuidMap::iterator iter = specifier_guid_map_.find(specifier); | |
764 if (iter != specifier_guid_map_.end()) { | |
765 guid = iter->second; | |
766 } else { | |
767 guid = base::GenerateGUID(); | |
768 specifier_guid_map_[specifier] = guid; | |
769 } | |
770 favorite->SetGuid(guid); | |
771 NetworkState* network = GetModifiableNetworkState(favorite->path()); | |
772 if (network) | |
773 network->SetGuid(guid); | |
774 } else if (managed->managed_type() == ManagedState::MANAGED_TYPE_NETWORK) { | |
775 // If the GUID is not set and a corresponding FavoriteState exists, get the | |
776 // GUID from the FavoriteState. Otherwise it will get set when the Favorite | |
777 // is created. | |
778 NetworkState* network = managed->AsNetworkState(); | |
779 if (!network->guid().empty()) | |
780 return; | |
781 FavoriteState* favorite = GetModifiableFavoriteState(network->path()); | |
pneubeck (no reviews)
2014/05/13 08:43:54
at least add a log message here if !favorite
stevenjb
2014/05/13 18:25:07
Hmm. I don't want to create log spam here, but in
pneubeck (no reviews)
2014/05/14 08:46:07
DCHECKs shouldn't be handled (next line with if(fa
| |
782 if (favorite && !favorite->guid().empty()) | |
783 network->SetGuid(favorite->guid()); | |
784 } | |
785 } | |
786 | |
731 void NetworkStateHandler::NotifyDeviceListChanged() { | 787 void NetworkStateHandler::NotifyDeviceListChanged() { |
732 NET_LOG_DEBUG("NotifyDeviceListChanged", | 788 NET_LOG_DEBUG("NotifyDeviceListChanged", |
733 base::StringPrintf("Size:%" PRIuS, device_list_.size())); | 789 base::StringPrintf("Size:%" PRIuS, device_list_.size())); |
734 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, | 790 FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_, |
735 DeviceListChanged()); | 791 DeviceListChanged()); |
736 } | 792 } |
737 | 793 |
738 DeviceState* NetworkStateHandler::GetModifiableDeviceState( | 794 DeviceState* NetworkStateHandler::GetModifiableDeviceState( |
739 const std::string& device_path) const { | 795 const std::string& device_path) const { |
740 ManagedState* managed = GetModifiableManagedState(&device_list_, device_path); | 796 ManagedState* managed = GetModifiableManagedState(&device_list_, device_path); |
741 if (!managed) | 797 if (!managed) |
742 return NULL; | 798 return NULL; |
743 return managed->AsDeviceState(); | 799 return managed->AsDeviceState(); |
744 } | 800 } |
745 | 801 |
746 NetworkState* NetworkStateHandler::GetModifiableNetworkState( | 802 NetworkState* NetworkStateHandler::GetModifiableNetworkState( |
747 const std::string& service_path) const { | 803 const std::string& service_path) const { |
748 ManagedState* managed = | 804 ManagedState* managed = |
749 GetModifiableManagedState(&network_list_, service_path); | 805 GetModifiableManagedState(&network_list_, service_path); |
750 if (!managed) | 806 if (!managed) |
751 return NULL; | 807 return NULL; |
752 return managed->AsNetworkState(); | 808 return managed->AsNetworkState(); |
753 } | 809 } |
754 | 810 |
811 FavoriteState* NetworkStateHandler::GetModifiableFavoriteState( | |
812 const std::string& service_path) const { | |
813 ManagedState* managed = | |
814 GetModifiableManagedState(&favorite_list_, service_path); | |
815 if (!managed) | |
816 return NULL; | |
817 return managed->AsFavoriteState(); | |
818 } | |
819 | |
755 ManagedState* NetworkStateHandler::GetModifiableManagedState( | 820 ManagedState* NetworkStateHandler::GetModifiableManagedState( |
756 const ManagedStateList* managed_list, | 821 const ManagedStateList* managed_list, |
757 const std::string& path) const { | 822 const std::string& path) const { |
758 for (ManagedStateList::const_iterator iter = managed_list->begin(); | 823 for (ManagedStateList::const_iterator iter = managed_list->begin(); |
759 iter != managed_list->end(); ++iter) { | 824 iter != managed_list->end(); ++iter) { |
760 ManagedState* managed = *iter; | 825 ManagedState* managed = *iter; |
761 if (managed->path() == path) | 826 if (managed->path() == path) |
762 return managed; | 827 return managed; |
763 } | 828 } |
764 return NULL; | 829 return NULL; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
862 if (type.MatchesType(shill::kTypeBluetooth)) | 927 if (type.MatchesType(shill::kTypeBluetooth)) |
863 technologies.push_back(new std::string(shill::kTypeBluetooth)); | 928 technologies.push_back(new std::string(shill::kTypeBluetooth)); |
864 if (type.MatchesType(shill::kTypeVPN)) | 929 if (type.MatchesType(shill::kTypeVPN)) |
865 technologies.push_back(new std::string(shill::kTypeVPN)); | 930 technologies.push_back(new std::string(shill::kTypeVPN)); |
866 | 931 |
867 CHECK_GT(technologies.size(), 0ul); | 932 CHECK_GT(technologies.size(), 0ul); |
868 return technologies.Pass(); | 933 return technologies.Pass(); |
869 } | 934 } |
870 | 935 |
871 } // namespace chromeos | 936 } // namespace chromeos |
OLD | NEW |