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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 bool ShouldIncludeNetworkInList(const NetworkState* network_state, | |
71 bool configured_only, | |
72 bool visible_only, | |
73 bool active) { | |
stevenjb
2017/06/22 16:44:38
nit: get_active might be more clear?
Kyle Horimoto
2017/06/22 17:27:22
Done.
| |
74 if (configured_only && !network_state->IsInProfile()) | |
75 return false; | |
76 | |
77 if (visible_only && !network_state->visible()) | |
78 return false; | |
79 | |
80 bool is_network_active = | |
81 network_state->IsConnectedState() || network_state->IsConnectingState(); | |
82 if (active != is_network_active) | |
stevenjb
2017/06/22 16:44:38
nit: Inverted order is a little more clear IMHO
Kyle Horimoto
2017/06/22 17:27:22
Done.
| |
83 return false; | |
84 | |
85 if (network_state->type() == shill::kTypeWifi && | |
86 !network_state->tether_guid().empty()) { | |
87 // Wi-Fi networks which are actually underlying Wi-Fi hotspots for a | |
88 // Tether network should not be included since they should only be shown | |
89 // to the user as Tether networks. | |
90 return false; | |
91 } | |
92 | |
93 return true; | |
94 } | |
95 | |
70 } // namespace | 96 } // namespace |
71 | 97 |
72 const char NetworkStateHandler::kDefaultCheckPortalList[] = | 98 const char NetworkStateHandler::kDefaultCheckPortalList[] = |
73 "ethernet,wifi,cellular"; | 99 "ethernet,wifi,cellular"; |
74 | 100 |
75 NetworkStateHandler::NetworkStateHandler() {} | 101 NetworkStateHandler::NetworkStateHandler() {} |
76 | 102 |
77 NetworkStateHandler::~NetworkStateHandler() { | 103 NetworkStateHandler::~NetworkStateHandler() { |
78 // Normally Shutdown() will get called in ~NetworkHandler, however unit | 104 // Normally Shutdown() will get called in ~NetworkHandler, however unit |
79 // tests do not use that class so this needs to call Shutdown when we | 105 // tests do not use that class so this needs to call Shutdown when we |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
407 true /* visible_only */, 0 /* no limit */, list); | 433 true /* visible_only */, 0 /* no limit */, list); |
408 } | 434 } |
409 | 435 |
410 void NetworkStateHandler::GetVisibleNetworkList(NetworkStateList* list) { | 436 void NetworkStateHandler::GetVisibleNetworkList(NetworkStateList* list) { |
411 GetVisibleNetworkListByType(NetworkTypePattern::Default(), list); | 437 GetVisibleNetworkListByType(NetworkTypePattern::Default(), list); |
412 } | 438 } |
413 | 439 |
414 void NetworkStateHandler::GetNetworkListByType(const NetworkTypePattern& type, | 440 void NetworkStateHandler::GetNetworkListByType(const NetworkTypePattern& type, |
415 bool configured_only, | 441 bool configured_only, |
416 bool visible_only, | 442 bool visible_only, |
417 int limit, | 443 size_t limit, |
418 NetworkStateList* list) { | 444 NetworkStateList* list) { |
419 DCHECK(list); | 445 DCHECK(list); |
420 list->clear(); | 446 list->clear(); |
421 | 447 |
422 // Sort the network list if necessary. | |
423 if (!network_list_sorted_) | 448 if (!network_list_sorted_) |
424 SortNetworkList(); | 449 SortNetworkList(); |
425 | 450 |
451 // First, add active Tether networks. | |
426 if (type.MatchesPattern(NetworkTypePattern::Tether())) | 452 if (type.MatchesPattern(NetworkTypePattern::Tether())) |
427 GetTetherNetworkList(limit, list); | 453 GetTetherNetworkList(true /* active */, limit, list); |
428 | 454 |
429 int count = list->size(); | 455 // Second, add active non-Tether networks. |
430 | 456 for (auto iter = network_list_.begin(); |
431 if (type.Equals(NetworkTypePattern::Tether()) || | 457 iter != network_list_.end() && (limit == 0 || list->size() < limit); |
stevenjb
2017/06/22 16:44:38
nit: (Sorry, just thought of this), since we're do
Kyle Horimoto
2017/06/22 17:27:22
Done.
| |
432 (limit != 0 && count >= limit)) { | 458 ++iter) { |
433 // If only searching for Tether networks, there is no need to continue | |
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(); | 459 const NetworkState* network = (*iter)->AsNetworkState(); |
441 DCHECK(network); | 460 DCHECK(network); |
442 if (!network->update_received() || !network->Matches(type)) | 461 if (!network->update_received() || !network->Matches(type)) |
443 continue; | 462 continue; |
444 if (configured_only && !network->IsInProfile()) | 463 if (!ShouldIncludeNetworkInList(network, configured_only, visible_only, |
464 true /* active */)) { | |
445 continue; | 465 continue; |
446 if (visible_only && !network->visible()) | 466 } |
447 continue; | |
448 if (network->type() == shill::kTypeEthernet) { | 467 if (network->type() == shill::kTypeEthernet) { |
449 // Ethernet networks should always be in front. | 468 // Ethernet networks should always be in front. |
450 list->insert(list->begin(), network); | 469 list->insert(list->begin(), network); |
451 } else { | 470 } else { |
452 list->push_back(network); | 471 list->push_back(network); |
453 } | 472 } |
454 if (limit > 0 && ++count >= limit) | 473 } |
455 break; | 474 |
475 // Third, add inactive Tether networks. | |
476 if (type.MatchesPattern(NetworkTypePattern::Tether())) | |
477 GetTetherNetworkList(false /* active */, limit, list); | |
478 | |
479 // Fourth, add inactive non-Tether networks. | |
480 for (auto iter = network_list_.begin(); | |
481 iter != network_list_.end() && (limit == 0 || list->size() < limit); | |
482 ++iter) { | |
483 const NetworkState* network = (*iter)->AsNetworkState(); | |
484 DCHECK(network); | |
485 if (!network->update_received() || !network->Matches(type)) | |
486 continue; | |
487 if (!ShouldIncludeNetworkInList(network, configured_only, visible_only, | |
488 false /* active */)) { | |
489 continue; | |
490 } | |
491 list->push_back(network); | |
456 } | 492 } |
457 } | 493 } |
458 | 494 |
459 void NetworkStateHandler::GetTetherNetworkList(int limit, | 495 void NetworkStateHandler::GetTetherNetworkList(bool active, |
stevenjb
2017/06/22 16:44:38
nit: We should probably rename this to something l
Kyle Horimoto
2017/06/22 17:27:22
Done.
| |
496 size_t limit, | |
460 NetworkStateList* list) { | 497 NetworkStateList* list) { |
461 DCHECK(list); | 498 DCHECK(list); |
462 list->clear(); | |
463 | 499 |
464 if (!IsTechnologyEnabled(NetworkTypePattern::Tether())) | 500 if (!IsTechnologyEnabled(NetworkTypePattern::Tether())) |
465 return; | 501 return; |
466 | 502 |
467 int count = 0; | |
468 for (auto iter = tether_network_list_.begin(); | 503 for (auto iter = tether_network_list_.begin(); |
469 iter != tether_network_list_.end(); ++iter) { | 504 iter != tether_network_list_.end() && |
470 list->push_back((*iter)->AsNetworkState()); | 505 (limit == 0 || list->size() < limit); |
471 if (limit > 0 && ++count >= limit) | 506 ++iter) { |
472 return; | 507 const NetworkState* network = (*iter)->AsNetworkState(); |
508 DCHECK(network); | |
509 | |
510 if (!ShouldIncludeNetworkInList(network, false /* configured_only */, | |
511 false /* visible_only */, active)) { | |
512 continue; | |
513 } | |
514 | |
515 list->push_back(network); | |
473 } | 516 } |
474 } | 517 } |
475 | 518 |
476 const NetworkState* NetworkStateHandler::GetNetworkStateFromServicePath( | 519 const NetworkState* NetworkStateHandler::GetNetworkStateFromServicePath( |
477 const std::string& service_path, | 520 const std::string& service_path, |
478 bool configured_only) const { | 521 bool configured_only) const { |
479 ManagedState* managed = | 522 ManagedState* managed = |
480 GetModifiableManagedState(&network_list_, service_path); | 523 GetModifiableManagedState(&network_list_, service_path); |
481 if (!managed) { | 524 if (!managed) { |
482 managed = GetModifiableManagedState(&tether_network_list_, service_path); | 525 managed = GetModifiableManagedState(&tether_network_list_, service_path); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 tether_network_state->set_visible(true); | 574 tether_network_state->set_visible(true); |
532 tether_network_state->set_update_received(); | 575 tether_network_state->set_update_received(); |
533 tether_network_state->set_update_requested(false); | 576 tether_network_state->set_update_requested(false); |
534 tether_network_state->set_connectable(true); | 577 tether_network_state->set_connectable(true); |
535 tether_network_state->set_carrier(carrier); | 578 tether_network_state->set_carrier(carrier); |
536 tether_network_state->set_battery_percentage(battery_percentage); | 579 tether_network_state->set_battery_percentage(battery_percentage); |
537 tether_network_state->set_tether_has_connected_to_host(has_connected_to_host); | 580 tether_network_state->set_tether_has_connected_to_host(has_connected_to_host); |
538 tether_network_state->set_signal_strength(signal_strength); | 581 tether_network_state->set_signal_strength(signal_strength); |
539 | 582 |
540 tether_network_list_.push_back(std::move(tether_network_state)); | 583 tether_network_list_.push_back(std::move(tether_network_state)); |
584 network_list_sorted_ = false; | |
585 | |
541 NotifyNetworkListChanged(); | 586 NotifyNetworkListChanged(); |
542 } | 587 } |
543 | 588 |
544 bool NetworkStateHandler::UpdateTetherNetworkProperties( | 589 bool NetworkStateHandler::UpdateTetherNetworkProperties( |
545 const std::string& guid, | 590 const std::string& guid, |
546 const std::string& carrier, | 591 const std::string& carrier, |
547 int battery_percentage, | 592 int battery_percentage, |
548 int signal_strength) { | 593 int signal_strength) { |
549 if (tether_technology_state_ != TECHNOLOGY_ENABLED) { | 594 if (tether_technology_state_ != TECHNOLOGY_ENABLED) { |
550 NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether " | 595 NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether " |
551 << "networks are not enabled. Cannot update."; | 596 << "networks are not enabled. Cannot update."; |
552 return false; | 597 return false; |
553 } | 598 } |
554 | 599 |
555 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); | 600 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); |
556 if (!tether_network_state) { | 601 if (!tether_network_state) { |
557 NET_LOG(ERROR) << "UpdateTetherNetworkProperties(): No NetworkState for " | 602 NET_LOG(ERROR) << "UpdateTetherNetworkProperties(): No NetworkState for " |
558 << "Tether network with GUID \"" << guid << "\"."; | 603 << "Tether network with GUID \"" << guid << "\"."; |
559 return false; | 604 return false; |
560 } | 605 } |
561 | 606 |
562 tether_network_state->set_carrier(carrier); | 607 tether_network_state->set_carrier(carrier); |
563 tether_network_state->set_battery_percentage(battery_percentage); | 608 tether_network_state->set_battery_percentage(battery_percentage); |
564 tether_network_state->set_signal_strength(signal_strength); | 609 tether_network_state->set_signal_strength(signal_strength); |
610 network_list_sorted_ = false; | |
565 | 611 |
566 NotifyNetworkPropertiesUpdated(tether_network_state); | 612 NotifyNetworkPropertiesUpdated(tether_network_state); |
567 return true; | 613 return true; |
568 } | 614 } |
569 | 615 |
570 bool NetworkStateHandler::SetTetherNetworkHasConnectedToHost( | 616 bool NetworkStateHandler::SetTetherNetworkHasConnectedToHost( |
571 const std::string& guid) { | 617 const std::string& guid) { |
572 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); | 618 NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid); |
573 if (!tether_network_state) { | 619 if (!tether_network_state) { |
574 NET_LOG(ERROR) << "SetTetherNetworkHasConnectedToHost(): No NetworkState " | 620 NET_LOG(ERROR) << "SetTetherNetworkHasConnectedToHost(): No NetworkState " |
575 << "for Tether network with GUID \"" << guid << "\"."; | 621 << "for Tether network with GUID \"" << guid << "\"."; |
576 return false; | 622 return false; |
577 } | 623 } |
578 | 624 |
579 if (tether_network_state->tether_has_connected_to_host()) { | 625 if (tether_network_state->tether_has_connected_to_host()) { |
580 return false; | 626 return false; |
581 } | 627 } |
582 | 628 |
583 tether_network_state->set_tether_has_connected_to_host(true); | 629 tether_network_state->set_tether_has_connected_to_host(true); |
630 network_list_sorted_ = false; | |
584 | 631 |
585 NotifyNetworkPropertiesUpdated(tether_network_state); | 632 NotifyNetworkPropertiesUpdated(tether_network_state); |
586 return true; | 633 return true; |
587 } | 634 } |
588 | 635 |
589 bool NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) { | 636 bool NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) { |
590 for (auto iter = tether_network_list_.begin(); | 637 for (auto iter = tether_network_list_.begin(); |
591 iter != tether_network_list_.end(); ++iter) { | 638 iter != tether_network_list_.end(); ++iter) { |
592 if (iter->get()->AsNetworkState()->guid() == guid) { | 639 if (iter->get()->AsNetworkState()->guid() == guid) { |
593 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid( | 640 NetworkState* wifi_network = GetModifiableNetworkStateFromGuid( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
627 return false; | 674 return false; |
628 } | 675 } |
629 | 676 |
630 if (wifi_network_state->tether_guid().empty() && | 677 if (wifi_network_state->tether_guid().empty() && |
631 tether_network_state->tether_guid().empty()) { | 678 tether_network_state->tether_guid().empty()) { |
632 return true; | 679 return true; |
633 } | 680 } |
634 | 681 |
635 wifi_network_state->set_tether_guid(std::string()); | 682 wifi_network_state->set_tether_guid(std::string()); |
636 tether_network_state->set_tether_guid(std::string()); | 683 tether_network_state->set_tether_guid(std::string()); |
684 network_list_sorted_ = false; | |
637 | 685 |
638 NotifyNetworkPropertiesUpdated(wifi_network_state); | 686 NotifyNetworkPropertiesUpdated(wifi_network_state); |
639 NotifyNetworkPropertiesUpdated(tether_network_state); | 687 NotifyNetworkPropertiesUpdated(tether_network_state); |
640 | 688 |
641 return true; | 689 return true; |
642 } | 690 } |
643 | 691 |
644 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork( | 692 bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork( |
645 const std::string& tether_network_guid, | 693 const std::string& tether_network_guid, |
646 const std::string& wifi_network_guid) { | 694 const std::string& wifi_network_guid) { |
(...skipping 27 matching lines...) Expand all Loading... | |
674 return false; | 722 return false; |
675 } | 723 } |
676 | 724 |
677 if (wifi_network_state->tether_guid() == tether_network_guid && | 725 if (wifi_network_state->tether_guid() == tether_network_guid && |
678 tether_network_state->tether_guid() == wifi_network_guid) { | 726 tether_network_state->tether_guid() == wifi_network_guid) { |
679 return true; | 727 return true; |
680 } | 728 } |
681 | 729 |
682 tether_network_state->set_tether_guid(wifi_network_guid); | 730 tether_network_state->set_tether_guid(wifi_network_guid); |
683 wifi_network_state->set_tether_guid(tether_network_guid); | 731 wifi_network_state->set_tether_guid(tether_network_guid); |
732 network_list_sorted_ = false; | |
684 | 733 |
685 NotifyNetworkPropertiesUpdated(wifi_network_state); | 734 NotifyNetworkPropertiesUpdated(wifi_network_state); |
686 NotifyNetworkPropertiesUpdated(tether_network_state); | 735 NotifyNetworkPropertiesUpdated(tether_network_state); |
687 | 736 |
688 return true; | 737 return true; |
689 } | 738 } |
690 | 739 |
691 void NetworkStateHandler::SetTetherNetworkStateDisconnected( | 740 void NetworkStateHandler::SetTetherNetworkStateDisconnected( |
692 const std::string& guid) { | 741 const std::string& guid) { |
693 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); | 742 SetTetherNetworkStateConnectionState(guid, shill::kStateDisconnect); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
732 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " | 781 NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network " |
733 << "not found: " << guid; | 782 << "not found: " << guid; |
734 return; | 783 return; |
735 } | 784 } |
736 | 785 |
737 DCHECK( | 786 DCHECK( |
738 NetworkTypePattern::Tether().MatchesType(tether_network_state->type())); | 787 NetworkTypePattern::Tether().MatchesType(tether_network_state->type())); |
739 | 788 |
740 std::string prev_connection_state = tether_network_state->connection_state(); | 789 std::string prev_connection_state = tether_network_state->connection_state(); |
741 tether_network_state->set_connection_state(connection_state); | 790 tether_network_state->set_connection_state(connection_state); |
791 network_list_sorted_ = false; | |
792 | |
742 DCHECK(!tether_network_state->is_captive_portal()); | 793 DCHECK(!tether_network_state->is_captive_portal()); |
743 | |
744 if (ConnectionStateChanged(tether_network_state, prev_connection_state, | 794 if (ConnectionStateChanged(tether_network_state, prev_connection_state, |
745 false /* prev_is_captive_portal */)) { | 795 false /* prev_is_captive_portal */)) { |
746 NET_LOG(EVENT) << "Changing connection state for Tether network with GUID " | 796 NET_LOG(EVENT) << "Changing connection state for Tether network with GUID " |
747 << guid << ". Old state: " << prev_connection_state << ", " | 797 << guid << ". Old state: " << prev_connection_state << ", " |
748 << "New state: " << connection_state; | 798 << "New state: " << connection_state; |
749 | 799 |
750 OnNetworkConnectionStateChanged(tether_network_state); | 800 OnNetworkConnectionStateChanged(tether_network_state); |
751 NotifyNetworkPropertiesUpdated(tether_network_state); | 801 NotifyNetworkPropertiesUpdated(tether_network_state); |
752 } | 802 } |
753 } | 803 } |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1200 devices += ", "; | 1250 devices += ", "; |
1201 devices += (*iter)->name(); | 1251 devices += (*iter)->name(); |
1202 } | 1252 } |
1203 NET_LOG_EVENT("DeviceList", devices); | 1253 NET_LOG_EVENT("DeviceList", devices); |
1204 NotifyDeviceListChanged(); | 1254 NotifyDeviceListChanged(); |
1205 } else { | 1255 } else { |
1206 NOTREACHED(); | 1256 NOTREACHED(); |
1207 } | 1257 } |
1208 } | 1258 } |
1209 | 1259 |
1210 // TODO(khorimoto): Add sorting for the Tether network list as well. | |
1211 void NetworkStateHandler::SortNetworkList() { | 1260 void NetworkStateHandler::SortNetworkList() { |
1261 if (tether_sort_delegate_) | |
1262 tether_sort_delegate_->SortTetherNetworkList(&tether_network_list_); | |
1263 | |
1212 // Note: usually active networks will precede inactive networks, however | 1264 // Note: usually active networks will precede inactive networks, however |
1213 // this may briefly be untrue during state transitions (e.g. a network may | 1265 // this may briefly be untrue during state transitions (e.g. a network may |
1214 // transition to idle before the list is updated). | 1266 // transition to idle before the list is updated). |
1215 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; | 1267 ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks; |
1216 for (ManagedStateList::iterator iter = network_list_.begin(); | 1268 for (ManagedStateList::iterator iter = network_list_.begin(); |
1217 iter != network_list_.end(); ++iter) { | 1269 iter != network_list_.end(); ++iter) { |
1218 NetworkState* network = (*iter)->AsNetworkState(); | 1270 NetworkState* network = (*iter)->AsNetworkState(); |
1219 if (!network->update_received()) { | 1271 if (!network->update_received()) { |
1220 new_networks.push_back(std::move(*iter)); | 1272 new_networks.push_back(std::move(*iter)); |
1221 continue; | 1273 continue; |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1542 if (type.MatchesType(shill::kTypeVPN)) | 1594 if (type.MatchesType(shill::kTypeVPN)) |
1543 technologies.emplace_back(shill::kTypeVPN); | 1595 technologies.emplace_back(shill::kTypeVPN); |
1544 if (type.MatchesType(kTypeTether)) | 1596 if (type.MatchesType(kTypeTether)) |
1545 technologies.emplace_back(kTypeTether); | 1597 technologies.emplace_back(kTypeTether); |
1546 | 1598 |
1547 CHECK_GT(technologies.size(), 0ul); | 1599 CHECK_GT(technologies.size(), 0ul); |
1548 return technologies; | 1600 return technologies; |
1549 } | 1601 } |
1550 | 1602 |
1551 } // namespace chromeos | 1603 } // namespace chromeos |
OLD | NEW |