Skip to content

Commit

Permalink
Add MacAddress to ONC and networkingPrivate
Browse files Browse the repository at this point in the history
BUG=373965
For fake_wifi_service.cc

R=armansito@chromium.org, pneubeck@chromium.org
TBR=mef@chromium.org

Review URL: https://codereview.chromium.org/285233008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271679 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
stevenjb@chromium.org committed May 20, 2014
1 parent dc27479 commit fad51d5
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ class ExtensionNetworkingPrivateApiTest
ipconfig.SetStringWithoutPathExpansion(shill::kAddressProperty, "0.0.0.0");
ipconfig.SetStringWithoutPathExpansion(shill::kGatewayProperty, "0.0.0.1");
ipconfig.SetIntegerWithoutPathExpansion(shill::kPrefixlenProperty, 0);
ipconfig.SetStringWithoutPathExpansion(
shill::kMethodProperty, shill::kTypeIPv4);
ipconfig.SetStringWithoutPathExpansion(shill::kMethodProperty,
shill::kTypeIPv4);
ip_config_test->AddIPConfig(kIPConfigPath, ipconfig);

// Add Devices
Expand All @@ -255,6 +255,9 @@ class ExtensionNetworkingPrivateApiTest
wifi_ip_configs.AppendString(kIPConfigPath);
device_test->SetDeviceProperty(
kWifiDevicePath, shill::kIPConfigsProperty, wifi_ip_configs);
device_test->SetDeviceProperty(kWifiDevicePath,
shill::kAddressProperty,
base::StringValue("001122aabbcc"));
device_test->AddDevice(
kCellularDevicePath, shill::kTypeCellular, "stub_cellular_device1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1524,8 +1524,11 @@ void InternetOptionsHandler::PopulateDictionaryDetailsCallback(
// Device hardware address
const DeviceState* device = NetworkHandler::Get()->network_state_handler()->
GetDeviceState(network->device_path());
if (device)
dictionary.SetString(kTagHardwareAddress, device->GetFormattedMacAddress());
if (device) {
dictionary.SetString(
kTagHardwareAddress,
network_util::FormattedMacAddress(device->mac_address()));
}

// IP config
scoped_ptr<base::DictionaryValue> ipconfig_dhcp(new base::DictionaryValue);
Expand Down
1 change: 1 addition & 0 deletions chrome/test/data/extensions/api_test/networking/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ var availableTests = [
assertEq({ "Connectable": true,
"ConnectionState": "Connected",
"GUID": "stub_wifi1",
"MacAddress": "00:11:22:AA:BB:CC",
"IPConfigs": [{
"Gateway": "0.0.0.1",
"IPAddress": "0.0.0.0",
Expand Down
6 changes: 6 additions & 0 deletions chromeos/dbus/fake_shill_manager_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,9 @@ void FakeShillManagerClient::SetupDefaultEnvironment() {
AddTechnology(shill::kTypeEthernet, enabled);
devices->AddDevice(
"/device/eth1", shill::kTypeEthernet, "stub_eth_device1");
devices->SetDeviceProperty("/device/eth1",
shill::kAddressProperty,
base::StringValue("0123456789ab"));
base::ListValue eth_ip_configs;
eth_ip_configs.AppendString("ipconfig_v4_path");
eth_ip_configs.AppendString("ipconfig_v6_path");
Expand All @@ -634,6 +637,9 @@ void FakeShillManagerClient::SetupDefaultEnvironment() {
}
AddTechnology(shill::kTypeWifi, enabled);
devices->AddDevice("/device/wifi1", shill::kTypeWifi, "stub_wifi_device1");
devices->SetDeviceProperty("/device/wifi1",
shill::kAddressProperty,
base::StringValue("23456789abc"));
base::ListValue wifi_ip_configs;
wifi_ip_configs.AppendString("ipconfig_v4_path");
wifi_ip_configs.AppendString("ipconfig_v6_path");
Expand Down
12 changes: 0 additions & 12 deletions chromeos/network/device_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,6 @@ void DeviceState::IPConfigPropertiesChanged(
ip_config->MergeDictionary(&properties);
}

std::string DeviceState::GetFormattedMacAddress() const {
if (mac_address_.size() % 2 != 0)
return mac_address_;
std::string result;
for (size_t i = 0; i < mac_address_.size(); ++i) {
if ((i != 0) && (i % 2 == 0))
result.push_back(':');
result.push_back(mac_address_[i]);
}
return result;
}

bool DeviceState::IsSimAbsent() const {
return technology_family_ == shill::kTechnologyFamilyGsm && !sim_present_;
}
Expand Down
3 changes: 0 additions & 3 deletions chromeos/network/device_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class CHROMEOS_EXPORT DeviceState : public ManagedState {
// Accessors
const std::string& mac_address() const { return mac_address_; }

// Returns |mac_address_| in aa:bb format.
std::string GetFormattedMacAddress() const;

// Cellular specific accessors
const std::string& home_provider_id() const { return home_provider_id_; }
bool allow_roaming() const { return allow_roaming_; }
Expand Down
19 changes: 13 additions & 6 deletions chromeos/network/managed_network_configuration_handler_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ void ManagedNetworkConfigurationHandlerImpl::GetManagedPropertiesCallback(

scoped_ptr<base::DictionaryValue> properties_copy(
shill_properties.DeepCopy());
// Add the IPConfigs to the dictionary before the ONC translation.
GetIPConfigs(service_path, properties_copy.get());
// Add associated Device properties before the ONC translation.
GetDeviceProperties(service_path, properties_copy.get());

scoped_ptr<base::DictionaryValue> active_settings(
onc::TranslateShillServiceToONCPart(
Expand Down Expand Up @@ -223,8 +223,8 @@ void ManagedNetworkConfigurationHandlerImpl::GetPropertiesCallback(
const base::DictionaryValue& shill_properties) {
scoped_ptr<base::DictionaryValue> properties_copy(
shill_properties.DeepCopy());
// Add the IPConfigs to the dictionary before the ONC translation.
GetIPConfigs(service_path, properties_copy.get());
// Add associated Device properties before the ONC translation.
GetDeviceProperties(service_path, properties_copy.get());

scoped_ptr<base::DictionaryValue> onc_network(
onc::TranslateShillServiceToONCPart(
Expand Down Expand Up @@ -613,7 +613,7 @@ void ManagedNetworkConfigurationHandlerImpl::OnPolicyAppliedToNetwork(
NetworkPolicyObserver, observers_, PolicyApplied(service_path));
}

void ManagedNetworkConfigurationHandlerImpl::GetIPConfigs(
void ManagedNetworkConfigurationHandlerImpl::GetDeviceProperties(
const std::string& service_path,
base::DictionaryValue* properties) {
std::string connection_state;
Expand All @@ -631,9 +631,16 @@ void ManagedNetworkConfigurationHandlerImpl::GetIPConfigs(
const DeviceState* device_state =
network_state_handler_->GetDeviceState(device);
if (!device_state) {
NET_LOG_ERROR("GetIPConfigs: no device: " + device, service_path);
NET_LOG_ERROR("GetDeviceProperties: no device: " + device, service_path);
return;
}

// Get the hardware MAC address from the DeviceState.
if (!device_state->mac_address().empty()) {
properties->SetStringWithoutPathExpansion(
shill::kAddressProperty, device_state->mac_address());
}

// Convert IPConfig dictionary to a ListValue.
base::ListValue* ip_configs = new base::ListValue;
for (base::DictionaryValue::Iterator iter(device_state->ip_configs());
Expand Down
7 changes: 3 additions & 4 deletions chromeos/network/managed_network_configuration_handler_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,9 @@ class CHROMEOS_EXPORT ManagedNetworkConfigurationHandlerImpl

void OnPolicyAppliedToNetwork(const std::string& service_path);

// Helper method to append "IPConfigs" property to |properties| by extracting
// them from the associated DeviceState.
void GetIPConfigs(const std::string& service_path,
base::DictionaryValue* properties);
// Helper method to append associated Device properties to |properties|.
void GetDeviceProperties(const std::string& service_path,
base::DictionaryValue* properties);

// If present, the empty string maps to the device policy.
UserToPoliciesMap policies_by_user_;
Expand Down
2 changes: 1 addition & 1 deletion chromeos/network/network_state_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ std::string NetworkStateHandler::FormattedHardwareAddressForType(
device = GetDeviceStateByType(type);
if (!device)
return std::string();
return device->GetFormattedMacAddress();
return network_util::FormattedMacAddress(device->mac_address());
}

void NetworkStateHandler::GetNetworkList(NetworkStateList* list) const {
Expand Down
13 changes: 13 additions & 0 deletions chromeos/network/network_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "chromeos/network/network_util.h"

#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chromeos/network/favorite_state.h"
#include "chromeos/network/network_state.h"
Expand Down Expand Up @@ -99,6 +100,18 @@ int32 NetmaskToPrefixLength(const std::string& netmask) {
return prefix_length;
}

std::string FormattedMacAddress(const std::string& shill_mac_address) {
if (shill_mac_address.size() % 2 != 0)
return shill_mac_address;
std::string result;
for (size_t i = 0; i < shill_mac_address.size(); ++i) {
if ((i != 0) && (i % 2 == 0))
result.push_back(':');
result.push_back(base::ToUpperASCII(shill_mac_address[i]));
}
return result;
}

bool ParseCellularScanResults(const base::ListValue& list,
std::vector<CellularScanResult>* scan_results) {
scan_results->clear();
Expand Down
4 changes: 4 additions & 0 deletions chromeos/network/network_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ CHROMEOS_EXPORT std::string PrefixLengthToNetmask(int32 prefix_length);
// e.g. a |netmask| of 255.255.255.0 is converted to a prefixlen of 24
CHROMEOS_EXPORT int32 NetmaskToPrefixLength(const std::string& netmask);

// Returns |shill_mac_address| in aa:bb format.
CHROMEOS_EXPORT std::string FormattedMacAddress(
const std::string& shill_mac_address);

// Parses |list|, which contains DictionaryValues and returns a vector of
// CellularScanResult in |scan_results|. Returns false if parsing fails,
// in which case the contents of |scan_results| will be undefined.
Expand Down
1 change: 1 addition & 0 deletions chromeos/network/onc/onc_signature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ const OncFieldSignature network_with_state_fields[] = {
{ ::onc::network_config::kConnectionState, &kStringSignature},
{ ::onc::network_config::kConnectable, &kBoolSignature},
{ ::onc::network_config::kErrorState, &kStringSignature},
{ ::onc::network_config::kMacAddress, &kStringSignature},
{ ::onc::network_config::kWiFi, &kWiFiWithStateSignature},
{NULL}};

Expand Down
18 changes: 10 additions & 8 deletions chromeos/network/onc/onc_translation_tables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,22 @@ const FieldTranslationEntry cellular_fields[] = {
{NULL}};

const FieldTranslationEntry network_fields[] = {
{ ::onc::network_config::kGUID, shill::kGuidProperty},
{ ::onc::network_config::kConnectable, shill::kConnectableProperty },
{ ::onc::network_config::kErrorState, shill::kErrorProperty },

// Shill doesn't allow setting the name for non-VPN networks.
// This field is conditionally translated, see onc_translator_*.
// Name is conditionally translated, see onc_translator_*.
// { ::onc::network_config::kName, shill::kNameProperty },
{ ::onc::network_config::kGUID, shill::kGuidProperty},
// This field is converted during translation, see onc_translator_*.

// Type is converted during translation, see onc_translator_*.
// { ::onc::network_config::kType, shill::kTypeProperty },

// This field is converted during translation, see
// onc_translator_shill_to_onc.cc. It is only converted when going from
// These fields are converted during translation, see
// onc_translator_shill_to_onc.cc. They are only converted when going from
// Shill->ONC, and ignored otherwise.
// { ::onc::network_config::kConnectionState, shill::kStateProperty },

{ ::onc::network_config::kConnectable, shill::kConnectableProperty },
{ ::onc::network_config::kErrorState, shill::kErrorProperty },
// { ::onc::network_config::kMacAddress, shill::kAddressProperty },
{NULL}};

const FieldTranslationEntry ipconfig_fields[] = {
Expand Down
17 changes: 15 additions & 2 deletions chromeos/network/onc/onc_translator_shill_to_onc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/logging.h"
#include "base/values.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_util.h"
#include "chromeos/network/onc/onc_signature.h"
#include "chromeos/network/onc/onc_translation_tables.h"
#include "chromeos/network/shill_property_util.h"
Expand Down Expand Up @@ -290,6 +291,7 @@ void ShillToONCTranslator::TranslateNetworkWithState() {
onc_object_->SetStringWithoutPathExpansion(::onc::network_config::kName,
name);

// Limit ONC state to "NotConnected", "Connected", or "Connecting".
std::string state;
if (shill_dictionary_->GetStringWithoutPathExpansion(shill::kStateProperty,
&state)) {
Expand All @@ -303,8 +305,19 @@ void ShillToONCTranslator::TranslateNetworkWithState() {
::onc::network_config::kConnectionState, onc_state);
}

// Shill's Service has an IPConfig property (note the singular, and not a
// IPConfigs property). However, we require the caller of the translation to
// Use a human-readable aa:bb format for any hardware MAC address. Note:
// this property is provided by the caller but is not part of the Shill
// Service properties (it is copied from the Device properties).
std::string address;
if (shill_dictionary_->GetStringWithoutPathExpansion(shill::kAddressProperty,
&address)) {
onc_object_->SetStringWithoutPathExpansion(
::onc::network_config::kMacAddress,
network_util::FormattedMacAddress(address));
}

// Shill's Service has an IPConfig property (note the singular), not an
// IPConfigs property. However, we require the caller of the translation to
// patch the Shill dictionary before passing it to the translator.
const base::ListValue* shill_ipconfigs = NULL;
if (shill_dictionary_->GetListWithoutPathExpansion(shill::kIPConfigsProperty,
Expand Down
10 changes: 10 additions & 0 deletions components/onc/docs/onc_spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,16 @@ <h1>Network Configuration</h1>
in an error state.
</dd>

<dt class="field">MacAddress</dt>
<dd>
<span class="field_meta">
(optional, read-only)
<span class="type">string</span>
</span>
The MAC address for the network. Only applies to connected non-virtual
networks. The format is 00:11:22:AA:BB:CC.
</dd>

</dl>

<section>
Expand Down
1 change: 1 addition & 0 deletions components/onc/onc_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const char kCellular[] = "Cellular";
const char kEthernet[] = "Ethernet";
const char kGUID[] = "GUID";
const char kIPConfigs[] = "IPConfigs";
const char kMacAddress[] = "MacAddress";
const char kName[] = "Name";
const char kNameServers[] = "NameServers";
const char kProxySettings[] = "ProxySettings";
Expand Down
1 change: 1 addition & 0 deletions components/onc/onc_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ONC_EXPORT extern const char kCellular[];
ONC_EXPORT extern const char kEthernet[];
ONC_EXPORT extern const char kGUID[];
ONC_EXPORT extern const char kIPConfigs[];
ONC_EXPORT extern const char kMacAddress[];
ONC_EXPORT extern const char kName[];
ONC_EXPORT extern const char kNameServers[];
ONC_EXPORT extern const char kProxySettings[];
Expand Down
1 change: 1 addition & 0 deletions components/wifi/fake_wifi_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ FakeWiFiService::FakeWiFiService() {
network_properties.signal_strength = 40;
network_properties.json_extra =
"{"
" \"MacAddress\": \"00:11:22:AA:BB:CC\","
" \"IPConfigs\": [{"
" \"Gateway\": \"0.0.0.1\","
" \"IPAddress\": \"0.0.0.0\","
Expand Down

0 comments on commit fad51d5

Please sign in to comment.