Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: chromeos/network/network_state_handler.cc

Issue 2945643002: [CrOS Tether] Sort Tether network lists. (Closed)
Patch Set: Cleanup - now ready for review. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 520 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 tether_network_state->set_visible(true); 531 tether_network_state->set_visible(true);
532 tether_network_state->set_update_received(); 532 tether_network_state->set_update_received();
533 tether_network_state->set_update_requested(false); 533 tether_network_state->set_update_requested(false);
534 tether_network_state->set_connectable(true); 534 tether_network_state->set_connectable(true);
535 tether_network_state->set_carrier(carrier); 535 tether_network_state->set_carrier(carrier);
536 tether_network_state->set_battery_percentage(battery_percentage); 536 tether_network_state->set_battery_percentage(battery_percentage);
537 tether_network_state->set_tether_has_connected_to_host(has_connected_to_host); 537 tether_network_state->set_tether_has_connected_to_host(has_connected_to_host);
538 tether_network_state->set_signal_strength(signal_strength); 538 tether_network_state->set_signal_strength(signal_strength);
539 539
540 tether_network_list_.push_back(std::move(tether_network_state)); 540 tether_network_list_.push_back(std::move(tether_network_state));
541 network_list_sorted_ = false;
542
541 NotifyNetworkListChanged(); 543 NotifyNetworkListChanged();
542 } 544 }
543 545
544 bool NetworkStateHandler::UpdateTetherNetworkProperties( 546 bool NetworkStateHandler::UpdateTetherNetworkProperties(
545 const std::string& guid, 547 const std::string& guid,
546 const std::string& carrier, 548 const std::string& carrier,
547 int battery_percentage, 549 int battery_percentage,
548 int signal_strength) { 550 int signal_strength) {
549 if (tether_technology_state_ != TECHNOLOGY_ENABLED) { 551 if (tether_technology_state_ != TECHNOLOGY_ENABLED) {
550 NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether " 552 NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether "
551 << "networks are not enabled. Cannot update."; 553 << "networks are not enabled. Cannot update.";
552 return false; 554 return false;
553 } 555 }
554 556
555 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); 557 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid);
556 if (!tether_network_state) { 558 if (!tether_network_state) {
557 NET_LOG(ERROR) << "UpdateTetherNetworkProperties(): No NetworkState for " 559 NET_LOG(ERROR) << "UpdateTetherNetworkProperties(): No NetworkState for "
558 << "Tether network with GUID \"" << guid << "\"."; 560 << "Tether network with GUID \"" << guid << "\".";
559 return false; 561 return false;
560 } 562 }
561 563
562 tether_network_state->set_carrier(carrier); 564 tether_network_state->set_carrier(carrier);
563 tether_network_state->set_battery_percentage(battery_percentage); 565 tether_network_state->set_battery_percentage(battery_percentage);
564 tether_network_state->set_signal_strength(signal_strength); 566 tether_network_state->set_signal_strength(signal_strength);
567 network_list_sorted_ = false;
Ryan Hansberry 2017/06/19 19:17:48 I thought sorting was only determined by most rece
Kyle Horimoto 2017/06/20 23:44:54 The algorithm used to sort is an implementation de
565 568
566 NotifyNetworkPropertiesUpdated(tether_network_state); 569 NotifyNetworkPropertiesUpdated(tether_network_state);
567 return true; 570 return true;
568 } 571 }
569 572
570 bool NetworkStateHandler::SetTetherNetworkHasConnectedToHost( 573 bool NetworkStateHandler::SetTetherNetworkHasConnectedToHost(
571 const std::string& guid) { 574 const std::string& guid) {
572 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); 575 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid);
573 if (!tether_network_state) { 576 if (!tether_network_state) {
574 NET_LOG(ERROR) << "SetTetherNetworkHasConnectedToHost(): No NetworkState " 577 NET_LOG(ERROR) << "SetTetherNetworkHasConnectedToHost(): No NetworkState "
575 << "for Tether network with GUID \"" << guid << "\"."; 578 << "for Tether network with GUID \"" << guid << "\".";
576 return false; 579 return false;
577 } 580 }
578 581
579 if (tether_network_state->tether_has_connected_to_host()) { 582 if (tether_network_state->tether_has_connected_to_host()) {
580 return false; 583 return false;
581 } 584 }
582 585
583 tether_network_state->set_tether_has_connected_to_host(true); 586 tether_network_state->set_tether_has_connected_to_host(true);
587 network_list_sorted_ = false;
584 588
585 NotifyNetworkPropertiesUpdated(tether_network_state); 589 NotifyNetworkPropertiesUpdated(tether_network_state);
586 return true; 590 return true;
587 } 591 }
588 592
589 bool NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) { 593 bool NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) {
590 for (auto iter = tether_network_list_.begin(); 594 for (auto iter = tether_network_list_.begin();
591 iter != tether_network_list_.end(); ++iter) { 595 iter != tether_network_list_.end(); ++iter) {
592 if (iter->get()->AsNetworkState()->guid() == guid) { 596 if (iter->get()->AsNetworkState()->guid() == guid) {
593 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid( 597 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid(
594 iter->get()->AsNetworkState()->tether_guid()); 598 iter->get()->AsNetworkState()->tether_guid());
595 if (wifi_network) 599 if (wifi_network)
596 wifi_network->set_tether_guid(std::string()); 600 wifi_network->set_tether_guid(std::string());
597 601
598 tether_network_list_.erase(iter); 602 tether_network_list_.erase(iter);
603 network_list_sorted_ = false;
Ryan Hansberry 2017/06/19 19:17:49 Is this true? Once an element is removed, I would
Kyle Horimoto 2017/06/20 23:44:54 You're right - removed.
604
599 NotifyNetworkListChanged(); 605 NotifyNetworkListChanged();
600 606
601 return true; 607 return true;
602 } 608 }
603 } 609 }
604 return false; 610 return false;
605 } 611 }
606 612
607 bool NetworkStateHandler::DisassociateTetherNetworkStateFromWifiNetwork( 613 bool NetworkStateHandler::DisassociateTetherNetworkStateFromWifiNetwork(
608 const std::string& tether_network_guid) { 614 const std::string& tether_network_guid) {
(...skipping 18 matching lines...) Expand all
627 return false; 633 return false;
628 } 634 }
629 635
630 if (wifi_network_state->tether_guid().empty() && 636 if (wifi_network_state->tether_guid().empty() &&
631 tether_network_state->tether_guid().empty()) { 637 tether_network_state->tether_guid().empty()) {
632 return true; 638 return true;
633 } 639 }
634 640
635 wifi_network_state->set_tether_guid(std::string()); 641 wifi_network_state->set_tether_guid(std::string());
636 tether_network_state->set_tether_guid(std::string()); 642 tether_network_state->set_tether_guid(std::string());
643 network_list_sorted_ = false;
637 644
638 NotifyNetworkPropertiesUpdated(wifi_network_state); 645 NotifyNetworkPropertiesUpdated(wifi_network_state);
639 NotifyNetworkPropertiesUpdated(tether_network_state); 646 NotifyNetworkPropertiesUpdated(tether_network_state);
640 647
641 return true; 648 return true;
642 } 649 }
643 650
644 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork( 651 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork(
645 const std::string& tether_network_guid, 652 const std::string& tether_network_guid,
646 const std::string& wifi_network_guid) { 653 const std::string& wifi_network_guid) {
(...skipping 27 matching lines...) Expand all
674 return false; 681 return false;
675 } 682 }
676 683
677 if (wifi_network_state->tether_guid() == tether_network_guid && 684 if (wifi_network_state->tether_guid() == tether_network_guid &&
678 tether_network_state->tether_guid() == wifi_network_guid) { 685 tether_network_state->tether_guid() == wifi_network_guid) {
679 return true; 686 return true;
680 } 687 }
681 688
682 tether_network_state->set_tether_guid(wifi_network_guid); 689 tether_network_state->set_tether_guid(wifi_network_guid);
683 wifi_network_state->set_tether_guid(tether_network_guid); 690 wifi_network_state->set_tether_guid(tether_network_guid);
691 network_list_sorted_ = false;
684 692
685 NotifyNetworkPropertiesUpdated(wifi_network_state); 693 NotifyNetworkPropertiesUpdated(wifi_network_state);
686 NotifyNetworkPropertiesUpdated(tether_network_state); 694 NotifyNetworkPropertiesUpdated(tether_network_state);
687 695
688 return true; 696 return true;
689 } 697 }
690 698
691 void NetworkStateHandler::SetTetherNetworkStateDisconnected( 699 void NetworkStateHandler::SetTetherNetworkStateDisconnected(
692 const std::string& guid) { 700 const std::string& guid) {
693 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); 701 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " 740 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network "
733 << "not found: " << guid; 741 << "not found: " << guid;
734 return; 742 return;
735 } 743 }
736 744
737 DCHECK( 745 DCHECK(
738 NetworkTypePattern::Tether().MatchesType(tether_network_state->type())); 746 NetworkTypePattern::Tether().MatchesType(tether_network_state->type()));
739 747
740 std::string prev_connection_state = tether_network_state->connection_state(); 748 std::string prev_connection_state = tether_network_state->connection_state();
741 tether_network_state->set_connection_state(connection_state); 749 tether_network_state->set_connection_state(connection_state);
750 network_list_sorted_ = false;
Ryan Hansberry 2017/06/19 19:17:48 Again, I'm not sure how this relates to how HostSc
Kyle Horimoto 2017/06/20 23:44:54 Connecting/connected networks should be prioritize
751
742 DCHECK(!tether_network_state->is_captive_portal()); 752 DCHECK(!tether_network_state->is_captive_portal());
743
744 if (ConnectionStateChanged(tether_network_state, prev_connection_state, 753 if (ConnectionStateChanged(tether_network_state, prev_connection_state,
745 false /* prev_is_captive_portal */)) { 754 false /* prev_is_captive_portal */)) {
746 NET_LOG(EVENT) << "Changing connection state for Tether network with GUID " 755 NET_LOG(EVENT) << "Changing connection state for Tether network with GUID "
747 << guid << ". Old state: " << prev_connection_state << ", " 756 << guid << ". Old state: " << prev_connection_state << ", "
748 << "New state: " << connection_state; 757 << "New state: " << connection_state;
749 758
750 OnNetworkConnectionStateChanged(tether_network_state); 759 OnNetworkConnectionStateChanged(tether_network_state);
751 NotifyNetworkPropertiesUpdated(tether_network_state); 760 NotifyNetworkPropertiesUpdated(tether_network_state);
752 } 761 }
753 } 762 }
754 763
764 void NetworkStateHandler::SetTetherNetworkListSorter(
765 const TetherNetworkListSorter* tether_network_list_sorter) {
766 if (tether_technology_state_ != TECHNOLOGY_ENABLED) {
767 NET_LOG(ERROR) << "SetTetherNetworkListSorter() called when Tether "
768 << "networks are not enabled.";
769 return;
770 }
771
772 tether_network_list_sorter_ = tether_network_list_sorter;
773 }
774
755 void NetworkStateHandler::EnsureTetherDeviceState() { 775 void NetworkStateHandler::EnsureTetherDeviceState() {
756 bool should_be_present = 776 bool should_be_present =
757 tether_technology_state_ != TechnologyState::TECHNOLOGY_UNAVAILABLE; 777 tether_technology_state_ != TechnologyState::TECHNOLOGY_UNAVAILABLE;
758 778
759 for (auto it = device_list_.begin(); it < device_list_.end(); ++it) { 779 for (auto it = device_list_.begin(); it < device_list_.end(); ++it) {
760 std::string path = (*it)->path(); 780 std::string path = (*it)->path();
761 if (path == kTetherDevicePath) { 781 if (path == kTetherDevicePath) {
762 // If the Tether DeviceState is in the list and it should not be, remove 782 // 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 783 // it and return. If it is in the list and it should be, the list is
764 // already valid, so return without removing it. 784 // already valid, so return without removing it.
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after
1200 devices += ", "; 1220 devices += ", ";
1201 devices += (*iter)->name(); 1221 devices += (*iter)->name();
1202 } 1222 }
1203 NET_LOG_EVENT("DeviceList", devices); 1223 NET_LOG_EVENT("DeviceList", devices);
1204 NotifyDeviceListChanged(); 1224 NotifyDeviceListChanged();
1205 } else { 1225 } else {
1206 NOTREACHED(); 1226 NOTREACHED();
1207 } 1227 }
1208 } 1228 }
1209 1229
1210 // TODO(khorimoto): Add sorting for the Tether network list as well.
1211 void NetworkStateHandler::SortNetworkList() { 1230 void NetworkStateHandler::SortNetworkList() {
1231 if (tether_network_list_sorter_)
Ryan Hansberry 2017/06/19 19:17:49 Check if network_list_sorted_ is false?
Kyle Horimoto 2017/06/20 23:44:54 That's already done before SortNetworkList() is ca
1232 tether_network_list_sorter_->SortTetherNetworkList(&tether_network_list_);
Ryan Hansberry 2017/06/19 19:17:49 set network_list_sorted_ = true
Kyle Horimoto 2017/06/20 23:44:54 That's already done at the end of this function.
1233
1212 // Note: usually active networks will precede inactive networks, however 1234 // Note: usually active networks will precede inactive networks, however
1213 // this may briefly be untrue during state transitions (e.g. a network may 1235 // this may briefly be untrue during state transitions (e.g. a network may
1214 // transition to idle before the list is updated). 1236 // transition to idle before the list is updated).
1215 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; 1237 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks;
1216 for (ManagedStateList::iterator iter = network_list_.begin(); 1238 for (ManagedStateList::iterator iter = network_list_.begin();
1217 iter != network_list_.end(); ++iter) { 1239 iter != network_list_.end(); ++iter) {
1218 NetworkState* network = (*iter)->AsNetworkState(); 1240 NetworkState* network = (*iter)->AsNetworkState();
1219 if (!network->update_received()) { 1241 if (!network->update_received()) {
1220 new_networks.push_back(std::move(*iter)); 1242 new_networks.push_back(std::move(*iter));
1221 continue; 1243 continue;
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
1542 if (type.MatchesType(shill::kTypeVPN)) 1564 if (type.MatchesType(shill::kTypeVPN))
1543 technologies.emplace_back(shill::kTypeVPN); 1565 technologies.emplace_back(shill::kTypeVPN);
1544 if (type.MatchesType(kTypeTether)) 1566 if (type.MatchesType(kTypeTether))
1545 technologies.emplace_back(kTypeTether); 1567 technologies.emplace_back(kTypeTether);
1546 1568
1547 CHECK_GT(technologies.size(), 0ul); 1569 CHECK_GT(technologies.size(), 0ul);
1548 return technologies; 1570 return technologies;
1549 } 1571 }
1550 1572
1551 } // namespace chromeos 1573 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698