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

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

Issue 2945643002: [CrOS Tether] Sort Tether network lists. (Closed)
Patch Set: stevenjb@ comments. 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 state->path().c_str()); 60 state->path().c_str());
61 } 61 }
62 62
63 std::string ValueAsString(const base::Value& value) { 63 std::string ValueAsString(const base::Value& value) {
64 std::string vstr; 64 std::string vstr;
65 base::JSONWriter::WriteWithOptions( 65 base::JSONWriter::WriteWithOptions(
66 value, base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &vstr); 66 value, base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &vstr);
67 return vstr.empty() ? "''" : vstr; 67 return vstr.empty() ? "''" : vstr;
68 } 68 }
69 69
70 void EnsureActiveNetworksInFrontOfList(
71 NetworkStateHandler::NetworkStateList* list) {
72 size_t last_active_network_index = -1;
stevenjb 2017/06/22 00:25:19 Don't use size_t with negative values, it's genera
Kyle Horimoto 2017/06/22 02:08:01 Acknowledged.
73 for (size_t i = 0; i < list->size(); ++i) {
74 const NetworkState* network = list->at(i);
75 if (network->IsConnectingState() || network->IsConnectedState()) {
stevenjb 2017/06/22 00:25:19 I started to make some suggestions here, but this
Kyle Horimoto 2017/06/22 02:08:01 Acknowledged.
76 // A new active network has been found in the list.
77 last_active_network_index++;
78
79 if (i != last_active_network_index) {
80 // If this is an active network that is not in the correct order
81 // (i.e., it is listed after non-active networks), move it to the
82 // correct spot.
83 list->erase(list->begin() + i);
84 list->insert(list->begin() + last_active_network_index, network);
85 }
86 }
87 }
88 }
89
70 } // namespace 90 } // namespace
71 91
72 const char NetworkStateHandler::kDefaultCheckPortalList[] = 92 const char NetworkStateHandler::kDefaultCheckPortalList[] =
73 "ethernet,wifi,cellular"; 93 "ethernet,wifi,cellular";
74 94
75 NetworkStateHandler::NetworkStateHandler() {} 95 NetworkStateHandler::NetworkStateHandler() {}
76 96
77 NetworkStateHandler::~NetworkStateHandler() { 97 NetworkStateHandler::~NetworkStateHandler() {
78 // Normally Shutdown() will get called in ~NetworkHandler, however unit 98 // Normally Shutdown() will get called in ~NetworkHandler, however unit
79 // tests do not use that class so this needs to call Shutdown when we 99 // tests do not use that class so this needs to call Shutdown when we
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 int limit, 437 int limit,
418 NetworkStateList* list) { 438 NetworkStateList* list) {
419 DCHECK(list); 439 DCHECK(list);
420 list->clear(); 440 list->clear();
421 441
422 // Sort the network list if necessary. 442 // Sort the network list if necessary.
423 if (!network_list_sorted_) 443 if (!network_list_sorted_)
424 SortNetworkList(); 444 SortNetworkList();
425 445
426 if (type.MatchesPattern(NetworkTypePattern::Tether())) 446 if (type.MatchesPattern(NetworkTypePattern::Tether()))
427 GetTetherNetworkList(limit, list); 447 GetTetherNetworkList(limit, list);
stevenjb 2017/06/22 00:25:19 So, rather than using the kind of confusing + kind
Kyle Horimoto 2017/06/22 02:08:01 Done.
428 448
429 int count = list->size(); 449 int count = list->size();
430 450
431 if (type.Equals(NetworkTypePattern::Tether()) || 451 // Only add more networks if the limit has not yet been reached.
432 (limit != 0 && count >= limit)) { 452 for (auto iter = network_list_.begin();
433 // If only searching for Tether networks, there is no need to continue 453 iter != network_list_.end() && (limit == 0 || count < limit); ++iter) {
stevenjb 2017/06/22 00:25:19 Now that we've changed the logic, this is probably
Kyle Horimoto 2017/06/22 02:08:01 Done.
434 // searching through other network types; likewise, if the limit has already
435 // been reached, there is no need to continue searching.
436 return;
437 }
438
439 for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) {
440 const NetworkState* network = (*iter)->AsNetworkState(); 454 const NetworkState* network = (*iter)->AsNetworkState();
441 DCHECK(network); 455 DCHECK(network);
442 if (!network->update_received() || !network->Matches(type)) 456 if (!network->update_received() || !network->Matches(type))
443 continue; 457 continue;
444 if (configured_only && !network->IsInProfile()) 458 if (configured_only && !network->IsInProfile())
445 continue; 459 continue;
446 if (visible_only && !network->visible()) 460 if (visible_only && !network->visible())
447 continue; 461 continue;
462 if (network->type() == shill::kTypeWifi &&
463 !network->tether_guid().empty()) {
464 // Wi-Fi networks which are actually underlying Wi-Fi hotspots for a
465 // Tether network should not be returned since they should only be shown
466 // to the user as Tether networks.
467 continue;
468 }
stevenjb 2017/06/22 00:25:19 Move all of the above logic into helper method (e.
Kyle Horimoto 2017/06/22 02:08:01 Done.
448 if (network->type() == shill::kTypeEthernet) { 469 if (network->type() == shill::kTypeEthernet) {
449 // Ethernet networks should always be in front. 470 // Ethernet networks should always be in front.
450 list->insert(list->begin(), network); 471 list->insert(list->begin(), network);
451 } else { 472 } else {
452 list->push_back(network); 473 list->push_back(network);
453 } 474 }
stevenjb 2017/06/22 00:25:19 break if this is not an active network (note: Ethe
Kyle Horimoto 2017/06/22 02:08:01 Done.
454 if (limit > 0 && ++count >= limit) 475 count++;
455 break; 476 }
477
478 if (type.MatchesPattern(NetworkTypePattern::Tether())) {
479 // Tether networks were added to |list| above before non-Tether networks.
480 // Thus, if |type| includes Tether networks, it is possible that |list|
481 // contains some non-active Tether networks before some active (i.e.,
482 // connecting or connected) non-Tether networks. Active networks should
483 // always be listed first, so adjust the list accordingly.
484 EnsureActiveNetworksInFrontOfList(list);
stevenjb 2017/06/22 00:25:19 Append GetTetherNetworkList(limit, list, active=fa
Kyle Horimoto 2017/06/22 02:08:01 Done.
456 } 485 }
457 } 486 }
458 487
459 void NetworkStateHandler::GetTetherNetworkList(int limit, 488 void NetworkStateHandler::GetTetherNetworkList(int limit,
460 NetworkStateList* list) { 489 NetworkStateList* list) {
461 DCHECK(list); 490 DCHECK(list);
462 list->clear(); 491 list->clear();
463 492
464 if (!IsTechnologyEnabled(NetworkTypePattern::Tether())) 493 if (!IsTechnologyEnabled(NetworkTypePattern::Tether()))
465 return; 494 return;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 tether_network_state->set_visible(true); 560 tether_network_state->set_visible(true);
532 tether_network_state->set_update_received(); 561 tether_network_state->set_update_received();
533 tether_network_state->set_update_requested(false); 562 tether_network_state->set_update_requested(false);
534 tether_network_state->set_connectable(true); 563 tether_network_state->set_connectable(true);
535 tether_network_state->set_carrier(carrier); 564 tether_network_state->set_carrier(carrier);
536 tether_network_state->set_battery_percentage(battery_percentage); 565 tether_network_state->set_battery_percentage(battery_percentage);
537 tether_network_state->set_tether_has_connected_to_host(has_connected_to_host); 566 tether_network_state->set_tether_has_connected_to_host(has_connected_to_host);
538 tether_network_state->set_signal_strength(signal_strength); 567 tether_network_state->set_signal_strength(signal_strength);
539 568
540 tether_network_list_.push_back(std::move(tether_network_state)); 569 tether_network_list_.push_back(std::move(tether_network_state));
570 network_list_sorted_ = false;
571
541 NotifyNetworkListChanged(); 572 NotifyNetworkListChanged();
542 } 573 }
543 574
544 bool NetworkStateHandler::UpdateTetherNetworkProperties( 575 bool NetworkStateHandler::UpdateTetherNetworkProperties(
545 const std::string& guid, 576 const std::string& guid,
546 const std::string& carrier, 577 const std::string& carrier,
547 int battery_percentage, 578 int battery_percentage,
548 int signal_strength) { 579 int signal_strength) {
549 if (tether_technology_state_ != TECHNOLOGY_ENABLED) { 580 if (tether_technology_state_ != TECHNOLOGY_ENABLED) {
550 NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether " 581 NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether "
551 << "networks are not enabled. Cannot update."; 582 << "networks are not enabled. Cannot update.";
552 return false; 583 return false;
553 } 584 }
554 585
555 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); 586 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid);
556 if (!tether_network_state) { 587 if (!tether_network_state) {
557 NET_LOG(ERROR) << "UpdateTetherNetworkProperties(): No NetworkState for " 588 NET_LOG(ERROR) << "UpdateTetherNetworkProperties(): No NetworkState for "
558 << "Tether network with GUID \"" << guid << "\"."; 589 << "Tether network with GUID \"" << guid << "\".";
559 return false; 590 return false;
560 } 591 }
561 592
562 tether_network_state->set_carrier(carrier); 593 tether_network_state->set_carrier(carrier);
563 tether_network_state->set_battery_percentage(battery_percentage); 594 tether_network_state->set_battery_percentage(battery_percentage);
564 tether_network_state->set_signal_strength(signal_strength); 595 tether_network_state->set_signal_strength(signal_strength);
596 network_list_sorted_ = false;
565 597
566 NotifyNetworkPropertiesUpdated(tether_network_state); 598 NotifyNetworkPropertiesUpdated(tether_network_state);
567 return true; 599 return true;
568 } 600 }
569 601
570 bool NetworkStateHandler::SetTetherNetworkHasConnectedToHost( 602 bool NetworkStateHandler::SetTetherNetworkHasConnectedToHost(
571 const std::string& guid) { 603 const std::string& guid) {
572 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); 604 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid);
573 if (!tether_network_state) { 605 if (!tether_network_state) {
574 NET_LOG(ERROR) << "SetTetherNetworkHasConnectedToHost(): No NetworkState " 606 NET_LOG(ERROR) << "SetTetherNetworkHasConnectedToHost(): No NetworkState "
575 << "for Tether network with GUID \"" << guid << "\"."; 607 << "for Tether network with GUID \"" << guid << "\".";
576 return false; 608 return false;
577 } 609 }
578 610
579 if (tether_network_state->tether_has_connected_to_host()) { 611 if (tether_network_state->tether_has_connected_to_host()) {
580 return false; 612 return false;
581 } 613 }
582 614
583 tether_network_state->set_tether_has_connected_to_host(true); 615 tether_network_state->set_tether_has_connected_to_host(true);
616 network_list_sorted_ = false;
584 617
585 NotifyNetworkPropertiesUpdated(tether_network_state); 618 NotifyNetworkPropertiesUpdated(tether_network_state);
586 return true; 619 return true;
587 } 620 }
588 621
589 bool NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) { 622 bool NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) {
590 for (auto iter = tether_network_list_.begin(); 623 for (auto iter = tether_network_list_.begin();
591 iter != tether_network_list_.end(); ++iter) { 624 iter != tether_network_list_.end(); ++iter) {
592 if (iter->get()->AsNetworkState()->guid() == guid) { 625 if (iter->get()->AsNetworkState()->guid() == guid) {
593 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid( 626 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid(
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 return false; 660 return false;
628 } 661 }
629 662
630 if (wifi_network_state->tether_guid().empty() && 663 if (wifi_network_state->tether_guid().empty() &&
631 tether_network_state->tether_guid().empty()) { 664 tether_network_state->tether_guid().empty()) {
632 return true; 665 return true;
633 } 666 }
634 667
635 wifi_network_state->set_tether_guid(std::string()); 668 wifi_network_state->set_tether_guid(std::string());
636 tether_network_state->set_tether_guid(std::string()); 669 tether_network_state->set_tether_guid(std::string());
670 network_list_sorted_ = false;
637 671
638 NotifyNetworkPropertiesUpdated(wifi_network_state); 672 NotifyNetworkPropertiesUpdated(wifi_network_state);
639 NotifyNetworkPropertiesUpdated(tether_network_state); 673 NotifyNetworkPropertiesUpdated(tether_network_state);
640 674
641 return true; 675 return true;
642 } 676 }
643 677
644 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork( 678 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork(
645 const std::string& tether_network_guid, 679 const std::string& tether_network_guid,
646 const std::string& wifi_network_guid) { 680 const std::string& wifi_network_guid) {
(...skipping 27 matching lines...) Expand all
674 return false; 708 return false;
675 } 709 }
676 710
677 if (wifi_network_state->tether_guid() == tether_network_guid && 711 if (wifi_network_state->tether_guid() == tether_network_guid &&
678 tether_network_state->tether_guid() == wifi_network_guid) { 712 tether_network_state->tether_guid() == wifi_network_guid) {
679 return true; 713 return true;
680 } 714 }
681 715
682 tether_network_state->set_tether_guid(wifi_network_guid); 716 tether_network_state->set_tether_guid(wifi_network_guid);
683 wifi_network_state->set_tether_guid(tether_network_guid); 717 wifi_network_state->set_tether_guid(tether_network_guid);
718 network_list_sorted_ = false;
684 719
685 NotifyNetworkPropertiesUpdated(wifi_network_state); 720 NotifyNetworkPropertiesUpdated(wifi_network_state);
686 NotifyNetworkPropertiesUpdated(tether_network_state); 721 NotifyNetworkPropertiesUpdated(tether_network_state);
687 722
688 return true; 723 return true;
689 } 724 }
690 725
691 void NetworkStateHandler::SetTetherNetworkStateDisconnected( 726 void NetworkStateHandler::SetTetherNetworkStateDisconnected(
692 const std::string& guid) { 727 const std::string& guid) {
693 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); 728 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " 767 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network "
733 << "not found: " << guid; 768 << "not found: " << guid;
734 return; 769 return;
735 } 770 }
736 771
737 DCHECK( 772 DCHECK(
738 NetworkTypePattern::Tether().MatchesType(tether_network_state->type())); 773 NetworkTypePattern::Tether().MatchesType(tether_network_state->type()));
739 774
740 std::string prev_connection_state = tether_network_state->connection_state(); 775 std::string prev_connection_state = tether_network_state->connection_state();
741 tether_network_state->set_connection_state(connection_state); 776 tether_network_state->set_connection_state(connection_state);
777 network_list_sorted_ = false;
778
742 DCHECK(!tether_network_state->is_captive_portal()); 779 DCHECK(!tether_network_state->is_captive_portal());
743
744 if (ConnectionStateChanged(tether_network_state, prev_connection_state, 780 if (ConnectionStateChanged(tether_network_state, prev_connection_state,
745 false /* prev_is_captive_portal */)) { 781 false /* prev_is_captive_portal */)) {
746 NET_LOG(EVENT) << "Changing connection state for Tether network with GUID " 782 NET_LOG(EVENT) << "Changing connection state for Tether network with GUID "
747 << guid << ". Old state: " << prev_connection_state << ", " 783 << guid << ". Old state: " << prev_connection_state << ", "
748 << "New state: " << connection_state; 784 << "New state: " << connection_state;
749 785
750 OnNetworkConnectionStateChanged(tether_network_state); 786 OnNetworkConnectionStateChanged(tether_network_state);
751 NotifyNetworkPropertiesUpdated(tether_network_state); 787 NotifyNetworkPropertiesUpdated(tether_network_state);
752 } 788 }
753 } 789 }
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after
1200 devices += ", "; 1236 devices += ", ";
1201 devices += (*iter)->name(); 1237 devices += (*iter)->name();
1202 } 1238 }
1203 NET_LOG_EVENT("DeviceList", devices); 1239 NET_LOG_EVENT("DeviceList", devices);
1204 NotifyDeviceListChanged(); 1240 NotifyDeviceListChanged();
1205 } else { 1241 } else {
1206 NOTREACHED(); 1242 NOTREACHED();
1207 } 1243 }
1208 } 1244 }
1209 1245
1210 // TODO(khorimoto): Add sorting for the Tether network list as well.
1211 void NetworkStateHandler::SortNetworkList() { 1246 void NetworkStateHandler::SortNetworkList() {
1247 if (tether_sort_delegate_)
1248 tether_sort_delegate_->SortTetherNetworkList(&tether_network_list_);
1249
1212 // Note: usually active networks will precede inactive networks, however 1250 // Note: usually active networks will precede inactive networks, however
1213 // this may briefly be untrue during state transitions (e.g. a network may 1251 // this may briefly be untrue during state transitions (e.g. a network may
1214 // transition to idle before the list is updated). 1252 // transition to idle before the list is updated).
1215 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; 1253 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks;
1216 for (ManagedStateList::iterator iter = network_list_.begin(); 1254 for (ManagedStateList::iterator iter = network_list_.begin();
1217 iter != network_list_.end(); ++iter) { 1255 iter != network_list_.end(); ++iter) {
1218 NetworkState* network = (*iter)->AsNetworkState(); 1256 NetworkState* network = (*iter)->AsNetworkState();
1219 if (!network->update_received()) { 1257 if (!network->update_received()) {
1220 new_networks.push_back(std::move(*iter)); 1258 new_networks.push_back(std::move(*iter));
1221 continue; 1259 continue;
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
1542 if (type.MatchesType(shill::kTypeVPN)) 1580 if (type.MatchesType(shill::kTypeVPN))
1543 technologies.emplace_back(shill::kTypeVPN); 1581 technologies.emplace_back(shill::kTypeVPN);
1544 if (type.MatchesType(kTypeTether)) 1582 if (type.MatchesType(kTypeTether))
1545 technologies.emplace_back(kTypeTether); 1583 technologies.emplace_back(kTypeTether);
1546 1584
1547 CHECK_GT(technologies.size(), 0ul); 1585 CHECK_GT(technologies.size(), 0ul);
1548 return technologies; 1586 return technologies;
1549 } 1587 }
1550 1588
1551 } // namespace chromeos 1589 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698