Commit 13be9e9a authored by pneubeck@chromium.org's avatar pneubeck@chromium.org

Move Shill property utility functions to a separate file.

This CL moves some functions to handle Shill properties to shill_property_util.*

This cleans up some dependencies (e.g. network_connection_handler.cc doesn't include managed_network_configuration_handler.h anymore) and allows to share some more code.

GetUIData was adapted slightly to make use of GetUIDataFromValue.

CopyIdentifyingProperties deviates from the original implementation in network_connection_handler (e.g. it didn't copy the SSID). This function will be used in ManagedNetConfHandler for the new "AllowAutoconnect" policy.

BUG=280146

Review URL: https://chromiumcodereview.appspot.com/23551004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221260 0039d316-1c4b-4281-b951-d872f2087c98
parent 6a427e8e
......@@ -16,6 +16,7 @@
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/shill_property_util.h"
#include "grit/ash_resources.h"
#include "grit/ash_strings.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
......@@ -243,7 +244,8 @@ void NetworkStateNotifier::ShowConnectErrorNotification(
service_path);
std::string network_name =
NetworkState::GetNameFromProperties(service_path, shill_properties);
chromeos::shill_property_util::GetNameFromProperties(service_path,
shill_properties);
std::string network_error_details;
shill_properties.GetStringWithoutPathExpansion(
shill::kErrorDetailsProperty, &network_error_details);
......
......@@ -294,6 +294,8 @@
'network/onc/onc_validator.h',
'network/shill_property_handler.cc',
'network/shill_property_handler.h',
'network/shill_property_util.cc',
'network/shill_property_util.h',
'network/sms_watcher.cc',
'network/sms_watcher.h',
'network/dhcp_proxy_script_fetcher_chromeos.cc',
......
......@@ -10,6 +10,7 @@
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_profile_handler.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/shill_property_util.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
......@@ -28,10 +29,13 @@ bool FavoriteState::PropertyChanged(const std::string& key,
if (key == flimflam::kProfileProperty) {
return GetStringValue(key, value, &profile_path_);
} else if (key == flimflam::kUIDataProperty) {
if (!NetworkState::GetUIDataFromValue(value, &ui_data_)) {
scoped_ptr<NetworkUIData> new_ui_data =
shill_property_util::GetUIDataFromValue(value);
if (!new_ui_data) {
NET_LOG_ERROR("Failed to parse " + key, path());
return false;
}
ui_data_ = *new_ui_data;
return true;
}
return false;
......
......@@ -14,22 +14,4 @@ namespace chromeos {
ManagedNetworkConfigurationHandler::~ManagedNetworkConfigurationHandler() {}
// static
scoped_ptr<NetworkUIData> ManagedNetworkConfigurationHandler::GetUIData(
const base::DictionaryValue& shill_dictionary) {
std::string ui_data_blob;
if (shill_dictionary.GetStringWithoutPathExpansion(flimflam::kUIDataProperty,
&ui_data_blob) &&
!ui_data_blob.empty()) {
scoped_ptr<base::DictionaryValue> ui_data_dict =
onc::ReadDictionaryFromJson(ui_data_blob);
if (ui_data_dict)
return make_scoped_ptr(new NetworkUIData(*ui_data_dict));
else
LOG(ERROR) << "UIData is not a valid JSON dictionary.";
}
VLOG(2) << "JSON dictionary has no UIData blob: " << shill_dictionary;
return scoped_ptr<NetworkUIData>();
}
} // namespace chromeos
......@@ -10,7 +10,6 @@
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/network/network_handler.h"
......@@ -25,7 +24,6 @@ class ListValue;
namespace chromeos {
class NetworkPolicyObserver;
class NetworkUIData;
// The ManagedNetworkConfigurationHandler class is used to create and configure
// networks in ChromeOS using ONC and takes care of network policies.
......@@ -55,12 +53,6 @@ class CHROMEOS_EXPORT ManagedNetworkConfigurationHandler {
public:
virtual ~ManagedNetworkConfigurationHandler();
// Returns the NetworkUIData parsed from the UIData property of
// |shill_dictionary|. If parsing fails or the field doesn't exist, returns
// NULL.
static scoped_ptr<NetworkUIData> GetUIData(
const base::DictionaryValue& shill_dictionary);
virtual void AddObserver(NetworkPolicyObserver* observer) = 0;
virtual void RemoveObserver(NetworkPolicyObserver* observer) = 0;
......
......@@ -9,7 +9,6 @@
#include "base/bind.h"
#include "base/guid.h"
#include "base/json/json_writer.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
......@@ -36,6 +35,7 @@
#include "chromeos/network/onc/onc_translator.h"
#include "chromeos/network/onc/onc_utils.h"
#include "chromeos/network/onc/onc_validator.h"
#include "chromeos/network/shill_property_util.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
......@@ -85,18 +85,6 @@ void RunErrorCallback(const std::string& service_path,
error_message)));
}
// Sets the UIData property in |shill_dictionary| to the serialization of
// |ui_data|.
void SetUIData(const NetworkUIData& ui_data,
base::DictionaryValue* shill_dictionary) {
base::DictionaryValue ui_data_dict;
ui_data.FillDictionary(&ui_data_dict);
std::string ui_data_blob;
base::JSONWriter::Write(&ui_data_dict, &ui_data_blob);
shill_dictionary->SetStringWithoutPathExpansion(flimflam::kUIDataProperty,
ui_data_blob);
}
void LogErrorWithDict(const tracked_objects::Location& from_where,
const std::string& error_name,
scoped_ptr<base::DictionaryValue> error_data) {
......@@ -227,7 +215,7 @@ scoped_ptr<base::DictionaryValue> CreateShillConfiguration(
ui_data->set_user_settings(sanitized_settings.Pass());
}
SetUIData(*ui_data, shill_dictionary.get());
shill_property_util::SetUIData(*ui_data, shill_dictionary.get());
VLOG(2) << "Created Shill properties: " << *shill_dictionary;
......@@ -414,7 +402,8 @@ void ManagedNetworkConfigurationHandlerImpl::GetManagedPropertiesCallback(
<< service_path << ".";
}
scoped_ptr<NetworkUIData> ui_data = GetUIData(shill_properties);
scoped_ptr<NetworkUIData> ui_data =
shill_property_util::GetUIDataFromProperties(shill_properties);
const base::DictionaryValue* user_settings = NULL;
const base::DictionaryValue* shared_settings = NULL;
......@@ -886,7 +875,8 @@ void ManagedNetworkConfigurationHandlerImpl::PolicyApplicator::GetEntryCallback(
// unmanaged.
}
scoped_ptr<NetworkUIData> ui_data = GetUIData(entry_properties);
scoped_ptr<NetworkUIData> ui_data =
shill_property_util::GetUIDataFromProperties(entry_properties);
if (!ui_data) {
VLOG(1) << "Entry " << entry << " of profile " << profile_.ToDebugString()
<< " contains no or no valid UIData.";
......
......@@ -21,8 +21,8 @@
#include "chromeos/dbus/shill_profile_client.h"
#include "chromeos/dbus/shill_service_client.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/shill_property_util.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
......@@ -57,8 +57,8 @@ void GetPropertiesCallback(
const base::DictionaryValue& properties) {
// Get the correct name from WifiHex if necessary.
scoped_ptr<base::DictionaryValue> properties_copy(properties.DeepCopy());
std::string name = NetworkState::GetNameFromProperties(
service_path, properties);
std::string name =
shill_property_util::GetNameFromProperties(service_path, properties);
if (!name.empty()) {
properties_copy->SetStringWithoutPathExpansion(
flimflam::kNameProperty, name);
......
......@@ -12,13 +12,13 @@
#include "chromeos/dbus/shill_manager_client.h"
#include "chromeos/dbus/shill_service_client.h"
#include "chromeos/network/client_cert_util.h"
#include "chromeos/network/managed_network_configuration_handler.h"
#include "chromeos/network/network_configuration_handler.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_handler_callbacks.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_ui_data.h"
#include "chromeos/network/shill_property_util.h"
#include "dbus/object_path.h"
#include "net/cert/x509_certificate.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
......@@ -47,14 +47,6 @@ bool IsAuthenticationError(const std::string& error) {
error == shill::kErrorEapAuthenticationFailed);
}
void CopyStringFromDictionary(const base::DictionaryValue& source,
const std::string& key,
base::DictionaryValue* dest) {
std::string string_value;
if (source.GetStringWithoutPathExpansion(key, &string_value))
dest->SetStringWithoutPathExpansion(key, string_value);
}
bool NetworkRequiresActivation(const NetworkState* network) {
return (network->type() == flimflam::kTypeCellular &&
((network->activation_state() != flimflam::kActivationStateActivated &&
......@@ -411,7 +403,7 @@ void NetworkConnectionHandler::VerifyConfiguredAndConnect(
// Note: Wifi/VPNConfigView set these properties explicitly, in which case
// only the TPM must be configured.
scoped_ptr<NetworkUIData> ui_data =
ManagedNetworkConfigurationHandler::GetUIData(service_properties);
shill_property_util::GetUIDataFromProperties(service_properties);
if (ui_data && ui_data->certificate_type() == CLIENT_CERT_TYPE_PATTERN) {
// User must be logged in to connect to a network requiring a certificate.
if (!logged_in_ || !cert_loader_) {
......@@ -456,32 +448,21 @@ void NetworkConnectionHandler::VerifyConfiguredAndConnect(
if (!config_properties.empty()) {
NET_LOG_EVENT("Configuring Network", service_path);
// Set configuration properties required by Shill to identify the network.
config_properties.SetStringWithoutPathExpansion(
flimflam::kTypeProperty, type);
CopyStringFromDictionary(service_properties, flimflam::kNameProperty,
&config_properties);
CopyStringFromDictionary(service_properties, flimflam::kGuidProperty,
&config_properties);
if (type == flimflam::kTypeVPN) {
config_properties.SetStringWithoutPathExpansion(
flimflam::kProviderTypeProperty, vpn_provider_type);
config_properties.SetStringWithoutPathExpansion(
flimflam::kProviderHostProperty, vpn_provider_host);
} else if (type == flimflam::kTypeWifi) {
config_properties.SetStringWithoutPathExpansion(
flimflam::kSecurityProperty, security);
if (shill_property_util::CopyIdentifyingProperties(service_properties,
&config_properties)) {
network_configuration_handler_->SetProperties(
service_path,
config_properties,
base::Bind(&NetworkConnectionHandler::CallShillConnect,
AsWeakPtr(),
service_path),
base::Bind(&NetworkConnectionHandler::HandleConfigurationFailure,
AsWeakPtr(),
service_path));
return;
}
network_configuration_handler_->SetProperties(
service_path,
config_properties,
base::Bind(&NetworkConnectionHandler::CallShillConnect,
AsWeakPtr(), service_path),
base::Bind(&NetworkConnectionHandler::HandleConfigurationFailure,
AsWeakPtr(), service_path));
return;
NET_LOG_ERROR("Shill dictionary is missing some relevant entries",
service_path);
}
// Otherwise, we probably still need to configure the network since
......
......@@ -4,17 +4,12 @@
#include "chromeos/network/network_state.h"
#include "base/i18n/icu_encoding_detection.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/json/json_writer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_profile_handler.h"
#include "chromeos/network/network_util.h"
#include "chromeos/network/onc/onc_utils.h"
#include "chromeos/network/shill_property_util.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace {
......@@ -32,26 +27,6 @@ bool ConvertListValueToStringVector(const base::ListValue& string_list,
return true;
}
// Replace non UTF8 characters in |str| with a replacement character.
std::string ValidateUTF8(const std::string& str) {
std::string result;
for (int32 index = 0; index < static_cast<int32>(str.size()); ++index) {
uint32 code_point_out;
bool is_unicode_char = base::ReadUnicodeCharacter(str.c_str(), str.size(),
&index, &code_point_out);
const uint32 kFirstNonControlChar = 0x20;
if (is_unicode_char && (code_point_out >= kFirstNonControlChar)) {
base::WriteUnicodeCharacter(code_point_out, &result);
} else {
const uint32 kReplacementChar = 0xFFFD;
// Puts kReplacementChar if character is a control character [0,0x20)
// or is not readable UTF8.
base::WriteUnicodeCharacter(kReplacementChar, &result);
}
}
return result;
}
bool IsCaCertNssSet(const base::DictionaryValue& properties) {
std::string ca_cert_nss;
if (properties.GetStringWithoutPathExpansion(flimflam::kEapCaCertNssProperty,
......@@ -184,10 +159,13 @@ bool NetworkState::PropertyChanged(const std::string& key,
}
return true;
} else if (key == flimflam::kUIDataProperty) {
if (!GetUIDataFromValue(value, &ui_data_)) {
scoped_ptr<NetworkUIData> new_ui_data =
shill_property_util::GetUIDataFromValue(value);
if (!new_ui_data) {
NET_LOG_ERROR("Failed to parse " + key, path());
return false;
}
ui_data_ = *new_ui_data;
return true;
} else if (key == flimflam::kNetworkTechnologyProperty) {
return GetStringValue(key, value, &network_technology_);
......@@ -339,7 +317,8 @@ std::string NetworkState::GetNetmask() const {
}
bool NetworkState::UpdateName(const base::DictionaryValue& properties) {
std::string updated_name = GetNameFromProperties(path(), properties);
std::string updated_name =
shill_property_util::GetNameFromProperties(path(), properties);
if (updated_name != name()) {
set_name(updated_name);
return true;
......@@ -347,80 +326,6 @@ bool NetworkState::UpdateName(const base::DictionaryValue& properties) {
return false;
}
// static
std::string NetworkState::GetNameFromProperties(
const std::string& service_path,
const base::DictionaryValue& properties) {
std::string name, hex_ssid;
properties.GetStringWithoutPathExpansion(flimflam::kNameProperty, &name);
properties.GetStringWithoutPathExpansion(flimflam::kWifiHexSsid, &hex_ssid);
if (hex_ssid.empty()) {
if (name.empty())
return name;
// Validate name for UTF8.
std::string valid_ssid = ValidateUTF8(name);
if (valid_ssid != name) {
NET_LOG_DEBUG("GetNameFromProperties", base::StringPrintf(
"%s: UTF8: %s", service_path.c_str(), valid_ssid.c_str()));
}
return valid_ssid;
}
std::string ssid;
std::vector<uint8> raw_ssid_bytes;
if (base::HexStringToBytes(hex_ssid, &raw_ssid_bytes)) {
ssid = std::string(raw_ssid_bytes.begin(), raw_ssid_bytes.end());
NET_LOG_DEBUG("GetNameFromProperties", base::StringPrintf(
"%s: %s, SSID: %s", service_path.c_str(),
hex_ssid.c_str(), ssid.c_str()));
} else {
NET_LOG_ERROR("GetNameFromProperties",
base::StringPrintf("%s: Error processing: %s",
service_path.c_str(), hex_ssid.c_str()));
return name;
}
if (IsStringUTF8(ssid)) {
if (ssid != name) {
NET_LOG_DEBUG("GetNameFromProperties", base::StringPrintf(
"%s: UTF8: %s", service_path.c_str(), ssid.c_str()));
}
return ssid;
}
// Detect encoding and convert to UTF-8.
std::string country_code;
properties.GetStringWithoutPathExpansion(
flimflam::kCountryProperty, &country_code);
std::string encoding;
if (!base::DetectEncoding(ssid, &encoding)) {
// TODO(stevenjb): This is currently experimental. If we find a case where
// base::DetectEncoding() fails, we need to figure out whether we can use
// country_code with ConvertToUtf8(). crbug.com/233267.
encoding = country_code;
}
if (!encoding.empty()) {
std::string utf8_ssid;
if (base::ConvertToUtf8AndNormalize(ssid, encoding, &utf8_ssid)) {
if (utf8_ssid != name) {
NET_LOG_DEBUG("GetNameFromProperties", base::StringPrintf(
"%s: Encoding=%s: %s", service_path.c_str(),
encoding.c_str(), utf8_ssid.c_str()));
}
return utf8_ssid;
}
}
// Unrecognized encoding. Only use raw bytes if name_ is empty.
NET_LOG_DEBUG("GetNameFromProperties", base::StringPrintf(
"%s: Unrecognized Encoding=%s: %s", service_path.c_str(),
encoding.c_str(), ssid.c_str()));
if (name.empty() && !ssid.empty())
return ssid;
return name;
}
// static
bool NetworkState::StateIsConnected(const std::string& connection_state) {
return (connection_state == flimflam::kStateReady ||
......@@ -440,22 +345,4 @@ std::string NetworkState::IPConfigProperty(const char* key) {
return base::StringPrintf("%s.%s", shill::kIPConfigProperty, key);
}
// static
bool NetworkState::GetUIDataFromValue(const base::Value& ui_data_value,
NetworkUIData* out) {
std::string ui_data_str;
if (!ui_data_value.GetAsString(&ui_data_str))
return false;
if (ui_data_str.empty()) {
*out = NetworkUIData();
return true;
}
scoped_ptr<base::DictionaryValue> ui_data_dict(
chromeos::onc::ReadDictionaryFromJson(ui_data_str));
if (!ui_data_dict)
return false;
*out = NetworkUIData(*ui_data_dict);
return true;
}
} // namespace chromeos
......@@ -105,15 +105,6 @@ class CHROMEOS_EXPORT NetworkState : public ManagedState {
// key.
static std::string IPConfigProperty(const char* key);
// Sets |out| to the UIData specified by |value|. Returns true if successfully
// parsed.
static bool GetUIDataFromValue(const base::Value& value, NetworkUIData* out);
// Generates a name from properties."Wifi.HexSSID" if present, otherwise
// validates properties.Name and returns a valid utf8 version.
static std::string GetNameFromProperties(
const std::string& service_path,
const base::DictionaryValue& properties);
private:
friend class MobileActivatorTest;
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/network/shill_property_util.h"
#include "base/i18n/icu_encoding_detection.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/json/json_writer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "base/values.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_ui_data.h"
#include "chromeos/network/onc/onc_utils.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
namespace shill_property_util {
namespace {
// Replace non UTF8 characters in |str| with a replacement character.
std::string ValidateUTF8(const std::string& str) {
std::string result;
for (int32 index = 0; index < static_cast<int32>(str.size()); ++index) {
uint32 code_point_out;
bool is_unicode_char = base::ReadUnicodeCharacter(
str.c_str(), str.size(), &index, &code_point_out);
const uint32 kFirstNonControlChar = 0x20;
if (is_unicode_char && (code_point_out >= kFirstNonControlChar)) {
base::WriteUnicodeCharacter(code_point_out, &result);
} else {
const uint32 kReplacementChar = 0xFFFD;
// Puts kReplacementChar if character is a control character [0,0x20)
// or is not readable UTF8.
base::WriteUnicodeCharacter(kReplacementChar, &result);
}
}
return result;
}
void CopyStringFromDictionary(const base::DictionaryValue& source,
const std::string& key,
base::DictionaryValue* dest) {
std::string string_value;
if (source.GetStringWithoutPathExpansion(key, &string_value))
dest->SetStringWithoutPathExpansion(key, string_value);
}
} // namespace
std::string GetNameFromProperties(const std::string& service_path,
const base::DictionaryValue& properties) {
std::string name, hex_ssid;
properties.GetStringWithoutPathExpansion(flimflam::kNameProperty, &name);
properties.GetStringWithoutPathExpansion(flimflam::kWifiHexSsid, &hex_ssid);
if (hex_ssid.empty()) {
if (name.empty())
return name;
// Validate name for UTF8.
std::string valid_ssid = ValidateUTF8(name);
if (valid_ssid != name) {
NET_LOG_DEBUG(
"GetNameFromProperties",
base::StringPrintf(
"%s: UTF8: %s", service_path.c_str(), valid_ssid.c_str()));
}
return valid_ssid;
}
std::string ssid;
std::vector<uint8> raw_ssid_bytes;
if (base::HexStringToBytes(hex_ssid, &raw_ssid_bytes)) {
ssid = std::string(raw_ssid_bytes.begin(), raw_ssid_bytes.end());
NET_LOG_DEBUG("GetNameFromProperties",
base::StringPrintf("%s: %s, SSID: %s",
service_path.c_str(),
hex_ssid.c_str(),
ssid.c_str()));
} else {
NET_LOG_ERROR("GetNameFromProperties",
base::StringPrintf("%s: Error processing: %s",
service_path.c_str(),
hex_ssid.c_str()));
return name;
}
if (IsStringUTF8(ssid)) {
if (ssid != name) {
NET_LOG_DEBUG("GetNameFromProperties",
base::StringPrintf(
"%s: UTF8: %s", service_path.c_str(), ssid.c_str()));
}
return ssid;
}
// Detect encoding and convert to UTF-8.
std::string country_code;
properties.GetStringWithoutPathExpansion(flimflam::kCountryProperty,
&country_code);
std::string encoding;
if (!base::DetectEncoding(ssid, &encoding)) {
// TODO(stevenjb): This is currently experimental. If we find a case where
// base::DetectEncoding() fails, we need to figure out whether we can use
// country_code with ConvertToUtf8(). crbug.com/233267.
encoding = country_code;
}
if (!encoding.empty()) {
std::string utf8_ssid;
if (base::ConvertToUtf8AndNormalize(ssid, encoding, &utf8_ssid)) {
if (utf8_ssid != name) {
NET_LOG_DEBUG("GetNameFromProperties",
base::StringPrintf("%s: Encoding=%s: %s",
service_path.c_str(),
encoding.c_str(),
utf8_ssid.c_str()));
}
return utf8_ssid;
}
}
// Unrecognized encoding. Only use raw bytes if name_ is empty.
NET_LOG_DEBUG("GetNameFromProperties",
base::StringPrintf("%s: Unrecognized Encoding=%s: %s",
service_path.c_str(),
encoding.c_str(),
ssid.c_str()));
if (name.empty() && !ssid.empty())
return ssid;
return name;
}
scoped_ptr<NetworkUIData> GetUIDataFromValue(const base::Value& ui_data_value) {
std::string ui_data_str;
if (!ui_data_value.GetAsString(&ui_data_str))
return scoped_ptr<NetworkUIData>();
if (ui_data_str.empty())
return make_scoped_ptr(new NetworkUIData());
scoped_ptr<base::DictionaryValue> ui_data_dict(
chromeos::onc::ReadDictionaryFromJson(ui_data_str));
if (!ui_data_dict)
return scoped_ptr<NetworkUIData>();
return make_scoped_ptr(new NetworkUIData(*ui_data_dict));
}
scoped_ptr<NetworkUIData> GetUIDataFromProperties(
const base::DictionaryValue& shill_dictionary) {
const base::Value* ui_data_value = NULL;
shill_dictionary.GetWithoutPathExpansion(flimflam::kUIDataProperty,
&ui_data_value);
if (!ui_data_value) {
VLOG(2) << "Dictionary has no UIData entry.";
return scoped_ptr<NetworkUIData>();
}
scoped_ptr<NetworkUIData> ui_data = GetUIDataFromValue(*ui_data_value);
if (!ui_data)
LOG(ERROR) << "UIData is not a valid JSON dictionary.";
return ui_data.Pass();
}
void SetUIData(const NetworkUIData& ui_data,
base::DictionaryValue* shill_dictionary) {
base::DictionaryValue ui_data_dict;
ui_data.FillDictionary(&ui_data_dict);
std::string ui_data_blob;
base::JSONWriter::Write(&ui_data_dict, &ui_data_blob);
shill_dictionary->SetStringWithoutPathExpansion(flimflam::kUIDataProperty,
ui_data_blob);
}
bool CopyIdentifyingProperties(const base::DictionaryValue& service_properties,
base::DictionaryValue* dest) {
CopyStringFromDictionary(service_properties, flimflam::kGuidProperty, dest);
std::string type;
service_properties.GetStringWithoutPathExpansion(flimflam::kTypeProperty,
&type);
dest->SetStringWithoutPathExpansion(flimflam::kTypeProperty, type);
if (type == flimflam::kTypeWifi) {
CopyStringFromDictionary(
service_properties, flimflam::kSecurityProperty, dest);
CopyStringFromDictionary(service_properties, flimflam::kSSIDProperty, dest);
CopyStringFromDictionary(service_properties, flimflam::kModeProperty, dest);
} else if (type == flimflam::kTypeVPN) {
CopyStringFromDictionary(service_properties, flimflam::kNameProperty, dest);
// VPN Provider values are read from the "Provider" dictionary, but written
// with the keys "Provider.Type" and "Provider.Host".
const base::DictionaryValue* provider_properties;
if (!service_properties.GetDictionaryWithoutPathExpansion(
flimflam::kProviderProperty, &provider_properties)) {
LOG(ERROR) << "Incomplete Shill dictionary missing VPN provider dict.";
return false;
}
std::string vpn_provider_type;
provider_properties->GetStringWithoutPathExpansion(flimflam::kTypeProperty,
&vpn_provider_type);
dest->SetStringWithoutPathExpansion(flimflam::kProviderTypeProperty,
vpn_provider_type);
std::string vpn_provider_host;
provider_properties->GetStringWithoutPathExpansion(flimflam::kHostProperty,
&vpn_provider_host);
dest->SetStringWithoutPathExpansion(flimflam::kProviderHostProperty,
vpn_provider_host);
} else if (type == flimflam::kTypeEthernet ||
type == shill::kTypeEthernetEap) {
// Ethernet and EthernetEAP don't have any additional identifying
// properties.
} else {
NOTREACHED() << "Unsupported network type " << type;
}
return true;
}
} // namespace shill_property_util
} // namespace chromeos
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_NETWORK_SHILL_PROPERTY_UTIL_H_
#define CHROMEOS_NETWORK_SHILL_PROPERTY_UTIL_H_
#include <string>
#include "base/memory/scoped_ptr.h"
#include "chromeos/chromeos_export.h"
namespace base {
class DictionaryValue;
class Value;
}
namespace chromeos {
class NetworkUIData;
namespace shill_property_util {
// Generates a name from properties."Wifi.HexSSID" if present, otherwise
// validates properties.Name and returns a valid utf8 version.
CHROMEOS_EXPORT std::string GetNameFromProperties(
const std::string& service_path,
const base::DictionaryValue& properties);
// Returns the UIData specified by |value|. Returns NULL if the value cannot be
// parsed.
scoped_ptr<NetworkUIData> GetUIDataFromValue(const base::Value& value);
// Returns the NetworkUIData parsed from the UIData property of
// |shill_dictionary|. If parsing fails or the field doesn't exist, returns
// NULL.
scoped_ptr<NetworkUIData> GetUIDataFromProperties(
const base::DictionaryValue& shill_dictionary);
// Sets the UIData property in |shill_dictionary| to the serialization of
// |ui_data|.
void SetUIData(const NetworkUIData& ui_data,
base::DictionaryValue* shill_dictionary);
// Copy configuration properties required by Shill to identify a network.
// Only WiFi, VPN, Ethernet and EthernetEAP are supported. WiMax and Cellular
// are not supported. Returns true only if all required properties could be
// copied.
bool CopyIdentifyingProperties(const base::DictionaryValue& service_properties,
base::DictionaryValue* dest);
} // namespace shill_property_util
} // namespace chromeos
#endif // CHROMEOS_NETWORK_SHILL_PROPERTY_UTIL_H_
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