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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 | 421 |
422 // Sort the network list if necessary. | 422 // Sort the network list if necessary. |
423 if (!network_list_sorted_) | 423 if (!network_list_sorted_) |
424 SortNetworkList(); | 424 SortNetworkList(); |
425 | 425 |
426 if (type.MatchesPattern(NetworkTypePattern::Tether())) | 426 if (type.MatchesPattern(NetworkTypePattern::Tether())) |
427 GetTetherNetworkList(limit, list); | 427 GetTetherNetworkList(limit, list); |
428 | 428 |
429 int count = list->size(); | 429 int count = list->size(); |
430 | 430 |
431 if (type.Equals(NetworkTypePattern::Tether()) || | 431 // Only add more networks if the limit has not yet been reached. |
432 (limit != 0 && count >= limit)) { | 432 if (limit == 0 || count < limit) { |
stevenjb
2017/06/21 20:59:04
Instead of indenting this, modify the test in line
Kyle Horimoto
2017/06/21 22:17:07
Done.
| |
433 // If only searching for Tether networks, there is no need to continue | 433 for (auto iter = network_list_.begin(); iter != network_list_.end(); |
434 // searching through other network types; likewise, if the limit has already | 434 ++iter) { |
435 // been reached, there is no need to continue searching. | 435 const NetworkState* network = (*iter)->AsNetworkState(); |
436 return; | 436 DCHECK(network); |
437 if (!network->update_received() || !network->Matches(type)) | |
438 continue; | |
439 if (configured_only && !network->IsInProfile()) | |
440 continue; | |
441 if (visible_only && !network->visible()) | |
442 continue; | |
443 if (network->type() == shill::kTypeWifi && | |
444 !network->tether_guid().empty()) { | |
445 // Wi-Fi networks which are actually underlying Wi-Fi hotspots for a | |
446 // Tether network should not be returned since they should only be shown | |
447 // to the user as Tether networks. | |
448 continue; | |
449 } | |
450 if (network->type() == shill::kTypeEthernet) { | |
451 // Ethernet networks should always be in front. | |
452 list->insert(list->begin(), network); | |
453 } else { | |
454 list->push_back(network); | |
455 } | |
456 if (limit > 0 && ++count >= limit) | |
457 break; | |
458 } | |
437 } | 459 } |
438 | 460 |
439 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) { | 461 if (type.MatchesPattern(NetworkTypePattern::Tether())) { |
440 const NetworkState* network = (*iter)->AsNetworkState(); | 462 // Tether networks were added to |list| above before non-Tether networks. |
441 DCHECK(network); | 463 // Thus, if |type| includes Tether networks, it is possible that |list| |
442 if (!network->update_received() || !network->Matches(type)) | 464 // contains some non-active Tether networks before some active (i.e., |
443 continue; | 465 // connecting or connected) non-Tether networks. Active networks should |
444 if (configured_only && !network->IsInProfile()) | 466 // always be listed first, so adjust the list accordingly. |
445 continue; | 467 size_t last_active_network_index = -1; |
446 if (visible_only && !network->visible()) | 468 for (size_t i = 0; i < list->size(); ++i) { |
447 continue; | 469 const NetworkState* network = list->at(i); |
448 if (network->type() == shill::kTypeEthernet) { | 470 if (network->IsConnectingState() || network->IsConnectedState()) { |
449 // Ethernet networks should always be in front. | 471 // A new active network has been found in the list. |
450 list->insert(list->begin(), network); | 472 last_active_network_index++; |
451 } else { | 473 |
452 list->push_back(network); | 474 if (i != last_active_network_index) { |
475 // If this is an active network that is not in the correct order | |
476 // (i.e., it is listed after non-active networks), move it to the | |
477 // correct spot. | |
478 list->erase(list->begin() + i); | |
479 list->insert(list->begin() + last_active_network_index, network); | |
480 } | |
481 } | |
453 } | 482 } |
stevenjb
2017/06/21 20:59:04
Can we move this logic to a separate helper?
Kyle Horimoto
2017/06/21 22:17:07
Done.
| |
454 if (limit > 0 && ++count >= limit) | |
455 break; | |
456 } | 483 } |
457 } | 484 } |
458 | 485 |
459 void NetworkStateHandler::GetTetherNetworkList(int limit, | 486 void NetworkStateHandler::GetTetherNetworkList(int limit, |
460 NetworkStateList* list) { | 487 NetworkStateList* list) { |
461 DCHECK(list); | 488 DCHECK(list); |
462 list->clear(); | 489 list->clear(); |
463 | 490 |
464 if (!IsTechnologyEnabled(NetworkTypePattern::Tether())) | 491 if (!IsTechnologyEnabled(NetworkTypePattern::Tether())) |
465 return; | 492 return; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 tether_network_state->set_visible(true); | 558 tether_network_state->set_visible(true); |
532 tether_network_state->set_update_received(); | 559 tether_network_state->set_update_received(); |
533 tether_network_state->set_update_requested(false); | 560 tether_network_state->set_update_requested(false); |
534 tether_network_state->set_connectable(true); | 561 tether_network_state->set_connectable(true); |
535 tether_network_state->set_carrier(carrier); | 562 tether_network_state->set_carrier(carrier); |
536 tether_network_state->set_battery_percentage(battery_percentage); | 563 tether_network_state->set_battery_percentage(battery_percentage); |
537 tether_network_state->set_tether_has_connected_to_host(has_connected_to_host); | 564 tether_network_state->set_tether_has_connected_to_host(has_connected_to_host); |
538 tether_network_state->set_signal_strength(signal_strength); | 565 tether_network_state->set_signal_strength(signal_strength); |
539 | 566 |
540 tether_network_list_.push_back(std::move(tether_network_state)); | 567 tether_network_list_.push_back(std::move(tether_network_state)); |
568 network_list_sorted_ = false; | |
569 | |
541 NotifyNetworkListChanged(); | 570 NotifyNetworkListChanged(); |
542 } | 571 } |
543 | 572 |
544 bool NetworkStateHandler::UpdateTetherNetworkProperties( | 573 bool NetworkStateHandler::UpdateTetherNetworkProperties( |
545 const std::string& guid, | 574 const std::string& guid, |
546 const std::string& carrier, | 575 const std::string& carrier, |
547 int battery_percentage, | 576 int battery_percentage, |
548 int signal_strength) { | 577 int signal_strength) { |
549 if (tether_technology_state_ != TECHNOLOGY_ENABLED) { | 578 if (tether_technology_state_ != TECHNOLOGY_ENABLED) { |
550 NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether " | 579 NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether " |
551 << "networks are not enabled. Cannot update."; | 580 << "networks are not enabled. Cannot update."; |
552 return false; | 581 return false; |
553 } | 582 } |
554 | 583 |
555 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); | 584 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); |
556 if (!tether_network_state) { | 585 if (!tether_network_state) { |
557 NET_LOG(ERROR) << "UpdateTetherNetworkProperties(): No NetworkState for " | 586 NET_LOG(ERROR) << "UpdateTetherNetworkProperties(): No NetworkState for " |
558 << "Tether network with GUID \"" << guid << "\"."; | 587 << "Tether network with GUID \"" << guid << "\"."; |
559 return false; | 588 return false; |
560 } | 589 } |
561 | 590 |
562 tether_network_state->set_carrier(carrier); | 591 tether_network_state->set_carrier(carrier); |
563 tether_network_state->set_battery_percentage(battery_percentage); | 592 tether_network_state->set_battery_percentage(battery_percentage); |
564 tether_network_state->set_signal_strength(signal_strength); | 593 tether_network_state->set_signal_strength(signal_strength); |
594 network_list_sorted_ = false; | |
565 | 595 |
566 NotifyNetworkPropertiesUpdated(tether_network_state); | 596 NotifyNetworkPropertiesUpdated(tether_network_state); |
567 return true; | 597 return true; |
568 } | 598 } |
569 | 599 |
570 bool NetworkStateHandler::SetTetherNetworkHasConnectedToHost( | 600 bool NetworkStateHandler::SetTetherNetworkHasConnectedToHost( |
571 const std::string& guid) { | 601 const std::string& guid) { |
572 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); | 602 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); |
573 if (!tether_network_state) { | 603 if (!tether_network_state) { |
574 NET_LOG(ERROR) << "SetTetherNetworkHasConnectedToHost(): No NetworkState " | 604 NET_LOG(ERROR) << "SetTetherNetworkHasConnectedToHost(): No NetworkState " |
575 << "for Tether network with GUID \"" << guid << "\"."; | 605 << "for Tether network with GUID \"" << guid << "\"."; |
576 return false; | 606 return false; |
577 } | 607 } |
578 | 608 |
579 if (tether_network_state->tether_has_connected_to_host()) { | 609 if (tether_network_state->tether_has_connected_to_host()) { |
580 return false; | 610 return false; |
581 } | 611 } |
582 | 612 |
583 tether_network_state->set_tether_has_connected_to_host(true); | 613 tether_network_state->set_tether_has_connected_to_host(true); |
614 network_list_sorted_ = false; | |
584 | 615 |
585 NotifyNetworkPropertiesUpdated(tether_network_state); | 616 NotifyNetworkPropertiesUpdated(tether_network_state); |
586 return true; | 617 return true; |
587 } | 618 } |
588 | 619 |
589 bool NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) { | 620 bool NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) { |
590 for (auto iter = tether_network_list_.begin(); | 621 for (auto iter = tether_network_list_.begin(); |
591 iter != tether_network_list_.end(); ++iter) { | 622 iter != tether_network_list_.end(); ++iter) { |
592 if (iter->get()->AsNetworkState()->guid() == guid) { | 623 if (iter->get()->AsNetworkState()->guid() == guid) { |
593 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid( | 624 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
627 return false; | 658 return false; |
628 } | 659 } |
629 | 660 |
630 if (wifi_network_state->tether_guid().empty() && | 661 if (wifi_network_state->tether_guid().empty() && |
631 tether_network_state->tether_guid().empty()) { | 662 tether_network_state->tether_guid().empty()) { |
632 return true; | 663 return true; |
633 } | 664 } |
634 | 665 |
635 wifi_network_state->set_tether_guid(std::string()); | 666 wifi_network_state->set_tether_guid(std::string()); |
636 tether_network_state->set_tether_guid(std::string()); | 667 tether_network_state->set_tether_guid(std::string()); |
668 network_list_sorted_ = false; | |
637 | 669 |
638 NotifyNetworkPropertiesUpdated(wifi_network_state); | 670 NotifyNetworkPropertiesUpdated(wifi_network_state); |
639 NotifyNetworkPropertiesUpdated(tether_network_state); | 671 NotifyNetworkPropertiesUpdated(tether_network_state); |
640 | 672 |
641 return true; | 673 return true; |
642 } | 674 } |
643 | 675 |
644 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork( | 676 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork( |
645 const std::string& tether_network_guid, | 677 const std::string& tether_network_guid, |
646 const std::string& wifi_network_guid) { | 678 const std::string& wifi_network_guid) { |
(...skipping 27 matching lines...) Expand all Loading... | |
674 return false; | 706 return false; |
675 } | 707 } |
676 | 708 |
677 if (wifi_network_state->tether_guid() == tether_network_guid && | 709 if (wifi_network_state->tether_guid() == tether_network_guid && |
678 tether_network_state->tether_guid() == wifi_network_guid) { | 710 tether_network_state->tether_guid() == wifi_network_guid) { |
679 return true; | 711 return true; |
680 } | 712 } |
681 | 713 |
682 tether_network_state->set_tether_guid(wifi_network_guid); | 714 tether_network_state->set_tether_guid(wifi_network_guid); |
683 wifi_network_state->set_tether_guid(tether_network_guid); | 715 wifi_network_state->set_tether_guid(tether_network_guid); |
716 network_list_sorted_ = false; | |
684 | 717 |
685 NotifyNetworkPropertiesUpdated(wifi_network_state); | 718 NotifyNetworkPropertiesUpdated(wifi_network_state); |
686 NotifyNetworkPropertiesUpdated(tether_network_state); | 719 NotifyNetworkPropertiesUpdated(tether_network_state); |
687 | 720 |
688 return true; | 721 return true; |
689 } | 722 } |
690 | 723 |
691 void NetworkStateHandler::SetTetherNetworkStateDisconnected( | 724 void NetworkStateHandler::SetTetherNetworkStateDisconnected( |
692 const std::string& guid) { | 725 const std::string& guid) { |
693 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); | 726 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
732 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " | 765 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " |
733 << "not found: " << guid; | 766 << "not found: " << guid; |
734 return; | 767 return; |
735 } | 768 } |
736 | 769 |
737 DCHECK( | 770 DCHECK( |
738 NetworkTypePattern::Tether().MatchesType(tether_network_state->type())); | 771 NetworkTypePattern::Tether().MatchesType(tether_network_state->type())); |
739 | 772 |
740 std::string prev_connection_state = tether_network_state->connection_state(); | 773 std::string prev_connection_state = tether_network_state->connection_state(); |
741 tether_network_state->set_connection_state(connection_state); | 774 tether_network_state->set_connection_state(connection_state); |
775 network_list_sorted_ = false; | |
776 | |
742 DCHECK(!tether_network_state->is_captive_portal()); | 777 DCHECK(!tether_network_state->is_captive_portal()); |
743 | |
744 if (ConnectionStateChanged(tether_network_state, prev_connection_state, | 778 if (ConnectionStateChanged(tether_network_state, prev_connection_state, |
745 false /* prev_is_captive_portal */)) { | 779 false /* prev_is_captive_portal */)) { |
746 NET_LOG(EVENT) << "Changing connection state for Tether network with GUID " | 780 NET_LOG(EVENT) << "Changing connection state for Tether network with GUID " |
747 << guid << ". Old state: " << prev_connection_state << ", " | 781 << guid << ". Old state: " << prev_connection_state << ", " |
748 << "New state: " << connection_state; | 782 << "New state: " << connection_state; |
749 | 783 |
750 OnNetworkConnectionStateChanged(tether_network_state); | 784 OnNetworkConnectionStateChanged(tether_network_state); |
751 NotifyNetworkPropertiesUpdated(tether_network_state); | 785 NotifyNetworkPropertiesUpdated(tether_network_state); |
752 } | 786 } |
753 } | 787 } |
754 | 788 |
789 void NetworkStateHandler::SetTetherNetworkListSorter( | |
790 const TetherNetworkListSorter* tether_network_list_sorter) { | |
791 if (tether_technology_state_ != TECHNOLOGY_ENABLED) { | |
792 NET_LOG(ERROR) << "SetTetherNetworkListSorter() called when Tether " | |
793 << "networks are not enabled."; | |
794 return; | |
795 } | |
796 | |
797 tether_network_list_sorter_ = tether_network_list_sorter; | |
798 } | |
799 | |
755 void NetworkStateHandler::EnsureTetherDeviceState() { | 800 void NetworkStateHandler::EnsureTetherDeviceState() { |
756 bool should_be_present = | 801 bool should_be_present = |
757 tether_technology_state_ != TechnologyState::TECHNOLOGY_UNAVAILABLE; | 802 tether_technology_state_ != TechnologyState::TECHNOLOGY_UNAVAILABLE; |
758 | 803 |
759 for (auto it = device_list_.begin(); it < device_list_.end(); ++it) { | 804 for (auto it = device_list_.begin(); it < device_list_.end(); ++it) { |
760 std::string path = (*it)->path(); | 805 std::string path = (*it)->path(); |
761 if (path == kTetherDevicePath) { | 806 if (path == kTetherDevicePath) { |
762 // If the Tether DeviceState is in the list and it should not be, remove | 807 // If the Tether DeviceState is in the list and it should not be, remove |
763 // it and return. If it is in the list and it should be, the list is | 808 // it and return. If it is in the list and it should be, the list is |
764 // already valid, so return without removing it. | 809 // already valid, so return without removing it. |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1200 devices += ", "; | 1245 devices += ", "; |
1201 devices += (*iter)->name(); | 1246 devices += (*iter)->name(); |
1202 } | 1247 } |
1203 NET_LOG_EVENT("DeviceList", devices); | 1248 NET_LOG_EVENT("DeviceList", devices); |
1204 NotifyDeviceListChanged(); | 1249 NotifyDeviceListChanged(); |
1205 } else { | 1250 } else { |
1206 NOTREACHED(); | 1251 NOTREACHED(); |
1207 } | 1252 } |
1208 } | 1253 } |
1209 | 1254 |
1210 // TODO(khorimoto): Add sorting for the Tether network list as well. | |
1211 void NetworkStateHandler::SortNetworkList() { | 1255 void NetworkStateHandler::SortNetworkList() { |
1256 if (tether_network_list_sorter_) | |
1257 tether_network_list_sorter_->SortTetherNetworkList(&tether_network_list_); | |
1258 | |
1212 // Note: usually active networks will precede inactive networks, however | 1259 // Note: usually active networks will precede inactive networks, however |
1213 // this may briefly be untrue during state transitions (e.g. a network may | 1260 // this may briefly be untrue during state transitions (e.g. a network may |
1214 // transition to idle before the list is updated). | 1261 // transition to idle before the list is updated). |
1215 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; | 1262 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; |
1216 for (ManagedStateList::iterator iter = network_list_.begin(); | 1263 for (ManagedStateList::iterator iter = network_list_.begin(); |
1217 iter != network_list_.end(); ++iter) { | 1264 iter != network_list_.end(); ++iter) { |
1218 NetworkState* network = (*iter)->AsNetworkState(); | 1265 NetworkState* network = (*iter)->AsNetworkState(); |
1219 if (!network->update_received()) { | 1266 if (!network->update_received()) { |
1220 new_networks.push_back(std::move(*iter)); | 1267 new_networks.push_back(std::move(*iter)); |
1221 continue; | 1268 continue; |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1542 if (type.MatchesType(shill::kTypeVPN)) | 1589 if (type.MatchesType(shill::kTypeVPN)) |
1543 technologies.emplace_back(shill::kTypeVPN); | 1590 technologies.emplace_back(shill::kTypeVPN); |
1544 if (type.MatchesType(kTypeTether)) | 1591 if (type.MatchesType(kTypeTether)) |
1545 technologies.emplace_back(kTypeTether); | 1592 technologies.emplace_back(kTypeTether); |
1546 | 1593 |
1547 CHECK_GT(technologies.size(), 0ul); | 1594 CHECK_GT(technologies.size(), 0ul); |
1548 return technologies; | 1595 return technologies; |
1549 } | 1596 } |
1550 | 1597 |
1551 } // namespace chromeos | 1598 } // namespace chromeos |
OLD | NEW |