Commit 131a5f99 authored by Trent Begin's avatar Trent Begin Committed by Commit Bot

network_health: add device state information

NetworkHealth now gathers a subeset the underlying network device's
properties.

Bug: chromium:1057739
Test: unit_tests --gtest_filter=NetworkHealthTest.*
Change-Id: I69a9be3226ce515a049708357978e9d313e84c1f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2133162
Commit-Queue: Trent Begin <tbegin@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756060}
parent 61fbaafd
......@@ -10,6 +10,7 @@
#include "chromeos/services/network_config/in_process_instance.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
using chromeos::network_config::mojom::DeviceStatePropertiesPtr;
using chromeos::network_config::mojom::FilterType;
using chromeos::network_config::mojom::NetworkFilter;
using chromeos::network_config::mojom::NetworkStatePropertiesPtr;
......@@ -28,6 +29,10 @@ NetworkHealth::NetworkState::NetworkState() {}
NetworkHealth::NetworkState::~NetworkState() {}
NetworkHealth::DeviceState::DeviceState() {}
NetworkHealth::DeviceState::~DeviceState() {}
NetworkHealth::NetworkHealth() {
chromeos::network_config::BindToInProcessInstance(
remote_cros_network_config_.BindNewPipeAndPassReceiver());
......@@ -45,13 +50,13 @@ NetworkHealth::NetworkHealthState NetworkHealth::GetNetworkHealthState() {
void NetworkHealth::RefreshNetworkHealthState() {
RequestActiveNetworks();
RequestDeviceStateList();
}
void NetworkHealth::OnActiveNetworksReceived(
std::vector<NetworkStatePropertiesPtr> network_props) {
std::vector<NetworkState> active_networks;
for (const auto& prop : network_props) {
DCHECK(prop);
NetworkState state;
state.name = prop->name;
state.type = prop->type;
......@@ -62,11 +67,29 @@ void NetworkHealth::OnActiveNetworksReceived(
network_health_state_.active_networks.swap(active_networks);
}
void NetworkHealth::OnDeviceStateListReceived(
std::vector<DeviceStatePropertiesPtr> device_props) {
std::vector<DeviceState> devices;
for (const auto& prop : device_props) {
DeviceState device;
device.mac_address = prop->mac_address.value_or("");
device.type = prop->type;
device.state = prop->device_state;
devices.push_back(std::move(device));
}
network_health_state_.devices.swap(devices);
}
void NetworkHealth::OnActiveNetworksChanged(
std::vector<NetworkStatePropertiesPtr> network_props) {
OnActiveNetworksReceived(std::move(network_props));
}
void NetworkHealth::OnDeviceStateListChanged() {
RequestDeviceStateList();
}
void NetworkHealth::RequestActiveNetworks() {
remote_cros_network_config_->GetNetworkStateList(
NetworkFilter::New(FilterType::kActive, NetworkType::kAll,
......@@ -75,4 +98,9 @@ void NetworkHealth::RequestActiveNetworks() {
base::Unretained(this)));
}
void NetworkHealth::RequestDeviceStateList() {
remote_cros_network_config_->GetDeviceStateList(base::BindOnce(
&NetworkHealth::OnDeviceStateListReceived, base::Unretained(this)));
}
} // namespace chromeos
......@@ -26,6 +26,16 @@ class NetworkHealth
chromeos::network_config::mojom::ConnectionStateType connection_state;
};
// Structure for a single device's status.
struct DeviceState {
DeviceState();
~DeviceState();
std::string mac_address;
chromeos::network_config::mojom::NetworkType type;
chromeos::network_config::mojom::DeviceStateType state;
};
// Structure containing the current snapshot of the state of Network Health.
struct NetworkHealthState {
NetworkHealthState();
......@@ -33,6 +43,7 @@ class NetworkHealth
~NetworkHealthState();
std::vector<NetworkState> active_networks;
std::vector<DeviceState> devices;
};
NetworkHealth();
......@@ -42,20 +53,24 @@ class NetworkHealth
// Returns the current NetworkHealthState.
NetworkHealthState GetNetworkHealthState();
// Handler for receiving new active networks.
// Handler for receiving active networks.
void OnActiveNetworksReceived(
std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>);
// Handler for receiving networking devices.
void OnDeviceStateListReceived(
std::vector<chromeos::network_config::mojom::DeviceStatePropertiesPtr>);
// CrosNetworkConfigObserver implementation
void OnActiveNetworksChanged(
std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>)
override;
void OnDeviceStateListChanged() override;
// CrosNetworkConfigObserver unimplemented callbacks
void OnNetworkStateListChanged() override {}
void OnNetworkStateChanged(
chromeos::network_config::mojom::NetworkStatePropertiesPtr) override {}
void OnDeviceStateListChanged() override {}
void OnVpnProvidersChanged() override {}
void OnNetworkCertificatesChanged() override {}
......@@ -63,6 +78,7 @@ class NetworkHealth
// Asynchronous call that refreshes the current Network Health State.
void RefreshNetworkHealthState();
void RequestActiveNetworks();
void RequestDeviceStateList();
mojo::Remote<chromeos::network_config::mojom::CrosNetworkConfig>
remote_cros_network_config_;
......
......@@ -10,15 +10,16 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
using chromeos::network_config::mojom::NetworkType;
namespace {
// Constant values for fake device and service.
constexpr char kServicePath[] = "/service/0";
constexpr char kServiceName[] = "service_name";
constexpr char kServiceGUID[] = "wifi_guid";
constexpr char kDevicePath[] = "/device/wifi";
constexpr char kServiceGUID[] = "eth_guid";
constexpr char kDevicePath[] = "/device/eth";
constexpr char kDeviceName[] = "device_name";
constexpr char kDefaultProfle[] = "/profile/default";
} // namespace
......@@ -26,7 +27,10 @@ namespace chromeos {
class NetworkHealthTest : public ::testing::Test {
public:
NetworkHealthTest() {}
NetworkHealthTest() {
// Wait until CrosNetworkConfigTestHelper is fully setup.
task_environment_.RunUntilIdle();
}
protected:
content::BrowserTaskEnvironment task_environment_;
......@@ -35,26 +39,28 @@ class NetworkHealthTest : public ::testing::Test {
};
TEST_F(NetworkHealthTest, GetNetworkHealthSucceeds) {
// Create an active network.
// Check that the default wifi device created by CrosNetworkConfigTestHelper
// exists.
auto network_health_state = network_health_.GetNetworkHealthState();
ASSERT_EQ(network_health_state.devices.size(), std::size_t(1));
// Create an ethernet device and service, and make it the active network.
cros_network_config_test_helper_.network_state_helper().AddDevice(
kDevicePath, shill::kTypeWifi, kDeviceName);
kDevicePath, shill::kTypeEthernet, kDeviceName);
cros_network_config_test_helper_.network_state_helper()
.service_test()
->AddService(kServicePath, kServiceGUID, kServiceName, shill::kTypeWifi,
shill::kStateOnline, true);
cros_network_config_test_helper_.network_state_helper()
.profile_test()
->AddService(kDefaultProfle, kServicePath);
->AddService(kServicePath, kServiceGUID, kServiceName,
shill::kTypeEthernet, shill::kStateOnline, true);
// Wait until the network and service have been created and configured.
task_environment_.RunUntilIdle();
auto network_health_state = network_health_.GetNetworkHealthState();
network_health_state = network_health_.GetNetworkHealthState();
EXPECT_EQ(network_health_state.devices.size(), std::size_t(2));
EXPECT_EQ(network_health_state.active_networks.size(), std::size_t(1));
EXPECT_EQ(network_health_state.active_networks[0].name, kServiceName);
EXPECT_EQ(network_health_state.devices[1].type, NetworkType::kEthernet);
}
} // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment