Commit 086767ed authored by Hugo Benichi's avatar Hugo Benichi Committed by Commit Bot

arc: net: migrate NetworConfiguration out of ONC

This patch partially migrates away from ONC for populating the
following fields in arc::mojom::NetworkConfiguration:
 - guid
 - connection state
 - tethering state
 - network type
The equivalent shill properties directly cached in NetworkState are used
in state.

Migrating away from ONC and basing arc::mojom::NetworkConfiguration
creation directly on shill properties will facilitate the migration of
net.mojom outside of Chrome and into platform2/patchpanel.

BUG=b:145960788
TEST=Flashed Chrome, tested various connectivity setup in ARC, tested
scanning Apps.

Change-Id: I3c4d821cb1064638deac2a65d6ba372ace1b836b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2210298
Commit-Queue: Hugo Benichi <hugobenichi@google.com>
Reviewed-by: default avatarYusuke Sato <yusukes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#772587}
parent 40aa84e7
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "chromeos/network/network_state.h" #include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h" #include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_type_pattern.h" #include "chromeos/network/network_type_pattern.h"
#include "chromeos/network/network_util.h"
#include "chromeos/network/onc/onc_utils.h" #include "chromeos/network/onc/onc_utils.h"
#include "components/arc/arc_browser_context_keyed_service_factory_base.h" #include "components/arc/arc_browser_context_keyed_service_factory_base.h"
#include "components/arc/arc_prefs.h" #include "components/arc/arc_prefs.h"
...@@ -64,80 +63,37 @@ bool IsDeviceOwner() { ...@@ -64,80 +63,37 @@ bool IsDeviceOwner() {
user_manager::UserManager::Get()->GetOwnerAccountId(); user_manager::UserManager::Get()->GetOwnerAccountId();
} }
std::string GetString(const base::Value* dict, const char* key) { arc::mojom::SecurityType TranslateWiFiSecurity(const std::string& type) {
DCHECK(dict->is_dict()); if (type == shill::kSecurityNone)
const base::Value* string_value = return arc::mojom::SecurityType::NONE;
dict->FindKeyOfType(key, base::Value::Type::STRING); if (type == shill::kSecurityWep)
return string_value ? string_value->GetString() : std::string();
}
arc::mojom::SecurityType TranslateONCWifiSecurityType(
const base::DictionaryValue* dict) {
std::string type = GetString(dict, onc::wifi::kSecurity);
if (type == onc::wifi::kWEP_PSK)
return arc::mojom::SecurityType::WEP_PSK; return arc::mojom::SecurityType::WEP_PSK;
if (type == onc::wifi::kWEP_8021X) if (type == shill::kSecurityPsk)
return arc::mojom::SecurityType::WEP_8021X;
if (type == onc::wifi::kWPA_PSK)
return arc::mojom::SecurityType::WPA_PSK; return arc::mojom::SecurityType::WPA_PSK;
if (type == onc::wifi::kWPA_EAP) if (type == shill::kSecurityWpa)
return arc::mojom::SecurityType::WPA_PSK;
if (type == shill::kSecurity8021x)
return arc::mojom::SecurityType::WPA_EAP;
// Robust Security Network does not appear to be defined in Android.
// Approximate it with WPA_EAP
if (type == shill::kSecurityRsn)
return arc::mojom::SecurityType::WPA_EAP; return arc::mojom::SecurityType::WPA_EAP;
if (type.empty()) LOG(WARNING) << "Unknown WiFi security type " << type;
LOG(WARNING) << "WiFi security type property not found";
return arc::mojom::SecurityType::NONE; return arc::mojom::SecurityType::NONE;
} }
arc::mojom::TetheringClientState TranslateTetheringState( arc::mojom::TetheringClientState TranslateTetheringState(
const std::string& tethering_state) { const std::string& tethering_state) {
if (tethering_state == onc::tethering_state::kTetheringConfirmedState) if (tethering_state == shill::kTetheringConfirmedState)
return arc::mojom::TetheringClientState::CONFIRMED; return arc::mojom::TetheringClientState::CONFIRMED;
else if (tethering_state == onc::tethering_state::kTetheringNotDetectedState) if (tethering_state == shill::kTetheringNotDetectedState)
return arc::mojom::TetheringClientState::NOT_DETECTED; return arc::mojom::TetheringClientState::NOT_DETECTED;
else if (tethering_state == onc::tethering_state::kTetheringSuspectedState) if (tethering_state == shill::kTetheringSuspectedState)
return arc::mojom::TetheringClientState::SUSPECTED; return arc::mojom::TetheringClientState::SUSPECTED;
NOTREACHED() << "Invalid tethering state: " << tethering_state; NOTREACHED() << "Invalid tethering state: " << tethering_state;
return arc::mojom::TetheringClientState::NOT_DETECTED; return arc::mojom::TetheringClientState::NOT_DETECTED;
} }
arc::mojom::WiFiPtr TranslateONCWifi(const base::DictionaryValue* dict) {
auto wifi = arc::mojom::WiFi::New();
// Optional; defaults to 0.
dict->GetInteger(onc::wifi::kFrequency, &wifi->frequency);
wifi->bssid = GetString(dict, onc::wifi::kBSSID);
wifi->hex_ssid = GetString(dict, onc::wifi::kHexSSID);
// Optional; defaults to false.
dict->GetBoolean(onc::wifi::kHiddenSSID, &wifi->hidden_ssid);
wifi->security = TranslateONCWifiSecurityType(dict);
// Optional; defaults to 0.
dict->GetInteger(onc::wifi::kSignalStrength, &wifi->signal_strength);
return wifi;
}
// Extracts WiFi's tethering client state from a dictionary of WiFi properties.
arc::mojom::TetheringClientState GetWifiTetheringClientState(
const base::DictionaryValue* dict) {
std::string tethering_state;
dict->GetString(onc::wifi::kTetheringState, &tethering_state);
return TranslateTetheringState(tethering_state);
}
arc::mojom::ConnectionStateType TranslateONCConnectionState(
const base::DictionaryValue* dict) {
std::string connection_state =
GetString(dict, onc::network_config::kConnectionState);
if (connection_state == onc::connection_state::kConnected)
return arc::mojom::ConnectionStateType::CONNECTED;
if (connection_state == onc::connection_state::kConnecting)
return arc::mojom::ConnectionStateType::CONNECTING;
return arc::mojom::ConnectionStateType::NOT_CONNECTED;
}
// Translates a shill connection state into a mojo ConnectionStateType. // Translates a shill connection state into a mojo ConnectionStateType.
// This is effectively the inverse function of shill.Service::GetStateString // This is effectively the inverse function of shill.Service::GetStateString
// defined in platform2/shill/service.cc, with in addition some of shill's // defined in platform2/shill/service.cc, with in addition some of shill's
...@@ -176,30 +132,19 @@ bool IsActiveNetworkState(const chromeos::NetworkState* network) { ...@@ -176,30 +132,19 @@ bool IsActiveNetworkState(const chromeos::NetworkState* network) {
state == shill::kStatePortalSuspected; state == shill::kStatePortalSuspected;
} }
void TranslateONCNetworkTypeDetails(const base::DictionaryValue* dict, arc::mojom::NetworkType TranslateNetworkType(const std::string& type) {
arc::mojom::NetworkConfiguration* mojo) { if (type == shill::kTypeWifi)
std::string type = GetString(dict, onc::network_config::kType); return arc::mojom::NetworkType::WIFI;
// This property will be updated as required by the relevant network types if (type == shill::kTypeVPN)
// below. return arc::mojom::NetworkType::VPN;
mojo->tethering_client_state = arc::mojom::TetheringClientState::NOT_DETECTED; if (type == shill::kTypeEthernet)
if (type.empty()) { return arc::mojom::NetworkType::ETHERNET;
LOG(ERROR) << "Required network type property not found"; if (type == shill::kTypeEthernetEap)
} else if (type == onc::network_type::kCellular) { return arc::mojom::NetworkType::ETHERNET;
mojo->type = arc::mojom::NetworkType::CELLULAR; if (type == shill::kTypeCellular)
} else if (type == onc::network_type::kEthernet) { return arc::mojom::NetworkType::CELLULAR;
mojo->type = arc::mojom::NetworkType::ETHERNET; NOTREACHED() << "Unknown network type: " << type;
} else if (type == onc::network_type::kVPN) { return arc::mojom::NetworkType::ETHERNET;
mojo->type = arc::mojom::NetworkType::VPN;
} else if (type == onc::network_type::kWiFi) {
mojo->type = arc::mojom::NetworkType::WIFI;
const base::DictionaryValue* wifi_dict = nullptr;
dict->GetDictionary(onc::network_config::kWiFi, &wifi_dict);
DCHECK(wifi_dict);
mojo->wifi = TranslateONCWifi(wifi_dict);
mojo->tethering_client_state = GetWifiTetheringClientState(wifi_dict);
} else {
NOTREACHED() << "Unknown network type: " << type;
}
} }
// Parse a shill IPConfig dictionary and appends the resulting mojo // Parse a shill IPConfig dictionary and appends the resulting mojo
...@@ -273,17 +218,19 @@ void AddDeviceProperties(arc::mojom::NetworkConfiguration* network, ...@@ -273,17 +218,19 @@ void AddDeviceProperties(arc::mojom::NetworkConfiguration* network,
network->ip_configs = std::move(ip_configs); network->ip_configs = std::move(ip_configs);
} }
arc::mojom::NetworkConfigurationPtr TranslateONCConfiguration( arc::mojom::NetworkConfigurationPtr TranslateNetworkProperties(
const chromeos::NetworkState* network_state, const chromeos::NetworkState* network_state,
const base::Value* shill_dict, const base::Value* shill_dict) {
const base::DictionaryValue* onc_dict) {
auto mojo = arc::mojom::NetworkConfiguration::New(); auto mojo = arc::mojom::NetworkConfiguration::New();
mojo->connection_state =
mojo->connection_state = TranslateONCConnectionState(onc_dict); TranslateConnectionState(network_state->connection_state());
mojo->guid = network_state->guid();
mojo->guid = GetString(onc_dict, onc::network_config::kGUID);
if (mojo->guid.empty()) if (mojo->guid.empty())
LOG(ERROR) << "Missing GUID property for network " << network_state->path(); LOG(ERROR) << "Missing GUID property for network " << network_state->path();
mojo->type = TranslateNetworkType(network_state->type());
// TODO(b/156302252) Migrate to kMeteredProperty
mojo->tethering_client_state =
TranslateTetheringState(network_state->tethering_state());
// IP configuration data is added from the properties of the underlying shill // IP configuration data is added from the properties of the underlying shill
// Device and shill Service attached to the Device. Device properties are // Device and shill Service attached to the Device. Device properties are
...@@ -310,14 +257,20 @@ arc::mojom::NetworkConfigurationPtr TranslateONCConfiguration( ...@@ -310,14 +257,20 @@ arc::mojom::NetworkConfigurationPtr TranslateONCConfiguration(
} }
mojo->ip_configs = std::move(ip_configs); mojo->ip_configs = std::move(ip_configs);
TranslateONCNetworkTypeDetails(onc_dict, mojo.get()); if (mojo->type == arc::mojom::NetworkType::WIFI) {
mojo->wifi = arc::mojom::WiFi::New();
if (network_state) { mojo->wifi->bssid = network_state->bssid();
mojo->connection_state = mojo->wifi->hex_ssid = network_state->GetHexSsid();
TranslateConnectionState(network_state->connection_state()); mojo->wifi->security =
AddDeviceProperties(mojo.get(), network_state->device_path()); TranslateWiFiSecurity(network_state->security_class());
mojo->wifi->frequency = network_state->frequency();
mojo->wifi->hidden_ssid = shill_dict &&
shill_dict->FindBoolPath(shill::kWifiHiddenSsid).value_or(false);
mojo->wifi->signal_strength = network_state->signal_strength();
} }
AddDeviceProperties(mojo.get(), network_state->device_path());
return mojo; return mojo;
} }
...@@ -364,9 +317,7 @@ std::vector<arc::mojom::NetworkConfigurationPtr> TranslateNetworkStates( ...@@ -364,9 +317,7 @@ std::vector<arc::mojom::NetworkConfigurationPtr> TranslateNetworkStates(
const auto it = shill_network_properties.find(network_path); const auto it = shill_network_properties.find(network_path);
const auto* shill_dict = const auto* shill_dict =
(it != shill_network_properties.end()) ? &it->second : nullptr; (it != shill_network_properties.end()) ? &it->second : nullptr;
const auto onc_dict = auto network = TranslateNetworkProperties(state, shill_dict);
chromeos::network_util::TranslateNetworkStateToONC(state);
auto network = TranslateONCConfiguration(state, shill_dict, onc_dict.get());
network->is_default_network = state == GetStateHandler()->DefaultNetwork(); network->is_default_network = state == GetStateHandler()->DefaultNetwork();
network->service_name = network_path; network->service_name = network_path;
networks.push_back(std::move(network)); networks.push_back(std::move(network));
......
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